8 #include "vk_mem_alloc.h"
9 #include "pipelinekey.h"
11 #include "rendertexturecube.h"
12 #include "renderutils.h"
13 #include "rendertag.h"
15 #include "videodriver.h"
18 #include <nap/service.h>
19 #include <windowevent.h>
25 class CameraComponentInstance;
26 class RenderableComponentInstance;
30 class DescriptorSetCache;
31 class DescriptorSetAllocator;
34 class MaterialInstance;
35 class ComputeMaterialInstance;
36 class ComputeComponentInstance;
39 class DepthRenderTexture2D;
65 mWindowHandle(windowHandle) { }
79 bool mHeadless =
false;
82 std::vector<std::string> mLayers = {
"VK_LAYER_KHRONOS_validation" };
83 std::vector<std::string> mAdditionalExtensions = { };
86 uint32 mAnisotropicFilterSamples = 8;
87 bool mEnableCompute =
true;
88 bool mEnableCaching =
true;
89 bool mEnableDebug =
true;
90 bool mEnableRobustBufferAccess =
false;
91 bool mPrintAvailableLayers =
false;
92 bool mPrintAvailableExtensions =
false;
97 void* mWindowHandle =
nullptr;
115 PhysicalDevice(VkPhysicalDevice device,
const VkPhysicalDeviceProperties& properties,
const VkQueueFlags& queueCapabilities,
int queueIndex);
130 const VkPhysicalDeviceProperties&
getProperties()
const {
return mProperties; }
135 const VkPhysicalDeviceFeatures&
getFeatures()
const {
return mFeatures; }
145 bool isValid()
const {
return mDevice != VK_NULL_HANDLE && mQueueIndex >= 0; }
148 VkPhysicalDevice mDevice = VK_NULL_HANDLE;
149 VkPhysicalDeviceProperties mProperties;
150 VkPhysicalDeviceFeatures mFeatures;
151 VkQueueFlags mQueueCapabilities;
152 int mQueueIndex = -1;
209 using SortFunction = std::function<void(std::vector<RenderableComponentInstance*>&,
const glm::mat4& viewMatrix)>;
222 bool isValid()
const {
return mPipeline != VK_NULL_HANDLE && mLayout != VK_NULL_HANDLE; }
224 VkPipeline mPipeline = VK_NULL_HANDLE;
225 VkPipelineLayout mLayout = VK_NULL_HANDLE;
293 bool beginHeadlessRecording();
309 void endHeadlessRecording();
386 bool beginComputeRecording();
403 void endComputeRecording();
461 void renderObjects(
IRenderTarget& renderTarget,
const glm::mat4& projection,
const glm::mat4& view,
const std::vector<RenderableComponentInstance*>& comps,
const SortFunction& sortFunction,
RenderMask renderMask =
mask::all);
468 void computeObjects(
const std::vector<ComputeComponentInstance*>& comps);
476 std::vector<RenderableComponentInstance*> filterObjects(
const std::vector<RenderableComponentInstance*>& comps,
RenderMask renderMask);
508 int getDisplayCount()
const;
515 Display findDisplay(
int index)
const;
527 std::vector<Display> getDisplays()
const;
532 std::vector<RenderWindow*> getWindows()
const;
721 VkSampleCountFlagBits getMaxRasterizationSamples()
const;
727 glm::uvec3 getMaxComputeWorkGroupSize()
const;
743 bool sampleShadingSupported()
const;
749 bool anisotropicFilteringSupported()
const;
813 VkImageAspectFlags getDepthAspectFlags()
const;
832 bool isComputeAvailable()
const;
881 RenderMask getRenderMask(
const std::string& tagName);
940 void getFormatProperties(VkFormat format, VkFormatProperties& outProperties)
const;
964 uint32 getVulkanVersionMajor()
const;
972 uint32 getVulkanVersionMinor()
const;
1027 void waitForFence(
int frameIndex);
1043 virtual void getDependentServices(std::vector<rtti::TypeInfo>& dependencies)
override;
1048 virtual void preShutdown()
override;
1053 virtual void shutdown()
override;
1058 virtual void preResourcesLoaded()
override;
1063 virtual void postResourcesLoaded()
override;
1069 virtual void update(
double deltaTime)
override;
1085 void removeTextureRequests(
Texture2D& texture);
1091 void requestTextureClear(
Texture& texture);
1097 void requestTextureUpload(
Texture2D& texture);
1103 void requestTextureDownload(
Texture2D& texture);
1109 void requestBufferClear(
GPUBuffer& buffer);
1115 void requestBufferUpload(
GPUBuffer& buffer);
1121 void requestBufferDownload(
GPUBuffer& buffer);
1128 void removeBufferRequests(
GPUBuffer& buffer);
1135 void transferData(VkCommandBuffer commandBuffer,
const std::function<
void()>& transferFunction);
1140 void downloadData();
1151 void updateDownloads();
1157 void processVulkanDestructors(
int frameIndex);
1163 void waitDeviceIdle();
1201 void removeTag(
const RenderTag& renderTag);
1216 struct UniqueMaterial;
1217 using PipelineCache = std::unordered_map<PipelineKey, Pipeline>;
1218 using ComputePipelineCache = std::unordered_map<ComputePipelineKey, Pipeline>;
1219 using WindowList = std::vector<RenderWindow*>;
1220 using DescriptorSetCacheMap = std::unordered_map<VkDescriptorSetLayout, std::unique_ptr<DescriptorSetCache>>;
1221 using TextureSet = std::unordered_set<Texture*>;
1222 using Texture2DSet = std::unordered_set<Texture2D*>;
1223 using BufferSet = std::unordered_set<GPUBuffer*>;
1224 using VulkanObjectDestructorList = std::vector<VulkanObjectDestructor>;
1225 using UniqueMaterialCache = std::unordered_map<rtti::TypeInfo, std::unique_ptr<UniqueMaterial>>;
1231 enum EQueueSubmitOp :
uint
1234 HeadlessRendering = 0x02,
1237 using QueueSubmitOps =
uint;
1245 std::vector<Texture2D*> mTextureDownloads;
1246 std::vector<GPUBuffer*> mBufferDownloads;
1247 VkCommandBuffer mUploadCommandBuffer;
1248 VkCommandBuffer mDownloadCommandBuffer;
1249 VkCommandBuffer mHeadlessCommandBuffer;
1250 VkCommandBuffer mComputeCommandBuffer;
1251 VulkanObjectDestructorList mQueuedVulkanObjectDestructors;
1252 QueueSubmitOps mQueueSubmitOps = 0U;
1259 struct UniqueMaterial
1261 UniqueMaterial() =
default;
1262 UniqueMaterial(std::unique_ptr<Shader> shader, std::unique_ptr<Material> material);
1263 std::unique_ptr<Shader> mShader =
nullptr;
1264 std::unique_ptr<Material> mMaterial =
nullptr;
1268 bool mEnableCaching =
true;
1269 bool mSampleShadingSupported =
false;
1270 bool mAnisotropicFilteringSupported =
false;
1271 bool mWideLinesSupported =
false;
1272 bool mLargePointsSupported =
false;
1273 bool mNonSolidFillModeSupported =
false;
1274 uint32 mAnisotropicSamples = 1;
1275 WindowList mWindows;
1276 SceneService* mSceneService =
nullptr;
1277 bool mIsRenderingFrame =
false;
1278 bool mCanDestroyVulkanObjectsImmediately =
true;
1281 std::unique_ptr<Texture2D> mEmptyTexture2D;
1282 std::unique_ptr<TextureCube> mEmptyTextureCube;
1283 std::unique_ptr<DepthRenderTexture2D> mEmptyDepthTexture2D;
1284 std::unique_ptr<DepthRenderTextureCube> mEmptyDepthTextureCube;
1287 std::unique_ptr<Texture2D> mErrorTexture2D;
1288 std::unique_ptr<TextureCube> mErrorTextureCube;
1289 const RGBAColorFloat mErrorColor = { 1.0f, 0.3137f, 0.3137f, 1.0f };
1291 TextureSet mTexturesToClear;
1292 Texture2DSet mTexturesToUpload;
1293 BufferSet mBuffersToClear;
1294 BufferSet mBuffersToUpload;
1296 int mCurrentFrameIndex = 0;
1297 std::vector<Frame> mFramesInFlight;
1298 RenderWindow* mCurrentRenderWindow =
nullptr;
1300 DescriptorSetCacheMap mDescriptorSetCaches;
1301 std::unique_ptr<DescriptorSetAllocator> mDescriptorSetAllocator;
1303 VkInstance mInstance = VK_NULL_HANDLE;
1304 VmaAllocator mVulkanAllocator = VK_NULL_HANDLE;
1305 VkDebugReportCallbackEXT mDebugCallback = VK_NULL_HANDLE;
1306 VkDebugUtilsMessengerEXT mDebugUtilsMessengerCallback = VK_NULL_HANDLE;
1308 PhysicalDevice mPhysicalDevice;
1309 VkDevice mDevice = VK_NULL_HANDLE;
1310 VkCommandBuffer mCurrentCommandBuffer = VK_NULL_HANDLE;
1312 VkCommandPool mCommandPool = VK_NULL_HANDLE;
1313 VkQueue mQueue = VK_NULL_HANDLE;
1315 PipelineCache mPipelineCache;
1316 ComputePipelineCache mComputePipelineCache;
1318 VkFormat mDepthFormat = VK_FORMAT_UNDEFINED;
1319 VkSampleCountFlagBits mMaxRasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
1322 bool mInitialized =
false;
1323 bool mSDLInitialized =
false;
1324 bool mShInitialized =
false;
1325 bool mHeadless =
false;
1327 UniqueMaterialCache mMaterials;
1330 std::vector<RenderCommand> mHeadlessCommandQueue;
1331 std::vector<RenderCommand> mComputeCommandQueue;
1334 std::vector<const RenderTag*> mRenderTags;
1337 std::vector<std::unique_ptr<rtti::Object>> mCache;
1340 std::vector<const RenderChain*> mRenderChains;