d3d11-subsystem.cpp 50 KB

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