d3d11-subsystem.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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.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. 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 ConvertGSTextureFormat(gs_color_format format)
  47. {
  48. switch (format) {
  49. case GS_UNKNOWN: return DXGI_FORMAT_UNKNOWN;
  50. case GS_A8: return DXGI_FORMAT_A8_UNORM;
  51. case GS_R8: return DXGI_FORMAT_R8_UNORM;
  52. case GS_RGBA: return DXGI_FORMAT_R8G8B8A8_UNORM;
  53. case GS_BGRX: return DXGI_FORMAT_B8G8R8X8_UNORM;
  54. case GS_BGRA: return DXGI_FORMAT_B8G8R8A8_UNORM;
  55. case GS_R10G10B10A2: return DXGI_FORMAT_R10G10B10A2_UNORM;
  56. case GS_RGBA16: return DXGI_FORMAT_R16G16B16A16_UNORM;
  57. case GS_R16: return DXGI_FORMAT_R16_UNORM;
  58. case GS_RGBA16F: return DXGI_FORMAT_R16G16B16A16_FLOAT;
  59. case GS_RGBA32F: return DXGI_FORMAT_R32G32B32A32_FLOAT;
  60. case GS_RG16F: return DXGI_FORMAT_R16G16_FLOAT;
  61. case GS_RG32F: return DXGI_FORMAT_R32G32_FLOAT;
  62. case GS_R16F: return DXGI_FORMAT_R16_FLOAT;
  63. case GS_R32F: return DXGI_FORMAT_R32_FLOAT;
  64. case GS_DXT1: return DXGI_FORMAT_BC1_UNORM;
  65. case GS_DXT3: return DXGI_FORMAT_BC2_UNORM;
  66. case GS_DXT5: return DXGI_FORMAT_BC3_UNORM;
  67. }
  68. return DXGI_FORMAT_UNKNOWN;
  69. }
  70. static inline gs_color_format ConvertDXGITextureFormat(DXGI_FORMAT format)
  71. {
  72. switch ((unsigned long)format) {
  73. case DXGI_FORMAT_A8_UNORM: return GS_A8;
  74. case DXGI_FORMAT_R8_UNORM: return GS_R8;
  75. case DXGI_FORMAT_R8G8B8A8_UNORM: return GS_RGBA;
  76. case DXGI_FORMAT_B8G8R8X8_UNORM: return GS_BGRX;
  77. case DXGI_FORMAT_B8G8R8A8_UNORM: return GS_BGRA;
  78. case DXGI_FORMAT_R10G10B10A2_UNORM: return GS_R10G10B10A2;
  79. case DXGI_FORMAT_R16G16B16A16_UNORM: return GS_RGBA16;
  80. case DXGI_FORMAT_R16_UNORM: return GS_R16;
  81. case DXGI_FORMAT_R16G16B16A16_FLOAT: return GS_RGBA16F;
  82. case DXGI_FORMAT_R32G32B32A32_FLOAT: return GS_RGBA32F;
  83. case DXGI_FORMAT_R16G16_FLOAT: return GS_RG16F;
  84. case DXGI_FORMAT_R32G32_FLOAT: return GS_RG32F;
  85. case DXGI_FORMAT_R16_FLOAT: return GS_R16F;
  86. case DXGI_FORMAT_R32_FLOAT: return GS_R32F;
  87. case DXGI_FORMAT_BC1_UNORM: return GS_DXT1;
  88. case DXGI_FORMAT_BC2_UNORM: return GS_DXT3;
  89. case DXGI_FORMAT_BC3_UNORM: return GS_DXT5;
  90. }
  91. return GS_UNKNOWN;
  92. }
  93. static inline DXGI_FORMAT ConvertGSZStencilFormat(gs_zstencil_format format)
  94. {
  95. switch (format) {
  96. case GS_ZS_NONE: return DXGI_FORMAT_UNKNOWN;
  97. case GS_Z16: return DXGI_FORMAT_D16_UNORM;
  98. case GS_Z24_S8: return DXGI_FORMAT_D24_UNORM_S8_UINT;
  99. case GS_Z32F: return DXGI_FORMAT_D32_FLOAT;
  100. case GS_Z32F_S8X24: return DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
  101. }
  102. return DXGI_FORMAT_UNKNOWN;
  103. }
  104. static inline D3D11_COMPARISON_FUNC ConvertGSDepthTest(gs_depth_test test)
  105. {
  106. switch (test) {
  107. case GS_NEVER: return D3D11_COMPARISON_NEVER;
  108. case GS_LESS: return D3D11_COMPARISON_LESS;
  109. case GS_LEQUAL: return D3D11_COMPARISON_LESS_EQUAL;
  110. case GS_EQUAL: return D3D11_COMPARISON_EQUAL;
  111. case GS_GEQUAL: return D3D11_COMPARISON_GREATER_EQUAL;
  112. case GS_GREATER: return D3D11_COMPARISON_GREATER;
  113. case GS_NOTEQUAL: return D3D11_COMPARISON_NOT_EQUAL;
  114. case GS_ALWAYS: return D3D11_COMPARISON_ALWAYS;
  115. }
  116. return D3D11_COMPARISON_NEVER;
  117. }
  118. static inline D3D11_STENCIL_OP ConvertGSStencilOp(gs_stencil_op_type op)
  119. {
  120. switch (op) {
  121. case GS_KEEP: return D3D11_STENCIL_OP_KEEP;
  122. case GS_ZERO: return D3D11_STENCIL_OP_ZERO;
  123. case GS_REPLACE: return D3D11_STENCIL_OP_REPLACE;
  124. case GS_INCR: return D3D11_STENCIL_OP_INCR;
  125. case GS_DECR: return D3D11_STENCIL_OP_DECR;
  126. case GS_INVERT: return D3D11_STENCIL_OP_INVERT;
  127. }
  128. return D3D11_STENCIL_OP_KEEP;
  129. }
  130. static inline D3D11_BLEND ConvertGSBlendType(gs_blend_type type)
  131. {
  132. switch (type) {
  133. case GS_BLEND_ZERO: return D3D11_BLEND_ZERO;
  134. case GS_BLEND_ONE: return D3D11_BLEND_ONE;
  135. case GS_BLEND_SRCCOLOR: return D3D11_BLEND_SRC_COLOR;
  136. case GS_BLEND_INVSRCCOLOR: return D3D11_BLEND_INV_SRC_COLOR;
  137. case GS_BLEND_SRCALPHA: return D3D11_BLEND_SRC_ALPHA;
  138. case GS_BLEND_INVSRCALPHA: return D3D11_BLEND_INV_SRC_ALPHA;
  139. case GS_BLEND_DSTCOLOR: return D3D11_BLEND_DEST_COLOR;
  140. case GS_BLEND_INVDSTCOLOR: return D3D11_BLEND_INV_DEST_COLOR;
  141. case GS_BLEND_DSTALPHA: return D3D11_BLEND_DEST_ALPHA;
  142. case GS_BLEND_INVDSTALPHA: return D3D11_BLEND_INV_DEST_ALPHA;
  143. case GS_BLEND_SRCALPHASAT: return D3D11_BLEND_SRC_ALPHA_SAT;
  144. }
  145. return D3D11_BLEND_ONE;
  146. }
  147. static inline D3D11_CULL_MODE ConvertGSCullMode(gs_cull_mode mode)
  148. {
  149. switch (mode) {
  150. case GS_BACK: return D3D11_CULL_BACK;
  151. case GS_FRONT: return D3D11_CULL_FRONT;
  152. case GS_NEITHER: return D3D11_CULL_NONE;
  153. }
  154. return D3D11_CULL_BACK;
  155. }
  156. static inline D3D11_PRIMITIVE_TOPOLOGY ConvertGSTopology(gs_draw_mode mode)
  157. {
  158. switch (mode) {
  159. case GS_POINTS: return D3D11_PRIMITIVE_TOPOLOGY_POINTLIST;
  160. case GS_LINES: return D3D11_PRIMITIVE_TOPOLOGY_LINELIST;
  161. case GS_LINESTRIP: return D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP;
  162. case GS_TRIS: return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
  163. case GS_TRISTRIP: return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
  164. }
  165. return D3D11_PRIMITIVE_TOPOLOGY_POINTLIST;
  166. }
  167. /* exception-safe RAII wrapper for vertex buffer data (NOTE: not copy-safe) */
  168. struct VBDataPtr {
  169. gs_vb_data *data;
  170. inline void Clear() {gs_vbdata_destroy(data); data = nullptr;}
  171. inline VBDataPtr(gs_vb_data *data) : data(data) {}
  172. inline ~VBDataPtr() {gs_vbdata_destroy(data);}
  173. };
  174. struct gs_vertex_buffer {
  175. ComPtr<ID3D11Buffer> vertexBuffer;
  176. ComPtr<ID3D11Buffer> normalBuffer;
  177. ComPtr<ID3D11Buffer> colorBuffer;
  178. ComPtr<ID3D11Buffer> tangentBuffer;
  179. vector<ComPtr<ID3D11Buffer>> uvBuffers;
  180. gs_device_t *device;
  181. bool dynamic;
  182. VBDataPtr vbd;
  183. size_t numVerts;
  184. vector<size_t> uvSizes;
  185. void FlushBuffer(ID3D11Buffer *buffer, void *array,
  186. size_t elementSize);
  187. void MakeBufferList(gs_vertex_shader *shader,
  188. vector<ID3D11Buffer*> &buffers,
  189. vector<uint32_t> &strides);
  190. inline void InitBuffer(const size_t elementSize,
  191. const size_t numVerts, void *array,
  192. ID3D11Buffer **buffer);
  193. gs_vertex_buffer(gs_device_t *device, struct gs_vb_data *data,
  194. uint32_t flags);
  195. };
  196. /* exception-safe RAII wrapper for index buffer data (NOTE: not copy-safe) */
  197. struct DataPtr {
  198. void *data;
  199. inline DataPtr(void *data) : data(data) {}
  200. inline ~DataPtr() {bfree(data);}
  201. };
  202. struct gs_index_buffer {
  203. ComPtr<ID3D11Buffer> indexBuffer;
  204. gs_device_t *device;
  205. bool dynamic;
  206. gs_index_type type;
  207. size_t indexSize;
  208. size_t num;
  209. DataPtr indices;
  210. void InitBuffer();
  211. gs_index_buffer(gs_device_t *device, enum gs_index_type type,
  212. void *indices, size_t num, uint32_t flags);
  213. };
  214. struct gs_texture {
  215. gs_texture_type type;
  216. gs_device *device;
  217. uint32_t levels;
  218. gs_color_format format;
  219. ComPtr<ID3D11ShaderResourceView> shaderRes;
  220. inline gs_texture() {}
  221. inline gs_texture(gs_device *device, gs_texture_type type,
  222. uint32_t levels, gs_color_format format)
  223. : type (type),
  224. device (device),
  225. levels (levels),
  226. format (format)
  227. {
  228. }
  229. virtual ~gs_texture() {}
  230. };
  231. struct gs_texture_2d : gs_texture {
  232. ComPtr<ID3D11Texture2D> texture;
  233. ComPtr<ID3D11RenderTargetView> renderTarget[6];
  234. ComPtr<IDXGISurface1> gdiSurface;
  235. uint32_t width = 0, height = 0;
  236. DXGI_FORMAT dxgiFormat = DXGI_FORMAT_UNKNOWN;
  237. bool isRenderTarget = false;
  238. bool isGDICompatible = false;
  239. bool isDynamic = false;
  240. bool isShared = false;
  241. bool genMipmaps = false;
  242. uint32_t sharedHandle = 0;
  243. vector<vector<uint8_t>> data;
  244. void InitSRD(vector<D3D11_SUBRESOURCE_DATA> &srd);
  245. void InitTexture(const uint8_t **data);
  246. void InitResourceView();
  247. void InitRenderTargets();
  248. void BackupTexture(const uint8_t **data);
  249. inline gs_texture_2d()
  250. : gs_texture (NULL, GS_TEXTURE_2D, 0, GS_UNKNOWN)
  251. {
  252. }
  253. gs_texture_2d(gs_device_t *device, uint32_t width, uint32_t height,
  254. gs_color_format colorFormat, uint32_t levels,
  255. const uint8_t **data, uint32_t flags,
  256. gs_texture_type type, bool gdiCompatible, bool shared);
  257. gs_texture_2d(gs_device_t *device, uint32_t handle);
  258. };
  259. struct gs_zstencil_buffer {
  260. ComPtr<ID3D11Texture2D> texture;
  261. ComPtr<ID3D11DepthStencilView> view;
  262. gs_device *device;
  263. uint32_t width, height;
  264. gs_zstencil_format format;
  265. DXGI_FORMAT dxgiFormat;
  266. void InitBuffer();
  267. inline gs_zstencil_buffer()
  268. : device (NULL),
  269. width (0),
  270. height (0),
  271. dxgiFormat (DXGI_FORMAT_UNKNOWN)
  272. {
  273. }
  274. gs_zstencil_buffer(gs_device_t *device, uint32_t width, uint32_t height,
  275. gs_zstencil_format format);
  276. };
  277. struct gs_stage_surface {
  278. ComPtr<ID3D11Texture2D> texture;
  279. gs_device *device;
  280. uint32_t width, height;
  281. gs_color_format format;
  282. DXGI_FORMAT dxgiFormat;
  283. gs_stage_surface(gs_device_t *device, uint32_t width, uint32_t height,
  284. gs_color_format colorFormat);
  285. };
  286. struct gs_sampler_state {
  287. ComPtr<ID3D11SamplerState> state;
  288. gs_device_t *device;
  289. gs_sampler_info info;
  290. gs_sampler_state(gs_device_t *device, const gs_sampler_info *info);
  291. };
  292. struct gs_shader_param {
  293. string name;
  294. gs_shader_param_type type;
  295. uint32_t textureID;
  296. struct gs_sampler_state *nextSampler = nullptr;
  297. int arrayCount;
  298. size_t pos;
  299. vector<uint8_t> curValue;
  300. vector<uint8_t> defaultValue;
  301. bool changed;
  302. gs_shader_param(shader_var &var, uint32_t &texCounter);
  303. };
  304. struct ShaderError {
  305. ComPtr<ID3D10Blob> errors;
  306. HRESULT hr;
  307. inline ShaderError(const ComPtr<ID3D10Blob> &errors, HRESULT hr)
  308. : errors (errors),
  309. hr (hr)
  310. {
  311. }
  312. };
  313. struct gs_shader {
  314. gs_device_t *device;
  315. gs_shader_type type;
  316. vector<gs_shader_param> params;
  317. ComPtr<ID3D11Buffer> constants;
  318. size_t constantSize;
  319. vector<uint8_t> data;
  320. inline void UpdateParam(vector<uint8_t> &constData,
  321. gs_shader_param &param, bool &upload);
  322. void UploadParams();
  323. void BuildConstantBuffer();
  324. void Compile(const char *shaderStr, const char *file,
  325. const char *target, ID3D10Blob **shader);
  326. inline gs_shader(gs_device_t *device, gs_shader_type type)
  327. : device (device),
  328. type (type),
  329. constantSize (0)
  330. {
  331. }
  332. virtual ~gs_shader() {}
  333. };
  334. struct ShaderSampler {
  335. string name;
  336. gs_sampler_state sampler;
  337. inline ShaderSampler(const char *name, gs_device_t *device,
  338. gs_sampler_info *info)
  339. : name (name),
  340. sampler (device, info)
  341. {
  342. }
  343. };
  344. struct gs_vertex_shader : gs_shader {
  345. ComPtr<ID3D11VertexShader> shader;
  346. ComPtr<ID3D11InputLayout> layout;
  347. gs_shader_param *world, *viewProj;
  348. vector<D3D11_INPUT_ELEMENT_DESC> layoutData;
  349. bool hasNormals;
  350. bool hasColors;
  351. bool hasTangents;
  352. uint32_t nTexUnits;
  353. inline uint32_t NumBuffersExpected() const
  354. {
  355. uint32_t count = nTexUnits+1;
  356. if (hasNormals) count++;
  357. if (hasColors) count++;
  358. if (hasTangents) count++;
  359. return count;
  360. }
  361. void GetBuffersExpected(const vector<D3D11_INPUT_ELEMENT_DESC> &inputs);
  362. gs_vertex_shader(gs_device_t *device, const char *file,
  363. const char *shaderString);
  364. };
  365. struct gs_duplicator {
  366. ComPtr<IDXGIOutputDuplication> duplicator;
  367. gs_texture_2d *texture;
  368. gs_device_t *device;
  369. int idx;
  370. void Start();
  371. gs_duplicator(gs_device_t *device, int monitor_idx);
  372. ~gs_duplicator();
  373. };
  374. struct gs_pixel_shader : gs_shader {
  375. ComPtr<ID3D11PixelShader> shader;
  376. vector<unique_ptr<ShaderSampler>> samplers;
  377. inline void GetSamplerStates(ID3D11SamplerState **states)
  378. {
  379. size_t i;
  380. for (i = 0; i < samplers.size(); i++)
  381. states[i] = samplers[i]->sampler.state;
  382. for (; i < GS_MAX_TEXTURES; i++)
  383. states[i] = NULL;
  384. }
  385. gs_pixel_shader(gs_device_t *device, const char *file,
  386. const char *shaderString);
  387. };
  388. struct gs_swap_chain {
  389. gs_device *device;
  390. uint32_t numBuffers;
  391. HWND hwnd;
  392. gs_init_data initData;
  393. gs_texture_2d target;
  394. gs_zstencil_buffer zs;
  395. ComPtr<IDXGISwapChain> swap;
  396. void InitTarget(uint32_t cx, uint32_t cy);
  397. void InitZStencilBuffer(uint32_t cx, uint32_t cy);
  398. void Resize(uint32_t cx, uint32_t cy);
  399. void Init();
  400. inline gs_swap_chain()
  401. : device (NULL),
  402. numBuffers (0),
  403. hwnd (NULL)
  404. {
  405. }
  406. gs_swap_chain(gs_device *device, const gs_init_data *data);
  407. };
  408. struct BlendState {
  409. bool blendEnabled;
  410. gs_blend_type srcFactorC;
  411. gs_blend_type destFactorC;
  412. gs_blend_type srcFactorA;
  413. gs_blend_type destFactorA;
  414. bool redEnabled;
  415. bool greenEnabled;
  416. bool blueEnabled;
  417. bool alphaEnabled;
  418. inline BlendState()
  419. : blendEnabled (true),
  420. srcFactorC (GS_BLEND_SRCALPHA),
  421. destFactorC (GS_BLEND_INVSRCALPHA),
  422. srcFactorA (GS_BLEND_ONE),
  423. destFactorA (GS_BLEND_ONE),
  424. redEnabled (true),
  425. greenEnabled (true),
  426. blueEnabled (true),
  427. alphaEnabled (true)
  428. {
  429. }
  430. inline BlendState(const BlendState &state)
  431. {
  432. memcpy(this, &state, sizeof(BlendState));
  433. }
  434. };
  435. struct SavedBlendState : BlendState {
  436. ComPtr<ID3D11BlendState> state;
  437. inline SavedBlendState(const BlendState &val) : BlendState(val)
  438. {
  439. }
  440. };
  441. struct StencilSide {
  442. gs_depth_test test;
  443. gs_stencil_op_type fail;
  444. gs_stencil_op_type zfail;
  445. gs_stencil_op_type zpass;
  446. inline StencilSide()
  447. : test (GS_ALWAYS),
  448. fail (GS_KEEP),
  449. zfail (GS_KEEP),
  450. zpass (GS_KEEP)
  451. {
  452. }
  453. };
  454. struct ZStencilState {
  455. bool depthEnabled;
  456. bool depthWriteEnabled;
  457. gs_depth_test depthFunc;
  458. bool stencilEnabled;
  459. bool stencilWriteEnabled;
  460. StencilSide stencilFront;
  461. StencilSide stencilBack;
  462. inline ZStencilState()
  463. : depthEnabled (true),
  464. depthWriteEnabled (true),
  465. depthFunc (GS_LESS),
  466. stencilEnabled (false),
  467. stencilWriteEnabled (true)
  468. {
  469. }
  470. inline ZStencilState(const ZStencilState &state)
  471. {
  472. memcpy(this, &state, sizeof(ZStencilState));
  473. }
  474. };
  475. struct SavedZStencilState : ZStencilState {
  476. ComPtr<ID3D11DepthStencilState> state;
  477. inline SavedZStencilState(const ZStencilState &val)
  478. : ZStencilState (val)
  479. {
  480. }
  481. };
  482. struct RasterState {
  483. gs_cull_mode cullMode;
  484. bool scissorEnabled;
  485. inline RasterState()
  486. : cullMode (GS_BACK),
  487. scissorEnabled (false)
  488. {
  489. }
  490. inline RasterState(const RasterState &state)
  491. {
  492. memcpy(this, &state, sizeof(RasterState));
  493. }
  494. };
  495. struct SavedRasterState : RasterState {
  496. ComPtr<ID3D11RasterizerState> state;
  497. inline SavedRasterState(const RasterState &val)
  498. : RasterState (val)
  499. {
  500. }
  501. };
  502. struct mat4float {
  503. float mat[16];
  504. };
  505. struct gs_device {
  506. ComPtr<IDXGIFactory1> factory;
  507. ComPtr<IDXGIAdapter1> adapter;
  508. ComPtr<ID3D11Device> device;
  509. ComPtr<ID3D11DeviceContext> context;
  510. uint32_t adpIdx = 0;
  511. gs_texture_2d *curRenderTarget = nullptr;
  512. gs_zstencil_buffer *curZStencilBuffer = nullptr;
  513. int curRenderSide = 0;
  514. gs_texture *curTextures[GS_MAX_TEXTURES];
  515. gs_sampler_state *curSamplers[GS_MAX_TEXTURES];
  516. gs_vertex_buffer *curVertexBuffer = nullptr;
  517. gs_index_buffer *curIndexBuffer = nullptr;
  518. gs_vertex_shader *curVertexShader = nullptr;
  519. gs_pixel_shader *curPixelShader = nullptr;
  520. gs_swap_chain *curSwapChain = nullptr;
  521. bool zstencilStateChanged = true;
  522. bool rasterStateChanged = true;
  523. bool blendStateChanged = true;
  524. ZStencilState zstencilState;
  525. RasterState rasterState;
  526. BlendState blendState;
  527. vector<SavedZStencilState> zstencilStates;
  528. vector<SavedRasterState> rasterStates;
  529. vector<SavedBlendState> blendStates;
  530. ID3D11DepthStencilState *curDepthStencilState = nullptr;
  531. ID3D11RasterizerState *curRasterState = nullptr;
  532. ID3D11BlendState *curBlendState = nullptr;
  533. D3D11_PRIMITIVE_TOPOLOGY curToplogy;
  534. pD3DCompile d3dCompile = nullptr;
  535. gs_rect viewport;
  536. vector<mat4float> projStack;
  537. matrix4 curProjMatrix;
  538. matrix4 curViewMatrix;
  539. matrix4 curViewProjMatrix;
  540. void InitCompiler();
  541. void InitFactory(uint32_t adapterIdx);
  542. void InitDevice(uint32_t adapterIdx);
  543. ID3D11DepthStencilState *AddZStencilState();
  544. ID3D11RasterizerState *AddRasterState();
  545. ID3D11BlendState *AddBlendState();
  546. void UpdateZStencilState();
  547. void UpdateRasterState();
  548. void UpdateBlendState();
  549. inline void CopyTex(ID3D11Texture2D *dst,
  550. uint32_t dst_x, uint32_t dst_y,
  551. gs_texture_t *src, uint32_t src_x, uint32_t src_y,
  552. uint32_t src_w, uint32_t src_h);
  553. void UpdateViewProjMatrix();
  554. gs_device(uint32_t adapterIdx);
  555. ~gs_device();
  556. };