From 2b27ddd134234d08c94fe2785b684d5ec5b78265 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Fri, 17 May 2024 18:08:11 +0200 Subject: [PATCH] Fixing some mac stuff --- .vscode/settings.json | 1 + CMakeLists.txt | 6 +- cmake/SuperBuild.cmake | 8 +- external/slang | 2 +- external/vcpkg | 2 +- res/shaders/ViewCullingTask.slang | 3 +- src/Editor/Asset/MeshLoader.cpp | 2 +- src/Editor/Asset/TextureLoader.cpp | 2 +- src/Editor/main.cpp | 2 +- src/Engine/Graphics/Buffer.h | 45 +- src/Engine/Graphics/Metal/Buffer.h | 32 +- src/Engine/Graphics/Metal/Buffer.mm | 103 +++- src/Engine/Graphics/Metal/Command.h | 13 +- src/Engine/Graphics/Metal/Command.mm | 20 +- src/Engine/Graphics/Metal/Descriptor.h | 44 +- src/Engine/Graphics/Metal/Descriptor.mm | 139 +++-- src/Engine/Graphics/Metal/Graphics.mm | 2 +- src/Engine/Graphics/Metal/Resources.h | 21 + src/Engine/Graphics/Metal/Shader.mm | 34 +- src/Engine/Graphics/Vulkan/Buffer.cpp | 744 +++++++++++++----------- src/Engine/Graphics/Vulkan/Buffer.h | 55 +- src/Engine/Graphics/slang-compile.cpp | 4 +- src/Engine/Material/Material.h | 2 +- 23 files changed, 747 insertions(+), 539 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6d5f35d..b270ba5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,6 +12,7 @@ "git.ignoreSubmodules": true, "cmake.configureOnOpen": true, "cmake.parallelJobs": 0, + "cmake.buildDirectory": "${workspaceFolder}/build", "C_Cpp.files.exclude": { "**/.vs": true, "**/.vscode": true, diff --git a/CMakeLists.txt b/CMakeLists.txt index 60b321e..85aee38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ set(CMAKE_TOOLCHAIN_FILE ${EXTERNAL_ROOT}/vcpkg/scripts/buildsystems/vcpkg.cmake set(CRCPP_ROOT ${EXTERNAL_ROOT}/CRCpp) set(METAL_ROOT ${EXTERNAL_ROOT}/metal-cpp) +set(SLANG_ROOT ${EXTERNAL_ROOT}/slang) set(Boost_NO_WARN_NEW_VERSIONS 1) @@ -51,7 +52,6 @@ find_package(Ktx CONFIG REQUIRED) find_package(nlohmann_json CONFIG REQUIRED) find_package(fmt CONFIG REQUIRED) find_package(VulkanMemoryAllocator CONFIG REQUIRED) -find_package(OpenMP REQUIRED) if(UNIX) find_package(Threads REQUIRED) @@ -77,7 +77,6 @@ target_link_libraries(Engine PUBLIC crcpp) target_link_libraries(Engine PUBLIC fmt::fmt) target_link_libraries(Engine PUBLIC GPUOpen::VulkanMemoryAllocator) target_link_libraries(Engine PUBLIC slang) -target_link_libraries(Engine PUBLIC OpenMP::OpenMP_CXX) if(APPLE) target_link_libraries(Engine PUBLIC metal) SET(CMAKE_OSX_DEPLOYMENT_TARGET 14.3) @@ -155,7 +154,8 @@ install( Editor Engine crcpp - EXPORT + metal + EXPORT EngineTargets FILE_SET HEADERS DESTINATION include/Seele) diff --git a/cmake/SuperBuild.cmake b/cmake/SuperBuild.cmake index 5870d6f..7e7b09e 100644 --- a/cmake/SuperBuild.cmake +++ b/cmake/SuperBuild.cmake @@ -19,11 +19,11 @@ if(WIN32) ${SLANG_ROOT}slang.lib DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) elseif(APPLE) - set(BINARY_ROOT ${SLANG_ROOT}/bin/macosx-aarch64/release/) - set_target_properties(slang PROPERTIES IMPORTED_LOCATION ${BINARY_ROOT}/libslang.dylib) + set(BINARY_ROOT ${SLANG_ROOT}/build/Debug/) + set_target_properties(slang PROPERTIES IMPORTED_LOCATION ${BINARY_ROOT}/lib/libslang.dylib) install(FILES - ${BINARY_ROOT}/libslang.dylib - DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) + ${BINARY_ROOT}/lib/libslang.dylib + DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) endif() target_include_directories(slang INTERFACE $ diff --git a/external/slang b/external/slang index 52dcb5b..725735a 160000 --- a/external/slang +++ b/external/slang @@ -1 +1 @@ -Subproject commit 52dcb5bd44aa15f07826062c53fae344d55959e9 +Subproject commit 725735a87b9d223c2afc83bbd049055b1e44a976 diff --git a/external/vcpkg b/external/vcpkg index 4002e5f..943c5ef 160000 --- a/external/vcpkg +++ b/external/vcpkg @@ -1 +1 @@ -Subproject commit 4002e5f3598cc0d216c395bc0db3de44a2d2ec9b +Subproject commit 943c5ef1c8f6b5e6ced092b242c8299caae2ff01 diff --git a/res/shaders/ViewCullingTask.slang b/res/shaders/ViewCullingTask.slang index ddb7f4d..b2aca4c 100644 --- a/res/shaders/ViewCullingTask.slang +++ b/res/shaders/ViewCullingTask.slang @@ -6,11 +6,10 @@ groupshared uint head; groupshared Frustum viewFrustum; [numthreads(TASK_GROUP_SIZE, 1, 1)] -[outputtopology("triangle")] [shader("amplification")] void taskMain( uint threadID: SV_GroupIndex, - uint groupID: SV_GroupID + uint groupID: SV_GroupThreadID ){ InstanceData instance = pScene.instances[pOffsets.instanceOffset + groupID]; MeshData mesh = pScene.meshData[pOffsets.instanceOffset + groupID]; diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index d1f02b4..357bf6d 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -206,7 +206,7 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Array& parameters.add(textureKey); bindingCounter++; - std::string samplerKey = std::format("{0}Sampler{1}", paramKey, index); + std::string samplerKey = fmt::format("{0}Sampler{1}", paramKey, index); SamplerCreateInfo samplerInfo = {}; switch (mapMode) { diff --git a/src/Editor/Asset/TextureLoader.cpp b/src/Editor/Asset/TextureLoader.cpp index b4fdcc3..16d7801 100644 --- a/src/Editor/Asset/TextureLoader.cpp +++ b/src/Editor/Asset/TextureLoader.cpp @@ -64,7 +64,7 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) ss << "toktx --encode etc1s "; if (args.type == TextureImportType::TEXTURE_NORMAL) { - ss << "--normal_mode "; + //ss << "--normal_mode "; } ss << ktxFile << " " << args.filePath; system(ss.str().c_str()); diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index 0457b47..8ee86a3 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -59,7 +59,7 @@ int main() { .filePath = sourcePath / "import/models/cube.fbx", }); AssetImporter::importMesh(MeshImportArgs{ - .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb", + .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.gltf", .importPath = "Whitechapel" }); //AssetImporter::importMesh(MeshImportArgs{ diff --git a/src/Engine/Graphics/Buffer.h b/src/Engine/Graphics/Buffer.h index 0416e82..b81c4c6 100644 --- a/src/Engine/Graphics/Buffer.h +++ b/src/Engine/Graphics/Buffer.h @@ -7,6 +7,7 @@ class Buffer : public QueueOwnedResource { public: Buffer(QueueFamilyMapping mapping, QueueType startQueueType); virtual ~Buffer(); + protected: // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(QueueType newOwner) = 0; @@ -14,19 +15,22 @@ protected: class VertexBuffer : public Buffer { public: - VertexBuffer(QueueFamilyMapping mapping, uint32 numVertices, uint32 vertexSize, QueueType startQueueType); + VertexBuffer(QueueFamilyMapping mapping, uint32 numVertices, + uint32 vertexSize, QueueType startQueueType); virtual ~VertexBuffer(); constexpr uint32 getNumVertices() const { return numVertices; } // Size of one vertex in bytes constexpr uint32 getVertexSize() const { return vertexSize; } virtual void updateRegion(DataSource update) = 0; - virtual void download(Array& buffer) = 0; + virtual void download(Array &buffer) = 0; protected: // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(QueueType newOwner) = 0; - virtual void executePipelineBarrier(SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, + virtual void executePipelineBarrier(SeAccessFlags srcAccess, + SePipelineStageFlags srcStage, + SeAccessFlags dstAccess, SePipelineStageFlags dstStage) = 0; uint32 numVertices; uint32 vertexSize; @@ -35,17 +39,20 @@ DEFINE_REF(VertexBuffer) class IndexBuffer : public Buffer { public: - IndexBuffer(QueueFamilyMapping mapping, uint64 size, Gfx::SeIndexType index, QueueType startQueueType); + IndexBuffer(QueueFamilyMapping mapping, uint64 size, Gfx::SeIndexType index, + QueueType startQueueType); virtual ~IndexBuffer(); constexpr uint64 getNumIndices() const { return numIndices; } constexpr Gfx::SeIndexType getIndexType() const { return indexType; } - virtual void download(Array& buffer) = 0; + virtual void download(Array &buffer) = 0; protected: // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(QueueType newOwner) = 0; - virtual void executePipelineBarrier(SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, + virtual void executePipelineBarrier(SeAccessFlags srcAccess, + SePipelineStageFlags srcStage, + SeAccessFlags dstAccess, SePipelineStageFlags dstStage) = 0; Gfx::SeIndexType indexType; uint64 numIndices; @@ -55,10 +62,11 @@ DEFINE_REF(IndexBuffer) DECLARE_REF(UniformBuffer) class UniformBuffer : public Buffer { public: - UniformBuffer(QueueFamilyMapping mapping, const DataSource& sourceData); + UniformBuffer(QueueFamilyMapping mapping, const DataSource &sourceData); virtual ~UniformBuffer(); - // returns true if an update was performed, false if the old contents == new contents - virtual bool updateContents(const DataSource& sourceData); + // returns true if an update was performed, false if the old contents == new + // contents + virtual bool updateContents(const DataSource &sourceData); bool isDataEquals(PUniformBuffer other) { if (other == nullptr) { return false; @@ -66,7 +74,8 @@ public: if (contents.size() != other->contents.size()) { return false; } - if (std::memcmp(contents.data(), other->contents.data(), contents.size()) != 0) { + if (std::memcmp(contents.data(), other->contents.data(), contents.size()) != + 0) { return false; } return true; @@ -76,24 +85,30 @@ protected: Array contents; // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(QueueType newOwner) = 0; - virtual void executePipelineBarrier(SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, + virtual void executePipelineBarrier(SeAccessFlags srcAccess, + SePipelineStageFlags srcStage, + SeAccessFlags dstAccess, SePipelineStageFlags dstStage) = 0; }; DEFINE_REF(UniformBuffer) class ShaderBuffer : public Buffer { public: - ShaderBuffer(QueueFamilyMapping mapping, uint32 numElements, const DataSource& bulkResourceData); + ShaderBuffer(QueueFamilyMapping mapping, uint32 numElements, + const DataSource &bulkResourceData); virtual ~ShaderBuffer(); virtual void rotateBuffer(uint64 size) = 0; - virtual void updateContents(const ShaderBufferCreateInfo& sourceData); + virtual void updateContents(const ShaderBufferCreateInfo &sourceData); constexpr uint32 getNumElements() const { return numElements; } - virtual void* mapRegion(uint64 offset = 0, uint64 size = -1, bool writeOnly = true) = 0; + virtual void *mapRegion(uint64 offset = 0, uint64 size = -1, + bool writeOnly = true) = 0; virtual void unmap() = 0; protected: // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(QueueType newOwner) = 0; - virtual void executePipelineBarrier(SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, + virtual void executePipelineBarrier(SeAccessFlags srcAccess, + SePipelineStageFlags srcStage, + SeAccessFlags dstAccess, SePipelineStageFlags dstStage) = 0; Array contents; diff --git a/src/Engine/Graphics/Metal/Buffer.h b/src/Engine/Graphics/Metal/Buffer.h index d0bf020..4887fc2 100644 --- a/src/Engine/Graphics/Metal/Buffer.h +++ b/src/Engine/Graphics/Metal/Buffer.h @@ -2,18 +2,29 @@ #include "Graphics/Buffer.h" #include "Graphics/Enums.h" #include "Graphics/Initializer.h" +#include "Metal/MTLTypes.hpp" +#include "MinimalEngine.h" #include "Resources.h" namespace Seele { namespace Metal { DECLARE_REF(Graphics) +class BufferAllocation : public CommandBoundResource +{ +public: + BufferAllocation(PGraphics graphics); + virtual ~BufferAllocation(); + MTL::Buffer* buffer = nullptr; + uint64 size = 0; +}; +DECLARE_REF(BufferAllocation) class Buffer { public: Buffer(PGraphics graphics, uint64 size, void *data, bool dynamic, const std::string& name); virtual ~Buffer(); - MTL::Buffer *getHandle() const { return buffers[currentBuffer]; } - uint64 getSize() const { return size; } - void advanceBuffer() { currentBuffer = (currentBuffer + 1) % numBuffers; } + MTL::Buffer *getHandle() const { return buffers[currentBuffer]->buffer; } + PBufferAllocation getAlloc() const { return buffers[currentBuffer]; } + uint64 getSize() const { return buffers[currentBuffer]->size; } void *map(bool writeOnly = true); void *mapRegion(uint64 regionOffset, uint64 regionSize, bool writeOnly); void unmap(); @@ -21,11 +32,14 @@ public: protected: PGraphics graphics; uint32 currentBuffer = 0; - uint64 size; - MTL::Buffer *buffers[Gfx::numFramesBuffered]; - uint32 numBuffers; + Array buffers; + bool dynamic; + std::string name; + void rotateBuffer(uint64 size); + void createBuffer(uint64 size, void* data); }; DEFINE_REF(Buffer) + class VertexBuffer : public Gfx::VertexBuffer, public Buffer { public: VertexBuffer(PGraphics graphics, const VertexBufferCreateInfo &createInfo); @@ -72,8 +86,10 @@ class ShaderBuffer : public Gfx::ShaderBuffer, public Buffer { public: ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo &createInfo); virtual ~ShaderBuffer(); - - virtual bool updateContents(const DataSource &sourceData) override; + virtual void rotateBuffer(uint64 size) override; + virtual void updateContents(const ShaderBufferCreateInfo &sourceData) override; + virtual void *mapRegion(uint64 offset, uint64 size, bool writeOnly) override; + virtual void unmap() override; protected: // Inherited via QueueOwnedResource diff --git a/src/Engine/Graphics/Metal/Buffer.mm b/src/Engine/Graphics/Metal/Buffer.mm index 8da8824..ecd6d7b 100644 --- a/src/Engine/Graphics/Metal/Buffer.mm +++ b/src/Engine/Graphics/Metal/Buffer.mm @@ -3,30 +3,35 @@ #include "Graphics/Buffer.h" #include "Graphics/Enums.h" #include "Graphics/Initializer.h" +#include "Graphics/Metal/Resources.h" +#include "Metal/MTLResource.hpp" #include using namespace Seele; using namespace Seele::Metal; -Buffer::Buffer(PGraphics graphics, uint64 size, void* data, bool dynamic, const std::string& name) : graphics(graphics), size(size) { - if (dynamic) { - numBuffers = Gfx::numFramesBuffered; - } else { - numBuffers = 1; - } - for (size_t i = 0; i < numBuffers; ++i) { - if (data != nullptr) { - buffers[i] = graphics->getDevice()->newBuffer(data, size, MTL::StorageModeShared); - } else { - buffers[i] = graphics->getDevice()->newBuffer(size, MTL::StorageModeShared); - } - buffers[i]->setLabel(NS::String::string(name.c_str(), NS::ASCIIStringEncoding)); +BufferAllocation::BufferAllocation(PGraphics graphics) + : CommandBoundResource(graphics) +{} + +BufferAllocation::~BufferAllocation() +{ + if(buffer != nullptr) + { + buffer->release(); } } +Buffer::Buffer(PGraphics graphics, uint64 size, void* data, bool dynamic, const std::string& name) + : graphics(graphics) + , dynamic(dynamic) + , name(name) { + createBuffer(size, data); +} + Buffer::~Buffer() { - for (size_t i = 0; i < numBuffers; ++i) { - buffers[i]->release(); + for (size_t i = 0; i < buffers.size(); ++i) { + //TODO } } @@ -36,6 +41,39 @@ void* Buffer::mapRegion(uint64 regionOffset, uint64, bool) { return (char*)getHa void Buffer::unmap() {} +void Buffer::rotateBuffer(uint64 size) +{ + size = std::max(getSize(), size); + for(size_t i = 0; i < buffers.size(); ++i) + { + if(buffers[i]->isCurrentlyBound()) + { + continue; + } + if(buffers[i]->size < size) + { + buffers[i]->buffer->release(); + buffers[i]->buffer = graphics->getDevice()->newBuffer(size, MTL::StorageModeShared); + buffers[i]->size = size; + } + currentBuffer = i; + return; + } + createBuffer(size, nullptr); + currentBuffer = buffers.size() - 1; +} + +void Buffer::createBuffer(uint64 size, void* data) +{ + buffers.add(new BufferAllocation(graphics)); + if (data != nullptr) { + buffers.back()->buffer = graphics->getDevice()->newBuffer(data, size, MTL::StorageModeShared); + } else { + buffers.back()->buffer = graphics->getDevice()->newBuffer(size, MTL::StorageModeShared); + } + buffers.back()->buffer->setLabel(NS::String::string(name.c_str(), NS::ASCIIStringEncoding)); +} + VertexBuffer::VertexBuffer(PGraphics graphics, const VertexBufferCreateInfo& createInfo) : Gfx::VertexBuffer(graphics->getFamilyMapping(), createInfo.numVertices, createInfo.vertexSize, createInfo.sourceData.owner), @@ -50,8 +88,8 @@ void VertexBuffer::updateRegion(DataSource update) { void VertexBuffer::download(Array& buffer) { void* data = getHandle()->contents(); - buffer.resize(size); - std::memcpy(buffer.data(), data, size); + buffer.resize(getSize()); + std::memcpy(buffer.data(), data, getSize()); } void VertexBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { currentOwner = newOwner; } @@ -70,8 +108,8 @@ IndexBuffer::~IndexBuffer() {} void IndexBuffer::download(Array& buffer) { void* data = getHandle()->contents(); - buffer.resize(size); - std::memcpy(buffer.data(), data, size); + buffer.resize(getSize()); + std::memcpy(buffer.data(), data, getSize()); } void IndexBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { currentOwner = newOwner; } @@ -101,10 +139,29 @@ ShaderBuffer::ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo& cre ShaderBuffer::~ShaderBuffer() {} -bool ShaderBuffer::updateContents(const DataSource& sourceData) { - void* data = getHandle()->contents(); - std::memcpy((char*)data + sourceData.offset, sourceData.data, sourceData.size); - return true; +void ShaderBuffer::rotateBuffer(uint64 size) { + Metal::Buffer::rotateBuffer(size); +} + +void ShaderBuffer::updateContents(const ShaderBufferCreateInfo& createInfo) { + Gfx::ShaderBuffer::updateContents(createInfo); + if(createInfo.sourceData.data == nullptr) + { + return; + } + void* data = map(); + std::memcpy((char*)data + createInfo.sourceData.offset, createInfo.sourceData.data, createInfo.sourceData.size); + unmap(); +} + +void* ShaderBuffer::mapRegion(uint64 offset, uint64 size, bool writeOnly) +{ + return Metal::Buffer::mapRegion(offset, size, writeOnly); +} + +void ShaderBuffer::unmap() +{ + Metal::Buffer::unmap(); } void ShaderBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { currentOwner = newOwner; } diff --git a/src/Engine/Graphics/Metal/Command.h b/src/Engine/Graphics/Metal/Command.h index 02fbf07..fe1d00b 100644 --- a/src/Engine/Graphics/Metal/Command.h +++ b/src/Engine/Graphics/Metal/Command.h @@ -56,16 +56,17 @@ public: void end(); virtual void setViewport(Gfx::PViewport viewport) override; virtual void bindPipeline(Gfx::PGraphicsPipeline pipeline) override; - virtual void bindDescriptor(Gfx::PDescriptorSet descriptorSet) override; - virtual void bindDescriptor(const Array& descriptorSets) override; + virtual void bindDescriptor(Gfx::PDescriptorSet descriptorSet, Array dynamicOffsets) override; + virtual void bindDescriptor(const Array& descriptorSets, Array dynamicOffsets) override; virtual void bindVertexBuffer(const Array& buffers) override; virtual void bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) override; - virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, + virtual void pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) override; virtual void draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance) override; virtual void drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset, uint32 firstInstance) override; virtual void drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) override; + virtual void drawMeshIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride) override; private: PGraphicsPipeline boundPipeline; @@ -81,9 +82,9 @@ public: virtual ~ComputeCommand(); void end(); virtual void bindPipeline(Gfx::PComputePipeline pipeline) override; - virtual void bindDescriptor(Gfx::PDescriptorSet set) override; - virtual void bindDescriptor(const Array& sets) override; - virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, + virtual void bindDescriptor(Gfx::PDescriptorSet set, Array dynamicOffsets) override; + virtual void bindDescriptor(const Array& sets, Array dynamicOffsets) override; + virtual void pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) override; virtual void dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) override; diff --git a/src/Engine/Graphics/Metal/Command.mm b/src/Engine/Graphics/Metal/Command.mm index 8d5e910..7213709 100644 --- a/src/Engine/Graphics/Metal/Command.mm +++ b/src/Engine/Graphics/Metal/Command.mm @@ -84,7 +84,7 @@ void RenderCommand::bindPipeline(Gfx::PGraphicsPipeline pipeline) { NS::String::string(boundPipeline->getPipelineLayout()->getName().c_str(), NS::ASCIIStringEncoding)); } -void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet) { +void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet, Array offsets) { auto metalSet = descriptorSet.cast(); uint32 parameterIndex = boundPipeline->getPipelineLayout()->findParameter(descriptorSet->getLayout()->getName()); uint64* topLevelTable = (uint64*)argumentBuffer->contents(); @@ -117,9 +117,9 @@ void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet) { } } -void RenderCommand::bindDescriptor(const Array& descriptorSets) { +void RenderCommand::bindDescriptor(const Array& descriptorSets, Array offsets) { for (auto set : descriptorSets) { - bindDescriptor(set); + bindDescriptor(set, offsets); } } void RenderCommand::bindVertexBuffer(const Array& buffers) { @@ -133,7 +133,7 @@ void RenderCommand::bindIndexBuffer(Gfx::PIndexBuffer gfxIndexBuffer) { boundIndexBuffer = gfxIndexBuffer.cast(); } -void RenderCommand::pushConstants(Gfx::PPipelineLayout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, +void RenderCommand::pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) { if (stage & Gfx::SE_SHADER_STAGE_VERTEX_BIT) { encoder->setVertexBytes((char*)data + offset, size, 0); @@ -162,6 +162,10 @@ void RenderCommand::drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) { encoder->drawMeshThreadgroups(MTL::Size(groupX, groupY, groupZ), MTL::Size(128, 1, 1), MTL::Size(32, 1, 1)); } +void RenderCommand::drawMeshIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride) { + //encoder->drawMeshThreadgroups() +} + ComputeCommand::ComputeCommand(MTL::CommandBuffer* cmdBuffer, const std::string& name) : commandBuffer(cmdBuffer), encoder(cmdBuffer->computeCommandEncoder()), name(name) {} @@ -181,7 +185,7 @@ void ComputeCommand::bindPipeline(Gfx::PComputePipeline pipeline) { NS::String::string(pipeline->getPipelineLayout()->getName().c_str(), NS::ASCIIStringEncoding)); } -void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet set) { +void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet set, Array offsets) { auto metalSet = set.cast(); metalSet->bind(); uint32 parameterIndex = boundPipeline->getPipelineLayout()->findParameter(set->getLayout()->getName()); @@ -215,13 +219,13 @@ void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet set) { } } -void ComputeCommand::bindDescriptor(const Array& sets) { +void ComputeCommand::bindDescriptor(const Array& sets, Array offsets) { for (auto& set : sets) { - bindDescriptor(set); + bindDescriptor(set, offsets); } } -void ComputeCommand::pushConstants(Gfx::PPipelineLayout, Gfx::SeShaderStageFlags, uint32 offset, uint32 size, +void ComputeCommand::pushConstants(Gfx::SeShaderStageFlags, uint32 offset, uint32 size, const void* data) { encoder->setBytes((char*)data + offset, size, 0); } diff --git a/src/Engine/Graphics/Metal/Descriptor.h b/src/Engine/Graphics/Metal/Descriptor.h index dc94774..59de66a 100644 --- a/src/Engine/Graphics/Metal/Descriptor.h +++ b/src/Engine/Graphics/Metal/Descriptor.h @@ -1,15 +1,16 @@ #pragma once +#include "Buffer.h" #include "Graphics/Descriptor.h" #include "Graphics/Initializer.h" +#include "Graphics/Metal/Resources.h" #include "MinimalEngine.h" -#include "Buffer.h" namespace Seele { namespace Metal { DECLARE_REF(Graphics) class DescriptorLayout : public Gfx::DescriptorLayout { public: - DescriptorLayout(PGraphics graphics, const std::string& name); + DescriptorLayout(PGraphics graphics, const std::string &name); virtual ~DescriptorLayout(); virtual void create() override; @@ -34,44 +35,47 @@ private: }; DEFINE_REF(DescriptorPool) -class DescriptorSet : public Gfx::DescriptorSet { +class DescriptorSet : public Gfx::DescriptorSet, public CommandBoundResource { public: DescriptorSet(PGraphics graphics, PDescriptorPool owner); virtual ~DescriptorSet(); - virtual void writeChanges(); - virtual void updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBuffer); - virtual void updateBuffer(uint32_t binding, Gfx::PShaderBuffer uniformBuffer); - virtual void updateSampler(uint32_t binding, Gfx::PSampler samplerState); - virtual void updateTexture(uint32_t binding, Gfx::PTexture texture, Gfx::PSampler sampler = nullptr); - virtual void updateTextureArray(uint32_t binding, Array texture); + virtual void writeChanges() override; + virtual void updateBuffer(uint32_t binding, + Gfx::PUniformBuffer uniformBuffer) override; + virtual void updateBuffer(uint32_t binding, Gfx::PShaderBuffer uniformBuffer) override; + virtual void updateBuffer(uint32_t binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) override; + virtual void updateSampler(uint32_t binding, Gfx::PSampler samplerState) override; + virtual void updateTexture(uint32_t binding, Gfx::PTexture texture, + Gfx::PSampler sampler = nullptr) override; + virtual void updateTextureArray(uint32_t binding, + Array texture) override; - constexpr bool isCurrentlyBound() const { return bindCount > 0; } constexpr bool isCurrentlyInUse() const { return currentlyInUse; } - constexpr void bind() { bindCount++; } - constexpr void unbind() { bindCount--; } constexpr void allocate() { currentlyInUse = true; } constexpr void free() { currentlyInUse = false; } - constexpr MTL::Buffer* getBuffer() const { return buffer; } - constexpr const Array& getBoundResources() const { return boundResources; } + constexpr MTL::Buffer *getBuffer() const { return buffer; } + constexpr const Array &getBoundResources() const { + return boundResources; + } private: PGraphics graphics; PDescriptorPool owner; - MTL::Buffer* buffer = nullptr; - uint64* argumentBuffer = nullptr; - Array boundResources; - uint32 bindCount; + MTL::Buffer *buffer = nullptr; + uint64 *argumentBuffer = nullptr; + Array boundResources; bool currentlyInUse; }; DEFINE_REF(DescriptorSet) class PipelineLayout : public Gfx::PipelineLayout { public: - PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout); + PipelineLayout(PGraphics graphics, const std::string &name, + Gfx::PPipelineLayout baseLayout); virtual ~PipelineLayout(); virtual void create() override; - + private: PGraphics graphics; }; diff --git a/src/Engine/Graphics/Metal/Descriptor.mm b/src/Engine/Graphics/Metal/Descriptor.mm index d59a655..f1cb43b 100644 --- a/src/Engine/Graphics/Metal/Descriptor.mm +++ b/src/Engine/Graphics/Metal/Descriptor.mm @@ -3,6 +3,7 @@ #include "Enums.h" #include "Graphics/Descriptor.h" #include "Graphics/Initializer.h" +#include "Graphics/Metal/Resources.h" #include "Metal/MTLArgument.hpp" #include "Metal/MTLDevice.hpp" #include "Metal/MTLResource.hpp" @@ -14,24 +15,28 @@ using namespace Seele; using namespace Seele::Metal; -DescriptorLayout::DescriptorLayout(PGraphics graphics, const std::string& name) +DescriptorLayout::DescriptorLayout(PGraphics graphics, const std::string &name) : Gfx::DescriptorLayout(name), graphics(graphics) {} DescriptorLayout::~DescriptorLayout() {} void DescriptorLayout::create() { pool = new DescriptorPool(graphics, this); - hash = CRC::Calculate(descriptorBindings.data(), sizeof(Gfx::DescriptorBinding) * descriptorBindings.size(), - CRC::CRC_32()); + hash = + CRC::Calculate(descriptorBindings.data(), + sizeof(Gfx::DescriptorBinding) * descriptorBindings.size(), + CRC::CRC_32()); } -DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout) : graphics(graphics), layout(layout) {} +DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout) + : graphics(graphics), layout(layout) {} DescriptorPool::~DescriptorPool() {} Gfx::PDescriptorSet DescriptorPool::allocateDescriptorSet() { for (uint32 setIndex = 0; setIndex < allocatedSets.size(); ++setIndex) { - if (allocatedSets[setIndex]->isCurrentlyBound() || allocatedSets[setIndex]->isCurrentlyInUse()) { + if (allocatedSets[setIndex]->isCurrentlyBound() || + allocatedSets[setIndex]->isCurrentlyInUse()) { // Currently in use, skip continue; } @@ -46,80 +51,104 @@ Gfx::PDescriptorSet DescriptorPool::allocateDescriptorSet() { } void DescriptorPool::reset() { - for (auto& set : allocatedSets) { + for (auto &set : allocatedSets) { set->free(); } } DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner) - : Gfx::DescriptorSet(owner->getLayout()), graphics(graphics), owner(owner), bindCount(0), currentlyInUse(false) { + : Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics), + graphics(graphics), owner(owner), currentlyInUse(false) { boundResources.resize(owner->getLayout()->getBindings().size()); - buffer = graphics->getDevice()->newBuffer(std::max(8, sizeof(uint64_t) * 3 * owner->getLayout()->getBindings().size()), MTL::ResourceStorageModeShared); - argumentBuffer = (uint64*)buffer->contents(); - buffer->setLabel(NS::String::string(owner->getLayout()->getName().c_str(), NS::ASCIIStringEncoding)); + buffer = graphics->getDevice()->newBuffer( + std::max(8, sizeof(uint64_t) * 3 * + owner->getLayout()->getBindings().size()), + MTL::ResourceStorageModeShared); + argumentBuffer = (uint64 *)buffer->contents(); + buffer->setLabel(NS::String::string(owner->getLayout()->getName().c_str(), + NS::ASCIIStringEncoding)); } -DescriptorSet::~DescriptorSet() {buffer->release();} +DescriptorSet::~DescriptorSet() { buffer->release(); } void DescriptorSet::writeChanges() {} -void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBuffer) { +void DescriptorSet::updateBuffer(uint32_t binding, + Gfx::PUniformBuffer uniformBuffer) { PUniformBuffer metalBuffer = uniformBuffer.cast(); - uint64 offset = binding * 3; - argumentBuffer[offset + 0] = metalBuffer->getHandle()->gpuAddress(); - argumentBuffer[offset + 1] = 0; - argumentBuffer[offset + 2] = (uint32)metalBuffer->getSize(); // TODO: buffer texture view, typed?? - boundResources[binding] = metalBuffer->getHandle(); + uint64 offset = binding * 3; + argumentBuffer[offset + 0] = metalBuffer->getHandle()->gpuAddress(); + argumentBuffer[offset + 1] = 0; + argumentBuffer[offset + 2] = + (uint32)metalBuffer->getSize(); // TODO: buffer texture view, typed?? + boundResources[binding] = metalBuffer->getHandle(); } -void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PShaderBuffer uniformBuffer) { +void DescriptorSet::updateBuffer(uint32_t binding, + Gfx::PShaderBuffer uniformBuffer) { PShaderBuffer metalBuffer = uniformBuffer.cast(); - uint64 offset = binding * 3; - argumentBuffer[offset + 0] = metalBuffer->getHandle()->gpuAddress(); - argumentBuffer[offset + 1] = 0; - argumentBuffer[offset + 2] = (uint32)metalBuffer->getSize(); // TODO: buffer texture view, typed?? - boundResources[binding] = metalBuffer->getHandle(); -} -void DescriptorSet::updateSampler(uint32_t binding, Gfx::PSampler samplerState) { - PSampler sampler = samplerState.cast(); - MTL::ResourceID resourceId =sampler->getHandle()->gpuResourceID(); - uint64 offset = binding * 3; - argumentBuffer[offset + 0] = 0; - argumentBuffer[offset + 1] = *(uint64*)&resourceId; - argumentBuffer[offset + 2] = 0; // LOD bias -} -void DescriptorSet::updateTexture(uint32_t binding, Gfx::PTexture texture, Gfx::PSampler samplerState) { - PTextureBase base = texture.cast(); - if(layout->getBindings()[binding].access == Gfx::SE_DESCRIPTOR_ACCESS_READ_ONLY_BIT) - { - MTL::ResourceID resourceId =base->getTexture()->gpuResourceID(); - uint64 offset = binding * 3; - argumentBuffer[offset + 0] = 0; - argumentBuffer[offset + 1] = *(uint64*)&resourceId; - argumentBuffer[offset + 2] = 0; // min LOD clamp - }else{ - uint64 offset = binding * 3; - argumentBuffer[offset + 0] = base->getTexture()->buffer()->gpuAddress(); - argumentBuffer[offset + 1] = 0; - argumentBuffer[offset + 2] = (uint32)base->getTexture()->buffer()->length(); // TODO: buffer texture view, typed?? - } - boundResources[binding] = base->getTexture(); + uint64 offset = binding * 3; + argumentBuffer[offset + 0] = metalBuffer->getHandle()->gpuAddress(); + argumentBuffer[offset + 1] = 0; + argumentBuffer[offset + 2] = + (uint32)metalBuffer->getSize(); // TODO: buffer texture view, typed?? + boundResources[binding] = metalBuffer->getHandle(); } -void DescriptorSet::updateTextureArray(uint32_t binding, Array array) { - for (auto& t : array) { - updateTexture(binding++, t); +void DescriptorSet::updateBuffer(uint32_t binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) +{ + +} + + +void DescriptorSet::updateSampler(uint32_t binding, + Gfx::PSampler samplerState) { + PSampler sampler = samplerState.cast(); + MTL::ResourceID resourceId = sampler->getHandle()->gpuResourceID(); + uint64 offset = binding * 3; + argumentBuffer[offset + 0] = 0; + argumentBuffer[offset + 1] = *(uint64 *)&resourceId; + argumentBuffer[offset + 2] = 0; // LOD bias +} +void DescriptorSet::updateTexture(uint32_t binding, Gfx::PTexture texture, + Gfx::PSampler samplerState) { + PTextureBase base = texture.cast(); + if (layout->getBindings()[binding].access == + Gfx::SE_DESCRIPTOR_ACCESS_READ_ONLY_BIT) { + MTL::ResourceID resourceId = base->getTexture()->gpuResourceID(); + uint64 offset = binding * 3; + argumentBuffer[offset + 0] = 0; + argumentBuffer[offset + 1] = *(uint64 *)&resourceId; + argumentBuffer[offset + 2] = 0; // min LOD clamp + } else { + uint64 offset = binding * 3; + argumentBuffer[offset + 0] = base->getTexture()->buffer()->gpuAddress(); + argumentBuffer[offset + 1] = 0; + argumentBuffer[offset + 2] = + (uint32)base->getTexture() + ->buffer() + ->length(); // TODO: buffer texture view, typed?? + } + boundResources[binding] = base->getTexture(); +} + +void DescriptorSet::updateTextureArray(uint32_t binding, + Array array) { + for (auto &t : array) { + updateTexture(binding++, t); } } -PipelineLayout::PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout) +PipelineLayout::PipelineLayout(PGraphics graphics, const std::string &name, + Gfx::PPipelineLayout baseLayout) : Gfx::PipelineLayout(name, baseLayout), graphics(graphics) {} PipelineLayout::~PipelineLayout() {} void PipelineLayout::create() { - for (auto& [_, set] : descriptorSetLayouts) { - assert(set->getHash() != 0); + for (auto &[_, set] : descriptorSetLayouts) { + assert(set->getHash() != 0); uint32 setHash = set->getHash(); - layoutHash = CRC::Calculate(&setHash, sizeof(uint32), CRC::CRC_32(), layoutHash); + layoutHash = + CRC::Calculate(&setHash, sizeof(uint32), CRC::CRC_32(), layoutHash); } } diff --git a/src/Engine/Graphics/Metal/Graphics.mm b/src/Engine/Graphics/Metal/Graphics.mm index c095d7b..c14b02d 100644 --- a/src/Engine/Graphics/Metal/Graphics.mm +++ b/src/Engine/Graphics/Metal/Graphics.mm @@ -31,7 +31,7 @@ void Graphics::init(GraphicsInitializer) queue = new CommandQueue(this); ioQueue = new IOCommandQueue(this); cache = new PipelineCache(this, "pipelines.metal"); - meshShadingEnabled = true; + meshShadingEnabled = false; } Gfx::OWindow Graphics::createWindow(const WindowCreateInfo &createInfo) diff --git a/src/Engine/Graphics/Metal/Resources.h b/src/Engine/Graphics/Metal/Resources.h index 53f781d..eeffd9a 100644 --- a/src/Engine/Graphics/Metal/Resources.h +++ b/src/Engine/Graphics/Metal/Resources.h @@ -10,6 +10,27 @@ namespace Seele { namespace Metal { DECLARE_REF(Graphics); +class CommandBoundResource +{ +public: + CommandBoundResource(PGraphics graphics) + : graphics(graphics) + { + } + virtual ~CommandBoundResource() + { + if (isCurrentlyBound()) + abort(); + } + constexpr bool isCurrentlyBound() const { return bindCount > 0; } + constexpr void bind() { bindCount++; } + constexpr void unbind() { bindCount--; } +protected: + PGraphics graphics; + uint64 bindCount = 0; +}; +DEFINE_REF(CommandBoundResource) + class Fence { public: Fence(PGraphics graphics); diff --git a/src/Engine/Graphics/Metal/Shader.mm b/src/Engine/Graphics/Metal/Shader.mm index c116dcc..3296ef0 100644 --- a/src/Engine/Graphics/Metal/Shader.mm +++ b/src/Engine/Graphics/Metal/Shader.mm @@ -14,7 +14,8 @@ using namespace Seele; using namespace Seele::Metal; -Shader::Shader(PGraphics graphics, Gfx::SeShaderStageFlags stage) : stage(stage), graphics(graphics) {} +Shader::Shader(PGraphics graphics, Gfx::SeShaderStageFlags stage) + : stage(stage), graphics(graphics) {} Shader::~Shader() { if (function) { @@ -23,20 +24,27 @@ Shader::~Shader() { } } -void Shader::create(const ShaderCreateInfo& createInfo) { +void Shader::create(const ShaderCreateInfo &createInfo) { std::cout << "Compiling " << createInfo.name << std::endl; Map paramMapping; - Slang::ComPtr kernelBlob = generateShader(createInfo, SLANG_METAL, paramMapping); - std::cout << (char*)kernelBlob->getBufferPointer() << std::endl; - hash = CRC::Calculate(kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), CRC::CRC_32()); - dispatch_data_t dispatchData = dispatch_data_create(kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), dispatch_get_main_queue(), NULL); - NS::Error* error; - library = graphics->getDevice()->newLibrary(dispatchData, &error); - if(error) - { - std::cout << error->localizedDescription()->cString(NS::ASCIIStringEncoding) << std::endl; - } - function = library->newFunction(NS::String::string("main", NS::ASCIIStringEncoding)); + Slang::ComPtr kernelBlob = + generateShader(createInfo, SLANG_METAL, paramMapping); + std::cout << (char*)kernelBlob->getBufferPointer() << std::endl; + hash = CRC::Calculate(kernelBlob->getBufferPointer(), + kernelBlob->getBufferSize(), CRC::CRC_32()); + NS::Error *error; + MTL::CompileOptions *options = MTL::CompileOptions::alloc()->init(); + library = graphics->getDevice()->newLibrary( + NS::String::string((char *)kernelBlob->getBufferPointer(), + NS::ASCIIStringEncoding), + options, &error); + options->release(); + if (error) { + std::cout << error->localizedDescription()->cString(NS::ASCIIStringEncoding) + << std::endl; + } + function = + library->newFunction(NS::String::string(createInfo.entryPoint.c_str(), NS::ASCIIStringEncoding)); if (!function) { assert(false); } diff --git a/src/Engine/Graphics/Vulkan/Buffer.cpp b/src/Engine/Graphics/Vulkan/Buffer.cpp index 21fd76b..b6e30e0 100644 --- a/src/Engine/Graphics/Vulkan/Buffer.cpp +++ b/src/Engine/Graphics/Vulkan/Buffer.cpp @@ -5,462 +5,510 @@ using namespace Seele; using namespace Seele::Vulkan; BufferAllocation::BufferAllocation(PGraphics graphics) - : CommandBoundResource(graphics) -{ -} + : CommandBoundResource(graphics) {} -BufferAllocation::~BufferAllocation() -{ - if (buffer != VK_NULL_HANDLE) - { - vmaDestroyBuffer(graphics->getAllocator(), buffer, allocation); - } +BufferAllocation::~BufferAllocation() { + if (buffer != VK_NULL_HANDLE) { + vmaDestroyBuffer(graphics->getAllocator(), buffer, allocation); + } } struct PendingBuffer { - OBufferAllocation allocation; - uint64 offset; - Gfx::QueueType prevQueue; - bool writeOnly; + OBufferAllocation allocation; + uint64 offset; + Gfx::QueueType prevQueue; + bool writeOnly; }; -static Map pendingBuffers; +static Map pendingBuffers; -Buffer::Buffer(PGraphics graphics, uint64 size, VkBufferUsageFlags usage, Gfx::QueueType& queueType, bool dynamic, - std::string name) - : graphics(graphics), currentBuffer(0), owner(queueType), usage(usage | VK_BUFFER_USAGE_TRANSFER_DST_BIT), dynamic(dynamic), name(name) { - createBuffer(size); +Buffer::Buffer(PGraphics graphics, uint64 size, VkBufferUsageFlags usage, + Gfx::QueueType &queueType, bool dynamic, std::string name) + : graphics(graphics), currentBuffer(0), owner(queueType), + usage(usage | VK_BUFFER_USAGE_TRANSFER_DST_BIT), dynamic(dynamic), + name(name) { + createBuffer(size); } Buffer::~Buffer() { - for (uint32 i = 0; i < buffers.size(); ++i) { - graphics->getDestructionManager()->queueResourceForDestruction(std::move(buffers[i])); - } + for (uint32 i = 0; i < buffers.size(); ++i) { + graphics->getDestructionManager()->queueResourceForDestruction( + std::move(buffers[i])); + } } void Buffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { - if (getSize() == 0) - return; - Gfx::QueueFamilyMapping mapping = graphics->getFamilyMapping(); - VkBufferMemoryBarrier barrier = { - .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, - .pNext = nullptr, - .srcQueueFamilyIndex = mapping.getQueueTypeFamilyIndex(owner), - .dstQueueFamilyIndex = mapping.getQueueTypeFamilyIndex(newOwner), - .buffer = getHandle(), - .offset = 0, - .size = getSize(), - }; - PCommandPool sourcePool = graphics->getQueueCommands(owner); - PCommandPool dstPool = nullptr; - VkPipelineStageFlags srcStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; - VkPipelineStageFlags dstStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; - assert(barrier.srcQueueFamilyIndex != barrier.dstQueueFamilyIndex); - if (owner == Gfx::QueueType::TRANSFER) { - barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - srcStage = VK_PIPELINE_STAGE_TRANSFER_BIT; - } - else if (owner == Gfx::QueueType::COMPUTE) { - barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; - srcStage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; - } - else if (owner == Gfx::QueueType::GRAPHICS) { - barrier.srcAccessMask = getSourceAccessMask(); - srcStage = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT; - } - if (newOwner == Gfx::QueueType::TRANSFER) { - barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; - dstStage = VK_PIPELINE_STAGE_TRANSFER_BIT; - dstPool = graphics->getTransferCommands(); - } - else if (newOwner == Gfx::QueueType::COMPUTE) { - barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; - dstStage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; - dstPool = graphics->getComputeCommands(); - } - else if (newOwner == Gfx::QueueType::GRAPHICS) { - barrier.dstAccessMask = getDestAccessMask(); - dstStage = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT; - dstPool = graphics->getGraphicsCommands(); - } - VkCommandBuffer srcCommand = sourcePool->getCommands()->getHandle(); - VkCommandBuffer dstCommand = dstPool->getCommands()->getHandle(); - vkCmdPipelineBarrier(srcCommand, srcStage, srcStage, 0, 0, nullptr, 1, &barrier, 0, nullptr); - vkCmdPipelineBarrier(dstCommand, dstStage, dstStage, 0, 0, nullptr, 1, &barrier, 0, nullptr); - sourcePool->submitCommands(); + if (getSize() == 0) + return; + Gfx::QueueFamilyMapping mapping = graphics->getFamilyMapping(); + VkBufferMemoryBarrier barrier = { + .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, + .pNext = nullptr, + .srcQueueFamilyIndex = mapping.getQueueTypeFamilyIndex(owner), + .dstQueueFamilyIndex = mapping.getQueueTypeFamilyIndex(newOwner), + .buffer = getHandle(), + .offset = 0, + .size = getSize(), + }; + PCommandPool sourcePool = graphics->getQueueCommands(owner); + PCommandPool dstPool = nullptr; + VkPipelineStageFlags srcStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; + VkPipelineStageFlags dstStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; + assert(barrier.srcQueueFamilyIndex != barrier.dstQueueFamilyIndex); + if (owner == Gfx::QueueType::TRANSFER) { + barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + srcStage = VK_PIPELINE_STAGE_TRANSFER_BIT; + } else if (owner == Gfx::QueueType::COMPUTE) { + barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; + srcStage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; + } else if (owner == Gfx::QueueType::GRAPHICS) { + barrier.srcAccessMask = getSourceAccessMask(); + srcStage = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT; + } + if (newOwner == Gfx::QueueType::TRANSFER) { + barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; + dstStage = VK_PIPELINE_STAGE_TRANSFER_BIT; + dstPool = graphics->getTransferCommands(); + } else if (newOwner == Gfx::QueueType::COMPUTE) { + barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; + dstStage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; + dstPool = graphics->getComputeCommands(); + } else if (newOwner == Gfx::QueueType::GRAPHICS) { + barrier.dstAccessMask = getDestAccessMask(); + dstStage = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT; + dstPool = graphics->getGraphicsCommands(); + } + VkCommandBuffer srcCommand = sourcePool->getCommands()->getHandle(); + VkCommandBuffer dstCommand = dstPool->getCommands()->getHandle(); + vkCmdPipelineBarrier(srcCommand, srcStage, srcStage, 0, 0, nullptr, 1, + &barrier, 0, nullptr); + vkCmdPipelineBarrier(dstCommand, dstStage, dstStage, 0, 0, nullptr, 1, + &barrier, 0, nullptr); + sourcePool->submitCommands(); } -void Buffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, VkAccessFlags dstAccess, - VkPipelineStageFlags dstStage) { - if (getSize() == 0) - return; - PCommand commandBuffer = graphics->getQueueCommands(owner)->getCommands(); - VkBufferMemoryBarrier barrier = { - .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, - .pNext = nullptr, - .srcAccessMask = srcAccess, - .dstAccessMask = dstAccess, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .buffer = getHandle(), - .offset = 0, - .size = getSize(), - }; - vkCmdPipelineBarrier(commandBuffer->getHandle(), srcStage, dstStage, 0, 0, nullptr, 1, &barrier, 0, - nullptr); +void Buffer::executePipelineBarrier(VkAccessFlags srcAccess, + VkPipelineStageFlags srcStage, + VkAccessFlags dstAccess, + VkPipelineStageFlags dstStage) { + if (getSize() == 0) + return; + PCommand commandBuffer = graphics->getQueueCommands(owner)->getCommands(); + VkBufferMemoryBarrier barrier = { + .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, + .pNext = nullptr, + .srcAccessMask = srcAccess, + .dstAccessMask = dstAccess, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .buffer = getHandle(), + .offset = 0, + .size = getSize(), + }; + vkCmdPipelineBarrier(commandBuffer->getHandle(), srcStage, dstStage, 0, 0, + nullptr, 1, &barrier, 0, nullptr); } -void* Buffer::map(bool writeOnly) { return mapRegion(0, getSize(), writeOnly); } +void *Buffer::map(bool writeOnly) { return mapRegion(0, getSize(), writeOnly); } -void* Buffer::mapRegion(uint64 regionOffset, uint64 regionSize, bool writeOnly) { - if (regionSize == 0) return nullptr; - void* data = nullptr; +void *Buffer::mapRegion(uint64 regionOffset, uint64 regionSize, + bool writeOnly) { + if (regionSize == 0) + return nullptr; + void *data = nullptr; - PendingBuffer pending; - pending.allocation = new BufferAllocation(graphics); - pending.allocation->size = regionSize; - pending.writeOnly = writeOnly; - pending.prevQueue = owner; - pending.offset = regionOffset; - if (writeOnly) { - if (buffers[currentBuffer]->properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { - VK_CHECK(vmaMapMemory(graphics->getAllocator(), buffers[currentBuffer]->allocation, &data)); - } - else { - VkBufferCreateInfo stagingInfo = { - .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, - .pNext = nullptr, - .flags = 0, - .size = regionSize, - .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - .sharingMode = VK_SHARING_MODE_EXCLUSIVE, - }; - VmaAllocationCreateInfo allocInfo = { - .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT, - .usage = VMA_MEMORY_USAGE_AUTO, - .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, - }; - VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, &allocInfo, &pending.allocation->buffer, - &pending.allocation->allocation, nullptr)); - vmaMapMemory(graphics->getAllocator(), pending.allocation->allocation, &data); - vmaSetAllocationName(graphics->getAllocator(), pending.allocation->allocation, "MappingStaging"); - } + PendingBuffer pending; + pending.allocation = new BufferAllocation(graphics); + pending.allocation->size = regionSize; + pending.writeOnly = writeOnly; + pending.prevQueue = owner; + pending.offset = regionOffset; + if (writeOnly) { + if (buffers[currentBuffer]->properties & + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { + VK_CHECK(vmaMapMemory(graphics->getAllocator(), + buffers[currentBuffer]->allocation, &data)); + } else { + VkBufferCreateInfo stagingInfo = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .size = regionSize, + .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + }; + VmaAllocationCreateInfo allocInfo = { + .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | + VMA_ALLOCATION_CREATE_MAPPED_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, + }; + VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, + &allocInfo, &pending.allocation->buffer, + &pending.allocation->allocation, nullptr)); + vmaMapMemory(graphics->getAllocator(), pending.allocation->allocation, + &data); + vmaSetAllocationName(graphics->getAllocator(), + pending.allocation->allocation, "MappingStaging"); } - else { - assert(false); - } - pendingBuffers[this] = std::move(pending); + } else { + assert(false); + } + pendingBuffers[this] = std::move(pending); - assert(data); - return data; + assert(data); + return data; } void Buffer::unmap() { - auto found = pendingBuffers.find(this); - if (found != pendingBuffers.end()) { - PendingBuffer& pending = found->value; - if (pending.writeOnly) { - if (buffers[currentBuffer]->properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { - vmaUnmapMemory(graphics->getAllocator(), buffers[currentBuffer]->allocation); - } - else { - vmaFlushAllocation(graphics->getAllocator(), pending.allocation->allocation, 0, VK_WHOLE_SIZE); - vmaUnmapMemory(graphics->getAllocator(), pending.allocation->allocation); - PCommand command = graphics->getQueueCommands(owner)->getCommands(); - command->bindResource(PBufferAllocation(pending.allocation)); - VkCommandBuffer cmdHandle = command->getHandle(); + auto found = pendingBuffers.find(this); + if (found != pendingBuffers.end()) { + PendingBuffer &pending = found->value; + if (pending.writeOnly) { + if (buffers[currentBuffer]->properties & + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { + vmaUnmapMemory(graphics->getAllocator(), + buffers[currentBuffer]->allocation); + } else { + vmaFlushAllocation(graphics->getAllocator(), + pending.allocation->allocation, 0, VK_WHOLE_SIZE); + vmaUnmapMemory(graphics->getAllocator(), + pending.allocation->allocation); + PCommand command = graphics->getQueueCommands(owner)->getCommands(); + command->bindResource(PBufferAllocation(pending.allocation)); + VkCommandBuffer cmdHandle = command->getHandle(); - VkBufferCopy region = { - .srcOffset = 0, - .dstOffset = pending.offset, - .size = pending.allocation->size, - }; - VkBufferMemoryBarrier barrier = { - .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, - .pNext = nullptr, - .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT, - .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .buffer = buffers[currentBuffer]->buffer, - .offset = 0, - .size = buffers[currentBuffer]->size, - }; - vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, - nullptr, 1, &barrier, 0, nullptr); - vkCmdCopyBuffer(cmdHandle, pending.allocation->buffer, buffers[currentBuffer]->buffer, 1, ®ion); - barrier = { - .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, - .pNext = nullptr, - .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, - .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .buffer = buffers[currentBuffer]->buffer, - .offset = 0, - .size = buffers[currentBuffer]->size, - }; - vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, - nullptr, 1, &barrier, 0, nullptr); - graphics->getDestructionManager()->queueResourceForDestruction(std::move(pending.allocation)); - } - } - pendingBuffers.erase(this); + VkBufferCopy region = { + .srcOffset = 0, + .dstOffset = pending.offset, + .size = pending.allocation->size, + }; + VkBufferMemoryBarrier barrier = { + .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, + .pNext = nullptr, + .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT, + .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .buffer = buffers[currentBuffer]->buffer, + .offset = 0, + .size = buffers[currentBuffer]->size, + }; + vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, + VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 1, + &barrier, 0, nullptr); + vkCmdCopyBuffer(cmdHandle, pending.allocation->buffer, + buffers[currentBuffer]->buffer, 1, ®ion); + barrier = { + .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, + .pNext = nullptr, + .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, + .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .buffer = buffers[currentBuffer]->buffer, + .offset = 0, + .size = buffers[currentBuffer]->size, + }; + vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, + 1, &barrier, 0, nullptr); + graphics->getDestructionManager()->queueResourceForDestruction( + std::move(pending.allocation)); + } } + pendingBuffers.erase(this); + } } -void Buffer::rotateBuffer(uint64 size) -{ - size = std::max(getSize(), size); - for (size_t i = 0; i < buffers.size(); ++i) - { - if (buffers[i]->isCurrentlyBound()) - { - continue; - } - if (buffers[i]->size < size) - { - vmaDestroyBuffer(graphics->getAllocator(), buffers[i]->buffer, buffers[i]->allocation); - VkBufferCreateInfo info = { - .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, - .pNext = nullptr, - .size = size, - .usage = usage, - .sharingMode = VK_SHARING_MODE_EXCLUSIVE, - }; - VmaAllocationCreateInfo allocInfo = { - .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | - VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, - .usage = VMA_MEMORY_USAGE_AUTO, - }; - VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &info, &allocInfo, &buffers[i]->buffer, &buffers[i]->allocation, - &buffers[i]->info)); - vmaGetAllocationMemoryProperties(graphics->getAllocator(), buffers[i]->allocation, &buffers[i]->properties); - if (!name.empty()) { - VkDebugUtilsObjectNameInfoEXT nameInfo = { .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, - .pNext = nullptr, - .objectType = VK_OBJECT_TYPE_BUFFER, - .objectHandle = (uint64)buffers[i]->buffer, - .pObjectName = this->name.c_str() }; - graphics->vkSetDebugUtilsObjectNameEXT(&nameInfo); - } - buffers[i]->size = size; - } - currentBuffer = i; - return; +void Buffer::rotateBuffer(uint64 size) { + size = std::max(getSize(), size); + for (size_t i = 0; i < buffers.size(); ++i) { + if (buffers[i]->isCurrentlyBound()) { + continue; } - createBuffer(size); + if (buffers[i]->size < size) { + vmaDestroyBuffer(graphics->getAllocator(), buffers[i]->buffer, + buffers[i]->allocation); + VkBufferCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = nullptr, + .size = size, + .usage = usage, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + }; + VmaAllocationCreateInfo allocInfo = { + .flags = + VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | + VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + }; + VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &info, &allocInfo, + &buffers[i]->buffer, &buffers[i]->allocation, + &buffers[i]->info)); + vmaGetAllocationMemoryProperties(graphics->getAllocator(), + buffers[i]->allocation, + &buffers[i]->properties); + if (!name.empty()) { + VkDebugUtilsObjectNameInfoEXT nameInfo = { + .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, + .pNext = nullptr, + .objectType = VK_OBJECT_TYPE_BUFFER, + .objectHandle = (uint64)buffers[i]->buffer, + .pObjectName = this->name.c_str()}; + graphics->vkSetDebugUtilsObjectNameEXT(&nameInfo); + } + buffers[i]->size = size; + } + currentBuffer = i; + return; + } + createBuffer(size); + currentBuffer = buffers.size() - 1; } -void Buffer::createBuffer(uint64 size) -{ - VkBufferCreateInfo info = { +void Buffer::createBuffer(uint64 size) { + VkBufferCreateInfo info = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .pNext = nullptr, .size = size, .usage = usage, .sharingMode = VK_SHARING_MODE_EXCLUSIVE, - }; - VmaAllocationCreateInfo allocInfo = { - .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | - VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, - .usage = VMA_MEMORY_USAGE_AUTO, - }; - buffers.add(new BufferAllocation(graphics)); - if (size > 0) - { - VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &info, &allocInfo, &buffers.back()->buffer, &buffers.back()->allocation, - &buffers.back()->info)); - buffers.back()->size = size; - vmaGetAllocationMemoryProperties(graphics->getAllocator(), buffers.back()->allocation, &buffers.back()->properties); - if (!name.empty()) { - VkDebugUtilsObjectNameInfoEXT nameInfo = { .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, - .pNext = nullptr, - .objectType = VK_OBJECT_TYPE_BUFFER, - .objectHandle = (uint64)buffers.back()->buffer, - .pObjectName = this->name.c_str() }; - graphics->vkSetDebugUtilsObjectNameEXT(&nameInfo); - } + }; + VmaAllocationCreateInfo allocInfo = { + .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | + VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + }; + buffers.add(new BufferAllocation(graphics)); + if (size > 0) { + VK_CHECK(vmaCreateBuffer( + graphics->getAllocator(), &info, &allocInfo, &buffers.back()->buffer, + &buffers.back()->allocation, &buffers.back()->info)); + buffers.back()->size = size; + vmaGetAllocationMemoryProperties(graphics->getAllocator(), + buffers.back()->allocation, + &buffers.back()->properties); + if (!name.empty()) { + VkDebugUtilsObjectNameInfoEXT nameInfo = { + .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, + .pNext = nullptr, + .objectType = VK_OBJECT_TYPE_BUFFER, + .objectHandle = (uint64)buffers.back()->buffer, + .pObjectName = this->name.c_str()}; + graphics->vkSetDebugUtilsObjectNameEXT(&nameInfo); } + } } -UniformBuffer::UniformBuffer(PGraphics graphics, const UniformBufferCreateInfo& createInfo) +UniformBuffer::UniformBuffer(PGraphics graphics, + const UniformBufferCreateInfo &createInfo) : Gfx::UniformBuffer(graphics->getFamilyMapping(), createInfo.sourceData), - Vulkan::Buffer(graphics, createInfo.sourceData.size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, currentOwner, - createInfo.dynamic, createInfo.name) { - if (getSize() > 0 && createInfo.sourceData.data != nullptr) { - void* data = map(); - std::memcpy(data, createInfo.sourceData.data, createInfo.sourceData.size); - unmap(); - } + Vulkan::Buffer(graphics, createInfo.sourceData.size, + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, currentOwner, + createInfo.dynamic, createInfo.name) { + if (getSize() > 0 && createInfo.sourceData.data != nullptr) { + void *data = map(); + std::memcpy(data, createInfo.sourceData.data, createInfo.sourceData.size); + unmap(); + } } UniformBuffer::~UniformBuffer() {} -bool UniformBuffer::updateContents(const DataSource& sourceData) { - if (!Gfx::UniformBuffer::updateContents(sourceData)) { - // no update was performed, skip - return false; - } - void* data = map(); - std::memcpy(data, sourceData.data, sourceData.size); - unmap(); - return true; +bool UniformBuffer::updateContents(const DataSource &sourceData) { + if (!Gfx::UniformBuffer::updateContents(sourceData)) { + // no update was performed, skip + return false; + } + void *data = map(); + std::memcpy(data, sourceData.data, sourceData.size); + unmap(); + return true; } void UniformBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) { - Gfx::QueueOwnedResource::transferOwnership(newOwner); + Gfx::QueueOwnedResource::transferOwnership(newOwner); } void UniformBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { - Vulkan::Buffer::executeOwnershipBarrier(newOwner); + Vulkan::Buffer::executeOwnershipBarrier(newOwner); } -void UniformBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, - VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) { - Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage); +void UniformBuffer::executePipelineBarrier(VkAccessFlags srcAccess, + VkPipelineStageFlags srcStage, + VkAccessFlags dstAccess, + VkPipelineStageFlags dstStage) { + Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, + dstStage); } -VkAccessFlags UniformBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; } +VkAccessFlags UniformBuffer::getSourceAccessMask() { + return VK_ACCESS_MEMORY_WRITE_BIT; +} -VkAccessFlags UniformBuffer::getDestAccessMask() { return VK_ACCESS_UNIFORM_READ_BIT; } +VkAccessFlags UniformBuffer::getDestAccessMask() { + return VK_ACCESS_UNIFORM_READ_BIT; +} -ShaderBuffer::ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo& sourceData) - : Gfx::ShaderBuffer(graphics->getFamilyMapping(), sourceData.numElements, sourceData.sourceData), - Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, currentOwner, - sourceData.dynamic, sourceData.name) { - if (getSize() > 0 && sourceData.sourceData.data != nullptr) { - void* data = map(); - std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size); - unmap(); - } +ShaderBuffer::ShaderBuffer(PGraphics graphics, + const ShaderBufferCreateInfo &sourceData) + : Gfx::ShaderBuffer(graphics->getFamilyMapping(), sourceData.numElements, + sourceData.sourceData), + Vulkan::Buffer(graphics, sourceData.sourceData.size, + VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, currentOwner, + sourceData.dynamic, sourceData.name) { + if (getSize() > 0 && sourceData.sourceData.data != nullptr) { + void *data = map(); + std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size); + unmap(); + } } ShaderBuffer::~ShaderBuffer() {} -void ShaderBuffer::updateContents(const ShaderBufferCreateInfo& createInfo) { - Gfx::ShaderBuffer::updateContents(createInfo); - // We always want to update, as the contents could be different on the GPU - if (createInfo.sourceData.data == nullptr) - { - return; - } - void* data = map(); - std::memcpy(data, createInfo.sourceData.data, createInfo.sourceData.size); - unmap(); +void ShaderBuffer::updateContents(const ShaderBufferCreateInfo &createInfo) { + Gfx::ShaderBuffer::updateContents(createInfo); + // We always want to update, as the contents could be different on the GPU + if (createInfo.sourceData.data == nullptr) { + return; + } + void *data = map(); + std::memcpy((char*)data + createInfo.sourceData.offset, createInfo.sourceData.data, createInfo.sourceData.size); + unmap(); } -void Seele::Vulkan::ShaderBuffer::rotateBuffer(uint64 size) -{ - assert(dynamic); - Vulkan::Buffer::rotateBuffer(size); +void Seele::Vulkan::ShaderBuffer::rotateBuffer(uint64 size) { + assert(dynamic); + Vulkan::Buffer::rotateBuffer(size); } -void* ShaderBuffer::mapRegion(uint64 offset, uint64 size, bool writeOnly) -{ - return Vulkan::Buffer::mapRegion(offset, size, writeOnly); +void *ShaderBuffer::mapRegion(uint64 offset, uint64 size, bool writeOnly) { + return Vulkan::Buffer::mapRegion(offset, size, writeOnly); } -void ShaderBuffer::unmap() -{ - Vulkan::Buffer::unmap(); -} +void ShaderBuffer::unmap() { Vulkan::Buffer::unmap(); } void ShaderBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) { - Gfx::QueueOwnedResource::transferOwnership(newOwner); + Gfx::QueueOwnedResource::transferOwnership(newOwner); } void ShaderBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { - Vulkan::Buffer::executeOwnershipBarrier(newOwner); + Vulkan::Buffer::executeOwnershipBarrier(newOwner); } -void ShaderBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, - VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) { - Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage); +void ShaderBuffer::executePipelineBarrier(VkAccessFlags srcAccess, + VkPipelineStageFlags srcStage, + VkAccessFlags dstAccess, + VkPipelineStageFlags dstStage) { + Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, + dstStage); } -VkAccessFlags ShaderBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; } +VkAccessFlags ShaderBuffer::getSourceAccessMask() { + return VK_ACCESS_MEMORY_WRITE_BIT; +} -VkAccessFlags ShaderBuffer::getDestAccessMask() { return VK_ACCESS_MEMORY_READ_BIT; } +VkAccessFlags ShaderBuffer::getDestAccessMask() { + return VK_ACCESS_MEMORY_READ_BIT; +} -VertexBuffer::VertexBuffer(PGraphics graphics, const VertexBufferCreateInfo& sourceData) - : Gfx::VertexBuffer(graphics->getFamilyMapping(), sourceData.numVertices, sourceData.vertexSize, - sourceData.sourceData.owner), - Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, currentOwner, false, - sourceData.name) { - if (sourceData.sourceData.data != nullptr) { - void* data = map(); - std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size); - unmap(); - } +VertexBuffer::VertexBuffer(PGraphics graphics, + const VertexBufferCreateInfo &sourceData) + : Gfx::VertexBuffer(graphics->getFamilyMapping(), sourceData.numVertices, + sourceData.vertexSize, sourceData.sourceData.owner), + Vulkan::Buffer(graphics, sourceData.sourceData.size, + VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, currentOwner, false, + sourceData.name) { + if (sourceData.sourceData.data != nullptr) { + void *data = map(); + std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size); + unmap(); + } } VertexBuffer::~VertexBuffer() {} void VertexBuffer::updateRegion(DataSource update) { - void* data = mapRegion(update.offset, update.size); - std::memcpy(data, update.data, update.size); - unmap(); + void *data = mapRegion(update.offset, update.size); + std::memcpy(data, update.data, update.size); + unmap(); } -void VertexBuffer::download(Array& buffer) { - void* data = map(false); - buffer.resize(getSize()); - std::memcpy(buffer.data(), data, getSize()); - unmap(); +void VertexBuffer::download(Array &buffer) { + void *data = map(false); + buffer.resize(getSize()); + std::memcpy(buffer.data(), data, getSize()); + unmap(); } void VertexBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) { - Gfx::QueueOwnedResource::transferOwnership(newOwner); + Gfx::QueueOwnedResource::transferOwnership(newOwner); } void VertexBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { - Vulkan::Buffer::executeOwnershipBarrier(newOwner); + Vulkan::Buffer::executeOwnershipBarrier(newOwner); } -void VertexBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, - VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) { - Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage); +void VertexBuffer::executePipelineBarrier(VkAccessFlags srcAccess, + VkPipelineStageFlags srcStage, + VkAccessFlags dstAccess, + VkPipelineStageFlags dstStage) { + Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, + dstStage); } -VkAccessFlags VertexBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; } +VkAccessFlags VertexBuffer::getSourceAccessMask() { + return VK_ACCESS_MEMORY_WRITE_BIT; +} -VkAccessFlags VertexBuffer::getDestAccessMask() { return VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; } +VkAccessFlags VertexBuffer::getDestAccessMask() { + return VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; +} -IndexBuffer::IndexBuffer(PGraphics graphics, const IndexBufferCreateInfo& sourceData) - : Gfx::IndexBuffer(graphics->getFamilyMapping(), sourceData.sourceData.size, sourceData.indexType, - sourceData.sourceData.owner), - Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, currentOwner, false, - sourceData.name) { - if (sourceData.sourceData.data != nullptr) { - void* data = map(); - std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size); - unmap(); - } +IndexBuffer::IndexBuffer(PGraphics graphics, + const IndexBufferCreateInfo &sourceData) + : Gfx::IndexBuffer(graphics->getFamilyMapping(), sourceData.sourceData.size, + sourceData.indexType, sourceData.sourceData.owner), + Vulkan::Buffer(graphics, sourceData.sourceData.size, + VK_BUFFER_USAGE_INDEX_BUFFER_BIT, currentOwner, false, + sourceData.name) { + if (sourceData.sourceData.data != nullptr) { + void *data = map(); + std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size); + unmap(); + } } IndexBuffer::~IndexBuffer() {} -void IndexBuffer::download(Array& buffer) { - void* data = map(false); - buffer.resize(getSize()); - std::memcpy(buffer.data(), data, getSize()); - unmap(); +void IndexBuffer::download(Array &buffer) { + void *data = map(false); + buffer.resize(getSize()); + std::memcpy(buffer.data(), data, getSize()); + unmap(); } void IndexBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) { - Gfx::QueueOwnedResource::transferOwnership(newOwner); + Gfx::QueueOwnedResource::transferOwnership(newOwner); } void IndexBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { - Vulkan::Buffer::executeOwnershipBarrier(newOwner); + Vulkan::Buffer::executeOwnershipBarrier(newOwner); } -void IndexBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, - VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) { - Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage); +void IndexBuffer::executePipelineBarrier(VkAccessFlags srcAccess, + VkPipelineStageFlags srcStage, + VkAccessFlags dstAccess, + VkPipelineStageFlags dstStage) { + Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, + dstStage); } -VkAccessFlags IndexBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; } +VkAccessFlags IndexBuffer::getSourceAccessMask() { + return VK_ACCESS_MEMORY_WRITE_BIT; +} -VkAccessFlags IndexBuffer::getDestAccessMask() { return VK_ACCESS_INDEX_READ_BIT; } \ No newline at end of file +VkAccessFlags IndexBuffer::getDestAccessMask() { + return VK_ACCESS_INDEX_READ_BIT; +} \ No newline at end of file diff --git a/src/Engine/Graphics/Vulkan/Buffer.h b/src/Engine/Graphics/Vulkan/Buffer.h index 2f2ed16..2b3478b 100644 --- a/src/Engine/Graphics/Vulkan/Buffer.h +++ b/src/Engine/Graphics/Vulkan/Buffer.h @@ -9,13 +9,13 @@ DECLARE_REF(Command) DECLARE_REF(Fence) class BufferAllocation : public CommandBoundResource { public: - BufferAllocation(PGraphics graphics); - virtual ~BufferAllocation(); - VkBuffer buffer = VK_NULL_HANDLE; - VmaAllocation allocation = VmaAllocation(); - VmaAllocationInfo info = VmaAllocationInfo(); - VkMemoryPropertyFlags properties = 0; - uint64 size = 0; + BufferAllocation(PGraphics graphics); + virtual ~BufferAllocation(); + VkBuffer buffer = VK_NULL_HANDLE; + VmaAllocation allocation = VmaAllocation(); + VmaAllocationInfo info = VmaAllocationInfo(); + VkMemoryPropertyFlags properties = 0; + uint64 size = 0; }; DEFINE_REF(BufferAllocation); class Buffer { @@ -70,10 +70,11 @@ protected: virtual void requestOwnershipTransfer(Gfx::QueueType newOwner) override; // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(Gfx::QueueType newOwner) override; - virtual void executePipelineBarrier(Gfx::SeAccessFlags srcAccess, - Gfx::SePipelineStageFlags srcStage, - Gfx::SeAccessFlags dstAccess, - Gfx::SePipelineStageFlags dstStage) override; + virtual void + executePipelineBarrier(Gfx::SeAccessFlags srcAccess, + Gfx::SePipelineStageFlags srcStage, + Gfx::SeAccessFlags dstAccess, + Gfx::SePipelineStageFlags dstStage) override; }; DEFINE_REF(VertexBuffer) @@ -91,10 +92,11 @@ protected: virtual void requestOwnershipTransfer(Gfx::QueueType newOwner) override; // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(Gfx::QueueType newOwner) override; - virtual void executePipelineBarrier(Gfx::SeAccessFlags srcAccess, - Gfx::SePipelineStageFlags srcStage, - Gfx::SeAccessFlags dstAccess, - Gfx::SePipelineStageFlags dstStage) override; + virtual void + executePipelineBarrier(Gfx::SeAccessFlags srcAccess, + Gfx::SePipelineStageFlags srcStage, + Gfx::SeAccessFlags dstAccess, + Gfx::SePipelineStageFlags dstStage) override; }; DEFINE_REF(IndexBuffer) class UniformBuffer : public Gfx::UniformBuffer, public Buffer { @@ -110,10 +112,11 @@ protected: virtual void requestOwnershipTransfer(Gfx::QueueType newOwner) override; // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(Gfx::QueueType newOwner) override; - virtual void executePipelineBarrier(Gfx::SeAccessFlags srcAccess, - Gfx::SePipelineStageFlags srcStage, - Gfx::SeAccessFlags dstAccess, - Gfx::SePipelineStageFlags dstStage) override; + virtual void + executePipelineBarrier(Gfx::SeAccessFlags srcAccess, + Gfx::SePipelineStageFlags srcStage, + Gfx::SeAccessFlags dstAccess, + Gfx::SePipelineStageFlags dstStage) override; private: }; @@ -123,9 +126,10 @@ class ShaderBuffer : public Gfx::ShaderBuffer, public Buffer { public: ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo &sourceData); virtual ~ShaderBuffer(); - virtual void updateContents(const ShaderBufferCreateInfo &createInfo) override; + virtual void + updateContents(const ShaderBufferCreateInfo &createInfo) override; virtual void rotateBuffer(uint64 size) override; - virtual void* mapRegion(uint64 offset, uint64 size, bool writeOnly) override; + virtual void *mapRegion(uint64 offset, uint64 size, bool writeOnly) override; virtual void unmap() override; protected: @@ -135,10 +139,11 @@ protected: virtual void requestOwnershipTransfer(Gfx::QueueType newOwner) override; // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(Gfx::QueueType newOwner) override; - virtual void executePipelineBarrier(Gfx::SeAccessFlags srcAccess, - Gfx::SePipelineStageFlags srcStage, - Gfx::SeAccessFlags dstAccess, - Gfx::SePipelineStageFlags dstStage) override; + virtual void + executePipelineBarrier(Gfx::SeAccessFlags srcAccess, + Gfx::SePipelineStageFlags srcStage, + Gfx::SeAccessFlags dstAccess, + Gfx::SePipelineStageFlags dstStage) override; private: }; diff --git a/src/Engine/Graphics/slang-compile.cpp b/src/Engine/Graphics/slang-compile.cpp index 33f846f..f886a94 100644 --- a/src/Engine/Graphics/slang-compile.cpp +++ b/src/Engine/Graphics/slang-compile.cpp @@ -23,9 +23,9 @@ Slang::ComPtr Seele::generateShader(const ShaderCreateInfo& create option[1].name = slang::CompilerOptionName::EmitSpirvViaGLSL; option[1].value.kind = slang::CompilerOptionValueKind::Int; option[1].value.intValue0 = 1; - option[2].name = slang::CompilerOptionName::DebugInformation; + option[2].name = slang::CompilerOptionName::EmitIr; option[2].value.kind = slang::CompilerOptionValueKind::Int; - option[2].value.intValue0 = SLANG_DEBUG_INFO_LEVEL_NONE; + option[2].value.intValue0 = 1; option[3].name = slang::CompilerOptionName::DebugInformationFormat; option[3].value.kind = slang::CompilerOptionValueKind::Int; option[3].value.intValue0 = SLANG_DEBUG_INFO_FORMAT_C7; diff --git a/src/Engine/Material/Material.h b/src/Engine/Material/Material.h index 26769be..c4893d9 100644 --- a/src/Engine/Material/Material.h +++ b/src/Engine/Material/Material.h @@ -22,7 +22,7 @@ public: Gfx::PDescriptorLayout getDescriptorLayout() { return layout; } OMaterialInstance instantiate(); const std::string& getName() const { return materialName; } - constexpr const uint64 getId() const { return materialId; } + constexpr uint64 getId() const { return materialId; } static constexpr const PMaterial findMaterialById(uint64 id) { return materials[id]; } void save(ArchiveBuffer& buffer) const;