renderer_d3d9.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #ifndef BGFX_RENDERER_D3D9_H_HEADER_GUARD
  6. #define BGFX_RENDERER_D3D9_H_HEADER_GUARD
  7. #define BGFX_CONFIG_RENDERER_DIRECT3D9EX BX_PLATFORM_WINDOWS
  8. #if BX_PLATFORM_WINDOWS
  9. # include <sal.h>
  10. # include <d3d9.h>
  11. #endif // BX_PLATFORM_
  12. #ifndef D3DSTREAMSOURCE_INDEXEDDATA
  13. # define D3DSTREAMSOURCE_INDEXEDDATA (1<<30)
  14. #endif// D3DSTREAMSOURCE_INDEXEDDATA
  15. #ifndef D3DSTREAMSOURCE_INSTANCEDATA
  16. # define D3DSTREAMSOURCE_INSTANCEDATA (2<<30)
  17. #endif // D3DSTREAMSOURCE_INSTANCEDATA
  18. #include "renderer.h"
  19. #include "renderer_d3d.h"
  20. #include "nvapi.h"
  21. #define BGFX_D3D9_PROFILER_BEGIN(_view, _abgr) \
  22. BX_MACRO_BLOCK_BEGIN \
  23. PIX_BEGINEVENT(_abgr, s_viewNameW[_view]); \
  24. BGFX_PROFILER_BEGIN(s_viewName[view], _abgr); \
  25. BX_MACRO_BLOCK_END
  26. #define BGFX_D3D9_PROFILER_BEGIN_LITERAL(_name, _abgr) \
  27. BX_MACRO_BLOCK_BEGIN \
  28. PIX_BEGINEVENT(_abgr, L"" # _name); \
  29. BGFX_PROFILER_BEGIN_LITERAL("" # _name, _abgr); \
  30. BX_MACRO_BLOCK_END
  31. #define BGFX_D3D9_PROFILER_END() \
  32. BX_MACRO_BLOCK_BEGIN \
  33. BGFX_PROFILER_END(); \
  34. PIX_ENDEVENT(); \
  35. BX_MACRO_BLOCK_END
  36. namespace bgfx { namespace d3d9
  37. {
  38. # if defined(D3D_DISABLE_9EX)
  39. # define D3DFMT_S8_LOCKABLE D3DFORMAT( 85)
  40. # define D3DFMT_A1 D3DFORMAT(118)
  41. # endif // defined(D3D_DISABLE_9EX)
  42. # ifndef D3DFMT_ATI1
  43. # define D3DFMT_ATI1 ( (D3DFORMAT)BX_MAKEFOURCC('A', 'T', 'I', '1') )
  44. # endif // D3DFMT_ATI1
  45. # ifndef D3DFMT_ATI2
  46. # define D3DFMT_ATI2 ( (D3DFORMAT)BX_MAKEFOURCC('A', 'T', 'I', '2') )
  47. # endif // D3DFMT_ATI2
  48. # ifndef D3DFMT_ATOC
  49. # define D3DFMT_ATOC ( (D3DFORMAT)BX_MAKEFOURCC('A', 'T', 'O', 'C') )
  50. # endif // D3DFMT_ATOC
  51. # ifndef D3DFMT_DF16
  52. # define D3DFMT_DF16 ( (D3DFORMAT)BX_MAKEFOURCC('D', 'F', '1', '6') )
  53. # endif // D3DFMT_DF16
  54. # ifndef D3DFMT_DF24
  55. # define D3DFMT_DF24 ( (D3DFORMAT)BX_MAKEFOURCC('D', 'F', '2', '4') )
  56. # endif // D3DFMT_DF24
  57. # ifndef D3DFMT_INST
  58. # define D3DFMT_INST ( (D3DFORMAT)BX_MAKEFOURCC('I', 'N', 'S', 'T') )
  59. # endif // D3DFMT_INST
  60. # ifndef D3DFMT_INTZ
  61. # define D3DFMT_INTZ ( (D3DFORMAT)BX_MAKEFOURCC('I', 'N', 'T', 'Z') )
  62. # endif // D3DFMT_INTZ
  63. # ifndef D3DFMT_NULL
  64. # define D3DFMT_NULL ( (D3DFORMAT)BX_MAKEFOURCC('N', 'U', 'L', 'L') )
  65. # endif // D3DFMT_NULL
  66. # ifndef D3DFMT_RESZ
  67. # define D3DFMT_RESZ ( (D3DFORMAT)BX_MAKEFOURCC('R', 'E', 'S', 'Z') )
  68. # endif // D3DFMT_RESZ
  69. # ifndef D3DFMT_RAWZ
  70. # define D3DFMT_RAWZ ( (D3DFORMAT)BX_MAKEFOURCC('R', 'A', 'W', 'Z') )
  71. # endif // D3DFMT_RAWZ
  72. # ifndef D3DFMT_S8_LOCKABLE
  73. # define D3DFMT_S8_LOCKABLE ( (D3DFORMAT)85)
  74. # endif // D3DFMT_S8_LOCKABLE
  75. # ifndef D3DFMT_A1
  76. # define D3DFMT_A1 ( (D3DFORMAT)118)
  77. # endif // D3DFMT_A1
  78. struct ExtendedFormat
  79. {
  80. enum Enum
  81. {
  82. Ati1,
  83. Ati2,
  84. Df16,
  85. Df24,
  86. Inst,
  87. Intz,
  88. Null,
  89. Resz,
  90. Rawz,
  91. Atoc,
  92. Count,
  93. };
  94. D3DFORMAT m_fmt;
  95. DWORD m_usage;
  96. D3DRESOURCETYPE m_type;
  97. bool m_supported;
  98. };
  99. struct Msaa
  100. {
  101. D3DMULTISAMPLE_TYPE m_type;
  102. DWORD m_quality;
  103. };
  104. struct IndexBufferD3D9
  105. {
  106. IndexBufferD3D9()
  107. : m_ptr(NULL)
  108. , m_dynamic(NULL)
  109. , m_size(0)
  110. , m_flags(BGFX_BUFFER_NONE)
  111. {
  112. }
  113. void create(uint32_t _size, void* _data, uint16_t _flags);
  114. void update(uint32_t _offset, uint32_t _size, void* _data, bool _discard = false)
  115. {
  116. if (NULL != m_dynamic
  117. && _data != m_dynamic)
  118. {
  119. bx::memCopy(&m_dynamic[_offset], _data, _size);
  120. }
  121. void* buffer;
  122. DX_CHECK(m_ptr->Lock(_offset
  123. , _size
  124. , &buffer
  125. , _discard || (m_dynamic && 0 == _offset && m_size == _size) ? D3DLOCK_DISCARD : 0
  126. ) );
  127. bx::memCopy(buffer, _data, _size);
  128. DX_CHECK(m_ptr->Unlock() );
  129. }
  130. void destroy()
  131. {
  132. if (NULL != m_ptr)
  133. {
  134. DX_RELEASE(m_ptr, 0);
  135. if (NULL != m_dynamic)
  136. {
  137. BX_FREE(g_allocator, m_dynamic);
  138. m_dynamic = NULL;
  139. }
  140. }
  141. }
  142. void preReset();
  143. void postReset();
  144. IDirect3DIndexBuffer9* m_ptr;
  145. uint8_t* m_dynamic;
  146. uint32_t m_size;
  147. uint16_t m_flags;
  148. };
  149. struct VertexBufferD3D9
  150. {
  151. VertexBufferD3D9()
  152. : m_ptr(NULL)
  153. , m_dynamic(NULL)
  154. , m_size(0)
  155. {
  156. }
  157. void create(uint32_t _size, void* _data, VertexLayoutHandle _layoutHandle);
  158. void update(uint32_t _offset, uint32_t _size, void* _data, bool _discard = false)
  159. {
  160. if (NULL != m_dynamic
  161. && _data != m_dynamic)
  162. {
  163. bx::memCopy(&m_dynamic[_offset], _data, _size);
  164. }
  165. void* buffer;
  166. DX_CHECK(m_ptr->Lock(_offset
  167. , _size
  168. , &buffer
  169. , _discard || (m_dynamic && 0 == _offset && m_size == _size) ? D3DLOCK_DISCARD : 0
  170. ) );
  171. bx::memCopy(buffer, _data, _size);
  172. DX_CHECK(m_ptr->Unlock() );
  173. }
  174. void destroy()
  175. {
  176. if (NULL != m_ptr)
  177. {
  178. DX_RELEASE(m_ptr, 0);
  179. if (NULL != m_dynamic)
  180. {
  181. BX_FREE(g_allocator, m_dynamic);
  182. m_dynamic = NULL;
  183. }
  184. }
  185. }
  186. void preReset();
  187. void postReset();
  188. IDirect3DVertexBuffer9* m_ptr;
  189. uint8_t* m_dynamic;
  190. uint32_t m_size;
  191. VertexLayoutHandle m_layoutHandle;
  192. };
  193. struct ShaderD3D9
  194. {
  195. ShaderD3D9()
  196. : m_vertexShader(NULL)
  197. , m_constantBuffer(NULL)
  198. , m_numPredefined(0)
  199. , m_type(0)
  200. {
  201. }
  202. void create(const Memory* _mem);
  203. void destroy()
  204. {
  205. if (NULL != m_constantBuffer)
  206. {
  207. UniformBuffer::destroy(m_constantBuffer);
  208. m_constantBuffer = NULL;
  209. }
  210. m_numPredefined = 0;
  211. switch (m_type)
  212. {
  213. case 0: DX_RELEASE(m_vertexShader, 0); BX_FALLTHROUGH;
  214. default: DX_RELEASE(m_pixelShader, 0);
  215. }
  216. }
  217. union
  218. {
  219. // X360 doesn't have interface inheritance (can't use IUnknown*).
  220. IDirect3DVertexShader9* m_vertexShader;
  221. IDirect3DPixelShader9* m_pixelShader;
  222. };
  223. UniformBuffer* m_constantBuffer;
  224. PredefinedUniform m_predefined[PredefinedUniform::Count];
  225. uint8_t m_numPredefined;
  226. uint8_t m_type;
  227. };
  228. struct ProgramD3D9
  229. {
  230. void create(const ShaderD3D9* _vsh, const ShaderD3D9* _fsh)
  231. {
  232. m_vsh = _vsh;
  233. m_fsh = _fsh;
  234. bx::memCopy(&m_predefined[0], _vsh->m_predefined, _vsh->m_numPredefined*sizeof(PredefinedUniform) );
  235. m_numPredefined = _vsh->m_numPredefined;
  236. if (NULL != _fsh)
  237. {
  238. bx::memCopy(&m_predefined[_vsh->m_numPredefined], _fsh->m_predefined, _fsh->m_numPredefined*sizeof(PredefinedUniform) );
  239. m_numPredefined += _fsh->m_numPredefined;
  240. }
  241. }
  242. void destroy()
  243. {
  244. m_numPredefined = 0;
  245. m_vsh = NULL;
  246. m_fsh = NULL;
  247. }
  248. const ShaderD3D9* m_vsh;
  249. const ShaderD3D9* m_fsh;
  250. PredefinedUniform m_predefined[PredefinedUniform::Count*2];
  251. uint8_t m_numPredefined;
  252. };
  253. struct TextureD3D9
  254. {
  255. enum Enum
  256. {
  257. Texture2D,
  258. Texture3D,
  259. TextureCube,
  260. };
  261. TextureD3D9()
  262. : m_ptr(NULL)
  263. , m_surface(NULL)
  264. , m_staging(NULL)
  265. , m_textureFormat(TextureFormat::Unknown)
  266. {
  267. }
  268. void createTexture(uint32_t _width, uint32_t _height, uint8_t _numMips);
  269. void createVolumeTexture(uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips);
  270. void createCubeTexture(uint32_t _width, uint8_t _numMips);
  271. uint8_t* lock(uint8_t _side, uint8_t _lod, uint32_t& _pitch, uint32_t& _slicePitch, const Rect* _rect = NULL);
  272. void unlock(uint8_t _side, uint8_t _lod);
  273. void dirty(uint8_t _side, const Rect& _rect, uint16_t _z, uint16_t _depth);
  274. IDirect3DSurface9* getSurface(uint8_t _side = 0, uint8_t _mip = 0) const;
  275. void create(const Memory* _mem, uint64_t _flags, uint8_t _skip);
  276. void destroy(bool _resize = false)
  277. {
  278. if (0 == (m_flags & BGFX_SAMPLER_INTERNAL_SHARED) )
  279. {
  280. if (_resize)
  281. {
  282. // BK - at the time of resize there might be one reference held by frame buffer
  283. // surface. This frame buffer will be recreated later, and release reference
  284. // to existing surface. That's why here we don't care about ref count.
  285. m_ptr->Release();
  286. }
  287. else
  288. {
  289. DX_RELEASE(m_ptr, 0);
  290. }
  291. }
  292. DX_RELEASE(m_surface, 0);
  293. DX_RELEASE(m_staging, 0);
  294. m_textureFormat = TextureFormat::Unknown;
  295. }
  296. void overrideInternal(uintptr_t _ptr)
  297. {
  298. destroy();
  299. m_flags |= BGFX_SAMPLER_INTERNAL_SHARED;
  300. m_ptr = (IDirect3DBaseTexture9*)_ptr;
  301. }
  302. void updateBegin(uint8_t _side, uint8_t _mip);
  303. void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem);
  304. void updateEnd();
  305. void commit(uint8_t _stage, uint32_t _flags, const float _palette[][4]);
  306. void resolve(uint8_t _resolve) const;
  307. void preReset();
  308. void postReset();
  309. union
  310. {
  311. IDirect3DBaseTexture9* m_ptr;
  312. IDirect3DTexture9* m_texture2d;
  313. IDirect3DVolumeTexture9* m_texture3d;
  314. IDirect3DCubeTexture9* m_textureCube;
  315. };
  316. IDirect3DSurface9* m_surface;
  317. union
  318. {
  319. IDirect3DBaseTexture9* m_staging;
  320. IDirect3DTexture9* m_staging2d;
  321. IDirect3DVolumeTexture9* m_staging3d;
  322. IDirect3DCubeTexture9* m_stagingCube;
  323. };
  324. uint64_t m_flags;
  325. uint32_t m_width;
  326. uint32_t m_height;
  327. uint32_t m_depth;
  328. uint8_t m_numMips;
  329. uint8_t m_type;
  330. uint8_t m_requestedFormat;
  331. uint8_t m_textureFormat;
  332. };
  333. struct FrameBufferD3D9
  334. {
  335. FrameBufferD3D9()
  336. : m_hwnd(NULL)
  337. , m_denseIdx(UINT16_MAX)
  338. , m_num(0)
  339. , m_numTh(0)
  340. , m_dsIdx(UINT8_MAX)
  341. , m_needResolve(false)
  342. , m_needPresent(false)
  343. {
  344. }
  345. void create(uint8_t _num, const Attachment* _attachment);
  346. void create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, TextureFormat::Enum _depthFormat);
  347. uint16_t destroy();
  348. HRESULT present();
  349. void resolve() const;
  350. void preReset();
  351. void postReset();
  352. void createNullColorRT();
  353. void set();
  354. IDirect3DSurface9* m_surface[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS-1];
  355. IDirect3DSwapChain9* m_swapChain;
  356. HWND m_hwnd;
  357. uint32_t m_width;
  358. uint32_t m_height;
  359. Attachment m_attachment[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
  360. uint16_t m_denseIdx;
  361. uint8_t m_num;
  362. uint8_t m_numTh;
  363. uint8_t m_dsIdx;
  364. bool m_needResolve;
  365. bool m_needPresent;
  366. };
  367. struct TimerQueryD3D9
  368. {
  369. TimerQueryD3D9()
  370. : m_control(BX_COUNTOF(m_query) )
  371. {
  372. }
  373. void postReset();
  374. void preReset();
  375. uint32_t begin(uint32_t _resultIdx);
  376. void end(uint32_t _idx);
  377. bool update();
  378. struct Query
  379. {
  380. IDirect3DQuery9* m_disjoint;
  381. IDirect3DQuery9* m_begin;
  382. IDirect3DQuery9* m_end;
  383. IDirect3DQuery9* m_freq;
  384. uint32_t m_resultIdx;
  385. bool m_ready;
  386. };
  387. struct Result
  388. {
  389. void reset()
  390. {
  391. m_begin = 0;
  392. m_end = 0;
  393. m_frequency = 1;
  394. m_pending = 0;
  395. }
  396. uint64_t m_begin;
  397. uint64_t m_end;
  398. uint64_t m_frequency;
  399. uint32_t m_pending;
  400. };
  401. Result m_result[BGFX_CONFIG_MAX_VIEWS+1];
  402. Query m_query[BGFX_CONFIG_MAX_VIEWS*4];
  403. bx::RingBufferControl m_control;
  404. };
  405. struct OcclusionQueryD3D9
  406. {
  407. OcclusionQueryD3D9()
  408. : m_control(BX_COUNTOF(m_query) )
  409. {
  410. }
  411. void postReset();
  412. void preReset();
  413. void begin(Frame* _render, OcclusionQueryHandle _handle);
  414. void end();
  415. void resolve(Frame* _render, bool _wait = false);
  416. void invalidate(OcclusionQueryHandle _handle);
  417. struct Query
  418. {
  419. IDirect3DQuery9* m_ptr;
  420. OcclusionQueryHandle m_handle;
  421. };
  422. Query m_query[BGFX_CONFIG_MAX_OCCLUSION_QUERIES];
  423. bx::RingBufferControl m_control;
  424. };
  425. } /* namespace d3d9 */ } // namespace bgfx
  426. #endif // BGFX_RENDERER_D3D9_H_HEADER_GUARD