BsD3D11RenderAPI.cpp 47 KB

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