BsRendererUtility.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsModule.h"
  6. #include "BsRect2.h"
  7. #include "BsVector2I.h"
  8. #include "BsRect2I.h"
  9. #include "BsRendererMaterial.h"
  10. namespace bs { namespace ct
  11. {
  12. /** @addtogroup Renderer-Engine-Internal
  13. * @{
  14. */
  15. /**
  16. * Shader that copies a source texture into a render target, and optionally resolves it.
  17. *
  18. * @tparam MSAA_COUNT Number of MSAA samples in the input texture. If larger than 1 the texture will be resolved
  19. * before written to the destination.
  20. * @tparam IS_COLOR If true the input is assumed to be a 4-component color texture. If false it is assumed
  21. * the input is a 1-component depth texture. This controls how is the texture resolve and is
  22. * only relevant if MSAA_COUNT > 1. Color texture MSAA samples will be averaged, while for
  23. * depth textures the minimum of all samples will be used.
  24. */
  25. template<int MSAA_COUNT, bool IS_COLOR = true>
  26. class BlitMat : public RendererMaterial<BlitMat<MSAA_COUNT, IS_COLOR>>
  27. {
  28. RMAT_DEF("Blit.bsl");
  29. public:
  30. BlitMat();
  31. /** Updates the parameter buffers used by the material. */
  32. void setParameters(const SPtr<Texture>& source);
  33. private:
  34. MaterialParamTexture mSource;
  35. };
  36. /**
  37. * Contains various utility methods that make various common operations in the renderer easier.
  38. *
  39. * @note Core thread only.
  40. */
  41. class BS_EXPORT RendererUtility : public Module<RendererUtility>
  42. {
  43. public:
  44. RendererUtility();
  45. ~RendererUtility();
  46. /**
  47. * Activates the specified material pass for rendering. Any further draw calls will be executed using this pass.
  48. *
  49. * @param[in] material Material containing the pass.
  50. * @param[in] passIdx Index of the pass in the material.
  51. * @param[in] techniqueIdx Index of the technique the pass belongs to, if the material has multiple techniques.
  52. *
  53. * @note Core thread.
  54. */
  55. void setPass(const SPtr<Material>& material, UINT32 passIdx = 0, UINT32 techniqueIdx = 0);
  56. /**
  57. * Activates the specified material pass for compute. Any further dispatch calls will be executed using this pass.
  58. *
  59. * @param[in] material Material containing the pass.
  60. * @param[in] passIdx Index of the pass in the material.
  61. *
  62. * @note Core thread.
  63. */
  64. void setComputePass(const SPtr<Material>& material, UINT32 passIdx = 0);
  65. /**
  66. * Sets parameters (textures, samplers, buffers) for the currently active pass.
  67. *
  68. * @param[in] params Object containing the parameters.
  69. * @param[in] passIdx Pass for which to set the parameters.
  70. *
  71. * @note Core thread.
  72. */
  73. void setPassParams(const SPtr<GpuParamsSet>& params, UINT32 passIdx = 0);
  74. /**
  75. * Draws the specified mesh.
  76. *
  77. * @param[in] mesh Mesh to draw.
  78. * @param[in] numInstances Number of times to draw the mesh using instanced rendering.
  79. *
  80. * @note Core thread.
  81. */
  82. void draw(const SPtr<MeshBase>& mesh, UINT32 numInstances = 1);
  83. /**
  84. * Draws the specified mesh.
  85. *
  86. * @param[in] mesh Mesh to draw.
  87. * @param[in] subMesh Portion of the mesh to draw.
  88. * @param[in] numInstances Number of times to draw the mesh using instanced rendering.
  89. *
  90. * @note Core thread.
  91. */
  92. void draw(const SPtr<MeshBase>& mesh, const SubMesh& subMesh, UINT32 numInstances = 1);
  93. /**
  94. * Draws the specified mesh with an additional vertex buffer containing morph shape vertices.
  95. *
  96. * @param[in] mesh Mesh to draw.
  97. * @param[in] subMesh Portion of the mesh to draw.
  98. * @param[in] morphVertices Buffer containing the morph shape vertices. Will be bound to stream 1.
  99. * Expected to contain the same number of vertices as the source mesh.
  100. * @param[in] morphVertexDeclaration Vertex declaration describing vertices of the provided mesh and the vertices
  101. * provided in the morph vertex buffer.
  102. *
  103. * @note Core thread.
  104. */
  105. void drawMorph(const SPtr<MeshBase>& mesh, const SubMesh& subMesh, const SPtr<VertexBuffer>& morphVertices,
  106. const SPtr<VertexDeclaration>& morphVertexDeclaration);
  107. /**
  108. * Blits contents of the provided texture into the currently bound render target. If the provided texture contains
  109. * multiple samples, they will be resolved.
  110. *
  111. * @param[in] texture Source texture to blit.
  112. * @param[in] area Area of the source texture to blit in pixels. If width or height is zero it is assumed
  113. * the entire texture should be blitted.
  114. * @param[in] flipUV If true, vertical UV coordinate will be flipped upside down.
  115. * @param[in] isDepth If true, the input texture is assumed to be a depth texture (instead of a color one).
  116. * Multisampled depth textures will be resolved by taking the minimum value of all samples,
  117. * unlike color textures which wil be averaged.
  118. */
  119. void blit(const SPtr<Texture>& texture, const Rect2I& area = Rect2I::EMPTY, bool flipUV = false,
  120. bool isDepth = false);
  121. /**
  122. * Draws a quad over the entire viewport in normalized device coordinates.
  123. *
  124. * @param[in] uv UV coordinates to assign to the corners of the quad.
  125. * @param[in] textureSize Size of the texture the UV coordinates are specified for. If the UV coordinates are
  126. * already in normalized (0, 1) range then keep this value as is. If the UV coordinates
  127. * are in texels then set this value to the texture size so they can be normalized
  128. * internally.
  129. * @param[in] numInstances How many instances of the quad to draw (using instanced rendering). Useful when
  130. * drawing to 3D textures.
  131. * @param[in] flipUV If true, vertical UV coordinate will be flipped upside down.
  132. *
  133. * @note Core thread.
  134. */
  135. void drawScreenQuad(const Rect2& uv, const Vector2I& textureSize = Vector2I(1, 1),
  136. UINT32 numInstances = 1, bool flipUV = false);
  137. /**
  138. * Draws a quad over the entire viewport in normalized device coordinates.
  139. *
  140. * @param[in] numInstances How many instances of the quad to draw (using instanced rendering). Useful when
  141. * drawing to 3D textures.
  142. *
  143. * @note Core thread.
  144. */
  145. void drawScreenQuad(UINT32 numInstances = 1)
  146. {
  147. Rect2 uv(0.0f, 0.0f, 1.0f, 1.0f);
  148. Vector2I textureSize(1, 1);
  149. drawScreenQuad(uv, textureSize, numInstances);
  150. }
  151. /** Returns a stencil mesh used for a radial light (a unit sphere). */
  152. SPtr<Mesh> getRadialLightStencil() const { return mPointLightStencilMesh; }
  153. /**
  154. * Returns a stencil mesh used for a spot light. Actual vertex positions need to be computed in shader as this
  155. * method will return uninitialized vertex positions.
  156. */
  157. SPtr<Mesh> getSpotLightStencil() const { return mSpotLightStencilMesh; }
  158. /** Returns a mesh that can be used for rendering a skybox. */
  159. SPtr<Mesh> getSkyBoxMesh() const { return mSkyBoxMesh; }
  160. private:
  161. SPtr<Mesh> mFullScreenQuadMesh;
  162. SPtr<Mesh> mPointLightStencilMesh;
  163. SPtr<Mesh> mSpotLightStencilMesh;
  164. SPtr<Mesh> mSkyBoxMesh;
  165. BlitMat<1, true> mBlitMat_Color_NoMSAA;
  166. BlitMat<2, true> mBlitMat_Color_MSAA2x;
  167. BlitMat<4, true> mBlitMat_Color_MSAA4x;
  168. BlitMat<8, true> mBlitMat_Color_MSAA8x;
  169. BlitMat<2, false> mBlitMat_Depth_MSAA2x;
  170. BlitMat<4, false> mBlitMat_Depth_MSAA4x;
  171. BlitMat<8, false> mBlitMat_Depth_MSAA8x;
  172. };
  173. /** Provides easy access to RendererUtility. */
  174. BS_EXPORT RendererUtility& gRendererUtility();
  175. /** @} */
  176. }}