d3d11-subsystem.cpp 50 KB

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