BsD3D11RenderAPI.cpp 46 KB

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