BsD3D11RenderAPI.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsD3D11RenderAPI.h"
  4. #include "BsD3D11DriverList.h"
  5. #include "BsD3D11Driver.h"
  6. #include "BsD3D11Device.h"
  7. #include "BsD3D11TextureManager.h"
  8. #include "BsD3D11Texture.h"
  9. #include "BsD3D11HardwareBufferManager.h"
  10. #include "BsD3D11RenderWindowManager.h"
  11. #include "BsD3D11HLSLProgramFactory.h"
  12. #include "BsD3D11BlendState.h"
  13. #include "BsD3D11RasterizerState.h"
  14. #include "BsD3D11DepthStencilState.h"
  15. #include "BsD3D11SamplerState.h"
  16. #include "BsD3D11GpuProgram.h"
  17. #include "BsD3D11Mappings.h"
  18. #include "BsD3D11VertexBuffer.h"
  19. #include "BsD3D11IndexBuffer.h"
  20. #include "BsD3D11RenderStateManager.h"
  21. #include "BsD3D11GpuParamBlockBuffer.h"
  22. #include "BsD3D11InputLayoutManager.h"
  23. #include "BsD3D11TextureView.h"
  24. #include "BsD3D11RenderUtility.h"
  25. #include "BsGpuParams.h"
  26. #include "BsCoreThread.h"
  27. #include "BsD3D11QueryManager.h"
  28. #include "BsDebug.h"
  29. #include "BsException.h"
  30. #include "BsRenderStats.h"
  31. #include "BsGpuParamDesc.h"
  32. #include "BsD3D11GpuBuffer.h"
  33. #include "BsD3D11CommandBuffer.h"
  34. namespace BansheeEngine
  35. {
  36. D3D11RenderAPI::D3D11RenderAPI()
  37. : mDXGIFactory(nullptr), mDevice(nullptr), mDriverList(nullptr), mActiveD3DDriver(nullptr)
  38. , mFeatureLevel(D3D_FEATURE_LEVEL_11_0), mHLSLFactory(nullptr), mIAManager(nullptr), mStencilRef(0)
  39. , mActiveDrawOp(DOT_TRIANGLE_LIST), mViewportNorm(0.0f, 0.0f, 1.0f, 1.0f)
  40. { }
  41. D3D11RenderAPI::~D3D11RenderAPI()
  42. {
  43. }
  44. const StringID& D3D11RenderAPI::getName() const
  45. {
  46. static StringID strName("D3D11RenderAPI");
  47. return strName;
  48. }
  49. const String& D3D11RenderAPI::getShadingLanguageName() const
  50. {
  51. static String strName("hlsl");
  52. return strName;
  53. }
  54. void D3D11RenderAPI::initialize()
  55. {
  56. THROW_IF_NOT_CORE_THREAD;
  57. HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&mDXGIFactory);
  58. if(FAILED(hr))
  59. BS_EXCEPT(RenderingAPIException, "Failed to create Direct3D11 DXGIFactory");
  60. mDriverList = bs_new<D3D11DriverList>(mDXGIFactory);
  61. mActiveD3DDriver = mDriverList->item(0); // TODO: Always get first driver, for now
  62. mVideoModeInfo = mActiveD3DDriver->getVideoModeInfo();
  63. IDXGIAdapter* selectedAdapter = mActiveD3DDriver->getDeviceAdapter();
  64. D3D_FEATURE_LEVEL requestedLevels[] = {
  65. D3D_FEATURE_LEVEL_11_0,
  66. D3D_FEATURE_LEVEL_10_1,
  67. D3D_FEATURE_LEVEL_10_0,
  68. D3D_FEATURE_LEVEL_9_3,
  69. D3D_FEATURE_LEVEL_9_2,
  70. D3D_FEATURE_LEVEL_9_1
  71. };
  72. UINT32 numRequestedLevel = sizeof(requestedLevels) / sizeof(requestedLevels[0]);
  73. UINT32 deviceFlags = 0;
  74. #if BS_DEBUG_MODE
  75. deviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
  76. #endif
  77. ID3D11Device* device;
  78. hr = D3D11CreateDevice(selectedAdapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, deviceFlags,
  79. requestedLevels, numRequestedLevel, D3D11_SDK_VERSION, &device, &mFeatureLevel, nullptr);
  80. if(FAILED(hr))
  81. BS_EXCEPT(RenderingAPIException, "Failed to create Direct3D11 object. D3D11CreateDeviceN returned this error code: " + toString(hr));
  82. mDevice = bs_new<D3D11Device>(device);
  83. // Create the texture manager for use by others
  84. TextureManager::startUp<D3D11TextureManager>();
  85. TextureCoreManager::startUp<D3D11TextureCoreManager>();
  86. // Create hardware buffer manager
  87. HardwareBufferManager::startUp();
  88. HardwareBufferCoreManager::startUp<D3D11HardwareBufferCoreManager>(std::ref(*mDevice));
  89. // Create render window manager
  90. RenderWindowManager::startUp<D3D11RenderWindowManager>(this);
  91. RenderWindowCoreManager::startUp<D3D11RenderWindowCoreManager>(this);
  92. // Create & register HLSL factory
  93. mHLSLFactory = bs_new<D3D11HLSLProgramFactory>();
  94. // Create render state manager
  95. RenderStateCoreManager::startUp<D3D11RenderStateCoreManager>();
  96. mNumDevices = 1;
  97. mCurrentCapabilities = bs_newN<RenderAPICapabilities>(mNumDevices);
  98. initCapabilites(selectedAdapter, mCurrentCapabilities[0]);
  99. GpuProgramCoreManager::instance().addFactory(mHLSLFactory);
  100. mIAManager = bs_new<D3D11InputLayoutManager>();
  101. RenderAPICore::initialize();
  102. }
  103. void D3D11RenderAPI::initializeWithWindow(const SPtr<RenderWindowCore>& primaryWindow)
  104. {
  105. D3D11RenderUtility::startUp(mDevice);
  106. QueryManager::startUp<D3D11QueryManager>();
  107. RenderAPICore::initializeWithWindow(primaryWindow);
  108. }
  109. void D3D11RenderAPI::destroyCore()
  110. {
  111. THROW_IF_NOT_CORE_THREAD;
  112. for (auto& boundUAV : mBoundUAVs)
  113. {
  114. if (boundUAV.second != nullptr)
  115. boundUAV.first->releaseView(boundUAV.second);
  116. boundUAV.second = nullptr;
  117. boundUAV.first = nullptr;
  118. }
  119. // Ensure that all GPU commands finish executing before shutting down the device. If we don't do this a crash
  120. // on shutdown may occurr as the driver is still executing the commands, and we unload this library.
  121. mDevice->getImmediateContext()->Flush();
  122. SPtr<EventQuery> query = EventQuery::create();
  123. query->begin();
  124. while(!query->isReady())
  125. {
  126. // Spin
  127. }
  128. query = nullptr;
  129. QueryManager::shutDown();
  130. D3D11RenderUtility::shutDown();
  131. if(mIAManager != nullptr)
  132. {
  133. bs_delete(mIAManager);
  134. mIAManager = nullptr;
  135. }
  136. if(mHLSLFactory != nullptr)
  137. {
  138. bs_delete(mHLSLFactory);
  139. mHLSLFactory = nullptr;
  140. }
  141. mActiveVertexDeclaration = nullptr;
  142. mActiveVertexShader = nullptr;
  143. mActiveRenderTarget = nullptr;
  144. mActiveDepthStencilState = nullptr;
  145. RenderStateCoreManager::shutDown();
  146. RenderWindowCoreManager::shutDown();
  147. RenderWindowManager::shutDown();
  148. HardwareBufferCoreManager::shutDown();
  149. HardwareBufferManager::shutDown();
  150. TextureCoreManager::shutDown();
  151. TextureManager::shutDown();
  152. SAFE_RELEASE(mDXGIFactory);
  153. if(mDevice != nullptr)
  154. {
  155. bs_delete(mDevice);
  156. mDevice = nullptr;
  157. }
  158. if(mDriverList != nullptr)
  159. {
  160. bs_delete(mDriverList);
  161. mDriverList = nullptr;
  162. }
  163. mActiveD3DDriver = nullptr;
  164. RenderAPICore::destroyCore();
  165. }
  166. void D3D11RenderAPI::setGraphicsPipeline(const SPtr<GpuPipelineStateCore>& pipelineState,
  167. const SPtr<CommandBuffer>& commandBuffer)
  168. {
  169. auto executeRef = [&](const SPtr<GpuPipelineStateCore>& pipelineState)
  170. {
  171. THROW_IF_NOT_CORE_THREAD;
  172. D3D11BlendStateCore* d3d11BlendState;
  173. D3D11RasterizerStateCore* d3d11RasterizerState;
  174. D3D11GpuFragmentProgramCore* d3d11FragmentProgram;
  175. D3D11GpuGeometryProgramCore* d3d11GeometryProgram;
  176. D3D11GpuDomainProgramCore* d3d11DomainProgram;
  177. D3D11GpuHullProgramCore* d3d11HullProgram;
  178. if(pipelineState != nullptr)
  179. {
  180. d3d11BlendState = static_cast<D3D11BlendStateCore*>(pipelineState->getBlendState().get());
  181. d3d11RasterizerState = static_cast<D3D11RasterizerStateCore*>(pipelineState->getRasterizerState().get());
  182. mActiveDepthStencilState = std::static_pointer_cast<D3D11DepthStencilStateCore>(pipelineState->getDepthStencilState());
  183. mActiveVertexShader = std::static_pointer_cast<D3D11GpuVertexProgramCore>(pipelineState->getVertexProgram());
  184. d3d11FragmentProgram = static_cast<D3D11GpuFragmentProgramCore*>(pipelineState->getFragmentProgram().get());
  185. d3d11GeometryProgram = static_cast<D3D11GpuGeometryProgramCore*>(pipelineState->getGeometryProgram().get());
  186. d3d11DomainProgram = static_cast<D3D11GpuDomainProgramCore*>(pipelineState->getDomainProgram().get());
  187. d3d11HullProgram = static_cast<D3D11GpuHullProgramCore*>(pipelineState->getHullProgram().get());
  188. if (d3d11BlendState == nullptr)
  189. d3d11BlendState = static_cast<D3D11BlendStateCore*>(BlendStateCore::getDefault().get());
  190. if (d3d11RasterizerState == nullptr)
  191. d3d11RasterizerState = static_cast<D3D11RasterizerStateCore*>(RasterizerStateCore::getDefault().get());
  192. if (mActiveDepthStencilState == nullptr)
  193. mActiveDepthStencilState = std::static_pointer_cast<D3D11DepthStencilStateCore>(DepthStencilStateCore::getDefault());
  194. }
  195. else
  196. {
  197. d3d11BlendState = static_cast<D3D11BlendStateCore*>(BlendStateCore::getDefault().get());
  198. d3d11RasterizerState = static_cast<D3D11RasterizerStateCore*>(RasterizerStateCore::getDefault().get());
  199. mActiveDepthStencilState = std::static_pointer_cast<D3D11DepthStencilStateCore>(DepthStencilStateCore::getDefault());
  200. mActiveVertexShader = nullptr;
  201. d3d11FragmentProgram = nullptr;
  202. d3d11GeometryProgram = nullptr;
  203. d3d11DomainProgram = nullptr;
  204. d3d11HullProgram = nullptr;
  205. }
  206. ID3D11DeviceContext* d3d11Context = mDevice->getImmediateContext();
  207. d3d11Context->OMSetBlendState(d3d11BlendState->getInternal(), nullptr, 0xFFFFFFFF);
  208. d3d11Context->RSSetState(d3d11RasterizerState->getInternal());
  209. d3d11Context->OMSetDepthStencilState(mActiveDepthStencilState->getInternal(), mStencilRef);
  210. if (mActiveVertexShader != nullptr)
  211. {
  212. D3D11GpuVertexProgramCore* vertexProgram = static_cast<D3D11GpuVertexProgramCore*>(mActiveVertexShader.get());
  213. d3d11Context->VSSetShader(vertexProgram->getVertexShader(), nullptr, 0);
  214. }
  215. else
  216. d3d11Context->VSSetShader(nullptr, nullptr, 0);
  217. if(d3d11FragmentProgram != nullptr)
  218. d3d11Context->PSSetShader(d3d11FragmentProgram->getPixelShader(), nullptr, 0);
  219. else
  220. d3d11Context->PSSetShader(nullptr, nullptr, 0);
  221. if (d3d11GeometryProgram != nullptr)
  222. d3d11Context->GSSetShader(d3d11GeometryProgram->getGeometryShader(), nullptr, 0);
  223. else
  224. d3d11Context->GSSetShader(nullptr, nullptr, 0);
  225. if (d3d11DomainProgram != nullptr)
  226. d3d11Context->DSSetShader(d3d11DomainProgram->getDomainShader(), nullptr, 0);
  227. else
  228. d3d11Context->DSSetShader(nullptr, nullptr, 0);
  229. if (d3d11HullProgram != nullptr)
  230. d3d11Context->HSSetShader(d3d11HullProgram->getHullShader(), nullptr, 0);
  231. else
  232. d3d11Context->HSSetShader(nullptr, nullptr, 0);
  233. };
  234. if (commandBuffer == nullptr)
  235. executeRef(pipelineState);
  236. else
  237. {
  238. auto execute = [=]() { executeRef(pipelineState); };
  239. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  240. cb->queueCommand(execute);
  241. }
  242. BS_INC_RENDER_STAT(NumPipelineStateChanges);
  243. }
  244. void D3D11RenderAPI::setComputePipeline(const SPtr<GpuProgramCore>& computeProgram,
  245. const SPtr<CommandBuffer>& commandBuffer)
  246. {
  247. auto executeRef = [&](const SPtr<GpuProgramCore>& computeProgram)
  248. {
  249. THROW_IF_NOT_CORE_THREAD;
  250. if (computeProgram != nullptr && computeProgram->getProperties().getType() == GPT_COMPUTE_PROGRAM)
  251. {
  252. D3D11GpuComputeProgramCore *d3d11ComputeProgram = static_cast<D3D11GpuComputeProgramCore*>(computeProgram.get());
  253. mDevice->getImmediateContext()->CSSetShader(d3d11ComputeProgram->getComputeShader(), nullptr, 0);
  254. }
  255. else
  256. mDevice->getImmediateContext()->CSSetShader(nullptr, nullptr, 0);
  257. };
  258. if (commandBuffer == nullptr)
  259. executeRef(computeProgram);
  260. else
  261. {
  262. auto execute = [=]() { executeRef(computeProgram); };
  263. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  264. cb->queueCommand(execute);
  265. }
  266. BS_INC_RENDER_STAT(NumPipelineStateChanges);
  267. }
  268. void D3D11RenderAPI::setGpuParams(const SPtr<GpuParamsCore>& gpuParams, const SPtr<CommandBuffer>& commandBuffer)
  269. {
  270. auto executeRef = [&](const SPtr<GpuParamsCore>& gpuParams)
  271. {
  272. THROW_IF_NOT_CORE_THREAD;
  273. bs_frame_mark();
  274. {
  275. FrameVector<ID3D11ShaderResourceView*> srvs(8);
  276. FrameVector<ID3D11UnorderedAccessView*> uavs(8);
  277. FrameVector<ID3D11Buffer*> constBuffers(8);
  278. FrameVector<ID3D11SamplerState*> samplers(8);
  279. auto populateViews = [&](GpuProgramType type)
  280. {
  281. srvs.clear();
  282. uavs.clear();
  283. constBuffers.clear();
  284. samplers.clear();
  285. SPtr<GpuParamDesc> paramDesc = gpuParams->getParamDesc(type);
  286. if (paramDesc == nullptr)
  287. return;
  288. for (auto iter = paramDesc->textures.begin(); iter != paramDesc->textures.end(); ++iter)
  289. {
  290. UINT32 slot = iter->second.slot;
  291. SPtr<TextureCore> texture = gpuParams->getTexture(iter->second.set, slot);
  292. while (slot >= (UINT32)srvs.size())
  293. srvs.push_back(nullptr);
  294. if (texture != nullptr)
  295. {
  296. D3D11TextureCore* d3d11Texture = static_cast<D3D11TextureCore*>(texture.get());
  297. srvs[slot] = d3d11Texture->getSRV();
  298. }
  299. }
  300. for (auto iter = paramDesc->buffers.begin(); iter != paramDesc->buffers.end(); ++iter)
  301. {
  302. UINT32 slot = iter->second.slot;
  303. SPtr<GpuBufferCore> buffer = gpuParams->getBuffer(iter->second.set, slot);
  304. bool isLoadStore = iter->second.type != GPOT_BYTE_BUFFER &&
  305. iter->second.type != GPOT_STRUCTURED_BUFFER;
  306. if (!isLoadStore)
  307. {
  308. while (slot >= (UINT32)srvs.size())
  309. srvs.push_back(nullptr);
  310. if (buffer != nullptr)
  311. {
  312. D3D11GpuBufferCore* d3d11buffer = static_cast<D3D11GpuBufferCore*>(buffer.get());
  313. srvs[slot] = d3d11buffer->getSRV();
  314. }
  315. }
  316. else
  317. {
  318. while (slot >= (UINT32)uavs.size())
  319. uavs.push_back(nullptr);
  320. if (buffer != nullptr)
  321. {
  322. D3D11GpuBufferCore* d3d11buffer = static_cast<D3D11GpuBufferCore*>(buffer.get());
  323. uavs[slot] = d3d11buffer->getUAV();
  324. }
  325. }
  326. }
  327. for (auto iter = paramDesc->loadStoreTextures.begin(); iter != paramDesc->loadStoreTextures.end(); ++iter)
  328. {
  329. UINT32 slot = iter->second.slot;
  330. SPtr<TextureCore> texture = gpuParams->getLoadStoreTexture(iter->second.set, slot);
  331. const TextureSurface& surface = gpuParams->getLoadStoreSurface(iter->second.set, slot);
  332. while (slot >= (UINT32)uavs.size())
  333. uavs.push_back(nullptr);
  334. if (texture != nullptr)
  335. {
  336. SPtr<TextureView> texView = TextureCore::requestView(texture, surface.mipLevel, 1,
  337. surface.arraySlice, surface.numArraySlices, GVU_RANDOMWRITE);
  338. D3D11TextureView* d3d11texView = static_cast<D3D11TextureView*>(texView.get());
  339. uavs[slot] = d3d11texView->getUAV();
  340. if (mBoundUAVs[slot].second != nullptr)
  341. mBoundUAVs[slot].first->releaseView(mBoundUAVs[slot].second);
  342. mBoundUAVs[slot] = std::make_pair(texture, texView);
  343. }
  344. else
  345. {
  346. uavs[slot] = nullptr;
  347. if (mBoundUAVs[slot].second != nullptr)
  348. mBoundUAVs[slot].first->releaseView(mBoundUAVs[slot].second);
  349. mBoundUAVs[slot] = std::pair<SPtr<TextureCore>, SPtr<TextureView>>();
  350. }
  351. }
  352. for (auto iter = paramDesc->samplers.begin(); iter != paramDesc->samplers.end(); ++iter)
  353. {
  354. UINT32 slot = iter->second.slot;
  355. SPtr<SamplerStateCore> samplerState = gpuParams->getSamplerState(iter->second.set, slot);
  356. while (slot >= (UINT32)samplers.size())
  357. samplers.push_back(nullptr);
  358. if (samplerState == nullptr)
  359. samplerState = SamplerStateCore::getDefault();
  360. D3D11SamplerStateCore* d3d11SamplerState =
  361. static_cast<D3D11SamplerStateCore*>(const_cast<SamplerStateCore*>(samplerState.get()));
  362. samplers[slot] = d3d11SamplerState->getInternal();
  363. }
  364. for (auto iter = paramDesc->paramBlocks.begin(); iter != paramDesc->paramBlocks.end(); ++iter)
  365. {
  366. UINT32 slot = iter->second.slot;
  367. SPtr<GpuParamBlockBufferCore> buffer = gpuParams->getParamBlockBuffer(iter->second.set, slot);
  368. buffer->flushToGPU();
  369. while (slot >= (UINT32)constBuffers.size())
  370. constBuffers.push_back(nullptr);
  371. if (buffer != nullptr)
  372. {
  373. const D3D11GpuParamBlockBufferCore* d3d11paramBlockBuffer =
  374. static_cast<const D3D11GpuParamBlockBufferCore*>(buffer.get());
  375. constBuffers[slot] = d3d11paramBlockBuffer->getD3D11Buffer();
  376. }
  377. }
  378. };
  379. ID3D11DeviceContext* context = mDevice->getImmediateContext();
  380. UINT32 numSRVs = 0;
  381. UINT32 numUAVs = 0;
  382. UINT32 numConstBuffers = 0;
  383. UINT32 numSamplers = 0;
  384. populateViews(GPT_VERTEX_PROGRAM);
  385. numSRVs = (UINT32)srvs.size();
  386. numUAVs = (UINT32)uavs.size();
  387. numConstBuffers = (UINT32)constBuffers.size();
  388. numSamplers = (UINT32)samplers.size();
  389. if(numSRVs > 0)
  390. context->VSSetShaderResources(0, numSRVs, srvs.data());
  391. if(numUAVs > 0)
  392. {
  393. context->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL, nullptr,
  394. nullptr, 0, numUAVs, uavs.data(), nullptr);
  395. }
  396. if (numConstBuffers > 0)
  397. context->VSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  398. if (numSamplers > 0)
  399. context->VSSetSamplers(0, numSamplers, samplers.data());
  400. populateViews(GPT_FRAGMENT_PROGRAM);
  401. numSRVs = (UINT32)srvs.size();
  402. numConstBuffers = (UINT32)constBuffers.size();
  403. numSamplers = (UINT32)samplers.size();
  404. if (numSRVs > 0)
  405. context->PSSetShaderResources(0, numSRVs, srvs.data());
  406. if (numConstBuffers > 0)
  407. context->PSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  408. if (numSamplers > 0)
  409. context->PSSetSamplers(0, numSamplers, samplers.data());
  410. populateViews(GPT_GEOMETRY_PROGRAM);
  411. numSRVs = (UINT32)srvs.size();
  412. numConstBuffers = (UINT32)constBuffers.size();
  413. numSamplers = (UINT32)samplers.size();
  414. if (numSRVs > 0)
  415. context->GSSetShaderResources(0, numSRVs, srvs.data());
  416. if (numConstBuffers > 0)
  417. context->GSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  418. if (numSamplers > 0)
  419. context->GSSetSamplers(0, numSamplers, samplers.data());
  420. populateViews(GPT_HULL_PROGRAM);
  421. numSRVs = (UINT32)srvs.size();
  422. numConstBuffers = (UINT32)constBuffers.size();
  423. numSamplers = (UINT32)samplers.size();
  424. if (numSRVs > 0)
  425. context->HSSetShaderResources(0, numSRVs, srvs.data());
  426. if (numConstBuffers > 0)
  427. context->HSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  428. if (numSamplers > 0)
  429. context->HSSetSamplers(0, numSamplers, samplers.data());
  430. populateViews(GPT_DOMAIN_PROGRAM);
  431. numSRVs = (UINT32)srvs.size();
  432. numConstBuffers = (UINT32)constBuffers.size();
  433. numSamplers = (UINT32)samplers.size();
  434. if (numSRVs > 0)
  435. context->DSSetShaderResources(0, numSRVs, srvs.data());
  436. if (numConstBuffers > 0)
  437. context->DSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  438. if (numSamplers > 0)
  439. context->DSSetSamplers(0, numSamplers, samplers.data());
  440. populateViews(GPT_COMPUTE_PROGRAM);
  441. numSRVs = (UINT32)srvs.size();
  442. numUAVs = (UINT32)uavs.size();
  443. numConstBuffers = (UINT32)constBuffers.size();
  444. numSamplers = (UINT32)samplers.size();
  445. if (numSRVs > 0)
  446. context->CSSetShaderResources(0, numSRVs, srvs.data());
  447. if(numUAVs > 0)
  448. context->CSSetUnorderedAccessViews(0, numUAVs, uavs.data(), nullptr);
  449. if (numConstBuffers > 0)
  450. context->CSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  451. if (numSamplers > 0)
  452. context->CSSetSamplers(0, numSamplers, samplers.data());
  453. }
  454. bs_frame_clear();
  455. if (mDevice->hasError())
  456. BS_EXCEPT(RenderingAPIException, "Failed to set GPU parameters: " + mDevice->getErrorDescription());
  457. };
  458. if (commandBuffer == nullptr)
  459. executeRef(gpuParams);
  460. else
  461. {
  462. auto execute = [=]() { executeRef(gpuParams); };
  463. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  464. cb->queueCommand(execute);
  465. }
  466. BS_INC_RENDER_STAT(NumGpuParamBinds);
  467. }
  468. void D3D11RenderAPI::beginFrame(const SPtr<CommandBuffer>& commandBuffer)
  469. {
  470. // Not used
  471. }
  472. void D3D11RenderAPI::endFrame(const SPtr<CommandBuffer>& commandBuffer)
  473. {
  474. // Not used
  475. }
  476. void D3D11RenderAPI::setViewport(const Rect2& vp, const SPtr<CommandBuffer>& commandBuffer)
  477. {
  478. auto executeRef = [&](const Rect2& vp)
  479. {
  480. THROW_IF_NOT_CORE_THREAD;
  481. mViewportNorm = vp;
  482. applyViewport();
  483. };
  484. if (commandBuffer == nullptr)
  485. executeRef(vp);
  486. else
  487. {
  488. auto execute = [=]() { executeRef(vp); };
  489. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  490. cb->queueCommand(execute);
  491. }
  492. }
  493. void D3D11RenderAPI::setVertexBuffers(UINT32 index, SPtr<VertexBufferCore>* buffers, UINT32 numBuffers,
  494. const SPtr<CommandBuffer>& commandBuffer)
  495. {
  496. auto executeRef = [&](UINT32 index, SPtr<VertexBufferCore>* buffers, UINT32 numBuffers)
  497. {
  498. THROW_IF_NOT_CORE_THREAD;
  499. UINT32 maxBoundVertexBuffers = mCurrentCapabilities[0].getMaxBoundVertexBuffers();
  500. if (index < 0 || (index + numBuffers) >= maxBoundVertexBuffers)
  501. {
  502. BS_EXCEPT(InvalidParametersException, "Invalid vertex index: " + toString(index) +
  503. ". Valid range is 0 .. " + toString(maxBoundVertexBuffers - 1));
  504. }
  505. ID3D11Buffer* dx11buffers[MAX_BOUND_VERTEX_BUFFERS];
  506. UINT32 strides[MAX_BOUND_VERTEX_BUFFERS];
  507. UINT32 offsets[MAX_BOUND_VERTEX_BUFFERS];
  508. for (UINT32 i = 0; i < numBuffers; i++)
  509. {
  510. SPtr<D3D11VertexBufferCore> vertexBuffer = std::static_pointer_cast<D3D11VertexBufferCore>(buffers[i]);
  511. const VertexBufferProperties& vbProps = vertexBuffer->getProperties();
  512. dx11buffers[i] = vertexBuffer->getD3DVertexBuffer();
  513. strides[i] = vbProps.getVertexSize();
  514. offsets[i] = 0;
  515. }
  516. mDevice->getImmediateContext()->IASetVertexBuffers(index, numBuffers, dx11buffers, strides, offsets);
  517. };
  518. if (commandBuffer == nullptr)
  519. executeRef(index, buffers, numBuffers);
  520. else
  521. {
  522. auto execute = [=]() { executeRef(index, buffers, numBuffers); };
  523. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  524. cb->queueCommand(execute);
  525. }
  526. BS_INC_RENDER_STAT(NumVertexBufferBinds);
  527. }
  528. void D3D11RenderAPI::setIndexBuffer(const SPtr<IndexBufferCore>& buffer, const SPtr<CommandBuffer>& commandBuffer)
  529. {
  530. auto executeRef = [&](const SPtr<IndexBufferCore>& buffer)
  531. {
  532. THROW_IF_NOT_CORE_THREAD;
  533. SPtr<D3D11IndexBufferCore> indexBuffer = std::static_pointer_cast<D3D11IndexBufferCore>(buffer);
  534. DXGI_FORMAT indexFormat = DXGI_FORMAT_R16_UINT;
  535. if (indexBuffer->getProperties().getType() == IT_16BIT)
  536. indexFormat = DXGI_FORMAT_R16_UINT;
  537. else if (indexBuffer->getProperties().getType() == IT_32BIT)
  538. indexFormat = DXGI_FORMAT_R32_UINT;
  539. else
  540. BS_EXCEPT(InternalErrorException, "Unsupported index format: " + toString(indexBuffer->getProperties().getType()));
  541. mDevice->getImmediateContext()->IASetIndexBuffer(indexBuffer->getD3DIndexBuffer(), indexFormat, 0);
  542. };
  543. if (commandBuffer == nullptr)
  544. executeRef(buffer);
  545. else
  546. {
  547. auto execute = [=]() { executeRef(buffer); };
  548. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  549. cb->queueCommand(execute);
  550. }
  551. BS_INC_RENDER_STAT(NumIndexBufferBinds);
  552. }
  553. void D3D11RenderAPI::setVertexDeclaration(const SPtr<VertexDeclarationCore>& vertexDeclaration,
  554. const SPtr<CommandBuffer>& commandBuffer)
  555. {
  556. auto executeRef = [&](const SPtr<VertexDeclarationCore>& vertexDeclaration)
  557. {
  558. THROW_IF_NOT_CORE_THREAD;
  559. mActiveVertexDeclaration = vertexDeclaration;
  560. };
  561. if (commandBuffer == nullptr)
  562. executeRef(vertexDeclaration);
  563. else
  564. {
  565. auto execute = [=]() { executeRef(vertexDeclaration); };
  566. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  567. cb->queueCommand(execute);
  568. }
  569. }
  570. void D3D11RenderAPI::setDrawOperation(DrawOperationType op, const SPtr<CommandBuffer>& commandBuffer)
  571. {
  572. auto executeRef = [&](DrawOperationType op)
  573. {
  574. THROW_IF_NOT_CORE_THREAD;
  575. mDevice->getImmediateContext()->IASetPrimitiveTopology(D3D11Mappings::getPrimitiveType(op));
  576. mActiveDrawOp = op;
  577. };
  578. if (commandBuffer == nullptr)
  579. executeRef(op);
  580. else
  581. {
  582. auto execute = [=]() { executeRef(op); };
  583. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  584. cb->queueCommand(execute);
  585. cb->mActiveDrawOp = op;
  586. }
  587. }
  588. void D3D11RenderAPI::draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount,
  589. const SPtr<CommandBuffer>& commandBuffer)
  590. {
  591. auto executeRef = [&](UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount)
  592. {
  593. THROW_IF_NOT_CORE_THREAD;
  594. applyInputLayout();
  595. if (instanceCount <= 1)
  596. mDevice->getImmediateContext()->Draw(vertexCount, vertexOffset);
  597. else
  598. mDevice->getImmediateContext()->DrawInstanced(vertexCount, instanceCount, vertexOffset, 0);
  599. #if BS_DEBUG_MODE
  600. if (mDevice->hasError())
  601. LOGWRN(mDevice->getErrorDescription());
  602. #endif
  603. };
  604. UINT32 primCount;
  605. if (commandBuffer == nullptr)
  606. {
  607. executeRef(vertexOffset, vertexCount, instanceCount);
  608. primCount = vertexCountToPrimCount(mActiveDrawOp, vertexCount);
  609. }
  610. else
  611. {
  612. auto execute = [=]() { executeRef(vertexOffset, vertexCount, instanceCount); };
  613. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  614. cb->queueCommand(execute);
  615. primCount = vertexCountToPrimCount(cb->mActiveDrawOp, vertexCount);
  616. }
  617. BS_INC_RENDER_STAT(NumDrawCalls);
  618. BS_ADD_RENDER_STAT(NumVertices, vertexCount);
  619. BS_ADD_RENDER_STAT(NumPrimitives, primCount);
  620. }
  621. void D3D11RenderAPI::drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount,
  622. UINT32 instanceCount, const SPtr<CommandBuffer>& commandBuffer)
  623. {
  624. auto executeRef = [&](UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount,
  625. UINT32 instanceCount)
  626. {
  627. THROW_IF_NOT_CORE_THREAD;
  628. applyInputLayout();
  629. if (instanceCount <= 1)
  630. mDevice->getImmediateContext()->DrawIndexed(indexCount, startIndex, vertexOffset);
  631. else
  632. mDevice->getImmediateContext()->DrawIndexedInstanced(indexCount, instanceCount, startIndex, vertexOffset, 0);
  633. #if BS_DEBUG_MODE
  634. if (mDevice->hasError())
  635. LOGWRN(mDevice->getErrorDescription());
  636. #endif
  637. };
  638. UINT32 primCount;
  639. if (commandBuffer == nullptr)
  640. {
  641. executeRef(startIndex, indexCount, vertexOffset, vertexCount, instanceCount);
  642. primCount = vertexCountToPrimCount(mActiveDrawOp, vertexCount);
  643. }
  644. else
  645. {
  646. auto execute = [=]() { executeRef(startIndex, indexCount, vertexOffset, vertexCount, instanceCount); };
  647. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  648. cb->queueCommand(execute);
  649. primCount = vertexCountToPrimCount(cb->mActiveDrawOp, vertexCount);
  650. }
  651. BS_INC_RENDER_STAT(NumDrawCalls);
  652. BS_ADD_RENDER_STAT(NumVertices, vertexCount);
  653. BS_ADD_RENDER_STAT(NumPrimitives, primCount);
  654. }
  655. void D3D11RenderAPI::dispatchCompute(UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ,
  656. const SPtr<CommandBuffer>& commandBuffer)
  657. {
  658. auto executeRef = [&](UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ)
  659. {
  660. THROW_IF_NOT_CORE_THREAD;
  661. mDevice->getImmediateContext()->Dispatch(numGroupsX, numGroupsY, numGroupsZ);
  662. #if BS_DEBUG_MODE
  663. if (mDevice->hasError())
  664. LOGWRN(mDevice->getErrorDescription());
  665. #endif
  666. };
  667. if (commandBuffer == nullptr)
  668. executeRef(numGroupsX, numGroupsY, numGroupsZ);
  669. else
  670. {
  671. auto execute = [=]() { executeRef(numGroupsX, numGroupsY, numGroupsZ); };
  672. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  673. cb->queueCommand(execute);
  674. }
  675. BS_INC_RENDER_STAT(NumComputeCalls);
  676. }
  677. void D3D11RenderAPI::setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom,
  678. const SPtr<CommandBuffer>& commandBuffer)
  679. {
  680. auto executeRef = [&](UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
  681. {
  682. THROW_IF_NOT_CORE_THREAD;
  683. mScissorRect.left = static_cast<LONG>(left);
  684. mScissorRect.top = static_cast<LONG>(top);
  685. mScissorRect.bottom = static_cast<LONG>(bottom);
  686. mScissorRect.right = static_cast<LONG>(right);
  687. mDevice->getImmediateContext()->RSSetScissorRects(1, &mScissorRect);
  688. };
  689. if (commandBuffer == nullptr)
  690. executeRef(left, top, right, bottom);
  691. else
  692. {
  693. auto execute = [=]() { executeRef(left, top, right, bottom); };
  694. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  695. cb->queueCommand(execute);
  696. }
  697. }
  698. void D3D11RenderAPI::setStencilRef(UINT32 value, const SPtr<CommandBuffer>& commandBuffer)
  699. {
  700. auto executeRef = [&](UINT32 value)
  701. {
  702. THROW_IF_NOT_CORE_THREAD;
  703. mStencilRef = value;
  704. if(mActiveDepthStencilState != nullptr)
  705. mDevice->getImmediateContext()->OMSetDepthStencilState(mActiveDepthStencilState->getInternal(), mStencilRef);
  706. else
  707. mDevice->getImmediateContext()->OMSetDepthStencilState(nullptr, mStencilRef);
  708. };
  709. if (commandBuffer == nullptr)
  710. executeRef(value);
  711. else
  712. {
  713. auto execute = [=]() { executeRef(value); };
  714. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  715. cb->queueCommand(execute);
  716. }
  717. }
  718. void D3D11RenderAPI::clearViewport(UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask,
  719. const SPtr<CommandBuffer>& commandBuffer)
  720. {
  721. auto executeRef = [&](UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask)
  722. {
  723. THROW_IF_NOT_CORE_THREAD;
  724. if (mActiveRenderTarget == nullptr)
  725. return;
  726. const RenderTargetProperties& rtProps = mActiveRenderTarget->getProperties();
  727. Rect2I clearArea((int)mViewport.TopLeftX, (int)mViewport.TopLeftY, (int)mViewport.Width, (int)mViewport.Height);
  728. bool clearEntireTarget = clearArea.width == 0 || clearArea.height == 0;
  729. clearEntireTarget |= (clearArea.x == 0 && clearArea.y == 0 && clearArea.width == rtProps.getWidth() &&
  730. clearArea.height == rtProps.getHeight());
  731. if (!clearEntireTarget)
  732. {
  733. // TODO - Ignoring targetMask here
  734. D3D11RenderUtility::instance().drawClearQuad(buffers, color, depth, stencil);
  735. BS_INC_RENDER_STAT(NumClears);
  736. }
  737. else
  738. clearRenderTarget(buffers, color, depth, stencil, targetMask);
  739. };
  740. if (commandBuffer == nullptr)
  741. executeRef(buffers, color, depth, stencil, targetMask);
  742. else
  743. {
  744. auto execute = [=]() { executeRef(buffers, color, depth, stencil, targetMask); };
  745. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  746. cb->queueCommand(execute);
  747. }
  748. }
  749. void D3D11RenderAPI::clearRenderTarget(UINT32 buffers, const Color& color, float depth, UINT16 stencil,
  750. UINT8 targetMask, const SPtr<CommandBuffer>& commandBuffer)
  751. {
  752. auto executeRef = [&](UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask)
  753. {
  754. THROW_IF_NOT_CORE_THREAD;
  755. if (mActiveRenderTarget == nullptr)
  756. return;
  757. // Clear render surfaces
  758. if (buffers & FBT_COLOR)
  759. {
  760. UINT32 maxRenderTargets = mCurrentCapabilities[0].getNumMultiRenderTargets();
  761. ID3D11RenderTargetView** views = bs_newN<ID3D11RenderTargetView*>(maxRenderTargets);
  762. memset(views, 0, sizeof(ID3D11RenderTargetView*) * maxRenderTargets);
  763. mActiveRenderTarget->getCustomAttribute("RTV", views);
  764. if (!views[0])
  765. {
  766. bs_deleteN(views, maxRenderTargets);
  767. return;
  768. }
  769. float clearColor[4];
  770. clearColor[0] = color.r;
  771. clearColor[1] = color.g;
  772. clearColor[2] = color.b;
  773. clearColor[3] = color.a;
  774. for (UINT32 i = 0; i < maxRenderTargets; i++)
  775. {
  776. if (views[i] != nullptr && ((1 << i) & targetMask) != 0)
  777. mDevice->getImmediateContext()->ClearRenderTargetView(views[i], clearColor);
  778. }
  779. bs_deleteN(views, maxRenderTargets);
  780. }
  781. // Clear depth stencil
  782. if ((buffers & FBT_DEPTH) != 0 || (buffers & FBT_STENCIL) != 0)
  783. {
  784. ID3D11DepthStencilView* depthStencilView = nullptr;
  785. mActiveRenderTarget->getCustomAttribute("DSV", &depthStencilView);
  786. D3D11_CLEAR_FLAG clearFlag;
  787. if ((buffers & FBT_DEPTH) != 0 && (buffers & FBT_STENCIL) != 0)
  788. clearFlag = (D3D11_CLEAR_FLAG)(D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL);
  789. else if ((buffers & FBT_STENCIL) != 0)
  790. clearFlag = D3D11_CLEAR_STENCIL;
  791. else
  792. clearFlag = D3D11_CLEAR_DEPTH;
  793. if (depthStencilView != nullptr)
  794. mDevice->getImmediateContext()->ClearDepthStencilView(depthStencilView, clearFlag, depth, (UINT8)stencil);
  795. }
  796. };
  797. if (commandBuffer == nullptr)
  798. executeRef(buffers, color, depth, stencil, targetMask);
  799. else
  800. {
  801. auto execute = [=]() { executeRef(buffers, color, depth, stencil, targetMask); };
  802. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  803. cb->queueCommand(execute);
  804. }
  805. BS_INC_RENDER_STAT(NumClears);
  806. }
  807. void D3D11RenderAPI::setRenderTarget(const SPtr<RenderTargetCore>& target, bool readOnlyDepthStencil,
  808. const SPtr<CommandBuffer>& commandBuffer)
  809. {
  810. auto executeRef = [&](const SPtr<RenderTargetCore>& target, bool readOnlyDepthStencil)
  811. {
  812. THROW_IF_NOT_CORE_THREAD;
  813. mActiveRenderTarget = target;
  814. UINT32 maxRenderTargets = mCurrentCapabilities[0].getNumMultiRenderTargets();
  815. ID3D11RenderTargetView** views = bs_newN<ID3D11RenderTargetView*>(maxRenderTargets);
  816. memset(views, 0, sizeof(ID3D11RenderTargetView*) * maxRenderTargets);
  817. ID3D11DepthStencilView* depthStencilView = nullptr;
  818. if (target != nullptr)
  819. {
  820. target->getCustomAttribute("RTV", views);
  821. if (readOnlyDepthStencil)
  822. target->getCustomAttribute("RODSV", &depthStencilView);
  823. else
  824. target->getCustomAttribute("DSV", &depthStencilView);
  825. }
  826. // Bind render targets
  827. mDevice->getImmediateContext()->OMSetRenderTargets(maxRenderTargets, views, depthStencilView);
  828. if (mDevice->hasError())
  829. BS_EXCEPT(RenderingAPIException, "Failed to setRenderTarget : " + mDevice->getErrorDescription());
  830. bs_deleteN(views, maxRenderTargets);
  831. applyViewport();
  832. };
  833. if (commandBuffer == nullptr)
  834. executeRef(target, readOnlyDepthStencil);
  835. else
  836. {
  837. auto execute = [=]() { executeRef(target, readOnlyDepthStencil); };
  838. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  839. cb->queueCommand(execute);
  840. }
  841. BS_INC_RENDER_STAT(NumRenderTargetChanges);
  842. }
  843. void D3D11RenderAPI::swapBuffers(const SPtr<RenderTargetCore>& target, UINT32 syncMask)
  844. {
  845. THROW_IF_NOT_CORE_THREAD;
  846. target->swapBuffers();
  847. BS_INC_RENDER_STAT(NumPresents);
  848. }
  849. void D3D11RenderAPI::addCommands(const SPtr<CommandBuffer>& commandBuffer, const SPtr<CommandBuffer>& secondary)
  850. {
  851. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  852. SPtr<D3D11CommandBuffer> secondaryCb = std::static_pointer_cast<D3D11CommandBuffer>(secondary);
  853. cb->appendSecondary(secondaryCb);
  854. }
  855. void D3D11RenderAPI::executeCommands(const SPtr<CommandBuffer>& commandBuffer, UINT32 syncMask)
  856. {
  857. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  858. cb->executeCommands();
  859. cb->clear();
  860. }
  861. void D3D11RenderAPI::applyViewport()
  862. {
  863. if (mActiveRenderTarget == nullptr)
  864. return;
  865. const RenderTargetProperties& rtProps = mActiveRenderTarget->getProperties();
  866. // Set viewport dimensions
  867. mViewport.TopLeftX = (FLOAT)(rtProps.getWidth() * mViewportNorm.x);
  868. mViewport.TopLeftY = (FLOAT)(rtProps.getHeight() * mViewportNorm.y);
  869. mViewport.Width = (FLOAT)(rtProps.getWidth() * mViewportNorm.width);
  870. mViewport.Height = (FLOAT)(rtProps.getHeight() * mViewportNorm.height);
  871. if (rtProps.requiresTextureFlipping())
  872. {
  873. // Convert "top-left" to "bottom-left"
  874. mViewport.TopLeftY = rtProps.getHeight() - mViewport.Height - mViewport.TopLeftY;
  875. }
  876. mViewport.MinDepth = 0.0f;
  877. mViewport.MaxDepth = 1.0f;
  878. mDevice->getImmediateContext()->RSSetViewports(1, &mViewport);
  879. }
  880. void D3D11RenderAPI::initCapabilites(IDXGIAdapter* adapter, RenderAPICapabilities& caps) const
  881. {
  882. THROW_IF_NOT_CORE_THREAD;
  883. // This must query for DirectX 10 interface as this is unsupported for DX11
  884. LARGE_INTEGER driverVersionNum;
  885. DriverVersion driverVersion;
  886. if (SUCCEEDED(adapter->CheckInterfaceSupport(IID_ID3D10Device, &driverVersionNum)))
  887. {
  888. driverVersion.major = HIWORD(driverVersionNum.HighPart);
  889. driverVersion.minor = LOWORD(driverVersionNum.HighPart);
  890. driverVersion.release = HIWORD(driverVersionNum.LowPart);
  891. driverVersion.build = LOWORD(driverVersionNum.LowPart);
  892. }
  893. caps.setDriverVersion(driverVersion);
  894. caps.setDeviceName(mActiveD3DDriver->getDriverDescription());
  895. caps.setRenderAPIName(getName());
  896. caps.setCapability(RSC_TEXTURE_COMPRESSION_BC);
  897. caps.addShaderProfile("hlsl");
  898. if(mFeatureLevel >= D3D_FEATURE_LEVEL_10_1)
  899. caps.setMaxBoundVertexBuffers(32);
  900. else
  901. caps.setMaxBoundVertexBuffers(16);
  902. if(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0)
  903. {
  904. caps.setCapability(RSC_GEOMETRY_PROGRAM);
  905. caps.addShaderProfile("ps_4_0");
  906. caps.addShaderProfile("vs_4_0");
  907. caps.addShaderProfile("gs_4_0");
  908. caps.addGpuProgramProfile(GPP_FS_4_0, "ps_4_0");
  909. caps.addGpuProgramProfile(GPP_VS_4_0, "vs_4_0");
  910. caps.addGpuProgramProfile(GPP_GS_4_0, "gs_4_0");
  911. caps.setNumTextureUnits(GPT_FRAGMENT_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  912. caps.setNumTextureUnits(GPT_VERTEX_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  913. caps.setNumTextureUnits(GPT_GEOMETRY_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  914. caps.setNumCombinedTextureUnits(caps.getNumTextureUnits(GPT_FRAGMENT_PROGRAM)
  915. + caps.getNumTextureUnits(GPT_VERTEX_PROGRAM) + caps.getNumTextureUnits(GPT_GEOMETRY_PROGRAM));
  916. caps.setNumGpuParamBlockBuffers(GPT_FRAGMENT_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  917. caps.setNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  918. caps.setNumGpuParamBlockBuffers(GPT_GEOMETRY_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  919. caps.setNumCombinedGpuParamBlockBuffers(caps.getNumGpuParamBlockBuffers(GPT_FRAGMENT_PROGRAM)
  920. + caps.getNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM) + caps.getNumGpuParamBlockBuffers(GPT_GEOMETRY_PROGRAM));
  921. }
  922. if(mFeatureLevel >= D3D_FEATURE_LEVEL_10_1)
  923. {
  924. caps.addShaderProfile("ps_4_1");
  925. caps.addShaderProfile("vs_4_1");
  926. caps.addShaderProfile("gs_4_1");
  927. caps.addGpuProgramProfile(GPP_FS_4_1, "ps_4_1");
  928. caps.addGpuProgramProfile(GPP_VS_4_1, "vs_4_1");
  929. caps.addGpuProgramProfile(GPP_GS_4_1, "gs_4_1");
  930. }
  931. if(mFeatureLevel >= D3D_FEATURE_LEVEL_11_0)
  932. {
  933. caps.setCapability(RSC_TESSELLATION_PROGRAM);
  934. caps.setCapability(RSC_COMPUTE_PROGRAM);
  935. caps.addShaderProfile("ps_5_0");
  936. caps.addShaderProfile("vs_5_0");
  937. caps.addShaderProfile("gs_5_0");
  938. caps.addShaderProfile("cs_5_0");
  939. caps.addShaderProfile("hs_5_0");
  940. caps.addShaderProfile("ds_5_0");
  941. caps.addGpuProgramProfile(GPP_FS_5_0, "ps_5_0");
  942. caps.addGpuProgramProfile(GPP_VS_5_0, "vs_5_0");
  943. caps.addGpuProgramProfile(GPP_GS_5_0, "gs_5_0");
  944. caps.addGpuProgramProfile(GPP_CS_5_0, "cs_5_0");
  945. caps.addGpuProgramProfile(GPP_HS_5_0, "hs_5_0");
  946. caps.addGpuProgramProfile(GPP_DS_5_0, "ds_5_0");
  947. caps.setNumTextureUnits(GPT_HULL_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_REGISTER_COUNT);
  948. caps.setNumTextureUnits(GPT_DOMAIN_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_REGISTER_COUNT);
  949. caps.setNumTextureUnits(GPT_COMPUTE_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_REGISTER_COUNT);
  950. caps.setNumCombinedTextureUnits(caps.getNumTextureUnits(GPT_FRAGMENT_PROGRAM)
  951. + caps.getNumTextureUnits(GPT_VERTEX_PROGRAM) + caps.getNumTextureUnits(GPT_GEOMETRY_PROGRAM)
  952. + caps.getNumTextureUnits(GPT_HULL_PROGRAM) + caps.getNumTextureUnits(GPT_DOMAIN_PROGRAM)
  953. + caps.getNumTextureUnits(GPT_COMPUTE_PROGRAM));
  954. caps.setNumGpuParamBlockBuffers(GPT_HULL_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  955. caps.setNumGpuParamBlockBuffers(GPT_DOMAIN_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  956. caps.setNumGpuParamBlockBuffers(GPT_COMPUTE_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  957. caps.setNumCombinedGpuParamBlockBuffers(caps.getNumGpuParamBlockBuffers(GPT_FRAGMENT_PROGRAM)
  958. + caps.getNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM) + caps.getNumGpuParamBlockBuffers(GPT_GEOMETRY_PROGRAM)
  959. + caps.getNumGpuParamBlockBuffers(GPT_HULL_PROGRAM) + caps.getNumGpuParamBlockBuffers(GPT_DOMAIN_PROGRAM)
  960. + caps.getNumGpuParamBlockBuffers(GPT_COMPUTE_PROGRAM));
  961. caps.setNumLoadStoreTextureUnits(GPT_FRAGMENT_PROGRAM, D3D11_PS_CS_UAV_REGISTER_COUNT);
  962. caps.setNumLoadStoreTextureUnits(GPT_COMPUTE_PROGRAM, D3D11_PS_CS_UAV_REGISTER_COUNT);
  963. caps.setNumCombinedLoadStoreTextureUnits(caps.getNumLoadStoreTextureUnits(GPT_FRAGMENT_PROGRAM)
  964. + caps.getNumLoadStoreTextureUnits(GPT_COMPUTE_PROGRAM));
  965. }
  966. // Adapter details
  967. const DXGI_ADAPTER_DESC& adapterID = mActiveD3DDriver->getAdapterIdentifier();
  968. // Determine vendor
  969. switch(adapterID.VendorId)
  970. {
  971. case 0x10DE:
  972. caps.setVendor(GPU_NVIDIA);
  973. break;
  974. case 0x1002:
  975. caps.setVendor(GPU_AMD);
  976. break;
  977. case 0x163C:
  978. case 0x8086:
  979. caps.setVendor(GPU_INTEL);
  980. break;
  981. default:
  982. caps.setVendor(GPU_UNKNOWN);
  983. break;
  984. };
  985. caps.setNumMultiRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT);
  986. }
  987. void D3D11RenderAPI::determineMultisampleSettings(UINT32 multisampleCount, DXGI_FORMAT format, DXGI_SAMPLE_DESC* outputSampleDesc)
  988. {
  989. bool tryCSAA = false; // Note: Disabled for now, but leaving the code for later so it might be useful
  990. enum CSAAMode { CSAA_Normal, CSAA_Quality };
  991. CSAAMode csaaMode = CSAA_Normal;
  992. bool foundValid = false;
  993. size_t origNumSamples = multisampleCount;
  994. while (!foundValid)
  995. {
  996. // Deal with special cases
  997. if (tryCSAA)
  998. {
  999. switch(multisampleCount)
  1000. {
  1001. case 8:
  1002. if (csaaMode == CSAA_Quality)
  1003. {
  1004. outputSampleDesc->Count = 8;
  1005. outputSampleDesc->Quality = 8;
  1006. }
  1007. else
  1008. {
  1009. outputSampleDesc->Count = 4;
  1010. outputSampleDesc->Quality = 8;
  1011. }
  1012. break;
  1013. case 16:
  1014. if (csaaMode == CSAA_Quality)
  1015. {
  1016. outputSampleDesc->Count = 8;
  1017. outputSampleDesc->Quality = 16;
  1018. }
  1019. else
  1020. {
  1021. outputSampleDesc->Count = 4;
  1022. outputSampleDesc->Quality = 16;
  1023. }
  1024. break;
  1025. }
  1026. }
  1027. else // !CSAA
  1028. {
  1029. outputSampleDesc->Count = multisampleCount == 0 ? 1 : multisampleCount;
  1030. outputSampleDesc->Quality = D3D11_STANDARD_MULTISAMPLE_PATTERN;
  1031. }
  1032. HRESULT hr;
  1033. UINT outQuality;
  1034. hr = mDevice->getD3D11Device()->CheckMultisampleQualityLevels(format, outputSampleDesc->Count, &outQuality);
  1035. if (SUCCEEDED(hr) && (!tryCSAA || outQuality > outputSampleDesc->Quality))
  1036. {
  1037. foundValid = true;
  1038. }
  1039. else
  1040. {
  1041. // Downgrade
  1042. if (tryCSAA && multisampleCount == 8)
  1043. {
  1044. // For CSAA, we'll try downgrading with quality mode at all samples.
  1045. // then try without quality, then drop CSAA
  1046. if (csaaMode == CSAA_Quality)
  1047. {
  1048. // Drop quality first
  1049. csaaMode = CSAA_Normal;
  1050. }
  1051. else
  1052. {
  1053. // Drop CSAA entirely
  1054. tryCSAA = false;
  1055. }
  1056. // Return to original requested samples
  1057. multisampleCount = static_cast<UINT32>(origNumSamples);
  1058. }
  1059. else
  1060. {
  1061. // Drop samples
  1062. multisampleCount--;
  1063. if (multisampleCount == 1)
  1064. {
  1065. // Ran out of options, no multisampling
  1066. multisampleCount = 0;
  1067. foundValid = true;
  1068. }
  1069. }
  1070. }
  1071. }
  1072. }
  1073. void D3D11RenderAPI::convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest)
  1074. {
  1075. dest = matrix;
  1076. // Convert depth range from [-1,+1] to [0,1]
  1077. dest[2][0] = (dest[2][0] + dest[3][0]) / 2;
  1078. dest[2][1] = (dest[2][1] + dest[3][1]) / 2;
  1079. dest[2][2] = (dest[2][2] + dest[3][2]) / 2;
  1080. dest[2][3] = (dest[2][3] + dest[3][3]) / 2;
  1081. }
  1082. const RenderAPIInfo& D3D11RenderAPI::getAPIInfo() const
  1083. {
  1084. static RenderAPIInfo info(0.0f, 0.0f, 0.0f, 1.0f, VET_COLOR_ABGR, false, true, false, false);
  1085. return info;
  1086. }
  1087. GpuParamBlockDesc D3D11RenderAPI::generateParamBlockDesc(const String& name, Vector<GpuParamDataDesc>& params)
  1088. {
  1089. GpuParamBlockDesc block;
  1090. block.blockSize = 0;
  1091. block.isShareable = true;
  1092. block.name = name;
  1093. block.slot = 0;
  1094. block.set = 0;
  1095. for (auto& param : params)
  1096. {
  1097. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[param.type];
  1098. UINT32 size = typeInfo.size / 4;
  1099. if (param.arraySize > 1)
  1100. {
  1101. // Arrays perform no packing and their elements are always padded and aligned to four component vectors
  1102. UINT32 alignOffset = size % typeInfo.baseTypeSize;
  1103. if (alignOffset != 0)
  1104. {
  1105. UINT32 padding = (typeInfo.baseTypeSize - alignOffset);
  1106. size += padding;
  1107. }
  1108. alignOffset = block.blockSize % typeInfo.baseTypeSize;
  1109. if (alignOffset != 0)
  1110. {
  1111. UINT32 padding = (typeInfo.baseTypeSize - alignOffset);
  1112. block.blockSize += padding;
  1113. }
  1114. param.elementSize = size;
  1115. param.arrayElementStride = size;
  1116. param.cpuMemOffset = block.blockSize;
  1117. param.gpuMemOffset = 0;
  1118. block.blockSize += size * param.arraySize;
  1119. }
  1120. else
  1121. {
  1122. // Pack everything as tightly as possible as long as the data doesn't cross 16 byte boundary
  1123. UINT32 alignOffset = block.blockSize % 4;
  1124. if (alignOffset != 0 && size > (4 - alignOffset))
  1125. {
  1126. UINT32 padding = (4 - alignOffset);
  1127. block.blockSize += padding;
  1128. }
  1129. param.elementSize = size;
  1130. param.arrayElementStride = size;
  1131. param.cpuMemOffset = block.blockSize;
  1132. param.gpuMemOffset = 0;
  1133. block.blockSize += size;
  1134. }
  1135. param.paramBlockSlot = 0;
  1136. param.paramBlockSet = 0;
  1137. }
  1138. // Constant buffer size must always be a multiple of 16
  1139. if (block.blockSize % 4 != 0)
  1140. block.blockSize += (4 - (block.blockSize % 4));
  1141. return block;
  1142. }
  1143. /************************************************************************/
  1144. /* PRIVATE */
  1145. /************************************************************************/
  1146. void D3D11RenderAPI::applyInputLayout()
  1147. {
  1148. if(mActiveVertexDeclaration == nullptr)
  1149. {
  1150. LOGWRN("Cannot apply input layout without a vertex declaration. Set vertex declaration before calling this method.");
  1151. return;
  1152. }
  1153. if(mActiveVertexShader == nullptr)
  1154. {
  1155. LOGWRN("Cannot apply input layout without a vertex shader. Set vertex shader before calling this method.");
  1156. return;
  1157. }
  1158. ID3D11InputLayout* ia = mIAManager->retrieveInputLayout(mActiveVertexShader->getInputDeclaration(), mActiveVertexDeclaration, *mActiveVertexShader);
  1159. mDevice->getImmediateContext()->IASetInputLayout(ia);
  1160. }
  1161. }