BsD3D11RenderAPI.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsD3D11Prerequisites.h"
  5. #include "BsRenderAPI.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup D3D11
  9. * @{
  10. */
  11. /** Implementation of a render system using DirectX 11. Provides abstracted access to various low level DX11 methods. */
  12. class BS_D3D11_EXPORT D3D11RenderAPI : public RenderAPICore
  13. {
  14. public:
  15. D3D11RenderAPI();
  16. ~D3D11RenderAPI();
  17. /** @copydoc RenderAPICore::getName */
  18. const StringID& getName() const override;
  19. /** @copydoc RenderAPICore::getShadingLanguageName */
  20. const String& getShadingLanguageName() const override;
  21. /** @copydoc RenderAPICore::setBlendState */
  22. void setBlendState(const SPtr<BlendStateCore>& blendState,
  23. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  24. /** @copydoc RenderAPICore::setRasterizerState */
  25. void setRasterizerState(const SPtr<RasterizerStateCore>& rasterizerState,
  26. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  27. /** @copydoc RenderAPICore::setDepthStencilState */
  28. void setDepthStencilState(const SPtr<DepthStencilStateCore>& depthStencilState, UINT32 stencilRefValue,
  29. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  30. /** @copydoc RenderAPICore::setSamplerState */
  31. void setSamplerState(GpuProgramType gptype, UINT16 texUnit, const SPtr<SamplerStateCore>& samplerState,
  32. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  33. /** @copydoc RenderAPICore::setTexture */
  34. void setTexture(GpuProgramType gptype, UINT16 texUnit, const SPtr<TextureCore>& texture,
  35. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  36. /** @copydoc RenderAPICore::setLoadStoreTexture */
  37. void setLoadStoreTexture(GpuProgramType gptype, UINT16 texUnit, const SPtr<TextureCore>& texture,
  38. const TextureSurface& surface, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  39. /** @copydoc RenderAPICore::setBuffer */
  40. void setBuffer(GpuProgramType gptype, UINT16 unit, const SPtr<GpuBufferCore>& buffer, bool loadStore = false,
  41. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  42. /** @copydoc RenderAPICore::beginFrame */
  43. void beginFrame(const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  44. /** @copydoc RenderAPICore::endFrame */
  45. void endFrame(const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  46. /** @copydoc RenderAPICore::clearRenderTarget */
  47. void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0,
  48. UINT8 targetMask = 0xFF, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  49. /** @copydoc RenderAPICore::clearViewport */
  50. void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0,
  51. UINT8 targetMask = 0xFF, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  52. /** @copydoc RenderAPICore::setRenderTarget */
  53. void setRenderTarget(const SPtr<RenderTargetCore>& target, bool readOnlyDepthStencil = false,
  54. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  55. /** @copydoc RenderAPICore::setViewport */
  56. void setViewport(const Rect2& area, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  57. /** @copydoc RenderAPICore::setScissorRect */
  58. void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom,
  59. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  60. /** @copydoc RenderAPICore::setVertexBuffers */
  61. void setVertexBuffers(UINT32 index, SPtr<VertexBufferCore>* buffers, UINT32 numBuffers,
  62. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  63. /** @copydoc RenderAPICore::setIndexBuffer */
  64. void setIndexBuffer(const SPtr<IndexBufferCore>& buffer,
  65. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  66. /** @copydoc RenderAPICore::setVertexDeclaration */
  67. void setVertexDeclaration(const SPtr<VertexDeclarationCore>& vertexDeclaration,
  68. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  69. /** @copydoc RenderAPICore::setDrawOperation */
  70. void setDrawOperation(DrawOperationType op,
  71. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  72. /** @copydoc RenderAPICore::draw */
  73. void draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount = 0,
  74. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  75. /** @copydoc RenderAPICore::drawIndexed */
  76. void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount,
  77. UINT32 instanceCount = 0, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  78. /** @copydoc RenderAPICore::dispatchCompute */
  79. void dispatchCompute(UINT32 numGroupsX, UINT32 numGroupsY = 1, UINT32 numGroupsZ = 1,
  80. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  81. /** @copydoc RenderAPICore::swapBuffers() */
  82. void swapBuffers(const SPtr<RenderTargetCore>& target,
  83. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  84. /** @copydoc RenderAPICore::bindGpuProgram */
  85. void bindGpuProgram(const SPtr<GpuProgramCore>& prg, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  86. /** @copydoc RenderAPICore::unbindGpuProgram */
  87. void unbindGpuProgram(GpuProgramType gptype, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  88. /** @copydoc RenderAPICore::setParamBuffer */
  89. void setParamBuffer(GpuProgramType gptype, UINT32 slot, const SPtr<GpuParamBlockBufferCore>& buffer,
  90. const SPtr<GpuParamDesc>& paramDesc, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  91. /** @copydoc RenderAPICore::addCommands() */
  92. void addCommands(const SPtr<CommandBuffer>& commandBuffer, const SPtr<CommandBuffer>& secondary) override;
  93. /** @copydoc RenderAPICore::executeCommands() */
  94. void executeCommands(const SPtr<CommandBuffer>& commandBuffer) override;
  95. /** @copydoc RenderAPICore::convertProjectionMatrix */
  96. void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest) override;
  97. /** @copydoc RenderAPICore::getAPIInfo */
  98. const RenderAPIInfo& getAPIInfo() const override;
  99. /** @copydoc RenderAPICore::generateParamBlockDesc() */
  100. GpuParamBlockDesc generateParamBlockDesc(const String& name, Vector<GpuParamDataDesc>& params) override;
  101. /************************************************************************/
  102. /* Internal use by DX11 RenderSystem only */
  103. /************************************************************************/
  104. /**
  105. * Determines DXGI multisample settings from the provided parameters.
  106. *
  107. * @param[in] multisampleCount Number of requested samples.
  108. * @param[in] format Pixel format used by the render target.
  109. * @param[out] outputSampleDesc Output structure that will contain the requested multisample settings.
  110. */
  111. void determineMultisampleSettings(UINT32 multisampleCount, DXGI_FORMAT format, DXGI_SAMPLE_DESC* outputSampleDesc);
  112. /** Returns the main DXGI factory object. */
  113. IDXGIFactory* getDXGIFactory() const { return mDXGIFactory; }
  114. /** Returns the primary DX11 device object. */
  115. D3D11Device& getPrimaryDevice() const { return *mDevice; }
  116. /** Returns information describing all available drivers. */
  117. D3D11DriverList* getDriverList() const { return mDriverList; }
  118. protected:
  119. friend class D3D11RenderAPIFactory;
  120. /** @copydoc RenderAPICore::initializePrepare */
  121. void initializePrepare() override;
  122. /** @copydoc RenderAPICore::initializeFinalize */
  123. void initializeFinalize(const SPtr<RenderWindowCore>& primaryWindow) override;
  124. /** @copydoc RenderAPICore::destroyCore */
  125. void destroyCore() override;
  126. /**
  127. * Creates or retrieves a proper input layout depending on the currently set vertex shader and vertex buffer.
  128. *
  129. * Applies the input layout to the pipeline.
  130. */
  131. void applyInputLayout();
  132. /**
  133. * Recalculates actual viewport dimensions based on currently set viewport normalized dimensions and render target
  134. * and applies them for further rendering.
  135. */
  136. void applyViewport();
  137. /** Creates and populates a set of render system capabilities describing which functionality is available. */
  138. RenderAPICapabilities* createRenderSystemCapabilities() const;
  139. private:
  140. IDXGIFactory* mDXGIFactory;
  141. D3D11Device* mDevice;
  142. D3D11DriverList* mDriverList;
  143. D3D11Driver* mActiveD3DDriver;
  144. D3D_FEATURE_LEVEL mFeatureLevel;
  145. D3D11HLSLProgramFactory* mHLSLFactory;
  146. D3D11InputLayoutManager* mIAManager;
  147. std::pair<SPtr<TextureCore>, SPtr<TextureView>> mBoundUAVs[D3D11_PS_CS_UAV_REGISTER_COUNT];
  148. UINT32 mStencilRef;
  149. Rect2 mViewportNorm;
  150. D3D11_VIEWPORT mViewport;
  151. D3D11_RECT mScissorRect;
  152. SPtr<VertexDeclarationCore> mActiveVertexDeclaration;
  153. SPtr<D3D11GpuProgramCore> mActiveVertexShader;
  154. DrawOperationType mActiveDrawOp;
  155. };
  156. /** @} */
  157. }