d3d11-subsystem.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573
  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. gs_swapchain_t *device_swapchain_create(gs_device_t *device,
  676. const struct gs_init_data *data)
  677. {
  678. gs_swap_chain *swap = NULL;
  679. try {
  680. swap = new gs_swap_chain(device, data);
  681. } catch (HRError error) {
  682. blog(LOG_ERROR, "device_swapchain_create (D3D11): %s (%08lX)",
  683. error.str, error.hr);
  684. LogD3D11ErrorDetails(error, device);
  685. }
  686. return swap;
  687. }
  688. void device_resize(gs_device_t *device, uint32_t cx, uint32_t cy)
  689. {
  690. if (!device->curSwapChain) {
  691. blog(LOG_WARNING, "device_resize (D3D11): No active swap");
  692. return;
  693. }
  694. try {
  695. ID3D11RenderTargetView *renderView = NULL;
  696. ID3D11DepthStencilView *depthView = NULL;
  697. int i = device->curRenderSide;
  698. device->context->OMSetRenderTargets(1, &renderView, depthView);
  699. device->curSwapChain->Resize(cx, cy);
  700. if (device->curRenderTarget)
  701. renderView = device->curRenderTarget->renderTarget[i];
  702. if (device->curZStencilBuffer)
  703. depthView = device->curZStencilBuffer->view;
  704. device->context->OMSetRenderTargets(1, &renderView, depthView);
  705. } catch (HRError error) {
  706. blog(LOG_ERROR, "device_resize (D3D11): %s (%08lX)", error.str,
  707. error.hr);
  708. LogD3D11ErrorDetails(error, device);
  709. }
  710. }
  711. void device_get_size(const gs_device_t *device, uint32_t *cx, uint32_t *cy)
  712. {
  713. if (device->curSwapChain) {
  714. *cx = device->curSwapChain->target.width;
  715. *cy = device->curSwapChain->target.height;
  716. } else {
  717. blog(LOG_ERROR, "device_get_size (D3D11): no active swap");
  718. *cx = 0;
  719. *cy = 0;
  720. }
  721. }
  722. uint32_t device_get_width(const gs_device_t *device)
  723. {
  724. if (device->curSwapChain) {
  725. return device->curSwapChain->target.width;
  726. } else {
  727. blog(LOG_ERROR, "device_get_size (D3D11): no active swap");
  728. return 0;
  729. }
  730. }
  731. uint32_t device_get_height(const gs_device_t *device)
  732. {
  733. if (device->curSwapChain) {
  734. return device->curSwapChain->target.height;
  735. } else {
  736. blog(LOG_ERROR, "device_get_size (D3D11): no active swap");
  737. return 0;
  738. }
  739. }
  740. gs_texture_t *device_texture_create(gs_device_t *device, uint32_t width,
  741. uint32_t height,
  742. enum gs_color_format color_format,
  743. uint32_t levels, const uint8_t **data,
  744. uint32_t flags)
  745. {
  746. gs_texture *texture = NULL;
  747. try {
  748. texture = new gs_texture_2d(device, width, height, color_format,
  749. levels, data, flags, GS_TEXTURE_2D,
  750. false);
  751. } catch (HRError error) {
  752. blog(LOG_ERROR, "device_texture_create (D3D11): %s (%08lX)",
  753. error.str, error.hr);
  754. LogD3D11ErrorDetails(error, device);
  755. } catch (const char *error) {
  756. blog(LOG_ERROR, "device_texture_create (D3D11): %s", error);
  757. }
  758. return texture;
  759. }
  760. gs_texture_t *device_cubetexture_create(gs_device_t *device, uint32_t size,
  761. enum gs_color_format color_format,
  762. uint32_t levels, const uint8_t **data,
  763. uint32_t flags)
  764. {
  765. gs_texture *texture = NULL;
  766. try {
  767. texture = new gs_texture_2d(device, size, size, color_format,
  768. levels, data, flags,
  769. GS_TEXTURE_CUBE, false);
  770. } catch (HRError error) {
  771. blog(LOG_ERROR,
  772. "device_cubetexture_create (D3D11): %s "
  773. "(%08lX)",
  774. error.str, error.hr);
  775. LogD3D11ErrorDetails(error, device);
  776. } catch (const char *error) {
  777. blog(LOG_ERROR, "device_cubetexture_create (D3D11): %s", error);
  778. }
  779. return texture;
  780. }
  781. gs_texture_t *device_voltexture_create(gs_device_t *device, uint32_t width,
  782. uint32_t height, uint32_t depth,
  783. enum gs_color_format color_format,
  784. uint32_t levels, const uint8_t **data,
  785. uint32_t flags)
  786. {
  787. /* TODO */
  788. UNUSED_PARAMETER(device);
  789. UNUSED_PARAMETER(width);
  790. UNUSED_PARAMETER(height);
  791. UNUSED_PARAMETER(depth);
  792. UNUSED_PARAMETER(color_format);
  793. UNUSED_PARAMETER(levels);
  794. UNUSED_PARAMETER(data);
  795. UNUSED_PARAMETER(flags);
  796. return NULL;
  797. }
  798. gs_zstencil_t *device_zstencil_create(gs_device_t *device, uint32_t width,
  799. uint32_t height,
  800. enum gs_zstencil_format format)
  801. {
  802. gs_zstencil_buffer *zstencil = NULL;
  803. try {
  804. zstencil =
  805. new gs_zstencil_buffer(device, width, height, format);
  806. } catch (HRError error) {
  807. blog(LOG_ERROR, "device_zstencil_create (D3D11): %s (%08lX)",
  808. error.str, error.hr);
  809. LogD3D11ErrorDetails(error, device);
  810. }
  811. return zstencil;
  812. }
  813. gs_stagesurf_t *device_stagesurface_create(gs_device_t *device, uint32_t width,
  814. uint32_t height,
  815. enum gs_color_format color_format)
  816. {
  817. gs_stage_surface *surf = NULL;
  818. try {
  819. surf = new gs_stage_surface(device, width, height,
  820. color_format);
  821. } catch (HRError error) {
  822. blog(LOG_ERROR,
  823. "device_stagesurface_create (D3D11): %s "
  824. "(%08lX)",
  825. error.str, error.hr);
  826. LogD3D11ErrorDetails(error, device);
  827. }
  828. return surf;
  829. }
  830. gs_samplerstate_t *
  831. device_samplerstate_create(gs_device_t *device,
  832. const struct gs_sampler_info *info)
  833. {
  834. gs_sampler_state *ss = NULL;
  835. try {
  836. ss = new gs_sampler_state(device, info);
  837. } catch (HRError error) {
  838. blog(LOG_ERROR,
  839. "device_samplerstate_create (D3D11): %s "
  840. "(%08lX)",
  841. error.str, error.hr);
  842. LogD3D11ErrorDetails(error, device);
  843. }
  844. return ss;
  845. }
  846. gs_shader_t *device_vertexshader_create(gs_device_t *device,
  847. const char *shader_string,
  848. const char *file, char **error_string)
  849. {
  850. gs_vertex_shader *shader = NULL;
  851. try {
  852. shader = new gs_vertex_shader(device, file, shader_string);
  853. } catch (HRError error) {
  854. blog(LOG_ERROR,
  855. "device_vertexshader_create (D3D11): %s "
  856. "(%08lX)",
  857. error.str, error.hr);
  858. LogD3D11ErrorDetails(error, device);
  859. } catch (ShaderError error) {
  860. const char *buf =
  861. (const char *)error.errors->GetBufferPointer();
  862. if (error_string)
  863. *error_string = bstrdup(buf);
  864. blog(LOG_ERROR,
  865. "device_vertexshader_create (D3D11): "
  866. "Compile warnings/errors for %s:\n%s",
  867. file, buf);
  868. } catch (const char *error) {
  869. blog(LOG_ERROR, "device_vertexshader_create (D3D11): %s",
  870. error);
  871. }
  872. return shader;
  873. }
  874. gs_shader_t *device_pixelshader_create(gs_device_t *device,
  875. const char *shader_string,
  876. const char *file, char **error_string)
  877. {
  878. gs_pixel_shader *shader = NULL;
  879. try {
  880. shader = new gs_pixel_shader(device, file, shader_string);
  881. } catch (HRError error) {
  882. blog(LOG_ERROR,
  883. "device_pixelshader_create (D3D11): %s "
  884. "(%08lX)",
  885. error.str, error.hr);
  886. LogD3D11ErrorDetails(error, device);
  887. } catch (ShaderError error) {
  888. const char *buf =
  889. (const char *)error.errors->GetBufferPointer();
  890. if (error_string)
  891. *error_string = bstrdup(buf);
  892. blog(LOG_ERROR,
  893. "device_pixelshader_create (D3D11): "
  894. "Compiler warnings/errors for %s:\n%s",
  895. file, buf);
  896. } catch (const char *error) {
  897. blog(LOG_ERROR, "device_pixelshader_create (D3D11): %s", error);
  898. }
  899. return shader;
  900. }
  901. gs_vertbuffer_t *device_vertexbuffer_create(gs_device_t *device,
  902. struct gs_vb_data *data,
  903. uint32_t flags)
  904. {
  905. gs_vertex_buffer *buffer = NULL;
  906. try {
  907. buffer = new gs_vertex_buffer(device, data, flags);
  908. } catch (HRError error) {
  909. blog(LOG_ERROR,
  910. "device_vertexbuffer_create (D3D11): %s "
  911. "(%08lX)",
  912. error.str, error.hr);
  913. LogD3D11ErrorDetails(error, device);
  914. } catch (const char *error) {
  915. blog(LOG_ERROR, "device_vertexbuffer_create (D3D11): %s",
  916. error);
  917. }
  918. return buffer;
  919. }
  920. gs_indexbuffer_t *device_indexbuffer_create(gs_device_t *device,
  921. enum gs_index_type type,
  922. void *indices, size_t num,
  923. uint32_t flags)
  924. {
  925. gs_index_buffer *buffer = NULL;
  926. try {
  927. buffer = new gs_index_buffer(device, type, indices, num, flags);
  928. } catch (HRError error) {
  929. blog(LOG_ERROR, "device_indexbuffer_create (D3D11): %s (%08lX)",
  930. error.str, error.hr);
  931. LogD3D11ErrorDetails(error, device);
  932. }
  933. return buffer;
  934. }
  935. gs_timer_t *device_timer_create(gs_device_t *device)
  936. {
  937. gs_timer *timer = NULL;
  938. try {
  939. timer = new gs_timer(device);
  940. } catch (HRError error) {
  941. blog(LOG_ERROR, "device_timer_create (D3D11): %s (%08lX)",
  942. error.str, error.hr);
  943. LogD3D11ErrorDetails(error, device);
  944. }
  945. return timer;
  946. }
  947. gs_timer_range_t *device_timer_range_create(gs_device_t *device)
  948. {
  949. gs_timer_range *range = NULL;
  950. try {
  951. range = new gs_timer_range(device);
  952. } catch (HRError error) {
  953. blog(LOG_ERROR, "device_timer_range_create (D3D11): %s (%08lX)",
  954. error.str, error.hr);
  955. LogD3D11ErrorDetails(error, device);
  956. }
  957. return range;
  958. }
  959. enum gs_texture_type device_get_texture_type(const gs_texture_t *texture)
  960. {
  961. return texture->type;
  962. }
  963. void gs_device::LoadVertexBufferData()
  964. {
  965. if (curVertexBuffer == lastVertexBuffer &&
  966. curVertexShader == lastVertexShader)
  967. return;
  968. vector<ID3D11Buffer *> buffers;
  969. vector<uint32_t> strides;
  970. vector<uint32_t> offsets;
  971. if (curVertexBuffer && curVertexShader) {
  972. curVertexBuffer->MakeBufferList(curVertexShader, buffers,
  973. strides);
  974. } else {
  975. size_t buffersToClear =
  976. curVertexShader ? curVertexShader->NumBuffersExpected()
  977. : 0;
  978. buffers.resize(buffersToClear);
  979. strides.resize(buffersToClear);
  980. }
  981. offsets.resize(buffers.size());
  982. context->IASetVertexBuffers(0, (UINT)buffers.size(), buffers.data(),
  983. strides.data(), offsets.data());
  984. lastVertexBuffer = curVertexBuffer;
  985. lastVertexShader = curVertexShader;
  986. }
  987. void device_load_vertexbuffer(gs_device_t *device, gs_vertbuffer_t *vertbuffer)
  988. {
  989. if (device->curVertexBuffer == vertbuffer)
  990. return;
  991. device->curVertexBuffer = vertbuffer;
  992. }
  993. void device_load_indexbuffer(gs_device_t *device, gs_indexbuffer_t *indexbuffer)
  994. {
  995. DXGI_FORMAT format;
  996. ID3D11Buffer *buffer;
  997. if (device->curIndexBuffer == indexbuffer)
  998. return;
  999. if (indexbuffer) {
  1000. switch (indexbuffer->indexSize) {
  1001. case 2:
  1002. format = DXGI_FORMAT_R16_UINT;
  1003. break;
  1004. default:
  1005. case 4:
  1006. format = DXGI_FORMAT_R32_UINT;
  1007. break;
  1008. }
  1009. buffer = indexbuffer->indexBuffer;
  1010. } else {
  1011. buffer = NULL;
  1012. format = DXGI_FORMAT_R32_UINT;
  1013. }
  1014. device->curIndexBuffer = indexbuffer;
  1015. device->context->IASetIndexBuffer(buffer, format, 0);
  1016. }
  1017. void device_load_texture(gs_device_t *device, gs_texture_t *tex, int unit)
  1018. {
  1019. ID3D11ShaderResourceView *view = NULL;
  1020. if (device->curTextures[unit] == tex)
  1021. return;
  1022. if (tex)
  1023. view = tex->shaderRes;
  1024. device->curTextures[unit] = tex;
  1025. device->context->PSSetShaderResources(unit, 1, &view);
  1026. }
  1027. void device_load_samplerstate(gs_device_t *device,
  1028. gs_samplerstate_t *samplerstate, int unit)
  1029. {
  1030. ID3D11SamplerState *state = NULL;
  1031. if (device->curSamplers[unit] == samplerstate)
  1032. return;
  1033. if (samplerstate)
  1034. state = samplerstate->state;
  1035. device->curSamplers[unit] = samplerstate;
  1036. device->context->PSSetSamplers(unit, 1, &state);
  1037. }
  1038. void device_load_vertexshader(gs_device_t *device, gs_shader_t *vertshader)
  1039. {
  1040. ID3D11VertexShader *shader = NULL;
  1041. ID3D11InputLayout *layout = NULL;
  1042. ID3D11Buffer *constants = NULL;
  1043. if (device->curVertexShader == vertshader)
  1044. return;
  1045. gs_vertex_shader *vs = static_cast<gs_vertex_shader *>(vertshader);
  1046. if (vertshader) {
  1047. if (vertshader->type != GS_SHADER_VERTEX) {
  1048. blog(LOG_ERROR, "device_load_vertexshader (D3D11): "
  1049. "Specified shader is not a vertex "
  1050. "shader");
  1051. return;
  1052. }
  1053. shader = vs->shader;
  1054. layout = vs->layout;
  1055. constants = vs->constants;
  1056. }
  1057. device->curVertexShader = vs;
  1058. device->context->VSSetShader(shader, NULL, 0);
  1059. device->context->IASetInputLayout(layout);
  1060. device->context->VSSetConstantBuffers(0, 1, &constants);
  1061. }
  1062. static inline void clear_textures(gs_device_t *device)
  1063. {
  1064. ID3D11ShaderResourceView *views[GS_MAX_TEXTURES];
  1065. memset(views, 0, sizeof(views));
  1066. memset(device->curTextures, 0, sizeof(device->curTextures));
  1067. device->context->PSSetShaderResources(0, GS_MAX_TEXTURES, views);
  1068. }
  1069. void device_load_pixelshader(gs_device_t *device, gs_shader_t *pixelshader)
  1070. {
  1071. ID3D11PixelShader *shader = NULL;
  1072. ID3D11Buffer *constants = NULL;
  1073. ID3D11SamplerState *states[GS_MAX_TEXTURES];
  1074. if (device->curPixelShader == pixelshader)
  1075. return;
  1076. gs_pixel_shader *ps = static_cast<gs_pixel_shader *>(pixelshader);
  1077. if (pixelshader) {
  1078. if (pixelshader->type != GS_SHADER_PIXEL) {
  1079. blog(LOG_ERROR, "device_load_pixelshader (D3D11): "
  1080. "Specified shader is not a pixel "
  1081. "shader");
  1082. return;
  1083. }
  1084. shader = ps->shader;
  1085. constants = ps->constants;
  1086. ps->GetSamplerStates(states);
  1087. } else {
  1088. memset(states, 0, sizeof(states));
  1089. }
  1090. clear_textures(device);
  1091. device->curPixelShader = ps;
  1092. device->context->PSSetShader(shader, NULL, 0);
  1093. device->context->PSSetConstantBuffers(0, 1, &constants);
  1094. device->context->PSSetSamplers(0, GS_MAX_TEXTURES, states);
  1095. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1096. if (device->curSamplers[i] &&
  1097. device->curSamplers[i]->state != states[i])
  1098. device->curSamplers[i] = nullptr;
  1099. }
  1100. void device_load_default_samplerstate(gs_device_t *device, bool b_3d, int unit)
  1101. {
  1102. /* TODO */
  1103. UNUSED_PARAMETER(device);
  1104. UNUSED_PARAMETER(b_3d);
  1105. UNUSED_PARAMETER(unit);
  1106. }
  1107. gs_shader_t *device_get_vertex_shader(const gs_device_t *device)
  1108. {
  1109. return device->curVertexShader;
  1110. }
  1111. gs_shader_t *device_get_pixel_shader(const gs_device_t *device)
  1112. {
  1113. return device->curPixelShader;
  1114. }
  1115. gs_texture_t *device_get_render_target(const gs_device_t *device)
  1116. {
  1117. if (device->curRenderTarget == &device->curSwapChain->target)
  1118. return NULL;
  1119. return device->curRenderTarget;
  1120. }
  1121. gs_zstencil_t *device_get_zstencil_target(const gs_device_t *device)
  1122. {
  1123. if (device->curZStencilBuffer == &device->curSwapChain->zs)
  1124. return NULL;
  1125. return device->curZStencilBuffer;
  1126. }
  1127. void device_set_render_target(gs_device_t *device, gs_texture_t *tex,
  1128. gs_zstencil_t *zstencil)
  1129. {
  1130. if (device->curSwapChain) {
  1131. if (!tex)
  1132. tex = &device->curSwapChain->target;
  1133. if (!zstencil)
  1134. zstencil = &device->curSwapChain->zs;
  1135. }
  1136. if (device->curRenderTarget == tex &&
  1137. device->curZStencilBuffer == zstencil)
  1138. return;
  1139. if (tex && tex->type != GS_TEXTURE_2D) {
  1140. blog(LOG_ERROR, "device_set_render_target (D3D11): "
  1141. "texture is not a 2D texture");
  1142. return;
  1143. }
  1144. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1145. if (tex2d && !tex2d->renderTarget[0]) {
  1146. blog(LOG_ERROR, "device_set_render_target (D3D11): "
  1147. "texture is not a render target");
  1148. return;
  1149. }
  1150. ID3D11RenderTargetView *rt = tex2d ? tex2d->renderTarget[0].Get()
  1151. : nullptr;
  1152. device->curRenderTarget = tex2d;
  1153. device->curRenderSide = 0;
  1154. device->curZStencilBuffer = zstencil;
  1155. device->context->OMSetRenderTargets(
  1156. 1, &rt, zstencil ? zstencil->view : nullptr);
  1157. }
  1158. void device_set_cube_render_target(gs_device_t *device, gs_texture_t *tex,
  1159. int side, gs_zstencil_t *zstencil)
  1160. {
  1161. if (device->curSwapChain) {
  1162. if (!tex) {
  1163. tex = &device->curSwapChain->target;
  1164. side = 0;
  1165. }
  1166. if (!zstencil)
  1167. zstencil = &device->curSwapChain->zs;
  1168. }
  1169. if (device->curRenderTarget == tex && device->curRenderSide == side &&
  1170. device->curZStencilBuffer == zstencil)
  1171. return;
  1172. if (tex->type != GS_TEXTURE_CUBE) {
  1173. blog(LOG_ERROR, "device_set_cube_render_target (D3D11): "
  1174. "texture is not a cube texture");
  1175. return;
  1176. }
  1177. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1178. if (!tex2d->renderTarget[side]) {
  1179. blog(LOG_ERROR, "device_set_cube_render_target (D3D11): "
  1180. "texture is not a render target");
  1181. return;
  1182. }
  1183. ID3D11RenderTargetView *rt = tex2d->renderTarget[0];
  1184. device->curRenderTarget = tex2d;
  1185. device->curRenderSide = side;
  1186. device->curZStencilBuffer = zstencil;
  1187. device->context->OMSetRenderTargets(1, &rt, zstencil->view);
  1188. }
  1189. inline void gs_device::CopyTex(ID3D11Texture2D *dst, uint32_t dst_x,
  1190. uint32_t dst_y, gs_texture_t *src,
  1191. uint32_t src_x, uint32_t src_y, uint32_t src_w,
  1192. uint32_t src_h)
  1193. {
  1194. if (src->type != GS_TEXTURE_2D)
  1195. throw "Source texture must be a 2D texture";
  1196. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(src);
  1197. if (dst_x == 0 && dst_y == 0 && src_x == 0 && src_y == 0 &&
  1198. src_w == 0 && src_h == 0) {
  1199. context->CopyResource(dst, tex2d->texture);
  1200. } else {
  1201. D3D11_BOX sbox;
  1202. sbox.left = src_x;
  1203. if (src_w > 0)
  1204. sbox.right = src_x + src_w;
  1205. else
  1206. sbox.right = tex2d->width - 1;
  1207. sbox.top = src_y;
  1208. if (src_h > 0)
  1209. sbox.bottom = src_y + src_h;
  1210. else
  1211. sbox.bottom = tex2d->height - 1;
  1212. sbox.front = 0;
  1213. sbox.back = 1;
  1214. context->CopySubresourceRegion(dst, 0, dst_x, dst_y, 0,
  1215. tex2d->texture, 0, &sbox);
  1216. }
  1217. }
  1218. void device_copy_texture_region(gs_device_t *device, gs_texture_t *dst,
  1219. uint32_t dst_x, uint32_t dst_y,
  1220. gs_texture_t *src, uint32_t src_x,
  1221. uint32_t src_y, uint32_t src_w, uint32_t src_h)
  1222. {
  1223. try {
  1224. gs_texture_2d *src2d = static_cast<gs_texture_2d *>(src);
  1225. gs_texture_2d *dst2d = static_cast<gs_texture_2d *>(dst);
  1226. if (!src)
  1227. throw "Source texture is NULL";
  1228. if (!dst)
  1229. throw "Destination texture is NULL";
  1230. if (src->type != GS_TEXTURE_2D || dst->type != GS_TEXTURE_2D)
  1231. throw "Source and destination textures must be a 2D "
  1232. "textures";
  1233. if (dst->format != src->format)
  1234. throw "Source and destination formats do not match";
  1235. /* apparently casting to the same type that the variable
  1236. * already exists as is supposed to prevent some warning
  1237. * when used with the conditional operator? */
  1238. uint32_t copyWidth = (uint32_t)src_w ? (uint32_t)src_w
  1239. : (src2d->width - src_x);
  1240. uint32_t copyHeight = (uint32_t)src_h ? (uint32_t)src_h
  1241. : (src2d->height - src_y);
  1242. uint32_t dstWidth = dst2d->width - dst_x;
  1243. uint32_t dstHeight = dst2d->height - dst_y;
  1244. if (dstWidth < copyWidth || dstHeight < copyHeight)
  1245. throw "Destination texture region is not big "
  1246. "enough to hold the source region";
  1247. if (dst_x == 0 && dst_y == 0 && src_x == 0 && src_y == 0 &&
  1248. src_w == 0 && src_h == 0) {
  1249. copyWidth = 0;
  1250. copyHeight = 0;
  1251. }
  1252. device->CopyTex(dst2d->texture, dst_x, dst_y, src, src_x, src_y,
  1253. copyWidth, copyHeight);
  1254. } catch (const char *error) {
  1255. blog(LOG_ERROR, "device_copy_texture (D3D11): %s", error);
  1256. }
  1257. }
  1258. void device_copy_texture(gs_device_t *device, gs_texture_t *dst,
  1259. gs_texture_t *src)
  1260. {
  1261. device_copy_texture_region(device, dst, 0, 0, src, 0, 0, 0, 0);
  1262. }
  1263. void device_stage_texture(gs_device_t *device, gs_stagesurf_t *dst,
  1264. gs_texture_t *src)
  1265. {
  1266. try {
  1267. gs_texture_2d *src2d = static_cast<gs_texture_2d *>(src);
  1268. if (!src)
  1269. throw "Source texture is NULL";
  1270. if (src->type != GS_TEXTURE_2D)
  1271. throw "Source texture must be a 2D texture";
  1272. if (!dst)
  1273. throw "Destination surface is NULL";
  1274. if (dst->format != GS_UNKNOWN && dst->format != src->format)
  1275. throw "Source and destination formats do not match";
  1276. if (dst->width != src2d->width || dst->height != src2d->height)
  1277. throw "Source and destination must have the same "
  1278. "dimensions";
  1279. device->CopyTex(dst->texture, 0, 0, src, 0, 0, 0, 0);
  1280. } catch (const char *error) {
  1281. blog(LOG_ERROR, "device_copy_texture (D3D11): %s", error);
  1282. }
  1283. }
  1284. void device_begin_scene(gs_device_t *device)
  1285. {
  1286. clear_textures(device);
  1287. }
  1288. void device_draw(gs_device_t *device, enum gs_draw_mode draw_mode,
  1289. uint32_t start_vert, uint32_t num_verts)
  1290. {
  1291. try {
  1292. if (!device->curVertexShader)
  1293. throw "No vertex shader specified";
  1294. if (!device->curPixelShader)
  1295. throw "No pixel shader specified";
  1296. if (!device->curVertexBuffer && (num_verts == 0))
  1297. throw "No vertex buffer specified";
  1298. if (!device->curSwapChain && !device->curRenderTarget)
  1299. throw "No render target or swap chain to render to";
  1300. gs_effect_t *effect = gs_get_effect();
  1301. if (effect)
  1302. gs_effect_update_params(effect);
  1303. device->LoadVertexBufferData();
  1304. device->UpdateBlendState();
  1305. device->UpdateRasterState();
  1306. device->UpdateZStencilState();
  1307. device->UpdateViewProjMatrix();
  1308. device->curVertexShader->UploadParams();
  1309. device->curPixelShader->UploadParams();
  1310. } catch (const char *error) {
  1311. blog(LOG_ERROR, "device_draw (D3D11): %s", error);
  1312. return;
  1313. } catch (HRError error) {
  1314. blog(LOG_ERROR, "device_draw (D3D11): %s (%08lX)", error.str,
  1315. error.hr);
  1316. LogD3D11ErrorDetails(error, device);
  1317. return;
  1318. }
  1319. D3D11_PRIMITIVE_TOPOLOGY newTopology = ConvertGSTopology(draw_mode);
  1320. if (device->curToplogy != newTopology) {
  1321. device->context->IASetPrimitiveTopology(newTopology);
  1322. device->curToplogy = newTopology;
  1323. }
  1324. if (device->curIndexBuffer) {
  1325. if (num_verts == 0)
  1326. num_verts = (uint32_t)device->curIndexBuffer->num;
  1327. device->context->DrawIndexed(num_verts, start_vert, 0);
  1328. } else {
  1329. if (num_verts == 0)
  1330. num_verts = (uint32_t)device->curVertexBuffer->numVerts;
  1331. device->context->Draw(num_verts, start_vert);
  1332. }
  1333. }
  1334. void device_end_scene(gs_device_t *device)
  1335. {
  1336. /* does nothing in D3D11 */
  1337. UNUSED_PARAMETER(device);
  1338. }
  1339. void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swapchain)
  1340. {
  1341. gs_texture_t *target = device->curRenderTarget;
  1342. gs_zstencil_t *zs = device->curZStencilBuffer;
  1343. bool is_cube =
  1344. device->curRenderTarget
  1345. ? (device->curRenderTarget->type == GS_TEXTURE_CUBE)
  1346. : false;
  1347. if (device->curSwapChain) {
  1348. if (target == &device->curSwapChain->target)
  1349. target = NULL;
  1350. if (zs == &device->curSwapChain->zs)
  1351. zs = NULL;
  1352. }
  1353. device->curSwapChain = swapchain;
  1354. if (is_cube)
  1355. device_set_cube_render_target(device, target,
  1356. device->curRenderSide, zs);
  1357. else
  1358. device_set_render_target(device, target, zs);
  1359. }
  1360. void device_clear(gs_device_t *device, uint32_t clear_flags,
  1361. const struct vec4 *color, float depth, uint8_t stencil)
  1362. {
  1363. int side = device->curRenderSide;
  1364. if ((clear_flags & GS_CLEAR_COLOR) != 0 && device->curRenderTarget)
  1365. device->context->ClearRenderTargetView(
  1366. device->curRenderTarget->renderTarget[side],
  1367. color->ptr);
  1368. if (device->curZStencilBuffer) {
  1369. uint32_t flags = 0;
  1370. if ((clear_flags & GS_CLEAR_DEPTH) != 0)
  1371. flags |= D3D11_CLEAR_DEPTH;
  1372. if ((clear_flags & GS_CLEAR_STENCIL) != 0)
  1373. flags |= D3D11_CLEAR_STENCIL;
  1374. if (flags && device->curZStencilBuffer->view)
  1375. device->context->ClearDepthStencilView(
  1376. device->curZStencilBuffer->view, flags, depth,
  1377. stencil);
  1378. }
  1379. }
  1380. void device_present(gs_device_t *device)
  1381. {
  1382. HRESULT hr;
  1383. if (device->curSwapChain) {
  1384. hr = device->curSwapChain->swap->Present(0, 0);
  1385. if (hr == DXGI_ERROR_DEVICE_REMOVED ||
  1386. hr == DXGI_ERROR_DEVICE_RESET) {
  1387. device->RebuildDevice();
  1388. }
  1389. } else {
  1390. blog(LOG_WARNING, "device_present (D3D11): No active swap");
  1391. }
  1392. }
  1393. extern "C" void reset_duplicators(void);
  1394. void device_flush(gs_device_t *device)
  1395. {
  1396. device->context->Flush();
  1397. reset_duplicators();
  1398. }
  1399. void device_set_cull_mode(gs_device_t *device, enum gs_cull_mode mode)
  1400. {
  1401. if (mode == device->rasterState.cullMode)
  1402. return;
  1403. device->rasterState.cullMode = mode;
  1404. device->rasterStateChanged = true;
  1405. }
  1406. enum gs_cull_mode device_get_cull_mode(const gs_device_t *device)
  1407. {
  1408. return device->rasterState.cullMode;
  1409. }
  1410. void device_enable_blending(gs_device_t *device, bool enable)
  1411. {
  1412. if (enable == device->blendState.blendEnabled)
  1413. return;
  1414. device->blendState.blendEnabled = enable;
  1415. device->blendStateChanged = true;
  1416. }
  1417. void device_enable_depth_test(gs_device_t *device, bool enable)
  1418. {
  1419. if (enable == device->zstencilState.depthEnabled)
  1420. return;
  1421. device->zstencilState.depthEnabled = enable;
  1422. device->zstencilStateChanged = true;
  1423. }
  1424. void device_enable_stencil_test(gs_device_t *device, bool enable)
  1425. {
  1426. if (enable == device->zstencilState.stencilEnabled)
  1427. return;
  1428. device->zstencilState.stencilEnabled = enable;
  1429. device->zstencilStateChanged = true;
  1430. }
  1431. void device_enable_stencil_write(gs_device_t *device, bool enable)
  1432. {
  1433. if (enable == device->zstencilState.stencilWriteEnabled)
  1434. return;
  1435. device->zstencilState.stencilWriteEnabled = enable;
  1436. device->zstencilStateChanged = true;
  1437. }
  1438. void device_enable_color(gs_device_t *device, bool red, bool green, bool blue,
  1439. bool alpha)
  1440. {
  1441. if (device->blendState.redEnabled == red &&
  1442. device->blendState.greenEnabled == green &&
  1443. device->blendState.blueEnabled == blue &&
  1444. device->blendState.alphaEnabled == alpha)
  1445. return;
  1446. device->blendState.redEnabled = red;
  1447. device->blendState.greenEnabled = green;
  1448. device->blendState.blueEnabled = blue;
  1449. device->blendState.alphaEnabled = alpha;
  1450. device->blendStateChanged = true;
  1451. }
  1452. void device_blend_function(gs_device_t *device, enum gs_blend_type src,
  1453. enum gs_blend_type dest)
  1454. {
  1455. if (device->blendState.srcFactorC == src &&
  1456. device->blendState.destFactorC == dest &&
  1457. device->blendState.srcFactorA == src &&
  1458. device->blendState.destFactorA == dest)
  1459. return;
  1460. device->blendState.srcFactorC = src;
  1461. device->blendState.destFactorC = dest;
  1462. device->blendState.srcFactorA = src;
  1463. device->blendState.destFactorA = dest;
  1464. device->blendStateChanged = true;
  1465. }
  1466. void device_blend_function_separate(gs_device_t *device,
  1467. enum gs_blend_type src_c,
  1468. enum gs_blend_type dest_c,
  1469. enum gs_blend_type src_a,
  1470. enum gs_blend_type dest_a)
  1471. {
  1472. if (device->blendState.srcFactorC == src_c &&
  1473. device->blendState.destFactorC == dest_c &&
  1474. device->blendState.srcFactorA == src_a &&
  1475. device->blendState.destFactorA == dest_a)
  1476. return;
  1477. device->blendState.srcFactorC = src_c;
  1478. device->blendState.destFactorC = dest_c;
  1479. device->blendState.srcFactorA = src_a;
  1480. device->blendState.destFactorA = dest_a;
  1481. device->blendStateChanged = true;
  1482. }
  1483. void device_depth_function(gs_device_t *device, enum gs_depth_test test)
  1484. {
  1485. if (device->zstencilState.depthFunc == test)
  1486. return;
  1487. device->zstencilState.depthFunc = test;
  1488. device->zstencilStateChanged = true;
  1489. }
  1490. static inline void update_stencilside_test(gs_device_t *device,
  1491. StencilSide &side,
  1492. gs_depth_test test)
  1493. {
  1494. if (side.test == test)
  1495. return;
  1496. side.test = test;
  1497. device->zstencilStateChanged = true;
  1498. }
  1499. void device_stencil_function(gs_device_t *device, enum gs_stencil_side side,
  1500. enum gs_depth_test test)
  1501. {
  1502. int sideVal = (int)side;
  1503. if (sideVal & GS_STENCIL_FRONT)
  1504. update_stencilside_test(
  1505. device, device->zstencilState.stencilFront, test);
  1506. if (sideVal & GS_STENCIL_BACK)
  1507. update_stencilside_test(
  1508. device, device->zstencilState.stencilBack, test);
  1509. }
  1510. static inline void update_stencilside_op(gs_device_t *device, StencilSide &side,
  1511. enum gs_stencil_op_type fail,
  1512. enum gs_stencil_op_type zfail,
  1513. enum gs_stencil_op_type zpass)
  1514. {
  1515. if (side.fail == fail && side.zfail == zfail && side.zpass == zpass)
  1516. return;
  1517. side.fail = fail;
  1518. side.zfail = zfail;
  1519. side.zpass = zpass;
  1520. device->zstencilStateChanged = true;
  1521. }
  1522. void device_stencil_op(gs_device_t *device, enum gs_stencil_side side,
  1523. enum gs_stencil_op_type fail,
  1524. enum gs_stencil_op_type zfail,
  1525. enum gs_stencil_op_type zpass)
  1526. {
  1527. int sideVal = (int)side;
  1528. if (sideVal & GS_STENCIL_FRONT)
  1529. update_stencilside_op(device,
  1530. device->zstencilState.stencilFront, fail,
  1531. zfail, zpass);
  1532. if (sideVal & GS_STENCIL_BACK)
  1533. update_stencilside_op(device, device->zstencilState.stencilBack,
  1534. fail, zfail, zpass);
  1535. }
  1536. void device_set_viewport(gs_device_t *device, int x, int y, int width,
  1537. int height)
  1538. {
  1539. D3D11_VIEWPORT vp;
  1540. memset(&vp, 0, sizeof(vp));
  1541. vp.MaxDepth = 1.0f;
  1542. vp.TopLeftX = (float)x;
  1543. vp.TopLeftY = (float)y;
  1544. vp.Width = (float)width;
  1545. vp.Height = (float)height;
  1546. device->context->RSSetViewports(1, &vp);
  1547. device->viewport.x = x;
  1548. device->viewport.y = y;
  1549. device->viewport.cx = width;
  1550. device->viewport.cy = height;
  1551. }
  1552. void device_get_viewport(const gs_device_t *device, struct gs_rect *rect)
  1553. {
  1554. memcpy(rect, &device->viewport, sizeof(gs_rect));
  1555. }
  1556. void device_set_scissor_rect(gs_device_t *device, const struct gs_rect *rect)
  1557. {
  1558. D3D11_RECT d3drect;
  1559. device->rasterState.scissorEnabled = (rect != NULL);
  1560. if (rect != NULL) {
  1561. d3drect.left = rect->x;
  1562. d3drect.top = rect->y;
  1563. d3drect.right = rect->x + rect->cx;
  1564. d3drect.bottom = rect->y + rect->cy;
  1565. device->context->RSSetScissorRects(1, &d3drect);
  1566. }
  1567. device->rasterStateChanged = true;
  1568. }
  1569. void device_ortho(gs_device_t *device, float left, float right, float top,
  1570. float bottom, float zNear, float zFar)
  1571. {
  1572. matrix4 *dst = &device->curProjMatrix;
  1573. float rml = right - left;
  1574. float bmt = bottom - top;
  1575. float fmn = zFar - zNear;
  1576. vec4_zero(&dst->x);
  1577. vec4_zero(&dst->y);
  1578. vec4_zero(&dst->z);
  1579. vec4_zero(&dst->t);
  1580. dst->x.x = 2.0f / rml;
  1581. dst->t.x = (left + right) / -rml;
  1582. dst->y.y = 2.0f / -bmt;
  1583. dst->t.y = (bottom + top) / bmt;
  1584. dst->z.z = 1.0f / fmn;
  1585. dst->t.z = zNear / -fmn;
  1586. dst->t.w = 1.0f;
  1587. }
  1588. void device_frustum(gs_device_t *device, float left, float right, float top,
  1589. float bottom, float zNear, float zFar)
  1590. {
  1591. matrix4 *dst = &device->curProjMatrix;
  1592. float rml = right - left;
  1593. float bmt = bottom - top;
  1594. float fmn = zFar - zNear;
  1595. float nearx2 = 2.0f * zNear;
  1596. vec4_zero(&dst->x);
  1597. vec4_zero(&dst->y);
  1598. vec4_zero(&dst->z);
  1599. vec4_zero(&dst->t);
  1600. dst->x.x = nearx2 / rml;
  1601. dst->z.x = (left + right) / -rml;
  1602. dst->y.y = nearx2 / -bmt;
  1603. dst->z.y = (bottom + top) / bmt;
  1604. dst->z.z = zFar / fmn;
  1605. dst->t.z = (zNear * zFar) / -fmn;
  1606. dst->z.w = 1.0f;
  1607. }
  1608. void device_projection_push(gs_device_t *device)
  1609. {
  1610. mat4float mat;
  1611. memcpy(&mat, &device->curProjMatrix, sizeof(matrix4));
  1612. device->projStack.push_back(mat);
  1613. }
  1614. void device_projection_pop(gs_device_t *device)
  1615. {
  1616. if (device->projStack.empty())
  1617. return;
  1618. const mat4float &mat = device->projStack.back();
  1619. memcpy(&device->curProjMatrix, &mat, sizeof(matrix4));
  1620. device->projStack.pop_back();
  1621. }
  1622. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1623. {
  1624. if (swapchain->device->curSwapChain == swapchain)
  1625. device_load_swapchain(swapchain->device, nullptr);
  1626. delete swapchain;
  1627. }
  1628. void gs_texture_destroy(gs_texture_t *tex)
  1629. {
  1630. delete tex;
  1631. }
  1632. uint32_t gs_texture_get_width(const gs_texture_t *tex)
  1633. {
  1634. if (tex->type != GS_TEXTURE_2D)
  1635. return 0;
  1636. return static_cast<const gs_texture_2d *>(tex)->width;
  1637. }
  1638. uint32_t gs_texture_get_height(const gs_texture_t *tex)
  1639. {
  1640. if (tex->type != GS_TEXTURE_2D)
  1641. return 0;
  1642. return static_cast<const gs_texture_2d *>(tex)->height;
  1643. }
  1644. enum gs_color_format gs_texture_get_color_format(const gs_texture_t *tex)
  1645. {
  1646. if (tex->type != GS_TEXTURE_2D)
  1647. return GS_UNKNOWN;
  1648. return static_cast<const gs_texture_2d *>(tex)->format;
  1649. }
  1650. bool gs_texture_map(gs_texture_t *tex, uint8_t **ptr, uint32_t *linesize)
  1651. {
  1652. HRESULT hr;
  1653. if (tex->type != GS_TEXTURE_2D)
  1654. return false;
  1655. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1656. D3D11_MAPPED_SUBRESOURCE map;
  1657. hr = tex2d->device->context->Map(tex2d->texture, 0,
  1658. D3D11_MAP_WRITE_DISCARD, 0, &map);
  1659. if (FAILED(hr))
  1660. return false;
  1661. *ptr = (uint8_t *)map.pData;
  1662. *linesize = map.RowPitch;
  1663. return true;
  1664. }
  1665. void gs_texture_unmap(gs_texture_t *tex)
  1666. {
  1667. if (tex->type != GS_TEXTURE_2D)
  1668. return;
  1669. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1670. tex2d->device->context->Unmap(tex2d->texture, 0);
  1671. }
  1672. void *gs_texture_get_obj(gs_texture_t *tex)
  1673. {
  1674. if (tex->type != GS_TEXTURE_2D)
  1675. return nullptr;
  1676. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1677. return tex2d->texture.Get();
  1678. }
  1679. void gs_cubetexture_destroy(gs_texture_t *cubetex)
  1680. {
  1681. delete cubetex;
  1682. }
  1683. uint32_t gs_cubetexture_get_size(const gs_texture_t *cubetex)
  1684. {
  1685. if (cubetex->type != GS_TEXTURE_CUBE)
  1686. return 0;
  1687. const gs_texture_2d *tex = static_cast<const gs_texture_2d *>(cubetex);
  1688. return tex->width;
  1689. }
  1690. enum gs_color_format
  1691. gs_cubetexture_get_color_format(const gs_texture_t *cubetex)
  1692. {
  1693. if (cubetex->type != GS_TEXTURE_CUBE)
  1694. return GS_UNKNOWN;
  1695. const gs_texture_2d *tex = static_cast<const gs_texture_2d *>(cubetex);
  1696. return tex->format;
  1697. }
  1698. void gs_voltexture_destroy(gs_texture_t *voltex)
  1699. {
  1700. delete voltex;
  1701. }
  1702. uint32_t gs_voltexture_get_width(const gs_texture_t *voltex)
  1703. {
  1704. /* TODO */
  1705. UNUSED_PARAMETER(voltex);
  1706. return 0;
  1707. }
  1708. uint32_t gs_voltexture_get_height(const gs_texture_t *voltex)
  1709. {
  1710. /* TODO */
  1711. UNUSED_PARAMETER(voltex);
  1712. return 0;
  1713. }
  1714. uint32_t gs_voltexture_get_depth(const gs_texture_t *voltex)
  1715. {
  1716. /* TODO */
  1717. UNUSED_PARAMETER(voltex);
  1718. return 0;
  1719. }
  1720. enum gs_color_format gs_voltexture_get_color_format(const gs_texture_t *voltex)
  1721. {
  1722. /* TODO */
  1723. UNUSED_PARAMETER(voltex);
  1724. return GS_UNKNOWN;
  1725. }
  1726. void gs_stagesurface_destroy(gs_stagesurf_t *stagesurf)
  1727. {
  1728. delete stagesurf;
  1729. }
  1730. uint32_t gs_stagesurface_get_width(const gs_stagesurf_t *stagesurf)
  1731. {
  1732. return stagesurf->width;
  1733. }
  1734. uint32_t gs_stagesurface_get_height(const gs_stagesurf_t *stagesurf)
  1735. {
  1736. return stagesurf->height;
  1737. }
  1738. enum gs_color_format
  1739. gs_stagesurface_get_color_format(const gs_stagesurf_t *stagesurf)
  1740. {
  1741. return stagesurf->format;
  1742. }
  1743. bool gs_stagesurface_map(gs_stagesurf_t *stagesurf, uint8_t **data,
  1744. uint32_t *linesize)
  1745. {
  1746. D3D11_MAPPED_SUBRESOURCE map;
  1747. if (FAILED(stagesurf->device->context->Map(stagesurf->texture, 0,
  1748. D3D11_MAP_READ, 0, &map)))
  1749. return false;
  1750. *data = (uint8_t *)map.pData;
  1751. *linesize = map.RowPitch;
  1752. return true;
  1753. }
  1754. void gs_stagesurface_unmap(gs_stagesurf_t *stagesurf)
  1755. {
  1756. stagesurf->device->context->Unmap(stagesurf->texture, 0);
  1757. }
  1758. void gs_zstencil_destroy(gs_zstencil_t *zstencil)
  1759. {
  1760. delete zstencil;
  1761. }
  1762. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1763. {
  1764. if (!samplerstate)
  1765. return;
  1766. if (samplerstate->device)
  1767. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1768. if (samplerstate->device->curSamplers[i] ==
  1769. samplerstate)
  1770. samplerstate->device->curSamplers[i] = nullptr;
  1771. delete samplerstate;
  1772. }
  1773. void gs_vertexbuffer_destroy(gs_vertbuffer_t *vertbuffer)
  1774. {
  1775. if (vertbuffer && vertbuffer->device->lastVertexBuffer == vertbuffer)
  1776. vertbuffer->device->lastVertexBuffer = nullptr;
  1777. delete vertbuffer;
  1778. }
  1779. static inline void gs_vertexbuffer_flush_internal(gs_vertbuffer_t *vertbuffer,
  1780. const gs_vb_data *data)
  1781. {
  1782. size_t num_tex = data->num_tex < vertbuffer->uvBuffers.size()
  1783. ? data->num_tex
  1784. : vertbuffer->uvBuffers.size();
  1785. if (!vertbuffer->dynamic) {
  1786. blog(LOG_ERROR, "gs_vertexbuffer_flush: vertex buffer is "
  1787. "not dynamic");
  1788. return;
  1789. }
  1790. if (data->points)
  1791. vertbuffer->FlushBuffer(vertbuffer->vertexBuffer, data->points,
  1792. sizeof(vec3));
  1793. if (vertbuffer->normalBuffer && data->normals)
  1794. vertbuffer->FlushBuffer(vertbuffer->normalBuffer, data->normals,
  1795. sizeof(vec3));
  1796. if (vertbuffer->tangentBuffer && data->tangents)
  1797. vertbuffer->FlushBuffer(vertbuffer->tangentBuffer,
  1798. data->tangents, sizeof(vec3));
  1799. if (vertbuffer->colorBuffer && data->colors)
  1800. vertbuffer->FlushBuffer(vertbuffer->colorBuffer, data->colors,
  1801. sizeof(uint32_t));
  1802. for (size_t i = 0; i < num_tex; i++) {
  1803. gs_tvertarray &tv = data->tvarray[i];
  1804. vertbuffer->FlushBuffer(vertbuffer->uvBuffers[i], tv.array,
  1805. tv.width * sizeof(float));
  1806. }
  1807. }
  1808. void gs_vertexbuffer_flush(gs_vertbuffer_t *vertbuffer)
  1809. {
  1810. gs_vertexbuffer_flush_internal(vertbuffer, vertbuffer->vbd.data);
  1811. }
  1812. void gs_vertexbuffer_flush_direct(gs_vertbuffer_t *vertbuffer,
  1813. const gs_vb_data *data)
  1814. {
  1815. gs_vertexbuffer_flush_internal(vertbuffer, data);
  1816. }
  1817. struct gs_vb_data *gs_vertexbuffer_get_data(const gs_vertbuffer_t *vertbuffer)
  1818. {
  1819. return vertbuffer->vbd.data;
  1820. }
  1821. void gs_indexbuffer_destroy(gs_indexbuffer_t *indexbuffer)
  1822. {
  1823. delete indexbuffer;
  1824. }
  1825. static inline void gs_indexbuffer_flush_internal(gs_indexbuffer_t *indexbuffer,
  1826. const void *data)
  1827. {
  1828. HRESULT hr;
  1829. if (!indexbuffer->dynamic)
  1830. return;
  1831. D3D11_MAPPED_SUBRESOURCE map;
  1832. hr = indexbuffer->device->context->Map(
  1833. indexbuffer->indexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
  1834. if (FAILED(hr))
  1835. return;
  1836. memcpy(map.pData, data, indexbuffer->num * indexbuffer->indexSize);
  1837. indexbuffer->device->context->Unmap(indexbuffer->indexBuffer, 0);
  1838. }
  1839. void gs_indexbuffer_flush(gs_indexbuffer_t *indexbuffer)
  1840. {
  1841. gs_indexbuffer_flush_internal(indexbuffer, indexbuffer->indices.data);
  1842. }
  1843. void gs_indexbuffer_flush_direct(gs_indexbuffer_t *indexbuffer,
  1844. const void *data)
  1845. {
  1846. gs_indexbuffer_flush_internal(indexbuffer, data);
  1847. }
  1848. void *gs_indexbuffer_get_data(const gs_indexbuffer_t *indexbuffer)
  1849. {
  1850. return indexbuffer->indices.data;
  1851. }
  1852. size_t gs_indexbuffer_get_num_indices(const gs_indexbuffer_t *indexbuffer)
  1853. {
  1854. return indexbuffer->num;
  1855. }
  1856. enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *indexbuffer)
  1857. {
  1858. return indexbuffer->type;
  1859. }
  1860. void gs_timer_destroy(gs_timer_t *timer)
  1861. {
  1862. delete timer;
  1863. }
  1864. void gs_timer_begin(gs_timer_t *timer)
  1865. {
  1866. timer->device->context->End(timer->query_begin);
  1867. }
  1868. void gs_timer_end(gs_timer_t *timer)
  1869. {
  1870. timer->device->context->End(timer->query_end);
  1871. }
  1872. bool gs_timer_get_data(gs_timer_t *timer, uint64_t *ticks)
  1873. {
  1874. uint64_t begin, end;
  1875. HRESULT hr_begin, hr_end;
  1876. do {
  1877. hr_begin = timer->device->context->GetData(
  1878. timer->query_begin, &begin, sizeof(begin), 0);
  1879. } while (hr_begin == S_FALSE);
  1880. do {
  1881. hr_end = timer->device->context->GetData(timer->query_end, &end,
  1882. sizeof(end), 0);
  1883. } while (hr_end == S_FALSE);
  1884. const bool succeeded = SUCCEEDED(hr_begin) && SUCCEEDED(hr_end);
  1885. if (succeeded)
  1886. *ticks = end - begin;
  1887. return succeeded;
  1888. }
  1889. void gs_timer_range_destroy(gs_timer_range_t *range)
  1890. {
  1891. delete range;
  1892. }
  1893. void gs_timer_range_begin(gs_timer_range_t *range)
  1894. {
  1895. range->device->context->Begin(range->query_disjoint);
  1896. }
  1897. void gs_timer_range_end(gs_timer_range_t *range)
  1898. {
  1899. range->device->context->End(range->query_disjoint);
  1900. }
  1901. bool gs_timer_range_get_data(gs_timer_range_t *range, bool *disjoint,
  1902. uint64_t *frequency)
  1903. {
  1904. D3D11_QUERY_DATA_TIMESTAMP_DISJOINT timestamp_disjoint;
  1905. HRESULT hr;
  1906. do {
  1907. hr = range->device->context->GetData(range->query_disjoint,
  1908. &timestamp_disjoint,
  1909. sizeof(timestamp_disjoint),
  1910. 0);
  1911. } while (hr == S_FALSE);
  1912. const bool succeeded = SUCCEEDED(hr);
  1913. if (succeeded) {
  1914. *disjoint = timestamp_disjoint.Disjoint;
  1915. *frequency = timestamp_disjoint.Frequency;
  1916. }
  1917. return succeeded;
  1918. }
  1919. gs_timer::gs_timer(gs_device_t *device) : gs_obj(device, gs_type::gs_timer)
  1920. {
  1921. Rebuild(device->device);
  1922. }
  1923. gs_timer_range::gs_timer_range(gs_device_t *device)
  1924. : gs_obj(device, gs_type::gs_timer_range)
  1925. {
  1926. Rebuild(device->device);
  1927. }
  1928. extern "C" EXPORT bool device_gdi_texture_available(void)
  1929. {
  1930. return true;
  1931. }
  1932. extern "C" EXPORT bool device_shared_texture_available(void)
  1933. {
  1934. return true;
  1935. }
  1936. extern "C" EXPORT bool device_nv12_available(gs_device_t *device)
  1937. {
  1938. return device->nv12Supported;
  1939. }
  1940. extern "C" EXPORT void device_debug_marker_begin(gs_device_t *,
  1941. const char *markername,
  1942. const float color[4])
  1943. {
  1944. D3DCOLOR bgra = D3DCOLOR_ARGB((DWORD)(255.0f * color[3]),
  1945. (DWORD)(255.0f * color[0]),
  1946. (DWORD)(255.0f * color[1]),
  1947. (DWORD)(255.0f * color[2]));
  1948. wchar_t wide[64];
  1949. os_utf8_to_wcs(markername, 0, wide, _countof(wide));
  1950. D3DPERF_BeginEvent(bgra, wide);
  1951. }
  1952. extern "C" EXPORT void device_debug_marker_end(gs_device_t *)
  1953. {
  1954. D3DPERF_EndEvent();
  1955. }
  1956. extern "C" EXPORT gs_texture_t *
  1957. device_texture_create_gdi(gs_device_t *device, uint32_t width, uint32_t height)
  1958. {
  1959. gs_texture *texture = nullptr;
  1960. try {
  1961. texture = new gs_texture_2d(device, width, height, GS_BGRA, 1,
  1962. nullptr, GS_RENDER_TARGET,
  1963. GS_TEXTURE_2D, true);
  1964. } catch (HRError error) {
  1965. blog(LOG_ERROR, "device_texture_create_gdi (D3D11): %s (%08lX)",
  1966. error.str, error.hr);
  1967. LogD3D11ErrorDetails(error, device);
  1968. } catch (const char *error) {
  1969. blog(LOG_ERROR, "device_texture_create_gdi (D3D11): %s", error);
  1970. }
  1971. return texture;
  1972. }
  1973. static inline bool TextureGDICompatible(gs_texture_2d *tex2d, const char *func)
  1974. {
  1975. if (!tex2d->isGDICompatible) {
  1976. blog(LOG_ERROR, "%s (D3D11): Texture is not GDI compatible",
  1977. func);
  1978. return false;
  1979. }
  1980. return true;
  1981. }
  1982. extern "C" EXPORT void *gs_texture_get_dc(gs_texture_t *tex)
  1983. {
  1984. HDC hDC = nullptr;
  1985. if (tex->type != GS_TEXTURE_2D)
  1986. return nullptr;
  1987. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  1988. if (!TextureGDICompatible(tex2d, "gs_texture_get_dc"))
  1989. return nullptr;
  1990. if (!tex2d->gdiSurface)
  1991. return nullptr;
  1992. tex2d->gdiSurface->GetDC(true, &hDC);
  1993. return hDC;
  1994. }
  1995. extern "C" EXPORT void gs_texture_release_dc(gs_texture_t *tex)
  1996. {
  1997. if (tex->type != GS_TEXTURE_2D)
  1998. return;
  1999. gs_texture_2d *tex2d = static_cast<gs_texture_2d *>(tex);
  2000. if (!TextureGDICompatible(tex2d, "gs_texture_release_dc"))
  2001. return;
  2002. tex2d->gdiSurface->ReleaseDC(nullptr);
  2003. }
  2004. extern "C" EXPORT gs_texture_t *device_texture_open_shared(gs_device_t *device,
  2005. uint32_t handle)
  2006. {
  2007. gs_texture *texture = nullptr;
  2008. try {
  2009. texture = new gs_texture_2d(device, handle);
  2010. } catch (HRError error) {
  2011. blog(LOG_ERROR, "gs_texture_open_shared (D3D11): %s (%08lX)",
  2012. error.str, error.hr);
  2013. LogD3D11ErrorDetails(error, device);
  2014. } catch (const char *error) {
  2015. blog(LOG_ERROR, "gs_texture_open_shared (D3D11): %s", error);
  2016. }
  2017. return texture;
  2018. }
  2019. extern "C" EXPORT uint32_t device_texture_get_shared_handle(gs_texture_t *tex)
  2020. {
  2021. gs_texture_2d *tex2d = reinterpret_cast<gs_texture_2d *>(tex);
  2022. if (tex->type != GS_TEXTURE_2D)
  2023. return GS_INVALID_HANDLE;
  2024. return tex2d->isShared ? tex2d->sharedHandle : GS_INVALID_HANDLE;
  2025. }
  2026. int device_texture_acquire_sync(gs_texture_t *tex, uint64_t key, uint32_t ms)
  2027. {
  2028. gs_texture_2d *tex2d = reinterpret_cast<gs_texture_2d *>(tex);
  2029. if (tex->type != GS_TEXTURE_2D)
  2030. return -1;
  2031. if (tex2d->acquired)
  2032. return 0;
  2033. ComQIPtr<IDXGIKeyedMutex> keyedMutex(tex2d->texture);
  2034. if (!keyedMutex)
  2035. return -1;
  2036. HRESULT hr = keyedMutex->AcquireSync(key, ms);
  2037. if (hr == S_OK) {
  2038. tex2d->acquired = true;
  2039. return 0;
  2040. } else if (hr == WAIT_TIMEOUT) {
  2041. return ETIMEDOUT;
  2042. }
  2043. return -1;
  2044. }
  2045. extern "C" EXPORT int device_texture_release_sync(gs_texture_t *tex,
  2046. uint64_t key)
  2047. {
  2048. gs_texture_2d *tex2d = reinterpret_cast<gs_texture_2d *>(tex);
  2049. if (tex->type != GS_TEXTURE_2D)
  2050. return -1;
  2051. if (!tex2d->acquired)
  2052. return 0;
  2053. ComQIPtr<IDXGIKeyedMutex> keyedMutex(tex2d->texture);
  2054. if (!keyedMutex)
  2055. return -1;
  2056. HRESULT hr = keyedMutex->ReleaseSync(key);
  2057. if (hr == S_OK) {
  2058. tex2d->acquired = false;
  2059. return 0;
  2060. }
  2061. return -1;
  2062. }
  2063. extern "C" EXPORT bool
  2064. device_texture_create_nv12(gs_device_t *device, gs_texture_t **p_tex_y,
  2065. gs_texture_t **p_tex_uv, uint32_t width,
  2066. uint32_t height, uint32_t flags)
  2067. {
  2068. if (!device->nv12Supported)
  2069. return false;
  2070. *p_tex_y = nullptr;
  2071. *p_tex_uv = nullptr;
  2072. gs_texture_2d *tex_y;
  2073. gs_texture_2d *tex_uv;
  2074. try {
  2075. tex_y = new gs_texture_2d(device, width, height, GS_R8, 1,
  2076. nullptr, flags, GS_TEXTURE_2D, false,
  2077. true);
  2078. tex_uv = new gs_texture_2d(device, tex_y->texture, flags);
  2079. } catch (HRError error) {
  2080. blog(LOG_ERROR, "gs_texture_create_nv12 (D3D11): %s (%08lX)",
  2081. error.str, error.hr);
  2082. LogD3D11ErrorDetails(error, device);
  2083. return false;
  2084. } catch (const char *error) {
  2085. blog(LOG_ERROR, "gs_texture_create_nv12 (D3D11): %s", error);
  2086. return false;
  2087. }
  2088. tex_y->pairedNV12texture = tex_uv;
  2089. tex_uv->pairedNV12texture = tex_y;
  2090. *p_tex_y = tex_y;
  2091. *p_tex_uv = tex_uv;
  2092. return true;
  2093. }
  2094. extern "C" EXPORT gs_stagesurf_t *
  2095. device_stagesurface_create_nv12(gs_device_t *device, uint32_t width,
  2096. uint32_t height)
  2097. {
  2098. gs_stage_surface *surf = NULL;
  2099. try {
  2100. surf = new gs_stage_surface(device, width, height);
  2101. } catch (HRError error) {
  2102. blog(LOG_ERROR,
  2103. "device_stagesurface_create (D3D11): %s "
  2104. "(%08lX)",
  2105. error.str, error.hr);
  2106. LogD3D11ErrorDetails(error, device);
  2107. }
  2108. return surf;
  2109. }