BsD3D11RenderAPI.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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::setGraphicsPipeline */
  22. void setGraphicsPipeline(const SPtr<GpuPipelineStateCore>& pipelineState,
  23. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  24. /** @copydoc RenderAPICore::setComputePipeline */
  25. void setComputePipeline(const SPtr<GpuProgramCore>& computeProgram,
  26. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  27. /** @copydoc RenderAPICore::setGpuParams */
  28. void setGpuParams(const SPtr<GpuParamsCore>& gpuParams,
  29. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  30. /** @copydoc RenderAPICore::beginFrame */
  31. void beginFrame(const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  32. /** @copydoc RenderAPICore::endFrame */
  33. void endFrame(const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  34. /** @copydoc RenderAPICore::clearRenderTarget */
  35. void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0,
  36. UINT8 targetMask = 0xFF, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  37. /** @copydoc RenderAPICore::clearViewport */
  38. void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0,
  39. UINT8 targetMask = 0xFF, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  40. /** @copydoc RenderAPICore::setRenderTarget */
  41. void setRenderTarget(const SPtr<RenderTargetCore>& target, bool readOnlyDepthStencil = false,
  42. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  43. /** @copydoc RenderAPICore::setViewport */
  44. void setViewport(const Rect2& area, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  45. /** @copydoc RenderAPICore::setScissorRect */
  46. void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom,
  47. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  48. /** @copydoc RenderAPICore::setStencilRef */
  49. void setStencilRef(UINT32 value, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  50. /** @copydoc RenderAPICore::setVertexBuffers */
  51. void setVertexBuffers(UINT32 index, SPtr<VertexBufferCore>* buffers, UINT32 numBuffers,
  52. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  53. /** @copydoc RenderAPICore::setIndexBuffer */
  54. void setIndexBuffer(const SPtr<IndexBufferCore>& buffer,
  55. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  56. /** @copydoc RenderAPICore::setVertexDeclaration */
  57. void setVertexDeclaration(const SPtr<VertexDeclarationCore>& vertexDeclaration,
  58. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  59. /** @copydoc RenderAPICore::setDrawOperation */
  60. void setDrawOperation(DrawOperationType op,
  61. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  62. /** @copydoc RenderAPICore::draw */
  63. void draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount = 0,
  64. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  65. /** @copydoc RenderAPICore::drawIndexed */
  66. void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount,
  67. UINT32 instanceCount = 0, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  68. /** @copydoc RenderAPICore::dispatchCompute */
  69. void dispatchCompute(UINT32 numGroupsX, UINT32 numGroupsY = 1, UINT32 numGroupsZ = 1,
  70. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  71. /** @copydoc RenderAPICore::swapBuffers() */
  72. void swapBuffers(const SPtr<RenderTargetCore>& target,
  73. const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  74. /** @copydoc RenderAPICore::addCommands() */
  75. void addCommands(const SPtr<CommandBuffer>& commandBuffer, const SPtr<CommandBuffer>& secondary) override;
  76. /** @copydoc RenderAPICore::executeCommands() */
  77. void executeCommands(const SPtr<CommandBuffer>& commandBuffer, UINT32 syncMask = 0xFFFFFFFF) override;
  78. /** @copydoc RenderAPICore::convertProjectionMatrix */
  79. void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest) override;
  80. /** @copydoc RenderAPICore::getAPIInfo */
  81. const RenderAPIInfo& getAPIInfo() const override;
  82. /** @copydoc RenderAPICore::generateParamBlockDesc() */
  83. GpuParamBlockDesc generateParamBlockDesc(const String& name, Vector<GpuParamDataDesc>& params) override;
  84. /************************************************************************/
  85. /* Internal use by DX11 RenderSystem only */
  86. /************************************************************************/
  87. /**
  88. * Determines DXGI multisample settings from the provided parameters.
  89. *
  90. * @param[in] multisampleCount Number of requested samples.
  91. * @param[in] format Pixel format used by the render target.
  92. * @param[out] outputSampleDesc Output structure that will contain the requested multisample settings.
  93. */
  94. void determineMultisampleSettings(UINT32 multisampleCount, DXGI_FORMAT format, DXGI_SAMPLE_DESC* outputSampleDesc);
  95. /** Returns the main DXGI factory object. */
  96. IDXGIFactory* getDXGIFactory() const { return mDXGIFactory; }
  97. /** Returns the primary DX11 device object. */
  98. D3D11Device& getPrimaryDevice() const { return *mDevice; }
  99. /** Returns information describing all available drivers. */
  100. D3D11DriverList* getDriverList() const { return mDriverList; }
  101. protected:
  102. friend class D3D11RenderAPIFactory;
  103. /** @copydoc RenderAPICore::initializePrepare */
  104. void initialize() override;
  105. /** @copydoc RenderAPICore::initializeFinalize */
  106. void initializeWithWindow(const SPtr<RenderWindowCore>& primaryWindow) override;
  107. /** @copydoc RenderAPICore::destroyCore */
  108. void destroyCore() override;
  109. /**
  110. * Sets a sampler state for the specified texture unit.
  111. *
  112. * @param[in] gptype Determines to which GPU program slot to bind the sampler state.
  113. * @param[in] texUnit Texture unit index to bind the state to.
  114. * @param[in] samplerState Sampler state to bind, or null to unbind.
  115. *
  116. * @see SamplerState
  117. */
  118. void setSamplerState(GpuProgramType gptype, UINT16 texUnit, const SPtr<SamplerStateCore>& samplerState);
  119. /**
  120. * Binds a texture to the pipeline for the specified GPU program type at the specified slot. If the slot matches
  121. * the one configured in the GPU program the program will be able to access this texture on the GPU.
  122. *
  123. * @param[in] gptype Determines to which GPU program slot to bind the texture.
  124. * @param[in] texUnit Texture unit index to bind the texture to.
  125. * @param[in] texture Texture to bind.
  126. */
  127. void setTexture(GpuProgramType gptype, UINT16 texUnit, const SPtr<TextureCore>& texture);
  128. /**
  129. * Binds a texture that can be used for random load/store operations from a GPU program.
  130. *
  131. * @param[in] gptype Determines to which GPU program slot to bind the texture.
  132. * @param[in] texUnit Texture unit index to bind the texture to.
  133. * @param[in] texture Texture to bind.
  134. * @param[in] surface Determines which surface of the texture to bind.
  135. */
  136. void setLoadStoreTexture(GpuProgramType gptype, UINT16 texUnit, const SPtr<TextureCore>& texture,
  137. const TextureSurface& surface);
  138. /**
  139. * Binds a buffer that can be used for read or write operations on the GPU.
  140. *
  141. * @param[in] gptype Determines to which GPU program slot to bind the buffer.
  142. * @param[in] unit GPU program unit index to bind the buffer to.
  143. * @param[in] buffer Buffer to bind.
  144. * @param[in] loadStore If true the buffer will be bound with support for unordered reads and writes,
  145. * otherwise it will only be bound for reads.
  146. */
  147. void setBuffer(GpuProgramType gptype, UINT16 unit, const SPtr<GpuBufferCore>& buffer,
  148. bool loadStore = false);
  149. /**
  150. * Creates or retrieves a proper input layout depending on the currently set vertex shader and vertex buffer.
  151. *
  152. * Applies the input layout to the pipeline.
  153. */
  154. void applyInputLayout();
  155. /**
  156. * Recalculates actual viewport dimensions based on currently set viewport normalized dimensions and render target
  157. * and applies them for further rendering.
  158. */
  159. void applyViewport();
  160. /** Creates and populates a set of render system capabilities describing which functionality is available. */
  161. void initCapabilites(IDXGIAdapter* adapter, RenderAPICapabilities& caps) const;
  162. private:
  163. IDXGIFactory* mDXGIFactory;
  164. D3D11Device* mDevice;
  165. D3D11DriverList* mDriverList;
  166. D3D11Driver* mActiveD3DDriver;
  167. D3D_FEATURE_LEVEL mFeatureLevel;
  168. D3D11HLSLProgramFactory* mHLSLFactory;
  169. D3D11InputLayoutManager* mIAManager;
  170. std::pair<SPtr<TextureCore>, SPtr<TextureView>> mBoundUAVs[D3D11_PS_CS_UAV_REGISTER_COUNT];
  171. UINT32 mStencilRef;
  172. Rect2 mViewportNorm;
  173. D3D11_VIEWPORT mViewport;
  174. D3D11_RECT mScissorRect;
  175. SPtr<VertexDeclarationCore> mActiveVertexDeclaration;
  176. SPtr<D3D11GpuProgramCore> mActiveVertexShader;
  177. SPtr<D3D11DepthStencilStateCore> mActiveDepthStencilState;
  178. DrawOperationType mActiveDrawOp;
  179. };
  180. /** @} */
  181. }