d3d11-subsystem.cpp 47 KB

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