d3d11-subsystem.cpp 52 KB

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