From 32d3bd8ad8d9b0ebab526bbfae28611602e77faa Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Fri, 1 Nov 2024 21:04:06 +0100 Subject: [PATCH] Some actually useful metal changes??? --- external/vcpkg | 2 +- src/Editor/Asset/MeshLoader.cpp | 3 +- src/Editor/main.cpp | 6 +- src/Engine/Graphics/CBT/CBT.h | 4 +- src/Engine/Graphics/Descriptor.h | 2 + src/Engine/Graphics/Graphics.h | 2 - src/Engine/Graphics/Metal/Buffer.h | 9 ++- src/Engine/Graphics/Metal/Buffer.mm | 22 +++--- src/Engine/Graphics/Metal/Command.h | 2 + src/Engine/Graphics/Metal/Command.mm | 9 ++- src/Engine/Graphics/Metal/Descriptor.h | 27 ++++--- src/Engine/Graphics/Metal/Descriptor.mm | 77 +++++++++++++------ src/Engine/Graphics/Metal/Graphics.h | 5 ++ src/Engine/Graphics/Metal/Graphics.mm | 13 ++++ src/Engine/Graphics/RenderPass/BasePass.cpp | 9 ++- .../Graphics/RenderPass/CachedDepthPass.cpp | 6 +- .../Graphics/RenderPass/LightCullingPass.cpp | 1 + src/Engine/Graphics/RenderPass/RenderPass.cpp | 3 +- src/Engine/Graphics/RenderPass/TextPass.cpp | 1 + src/Engine/Graphics/RenderPass/UIPass.cpp | 2 + .../Graphics/RenderPass/WaterRenderer.cpp | 1 + src/Engine/Graphics/Shader.cpp | 8 +- src/Engine/Graphics/Shader.h | 2 +- src/Engine/Graphics/slang-compile.cpp | 4 +- src/Engine/Scene/LightEnvironment.cpp | 1 + src/Engine/ThreadPool.h | 4 +- 26 files changed, 152 insertions(+), 73 deletions(-) diff --git a/external/vcpkg b/external/vcpkg index 4002e5f..509f71e 160000 --- a/external/vcpkg +++ b/external/vcpkg @@ -1 +1 @@ -Subproject commit 4002e5f3598cc0d216c395bc0db3de44a2d2ec9b +Subproject commit 509f71e53f45e46c13fa7935d2f6a45803580c07 diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index 6333528..6028a06 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -399,7 +399,8 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Array& }; uint32 twoSided = false; float opacity = 1.0f; - const char* mat = material->GetName().C_Str(); + aiString matName = material->GetName(); + const char* mat = matName.C_Str(); if (strcmp(mat, "Leaves0119_14_S") == 0) { opacity = 0.5f; diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index 0a022e7..ae09a82 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -106,9 +106,9 @@ int main() { //AssetImporter::importTexture(TextureImportArgs{ // .filePath = sourcePath / "import/textures/azeroth_height.png", //}); - AssetImporter::importTexture(TextureImportArgs{ - .filePath = sourcePath / "import/textures/wgen.png", - }); + //AssetImporter::importTexture(TextureImportArgs{ + // .filePath = sourcePath / "import/textures/wgen.png", + //}); //AssetImporter::importMesh(MeshImportArgs{ // .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb", // .importPath = "Whitechapel", diff --git a/src/Engine/Graphics/CBT/CBT.h b/src/Engine/Graphics/CBT/CBT.h index 6c22279..7bd6ab1 100644 --- a/src/Engine/Graphics/CBT/CBT.h +++ b/src/Engine/Graphics/CBT/CBT.h @@ -216,7 +216,7 @@ template class CBT { uint32_t first_bit = tree.depthOffset[currentDepth] + tree.bitCount[currentDepth] * id_in_level; uint32_t local_id = first_bit % 64; uint64_t target_bits = (heapValue >> local_id) & tree.bitMask[currentDepth]; - uint32_t heapValue = c - countbits(target_bits); + uint32_t heapValue = c - std::popcount(target_bits); uint32_t b = handle < heapValue ? 0u : 1u; @@ -507,4 +507,4 @@ struct MeshUpdater { Gfx::OComputeShader lebEvaluateCS; Gfx::PComputePipeline lebEvaluate; }; -}; // namespace Seele \ No newline at end of file +}; // namespace Seele diff --git a/src/Engine/Graphics/Descriptor.h b/src/Engine/Graphics/Descriptor.h index 18c03f2..18561fb 100644 --- a/src/Engine/Graphics/Descriptor.h +++ b/src/Engine/Graphics/Descriptor.h @@ -13,6 +13,8 @@ struct DescriptorBinding { SeDescriptorBindingFlags bindingFlags = 0; SeShaderStageFlags shaderStages = SE_SHADER_STAGE_ALL; Gfx::SeDescriptorAccessTypeFlags access = SE_DESCRIPTOR_ACCESS_READ_ONLY_BIT; + // In Metal uniforms are plain bytes, and for that we need to know the struct size + uint32 uniformLength = 0; }; DECLARE_REF(DescriptorPool) diff --git a/src/Engine/Graphics/Graphics.h b/src/Engine/Graphics/Graphics.h index 6769a04..2117048 100644 --- a/src/Engine/Graphics/Graphics.h +++ b/src/Engine/Graphics/Graphics.h @@ -88,8 +88,6 @@ class Graphics { virtual PComputePipeline createComputePipeline(ComputePipelineCreateInfo createInfo) = 0; virtual OSampler createSampler(const SamplerCreateInfo& createInfo) = 0; - virtual OComputeShader createComputeShaderFromBinary(std::string_view binaryName) = 0; - virtual ODescriptorLayout createDescriptorLayout(const std::string& name = "") = 0; virtual OPipelineLayout createPipelineLayout(const std::string& name = "", PPipelineLayout baseLayout = nullptr) = 0; diff --git a/src/Engine/Graphics/Metal/Buffer.h b/src/Engine/Graphics/Metal/Buffer.h index cbf36a1..e4dd91f 100644 --- a/src/Engine/Graphics/Metal/Buffer.h +++ b/src/Engine/Graphics/Metal/Buffer.h @@ -89,18 +89,23 @@ class IndexBuffer : public Gfx::IndexBuffer, public Buffer { }; DEFINE_REF(IndexBuffer) DEFINE_REF(IndexBuffer) -class UniformBuffer : public Gfx::UniformBuffer, public Buffer { +// Metal Uniform Buffer is not a buffer, but just plain bytes +class UniformBuffer : public Gfx::UniformBuffer { public: UniformBuffer(PGraphics graphics, const UniformBufferCreateInfo& createInfo); virtual ~UniformBuffer(); virtual void updateContents(uint64 offset, uint64 size, void* data) override; virtual void rotateBuffer(uint64 size) override; - + void* getContents() const { return contents.data(); } + size_t getSize() const { return contents.size(); } + protected: // 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; +private: + Array contents; }; DEFINE_REF(UniformBuffer) class ShaderBuffer : public Gfx::ShaderBuffer, public Buffer { diff --git a/src/Engine/Graphics/Metal/Buffer.mm b/src/Engine/Graphics/Metal/Buffer.mm index 012ce0f..ffa7605 100644 --- a/src/Engine/Graphics/Metal/Buffer.mm +++ b/src/Engine/Graphics/Metal/Buffer.mm @@ -164,36 +164,38 @@ void IndexBuffer::executePipelineBarrier(Gfx::SeAccessFlags srcAccess, Gfx::SePi } UniformBuffer::UniformBuffer(PGraphics graphics, const UniformBufferCreateInfo& createInfo) - : Gfx::UniformBuffer(graphics->getFamilyMapping(), createInfo), - Metal::Buffer(graphics, createInfo.sourceData.size, Gfx::SE_BUFFER_USAGE_UNIFORM_BUFFER_BIT, createInfo.sourceData.owner, - createInfo.dynamic, createInfo.name) { + : Gfx::UniformBuffer(graphics->getFamilyMapping(), createInfo) + , contents(createInfo.sourceData.size) { if (createInfo.sourceData.size > 0 && createInfo.sourceData.data != nullptr) { - getAlloc()->updateContents(createInfo.sourceData.offset, createInfo.sourceData.size, createInfo.sourceData.data); + std::memcpy(contents.data(), createInfo.sourceData.data, createInfo.sourceData.size); } } UniformBuffer::~UniformBuffer() {} -void UniformBuffer::rotateBuffer(uint64 size) { Metal::Buffer::rotateBuffer(size); } +void UniformBuffer::rotateBuffer(uint64 size) { + contents.clear(); + contents.resize(size); +} void UniformBuffer::updateContents(uint64 offset, uint64 size, void* data) { if (size > 0 && data != nullptr) { - Metal::Buffer::rotateBuffer(size); - getAlloc()->updateContents(offset, size, data); + contents.resize(offset+size); + std::memcpy(contents.data()+offset, data, size); } } -void UniformBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { Metal::Buffer::transferOwnership(newOwner); } +void UniformBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { } void UniformBuffer::executePipelineBarrier(Gfx::SeAccessFlags srcAccess, Gfx::SePipelineStageFlags srcStage, Gfx::SeAccessFlags dstAccess, Gfx::SePipelineStageFlags dstStage) { - Metal::Buffer::pipelineBarrier(srcAccess, srcStage, dstAccess, dstStage); + } ShaderBuffer::ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo& createInfo) : Gfx::ShaderBuffer(graphics->getFamilyMapping(), createInfo), Seele::Metal::Buffer(graphics, createInfo.sourceData.size, Gfx::SE_BUFFER_USAGE_STORAGE_BUFFER_BIT, createInfo.sourceData.owner, - createInfo.dynamic, createInfo.name, createInfo.createCleared, createInfo.clearValue) {} + true, createInfo.name, createInfo.sourceData.data == nullptr, createInfo.clearValue) {} ShaderBuffer::~ShaderBuffer() {} diff --git a/src/Engine/Graphics/Metal/Command.h b/src/Engine/Graphics/Metal/Command.h index 80a57b9..d181e11 100644 --- a/src/Engine/Graphics/Metal/Command.h +++ b/src/Engine/Graphics/Metal/Command.h @@ -63,6 +63,7 @@ class RenderCommand : public Gfx::RenderCommand { virtual void bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) override; 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 drawIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride); 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; @@ -86,6 +87,7 @@ class ComputeCommand : public Gfx::ComputeCommand { virtual void bindDescriptor(const Array& sets) 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; + virtual void dispatchIndirect(Gfx::PShaderBuffer buffer, uint32 offset) override; private: PComputePipeline boundPipeline; diff --git a/src/Engine/Graphics/Metal/Command.mm b/src/Engine/Graphics/Metal/Command.mm index 562dc09..4b512c1 100644 --- a/src/Engine/Graphics/Metal/Command.mm +++ b/src/Engine/Graphics/Metal/Command.mm @@ -127,11 +127,14 @@ void RenderCommand::draw(uint32 vertexCount, uint32 instanceCount, int32 firstVe encoder->drawPrimitives(boundPipeline->getPrimitive(), firstVertex, vertexCount, instanceCount, firstInstance); } +void RenderCommand::drawIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride) { + encoder->drawPrimitives(MTL::PrimitiveTypeTriangle, buffer.cast()->getHandle(), offset); +} + void RenderCommand::drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset, uint32 firstInstance) { encoder->drawIndexedPrimitives(boundPipeline->getPrimitive(), indexCount, cast(boundIndexBuffer->getIndexType()), boundIndexBuffer->getHandle(), firstIndex, instanceCount, vertexOffset, firstInstance); } - 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)); } @@ -184,6 +187,10 @@ void ComputeCommand::dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) { encoder->dispatchThreadgroups(MTL::Size(threadX, threadY, threadZ), MTL::Size(32, 32, 1)); } +void ComputeCommand::dispatchIndirect(Gfx::PShaderBuffer buffer, uint32 offset){ + encoder->dispatchThreadgroups(buffer.cast()->getHandle(), offset, MTL::Size(32, 32, 1)); +} + CommandQueue::CommandQueue(PGraphics graphics) : graphics(graphics) { queue = graphics->getDevice()->newCommandQueue(); MTL::CommandBufferDescriptor* descriptor = MTL::CommandBufferDescriptor::alloc()->init(); diff --git a/src/Engine/Graphics/Metal/Descriptor.h b/src/Engine/Graphics/Metal/Descriptor.h index 7bae82b..6bf4de9 100644 --- a/src/Engine/Graphics/Metal/Descriptor.h +++ b/src/Engine/Graphics/Metal/Descriptor.h @@ -19,10 +19,15 @@ class DescriptorLayout : public Gfx::DescriptorLayout { virtual ~DescriptorLayout(); virtual void create() override; MTL::ArgumentEncoder* createEncoder(); - + uint32 getFlattenedIndex(uint32 binding, uint32 arrayIndex) const { return flattenMap.at(flattenIndex(binding, arrayIndex)); } + constexpr uint32 getTotalBindingCount() const { return flattenedBindingCount; } + private: + constexpr uint64 flattenIndex(uint32 binding, uint32 arrayIndex) const { return uint64(arrayIndex) << 32 | binding; } PGraphics graphics; NS::Array* arguments; + Map flattenMap; + uint32 flattenedBindingCount = 0; }; DEFINE_REF(DescriptorLayout) @@ -41,21 +46,21 @@ class DescriptorPool : public Gfx::DescriptorPool { Array allocatedSets; }; DEFINE_REF(DescriptorPool) - class DescriptorSet : public Gfx::DescriptorSet, public CommandBoundResource { public: DescriptorSet(PGraphics graphics, PDescriptorPool owner); virtual ~DescriptorSet(); virtual void writeChanges() override; - virtual void updateBuffer(uint32 binding, Gfx::PUniformBuffer uniformBuffer) override; - virtual void updateBuffer(uint32 binding, Gfx::PShaderBuffer uniformBuffer) override; - virtual void updateBuffer(uint32 binding, Gfx::PIndexBuffer uniformBuffer) override; - virtual void updateSampler(uint32 binding, Gfx::PSampler samplerState) override; - virtual void updateTexture(uint32 binding, Gfx::PTexture texture, Gfx::PSampler sampler = nullptr) override; - virtual void updateTextureArray(uint32 binding, Array texture) override; - virtual void updateSamplerArray(uint32 binding, Array samplers) override; - virtual void updateAccelerationStructure(uint32 binding, Gfx::PTopLevelAS as) override; - + virtual void updateBuffer(uint32 binding, uint32 index, Gfx::PUniformBuffer uniformBuffer) override; + virtual void updateBuffer(uint32 binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) override; + virtual void updateBuffer(uint32 binding, uint32 index, Gfx::PVertexBuffer uniformBuffer) override; + virtual void updateBuffer(uint32 binding, uint32 index, Gfx::PIndexBuffer uniformBuffer) override; + virtual void updateSampler(uint32 binding, uint32 index, Gfx::PSampler samplerState) override; + virtual void updateTexture(uint32 binding, uint32 index, Gfx::PTexture2D texture) override; + virtual void updateTexture(uint32 binding, uint32 index, Gfx::PTexture3D texture) override; + virtual void updateTexture(uint32 binding, uint32 index, Gfx::PTextureCube texture) override; + virtual void updateAccelerationStructure(uint32 binding, uint32 index, Gfx::PTopLevelAS as) override; + constexpr const Array& getBoundResources() const { return boundResources; } MTL::Buffer* getArgumentBuffer() const { return argumentBuffer; } diff --git a/src/Engine/Graphics/Metal/Descriptor.mm b/src/Engine/Graphics/Metal/Descriptor.mm index 114339e..bfc6931 100644 --- a/src/Engine/Graphics/Metal/Descriptor.mm +++ b/src/Engine/Graphics/Metal/Descriptor.mm @@ -32,9 +32,10 @@ void DescriptorLayout::create() { pool = new DescriptorPool(graphics, this); hash = CRC::Calculate(descriptorBindings.data(), sizeof(Gfx::DescriptorBinding) * descriptorBindings.size(), CRC::CRC_32()); MTL::ArgumentDescriptor** objects = new MTL::ArgumentDescriptor*[descriptorBindings.size()]; + flattenedBindingCount = 0; for (uint32 i = 0; i < descriptorBindings.size(); ++i) { objects[i] = MTL::ArgumentDescriptor::alloc()->init(); - objects[i]->setIndex(i); + objects[i]->setIndex(flattenedBindingCount); objects[i]->setAccess(cast(descriptorBindings[i].access)); objects[i]->setArrayLength(descriptorBindings[i].descriptorCount); MTL::DataType dataType; @@ -47,13 +48,16 @@ void DescriptorLayout::create() { case Gfx::SE_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: dataType = MTL::DataTypeTexture; break; - case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER: case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER: - case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: - case Gfx::SE_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK: dataType = MTL::DataTypePointer; break; + case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER: + case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: + case Gfx::SE_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK: + dataType = MTL::DataTypeChar; + objects[i]->setArrayLength(descriptorBindings[i].uniformLength); + break; case Gfx::SE_DESCRIPTOR_TYPE_SAMPLER: dataType = MTL::DataTypeSampler; break; @@ -89,6 +93,9 @@ void DescriptorLayout::create() { break; } objects[i]->setTextureType(textureType); + for(uint32 j = 0; j < descriptorBindings[i].descriptorCount; ++j) { + flattenMap[flattenIndex(i, j)] = flattenedBindingCount++; + } } arguments = NS::Array::array((NS::Object**)objects, descriptorBindings.size()); } @@ -119,49 +126,71 @@ DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner) : Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics), graphics(graphics), owner(owner) { encoder = owner->getLayout()->createEncoder(); argumentBuffer = graphics->getDevice()->newBuffer(encoder->encodedLength(), 0); + std::cout << owner->getLayout()->getName() << ": " << encoder->encodedLength() << std::endl; encoder->setArgumentBuffer(argumentBuffer, 0); - boundResources.resize(owner->getLayout()->getBindings().size()); + boundResources.resize(owner->getLayout()->getTotalBindingCount()); } DescriptorSet::~DescriptorSet() {} void DescriptorSet::writeChanges() {} -void DescriptorSet::updateBuffer(uint32 binding, Gfx::PUniformBuffer uniformBuffer) { +void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PUniformBuffer uniformBuffer) { + uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index); PUniformBuffer buffer = uniformBuffer.cast(); - encoder->setBuffer(buffer->getHandle(), 0, binding); - boundResources[binding] = buffer->getHandle(); + std::memcpy(encoder->constantData(flattenedIndex), buffer->getContents(), buffer->getSize()); + boundResources[flattenedIndex] = nullptr; } -void DescriptorSet::updateBuffer(uint32 binding, Gfx::PShaderBuffer uniformBuffer) { +void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) { + uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index); PShaderBuffer buffer = uniformBuffer.cast(); - encoder->setBuffer(buffer->getHandle(), 0, binding); - boundResources[binding] = buffer->getHandle(); + encoder->setBuffer(buffer->getHandle(), 0, flattenedIndex); + boundResources[flattenedIndex] = buffer->getHandle(); } -void DescriptorSet::updateBuffer(uint32 binding, Gfx::PIndexBuffer uniformBuffer) { +void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PVertexBuffer uniformBuffer) { + uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index); + PVertexBuffer buffer = uniformBuffer.cast(); + encoder->setBuffer(buffer->getHandle(), 0, flattenedIndex); + boundResources[flattenedIndex] = buffer->getHandle(); +} +void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PIndexBuffer uniformBuffer) { + uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index); PIndexBuffer buffer = uniformBuffer.cast(); - encoder->setBuffer(buffer->getHandle(), 0, binding); - boundResources[binding] = buffer->getHandle(); + encoder->setBuffer(buffer->getHandle(), 0, flattenedIndex); + boundResources[flattenedIndex] = buffer->getHandle(); } -void DescriptorSet::updateSampler(uint32 binding, Gfx::PSampler samplerState) { +void DescriptorSet::updateSampler(uint32 binding, uint32 index, Gfx::PSampler samplerState) { + uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index); PSampler sampler = samplerState.cast(); - encoder->setSamplerState(sampler->getHandle(), binding); - boundResources[binding] = nullptr; // Samplers are not resources?????? + encoder->setSamplerState(sampler->getHandle(), flattenedIndex); + boundResources[flattenedIndex] = nullptr; // Samplers are not resources?????? } -void DescriptorSet::updateTexture(uint32 binding, Gfx::PTexture texture, Gfx::PSampler sampler) { - PTextureBase base = texture.cast(); - encoder->setTexture(base->getHandle()->texture, binding); - boundResources[binding] = base->getHandle()->texture; +void DescriptorSet::updateTexture(uint32 binding, uint32 index, Gfx::PTexture2D texture) { + uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index); + PTextureBase tex = texture.cast(); + encoder->setTexture(tex->getImage(), flattenedIndex); + boundResources[flattenedIndex] = tex->getImage(); } -void DescriptorSet::updateTextureArray(uint32 binding, Array texture) { assert(false && "TODO"); } +void DescriptorSet::updateTexture(uint32 binding, uint32 index, Gfx::PTexture3D texture) { + uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index); + PTextureBase tex = texture.cast(); + encoder->setTexture(tex->getImage(), flattenedIndex); + boundResources[flattenedIndex] = tex->getImage(); +} -void DescriptorSet::updateSamplerArray(uint32 binding, Array samplers) { assert(false && "TODO"); } +void DescriptorSet::updateTexture(uint32 binding, uint32 index, Gfx::PTextureCube texture) { + uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index); + PTextureBase tex = texture.cast(); + encoder->setTexture(tex->getImage(), flattenedIndex); + boundResources[flattenedIndex] = tex->getImage(); +} -void DescriptorSet::updateAccelerationStructure(uint32 binding, Gfx::PTopLevelAS as) { assert(false && "TODO"); } +void DescriptorSet::updateAccelerationStructure(uint32 binding, uint32 index, Gfx::PTopLevelAS as) { assert(false && "TODO"); } PipelineLayout::PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout) : Gfx::PipelineLayout(name, baseLayout), graphics(graphics) {} diff --git a/src/Engine/Graphics/Metal/Graphics.h b/src/Engine/Graphics/Metal/Graphics.h index 5a1c19c..fec5363 100644 --- a/src/Engine/Graphics/Metal/Graphics.h +++ b/src/Engine/Graphics/Metal/Graphics.h @@ -58,8 +58,13 @@ class Graphics : public Gfx::Graphics { 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; diff --git a/src/Engine/Graphics/Metal/Graphics.mm b/src/Engine/Graphics/Metal/Graphics.mm index d664204..70c025b 100644 --- a/src/Engine/Graphics/Metal/Graphics.mm +++ b/src/Engine/Graphics/Metal/Graphics.mm @@ -143,10 +143,23 @@ Gfx::OTimestampQuery Graphics::createTimestampQuery(uint64 numTimestamps, const return new TimestampQuery(this, name, numTimestamps); } +void Graphics::beginDebugRegion(const std::string& name) { + queue->getCommands()->getHandle()->pushDebugGroup(NS::String::string(name.c_str(), NS::ASCIIStringEncoding)); +} + +void Graphics::endDebugRegion() { + queue->getCommands()->getHandle()->popDebugGroup(); +} + void Graphics::resolveTexture(Gfx::PTexture, Gfx::PTexture) {} void Graphics::copyTexture(Gfx::PTexture, Gfx::PTexture) {} +void Graphics::copyBuffer(Gfx::PShaderBuffer src, Gfx::PShaderBuffer dst) +{ + // TODO blit commands +} + // Ray Tracing Gfx::OBottomLevelAS Graphics::createBottomLevelAccelerationStructure(const Gfx::BottomLevelASCreateInfo& createInfo) { return nullptr; } diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index df55da5..b875af3 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -99,7 +99,7 @@ void BasePass::beginFrame(const Component::Camera& cam) { transparentCulling = lightCullingLayout->allocateDescriptorSet(); //waterRenderer->beginFrame(); - terrainRenderer->beginFrame(viewParamsSet, cam); + //terrainRenderer->beginFrame(viewParamsSet, cam); // Debug vertices { @@ -249,7 +249,7 @@ void BasePass::render() { } //commands.add(waterRenderer->render(viewParamsSet)); - commands.add(terrainRenderer->render(viewParamsSet)); + //commands.add(terrainRenderer->render(viewParamsSet)); // Skybox { @@ -432,7 +432,7 @@ void BasePass::publishOutputs() { void BasePass::createRenderPass() { RenderPass::beginFrame(Component::Camera()); - terrainRenderer = new TerrainRenderer(graphics, scene, viewParamsLayout, viewParamsSet); + //terrainRenderer = new TerrainRenderer(graphics, scene, viewParamsLayout, viewParamsSet); cullingBuffer = resources->requestBuffer("CULLINGBUFFER"); timestamps = resources->requestTimestampQuery("TIMESTAMPS"); @@ -475,7 +475,7 @@ void BasePass::createRenderPass() { tLightGrid = resources->requestTexture("LIGHTCULLING_TLIGHTGRID"); //waterRenderer->setViewport(viewport, renderPass); - terrainRenderer->setViewport(viewport, renderPass); + //terrainRenderer->setViewport(viewport, renderPass); // Debug rendering { @@ -552,6 +552,7 @@ void BasePass::createRenderPass() { skyboxDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .uniformLength = sizeof(SkyboxData) }); skyboxDataLayout->create(); textureLayout = graphics->createDescriptorLayout("pSkyboxTextures"); diff --git a/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp b/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp index 1be4212..d88047a 100644 --- a/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp +++ b/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp @@ -105,9 +105,11 @@ void CachedDepthPass::render() { command->bindPipeline(pipeline); } command->bindDescriptor({viewParamsSet, vertexData->getVertexDataSet(), vertexData->getInstanceDataSet()}); - uint32 offset = 0; + VertexData::DrawCallOffsets offsets = { + .instanceOffset = 0, + }; command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT, 0, sizeof(VertexData::DrawCallOffsets), - &offset); + &offsets); if (graphics->supportMeshShading()) { command->drawMesh(vertexData->getNumInstances(), 1, 1); } else { diff --git a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp index 08fe3e3..c359987 100644 --- a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp +++ b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp @@ -248,6 +248,7 @@ void LightCullingPass::setupFrustums() { dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .uniformLength = sizeof(DispatchParams), }); dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, diff --git a/src/Engine/Graphics/RenderPass/RenderPass.cpp b/src/Engine/Graphics/RenderPass/RenderPass.cpp index d381343..bab5c46 100644 --- a/src/Engine/Graphics/RenderPass/RenderPass.cpp +++ b/src/Engine/Graphics/RenderPass/RenderPass.cpp @@ -7,6 +7,7 @@ RenderPass::RenderPass(Gfx::PGraphics graphics, PScene scene) : graphics(graphic viewParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .uniformLength = sizeof(ViewParameter), }); UniformBufferCreateInfo uniformInitializer = { .sourceData = @@ -129,4 +130,4 @@ void RenderPass::extract_planes_from_view_projection_matrix(const Matrix4 viewPr // Normalize all the planes for (uint32_t planeIdx = 0; planeIdx < 4; ++planeIdx) normalize_plane(frustum.planes[planeIdx]); -} \ No newline at end of file +} diff --git a/src/Engine/Graphics/RenderPass/TextPass.cpp b/src/Engine/Graphics/RenderPass/TextPass.cpp index 0466443..3f9aa67 100644 --- a/src/Engine/Graphics/RenderPass/TextPass.cpp +++ b/src/Engine/Graphics/RenderPass/TextPass.cpp @@ -105,6 +105,7 @@ void TextPass::createRenderPass() { generalLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .uniformLength = sizeof(Matrix4), }); generalLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, diff --git a/src/Engine/Graphics/RenderPass/UIPass.cpp b/src/Engine/Graphics/RenderPass/UIPass.cpp index 25afe32..98f9376 100644 --- a/src/Engine/Graphics/RenderPass/UIPass.cpp +++ b/src/Engine/Graphics/RenderPass/UIPass.cpp @@ -99,6 +99,7 @@ void UIPass::createRenderPass() { descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .uniformLength = sizeof(Matrix4), }); descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, @@ -107,6 +108,7 @@ void UIPass::createRenderPass() { descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 2, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .uniformLength = sizeof(uint32) }); descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 3, diff --git a/src/Engine/Graphics/RenderPass/WaterRenderer.cpp b/src/Engine/Graphics/RenderPass/WaterRenderer.cpp index 6e9e6d4..7edfc61 100644 --- a/src/Engine/Graphics/RenderPass/WaterRenderer.cpp +++ b/src/Engine/Graphics/RenderPass/WaterRenderer.cpp @@ -124,6 +124,7 @@ WaterRenderer::WaterRenderer(Gfx::PGraphics graphics, PScene scene, Gfx::PDescri computeLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .uniformLength = sizeof(MaterialParams), }); computeLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, diff --git a/src/Engine/Graphics/Shader.cpp b/src/Engine/Graphics/Shader.cpp index 9fc216f..7655105 100644 --- a/src/Engine/Graphics/Shader.cpp +++ b/src/Engine/Graphics/Shader.cpp @@ -67,7 +67,7 @@ void ShaderCompiler::compile() { layout->addDescriptorLayout(vd->getVertexDataLayout()); layout->addDescriptorLayout(vd->getInstanceDataLayout()); permutation.setMaterial(mat->getName()); - createShaders(permutation, std::move(layout)); + createShaders(permutation, std::move(layout), name); }); } } @@ -82,7 +82,7 @@ void ShaderCompiler::compile() { OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout); layout->addDescriptorLayout(vd->getVertexDataLayout()); layout->addDescriptorLayout(vd->getInstanceDataLayout()); - createShaders(permutation, std::move(layout)); + createShaders(permutation, std::move(layout), name); }); } } @@ -92,7 +92,7 @@ void ShaderCompiler::compile() { getThreadPool().runAndWait(std::move(work)); } -void ShaderCompiler::createShaders(ShaderPermutation permutation, Gfx::OPipelineLayout layout) { +void ShaderCompiler::createShaders(ShaderPermutation permutation, Gfx::OPipelineLayout layout, std::string debugName) { PermutationId perm = PermutationId(permutation); { std::scoped_lock lock(shadersLock); @@ -103,7 +103,7 @@ void ShaderCompiler::createShaders(ShaderPermutation permutation, Gfx::OPipeline collection.pipelineLayout = std::move(layout); ShaderCompilationInfo createInfo; - createInfo.name = fmt::format("Material {0}", permutation.materialName); + createInfo.name = fmt::format("{0} Material {1}", debugName, permutation.materialName); createInfo.rootSignature = collection.pipelineLayout; if (std::strlen(permutation.materialName) > 0) { createInfo.modules.add(permutation.materialName); diff --git a/src/Engine/Graphics/Shader.h b/src/Engine/Graphics/Shader.h index ed2f680..83c3efd 100644 --- a/src/Engine/Graphics/Shader.h +++ b/src/Engine/Graphics/Shader.h @@ -184,7 +184,7 @@ class ShaderCompiler { private: void compile(); - void createShaders(ShaderPermutation permutation, OPipelineLayout layout); + void createShaders(ShaderPermutation permutation, OPipelineLayout layout, std::string debugName); std::mutex shadersLock; Map shaders; Map materials; diff --git a/src/Engine/Graphics/slang-compile.cpp b/src/Engine/Graphics/slang-compile.cpp index 61de6ff..6bfb5bd 100644 --- a/src/Engine/Graphics/slang-compile.cpp +++ b/src/Engine/Graphics/slang-compile.cpp @@ -138,11 +138,11 @@ void Seele::beginCompilation(const ShaderCompilationInfo& info, SlangCompileTarg slang::ProgramLayout* signature = specializedComponent->getLayout(0, diagnostics.writeRef()); CHECK_DIAGNOSTICS(); - // std::cout << info.name << std::endl; + std::cout << info.name << std::endl; for (size_t i = 0; i < signature->getParameterCount(); ++i) { auto param = signature->getParameterByIndex(i); layout->addMapping(param->getName(), param->getBindingIndex()); - // std::cout << param->getName() << ": " << param->getBindingIndex() << std::endl; + std::cout << param->getName() << ": " << param->getBindingIndex() << std::endl; } // workaround diff --git a/src/Engine/Scene/LightEnvironment.cpp b/src/Engine/Scene/LightEnvironment.cpp index 5143aaa..c18936f 100644 --- a/src/Engine/Scene/LightEnvironment.cpp +++ b/src/Engine/Scene/LightEnvironment.cpp @@ -8,6 +8,7 @@ LightEnvironment::LightEnvironment(Gfx::PGraphics graphics) : graphics(graphics) layout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .uniformLength = sizeof(LightEnv) }); layout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, diff --git a/src/Engine/ThreadPool.h b/src/Engine/ThreadPool.h index 62bfc2e..7905745 100644 --- a/src/Engine/ThreadPool.h +++ b/src/Engine/ThreadPool.h @@ -7,7 +7,7 @@ namespace Seele { class ThreadPool { public: - ThreadPool(uint32 numWorkers = std::thread::hardware_concurrency() - 2); + ThreadPool(uint32 numWorkers = 1); ~ThreadPool(); void runAndWait(List> functions); void runAsync(std::function func); @@ -36,4 +36,4 @@ class ThreadPool { bool running = true; }; ThreadPool& getThreadPool(); -} // namespace Seele \ No newline at end of file +} // namespace Seele