#pragma once #include "Graphics/Graphics.h" #include namespace Seele { namespace Vulkan { DECLARE_REF(DestructionManager) DECLARE_REF(CommandPool) DECLARE_REF(Queue) DECLARE_REF(PipelineCache) DECLARE_REF(Framebuffer) template concept chainable_struct = requires(T t) { t.pNext; t.sType; }; template struct Helper {}; template struct is_unique : std::true_type {}; template struct is_unique : std::bool_constant<(!std::same_as && ... && is_unique::value)> {}; template concept unique_chainable_structs = (chainable_struct && ...) && is_unique::value; template struct StructChain; template struct StructChain> { StructChain() { std::memset(&b, 0, sizeof(Base)); b.sType = struct_type; b.pNext = nullptr; } Base b; template Query& get() requires std::same_as { return b; } template const Query& get() const requires std::same_as { return b; } Base& get() { return b; } const Base& get() const { return b; } }; template struct StructChain, Right...> : StructChain { StructChain() { std::memset(&t, 0, sizeof(This)); t.sType = struct_type; t.pNext = &StructChain::template get(); } This t; template constexpr Query& get() { if constexpr (std::same_as) return t; else return StructChain::template get(); } template constexpr const Query& get() const { if constexpr (std::same_as) return t; else return StructChain::template get(); } constexpr This& get() { return t; } constexpr const This& get() const { return t; } }; class Graphics : public Gfx::Graphics { public: Graphics(); virtual ~Graphics(); constexpr VkInstance getInstance() const { return instance; } constexpr VkDevice getDevice() const { return handle; } constexpr VkPhysicalDevice getPhysicalDevice() const { return physicalDevice; } constexpr VkPhysicalDeviceAccelerationStructurePropertiesKHR getAccelerationProperties() const { return props.get(); } constexpr VkPhysicalDeviceRayTracingPipelinePropertiesKHR getRayTracingProperties() const { return props.get(); } constexpr float getTimestampPeriod() const { return props.get().properties.limits.timestampPeriod; } constexpr uint64 getTimestampValidBits() const { return graphicsProps.timestampValidBits; } PCommandPool getQueueCommands(Gfx::QueueType queueType); PCommandPool getGraphicsCommands(); PCommandPool getComputeCommands(); PCommandPool getTransferCommands(); VmaAllocator getAllocator() const; PDestructionManager getDestructionManager(); // Inherited via Graphics virtual void init(GraphicsInitializer initializer) override; virtual Gfx::OWindow createWindow(const WindowCreateInfo& createInfo) override; virtual Gfx::OViewport createViewport(Gfx::PWindow owner, const ViewportCreateInfo& createInfo) override; virtual Gfx::ORenderPass createRenderPass(Gfx::RenderTargetLayout layout, Array dependencies, URect renderArea, std::string name, Array viewMasks, Array correlationMasks) override; virtual void beginRenderPass(Gfx::PRenderPass renderPass) override; virtual void endRenderPass() override; virtual void waitDeviceIdle() override; virtual void executeCommands(Gfx::ORenderCommand commands) override; virtual void executeCommands(Array commands) override; virtual void executeCommands(Gfx::OComputeCommand commands) override; virtual void executeCommands(Array commands) override; virtual Gfx::OTexture2D createTexture2D(const TextureCreateInfo& createInfo) override; virtual Gfx::OTexture3D createTexture3D(const TextureCreateInfo& createInfo) override; virtual Gfx::OTextureCube createTextureCube(const TextureCreateInfo& createInfo) override; virtual Gfx::OUniformBuffer createUniformBuffer(const UniformBufferCreateInfo& bulkData) override; virtual Gfx::OShaderBuffer createShaderBuffer(const ShaderBufferCreateInfo& bulkData) override; virtual Gfx::OVertexBuffer createVertexBuffer(const VertexBufferCreateInfo& bulkData) override; virtual Gfx::OIndexBuffer createIndexBuffer(const IndexBufferCreateInfo& bulkData) override; virtual Gfx::ORenderCommand createRenderCommand(const std::string& name) override; virtual Gfx::OComputeCommand createComputeCommand(const std::string& name) override; virtual void beginShaderCompilation(const ShaderCompilationInfo& compileInfo) override; virtual Gfx::OVertexShader createVertexShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::OFragmentShader createFragmentShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::OComputeShader createComputeShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::OTaskShader createTaskShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::OMeshShader createMeshShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::PGraphicsPipeline createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo createInfo) override; virtual Gfx::PGraphicsPipeline createGraphicsPipeline(Gfx::MeshPipelineCreateInfo createInfo) override; virtual Gfx::PRayTracingPipeline createRayTracingPipeline(Gfx::RayTracingPipelineCreateInfo createInfo) override; virtual Gfx::PComputePipeline createComputePipeline(Gfx::ComputePipelineCreateInfo createInfo) override; virtual Gfx::OSampler createSampler(const SamplerCreateInfo& createInfo) override; virtual Gfx::ODescriptorLayout createDescriptorLayout(const std::string& name = "") override; virtual Gfx::OPipelineLayout createPipelineLayout(const std::string& name = "", Gfx::PPipelineLayout baseLayout = nullptr) override; virtual Gfx::OVertexInput createVertexInput(VertexInputStateCreateInfo createInfo) override; virtual Gfx::OOcclusionQuery createOcclusionQuery(const std::string& name = "") override; virtual Gfx::OPipelineStatisticsQuery createPipelineStatisticsQuery(const std::string& name = "") override; virtual Gfx::OTimestampQuery createTimestampQuery(uint64 numTimestamps, const std::string& name = "") override; virtual void beginDebugRegion(const std::string& name) override; virtual void endDebugRegion() override; virtual void resolveTexture(Gfx::PTexture source, Gfx::PTexture destination) override; virtual void copyTexture(Gfx::PTexture src, Gfx::PTexture dst) override; virtual void copyBuffer(Gfx::PShaderBuffer src, Gfx::PShaderBuffer dst) override; // Ray Tracing virtual Gfx::OBottomLevelAS createBottomLevelAccelerationStructure(const Gfx::BottomLevelASCreateInfo& createInfo) override; virtual Gfx::OTopLevelAS createTopLevelAccelerationStructure(const Gfx::TopLevelASCreateInfo& createInfo) override; virtual void buildBottomLevelAccelerationStructures(Array data) override; virtual Gfx::ORayGenShader createRayGenShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::OAnyHitShader createAnyHitShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::OClosestHitShader createClosestHitShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::OMissShader createMissShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::OIntersectionShader createIntersectionShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::OCallableShader createCallableShader(const ShaderCreateInfo& createInfo) override; protected: Array getRequiredExtensions(); void initInstance(GraphicsInitializer initInfo); void setupDebugCallback(); void pickPhysicalDevice(); void createDevice(GraphicsInitializer initInfo); VkInstance instance; VkDevice handle; VkPhysicalDevice physicalDevice; Array queues; uint32 graphicsQueue; uint32 computeQueue; uint32 transferQueue; thread_local static PCommandPool graphicsCommands; thread_local static PCommandPool computeCommands; thread_local static PCommandPool transferCommands; std::mutex poolLock; Array pools; VkQueueFamilyProperties graphicsProps; StructChain< Helper, Helper, Helper> props; StructChain< Helper, Helper, Helper, Helper, Helper, Helper> features; VkDebugUtilsMessengerEXT callback; Map allocatedFramebuffers; VmaAllocator allocator; OPipelineCache pipelineCache; ODestructionManager destructionManager; friend class Window; }; DEFINE_REF(Graphics) } // namespace Vulkan } // namespace Seele