Is.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_RENDERER_IS_H
  6. #define ANKI_RENDERER_IS_H
  7. #include "anki/renderer/RenderingPass.h"
  8. #include "anki/resource/TextureResource.h"
  9. #include "anki/resource/Resource.h"
  10. #include "anki/resource/ProgramResource.h"
  11. #include "anki/Gl.h"
  12. #include "anki/Math.h"
  13. #include "anki/renderer/Sm.h"
  14. #include "anki/util/StdTypes.h"
  15. #include "anki/util/Array.h"
  16. #include "anki/core/Timestamp.h"
  17. #include "anki/collision/Plane.h"
  18. namespace anki {
  19. // Forward
  20. class Camera;
  21. class PerspectiveCamera;
  22. class PointLight;
  23. class SpotLight;
  24. class LightComponent;
  25. class MoveComponent;
  26. class SpatialComponent;
  27. class FrustumComponent;
  28. class TaskCommonData;
  29. /// @addtogroup renderer
  30. /// @{
  31. /// Illumination stage
  32. class Is: private RenderingPass
  33. {
  34. friend class Renderer;
  35. friend class Sslr;
  36. friend class WriteLightsTask;
  37. public:
  38. /// @privatesection
  39. /// @{
  40. GlTextureHandle& _getRt()
  41. {
  42. return m_rt;
  43. }
  44. /// @}
  45. private:
  46. enum
  47. {
  48. COMMON_UNIFORMS_BLOCK_BINDING = 0,
  49. POINT_LIGHTS_BLOCK_BINDING = 1,
  50. SPOT_LIGHTS_BLOCK_BINDING = 2,
  51. SPOT_TEX_LIGHTS_BLOCK_BINDING = 3,
  52. TILES_BLOCK_BINDING = 4,
  53. LIGHT_IDS_BLOCK_BINDING = 5
  54. };
  55. static const U MAX_FRAMES = 3;
  56. U32 m_currentFrame = 0; ///< Cache value.
  57. /// The IS render target
  58. GlTextureHandle m_rt;
  59. /// The IS FBO
  60. GlFramebufferHandle m_fb;
  61. /// @name GPU buffers
  62. /// @{
  63. /// Contains common data for all shader programs
  64. GlBufferHandle m_commonBuffer;
  65. /// Track the updates of commonUbo
  66. Timestamp m_commonBuffUpdateTimestamp = 0;
  67. /// Contains all the lights
  68. Array<GlBufferHandle, MAX_FRAMES> m_lightsBuffers;
  69. Array<void*, MAX_FRAMES> m_lightsBufferAddresses;
  70. /// Contains the number of lights per tile
  71. Array<GlBufferHandle, MAX_FRAMES> m_tilesBuffers;
  72. Array<void*, MAX_FRAMES> m_tilesBufferAddresses;
  73. /// Contains light indices.
  74. Array<GlBufferHandle, MAX_FRAMES> m_lightIdsBuffers;
  75. Array<void*, MAX_FRAMES> m_lightIdsBufferAddresses;
  76. /// @}
  77. // Light shaders
  78. ProgramResourcePointer m_lightVert;
  79. ProgramResourcePointer m_lightFrag;
  80. GlPipelineHandle m_lightPpline;
  81. /// Shadow mapping
  82. Sm m_sm;
  83. /// Opt because many ask for it
  84. Camera* m_cam;
  85. /// If enabled the ground emmits a light
  86. Bool8 m_groundLightEnabled;
  87. /// Keep the prev light dir to avoid uniform block updates
  88. Vec3 m_prevGroundLightDir = Vec3(0.0);
  89. /// @name For drawing a quad into the active framebuffer
  90. /// @{
  91. GlBufferHandle m_quadPositionsVertBuff;
  92. /// @}
  93. /// @name Limits
  94. /// @{
  95. U16 m_maxPointLights;
  96. U32 m_maxSpotLights;
  97. U32 m_maxSpotTexLights;
  98. U32 m_maxLightIds;
  99. /// @}
  100. Is(Renderer* r);
  101. ~Is();
  102. ANKI_USE_RESULT Error init(const ConfigSet& initializer);
  103. ANKI_USE_RESULT Error run(GlCommandBufferHandle& cmdBuff);
  104. /// Called by init
  105. ANKI_USE_RESULT Error initInternal(const ConfigSet& initializer);
  106. /// Do the actual pass
  107. ANKI_USE_RESULT Error lightPass(GlCommandBufferHandle& cmdBuff);
  108. /// Prepare GL for rendering
  109. void setState(GlCommandBufferHandle& cmdBuff);
  110. /// Calculate the size of the lights UBO
  111. PtrSize calcLightsBufferSize() const;
  112. /// Calculate the size of the tile
  113. PtrSize calcTileSize() const;
  114. ANKI_USE_RESULT Error updateCommonBlock(GlCommandBufferHandle& cmdBuff);
  115. // Binning
  116. void binLights(U32 threadId, PtrSize threadsCount, TaskCommonData& data);
  117. I writePointLight(const LightComponent& light, const MoveComponent& move,
  118. const FrustumComponent& camfrc, TaskCommonData& task);
  119. I writeSpotLight(const LightComponent& lightc,
  120. const MoveComponent& lightMove, const FrustumComponent* lightFrc,
  121. const MoveComponent& camMove, const FrustumComponent& camFrc,
  122. TaskCommonData& task);
  123. void binLight(SpatialComponent& sp, U pos, U lightType,
  124. TaskCommonData& task);
  125. };
  126. /// @}
  127. } // end namespace anki
  128. #endif