BsD3D11RenderAPI.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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 bs
  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<GraphicsPipelineStateCore>& pipelineState,
  167. const SPtr<CommandBuffer>& commandBuffer)
  168. {
  169. auto executeRef = [&](const SPtr<GraphicsPipelineStateCore>& 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<ComputePipelineStateCore>& pipelineState,
  245. const SPtr<CommandBuffer>& commandBuffer)
  246. {
  247. auto executeRef = [&](const SPtr<ComputePipelineStateCore>& pipelineState)
  248. {
  249. THROW_IF_NOT_CORE_THREAD;
  250. SPtr<GpuProgramCore> program;
  251. if (pipelineState != nullptr)
  252. program = pipelineState->getProgram();
  253. if (program != nullptr && program->getProperties().getType() == GPT_COMPUTE_PROGRAM)
  254. {
  255. D3D11GpuComputeProgramCore *d3d11ComputeProgram = static_cast<D3D11GpuComputeProgramCore*>(program.get());
  256. mDevice->getImmediateContext()->CSSetShader(d3d11ComputeProgram->getComputeShader(), nullptr, 0);
  257. }
  258. else
  259. mDevice->getImmediateContext()->CSSetShader(nullptr, nullptr, 0);
  260. };
  261. if (commandBuffer == nullptr)
  262. executeRef(pipelineState);
  263. else
  264. {
  265. auto execute = [=]() { executeRef(pipelineState); };
  266. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  267. cb->queueCommand(execute);
  268. }
  269. BS_INC_RENDER_STAT(NumPipelineStateChanges);
  270. }
  271. void D3D11RenderAPI::setGpuParams(const SPtr<GpuParamsCore>& gpuParams, const SPtr<CommandBuffer>& commandBuffer)
  272. {
  273. auto executeRef = [&](const SPtr<GpuParamsCore>& gpuParams)
  274. {
  275. THROW_IF_NOT_CORE_THREAD;
  276. bs_frame_mark();
  277. {
  278. FrameVector<ID3D11ShaderResourceView*> srvs(8);
  279. FrameVector<ID3D11UnorderedAccessView*> uavs(8);
  280. FrameVector<ID3D11Buffer*> constBuffers(8);
  281. FrameVector<ID3D11SamplerState*> samplers(8);
  282. auto populateViews = [&](GpuProgramType type)
  283. {
  284. srvs.clear();
  285. uavs.clear();
  286. constBuffers.clear();
  287. samplers.clear();
  288. SPtr<GpuParamDesc> paramDesc = gpuParams->getParamDesc(type);
  289. if (paramDesc == nullptr)
  290. return;
  291. for (auto iter = paramDesc->textures.begin(); iter != paramDesc->textures.end(); ++iter)
  292. {
  293. UINT32 slot = iter->second.slot;
  294. SPtr<TextureCore> texture = gpuParams->getTexture(iter->second.set, slot);
  295. while (slot >= (UINT32)srvs.size())
  296. srvs.push_back(nullptr);
  297. if (texture != nullptr)
  298. {
  299. D3D11TextureCore* d3d11Texture = static_cast<D3D11TextureCore*>(texture.get());
  300. srvs[slot] = d3d11Texture->getSRV();
  301. }
  302. }
  303. for (auto iter = paramDesc->buffers.begin(); iter != paramDesc->buffers.end(); ++iter)
  304. {
  305. UINT32 slot = iter->second.slot;
  306. SPtr<GpuBufferCore> buffer = gpuParams->getBuffer(iter->second.set, slot);
  307. bool isLoadStore = iter->second.type != GPOT_BYTE_BUFFER &&
  308. iter->second.type != GPOT_STRUCTURED_BUFFER;
  309. if (!isLoadStore)
  310. {
  311. while (slot >= (UINT32)srvs.size())
  312. srvs.push_back(nullptr);
  313. if (buffer != nullptr)
  314. {
  315. D3D11GpuBufferCore* d3d11buffer = static_cast<D3D11GpuBufferCore*>(buffer.get());
  316. srvs[slot] = d3d11buffer->getSRV();
  317. }
  318. }
  319. else
  320. {
  321. while (slot >= (UINT32)uavs.size())
  322. uavs.push_back(nullptr);
  323. if (buffer != nullptr)
  324. {
  325. D3D11GpuBufferCore* d3d11buffer = static_cast<D3D11GpuBufferCore*>(buffer.get());
  326. uavs[slot] = d3d11buffer->getUAV();
  327. }
  328. }
  329. }
  330. for (auto iter = paramDesc->loadStoreTextures.begin(); iter != paramDesc->loadStoreTextures.end(); ++iter)
  331. {
  332. UINT32 slot = iter->second.slot;
  333. SPtr<TextureCore> texture = gpuParams->getLoadStoreTexture(iter->second.set, slot);
  334. const TextureSurface& surface = gpuParams->getLoadStoreSurface(iter->second.set, slot);
  335. while (slot >= (UINT32)uavs.size())
  336. uavs.push_back(nullptr);
  337. if (texture != nullptr)
  338. {
  339. SPtr<TextureView> texView = TextureCore::requestView(texture, surface.mipLevel, 1,
  340. surface.arraySlice, surface.numArraySlices, GVU_RANDOMWRITE);
  341. D3D11TextureView* d3d11texView = static_cast<D3D11TextureView*>(texView.get());
  342. uavs[slot] = d3d11texView->getUAV();
  343. if (mBoundUAVs[slot].second != nullptr)
  344. mBoundUAVs[slot].first->releaseView(mBoundUAVs[slot].second);
  345. mBoundUAVs[slot] = std::make_pair(texture, texView);
  346. }
  347. else
  348. {
  349. uavs[slot] = nullptr;
  350. if (mBoundUAVs[slot].second != nullptr)
  351. mBoundUAVs[slot].first->releaseView(mBoundUAVs[slot].second);
  352. mBoundUAVs[slot] = std::pair<SPtr<TextureCore>, SPtr<TextureView>>();
  353. }
  354. }
  355. for (auto iter = paramDesc->samplers.begin(); iter != paramDesc->samplers.end(); ++iter)
  356. {
  357. UINT32 slot = iter->second.slot;
  358. SPtr<SamplerStateCore> samplerState = gpuParams->getSamplerState(iter->second.set, slot);
  359. while (slot >= (UINT32)samplers.size())
  360. samplers.push_back(nullptr);
  361. if (samplerState == nullptr)
  362. samplerState = SamplerStateCore::getDefault();
  363. D3D11SamplerStateCore* d3d11SamplerState =
  364. static_cast<D3D11SamplerStateCore*>(const_cast<SamplerStateCore*>(samplerState.get()));
  365. samplers[slot] = d3d11SamplerState->getInternal();
  366. }
  367. for (auto iter = paramDesc->paramBlocks.begin(); iter != paramDesc->paramBlocks.end(); ++iter)
  368. {
  369. UINT32 slot = iter->second.slot;
  370. SPtr<GpuParamBlockBufferCore> buffer = gpuParams->getParamBlockBuffer(iter->second.set, slot);
  371. while (slot >= (UINT32)constBuffers.size())
  372. constBuffers.push_back(nullptr);
  373. if (buffer != nullptr)
  374. {
  375. buffer->flushToGPU();
  376. const D3D11GpuParamBlockBufferCore* d3d11paramBlockBuffer =
  377. static_cast<const D3D11GpuParamBlockBufferCore*>(buffer.get());
  378. constBuffers[slot] = d3d11paramBlockBuffer->getD3D11Buffer();
  379. }
  380. }
  381. };
  382. ID3D11DeviceContext* context = mDevice->getImmediateContext();
  383. UINT32 numSRVs = 0;
  384. UINT32 numUAVs = 0;
  385. UINT32 numConstBuffers = 0;
  386. UINT32 numSamplers = 0;
  387. populateViews(GPT_VERTEX_PROGRAM);
  388. numSRVs = (UINT32)srvs.size();
  389. numUAVs = (UINT32)uavs.size();
  390. numConstBuffers = (UINT32)constBuffers.size();
  391. numSamplers = (UINT32)samplers.size();
  392. if(numSRVs > 0)
  393. context->VSSetShaderResources(0, numSRVs, srvs.data());
  394. if(numUAVs > 0)
  395. {
  396. context->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL, nullptr,
  397. nullptr, 0, numUAVs, uavs.data(), nullptr);
  398. }
  399. if (numConstBuffers > 0)
  400. context->VSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  401. if (numSamplers > 0)
  402. context->VSSetSamplers(0, numSamplers, samplers.data());
  403. populateViews(GPT_FRAGMENT_PROGRAM);
  404. numSRVs = (UINT32)srvs.size();
  405. numConstBuffers = (UINT32)constBuffers.size();
  406. numSamplers = (UINT32)samplers.size();
  407. if (numSRVs > 0)
  408. context->PSSetShaderResources(0, numSRVs, srvs.data());
  409. if (numConstBuffers > 0)
  410. context->PSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  411. if (numSamplers > 0)
  412. context->PSSetSamplers(0, numSamplers, samplers.data());
  413. populateViews(GPT_GEOMETRY_PROGRAM);
  414. numSRVs = (UINT32)srvs.size();
  415. numConstBuffers = (UINT32)constBuffers.size();
  416. numSamplers = (UINT32)samplers.size();
  417. if (numSRVs > 0)
  418. context->GSSetShaderResources(0, numSRVs, srvs.data());
  419. if (numConstBuffers > 0)
  420. context->GSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  421. if (numSamplers > 0)
  422. context->GSSetSamplers(0, numSamplers, samplers.data());
  423. populateViews(GPT_HULL_PROGRAM);
  424. numSRVs = (UINT32)srvs.size();
  425. numConstBuffers = (UINT32)constBuffers.size();
  426. numSamplers = (UINT32)samplers.size();
  427. if (numSRVs > 0)
  428. context->HSSetShaderResources(0, numSRVs, srvs.data());
  429. if (numConstBuffers > 0)
  430. context->HSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  431. if (numSamplers > 0)
  432. context->HSSetSamplers(0, numSamplers, samplers.data());
  433. populateViews(GPT_DOMAIN_PROGRAM);
  434. numSRVs = (UINT32)srvs.size();
  435. numConstBuffers = (UINT32)constBuffers.size();
  436. numSamplers = (UINT32)samplers.size();
  437. if (numSRVs > 0)
  438. context->DSSetShaderResources(0, numSRVs, srvs.data());
  439. if (numConstBuffers > 0)
  440. context->DSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  441. if (numSamplers > 0)
  442. context->DSSetSamplers(0, numSamplers, samplers.data());
  443. populateViews(GPT_COMPUTE_PROGRAM);
  444. numSRVs = (UINT32)srvs.size();
  445. numUAVs = (UINT32)uavs.size();
  446. numConstBuffers = (UINT32)constBuffers.size();
  447. numSamplers = (UINT32)samplers.size();
  448. if (numSRVs > 0)
  449. context->CSSetShaderResources(0, numSRVs, srvs.data());
  450. if(numUAVs > 0)
  451. context->CSSetUnorderedAccessViews(0, numUAVs, uavs.data(), nullptr);
  452. if (numConstBuffers > 0)
  453. context->CSSetConstantBuffers(0, numConstBuffers, constBuffers.data());
  454. if (numSamplers > 0)
  455. context->CSSetSamplers(0, numSamplers, samplers.data());
  456. }
  457. bs_frame_clear();
  458. if (mDevice->hasError())
  459. BS_EXCEPT(RenderingAPIException, "Failed to set GPU parameters: " + mDevice->getErrorDescription());
  460. };
  461. if (commandBuffer == nullptr)
  462. executeRef(gpuParams);
  463. else
  464. {
  465. auto execute = [=]() { executeRef(gpuParams); };
  466. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  467. cb->queueCommand(execute);
  468. }
  469. BS_INC_RENDER_STAT(NumGpuParamBinds);
  470. }
  471. void D3D11RenderAPI::setViewport(const Rect2& vp, const SPtr<CommandBuffer>& commandBuffer)
  472. {
  473. auto executeRef = [&](const Rect2& vp)
  474. {
  475. THROW_IF_NOT_CORE_THREAD;
  476. mViewportNorm = vp;
  477. applyViewport();
  478. };
  479. if (commandBuffer == nullptr)
  480. executeRef(vp);
  481. else
  482. {
  483. auto execute = [=]() { executeRef(vp); };
  484. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  485. cb->queueCommand(execute);
  486. }
  487. }
  488. void D3D11RenderAPI::setVertexBuffers(UINT32 index, SPtr<VertexBufferCore>* buffers, UINT32 numBuffers,
  489. const SPtr<CommandBuffer>& commandBuffer)
  490. {
  491. auto executeRef = [&](UINT32 index, SPtr<VertexBufferCore>* buffers, UINT32 numBuffers)
  492. {
  493. THROW_IF_NOT_CORE_THREAD;
  494. UINT32 maxBoundVertexBuffers = mCurrentCapabilities[0].getMaxBoundVertexBuffers();
  495. if (index < 0 || (index + numBuffers) >= maxBoundVertexBuffers)
  496. {
  497. BS_EXCEPT(InvalidParametersException, "Invalid vertex index: " + toString(index) +
  498. ". Valid range is 0 .. " + toString(maxBoundVertexBuffers - 1));
  499. }
  500. ID3D11Buffer* dx11buffers[BS_MAX_BOUND_VERTEX_BUFFERS];
  501. UINT32 strides[BS_MAX_BOUND_VERTEX_BUFFERS];
  502. UINT32 offsets[BS_MAX_BOUND_VERTEX_BUFFERS];
  503. for (UINT32 i = 0; i < numBuffers; i++)
  504. {
  505. SPtr<D3D11VertexBufferCore> vertexBuffer = std::static_pointer_cast<D3D11VertexBufferCore>(buffers[i]);
  506. const VertexBufferProperties& vbProps = vertexBuffer->getProperties();
  507. dx11buffers[i] = vertexBuffer->getD3DVertexBuffer();
  508. strides[i] = vbProps.getVertexSize();
  509. offsets[i] = 0;
  510. }
  511. mDevice->getImmediateContext()->IASetVertexBuffers(index, numBuffers, dx11buffers, strides, offsets);
  512. };
  513. if (commandBuffer == nullptr)
  514. executeRef(index, buffers, numBuffers);
  515. else
  516. {
  517. auto execute = [=]() { executeRef(index, buffers, numBuffers); };
  518. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  519. cb->queueCommand(execute);
  520. }
  521. BS_INC_RENDER_STAT(NumVertexBufferBinds);
  522. }
  523. void D3D11RenderAPI::setIndexBuffer(const SPtr<IndexBufferCore>& buffer, const SPtr<CommandBuffer>& commandBuffer)
  524. {
  525. auto executeRef = [&](const SPtr<IndexBufferCore>& buffer)
  526. {
  527. THROW_IF_NOT_CORE_THREAD;
  528. SPtr<D3D11IndexBufferCore> indexBuffer = std::static_pointer_cast<D3D11IndexBufferCore>(buffer);
  529. DXGI_FORMAT indexFormat = DXGI_FORMAT_R16_UINT;
  530. if (indexBuffer->getProperties().getType() == IT_16BIT)
  531. indexFormat = DXGI_FORMAT_R16_UINT;
  532. else if (indexBuffer->getProperties().getType() == IT_32BIT)
  533. indexFormat = DXGI_FORMAT_R32_UINT;
  534. else
  535. BS_EXCEPT(InternalErrorException, "Unsupported index format: " + toString(indexBuffer->getProperties().getType()));
  536. mDevice->getImmediateContext()->IASetIndexBuffer(indexBuffer->getD3DIndexBuffer(), indexFormat, 0);
  537. };
  538. if (commandBuffer == nullptr)
  539. executeRef(buffer);
  540. else
  541. {
  542. auto execute = [=]() { executeRef(buffer); };
  543. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  544. cb->queueCommand(execute);
  545. }
  546. BS_INC_RENDER_STAT(NumIndexBufferBinds);
  547. }
  548. void D3D11RenderAPI::setVertexDeclaration(const SPtr<VertexDeclarationCore>& vertexDeclaration,
  549. const SPtr<CommandBuffer>& commandBuffer)
  550. {
  551. auto executeRef = [&](const SPtr<VertexDeclarationCore>& vertexDeclaration)
  552. {
  553. THROW_IF_NOT_CORE_THREAD;
  554. mActiveVertexDeclaration = vertexDeclaration;
  555. };
  556. if (commandBuffer == nullptr)
  557. executeRef(vertexDeclaration);
  558. else
  559. {
  560. auto execute = [=]() { executeRef(vertexDeclaration); };
  561. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  562. cb->queueCommand(execute);
  563. }
  564. }
  565. void D3D11RenderAPI::setDrawOperation(DrawOperationType op, const SPtr<CommandBuffer>& commandBuffer)
  566. {
  567. auto executeRef = [&](DrawOperationType op)
  568. {
  569. THROW_IF_NOT_CORE_THREAD;
  570. mDevice->getImmediateContext()->IASetPrimitiveTopology(D3D11Mappings::getPrimitiveType(op));
  571. mActiveDrawOp = op;
  572. };
  573. if (commandBuffer == nullptr)
  574. executeRef(op);
  575. else
  576. {
  577. auto execute = [=]() { executeRef(op); };
  578. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  579. cb->queueCommand(execute);
  580. cb->mActiveDrawOp = op;
  581. }
  582. }
  583. void D3D11RenderAPI::draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount,
  584. const SPtr<CommandBuffer>& commandBuffer)
  585. {
  586. auto executeRef = [&](UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount)
  587. {
  588. THROW_IF_NOT_CORE_THREAD;
  589. applyInputLayout();
  590. if (instanceCount <= 1)
  591. mDevice->getImmediateContext()->Draw(vertexCount, vertexOffset);
  592. else
  593. mDevice->getImmediateContext()->DrawInstanced(vertexCount, instanceCount, vertexOffset, 0);
  594. #if BS_DEBUG_MODE
  595. if (mDevice->hasError())
  596. LOGWRN(mDevice->getErrorDescription());
  597. #endif
  598. };
  599. UINT32 primCount;
  600. if (commandBuffer == nullptr)
  601. {
  602. executeRef(vertexOffset, vertexCount, instanceCount);
  603. primCount = vertexCountToPrimCount(mActiveDrawOp, vertexCount);
  604. }
  605. else
  606. {
  607. auto execute = [=]() { executeRef(vertexOffset, vertexCount, instanceCount); };
  608. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  609. cb->queueCommand(execute);
  610. primCount = vertexCountToPrimCount(cb->mActiveDrawOp, vertexCount);
  611. }
  612. BS_INC_RENDER_STAT(NumDrawCalls);
  613. BS_ADD_RENDER_STAT(NumVertices, vertexCount);
  614. BS_ADD_RENDER_STAT(NumPrimitives, primCount);
  615. }
  616. void D3D11RenderAPI::drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount,
  617. UINT32 instanceCount, const SPtr<CommandBuffer>& commandBuffer)
  618. {
  619. auto executeRef = [&](UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount,
  620. UINT32 instanceCount)
  621. {
  622. THROW_IF_NOT_CORE_THREAD;
  623. applyInputLayout();
  624. if (instanceCount <= 1)
  625. mDevice->getImmediateContext()->DrawIndexed(indexCount, startIndex, vertexOffset);
  626. else
  627. mDevice->getImmediateContext()->DrawIndexedInstanced(indexCount, instanceCount, startIndex, vertexOffset, 0);
  628. #if BS_DEBUG_MODE
  629. if (mDevice->hasError())
  630. LOGWRN(mDevice->getErrorDescription());
  631. #endif
  632. };
  633. UINT32 primCount;
  634. if (commandBuffer == nullptr)
  635. {
  636. executeRef(startIndex, indexCount, vertexOffset, vertexCount, instanceCount);
  637. primCount = vertexCountToPrimCount(mActiveDrawOp, vertexCount);
  638. }
  639. else
  640. {
  641. auto execute = [=]() { executeRef(startIndex, indexCount, vertexOffset, vertexCount, instanceCount); };
  642. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  643. cb->queueCommand(execute);
  644. primCount = vertexCountToPrimCount(cb->mActiveDrawOp, vertexCount);
  645. }
  646. BS_INC_RENDER_STAT(NumDrawCalls);
  647. BS_ADD_RENDER_STAT(NumVertices, vertexCount);
  648. BS_ADD_RENDER_STAT(NumPrimitives, primCount);
  649. }
  650. void D3D11RenderAPI::dispatchCompute(UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ,
  651. const SPtr<CommandBuffer>& commandBuffer)
  652. {
  653. auto executeRef = [&](UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ)
  654. {
  655. THROW_IF_NOT_CORE_THREAD;
  656. mDevice->getImmediateContext()->Dispatch(numGroupsX, numGroupsY, numGroupsZ);
  657. #if BS_DEBUG_MODE
  658. if (mDevice->hasError())
  659. LOGWRN(mDevice->getErrorDescription());
  660. #endif
  661. };
  662. if (commandBuffer == nullptr)
  663. executeRef(numGroupsX, numGroupsY, numGroupsZ);
  664. else
  665. {
  666. auto execute = [=]() { executeRef(numGroupsX, numGroupsY, numGroupsZ); };
  667. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  668. cb->queueCommand(execute);
  669. }
  670. BS_INC_RENDER_STAT(NumComputeCalls);
  671. }
  672. void D3D11RenderAPI::setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom,
  673. const SPtr<CommandBuffer>& commandBuffer)
  674. {
  675. auto executeRef = [&](UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
  676. {
  677. THROW_IF_NOT_CORE_THREAD;
  678. mScissorRect.left = static_cast<LONG>(left);
  679. mScissorRect.top = static_cast<LONG>(top);
  680. mScissorRect.bottom = static_cast<LONG>(bottom);
  681. mScissorRect.right = static_cast<LONG>(right);
  682. mDevice->getImmediateContext()->RSSetScissorRects(1, &mScissorRect);
  683. };
  684. if (commandBuffer == nullptr)
  685. executeRef(left, top, right, bottom);
  686. else
  687. {
  688. auto execute = [=]() { executeRef(left, top, right, bottom); };
  689. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  690. cb->queueCommand(execute);
  691. }
  692. }
  693. void D3D11RenderAPI::setStencilRef(UINT32 value, const SPtr<CommandBuffer>& commandBuffer)
  694. {
  695. auto executeRef = [&](UINT32 value)
  696. {
  697. THROW_IF_NOT_CORE_THREAD;
  698. mStencilRef = value;
  699. if(mActiveDepthStencilState != nullptr)
  700. mDevice->getImmediateContext()->OMSetDepthStencilState(mActiveDepthStencilState->getInternal(), mStencilRef);
  701. else
  702. mDevice->getImmediateContext()->OMSetDepthStencilState(nullptr, mStencilRef);
  703. };
  704. if (commandBuffer == nullptr)
  705. executeRef(value);
  706. else
  707. {
  708. auto execute = [=]() { executeRef(value); };
  709. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  710. cb->queueCommand(execute);
  711. }
  712. }
  713. void D3D11RenderAPI::clearViewport(UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask,
  714. const SPtr<CommandBuffer>& commandBuffer)
  715. {
  716. auto executeRef = [&](UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask)
  717. {
  718. THROW_IF_NOT_CORE_THREAD;
  719. if (mActiveRenderTarget == nullptr)
  720. return;
  721. const RenderTargetProperties& rtProps = mActiveRenderTarget->getProperties();
  722. Rect2I clearArea((int)mViewport.TopLeftX, (int)mViewport.TopLeftY, (int)mViewport.Width, (int)mViewport.Height);
  723. bool clearEntireTarget = clearArea.width == 0 || clearArea.height == 0;
  724. clearEntireTarget |= (clearArea.x == 0 && clearArea.y == 0 && clearArea.width == rtProps.getWidth() &&
  725. clearArea.height == rtProps.getHeight());
  726. if (!clearEntireTarget)
  727. {
  728. // TODO - Ignoring targetMask here
  729. D3D11RenderUtility::instance().drawClearQuad(buffers, color, depth, stencil);
  730. BS_INC_RENDER_STAT(NumClears);
  731. }
  732. else
  733. clearRenderTarget(buffers, color, depth, stencil, targetMask);
  734. };
  735. if (commandBuffer == nullptr)
  736. executeRef(buffers, color, depth, stencil, targetMask);
  737. else
  738. {
  739. auto execute = [=]() { executeRef(buffers, color, depth, stencil, targetMask); };
  740. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  741. cb->queueCommand(execute);
  742. }
  743. }
  744. void D3D11RenderAPI::clearRenderTarget(UINT32 buffers, const Color& color, float depth, UINT16 stencil,
  745. UINT8 targetMask, const SPtr<CommandBuffer>& commandBuffer)
  746. {
  747. auto executeRef = [&](UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask)
  748. {
  749. THROW_IF_NOT_CORE_THREAD;
  750. if (mActiveRenderTarget == nullptr)
  751. return;
  752. // Clear render surfaces
  753. if (buffers & FBT_COLOR)
  754. {
  755. UINT32 maxRenderTargets = mCurrentCapabilities[0].getNumMultiRenderTargets();
  756. ID3D11RenderTargetView** views = bs_newN<ID3D11RenderTargetView*>(maxRenderTargets);
  757. memset(views, 0, sizeof(ID3D11RenderTargetView*) * maxRenderTargets);
  758. mActiveRenderTarget->getCustomAttribute("RTV", views);
  759. if (!views[0])
  760. {
  761. bs_deleteN(views, maxRenderTargets);
  762. return;
  763. }
  764. float clearColor[4];
  765. clearColor[0] = color.r;
  766. clearColor[1] = color.g;
  767. clearColor[2] = color.b;
  768. clearColor[3] = color.a;
  769. for (UINT32 i = 0; i < maxRenderTargets; i++)
  770. {
  771. if (views[i] != nullptr && ((1 << i) & targetMask) != 0)
  772. mDevice->getImmediateContext()->ClearRenderTargetView(views[i], clearColor);
  773. }
  774. bs_deleteN(views, maxRenderTargets);
  775. }
  776. // Clear depth stencil
  777. if ((buffers & FBT_DEPTH) != 0 || (buffers & FBT_STENCIL) != 0)
  778. {
  779. ID3D11DepthStencilView* depthStencilView = nullptr;
  780. mActiveRenderTarget->getCustomAttribute("DSV", &depthStencilView);
  781. D3D11_CLEAR_FLAG clearFlag;
  782. if ((buffers & FBT_DEPTH) != 0 && (buffers & FBT_STENCIL) != 0)
  783. clearFlag = (D3D11_CLEAR_FLAG)(D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL);
  784. else if ((buffers & FBT_STENCIL) != 0)
  785. clearFlag = D3D11_CLEAR_STENCIL;
  786. else
  787. clearFlag = D3D11_CLEAR_DEPTH;
  788. if (depthStencilView != nullptr)
  789. mDevice->getImmediateContext()->ClearDepthStencilView(depthStencilView, clearFlag, depth, (UINT8)stencil);
  790. }
  791. };
  792. if (commandBuffer == nullptr)
  793. executeRef(buffers, color, depth, stencil, targetMask);
  794. else
  795. {
  796. auto execute = [=]() { executeRef(buffers, color, depth, stencil, targetMask); };
  797. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  798. cb->queueCommand(execute);
  799. }
  800. BS_INC_RENDER_STAT(NumClears);
  801. }
  802. void D3D11RenderAPI::setRenderTarget(const SPtr<RenderTargetCore>& target, bool readOnlyDepthStencil,
  803. RenderSurfaceMask loadMask, const SPtr<CommandBuffer>& commandBuffer)
  804. {
  805. auto executeRef = [&](const SPtr<RenderTargetCore>& target, bool readOnlyDepthStencil)
  806. {
  807. THROW_IF_NOT_CORE_THREAD;
  808. mActiveRenderTarget = target;
  809. UINT32 maxRenderTargets = mCurrentCapabilities[0].getNumMultiRenderTargets();
  810. ID3D11RenderTargetView** views = bs_newN<ID3D11RenderTargetView*>(maxRenderTargets);
  811. memset(views, 0, sizeof(ID3D11RenderTargetView*) * maxRenderTargets);
  812. ID3D11DepthStencilView* depthStencilView = nullptr;
  813. if (target != nullptr)
  814. {
  815. target->getCustomAttribute("RTV", views);
  816. if (readOnlyDepthStencil)
  817. target->getCustomAttribute("RODSV", &depthStencilView);
  818. else
  819. target->getCustomAttribute("DSV", &depthStencilView);
  820. }
  821. // Bind render targets
  822. mDevice->getImmediateContext()->OMSetRenderTargets(maxRenderTargets, views, depthStencilView);
  823. if (mDevice->hasError())
  824. BS_EXCEPT(RenderingAPIException, "Failed to setRenderTarget : " + mDevice->getErrorDescription());
  825. bs_deleteN(views, maxRenderTargets);
  826. applyViewport();
  827. };
  828. if (commandBuffer == nullptr)
  829. executeRef(target, readOnlyDepthStencil);
  830. else
  831. {
  832. auto execute = [=]() { executeRef(target, readOnlyDepthStencil); };
  833. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  834. cb->queueCommand(execute);
  835. }
  836. BS_INC_RENDER_STAT(NumRenderTargetChanges);
  837. }
  838. void D3D11RenderAPI::swapBuffers(const SPtr<RenderTargetCore>& target, UINT32 syncMask)
  839. {
  840. THROW_IF_NOT_CORE_THREAD;
  841. target->swapBuffers();
  842. BS_INC_RENDER_STAT(NumPresents);
  843. }
  844. void D3D11RenderAPI::addCommands(const SPtr<CommandBuffer>& commandBuffer, const SPtr<CommandBuffer>& secondary)
  845. {
  846. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  847. SPtr<D3D11CommandBuffer> secondaryCb = std::static_pointer_cast<D3D11CommandBuffer>(secondary);
  848. cb->appendSecondary(secondaryCb);
  849. }
  850. void D3D11RenderAPI::submitCommandBuffer(const SPtr<CommandBuffer>& commandBuffer, UINT32 syncMask)
  851. {
  852. SPtr<D3D11CommandBuffer> cb = std::static_pointer_cast<D3D11CommandBuffer>(commandBuffer);
  853. cb->executeCommands();
  854. cb->clear();
  855. }
  856. void D3D11RenderAPI::applyViewport()
  857. {
  858. if (mActiveRenderTarget == nullptr)
  859. return;
  860. const RenderTargetProperties& rtProps = mActiveRenderTarget->getProperties();
  861. // Set viewport dimensions
  862. mViewport.TopLeftX = (FLOAT)(rtProps.getWidth() * mViewportNorm.x);
  863. mViewport.TopLeftY = (FLOAT)(rtProps.getHeight() * mViewportNorm.y);
  864. mViewport.Width = (FLOAT)(rtProps.getWidth() * mViewportNorm.width);
  865. mViewport.Height = (FLOAT)(rtProps.getHeight() * mViewportNorm.height);
  866. if (rtProps.requiresTextureFlipping())
  867. {
  868. // Convert "top-left" to "bottom-left"
  869. mViewport.TopLeftY = rtProps.getHeight() - mViewport.Height - mViewport.TopLeftY;
  870. }
  871. mViewport.MinDepth = 0.0f;
  872. mViewport.MaxDepth = 1.0f;
  873. mDevice->getImmediateContext()->RSSetViewports(1, &mViewport);
  874. }
  875. void D3D11RenderAPI::initCapabilites(IDXGIAdapter* adapter, RenderAPICapabilities& caps) const
  876. {
  877. THROW_IF_NOT_CORE_THREAD;
  878. // This must query for DirectX 10 interface as this is unsupported for DX11
  879. LARGE_INTEGER driverVersionNum;
  880. DriverVersion driverVersion;
  881. if (SUCCEEDED(adapter->CheckInterfaceSupport(IID_ID3D10Device, &driverVersionNum)))
  882. {
  883. driverVersion.major = HIWORD(driverVersionNum.HighPart);
  884. driverVersion.minor = LOWORD(driverVersionNum.HighPart);
  885. driverVersion.release = HIWORD(driverVersionNum.LowPart);
  886. driverVersion.build = LOWORD(driverVersionNum.LowPart);
  887. }
  888. caps.setDriverVersion(driverVersion);
  889. caps.setDeviceName(mActiveD3DDriver->getDriverDescription());
  890. caps.setRenderAPIName(getName());
  891. caps.setCapability(RSC_TEXTURE_COMPRESSION_BC);
  892. caps.addShaderProfile("hlsl");
  893. if(mFeatureLevel >= D3D_FEATURE_LEVEL_10_1)
  894. caps.setMaxBoundVertexBuffers(32);
  895. else
  896. caps.setMaxBoundVertexBuffers(16);
  897. if(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0)
  898. {
  899. caps.setCapability(RSC_GEOMETRY_PROGRAM);
  900. caps.setNumTextureUnits(GPT_FRAGMENT_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  901. caps.setNumTextureUnits(GPT_VERTEX_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  902. caps.setNumTextureUnits(GPT_GEOMETRY_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  903. caps.setNumCombinedTextureUnits(caps.getNumTextureUnits(GPT_FRAGMENT_PROGRAM)
  904. + caps.getNumTextureUnits(GPT_VERTEX_PROGRAM) + caps.getNumTextureUnits(GPT_GEOMETRY_PROGRAM));
  905. caps.setNumGpuParamBlockBuffers(GPT_FRAGMENT_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  906. caps.setNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  907. caps.setNumGpuParamBlockBuffers(GPT_GEOMETRY_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  908. caps.setNumCombinedGpuParamBlockBuffers(caps.getNumGpuParamBlockBuffers(GPT_FRAGMENT_PROGRAM)
  909. + caps.getNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM) + caps.getNumGpuParamBlockBuffers(GPT_GEOMETRY_PROGRAM));
  910. }
  911. if(mFeatureLevel >= D3D_FEATURE_LEVEL_11_0)
  912. {
  913. caps.setCapability(RSC_TESSELLATION_PROGRAM);
  914. caps.setCapability(RSC_COMPUTE_PROGRAM);
  915. caps.setNumTextureUnits(GPT_HULL_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_REGISTER_COUNT);
  916. caps.setNumTextureUnits(GPT_DOMAIN_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_REGISTER_COUNT);
  917. caps.setNumTextureUnits(GPT_COMPUTE_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_REGISTER_COUNT);
  918. caps.setNumCombinedTextureUnits(caps.getNumTextureUnits(GPT_FRAGMENT_PROGRAM)
  919. + caps.getNumTextureUnits(GPT_VERTEX_PROGRAM) + caps.getNumTextureUnits(GPT_GEOMETRY_PROGRAM)
  920. + caps.getNumTextureUnits(GPT_HULL_PROGRAM) + caps.getNumTextureUnits(GPT_DOMAIN_PROGRAM)
  921. + caps.getNumTextureUnits(GPT_COMPUTE_PROGRAM));
  922. caps.setNumGpuParamBlockBuffers(GPT_HULL_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  923. caps.setNumGpuParamBlockBuffers(GPT_DOMAIN_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  924. caps.setNumGpuParamBlockBuffers(GPT_COMPUTE_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  925. caps.setNumCombinedGpuParamBlockBuffers(caps.getNumGpuParamBlockBuffers(GPT_FRAGMENT_PROGRAM)
  926. + caps.getNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM) + caps.getNumGpuParamBlockBuffers(GPT_GEOMETRY_PROGRAM)
  927. + caps.getNumGpuParamBlockBuffers(GPT_HULL_PROGRAM) + caps.getNumGpuParamBlockBuffers(GPT_DOMAIN_PROGRAM)
  928. + caps.getNumGpuParamBlockBuffers(GPT_COMPUTE_PROGRAM));
  929. caps.setNumLoadStoreTextureUnits(GPT_FRAGMENT_PROGRAM, D3D11_PS_CS_UAV_REGISTER_COUNT);
  930. caps.setNumLoadStoreTextureUnits(GPT_COMPUTE_PROGRAM, D3D11_PS_CS_UAV_REGISTER_COUNT);
  931. caps.setNumCombinedLoadStoreTextureUnits(caps.getNumLoadStoreTextureUnits(GPT_FRAGMENT_PROGRAM)
  932. + caps.getNumLoadStoreTextureUnits(GPT_COMPUTE_PROGRAM));
  933. }
  934. // Adapter details
  935. const DXGI_ADAPTER_DESC& adapterID = mActiveD3DDriver->getAdapterIdentifier();
  936. // Determine vendor
  937. switch(adapterID.VendorId)
  938. {
  939. case 0x10DE:
  940. caps.setVendor(GPU_NVIDIA);
  941. break;
  942. case 0x1002:
  943. caps.setVendor(GPU_AMD);
  944. break;
  945. case 0x163C:
  946. case 0x8086:
  947. caps.setVendor(GPU_INTEL);
  948. break;
  949. default:
  950. caps.setVendor(GPU_UNKNOWN);
  951. break;
  952. };
  953. caps.setNumMultiRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT);
  954. }
  955. void D3D11RenderAPI::determineMultisampleSettings(UINT32 multisampleCount, DXGI_FORMAT format, DXGI_SAMPLE_DESC* outputSampleDesc)
  956. {
  957. bool tryCSAA = false; // Note: Disabled for now, but leaving the code for later so it might be useful
  958. enum CSAAMode { CSAA_Normal, CSAA_Quality };
  959. CSAAMode csaaMode = CSAA_Normal;
  960. bool foundValid = false;
  961. size_t origNumSamples = multisampleCount;
  962. while (!foundValid)
  963. {
  964. // Deal with special cases
  965. if (tryCSAA)
  966. {
  967. switch(multisampleCount)
  968. {
  969. case 8:
  970. if (csaaMode == CSAA_Quality)
  971. {
  972. outputSampleDesc->Count = 8;
  973. outputSampleDesc->Quality = 8;
  974. }
  975. else
  976. {
  977. outputSampleDesc->Count = 4;
  978. outputSampleDesc->Quality = 8;
  979. }
  980. break;
  981. case 16:
  982. if (csaaMode == CSAA_Quality)
  983. {
  984. outputSampleDesc->Count = 8;
  985. outputSampleDesc->Quality = 16;
  986. }
  987. else
  988. {
  989. outputSampleDesc->Count = 4;
  990. outputSampleDesc->Quality = 16;
  991. }
  992. break;
  993. }
  994. }
  995. else // !CSAA
  996. {
  997. outputSampleDesc->Count = multisampleCount == 0 ? 1 : multisampleCount;
  998. outputSampleDesc->Quality = D3D11_STANDARD_MULTISAMPLE_PATTERN;
  999. }
  1000. HRESULT hr;
  1001. UINT outQuality;
  1002. hr = mDevice->getD3D11Device()->CheckMultisampleQualityLevels(format, outputSampleDesc->Count, &outQuality);
  1003. if (SUCCEEDED(hr) && (!tryCSAA || outQuality > outputSampleDesc->Quality))
  1004. {
  1005. foundValid = true;
  1006. }
  1007. else
  1008. {
  1009. // Downgrade
  1010. if (tryCSAA && multisampleCount == 8)
  1011. {
  1012. // For CSAA, we'll try downgrading with quality mode at all samples.
  1013. // then try without quality, then drop CSAA
  1014. if (csaaMode == CSAA_Quality)
  1015. {
  1016. // Drop quality first
  1017. csaaMode = CSAA_Normal;
  1018. }
  1019. else
  1020. {
  1021. // Drop CSAA entirely
  1022. tryCSAA = false;
  1023. }
  1024. // Return to original requested samples
  1025. multisampleCount = static_cast<UINT32>(origNumSamples);
  1026. }
  1027. else
  1028. {
  1029. // Drop samples
  1030. multisampleCount--;
  1031. if (multisampleCount == 1)
  1032. {
  1033. // Ran out of options, no multisampling
  1034. multisampleCount = 0;
  1035. foundValid = true;
  1036. }
  1037. }
  1038. }
  1039. }
  1040. }
  1041. void D3D11RenderAPI::convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest)
  1042. {
  1043. dest = matrix;
  1044. // Convert depth range from [-1,+1] to [0,1]
  1045. dest[2][0] = (dest[2][0] + dest[3][0]) / 2;
  1046. dest[2][1] = (dest[2][1] + dest[3][1]) / 2;
  1047. dest[2][2] = (dest[2][2] + dest[3][2]) / 2;
  1048. dest[2][3] = (dest[2][3] + dest[3][3]) / 2;
  1049. }
  1050. const RenderAPIInfo& D3D11RenderAPI::getAPIInfo() const
  1051. {
  1052. static RenderAPIInfo info(0.0f, 0.0f, 0.0f, 1.0f, VET_COLOR_ABGR, false, false, false, false, false);
  1053. return info;
  1054. }
  1055. GpuParamBlockDesc D3D11RenderAPI::generateParamBlockDesc(const String& name, Vector<GpuParamDataDesc>& params)
  1056. {
  1057. GpuParamBlockDesc block;
  1058. block.blockSize = 0;
  1059. block.isShareable = true;
  1060. block.name = name;
  1061. block.slot = 0;
  1062. block.set = 0;
  1063. for (auto& param : params)
  1064. {
  1065. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[param.type];
  1066. UINT32 size = typeInfo.size / 4;
  1067. if (param.arraySize > 1)
  1068. {
  1069. // Arrays perform no packing and their elements are always padded and aligned to four component vectors
  1070. UINT32 alignOffset = size % typeInfo.baseTypeSize;
  1071. if (alignOffset != 0)
  1072. {
  1073. UINT32 padding = (typeInfo.baseTypeSize - alignOffset);
  1074. size += padding;
  1075. }
  1076. alignOffset = block.blockSize % typeInfo.baseTypeSize;
  1077. if (alignOffset != 0)
  1078. {
  1079. UINT32 padding = (typeInfo.baseTypeSize - alignOffset);
  1080. block.blockSize += padding;
  1081. }
  1082. param.elementSize = size;
  1083. param.arrayElementStride = size;
  1084. param.cpuMemOffset = block.blockSize;
  1085. param.gpuMemOffset = 0;
  1086. block.blockSize += size * param.arraySize;
  1087. }
  1088. else
  1089. {
  1090. // Pack everything as tightly as possible as long as the data doesn't cross 16 byte boundary
  1091. UINT32 alignOffset = block.blockSize % 4;
  1092. if (alignOffset != 0 && size > (4 - alignOffset))
  1093. {
  1094. UINT32 padding = (4 - alignOffset);
  1095. block.blockSize += padding;
  1096. }
  1097. param.elementSize = size;
  1098. param.arrayElementStride = size;
  1099. param.cpuMemOffset = block.blockSize;
  1100. param.gpuMemOffset = 0;
  1101. block.blockSize += size;
  1102. }
  1103. param.paramBlockSlot = 0;
  1104. param.paramBlockSet = 0;
  1105. }
  1106. // Constant buffer size must always be a multiple of 16
  1107. if (block.blockSize % 4 != 0)
  1108. block.blockSize += (4 - (block.blockSize % 4));
  1109. return block;
  1110. }
  1111. /************************************************************************/
  1112. /* PRIVATE */
  1113. /************************************************************************/
  1114. void D3D11RenderAPI::applyInputLayout()
  1115. {
  1116. if(mActiveVertexDeclaration == nullptr)
  1117. {
  1118. LOGWRN("Cannot apply input layout without a vertex declaration. Set vertex declaration before calling this method.");
  1119. return;
  1120. }
  1121. if(mActiveVertexShader == nullptr)
  1122. {
  1123. LOGWRN("Cannot apply input layout without a vertex shader. Set vertex shader before calling this method.");
  1124. return;
  1125. }
  1126. ID3D11InputLayout* ia = mIAManager->retrieveInputLayout(mActiveVertexShader->getInputDeclaration(), mActiveVertexDeclaration, *mActiveVertexShader);
  1127. mDevice->getImmediateContext()->IASetInputLayout(ia);
  1128. }
  1129. }