d3d11-subsystem.hpp 25 KB

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