d3d11-subsystem.hpp 18 KB

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