d3d11-subsystem.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  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 <util/dstr.h>
  18. #include <util/util.hpp>
  19. #include <graphics/matrix3.h>
  20. #include <d3d9.h>
  21. #include "d3d11-subsystem.hpp"
  22. struct UnsupportedHWError : HRError {
  23. inline UnsupportedHWError(const char *str, HRESULT hr)
  24. : HRError(str, hr)
  25. {
  26. }
  27. };
  28. #ifdef _MSC_VER
  29. /* alignment warning - despite the fact that alignment is already fixed */
  30. #pragma warning(disable : 4316)
  31. #endif
  32. static inline void LogD3D11ErrorDetails(HRError error, gs_device_t *device)
  33. {
  34. if (error.hr == DXGI_ERROR_DEVICE_REMOVED) {
  35. HRESULT DeviceRemovedReason =
  36. device->device->GetDeviceRemovedReason();
  37. blog(LOG_ERROR, " Device Removed Reason: %08lX",
  38. DeviceRemovedReason);
  39. }
  40. }
  41. static const IID dxgiFactory2 = {0x50c83a1c,
  42. 0xe072,
  43. 0x4c48,
  44. {0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6,
  45. 0xd0}};
  46. gs_obj::gs_obj(gs_device_t *device_, gs_type type)
  47. : device(device_), obj_type(type)
  48. {
  49. prev_next = &device->first_obj;
  50. next = device->first_obj;
  51. device->first_obj = this;
  52. if (next)
  53. next->prev_next = &next;
  54. }
  55. gs_obj::~gs_obj()
  56. {
  57. if (prev_next)
  58. *prev_next = next;
  59. if (next)
  60. next->prev_next = prev_next;
  61. }
  62. static inline void make_swap_desc(DXGI_SWAP_CHAIN_DESC &desc,
  63. const gs_init_data *data)
  64. {
  65. memset(&desc, 0, sizeof(desc));
  66. desc.BufferCount = data->num_backbuffers;
  67. desc.BufferDesc.Format = ConvertGSTextureFormat(data->format);
  68. desc.BufferDesc.Width = data->cx;
  69. desc.BufferDesc.Height = data->cy;
  70. desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  71. desc.OutputWindow = (HWND)data->window.hwnd;
  72. desc.SampleDesc.Count = 1;
  73. desc.Windowed = true;
  74. }
  75. void gs_swap_chain::InitTarget(uint32_t cx, uint32_t cy)
  76. {
  77. HRESULT hr;
  78. target.width = cx;
  79. target.height = cy;
  80. hr = swap->GetBuffer(0, __uuidof(ID3D11Texture2D),
  81. (void **)target.texture.Assign());
  82. if (FAILED(hr))
  83. throw HRError("Failed to get swap buffer texture", hr);
  84. hr = device->device->CreateRenderTargetView(
  85. target.texture, NULL, target.renderTarget[0].Assign());
  86. if (FAILED(hr))
  87. throw HRError("Failed to create swap render target view", hr);
  88. }
  89. void gs_swap_chain::InitZStencilBuffer(uint32_t cx, uint32_t cy)
  90. {
  91. zs.width = cx;
  92. zs.height = cy;
  93. if (zs.format != GS_ZS_NONE && cx != 0 && cy != 0) {
  94. zs.InitBuffer();
  95. } else {
  96. zs.texture.Clear();
  97. zs.view.Clear();
  98. }
  99. }
  100. void gs_swap_chain::Resize(uint32_t cx, uint32_t cy)
  101. {
  102. RECT clientRect;
  103. HRESULT hr;
  104. target.texture.Clear();
  105. target.renderTarget[0].Clear();
  106. zs.texture.Clear();
  107. zs.view.Clear();
  108. initData.cx = cx;
  109. initData.cy = cy;
  110. if (cx == 0 || cy == 0) {
  111. GetClientRect(hwnd, &clientRect);
  112. if (cx == 0)
  113. cx = clientRect.right;
  114. if (cy == 0)
  115. cy = clientRect.bottom;
  116. }
  117. hr = swap->ResizeBuffers(numBuffers, cx, cy, target.dxgiFormat, 0);
  118. if (FAILED(hr))
  119. throw HRError("Failed to resize swap buffers", hr);
  120. InitTarget(cx, cy);
  121. InitZStencilBuffer(cx, cy);
  122. }
  123. void gs_swap_chain::Init()
  124. {
  125. target.device = device;
  126. target.isRenderTarget = true;
  127. target.format = initData.format;
  128. target.dxgiFormat = ConvertGSTextureFormat(initData.format);
  129. InitTarget(initData.cx, initData.cy);
  130. zs.device = device;
  131. zs.format = initData.zsformat;
  132. zs.dxgiFormat = ConvertGSZStencilFormat(initData.zsformat);
  133. InitZStencilBuffer(initData.cx, initData.cy);
  134. }
  135. gs_swap_chain::gs_swap_chain(gs_device *device, const gs_init_data *data)
  136. : gs_obj(device, gs_type::gs_swap_chain),
  137. numBuffers(data->num_backbuffers),
  138. hwnd((HWND)data->window.hwnd),
  139. initData(*data)
  140. {
  141. HRESULT hr;
  142. make_swap_desc(swapDesc, data);
  143. hr = device->factory->CreateSwapChain(device->device, &swapDesc,
  144. swap.Assign());
  145. if (FAILED(hr))
  146. throw HRError("Failed to create swap chain", hr);
  147. /* Ignore Alt+Enter */
  148. device->factory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER);
  149. Init();
  150. }
  151. void gs_device::InitCompiler()
  152. {
  153. char d3dcompiler[40] = {};
  154. int ver = 49;
  155. while (ver > 30) {
  156. sprintf(d3dcompiler, "D3DCompiler_%02d.dll", ver);
  157. HMODULE module = LoadLibraryA(d3dcompiler);
  158. if (module) {
  159. d3dCompile = (pD3DCompile)GetProcAddress(module,
  160. "D3DCompile");
  161. #ifdef DISASSEMBLE_SHADERS
  162. d3dDisassemble = (pD3DDisassemble)GetProcAddress(
  163. module, "D3DDisassemble");
  164. #endif
  165. if (d3dCompile) {
  166. return;
  167. }
  168. FreeLibrary(module);
  169. }
  170. ver--;
  171. }
  172. throw "Could not find any D3DCompiler libraries. Make sure you've "
  173. "installed the <a href=\"https://obsproject.com/go/dxwebsetup\">"
  174. "DirectX components</a> that OBS Studio requires.";
  175. }
  176. void gs_device::InitFactory(uint32_t adapterIdx)
  177. {
  178. HRESULT hr;
  179. IID factoryIID = (GetWinVer() >= 0x602) ? dxgiFactory2
  180. : __uuidof(IDXGIFactory1);
  181. hr = CreateDXGIFactory1(factoryIID, (void **)factory.Assign());
  182. if (FAILED(hr))
  183. throw UnsupportedHWError("Failed to create DXGIFactory", hr);
  184. hr = factory->EnumAdapters1(adapterIdx, &adapter);
  185. if (FAILED(hr))
  186. throw UnsupportedHWError("Failed to enumerate DXGIAdapter", hr);
  187. }
  188. const static D3D_FEATURE_LEVEL featureLevels[] = {
  189. D3D_FEATURE_LEVEL_11_0,
  190. D3D_FEATURE_LEVEL_10_1,
  191. D3D_FEATURE_LEVEL_10_0,
  192. D3D_FEATURE_LEVEL_9_3,
  193. };
  194. /* ------------------------------------------------------------------------- */
  195. #define VERT_IN_OUT \
  196. "\
  197. struct VertInOut { \
  198. float4 pos : POSITION; \
  199. }; "
  200. #define NV12_Y_PS \
  201. VERT_IN_OUT "\
  202. float main(VertInOut vert_in) : TARGET \
  203. { \
  204. return 1.0; \
  205. }"
  206. #define NV12_UV_PS \
  207. VERT_IN_OUT "\
  208. float2 main(VertInOut vert_in) : TARGET \
  209. { \
  210. return float2(1.0, 1.0); \
  211. }"
  212. #define NV12_VS \
  213. VERT_IN_OUT "\
  214. VertInOut main(VertInOut vert_in) \
  215. { \
  216. VertInOut vert_out; \
  217. vert_out.pos = float4(vert_in.pos.xyz, 1.0); \
  218. return vert_out; \
  219. } "
  220. /* ------------------------------------------------------------------------- */
  221. #define NV12_CX 128
  222. #define NV12_CY 128
  223. bool gs_device::HasBadNV12Output()
  224. try {
  225. vec3 points[4];
  226. vec3_set(&points[0], -1.0f, -1.0f, 0.0f);
  227. vec3_set(&points[1], -1.0f, 1.0f, 0.0f);
  228. vec3_set(&points[2], 1.0f, -1.0f, 0.0f);
  229. vec3_set(&points[3], 1.0f, 1.0f, 0.0f);
  230. gs_texture_2d nv12_y(this, NV12_CX, NV12_CY, GS_R8, 1, nullptr,
  231. GS_RENDER_TARGET | GS_SHARED_KM_TEX, GS_TEXTURE_2D,
  232. false, true);
  233. gs_texture_2d nv12_uv(this, nv12_y.texture,
  234. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  235. gs_vertex_shader nv12_vs(this, "", NV12_VS);
  236. gs_pixel_shader nv12_y_ps(this, "", NV12_Y_PS);
  237. gs_pixel_shader nv12_uv_ps(this, "", NV12_UV_PS);
  238. gs_stage_surface nv12_stage(this, NV12_CX, NV12_CY);
  239. gs_vb_data *vbd = gs_vbdata_create();
  240. vbd->num = 4;
  241. vbd->points = (vec3 *)bmemdup(&points, sizeof(points));
  242. gs_vertex_buffer buf(this, vbd, 0);
  243. device_load_vertexbuffer(this, &buf);
  244. device_load_vertexshader(this, &nv12_vs);
  245. context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
  246. device_set_viewport(this, 0, 0, NV12_CX, NV12_CY);
  247. device_set_cull_mode(this, GS_NEITHER);
  248. device_enable_depth_test(this, false);
  249. device_enable_blending(this, false);
  250. LoadVertexBufferData();
  251. device_set_render_target(this, &nv12_y, nullptr);
  252. device_load_pixelshader(this, &nv12_y_ps);
  253. UpdateBlendState();
  254. UpdateRasterState();
  255. UpdateZStencilState();
  256. context->Draw(4, 0);
  257. device_set_viewport(this, 0, 0, NV12_CX / 2, NV12_CY / 2);
  258. device_set_render_target(this, &nv12_uv, nullptr);
  259. device_load_pixelshader(this, &nv12_uv_ps);
  260. UpdateBlendState();
  261. UpdateRasterState();
  262. UpdateZStencilState();
  263. context->Draw(4, 0);
  264. device_load_pixelshader(this, nullptr);
  265. device_load_vertexbuffer(this, nullptr);
  266. device_load_vertexshader(this, nullptr);
  267. device_set_render_target(this, nullptr, nullptr);
  268. device_stage_texture(this, &nv12_stage, &nv12_y);
  269. uint8_t *data;
  270. uint32_t linesize;
  271. bool bad_driver = false;
  272. if (gs_stagesurface_map(&nv12_stage, &data, &linesize)) {
  273. bad_driver = data[linesize * NV12_CY] == 0;
  274. gs_stagesurface_unmap(&nv12_stage);
  275. } else {
  276. throw "Could not map surface";
  277. }
  278. if (bad_driver) {
  279. blog(LOG_WARNING, "Bad NV12 texture handling detected! "
  280. "Disabling NV12 texture support.");
  281. }
  282. return bad_driver;
  283. } catch (HRError &error) {
  284. blog(LOG_WARNING, "HasBadNV12Output failed: %s (%08lX)", error.str,
  285. error.hr);
  286. return false;
  287. } catch (const char *error) {
  288. blog(LOG_WARNING, "HasBadNV12Output failed: %s", error);
  289. return false;
  290. }
  291. void gs_device::InitDevice(uint32_t adapterIdx)
  292. {
  293. wstring adapterName;
  294. DXGI_ADAPTER_DESC desc;
  295. D3D_FEATURE_LEVEL levelUsed = D3D_FEATURE_LEVEL_9_3;
  296. HRESULT hr = 0;
  297. adpIdx = adapterIdx;
  298. uint32_t createFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
  299. #ifdef _DEBUG
  300. //createFlags |= D3D11_CREATE_DEVICE_DEBUG;
  301. #endif
  302. adapterName = (adapter->GetDesc(&desc) == S_OK) ? desc.Description
  303. : L"<unknown>";
  304. BPtr<char> adapterNameUTF8;
  305. os_wcs_to_utf8_ptr(adapterName.c_str(), 0, &adapterNameUTF8);
  306. blog(LOG_INFO, "Loading up D3D11 on adapter %s (%" PRIu32 ")",
  307. adapterNameUTF8.Get(), adapterIdx);
  308. hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL,
  309. createFlags, featureLevels,
  310. sizeof(featureLevels) /
  311. sizeof(D3D_FEATURE_LEVEL),
  312. D3D11_SDK_VERSION, device.Assign(), &levelUsed,
  313. context.Assign());
  314. if (FAILED(hr))
  315. throw UnsupportedHWError("Failed to create device", hr);
  316. blog(LOG_INFO, "D3D11 loaded successfully, feature level used: %u",
  317. (unsigned int)levelUsed);
  318. /* ---------------------------------------- */
  319. /* check for nv12 texture output support */
  320. nv12Supported = false;
  321. /* Intel CopyResource is very slow with NV12 */
  322. if (desc.VendorId == 0x8086) {
  323. return;
  324. }
  325. ComQIPtr<ID3D11Device1> d3d11_1(device);
  326. if (!d3d11_1) {
  327. return;
  328. }
  329. /* needs to support extended resource sharing */
  330. D3D11_FEATURE_DATA_D3D11_OPTIONS opts = {};
  331. hr = d3d11_1->CheckFeatureSupport(D3D11_FEATURE_D3D11_OPTIONS, &opts,
  332. sizeof(opts));
  333. if (FAILED(hr) || !opts.ExtendedResourceSharing) {
  334. return;
  335. }
  336. /* needs to support the actual format */
  337. UINT support = 0;
  338. hr = device->CheckFormatSupport(DXGI_FORMAT_NV12, &support);
  339. if (FAILED(hr)) {
  340. return;
  341. }
  342. if ((support & D3D11_FORMAT_SUPPORT_TEXTURE2D) == 0) {
  343. return;
  344. }
  345. /* must be usable as a render target */
  346. if ((support & D3D11_FORMAT_SUPPORT_RENDER_TARGET) == 0) {
  347. return;
  348. }
  349. if (HasBadNV12Output()) {
  350. return;
  351. }
  352. nv12Supported = true;
  353. }
  354. static inline void ConvertStencilSide(D3D11_DEPTH_STENCILOP_DESC &desc,
  355. const StencilSide &side)
  356. {
  357. desc.StencilFunc = ConvertGSDepthTest(side.test);
  358. desc.StencilFailOp = ConvertGSStencilOp(side.fail);
  359. desc.StencilDepthFailOp = ConvertGSStencilOp(side.zfail);
  360. desc.StencilPassOp = ConvertGSStencilOp(side.zpass);
  361. }
  362. ID3D11DepthStencilState *gs_device::AddZStencilState()
  363. {
  364. HRESULT hr;
  365. D3D11_DEPTH_STENCIL_DESC dsd;
  366. ID3D11DepthStencilState *state;
  367. dsd.DepthEnable = zstencilState.depthEnabled;
  368. dsd.DepthFunc = ConvertGSDepthTest(zstencilState.depthFunc);
  369. dsd.DepthWriteMask = zstencilState.depthWriteEnabled
  370. ? D3D11_DEPTH_WRITE_MASK_ALL
  371. : D3D11_DEPTH_WRITE_MASK_ZERO;
  372. dsd.StencilEnable = zstencilState.stencilEnabled;
  373. dsd.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
  374. dsd.StencilWriteMask = zstencilState.stencilWriteEnabled
  375. ? D3D11_DEFAULT_STENCIL_WRITE_MASK
  376. : 0;
  377. ConvertStencilSide(dsd.FrontFace, zstencilState.stencilFront);
  378. ConvertStencilSide(dsd.BackFace, zstencilState.stencilBack);
  379. SavedZStencilState savedState(zstencilState, dsd);
  380. hr = device->CreateDepthStencilState(&dsd, savedState.state.Assign());
  381. if (FAILED(hr))
  382. throw HRError("Failed to create depth stencil state", hr);
  383. state = savedState.state;
  384. zstencilStates.push_back(savedState);
  385. return state;
  386. }
  387. ID3D11RasterizerState *gs_device::AddRasterState()
  388. {
  389. HRESULT hr;
  390. D3D11_RASTERIZER_DESC rd;
  391. ID3D11RasterizerState *state;
  392. memset(&rd, 0, sizeof(rd));
  393. /* use CCW to convert to a right-handed coordinate system */
  394. rd.FrontCounterClockwise = true;
  395. rd.FillMode = D3D11_FILL_SOLID;
  396. rd.CullMode = ConvertGSCullMode(rasterState.cullMode);
  397. rd.DepthClipEnable = true;
  398. rd.ScissorEnable = rasterState.scissorEnabled;
  399. SavedRasterState savedState(rasterState, rd);
  400. hr = device->CreateRasterizerState(&rd, savedState.state.Assign());
  401. if (FAILED(hr))
  402. throw HRError("Failed to create rasterizer state", hr);
  403. state = savedState.state;
  404. rasterStates.push_back(savedState);
  405. return state;
  406. }
  407. ID3D11BlendState *gs_device::AddBlendState()
  408. {
  409. HRESULT hr;
  410. D3D11_BLEND_DESC bd;
  411. ID3D11BlendState *state;
  412. memset(&bd, 0, sizeof(bd));
  413. for (int i = 0; i < 8; i++) {
  414. bd.RenderTarget[i].BlendEnable = blendState.blendEnabled;
  415. bd.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD;
  416. bd.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  417. bd.RenderTarget[i].SrcBlend =
  418. ConvertGSBlendType(blendState.srcFactorC);
  419. bd.RenderTarget[i].DestBlend =
  420. ConvertGSBlendType(blendState.destFactorC);
  421. bd.RenderTarget[i].SrcBlendAlpha =
  422. ConvertGSBlendType(blendState.srcFactorA);
  423. bd.RenderTarget[i].DestBlendAlpha =
  424. ConvertGSBlendType(blendState.destFactorA);
  425. bd.RenderTarget[i].RenderTargetWriteMask =
  426. (blendState.redEnabled ? D3D11_COLOR_WRITE_ENABLE_RED
  427. : 0) |
  428. (blendState.greenEnabled
  429. ? D3D11_COLOR_WRITE_ENABLE_GREEN
  430. : 0) |
  431. (blendState.blueEnabled ? D3D11_COLOR_WRITE_ENABLE_BLUE
  432. : 0) |
  433. (blendState.alphaEnabled
  434. ? D3D11_COLOR_WRITE_ENABLE_ALPHA
  435. : 0);
  436. }
  437. SavedBlendState savedState(blendState, bd);
  438. hr = device->CreateBlendState(&bd, savedState.state.Assign());
  439. if (FAILED(hr))
  440. throw HRError("Failed to create blend state", hr);
  441. state = savedState.state;
  442. blendStates.push_back(savedState);
  443. return state;
  444. }
  445. void gs_device::UpdateZStencilState()
  446. {
  447. ID3D11DepthStencilState *state = NULL;
  448. if (!zstencilStateChanged)
  449. return;
  450. for (size_t i = 0; i < zstencilStates.size(); i++) {
  451. SavedZStencilState &s = zstencilStates[i];
  452. if (memcmp(&s, &zstencilState, sizeof(zstencilState)) == 0) {
  453. state = s.state;
  454. break;
  455. }
  456. }
  457. if (!state)
  458. state = AddZStencilState();
  459. if (state != curDepthStencilState) {
  460. context->OMSetDepthStencilState(state, 0);
  461. curDepthStencilState = state;
  462. }
  463. zstencilStateChanged = false;
  464. }
  465. void gs_device::UpdateRasterState()
  466. {
  467. ID3D11RasterizerState *state = NULL;
  468. if (!rasterStateChanged)
  469. return;
  470. for (size_t i = 0; i < rasterStates.size(); i++) {
  471. SavedRasterState &s = rasterStates[i];
  472. if (memcmp(&s, &rasterState, sizeof(rasterState)) == 0) {
  473. state = s.state;
  474. break;
  475. }
  476. }
  477. if (!state)
  478. state = AddRasterState();
  479. if (state != curRasterState) {
  480. context->RSSetState(state);
  481. curRasterState = state;
  482. }
  483. rasterStateChanged = false;
  484. }
  485. void gs_device::UpdateBlendState()
  486. {
  487. ID3D11BlendState *state = NULL;
  488. if (!blendStateChanged)
  489. return;
  490. for (size_t i = 0; i < blendStates.size(); i++) {
  491. SavedBlendState &s = blendStates[i];
  492. if (memcmp(&s, &blendState, sizeof(blendState)) == 0) {
  493. state = s.state;
  494. break;
  495. }
  496. }
  497. if (!state)
  498. state = AddBlendState();
  499. if (state != curBlendState) {
  500. float f[4] = {1.0f, 1.0f, 1.0f, 1.0f};
  501. context->OMSetBlendState(state, f, 0xFFFFFFFF);
  502. curBlendState = state;
  503. }
  504. blendStateChanged = false;
  505. }
  506. void gs_device::UpdateViewProjMatrix()
  507. {
  508. gs_matrix_get(&curViewMatrix);
  509. /* negate Z col of the view matrix for right-handed coordinate system */
  510. curViewMatrix.x.z = -curViewMatrix.x.z;
  511. curViewMatrix.y.z = -curViewMatrix.y.z;
  512. curViewMatrix.z.z = -curViewMatrix.z.z;
  513. curViewMatrix.t.z = -curViewMatrix.t.z;
  514. matrix4_mul(&curViewProjMatrix, &curViewMatrix, &curProjMatrix);
  515. matrix4_transpose(&curViewProjMatrix, &curViewProjMatrix);
  516. if (curVertexShader->viewProj)
  517. gs_shader_set_matrix4(curVertexShader->viewProj,
  518. &curViewProjMatrix);
  519. }
  520. gs_device::gs_device(uint32_t adapterIdx)
  521. : curToplogy(D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED)
  522. {
  523. matrix4_identity(&curProjMatrix);
  524. matrix4_identity(&curViewMatrix);
  525. matrix4_identity(&curViewProjMatrix);
  526. memset(&viewport, 0, sizeof(viewport));
  527. for (size_t i = 0; i < GS_MAX_TEXTURES; i++) {
  528. curTextures[i] = NULL;
  529. curSamplers[i] = NULL;
  530. }
  531. InitCompiler();
  532. InitFactory(adapterIdx);
  533. InitDevice(adapterIdx);
  534. device_set_render_target(this, NULL, NULL);
  535. }
  536. gs_device::~gs_device()
  537. {
  538. context->ClearState();
  539. }
  540. const char *device_get_name(void)
  541. {
  542. return "Direct3D 11";
  543. }
  544. int device_get_type(void)
  545. {
  546. return GS_DEVICE_DIRECT3D_11;
  547. }
  548. const char *device_preprocessor_name(void)
  549. {
  550. return "_D3D11";
  551. }
  552. static inline void
  553. EnumD3DAdapters(bool (*callback)(void *, const char *, uint32_t), void *param)
  554. {
  555. ComPtr<IDXGIFactory1> factory;
  556. ComPtr<IDXGIAdapter1> adapter;
  557. HRESULT hr;
  558. UINT i;
  559. IID factoryIID = (GetWinVer() >= 0x602) ? dxgiFactory2
  560. : __uuidof(IDXGIFactory1);
  561. hr = CreateDXGIFactory1(factoryIID, (void **)factory.Assign());
  562. if (FAILED(hr))
  563. throw HRError("Failed to create DXGIFactory", hr);
  564. for (i = 0; factory->EnumAdapters1(i, adapter.Assign()) == S_OK; ++i) {
  565. DXGI_ADAPTER_DESC desc;
  566. char name[512] = "";
  567. hr = adapter->GetDesc(&desc);
  568. if (FAILED(hr))
  569. continue;
  570. /* ignore Microsoft's 'basic' renderer' */
  571. if (desc.VendorId == 0x1414 && desc.DeviceId == 0x8c)
  572. continue;
  573. os_wcs_to_utf8(desc.Description, 0, name, sizeof(name));
  574. if (!callback(param, name, i))
  575. break;
  576. }
  577. }
  578. bool device_enum_adapters(bool (*callback)(void *param, const char *name,
  579. uint32_t id),
  580. void *param)
  581. {
  582. try {
  583. EnumD3DAdapters(callback, param);
  584. return true;
  585. } catch (HRError &error) {
  586. blog(LOG_WARNING, "Failed enumerating devices: %s (%08lX)",
  587. error.str, error.hr);
  588. return false;
  589. }
  590. }
  591. static inline void LogAdapterMonitors(IDXGIAdapter1 *adapter)
  592. {
  593. UINT i;
  594. ComPtr<IDXGIOutput> output;
  595. for (i = 0; adapter->EnumOutputs(i, &output) == S_OK; ++i) {
  596. DXGI_OUTPUT_DESC desc;
  597. if (FAILED(output->GetDesc(&desc)))
  598. continue;
  599. RECT rect = desc.DesktopCoordinates;
  600. blog(LOG_INFO,
  601. "\t output %u: "
  602. "pos={%d, %d}, "
  603. "size={%d, %d}, "
  604. "attached=%s",
  605. i, rect.left, rect.top, rect.right - rect.left,
  606. rect.bottom - rect.top,
  607. desc.AttachedToDesktop ? "true" : "false");
  608. }
  609. }
  610. static inline void LogD3DAdapters()
  611. {
  612. ComPtr<IDXGIFactory1> factory;
  613. ComPtr<IDXGIAdapter1> adapter;
  614. HRESULT hr;
  615. UINT i;
  616. blog(LOG_INFO, "Available Video Adapters: ");
  617. IID factoryIID = (GetWinVer() >= 0x602) ? dxgiFactory2
  618. : __uuidof(IDXGIFactory1);
  619. hr = CreateDXGIFactory1(factoryIID, (void **)factory.Assign());
  620. if (FAILED(hr))
  621. throw HRError("Failed to create DXGIFactory", hr);
  622. for (i = 0; factory->EnumAdapters1(i, adapter.Assign()) == S_OK; ++i) {
  623. DXGI_ADAPTER_DESC desc;
  624. char name[512] = "";
  625. hr = adapter->GetDesc(&desc);
  626. if (FAILED(hr))
  627. continue;
  628. /* ignore Microsoft's 'basic' renderer' */
  629. if (desc.VendorId == 0x1414 && desc.DeviceId == 0x8c)
  630. continue;
  631. os_wcs_to_utf8(desc.Description, 0, name, sizeof(name));
  632. blog(LOG_INFO, "\tAdapter %u: %s", i, name);
  633. blog(LOG_INFO, "\t Dedicated VRAM: %u",
  634. desc.DedicatedVideoMemory);
  635. blog(LOG_INFO, "\t Shared VRAM: %u",
  636. desc.SharedSystemMemory);
  637. LogAdapterMonitors(adapter);
  638. }
  639. }
  640. int device_create(gs_device_t **p_device, uint32_t adapter)
  641. {
  642. gs_device *device = NULL;
  643. int errorcode = GS_SUCCESS;
  644. try {
  645. blog(LOG_INFO, "---------------------------------");
  646. blog(LOG_INFO, "Initializing D3D11...");
  647. LogD3DAdapters();
  648. device = new gs_device(adapter);
  649. } catch (UnsupportedHWError &error) {
  650. blog(LOG_ERROR, "device_create (D3D11): %s (%08lX)", error.str,
  651. error.hr);
  652. errorcode = GS_ERROR_NOT_SUPPORTED;
  653. } catch (HRError &error) {
  654. blog(LOG_ERROR, "device_create (D3D11): %s (%08lX)", error.str,
  655. error.hr);
  656. errorcode = GS_ERROR_FAIL;
  657. }
  658. *p_device = device;
  659. return errorcode;
  660. }
  661. void device_destroy(gs_device_t *device)
  662. {
  663. delete device;
  664. }
  665. void device_enter_context(gs_device_t *device)
  666. {
  667. /* does nothing */
  668. UNUSED_PARAMETER(device);
  669. }
  670. void device_leave_context(gs_device_t *device)
  671. {
  672. /* does nothing */
  673. UNUSED_PARAMETER(device);
  674. }
  675. void *device_get_device_obj(gs_device_t *device)
  676. {
  677. return (void *)device->device.Get();
  678. }
  679. gs_swapchain_t *device_swapchain_create(gs_device_t *device,
  680. const struct gs_init_data *data)
  681. {
  682. gs_swap_chain *swap = NULL;
  683. try {
  684. swap = new gs_swap_chain(device, data);
  685. } catch (HRError &error) {
  686. blog(LOG_ERROR, "device_swapchain_create (D3D11): %s (%08lX)",
  687. error.str, error.hr);
  688. LogD3D11ErrorDetails(error, device);
  689. }
  690. return swap;
  691. }
  692. void device_resize(gs_device_t *device, uint32_t cx, uint32_t cy)
  693. {
  694. if (!device->curSwapChain) {
  695. blog(LOG_WARNING, "device_resize (D3D11): No active swap");
  696. return;
  697. }
  698. try {
  699. ID3D11RenderTargetView *renderView = NULL;
  700. ID3D11DepthStencilView *depthView = NULL;
  701. int i = device->curRenderSide;
  702. device->context->OMSetRenderTargets(1, &renderView, depthView);
  703. device->curSwapChain->Resize(cx, cy);
  704. if (device->curRenderTarget)
  705. renderView = device->curRenderTarget->renderTarget[i];
  706. if (device->curZStencilBuffer)
  707. depthView = device->curZStencilBuffer->view;
  708. device->context->OMSetRenderTargets(1, &renderView, depthView);
  709. } catch (HRError &error) {
  710. blog(LOG_ERROR, "device_resize (D3D11): %s (%08lX)", error.str,
  711. error.hr);
  712. LogD3D11ErrorDetails(error, device);
  713. }
  714. }
  715. void device_get_size(const gs_device_t *device, uint32_t *cx, uint32_t *cy)
  716. {
  717. if (device->curSwapChain) {
  718. *cx = device->curSwapChain->target.width;
  719. *cy = device->curSwapChain->target.height;
  720. } else {
  721. blog(LOG_ERROR, "device_get_size (D3D11): no active swap");
  722. *cx = 0;
  723. *cy = 0;
  724. }
  725. }
  726. uint32_t device_get_width(const gs_device_t *device)
  727. {
  728. if (device->curSwapChain) {
  729. return device->curSwapChain->target.width;
  730. } else {
  731. blog(LOG_ERROR, "device_get_size (D3D11): no active swap");
  732. return 0;
  733. }
  734. }
  735. uint32_t device_get_height(const gs_device_t *device)
  736. {
  737. if (device->curSwapChain) {
  738. return device->curSwapChain->target.height;
  739. } else {
  740. blog(LOG_ERROR, "device_get_size (D3D11): no active swap");
  741. return 0;
  742. }
  743. }
  744. gs_texture_t *device_texture_create(gs_device_t *device, uint32_t width,
  745. uint32_t height,
  746. enum gs_color_format color_format,
  747. uint32_t levels, const uint8_t **data,
  748. uint32_t flags)
  749. {
  750. gs_texture *texture = NULL;
  751. try {
  752. texture = new gs_texture_2d(device, width, height, color_format,
  753. levels, data, flags, GS_TEXTURE_2D,
  754. false);
  755. } catch (HRError &error) {
  756. blog(LOG_ERROR, "device_texture_create (D3D11): %s (%08lX)",
  757. error.str, error.hr);
  758. LogD3D11ErrorDetails(error, device);
  759. } catch (const char *error) {
  760. blog(LOG_ERROR, "device_texture_create (D3D11): %s", error);
  761. }
  762. return texture;
  763. }
  764. gs_texture_t *device_cubetexture_create(gs_device_t *device, uint32_t size,
  765. enum gs_color_format color_format,
  766. uint32_t levels, const uint8_t **data,
  767. uint32_t flags)
  768. {
  769. gs_texture *texture = NULL;
  770. try {
  771. texture = new gs_texture_2d(device, size, size, color_format,
  772. levels, data, flags,
  773. GS_TEXTURE_CUBE, false);
  774. } catch (HRError &error) {
  775. blog(LOG_ERROR,
  776. "device_cubetexture_create (D3D11): %s "
  777. "(%08lX)",
  778. error.str, error.hr);
  779. LogD3D11ErrorDetails(error, device);
  780. } catch (const char *error) {
  781. blog(LOG_ERROR, "device_cubetexture_create (D3D11): %s", error);
  782. }
  783. return texture;
  784. }
  785. gs_texture_t *device_voltexture_create(gs_device_t *device, uint32_t width,
  786. uint32_t height, uint32_t depth,
  787. enum gs_color_format color_format,
  788. uint32_t levels, const uint8_t **data,
  789. uint32_t flags)
  790. {
  791. /* TODO */
  792. UNUSED_PARAMETER(device);
  793. UNUSED_PARAMETER(width);
  794. UNUSED_PARAMETER(height);
  795. UNUSED_PARAMETER(depth);
  796. UNUSED_PARAMETER(color_format);
  797. UNUSED_PARAMETER(levels);
  798. UNUSED_PARAMETER(data);
  799. UNUSED_PARAMETER(flags);
  800. return NULL;
  801. }
  802. gs_zstencil_t *device_zstencil_create(gs_device_t *device, uint32_t width,
  803. uint32_t height,
  804. enum gs_zstencil_format format)
  805. {
  806. gs_zstencil_buffer *zstencil = NULL;
  807. try {
  808. zstencil =
  809. new gs_zstencil_buffer(device, width, height, format);
  810. } catch (HRError &error) {
  811. blog(LOG_ERROR, "device_zstencil_create (D3D11): %s (%08lX)",
  812. error.str, error.hr);
  813. LogD3D11ErrorDetails(error, device);
  814. }
  815. return zstencil;
  816. }
  817. gs_stagesurf_t *device_stagesurface_create(gs_device_t *device, uint32_t width,
  818. uint32_t height,
  819. enum gs_color_format color_format)
  820. {
  821. gs_stage_surface *surf = NULL;
  822. try {
  823. surf = new gs_stage_surface(device, width, height,
  824. color_format);
  825. } catch (HRError &error) {
  826. blog(LOG_ERROR,
  827. "device_stagesurface_create (D3D11): %s "
  828. "(%08lX)",
  829. error.str, error.hr);
  830. LogD3D11ErrorDetails(error, device);
  831. }
  832. return surf;
  833. }
  834. gs_samplerstate_t *
  835. device_samplerstate_create(gs_device_t *device,
  836. const struct gs_sampler_info *info)
  837. {
  838. gs_sampler_state *ss = NULL;
  839. try {
  840. ss = new gs_sampler_state(device, info);
  841. } catch (HRError &error) {
  842. blog(LOG_ERROR,
  843. "device_samplerstate_create (D3D11): %s "
  844. "(%08lX)",
  845. error.str, error.hr);
  846. LogD3D11ErrorDetails(error, device);
  847. }
  848. return ss;
  849. }
  850. gs_shader_t *device_vertexshader_create(gs_device_t *device,
  851. const char *shader_string,
  852. const char *file, char **error_string)
  853. {
  854. gs_vertex_shader *shader = NULL;
  855. try {
  856. shader = new gs_vertex_shader(device, file, shader_string);
  857. } catch (HRError &error) {
  858. blog(LOG_ERROR,
  859. "device_vertexshader_create (D3D11): %s "
  860. "(%08lX)",
  861. error.str, error.hr);
  862. LogD3D11ErrorDetails(error, device);
  863. } catch (ShaderError &const error) {
  864. const char *buf =
  865. (const char *)error.errors->GetBufferPointer();
  866. if (error_string)
  867. *error_string = bstrdup(buf);
  868. blog(LOG_ERROR,
  869. "device_vertexshader_create (D3D11): "
  870. "Compile warnings/errors for %s:\n%s",
  871. file, buf);
  872. } catch (const char *error) {
  873. blog(LOG_ERROR, "device_vertexshader_create (D3D11): %s",
  874. error);
  875. }
  876. return shader;
  877. }
  878. gs_shader_t *device_pixelshader_create(gs_device_t *device,
  879. const char *shader_string,
  880. const char *file, char **error_string)
  881. {
  882. gs_pixel_shader *shader = NULL;
  883. try {
  884. shader = new gs_pixel_shader(device, file, shader_string);
  885. } catch (HRError &error) {
  886. blog(LOG_ERROR,
  887. "device_pixelshader_create (D3D11): %s "
  888. "(%08lX)",
  889. error.str, error.hr);
  890. LogD3D11ErrorDetails(error, device);
  891. } catch (ShaderError &const error) {
  892. const char *buf =
  893. (const char *)error.errors->GetBufferPointer();
  894. if (error_string)
  895. *error_string = bstrdup(buf);
  896. blog(LOG_ERROR,
  897. "device_pixelshader_create (D3D11): "
  898. "Compiler warnings/errors for %s:\n%s",
  899. file, buf);
  900. } catch (const char *error) {
  901. blog(LOG_ERROR, "device_pixelshader_create (D3D11): %s", error);
  902. }
  903. return shader;
  904. }
  905. gs_vertbuffer_t *device_vertexbuffer_create(gs_device_t *device,
  906. struct gs_vb_data *data,
  907. uint32_t flags)
  908. {
  909. gs_vertex_buffer *buffer = NULL;
  910. try {
  911. buffer = new gs_vertex_buffer(device, data, flags);
  912. } catch (HRError &error) {
  913. blog(LOG_ERROR,
  914. "device_vertexbuffer_create (D3D11): %s "
  915. "(%08lX)",
  916. error.str, error.hr);
  917. LogD3D11ErrorDetails(error, device);
  918. } catch (const char *error) {
  919. blog(LOG_ERROR, "device_vertexbuffer_create (D3D11): %s",
  920. error);
  921. }
  922. return buffer;
  923. }
  924. gs_indexbuffer_t *device_indexbuffer_create(gs_device_t *device,
  925. enum gs_index_type type,
  926. void *indices, size_t num,
  927. uint32_t flags)
  928. {
  929. gs_index_buffer *buffer = NULL;
  930. try {
  931. buffer = new gs_index_buffer(device, type, indices, num, flags);
  932. } catch (HRError &error) {
  933. blog(LOG_ERROR, "device_indexbuffer_create (D3D11): %s (%08lX)",
  934. error.str, error.hr);
  935. LogD3D11ErrorDetails(error, device);
  936. }
  937. return buffer;
  938. }
  939. gs_timer_t *device_timer_create(gs_device_t *device)
  940. {
  941. gs_timer *timer = NULL;
  942. try {
  943. timer = new gs_timer(device);
  944. } catch (HRError error) {
  945. blog(LOG_ERROR, "device_timer_create (D3D11): %s (%08lX)",
  946. error.str, error.hr);
  947. LogD3D11ErrorDetails(error, device);
  948. }
  949. return timer;
  950. }
  951. gs_timer_range_t *device_timer_range_create(gs_device_t *device)
  952. {
  953. gs_timer_range *range = NULL;
  954. try {
  955. range = new gs_timer_range(device);
  956. } catch (HRError error) {
  957. blog(LOG_ERROR, "device_timer_range_create (D3D11): %s (%08lX)",
  958. error.str, error.hr);
  959. LogD3D11ErrorDetails(error, device);
  960. }
  961. return range;
  962. }
  963. enum gs_texture_type device_get_texture_type(const gs_texture_t *texture)
  964. {
  965. return texture->type;
  966. }
  967. void gs_device::LoadVertexBufferData()
  968. {
  969. if (curVertexBuffer == lastVertexBuffer &&
  970. curVertexShader == lastVertexShader)
  971. return;
  972. vector<ID3D11Buffer *> buffers;
  973. vector<uint32_t> strides;
  974. vector<uint32_t> offsets;
  975. if (curVertexBuffer && curVertexShader) {
  976. curVertexBuffer->MakeBufferList(curVertexShader, buffers,
  977. strides);
  978. } else {
  979. size_t buffersToClear =
  980. curVertexShader ? curVertexShader->NumBuffersExpected()
  981. : 0;
  982. buffers.resize(buffersToClear);
  983. strides.resize(buffersToClear);
  984. }
  985. offsets.resize(buffers.size());
  986. context->IASetVertexBuffers(0, (UINT)buffers.size(), buffers.data(),
  987. strides.data(), offsets.data());
  988. lastVertexBuffer = curVertexBuffer;
  989. lastVertexShader = curVertexShader;
  990. }
  991. void device_load_vertexbuffer(gs_device_t *device, gs_vertbuffer_t *vertbuffer)
  992. {
  993. if (device->curVertexBuffer == vertbuffer)
  994. return;
  995. device->curVertexBuffer = vertbuffer;
  996. }
  997. void device_load_indexbuffer(gs_device_t *device, gs_indexbuffer_t *indexbuffer)
  998. {
  999. DXGI_FORMAT format;
  1000. ID3D11Buffer *buffer;
  1001. if (device->curIndexBuffer == indexbuffer)
  1002. return;
  1003. if (indexbuffer) {
  1004. switch (indexbuffer->indexSize) {
  1005. case 2:
  1006. format = DXGI_FORMAT_R16_UINT;
  1007. break;
  1008. default:
  1009. case 4:
  1010. format = DXGI_FORMAT_R32_UINT;
  1011. break;
  1012. }
  1013. buffer = indexbuffer->indexBuffer;
  1014. } else {
  1015. buffer = NULL;
  1016. format = DXGI_FORMAT_R32_UINT;
  1017. }
  1018. device->curIndexBuffer = indexbuffer;
  1019. device->context->IASetIndexBuffer(buffer, format, 0);
  1020. }
  1021. void device_load_texture(gs_device_t *device, gs_texture_t *tex, int unit)
  1022. {
  1023. ID3D11ShaderResourceView *view = NULL;
  1024. if (device->curTextures[unit] == tex)
  1025. return;
  1026. if (tex)
  1027. view = tex->shaderRes;
  1028. device->curTextures[unit] = tex;
  1029. device->context->PSSetShaderResources(unit, 1, &view);
  1030. }
  1031. void device_load_samplerstate(gs_device_t *device,
  1032. gs_samplerstate_t *samplerstate, int unit)
  1033. {
  1034. ID3D11SamplerState *state = NULL;
  1035. if (device->curSamplers[unit] == samplerstate)
  1036. return;
  1037. if (samplerstate)
  1038. state = samplerstate->state;
  1039. device->curSamplers[unit] = samplerstate;
  1040. device->context->PSSetSamplers(unit, 1, &state);
  1041. }
  1042. void device_load_vertexshader(gs_device_t *device, gs_shader_t *vertshader)
  1043. {
  1044. ID3D11VertexShader *shader = NULL;
  1045. ID3D11InputLayout *layout = NULL;
  1046. ID3D11Buffer *constants = NULL;
  1047. if (device->curVertexShader == vertshader)
  1048. return;
  1049. gs_vertex_shader *vs = static_cast<gs_vertex_shader *>(vertshader);
  1050. if (vertshader) {
  1051. if (vertshader->type != GS_SHADER_VERTEX) {
  1052. blog(LOG_ERROR, "device_load_vertexshader (D3D11): "
  1053. "Specified shader is not a vertex "
  1054. "shader");
  1055. return;
  1056. }
  1057. shader = vs->shader;
  1058. layout = vs->layout;
  1059. constants = vs->constants;
  1060. }
  1061. device->curVertexShader = vs;
  1062. device->context->VSSetShader(shader, NULL, 0);
  1063. device->context->IASetInputLayout(layout);
  1064. device->context->VSSetConstantBuffers(0, 1, &constants);
  1065. }
  1066. static inline void clear_textures(gs_device_t *device)
  1067. {
  1068. ID3D11ShaderResourceView *views[GS_MAX_TEXTURES];
  1069. memset(views, 0, sizeof(views));
  1070. memset(device->curTextures, 0, sizeof(device->curTextures));
  1071. device->context->PSSetShaderResources(0, GS_MAX_TEXTURES, views);
  1072. }
  1073. void device_load_pixelshader(gs_device_t *device, gs_shader_t *pixelshader)
  1074. {
  1075. ID3D11PixelShader *shader = NULL;
  1076. ID3D11Buffer *constants = NULL;
  1077. ID3D11SamplerState *states[GS_MAX_TEXTURES];
  1078. if (device->curPixelShader == pixelshader)
  1079. return;
  1080. gs_pixel_shader *ps = static_cast<gs_pixel_shader *>(pixelshader);
  1081. if (pixelshader) {
  1082. if (pixelshader->type != GS_SHADER_PIXEL) {
  1083. blog(LOG_ERROR, "device_load_pixelshader (D3D11): "
  1084. "Specified shader is not a pixel "
  1085. "shader");
  1086. return;
  1087. }
  1088. shader = ps->shader;
  1089. constants = ps->constants;
  1090. ps->GetSamplerStates(states);
  1091. } else {
  1092. memset(states, 0, sizeof(states));
  1093. }
  1094. clear_textures(device);
  1095. device->curPixelShader = ps;
  1096. device->context->PSSetShader(shader, NULL, 0);
  1097. device->context->PSSetConstantBuffers(0, 1, &constants);
  1098. device->context->PSSetSamplers(0, GS_MAX_TEXTURES, states);
  1099. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1100. if (device->curSamplers[i] &&
  1101. device->curSamplers[i]->state != states[i])
  1102. device->curSamplers[i] = nullptr;
  1103. }
  1104. void device_load_default_samplerstate(gs_device_t *device, bool b_3d, int unit)
  1105. {
  1106. /* TODO */
  1107. UNUSED_PARAMETER(device);
  1108. UNUSED_PARAMETER(b_3d);
  1109. UNUSED_PARAMETER(unit);
  1110. }
  1111. gs_shader_t *device_get_vertex_shader(const gs_device_t *device)
  1112. {
  1113. return device->curVertexShader;
  1114. }
  1115. gs_shader_t *device_get_pixel_shader(const gs_device_t *device)
  1116. {
  1117. return device->curPixelShader;
  1118. }
  1119. gs_texture_t *device_get_render_target(const gs_device_t *device)
  1120. {
  1121. if (device->curRenderTarget == &device->curSwapChain->target)
  1122. return NULL;
  1123. return device->curRenderTarget;
  1124. }
  1125. gs_zstencil_t *device_get_zstencil_target(const gs_device_t *device)
  1126. {
  1127. if (device->curZStencilBuffer == &device->curSwapChain->zs)
  1128. return NULL;
  1129. return device->curZStencilBuffer;
  1130. }
  1131. void device_set_render_target(gs_device_t *device, gs_texture_t *tex,
  1132. gs_zstencil_t *zstencil)
  1133. {
  1134. if (device->curSwapChain) {
  1135. if (!tex)
  1136. tex = &device->curSwapChain->target;
  1137. if (!zstencil)
  1138. zstencil = &device->curSwapChain->zs;
  1139. }
  1140. if (device->curRenderTarget == tex &&
  1141. device->curZStencilBuffer == zstencil)
  1142. return;
  1143. if (tex && tex->type != GS_TEXTURE_2D) {
  1144. blog(LOG_ERROR, "device_set_render_target (D3D11): "
  1145. "texture is not a 2D texture");
  1146. return;
  1147. }
  1148. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1149. if (tex2d && !tex2d->renderTarget[0]) {
  1150. blog(LOG_ERROR, "device_set_render_target (D3D11): "
  1151. "texture is not a render target");
  1152. return;
  1153. }
  1154. ID3D11RenderTargetView *rt = tex2d ? tex2d->renderTarget[0].Get()
  1155. : nullptr;
  1156. device->curRenderTarget = tex2d;
  1157. device->curRenderSide = 0;
  1158. device->curZStencilBuffer = zstencil;
  1159. device->context->OMSetRenderTargets(
  1160. 1, &rt, zstencil ? zstencil->view : nullptr);
  1161. }
  1162. void device_set_cube_render_target(gs_device_t *device, gs_texture_t *tex,
  1163. int side, gs_zstencil_t *zstencil)
  1164. {
  1165. if (device->curSwapChain) {
  1166. if (!tex) {
  1167. tex = &device->curSwapChain->target;
  1168. side = 0;
  1169. }
  1170. if (!zstencil)
  1171. zstencil = &device->curSwapChain->zs;
  1172. }
  1173. if (device->curRenderTarget == tex && device->curRenderSide == side &&
  1174. device->curZStencilBuffer == zstencil)
  1175. return;
  1176. if (tex->type != GS_TEXTURE_CUBE) {
  1177. blog(LOG_ERROR, "device_set_cube_render_target (D3D11): "
  1178. "texture is not a cube texture");
  1179. return;
  1180. }
  1181. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1182. if (!tex2d->renderTarget[side]) {
  1183. blog(LOG_ERROR, "device_set_cube_render_target (D3D11): "
  1184. "texture is not a render target");
  1185. return;
  1186. }
  1187. ID3D11RenderTargetView *rt = tex2d->renderTarget[0];
  1188. device->curRenderTarget = tex2d;
  1189. device->curRenderSide = side;
  1190. device->curZStencilBuffer = zstencil;
  1191. device->context->OMSetRenderTargets(1, &rt, zstencil->view);
  1192. }
  1193. inline void gs_device::CopyTex(ID3D11Texture2D *dst, uint32_t dst_x,
  1194. uint32_t dst_y, gs_texture_t *src,
  1195. uint32_t src_x, uint32_t src_y, uint32_t src_w,
  1196. uint32_t src_h)
  1197. {
  1198. if (src->type != GS_TEXTURE_2D)
  1199. throw "Source texture must be a 2D texture";
  1200. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(src);
  1201. if (dst_x == 0 && dst_y == 0 && src_x == 0 && src_y == 0 &&
  1202. src_w == 0 && src_h == 0) {
  1203. context->CopyResource(dst, tex2d->texture);
  1204. } else {
  1205. D3D11_BOX sbox;
  1206. sbox.left = src_x;
  1207. if (src_w > 0)
  1208. sbox.right = src_x + src_w;
  1209. else
  1210. sbox.right = tex2d->width - 1;
  1211. sbox.top = src_y;
  1212. if (src_h > 0)
  1213. sbox.bottom = src_y + src_h;
  1214. else
  1215. sbox.bottom = tex2d->height - 1;
  1216. sbox.front = 0;
  1217. sbox.back = 1;
  1218. context->CopySubresourceRegion(dst, 0, dst_x, dst_y, 0,
  1219. tex2d->texture, 0, &sbox);
  1220. }
  1221. }
  1222. void device_copy_texture_region(gs_device_t *device, gs_texture_t *dst,
  1223. uint32_t dst_x, uint32_t dst_y,
  1224. gs_texture_t *src, uint32_t src_x,
  1225. uint32_t src_y, uint32_t src_w, uint32_t src_h)
  1226. {
  1227. try {
  1228. gs_texture_2d *src2d = static_cast<gs_texture_2d *>(src);
  1229. gs_texture_2d *dst2d = static_cast<gs_texture_2d *>(dst);
  1230. if (!src)
  1231. throw "Source texture is NULL";
  1232. if (!dst)
  1233. throw "Destination texture is NULL";
  1234. if (src->type != GS_TEXTURE_2D || dst->type != GS_TEXTURE_2D)
  1235. throw "Source and destination textures must be a 2D "
  1236. "textures";
  1237. if (dst->format != src->format)
  1238. throw "Source and destination formats do not match";
  1239. /* apparently casting to the same type that the variable
  1240. * already exists as is supposed to prevent some warning
  1241. * when used with the conditional operator? */
  1242. uint32_t copyWidth = (uint32_t)src_w ? (uint32_t)src_w
  1243. : (src2d->width - src_x);
  1244. uint32_t copyHeight = (uint32_t)src_h ? (uint32_t)src_h
  1245. : (src2d->height - src_y);
  1246. uint32_t dstWidth = dst2d->width - dst_x;
  1247. uint32_t dstHeight = dst2d->height - dst_y;
  1248. if (dstWidth < copyWidth || dstHeight < copyHeight)
  1249. throw "Destination texture region is not big "
  1250. "enough to hold the source region";
  1251. if (dst_x == 0 && dst_y == 0 && src_x == 0 && src_y == 0 &&
  1252. src_w == 0 && src_h == 0) {
  1253. copyWidth = 0;
  1254. copyHeight = 0;
  1255. }
  1256. device->CopyTex(dst2d->texture, dst_x, dst_y, src, src_x, src_y,
  1257. copyWidth, copyHeight);
  1258. } catch (const char *error) {
  1259. blog(LOG_ERROR, "device_copy_texture (D3D11): %s", error);
  1260. }
  1261. }
  1262. void device_copy_texture(gs_device_t *device, gs_texture_t *dst,
  1263. gs_texture_t *src)
  1264. {
  1265. device_copy_texture_region(device, dst, 0, 0, src, 0, 0, 0, 0);
  1266. }
  1267. void device_stage_texture(gs_device_t *device, gs_stagesurf_t *dst,
  1268. gs_texture_t *src)
  1269. {
  1270. try {
  1271. gs_texture_2d *src2d = static_cast<gs_texture_2d *>(src);
  1272. if (!src)
  1273. throw "Source texture is NULL";
  1274. if (src->type != GS_TEXTURE_2D)
  1275. throw "Source texture must be a 2D texture";
  1276. if (!dst)
  1277. throw "Destination surface is NULL";
  1278. if (dst->format != GS_UNKNOWN && dst->format != src->format)
  1279. throw "Source and destination formats do not match";
  1280. if (dst->width != src2d->width || dst->height != src2d->height)
  1281. throw "Source and destination must have the same "
  1282. "dimensions";
  1283. device->CopyTex(dst->texture, 0, 0, src, 0, 0, 0, 0);
  1284. } catch (const char *error) {
  1285. blog(LOG_ERROR, "device_copy_texture (D3D11): %s", error);
  1286. }
  1287. }
  1288. void device_begin_scene(gs_device_t *device)
  1289. {
  1290. clear_textures(device);
  1291. }
  1292. void device_draw(gs_device_t *device, enum gs_draw_mode draw_mode,
  1293. uint32_t start_vert, uint32_t num_verts)
  1294. {
  1295. try {
  1296. if (!device->curVertexShader)
  1297. throw "No vertex shader specified";
  1298. if (!device->curPixelShader)
  1299. throw "No pixel shader specified";
  1300. if (!device->curVertexBuffer && (num_verts == 0))
  1301. throw "No vertex buffer specified";
  1302. if (!device->curSwapChain && !device->curRenderTarget)
  1303. throw "No render target or swap chain to render to";
  1304. gs_effect_t *effect = gs_get_effect();
  1305. if (effect)
  1306. gs_effect_update_params(effect);
  1307. device->LoadVertexBufferData();
  1308. device->UpdateBlendState();
  1309. device->UpdateRasterState();
  1310. device->UpdateZStencilState();
  1311. device->UpdateViewProjMatrix();
  1312. device->curVertexShader->UploadParams();
  1313. device->curPixelShader->UploadParams();
  1314. } catch (const char *error) {
  1315. blog(LOG_ERROR, "device_draw (D3D11): %s", error);
  1316. return;
  1317. } catch (HRError &error) {
  1318. blog(LOG_ERROR, "device_draw (D3D11): %s (%08lX)", error.str,
  1319. error.hr);
  1320. LogD3D11ErrorDetails(error, device);
  1321. return;
  1322. }
  1323. D3D11_PRIMITIVE_TOPOLOGY newTopology = ConvertGSTopology(draw_mode);
  1324. if (device->curToplogy != newTopology) {
  1325. device->context->IASetPrimitiveTopology(newTopology);
  1326. device->curToplogy = newTopology;
  1327. }
  1328. if (device->curIndexBuffer) {
  1329. if (num_verts == 0)
  1330. num_verts = (uint32_t)device->curIndexBuffer->num;
  1331. device->context->DrawIndexed(num_verts, start_vert, 0);
  1332. } else {
  1333. if (num_verts == 0)
  1334. num_verts = (uint32_t)device->curVertexBuffer->numVerts;
  1335. device->context->Draw(num_verts, start_vert);
  1336. }
  1337. }
  1338. void device_end_scene(gs_device_t *device)
  1339. {
  1340. /* does nothing in D3D11 */
  1341. UNUSED_PARAMETER(device);
  1342. }
  1343. void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swapchain)
  1344. {
  1345. gs_texture_t *target = device->curRenderTarget;
  1346. gs_zstencil_t *zs = device->curZStencilBuffer;
  1347. bool is_cube =
  1348. device->curRenderTarget
  1349. ? (device->curRenderTarget->type == GS_TEXTURE_CUBE)
  1350. : false;
  1351. if (device->curSwapChain) {
  1352. if (target == &device->curSwapChain->target)
  1353. target = NULL;
  1354. if (zs == &device->curSwapChain->zs)
  1355. zs = NULL;
  1356. }
  1357. device->curSwapChain = swapchain;
  1358. if (is_cube)
  1359. device_set_cube_render_target(device, target,
  1360. device->curRenderSide, zs);
  1361. else
  1362. device_set_render_target(device, target, zs);
  1363. }
  1364. void device_clear(gs_device_t *device, uint32_t clear_flags,
  1365. const struct vec4 *color, float depth, uint8_t stencil)
  1366. {
  1367. int side = device->curRenderSide;
  1368. if ((clear_flags & GS_CLEAR_COLOR) != 0 && device->curRenderTarget)
  1369. device->context->ClearRenderTargetView(
  1370. device->curRenderTarget->renderTarget[side],
  1371. color->ptr);
  1372. if (device->curZStencilBuffer) {
  1373. uint32_t flags = 0;
  1374. if ((clear_flags & GS_CLEAR_DEPTH) != 0)
  1375. flags |= D3D11_CLEAR_DEPTH;
  1376. if ((clear_flags & GS_CLEAR_STENCIL) != 0)
  1377. flags |= D3D11_CLEAR_STENCIL;
  1378. if (flags && device->curZStencilBuffer->view)
  1379. device->context->ClearDepthStencilView(
  1380. device->curZStencilBuffer->view, flags, depth,
  1381. stencil);
  1382. }
  1383. }
  1384. void device_present(gs_device_t *device)
  1385. {
  1386. HRESULT hr;
  1387. if (device->curSwapChain) {
  1388. hr = device->curSwapChain->swap->Present(0, 0);
  1389. if (hr == DXGI_ERROR_DEVICE_REMOVED ||
  1390. hr == DXGI_ERROR_DEVICE_RESET) {
  1391. device->RebuildDevice();
  1392. }
  1393. } else {
  1394. blog(LOG_WARNING, "device_present (D3D11): No active swap");
  1395. }
  1396. }
  1397. extern "C" void reset_duplicators(void);
  1398. void device_flush(gs_device_t *device)
  1399. {
  1400. device->context->Flush();
  1401. reset_duplicators();
  1402. }
  1403. void device_set_cull_mode(gs_device_t *device, enum gs_cull_mode mode)
  1404. {
  1405. if (mode == device->rasterState.cullMode)
  1406. return;
  1407. device->rasterState.cullMode = mode;
  1408. device->rasterStateChanged = true;
  1409. }
  1410. enum gs_cull_mode device_get_cull_mode(const gs_device_t *device)
  1411. {
  1412. return device->rasterState.cullMode;
  1413. }
  1414. void device_enable_blending(gs_device_t *device, bool enable)
  1415. {
  1416. if (enable == device->blendState.blendEnabled)
  1417. return;
  1418. device->blendState.blendEnabled = enable;
  1419. device->blendStateChanged = true;
  1420. }
  1421. void device_enable_depth_test(gs_device_t *device, bool enable)
  1422. {
  1423. if (enable == device->zstencilState.depthEnabled)
  1424. return;
  1425. device->zstencilState.depthEnabled = enable;
  1426. device->zstencilStateChanged = true;
  1427. }
  1428. void device_enable_stencil_test(gs_device_t *device, bool enable)
  1429. {
  1430. if (enable == device->zstencilState.stencilEnabled)
  1431. return;
  1432. device->zstencilState.stencilEnabled = enable;
  1433. device->zstencilStateChanged = true;
  1434. }
  1435. void device_enable_stencil_write(gs_device_t *device, bool enable)
  1436. {
  1437. if (enable == device->zstencilState.stencilWriteEnabled)
  1438. return;
  1439. device->zstencilState.stencilWriteEnabled = enable;
  1440. device->zstencilStateChanged = true;
  1441. }
  1442. void device_enable_color(gs_device_t *device, bool red, bool green, bool blue,
  1443. bool alpha)
  1444. {
  1445. if (device->blendState.redEnabled == red &&
  1446. device->blendState.greenEnabled == green &&
  1447. device->blendState.blueEnabled == blue &&
  1448. device->blendState.alphaEnabled == alpha)
  1449. return;
  1450. device->blendState.redEnabled = red;
  1451. device->blendState.greenEnabled = green;
  1452. device->blendState.blueEnabled = blue;
  1453. device->blendState.alphaEnabled = alpha;
  1454. device->blendStateChanged = true;
  1455. }
  1456. void device_blend_function(gs_device_t *device, enum gs_blend_type src,
  1457. enum gs_blend_type dest)
  1458. {
  1459. if (device->blendState.srcFactorC == src &&
  1460. device->blendState.destFactorC == dest &&
  1461. device->blendState.srcFactorA == src &&
  1462. device->blendState.destFactorA == dest)
  1463. return;
  1464. device->blendState.srcFactorC = src;
  1465. device->blendState.destFactorC = dest;
  1466. device->blendState.srcFactorA = src;
  1467. device->blendState.destFactorA = dest;
  1468. device->blendStateChanged = true;
  1469. }
  1470. void device_blend_function_separate(gs_device_t *device,
  1471. enum gs_blend_type src_c,
  1472. enum gs_blend_type dest_c,
  1473. enum gs_blend_type src_a,
  1474. enum gs_blend_type dest_a)
  1475. {
  1476. if (device->blendState.srcFactorC == src_c &&
  1477. device->blendState.destFactorC == dest_c &&
  1478. device->blendState.srcFactorA == src_a &&
  1479. device->blendState.destFactorA == dest_a)
  1480. return;
  1481. device->blendState.srcFactorC = src_c;
  1482. device->blendState.destFactorC = dest_c;
  1483. device->blendState.srcFactorA = src_a;
  1484. device->blendState.destFactorA = dest_a;
  1485. device->blendStateChanged = true;
  1486. }
  1487. void device_depth_function(gs_device_t *device, enum gs_depth_test test)
  1488. {
  1489. if (device->zstencilState.depthFunc == test)
  1490. return;
  1491. device->zstencilState.depthFunc = test;
  1492. device->zstencilStateChanged = true;
  1493. }
  1494. static inline void update_stencilside_test(gs_device_t *device,
  1495. StencilSide &side,
  1496. gs_depth_test test)
  1497. {
  1498. if (side.test == test)
  1499. return;
  1500. side.test = test;
  1501. device->zstencilStateChanged = true;
  1502. }
  1503. void device_stencil_function(gs_device_t *device, enum gs_stencil_side side,
  1504. enum gs_depth_test test)
  1505. {
  1506. int sideVal = (int)side;
  1507. if (sideVal & GS_STENCIL_FRONT)
  1508. update_stencilside_test(
  1509. device, device->zstencilState.stencilFront, test);
  1510. if (sideVal & GS_STENCIL_BACK)
  1511. update_stencilside_test(
  1512. device, device->zstencilState.stencilBack, test);
  1513. }
  1514. static inline void update_stencilside_op(gs_device_t *device, StencilSide &side,
  1515. enum gs_stencil_op_type fail,
  1516. enum gs_stencil_op_type zfail,
  1517. enum gs_stencil_op_type zpass)
  1518. {
  1519. if (side.fail == fail && side.zfail == zfail && side.zpass == zpass)
  1520. return;
  1521. side.fail = fail;
  1522. side.zfail = zfail;
  1523. side.zpass = zpass;
  1524. device->zstencilStateChanged = true;
  1525. }
  1526. void device_stencil_op(gs_device_t *device, enum gs_stencil_side side,
  1527. enum gs_stencil_op_type fail,
  1528. enum gs_stencil_op_type zfail,
  1529. enum gs_stencil_op_type zpass)
  1530. {
  1531. int sideVal = (int)side;
  1532. if (sideVal & GS_STENCIL_FRONT)
  1533. update_stencilside_op(device,
  1534. device->zstencilState.stencilFront, fail,
  1535. zfail, zpass);
  1536. if (sideVal & GS_STENCIL_BACK)
  1537. update_stencilside_op(device, device->zstencilState.stencilBack,
  1538. fail, zfail, zpass);
  1539. }
  1540. void device_set_viewport(gs_device_t *device, int x, int y, int width,
  1541. int height)
  1542. {
  1543. D3D11_VIEWPORT vp;
  1544. memset(&vp, 0, sizeof(vp));
  1545. vp.MaxDepth = 1.0f;
  1546. vp.TopLeftX = (float)x;
  1547. vp.TopLeftY = (float)y;
  1548. vp.Width = (float)width;
  1549. vp.Height = (float)height;
  1550. device->context->RSSetViewports(1, &vp);
  1551. device->viewport.x = x;
  1552. device->viewport.y = y;
  1553. device->viewport.cx = width;
  1554. device->viewport.cy = height;
  1555. }
  1556. void device_get_viewport(const gs_device_t *device, struct gs_rect *rect)
  1557. {
  1558. memcpy(rect, &device->viewport, sizeof(gs_rect));
  1559. }
  1560. void device_set_scissor_rect(gs_device_t *device, const struct gs_rect *rect)
  1561. {
  1562. D3D11_RECT d3drect;
  1563. device->rasterState.scissorEnabled = (rect != NULL);
  1564. if (rect != NULL) {
  1565. d3drect.left = rect->x;
  1566. d3drect.top = rect->y;
  1567. d3drect.right = rect->x + rect->cx;
  1568. d3drect.bottom = rect->y + rect->cy;
  1569. device->context->RSSetScissorRects(1, &d3drect);
  1570. }
  1571. device->rasterStateChanged = true;
  1572. }
  1573. void device_ortho(gs_device_t *device, float left, float right, float top,
  1574. float bottom, float zNear, float zFar)
  1575. {
  1576. matrix4 *dst = &device->curProjMatrix;
  1577. float rml = right - left;
  1578. float bmt = bottom - top;
  1579. float fmn = zFar - zNear;
  1580. vec4_zero(&dst->x);
  1581. vec4_zero(&dst->y);
  1582. vec4_zero(&dst->z);
  1583. vec4_zero(&dst->t);
  1584. dst->x.x = 2.0f / rml;
  1585. dst->t.x = (left + right) / -rml;
  1586. dst->y.y = 2.0f / -bmt;
  1587. dst->t.y = (bottom + top) / bmt;
  1588. dst->z.z = 1.0f / fmn;
  1589. dst->t.z = zNear / -fmn;
  1590. dst->t.w = 1.0f;
  1591. }
  1592. void device_frustum(gs_device_t *device, float left, float right, float top,
  1593. float bottom, float zNear, float zFar)
  1594. {
  1595. matrix4 *dst = &device->curProjMatrix;
  1596. float rml = right - left;
  1597. float bmt = bottom - top;
  1598. float fmn = zFar - zNear;
  1599. float nearx2 = 2.0f * zNear;
  1600. vec4_zero(&dst->x);
  1601. vec4_zero(&dst->y);
  1602. vec4_zero(&dst->z);
  1603. vec4_zero(&dst->t);
  1604. dst->x.x = nearx2 / rml;
  1605. dst->z.x = (left + right) / -rml;
  1606. dst->y.y = nearx2 / -bmt;
  1607. dst->z.y = (bottom + top) / bmt;
  1608. dst->z.z = zFar / fmn;
  1609. dst->t.z = (zNear * zFar) / -fmn;
  1610. dst->z.w = 1.0f;
  1611. }
  1612. void device_projection_push(gs_device_t *device)
  1613. {
  1614. mat4float mat;
  1615. memcpy(&mat, &device->curProjMatrix, sizeof(matrix4));
  1616. device->projStack.push_back(mat);
  1617. }
  1618. void device_projection_pop(gs_device_t *device)
  1619. {
  1620. if (device->projStack.empty())
  1621. return;
  1622. const mat4float &mat = device->projStack.back();
  1623. memcpy(&device->curProjMatrix, &mat, sizeof(matrix4));
  1624. device->projStack.pop_back();
  1625. }
  1626. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1627. {
  1628. if (swapchain->device->curSwapChain == swapchain)
  1629. device_load_swapchain(swapchain->device, nullptr);
  1630. delete swapchain;
  1631. }
  1632. void gs_texture_destroy(gs_texture_t *tex)
  1633. {
  1634. delete tex;
  1635. }
  1636. uint32_t gs_texture_get_width(const gs_texture_t *tex)
  1637. {
  1638. if (tex->type != GS_TEXTURE_2D)
  1639. return 0;
  1640. return static_cast<const gs_texture_2d *>(tex)->width;
  1641. }
  1642. uint32_t gs_texture_get_height(const gs_texture_t *tex)
  1643. {
  1644. if (tex->type != GS_TEXTURE_2D)
  1645. return 0;
  1646. return static_cast<const gs_texture_2d *>(tex)->height;
  1647. }
  1648. enum gs_color_format gs_texture_get_color_format(const gs_texture_t *tex)
  1649. {
  1650. if (tex->type != GS_TEXTURE_2D)
  1651. return GS_UNKNOWN;
  1652. return static_cast<const gs_texture_2d *>(tex)->format;
  1653. }
  1654. bool gs_texture_map(gs_texture_t *tex, uint8_t **ptr, uint32_t *linesize)
  1655. {
  1656. HRESULT hr;
  1657. if (tex->type != GS_TEXTURE_2D)
  1658. return false;
  1659. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1660. D3D11_MAPPED_SUBRESOURCE map;
  1661. hr = tex2d->device->context->Map(tex2d->texture, 0,
  1662. D3D11_MAP_WRITE_DISCARD, 0, &map);
  1663. if (FAILED(hr))
  1664. return false;
  1665. *ptr = (uint8_t *)map.pData;
  1666. *linesize = map.RowPitch;
  1667. return true;
  1668. }
  1669. void gs_texture_unmap(gs_texture_t *tex)
  1670. {
  1671. if (tex->type != GS_TEXTURE_2D)
  1672. return;
  1673. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1674. tex2d->device->context->Unmap(tex2d->texture, 0);
  1675. }
  1676. void *gs_texture_get_obj(gs_texture_t *tex)
  1677. {
  1678. if (tex->type != GS_TEXTURE_2D)
  1679. return nullptr;
  1680. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1681. return tex2d->texture.Get();
  1682. }
  1683. void gs_cubetexture_destroy(gs_texture_t *cubetex)
  1684. {
  1685. delete cubetex;
  1686. }
  1687. uint32_t gs_cubetexture_get_size(const gs_texture_t *cubetex)
  1688. {
  1689. if (cubetex->type != GS_TEXTURE_CUBE)
  1690. return 0;
  1691. const gs_texture_2d *tex = static_cast<const gs_texture_2d *>(cubetex);
  1692. return tex->width;
  1693. }
  1694. enum gs_color_format
  1695. gs_cubetexture_get_color_format(const gs_texture_t *cubetex)
  1696. {
  1697. if (cubetex->type != GS_TEXTURE_CUBE)
  1698. return GS_UNKNOWN;
  1699. const gs_texture_2d *tex = static_cast<const gs_texture_2d *>(cubetex);
  1700. return tex->format;
  1701. }
  1702. void gs_voltexture_destroy(gs_texture_t *voltex)
  1703. {
  1704. delete voltex;
  1705. }
  1706. uint32_t gs_voltexture_get_width(const gs_texture_t *voltex)
  1707. {
  1708. /* TODO */
  1709. UNUSED_PARAMETER(voltex);
  1710. return 0;
  1711. }
  1712. uint32_t gs_voltexture_get_height(const gs_texture_t *voltex)
  1713. {
  1714. /* TODO */
  1715. UNUSED_PARAMETER(voltex);
  1716. return 0;
  1717. }
  1718. uint32_t gs_voltexture_get_depth(const gs_texture_t *voltex)
  1719. {
  1720. /* TODO */
  1721. UNUSED_PARAMETER(voltex);
  1722. return 0;
  1723. }
  1724. enum gs_color_format gs_voltexture_get_color_format(const gs_texture_t *voltex)
  1725. {
  1726. /* TODO */
  1727. UNUSED_PARAMETER(voltex);
  1728. return GS_UNKNOWN;
  1729. }
  1730. void gs_stagesurface_destroy(gs_stagesurf_t *stagesurf)
  1731. {
  1732. delete stagesurf;
  1733. }
  1734. uint32_t gs_stagesurface_get_width(const gs_stagesurf_t *stagesurf)
  1735. {
  1736. return stagesurf->width;
  1737. }
  1738. uint32_t gs_stagesurface_get_height(const gs_stagesurf_t *stagesurf)
  1739. {
  1740. return stagesurf->height;
  1741. }
  1742. enum gs_color_format
  1743. gs_stagesurface_get_color_format(const gs_stagesurf_t *stagesurf)
  1744. {
  1745. return stagesurf->format;
  1746. }
  1747. bool gs_stagesurface_map(gs_stagesurf_t *stagesurf, uint8_t **data,
  1748. uint32_t *linesize)
  1749. {
  1750. D3D11_MAPPED_SUBRESOURCE map;
  1751. if (FAILED(stagesurf->device->context->Map(stagesurf->texture, 0,
  1752. D3D11_MAP_READ, 0, &map)))
  1753. return false;
  1754. *data = (uint8_t *)map.pData;
  1755. *linesize = map.RowPitch;
  1756. return true;
  1757. }
  1758. void gs_stagesurface_unmap(gs_stagesurf_t *stagesurf)
  1759. {
  1760. stagesurf->device->context->Unmap(stagesurf->texture, 0);
  1761. }
  1762. void gs_zstencil_destroy(gs_zstencil_t *zstencil)
  1763. {
  1764. delete zstencil;
  1765. }
  1766. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1767. {
  1768. if (!samplerstate)
  1769. return;
  1770. if (samplerstate->device)
  1771. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1772. if (samplerstate->device->curSamplers[i] ==
  1773. samplerstate)
  1774. samplerstate->device->curSamplers[i] = nullptr;
  1775. delete samplerstate;
  1776. }
  1777. void gs_vertexbuffer_destroy(gs_vertbuffer_t *vertbuffer)
  1778. {
  1779. if (vertbuffer && vertbuffer->device->lastVertexBuffer == vertbuffer)
  1780. vertbuffer->device->lastVertexBuffer = nullptr;
  1781. delete vertbuffer;
  1782. }
  1783. static inline void gs_vertexbuffer_flush_internal(gs_vertbuffer_t *vertbuffer,
  1784. const gs_vb_data *data)
  1785. {
  1786. size_t num_tex = data->num_tex < vertbuffer->uvBuffers.size()
  1787. ? data->num_tex
  1788. : vertbuffer->uvBuffers.size();
  1789. if (!vertbuffer->dynamic) {
  1790. blog(LOG_ERROR, "gs_vertexbuffer_flush: vertex buffer is "
  1791. "not dynamic");
  1792. return;
  1793. }
  1794. if (data->points)
  1795. vertbuffer->FlushBuffer(vertbuffer->vertexBuffer, data->points,
  1796. sizeof(vec3));
  1797. if (vertbuffer->normalBuffer && data->normals)
  1798. vertbuffer->FlushBuffer(vertbuffer->normalBuffer, data->normals,
  1799. sizeof(vec3));
  1800. if (vertbuffer->tangentBuffer && data->tangents)
  1801. vertbuffer->FlushBuffer(vertbuffer->tangentBuffer,
  1802. data->tangents, sizeof(vec3));
  1803. if (vertbuffer->colorBuffer && data->colors)
  1804. vertbuffer->FlushBuffer(vertbuffer->colorBuffer, data->colors,
  1805. sizeof(uint32_t));
  1806. for (size_t i = 0; i < num_tex; i++) {
  1807. gs_tvertarray &tv = data->tvarray[i];
  1808. vertbuffer->FlushBuffer(vertbuffer->uvBuffers[i], tv.array,
  1809. tv.width * sizeof(float));
  1810. }
  1811. }
  1812. void gs_vertexbuffer_flush(gs_vertbuffer_t *vertbuffer)
  1813. {
  1814. gs_vertexbuffer_flush_internal(vertbuffer, vertbuffer->vbd.data);
  1815. }
  1816. void gs_vertexbuffer_flush_direct(gs_vertbuffer_t *vertbuffer,
  1817. const gs_vb_data *data)
  1818. {
  1819. gs_vertexbuffer_flush_internal(vertbuffer, data);
  1820. }
  1821. struct gs_vb_data *gs_vertexbuffer_get_data(const gs_vertbuffer_t *vertbuffer)
  1822. {
  1823. return vertbuffer->vbd.data;
  1824. }
  1825. void gs_indexbuffer_destroy(gs_indexbuffer_t *indexbuffer)
  1826. {
  1827. delete indexbuffer;
  1828. }
  1829. static inline void gs_indexbuffer_flush_internal(gs_indexbuffer_t *indexbuffer,
  1830. const void *data)
  1831. {
  1832. HRESULT hr;
  1833. if (!indexbuffer->dynamic)
  1834. return;
  1835. D3D11_MAPPED_SUBRESOURCE map;
  1836. hr = indexbuffer->device->context->Map(
  1837. indexbuffer->indexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
  1838. if (FAILED(hr))
  1839. return;
  1840. memcpy(map.pData, data, indexbuffer->num * indexbuffer->indexSize);
  1841. indexbuffer->device->context->Unmap(indexbuffer->indexBuffer, 0);
  1842. }
  1843. void gs_indexbuffer_flush(gs_indexbuffer_t *indexbuffer)
  1844. {
  1845. gs_indexbuffer_flush_internal(indexbuffer, indexbuffer->indices.data);
  1846. }
  1847. void gs_indexbuffer_flush_direct(gs_indexbuffer_t *indexbuffer,
  1848. const void *data)
  1849. {
  1850. gs_indexbuffer_flush_internal(indexbuffer, data);
  1851. }
  1852. void *gs_indexbuffer_get_data(const gs_indexbuffer_t *indexbuffer)
  1853. {
  1854. return indexbuffer->indices.data;
  1855. }
  1856. size_t gs_indexbuffer_get_num_indices(const gs_indexbuffer_t *indexbuffer)
  1857. {
  1858. return indexbuffer->num;
  1859. }
  1860. enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *indexbuffer)
  1861. {
  1862. return indexbuffer->type;
  1863. }
  1864. void gs_timer_destroy(gs_timer_t *timer)
  1865. {
  1866. delete timer;
  1867. }
  1868. void gs_timer_begin(gs_timer_t *timer)
  1869. {
  1870. timer->device->context->End(timer->query_begin);
  1871. }
  1872. void gs_timer_end(gs_timer_t *timer)
  1873. {
  1874. timer->device->context->End(timer->query_end);
  1875. }
  1876. bool gs_timer_get_data(gs_timer_t *timer, uint64_t *ticks)
  1877. {
  1878. uint64_t begin, end;
  1879. HRESULT hr_begin, hr_end;
  1880. do {
  1881. hr_begin = timer->device->context->GetData(
  1882. timer->query_begin, &begin, sizeof(begin), 0);
  1883. } while (hr_begin == S_FALSE);
  1884. do {
  1885. hr_end = timer->device->context->GetData(timer->query_end, &end,
  1886. sizeof(end), 0);
  1887. } while (hr_end == S_FALSE);
  1888. const bool succeeded = SUCCEEDED(hr_begin) && SUCCEEDED(hr_end);
  1889. if (succeeded)
  1890. *ticks = end - begin;
  1891. return succeeded;
  1892. }
  1893. void gs_timer_range_destroy(gs_timer_range_t *range)
  1894. {
  1895. delete range;
  1896. }
  1897. void gs_timer_range_begin(gs_timer_range_t *range)
  1898. {
  1899. range->device->context->Begin(range->query_disjoint);
  1900. }
  1901. void gs_timer_range_end(gs_timer_range_t *range)
  1902. {
  1903. range->device->context->End(range->query_disjoint);
  1904. }
  1905. bool gs_timer_range_get_data(gs_timer_range_t *range, bool *disjoint,
  1906. uint64_t *frequency)
  1907. {
  1908. D3D11_QUERY_DATA_TIMESTAMP_DISJOINT timestamp_disjoint;
  1909. HRESULT hr;
  1910. do {
  1911. hr = range->device->context->GetData(range->query_disjoint,
  1912. &timestamp_disjoint,
  1913. sizeof(timestamp_disjoint),
  1914. 0);
  1915. } while (hr == S_FALSE);
  1916. const bool succeeded = SUCCEEDED(hr);
  1917. if (succeeded) {
  1918. *disjoint = timestamp_disjoint.Disjoint;
  1919. *frequency = timestamp_disjoint.Frequency;
  1920. }
  1921. return succeeded;
  1922. }
  1923. gs_timer::gs_timer(gs_device_t *device) : gs_obj(device, gs_type::gs_timer)
  1924. {
  1925. Rebuild(device->device);
  1926. }
  1927. gs_timer_range::gs_timer_range(gs_device_t *device)
  1928. : gs_obj(device, gs_type::gs_timer_range)
  1929. {
  1930. Rebuild(device->device);
  1931. }
  1932. extern "C" EXPORT bool device_gdi_texture_available(void)
  1933. {
  1934. return true;
  1935. }
  1936. extern "C" EXPORT bool device_shared_texture_available(void)
  1937. {
  1938. return true;
  1939. }
  1940. extern "C" EXPORT bool device_nv12_available(gs_device_t *device)
  1941. {
  1942. return device->nv12Supported;
  1943. }
  1944. extern "C" EXPORT void device_debug_marker_begin(gs_device_t *,
  1945. const char *markername,
  1946. const float color[4])
  1947. {
  1948. D3DCOLOR bgra = D3DCOLOR_ARGB((DWORD)(255.0f * color[3]),
  1949. (DWORD)(255.0f * color[0]),
  1950. (DWORD)(255.0f * color[1]),
  1951. (DWORD)(255.0f * color[2]));
  1952. wchar_t wide[64];
  1953. os_utf8_to_wcs(markername, 0, wide, _countof(wide));
  1954. D3DPERF_BeginEvent(bgra, wide);
  1955. }
  1956. extern "C" EXPORT void device_debug_marker_end(gs_device_t *)
  1957. {
  1958. D3DPERF_EndEvent();
  1959. }
  1960. extern "C" EXPORT gs_texture_t *
  1961. device_texture_create_gdi(gs_device_t *device, uint32_t width, uint32_t height)
  1962. {
  1963. gs_texture *texture = nullptr;
  1964. try {
  1965. texture = new gs_texture_2d(device, width, height, GS_BGRA, 1,
  1966. nullptr, GS_RENDER_TARGET,
  1967. GS_TEXTURE_2D, true);
  1968. } catch (HRError &error) {
  1969. blog(LOG_ERROR, "device_texture_create_gdi (D3D11): %s (%08lX)",
  1970. error.str, error.hr);
  1971. LogD3D11ErrorDetails(error, device);
  1972. } catch (const char *error) {
  1973. blog(LOG_ERROR, "device_texture_create_gdi (D3D11): %s", error);
  1974. }
  1975. return texture;
  1976. }
  1977. static inline bool TextureGDICompatible(gs_texture_2d *tex2d, const char *func)
  1978. {
  1979. if (!tex2d->isGDICompatible) {
  1980. blog(LOG_ERROR, "%s (D3D11): Texture is not GDI compatible",
  1981. func);
  1982. return false;
  1983. }
  1984. return true;
  1985. }
  1986. extern "C" EXPORT void *gs_texture_get_dc(gs_texture_t *tex)
  1987. {
  1988. HDC hDC = nullptr;
  1989. if (tex->type != GS_TEXTURE_2D)
  1990. return nullptr;
  1991. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1992. if (!TextureGDICompatible(tex2d, "gs_texture_get_dc"))
  1993. return nullptr;
  1994. if (!tex2d->gdiSurface)
  1995. return nullptr;
  1996. tex2d->gdiSurface->GetDC(true, &hDC);
  1997. return hDC;
  1998. }
  1999. extern "C" EXPORT void gs_texture_release_dc(gs_texture_t *tex)
  2000. {
  2001. if (tex->type != GS_TEXTURE_2D)
  2002. return;
  2003. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  2004. if (!TextureGDICompatible(tex2d, "gs_texture_release_dc"))
  2005. return;
  2006. tex2d->gdiSurface->ReleaseDC(nullptr);
  2007. }
  2008. extern "C" EXPORT gs_texture_t *device_texture_open_shared(gs_device_t *device,
  2009. uint32_t handle)
  2010. {
  2011. gs_texture *texture = nullptr;
  2012. try {
  2013. texture = new gs_texture_2d(device, handle);
  2014. } catch (HRError &error) {
  2015. blog(LOG_ERROR, "gs_texture_open_shared (D3D11): %s (%08lX)",
  2016. error.str, error.hr);
  2017. LogD3D11ErrorDetails(error, device);
  2018. } catch (const char *error) {
  2019. blog(LOG_ERROR, "gs_texture_open_shared (D3D11): %s", error);
  2020. }
  2021. return texture;
  2022. }
  2023. extern "C" EXPORT uint32_t device_texture_get_shared_handle(gs_texture_t *tex)
  2024. {
  2025. gs_texture_2d *tex2d = reinterpret_cast<gs_texture_2d *>(tex);
  2026. if (tex->type != GS_TEXTURE_2D)
  2027. return GS_INVALID_HANDLE;
  2028. return tex2d->isShared ? tex2d->sharedHandle : GS_INVALID_HANDLE;
  2029. }
  2030. int device_texture_acquire_sync(gs_texture_t *tex, uint64_t key, uint32_t ms)
  2031. {
  2032. gs_texture_2d *tex2d = reinterpret_cast<gs_texture_2d *>(tex);
  2033. if (tex->type != GS_TEXTURE_2D)
  2034. return -1;
  2035. if (tex2d->acquired)
  2036. return 0;
  2037. ComQIPtr<IDXGIKeyedMutex> keyedMutex(tex2d->texture);
  2038. if (!keyedMutex)
  2039. return -1;
  2040. HRESULT hr = keyedMutex->AcquireSync(key, ms);
  2041. if (hr == S_OK) {
  2042. tex2d->acquired = true;
  2043. return 0;
  2044. } else if (hr == WAIT_TIMEOUT) {
  2045. return ETIMEDOUT;
  2046. }
  2047. return -1;
  2048. }
  2049. extern "C" EXPORT int device_texture_release_sync(gs_texture_t *tex,
  2050. uint64_t key)
  2051. {
  2052. gs_texture_2d *tex2d = reinterpret_cast<gs_texture_2d *>(tex);
  2053. if (tex->type != GS_TEXTURE_2D)
  2054. return -1;
  2055. if (!tex2d->acquired)
  2056. return 0;
  2057. ComQIPtr<IDXGIKeyedMutex> keyedMutex(tex2d->texture);
  2058. if (!keyedMutex)
  2059. return -1;
  2060. HRESULT hr = keyedMutex->ReleaseSync(key);
  2061. if (hr == S_OK) {
  2062. tex2d->acquired = false;
  2063. return 0;
  2064. }
  2065. return -1;
  2066. }
  2067. extern "C" EXPORT bool
  2068. device_texture_create_nv12(gs_device_t *device, gs_texture_t **p_tex_y,
  2069. gs_texture_t **p_tex_uv, uint32_t width,
  2070. uint32_t height, uint32_t flags)
  2071. {
  2072. if (!device->nv12Supported)
  2073. return false;
  2074. *p_tex_y = nullptr;
  2075. *p_tex_uv = nullptr;
  2076. gs_texture_2d *tex_y;
  2077. gs_texture_2d *tex_uv;
  2078. try {
  2079. tex_y = new gs_texture_2d(device, width, height, GS_R8, 1,
  2080. nullptr, flags, GS_TEXTURE_2D, false,
  2081. true);
  2082. tex_uv = new gs_texture_2d(device, tex_y->texture, flags);
  2083. } catch (HRError &error) {
  2084. blog(LOG_ERROR, "gs_texture_create_nv12 (D3D11): %s (%08lX)",
  2085. error.str, error.hr);
  2086. LogD3D11ErrorDetails(error, device);
  2087. return false;
  2088. } catch (const char *error) {
  2089. blog(LOG_ERROR, "gs_texture_create_nv12 (D3D11): %s", error);
  2090. return false;
  2091. }
  2092. tex_y->pairedNV12texture = tex_uv;
  2093. tex_uv->pairedNV12texture = tex_y;
  2094. *p_tex_y = tex_y;
  2095. *p_tex_uv = tex_uv;
  2096. return true;
  2097. }
  2098. extern "C" EXPORT gs_stagesurf_t *
  2099. device_stagesurface_create_nv12(gs_device_t *device, uint32_t width,
  2100. uint32_t height)
  2101. {
  2102. gs_stage_surface *surf = NULL;
  2103. try {
  2104. surf = new gs_stage_surface(device, width, height);
  2105. } catch (HRError &error) {
  2106. blog(LOG_ERROR,
  2107. "device_stagesurface_create (D3D11): %s "
  2108. "(%08lX)",
  2109. error.str, error.hr);
  2110. LogD3D11ErrorDetails(error, device);
  2111. }
  2112. return surf;
  2113. }