d3d11-subsystem.hpp 18 KB

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