00001 #ifndef N_D3D9SERVER_H
00002 #define N_D3D9SERVER_H
00003
00012 #include "gfx2/ngfxserver2.h"
00013 #include "input/ninputserver.h"
00014 #include "misc/nwatched.h"
00015 #include "gfx2/nd3d9windowhandler.h"
00016
00017 #ifndef WIN32_LEAN_AND_MEAN
00018 #define WIN32_LEAN_AND_MEAN
00019 #endif
00020
00021 #ifdef _DEBUG
00022 #define D3D_DEBUG_INFO
00023 #endif
00024
00025 #include <d3d9.h>
00026 #include <d3dx9.h>
00027 #include <dxerr9.h>
00028
00029 #if D3D_SDK_VERSION < 32
00030 #error You must be using the DirectX 9 October 2004 Update SDK! You may download it from http://www.microsoft.com/downloads/details.aspx?FamilyID=b7bc31fa-2df1-44fd-95a4-c2555446aed4&DisplayLang=en
00031 #endif
00032
00033
00034
00035
00036 #define N_D3D9_USENVPERFHUD (0)
00037
00038 #define N_D3D9_DEBUG (0)
00039
00040 #if N_D3D9_USENVPERFHUD
00041 #define N_D3D9_DEVICETYPE D3DDEVTYPE_REF
00042 #define N_D3D9_ADAPTER (nD3D9Server::Instance()->d3d9->GetAdapterCount() - 1)
00043 #else
00044 #define N_D3D9_DEVICETYPE D3DDEVTYPE_HAL
00045 #define N_D3D9_ADAPTER D3DADAPTER_DEFAULT
00046 #endif
00047
00048 #define N_D3D9_FORCEMIXEDVERTEXPROCESSING (0)
00049 #define N_D3D9_FORCESOFTWAREVERTEXPROCESSING (0)
00050
00051
00052 class nD3D9Server : public nGfxServer2
00053 {
00054 public:
00056 nD3D9Server();
00058 virtual ~nD3D9Server();
00060 static nD3D9Server* Instance();
00061
00063 virtual nMesh2* NewMesh(const nString& rsrcName);
00065 virtual nMeshArray* NewMeshArray(const nString& rsrcName);
00067 virtual nTexture2* NewTexture(const nString& rsrcName);
00069 virtual nShader2* NewShader(const nString& rsrcName);
00071 virtual nFont2* NewFont(const nString& rsrcName, const nFontDesc& fontDesc);
00073 virtual nTexture2* NewRenderTarget(const nString& rsrcName, int width, int height, nTexture2::Format fmt, int usageFlags);
00075 virtual nOcclusionQuery* NewOcclusionQuery();
00076
00078 virtual void SetDisplayMode(const nDisplayMode2& mode);
00080 virtual const nDisplayMode2& GetDisplayMode() const;
00082 virtual void SetWindowTitle(const char* title);
00084 virtual void SetCamera(nCamera2& cam);
00086 virtual void SetViewport(nViewport& vp);
00088 virtual bool OpenDisplay();
00090 virtual void CloseDisplay();
00092 virtual FeatureSet GetFeatureSet();
00094 virtual bool AreVertexShadersEmulated();
00096 virtual HWND GetAppHwnd() const;
00098 virtual HWND GetParentHwnd() const;
00100 virtual int GetNumStencilBits() const;
00102 virtual int GetNumDepthBits() const;
00104 virtual void SetScissorRect(const rectangle& r);
00106 virtual void SetClipPlanes(const nArray<plane>& planes);
00107
00109 virtual void SetRenderTarget(int index, nTexture2* t);
00110
00112 virtual bool BeginFrame();
00114 virtual bool BeginScene();
00116 virtual void EndScene();
00118 virtual void PresentScene();
00120 virtual void EndFrame();
00122 virtual void Clear(int bufferTypes, float red, float green, float blue, float alpha, float z, int stencil);
00123
00125 virtual void ClearLights();
00127 virtual void ClearLight(int index);
00129 virtual int AddLight(const nLight& light);
00131 virtual void SetMesh(nMesh2* vbMesh, nMesh2* ibMesh);
00133 virtual void SetMeshArray(nMeshArray* meshArray);
00135 virtual void SetShader(nShader2* shader);
00137 virtual void SetTransform(TransformType type, const matrix44& matrix);
00139 virtual void DrawIndexed(PrimitiveType primType);
00141 virtual void Draw(PrimitiveType primType);
00143 virtual void DrawIndexedNS(PrimitiveType primType);
00145 virtual void DrawNS(PrimitiveType primType);
00146
00148 virtual bool Trigger();
00149
00151 virtual void DrawText(const nString& text, const vector4& color, const rectangle& rect, uint flags, bool immediate = true);
00153 virtual vector2 GetTextExtent(const nString& text);
00155 virtual void DrawTextBuffer();
00156
00158 virtual void EnterDialogBoxMode();
00160 virtual void LeaveDialogBoxMode();
00161
00163 virtual bool SaveScreenshot(const char* fileName, nTexture2::FileFormat fileFormat);
00164
00166 virtual void BeginLines();
00168 virtual void DrawLines3d(const vector3* vertexList, int numVertices, const vector4& color);
00170 virtual void DrawLines2d(const vector2* vertexList, int numVertices, const vector4& color);
00172 virtual void EndLines();
00173
00175 virtual void BeginShapes();
00177 virtual void DrawShape(ShapeType type, const matrix44& model, const vector4& color);
00179 virtual void DrawShapeNS(ShapeType type, const matrix44& model);
00181 virtual void DrawShapePrimitives(PrimitiveType type, int numPrimitives, const vector3* vertexList, int vertexWidth, const matrix44& model, const vector4& color);
00183 virtual void DrawShapeIndexedPrimitives(PrimitiveType type, int numPrimitives, const vector3* vertexList, int numVertices, int vertexWidth, void* indices, IndexType indexType, const matrix44& model, const vector4& color);
00185 virtual void EndShapes();
00186
00188 IDirect3DDevice9* GetD3DDevice() const;
00190 IDirect3D9* GetD3D() const;
00192 virtual void AdjustGamma();
00194 virtual void RestoreGamma();
00196 virtual void SetSkipMsgLoop(bool skip);
00197
00198 private:
00200 void OpenTextRenderer();
00202 void CloseTextRenderer();
00204 bool TestResetDevice();
00206 bool CheckDepthFormat(D3DFORMAT adapterFormat, D3DFORMAT backbufferFormat, D3DFORMAT depthFormat);
00208 void FindBufferFormats(nDisplayMode2::Bpp bpp, D3DFORMAT& dispFormat, D3DFORMAT& backFormat, D3DFORMAT& zbufFormat);
00210 void D3dOpen();
00212 void D3dClose();
00214 bool DeviceOpen();
00216 void DeviceClose();
00218 void OnDeviceCleanup(bool shutdown);
00220 void OnDeviceInit(bool startup);
00222 void InitDeviceState();
00224 void UpdateFeatureSet();
00225 #ifdef __NEBULA_STATS__
00226
00227 void QueryStatistics();
00228 #endif
00229
00230 int GetD3DPrimTypeAndNumIndexed(PrimitiveType primType, D3DPRIMITIVETYPE& d3dPrimType) const;
00232 int GetD3DPrimTypeAndNum(PrimitiveType primType, D3DPRIMITIVETYPE& d3dPrimType) const;
00234 void UpdateCursor();
00236 ID3DXEffectPool* GetEffectPool() const;
00238 void DrawIndexedInstancedNS(PrimitiveType primType);
00240 void DrawInstancedNS(PrimitiveType primType);
00242 void UpdatePerFrameSharedShaderParams();
00244 void UpdatePerSceneSharedShaderParams();
00246 int GetD3DFormatNumBits(D3DFORMAT fmt);
00248 void SetSoftwareVertexProcessing(bool b);
00250 bool GetSoftwareVertexProcessing();
00252 void InitDeviceIdentifier();
00254 void UpdateScissorRect();
00256 vector2 GetCurrentRenderTargetSize() const;
00258 D3DMULTISAMPLE_TYPE CheckMultiSampleType(D3DFORMAT backbufferFormat, D3DFORMAT depthFormat, bool windowed);
00260 void DrawTextImmediate(nFont2* font, const nString& text, const vector4& color, const rectangle& rect, uint flags);
00262 void CreateDisplayModeEnvVars();
00263
00264 friend class nD3D9Mesh;
00265 friend class nD3D9Texture;
00266 friend class nD3D9Shader;
00267
00268 static nD3D9Server* Singleton;
00269
00270 nD3D9WindowHandler windowHandler;
00271
00272 DWORD deviceBehaviourFlags;
00273 D3DCAPS9 devCaps;
00274 D3DDISPLAYMODE d3dDisplayMode;
00275 FeatureSet featureSet;
00276
00277 class TextElement
00278 {
00279 public:
00281 TextElement();
00283 TextElement(nFont2* f, const nString& t, const vector4& c, const rectangle& r, uint flg);
00284
00285 nRef<nFont2> refFont;
00286 nString text;
00287 vector4 color;
00288 rectangle rect;
00289 uint flags;
00290 };
00291 nArray<TextElement> textElements;
00292 ID3DXSprite* d3dSprite;
00293 nRef<nFont2> refDefaultFont;
00294
00295 ID3DXLine* d3dxLine;
00296 ID3DXMesh* shapeMeshes[NumShapeTypes];
00297 nRef<nD3D9Shader> refShapeShader;
00298
00299 D3DPRESENT_PARAMETERS presentParams;
00300 IDirect3DSurface9* backBufferSurface;
00301 IDirect3DSurface9* depthStencilSurface;
00302 IDirect3DSurface9* captureSurface;
00303 ID3DXEffectPool* effectPool;
00304 nRef<nD3D9Shader> refSharedShader;
00305
00306 #ifdef __NEBULA_STATS__
00307 IDirect3DQuery9* queryResourceManager;
00308 nTime timeStamp;
00309
00310 WATCHER_DECLARE(watchNumPrimitives);
00311 WATCHER_DECLARE(watchFPS);
00312 WATCHER_DECLARE(watchNumDrawCalls);
00313 WATCHER_DECLARE(watchNumRenderStateChanges);
00314
00315 int statsFrameCount;
00316 int statsNumTextureChanges;
00317 int statsNumRenderStateChanges;
00318 int statsNumDrawCalls;
00319 int statsNumPrimitives;
00320 #endif
00321
00322 public:
00323
00324 IDirect3DDevice9* d3d9Device;
00325 IDirect3D9* d3d9;
00326 };
00327
00328
00331 inline
00332 nD3D9Server*
00333 nD3D9Server::Instance()
00334 {
00335 n_assert(0 != Singleton);
00336 return Singleton;
00337 }
00338
00339
00342 inline
00343 nD3D9Server::TextElement::TextElement() :
00344 flags(0)
00345 {
00346
00347 }
00348
00349
00352 inline
00353 nD3D9Server::TextElement::TextElement(nFont2* f, const nString& t, const vector4& c, const rectangle& r, uint flg) :
00354 refFont(f),
00355 text(t),
00356 color(c),
00357 rect(r),
00358 flags(flg)
00359 {
00360
00361 }
00362
00363
00366 inline
00367 int
00368 nD3D9Server::GetD3DPrimTypeAndNumIndexed(PrimitiveType primType, D3DPRIMITIVETYPE& d3dPrimType) const
00369 {
00370 int d3dNumPrimitives = 0;
00371 switch (primType)
00372 {
00373 case PointList:
00374 d3dPrimType = D3DPT_POINTLIST;
00375 d3dNumPrimitives = this->indexRangeNum;
00376 break;
00377
00378 case LineList:
00379 d3dPrimType = D3DPT_LINELIST;
00380 d3dNumPrimitives = this->indexRangeNum / 2;
00381 break;
00382
00383 case LineStrip:
00384 d3dPrimType = D3DPT_LINESTRIP;
00385 d3dNumPrimitives = this->indexRangeNum - 1;
00386 break;
00387
00388 case TriangleList:
00389 d3dPrimType = D3DPT_TRIANGLELIST;
00390 d3dNumPrimitives = this->indexRangeNum / 3;
00391 break;
00392
00393 case TriangleStrip:
00394 d3dPrimType = D3DPT_TRIANGLESTRIP;
00395 d3dNumPrimitives = this->indexRangeNum - 2;
00396 break;
00397
00398 case TriangleFan:
00399 d3dPrimType = D3DPT_TRIANGLEFAN;
00400 d3dNumPrimitives = this->indexRangeNum - 2;
00401 break;
00402 }
00403 return d3dNumPrimitives;
00404 }
00405
00406
00409 inline
00410 int
00411 nD3D9Server::GetD3DPrimTypeAndNum(PrimitiveType primType, D3DPRIMITIVETYPE& d3dPrimType) const
00412 {
00413 int d3dNumPrimitives = 0;
00414 switch (primType)
00415 {
00416 case PointList:
00417 d3dPrimType = D3DPT_POINTLIST;
00418 d3dNumPrimitives = this->vertexRangeNum;
00419 break;
00420
00421 case LineList:
00422 d3dPrimType = D3DPT_LINELIST;
00423 d3dNumPrimitives = this->vertexRangeNum / 2;
00424 break;
00425
00426 case LineStrip:
00427 d3dPrimType = D3DPT_LINESTRIP;
00428 d3dNumPrimitives = this->vertexRangeNum - 1;
00429 break;
00430
00431 case TriangleList:
00432 d3dPrimType = D3DPT_TRIANGLELIST;
00433 d3dNumPrimitives = this->vertexRangeNum / 3;
00434 break;
00435
00436 case TriangleStrip:
00437 d3dPrimType = D3DPT_TRIANGLESTRIP;
00438 d3dNumPrimitives = this->vertexRangeNum - 2;
00439 break;
00440
00441 case TriangleFan:
00442 d3dPrimType = D3DPT_TRIANGLEFAN;
00443 d3dNumPrimitives = this->vertexRangeNum - 2;
00444 break;
00445 }
00446 return d3dNumPrimitives;
00447 }
00448
00449
00452 inline
00453 HWND
00454 nD3D9Server::GetAppHwnd() const
00455 {
00456 return this->windowHandler.GetAppHwnd();
00457 }
00458
00459
00462 inline
00463 HWND
00464 nD3D9Server::GetParentHwnd() const
00465 {
00466 return this->windowHandler.GetParentHwnd();
00467 }
00468
00469
00472 inline
00473 ID3DXEffectPool*
00474 nD3D9Server::GetEffectPool() const
00475 {
00476 return this->effectPool;
00477 }
00478
00479
00482 inline
00483 IDirect3DDevice9*
00484 nD3D9Server::GetD3DDevice() const
00485 {
00486 return this->d3d9Device;
00487 }
00488
00489
00492 inline
00493 IDirect3D9*
00494 nD3D9Server::GetD3D() const
00495 {
00496 return this->d3d9;
00497 }
00498
00499
00500 #endif