BsD3D11RenderAPI.cpp 45 KB

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