d3d11-subsystem.cpp 62 KB

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