RendererVK.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Renderer/Renderer.h>
  6. #include <Renderer/VK/ConstantBufferVK.h>
  7. #include <Renderer/VK/TextureVK.h>
  8. #include <Jolt/Core/UnorderedMap.h>
  9. #include <vulkan/vulkan.h>
  10. /// Vulkan renderer
  11. class RendererVK : public Renderer
  12. {
  13. public:
  14. /// Destructor
  15. virtual ~RendererVK() override;
  16. // See: Renderer
  17. virtual void Initialize(ApplicationWindow *inWindow) override;
  18. virtual void BeginFrame(const CameraState &inCamera, float inWorldScale) override;
  19. virtual void EndShadowPass() override;
  20. virtual void EndFrame() override;
  21. virtual void SetProjectionMode() override;
  22. virtual void SetOrthoMode() override;
  23. virtual Ref<Texture> CreateTexture(const Surface *inSurface) override;
  24. virtual Ref<VertexShader> CreateVertexShader(const char *inName) override;
  25. virtual Ref<PixelShader> CreatePixelShader(const char *inName) override;
  26. 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) override;
  27. virtual RenderPrimitive * CreateRenderPrimitive(PipelineState::ETopology inType) override;
  28. virtual RenderInstances * CreateRenderInstances() override;
  29. virtual Texture * GetShadowMap() const override { return mShadowMap.GetPtr(); }
  30. virtual void OnWindowResize() override;
  31. VkDevice GetDevice() const { return mDevice; }
  32. VkDescriptorPool GetDescriptorPool() const { return mDescriptorPool; }
  33. VkDescriptorSetLayout GetDescriptorSetLayoutTexture() const { return mDescriptorSetLayoutTexture; }
  34. VkSampler GetTextureSamplerRepeat() const { return mTextureSamplerRepeat; }
  35. VkSampler GetTextureSamplerShadow() const { return mTextureSamplerShadow; }
  36. VkRenderPass GetRenderPassShadow() const { return mRenderPassShadow; }
  37. VkRenderPass GetRenderPass() const { return mRenderPass; }
  38. VkPipelineLayout GetPipelineLayout() const { return mPipelineLayout; }
  39. VkCommandBuffer GetCommandBuffer() { JPH_ASSERT(mInFrame); return mCommandBuffers[mFrameIndex]; }
  40. VkCommandBuffer StartTempCommandBuffer();
  41. void EndTempCommandBuffer(VkCommandBuffer inCommandBuffer);
  42. void AllocateMemory(VkDeviceSize inSize, uint32 inMemoryTypeBits, VkMemoryPropertyFlags inProperties, VkDeviceMemory &outMemory);
  43. void FreeMemory(VkDeviceMemory inMemory, VkDeviceSize inSize);
  44. void CreateBuffer(VkDeviceSize inSize, VkBufferUsageFlags inUsage, VkMemoryPropertyFlags inProperties, BufferVK &outBuffer);
  45. void CopyBuffer(VkBuffer inSrc, VkBuffer inDst, VkDeviceSize inSize);
  46. void CreateDeviceLocalBuffer(const void *inData, VkDeviceSize inSize, VkBufferUsageFlags inUsage, BufferVK &outBuffer);
  47. void FreeBuffer(BufferVK &ioBuffer);
  48. unique_ptr<ConstantBufferVK> CreateConstantBuffer(VkDeviceSize inBufferSize);
  49. void CreateImage(uint32 inWidth, uint32 inHeight, VkFormat inFormat, VkImageTiling inTiling, VkImageUsageFlags inUsage, VkMemoryPropertyFlags inProperties, VkImage &outImage, VkDeviceMemory &outMemory);
  50. void DestroyImage(VkImage inImage, VkDeviceMemory inMemory);
  51. VkImageView CreateImageView(VkImage inImage, VkFormat inFormat, VkImageAspectFlags inAspectFlags);
  52. VkFormat FindDepthFormat();
  53. private:
  54. uint32 FindMemoryType(uint32 inTypeFilter, VkMemoryPropertyFlags inProperties);
  55. void FreeBufferInternal(BufferVK &ioBuffer);
  56. VkSurfaceFormatKHR SelectFormat(VkPhysicalDevice inDevice);
  57. void CreateSwapChain(VkPhysicalDevice inDevice);
  58. void DestroySwapChain();
  59. void UpdateViewPortAndScissorRect(uint32 inWidth, uint32 inHeight);
  60. VkInstance mInstance = VK_NULL_HANDLE;
  61. #ifdef JPH_DEBUG
  62. VkDebugUtilsMessengerEXT mDebugMessenger = VK_NULL_HANDLE;
  63. #endif
  64. VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
  65. VkPhysicalDeviceMemoryProperties mMemoryProperties;
  66. VkDevice mDevice = VK_NULL_HANDLE;
  67. uint32 mGraphicsQueueIndex = 0;
  68. uint32 mPresentQueueIndex = 0;
  69. VkQueue mGraphicsQueue = VK_NULL_HANDLE;
  70. VkQueue mPresentQueue = VK_NULL_HANDLE;
  71. VkSurfaceKHR mSurface = VK_NULL_HANDLE;
  72. VkSwapchainKHR mSwapChain = VK_NULL_HANDLE;
  73. bool mSubOptimalSwapChain = false;
  74. Array<VkImage> mSwapChainImages;
  75. VkFormat mSwapChainImageFormat;
  76. VkExtent2D mSwapChainExtent;
  77. Array<VkImageView> mSwapChainImageViews;
  78. VkImage mDepthImage = VK_NULL_HANDLE;
  79. VkDeviceMemory mDepthImageMemory = VK_NULL_HANDLE;
  80. VkImageView mDepthImageView = VK_NULL_HANDLE;
  81. VkDescriptorSetLayout mDescriptorSetLayoutUBO = VK_NULL_HANDLE;
  82. VkDescriptorSetLayout mDescriptorSetLayoutTexture = VK_NULL_HANDLE;
  83. VkDescriptorPool mDescriptorPool = VK_NULL_HANDLE;
  84. VkDescriptorSet mDescriptorSets[cFrameCount];
  85. VkDescriptorSet mDescriptorSetsOrtho[cFrameCount];
  86. VkSampler mTextureSamplerShadow = VK_NULL_HANDLE;
  87. VkSampler mTextureSamplerRepeat = VK_NULL_HANDLE;
  88. VkRenderPass mRenderPassShadow = VK_NULL_HANDLE;
  89. VkRenderPass mRenderPass = VK_NULL_HANDLE;
  90. VkPipelineLayout mPipelineLayout = VK_NULL_HANDLE;
  91. VkFramebuffer mShadowFrameBuffer = VK_NULL_HANDLE;
  92. Array<VkFramebuffer> mSwapChainFramebuffers;
  93. uint32 mImageIndex = 0;
  94. VkCommandPool mCommandPool = VK_NULL_HANDLE;
  95. VkCommandBuffer mCommandBuffers[cFrameCount];
  96. VkSemaphore mImageAvailableSemaphores[cFrameCount];
  97. VkSemaphore mRenderFinishedSemaphores[cFrameCount];
  98. VkFence mInFlightFences[cFrameCount];
  99. Ref<TextureVK> mShadowMap;
  100. unique_ptr<ConstantBufferVK> mVertexShaderConstantBufferProjection[cFrameCount];
  101. unique_ptr<ConstantBufferVK> mVertexShaderConstantBufferOrtho[cFrameCount];
  102. unique_ptr<ConstantBufferVK> mPixelShaderConstantBuffer[cFrameCount];
  103. struct Key
  104. {
  105. bool operator == (const Key &inRHS) const
  106. {
  107. return mSize == inRHS.mSize && mUsage == inRHS.mUsage && mProperties == inRHS.mProperties;
  108. }
  109. VkDeviceSize mSize;
  110. VkBufferUsageFlags mUsage;
  111. VkMemoryPropertyFlags mProperties;
  112. };
  113. JPH_MAKE_HASH_STRUCT(Key, KeyHasher, t.mSize, t.mUsage, t.mProperties)
  114. // We try to recycle buffers from frame to frame
  115. using BufferCache = UnorderedMap<Key, Array<BufferVK>, KeyHasher>;
  116. BufferCache mFreedBuffers[cFrameCount];
  117. BufferCache mBufferCache;
  118. // Smaller allocations (from cMinAllocSize to cMaxAllocSize) will be done in blocks of cBlockSize bytes.
  119. // We do this because there is a limit to the number of allocations that we can make in Vulkan.
  120. static constexpr VkDeviceSize cMinAllocSize = 512;
  121. static constexpr VkDeviceSize cMaxAllocSize = 65536;
  122. static constexpr VkDeviceSize cBlockSize = 524288;
  123. JPH_MAKE_HASH_STRUCT(Key, MemKeyHasher, t.mUsage, t.mProperties, t.mSize)
  124. struct Memory
  125. {
  126. VkDeviceMemory mMemory;
  127. VkDeviceSize mOffset;
  128. };
  129. using MemoryCache = UnorderedMap<Key, Array<Memory>, KeyHasher>;
  130. MemoryCache mMemoryCache;
  131. uint32 mNumAllocations = 0;
  132. uint32 mMaxNumAllocations = 0;
  133. VkDeviceSize mTotalAllocated = 0;
  134. VkDeviceSize mMaxTotalAllocated = 0;
  135. };