Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1444 lines
54 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. // TOGL CODE LICENSE
  3. //
  4. // Copyright 2011-2014 Valve Corporation
  5. // All Rights Reserved.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. // dxabstract.h
  26. //
  27. //==================================================================================================
  28. #ifndef DXABSTRACT_H
  29. #define DXABSTRACT_H
  30. #ifdef DX_TO_GL_ABSTRACTION
  31. #include "togl/rendermechanism.h"
  32. #include "tier0/platform.h"
  33. #include "tier0/dbg.h"
  34. #include "tier1/utlmap.h"
  35. // turn this on to get refcount logging from IUnknown
  36. #define IUNKNOWN_ALLOC_SPEW 0
  37. #define IUNKNOWN_ALLOC_SPEW_MARK_ALL 0
  38. TOGL_INTERFACE void toglGetClientRect( VD3DHWND hWnd, RECT *destRect );
  39. struct TOGL_CLASS IUnknown
  40. {
  41. int m_refcount[2];
  42. bool m_mark;
  43. IUnknown()
  44. {
  45. m_refcount[0] = 1;
  46. m_refcount[1] = 0;
  47. m_mark = (IUNKNOWN_ALLOC_SPEW_MARK_ALL != 0); // either all are marked, or only the ones that have SetMark(true) called on them
  48. #if IUNKNOWN_ALLOC_SPEW
  49. if (m_mark)
  50. {
  51. GLMPRINTF(("-A- IUnew (%08x) refc -> (%d,%d) ",this,m_refcount[0],m_refcount[1]));
  52. }
  53. #endif
  54. };
  55. virtual ~IUnknown()
  56. {
  57. #if IUNKNOWN_ALLOC_SPEW
  58. if (m_mark)
  59. {
  60. GLMPRINTF(("-A- IUdel (%08x) ",this ));
  61. }
  62. #endif
  63. };
  64. void AddRef( int which=0, char *comment = NULL )
  65. {
  66. Assert( which >= 0 );
  67. Assert( which < 2 );
  68. m_refcount[which]++;
  69. #if IUNKNOWN_ALLOC_SPEW
  70. if (m_mark)
  71. {
  72. GLMPRINTF(("-A- IUAddRef (%08x,%d) refc -> (%d,%d) [%s]",this,which,m_refcount[0],m_refcount[1],comment?comment:"...")) ;
  73. if (!comment)
  74. {
  75. GLMPRINTF(("")) ; // place to hang a breakpoint
  76. }
  77. }
  78. #endif
  79. };
  80. ULONG __stdcall Release( int which=0, char *comment = NULL )
  81. {
  82. Assert( which >= 0 );
  83. Assert( which < 2 );
  84. //int oldrefcs[2] = { m_refcount[0], m_refcount[1] };
  85. bool deleting = false;
  86. m_refcount[which]--;
  87. if ( (!m_refcount[0]) && (!m_refcount[1]) )
  88. {
  89. deleting = true;
  90. }
  91. #if IUNKNOWN_ALLOC_SPEW
  92. if (m_mark)
  93. {
  94. GLMPRINTF(("-A- IURelease (%08x,%d) refc -> (%d,%d) [%s] %s",this,which,m_refcount[0],m_refcount[1],comment?comment:"...",deleting?"->DELETING":""));
  95. if (!comment)
  96. {
  97. GLMPRINTF(("")) ; // place to hang a breakpoint
  98. }
  99. }
  100. #endif
  101. if (deleting)
  102. {
  103. if (m_mark)
  104. {
  105. GLMPRINTF(("")) ; // place to hang a breakpoint
  106. }
  107. delete this;
  108. return 0;
  109. }
  110. else
  111. {
  112. return m_refcount[0];
  113. }
  114. };
  115. void SetMark( bool markValue, char *comment=NULL )
  116. {
  117. #if IUNKNOWN_ALLOC_SPEW
  118. if (!m_mark && markValue) // leading edge detect
  119. {
  120. // print the same thing that the constructor would have printed if it had been marked from the beginning
  121. // i.e. it's anticipated that callers asking for marking will do so right at create time
  122. GLMPRINTF(("-A- IUSetMark (%08x) refc -> (%d,%d) (%s) ",this,m_refcount[0],m_refcount[1],comment?comment:"..."));
  123. }
  124. #endif
  125. m_mark = markValue;
  126. }
  127. };
  128. // ------------------------------------------------------------------------------------------------------------------------------ //
  129. // INTERFACES
  130. // ------------------------------------------------------------------------------------------------------------------------------ //
  131. struct TOGL_CLASS IDirect3DResource9 : public IUnknown
  132. {
  133. IDirect3DDevice9 *m_device; // parent device
  134. D3DRESOURCETYPE m_restype;
  135. DWORD SetPriority(DWORD PriorityNew);
  136. };
  137. struct TOGL_CLASS IDirect3DBaseTexture9 : public IDirect3DResource9 // "A Texture.."
  138. {
  139. D3DSURFACE_DESC m_descZero; // desc of top level.
  140. CGLMTex *m_tex; // a CGLMTex can represent all forms of tex
  141. virtual ~IDirect3DBaseTexture9();
  142. D3DRESOURCETYPE TOGLMETHODCALLTYPE GetType();
  143. DWORD TOGLMETHODCALLTYPE GetLevelCount();
  144. HRESULT TOGLMETHODCALLTYPE GetLevelDesc(UINT Level,D3DSURFACE_DESC *pDesc);
  145. };
  146. struct TOGL_CLASS IDirect3DTexture9 : public IDirect3DBaseTexture9 // "Texture 2D"
  147. {
  148. IDirect3DSurface9 *m_surfZero; // surf of top level.
  149. virtual ~IDirect3DTexture9();
  150. HRESULT TOGLMETHODCALLTYPE LockRect(UINT Level,D3DLOCKED_RECT* pLockedRect,CONST RECT* pRect,DWORD Flags);
  151. HRESULT TOGLMETHODCALLTYPE UnlockRect(UINT Level);
  152. HRESULT TOGLMETHODCALLTYPE GetSurfaceLevel(UINT Level,IDirect3DSurface9** ppSurfaceLevel);
  153. };
  154. struct TOGL_CLASS IDirect3DCubeTexture9 : public IDirect3DBaseTexture9 // "Texture Cube Map"
  155. {
  156. IDirect3DSurface9 *m_surfZero[6]; // surfs of top level.
  157. virtual ~IDirect3DCubeTexture9();
  158. HRESULT TOGLMETHODCALLTYPE GetCubeMapSurface(D3DCUBEMAP_FACES FaceType,UINT Level,IDirect3DSurface9** ppCubeMapSurface);
  159. HRESULT TOGLMETHODCALLTYPE GetLevelDesc(UINT Level,D3DSURFACE_DESC *pDesc);
  160. };
  161. struct TOGL_CLASS IDirect3DVolumeTexture9 : public IDirect3DBaseTexture9 // "Texture 3D"
  162. {
  163. IDirect3DSurface9 *m_surfZero; // surf of top level.
  164. D3DVOLUME_DESC m_volDescZero; // volume desc top level
  165. virtual ~IDirect3DVolumeTexture9();
  166. HRESULT TOGLMETHODCALLTYPE LockBox(UINT Level,D3DLOCKED_BOX* pLockedVolume,CONST D3DBOX* pBox,DWORD Flags);
  167. HRESULT TOGLMETHODCALLTYPE UnlockBox(UINT Level);
  168. HRESULT TOGLMETHODCALLTYPE GetLevelDesc( UINT level, D3DVOLUME_DESC *pDesc );
  169. };
  170. // for the moment, a "D3D surface" is modeled as a GLM tex, a face, and a mip.
  171. // no Create method, these are filled in by the various create surface methods.
  172. struct TOGL_CLASS IDirect3DSurface9 : public IDirect3DResource9
  173. {
  174. virtual ~IDirect3DSurface9();
  175. HRESULT TOGLMETHODCALLTYPE LockRect(D3DLOCKED_RECT* pLockedRect,CONST RECT* pRect,DWORD Flags);
  176. HRESULT TOGLMETHODCALLTYPE UnlockRect();
  177. HRESULT TOGLMETHODCALLTYPE GetDesc(D3DSURFACE_DESC *pDesc);
  178. D3DSURFACE_DESC m_desc;
  179. CGLMTex *m_tex;
  180. int m_face;
  181. int m_mip;
  182. };
  183. struct TOGL_CLASS IDirect3D9 : public IUnknown
  184. {
  185. virtual ~IDirect3D9();
  186. UINT TOGLMETHODCALLTYPE GetAdapterCount();
  187. HRESULT TOGLMETHODCALLTYPE GetDeviceCaps (UINT Adapter,D3DDEVTYPE DeviceType,D3DCAPS9* pCaps);
  188. HRESULT TOGLMETHODCALLTYPE GetAdapterIdentifier (UINT Adapter,DWORD Flags,D3DADAPTER_IDENTIFIER9* pIdentifier);
  189. HRESULT TOGLMETHODCALLTYPE CheckDeviceFormat (UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,DWORD Usage,D3DRESOURCETYPE RType,D3DFORMAT CheckFormat);
  190. UINT TOGLMETHODCALLTYPE GetAdapterModeCount (UINT Adapter,D3DFORMAT Format);
  191. HRESULT TOGLMETHODCALLTYPE EnumAdapterModes (UINT Adapter,D3DFORMAT Format,UINT Mode,D3DDISPLAYMODE* pMode);
  192. HRESULT TOGLMETHODCALLTYPE CheckDeviceType (UINT Adapter,D3DDEVTYPE DevType,D3DFORMAT AdapterFormat,D3DFORMAT BackBufferFormat,BOOL bWindowed);
  193. HRESULT TOGLMETHODCALLTYPE GetAdapterDisplayMode (UINT Adapter,D3DDISPLAYMODE* pMode);
  194. HRESULT TOGLMETHODCALLTYPE CheckDepthStencilMatch (UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,D3DFORMAT RenderTargetFormat,D3DFORMAT DepthStencilFormat);
  195. HRESULT TOGLMETHODCALLTYPE CheckDeviceMultiSampleType (UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SurfaceFormat,BOOL Windowed,D3DMULTISAMPLE_TYPE MultiSampleType,DWORD* pQualityLevels);
  196. HRESULT TOGLMETHODCALLTYPE CreateDevice (UINT Adapter,D3DDEVTYPE DeviceType,VD3DHWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DDevice9** ppReturnedDeviceInterface);
  197. };
  198. struct TOGL_CLASS IDirect3DVertexDeclaration9 : public IUnknown
  199. {
  200. IDirect3DDevice9 *m_device;
  201. uint m_elemCount;
  202. D3DVERTEXELEMENT9_GL m_elements[ MAX_D3DVERTEXELEMENTS ];
  203. uint8 m_VertexAttribDescToStreamIndex[256];
  204. virtual ~IDirect3DVertexDeclaration9();
  205. };
  206. struct TOGL_CLASS IDirect3DQuery9 : public IDirect3DResource9 //was IUnknown
  207. {
  208. D3DQUERYTYPE m_type; // D3DQUERYTYPE_OCCLUSION or D3DQUERYTYPE_EVENT
  209. GLMContext *m_ctx;
  210. CGLMQuery *m_query;
  211. uint m_nIssueStartThreadID, m_nIssueEndThreadID;
  212. uint m_nIssueStartDrawCallIndex, m_nIssueEndDrawCallIndex;
  213. uint m_nIssueStartFrameIndex, m_nIssueEndFrameIndex;
  214. uint m_nIssueStartQueryCreationCounter, m_nIssueEndQueryCreationCounter;
  215. virtual ~IDirect3DQuery9();
  216. HRESULT Issue(DWORD dwIssueFlags);
  217. HRESULT GetData(void* pData,DWORD dwSize,DWORD dwGetDataFlags);
  218. };
  219. struct TOGL_CLASS IDirect3DVertexBuffer9 : public IDirect3DResource9 //was IUnknown
  220. {
  221. GLMContext *m_ctx;
  222. CGLMBuffer *m_vtxBuffer;
  223. D3DVERTEXBUFFER_DESC m_vtxDesc; // to satisfy GetDesc
  224. virtual ~IDirect3DVertexBuffer9();
  225. HRESULT Lock(UINT OffsetToLock,UINT SizeToLock,void** ppbData,DWORD Flags);
  226. HRESULT Unlock();
  227. void UnlockActualSize( uint nActualSize, const void *pActualData = NULL );
  228. };
  229. struct TOGL_CLASS IDirect3DIndexBuffer9 : public IDirect3DResource9 //was IUnknown
  230. {
  231. GLMContext *m_ctx;
  232. CGLMBuffer *m_idxBuffer;
  233. D3DINDEXBUFFER_DESC m_idxDesc; // to satisfy GetDesc
  234. virtual ~IDirect3DIndexBuffer9();
  235. HRESULT Lock(UINT OffsetToLock,UINT SizeToLock,void** ppbData,DWORD Flags);
  236. HRESULT Unlock();
  237. void UnlockActualSize( uint nActualSize, const void *pActualData = NULL );
  238. HRESULT GetDesc(D3DINDEXBUFFER_DESC *pDesc);
  239. };
  240. struct TOGL_CLASS IDirect3DPixelShader9 : public IDirect3DResource9 //was IUnknown
  241. {
  242. CGLMProgram *m_pixProgram;
  243. uint m_pixHighWater; // count of active constant slots referenced by shader.
  244. uint m_pixSamplerMask; // (1<<n) mask of samplers referemnced by this pixel shader
  245. // this can help FlushSamplers avoid SRGB flipping on textures not being referenced...
  246. uint m_pixSamplerTypes; // SAMPLER_TYPE_2D, etc.
  247. uint m_pixFragDataMask; // (1<<n) mask of gl_FragData[n] referenced by this pixel shader
  248. virtual ~IDirect3DPixelShader9();
  249. };
  250. struct TOGL_CLASS IDirect3DVertexShader9 : public IDirect3DResource9 //was IUnknown
  251. {
  252. CGLMProgram *m_vtxProgram;
  253. uint m_vtxHighWater; // count of active constant slots referenced by shader.
  254. uint m_vtxHighWaterBone;
  255. unsigned char m_vtxAttribMap[16]; // high nibble is usage, low nibble is usageindex, array position is attrib number
  256. uint m_maxVertexAttrs;
  257. virtual ~IDirect3DVertexShader9();
  258. };
  259. #ifdef _MSC_VER
  260. typedef class TOGL_CLASS CUtlMemory<D3DMATRIX> CD3DMATRIXAllocator;
  261. typedef class TOGL_CLASS CUtlVector<D3DMATRIX, CD3DMATRIXAllocator> CD3DMATRIXStack;
  262. #else
  263. typedef class CUtlMemory<D3DMATRIX> CD3DMATRIXAllocator;
  264. typedef class CUtlVector<D3DMATRIX, CD3DMATRIXAllocator> CD3DMATRIXStack;
  265. #endif
  266. struct TOGL_CLASS ID3DXMatrixStack //: public IUnknown
  267. {
  268. int m_refcount[2];
  269. bool m_mark;
  270. CD3DMATRIXStack m_stack;
  271. int m_stackTop; // top of stack is at the highest index, this is that index. push increases, pop decreases.
  272. ID3DXMatrixStack();
  273. void AddRef( int which=0, char *comment = NULL );
  274. ULONG Release( int which=0, char *comment = NULL );
  275. HRESULT Create( void );
  276. D3DXMATRIX* GetTop();
  277. void Push();
  278. void Pop();
  279. void LoadIdentity();
  280. void LoadMatrix( const D3DXMATRIX *pMat );
  281. void MultMatrix( const D3DXMATRIX *pMat );
  282. void MultMatrixLocal( const D3DXMATRIX *pMat );
  283. HRESULT ScaleLocal(FLOAT x, FLOAT y, FLOAT z);
  284. // Left multiply the current matrix with the computed rotation
  285. // matrix, counterclockwise about the given axis with the given angle.
  286. // (rotation is about the local origin of the object)
  287. HRESULT RotateAxisLocal(CONST D3DXVECTOR3* pV, FLOAT Angle);
  288. // Left multiply the current matrix with the computed translation
  289. // matrix. (transformation is about the local origin of the object)
  290. HRESULT TranslateLocal(FLOAT x, FLOAT y, FLOAT z);
  291. };
  292. typedef ID3DXMatrixStack* LPD3DXMATRIXSTACK;
  293. struct RenderTargetState_t
  294. {
  295. void clear() { V_memset( this, 0, sizeof( *this ) ); }
  296. CGLMTex *m_pRenderTargets[4];
  297. CGLMTex *m_pDepthStencil;
  298. inline bool RefersTo( CGLMTex * pSurf ) const
  299. {
  300. for ( uint i = 0; i < 4; i++ )
  301. if ( m_pRenderTargets[i] == pSurf )
  302. return true;
  303. if ( m_pDepthStencil == pSurf )
  304. return true;
  305. return false;
  306. }
  307. static inline bool LessFunc( const RenderTargetState_t &lhs, const RenderTargetState_t &rhs )
  308. {
  309. COMPILE_TIME_ASSERT( sizeof( lhs.m_pRenderTargets[0] ) == sizeof( uint32 ) );
  310. uint64 lhs0 = reinterpret_cast<const uint64 *>(lhs.m_pRenderTargets)[0];
  311. uint64 rhs0 = reinterpret_cast<const uint64 *>(rhs.m_pRenderTargets)[0];
  312. if ( lhs0 < rhs0 )
  313. return true;
  314. else if ( lhs0 == rhs0 )
  315. {
  316. uint64 lhs1 = reinterpret_cast<const uint64 *>(lhs.m_pRenderTargets)[1];
  317. uint64 rhs1 = reinterpret_cast<const uint64 *>(rhs.m_pRenderTargets)[1];
  318. if ( lhs1 < rhs1 )
  319. return true;
  320. else if ( lhs1 == rhs1 )
  321. {
  322. return lhs.m_pDepthStencil < rhs.m_pDepthStencil;
  323. }
  324. }
  325. return false;
  326. }
  327. inline bool operator < ( const RenderTargetState_t &rhs ) const
  328. {
  329. return LessFunc( *this, rhs );
  330. }
  331. };
  332. typedef CUtlMap< RenderTargetState_t, CGLMFBO *> CGLMFBOMap;
  333. class simple_bitmap;
  334. struct TOGL_CLASS IDirect3DDevice9 : public IUnknown
  335. {
  336. friend class GLMContext;
  337. friend struct IDirect3DBaseTexture9;
  338. friend struct IDirect3DTexture9;
  339. friend struct IDirect3DCubeTexture9;
  340. friend struct IDirect3DVolumeTexture9;
  341. friend struct IDirect3DSurface9;
  342. friend struct IDirect3DVertexBuffer9;
  343. friend struct IDirect3DIndexBuffer9;
  344. friend struct IDirect3DPixelShader9;
  345. friend struct IDirect3DVertexShader9;
  346. friend struct IDirect3DQuery9;
  347. friend struct IDirect3DVertexDeclaration9;
  348. IDirect3DDevice9();
  349. virtual ~IDirect3DDevice9();
  350. // Create call invoked from IDirect3D9
  351. HRESULT TOGLMETHODCALLTYPE Create( IDirect3DDevice9Params *params );
  352. //
  353. // Basics
  354. //
  355. HRESULT TOGLMETHODCALLTYPE Reset(D3DPRESENT_PARAMETERS* pPresentationParameters);
  356. HRESULT TOGLMETHODCALLTYPE SetViewport(CONST D3DVIEWPORT9* pViewport);
  357. HRESULT TOGLMETHODCALLTYPE GetViewport(D3DVIEWPORT9* pViewport);
  358. HRESULT TOGLMETHODCALLTYPE BeginScene();
  359. HRESULT TOGLMETHODCALLTYPE Clear(DWORD Count,CONST D3DRECT* pRects,DWORD Flags,D3DCOLOR Color,float Z,DWORD Stencil);
  360. HRESULT TOGLMETHODCALLTYPE EndScene();
  361. HRESULT TOGLMETHODCALLTYPE Present(CONST RECT* pSourceRect,CONST RECT* pDestRect,VD3DHWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion);
  362. // textures
  363. HRESULT TOGLMETHODCALLTYPE CreateTexture(UINT Width,UINT Height,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DTexture9** ppTexture,VD3DHANDLE* pSharedHandle, char *debugLabel=NULL);
  364. HRESULT TOGLMETHODCALLTYPE CreateCubeTexture(UINT EdgeLength,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DCubeTexture9** ppCubeTexture,VD3DHANDLE* pSharedHandle, char *debugLabel=NULL);
  365. HRESULT TOGLMETHODCALLTYPE CreateVolumeTexture(UINT Width,UINT Height,UINT Depth,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DVolumeTexture9** ppVolumeTexture,VD3DHANDLE* pSharedHandle, char *debugLabel=NULL);
  366. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetTexture(DWORD Stage,IDirect3DBaseTexture9* pTexture);
  367. HRESULT TOGLMETHODCALLTYPE SetTextureNonInline(DWORD Stage,IDirect3DBaseTexture9* pTexture);
  368. HRESULT TOGLMETHODCALLTYPE GetTexture(DWORD Stage,IDirect3DBaseTexture9** ppTexture);
  369. // render targets, color and depthstencil, surfaces, blit
  370. HRESULT TOGLMETHODCALLTYPE CreateRenderTarget(UINT Width,UINT Height,D3DFORMAT Format,D3DMULTISAMPLE_TYPE MultiSample,DWORD MultisampleQuality,BOOL Lockable,IDirect3DSurface9** ppSurface,VD3DHANDLE* pSharedHandle, char *debugLabel=NULL);
  371. HRESULT TOGLMETHODCALLTYPE SetRenderTarget(DWORD RenderTargetIndex,IDirect3DSurface9* pRenderTarget);
  372. HRESULT TOGLMETHODCALLTYPE GetRenderTarget(DWORD RenderTargetIndex,IDirect3DSurface9** ppRenderTarget);
  373. HRESULT TOGLMETHODCALLTYPE CreateOffscreenPlainSurface(UINT Width,UINT Height,D3DFORMAT Format,D3DPOOL Pool,IDirect3DSurface9** ppSurface,VD3DHANDLE* pSharedHandle);
  374. HRESULT TOGLMETHODCALLTYPE CreateDepthStencilSurface(UINT Width,UINT Height,D3DFORMAT Format,D3DMULTISAMPLE_TYPE MultiSample,DWORD MultisampleQuality,BOOL Discard,IDirect3DSurface9** ppSurface,VD3DHANDLE* pSharedHandle);
  375. HRESULT TOGLMETHODCALLTYPE SetDepthStencilSurface(IDirect3DSurface9* pNewZStencil);
  376. HRESULT TOGLMETHODCALLTYPE GetDepthStencilSurface(IDirect3DSurface9** ppZStencilSurface);
  377. HRESULT TOGLMETHODCALLTYPE GetRenderTargetData(IDirect3DSurface9* pRenderTarget,IDirect3DSurface9* pDestSurface); // ? is anyone using this ?
  378. HRESULT TOGLMETHODCALLTYPE GetFrontBufferData(UINT iSwapChain,IDirect3DSurface9* pDestSurface);
  379. HRESULT TOGLMETHODCALLTYPE StretchRect(IDirect3DSurface9* pSourceSurface,CONST RECT* pSourceRect,IDirect3DSurface9* pDestSurface,CONST RECT* pDestRect,D3DTEXTUREFILTERTYPE Filter);
  380. // pixel shaders
  381. HRESULT TOGLMETHODCALLTYPE CreatePixelShader(CONST DWORD* pFunction,IDirect3DPixelShader9** ppShader, const char *pShaderName, char *debugLabel = NULL, const uint32 *pCentroidMask = NULL );
  382. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetPixelShader(IDirect3DPixelShader9* pShader);
  383. HRESULT TOGLMETHODCALLTYPE SetPixelShaderNonInline(IDirect3DPixelShader9* pShader);
  384. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetPixelShaderConstantF(UINT StartRegister,CONST float* pConstantData,UINT Vector4fCount);
  385. HRESULT TOGLMETHODCALLTYPE SetPixelShaderConstantFNonInline(UINT StartRegister,CONST float* pConstantData,UINT Vector4fCount);
  386. HRESULT TOGLMETHODCALLTYPE SetPixelShaderConstantB(UINT StartRegister,CONST BOOL* pConstantData,UINT BoolCount);
  387. HRESULT TOGLMETHODCALLTYPE SetPixelShaderConstantI(UINT StartRegister,CONST int* pConstantData,UINT Vector4iCount);
  388. // vertex shaders
  389. HRESULT TOGLMETHODCALLTYPE CreateVertexShader(CONST DWORD* pFunction,IDirect3DVertexShader9** ppShader, const char *pShaderName, char *debugLabel = NULL);
  390. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetVertexShader(IDirect3DVertexShader9* pShader);
  391. HRESULT TOGLMETHODCALLTYPE SetVertexShaderNonInline(IDirect3DVertexShader9* pShader);
  392. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetVertexShaderConstantF(UINT StartRegister,CONST float* pConstantData,UINT Vector4fCount);
  393. HRESULT TOGLMETHODCALLTYPE SetVertexShaderConstantFNonInline(UINT StartRegister,CONST float* pConstantData,UINT Vector4fCount);
  394. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetVertexShaderConstantB(UINT StartRegister,CONST BOOL* pConstantData,UINT BoolCount);
  395. HRESULT TOGLMETHODCALLTYPE SetVertexShaderConstantBNonInline(UINT StartRegister,CONST BOOL* pConstantData,UINT BoolCount);
  396. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetVertexShaderConstantI(UINT StartRegister,CONST int* pConstantData,UINT Vector4iCount);
  397. HRESULT TOGLMETHODCALLTYPE SetVertexShaderConstantINonInline(UINT StartRegister,CONST int* pConstantData,UINT Vector4iCount);
  398. // POSIX only - preheating for a specific vertex/pixel shader pair - trigger GLSL link inside GLM
  399. HRESULT TOGLMETHODCALLTYPE LinkShaderPair( IDirect3DVertexShader9* vs, IDirect3DPixelShader9* ps );
  400. HRESULT TOGLMETHODCALLTYPE ValidateShaderPair( IDirect3DVertexShader9* vs, IDirect3DPixelShader9* ps );
  401. HRESULT TOGLMETHODCALLTYPE QueryShaderPair( int index, GLMShaderPairInfo *infoOut );
  402. // vertex buffers
  403. HRESULT TOGLMETHODCALLTYPE CreateVertexDeclaration(CONST D3DVERTEXELEMENT9* pVertexElements,IDirect3DVertexDeclaration9** ppDecl);
  404. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetVertexDeclaration(IDirect3DVertexDeclaration9* pDecl);
  405. HRESULT TOGLMETHODCALLTYPE SetVertexDeclarationNonInline(IDirect3DVertexDeclaration9* pDecl);
  406. HRESULT TOGLMETHODCALLTYPE SetFVF(DWORD FVF); // we might not be using these ?
  407. HRESULT TOGLMETHODCALLTYPE GetFVF(DWORD* pFVF);
  408. HRESULT CreateVertexBuffer(UINT Length,DWORD Usage,DWORD FVF,D3DPOOL Pool,IDirect3DVertexBuffer9** ppVertexBuffer,VD3DHANDLE* pSharedHandle);
  409. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetStreamSource(UINT StreamNumber,IDirect3DVertexBuffer9* pStreamData,UINT OffsetInBytes,UINT Stride);
  410. HRESULT SetStreamSourceNonInline(UINT StreamNumber,IDirect3DVertexBuffer9* pStreamData,UINT OffsetInBytes,UINT Stride);
  411. // index buffers
  412. HRESULT TOGLMETHODCALLTYPE CreateIndexBuffer(UINT Length,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DIndexBuffer9** ppIndexBuffer,VD3DHANDLE* pSharedHandle);
  413. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetIndices(IDirect3DIndexBuffer9* pIndexData);
  414. HRESULT TOGLMETHODCALLTYPE SetIndicesNonInline(IDirect3DIndexBuffer9* pIndexData);
  415. // State management.
  416. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetRenderStateInline(D3DRENDERSTATETYPE State,DWORD Value);
  417. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetRenderStateConstInline(D3DRENDERSTATETYPE State,DWORD Value);
  418. HRESULT TOGLMETHODCALLTYPE SetRenderState(D3DRENDERSTATETYPE State,DWORD Value);
  419. FORCEINLINE HRESULT TOGLMETHODCALLTYPE SetSamplerState(DWORD Sampler,D3DSAMPLERSTATETYPE Type,DWORD Value);
  420. HRESULT TOGLMETHODCALLTYPE SetSamplerStateNonInline(DWORD Sampler,D3DSAMPLERSTATETYPE Type,DWORD Value);
  421. FORCEINLINE void TOGLMETHODCALLTYPE SetSamplerStates(DWORD Sampler, DWORD AddressU, DWORD AddressV, DWORD AddressW, DWORD MinFilter, DWORD MagFilter, DWORD MipFilter, DWORD MinLod, float LodBias );
  422. void TOGLMETHODCALLTYPE SetSamplerStatesNonInline(DWORD Sampler, DWORD AddressU, DWORD AddressV, DWORD AddressW, DWORD MinFilter, DWORD MagFilter, DWORD MipFilter, DWORD MinLod, float LodBias );
  423. #ifdef OSX
  424. // required for 10.6 support
  425. HRESULT TOGLMETHODCALLTYPE FlushIndexBindings(void); // push index buffer (set index ptr)
  426. HRESULT TOGLMETHODCALLTYPE FlushVertexBindings(uint baseVertexIndex); // push vertex streams (set attrib ptrs)
  427. #endif
  428. // Draw.
  429. HRESULT TOGLMETHODCALLTYPE DrawPrimitive(D3DPRIMITIVETYPE PrimitiveType,UINT StartVertex,UINT PrimitiveCount);
  430. HRESULT TOGLMETHODCALLTYPE DrawIndexedPrimitive(D3DPRIMITIVETYPE PrimitiveType,INT BaseVertexIndex,UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount);
  431. HRESULT TOGLMETHODCALLTYPE DrawIndexedPrimitiveUP(D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,UINT NumVertices,UINT PrimitiveCount,CONST void* pIndexData,D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride);
  432. // misc
  433. BOOL TOGLMETHODCALLTYPE ShowCursor(BOOL bShow);
  434. HRESULT TOGLMETHODCALLTYPE ValidateDevice(DWORD* pNumPasses);
  435. HRESULT TOGLMETHODCALLTYPE SetMaterial(CONST D3DMATERIAL9* pMaterial);
  436. HRESULT TOGLMETHODCALLTYPE LightEnable(DWORD Index,BOOL Enable);
  437. HRESULT TOGLMETHODCALLTYPE SetScissorRect(CONST RECT* pRect);
  438. HRESULT TOGLMETHODCALLTYPE CreateQuery(D3DQUERYTYPE Type,IDirect3DQuery9** ppQuery);
  439. HRESULT TOGLMETHODCALLTYPE GetDeviceCaps(D3DCAPS9* pCaps);
  440. HRESULT TOGLMETHODCALLTYPE TestCooperativeLevel();
  441. HRESULT TOGLMETHODCALLTYPE EvictManagedResources();
  442. HRESULT TOGLMETHODCALLTYPE SetLight(DWORD Index,CONST D3DLIGHT9*);
  443. void TOGLMETHODCALLTYPE SetGammaRamp(UINT iSwapChain,DWORD Flags,CONST D3DGAMMARAMP* pRamp);
  444. void TOGLMETHODCALLTYPE SaveGLState();
  445. void TOGLMETHODCALLTYPE RestoreGLState();
  446. // Talk to JasonM about this one. It's tricky in GL.
  447. HRESULT TOGLMETHODCALLTYPE SetClipPlane(DWORD Index,CONST float* pPlane);
  448. //
  449. //
  450. // **** FIXED FUNCTION STUFF - None of this stuff needs support in GL.
  451. //
  452. //
  453. HRESULT TOGLMETHODCALLTYPE SetTransform(D3DTRANSFORMSTATETYPE State,CONST D3DMATRIX* pMatrix);
  454. HRESULT TOGLMETHODCALLTYPE SetTextureStageState(DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD Value);
  455. void TOGLMETHODCALLTYPE AcquireThreadOwnership( );
  456. void TOGLMETHODCALLTYPE ReleaseThreadOwnership( );
  457. inline DWORD TOGLMETHODCALLTYPE GetCurrentOwnerThreadId() const { return m_ctx->m_nCurOwnerThreadId; }
  458. FORCEINLINE void TOGLMETHODCALLTYPE SetMaxUsedVertexShaderConstantsHint( uint nMaxReg );
  459. void TOGLMETHODCALLTYPE SetMaxUsedVertexShaderConstantsHintNonInline( uint nMaxReg );
  460. void DumpStatsToConsole( const CCommand *pArgs );
  461. #if GLMDEBUG
  462. void DumpTextures( const CCommand *pArgs );
  463. #endif
  464. private:
  465. IDirect3DDevice9( const IDirect3DDevice9& );
  466. IDirect3DDevice9& operator= ( const IDirect3DDevice9& );
  467. // Flushing changes to GL
  468. void FlushClipPlaneEquation();
  469. void InitStates();
  470. void FullFlushStates();
  471. void UpdateBoundFBO();
  472. void ResetFBOMap();
  473. void ScrubFBOMap( CGLMTex *pTex );
  474. // response to retired objects (when refcount goes to zero and they self-delete..)
  475. void ReleasedVertexDeclaration( IDirect3DVertexDeclaration9 *pDecl );
  476. void ReleasedTexture( IDirect3DBaseTexture9 *baseTex ); // called from texture destructor - need to scrub samplers
  477. void ReleasedCGLMTex( CGLMTex *pTex );
  478. void ReleasedSurface( IDirect3DSurface9 *surface ); // called from any surface destructor - need to scrub RT table if an RT
  479. void ReleasedPixelShader( IDirect3DPixelShader9 *pixelShader ); // called from IDirect3DPixelShader9 destructor
  480. void ReleasedVertexShader( IDirect3DVertexShader9 *vertexShader ); // called from IDirect3DVertexShader9 destructor
  481. void ReleasedVertexBuffer( IDirect3DVertexBuffer9 *vertexBuffer ); // called from IDirect3DVertexBuffer9 destructor
  482. void ReleasedIndexBuffer( IDirect3DIndexBuffer9 *indexBuffer ); // called from IDirect3DIndexBuffer9 destructor
  483. void ReleasedQuery( IDirect3DQuery9 *query ); // called from IDirect3DQuery9 destructor
  484. // Member variables
  485. DWORD m_nValidMarker;
  486. public:
  487. IDirect3DDevice9Params m_params; // mirror of the creation inputs
  488. private:
  489. // D3D flavor stuff
  490. IDirect3DSurface9 *m_pRenderTargets[4];
  491. IDirect3DSurface9 *m_pDepthStencil;
  492. IDirect3DSurface9 *m_pDefaultColorSurface; // default color surface.
  493. IDirect3DSurface9 *m_pDefaultDepthStencilSurface; // queried by GetDepthStencilSurface.
  494. IDirect3DVertexDeclaration9 *m_pVertDecl; // Set by SetVertexDeclaration...
  495. D3DStreamDesc m_streams[ D3D_MAX_STREAMS ]; // Set by SetStreamSource..
  496. CGLMBuffer *m_vtx_buffers[ D3D_MAX_STREAMS ];
  497. CGLMBuffer *m_pDummy_vtx_buffer;
  498. D3DIndexDesc m_indices; // Set by SetIndices..
  499. IDirect3DVertexShader9 *m_vertexShader; // Set by SetVertexShader...
  500. IDirect3DPixelShader9 *m_pixelShader; // Set by SetPixelShader...
  501. IDirect3DBaseTexture9 *m_textures[GLM_SAMPLER_COUNT]; // set by SetTexture... NULL if stage inactive
  502. // GLM flavor stuff
  503. GLMContext *m_ctx;
  504. CGLMFBOMap *m_pFBOs;
  505. bool m_bFBODirty;
  506. struct ObjectStats_t
  507. {
  508. int m_nTotalFBOs;
  509. int m_nTotalVertexShaders;
  510. int m_nTotalPixelShaders;
  511. int m_nTotalVertexDecls;
  512. int m_nTotalIndexBuffers;
  513. int m_nTotalVertexBuffers;
  514. int m_nTotalRenderTargets;
  515. int m_nTotalTextures;
  516. int m_nTotalSurfaces;
  517. int m_nTotalQueries;
  518. void clear() { V_memset( this, 0, sizeof(* this ) ); }
  519. ObjectStats_t &operator -= ( const ObjectStats_t &rhs )
  520. {
  521. m_nTotalFBOs -= rhs.m_nTotalFBOs;
  522. m_nTotalVertexShaders -= rhs.m_nTotalVertexShaders;
  523. m_nTotalPixelShaders -= rhs.m_nTotalPixelShaders;
  524. m_nTotalVertexDecls -= rhs.m_nTotalVertexDecls;
  525. m_nTotalIndexBuffers -= rhs.m_nTotalIndexBuffers;
  526. m_nTotalVertexBuffers -= rhs.m_nTotalVertexBuffers;
  527. m_nTotalRenderTargets -= rhs.m_nTotalRenderTargets;
  528. m_nTotalTextures -= rhs.m_nTotalTextures;
  529. m_nTotalSurfaces -= rhs.m_nTotalSurfaces;
  530. m_nTotalQueries -= m_nTotalQueries;
  531. return *this;
  532. }
  533. };
  534. ObjectStats_t m_ObjectStats;
  535. ObjectStats_t m_PrevObjectStats;
  536. void PrintObjectStats( const ObjectStats_t &stats );
  537. // GL state
  538. struct
  539. {
  540. // render state buckets
  541. GLAlphaTestEnable_t m_AlphaTestEnable;
  542. GLAlphaTestFunc_t m_AlphaTestFunc;
  543. GLAlphaToCoverageEnable_t m_AlphaToCoverageEnable;
  544. GLDepthTestEnable_t m_DepthTestEnable;
  545. GLDepthMask_t m_DepthMask;
  546. GLDepthFunc_t m_DepthFunc;
  547. GLClipPlaneEnable_t m_ClipPlaneEnable[kGLMUserClipPlanes];
  548. GLClipPlaneEquation_t m_ClipPlaneEquation[kGLMUserClipPlanes];
  549. GLColorMaskSingle_t m_ColorMaskSingle;
  550. GLColorMaskMultiple_t m_ColorMaskMultiple;
  551. GLCullFaceEnable_t m_CullFaceEnable;
  552. GLCullFrontFace_t m_CullFrontFace;
  553. GLPolygonMode_t m_PolygonMode;
  554. GLDepthBias_t m_DepthBias;
  555. GLScissorEnable_t m_ScissorEnable;
  556. GLScissorBox_t m_ScissorBox;
  557. GLViewportBox_t m_ViewportBox;
  558. GLViewportDepthRange_t m_ViewportDepthRange;
  559. GLBlendEnable_t m_BlendEnable;
  560. GLBlendFactor_t m_BlendFactor;
  561. GLBlendEquation_t m_BlendEquation;
  562. GLBlendColor_t m_BlendColor;
  563. GLBlendEnableSRGB_t m_BlendEnableSRGB;
  564. GLStencilTestEnable_t m_StencilTestEnable;
  565. GLStencilFunc_t m_StencilFunc;
  566. GLStencilOp_t m_StencilOp;
  567. GLStencilWriteMask_t m_StencilWriteMask;
  568. GLClearColor_t m_ClearColor;
  569. GLClearDepth_t m_ClearDepth;
  570. GLClearStencil_t m_ClearStencil;
  571. bool m_FogEnable; // not really pushed to GL, just latched here
  572. // samplers
  573. //GLMTexSamplingParams m_samplers[GLM_SAMPLER_COUNT];
  574. } gl;
  575. #if GL_BATCH_PERF_ANALYSIS
  576. simple_bitmap *m_pBatch_vis_bitmap;
  577. uint m_nBatchVisY;
  578. uint m_nBatchVisFrameIndex, m_nBatchVisFileIdx;
  579. uint m_nNumProgramChanges;
  580. uint m_nTotalD3DCalls;
  581. double m_flTotalD3DTime;
  582. uint m_nTotalGLCalls;
  583. double m_flTotalGLTime;
  584. uint m_nTotalPrims;
  585. uint m_nOverallProgramChanges;
  586. uint m_nOverallDraws;
  587. uint m_nOverallPrims;
  588. uint m_nOverallD3DCalls;
  589. double m_flOverallD3DTime;
  590. uint m_nOverallGLCalls;
  591. double m_flOverallGLTime;
  592. double m_flOverallPresentTime;
  593. double m_flOverallPresentTimeSquared;
  594. double m_flOverallSwapWindowTime;
  595. double m_flOverallSwapWindowTimeSquared;
  596. uint m_nOverallPresents;
  597. #endif
  598. };
  599. FORCEINLINE HRESULT TOGLMETHODCALLTYPE IDirect3DDevice9::SetSamplerState( DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value )
  600. {
  601. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  602. return SetSamplerStateNonInline( Sampler, Type, Value );
  603. #else
  604. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  605. Assert( Sampler < GLM_SAMPLER_COUNT );
  606. m_ctx->SetSamplerDirty( Sampler );
  607. switch( Type )
  608. {
  609. case D3DSAMP_ADDRESSU:
  610. m_ctx->SetSamplerAddressU( Sampler, Value );
  611. break;
  612. case D3DSAMP_ADDRESSV:
  613. m_ctx->SetSamplerAddressV( Sampler, Value );
  614. break;
  615. case D3DSAMP_ADDRESSW:
  616. m_ctx->SetSamplerAddressW( Sampler, Value );
  617. break;
  618. case D3DSAMP_BORDERCOLOR:
  619. m_ctx->SetSamplerBorderColor( Sampler, Value );
  620. break;
  621. case D3DSAMP_MAGFILTER:
  622. m_ctx->SetSamplerMagFilter( Sampler, Value );
  623. break;
  624. case D3DSAMP_MIPFILTER:
  625. m_ctx->SetSamplerMipFilter( Sampler, Value );
  626. break;
  627. case D3DSAMP_MINFILTER:
  628. m_ctx->SetSamplerMinFilter( Sampler, Value );
  629. break;
  630. case D3DSAMP_MIPMAPLODBIAS:
  631. m_ctx->SetSamplerMipMapLODBias( Sampler, Value );
  632. break;
  633. case D3DSAMP_MAXMIPLEVEL:
  634. m_ctx->SetSamplerMaxMipLevel( Sampler, Value);
  635. break;
  636. case D3DSAMP_MAXANISOTROPY:
  637. m_ctx->SetSamplerMaxAnisotropy( Sampler, Value);
  638. break;
  639. case D3DSAMP_SRGBTEXTURE:
  640. //m_samplers[ Sampler ].m_srgb = Value;
  641. m_ctx->SetSamplerSRGBTexture(Sampler, Value);
  642. break;
  643. case D3DSAMP_SHADOWFILTER:
  644. m_ctx->SetShadowFilter(Sampler, Value);
  645. break;
  646. default: DXABSTRACT_BREAK_ON_ERROR(); break;
  647. }
  648. return S_OK;
  649. #endif
  650. }
  651. FORCEINLINE void TOGLMETHODCALLTYPE IDirect3DDevice9::SetSamplerStates(
  652. DWORD Sampler, DWORD AddressU, DWORD AddressV, DWORD AddressW,
  653. DWORD MinFilter, DWORD MagFilter, DWORD MipFilter, DWORD MinLod,
  654. float LodBias)
  655. {
  656. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  657. SetSamplerStatesNonInline( Sampler, AddressU, AddressV, AddressW, MinFilter, MagFilter, MipFilter, MinLod, LodBias );
  658. #else
  659. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  660. Assert( Sampler < GLM_SAMPLER_COUNT);
  661. m_ctx->SetSamplerDirty( Sampler );
  662. m_ctx->SetSamplerStates( Sampler, AddressU, AddressV, AddressW, MinFilter, MagFilter, MipFilter, MinLod, LodBias );
  663. #endif
  664. }
  665. FORCEINLINE HRESULT TOGLMETHODCALLTYPE IDirect3DDevice9::SetTexture(DWORD Stage,IDirect3DBaseTexture9* pTexture)
  666. {
  667. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  668. return SetTextureNonInline( Stage, pTexture );
  669. #else
  670. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  671. Assert( Stage < GLM_SAMPLER_COUNT );
  672. m_textures[Stage] = pTexture;
  673. m_ctx->SetSamplerTex( Stage, pTexture ? pTexture->m_tex : NULL );
  674. return S_OK;
  675. #endif
  676. }
  677. inline GLenum D3DCompareFuncToGL( DWORD function )
  678. {
  679. switch ( function )
  680. {
  681. case D3DCMP_NEVER : return GL_NEVER; // Always fail the test.
  682. case D3DCMP_LESS : return GL_LESS; // Accept the new pixel if its value is less than the value of the current pixel.
  683. case D3DCMP_EQUAL : return GL_EQUAL; // Accept the new pixel if its value equals the value of the current pixel.
  684. case D3DCMP_LESSEQUAL : return GL_LEQUAL; // Accept the new pixel if its value is less than or equal to the value of the current pixel. **
  685. case D3DCMP_GREATER : return GL_GREATER; // Accept the new pixel if its value is greater than the value of the current pixel.
  686. case D3DCMP_NOTEQUAL : return GL_NOTEQUAL; // Accept the new pixel if its value does not equal the value of the current pixel.
  687. case D3DCMP_GREATEREQUAL: return GL_GEQUAL; // Accept the new pixel if its value is greater than or equal to the value of the current pixel.
  688. case D3DCMP_ALWAYS : return GL_ALWAYS; // Always pass the test.
  689. default : DXABSTRACT_BREAK_ON_ERROR(); return 0xFFFFFFFF;
  690. }
  691. }
  692. FORCEINLINE GLenum D3DBlendOperationToGL( DWORD operation )
  693. {
  694. switch (operation)
  695. {
  696. case D3DBLENDOP_ADD : return GL_FUNC_ADD; // The result is the destination added to the source. Result = Source + Destination
  697. case D3DBLENDOP_SUBTRACT : return GL_FUNC_SUBTRACT; // The result is the destination subtracted from to the source. Result = Source - Destination
  698. case D3DBLENDOP_REVSUBTRACT : return GL_FUNC_REVERSE_SUBTRACT; // The result is the source subtracted from the destination. Result = Destination - Source
  699. case D3DBLENDOP_MIN : return GL_MIN; // The result is the minimum of the source and destination. Result = MIN(Source, Destination)
  700. case D3DBLENDOP_MAX : return GL_MAX; // The result is the maximum of the source and destination. Result = MAX(Source, Destination)
  701. default:
  702. DXABSTRACT_BREAK_ON_ERROR();
  703. return 0xFFFFFFFF;
  704. break;
  705. }
  706. }
  707. FORCEINLINE GLenum D3DBlendFactorToGL( DWORD equation )
  708. {
  709. switch (equation)
  710. {
  711. case D3DBLEND_ZERO : return GL_ZERO; // Blend factor is (0, 0, 0, 0).
  712. case D3DBLEND_ONE : return GL_ONE; // Blend factor is (1, 1, 1, 1).
  713. case D3DBLEND_SRCCOLOR : return GL_SRC_COLOR; // Blend factor is (Rs, Gs, Bs, As).
  714. case D3DBLEND_INVSRCCOLOR : return GL_ONE_MINUS_SRC_COLOR; // Blend factor is (1 - Rs, 1 - Gs, 1 - Bs, 1 - As).
  715. case D3DBLEND_SRCALPHA : return GL_SRC_ALPHA; // Blend factor is (As, As, As, As).
  716. case D3DBLEND_INVSRCALPHA : return GL_ONE_MINUS_SRC_ALPHA; // Blend factor is ( 1 - As, 1 - As, 1 - As, 1 - As).
  717. case D3DBLEND_DESTALPHA : return GL_DST_ALPHA; // Blend factor is (Ad Ad Ad Ad).
  718. case D3DBLEND_INVDESTALPHA : return GL_ONE_MINUS_DST_ALPHA; // Blend factor is (1 - Ad 1 - Ad 1 - Ad 1 - Ad).
  719. case D3DBLEND_DESTCOLOR : return GL_DST_COLOR; // Blend factor is (Rd, Gd, Bd, Ad).
  720. case D3DBLEND_INVDESTCOLOR : return GL_ONE_MINUS_DST_COLOR; // Blend factor is (1 - Rd, 1 - Gd, 1 - Bd, 1 - Ad).
  721. case D3DBLEND_SRCALPHASAT : return GL_SRC_ALPHA_SATURATE; // Blend factor is (f, f, f, 1); where f = min(As, 1 - Ad).
  722. /*
  723. // these are weird.... break if we hit them
  724. case D3DBLEND_BOTHSRCALPHA : Assert(0); return GL_ZERO; // Obsolete. Starting with DirectX 6, you can achieve the same effect by setting the source and destination blend factors to D3DBLEND_SRCALPHA and D3DBLEND_INVSRCALPHA in separate calls.
  725. case D3DBLEND_BOTHINVSRCALPHA: Assert(0); return GL_ZERO; // Source blend factor is (1 - As, 1 - As, 1 - As, 1 - As), and destination blend factor is (As, As, As, As); the destination blend selection is overridden. This blend mode is supported only for the D3DRS_SRCBLEND render state.
  726. case D3DBLEND_BLENDFACTOR : Assert(0); return GL_ZERO; // Constant color blending factor used by the frame-buffer blender. This blend mode is supported only if D3DPBLENDCAPS_BLENDFACTOR is set in the SrcBlendCaps or DestBlendCaps members of D3DCAPS9.
  727. dxabstract.h has not heard of these, so let them hit the debugger if they come through
  728. case D3DBLEND_INVBLENDFACTOR: //Inverted constant color-blending factor used by the frame-buffer blender. This blend mode is supported only if the D3DPBLENDCAPS_BLENDFACTOR bit is set in the SrcBlendCaps or DestBlendCaps members of D3DCAPS9.
  729. case D3DBLEND_SRCCOLOR2: // Blend factor is (PSOutColor[1]r, PSOutColor[1]g, PSOutColor[1]b, not used). This flag is available in Direct3D 9Ex only.
  730. case D3DBLEND_INVSRCCOLOR2: // Blend factor is (1 - PSOutColor[1]r, 1 - PSOutColor[1]g, 1 - PSOutColor[1]b, not used)). This flag is available in Direct3D 9Ex only.
  731. */
  732. default:
  733. DXABSTRACT_BREAK_ON_ERROR();
  734. return 0xFFFFFFFF;
  735. break;
  736. }
  737. }
  738. FORCEINLINE GLenum D3DStencilOpToGL( DWORD operation )
  739. {
  740. switch( operation )
  741. {
  742. case D3DSTENCILOP_KEEP : return GL_KEEP;
  743. case D3DSTENCILOP_ZERO : return GL_ZERO;
  744. case D3DSTENCILOP_REPLACE : return GL_REPLACE;
  745. case D3DSTENCILOP_INCRSAT : return GL_INCR;
  746. case D3DSTENCILOP_DECRSAT : return GL_DECR;
  747. case D3DSTENCILOP_INVERT : return GL_INVERT;
  748. case D3DSTENCILOP_INCR : return GL_INCR_WRAP_EXT;
  749. case D3DSTENCILOP_DECR : return GL_DECR_WRAP_EXT;
  750. default : DXABSTRACT_BREAK_ON_ERROR(); return 0xFFFFFFFF;
  751. }
  752. }
  753. FORCEINLINE HRESULT TOGLMETHODCALLTYPE IDirect3DDevice9::SetRenderStateInline( D3DRENDERSTATETYPE State, DWORD Value )
  754. {
  755. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  756. return SetRenderState( State, Value );
  757. #else
  758. TOGL_NULL_DEVICE_CHECK;
  759. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  760. switch (State)
  761. {
  762. case D3DRS_ZENABLE: // kGLDepthTestEnable
  763. {
  764. gl.m_DepthTestEnable.enable = Value;
  765. m_ctx->WriteDepthTestEnable( &gl.m_DepthTestEnable );
  766. break;
  767. }
  768. case D3DRS_ZWRITEENABLE: // kGLDepthMask
  769. {
  770. gl.m_DepthMask.mask = Value;
  771. m_ctx->WriteDepthMask( &gl.m_DepthMask );
  772. break;
  773. }
  774. case D3DRS_ZFUNC:
  775. {
  776. // kGLDepthFunc
  777. GLenum func = D3DCompareFuncToGL( Value );
  778. gl.m_DepthFunc.func = func;
  779. m_ctx->WriteDepthFunc( &gl.m_DepthFunc );
  780. break;
  781. }
  782. case D3DRS_COLORWRITEENABLE: // kGLColorMaskSingle
  783. {
  784. gl.m_ColorMaskSingle.r = ((Value & D3DCOLORWRITEENABLE_RED) != 0) ? 0xFF : 0x00;
  785. gl.m_ColorMaskSingle.g = ((Value & D3DCOLORWRITEENABLE_GREEN)!= 0) ? 0xFF : 0x00;
  786. gl.m_ColorMaskSingle.b = ((Value & D3DCOLORWRITEENABLE_BLUE) != 0) ? 0xFF : 0x00;
  787. gl.m_ColorMaskSingle.a = ((Value & D3DCOLORWRITEENABLE_ALPHA)!= 0) ? 0xFF : 0x00;
  788. m_ctx->WriteColorMaskSingle( &gl.m_ColorMaskSingle );
  789. break;
  790. }
  791. case D3DRS_CULLMODE: // kGLCullFaceEnable / kGLCullFrontFace
  792. {
  793. switch (Value)
  794. {
  795. case D3DCULL_NONE:
  796. {
  797. gl.m_CullFaceEnable.enable = false;
  798. gl.m_CullFrontFace.value = GL_CCW; //doesn't matter
  799. m_ctx->WriteCullFaceEnable( &gl.m_CullFaceEnable );
  800. m_ctx->WriteCullFrontFace( &gl.m_CullFrontFace );
  801. break;
  802. }
  803. case D3DCULL_CW:
  804. {
  805. gl.m_CullFaceEnable.enable = true;
  806. gl.m_CullFrontFace.value = GL_CW; //origGL_CCW;
  807. m_ctx->WriteCullFaceEnable( &gl.m_CullFaceEnable );
  808. m_ctx->WriteCullFrontFace( &gl.m_CullFrontFace );
  809. break;
  810. }
  811. case D3DCULL_CCW:
  812. {
  813. gl.m_CullFaceEnable.enable = true;
  814. gl.m_CullFrontFace.value = GL_CCW; //origGL_CW;
  815. m_ctx->WriteCullFaceEnable( &gl.m_CullFaceEnable );
  816. m_ctx->WriteCullFrontFace( &gl.m_CullFrontFace );
  817. break;
  818. }
  819. default:
  820. {
  821. DXABSTRACT_BREAK_ON_ERROR();
  822. break;
  823. }
  824. }
  825. break;
  826. }
  827. //-------------------------------------------------------------------------------------------- alphablend stuff
  828. case D3DRS_ALPHABLENDENABLE: // kGLBlendEnable
  829. {
  830. gl.m_BlendEnable.enable = Value;
  831. m_ctx->WriteBlendEnable( &gl.m_BlendEnable );
  832. break;
  833. }
  834. case D3DRS_BLENDOP: // kGLBlendEquation // D3D blend-op ==> GL blend equation
  835. {
  836. GLenum equation = D3DBlendOperationToGL( Value );
  837. gl.m_BlendEquation.equation = equation;
  838. m_ctx->WriteBlendEquation( &gl.m_BlendEquation );
  839. break;
  840. }
  841. case D3DRS_SRCBLEND: // kGLBlendFactor // D3D blend-factor ==> GL blend factor
  842. case D3DRS_DESTBLEND: // kGLBlendFactor
  843. {
  844. GLenum factor = D3DBlendFactorToGL( Value );
  845. if (State==D3DRS_SRCBLEND)
  846. {
  847. gl.m_BlendFactor.srcfactor = factor;
  848. }
  849. else
  850. {
  851. gl.m_BlendFactor.dstfactor = factor;
  852. }
  853. m_ctx->WriteBlendFactor( &gl.m_BlendFactor );
  854. break;
  855. }
  856. case D3DRS_SRGBWRITEENABLE: // kGLBlendEnableSRGB
  857. {
  858. gl.m_BlendEnableSRGB.enable = Value;
  859. m_ctx->WriteBlendEnableSRGB( &gl.m_BlendEnableSRGB );
  860. break;
  861. }
  862. //-------------------------------------------------------------------------------------------- alphatest stuff
  863. case D3DRS_ALPHATESTENABLE:
  864. {
  865. gl.m_AlphaTestEnable.enable = Value;
  866. m_ctx->WriteAlphaTestEnable( &gl.m_AlphaTestEnable );
  867. break;
  868. }
  869. case D3DRS_ALPHAREF:
  870. {
  871. gl.m_AlphaTestFunc.ref = Value / 255.0f;
  872. m_ctx->WriteAlphaTestFunc( &gl.m_AlphaTestFunc );
  873. break;
  874. }
  875. case D3DRS_ALPHAFUNC:
  876. {
  877. GLenum func = D3DCompareFuncToGL( Value );;
  878. gl.m_AlphaTestFunc.func = func;
  879. m_ctx->WriteAlphaTestFunc( &gl.m_AlphaTestFunc );
  880. break;
  881. }
  882. //-------------------------------------------------------------------------------------------- stencil stuff
  883. case D3DRS_STENCILENABLE: // GLStencilTestEnable_t
  884. {
  885. gl.m_StencilTestEnable.enable = Value;
  886. m_ctx->WriteStencilTestEnable( &gl.m_StencilTestEnable );
  887. break;
  888. }
  889. case D3DRS_STENCILFAIL: // GLStencilOp_t "what do you do if stencil test fails"
  890. {
  891. GLenum stencilop = D3DStencilOpToGL( Value );
  892. gl.m_StencilOp.sfail = stencilop;
  893. m_ctx->WriteStencilOp( &gl.m_StencilOp,0 );
  894. m_ctx->WriteStencilOp( &gl.m_StencilOp,1 ); // ********* need to recheck this
  895. break;
  896. }
  897. case D3DRS_STENCILZFAIL: // GLStencilOp_t "what do you do if stencil test passes *but* depth test fails, if depth test happened"
  898. {
  899. GLenum stencilop = D3DStencilOpToGL( Value );
  900. gl.m_StencilOp.dpfail = stencilop;
  901. m_ctx->WriteStencilOp( &gl.m_StencilOp,0 );
  902. m_ctx->WriteStencilOp( &gl.m_StencilOp,1 ); // ********* need to recheck this
  903. break;
  904. }
  905. case D3DRS_STENCILPASS: // GLStencilOp_t "what do you do if stencil test and depth test both pass"
  906. {
  907. GLenum stencilop = D3DStencilOpToGL( Value );
  908. gl.m_StencilOp.dppass = stencilop;
  909. m_ctx->WriteStencilOp( &gl.m_StencilOp,0 );
  910. m_ctx->WriteStencilOp( &gl.m_StencilOp,1 ); // ********* need to recheck this
  911. break;
  912. }
  913. case D3DRS_STENCILFUNC: // GLStencilFunc_t
  914. {
  915. GLenum stencilfunc = D3DCompareFuncToGL( Value );
  916. gl.m_StencilFunc.frontfunc = gl.m_StencilFunc.backfunc = stencilfunc;
  917. m_ctx->WriteStencilFunc( &gl.m_StencilFunc );
  918. break;
  919. }
  920. case D3DRS_STENCILREF: // GLStencilFunc_t
  921. {
  922. gl.m_StencilFunc.ref = Value;
  923. m_ctx->WriteStencilFunc( &gl.m_StencilFunc );
  924. break;
  925. }
  926. case D3DRS_STENCILMASK: // GLStencilFunc_t
  927. {
  928. gl.m_StencilFunc.mask = Value;
  929. m_ctx->WriteStencilFunc( &gl.m_StencilFunc );
  930. break;
  931. }
  932. case D3DRS_STENCILWRITEMASK: // GLStencilWriteMask_t
  933. {
  934. gl.m_StencilWriteMask.mask = Value;
  935. m_ctx->WriteStencilWriteMask( &gl.m_StencilWriteMask );
  936. break;
  937. }
  938. case D3DRS_FOGENABLE: // none of these are implemented yet... erk
  939. {
  940. gl.m_FogEnable = (Value != 0);
  941. GLMPRINTF(("-D- fogenable = %d",Value ));
  942. break;
  943. }
  944. case D3DRS_SCISSORTESTENABLE: // kGLScissorEnable
  945. {
  946. gl.m_ScissorEnable.enable = Value;
  947. m_ctx->WriteScissorEnable( &gl.m_ScissorEnable );
  948. break;
  949. }
  950. case D3DRS_DEPTHBIAS: // kGLDepthBias
  951. {
  952. // the value in the dword is actually a float
  953. float fvalue = *(float*)&Value;
  954. gl.m_DepthBias.units = fvalue;
  955. m_ctx->WriteDepthBias( &gl.m_DepthBias );
  956. break;
  957. }
  958. // good ref on these: http://aras-p.info/blog/2008/06/12/depth-bias-and-the-power-of-deceiving-yourself/
  959. case D3DRS_SLOPESCALEDEPTHBIAS:
  960. {
  961. // the value in the dword is actually a float
  962. float fvalue = *(float*)&Value;
  963. gl.m_DepthBias.factor = fvalue;
  964. m_ctx->WriteDepthBias( &gl.m_DepthBias );
  965. break;
  966. }
  967. // Alpha to coverage
  968. case D3DRS_ADAPTIVETESS_Y:
  969. {
  970. gl.m_AlphaToCoverageEnable.enable = Value;
  971. m_ctx->WriteAlphaToCoverageEnable( &gl.m_AlphaToCoverageEnable );
  972. break;
  973. }
  974. case D3DRS_CLIPPLANEENABLE: // kGLClipPlaneEnable
  975. {
  976. // d3d packs all the enables into one word.
  977. // we break that out so we don't do N glEnable calls to sync -
  978. // GLM is tracking one unique enable per plane.
  979. for( int i=0; i<kGLMUserClipPlanes; i++)
  980. {
  981. gl.m_ClipPlaneEnable[i].enable = (Value & (1<<i)) != 0;
  982. }
  983. for( int x=0; x<kGLMUserClipPlanes; x++)
  984. m_ctx->WriteClipPlaneEnable( &gl.m_ClipPlaneEnable[x], x );
  985. break;
  986. }
  987. //-------------------------------------------------------------------------------------------- polygon/fill mode
  988. case D3DRS_FILLMODE:
  989. {
  990. GLuint mode = 0;
  991. switch(Value)
  992. {
  993. case D3DFILL_POINT: mode = GL_POINT; break;
  994. case D3DFILL_WIREFRAME: mode = GL_LINE; break;
  995. case D3DFILL_SOLID: mode = GL_FILL; break;
  996. default: DXABSTRACT_BREAK_ON_ERROR(); break;
  997. }
  998. gl.m_PolygonMode.values[0] = gl.m_PolygonMode.values[1] = mode;
  999. m_ctx->WritePolygonMode( &gl.m_PolygonMode );
  1000. break;
  1001. }
  1002. }
  1003. return S_OK;
  1004. #endif
  1005. }
  1006. FORCEINLINE HRESULT TOGLMETHODCALLTYPE IDirect3DDevice9::SetRenderStateConstInline( D3DRENDERSTATETYPE State, DWORD Value )
  1007. {
  1008. // State is a compile time constant - luckily no need to do anything special to get the compiler to optimize this case.
  1009. return SetRenderStateInline( State, Value );
  1010. }
  1011. FORCEINLINE HRESULT TOGLMETHODCALLTYPE IDirect3DDevice9::SetIndices(IDirect3DIndexBuffer9* pIndexData)
  1012. {
  1013. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  1014. return SetIndicesNonInline( pIndexData );
  1015. #else
  1016. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  1017. // just latch it.
  1018. m_indices.m_idxBuffer = pIndexData;
  1019. return S_OK;
  1020. #endif
  1021. }
  1022. FORCEINLINE HRESULT TOGLMETHODCALLTYPE IDirect3DDevice9::SetStreamSource(UINT StreamNumber,IDirect3DVertexBuffer9* pStreamData,UINT OffsetInBytes,UINT Stride)
  1023. {
  1024. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  1025. return SetStreamSourceNonInline( StreamNumber, pStreamData, OffsetInBytes, Stride );
  1026. #else
  1027. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  1028. Assert( StreamNumber < D3D_MAX_STREAMS );
  1029. Assert( ( Stride & 3 ) == 0 ); // we support non-DWORD aligned strides, but on some drivers (like AMD's) perf goes off a cliff
  1030. // perfectly legal to see a vertex buffer of NULL get passed in here.
  1031. // so we need an array to track these.
  1032. // OK, we are being given the stride, we don't need to calc it..
  1033. GLMPRINTF(("-X- IDirect3DDevice9::SetStreamSource setting stream #%d to D3D buf %p (GL name %d); offset %d, stride %d", StreamNumber, pStreamData, (pStreamData) ? pStreamData->m_vtxBuffer->m_name: -1, OffsetInBytes, Stride));
  1034. if ( !pStreamData )
  1035. {
  1036. OffsetInBytes = 0;
  1037. Stride = 0;
  1038. m_vtx_buffers[ StreamNumber ] = m_pDummy_vtx_buffer;
  1039. }
  1040. else
  1041. {
  1042. // We do not support strides of 0
  1043. Assert( Stride > 0 );
  1044. m_vtx_buffers[ StreamNumber ] = pStreamData->m_vtxBuffer;
  1045. }
  1046. m_streams[ StreamNumber ].m_vtxBuffer = pStreamData;
  1047. m_streams[ StreamNumber ].m_offset = OffsetInBytes;
  1048. m_streams[ StreamNumber ].m_stride = Stride;
  1049. return S_OK;
  1050. #endif
  1051. }
  1052. FORCEINLINE HRESULT TOGLMETHODCALLTYPE IDirect3DDevice9::SetVertexShaderConstantF(UINT StartRegister,CONST float* pConstantData,UINT Vector4fCount) // groups of 4 floats!
  1053. {
  1054. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  1055. return SetVertexShaderConstantFNonInline( StartRegister, pConstantData, Vector4fCount );
  1056. #else
  1057. TOGL_NULL_DEVICE_CHECK;
  1058. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  1059. m_ctx->SetProgramParametersF( kGLMVertexProgram, StartRegister, (float *)pConstantData, Vector4fCount );
  1060. return S_OK;
  1061. #endif
  1062. }
  1063. FORCEINLINE HRESULT TOGLMETHODCALLTYPE IDirect3DDevice9::SetVertexShaderConstantB(UINT StartRegister,CONST BOOL* pConstantData,UINT BoolCount)
  1064. {
  1065. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  1066. return SetVertexShaderConstantBNonInline( StartRegister, pConstantData, BoolCount );
  1067. #else
  1068. TOGL_NULL_DEVICE_CHECK;
  1069. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  1070. m_ctx->SetProgramParametersB( kGLMVertexProgram, StartRegister, (int *)pConstantData, BoolCount );
  1071. return S_OK;
  1072. #endif
  1073. }
  1074. FORCEINLINE HRESULT IDirect3DDevice9::SetVertexShaderConstantI(UINT StartRegister,CONST int* pConstantData,UINT Vector4iCount) // groups of 4 ints!
  1075. {
  1076. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  1077. return SetVertexShaderConstantINonInline( StartRegister, pConstantData, Vector4iCount );
  1078. #else
  1079. TOGL_NULL_DEVICE_CHECK;
  1080. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  1081. m_ctx->SetProgramParametersI( kGLMVertexProgram, StartRegister, (int *)pConstantData, Vector4iCount );
  1082. return S_OK;
  1083. #endif
  1084. }
  1085. FORCEINLINE HRESULT TOGLMETHODCALLTYPE IDirect3DDevice9::SetPixelShaderConstantF(UINT StartRegister,CONST float* pConstantData,UINT Vector4fCount)
  1086. {
  1087. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  1088. return SetPixelShaderConstantFNonInline(StartRegister, pConstantData, Vector4fCount);
  1089. #else
  1090. TOGL_NULL_DEVICE_CHECK;
  1091. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  1092. m_ctx->SetProgramParametersF( kGLMFragmentProgram, StartRegister, (float *)pConstantData, Vector4fCount );
  1093. return S_OK;
  1094. #endif
  1095. }
  1096. HRESULT IDirect3DDevice9::SetVertexShader(IDirect3DVertexShader9* pShader)
  1097. {
  1098. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  1099. return SetVertexShaderNonInline(pShader);
  1100. #else
  1101. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  1102. m_ctx->SetVertexProgram( pShader ? pShader->m_vtxProgram : NULL );
  1103. m_vertexShader = pShader;
  1104. return S_OK;
  1105. #endif
  1106. }
  1107. FORCEINLINE HRESULT TOGLMETHODCALLTYPE IDirect3DDevice9::SetPixelShader(IDirect3DPixelShader9* pShader)
  1108. {
  1109. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  1110. return SetPixelShaderNonInline(pShader);
  1111. #else
  1112. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  1113. m_ctx->SetFragmentProgram( pShader ? pShader->m_pixProgram : NULL );
  1114. m_pixelShader = pShader;
  1115. return S_OK;
  1116. #endif
  1117. }
  1118. FORCEINLINE HRESULT IDirect3DDevice9::SetVertexDeclaration(IDirect3DVertexDeclaration9* pDecl)
  1119. {
  1120. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  1121. return SetVertexDeclarationNonInline(pDecl);
  1122. #else
  1123. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  1124. m_pVertDecl = pDecl;
  1125. return S_OK;
  1126. #endif
  1127. }
  1128. FORCEINLINE void IDirect3DDevice9::SetMaxUsedVertexShaderConstantsHint( uint nMaxReg )
  1129. {
  1130. #if GLMDEBUG || GL_BATCH_PERF_ANALYSIS
  1131. return SetMaxUsedVertexShaderConstantsHintNonInline( nMaxReg );
  1132. #else
  1133. Assert( GetCurrentOwnerThreadId() == ThreadGetCurrentId() );
  1134. m_ctx->SetMaxUsedVertexShaderConstantsHint( nMaxReg );
  1135. #endif
  1136. }
  1137. // ------------------------------------------------------------------------------------------------------------------------------ //
  1138. // D3DX
  1139. // ------------------------------------------------------------------------------------------------------------------------------ //
  1140. struct ID3DXInclude
  1141. {
  1142. virtual HRESULT Open(D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes);
  1143. virtual HRESULT Close(LPCVOID pData);
  1144. };
  1145. typedef ID3DXInclude* LPD3DXINCLUDE;
  1146. struct TOGL_CLASS ID3DXBuffer : public IUnknown
  1147. {
  1148. void* GetBufferPointer();
  1149. DWORD GetBufferSize();
  1150. };
  1151. typedef ID3DXBuffer* LPD3DXBUFFER;
  1152. class ID3DXConstantTable : public IUnknown
  1153. {
  1154. };
  1155. typedef ID3DXConstantTable* LPD3DXCONSTANTTABLE;
  1156. TOGL_INTERFACE const char* D3DXGetPixelShaderProfile( IDirect3DDevice9 *pDevice );
  1157. TOGL_INTERFACE D3DXMATRIX* D3DXMatrixMultiply( D3DXMATRIX *pOut, CONST D3DXMATRIX *pM1, CONST D3DXMATRIX *pM2 );
  1158. TOGL_INTERFACE D3DXVECTOR3* D3DXVec3TransformCoord( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV, CONST D3DXMATRIX *pM );
  1159. TOGL_INTERFACE HRESULT D3DXCreateMatrixStack( DWORD Flags, LPD3DXMATRIXSTACK* ppStack);
  1160. TOGL_INTERFACE void D3DXMatrixIdentity( D3DXMATRIX * );
  1161. TOGL_INTERFACE D3DXINLINE D3DXVECTOR3* D3DXVec3Subtract( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2 )
  1162. {
  1163. pOut->x = pV1->x - pV2->x;
  1164. pOut->y = pV1->y - pV2->y;
  1165. pOut->z = pV1->z - pV2->z;
  1166. return pOut;
  1167. }
  1168. TOGL_INTERFACE D3DXINLINE D3DXVECTOR3* D3DXVec3Cross( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2 )
  1169. {
  1170. D3DXVECTOR3 v;
  1171. v.x = pV1->y * pV2->z - pV1->z * pV2->y;
  1172. v.y = pV1->z * pV2->x - pV1->x * pV2->z;
  1173. v.z = pV1->x * pV2->y - pV1->y * pV2->x;
  1174. *pOut = v;
  1175. return pOut;
  1176. }
  1177. TOGL_INTERFACE D3DXINLINE FLOAT D3DXVec3Dot( CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2 )
  1178. {
  1179. return pV1->x * pV2->x + pV1->y * pV2->y + pV1->z * pV2->z;
  1180. }
  1181. TOGL_INTERFACE D3DXMATRIX* D3DXMatrixInverse( D3DXMATRIX *pOut, FLOAT *pDeterminant, CONST D3DXMATRIX *pM );
  1182. TOGL_INTERFACE D3DXMATRIX* D3DXMatrixTranspose( D3DXMATRIX *pOut, CONST D3DXMATRIX *pM );
  1183. TOGL_INTERFACE D3DXPLANE* D3DXPlaneNormalize( D3DXPLANE *pOut, CONST D3DXPLANE *pP);
  1184. TOGL_INTERFACE D3DXVECTOR4* D3DXVec4Transform( D3DXVECTOR4 *pOut, CONST D3DXVECTOR4 *pV, CONST D3DXMATRIX *pM );
  1185. TOGL_INTERFACE D3DXVECTOR4* D3DXVec4Normalize( D3DXVECTOR4 *pOut, CONST D3DXVECTOR4 *pV );
  1186. TOGL_INTERFACE D3DXMATRIX* D3DXMatrixTranslation( D3DXMATRIX *pOut, FLOAT x, FLOAT y, FLOAT z );
  1187. // Build an ortho projection matrix. (right-handed)
  1188. TOGL_INTERFACE D3DXMATRIX* D3DXMatrixOrthoOffCenterRH( D3DXMATRIX *pOut, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn,FLOAT zf );
  1189. TOGL_INTERFACE D3DXMATRIX* D3DXMatrixPerspectiveRH( D3DXMATRIX *pOut, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf );
  1190. TOGL_INTERFACE D3DXMATRIX* D3DXMatrixPerspectiveOffCenterRH( D3DXMATRIX *pOut, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf );
  1191. // Transform a plane by a matrix. The vector (a,b,c) must be normal.
  1192. // M should be the inverse transpose of the transformation desired.
  1193. TOGL_INTERFACE D3DXPLANE* D3DXPlaneTransform( D3DXPLANE *pOut, CONST D3DXPLANE *pP, CONST D3DXMATRIX *pM );
  1194. TOGL_INTERFACE IDirect3D9 *Direct3DCreate9(UINT SDKVersion);
  1195. TOGL_INTERFACE void D3DPERF_SetOptions( DWORD dwOptions );
  1196. TOGL_INTERFACE HRESULT D3DXCompileShader(
  1197. LPCSTR pSrcData,
  1198. UINT SrcDataLen,
  1199. CONST D3DXMACRO* pDefines,
  1200. LPD3DXINCLUDE pInclude,
  1201. LPCSTR pFunctionName,
  1202. LPCSTR pProfile,
  1203. DWORD Flags,
  1204. LPD3DXBUFFER* ppShader,
  1205. LPD3DXBUFFER* ppErrorMsgs,
  1206. LPD3DXCONSTANTTABLE* ppConstantTable);
  1207. // fake D3D usage constant for SRGB tex creation
  1208. #define D3DUSAGE_TEXTURE_SRGB (0x80000000L)
  1209. #else
  1210. //USE_ACTUAL_DX
  1211. #ifndef WIN32
  1212. #error sorry man
  1213. #endif
  1214. #ifdef _X360
  1215. #include "d3d9.h"
  1216. #include "d3dx9.h"
  1217. #else
  1218. #include <windows.h>
  1219. #include "../../dx9sdk/include/d3d9.h"
  1220. #include "../../dx9sdk/include/d3dx9.h"
  1221. #endif
  1222. typedef HWND VD3DHWND;
  1223. #endif // DX_TO_GL_ABSTRACTION
  1224. #endif // DXABSTRACT_H