d3d11-subsystem.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  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. #include <util/base.h>
  15. #include <util/platform.h>
  16. #include <graphics/matrix3.h>
  17. #include "d3d11-subsystem.hpp"
  18. struct UnsupportedHWError : HRError {
  19. inline UnsupportedHWError(const char *str, HRESULT hr)
  20. : HRError(str, hr)
  21. {
  22. }
  23. };
  24. #ifdef _MSC_VER
  25. /* alignment warning - despite the fact that alignment is already fixed */
  26. #pragma warning (disable : 4316)
  27. #endif
  28. static const IID dxgiFactory2 =
  29. {0x50c83a1c, 0xe072, 0x4c48, {0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6, 0xd0}};
  30. static inline void make_swap_desc(DXGI_SWAP_CHAIN_DESC &desc,
  31. gs_init_data *data)
  32. {
  33. memset(&desc, 0, sizeof(desc));
  34. desc.BufferCount = data->num_backbuffers;
  35. desc.BufferDesc.Format = ConvertGSTextureFormat(data->format);
  36. desc.BufferDesc.Width = data->cx;
  37. desc.BufferDesc.Height = data->cy;
  38. desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  39. desc.OutputWindow = (HWND)data->window.hwnd;
  40. desc.SampleDesc.Count = 1;
  41. desc.Windowed = true;
  42. }
  43. void gs_swap_chain::InitTarget(uint32_t cx, uint32_t cy)
  44. {
  45. HRESULT hr;
  46. target.width = cx;
  47. target.height = cy;
  48. hr = swap->GetBuffer(0, __uuidof(ID3D11Texture2D),
  49. (void**)target.texture.Assign());
  50. if (FAILED(hr))
  51. throw HRError("Failed to get swap buffer texture", hr);
  52. hr = device->device->CreateRenderTargetView(target.texture, NULL,
  53. target.renderTarget[0].Assign());
  54. if (FAILED(hr))
  55. throw HRError("Failed to create swap render target view", hr);
  56. }
  57. void gs_swap_chain::InitZStencilBuffer(uint32_t cx, uint32_t cy)
  58. {
  59. zs.width = cx;
  60. zs.height = cy;
  61. if (zs.format != GS_ZS_NONE && cx != 0 && cy != 0) {
  62. zs.InitBuffer();
  63. } else {
  64. zs.texture.Clear();
  65. zs.view.Clear();
  66. }
  67. }
  68. void gs_swap_chain::Resize(uint32_t cx, uint32_t cy)
  69. {
  70. RECT clientRect;
  71. HRESULT hr;
  72. target.texture.Clear();
  73. target.renderTarget[0].Clear();
  74. zs.texture.Clear();
  75. zs.view.Clear();
  76. if (cx == 0 || cy == 0) {
  77. GetClientRect(hwnd, &clientRect);
  78. if (cx == 0) cx = clientRect.right;
  79. if (cy == 0) cy = clientRect.bottom;
  80. }
  81. hr = swap->ResizeBuffers(numBuffers, cx, cy, target.dxgiFormat, 0);
  82. if (FAILED(hr))
  83. throw HRError("Failed to resize swap buffers", hr);
  84. InitTarget(cx, cy);
  85. InitZStencilBuffer(cx, cy);
  86. }
  87. void gs_swap_chain::Init(gs_init_data *data)
  88. {
  89. target.device = device;
  90. target.isRenderTarget = true;
  91. target.format = data->format;
  92. target.dxgiFormat = ConvertGSTextureFormat(data->format);
  93. InitTarget(data->cx, data->cy);
  94. zs.device = device;
  95. zs.format = data->zsformat;
  96. zs.dxgiFormat = ConvertGSZStencilFormat(data->zsformat);
  97. InitZStencilBuffer(data->cx, data->cy);
  98. }
  99. gs_swap_chain::gs_swap_chain(gs_device *device, gs_init_data *data)
  100. : device (device),
  101. numBuffers (data->num_backbuffers),
  102. hwnd ((HWND)data->window.hwnd)
  103. {
  104. HRESULT hr;
  105. DXGI_SWAP_CHAIN_DESC swapDesc;
  106. make_swap_desc(swapDesc, data);
  107. hr = device->factory->CreateSwapChain(device->device, &swapDesc,
  108. swap.Assign());
  109. if (FAILED(hr))
  110. throw HRError("Failed to create swap chain", hr);
  111. Init(data);
  112. }
  113. void gs_device::InitFactory(uint32_t adapterIdx, IDXGIAdapter1 **padapter)
  114. {
  115. HRESULT hr;
  116. IID factoryIID = (GetWinVer() >= 0x602) ? dxgiFactory2 :
  117. __uuidof(IDXGIFactory1);
  118. hr = CreateDXGIFactory1(factoryIID, (void**)factory.Assign());
  119. if (FAILED(hr))
  120. throw UnsupportedHWError("Failed to create DXGIFactory", hr);
  121. hr = factory->EnumAdapters1(adapterIdx, padapter);
  122. if (FAILED(hr))
  123. throw UnsupportedHWError("Failed to enumerate DXGIAdapter", hr);
  124. }
  125. const static D3D_FEATURE_LEVEL featureLevels[] =
  126. {
  127. D3D_FEATURE_LEVEL_11_0,
  128. D3D_FEATURE_LEVEL_10_1,
  129. D3D_FEATURE_LEVEL_10_0,
  130. D3D_FEATURE_LEVEL_9_3,
  131. };
  132. void gs_device::InitDevice(gs_init_data *data, IDXGIAdapter *adapter)
  133. {
  134. wstring adapterName;
  135. DXGI_SWAP_CHAIN_DESC swapDesc;
  136. DXGI_ADAPTER_DESC desc;
  137. D3D_FEATURE_LEVEL levelUsed;
  138. HRESULT hr;
  139. make_swap_desc(swapDesc, data);
  140. uint32_t createFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
  141. #ifdef _DEBUG
  142. //createFlags |= D3D11_CREATE_DEVICE_DEBUG;
  143. #endif
  144. adapterName = (adapter->GetDesc(&desc) == S_OK) ? desc.Description :
  145. L"<unknown>";
  146. char *adapterNameUTF8;
  147. os_wcs_to_utf8_ptr(adapterName.c_str(), 0, &adapterNameUTF8);
  148. blog(LOG_INFO, "Loading up D3D11 on adapter %s", adapterNameUTF8);
  149. bfree(adapterNameUTF8);
  150. hr = D3D11CreateDeviceAndSwapChain(adapter, D3D_DRIVER_TYPE_UNKNOWN,
  151. NULL, createFlags, featureLevels,
  152. sizeof(featureLevels) / sizeof(D3D_FEATURE_LEVEL),
  153. D3D11_SDK_VERSION, &swapDesc,
  154. defaultSwap.swap.Assign(), device.Assign(),
  155. &levelUsed, context.Assign());
  156. if (FAILED(hr))
  157. throw UnsupportedHWError("Failed to create device and "
  158. "swap chain", hr);
  159. blog(LOG_INFO, "D3D11 loaded sucessfully, feature level used: %u",
  160. (uint32_t)levelUsed);
  161. defaultSwap.device = this;
  162. defaultSwap.hwnd = (HWND)data->window.hwnd;
  163. defaultSwap.numBuffers = data->num_backbuffers;
  164. defaultSwap.Init(data);
  165. }
  166. static inline void ConvertStencilSide(D3D11_DEPTH_STENCILOP_DESC &desc,
  167. const StencilSide &side)
  168. {
  169. desc.StencilFunc = ConvertGSDepthTest(side.test);
  170. desc.StencilFailOp = ConvertGSStencilOp(side.fail);
  171. desc.StencilDepthFailOp = ConvertGSStencilOp(side.zfail);
  172. desc.StencilPassOp = ConvertGSStencilOp(side.zpass);
  173. }
  174. ID3D11DepthStencilState *gs_device::AddZStencilState()
  175. {
  176. HRESULT hr;
  177. D3D11_DEPTH_STENCIL_DESC dsd;
  178. SavedZStencilState savedState(zstencilState);
  179. ID3D11DepthStencilState *state;
  180. dsd.DepthEnable = zstencilState.depthEnabled;
  181. dsd.DepthFunc = ConvertGSDepthTest(zstencilState.depthFunc);
  182. dsd.DepthWriteMask = zstencilState.depthWriteEnabled ?
  183. D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
  184. dsd.StencilEnable = zstencilState.stencilEnabled;
  185. dsd.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
  186. dsd.StencilWriteMask = zstencilState.stencilWriteEnabled ?
  187. D3D11_DEFAULT_STENCIL_WRITE_MASK : 0;
  188. ConvertStencilSide(dsd.FrontFace, zstencilState.stencilFront);
  189. ConvertStencilSide(dsd.BackFace, zstencilState.stencilBack);
  190. hr = device->CreateDepthStencilState(&dsd, savedState.state.Assign());
  191. if (FAILED(hr))
  192. throw HRError("Failed to create depth stencil state", hr);
  193. state = savedState.state;
  194. zstencilStates.push_back(savedState);
  195. return state;
  196. }
  197. ID3D11RasterizerState *gs_device::AddRasterState()
  198. {
  199. HRESULT hr;
  200. D3D11_RASTERIZER_DESC rd;
  201. SavedRasterState savedState(rasterState);
  202. ID3D11RasterizerState *state;
  203. memset(&rd, 0, sizeof(rd));
  204. /* use CCW to convert to a right-handed coordinate system */
  205. rd.FrontCounterClockwise = true;
  206. rd.FillMode = D3D11_FILL_SOLID;
  207. rd.CullMode = ConvertGSCullMode(rasterState.cullMode);
  208. rd.DepthClipEnable = true;
  209. rd.ScissorEnable = rasterState.scissorEnabled;
  210. hr = device->CreateRasterizerState(&rd, savedState.state.Assign());
  211. if (FAILED(hr))
  212. throw HRError("Failed to create rasterizer state", hr);
  213. state = savedState.state;
  214. rasterStates.push_back(savedState);
  215. return state;
  216. }
  217. ID3D11BlendState *gs_device::AddBlendState()
  218. {
  219. HRESULT hr;
  220. D3D11_BLEND_DESC bd;
  221. SavedBlendState savedState(blendState);
  222. ID3D11BlendState *state;
  223. memset(&bd, 0, sizeof(bd));
  224. for (int i = 0; i < 8; i++) {
  225. bd.RenderTarget[i].BlendEnable = blendState.blendEnabled;
  226. bd.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD;
  227. bd.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  228. bd.RenderTarget[i].SrcBlend =
  229. ConvertGSBlendType(blendState.srcFactor);
  230. bd.RenderTarget[i].DestBlend =
  231. ConvertGSBlendType(blendState.destFactor);
  232. bd.RenderTarget[i].SrcBlendAlpha =
  233. bd.RenderTarget[i].SrcBlend;
  234. bd.RenderTarget[i].DestBlendAlpha =
  235. bd.RenderTarget[i].DestBlend;
  236. bd.RenderTarget[i].RenderTargetWriteMask =
  237. D3D11_COLOR_WRITE_ENABLE_ALL;
  238. }
  239. hr = device->CreateBlendState(&bd, savedState.state.Assign());
  240. if (FAILED(hr))
  241. throw HRError("Failed to create disabled blend state", hr);
  242. state = savedState.state;
  243. blendStates.push_back(savedState);
  244. return state;
  245. }
  246. void gs_device::UpdateZStencilState()
  247. {
  248. ID3D11DepthStencilState *state = NULL;
  249. if (!zstencilStateChanged)
  250. return;
  251. for (size_t i = 0; i < zstencilStates.size(); i++) {
  252. SavedZStencilState &s = zstencilStates[i];
  253. if (memcmp(&s, &zstencilState, sizeof(zstencilState)) == 0) {
  254. state = s.state;
  255. break;
  256. }
  257. }
  258. if (!state)
  259. state = AddZStencilState();
  260. if (state != curDepthStencilState) {
  261. context->OMSetDepthStencilState(state, 0);
  262. curDepthStencilState = state;
  263. }
  264. zstencilStateChanged = false;
  265. }
  266. void gs_device::UpdateRasterState()
  267. {
  268. ID3D11RasterizerState *state = NULL;
  269. if (!rasterStateChanged)
  270. return;
  271. for (size_t i = 0; i < rasterStates.size(); i++) {
  272. SavedRasterState &s = rasterStates[i];
  273. if (memcmp(&s, &rasterState, sizeof(rasterState)) == 0) {
  274. state = s.state;
  275. break;
  276. }
  277. }
  278. if (!state)
  279. state = AddRasterState();
  280. if (state != curRasterState) {
  281. context->RSSetState(state);
  282. curRasterState = state;
  283. }
  284. rasterStateChanged = false;
  285. }
  286. void gs_device::UpdateBlendState()
  287. {
  288. ID3D11BlendState *state = NULL;
  289. if (!blendStateChanged)
  290. return;
  291. for (size_t i = 0; i < blendStates.size(); i++) {
  292. SavedBlendState &s = blendStates[i];
  293. if (memcmp(&s, &blendState, sizeof(blendState)) == 0) {
  294. state = s.state;
  295. break;
  296. }
  297. }
  298. if (!state)
  299. state = AddBlendState();
  300. if (state != curBlendState) {
  301. float f[4] = {1.0f, 1.0f, 1.0f, 1.0f};
  302. context->OMSetBlendState(state, f, 0xFFFFFFFF);
  303. curBlendState = state;
  304. }
  305. blendStateChanged = false;
  306. }
  307. void gs_device::UpdateViewProjMatrix()
  308. {
  309. gs_matrix_get(&curViewMatrix);
  310. /* negate Z col of the view matrix for right-handed coordinate system */
  311. curViewMatrix.x.z = -curViewMatrix.x.z;
  312. curViewMatrix.y.z = -curViewMatrix.y.z;
  313. curViewMatrix.z.z = -curViewMatrix.z.z;
  314. curViewMatrix.t.z = -curViewMatrix.t.z;
  315. matrix4_mul(&curViewProjMatrix, &curViewMatrix, &curProjMatrix);
  316. matrix4_transpose(&curViewProjMatrix, &curViewProjMatrix);
  317. if (curVertexShader->viewProj)
  318. gs_shader_set_matrix4(curVertexShader->viewProj,
  319. &curViewProjMatrix);
  320. }
  321. gs_device::gs_device(gs_init_data *data)
  322. : curRenderTarget (NULL),
  323. curZStencilBuffer (NULL),
  324. curRenderSide (0),
  325. curIndexBuffer (NULL),
  326. curVertexBuffer (NULL),
  327. curVertexShader (NULL),
  328. curPixelShader (NULL),
  329. curSwapChain (&defaultSwap),
  330. zstencilStateChanged (true),
  331. rasterStateChanged (true),
  332. blendStateChanged (true),
  333. curDepthStencilState (NULL),
  334. curRasterState (NULL),
  335. curBlendState (NULL),
  336. curToplogy (D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED)
  337. {
  338. ComPtr<IDXGIAdapter1> adapter;
  339. matrix4_identity(&curProjMatrix);
  340. matrix4_identity(&curViewMatrix);
  341. matrix4_identity(&curViewProjMatrix);
  342. memset(&viewport, 0, sizeof(viewport));
  343. for (size_t i = 0; i < GS_MAX_TEXTURES; i++) {
  344. curTextures[i] = NULL;
  345. curSamplers[i] = NULL;
  346. }
  347. InitFactory(data->adapter, adapter.Assign());
  348. InitDevice(data, adapter);
  349. device_set_render_target(this, NULL, NULL);
  350. }
  351. const char *device_get_name(void)
  352. {
  353. return "Direct3D 11";
  354. }
  355. int device_get_type(void)
  356. {
  357. return GS_DEVICE_DIRECT3D_11;
  358. }
  359. const char *device_preprocessor_name(void)
  360. {
  361. return "_D3D11";
  362. }
  363. int device_create(gs_device_t *p_device, gs_init_data *data)
  364. {
  365. gs_device *device = NULL;
  366. int errorcode = GS_SUCCESS;
  367. try {
  368. device = new gs_device(data);
  369. } catch (UnsupportedHWError error) {
  370. blog(LOG_ERROR, "device_create (D3D11): %s (%08lX)", error.str,
  371. error.hr);
  372. errorcode = GS_ERROR_NOT_SUPPORTED;
  373. } catch (HRError error) {
  374. blog(LOG_ERROR, "device_create (D3D11): %s (%08lX)", error.str,
  375. error.hr);
  376. errorcode = GS_ERROR_FAIL;
  377. }
  378. *p_device = device;
  379. return errorcode;
  380. }
  381. void device_destroy(gs_device_t device)
  382. {
  383. delete device;
  384. }
  385. void device_enter_context(gs_device_t device)
  386. {
  387. /* does nothing */
  388. UNUSED_PARAMETER(device);
  389. }
  390. void device_leave_context(gs_device_t device)
  391. {
  392. /* does nothing */
  393. UNUSED_PARAMETER(device);
  394. }
  395. gs_swapchain_t device_swapchain_create(gs_device_t device,
  396. struct gs_init_data *data)
  397. {
  398. gs_swap_chain *swap = NULL;
  399. try {
  400. swap = new gs_swap_chain(device, data);
  401. } catch (HRError error) {
  402. blog(LOG_ERROR, "device_swapchain_create (D3D11): %s (%08lX)",
  403. error.str, error.hr);
  404. }
  405. return swap;
  406. }
  407. void device_resize(gs_device_t device, uint32_t cx, uint32_t cy)
  408. {
  409. try {
  410. ID3D11RenderTargetView *renderView = NULL;
  411. ID3D11DepthStencilView *depthView = NULL;
  412. int i = device->curRenderSide;
  413. device->context->OMSetRenderTargets(1, &renderView, depthView);
  414. device->curSwapChain->Resize(cx, cy);
  415. if (device->curRenderTarget)
  416. renderView = device->curRenderTarget->renderTarget[i];
  417. if (device->curZStencilBuffer)
  418. depthView = device->curZStencilBuffer->view;
  419. device->context->OMSetRenderTargets(1, &renderView, depthView);
  420. } catch (HRError error) {
  421. blog(LOG_ERROR, "device_resize (D3D11): %s (%08lX)",
  422. error.str, error.hr);
  423. }
  424. }
  425. void device_get_size(gs_device_t device, uint32_t *cx, uint32_t *cy)
  426. {
  427. *cx = device->curSwapChain->target.width;
  428. *cy = device->curSwapChain->target.height;
  429. }
  430. uint32_t device_get_width(gs_device_t device)
  431. {
  432. return device->curSwapChain->target.width;
  433. }
  434. uint32_t device_get_height(gs_device_t device)
  435. {
  436. return device->curSwapChain->target.height;
  437. }
  438. gs_texture_t device_texture_create(gs_device_t device, uint32_t width,
  439. uint32_t height, enum gs_color_format color_format,
  440. uint32_t levels, const uint8_t **data, uint32_t flags)
  441. {
  442. gs_texture *texture = NULL;
  443. try {
  444. texture = new gs_texture_2d(device, width, height, color_format,
  445. levels, data, flags, GS_TEXTURE_2D, false,
  446. false);
  447. } catch (HRError error) {
  448. blog(LOG_ERROR, "device_texture_create (D3D11): %s (%08lX)",
  449. error.str, error.hr);
  450. } catch (const char *error) {
  451. blog(LOG_ERROR, "device_texture_create (D3D11): %s", error);
  452. }
  453. return texture;
  454. }
  455. gs_texture_t device_cubetexture_create(gs_device_t device, uint32_t size,
  456. enum gs_color_format color_format, uint32_t levels,
  457. const uint8_t **data, uint32_t flags)
  458. {
  459. gs_texture *texture = NULL;
  460. try {
  461. texture = new gs_texture_2d(device, size, size, color_format,
  462. levels, data, flags, GS_TEXTURE_CUBE, false,
  463. false);
  464. } catch (HRError error) {
  465. blog(LOG_ERROR, "device_cubetexture_create (D3D11): %s "
  466. "(%08lX)",
  467. error.str, error.hr);
  468. } catch (const char *error) {
  469. blog(LOG_ERROR, "device_cubetexture_create (D3D11): %s",
  470. error);
  471. }
  472. return texture;
  473. }
  474. gs_texture_t device_voltexture_create(gs_device_t device, uint32_t width,
  475. uint32_t height, uint32_t depth,
  476. enum gs_color_format color_format, uint32_t levels,
  477. const uint8_t **data, uint32_t flags)
  478. {
  479. /* TODO */
  480. UNUSED_PARAMETER(device);
  481. UNUSED_PARAMETER(width);
  482. UNUSED_PARAMETER(height);
  483. UNUSED_PARAMETER(depth);
  484. UNUSED_PARAMETER(color_format);
  485. UNUSED_PARAMETER(levels);
  486. UNUSED_PARAMETER(data);
  487. UNUSED_PARAMETER(flags);
  488. return NULL;
  489. }
  490. gs_zstencil_t device_zstencil_create(gs_device_t device, uint32_t width,
  491. uint32_t height, enum gs_zstencil_format format)
  492. {
  493. gs_zstencil_buffer *zstencil = NULL;
  494. try {
  495. zstencil = new gs_zstencil_buffer(device, width, height,
  496. format);
  497. } catch (HRError error) {
  498. blog(LOG_ERROR, "device_zstencil_create (D3D11): %s (%08lX)",
  499. error.str, error.hr);
  500. }
  501. return zstencil;
  502. }
  503. gs_stagesurf_t device_stagesurface_create(gs_device_t device, uint32_t width,
  504. uint32_t height, enum gs_color_format color_format)
  505. {
  506. gs_stage_surface *surf = NULL;
  507. try {
  508. surf = new gs_stage_surface(device, width, height,
  509. color_format);
  510. } catch (HRError error) {
  511. blog(LOG_ERROR, "device_stagesurface_create (D3D11): %s "
  512. "(%08lX)",
  513. error.str, error.hr);
  514. }
  515. return surf;
  516. }
  517. gs_samplerstate_t device_samplerstate_create(gs_device_t device,
  518. struct gs_sampler_info *info)
  519. {
  520. gs_sampler_state *ss = NULL;
  521. try {
  522. ss = new gs_sampler_state(device, info);
  523. } catch (HRError error) {
  524. blog(LOG_ERROR, "device_samplerstate_create (D3D11): %s "
  525. "(%08lX)",
  526. error.str, error.hr);
  527. }
  528. return ss;
  529. }
  530. gs_shader_t device_vertexshader_create(gs_device_t device,
  531. const char *shader_string, const char *file,
  532. char **error_string)
  533. {
  534. gs_vertex_shader *shader = NULL;
  535. try {
  536. shader = new gs_vertex_shader(device, file, shader_string);
  537. } catch (HRError error) {
  538. blog(LOG_ERROR, "device_vertexshader_create (D3D11): %s "
  539. "(%08lX)",
  540. error.str, error.hr);
  541. } catch (ShaderError error) {
  542. const char *buf = (const char*)error.errors->GetBufferPointer();
  543. if (error_string)
  544. *error_string = bstrdup(buf);
  545. blog(LOG_ERROR, "device_vertexshader_create (D3D11): "
  546. "Compile warnings/errors for %s:\n%s",
  547. file, buf);
  548. } catch (const char *error) {
  549. blog(LOG_ERROR, "device_vertexshader_create (D3D11): %s",
  550. error);
  551. }
  552. return shader;
  553. }
  554. gs_shader_t device_pixelshader_create(gs_device_t device,
  555. const char *shader_string, const char *file,
  556. char **error_string)
  557. {
  558. gs_pixel_shader *shader = NULL;
  559. try {
  560. shader = new gs_pixel_shader(device, file, shader_string);
  561. } catch (HRError error) {
  562. blog(LOG_ERROR, "device_pixelshader_create (D3D11): %s "
  563. "(%08lX)",
  564. error.str, error.hr);
  565. } catch (ShaderError error) {
  566. const char *buf = (const char*)error.errors->GetBufferPointer();
  567. if (error_string)
  568. *error_string = bstrdup(buf);
  569. blog(LOG_ERROR, "device_pixelshader_create (D3D11): "
  570. "Compiler warnings/errors for %s:\n%s",
  571. file, buf);
  572. } catch (const char *error) {
  573. blog(LOG_ERROR, "device_pixelshader_create (D3D11): %s",
  574. error);
  575. }
  576. return shader;
  577. }
  578. gs_vertbuffer_t device_vertexbuffer_create(gs_device_t device,
  579. struct gs_vb_data *data, uint32_t flags)
  580. {
  581. gs_vertex_buffer *buffer = NULL;
  582. try {
  583. buffer = new gs_vertex_buffer(device, data, flags);
  584. } catch (HRError error) {
  585. blog(LOG_ERROR, "device_vertexbuffer_create (D3D11): %s "
  586. "(%08lX)",
  587. error.str, error.hr);
  588. } catch (const char *error) {
  589. blog(LOG_ERROR, "device_vertexbuffer_create (D3D11): %s",
  590. error);
  591. }
  592. return buffer;
  593. }
  594. gs_indexbuffer_t device_indexbuffer_create(gs_device_t device,
  595. enum gs_index_type type, void *indices, size_t num,
  596. uint32_t flags)
  597. {
  598. gs_index_buffer *buffer = NULL;
  599. try {
  600. buffer = new gs_index_buffer(device, type, indices, num, flags);
  601. } catch (HRError error) {
  602. blog(LOG_ERROR, "device_indexbuffer_create (D3D11): %s (%08lX)",
  603. error.str, error.hr);
  604. }
  605. return buffer;
  606. }
  607. enum gs_texture_type device_get_texture_type(gs_texture_t texture)
  608. {
  609. return texture->type;
  610. }
  611. void device_load_vertexbuffer(gs_device_t device, gs_vertbuffer_t vertbuffer)
  612. {
  613. if (device->curVertexBuffer == vertbuffer)
  614. return;
  615. device->curVertexBuffer = vertbuffer;
  616. if (!device->curVertexShader)
  617. return;
  618. vector<ID3D11Buffer*> buffers;
  619. vector<uint32_t> strides;
  620. vector<uint32_t> offsets;
  621. if (vertbuffer) {
  622. vertbuffer->MakeBufferList(device->curVertexShader,
  623. buffers, strides);
  624. } else {
  625. size_t buffersToClear =
  626. device->curVertexShader->NumBuffersExpected();
  627. buffers.resize(buffersToClear);
  628. strides.resize(buffersToClear);
  629. }
  630. offsets.resize(buffers.size());
  631. device->context->IASetVertexBuffers(0, (UINT)buffers.size(),
  632. buffers.data(), strides.data(), offsets.data());
  633. }
  634. void device_load_indexbuffer(gs_device_t device, gs_indexbuffer_t indexbuffer)
  635. {
  636. DXGI_FORMAT format;
  637. ID3D11Buffer *buffer;
  638. if (device->curIndexBuffer == indexbuffer)
  639. return;
  640. if (indexbuffer) {
  641. switch (indexbuffer->indexSize) {
  642. case 2: format = DXGI_FORMAT_R16_UINT; break;
  643. default:
  644. case 4: format = DXGI_FORMAT_R32_UINT; break;
  645. }
  646. buffer = indexbuffer->indexBuffer;
  647. } else {
  648. buffer = NULL;
  649. format = DXGI_FORMAT_R32_UINT;
  650. }
  651. device->curIndexBuffer = indexbuffer;
  652. device->context->IASetIndexBuffer(buffer, format, 0);
  653. }
  654. void device_load_texture(gs_device_t device, gs_texture_t tex, int unit)
  655. {
  656. ID3D11ShaderResourceView *view = NULL;
  657. if (device->curTextures[unit] == tex)
  658. return;
  659. if (tex)
  660. view = tex->shaderRes;
  661. device->curTextures[unit] = tex;
  662. device->context->PSSetShaderResources(unit, 1, &view);
  663. }
  664. void device_load_samplerstate(gs_device_t device,
  665. gs_samplerstate_t samplerstate, int unit)
  666. {
  667. ID3D11SamplerState *state = NULL;
  668. if (device->curSamplers[unit] == samplerstate)
  669. return;
  670. if (samplerstate)
  671. state = samplerstate->state;
  672. device->curSamplers[unit] = samplerstate;
  673. device->context->PSSetSamplers(unit, 1, &state);
  674. }
  675. void device_load_vertexshader(gs_device_t device, gs_shader_t vertshader)
  676. {
  677. ID3D11VertexShader *shader = NULL;
  678. ID3D11InputLayout *layout = NULL;
  679. ID3D11Buffer *constants = NULL;
  680. if (device->curVertexShader == vertshader)
  681. return;
  682. gs_vertex_shader *vs = static_cast<gs_vertex_shader*>(vertshader);
  683. gs_vertex_buffer *curVB = device->curVertexBuffer;
  684. if (vertshader) {
  685. if (vertshader->type != GS_SHADER_VERTEX) {
  686. blog(LOG_ERROR, "device_load_vertexshader (D3D11): "
  687. "Specified shader is not a vertex "
  688. "shader");
  689. return;
  690. }
  691. if (curVB)
  692. device_load_vertexbuffer(device, NULL);
  693. shader = vs->shader;
  694. layout = vs->layout;
  695. constants = vs->constants;
  696. }
  697. device->curVertexShader = vs;
  698. device->context->VSSetShader(shader, NULL, 0);
  699. device->context->IASetInputLayout(layout);
  700. device->context->VSSetConstantBuffers(0, 1, &constants);
  701. if (vertshader && curVB)
  702. device_load_vertexbuffer(device, curVB);
  703. }
  704. static inline void clear_textures(gs_device_t device)
  705. {
  706. ID3D11ShaderResourceView *views[GS_MAX_TEXTURES];
  707. memset(views, 0, sizeof(views));
  708. memset(device->curTextures, 0, sizeof(device->curTextures));
  709. device->context->PSSetShaderResources(0, GS_MAX_TEXTURES, views);
  710. }
  711. void device_load_pixelshader(gs_device_t device, gs_shader_t pixelshader)
  712. {
  713. ID3D11PixelShader *shader = NULL;
  714. ID3D11Buffer *constants = NULL;
  715. ID3D11SamplerState *states[GS_MAX_TEXTURES];
  716. if (device->curPixelShader == pixelshader)
  717. return;
  718. gs_pixel_shader *ps = static_cast<gs_pixel_shader*>(pixelshader);
  719. if (pixelshader) {
  720. if (pixelshader->type != GS_SHADER_PIXEL) {
  721. blog(LOG_ERROR, "device_load_pixelshader (D3D11): "
  722. "Specified shader is not a pixel "
  723. "shader");
  724. return;
  725. }
  726. shader = ps->shader;
  727. constants = ps->constants;
  728. ps->GetSamplerStates(states);
  729. } else {
  730. memset(states, 0, sizeof(states));
  731. }
  732. clear_textures(device);
  733. device->curPixelShader = ps;
  734. device->context->PSSetShader(shader, NULL, 0);
  735. device->context->PSSetConstantBuffers(0, 1, &constants);
  736. device->context->PSSetSamplers(0, GS_MAX_TEXTURES, states);
  737. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  738. if (device->curSamplers[i] &&
  739. device->curSamplers[i]->state != states[i])
  740. device->curSamplers[i] = nullptr;
  741. }
  742. void device_load_default_samplerstate(gs_device_t device, bool b_3d, int unit)
  743. {
  744. /* TODO */
  745. UNUSED_PARAMETER(device);
  746. UNUSED_PARAMETER(b_3d);
  747. UNUSED_PARAMETER(unit);
  748. }
  749. gs_shader_t device_get_vertex_shader(gs_device_t device)
  750. {
  751. return device->curVertexShader;
  752. }
  753. gs_shader_t device_get_pixel_shader(gs_device_t device)
  754. {
  755. return device->curPixelShader;
  756. }
  757. gs_texture_t device_get_render_target(gs_device_t device)
  758. {
  759. if (device->curRenderTarget == &device->curSwapChain->target)
  760. return NULL;
  761. return device->curRenderTarget;
  762. }
  763. gs_zstencil_t device_get_zstencil_target(gs_device_t device)
  764. {
  765. if (device->curZStencilBuffer == &device->curSwapChain->zs)
  766. return NULL;
  767. return device->curZStencilBuffer;
  768. }
  769. void device_set_render_target(gs_device_t device, gs_texture_t tex,
  770. gs_zstencil_t zstencil)
  771. {
  772. if (!tex)
  773. tex = &device->curSwapChain->target;
  774. if (!zstencil)
  775. zstencil = &device->curSwapChain->zs;
  776. if (device->curRenderTarget == tex &&
  777. device->curZStencilBuffer == zstencil)
  778. return;
  779. if (tex->type != GS_TEXTURE_2D) {
  780. blog(LOG_ERROR, "device_set_render_target (D3D11): "
  781. "texture is not a 2D texture");
  782. return;
  783. }
  784. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  785. if (!tex2d->renderTarget[0]) {
  786. blog(LOG_ERROR, "device_set_render_target (D3D11): "
  787. "texture is not a render target");
  788. return;
  789. }
  790. ID3D11RenderTargetView *rt = tex2d->renderTarget[0];
  791. device->curRenderTarget = tex2d;
  792. device->curRenderSide = 0;
  793. device->curZStencilBuffer = zstencil;
  794. device->context->OMSetRenderTargets(1, &rt, zstencil->view);
  795. }
  796. void device_set_cube_render_target(gs_device_t device, gs_texture_t tex,
  797. int side, gs_zstencil_t zstencil)
  798. {
  799. if (!tex) {
  800. tex = &device->curSwapChain->target;
  801. side = 0;
  802. }
  803. if (!zstencil)
  804. zstencil = &device->curSwapChain->zs;
  805. if (device->curRenderTarget == tex &&
  806. device->curRenderSide == side &&
  807. device->curZStencilBuffer == zstencil)
  808. return;
  809. if (tex->type != GS_TEXTURE_CUBE) {
  810. blog(LOG_ERROR, "device_set_cube_render_target (D3D11): "
  811. "texture is not a cube texture");
  812. return;
  813. }
  814. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  815. if (!tex2d->renderTarget[side]) {
  816. blog(LOG_ERROR, "device_set_cube_render_target (D3D11): "
  817. "texture is not a render target");
  818. return;
  819. }
  820. ID3D11RenderTargetView *rt = tex2d->renderTarget[0];
  821. device->curRenderTarget = tex2d;
  822. device->curRenderSide = side;
  823. device->curZStencilBuffer = zstencil;
  824. device->context->OMSetRenderTargets(1, &rt, zstencil->view);
  825. }
  826. inline void gs_device::CopyTex(ID3D11Texture2D *dst,
  827. uint32_t dst_x, uint32_t dst_y,
  828. gs_texture_t src, uint32_t src_x, uint32_t src_y,
  829. uint32_t src_w, uint32_t src_h)
  830. {
  831. if (src->type != GS_TEXTURE_2D)
  832. throw "Source texture must be a 2D texture";
  833. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(src);
  834. if (dst_x == 0 && dst_y == 0 &&
  835. src_x == 0 && src_y == 0 &&
  836. src_w == 0 && src_h == 0) {
  837. context->CopyResource(dst, tex2d->texture);
  838. } else {
  839. D3D11_BOX sbox;
  840. sbox.left = src_x;
  841. if (src_w > 0)
  842. sbox.right = src_x + src_w;
  843. else
  844. sbox.right = tex2d->width - 1;
  845. sbox.top = src_y;
  846. if (src_h > 0)
  847. sbox.bottom = src_y + src_h;
  848. else
  849. sbox.bottom = tex2d->height - 1;
  850. sbox.front = 0;
  851. sbox.back = 1;
  852. context->CopySubresourceRegion(dst, 0, dst_x, dst_y, 0,
  853. tex2d->texture, 0, &sbox);
  854. }
  855. }
  856. void device_copy_texture_region(gs_device_t device,
  857. gs_texture_t dst, uint32_t dst_x, uint32_t dst_y,
  858. gs_texture_t src, uint32_t src_x, uint32_t src_y,
  859. uint32_t src_w, uint32_t src_h)
  860. {
  861. try {
  862. gs_texture_2d *src2d = static_cast<gs_texture_2d*>(src);
  863. gs_texture_2d *dst2d = static_cast<gs_texture_2d*>(dst);
  864. if (!src)
  865. throw "Source texture is NULL";
  866. if (!dst)
  867. throw "Destination texture is NULL";
  868. if (src->type != GS_TEXTURE_2D || dst->type != GS_TEXTURE_2D)
  869. throw "Source and destination textures must be a 2D "
  870. "textures";
  871. if (dst->format != src->format)
  872. throw "Source and destination formats do not match";
  873. /* apparently casting to the same type that the variable
  874. * already exists as is supposed to prevent some warning
  875. * when used with the conditional operator? */
  876. uint32_t copyWidth = (uint32_t)src_w ?
  877. (uint32_t)src_w : (src2d->width - src_x);
  878. uint32_t copyHeight = (uint32_t)src_h ?
  879. (uint32_t)src_h : (src2d->height - src_y);
  880. uint32_t dstWidth = dst2d->width - dst_x;
  881. uint32_t dstHeight = dst2d->height - dst_y;
  882. if (dstWidth < copyWidth || dstHeight < copyHeight)
  883. throw "Destination texture region is not big "
  884. "enough to hold the source region";
  885. if (dst_x == 0 && dst_y == 0 &&
  886. src_x == 0 && src_y == 0 &&
  887. src_w == 0 && src_h == 0) {
  888. copyWidth = 0;
  889. copyHeight = 0;
  890. }
  891. device->CopyTex(dst2d->texture, dst_x, dst_y,
  892. src, src_x, src_y, copyWidth, copyHeight);
  893. } catch(const char *error) {
  894. blog(LOG_ERROR, "device_copy_texture (D3D11): %s", error);
  895. }
  896. }
  897. void device_copy_texture(gs_device_t device, gs_texture_t dst, gs_texture_t src)
  898. {
  899. device_copy_texture_region(device, dst, 0, 0, src, 0, 0, 0, 0);
  900. }
  901. void device_stage_texture(gs_device_t device, gs_stagesurf_t dst,
  902. gs_texture_t src)
  903. {
  904. try {
  905. gs_texture_2d *src2d = static_cast<gs_texture_2d*>(src);
  906. if (!src)
  907. throw "Source texture is NULL";
  908. if (src->type != GS_TEXTURE_2D)
  909. throw "Source texture must be a 2D texture";
  910. if (!dst)
  911. throw "Destination surface is NULL";
  912. if (dst->format != src->format)
  913. throw "Source and destination formats do not match";
  914. if (dst->width != src2d->width ||
  915. dst->height != src2d->height)
  916. throw "Source and destination must have the same "
  917. "dimensions";
  918. device->CopyTex(dst->texture, 0, 0, src, 0, 0, 0, 0);
  919. } catch (const char *error) {
  920. blog(LOG_ERROR, "device_copy_texture (D3D11): %s", error);
  921. }
  922. }
  923. void device_begin_scene(gs_device_t device)
  924. {
  925. clear_textures(device);
  926. }
  927. void device_draw(gs_device_t device, enum gs_draw_mode draw_mode,
  928. uint32_t start_vert, uint32_t num_verts)
  929. {
  930. try {
  931. if (!device->curVertexShader)
  932. throw "No vertex shader specified";
  933. if (!device->curPixelShader)
  934. throw "No pixel shader specified";
  935. if (!device->curVertexBuffer)
  936. throw "No vertex buffer specified";
  937. gs_effect_t effect = gs_get_effect();
  938. if (effect)
  939. gs_effect_update_params(effect);
  940. device->UpdateBlendState();
  941. device->UpdateRasterState();
  942. device->UpdateZStencilState();
  943. device->UpdateViewProjMatrix();
  944. device->curVertexShader->UploadParams();
  945. device->curPixelShader->UploadParams();
  946. } catch (const char *error) {
  947. blog(LOG_ERROR, "device_draw (D3D11): %s", error);
  948. return;
  949. } catch (HRError error) {
  950. blog(LOG_ERROR, "device_draw (D3D11): %s (%08lX)", error.str,
  951. error.hr);
  952. return;
  953. }
  954. D3D11_PRIMITIVE_TOPOLOGY newTopology = ConvertGSTopology(draw_mode);
  955. if (device->curToplogy != newTopology) {
  956. device->context->IASetPrimitiveTopology(newTopology);
  957. device->curToplogy = newTopology;
  958. }
  959. if (device->curIndexBuffer) {
  960. if (num_verts == 0)
  961. num_verts = (uint32_t)device->curIndexBuffer->num;
  962. device->context->DrawIndexed(num_verts, start_vert, 0);
  963. } else {
  964. if (num_verts == 0)
  965. num_verts = (uint32_t)device->curVertexBuffer->numVerts;
  966. device->context->Draw(num_verts, start_vert);
  967. }
  968. }
  969. void device_end_scene(gs_device_t device)
  970. {
  971. /* does nothing in D3D11 */
  972. UNUSED_PARAMETER(device);
  973. }
  974. void device_load_swapchain(gs_device_t device, gs_swapchain_t swapchain)
  975. {
  976. gs_texture_t target = device->curRenderTarget;
  977. gs_zstencil_t zs = device->curZStencilBuffer;
  978. bool is_cube = device->curRenderTarget->type == GS_TEXTURE_CUBE;
  979. if (target == &device->curSwapChain->target)
  980. target = NULL;
  981. if (zs == &device->curSwapChain->zs)
  982. zs = NULL;
  983. if (swapchain == NULL)
  984. swapchain = &device->defaultSwap;
  985. device->curSwapChain = swapchain;
  986. if (is_cube)
  987. device_set_cube_render_target(device, target,
  988. device->curRenderSide, zs);
  989. else
  990. device_set_render_target(device, target, zs);
  991. }
  992. void device_clear(gs_device_t device, uint32_t clear_flags, struct vec4 *color,
  993. float depth, uint8_t stencil)
  994. {
  995. int side = device->curRenderSide;
  996. if ((clear_flags & GS_CLEAR_COLOR) != 0 && device->curRenderTarget)
  997. device->context->ClearRenderTargetView(
  998. device->curRenderTarget->renderTarget[side],
  999. color->ptr);
  1000. if (device->curZStencilBuffer) {
  1001. uint32_t flags = 0;
  1002. if ((clear_flags & GS_CLEAR_DEPTH) != 0)
  1003. flags |= D3D11_CLEAR_DEPTH;
  1004. if ((clear_flags & GS_CLEAR_STENCIL) != 0)
  1005. flags |= D3D11_CLEAR_STENCIL;
  1006. if (flags && device->curZStencilBuffer->view)
  1007. device->context->ClearDepthStencilView(
  1008. device->curZStencilBuffer->view,
  1009. flags, depth, stencil);
  1010. }
  1011. }
  1012. void device_present(gs_device_t device)
  1013. {
  1014. device->curSwapChain->swap->Present(0, 0);
  1015. }
  1016. void device_flush(gs_device_t device)
  1017. {
  1018. device->context->Flush();
  1019. }
  1020. void device_set_cull_mode(gs_device_t device, enum gs_cull_mode mode)
  1021. {
  1022. if (mode == device->rasterState.cullMode)
  1023. return;
  1024. device->rasterState.cullMode = mode;
  1025. device->rasterStateChanged = true;
  1026. }
  1027. enum gs_cull_mode device_get_cull_mode(gs_device_t device)
  1028. {
  1029. return device->rasterState.cullMode;
  1030. }
  1031. void device_enable_blending(gs_device_t device, bool enable)
  1032. {
  1033. if (enable == device->blendState.blendEnabled)
  1034. return;
  1035. device->blendState.blendEnabled = enable;
  1036. device->blendStateChanged = true;
  1037. }
  1038. void device_enable_depth_test(gs_device_t device, bool enable)
  1039. {
  1040. if (enable == device->zstencilState.depthEnabled)
  1041. return;
  1042. device->zstencilState.depthEnabled = enable;
  1043. device->zstencilStateChanged = true;
  1044. }
  1045. void device_enable_stencil_test(gs_device_t device, bool enable)
  1046. {
  1047. if (enable == device->zstencilState.stencilEnabled)
  1048. return;
  1049. device->zstencilState.stencilEnabled = enable;
  1050. device->zstencilStateChanged = true;
  1051. }
  1052. void device_enable_stencil_write(gs_device_t device, bool enable)
  1053. {
  1054. if (enable == device->zstencilState.stencilWriteEnabled)
  1055. return;
  1056. device->zstencilState.stencilWriteEnabled = enable;
  1057. device->zstencilStateChanged = true;
  1058. }
  1059. void device_enable_color(gs_device_t device, bool red, bool green,
  1060. bool blue, bool alpha)
  1061. {
  1062. if (device->blendState.redEnabled == red &&
  1063. device->blendState.greenEnabled == green &&
  1064. device->blendState.blueEnabled == blue &&
  1065. device->blendState.alphaEnabled == alpha)
  1066. return;
  1067. device->blendState.redEnabled = red;
  1068. device->blendState.greenEnabled = green;
  1069. device->blendState.blueEnabled = blue;
  1070. device->blendState.alphaEnabled = alpha;
  1071. device->blendStateChanged = true;
  1072. }
  1073. void device_blend_function(gs_device_t device, enum gs_blend_type src,
  1074. enum gs_blend_type dest)
  1075. {
  1076. if (device->blendState.srcFactor == src &&
  1077. device->blendState.destFactor == dest)
  1078. return;
  1079. device->blendState.srcFactor = src;
  1080. device->blendState.destFactor = dest;
  1081. device->blendStateChanged = true;
  1082. }
  1083. void device_depth_function(gs_device_t device, enum gs_depth_test test)
  1084. {
  1085. if (device->zstencilState.depthFunc == test)
  1086. return;
  1087. device->zstencilState.depthFunc = test;
  1088. device->zstencilStateChanged = true;
  1089. }
  1090. static inline void update_stencilside_test(gs_device_t device, StencilSide &side,
  1091. gs_depth_test test)
  1092. {
  1093. if (side.test == test)
  1094. return;
  1095. side.test = test;
  1096. device->zstencilStateChanged = true;
  1097. }
  1098. void device_stencil_function(gs_device_t device, enum gs_stencil_side side,
  1099. enum gs_depth_test test)
  1100. {
  1101. int sideVal = (int)side;
  1102. if (sideVal & GS_STENCIL_FRONT)
  1103. update_stencilside_test(device,
  1104. device->zstencilState.stencilFront, test);
  1105. if (sideVal & GS_STENCIL_BACK)
  1106. update_stencilside_test(device,
  1107. device->zstencilState.stencilBack, test);
  1108. }
  1109. static inline void update_stencilside_op(gs_device_t device, StencilSide &side,
  1110. enum gs_stencil_op_type fail, enum gs_stencil_op_type zfail,
  1111. enum gs_stencil_op_type zpass)
  1112. {
  1113. if (side.fail == fail && side.zfail == zfail && side.zpass == zpass)
  1114. return;
  1115. side.fail = fail;
  1116. side.zfail = zfail;
  1117. side.zpass = zpass;
  1118. device->zstencilStateChanged = true;
  1119. }
  1120. void device_stencil_op(gs_device_t device, enum gs_stencil_side side,
  1121. enum gs_stencil_op_type fail, enum gs_stencil_op_type zfail,
  1122. enum gs_stencil_op_type zpass)
  1123. {
  1124. int sideVal = (int)side;
  1125. if (sideVal & GS_STENCIL_FRONT)
  1126. update_stencilside_op(device,
  1127. device->zstencilState.stencilFront,
  1128. fail, zfail, zpass);
  1129. if (sideVal & GS_STENCIL_BACK)
  1130. update_stencilside_op(device,
  1131. device->zstencilState.stencilBack,
  1132. fail, zfail, zpass);
  1133. }
  1134. void device_set_viewport(gs_device_t device, int x, int y, int width,
  1135. int height)
  1136. {
  1137. D3D11_VIEWPORT vp;
  1138. memset(&vp, 0, sizeof(vp));
  1139. vp.MaxDepth = 1.0f;
  1140. vp.TopLeftX = (float)x;
  1141. vp.TopLeftY = (float)y;
  1142. vp.Width = (float)width;
  1143. vp.Height = (float)height;
  1144. device->context->RSSetViewports(1, &vp);
  1145. device->viewport.x = x;
  1146. device->viewport.y = y;
  1147. device->viewport.cx = width;
  1148. device->viewport.cy = height;
  1149. }
  1150. void device_get_viewport(gs_device_t device, struct gs_rect *rect)
  1151. {
  1152. memcpy(rect, &device->viewport, sizeof(gs_rect));
  1153. }
  1154. void device_set_scissor_rect(gs_device_t device, struct gs_rect *rect)
  1155. {
  1156. D3D11_RECT d3drect;
  1157. device->rasterState.scissorEnabled = (rect != NULL);
  1158. if (rect != NULL) {
  1159. d3drect.left = rect->x;
  1160. d3drect.top = rect->y;
  1161. d3drect.right = rect->x + rect->cx;
  1162. d3drect.bottom = rect->y + rect->cy;
  1163. device->context->RSSetScissorRects(1, &d3drect);
  1164. }
  1165. device->rasterStateChanged = true;
  1166. }
  1167. void device_ortho(gs_device_t device, float left, float right, float top,
  1168. float bottom, float zNear, float zFar)
  1169. {
  1170. matrix4 *dst = &device->curProjMatrix;
  1171. float rml = right-left;
  1172. float bmt = bottom-top;
  1173. float fmn = zFar-zNear;
  1174. vec4_zero(&dst->x);
  1175. vec4_zero(&dst->y);
  1176. vec4_zero(&dst->z);
  1177. vec4_zero(&dst->t);
  1178. dst->x.x = 2.0f / rml;
  1179. dst->t.x = (left+right) / -rml;
  1180. dst->y.y = 2.0f / -bmt;
  1181. dst->t.y = (bottom+top) / bmt;
  1182. dst->z.z = 1.0f / fmn;
  1183. dst->t.z = zNear / -fmn;
  1184. dst->t.w = 1.0f;
  1185. }
  1186. void device_frustum(gs_device_t device, float left, float right, float top,
  1187. float bottom, float zNear, float zFar)
  1188. {
  1189. matrix4 *dst = &device->curProjMatrix;
  1190. float rml = right-left;
  1191. float bmt = bottom-top;
  1192. float fmn = zFar-zNear;
  1193. float nearx2 = 2.0f*zNear;
  1194. vec4_zero(&dst->x);
  1195. vec4_zero(&dst->y);
  1196. vec4_zero(&dst->z);
  1197. vec4_zero(&dst->t);
  1198. dst->x.x = nearx2 / rml;
  1199. dst->z.x = (left+right) / -rml;
  1200. dst->y.y = nearx2 / -bmt;
  1201. dst->z.y = (bottom+top) / bmt;
  1202. dst->z.z = zFar / fmn;
  1203. dst->t.z = (zNear*zFar) / -fmn;
  1204. dst->z.w = 1.0f;
  1205. }
  1206. void device_projection_push(gs_device_t device)
  1207. {
  1208. mat4float mat;
  1209. memcpy(&mat, &device->curProjMatrix, sizeof(matrix4));
  1210. device->projStack.push_back(mat);
  1211. }
  1212. void device_projection_pop(gs_device_t device)
  1213. {
  1214. if (!device->projStack.size())
  1215. return;
  1216. mat4float *mat = device->projStack.data();
  1217. size_t end = device->projStack.size()-1;
  1218. /* XXX - does anyone know a better way of doing this? */
  1219. memcpy(&device->curProjMatrix, mat+end, sizeof(matrix4));
  1220. device->projStack.pop_back();
  1221. }
  1222. void gs_swapchain_destroy(gs_swapchain_t swapchain)
  1223. {
  1224. if (!swapchain)
  1225. return;
  1226. gs_device *device = swapchain->device;
  1227. if (device->curSwapChain == swapchain)
  1228. device->curSwapChain = &device->defaultSwap;
  1229. delete swapchain;
  1230. }
  1231. void gs_texture_destroy(gs_texture_t tex)
  1232. {
  1233. delete tex;
  1234. }
  1235. uint32_t gs_texture_get_width(gs_texture_t tex)
  1236. {
  1237. if (tex->type != GS_TEXTURE_2D)
  1238. return 0;
  1239. return static_cast<gs_texture_2d*>(tex)->width;
  1240. }
  1241. uint32_t gs_texture_get_height(gs_texture_t tex)
  1242. {
  1243. if (tex->type != GS_TEXTURE_2D)
  1244. return 0;
  1245. return static_cast<gs_texture_2d*>(tex)->height;
  1246. }
  1247. enum gs_color_format gs_texture_get_color_format(gs_texture_t tex)
  1248. {
  1249. if (tex->type != GS_TEXTURE_2D)
  1250. return GS_UNKNOWN;
  1251. return static_cast<gs_texture_2d*>(tex)->format;
  1252. }
  1253. bool gs_texture_map(gs_texture_t tex, uint8_t **ptr, uint32_t *linesize)
  1254. {
  1255. HRESULT hr;
  1256. if (tex->type != GS_TEXTURE_2D)
  1257. return false;
  1258. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  1259. D3D11_MAPPED_SUBRESOURCE map;
  1260. hr = tex2d->device->context->Map(tex2d->texture, 0,
  1261. D3D11_MAP_WRITE_DISCARD, 0, &map);
  1262. if (FAILED(hr))
  1263. return false;
  1264. *ptr = (uint8_t*)map.pData;
  1265. *linesize = map.RowPitch;
  1266. return true;
  1267. }
  1268. void gs_texture_unmap(gs_texture_t tex)
  1269. {
  1270. if (tex->type != GS_TEXTURE_2D)
  1271. return;
  1272. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  1273. tex2d->device->context->Unmap(tex2d->texture, 0);
  1274. }
  1275. void *gs_texture_get_obj(gs_texture_t tex)
  1276. {
  1277. if (tex->type != GS_TEXTURE_2D)
  1278. return nullptr;
  1279. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  1280. return tex2d->texture.Get();
  1281. }
  1282. void gs_cubetexture_destroy(gs_texture_t cubetex)
  1283. {
  1284. delete cubetex;
  1285. }
  1286. uint32_t gs_cubetexture_get_size(gs_texture_t cubetex)
  1287. {
  1288. if (cubetex->type != GS_TEXTURE_CUBE)
  1289. return 0;
  1290. gs_texture_2d *tex = static_cast<gs_texture_2d*>(cubetex);
  1291. return tex->width;
  1292. }
  1293. enum gs_color_format gs_cubetexture_get_color_format(gs_texture_t cubetex)
  1294. {
  1295. if (cubetex->type != GS_TEXTURE_CUBE)
  1296. return GS_UNKNOWN;
  1297. gs_texture_2d *tex = static_cast<gs_texture_2d*>(cubetex);
  1298. return tex->format;
  1299. }
  1300. void gs_voltexture_destroy(gs_texture_t voltex)
  1301. {
  1302. delete voltex;
  1303. }
  1304. uint32_t gs_voltexture_get_width(gs_texture_t voltex)
  1305. {
  1306. /* TODO */
  1307. UNUSED_PARAMETER(voltex);
  1308. return 0;
  1309. }
  1310. uint32_t gs_voltexture_get_height(gs_texture_t voltex)
  1311. {
  1312. /* TODO */
  1313. UNUSED_PARAMETER(voltex);
  1314. return 0;
  1315. }
  1316. uint32_t gs_voltexture_getdepth(gs_texture_t voltex)
  1317. {
  1318. /* TODO */
  1319. UNUSED_PARAMETER(voltex);
  1320. return 0;
  1321. }
  1322. enum gs_color_format gs_voltexture_get_color_format(gs_texture_t voltex)
  1323. {
  1324. /* TODO */
  1325. UNUSED_PARAMETER(voltex);
  1326. return GS_UNKNOWN;
  1327. }
  1328. void gs_stagesurface_destroy(gs_stagesurf_t stagesurf)
  1329. {
  1330. delete stagesurf;
  1331. }
  1332. uint32_t gs_stagesurface_get_width(gs_stagesurf_t stagesurf)
  1333. {
  1334. return stagesurf->width;
  1335. }
  1336. uint32_t gs_stagesurface_get_height(gs_stagesurf_t stagesurf)
  1337. {
  1338. return stagesurf->height;
  1339. }
  1340. enum gs_color_format gs_stagesurface_get_color_format(gs_stagesurf_t stagesurf)
  1341. {
  1342. return stagesurf->format;
  1343. }
  1344. bool gs_stagesurface_map(gs_stagesurf_t stagesurf, uint8_t **data,
  1345. uint32_t *linesize)
  1346. {
  1347. D3D11_MAPPED_SUBRESOURCE map;
  1348. if (FAILED(stagesurf->device->context->Map(stagesurf->texture, 0,
  1349. D3D11_MAP_READ, 0, &map)))
  1350. return false;
  1351. *data = (uint8_t*)map.pData;
  1352. *linesize = map.RowPitch;
  1353. return true;
  1354. }
  1355. void gs_stagesurface_unmap(gs_stagesurf_t stagesurf)
  1356. {
  1357. stagesurf->device->context->Unmap(stagesurf->texture, 0);
  1358. }
  1359. void gs_zstencil_destroy(gs_zstencil_t zstencil)
  1360. {
  1361. delete zstencil;
  1362. }
  1363. void gs_samplerstate_destroy(gs_samplerstate_t samplerstate)
  1364. {
  1365. if (!samplerstate)
  1366. return;
  1367. if (samplerstate->device)
  1368. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1369. if (samplerstate->device->curSamplers[i] ==
  1370. samplerstate)
  1371. samplerstate->device->curSamplers[i] = nullptr;
  1372. delete samplerstate;
  1373. }
  1374. void gs_vertexbuffer_destroy(gs_vertbuffer_t vertbuffer)
  1375. {
  1376. delete vertbuffer;
  1377. }
  1378. void gs_vertexbuffer_flush(gs_vertbuffer_t vertbuffer)
  1379. {
  1380. if (!vertbuffer->dynamic) {
  1381. blog(LOG_ERROR, "gs_vertexbuffer_flush: vertex buffer is "
  1382. "not dynamic");
  1383. return;
  1384. }
  1385. vertbuffer->FlushBuffer(vertbuffer->vertexBuffer,
  1386. vertbuffer->vbd.data->points, sizeof(vec3));
  1387. if (vertbuffer->normalBuffer)
  1388. vertbuffer->FlushBuffer(vertbuffer->normalBuffer,
  1389. vertbuffer->vbd.data->normals, sizeof(vec3));
  1390. if (vertbuffer->tangentBuffer)
  1391. vertbuffer->FlushBuffer(vertbuffer->tangentBuffer,
  1392. vertbuffer->vbd.data->tangents, sizeof(vec3));
  1393. if (vertbuffer->colorBuffer)
  1394. vertbuffer->FlushBuffer(vertbuffer->colorBuffer,
  1395. vertbuffer->vbd.data->colors, sizeof(uint32_t));
  1396. for (size_t i = 0; i < vertbuffer->uvBuffers.size(); i++) {
  1397. gs_tvertarray &tv = vertbuffer->vbd.data->tvarray[i];
  1398. vertbuffer->FlushBuffer(vertbuffer->uvBuffers[i],
  1399. tv.array, tv.width*sizeof(float));
  1400. }
  1401. }
  1402. struct gs_vb_data *gs_vertexbuffer_get_data(gs_vertbuffer_t vertbuffer)
  1403. {
  1404. return vertbuffer->vbd.data;
  1405. }
  1406. void gs_indexbuffer_destroy(gs_indexbuffer_t indexbuffer)
  1407. {
  1408. delete indexbuffer;
  1409. }
  1410. void gs_indexbuffer_flush(gs_indexbuffer_t indexbuffer)
  1411. {
  1412. HRESULT hr;
  1413. if (!indexbuffer->dynamic)
  1414. return;
  1415. D3D11_MAPPED_SUBRESOURCE map;
  1416. hr = indexbuffer->device->context->Map(indexbuffer->indexBuffer, 0,
  1417. D3D11_MAP_WRITE_DISCARD, 0, &map);
  1418. if (FAILED(hr))
  1419. return;
  1420. memcpy(map.pData, indexbuffer->indices.data,
  1421. indexbuffer->num * indexbuffer->indexSize);
  1422. indexbuffer->device->context->Unmap(indexbuffer->indexBuffer, 0);
  1423. }
  1424. void *gs_indexbuffer_get_data(gs_indexbuffer_t indexbuffer)
  1425. {
  1426. return indexbuffer->indices.data;
  1427. }
  1428. size_t gs_indexbuffer_get_num_indices(gs_indexbuffer_t indexbuffer)
  1429. {
  1430. return indexbuffer->num;
  1431. }
  1432. enum gs_index_type gs_indexbuffer_get_type(gs_indexbuffer_t indexbuffer)
  1433. {
  1434. return indexbuffer->type;
  1435. }
  1436. extern "C" EXPORT bool device_gdi_texture_available(void)
  1437. {
  1438. return true;
  1439. }
  1440. extern "C" EXPORT gs_texture_t device_texture_create_gdi(gs_device_t device,
  1441. uint32_t width, uint32_t height)
  1442. {
  1443. gs_texture *texture = nullptr;
  1444. try {
  1445. texture = new gs_texture_2d(device, width, height, GS_BGRA,
  1446. 1, nullptr, GS_RENDER_TARGET, GS_TEXTURE_2D,
  1447. true, false);
  1448. } catch (HRError error) {
  1449. blog(LOG_ERROR, "device_texture_create_gdi (D3D11): %s (%08lX)",
  1450. error.str, error.hr);
  1451. } catch (const char *error) {
  1452. blog(LOG_ERROR, "device_texture_create_gdi (D3D11): %s", error);
  1453. }
  1454. return texture;
  1455. }
  1456. static inline bool TextureGDICompatible(gs_texture_2d *tex2d, const char *func)
  1457. {
  1458. if (!tex2d->isGDICompatible) {
  1459. blog(LOG_ERROR, "%s (D3D11): Texture is not GDI compatible",
  1460. func);
  1461. return false;
  1462. }
  1463. return true;
  1464. }
  1465. extern "C" EXPORT void *gs_texture_get_dc(gs_texture_t tex)
  1466. {
  1467. HDC hDC = nullptr;
  1468. if (tex->type != GS_TEXTURE_2D)
  1469. return nullptr;
  1470. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  1471. if (!TextureGDICompatible(tex2d, "gs_texture_get_dc"))
  1472. return nullptr;
  1473. tex2d->gdiSurface->GetDC(true, &hDC);
  1474. return hDC;
  1475. }
  1476. extern "C" EXPORT void gs_texture_release_dc(gs_texture_t tex)
  1477. {
  1478. if (tex->type != GS_TEXTURE_2D)
  1479. return;
  1480. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  1481. if (!TextureGDICompatible(tex2d, "gs_texture_release_dc"))
  1482. return;
  1483. tex2d->gdiSurface->ReleaseDC(nullptr);
  1484. }