d3d11-subsystem.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. enum class gs_type {
  175. gs_vertex_buffer,
  176. gs_index_buffer,
  177. gs_texture_2d,
  178. gs_zstencil_buffer,
  179. gs_stage_surface,
  180. gs_sampler_state,
  181. gs_vertex_shader,
  182. gs_pixel_shader,
  183. gs_duplicator,
  184. gs_swap_chain,
  185. };
  186. struct gs_obj {
  187. gs_device_t *device;
  188. gs_type obj_type;
  189. gs_obj *next;
  190. gs_obj **prev_next;
  191. inline gs_obj() :
  192. device(nullptr),
  193. next(nullptr),
  194. prev_next(nullptr)
  195. {}
  196. gs_obj(gs_device_t *device, gs_type type);
  197. virtual ~gs_obj();
  198. };
  199. struct gs_vertex_buffer : gs_obj {
  200. ComPtr<ID3D11Buffer> vertexBuffer;
  201. ComPtr<ID3D11Buffer> normalBuffer;
  202. ComPtr<ID3D11Buffer> colorBuffer;
  203. ComPtr<ID3D11Buffer> tangentBuffer;
  204. vector<ComPtr<ID3D11Buffer>> uvBuffers;
  205. bool dynamic;
  206. VBDataPtr vbd;
  207. size_t numVerts;
  208. vector<size_t> uvSizes;
  209. void FlushBuffer(ID3D11Buffer *buffer, void *array,
  210. size_t elementSize);
  211. void MakeBufferList(gs_vertex_shader *shader,
  212. vector<ID3D11Buffer*> &buffers,
  213. vector<uint32_t> &strides);
  214. void InitBuffer(const size_t elementSize,
  215. const size_t numVerts, void *array,
  216. ID3D11Buffer **buffer);
  217. void BuildBuffers();
  218. gs_vertex_buffer(gs_device_t *device, struct gs_vb_data *data,
  219. uint32_t flags);
  220. };
  221. /* exception-safe RAII wrapper for index buffer data (NOTE: not copy-safe) */
  222. struct DataPtr {
  223. void *data;
  224. inline DataPtr(void *data) : data(data) {}
  225. inline ~DataPtr() {bfree(data);}
  226. };
  227. struct gs_index_buffer : gs_obj {
  228. ComPtr<ID3D11Buffer> indexBuffer;
  229. bool dynamic;
  230. gs_index_type type;
  231. size_t indexSize;
  232. size_t num;
  233. DataPtr indices;
  234. D3D11_BUFFER_DESC bd = {};
  235. D3D11_SUBRESOURCE_DATA srd = {};
  236. void InitBuffer();
  237. gs_index_buffer(gs_device_t *device, enum gs_index_type type,
  238. void *indices, size_t num, uint32_t flags);
  239. };
  240. struct gs_texture : gs_obj {
  241. gs_texture_type type;
  242. uint32_t levels;
  243. gs_color_format format;
  244. ComPtr<ID3D11ShaderResourceView> shaderRes;
  245. D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesc = {};
  246. inline gs_texture(gs_texture_type type, uint32_t levels,
  247. gs_color_format format)
  248. : type (type),
  249. levels (levels),
  250. format (format)
  251. {
  252. }
  253. inline gs_texture(gs_device *device, gs_type obj_type,
  254. gs_texture_type type)
  255. : gs_obj (device, obj_type),
  256. type (type)
  257. {
  258. }
  259. inline gs_texture(gs_device *device, gs_type obj_type,
  260. gs_texture_type type,
  261. uint32_t levels, gs_color_format format)
  262. : gs_obj (device, obj_type),
  263. type (type),
  264. levels (levels),
  265. format (format)
  266. {
  267. }
  268. };
  269. struct gs_texture_2d : gs_texture {
  270. ComPtr<ID3D11Texture2D> texture;
  271. ComPtr<ID3D11RenderTargetView> renderTarget[6];
  272. ComPtr<IDXGISurface1> gdiSurface;
  273. uint32_t width = 0, height = 0;
  274. DXGI_FORMAT dxgiFormat = DXGI_FORMAT_UNKNOWN;
  275. bool isRenderTarget = false;
  276. bool isGDICompatible = false;
  277. bool isDynamic = false;
  278. bool isShared = false;
  279. bool genMipmaps = false;
  280. uint32_t sharedHandle = 0;
  281. vector<vector<uint8_t>> data;
  282. vector<D3D11_SUBRESOURCE_DATA> srd;
  283. D3D11_TEXTURE2D_DESC td = {};
  284. void InitSRD(vector<D3D11_SUBRESOURCE_DATA> &srd);
  285. void InitTexture(const uint8_t **data);
  286. void InitResourceView();
  287. void InitRenderTargets();
  288. void BackupTexture(const uint8_t **data);
  289. inline gs_texture_2d()
  290. : gs_texture (GS_TEXTURE_2D, 0, GS_UNKNOWN)
  291. {
  292. }
  293. gs_texture_2d(gs_device_t *device, uint32_t width, uint32_t height,
  294. gs_color_format colorFormat, uint32_t levels,
  295. const uint8_t **data, uint32_t flags,
  296. gs_texture_type type, bool gdiCompatible, bool shared);
  297. gs_texture_2d(gs_device_t *device, uint32_t handle);
  298. };
  299. struct gs_zstencil_buffer : gs_obj {
  300. ComPtr<ID3D11Texture2D> texture;
  301. ComPtr<ID3D11DepthStencilView> view;
  302. uint32_t width, height;
  303. gs_zstencil_format format;
  304. DXGI_FORMAT dxgiFormat;
  305. D3D11_TEXTURE2D_DESC td = {};
  306. D3D11_DEPTH_STENCIL_VIEW_DESC dsvd = {};
  307. void InitBuffer();
  308. inline gs_zstencil_buffer()
  309. : width (0),
  310. height (0),
  311. dxgiFormat (DXGI_FORMAT_UNKNOWN)
  312. {
  313. }
  314. gs_zstencil_buffer(gs_device_t *device, uint32_t width, uint32_t height,
  315. gs_zstencil_format format);
  316. };
  317. struct gs_stage_surface : gs_obj {
  318. ComPtr<ID3D11Texture2D> texture;
  319. D3D11_TEXTURE2D_DESC td = {};
  320. uint32_t width, height;
  321. gs_color_format format;
  322. DXGI_FORMAT dxgiFormat;
  323. gs_stage_surface(gs_device_t *device, uint32_t width, uint32_t height,
  324. gs_color_format colorFormat);
  325. };
  326. struct gs_sampler_state : gs_obj {
  327. ComPtr<ID3D11SamplerState> state;
  328. D3D11_SAMPLER_DESC sd = {};
  329. gs_sampler_info info;
  330. gs_sampler_state(gs_device_t *device, const gs_sampler_info *info);
  331. };
  332. struct gs_shader_param {
  333. string name;
  334. gs_shader_param_type type;
  335. uint32_t textureID;
  336. struct gs_sampler_state *nextSampler = nullptr;
  337. int arrayCount;
  338. size_t pos;
  339. vector<uint8_t> curValue;
  340. vector<uint8_t> defaultValue;
  341. bool changed;
  342. gs_shader_param(shader_var &var, uint32_t &texCounter);
  343. };
  344. struct ShaderError {
  345. ComPtr<ID3D10Blob> errors;
  346. HRESULT hr;
  347. inline ShaderError(const ComPtr<ID3D10Blob> &errors, HRESULT hr)
  348. : errors (errors),
  349. hr (hr)
  350. {
  351. }
  352. };
  353. struct gs_shader : gs_obj {
  354. gs_shader_type type;
  355. vector<gs_shader_param> params;
  356. ComPtr<ID3D11Buffer> constants;
  357. size_t constantSize;
  358. D3D11_BUFFER_DESC bd = {};
  359. vector<uint8_t> data;
  360. inline void UpdateParam(vector<uint8_t> &constData,
  361. gs_shader_param &param, bool &upload);
  362. void UploadParams();
  363. void BuildConstantBuffer();
  364. void Compile(const char *shaderStr, const char *file,
  365. const char *target, ID3D10Blob **shader);
  366. inline gs_shader(gs_device_t *device, gs_type obj_type,
  367. gs_shader_type type)
  368. : gs_obj (device, obj_type),
  369. type (type),
  370. constantSize (0)
  371. {
  372. }
  373. virtual ~gs_shader() {}
  374. };
  375. struct ShaderSampler {
  376. string name;
  377. gs_sampler_state sampler;
  378. inline ShaderSampler(const char *name, gs_device_t *device,
  379. gs_sampler_info *info)
  380. : name (name),
  381. sampler (device, info)
  382. {
  383. }
  384. };
  385. struct gs_vertex_shader : gs_shader {
  386. ComPtr<ID3D11VertexShader> shader;
  387. ComPtr<ID3D11InputLayout> layout;
  388. gs_shader_param *world, *viewProj;
  389. vector<D3D11_INPUT_ELEMENT_DESC> layoutData;
  390. bool hasNormals;
  391. bool hasColors;
  392. bool hasTangents;
  393. uint32_t nTexUnits;
  394. inline uint32_t NumBuffersExpected() const
  395. {
  396. uint32_t count = nTexUnits+1;
  397. if (hasNormals) count++;
  398. if (hasColors) count++;
  399. if (hasTangents) count++;
  400. return count;
  401. }
  402. void GetBuffersExpected(const vector<D3D11_INPUT_ELEMENT_DESC> &inputs);
  403. gs_vertex_shader(gs_device_t *device, const char *file,
  404. const char *shaderString);
  405. };
  406. struct gs_duplicator : gs_obj {
  407. ComPtr<IDXGIOutputDuplication> duplicator;
  408. gs_texture_2d *texture;
  409. int idx;
  410. void Start();
  411. gs_duplicator(gs_device_t *device, int monitor_idx);
  412. ~gs_duplicator();
  413. };
  414. struct gs_pixel_shader : gs_shader {
  415. ComPtr<ID3D11PixelShader> shader;
  416. vector<unique_ptr<ShaderSampler>> samplers;
  417. inline void GetSamplerStates(ID3D11SamplerState **states)
  418. {
  419. size_t i;
  420. for (i = 0; i < samplers.size(); i++)
  421. states[i] = samplers[i]->sampler.state;
  422. for (; i < GS_MAX_TEXTURES; i++)
  423. states[i] = NULL;
  424. }
  425. gs_pixel_shader(gs_device_t *device, const char *file,
  426. const char *shaderString);
  427. };
  428. struct gs_swap_chain : gs_obj {
  429. uint32_t numBuffers;
  430. HWND hwnd;
  431. gs_init_data initData;
  432. DXGI_SWAP_CHAIN_DESC swapDesc = {};
  433. gs_texture_2d target;
  434. gs_zstencil_buffer zs;
  435. ComPtr<IDXGISwapChain> swap;
  436. void InitTarget(uint32_t cx, uint32_t cy);
  437. void InitZStencilBuffer(uint32_t cx, uint32_t cy);
  438. void Resize(uint32_t cx, uint32_t cy);
  439. void Init();
  440. gs_swap_chain(gs_device *device, const gs_init_data *data);
  441. };
  442. struct BlendState {
  443. bool blendEnabled;
  444. gs_blend_type srcFactorC;
  445. gs_blend_type destFactorC;
  446. gs_blend_type srcFactorA;
  447. gs_blend_type destFactorA;
  448. bool redEnabled;
  449. bool greenEnabled;
  450. bool blueEnabled;
  451. bool alphaEnabled;
  452. inline BlendState()
  453. : blendEnabled (true),
  454. srcFactorC (GS_BLEND_SRCALPHA),
  455. destFactorC (GS_BLEND_INVSRCALPHA),
  456. srcFactorA (GS_BLEND_ONE),
  457. destFactorA (GS_BLEND_ONE),
  458. redEnabled (true),
  459. greenEnabled (true),
  460. blueEnabled (true),
  461. alphaEnabled (true)
  462. {
  463. }
  464. inline BlendState(const BlendState &state)
  465. {
  466. memcpy(this, &state, sizeof(BlendState));
  467. }
  468. };
  469. struct SavedBlendState : BlendState {
  470. ComPtr<ID3D11BlendState> state;
  471. D3D11_BLEND_DESC bd;
  472. inline SavedBlendState(const BlendState &val, D3D11_BLEND_DESC &desc)
  473. : BlendState(val), bd(desc)
  474. {
  475. }
  476. };
  477. struct StencilSide {
  478. gs_depth_test test;
  479. gs_stencil_op_type fail;
  480. gs_stencil_op_type zfail;
  481. gs_stencil_op_type zpass;
  482. inline StencilSide()
  483. : test (GS_ALWAYS),
  484. fail (GS_KEEP),
  485. zfail (GS_KEEP),
  486. zpass (GS_KEEP)
  487. {
  488. }
  489. };
  490. struct ZStencilState {
  491. bool depthEnabled;
  492. bool depthWriteEnabled;
  493. gs_depth_test depthFunc;
  494. bool stencilEnabled;
  495. bool stencilWriteEnabled;
  496. StencilSide stencilFront;
  497. StencilSide stencilBack;
  498. inline ZStencilState()
  499. : depthEnabled (true),
  500. depthWriteEnabled (true),
  501. depthFunc (GS_LESS),
  502. stencilEnabled (false),
  503. stencilWriteEnabled (true)
  504. {
  505. }
  506. inline ZStencilState(const ZStencilState &state)
  507. {
  508. memcpy(this, &state, sizeof(ZStencilState));
  509. }
  510. };
  511. struct SavedZStencilState : ZStencilState {
  512. ComPtr<ID3D11DepthStencilState> state;
  513. D3D11_DEPTH_STENCIL_DESC dsd;
  514. inline SavedZStencilState(const ZStencilState &val,
  515. D3D11_DEPTH_STENCIL_DESC desc)
  516. : ZStencilState (val),
  517. dsd (desc)
  518. {
  519. }
  520. };
  521. struct RasterState {
  522. gs_cull_mode cullMode;
  523. bool scissorEnabled;
  524. inline RasterState()
  525. : cullMode (GS_BACK),
  526. scissorEnabled (false)
  527. {
  528. }
  529. inline RasterState(const RasterState &state)
  530. {
  531. memcpy(this, &state, sizeof(RasterState));
  532. }
  533. };
  534. struct SavedRasterState : RasterState {
  535. ComPtr<ID3D11RasterizerState> state;
  536. D3D11_RASTERIZER_DESC rd;
  537. inline SavedRasterState(const RasterState &val,
  538. D3D11_RASTERIZER_DESC &desc)
  539. : RasterState (val),
  540. rd (desc)
  541. {
  542. }
  543. };
  544. struct mat4float {
  545. float mat[16];
  546. };
  547. struct gs_device {
  548. ComPtr<IDXGIFactory1> factory;
  549. ComPtr<IDXGIAdapter1> adapter;
  550. ComPtr<ID3D11Device> device;
  551. ComPtr<ID3D11DeviceContext> context;
  552. uint32_t adpIdx = 0;
  553. gs_texture_2d *curRenderTarget = nullptr;
  554. gs_zstencil_buffer *curZStencilBuffer = nullptr;
  555. int curRenderSide = 0;
  556. gs_texture *curTextures[GS_MAX_TEXTURES];
  557. gs_sampler_state *curSamplers[GS_MAX_TEXTURES];
  558. gs_vertex_buffer *curVertexBuffer = nullptr;
  559. gs_index_buffer *curIndexBuffer = nullptr;
  560. gs_vertex_shader *curVertexShader = nullptr;
  561. gs_pixel_shader *curPixelShader = nullptr;
  562. gs_swap_chain *curSwapChain = nullptr;
  563. bool zstencilStateChanged = true;
  564. bool rasterStateChanged = true;
  565. bool blendStateChanged = true;
  566. ZStencilState zstencilState;
  567. RasterState rasterState;
  568. BlendState blendState;
  569. vector<SavedZStencilState> zstencilStates;
  570. vector<SavedRasterState> rasterStates;
  571. vector<SavedBlendState> blendStates;
  572. ID3D11DepthStencilState *curDepthStencilState = nullptr;
  573. ID3D11RasterizerState *curRasterState = nullptr;
  574. ID3D11BlendState *curBlendState = nullptr;
  575. D3D11_PRIMITIVE_TOPOLOGY curToplogy;
  576. pD3DCompile d3dCompile = nullptr;
  577. gs_rect viewport;
  578. vector<mat4float> projStack;
  579. matrix4 curProjMatrix;
  580. matrix4 curViewMatrix;
  581. matrix4 curViewProjMatrix;
  582. gs_obj *first_obj = nullptr;
  583. void InitCompiler();
  584. void InitFactory(uint32_t adapterIdx);
  585. void InitDevice(uint32_t adapterIdx);
  586. ID3D11DepthStencilState *AddZStencilState();
  587. ID3D11RasterizerState *AddRasterState();
  588. ID3D11BlendState *AddBlendState();
  589. void UpdateZStencilState();
  590. void UpdateRasterState();
  591. void UpdateBlendState();
  592. inline void CopyTex(ID3D11Texture2D *dst,
  593. uint32_t dst_x, uint32_t dst_y,
  594. gs_texture_t *src, uint32_t src_x, uint32_t src_y,
  595. uint32_t src_w, uint32_t src_h);
  596. void UpdateViewProjMatrix();
  597. gs_device(uint32_t adapterIdx);
  598. ~gs_device();
  599. };