RendererVK.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 bool 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. VkSemaphore AllocateSemaphore();
  61. void FreeSemaphore(VkSemaphore inSemaphore);
  62. VkInstance mInstance = VK_NULL_HANDLE;
  63. #ifdef JPH_DEBUG
  64. VkDebugUtilsMessengerEXT mDebugMessenger = VK_NULL_HANDLE;
  65. #endif
  66. VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
  67. VkPhysicalDeviceMemoryProperties mMemoryProperties;
  68. VkDevice mDevice = VK_NULL_HANDLE;
  69. uint32 mGraphicsQueueIndex = 0;
  70. uint32 mPresentQueueIndex = 0;
  71. VkQueue mGraphicsQueue = VK_NULL_HANDLE;
  72. VkQueue mPresentQueue = VK_NULL_HANDLE;
  73. VkSurfaceKHR mSurface = VK_NULL_HANDLE;
  74. VkSwapchainKHR mSwapChain = VK_NULL_HANDLE;
  75. bool mSubOptimalSwapChain = false;
  76. Array<VkImage> mSwapChainImages;
  77. VkFormat mSwapChainImageFormat;
  78. VkExtent2D mSwapChainExtent;
  79. Array<VkImageView> mSwapChainImageViews;
  80. VkImage mDepthImage = VK_NULL_HANDLE;
  81. VkDeviceMemory mDepthImageMemory = VK_NULL_HANDLE;
  82. VkImageView mDepthImageView = VK_NULL_HANDLE;
  83. VkDescriptorSetLayout mDescriptorSetLayoutUBO = VK_NULL_HANDLE;
  84. VkDescriptorSetLayout mDescriptorSetLayoutTexture = VK_NULL_HANDLE;
  85. VkDescriptorPool mDescriptorPool = VK_NULL_HANDLE;
  86. VkDescriptorSet mDescriptorSets[cFrameCount];
  87. VkDescriptorSet mDescriptorSetsOrtho[cFrameCount];
  88. VkSampler mTextureSamplerShadow = VK_NULL_HANDLE;
  89. VkSampler mTextureSamplerRepeat = VK_NULL_HANDLE;
  90. VkRenderPass mRenderPassShadow = VK_NULL_HANDLE;
  91. VkRenderPass mRenderPass = VK_NULL_HANDLE;
  92. VkPipelineLayout mPipelineLayout = VK_NULL_HANDLE;
  93. VkFramebuffer mShadowFrameBuffer = VK_NULL_HANDLE;
  94. Array<VkFramebuffer> mSwapChainFramebuffers;
  95. uint32 mImageIndex = 0;
  96. VkCommandPool mCommandPool = VK_NULL_HANDLE;
  97. VkCommandBuffer mCommandBuffers[cFrameCount];
  98. Array<VkSemaphore> mAvailableSemaphores;
  99. Array<VkSemaphore> mImageAvailableSemaphores;
  100. Array<VkSemaphore> mRenderFinishedSemaphores;
  101. VkFence mInFlightFences[cFrameCount];
  102. Ref<TextureVK> mShadowMap;
  103. unique_ptr<ConstantBufferVK> mVertexShaderConstantBufferProjection[cFrameCount];
  104. unique_ptr<ConstantBufferVK> mVertexShaderConstantBufferOrtho[cFrameCount];
  105. unique_ptr<ConstantBufferVK> mPixelShaderConstantBuffer[cFrameCount];
  106. struct Key
  107. {
  108. bool operator == (const Key &inRHS) const
  109. {
  110. return mSize == inRHS.mSize && mUsage == inRHS.mUsage && mProperties == inRHS.mProperties;
  111. }
  112. VkDeviceSize mSize;
  113. VkBufferUsageFlags mUsage;
  114. VkMemoryPropertyFlags mProperties;
  115. };
  116. JPH_MAKE_HASH_STRUCT(Key, KeyHasher, t.mSize, t.mUsage, t.mProperties)
  117. // We try to recycle buffers from frame to frame
  118. using BufferCache = UnorderedMap<Key, Array<BufferVK>, KeyHasher>;
  119. BufferCache mFreedBuffers[cFrameCount];
  120. BufferCache mBufferCache;
  121. // Smaller allocations (from cMinAllocSize to cMaxAllocSize) will be done in blocks of cBlockSize bytes.
  122. // We do this because there is a limit to the number of allocations that we can make in Vulkan.
  123. static constexpr VkDeviceSize cMinAllocSize = 512;
  124. static constexpr VkDeviceSize cMaxAllocSize = 65536;
  125. static constexpr VkDeviceSize cBlockSize = 524288;
  126. JPH_MAKE_HASH_STRUCT(Key, MemKeyHasher, t.mUsage, t.mProperties, t.mSize)
  127. struct Memory
  128. {
  129. VkDeviceMemory mMemory;
  130. VkDeviceSize mOffset;
  131. };
  132. using MemoryCache = UnorderedMap<Key, Array<Memory>, KeyHasher>;
  133. MemoryCache mMemoryCache;
  134. uint32 mNumAllocations = 0;
  135. uint32 mMaxNumAllocations = 0;
  136. VkDeviceSize mTotalAllocated = 0;
  137. VkDeviceSize mMaxTotalAllocated = 0;
  138. };