d3d11-subsystem.cpp 50 KB

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