d3d11-subsystem.cpp 51 KB

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