Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
GPUInternal.hpp
1#pragma once
2
3#include "Common.hpp"
4#include "graphics/GPUObjects.hpp"
5#include "graphics/DescriptorAllocator.hpp"
6#include "graphics/QueryAllocator.hpp"
7#include "graphics/PipelineCache.hpp"
8#include "graphics/SamplerLibrary.hpp"
9#include "resources/Buffer.hpp"
10
11#include <deque>
12#include <functional>
13
14
15// Forward declaration.
16class Texture;
17
23#ifdef DEBUG
25 #define VK_RETURN_CHECK(F, L) do {\
26 VkResult res = (F);\
27 if(res != VK_SUCCESS){\
28 Log::Error() << Log::GPU << "Vulkan return error at line " << L << std::endl;\
29 VkUtils::checkResult(res);\
30 }\
31 } while(0);
32#else
34#define VK_RETURN_CHECK(F, L) (F);
35#endif
36
38#define VK_RET(F) VK_RETURN_CHECK((F), __LINE__)
39
47 VkImageView view = VK_NULL_HANDLE;
48 VkSampler sampler = VK_NULL_HANDLE;
49 VkImage image = VK_NULL_HANDLE;
50 VkBuffer buffer = VK_NULL_HANDLE;
51 VmaAllocation data = VK_NULL_HANDLE;
52 uint64_t frame = 0;
53};
54
61 std::shared_ptr<Buffer> dstBuffer;
62 std::unique_ptr<Texture> dstTexture;
63 std::function<void(const Texture&)> callback;
64 glm::uvec2 dstImageRange{0u,0u};
65 uint64_t frame = 0;
66 GPUAsyncTask id = 0;
67};
68
72struct GPUContext {
73
74 VkInstance instance = VK_NULL_HANDLE;
75 VkDebugUtilsMessengerEXT debugMessenger = VK_NULL_HANDLE;
76 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
77 VkDevice device = VK_NULL_HANDLE;
78 VkSurfaceKHR surface = VK_NULL_HANDLE;
79 VkCommandPool commandPool = VK_NULL_HANDLE;
80 std::vector<VkCommandBuffer> renderCommandBuffers;
81 std::vector<VkCommandBuffer> uploadCommandBuffers;
82 VkQueue graphicsQueue= VK_NULL_HANDLE;
83 VkQueue presentQueue= VK_NULL_HANDLE;
85 std::unordered_map<GPUQuery::Type, QueryAllocator> queryAllocators;
88
89 std::deque<ResourceToDelete> resourcesToDelete;
90 std::deque<AsyncTextureTask> textureTasks;
91 uint64_t tasksCount = 0;
92
93 VkPipeline graphicsPipeline = VK_NULL_HANDLE;
94 VkPipeline computePipeline = VK_NULL_HANDLE;
95
96 uint32_t graphicsId = 0;
97 uint32_t presentId = 0;
98
99 uint64_t frameIndex = 0;
100 uint32_t swapIndex = 0;
101
102 double timestep = 0.0;
103 size_t uniformAlignment = 0;
104 bool portability = false;
105 const uint frameCount = 2;
106 bool newRenderPass = true;
107 bool hadRenderPass = false;
108 bool inRenderPass = false;
109 bool markersEnabled = false;
110
112 void nextFrame(){
113 ++frameIndex;
114 swapIndex = (uint32_t)(frameIndex % (uint64_t)frameCount);
115 }
116
118 VkCommandBuffer& getRenderCommandBuffer(){
120 }
121
123 VkCommandBuffer& getUploadCommandBuffer(){
125 }
126
127};
128
137VKAPI_ATTR VkBool32 VKAPI_CALL vkDebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT* callbackData, void* userData);
138
142namespace VkUtils {
143
148 bool checkLayersSupport(const std::vector<const char*> & requestedLayers);
149
154 bool checkExtensionsSupport(const std::vector<const char*> & requestedExtensions);
155
161 std::vector<const char *> getRequiredInstanceExtensions(bool enableDebugMarkers, bool enablePortability);
162
169 bool checkDeviceExtensionsSupport(VkPhysicalDevice device, const std::vector<const char*> & requestedExtensions, bool & hasPortability);
170
178 bool getQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface, int & graphicsFamily, int & presentFamily);
179
184 Layout convertFormat(const VkFormat& format);
185
193 Layout findSupportedFormat(const VkPhysicalDevice & physicalDevice, const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
194
200 void typesFromShape(const TextureShape & shape, VkImageType & imgType, VkImageViewType & viewType);
201
206 VkCommandBuffer beginSyncOperations(GPUContext & context);
207
212 void endSyncOperations(VkCommandBuffer & commandBuffer, GPUContext & context);
213
223 void imageLayoutBarrier(VkCommandBuffer& commandBuffer, GPUTexture& texture, VkImageLayout newLayout, uint mipStart, uint mipCount, uint layerStart, uint layerCount);
224
231 void mipLayoutBarrier(VkCommandBuffer& commandBuffer, const Texture& texture, VkImageLayout newLayout, uint mip);
232
238 void textureLayoutBarrier(VkCommandBuffer& commandBuffer, const Texture& texture, VkImageLayout newLayout);
239
244 void createCommandBuffers(GPUContext & context, uint count);
245
249 void checkResult(VkResult status);
250
255 VkSamplerAddressMode getGPUWrapping(Wrap mode);
256
262 void getGPUFilters(Filter filtering, VkFilter & imgFiltering, VkSamplerMipmapMode & mipFiltering);
263
269 unsigned int getGPULayout(Layout typedFormat, VkFormat & format);
270
284 glm::uvec2 copyTextureRegionToBuffer(VkCommandBuffer& commandBuffer, const Texture & srcTexture, std::shared_ptr<Buffer> & dstBuffer, uint mipStart, uint mipCount, uint layerStart, uint layerCount, const glm::uvec2& offset, const glm::uvec2& size);
285
303 void blitTexture(VkCommandBuffer& commandBuffer, const Texture& src, const Texture& dst, uint mipStartSrc, uint mipStartDst, uint mipCount, uint layerStartSrc, uint layerStartDst, uint layerCount, const glm::uvec2& srcBaseOffset, const glm::uvec2& srcBaseSize, const glm::uvec2& dstBaseOffset, const glm::uvec2& dstBaseSize, Filter filter);
304
313 void setDebugName(GPUContext& context, VkObjectType type, uint64_t handle, const char* format, ...);
314
315}
316
317STD_HASH(VkImageLayout);
Manage descriptor set allocations by creating and reusing internal descriptor pools.
Definition: DescriptorAllocator.hpp:14
Store a texture data on the GPU.
Definition: GPUObjects.hpp:18
Create and reuse GPU pipelines based on a given state. This supports both graphics and compute pipeli...
Definition: PipelineCache.hpp:18
Manage all samplers for GPU textures. Samplers are shared between all shader programs,...
Definition: SamplerLibrary.hpp:13
Represents a texture containing one or more images, stored on the CPU and/or GPU.
Definition: Texture.hpp:12
VKAPI_ATTR VkBool32 VKAPI_CALL vkDebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT *callbackData, void *userData)
Definition: GPUInternal.cpp:136
Filter
The filtering mode of a texture: we deduce the magnification filter from the minification filter for ...
Definition: GPUTypes.hpp:198
TextureShape
The shape of a texture: dimensions, layers organisation.
Definition: GPUTypes.hpp:153
Layout
The layout of a texture: components count and type.
Definition: GPUTypes.hpp:225
Wrap
The wrapping mode of a texture.
Definition: GPUTypes.hpp:213
Utility functions for the Vulkan backend.
Definition: GPUInternal.hpp:142
unsigned int getGPULayout(Layout typedFormat, VkFormat &format)
Definition: GPUInternal.cpp:527
Layout findSupportedFormat(const VkPhysicalDevice &physicalDevice, const std::vector< VkFormat > &candidates, VkImageTiling tiling, VkFormatFeatureFlags features)
Definition: GPUInternal.cpp:226
VkCommandBuffer beginSyncOperations(GPUContext &context)
Definition: GPUInternal.cpp:269
Layout convertFormat(const VkFormat &format)
Definition: GPUInternal.cpp:175
std::vector< const char * > getRequiredInstanceExtensions(bool enableDebugMarkers, bool enablePortability)
Definition: GPUInternal.cpp:52
bool checkExtensionsSupport(const std::vector< const char * > &requestedExtensions)
Definition: GPUInternal.cpp:30
void createCommandBuffers(GPUContext &context, uint count)
Definition: GPUInternal.cpp:419
VkSamplerAddressMode getGPUWrapping(Wrap mode)
Definition: GPUInternal.cpp:502
bool getQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface, int &graphicsFamily, int &presentFamily)
Definition: GPUInternal.cpp:101
glm::uvec2 copyTextureRegionToBuffer(VkCommandBuffer &commandBuffer, const Texture &srcTexture, std::shared_ptr< Buffer > &dstBuffer, uint mipStart, uint mipCount, uint layerStart, uint layerCount, const glm::uvec2 &offset, const glm::uvec2 &size)
Definition: GPUInternal.cpp:591
void endSyncOperations(VkCommandBuffer &commandBuffer, GPUContext &context)
Definition: GPUInternal.cpp:290
bool checkDeviceExtensionsSupport(VkPhysicalDevice device, const std::vector< const char * > &requestedExtensions, bool &hasPortability)
Definition: GPUInternal.cpp:68
void setDebugName(GPUContext &context, VkObjectType type, uint64_t handle, const char *format,...)
Definition: GPUInternal.cpp:721
void typesFromShape(const TextureShape &shape, VkImageType &imgType, VkImageViewType &viewType)
Definition: GPUInternal.cpp:243
void mipLayoutBarrier(VkCommandBuffer &commandBuffer, const Texture &texture, VkImageLayout newLayout, uint mip)
Definition: GPUInternal.cpp:405
void textureLayoutBarrier(VkCommandBuffer &commandBuffer, const Texture &texture, VkImageLayout newLayout)
Definition: GPUInternal.cpp:412
void blitTexture(VkCommandBuffer &commandBuffer, const Texture &src, const Texture &dst, uint mipStartSrc, uint mipStartDst, uint mipCount, uint layerStartSrc, uint layerStartDst, uint layerCount, const glm::uvec2 &srcBaseOffset, const glm::uvec2 &srcBaseSize, const glm::uvec2 &dstBaseOffset, const glm::uvec2 &dstBaseSize, Filter filter)
Definition: GPUInternal.cpp:663
void checkResult(VkResult status)
Definition: GPUInternal.cpp:452
void imageLayoutBarrier(VkCommandBuffer &commandBuffer, GPUTexture &texture, VkImageLayout newLayout, uint mipStart, uint mipCount, uint layerStart, uint layerCount)
Definition: GPUInternal.cpp:304
bool checkLayersSupport(const std::vector< const char * > &requestedLayers)
Definition: GPUInternal.cpp:8
void getGPUFilters(Filter filtering, VkFilter &imgFiltering, VkSamplerMipmapMode &mipFiltering)
Definition: GPUInternal.cpp:510
Request for an asynchronous texture download. Data has been copied from the source texture to a buffe...
Definition: GPUInternal.hpp:60
glm::uvec2 dstImageRange
First index and number of images to populate (some mips/layers can be skipped).
Definition: GPUInternal.hpp:64
std::unique_ptr< Texture > dstTexture
Destination texture whose images have to be populated.
Definition: GPUInternal.hpp:62
std::shared_ptr< Buffer > dstBuffer
Transfer buffer containing the texture data (mappable).
Definition: GPUInternal.hpp:61
uint64_t frame
Frame where the download was requested.
Definition: GPUInternal.hpp:65
std::function< void(const Texture &)> callback
Callback to execute once the images creation is complete.
Definition: GPUInternal.hpp:63
Global GPU context, storing all structures used for resource allocation and tracking,...
Definition: GPUInternal.hpp:72
PipelineCache pipelineCache
Pipeline cache and creation.
Definition: GPUInternal.hpp:86
uint32_t swapIndex
Current buffered frame (in 0, frameCount-1).
Definition: GPUInternal.hpp:100
std::deque< AsyncTextureTask > textureTasks
List of async tasks waiting for completion.
Definition: GPUInternal.hpp:90
uint32_t presentId
Present queue index.
Definition: GPUInternal.hpp:97
double timestep
Query timing timestep.
Definition: GPUInternal.hpp:102
VkPipeline graphicsPipeline
Current graphics pipeline.
Definition: GPUInternal.hpp:93
bool hadRenderPass
Has a render pass just ended.
Definition: GPUInternal.hpp:107
std::vector< VkCommandBuffer > uploadCommandBuffers
Per-frame command buffers.
Definition: GPUInternal.hpp:81
VkSurfaceKHR surface
Native surface handle.
Definition: GPUInternal.hpp:78
std::vector< VkCommandBuffer > renderCommandBuffers
Per-frame command buffers.
Definition: GPUInternal.hpp:80
DescriptorAllocator descriptorAllocator
Descriptor sets common allocator.
Definition: GPUInternal.hpp:84
uint32_t graphicsId
Graphics queue index.
Definition: GPUInternal.hpp:96
VkPhysicalDevice physicalDevice
Native physical device handle.
Definition: GPUInternal.hpp:76
bool inRenderPass
Is a rendering pass currently active.
Definition: GPUInternal.hpp:108
bool portability
If the portability extension is present, we have to enable it.
Definition: GPUInternal.hpp:104
VkCommandPool commandPool
Command pool for all frames.
Definition: GPUInternal.hpp:79
std::deque< ResourceToDelete > resourcesToDelete
List of resources waiting for deletion.
Definition: GPUInternal.hpp:89
VkCommandBuffer & getUploadCommandBuffer()
Definition: GPUInternal.hpp:123
uint64_t tasksCount
Number of async tasks created.
Definition: GPUInternal.hpp:91
void nextFrame()
Move to the next frame.
Definition: GPUInternal.hpp:112
bool markersEnabled
Are debug markers and labels enabled.
Definition: GPUInternal.hpp:109
VkDebugUtilsMessengerEXT debugMessenger
Native debug extension handle.
Definition: GPUInternal.hpp:75
VkQueue graphicsQueue
Graphics submission queue.
Definition: GPUInternal.hpp:82
VkQueue presentQueue
Presentation submission queue.
Definition: GPUInternal.hpp:83
SamplerLibrary samplerLibrary
List of static samplers shared by all programs.
Definition: GPUInternal.hpp:87
VkCommandBuffer & getRenderCommandBuffer()
Definition: GPUInternal.hpp:118
VkInstance instance
Native instance handle.
Definition: GPUInternal.hpp:74
uint64_t frameIndex
Current frame index.
Definition: GPUInternal.hpp:99
bool newRenderPass
Has a render pass just started (pipeline needs to be re-bound).
Definition: GPUInternal.hpp:106
std::unordered_map< GPUQuery::Type, QueryAllocator > queryAllocators
Per-type query buffered allocators.
Definition: GPUInternal.hpp:85
VkDevice device
Native logical device handle.
Definition: GPUInternal.hpp:77
size_t uniformAlignment
Minimal buffer alignment.
Definition: GPUInternal.hpp:103
VkPipeline computePipeline
Current compute pipeline.
Definition: GPUInternal.hpp:94
const uint frameCount
Number of buffered frames (should be lower or equal to the swapchain image count).
Definition: GPUInternal.hpp:105
Request for the buffered deletion of a resource. Any of the handles stored below is optional,...
Definition: GPUInternal.hpp:46
VkImageView view
Image view to delete.
Definition: GPUInternal.hpp:47
VkSampler sampler
Sampler to delete.
Definition: GPUInternal.hpp:48
VmaAllocation data
Internal allocation to free.
Definition: GPUInternal.hpp:51
VkImage image
Image to delete.
Definition: GPUInternal.hpp:49
uint64_t frame
Frame where the deletion was requested.
Definition: GPUInternal.hpp:52
VkBuffer buffer
Buffer to delete.
Definition: GPUInternal.hpp:50