BsD3D11RenderAPI.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. #include "BsD3D11RenderAPI.h"
  2. #include "BsD3D11DriverList.h"
  3. #include "BsD3D11Driver.h"
  4. #include "BsD3D11Device.h"
  5. #include "BsD3D11TextureManager.h"
  6. #include "BsD3D11Texture.h"
  7. #include "BsD3D11HardwareBufferManager.h"
  8. #include "BsD3D11RenderWindowManager.h"
  9. #include "BsD3D11HLSLProgramFactory.h"
  10. #include "BsD3D11BlendState.h"
  11. #include "BsD3D11RasterizerState.h"
  12. #include "BsD3D11DepthStencilState.h"
  13. #include "BsD3D11SamplerState.h"
  14. #include "BsD3D11GpuProgram.h"
  15. #include "BsD3D11Mappings.h"
  16. #include "BsD3D11VertexBuffer.h"
  17. #include "BsD3D11IndexBuffer.h"
  18. #include "BsD3D11RenderStateManager.h"
  19. #include "BsD3D11GpuParamBlockBuffer.h"
  20. #include "BsD3D11InputLayoutManager.h"
  21. #include "BsD3D11TextureView.h"
  22. #include "BsD3D11RenderUtility.h"
  23. #include "BsGpuParams.h"
  24. #include "BsCoreThread.h"
  25. #include "BsD3D11QueryManager.h"
  26. #include "BsDebug.h"
  27. #include "BsException.h"
  28. #include "BsRenderStats.h"
  29. #include "BsGpuParamDesc.h"
  30. namespace BansheeEngine
  31. {
  32. D3D11RenderAPI::D3D11RenderAPI()
  33. : mDXGIFactory(nullptr), mDevice(nullptr), mDriverList(nullptr)
  34. , mActiveD3DDriver(nullptr), mFeatureLevel(D3D_FEATURE_LEVEL_11_0)
  35. , mHLSLFactory(nullptr), mIAManager(nullptr)
  36. , mStencilRef(0), mActiveDrawOp(DOT_TRIANGLE_LIST)
  37. , mViewportNorm(0.0f, 0.0f, 1.0f, 1.0f)
  38. {
  39. mClipPlanesDirty = false; // DX11 handles clip planes through shaders
  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::initializePrepare()
  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. // This must query for DirectX 10 interface as this is unsupported for DX11
  84. LARGE_INTEGER driverVersion;
  85. if(SUCCEEDED(selectedAdapter->CheckInterfaceSupport(IID_ID3D10Device, &driverVersion)))
  86. {
  87. mDriverVersion.major = HIWORD(driverVersion.HighPart);
  88. mDriverVersion.minor = LOWORD(driverVersion.HighPart);
  89. mDriverVersion.release = HIWORD(driverVersion.LowPart);
  90. mDriverVersion.build = LOWORD(driverVersion.LowPart);
  91. }
  92. // Create the texture manager for use by others
  93. TextureManager::startUp<D3D11TextureManager>();
  94. TextureCoreManager::startUp<D3D11TextureCoreManager>();
  95. // Create hardware buffer manager
  96. HardwareBufferManager::startUp();
  97. HardwareBufferCoreManager::startUp<D3D11HardwareBufferCoreManager>(std::ref(*mDevice));
  98. // Create render window manager
  99. RenderWindowManager::startUp<D3D11RenderWindowManager>(this);
  100. RenderWindowCoreManager::startUp<D3D11RenderWindowCoreManager>(this);
  101. // Create & register HLSL factory
  102. mHLSLFactory = bs_new<D3D11HLSLProgramFactory>();
  103. // Create render state manager
  104. RenderStateCoreManager::startUp<D3D11RenderStateCoreManager>();
  105. mCurrentCapabilities = createRenderSystemCapabilities();
  106. mCurrentCapabilities->addShaderProfile("hlsl");
  107. GpuProgramCoreManager::instance().addFactory(mHLSLFactory);
  108. mIAManager = bs_new<D3D11InputLayoutManager>();
  109. RenderAPICore::initializePrepare();
  110. }
  111. void D3D11RenderAPI::initializeFinalize(const SPtr<RenderWindowCore>& primaryWindow)
  112. {
  113. D3D11RenderUtility::startUp(mDevice);
  114. QueryManager::startUp<D3D11QueryManager>();
  115. RenderAPICore::initializeFinalize(primaryWindow);
  116. }
  117. void D3D11RenderAPI::destroyCore()
  118. {
  119. THROW_IF_NOT_CORE_THREAD;
  120. for (auto& boundUAV : mBoundUAVs)
  121. {
  122. if (boundUAV.second != nullptr)
  123. boundUAV.first->releaseView(boundUAV.second);
  124. }
  125. QueryManager::shutDown();
  126. D3D11RenderUtility::shutDown();
  127. if(mIAManager != nullptr)
  128. {
  129. bs_delete(mIAManager);
  130. mIAManager = nullptr;
  131. }
  132. if(mHLSLFactory != nullptr)
  133. {
  134. bs_delete(mHLSLFactory);
  135. mHLSLFactory = nullptr;
  136. }
  137. mActiveVertexDeclaration = nullptr;
  138. mActiveVertexShader = nullptr;
  139. mActiveRenderTarget = nullptr;
  140. RenderStateCoreManager::shutDown();
  141. RenderWindowCoreManager::shutDown();
  142. RenderWindowManager::shutDown();
  143. HardwareBufferCoreManager::shutDown();
  144. HardwareBufferManager::shutDown();
  145. TextureCoreManager::shutDown();
  146. TextureManager::shutDown();
  147. SAFE_RELEASE(mDXGIFactory);
  148. if(mDevice != nullptr)
  149. {
  150. bs_delete(mDevice);
  151. mDevice = nullptr;
  152. }
  153. if(mDriverList != nullptr)
  154. {
  155. bs_delete(mDriverList);
  156. mDriverList = nullptr;
  157. }
  158. mActiveD3DDriver = nullptr;
  159. RenderAPICore::destroyCore();
  160. }
  161. void D3D11RenderAPI::setSamplerState(GpuProgramType gptype, UINT16 texUnit, const SPtr<SamplerStateCore>& samplerState)
  162. {
  163. THROW_IF_NOT_CORE_THREAD;
  164. // TODO - I'm setting up views one by one, it might be more efficient to hold them in an array
  165. // and then set them all up at once before rendering? Needs testing
  166. ID3D11SamplerState* samplerArray[1];
  167. D3D11SamplerStateCore* d3d11SamplerState = static_cast<D3D11SamplerStateCore*>(const_cast<SamplerStateCore*>(samplerState.get()));
  168. samplerArray[0] = d3d11SamplerState->getInternal();
  169. switch(gptype)
  170. {
  171. case GPT_VERTEX_PROGRAM:
  172. mDevice->getImmediateContext()->VSSetSamplers(texUnit, 1, samplerArray);
  173. break;
  174. case GPT_FRAGMENT_PROGRAM:
  175. mDevice->getImmediateContext()->PSSetSamplers(texUnit, 1, samplerArray);
  176. break;
  177. case GPT_GEOMETRY_PROGRAM:
  178. mDevice->getImmediateContext()->GSSetSamplers(texUnit, 1, samplerArray);
  179. break;
  180. case GPT_DOMAIN_PROGRAM:
  181. mDevice->getImmediateContext()->DSSetSamplers(texUnit, 1, samplerArray);
  182. break;
  183. case GPT_HULL_PROGRAM:
  184. mDevice->getImmediateContext()->HSSetSamplers(texUnit, 1, samplerArray);
  185. break;
  186. case GPT_COMPUTE_PROGRAM:
  187. mDevice->getImmediateContext()->CSSetSamplers(texUnit, 1, samplerArray);
  188. break;
  189. default:
  190. BS_EXCEPT(InvalidParametersException, "Unsupported gpu program type: " + toString(gptype));
  191. }
  192. BS_INC_RENDER_STAT(NumSamplerBinds);
  193. }
  194. void D3D11RenderAPI::setBlendState(const SPtr<BlendStateCore>& blendState)
  195. {
  196. THROW_IF_NOT_CORE_THREAD;
  197. D3D11BlendStateCore* d3d11BlendState = static_cast<D3D11BlendStateCore*>(const_cast<BlendStateCore*>(blendState.get()));
  198. mDevice->getImmediateContext()->OMSetBlendState(d3d11BlendState->getInternal(), nullptr, 0xFFFFFFFF);
  199. BS_INC_RENDER_STAT(NumBlendStateChanges);
  200. }
  201. void D3D11RenderAPI::setRasterizerState(const SPtr<RasterizerStateCore>& rasterizerState)
  202. {
  203. THROW_IF_NOT_CORE_THREAD;
  204. D3D11RasterizerStateCore* d3d11RasterizerState = static_cast<D3D11RasterizerStateCore*>(const_cast<RasterizerStateCore*>(rasterizerState.get()));
  205. mDevice->getImmediateContext()->RSSetState(d3d11RasterizerState->getInternal());
  206. BS_INC_RENDER_STAT(NumRasterizerStateChanges);
  207. }
  208. void D3D11RenderAPI::setDepthStencilState(const SPtr<DepthStencilStateCore>& depthStencilState, UINT32 stencilRefValue)
  209. {
  210. THROW_IF_NOT_CORE_THREAD;
  211. D3D11DepthStencilStateCore* d3d11RasterizerState = static_cast<D3D11DepthStencilStateCore*>(const_cast<DepthStencilStateCore*>(depthStencilState.get()));
  212. mDevice->getImmediateContext()->OMSetDepthStencilState(d3d11RasterizerState->getInternal(), stencilRefValue);
  213. BS_INC_RENDER_STAT(NumDepthStencilStateChanges);
  214. }
  215. void D3D11RenderAPI::setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const SPtr<TextureCore>& texPtr)
  216. {
  217. THROW_IF_NOT_CORE_THREAD;
  218. // TODO - I'm setting up views one by one, it might be more efficient to hold them in an array
  219. // and then set them all up at once before rendering? Needs testing
  220. ID3D11ShaderResourceView* viewArray[1];
  221. if(texPtr != nullptr && enabled)
  222. {
  223. D3D11TextureCore* d3d11Texture = static_cast<D3D11TextureCore*>(texPtr.get());
  224. viewArray[0] = d3d11Texture->getSRV();
  225. }
  226. else
  227. viewArray[0] = nullptr;
  228. switch(gptype)
  229. {
  230. case GPT_VERTEX_PROGRAM:
  231. mDevice->getImmediateContext()->VSSetShaderResources(unit, 1, viewArray);
  232. break;
  233. case GPT_FRAGMENT_PROGRAM:
  234. mDevice->getImmediateContext()->PSSetShaderResources(unit, 1, viewArray);
  235. break;
  236. case GPT_GEOMETRY_PROGRAM:
  237. mDevice->getImmediateContext()->GSSetShaderResources(unit, 1, viewArray);
  238. break;
  239. case GPT_DOMAIN_PROGRAM:
  240. mDevice->getImmediateContext()->DSSetShaderResources(unit, 1, viewArray);
  241. break;
  242. case GPT_HULL_PROGRAM:
  243. mDevice->getImmediateContext()->HSSetShaderResources(unit, 1, viewArray);
  244. break;
  245. case GPT_COMPUTE_PROGRAM:
  246. mDevice->getImmediateContext()->CSSetShaderResources(unit, 1, viewArray);
  247. break;
  248. default:
  249. BS_EXCEPT(InvalidParametersException, "Unsupported gpu program type: " + toString(gptype));
  250. }
  251. BS_INC_RENDER_STAT(NumTextureBinds);
  252. }
  253. void D3D11RenderAPI::setLoadStoreTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const SPtr<TextureCore>& texPtr,
  254. const TextureSurface& surface)
  255. {
  256. THROW_IF_NOT_CORE_THREAD;
  257. // TODO - This hasn't bee tested and might be incorrect. I might need to set UAVs together with render targets,
  258. // especially considering DX11 expects number of UAVs to match number of render targets.
  259. ID3D11UnorderedAccessView* viewArray[1];
  260. if (texPtr != nullptr && enabled)
  261. {
  262. D3D11TextureCore* d3d11Texture = static_cast<D3D11TextureCore*>(texPtr.get());
  263. TextureViewPtr texView = TextureCore::requestView(texPtr, surface.mipLevel, 1,
  264. surface.arraySlice, surface.numArraySlices, GVU_RANDOMWRITE);
  265. D3D11TextureView* d3d11texView = static_cast<D3D11TextureView*>(texView.get());
  266. viewArray[0] = d3d11texView->getUAV();
  267. if (mBoundUAVs[unit].second != nullptr)
  268. mBoundUAVs[unit].first->releaseView(mBoundUAVs[unit].second);
  269. mBoundUAVs[unit] = std::make_pair(texPtr, texView);
  270. }
  271. else
  272. {
  273. viewArray[0] = nullptr;
  274. if (mBoundUAVs[unit].second != nullptr)
  275. mBoundUAVs[unit].first->releaseView(mBoundUAVs[unit].second);
  276. mBoundUAVs[unit] = std::pair<SPtr<TextureCore>, TextureViewPtr>();
  277. }
  278. if (gptype == GPT_FRAGMENT_PROGRAM)
  279. {
  280. mDevice->getImmediateContext()->OMSetRenderTargetsAndUnorderedAccessViews(
  281. D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL, nullptr, nullptr, unit, 1, viewArray, nullptr);
  282. }
  283. else if (gptype == GPT_COMPUTE_PROGRAM)
  284. {
  285. mDevice->getImmediateContext()->CSSetUnorderedAccessViews(unit, 1, viewArray, nullptr);
  286. }
  287. else
  288. BS_EXCEPT(InvalidParametersException, "Unsupported gpu program type: " + toString(gptype));
  289. BS_INC_RENDER_STAT(NumTextureBinds);
  290. }
  291. void D3D11RenderAPI::disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  292. {
  293. THROW_IF_NOT_CORE_THREAD;
  294. setTexture(gptype, texUnit, false, nullptr);
  295. }
  296. void D3D11RenderAPI::beginFrame()
  297. {
  298. // Not used
  299. }
  300. void D3D11RenderAPI::endFrame()
  301. {
  302. // Not used
  303. }
  304. void D3D11RenderAPI::setViewport(const Rect2& vp)
  305. {
  306. THROW_IF_NOT_CORE_THREAD;
  307. mViewportNorm = vp;
  308. applyViewport();
  309. }
  310. void D3D11RenderAPI::applyViewport()
  311. {
  312. if (mActiveRenderTarget == nullptr)
  313. return;
  314. const RenderTargetProperties& rtProps = mActiveRenderTarget->getProperties();
  315. // Set viewport dimensions
  316. mViewport.TopLeftX = (FLOAT)(rtProps.getWidth() * mViewportNorm.x);
  317. mViewport.TopLeftY = (FLOAT)(rtProps.getHeight() * mViewportNorm.y);
  318. mViewport.Width = (FLOAT)(rtProps.getWidth() * mViewportNorm.width);
  319. mViewport.Height = (FLOAT)(rtProps.getHeight() * mViewportNorm.height);
  320. if (rtProps.requiresTextureFlipping())
  321. {
  322. // Convert "top-left" to "bottom-left"
  323. mViewport.TopLeftY = rtProps.getHeight() - mViewport.Height - mViewport.TopLeftY;
  324. }
  325. mViewport.MinDepth = 0.0f;
  326. mViewport.MaxDepth = 1.0f;
  327. mDevice->getImmediateContext()->RSSetViewports(1, &mViewport);
  328. }
  329. void D3D11RenderAPI::setVertexBuffers(UINT32 index, SPtr<VertexBufferCore>* buffers, UINT32 numBuffers)
  330. {
  331. THROW_IF_NOT_CORE_THREAD;
  332. UINT32 maxBoundVertexBuffers = mCurrentCapabilities->getMaxBoundVertexBuffers();
  333. if(index < 0 || (index + numBuffers) >= maxBoundVertexBuffers)
  334. BS_EXCEPT(InvalidParametersException, "Invalid vertex index: " + toString(index) + ". Valid range is 0 .. " + toString(maxBoundVertexBuffers - 1));
  335. ID3D11Buffer* dx11buffers[MAX_BOUND_VERTEX_BUFFERS];
  336. UINT32 strides[MAX_BOUND_VERTEX_BUFFERS];
  337. UINT32 offsets[MAX_BOUND_VERTEX_BUFFERS];
  338. for(UINT32 i = 0; i < numBuffers; i++)
  339. {
  340. SPtr<D3D11VertexBufferCore> vertexBuffer = std::static_pointer_cast<D3D11VertexBufferCore>(buffers[i]);
  341. const VertexBufferProperties& vbProps = vertexBuffer->getProperties();
  342. dx11buffers[i] = vertexBuffer->getD3DVertexBuffer();
  343. strides[i] = vbProps.getVertexSize();
  344. offsets[i] = 0;
  345. }
  346. mDevice->getImmediateContext()->IASetVertexBuffers(index, numBuffers, dx11buffers, strides, offsets);
  347. BS_INC_RENDER_STAT(NumVertexBufferBinds);
  348. }
  349. void D3D11RenderAPI::setIndexBuffer(const SPtr<IndexBufferCore>& buffer)
  350. {
  351. THROW_IF_NOT_CORE_THREAD;
  352. SPtr<D3D11IndexBufferCore> indexBuffer = std::static_pointer_cast<D3D11IndexBufferCore>(buffer);
  353. DXGI_FORMAT indexFormat = DXGI_FORMAT_R16_UINT;
  354. if(indexBuffer->getProperties().getType() == IT_16BIT)
  355. indexFormat = DXGI_FORMAT_R16_UINT;
  356. else if (indexBuffer->getProperties().getType() == IT_32BIT)
  357. indexFormat = DXGI_FORMAT_R32_UINT;
  358. else
  359. BS_EXCEPT(InternalErrorException, "Unsupported index format: " + toString(indexBuffer->getProperties().getType()));
  360. mDevice->getImmediateContext()->IASetIndexBuffer(indexBuffer->getD3DIndexBuffer(), indexFormat, 0);
  361. BS_INC_RENDER_STAT(NumIndexBufferBinds);
  362. }
  363. void D3D11RenderAPI::setVertexDeclaration(const SPtr<VertexDeclarationCore>& vertexDeclaration)
  364. {
  365. THROW_IF_NOT_CORE_THREAD;
  366. mActiveVertexDeclaration = vertexDeclaration;
  367. }
  368. void D3D11RenderAPI::setDrawOperation(DrawOperationType op)
  369. {
  370. THROW_IF_NOT_CORE_THREAD;
  371. mDevice->getImmediateContext()->IASetPrimitiveTopology(D3D11Mappings::getPrimitiveType(op));
  372. }
  373. void D3D11RenderAPI::bindGpuProgram(const SPtr<GpuProgramCore>& prg)
  374. {
  375. THROW_IF_NOT_CORE_THREAD;
  376. switch(prg->getProperties().getType())
  377. {
  378. case GPT_VERTEX_PROGRAM:
  379. {
  380. D3D11GpuVertexProgramCore* d3d11GpuProgram = static_cast<D3D11GpuVertexProgramCore*>(prg.get());
  381. mDevice->getImmediateContext()->VSSetShader(d3d11GpuProgram->getVertexShader(), nullptr, 0);
  382. mActiveVertexShader = std::static_pointer_cast<D3D11GpuProgramCore>(prg);
  383. break;
  384. }
  385. case GPT_FRAGMENT_PROGRAM:
  386. {
  387. D3D11GpuFragmentProgramCore* d3d11GpuProgram = static_cast<D3D11GpuFragmentProgramCore*>(prg.get());
  388. mDevice->getImmediateContext()->PSSetShader(d3d11GpuProgram->getPixelShader(), nullptr, 0);
  389. break;
  390. }
  391. case GPT_GEOMETRY_PROGRAM:
  392. {
  393. D3D11GpuGeometryProgramCore* d3d11GpuProgram = static_cast<D3D11GpuGeometryProgramCore*>(prg.get());
  394. mDevice->getImmediateContext()->GSSetShader(d3d11GpuProgram->getGeometryShader(), nullptr, 0);
  395. break;
  396. }
  397. case GPT_DOMAIN_PROGRAM:
  398. {
  399. D3D11GpuDomainProgramCore* d3d11GpuProgram = static_cast<D3D11GpuDomainProgramCore*>(prg.get());
  400. mDevice->getImmediateContext()->DSSetShader(d3d11GpuProgram->getDomainShader(), nullptr, 0);
  401. break;
  402. }
  403. case GPT_HULL_PROGRAM:
  404. {
  405. D3D11GpuHullProgramCore* d3d11GpuProgram = static_cast<D3D11GpuHullProgramCore*>(prg.get());
  406. mDevice->getImmediateContext()->HSSetShader(d3d11GpuProgram->getHullShader(), nullptr, 0);
  407. break;
  408. }
  409. case GPT_COMPUTE_PROGRAM:
  410. {
  411. D3D11GpuComputeProgramCore* d3d11GpuProgram = static_cast<D3D11GpuComputeProgramCore*>(prg.get());
  412. mDevice->getImmediateContext()->CSSetShader(d3d11GpuProgram->getComputeShader(), nullptr, 0);
  413. break;
  414. }
  415. default:
  416. BS_EXCEPT(InvalidParametersException, "Unsupported gpu program type: " + toString(prg->getProperties().getType()));
  417. }
  418. if (mDevice->hasError())
  419. BS_EXCEPT(RenderingAPIException, "Failed to bindGpuProgram : " + mDevice->getErrorDescription());
  420. BS_INC_RENDER_STAT(NumGpuProgramBinds);
  421. }
  422. void D3D11RenderAPI::unbindGpuProgram(GpuProgramType gptype)
  423. {
  424. THROW_IF_NOT_CORE_THREAD;
  425. switch(gptype)
  426. {
  427. case GPT_VERTEX_PROGRAM:
  428. mDevice->getImmediateContext()->VSSetShader(nullptr, nullptr, 0);
  429. mActiveVertexShader = nullptr;
  430. break;
  431. case GPT_FRAGMENT_PROGRAM:
  432. mDevice->getImmediateContext()->PSSetShader(nullptr, nullptr, 0);
  433. break;
  434. case GPT_GEOMETRY_PROGRAM:
  435. mDevice->getImmediateContext()->GSSetShader(nullptr, nullptr, 0);
  436. break;
  437. case GPT_DOMAIN_PROGRAM:
  438. mDevice->getImmediateContext()->DSSetShader(nullptr, nullptr, 0);
  439. break;
  440. case GPT_HULL_PROGRAM:
  441. mDevice->getImmediateContext()->HSSetShader(nullptr, nullptr, 0);
  442. break;
  443. case GPT_COMPUTE_PROGRAM:
  444. mDevice->getImmediateContext()->CSSetShader(nullptr, nullptr, 0);
  445. break;
  446. default:
  447. BS_EXCEPT(InvalidParametersException, "Unsupported gpu program type: " + toString(gptype));
  448. }
  449. BS_INC_RENDER_STAT(NumGpuProgramBinds);
  450. }
  451. void D3D11RenderAPI::setConstantBuffers(GpuProgramType gptype, const SPtr<GpuParamsCore>& bindableParams)
  452. {
  453. THROW_IF_NOT_CORE_THREAD;
  454. bindableParams->updateHardwareBuffers();
  455. const GpuParamDesc& paramDesc = bindableParams->getParamDesc();
  456. // TODO - I assign constant buffers one by one but it might be more efficient to do them all at once?
  457. ID3D11Buffer* bufferArray[1];
  458. for(auto iter = paramDesc.paramBlocks.begin(); iter != paramDesc.paramBlocks.end(); ++iter)
  459. {
  460. SPtr<GpuParamBlockBufferCore> currentBlockBuffer = bindableParams->getParamBlockBuffer(iter->second.slot);
  461. if(currentBlockBuffer != nullptr)
  462. {
  463. const D3D11GpuParamBlockBufferCore* d3d11paramBlockBuffer =
  464. static_cast<const D3D11GpuParamBlockBufferCore*>(currentBlockBuffer.get());
  465. bufferArray[0] = d3d11paramBlockBuffer->getD3D11Buffer();
  466. }
  467. else
  468. bufferArray[0] = nullptr;
  469. switch(gptype)
  470. {
  471. case GPT_VERTEX_PROGRAM:
  472. mDevice->getImmediateContext()->VSSetConstantBuffers(iter->second.slot, 1, bufferArray);
  473. break;
  474. case GPT_FRAGMENT_PROGRAM:
  475. mDevice->getImmediateContext()->PSSetConstantBuffers(iter->second.slot, 1, bufferArray);
  476. break;
  477. case GPT_GEOMETRY_PROGRAM:
  478. mDevice->getImmediateContext()->GSSetConstantBuffers(iter->second.slot, 1, bufferArray);
  479. break;
  480. case GPT_HULL_PROGRAM:
  481. mDevice->getImmediateContext()->HSSetConstantBuffers(iter->second.slot, 1, bufferArray);
  482. break;
  483. case GPT_DOMAIN_PROGRAM:
  484. mDevice->getImmediateContext()->DSSetConstantBuffers(iter->second.slot, 1, bufferArray);
  485. break;
  486. case GPT_COMPUTE_PROGRAM:
  487. mDevice->getImmediateContext()->CSSetConstantBuffers(iter->second.slot, 1, bufferArray);
  488. break;
  489. };
  490. BS_INC_RENDER_STAT(NumGpuParamBufferBinds);
  491. }
  492. if (mDevice->hasError())
  493. BS_EXCEPT(RenderingAPIException, "Failed to setConstantBuffers : " + mDevice->getErrorDescription());
  494. }
  495. void D3D11RenderAPI::draw(UINT32 vertexOffset, UINT32 vertexCount)
  496. {
  497. THROW_IF_NOT_CORE_THREAD;
  498. applyInputLayout();
  499. mDevice->getImmediateContext()->Draw(vertexCount, vertexOffset);
  500. #if BS_DEBUG_MODE
  501. if(mDevice->hasError())
  502. LOGWRN(mDevice->getErrorDescription());
  503. #endif
  504. UINT32 primCount = vertexCountToPrimCount(mActiveDrawOp, vertexCount);
  505. BS_INC_RENDER_STAT(NumDrawCalls);
  506. BS_ADD_RENDER_STAT(NumVertices, vertexCount);
  507. BS_ADD_RENDER_STAT(NumPrimitives, primCount);
  508. }
  509. void D3D11RenderAPI::drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount)
  510. {
  511. THROW_IF_NOT_CORE_THREAD;
  512. applyInputLayout();
  513. mDevice->getImmediateContext()->DrawIndexed(indexCount, startIndex, vertexOffset);
  514. #if BS_DEBUG_MODE
  515. if(mDevice->hasError())
  516. LOGWRN(mDevice->getErrorDescription());
  517. #endif
  518. UINT32 primCount = vertexCountToPrimCount(mActiveDrawOp, vertexCount);
  519. BS_INC_RENDER_STAT(NumDrawCalls);
  520. BS_ADD_RENDER_STAT(NumVertices, vertexCount);
  521. BS_ADD_RENDER_STAT(NumPrimitives, primCount);
  522. }
  523. void D3D11RenderAPI::setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
  524. {
  525. THROW_IF_NOT_CORE_THREAD;
  526. mScissorRect.left = static_cast<LONG>(left);
  527. mScissorRect.top = static_cast<LONG>(top);
  528. mScissorRect.bottom = static_cast<LONG>(bottom);
  529. mScissorRect.right = static_cast<LONG>(right);
  530. mDevice->getImmediateContext()->RSSetScissorRects(1, &mScissorRect);
  531. }
  532. void D3D11RenderAPI::clearViewport(UINT32 buffers, const Color& color, float depth, UINT16 stencil)
  533. {
  534. THROW_IF_NOT_CORE_THREAD;
  535. if(mActiveRenderTarget == nullptr)
  536. return;
  537. const RenderTargetProperties& rtProps = mActiveRenderTarget->getProperties();
  538. Rect2I clearArea((int)mViewport.TopLeftX, (int)mViewport.TopLeftY, (int)mViewport.Width, (int)mViewport.Height);
  539. bool clearEntireTarget = clearArea.width == 0 || clearArea.height == 0;
  540. clearEntireTarget |= (clearArea.x == 0 && clearArea.y == 0 && clearArea.width == rtProps.getWidth() && clearArea.height == rtProps.getHeight());
  541. if (!clearEntireTarget)
  542. {
  543. D3D11RenderUtility::instance().drawClearQuad(buffers, color, depth, stencil);
  544. BS_INC_RENDER_STAT(NumClears);
  545. }
  546. else
  547. clearRenderTarget(buffers, color, depth, stencil);
  548. }
  549. void D3D11RenderAPI::clearRenderTarget(UINT32 buffers, const Color& color, float depth, UINT16 stencil)
  550. {
  551. THROW_IF_NOT_CORE_THREAD;
  552. if(mActiveRenderTarget == nullptr)
  553. return;
  554. // Clear render surfaces
  555. if (buffers & FBT_COLOR)
  556. {
  557. UINT32 maxRenderTargets = mCurrentCapabilities->getNumMultiRenderTargets();
  558. ID3D11RenderTargetView** views = bs_newN<ID3D11RenderTargetView*>(maxRenderTargets);
  559. memset(views, 0, sizeof(ID3D11RenderTargetView*) * maxRenderTargets);
  560. mActiveRenderTarget->getCustomAttribute("RTV", views);
  561. if (!views[0])
  562. {
  563. bs_deleteN(views, maxRenderTargets);
  564. return;
  565. }
  566. float clearColor[4];
  567. clearColor[0] = color.r;
  568. clearColor[1] = color.g;
  569. clearColor[2] = color.b;
  570. clearColor[3] = color.a;
  571. for(UINT32 i = 0; i < maxRenderTargets; i++)
  572. {
  573. if(views[i] != nullptr)
  574. mDevice->getImmediateContext()->ClearRenderTargetView(views[i], clearColor);
  575. }
  576. bs_deleteN(views, maxRenderTargets);
  577. }
  578. // Clear depth stencil
  579. if((buffers & FBT_DEPTH) != 0 || (buffers & FBT_STENCIL) != 0)
  580. {
  581. ID3D11DepthStencilView* depthStencilView = nullptr;
  582. mActiveRenderTarget->getCustomAttribute("DSV", &depthStencilView);
  583. D3D11_CLEAR_FLAG clearFlag;
  584. if((buffers & FBT_DEPTH) != 0 && (buffers & FBT_STENCIL) != 0)
  585. clearFlag = (D3D11_CLEAR_FLAG)(D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL);
  586. else if((buffers & FBT_STENCIL) != 0)
  587. clearFlag = D3D11_CLEAR_STENCIL;
  588. else
  589. clearFlag = D3D11_CLEAR_DEPTH;
  590. if(depthStencilView != nullptr)
  591. mDevice->getImmediateContext()->ClearDepthStencilView(depthStencilView, clearFlag, depth, (UINT8)stencil);
  592. }
  593. BS_INC_RENDER_STAT(NumClears);
  594. }
  595. void D3D11RenderAPI::setRenderTarget(const SPtr<RenderTargetCore>& target, bool readOnlyDepthStencil)
  596. {
  597. THROW_IF_NOT_CORE_THREAD;
  598. mActiveRenderTarget = target;
  599. UINT32 maxRenderTargets = mCurrentCapabilities->getNumMultiRenderTargets();
  600. ID3D11RenderTargetView** views = bs_newN<ID3D11RenderTargetView*>(maxRenderTargets);
  601. memset(views, 0, sizeof(ID3D11RenderTargetView*) * maxRenderTargets);
  602. ID3D11DepthStencilView* depthStencilView = nullptr;
  603. if (target != nullptr)
  604. {
  605. target->getCustomAttribute("RTV", views);
  606. if(readOnlyDepthStencil)
  607. target->getCustomAttribute("RODSV", &depthStencilView);
  608. else
  609. target->getCustomAttribute("DSV", &depthStencilView);
  610. }
  611. // Bind render targets
  612. mDevice->getImmediateContext()->OMSetRenderTargets(maxRenderTargets, views, depthStencilView);
  613. if (mDevice->hasError())
  614. BS_EXCEPT(RenderingAPIException, "Failed to setRenderTarget : " + mDevice->getErrorDescription());
  615. bs_deleteN(views, maxRenderTargets);
  616. applyViewport();
  617. BS_INC_RENDER_STAT(NumRenderTargetChanges);
  618. }
  619. void D3D11RenderAPI::setClipPlanesImpl(const PlaneList& clipPlanes)
  620. {
  621. LOGWRN("This call will be ignored. DX11 uses shaders for setting clip planes.");
  622. }
  623. RenderAPICapabilities* D3D11RenderAPI::createRenderSystemCapabilities() const
  624. {
  625. THROW_IF_NOT_CORE_THREAD;
  626. RenderAPICapabilities* rsc = bs_new<RenderAPICapabilities>();
  627. rsc->setDriverVersion(mDriverVersion);
  628. rsc->setDeviceName(mActiveD3DDriver->getDriverDescription());
  629. rsc->setRenderAPIName(getName());
  630. rsc->setStencilBufferBitDepth(8);
  631. rsc->setCapability(RSC_ANISOTROPY);
  632. rsc->setCapability(RSC_AUTOMIPMAP);
  633. // Cube map
  634. rsc->setCapability(RSC_CUBEMAPPING);
  635. // We always support compression, D3DX will decompress if device does not support
  636. rsc->setCapability(RSC_TEXTURE_COMPRESSION);
  637. rsc->setCapability(RSC_TEXTURE_COMPRESSION_DXT);
  638. rsc->setCapability(RSC_TWO_SIDED_STENCIL);
  639. rsc->setCapability(RSC_STENCIL_WRAP);
  640. rsc->setCapability(RSC_HWOCCLUSION);
  641. rsc->setCapability(RSC_HWOCCLUSION_ASYNCHRONOUS);
  642. if(mFeatureLevel >= D3D_FEATURE_LEVEL_10_1)
  643. rsc->setMaxBoundVertexBuffers(32);
  644. else
  645. rsc->setMaxBoundVertexBuffers(16);
  646. if(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0)
  647. {
  648. rsc->addShaderProfile("ps_4_0");
  649. rsc->addShaderProfile("vs_4_0");
  650. rsc->addShaderProfile("gs_4_0");
  651. rsc->addGpuProgramProfile(GPP_FS_4_0, "ps_4_0");
  652. rsc->addGpuProgramProfile(GPP_VS_4_0, "vs_4_0");
  653. rsc->addGpuProgramProfile(GPP_GS_4_0, "gs_4_0");
  654. rsc->setNumTextureUnits(GPT_FRAGMENT_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  655. rsc->setNumTextureUnits(GPT_VERTEX_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  656. rsc->setNumTextureUnits(GPT_GEOMETRY_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  657. rsc->setNumCombinedTextureUnits(rsc->getNumTextureUnits(GPT_FRAGMENT_PROGRAM)
  658. + rsc->getNumTextureUnits(GPT_VERTEX_PROGRAM) + rsc->getNumTextureUnits(GPT_VERTEX_PROGRAM));
  659. rsc->setNumGpuParamBlockBuffers(GPT_FRAGMENT_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  660. rsc->setNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  661. rsc->setNumGpuParamBlockBuffers(GPT_GEOMETRY_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  662. rsc->setNumCombinedGpuParamBlockBuffers(rsc->getNumGpuParamBlockBuffers(GPT_FRAGMENT_PROGRAM)
  663. + rsc->getNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM) + rsc->getNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM));
  664. }
  665. if(mFeatureLevel >= D3D_FEATURE_LEVEL_10_1)
  666. {
  667. rsc->addShaderProfile("ps_4_1");
  668. rsc->addShaderProfile("vs_4_1");
  669. rsc->addShaderProfile("gs_4_1");
  670. rsc->addGpuProgramProfile(GPP_FS_4_1, "ps_4_1");
  671. rsc->addGpuProgramProfile(GPP_VS_4_1, "vs_4_1");
  672. rsc->addGpuProgramProfile(GPP_GS_4_1, "gs_4_1");
  673. }
  674. if(mFeatureLevel >= D3D_FEATURE_LEVEL_11_0)
  675. {
  676. rsc->addShaderProfile("ps_5_0");
  677. rsc->addShaderProfile("vs_5_0");
  678. rsc->addShaderProfile("gs_5_0");
  679. rsc->addShaderProfile("cs_5_0");
  680. rsc->addShaderProfile("hs_5_0");
  681. rsc->addShaderProfile("ds_5_0");
  682. rsc->addGpuProgramProfile(GPP_FS_5_0, "ps_5_0");
  683. rsc->addGpuProgramProfile(GPP_VS_5_0, "vs_5_0");
  684. rsc->addGpuProgramProfile(GPP_GS_5_0, "gs_5_0");
  685. rsc->addGpuProgramProfile(GPP_CS_5_0, "cs_5_0");
  686. rsc->addGpuProgramProfile(GPP_HS_5_0, "hs_5_0");
  687. rsc->addGpuProgramProfile(GPP_DS_5_0, "ds_5_0");
  688. rsc->setNumTextureUnits(GPT_HULL_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  689. rsc->setNumTextureUnits(GPT_DOMAIN_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  690. rsc->setNumTextureUnits(GPT_COMPUTE_PROGRAM, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
  691. rsc->setNumCombinedTextureUnits(rsc->getNumTextureUnits(GPT_FRAGMENT_PROGRAM)
  692. + rsc->getNumTextureUnits(GPT_VERTEX_PROGRAM) + rsc->getNumTextureUnits(GPT_VERTEX_PROGRAM)
  693. + rsc->getNumTextureUnits(GPT_HULL_PROGRAM) + rsc->getNumTextureUnits(GPT_DOMAIN_PROGRAM)
  694. + rsc->getNumTextureUnits(GPT_COMPUTE_PROGRAM));
  695. rsc->setNumGpuParamBlockBuffers(GPT_HULL_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  696. rsc->setNumGpuParamBlockBuffers(GPT_DOMAIN_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  697. rsc->setNumGpuParamBlockBuffers(GPT_COMPUTE_PROGRAM, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
  698. rsc->setNumCombinedGpuParamBlockBuffers(rsc->getNumGpuParamBlockBuffers(GPT_FRAGMENT_PROGRAM)
  699. + rsc->getNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM) + rsc->getNumGpuParamBlockBuffers(GPT_VERTEX_PROGRAM)
  700. + rsc->getNumGpuParamBlockBuffers(GPT_HULL_PROGRAM) + rsc->getNumGpuParamBlockBuffers(GPT_DOMAIN_PROGRAM)
  701. + rsc->getNumGpuParamBlockBuffers(GPT_COMPUTE_PROGRAM));
  702. rsc->setCapability(RSC_SHADER_SUBROUTINE);
  703. }
  704. rsc->setCapability(RSC_USER_CLIP_PLANES);
  705. rsc->setCapability(RSC_VERTEX_FORMAT_UBYTE4);
  706. // Adapter details
  707. const DXGI_ADAPTER_DESC& adapterID = mActiveD3DDriver->getAdapterIdentifier();
  708. // Determine vendor
  709. switch(adapterID.VendorId)
  710. {
  711. case 0x10DE:
  712. rsc->setVendor(GPU_NVIDIA);
  713. break;
  714. case 0x1002:
  715. rsc->setVendor(GPU_AMD);
  716. break;
  717. case 0x163C:
  718. case 0x8086:
  719. rsc->setVendor(GPU_INTEL);
  720. break;
  721. default:
  722. rsc->setVendor(GPU_UNKNOWN);
  723. break;
  724. };
  725. rsc->setCapability(RSC_INFINITE_FAR_PLANE);
  726. rsc->setCapability(RSC_TEXTURE_3D);
  727. rsc->setCapability(RSC_NON_POWER_OF_2_TEXTURES);
  728. rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
  729. rsc->setCapability(RSC_TEXTURE_FLOAT);
  730. rsc->setNumMultiRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT);
  731. rsc->setCapability(RSC_MRT_DIFFERENT_BIT_DEPTHS);
  732. rsc->setCapability(RSC_POINT_SPRITES);
  733. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS);
  734. rsc->setMaxPointSize(256);
  735. rsc->setCapability(RSC_VERTEX_TEXTURE_FETCH);
  736. rsc->setCapability(RSC_MIPMAP_LOD_BIAS);
  737. rsc->setCapability(RSC_PERSTAGECONSTANT);
  738. return rsc;
  739. }
  740. void D3D11RenderAPI::determineMultisampleSettings(UINT32 multisampleCount, DXGI_FORMAT format, DXGI_SAMPLE_DESC* outputSampleDesc)
  741. {
  742. bool tryCSAA = false; // Note: Disabled for now, but leaving the code for later so it might be useful
  743. enum CSAAMode { CSAA_Normal, CSAA_Quality };
  744. CSAAMode csaaMode = CSAA_Normal;
  745. bool foundValid = false;
  746. size_t origNumSamples = multisampleCount;
  747. while (!foundValid)
  748. {
  749. // Deal with special cases
  750. if (tryCSAA)
  751. {
  752. switch(multisampleCount)
  753. {
  754. case 8:
  755. if (csaaMode == CSAA_Quality)
  756. {
  757. outputSampleDesc->Count = 8;
  758. outputSampleDesc->Quality = 8;
  759. }
  760. else
  761. {
  762. outputSampleDesc->Count = 4;
  763. outputSampleDesc->Quality = 8;
  764. }
  765. break;
  766. case 16:
  767. if (csaaMode == CSAA_Quality)
  768. {
  769. outputSampleDesc->Count = 8;
  770. outputSampleDesc->Quality = 16;
  771. }
  772. else
  773. {
  774. outputSampleDesc->Count = 4;
  775. outputSampleDesc->Quality = 16;
  776. }
  777. break;
  778. }
  779. }
  780. else // !CSAA
  781. {
  782. outputSampleDesc->Count = multisampleCount == 0 ? 1 : multisampleCount;
  783. outputSampleDesc->Quality = D3D11_STANDARD_MULTISAMPLE_PATTERN;
  784. }
  785. HRESULT hr;
  786. UINT outQuality;
  787. hr = mDevice->getD3D11Device()->CheckMultisampleQualityLevels(format, outputSampleDesc->Count, &outQuality);
  788. if (SUCCEEDED(hr) && (!tryCSAA || outQuality > outputSampleDesc->Quality))
  789. {
  790. foundValid = true;
  791. }
  792. else
  793. {
  794. // Downgrade
  795. if (tryCSAA && multisampleCount == 8)
  796. {
  797. // For CSAA, we'll try downgrading with quality mode at all samples.
  798. // then try without quality, then drop CSAA
  799. if (csaaMode == CSAA_Quality)
  800. {
  801. // Drop quality first
  802. csaaMode = CSAA_Normal;
  803. }
  804. else
  805. {
  806. // Drop CSAA entirely
  807. tryCSAA = false;
  808. }
  809. // Return to original requested samples
  810. multisampleCount = static_cast<UINT32>(origNumSamples);
  811. }
  812. else
  813. {
  814. // Drop samples
  815. multisampleCount--;
  816. if (multisampleCount == 1)
  817. {
  818. // Ran out of options, no multisampling
  819. multisampleCount = 0;
  820. foundValid = true;
  821. }
  822. }
  823. }
  824. }
  825. }
  826. VertexElementType D3D11RenderAPI::getColorVertexElementType() const
  827. {
  828. return VET_COLOR_ABGR;
  829. }
  830. void D3D11RenderAPI::convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest)
  831. {
  832. dest = matrix;
  833. // Convert depth range from [-1,+1] to [0,1]
  834. dest[2][0] = (dest[2][0] + dest[3][0]) / 2;
  835. dest[2][1] = (dest[2][1] + dest[3][1]) / 2;
  836. dest[2][2] = (dest[2][2] + dest[3][2]) / 2;
  837. dest[2][3] = (dest[2][3] + dest[3][3]) / 2;
  838. }
  839. float D3D11RenderAPI::getHorizontalTexelOffset()
  840. {
  841. return 0.0f;
  842. }
  843. float D3D11RenderAPI::getVerticalTexelOffset()
  844. {
  845. return 0.0f;
  846. }
  847. float D3D11RenderAPI::getMinimumDepthInputValue()
  848. {
  849. return 0.0f;
  850. }
  851. float D3D11RenderAPI::getMaximumDepthInputValue()
  852. {
  853. return 1.0f;
  854. }
  855. GpuParamBlockDesc D3D11RenderAPI::generateParamBlockDesc(const String& name, Vector<GpuParamDataDesc>& params)
  856. {
  857. GpuParamBlockDesc block;
  858. block.blockSize = 0;
  859. block.isShareable = true;
  860. block.name = name;
  861. block.slot = 0;
  862. for (auto& param : params)
  863. {
  864. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[param.type];
  865. UINT32 size = typeInfo.size / 4;
  866. if (param.arraySize > 1)
  867. {
  868. // Arrays perform no packing and their elements are always padded and aligned to four component vectors
  869. UINT32 alignOffset = size % typeInfo.baseTypeSize;
  870. if (alignOffset != 0)
  871. {
  872. UINT32 padding = (typeInfo.baseTypeSize - alignOffset);
  873. size += padding;
  874. }
  875. alignOffset = block.blockSize % typeInfo.baseTypeSize;
  876. if (alignOffset != 0)
  877. {
  878. UINT32 padding = (typeInfo.baseTypeSize - alignOffset);
  879. block.blockSize += padding;
  880. }
  881. param.elementSize = size;
  882. param.arrayElementStride = size;
  883. param.cpuMemOffset = block.blockSize;
  884. param.gpuMemOffset = 0;
  885. block.blockSize += size * param.arraySize;
  886. }
  887. else
  888. {
  889. // Pack everything as tightly as possible as long as the data doesn't cross 16 byte boundary
  890. UINT32 alignOffset = block.blockSize % 4;
  891. if (alignOffset != 0 && size > (4 - alignOffset))
  892. {
  893. UINT32 padding = (4 - alignOffset);
  894. block.blockSize += padding;
  895. }
  896. param.elementSize = size;
  897. param.arrayElementStride = size;
  898. param.cpuMemOffset = block.blockSize;
  899. param.gpuMemOffset = 0;
  900. block.blockSize += size;
  901. }
  902. param.paramBlockSlot = 0;
  903. }
  904. // Constant buffer size must always be a multiple of 16
  905. if (block.blockSize % 4 != 0)
  906. block.blockSize += (4 - (block.blockSize % 4));
  907. return block;
  908. }
  909. /************************************************************************/
  910. /* PRIVATE */
  911. /************************************************************************/
  912. void D3D11RenderAPI::applyInputLayout()
  913. {
  914. if(mActiveVertexDeclaration == nullptr)
  915. {
  916. LOGWRN("Cannot apply input layout without a vertex declaration. Set vertex declaration before calling this method.");
  917. return;
  918. }
  919. if(mActiveVertexShader == nullptr)
  920. {
  921. LOGWRN("Cannot apply input layout without a vertex shader. Set vertex shader before calling this method.");
  922. return;
  923. }
  924. ID3D11InputLayout* ia = mIAManager->retrieveInputLayout(mActiveVertexShader->getInputDeclaration(), mActiveVertexDeclaration, *mActiveVertexShader);
  925. mDevice->getImmediateContext()->IASetInputLayout(ia);
  926. }
  927. }