d3d11-subsystem.cpp 48 KB

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