Renderer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Image/Surface.h>
  6. #include <Window/ApplicationWindow.h>
  7. #include <Renderer/Frustum.h>
  8. #include <Renderer/PipelineState.h>
  9. #include <Renderer/VertexShader.h>
  10. #include <Renderer/PixelShader.h>
  11. #include <Renderer/RenderPrimitive.h>
  12. #include <Renderer/RenderInstances.h>
  13. #include <memory>
  14. // Forward declares
  15. class Texture;
  16. /// Camera setup
  17. struct CameraState
  18. {
  19. CameraState() : mPos(RVec3::sZero()), mForward(0, 0, -1), mUp(0, 1, 0), mFOVY(DegreesToRadians(70.0f)) { }
  20. RVec3 mPos; ///< Camera position
  21. Vec3 mForward; ///< Camera forward vector
  22. Vec3 mUp; ///< Camera up vector
  23. float mFOVY; ///< Field of view in radians in up direction
  24. };
  25. /// Responsible for rendering primitives to the screen
  26. class Renderer
  27. {
  28. public:
  29. /// Destructor
  30. virtual ~Renderer();
  31. /// Initialize renderer
  32. virtual void Initialize(ApplicationWindow *inWindow);
  33. /// Start / end drawing a frame
  34. virtual void BeginFrame(const CameraState &inCamera, float inWorldScale);
  35. virtual void EndShadowPass() = 0;
  36. virtual void EndFrame();
  37. /// Switch between orthographic and 3D projection mode
  38. virtual void SetProjectionMode() = 0;
  39. virtual void SetOrthoMode() = 0;
  40. /// Create texture from an image surface
  41. virtual Ref<Texture> CreateTexture(const Surface *inSurface) = 0;
  42. /// Compile a vertex shader
  43. virtual Ref<VertexShader> CreateVertexShader(const char *inName) = 0;
  44. /// Compile a pixel shader
  45. virtual Ref<PixelShader> CreatePixelShader(const char *inName) = 0;
  46. /// Create pipeline state object that defines the complete state of how primitives should be rendered
  47. virtual unique_ptr<PipelineState> CreatePipelineState(const VertexShader *inVertexShader, const PipelineState::EInputDescription *inInputDescription, uint inInputDescriptionCount, const PixelShader *inPixelShader, PipelineState::EDrawPass inDrawPass, PipelineState::EFillMode inFillMode, PipelineState::ETopology inTopology, PipelineState::EDepthTest inDepthTest, PipelineState::EBlendMode inBlendMode, PipelineState::ECullMode inCullMode) = 0;
  48. /// Create a render primitive
  49. virtual RenderPrimitive * CreateRenderPrimitive(PipelineState::ETopology inType) = 0;
  50. /// Create render instances object to allow drawing batches of objects
  51. virtual RenderInstances * CreateRenderInstances() = 0;
  52. /// Get the shadow map texture
  53. virtual Texture * GetShadowMap() const = 0;
  54. /// Get the camera state / frustum (only valid between BeginFrame() / EndFrame())
  55. const CameraState & GetCameraState() const { JPH_ASSERT(mInFrame); return mCameraState; }
  56. const Frustum & GetCameraFrustum() const { JPH_ASSERT(mInFrame); return mCameraFrustum; }
  57. /// Offset relative to which the world is rendered, helps avoiding rendering artifacts at big distances
  58. RVec3 GetBaseOffset() const { return mBaseOffset; }
  59. void SetBaseOffset(RVec3 inOffset) { mBaseOffset = inOffset; }
  60. /// Get the light frustum (only valid between BeginFrame() / EndFrame())
  61. const Frustum & GetLightFrustum() const { JPH_ASSERT(mInFrame); return mLightFrustum; }
  62. /// How many frames our pipeline is
  63. static const uint cFrameCount = 2;
  64. /// Size of the shadow map will be cShadowMapSize x cShadowMapSize pixels
  65. static const uint cShadowMapSize = 4096;
  66. /// Which frame is currently rendering (to keep track of which buffers are free to overwrite)
  67. uint GetCurrentFrameIndex() const { JPH_ASSERT(mInFrame); return mFrameIndex; }
  68. /// Get the window we're rendering to
  69. ApplicationWindow * GetWindow() const { return mWindow; }
  70. /// Callback when the window resizes and the back buffer needs to be adjusted
  71. virtual void OnWindowResize() = 0;
  72. /// Create a platform specific Renderer instance
  73. static Renderer * sCreate();
  74. protected:
  75. struct VertexShaderConstantBuffer
  76. {
  77. Mat44 mView;
  78. Mat44 mProjection;
  79. Mat44 mLightView;
  80. Mat44 mLightProjection;
  81. };
  82. struct PixelShaderConstantBuffer
  83. {
  84. Vec4 mCameraPos;
  85. Vec4 mLightPos;
  86. };
  87. ApplicationWindow * mWindow = nullptr; ///< The window we're rendering to
  88. float mPerspectiveYSign = 1.0f; ///< Sign for the Y coordinate in the projection matrix (1 for DX, -1 for Vulkan)
  89. bool mInFrame = false; ///< If we're within a BeginFrame() / EndFrame() pair
  90. CameraState mCameraState;
  91. RVec3 mBaseOffset { RVec3::sZero() }; ///< Offset to subtract from the camera position to deal with large worlds
  92. Frustum mCameraFrustum;
  93. Frustum mLightFrustum;
  94. uint mFrameIndex = 0; ///< Current frame index (0 or 1)
  95. VertexShaderConstantBuffer mVSBuffer;
  96. VertexShaderConstantBuffer mVSBufferOrtho;
  97. PixelShaderConstantBuffer mPSBuffer;
  98. };