8 #include "vk_mem_alloc.h"
9 #include "pipelinekey.h"
10 #include "renderutils.h"
11 #include "imagedata.h"
12 #include "rendertag.h"
15 #include <nap/service.h>
16 #include <windowevent.h>
17 #include <rendertarget.h>
25 class CameraComponentInstance;
26 class RenderableComponentInstance;
30 class DescriptorSetCache;
31 class DescriptorSetAllocator;
34 class MaterialInstance;
35 class ComputeMaterialInstance;
36 class ComputeComponentInstance;
65 bool mHeadless =
false;
67 std::vector<std::string> mLayers = {
"VK_LAYER_KHRONOS_validation" };
68 std::vector<std::string> mAdditionalExtensions = { };
71 uint32 mAnisotropicFilterSamples = 8;
72 bool mEnableHighDPIMode =
true;
73 bool mEnableCompute =
true;
74 bool mEnableCaching =
true;
75 bool mEnableDebug =
true;
76 bool mEnableRobustBufferAccess =
false;
77 bool mPrintAvailableLayers =
false;
78 bool mPrintAvailableExtensions =
false;
98 PhysicalDevice(VkPhysicalDevice device,
const VkPhysicalDeviceProperties& properties,
const VkQueueFlags& queueCapabilities,
int queueIndex);
113 const VkPhysicalDeviceProperties&
getProperties()
const {
return mProperties; }
118 const VkPhysicalDeviceFeatures&
getFeatures()
const {
return mFeatures; }
128 bool isValid()
const {
return mDevice != VK_NULL_HANDLE && mQueueIndex >= 0; }
131 VkPhysicalDevice mDevice = VK_NULL_HANDLE;
132 VkPhysicalDeviceProperties mProperties;
133 VkPhysicalDeviceFeatures mFeatures;
134 VkQueueFlags mQueueCapabilities;
135 int mQueueIndex = -1;
182 const std::string&
getName()
const {
return mName; }
187 const glm::ivec2&
getMin()
const {
return mMin; }
192 const glm::ivec2&
getMax()
const {
return mMax; }
213 bool operator== (
const Display& rhs)
const {
return rhs.
getIndex() == this->getIndex(); }
226 glm::ivec2 mMin = { 0, 0 };
227 glm::ivec2 mMax = { 0, 0 };
286 using SortFunction = std::function<void(std::vector<RenderableComponentInstance*>&,
const glm::mat4& viewMatrix)>;
299 bool isValid()
const {
return mPipeline != VK_NULL_HANDLE && mLayout != VK_NULL_HANDLE; }
301 VkPipeline mPipeline = VK_NULL_HANDLE;
302 VkPipelineLayout mLayout = VK_NULL_HANDLE;
370 bool beginHeadlessRecording();
386 void endHeadlessRecording();
463 bool beginComputeRecording();
480 void endComputeRecording();
538 void renderObjects(
IRenderTarget& renderTarget,
const glm::mat4& projection,
const glm::mat4& view,
const std::vector<RenderableComponentInstance*>& comps,
const SortFunction& sortFunction,
RenderMask renderMask =
mask::all);
545 void computeObjects(
const std::vector<ComputeComponentInstance*>& comps);
553 std::vector<RenderableComponentInstance*> filterObjects(
const std::vector<RenderableComponentInstance*>& comps,
RenderMask renderMask);
586 int getDisplayCount()
const;
594 const Display* findDisplay(
int index)
const;
794 VkSampleCountFlagBits getMaxRasterizationSamples()
const;
800 glm::uvec3 getMaxComputeWorkGroupSize()
const;
816 bool sampleShadingSupported()
const;
822 bool anisotropicFilteringSupported()
const;
878 VkImageAspectFlags getDepthAspectFlags()
const;
897 bool isComputeAvailable()
const;
934 RenderMask getRenderMask(
const std::string& tagName);
993 void getFormatProperties(VkFormat format, VkFormatProperties& outProperties)
const;
1017 uint32 getVulkanVersionMajor()
const;
1025 uint32 getVulkanVersionMinor()
const;
1060 void waitForFence(
int frameIndex);
1076 virtual void getDependentServices(std::vector<rtti::TypeInfo>& dependencies)
override;
1088 virtual void preShutdown()
override;
1093 virtual void shutdown()
override;
1098 virtual void preResourcesLoaded()
override;
1103 virtual void postResourcesLoaded()
override;
1109 virtual void update(
double deltaTime)
override;
1125 void removeTextureRequests(
Texture2D& texture);
1131 void requestTextureClear(
Texture& texture);
1137 void requestTextureUpload(
Texture2D& texture);
1143 void requestTextureDownload(
Texture2D& texture);
1149 void requestBufferClear(
GPUBuffer& buffer);
1155 void requestBufferUpload(
GPUBuffer& buffer);
1161 void requestBufferDownload(
GPUBuffer& buffer);
1168 void removeBufferRequests(
GPUBuffer& buffer);
1175 void transferData(VkCommandBuffer commandBuffer,
const std::function<
void()>& transferFunction);
1180 void downloadData();
1191 void updateDownloads();
1197 void processVulkanDestructors(
int frameIndex);
1203 void waitDeviceIdle();
1241 void removeTag(
const RenderTag& renderTag);
1256 struct UniqueMaterial;
1257 using PipelineCache = std::unordered_map<PipelineKey, Pipeline>;
1258 using ComputePipelineCache = std::unordered_map<ComputePipelineKey, Pipeline>;
1259 using WindowList = std::vector<RenderWindow*>;
1260 using DescriptorSetCacheMap = std::unordered_map<VkDescriptorSetLayout, std::unique_ptr<DescriptorSetCache>>;
1261 using TextureSet = std::unordered_set<Texture*>;
1262 using Texture2DSet = std::unordered_set<Texture2D*>;
1263 using BufferSet = std::unordered_set<GPUBuffer*>;
1264 using VulkanObjectDestructorList = std::vector<VulkanObjectDestructor>;
1265 using UniqueMaterialCache = std::unordered_map<rtti::TypeInfo, std::unique_ptr<UniqueMaterial>>;
1271 enum EQueueSubmitOp :
uint
1274 HeadlessRendering = 0x02,
1277 using QueueSubmitOps =
uint;
1285 std::vector<Texture2D*> mTextureDownloads;
1286 std::vector<GPUBuffer*> mBufferDownloads;
1287 VkCommandBuffer mUploadCommandBuffer;
1288 VkCommandBuffer mDownloadCommandBuffer;
1289 VkCommandBuffer mHeadlessCommandBuffer;
1290 VkCommandBuffer mComputeCommandBuffer;
1291 VulkanObjectDestructorList mQueuedVulkanObjectDestructors;
1292 QueueSubmitOps mQueueSubmitOps = 0U;
1299 struct UniqueMaterial
1301 UniqueMaterial() =
default;
1302 UniqueMaterial(std::unique_ptr<Shader> shader, std::unique_ptr<Material> material);
1303 std::unique_ptr<Shader> mShader =
nullptr;
1304 std::unique_ptr<Material> mMaterial =
nullptr;
1308 bool mEnableHighDPIMode =
true;
1309 bool mEnableCaching =
true;
1310 bool mSampleShadingSupported =
false;
1311 bool mAnisotropicFilteringSupported =
false;
1312 bool mWideLinesSupported =
false;
1313 bool mLargePointsSupported =
false;
1314 bool mNonSolidFillModeSupported =
false;
1315 uint32 mAnisotropicSamples = 1;
1316 WindowList mWindows;
1318 SceneService* mSceneService =
nullptr;
1319 bool mIsRenderingFrame =
false;
1320 bool mCanDestroyVulkanObjectsImmediately =
true;
1323 std::unique_ptr<Texture2D> mEmptyTexture2D;
1324 std::unique_ptr<TextureCube> mEmptyTextureCube;
1327 std::unique_ptr<Texture2D> mErrorTexture2D;
1328 std::unique_ptr<TextureCube> mErrorTextureCube;
1329 const RGBAColorFloat mErrorColor = { 1.0f, 0.3137f, 0.3137f, 1.0f };
1331 TextureSet mTexturesToClear;
1332 Texture2DSet mTexturesToUpload;
1333 BufferSet mBuffersToClear;
1334 BufferSet mBuffersToUpload;
1336 int mCurrentFrameIndex = 0;
1337 std::vector<Frame> mFramesInFlight;
1338 RenderWindow* mCurrentRenderWindow =
nullptr;
1340 DescriptorSetCacheMap mDescriptorSetCaches;
1341 std::unique_ptr<DescriptorSetAllocator> mDescriptorSetAllocator;
1343 VkInstance mInstance = VK_NULL_HANDLE;
1344 VmaAllocator mVulkanAllocator = VK_NULL_HANDLE;
1345 VkDebugReportCallbackEXT mDebugCallback = VK_NULL_HANDLE;
1346 VkDebugUtilsMessengerEXT mDebugUtilsMessengerCallback = VK_NULL_HANDLE;
1348 PhysicalDevice mPhysicalDevice;
1349 VkDevice mDevice = VK_NULL_HANDLE;
1350 VkCommandBuffer mCurrentCommandBuffer = VK_NULL_HANDLE;
1352 VkCommandPool mCommandPool = VK_NULL_HANDLE;
1353 VkQueue mQueue = VK_NULL_HANDLE;
1355 PipelineCache mPipelineCache;
1356 ComputePipelineCache mComputePipelineCache;
1358 VkFormat mDepthFormat = VK_FORMAT_UNDEFINED;
1359 VkSampleCountFlagBits mMaxRasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
1362 bool mInitialized =
false;
1363 bool mSDLInitialized =
false;
1364 bool mShInitialized =
false;
1365 bool mHeadless =
false;
1367 UniqueMaterialCache mMaterials;
1370 std::vector<RenderCommand> mHeadlessCommandQueue;
1371 std::vector<RenderCommand> mComputeCommandQueue;
1374 std::vector<const RenderTag*> mRenderTags;
1377 std::vector<std::unique_ptr<rtti::Object>> mCache;
1380 std::vector<const RenderChain*> mRenderChains;