d3d11-subsystem.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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,
  898. gs_texture_t *src)
  899. {
  900. device_copy_texture_region(device, dst, 0, 0, src, 0, 0, 0, 0);
  901. }
  902. void device_stage_texture(gs_device_t *device, gs_stagesurf_t *dst,
  903. gs_texture_t *src)
  904. {
  905. try {
  906. gs_texture_2d *src2d = static_cast<gs_texture_2d*>(src);
  907. if (!src)
  908. throw "Source texture is NULL";
  909. if (src->type != GS_TEXTURE_2D)
  910. throw "Source texture must be a 2D texture";
  911. if (!dst)
  912. throw "Destination surface is NULL";
  913. if (dst->format != src->format)
  914. throw "Source and destination formats do not match";
  915. if (dst->width != src2d->width ||
  916. dst->height != src2d->height)
  917. throw "Source and destination must have the same "
  918. "dimensions";
  919. device->CopyTex(dst->texture, 0, 0, src, 0, 0, 0, 0);
  920. } catch (const char *error) {
  921. blog(LOG_ERROR, "device_copy_texture (D3D11): %s", error);
  922. }
  923. }
  924. void device_begin_scene(gs_device_t *device)
  925. {
  926. clear_textures(device);
  927. }
  928. void device_draw(gs_device_t *device, enum gs_draw_mode draw_mode,
  929. uint32_t start_vert, uint32_t num_verts)
  930. {
  931. try {
  932. if (!device->curVertexShader)
  933. throw "No vertex shader specified";
  934. if (!device->curPixelShader)
  935. throw "No pixel shader specified";
  936. if (!device->curVertexBuffer)
  937. throw "No vertex buffer specified";
  938. gs_effect_t *effect = gs_get_effect();
  939. if (effect)
  940. gs_effect_update_params(effect);
  941. device->UpdateBlendState();
  942. device->UpdateRasterState();
  943. device->UpdateZStencilState();
  944. device->UpdateViewProjMatrix();
  945. device->curVertexShader->UploadParams();
  946. device->curPixelShader->UploadParams();
  947. } catch (const char *error) {
  948. blog(LOG_ERROR, "device_draw (D3D11): %s", error);
  949. return;
  950. } catch (HRError error) {
  951. blog(LOG_ERROR, "device_draw (D3D11): %s (%08lX)", error.str,
  952. error.hr);
  953. return;
  954. }
  955. D3D11_PRIMITIVE_TOPOLOGY newTopology = ConvertGSTopology(draw_mode);
  956. if (device->curToplogy != newTopology) {
  957. device->context->IASetPrimitiveTopology(newTopology);
  958. device->curToplogy = newTopology;
  959. }
  960. if (device->curIndexBuffer) {
  961. if (num_verts == 0)
  962. num_verts = (uint32_t)device->curIndexBuffer->num;
  963. device->context->DrawIndexed(num_verts, start_vert, 0);
  964. } else {
  965. if (num_verts == 0)
  966. num_verts = (uint32_t)device->curVertexBuffer->numVerts;
  967. device->context->Draw(num_verts, start_vert);
  968. }
  969. }
  970. void device_end_scene(gs_device_t *device)
  971. {
  972. /* does nothing in D3D11 */
  973. UNUSED_PARAMETER(device);
  974. }
  975. void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swapchain)
  976. {
  977. gs_texture_t *target = device->curRenderTarget;
  978. gs_zstencil_t *zs = device->curZStencilBuffer;
  979. bool is_cube = device->curRenderTarget->type == GS_TEXTURE_CUBE;
  980. if (target == &device->curSwapChain->target)
  981. target = NULL;
  982. if (zs == &device->curSwapChain->zs)
  983. zs = NULL;
  984. if (swapchain == NULL)
  985. swapchain = &device->defaultSwap;
  986. device->curSwapChain = swapchain;
  987. if (is_cube)
  988. device_set_cube_render_target(device, target,
  989. device->curRenderSide, zs);
  990. else
  991. device_set_render_target(device, target, zs);
  992. }
  993. void device_clear(gs_device_t *device, uint32_t clear_flags, struct vec4 *color,
  994. float depth, uint8_t stencil)
  995. {
  996. int side = device->curRenderSide;
  997. if ((clear_flags & GS_CLEAR_COLOR) != 0 && device->curRenderTarget)
  998. device->context->ClearRenderTargetView(
  999. device->curRenderTarget->renderTarget[side],
  1000. color->ptr);
  1001. if (device->curZStencilBuffer) {
  1002. uint32_t flags = 0;
  1003. if ((clear_flags & GS_CLEAR_DEPTH) != 0)
  1004. flags |= D3D11_CLEAR_DEPTH;
  1005. if ((clear_flags & GS_CLEAR_STENCIL) != 0)
  1006. flags |= D3D11_CLEAR_STENCIL;
  1007. if (flags && device->curZStencilBuffer->view)
  1008. device->context->ClearDepthStencilView(
  1009. device->curZStencilBuffer->view,
  1010. flags, depth, stencil);
  1011. }
  1012. }
  1013. void device_present(gs_device_t *device)
  1014. {
  1015. device->curSwapChain->swap->Present(0, 0);
  1016. }
  1017. void device_flush(gs_device_t *device)
  1018. {
  1019. device->context->Flush();
  1020. }
  1021. void device_set_cull_mode(gs_device_t *device, enum gs_cull_mode mode)
  1022. {
  1023. if (mode == device->rasterState.cullMode)
  1024. return;
  1025. device->rasterState.cullMode = mode;
  1026. device->rasterStateChanged = true;
  1027. }
  1028. enum gs_cull_mode device_get_cull_mode(gs_device_t *device)
  1029. {
  1030. return device->rasterState.cullMode;
  1031. }
  1032. void device_enable_blending(gs_device_t *device, bool enable)
  1033. {
  1034. if (enable == device->blendState.blendEnabled)
  1035. return;
  1036. device->blendState.blendEnabled = enable;
  1037. device->blendStateChanged = true;
  1038. }
  1039. void device_enable_depth_test(gs_device_t *device, bool enable)
  1040. {
  1041. if (enable == device->zstencilState.depthEnabled)
  1042. return;
  1043. device->zstencilState.depthEnabled = enable;
  1044. device->zstencilStateChanged = true;
  1045. }
  1046. void device_enable_stencil_test(gs_device_t *device, bool enable)
  1047. {
  1048. if (enable == device->zstencilState.stencilEnabled)
  1049. return;
  1050. device->zstencilState.stencilEnabled = enable;
  1051. device->zstencilStateChanged = true;
  1052. }
  1053. void device_enable_stencil_write(gs_device_t *device, bool enable)
  1054. {
  1055. if (enable == device->zstencilState.stencilWriteEnabled)
  1056. return;
  1057. device->zstencilState.stencilWriteEnabled = enable;
  1058. device->zstencilStateChanged = true;
  1059. }
  1060. void device_enable_color(gs_device_t *device, bool red, bool green,
  1061. bool blue, bool alpha)
  1062. {
  1063. if (device->blendState.redEnabled == red &&
  1064. device->blendState.greenEnabled == green &&
  1065. device->blendState.blueEnabled == blue &&
  1066. device->blendState.alphaEnabled == alpha)
  1067. return;
  1068. device->blendState.redEnabled = red;
  1069. device->blendState.greenEnabled = green;
  1070. device->blendState.blueEnabled = blue;
  1071. device->blendState.alphaEnabled = alpha;
  1072. device->blendStateChanged = true;
  1073. }
  1074. void device_blend_function(gs_device_t *device, enum gs_blend_type src,
  1075. enum gs_blend_type dest)
  1076. {
  1077. if (device->blendState.srcFactor == src &&
  1078. device->blendState.destFactor == dest)
  1079. return;
  1080. device->blendState.srcFactor = src;
  1081. device->blendState.destFactor = dest;
  1082. device->blendStateChanged = true;
  1083. }
  1084. void device_depth_function(gs_device_t *device, enum gs_depth_test test)
  1085. {
  1086. if (device->zstencilState.depthFunc == test)
  1087. return;
  1088. device->zstencilState.depthFunc = test;
  1089. device->zstencilStateChanged = true;
  1090. }
  1091. static inline void update_stencilside_test(gs_device_t *device,
  1092. StencilSide &side, gs_depth_test test)
  1093. {
  1094. if (side.test == test)
  1095. return;
  1096. side.test = test;
  1097. device->zstencilStateChanged = true;
  1098. }
  1099. void device_stencil_function(gs_device_t *device, enum gs_stencil_side side,
  1100. enum gs_depth_test test)
  1101. {
  1102. int sideVal = (int)side;
  1103. if (sideVal & GS_STENCIL_FRONT)
  1104. update_stencilside_test(device,
  1105. device->zstencilState.stencilFront, test);
  1106. if (sideVal & GS_STENCIL_BACK)
  1107. update_stencilside_test(device,
  1108. device->zstencilState.stencilBack, test);
  1109. }
  1110. static inline void update_stencilside_op(gs_device_t *device, StencilSide &side,
  1111. enum gs_stencil_op_type fail, enum gs_stencil_op_type zfail,
  1112. enum gs_stencil_op_type zpass)
  1113. {
  1114. if (side.fail == fail && side.zfail == zfail && side.zpass == zpass)
  1115. return;
  1116. side.fail = fail;
  1117. side.zfail = zfail;
  1118. side.zpass = zpass;
  1119. device->zstencilStateChanged = true;
  1120. }
  1121. void device_stencil_op(gs_device_t *device, enum gs_stencil_side side,
  1122. enum gs_stencil_op_type fail, enum gs_stencil_op_type zfail,
  1123. enum gs_stencil_op_type zpass)
  1124. {
  1125. int sideVal = (int)side;
  1126. if (sideVal & GS_STENCIL_FRONT)
  1127. update_stencilside_op(device,
  1128. device->zstencilState.stencilFront,
  1129. fail, zfail, zpass);
  1130. if (sideVal & GS_STENCIL_BACK)
  1131. update_stencilside_op(device,
  1132. device->zstencilState.stencilBack,
  1133. fail, zfail, zpass);
  1134. }
  1135. void device_set_viewport(gs_device_t *device, int x, int y, int width,
  1136. int height)
  1137. {
  1138. D3D11_VIEWPORT vp;
  1139. memset(&vp, 0, sizeof(vp));
  1140. vp.MaxDepth = 1.0f;
  1141. vp.TopLeftX = (float)x;
  1142. vp.TopLeftY = (float)y;
  1143. vp.Width = (float)width;
  1144. vp.Height = (float)height;
  1145. device->context->RSSetViewports(1, &vp);
  1146. device->viewport.x = x;
  1147. device->viewport.y = y;
  1148. device->viewport.cx = width;
  1149. device->viewport.cy = height;
  1150. }
  1151. void device_get_viewport(gs_device_t *device, struct gs_rect *rect)
  1152. {
  1153. memcpy(rect, &device->viewport, sizeof(gs_rect));
  1154. }
  1155. void device_set_scissor_rect(gs_device_t *device, struct gs_rect *rect)
  1156. {
  1157. D3D11_RECT d3drect;
  1158. device->rasterState.scissorEnabled = (rect != NULL);
  1159. if (rect != NULL) {
  1160. d3drect.left = rect->x;
  1161. d3drect.top = rect->y;
  1162. d3drect.right = rect->x + rect->cx;
  1163. d3drect.bottom = rect->y + rect->cy;
  1164. device->context->RSSetScissorRects(1, &d3drect);
  1165. }
  1166. device->rasterStateChanged = true;
  1167. }
  1168. void device_ortho(gs_device_t *device, float left, float right, float top,
  1169. float bottom, float zNear, float zFar)
  1170. {
  1171. matrix4 *dst = &device->curProjMatrix;
  1172. float rml = right-left;
  1173. float bmt = bottom-top;
  1174. float fmn = zFar-zNear;
  1175. vec4_zero(&dst->x);
  1176. vec4_zero(&dst->y);
  1177. vec4_zero(&dst->z);
  1178. vec4_zero(&dst->t);
  1179. dst->x.x = 2.0f / rml;
  1180. dst->t.x = (left+right) / -rml;
  1181. dst->y.y = 2.0f / -bmt;
  1182. dst->t.y = (bottom+top) / bmt;
  1183. dst->z.z = 1.0f / fmn;
  1184. dst->t.z = zNear / -fmn;
  1185. dst->t.w = 1.0f;
  1186. }
  1187. void device_frustum(gs_device_t *device, float left, float right, float top,
  1188. float bottom, float zNear, float zFar)
  1189. {
  1190. matrix4 *dst = &device->curProjMatrix;
  1191. float rml = right-left;
  1192. float bmt = bottom-top;
  1193. float fmn = zFar-zNear;
  1194. float nearx2 = 2.0f*zNear;
  1195. vec4_zero(&dst->x);
  1196. vec4_zero(&dst->y);
  1197. vec4_zero(&dst->z);
  1198. vec4_zero(&dst->t);
  1199. dst->x.x = nearx2 / rml;
  1200. dst->z.x = (left+right) / -rml;
  1201. dst->y.y = nearx2 / -bmt;
  1202. dst->z.y = (bottom+top) / bmt;
  1203. dst->z.z = zFar / fmn;
  1204. dst->t.z = (zNear*zFar) / -fmn;
  1205. dst->z.w = 1.0f;
  1206. }
  1207. void device_projection_push(gs_device_t *device)
  1208. {
  1209. mat4float mat;
  1210. memcpy(&mat, &device->curProjMatrix, sizeof(matrix4));
  1211. device->projStack.push_back(mat);
  1212. }
  1213. void device_projection_pop(gs_device_t *device)
  1214. {
  1215. if (!device->projStack.size())
  1216. return;
  1217. mat4float *mat = device->projStack.data();
  1218. size_t end = device->projStack.size()-1;
  1219. /* XXX - does anyone know a better way of doing this? */
  1220. memcpy(&device->curProjMatrix, mat+end, sizeof(matrix4));
  1221. device->projStack.pop_back();
  1222. }
  1223. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1224. {
  1225. if (!swapchain)
  1226. return;
  1227. gs_device *device = swapchain->device;
  1228. if (device->curSwapChain == swapchain)
  1229. device->curSwapChain = &device->defaultSwap;
  1230. delete swapchain;
  1231. }
  1232. void gs_texture_destroy(gs_texture_t *tex)
  1233. {
  1234. delete tex;
  1235. }
  1236. uint32_t gs_texture_get_width(gs_texture_t *tex)
  1237. {
  1238. if (tex->type != GS_TEXTURE_2D)
  1239. return 0;
  1240. return static_cast<gs_texture_2d*>(tex)->width;
  1241. }
  1242. uint32_t gs_texture_get_height(gs_texture_t *tex)
  1243. {
  1244. if (tex->type != GS_TEXTURE_2D)
  1245. return 0;
  1246. return static_cast<gs_texture_2d*>(tex)->height;
  1247. }
  1248. enum gs_color_format gs_texture_get_color_format(gs_texture_t *tex)
  1249. {
  1250. if (tex->type != GS_TEXTURE_2D)
  1251. return GS_UNKNOWN;
  1252. return static_cast<gs_texture_2d*>(tex)->format;
  1253. }
  1254. bool gs_texture_map(gs_texture_t *tex, uint8_t **ptr, uint32_t *linesize)
  1255. {
  1256. HRESULT hr;
  1257. if (tex->type != GS_TEXTURE_2D)
  1258. return false;
  1259. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  1260. D3D11_MAPPED_SUBRESOURCE map;
  1261. hr = tex2d->device->context->Map(tex2d->texture, 0,
  1262. D3D11_MAP_WRITE_DISCARD, 0, &map);
  1263. if (FAILED(hr))
  1264. return false;
  1265. *ptr = (uint8_t*)map.pData;
  1266. *linesize = map.RowPitch;
  1267. return true;
  1268. }
  1269. void gs_texture_unmap(gs_texture_t *tex)
  1270. {
  1271. if (tex->type != GS_TEXTURE_2D)
  1272. return;
  1273. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  1274. tex2d->device->context->Unmap(tex2d->texture, 0);
  1275. }
  1276. void *gs_texture_get_obj(gs_texture_t *tex)
  1277. {
  1278. if (tex->type != GS_TEXTURE_2D)
  1279. return nullptr;
  1280. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  1281. return tex2d->texture.Get();
  1282. }
  1283. void gs_cubetexture_destroy(gs_texture_t *cubetex)
  1284. {
  1285. delete cubetex;
  1286. }
  1287. uint32_t gs_cubetexture_get_size(gs_texture_t *cubetex)
  1288. {
  1289. if (cubetex->type != GS_TEXTURE_CUBE)
  1290. return 0;
  1291. gs_texture_2d *tex = static_cast<gs_texture_2d*>(cubetex);
  1292. return tex->width;
  1293. }
  1294. enum gs_color_format gs_cubetexture_get_color_format(gs_texture_t *cubetex)
  1295. {
  1296. if (cubetex->type != GS_TEXTURE_CUBE)
  1297. return GS_UNKNOWN;
  1298. gs_texture_2d *tex = static_cast<gs_texture_2d*>(cubetex);
  1299. return tex->format;
  1300. }
  1301. void gs_voltexture_destroy(gs_texture_t *voltex)
  1302. {
  1303. delete voltex;
  1304. }
  1305. uint32_t gs_voltexture_get_width(gs_texture_t *voltex)
  1306. {
  1307. /* TODO */
  1308. UNUSED_PARAMETER(voltex);
  1309. return 0;
  1310. }
  1311. uint32_t gs_voltexture_get_height(gs_texture_t *voltex)
  1312. {
  1313. /* TODO */
  1314. UNUSED_PARAMETER(voltex);
  1315. return 0;
  1316. }
  1317. uint32_t gs_voltexture_getdepth(gs_texture_t *voltex)
  1318. {
  1319. /* TODO */
  1320. UNUSED_PARAMETER(voltex);
  1321. return 0;
  1322. }
  1323. enum gs_color_format gs_voltexture_get_color_format(gs_texture_t *voltex)
  1324. {
  1325. /* TODO */
  1326. UNUSED_PARAMETER(voltex);
  1327. return GS_UNKNOWN;
  1328. }
  1329. void gs_stagesurface_destroy(gs_stagesurf_t *stagesurf)
  1330. {
  1331. delete stagesurf;
  1332. }
  1333. uint32_t gs_stagesurface_get_width(gs_stagesurf_t *stagesurf)
  1334. {
  1335. return stagesurf->width;
  1336. }
  1337. uint32_t gs_stagesurface_get_height(gs_stagesurf_t *stagesurf)
  1338. {
  1339. return stagesurf->height;
  1340. }
  1341. enum gs_color_format gs_stagesurface_get_color_format(gs_stagesurf_t *stagesurf)
  1342. {
  1343. return stagesurf->format;
  1344. }
  1345. bool gs_stagesurface_map(gs_stagesurf_t *stagesurf, uint8_t **data,
  1346. uint32_t *linesize)
  1347. {
  1348. D3D11_MAPPED_SUBRESOURCE map;
  1349. if (FAILED(stagesurf->device->context->Map(stagesurf->texture, 0,
  1350. D3D11_MAP_READ, 0, &map)))
  1351. return false;
  1352. *data = (uint8_t*)map.pData;
  1353. *linesize = map.RowPitch;
  1354. return true;
  1355. }
  1356. void gs_stagesurface_unmap(gs_stagesurf_t *stagesurf)
  1357. {
  1358. stagesurf->device->context->Unmap(stagesurf->texture, 0);
  1359. }
  1360. void gs_zstencil_destroy(gs_zstencil_t *zstencil)
  1361. {
  1362. delete zstencil;
  1363. }
  1364. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1365. {
  1366. if (!samplerstate)
  1367. return;
  1368. if (samplerstate->device)
  1369. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1370. if (samplerstate->device->curSamplers[i] ==
  1371. samplerstate)
  1372. samplerstate->device->curSamplers[i] = nullptr;
  1373. delete samplerstate;
  1374. }
  1375. void gs_vertexbuffer_destroy(gs_vertbuffer_t *vertbuffer)
  1376. {
  1377. delete vertbuffer;
  1378. }
  1379. void gs_vertexbuffer_flush(gs_vertbuffer_t *vertbuffer)
  1380. {
  1381. if (!vertbuffer->dynamic) {
  1382. blog(LOG_ERROR, "gs_vertexbuffer_flush: vertex buffer is "
  1383. "not dynamic");
  1384. return;
  1385. }
  1386. vertbuffer->FlushBuffer(vertbuffer->vertexBuffer,
  1387. vertbuffer->vbd.data->points, sizeof(vec3));
  1388. if (vertbuffer->normalBuffer)
  1389. vertbuffer->FlushBuffer(vertbuffer->normalBuffer,
  1390. vertbuffer->vbd.data->normals, sizeof(vec3));
  1391. if (vertbuffer->tangentBuffer)
  1392. vertbuffer->FlushBuffer(vertbuffer->tangentBuffer,
  1393. vertbuffer->vbd.data->tangents, sizeof(vec3));
  1394. if (vertbuffer->colorBuffer)
  1395. vertbuffer->FlushBuffer(vertbuffer->colorBuffer,
  1396. vertbuffer->vbd.data->colors, sizeof(uint32_t));
  1397. for (size_t i = 0; i < vertbuffer->uvBuffers.size(); i++) {
  1398. gs_tvertarray &tv = vertbuffer->vbd.data->tvarray[i];
  1399. vertbuffer->FlushBuffer(vertbuffer->uvBuffers[i],
  1400. tv.array, tv.width*sizeof(float));
  1401. }
  1402. }
  1403. struct gs_vb_data *gs_vertexbuffer_get_data(gs_vertbuffer_t *vertbuffer)
  1404. {
  1405. return vertbuffer->vbd.data;
  1406. }
  1407. void gs_indexbuffer_destroy(gs_indexbuffer_t *indexbuffer)
  1408. {
  1409. delete indexbuffer;
  1410. }
  1411. void gs_indexbuffer_flush(gs_indexbuffer_t *indexbuffer)
  1412. {
  1413. HRESULT hr;
  1414. if (!indexbuffer->dynamic)
  1415. return;
  1416. D3D11_MAPPED_SUBRESOURCE map;
  1417. hr = indexbuffer->device->context->Map(indexbuffer->indexBuffer, 0,
  1418. D3D11_MAP_WRITE_DISCARD, 0, &map);
  1419. if (FAILED(hr))
  1420. return;
  1421. memcpy(map.pData, indexbuffer->indices.data,
  1422. indexbuffer->num * indexbuffer->indexSize);
  1423. indexbuffer->device->context->Unmap(indexbuffer->indexBuffer, 0);
  1424. }
  1425. void *gs_indexbuffer_get_data(gs_indexbuffer_t *indexbuffer)
  1426. {
  1427. return indexbuffer->indices.data;
  1428. }
  1429. size_t gs_indexbuffer_get_num_indices(gs_indexbuffer_t *indexbuffer)
  1430. {
  1431. return indexbuffer->num;
  1432. }
  1433. enum gs_index_type gs_indexbuffer_get_type(gs_indexbuffer_t *indexbuffer)
  1434. {
  1435. return indexbuffer->type;
  1436. }
  1437. extern "C" EXPORT bool device_gdi_texture_available(void)
  1438. {
  1439. return true;
  1440. }
  1441. extern "C" EXPORT gs_texture_t *device_texture_create_gdi(gs_device_t *device,
  1442. uint32_t width, uint32_t height)
  1443. {
  1444. gs_texture *texture = nullptr;
  1445. try {
  1446. texture = new gs_texture_2d(device, width, height, GS_BGRA,
  1447. 1, nullptr, GS_RENDER_TARGET, GS_TEXTURE_2D,
  1448. true, false);
  1449. } catch (HRError error) {
  1450. blog(LOG_ERROR, "device_texture_create_gdi (D3D11): %s (%08lX)",
  1451. error.str, error.hr);
  1452. } catch (const char *error) {
  1453. blog(LOG_ERROR, "device_texture_create_gdi (D3D11): %s", error);
  1454. }
  1455. return texture;
  1456. }
  1457. static inline bool TextureGDICompatible(gs_texture_2d *tex2d, const char *func)
  1458. {
  1459. if (!tex2d->isGDICompatible) {
  1460. blog(LOG_ERROR, "%s (D3D11): Texture is not GDI compatible",
  1461. func);
  1462. return false;
  1463. }
  1464. return true;
  1465. }
  1466. extern "C" EXPORT void *gs_texture_get_dc(gs_texture_t *tex)
  1467. {
  1468. HDC hDC = nullptr;
  1469. if (tex->type != GS_TEXTURE_2D)
  1470. return nullptr;
  1471. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  1472. if (!TextureGDICompatible(tex2d, "gs_texture_get_dc"))
  1473. return nullptr;
  1474. tex2d->gdiSurface->GetDC(true, &hDC);
  1475. return hDC;
  1476. }
  1477. extern "C" EXPORT void gs_texture_release_dc(gs_texture_t *tex)
  1478. {
  1479. if (tex->type != GS_TEXTURE_2D)
  1480. return;
  1481. gs_texture_2d *tex2d = static_cast<gs_texture_2d*>(tex);
  1482. if (!TextureGDICompatible(tex2d, "gs_texture_release_dc"))
  1483. return;
  1484. tex2d->gdiSurface->ReleaseDC(nullptr);
  1485. }