BsD3D11RenderAPI.cpp 45 KB

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