d3d11-subsystem.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <util/AlignedNew.hpp>
  16. #include <util/windows/win-version.h>
  17. #include <vector>
  18. #include <string>
  19. #include <memory>
  20. #include <windows.h>
  21. #include <dxgi.h>
  22. #include <dxgi1_2.h>
  23. #include <d3d11_1.h>
  24. #include <d3dcompiler.h>
  25. #include <util/base.h>
  26. #include <graphics/matrix4.h>
  27. #include <graphics/graphics.h>
  28. #include <graphics/device-exports.h>
  29. #include <util/windows/ComPtr.hpp>
  30. #include <util/windows/HRError.hpp>
  31. // #define DISASSEMBLE_SHADERS
  32. struct shader_var;
  33. struct shader_sampler;
  34. struct gs_vertex_shader;
  35. using namespace std;
  36. /*
  37. * Just to clarify, all structs, and all public. These are exporting only
  38. * via encapsulated C bindings, not C++ bindings, so the whole concept of
  39. * "public" and "private" does not matter at all for this subproject.
  40. */
  41. static inline uint32_t GetWinVer()
  42. {
  43. struct win_version_info ver;
  44. get_win_ver(&ver);
  45. return (ver.major << 8) | ver.minor;
  46. }
  47. static inline DXGI_FORMAT ConvertGSTextureFormat(gs_color_format format)
  48. {
  49. switch (format) {
  50. case GS_UNKNOWN:
  51. return DXGI_FORMAT_UNKNOWN;
  52. case GS_A8:
  53. return DXGI_FORMAT_A8_UNORM;
  54. case GS_R8:
  55. return DXGI_FORMAT_R8_UNORM;
  56. case GS_RGBA:
  57. return DXGI_FORMAT_R8G8B8A8_UNORM;
  58. case GS_BGRX:
  59. return DXGI_FORMAT_B8G8R8X8_UNORM;
  60. case GS_BGRA:
  61. return DXGI_FORMAT_B8G8R8A8_UNORM;
  62. case GS_R10G10B10A2:
  63. return DXGI_FORMAT_R10G10B10A2_UNORM;
  64. case GS_RGBA16:
  65. return DXGI_FORMAT_R16G16B16A16_UNORM;
  66. case GS_R16:
  67. return DXGI_FORMAT_R16_UNORM;
  68. case GS_RGBA16F:
  69. return DXGI_FORMAT_R16G16B16A16_FLOAT;
  70. case GS_RGBA32F:
  71. return DXGI_FORMAT_R32G32B32A32_FLOAT;
  72. case GS_RG16F:
  73. return DXGI_FORMAT_R16G16_FLOAT;
  74. case GS_RG32F:
  75. return DXGI_FORMAT_R32G32_FLOAT;
  76. case GS_R16F:
  77. return DXGI_FORMAT_R16_FLOAT;
  78. case GS_R32F:
  79. return DXGI_FORMAT_R32_FLOAT;
  80. case GS_DXT1:
  81. return DXGI_FORMAT_BC1_UNORM;
  82. case GS_DXT3:
  83. return DXGI_FORMAT_BC2_UNORM;
  84. case GS_DXT5:
  85. return DXGI_FORMAT_BC3_UNORM;
  86. case GS_R8G8:
  87. return DXGI_FORMAT_R8G8_UNORM;
  88. }
  89. return DXGI_FORMAT_UNKNOWN;
  90. }
  91. static inline gs_color_format ConvertDXGITextureFormat(DXGI_FORMAT format)
  92. {
  93. switch ((unsigned long)format) {
  94. case DXGI_FORMAT_A8_UNORM:
  95. return GS_A8;
  96. case DXGI_FORMAT_R8_UNORM:
  97. return GS_R8;
  98. case DXGI_FORMAT_R8G8_UNORM:
  99. return GS_R8G8;
  100. case DXGI_FORMAT_R8G8B8A8_UNORM:
  101. return GS_RGBA;
  102. case DXGI_FORMAT_B8G8R8X8_UNORM:
  103. return GS_BGRX;
  104. case DXGI_FORMAT_B8G8R8A8_UNORM:
  105. return GS_BGRA;
  106. case DXGI_FORMAT_R10G10B10A2_UNORM:
  107. return GS_R10G10B10A2;
  108. case DXGI_FORMAT_R16G16B16A16_UNORM:
  109. return GS_RGBA16;
  110. case DXGI_FORMAT_R16_UNORM:
  111. return GS_R16;
  112. case DXGI_FORMAT_R16G16B16A16_FLOAT:
  113. return GS_RGBA16F;
  114. case DXGI_FORMAT_R32G32B32A32_FLOAT:
  115. return GS_RGBA32F;
  116. case DXGI_FORMAT_R16G16_FLOAT:
  117. return GS_RG16F;
  118. case DXGI_FORMAT_R32G32_FLOAT:
  119. return GS_RG32F;
  120. case DXGI_FORMAT_R16_FLOAT:
  121. return GS_R16F;
  122. case DXGI_FORMAT_R32_FLOAT:
  123. return GS_R32F;
  124. case DXGI_FORMAT_BC1_UNORM:
  125. return GS_DXT1;
  126. case DXGI_FORMAT_BC2_UNORM:
  127. return GS_DXT3;
  128. case DXGI_FORMAT_BC3_UNORM:
  129. return GS_DXT5;
  130. }
  131. return GS_UNKNOWN;
  132. }
  133. static inline DXGI_FORMAT ConvertGSZStencilFormat(gs_zstencil_format format)
  134. {
  135. switch (format) {
  136. case GS_ZS_NONE:
  137. return DXGI_FORMAT_UNKNOWN;
  138. case GS_Z16:
  139. return DXGI_FORMAT_D16_UNORM;
  140. case GS_Z24_S8:
  141. return DXGI_FORMAT_D24_UNORM_S8_UINT;
  142. case GS_Z32F:
  143. return DXGI_FORMAT_D32_FLOAT;
  144. case GS_Z32F_S8X24:
  145. return DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
  146. }
  147. return DXGI_FORMAT_UNKNOWN;
  148. }
  149. static inline D3D11_COMPARISON_FUNC ConvertGSDepthTest(gs_depth_test test)
  150. {
  151. switch (test) {
  152. case GS_NEVER:
  153. return D3D11_COMPARISON_NEVER;
  154. case GS_LESS:
  155. return D3D11_COMPARISON_LESS;
  156. case GS_LEQUAL:
  157. return D3D11_COMPARISON_LESS_EQUAL;
  158. case GS_EQUAL:
  159. return D3D11_COMPARISON_EQUAL;
  160. case GS_GEQUAL:
  161. return D3D11_COMPARISON_GREATER_EQUAL;
  162. case GS_GREATER:
  163. return D3D11_COMPARISON_GREATER;
  164. case GS_NOTEQUAL:
  165. return D3D11_COMPARISON_NOT_EQUAL;
  166. case GS_ALWAYS:
  167. return D3D11_COMPARISON_ALWAYS;
  168. }
  169. return D3D11_COMPARISON_NEVER;
  170. }
  171. static inline D3D11_STENCIL_OP ConvertGSStencilOp(gs_stencil_op_type op)
  172. {
  173. switch (op) {
  174. case GS_KEEP:
  175. return D3D11_STENCIL_OP_KEEP;
  176. case GS_ZERO:
  177. return D3D11_STENCIL_OP_ZERO;
  178. case GS_REPLACE:
  179. return D3D11_STENCIL_OP_REPLACE;
  180. case GS_INCR:
  181. return D3D11_STENCIL_OP_INCR;
  182. case GS_DECR:
  183. return D3D11_STENCIL_OP_DECR;
  184. case GS_INVERT:
  185. return D3D11_STENCIL_OP_INVERT;
  186. }
  187. return D3D11_STENCIL_OP_KEEP;
  188. }
  189. static inline D3D11_BLEND ConvertGSBlendType(gs_blend_type type)
  190. {
  191. switch (type) {
  192. case GS_BLEND_ZERO:
  193. return D3D11_BLEND_ZERO;
  194. case GS_BLEND_ONE:
  195. return D3D11_BLEND_ONE;
  196. case GS_BLEND_SRCCOLOR:
  197. return D3D11_BLEND_SRC_COLOR;
  198. case GS_BLEND_INVSRCCOLOR:
  199. return D3D11_BLEND_INV_SRC_COLOR;
  200. case GS_BLEND_SRCALPHA:
  201. return D3D11_BLEND_SRC_ALPHA;
  202. case GS_BLEND_INVSRCALPHA:
  203. return D3D11_BLEND_INV_SRC_ALPHA;
  204. case GS_BLEND_DSTCOLOR:
  205. return D3D11_BLEND_DEST_COLOR;
  206. case GS_BLEND_INVDSTCOLOR:
  207. return D3D11_BLEND_INV_DEST_COLOR;
  208. case GS_BLEND_DSTALPHA:
  209. return D3D11_BLEND_DEST_ALPHA;
  210. case GS_BLEND_INVDSTALPHA:
  211. return D3D11_BLEND_INV_DEST_ALPHA;
  212. case GS_BLEND_SRCALPHASAT:
  213. return D3D11_BLEND_SRC_ALPHA_SAT;
  214. }
  215. return D3D11_BLEND_ONE;
  216. }
  217. static inline D3D11_CULL_MODE ConvertGSCullMode(gs_cull_mode mode)
  218. {
  219. switch (mode) {
  220. case GS_BACK:
  221. return D3D11_CULL_BACK;
  222. case GS_FRONT:
  223. return D3D11_CULL_FRONT;
  224. case GS_NEITHER:
  225. return D3D11_CULL_NONE;
  226. }
  227. return D3D11_CULL_BACK;
  228. }
  229. static inline D3D11_PRIMITIVE_TOPOLOGY ConvertGSTopology(gs_draw_mode mode)
  230. {
  231. switch (mode) {
  232. case GS_POINTS:
  233. return D3D11_PRIMITIVE_TOPOLOGY_POINTLIST;
  234. case GS_LINES:
  235. return D3D11_PRIMITIVE_TOPOLOGY_LINELIST;
  236. case GS_LINESTRIP:
  237. return D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP;
  238. case GS_TRIS:
  239. return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
  240. case GS_TRISTRIP:
  241. return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
  242. }
  243. return D3D11_PRIMITIVE_TOPOLOGY_POINTLIST;
  244. }
  245. /* exception-safe RAII wrapper for vertex buffer data (NOTE: not copy-safe) */
  246. struct VBDataPtr {
  247. gs_vb_data *data;
  248. inline VBDataPtr(gs_vb_data *data) : data(data) {}
  249. inline ~VBDataPtr() { gs_vbdata_destroy(data); }
  250. };
  251. enum class gs_type {
  252. gs_vertex_buffer,
  253. gs_index_buffer,
  254. gs_texture_2d,
  255. gs_zstencil_buffer,
  256. gs_stage_surface,
  257. gs_sampler_state,
  258. gs_vertex_shader,
  259. gs_pixel_shader,
  260. gs_duplicator,
  261. gs_swap_chain,
  262. gs_timer,
  263. gs_timer_range,
  264. gs_texture_3d,
  265. };
  266. struct gs_obj {
  267. gs_device_t *device;
  268. gs_type obj_type;
  269. gs_obj *next;
  270. gs_obj **prev_next;
  271. inline gs_obj() : device(nullptr), next(nullptr), prev_next(nullptr) {}
  272. gs_obj(gs_device_t *device, gs_type type);
  273. virtual ~gs_obj();
  274. };
  275. struct gs_vertex_buffer : gs_obj {
  276. ComPtr<ID3D11Buffer> vertexBuffer;
  277. ComPtr<ID3D11Buffer> normalBuffer;
  278. ComPtr<ID3D11Buffer> colorBuffer;
  279. ComPtr<ID3D11Buffer> tangentBuffer;
  280. vector<ComPtr<ID3D11Buffer>> uvBuffers;
  281. bool dynamic;
  282. VBDataPtr vbd;
  283. size_t numVerts;
  284. vector<size_t> uvSizes;
  285. void FlushBuffer(ID3D11Buffer *buffer, void *array, size_t elementSize);
  286. void MakeBufferList(gs_vertex_shader *shader,
  287. vector<ID3D11Buffer *> &buffers,
  288. vector<uint32_t> &strides);
  289. void InitBuffer(const size_t elementSize, const size_t numVerts,
  290. void *array, ID3D11Buffer **buffer);
  291. void BuildBuffers();
  292. inline void Release()
  293. {
  294. vertexBuffer.Release();
  295. normalBuffer.Release();
  296. colorBuffer.Release();
  297. tangentBuffer.Release();
  298. uvBuffers.clear();
  299. }
  300. void Rebuild();
  301. gs_vertex_buffer(gs_device_t *device, struct gs_vb_data *data,
  302. uint32_t flags);
  303. };
  304. /* exception-safe RAII wrapper for index buffer data (NOTE: not copy-safe) */
  305. struct DataPtr {
  306. void *data;
  307. inline DataPtr(void *data) : data(data) {}
  308. inline ~DataPtr() { bfree(data); }
  309. };
  310. struct gs_index_buffer : gs_obj {
  311. ComPtr<ID3D11Buffer> indexBuffer;
  312. bool dynamic;
  313. gs_index_type type;
  314. size_t indexSize;
  315. size_t num;
  316. DataPtr indices;
  317. D3D11_BUFFER_DESC bd = {};
  318. D3D11_SUBRESOURCE_DATA srd = {};
  319. void InitBuffer();
  320. void Rebuild(ID3D11Device *dev);
  321. inline void Release() { indexBuffer.Release(); }
  322. gs_index_buffer(gs_device_t *device, enum gs_index_type type,
  323. void *indices, size_t num, uint32_t flags);
  324. };
  325. struct gs_timer : gs_obj {
  326. ComPtr<ID3D11Query> query_begin;
  327. ComPtr<ID3D11Query> query_end;
  328. void Rebuild(ID3D11Device *dev);
  329. inline void Release()
  330. {
  331. query_begin.Release();
  332. query_end.Release();
  333. }
  334. gs_timer(gs_device_t *device);
  335. };
  336. struct gs_timer_range : gs_obj {
  337. ComPtr<ID3D11Query> query_disjoint;
  338. void Rebuild(ID3D11Device *dev);
  339. inline void Release() { query_disjoint.Release(); }
  340. gs_timer_range(gs_device_t *device);
  341. };
  342. struct gs_texture : gs_obj {
  343. gs_texture_type type;
  344. uint32_t levels;
  345. gs_color_format format;
  346. ComPtr<ID3D11ShaderResourceView> shaderRes;
  347. D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesc = {};
  348. void Rebuild(ID3D11Device *dev);
  349. inline gs_texture(gs_texture_type type, uint32_t levels,
  350. gs_color_format format)
  351. : type(type), levels(levels), format(format)
  352. {
  353. }
  354. inline gs_texture(gs_device *device, gs_type obj_type,
  355. gs_texture_type type)
  356. : gs_obj(device, obj_type), type(type)
  357. {
  358. }
  359. inline gs_texture(gs_device *device, gs_type obj_type,
  360. gs_texture_type type, uint32_t levels,
  361. gs_color_format format)
  362. : gs_obj(device, obj_type),
  363. type(type),
  364. levels(levels),
  365. format(format)
  366. {
  367. }
  368. };
  369. struct gs_texture_2d : gs_texture {
  370. ComPtr<ID3D11Texture2D> texture;
  371. ComPtr<ID3D11RenderTargetView> renderTarget[6];
  372. ComPtr<IDXGISurface1> gdiSurface;
  373. uint32_t width = 0, height = 0;
  374. uint32_t flags = 0;
  375. DXGI_FORMAT dxgiFormat = DXGI_FORMAT_UNKNOWN;
  376. bool isRenderTarget = false;
  377. bool isGDICompatible = false;
  378. bool isDynamic = false;
  379. bool isShared = false;
  380. bool genMipmaps = false;
  381. uint32_t sharedHandle = GS_INVALID_HANDLE;
  382. gs_texture_2d *pairedNV12texture = nullptr;
  383. bool nv12 = false;
  384. bool chroma = false;
  385. bool acquired = false;
  386. vector<vector<uint8_t>> data;
  387. vector<D3D11_SUBRESOURCE_DATA> srd;
  388. D3D11_TEXTURE2D_DESC td = {};
  389. void InitSRD(vector<D3D11_SUBRESOURCE_DATA> &srd);
  390. void InitTexture(const uint8_t *const *data);
  391. void InitResourceView();
  392. void InitRenderTargets();
  393. void BackupTexture(const uint8_t *const *data);
  394. void GetSharedHandle(IDXGIResource *dxgi_res);
  395. void RebuildSharedTextureFallback();
  396. void Rebuild(ID3D11Device *dev);
  397. void RebuildNV12_Y(ID3D11Device *dev);
  398. void RebuildNV12_UV(ID3D11Device *dev);
  399. inline void Release()
  400. {
  401. texture.Release();
  402. for (auto &rt : renderTarget)
  403. rt.Release();
  404. gdiSurface.Release();
  405. shaderRes.Release();
  406. }
  407. inline gs_texture_2d() : gs_texture(GS_TEXTURE_2D, 0, GS_UNKNOWN) {}
  408. gs_texture_2d(gs_device_t *device, uint32_t width, uint32_t height,
  409. gs_color_format colorFormat, uint32_t levels,
  410. const uint8_t *const *data, uint32_t flags,
  411. gs_texture_type type, bool gdiCompatible,
  412. bool nv12 = false);
  413. gs_texture_2d(gs_device_t *device, ID3D11Texture2D *nv12,
  414. uint32_t flags);
  415. gs_texture_2d(gs_device_t *device, uint32_t handle);
  416. };
  417. struct gs_texture_3d : gs_texture {
  418. ComPtr<ID3D11Texture3D> texture;
  419. uint32_t width = 0, height = 0, depth = 0;
  420. uint32_t flags = 0;
  421. DXGI_FORMAT dxgiFormat = DXGI_FORMAT_UNKNOWN;
  422. bool isDynamic = false;
  423. bool isShared = false;
  424. bool genMipmaps = false;
  425. uint32_t sharedHandle = GS_INVALID_HANDLE;
  426. bool chroma = false;
  427. bool acquired = false;
  428. vector<vector<uint8_t>> data;
  429. vector<D3D11_SUBRESOURCE_DATA> srd;
  430. D3D11_TEXTURE3D_DESC td = {};
  431. void InitSRD(vector<D3D11_SUBRESOURCE_DATA> &srd);
  432. void InitTexture(const uint8_t *const *data);
  433. void InitResourceView();
  434. void BackupTexture(const uint8_t *const *data);
  435. void GetSharedHandle(IDXGIResource *dxgi_res);
  436. void RebuildSharedTextureFallback();
  437. void Rebuild(ID3D11Device *dev);
  438. void RebuildNV12_Y(ID3D11Device *dev);
  439. void RebuildNV12_UV(ID3D11Device *dev);
  440. inline void Release()
  441. {
  442. texture.Release();
  443. shaderRes.Release();
  444. }
  445. inline gs_texture_3d() : gs_texture(GS_TEXTURE_3D, 0, GS_UNKNOWN) {}
  446. gs_texture_3d(gs_device_t *device, uint32_t width, uint32_t height,
  447. uint32_t depth, gs_color_format colorFormat,
  448. uint32_t levels, const uint8_t *const *data,
  449. uint32_t flags);
  450. gs_texture_3d(gs_device_t *device, uint32_t handle);
  451. };
  452. struct gs_zstencil_buffer : gs_obj {
  453. ComPtr<ID3D11Texture2D> texture;
  454. ComPtr<ID3D11DepthStencilView> view;
  455. uint32_t width, height;
  456. gs_zstencil_format format;
  457. DXGI_FORMAT dxgiFormat;
  458. D3D11_TEXTURE2D_DESC td = {};
  459. D3D11_DEPTH_STENCIL_VIEW_DESC dsvd = {};
  460. void InitBuffer();
  461. void Rebuild(ID3D11Device *dev);
  462. inline void Release()
  463. {
  464. texture.Release();
  465. view.Release();
  466. }
  467. inline gs_zstencil_buffer()
  468. : width(0), height(0), dxgiFormat(DXGI_FORMAT_UNKNOWN)
  469. {
  470. }
  471. gs_zstencil_buffer(gs_device_t *device, uint32_t width, uint32_t height,
  472. gs_zstencil_format format);
  473. };
  474. struct gs_stage_surface : gs_obj {
  475. ComPtr<ID3D11Texture2D> texture;
  476. D3D11_TEXTURE2D_DESC td = {};
  477. uint32_t width, height;
  478. gs_color_format format;
  479. DXGI_FORMAT dxgiFormat;
  480. void Rebuild(ID3D11Device *dev);
  481. inline void Release() { texture.Release(); }
  482. gs_stage_surface(gs_device_t *device, uint32_t width, uint32_t height,
  483. gs_color_format colorFormat);
  484. gs_stage_surface(gs_device_t *device, uint32_t width, uint32_t height);
  485. };
  486. struct gs_sampler_state : gs_obj {
  487. ComPtr<ID3D11SamplerState> state;
  488. D3D11_SAMPLER_DESC sd = {};
  489. gs_sampler_info info;
  490. void Rebuild(ID3D11Device *dev);
  491. inline void Release() { state.Release(); }
  492. gs_sampler_state(gs_device_t *device, const gs_sampler_info *info);
  493. };
  494. struct gs_shader_param {
  495. string name;
  496. gs_shader_param_type type;
  497. uint32_t textureID;
  498. struct gs_sampler_state *nextSampler = nullptr;
  499. int arrayCount;
  500. size_t pos;
  501. vector<uint8_t> curValue;
  502. vector<uint8_t> defaultValue;
  503. bool changed;
  504. gs_shader_param(shader_var &var, uint32_t &texCounter);
  505. };
  506. struct ShaderError {
  507. ComPtr<ID3D10Blob> errors;
  508. HRESULT hr;
  509. inline ShaderError(const ComPtr<ID3D10Blob> &errors, HRESULT hr)
  510. : errors(errors), hr(hr)
  511. {
  512. }
  513. };
  514. struct gs_shader : gs_obj {
  515. gs_shader_type type;
  516. vector<gs_shader_param> params;
  517. ComPtr<ID3D11Buffer> constants;
  518. size_t constantSize;
  519. D3D11_BUFFER_DESC bd = {};
  520. vector<uint8_t> data;
  521. inline void UpdateParam(vector<uint8_t> &constData,
  522. gs_shader_param &param, bool &upload);
  523. void UploadParams();
  524. void BuildConstantBuffer();
  525. void Compile(const char *shaderStr, const char *file,
  526. const char *target, ID3D10Blob **shader);
  527. inline gs_shader(gs_device_t *device, gs_type obj_type,
  528. gs_shader_type type)
  529. : gs_obj(device, obj_type), type(type), constantSize(0)
  530. {
  531. }
  532. virtual ~gs_shader() {}
  533. };
  534. struct ShaderSampler {
  535. string name;
  536. gs_sampler_state sampler;
  537. inline ShaderSampler(const char *name, gs_device_t *device,
  538. gs_sampler_info *info)
  539. : name(name), sampler(device, info)
  540. {
  541. }
  542. };
  543. struct gs_vertex_shader : gs_shader {
  544. ComPtr<ID3D11VertexShader> shader;
  545. ComPtr<ID3D11InputLayout> layout;
  546. gs_shader_param *world, *viewProj;
  547. vector<D3D11_INPUT_ELEMENT_DESC> layoutData;
  548. bool hasNormals;
  549. bool hasColors;
  550. bool hasTangents;
  551. uint32_t nTexUnits;
  552. void Rebuild(ID3D11Device *dev);
  553. inline void Release()
  554. {
  555. shader.Release();
  556. layout.Release();
  557. constants.Release();
  558. }
  559. inline uint32_t NumBuffersExpected() const
  560. {
  561. uint32_t count = nTexUnits + 1;
  562. if (hasNormals)
  563. count++;
  564. if (hasColors)
  565. count++;
  566. if (hasTangents)
  567. count++;
  568. return count;
  569. }
  570. void GetBuffersExpected(const vector<D3D11_INPUT_ELEMENT_DESC> &inputs);
  571. gs_vertex_shader(gs_device_t *device, const char *file,
  572. const char *shaderString);
  573. };
  574. struct gs_duplicator : gs_obj {
  575. ComPtr<IDXGIOutputDuplication> duplicator;
  576. gs_texture_2d *texture;
  577. int idx;
  578. long refs;
  579. bool updated;
  580. void Start();
  581. inline void Release() { duplicator.Release(); }
  582. gs_duplicator(gs_device_t *device, int monitor_idx);
  583. ~gs_duplicator();
  584. };
  585. struct gs_pixel_shader : gs_shader {
  586. ComPtr<ID3D11PixelShader> shader;
  587. vector<unique_ptr<ShaderSampler>> samplers;
  588. void Rebuild(ID3D11Device *dev);
  589. inline void Release()
  590. {
  591. shader.Release();
  592. constants.Release();
  593. }
  594. inline void GetSamplerStates(ID3D11SamplerState **states)
  595. {
  596. size_t i;
  597. for (i = 0; i < samplers.size(); i++)
  598. states[i] = samplers[i]->sampler.state;
  599. for (; i < GS_MAX_TEXTURES; i++)
  600. states[i] = NULL;
  601. }
  602. gs_pixel_shader(gs_device_t *device, const char *file,
  603. const char *shaderString);
  604. };
  605. struct gs_swap_chain : gs_obj {
  606. uint32_t numBuffers;
  607. HWND hwnd;
  608. gs_init_data initData;
  609. DXGI_SWAP_CHAIN_DESC swapDesc = {};
  610. gs_texture_2d target;
  611. gs_zstencil_buffer zs;
  612. ComPtr<IDXGISwapChain> swap;
  613. void InitTarget(uint32_t cx, uint32_t cy);
  614. void InitZStencilBuffer(uint32_t cx, uint32_t cy);
  615. void Resize(uint32_t cx, uint32_t cy);
  616. void Init();
  617. void Rebuild(ID3D11Device *dev);
  618. inline void Release()
  619. {
  620. target.Release();
  621. zs.Release();
  622. swap.Release();
  623. }
  624. gs_swap_chain(gs_device *device, const gs_init_data *data);
  625. };
  626. struct BlendState {
  627. bool blendEnabled;
  628. gs_blend_type srcFactorC;
  629. gs_blend_type destFactorC;
  630. gs_blend_type srcFactorA;
  631. gs_blend_type destFactorA;
  632. bool redEnabled;
  633. bool greenEnabled;
  634. bool blueEnabled;
  635. bool alphaEnabled;
  636. inline BlendState()
  637. : blendEnabled(true),
  638. srcFactorC(GS_BLEND_SRCALPHA),
  639. destFactorC(GS_BLEND_INVSRCALPHA),
  640. srcFactorA(GS_BLEND_ONE),
  641. destFactorA(GS_BLEND_INVSRCALPHA),
  642. redEnabled(true),
  643. greenEnabled(true),
  644. blueEnabled(true),
  645. alphaEnabled(true)
  646. {
  647. }
  648. inline BlendState(const BlendState &state)
  649. {
  650. memcpy(this, &state, sizeof(BlendState));
  651. }
  652. };
  653. struct SavedBlendState : BlendState {
  654. ComPtr<ID3D11BlendState> state;
  655. D3D11_BLEND_DESC bd;
  656. void Rebuild(ID3D11Device *dev);
  657. inline void Release() { state.Release(); }
  658. inline SavedBlendState(const BlendState &val, D3D11_BLEND_DESC &desc)
  659. : BlendState(val), bd(desc)
  660. {
  661. }
  662. };
  663. struct StencilSide {
  664. gs_depth_test test;
  665. gs_stencil_op_type fail;
  666. gs_stencil_op_type zfail;
  667. gs_stencil_op_type zpass;
  668. inline StencilSide()
  669. : test(GS_ALWAYS), fail(GS_KEEP), zfail(GS_KEEP), zpass(GS_KEEP)
  670. {
  671. }
  672. };
  673. struct ZStencilState {
  674. bool depthEnabled;
  675. bool depthWriteEnabled;
  676. gs_depth_test depthFunc;
  677. bool stencilEnabled;
  678. bool stencilWriteEnabled;
  679. StencilSide stencilFront;
  680. StencilSide stencilBack;
  681. inline ZStencilState()
  682. : depthEnabled(true),
  683. depthWriteEnabled(true),
  684. depthFunc(GS_LESS),
  685. stencilEnabled(false),
  686. stencilWriteEnabled(true)
  687. {
  688. }
  689. inline ZStencilState(const ZStencilState &state)
  690. {
  691. memcpy(this, &state, sizeof(ZStencilState));
  692. }
  693. };
  694. struct SavedZStencilState : ZStencilState {
  695. ComPtr<ID3D11DepthStencilState> state;
  696. D3D11_DEPTH_STENCIL_DESC dsd;
  697. void Rebuild(ID3D11Device *dev);
  698. inline void Release() { state.Release(); }
  699. inline SavedZStencilState(const ZStencilState &val,
  700. D3D11_DEPTH_STENCIL_DESC desc)
  701. : ZStencilState(val), dsd(desc)
  702. {
  703. }
  704. };
  705. struct RasterState {
  706. gs_cull_mode cullMode;
  707. bool scissorEnabled;
  708. inline RasterState() : cullMode(GS_BACK), scissorEnabled(false) {}
  709. inline RasterState(const RasterState &state)
  710. {
  711. memcpy(this, &state, sizeof(RasterState));
  712. }
  713. };
  714. struct SavedRasterState : RasterState {
  715. ComPtr<ID3D11RasterizerState> state;
  716. D3D11_RASTERIZER_DESC rd;
  717. void Rebuild(ID3D11Device *dev);
  718. inline void Release() { state.Release(); }
  719. inline SavedRasterState(const RasterState &val,
  720. D3D11_RASTERIZER_DESC &desc)
  721. : RasterState(val), rd(desc)
  722. {
  723. }
  724. };
  725. struct mat4float {
  726. float mat[16];
  727. };
  728. struct gs_device {
  729. ComPtr<IDXGIFactory1> factory;
  730. ComPtr<IDXGIAdapter1> adapter;
  731. ComPtr<ID3D11Device> device;
  732. ComPtr<ID3D11DeviceContext> context;
  733. uint32_t adpIdx = 0;
  734. bool nv12Supported = false;
  735. gs_texture_2d *curRenderTarget = nullptr;
  736. gs_zstencil_buffer *curZStencilBuffer = nullptr;
  737. int curRenderSide = 0;
  738. gs_texture *curTextures[GS_MAX_TEXTURES];
  739. gs_sampler_state *curSamplers[GS_MAX_TEXTURES];
  740. gs_vertex_buffer *curVertexBuffer = nullptr;
  741. gs_index_buffer *curIndexBuffer = nullptr;
  742. gs_vertex_shader *curVertexShader = nullptr;
  743. gs_pixel_shader *curPixelShader = nullptr;
  744. gs_swap_chain *curSwapChain = nullptr;
  745. gs_vertex_buffer *lastVertexBuffer = nullptr;
  746. gs_vertex_shader *lastVertexShader = nullptr;
  747. bool zstencilStateChanged = true;
  748. bool rasterStateChanged = true;
  749. bool blendStateChanged = true;
  750. ZStencilState zstencilState;
  751. RasterState rasterState;
  752. BlendState blendState;
  753. vector<SavedZStencilState> zstencilStates;
  754. vector<SavedRasterState> rasterStates;
  755. vector<SavedBlendState> blendStates;
  756. ID3D11DepthStencilState *curDepthStencilState = nullptr;
  757. ID3D11RasterizerState *curRasterState = nullptr;
  758. ID3D11BlendState *curBlendState = nullptr;
  759. D3D11_PRIMITIVE_TOPOLOGY curToplogy;
  760. pD3DCompile d3dCompile = nullptr;
  761. #ifdef DISASSEMBLE_SHADERS
  762. pD3DDisassemble d3dDisassemble = nullptr;
  763. #endif
  764. gs_rect viewport;
  765. vector<mat4float> projStack;
  766. matrix4 curProjMatrix;
  767. matrix4 curViewMatrix;
  768. matrix4 curViewProjMatrix;
  769. vector<gs_device_loss> loss_callbacks;
  770. gs_obj *first_obj = nullptr;
  771. void InitCompiler();
  772. void InitFactory(uint32_t adapterIdx);
  773. void InitDevice(uint32_t adapterIdx);
  774. ID3D11DepthStencilState *AddZStencilState();
  775. ID3D11RasterizerState *AddRasterState();
  776. ID3D11BlendState *AddBlendState();
  777. void UpdateZStencilState();
  778. void UpdateRasterState();
  779. void UpdateBlendState();
  780. void LoadVertexBufferData();
  781. inline void CopyTex(ID3D11Texture2D *dst, uint32_t dst_x,
  782. uint32_t dst_y, gs_texture_t *src, uint32_t src_x,
  783. uint32_t src_y, uint32_t src_w, uint32_t src_h);
  784. void UpdateViewProjMatrix();
  785. void RebuildDevice();
  786. bool HasBadNV12Output();
  787. gs_device(uint32_t adapterIdx);
  788. ~gs_device();
  789. };
  790. extern "C" EXPORT int device_texture_acquire_sync(gs_texture_t *tex,
  791. uint64_t key, uint32_t ms);