d3d11-subsystem.cpp 56 KB

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