Renderer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <Renderer/Frustum.h>
  7. #include <Renderer/PipelineState.h>
  8. #include <Renderer/VertexShader.h>
  9. #include <Renderer/PixelShader.h>
  10. #include <Renderer/RenderPrimitive.h>
  11. #include <Renderer/RenderInstances.h>
  12. #include <memory>
  13. #include <functional>
  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() = default;
  31. /// Initialize DirectX
  32. virtual void Initialize();
  33. /// Get window size
  34. int GetWindowWidth() { return mWindowWidth; }
  35. int GetWindowHeight() { return mWindowHeight; }
  36. #ifdef JPH_PLATFORM_WINDOWS
  37. /// Access to the window handle
  38. HWND GetWindowHandle() const { return mhWnd; }
  39. #elif defined(JPH_PLATFORM_LINUX)
  40. /// Access to the window handle
  41. Display * GetDisplay() const { return mDisplay; }
  42. Window GetWindow() const { return mWindow; }
  43. using EventListener = std::function<void(const XEvent &)>;
  44. void SetEventListener(const EventListener &inListener) { mEventListener = inListener; }
  45. #endif // JPH_PLATFORM_WINDOWS
  46. /// Update the system window, returns false if the application should quit
  47. bool WindowUpdate();
  48. /// Start / end drawing a frame
  49. virtual void BeginFrame(const CameraState &inCamera, float inWorldScale);
  50. virtual void EndShadowPass() = 0;
  51. virtual void EndFrame();
  52. /// Switch between orthographic and 3D projection mode
  53. virtual void SetProjectionMode() = 0;
  54. virtual void SetOrthoMode() = 0;
  55. /// Create texture from an image surface
  56. virtual Ref<Texture> CreateTexture(const Surface *inSurface) = 0;
  57. /// Compile a vertex shader
  58. virtual Ref<VertexShader> CreateVertexShader(const char *inFileName) = 0;
  59. /// Compile a pixel shader
  60. virtual Ref<PixelShader> CreatePixelShader(const char *inFileName) = 0;
  61. /// Create pipeline state object that defines the complete state of how primitives should be rendered
  62. 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;
  63. /// Create a render primitive
  64. virtual RenderPrimitive * CreateRenderPrimitive(PipelineState::ETopology inType) = 0;
  65. /// Create render instances object to allow drawing batches of objects
  66. virtual RenderInstances * CreateRenderInstances() = 0;
  67. /// Get the shadow map texture
  68. virtual Texture * GetShadowMap() const = 0;
  69. /// Get the camera state / frustum (only valid between BeginFrame() / EndFrame())
  70. const CameraState & GetCameraState() const { JPH_ASSERT(mInFrame); return mCameraState; }
  71. const Frustum & GetCameraFrustum() const { JPH_ASSERT(mInFrame); return mCameraFrustum; }
  72. /// Offset relative to which the world is rendered, helps avoiding rendering artifacts at big distances
  73. RVec3 GetBaseOffset() const { return mBaseOffset; }
  74. void SetBaseOffset(RVec3 inOffset) { mBaseOffset = inOffset; }
  75. /// Get the light frustum (only valid between BeginFrame() / EndFrame())
  76. const Frustum & GetLightFrustum() const { JPH_ASSERT(mInFrame); return mLightFrustum; }
  77. /// How many frames our pipeline is
  78. static const uint cFrameCount = 2;
  79. /// Size of the shadow map will be cShadowMapSize x cShadowMapSize pixels
  80. static const uint cShadowMapSize = 4096;
  81. /// Which frame is currently rendering (to keep track of which buffers are free to overwrite)
  82. uint GetCurrentFrameIndex() const { JPH_ASSERT(mInFrame); return mFrameIndex; }
  83. /// Callback when the window resizes and the back buffer needs to be adjusted
  84. virtual void OnWindowResize();
  85. protected:
  86. struct VertexShaderConstantBuffer
  87. {
  88. Mat44 mView;
  89. Mat44 mProjection;
  90. Mat44 mLightView;
  91. Mat44 mLightProjection;
  92. };
  93. struct PixelShaderConstantBuffer
  94. {
  95. Vec4 mCameraPos;
  96. Vec4 mLightPos;
  97. };
  98. #ifdef JPH_PLATFORM_WINDOWS
  99. HWND mhWnd;
  100. #elif defined(JPH_PLATFORM_LINUX)
  101. Display * mDisplay;
  102. Window mWindow;
  103. Atom mWmDeleteWindow;
  104. EventListener mEventListener;
  105. #endif
  106. int mWindowWidth = 1920;
  107. int mWindowHeight = 1080;
  108. float mPerspectiveYSign = 1.0f; ///< Sign for the Y coordinate in the projection matrix (1 for DX, -1 for Vulkan)
  109. bool mInFrame = false; ///< If we're within a BeginFrame() / EndFrame() pair
  110. CameraState mCameraState;
  111. RVec3 mBaseOffset { RVec3::sZero() }; ///< Offset to subtract from the camera position to deal with large worlds
  112. Frustum mCameraFrustum;
  113. Frustum mLightFrustum;
  114. uint mFrameIndex = 0; ///< Current frame index (0 or 1)
  115. VertexShaderConstantBuffer mVSBuffer;
  116. VertexShaderConstantBuffer mVSBufferOrtho;
  117. PixelShaderConstantBuffer mPSBuffer;
  118. };