Torque3D Documentation / _generateds / gfxD3D9Device.h

gfxD3D9Device.h

Engine/source/gfx/D3D9/gfxD3D9Device.h

More...

Classes:

Public Defines

define
D3DX_FUNCTION(fn_name, fn_return, fn_args)    typedef fn_return ( *D3DXFNPTR##fn_name)fn_args;
define
D3DX_FUNCTION(fn_name, fn_return, fn_args)    D3DXFNPTR##fn_name fn_name;
define
GFXD3DX() static_cast< *>()->smD3DX

Public Functions

D3D9Assert(HRESULT hr, const char * info)

Detailed Description

Public Defines

D3DX_FUNCTION(fn_name, fn_return, fn_args)    typedef fn_return ( *D3DXFNPTR##fn_name)fn_args;
D3DX_FUNCTION(fn_name, fn_return, fn_args)    D3DXFNPTR##fn_name fn_name;
GFXD3DX() static_cast< *>()->smD3DX

Public Functions

D3D9Assert(HRESULT hr, const char * info)

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _GFXD3D9DEVICE_H_
 25#define _GFXD3D9DEVICE_H_
 26
 27#include "platform/tmm_off.h"
 28
 29#ifdef TORQUE_OS_XENON
 30#  include "platformXbox/platformXbox.h"
 31#else
 32#  include <d3dx9.h>
 33#  include "platformWin32/platformWin32.h"
 34#endif
 35#ifndef _GFXD3D9STATEBLOCK_H_
 36#include "gfx/D3D9/gfxD3D9StateBlock.h"
 37#endif
 38#ifndef _GFXD3DTEXTUREMANAGER_H_
 39#include "gfx/D3D9/gfxD3D9TextureManager.h"
 40#endif
 41#ifndef _GFXD3D9CUBEMAP_H_
 42#include "gfx/D3D9/gfxD3D9Cubemap.h"
 43#endif
 44#ifndef _GFXD3D9PRIMITIVEBUFFER_H_
 45#include "gfx/D3D9/gfxD3D9PrimitiveBuffer.h"
 46#endif
 47#ifndef _GFXINIT_H_
 48#include "gfx/gfxInit.h"
 49#endif
 50#ifndef _PLATFORMDLIBRARY_H
 51#include "platform/platformDlibrary.h"
 52#endif
 53
 54#ifndef TORQUE_OS_XENON
 55#include <DxErr.h>
 56#else
 57#include <dxerr9.h>
 58#define DXGetErrorStringA DXGetErrorString9A
 59#define DXGetErrorDescriptionA DXGetErrorDescription9A
 60#endif
 61
 62#include "platform/tmm_on.h"
 63
 64inline void D3D9Assert( HRESULT hr, const char *info ) 
 65{
 66#if defined( TORQUE_DEBUG )
 67   if( FAILED( hr ) ) 
 68   {
 69      char buf[256];
 70      dSprintf( buf, 256, "%s\n%s\n%s", DXGetErrorStringA( hr ), DXGetErrorDescriptionA( hr ), info );
 71      AssertFatal( false, buf ); 
 72      //      DXTrace( __FILE__, __LINE__, hr, info, true );
 73   }
 74#endif
 75}
 76
 77
 78// Typedefs
 79#define D3DX_FUNCTION(fn_name, fn_return, fn_args) \
 80   typedef fn_return (WINAPI *D3DXFNPTR##fn_name)fn_args;
 81#include "gfx/D3D9/d3dx9Functions.h"
 82#undef D3DX_FUNCTION
 83
 84// Function table
 85struct D3DXFNTable
 86{
 87   D3DXFNTable() : isLoaded( false ){};
 88   bool isLoaded;
 89   DLibraryRef dllRef;
 90   DLibraryRef compilerDllRef;
 91#define D3DX_FUNCTION(fn_name, fn_return, fn_args) \
 92   D3DXFNPTR##fn_name fn_name;
 93#include "gfx/D3D9/d3dx9Functions.h"
 94#undef D3DX_FUNCTION
 95};
 96
 97#define GFXD3DX static_cast<GFXD3D9Device *>(GFX)->smD3DX 
 98
 99class GFXResource;
100class GFXD3D9ShaderConstBuffer;
101
102//------------------------------------------------------------------------------
103
104class GFXD3D9Device : public GFXDevice
105{
106   friend class GFXResource;
107   friend class GFXD3D9PrimitiveBuffer;
108   friend class GFXD3D9VertexBuffer;
109   friend class GFXD3D9TextureObject;
110   friend class GFXPCD3D9TextureTarget;
111   friend class GFXPCD3D9WindowTarget;
112
113   typedef GFXDevice Parent;
114
115protected:
116
117   MatrixF mTempMatrix;    ///< Temporary matrix, no assurances on value at all
118   RectI mClipRect;
119
120   typedef StrongRefPtr<GFXD3D9VertexBuffer> RPGDVB;
121   Vector<RPGDVB> mVolatileVBList;
122
123   class D3D9VertexDecl : public GFXVertexDecl
124   {
125   public:
126      virtual ~D3D9VertexDecl()
127      {
128         SAFE_RELEASE( decl );
129      }
130
131      IDirect3DVertexDeclaration9 *decl;
132   };
133
134   /// Used to lookup a vertex declaration for the vertex format.
135   /// @see allocVertexDecl
136   typedef Map<String,D3D9VertexDecl*> VertexDeclMap;
137   VertexDeclMap mVertexDecls;
138
139   IDirect3DSurface9 *mDeviceBackbuffer;
140   IDirect3DSurface9 *mDeviceDepthStencil;
141   IDirect3DSurface9 *mDeviceColor;
142
143   /// The stream 0 vertex buffer used for volatile VB offseting.
144   GFXD3D9VertexBuffer *mVolatileVB;
145
146   static void initD3DXFnTable();
147   //-----------------------------------------------------------------------
148   StrongRefPtr<GFXD3D9PrimitiveBuffer> mDynamicPB;                       ///< Dynamic index buffer
149   GFXD3D9PrimitiveBuffer *mCurrentPB;
150
151   IDirect3DVertexShader9 *mLastVertShader;
152   IDirect3DPixelShader9 *mLastPixShader;
153
154   GFXShaderRef mGenericShader[GS_COUNT];
155   GFXShaderConstBufferRef mGenericShaderBuffer[GS_COUNT];
156   GFXShaderConstHandle *mModelViewProjSC[GS_COUNT];
157
158   S32 mCreateFenceType;
159
160   LPDIRECT3D9       mD3D;        ///< D3D Handle
161   LPDIRECT3DDEVICE9 mD3DDevice;  ///< Handle for D3DDevice
162
163#if !defined(TORQUE_OS_XENON)
164   LPDIRECT3D9EX       mD3DEx;             ///< D3D9Ex Handle
165   LPDIRECT3DDEVICE9EX mD3DDeviceEx; ///< Handle for D3DDevice9Ex
166#endif
167
168   U32  mAdapterIndex;            ///< Adapter index because D3D supports multiple adapters
169
170   F32 mPixVersion;
171   U32 mNumSamplers;               ///< Profiled (via caps)
172   U32 mNumRenderTargets;          ///< Profiled (via caps)
173
174   D3DMULTISAMPLE_TYPE mMultisampleType;
175   DWORD mMultisampleLevel;
176
177   bool mOcclusionQuerySupported;
178
179   /// The current adapter display mode.
180   D3DDISPLAYMODE mDisplayMode;
181
182   /// To manage creating and re-creating of these when device is aquired
183   void reacquireDefaultPoolResources();
184
185   /// To release all resources we control from D3DPOOL_DEFAULT
186   void releaseDefaultPoolResources();
187
188   /// This you will probably never, ever use, but it is used to generate the code for
189   /// the initStates() function
190   void regenStates();
191
192   virtual GFXD3D9VertexBuffer* findVBPool( const GFXVertexFormat *vertexFormat, U32 numVertsNeeded );
193   virtual GFXD3D9VertexBuffer* createVBPool( const GFXVertexFormat *vertexFormat, U32 vertSize );
194
195#ifdef TORQUE_DEBUG
196   /// @name Debug Vertex Buffer information/management
197   /// @{
198
199   ///
200   U32 mNumAllocatedVertexBuffers; ///< To keep track of how many are allocated and freed
201   GFXD3D9VertexBuffer *mVBListHead;
202   void addVertexBuffer( GFXD3D9VertexBuffer *buffer );
203   void removeVertexBuffer( GFXD3D9VertexBuffer *buffer );
204   void logVertexBuffers();
205   /// @}
206#endif
207
208   // State overrides
209   // {
210
211   ///
212   virtual void setTextureInternal(U32 textureUnit, const GFXTextureObject* texture);
213
214   /// Called by GFXDevice to create a device specific stateblock
215   virtual GFXStateBlockRef createStateBlockInternal(const GFXStateBlockDesc& desc);
216   /// Called by GFXDevice to actually set a stateblock.
217   virtual void setStateBlockInternal(GFXStateBlock* block, bool force);
218
219   /// Track the last const buffer we've used.  Used to notify new constant buffers that
220   /// they should send all of their constants up
221   StrongRefPtr<GFXD3D9ShaderConstBuffer> mCurrentConstBuffer;
222   /// Called by base GFXDevice to actually set a const buffer
223   virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer);
224
225   // CodeReview - How exactly do we want to deal with this on the Xenon?
226   // Right now it's just in an #ifndef in gfxD3D9Device.cpp - AlexS 4/11/07
227   virtual void setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable);
228   virtual void setLightMaterialInternal(const GFXLightMaterial mat);
229   virtual void setGlobalAmbientInternal(ColorF color);
230
231   virtual void initStates()=0;
232   // }
233
234   // Index buffer management
235   // {
236   virtual void _setPrimitiveBuffer( GFXPrimitiveBuffer *buffer );
237   virtual void drawIndexedPrimitive(  GFXPrimitiveType primType, 
238                                       U32 startVertex, 
239                                       U32 minIndex, 
240                                       U32 numVerts, 
241                                       U32 startIndex, 
242                                       U32 primitiveCount );
243   // }
244
245   virtual GFXShader* createShader();
246   void disableShaders(bool force = false);
247
248   /// Device helper function
249   virtual D3DPRESENT_PARAMETERS setupPresentParams( const GFXVideoMode &mode, const HWND &hwnd ) const = 0;
250   
251public:
252   static D3DXFNTable smD3DX;
253
254   static GFXDevice *createInstance( U32 adapterIndex );
255
256   GFXTextureObject* createRenderSurface( U32 width, U32 height, GFXFormat format, U32 mipLevel );
257
258   const D3DDISPLAYMODE& getDisplayMode() const { return mDisplayMode; }
259
260   /// Constructor
261   /// @param   d3d   Direct3D object to instantiate this device with
262   /// @param   index   Adapter index since D3D can use multiple graphics adapters
263   GFXD3D9Device( LPDIRECT3D9 d3d, U32 index );
264   virtual ~GFXD3D9Device();
265
266   // Activate/deactivate
267   // {
268   virtual void init( const GFXVideoMode &mode, PlatformWindow *window = NULL ) = 0;
269
270   virtual void preDestroy() { Parent::preDestroy(); if(mTextureManager) mTextureManager->kill(); }
271
272   GFXAdapterType getAdapterType(){ return Direct3D9; }
273
274   U32 getAdaterIndex() const { return mAdapterIndex; }
275
276   virtual GFXCubemap *createCubemap();
277
278   virtual F32  getPixelShaderVersion() const { return mPixVersion; }
279   virtual void setPixelShaderVersion( F32 version ){ mPixVersion = version; }
280   virtual void setShader( GFXShader *shader, bool force = false );
281   virtual U32  getNumSamplers() const { return mNumSamplers; }
282   virtual U32  getNumRenderTargets() const { return mNumRenderTargets; }
283   // }
284
285   // Misc rendering control
286   // {
287   virtual void clear( U32 flags, ColorI color, F32 z, U32 stencil );
288   virtual bool beginSceneInternal();
289   virtual void endSceneInternal();
290
291   virtual void setClipRect( const RectI &rect );
292   virtual const RectI& getClipRect() const { return mClipRect; }
293
294   // }
295
296   /// @name Render Targets
297   /// @{
298   virtual void _updateRenderTargets();
299   /// @}
300
301   // Vertex/Index buffer management
302   // {
303   virtual GFXVertexBuffer* allocVertexBuffer(  U32 numVerts, 
304                                                const GFXVertexFormat *vertexFormat,
305                                                U32 vertSize,
306                                                GFXBufferType bufferType,
307                                                void* data = NULL );
308   virtual GFXPrimitiveBuffer *allocPrimitiveBuffer(  U32 numIndices, 
309                                                      U32 numPrimitives, 
310                                                      GFXBufferType bufferType,
311                                                      void* data = NULL );
312   virtual void deallocVertexBuffer( GFXD3D9VertexBuffer *vertBuff );
313   virtual GFXVertexDecl* allocVertexDecl( const GFXVertexFormat *vertexFormat );
314   virtual void setVertexDecl( const GFXVertexDecl *decl );
315
316   virtual void setVertexStream( U32 stream, GFXVertexBuffer *buffer );
317   virtual void setVertexStreamFrequency( U32 stream, U32 frequency );
318   // }
319
320   virtual U32 getMaxDynamicVerts() { return MAX_DYNAMIC_VERTS; }
321   virtual U32 getMaxDynamicIndices() { return MAX_DYNAMIC_INDICES; }
322
323   // Rendering
324   // {
325   virtual void drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount );
326   // }
327
328   virtual LPDIRECT3DDEVICE9 getDevice(){ return mD3DDevice; }
329   virtual LPDIRECT3D9 getD3D() { return mD3D; }
330
331   /// Reset
332   virtual void reset( D3DPRESENT_PARAMETERS &d3dpp ) = 0;
333
334   virtual void setupGenericShaders( GenericShaderType type  = GSColor );
335
336   // Function only really used on the, however a centralized function for
337   // destroying resources is probably a good thing -patw
338   virtual void destroyD3DResource( IDirect3DResource9 *d3dResource ) { SAFE_RELEASE( d3dResource ); }; 
339
340   inline virtual F32 getFillConventionOffset() const { return 0.5f; }
341   virtual void doParanoidStateCheck();
342
343   GFXFence *createFence();
344
345   GFXOcclusionQuery* createOcclusionQuery();   
346
347   // Default multisample parameters
348   D3DMULTISAMPLE_TYPE getMultisampleType() const { return mMultisampleType; }
349   DWORD getMultisampleLevel() const { return mMultisampleLevel; } 
350
351   // Whether or not the Direct3D device was created with Direct3D9Ex support
352#if !defined(TORQUE_OS_XENON)
353   virtual bool isD3D9Ex() { return mD3DEx != NULL; }
354#else
355   virtual bool isD3D9Ex() { return false; }
356#endif
357
358   // Get the backbuffer, currently only access for WPF support
359   virtual IDirect3DSurface9* getBackBuffer() { return mDeviceBackbuffer; }
360
361};
362
363
364#endif // _GFXD3D9DEVICE_H_
365