diff --git a/res/shaders/Amp.dxil b/res/shaders/Amp.dxil new file mode 100755 index 0000000..229a32c Binary files /dev/null and b/res/shaders/Amp.dxil differ diff --git a/res/shaders/MeshletBasePass.slang b/res/shaders/MeshletBasePass.slang index d29ea41..cc57793 100644 --- a/res/shaders/MeshletBasePass.slang +++ b/res/shaders/MeshletBasePass.slang @@ -52,7 +52,7 @@ void taskMain( uint index; InterlockedAdd(head, 1, index); p.meshletId[index] = m; - p.instanceId[index] = groupID; + p.instanceId[index] = groupID + pScene.primitiveIndices[m]; } } } @@ -90,9 +90,9 @@ void meshMain( { uint p = min(i, m.primitiveCount - 1); { - uint local_idx0 = unpackPrimitiveIndices(m.primitiveOffset + (p * 3) + 0); - uint local_idx1 = unpackPrimitiveIndices(m.primitiveOffset + (p * 3) + 1); - uint local_idx2 = unpackPrimitiveIndices(m.primitiveOffset + (p * 3) + 2); + uint local_idx0 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 0]; + uint local_idx1 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 1]; + uint local_idx2 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 2]; indices[p] = uint3(local_idx0, local_idx1, local_idx2); } } diff --git a/res/shaders/lib/Scene.slang b/res/shaders/lib/Scene.slang index 6b24fe5..a1157ae 100644 --- a/res/shaders/lib/Scene.slang +++ b/res/shaders/lib/Scene.slang @@ -42,6 +42,5 @@ struct Scene StructuredBuffer primitiveIndices; StructuredBuffer vertexIndices; }; -layout(set=2) ParameterBlock pScene; diff --git a/res/shaders/test.json b/res/shaders/test.json new file mode 100644 index 0000000..a99e4b7 --- /dev/null +++ b/res/shaders/test.json @@ -0,0 +1,77 @@ +{ + "EntryPoint": "taskMain", + "FunctionConstants": [], + "NeedsFunctionConstants": false, + "Resources": [ + { + "abIndex": 0, + "slot": 0, + "type": "SRV" + }, + { + "abIndex": 1, + "slot": 1, + "type": "SRV" + }, + { + "abIndex": 2, + "slot": 2, + "type": "SRV" + }, + { + "abIndex": 3, + "slot": 3, + "type": "SRV" + }, + { + "abIndex": 4, + "slot": 0, + "type": "CBV" + } + ], + "ShaderID": "2266638404583382645", + "ShaderType": "Amplification", + "TopLevelArgumentBuffer": [ + { + "EltOffset": 0, + "Size": 24, + "Slot": 0, + "Space": 2, + "Type": "SRV" + }, + { + "EltOffset": 24, + "Size": 24, + "Slot": 1, + "Space": 2, + "Type": "SRV" + }, + { + "EltOffset": 48, + "Size": 24, + "Slot": 2, + "Space": 2, + "Type": "SRV" + }, + { + "EltOffset": 72, + "Size": 24, + "Slot": 3, + "Space": 2, + "Type": "SRV" + }, + { + "EltOffset": 96, + "Size": 24, + "Slot": 0, + "Space": 1, + "Type": "CBV" + } + ], + "max_payload_size_in_bytes": 4096, + "num_threads": [ + 128, + 1, + 1 + ] +} \ No newline at end of file diff --git a/res/shaders/test.metallib b/res/shaders/test.metallib new file mode 100644 index 0000000..71da910 Binary files /dev/null and b/res/shaders/test.metallib differ diff --git a/src/Engine/Graphics/Descriptor.cpp b/src/Engine/Graphics/Descriptor.cpp index d71fcff..1d32d32 100644 --- a/src/Engine/Graphics/Descriptor.cpp +++ b/src/Engine/Graphics/Descriptor.cpp @@ -43,9 +43,9 @@ DescriptorSet::DescriptorSet(PDescriptorLayout layout) : layout(layout) {} DescriptorSet::~DescriptorSet() {} -PipelineLayout::PipelineLayout() {} +PipelineLayout::PipelineLayout(const std::string& name) : name(name) {} -PipelineLayout::PipelineLayout(PPipelineLayout baseLayout) { +PipelineLayout::PipelineLayout(const std::string& name, PPipelineLayout baseLayout) : name(name){ if (baseLayout != nullptr) { descriptorSetLayouts = baseLayout->descriptorSetLayouts; pushConstants = baseLayout->pushConstants; diff --git a/src/Engine/Graphics/Descriptor.h b/src/Engine/Graphics/Descriptor.h index bbd2d8a..61a356c 100644 --- a/src/Engine/Graphics/Descriptor.h +++ b/src/Engine/Graphics/Descriptor.h @@ -75,8 +75,8 @@ DEFINE_REF(DescriptorSet) class PipelineLayout { public: - PipelineLayout(); - PipelineLayout(PPipelineLayout baseLayout); + PipelineLayout(const std::string& name); + PipelineLayout(const std::string& name, PPipelineLayout baseLayout); virtual ~PipelineLayout(); virtual void create() = 0; void addDescriptorLayout(PDescriptorLayout layout); @@ -85,12 +85,14 @@ public: constexpr const Map& getLayouts() const { return descriptorSetLayouts; } constexpr uint32 findParameter(const std::string& name) const { return parameterMapping[name]; } void addMapping(Map mapping); - + constexpr std::string getName() const {return name;}; + protected: uint32 layoutHash = 0; Map descriptorSetLayouts; Map parameterMapping; Array pushConstants; + std::string name; }; DEFINE_REF(PipelineLayout) } // namespace Gfx diff --git a/src/Engine/Graphics/Graphics.h b/src/Engine/Graphics/Graphics.h index ab45350..1e96041 100644 --- a/src/Engine/Graphics/Graphics.h +++ b/src/Engine/Graphics/Graphics.h @@ -81,7 +81,7 @@ public: virtual OSampler createSampler(const SamplerCreateInfo& createInfo) = 0; virtual ODescriptorLayout createDescriptorLayout(const std::string& name = "") = 0; - virtual OPipelineLayout createPipelineLayout(PPipelineLayout baseLayout = nullptr) = 0; + virtual OPipelineLayout createPipelineLayout(const std::string& name = "", PPipelineLayout baseLayout = nullptr) = 0; virtual OVertexInput createVertexInput(VertexInputStateCreateInfo createInfo) = 0; @@ -96,4 +96,4 @@ protected: }; DEFINE_REF(Graphics) } // namespace Gfx -} // namespace Seele \ No newline at end of file +} // namespace Seele diff --git a/src/Engine/Graphics/Initializer.h b/src/Engine/Graphics/Initializer.h index fc7000c..fbf8601 100644 --- a/src/Engine/Graphics/Initializer.h +++ b/src/Engine/Graphics/Initializer.h @@ -94,26 +94,26 @@ struct VertexBufferCreateInfo // bytes per vertex uint32 vertexSize = 0; uint32 numVertices = 0; - std::string name; + std::string name = ""; }; struct IndexBufferCreateInfo { DataSource sourceData = DataSource(); Gfx::SeIndexType indexType = Gfx::SeIndexType::SE_INDEX_TYPE_UINT16; - std::string name; + std::string name = ""; }; struct UniformBufferCreateInfo { DataSource sourceData = DataSource(); uint8 dynamic = 0; - std::string name; + std::string name = ""; }; struct ShaderBufferCreateInfo { DataSource sourceData = DataSource(); uint64 numElements = 1; uint8 dynamic = 0; - std::string name; + std::string name = ""; }; DECLARE_NAME_REF(Gfx, PipelineLayout) struct ShaderCreateInfo diff --git a/src/Engine/Graphics/Metal/Buffer.h b/src/Engine/Graphics/Metal/Buffer.h index baf1d64..d0bf020 100644 --- a/src/Engine/Graphics/Metal/Buffer.h +++ b/src/Engine/Graphics/Metal/Buffer.h @@ -9,7 +9,7 @@ namespace Metal { DECLARE_REF(Graphics) class Buffer { public: - Buffer(PGraphics graphics, uint64 size, void *data, bool dynamic); + 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; } @@ -83,4 +83,4 @@ protected: }; DEFINE_REF(ShaderBuffer) } // namespace Metal -} // namespace Seele \ No newline at end of file +} // namespace Seele diff --git a/src/Engine/Graphics/Metal/Buffer.mm b/src/Engine/Graphics/Metal/Buffer.mm index afe9907..8da8824 100644 --- a/src/Engine/Graphics/Metal/Buffer.mm +++ b/src/Engine/Graphics/Metal/Buffer.mm @@ -3,10 +3,12 @@ #include "Graphics/Buffer.h" #include "Graphics/Enums.h" #include "Graphics/Initializer.h" +#include + using namespace Seele; using namespace Seele::Metal; -Buffer::Buffer(PGraphics graphics, uint64 size, void* data, bool dynamic) : graphics(graphics), size(size) { +Buffer::Buffer(PGraphics graphics, uint64 size, void* data, bool dynamic, const std::string& name) : graphics(graphics), size(size) { if (dynamic) { numBuffers = Gfx::numFramesBuffered; } else { @@ -14,10 +16,11 @@ Buffer::Buffer(PGraphics graphics, uint64 size, void* data, bool dynamic) : grap } for (size_t i = 0; i < numBuffers; ++i) { if (data != nullptr) { - buffers[i] = graphics->getDevice()->newBuffer(data, size, MTL::ResourceOptionCPUCacheModeDefault); + buffers[i] = graphics->getDevice()->newBuffer(data, size, MTL::StorageModeShared); } else { - buffers[i] = graphics->getDevice()->newBuffer(size, MTL::ResourceOptionCPUCacheModeDefault); + buffers[i] = graphics->getDevice()->newBuffer(size, MTL::StorageModeShared); } + buffers[i]->setLabel(NS::String::string(name.c_str(), NS::ASCIIStringEncoding)); } } @@ -31,12 +34,12 @@ void* Buffer::map(bool) { return getHandle()->contents(); } void* Buffer::mapRegion(uint64 regionOffset, uint64, bool) { return (char*)getHandle()->contents() + regionOffset; } -void unmap() {} +void Buffer::unmap() {} VertexBuffer::VertexBuffer(PGraphics graphics, const VertexBufferCreateInfo& createInfo) : Gfx::VertexBuffer(graphics->getFamilyMapping(), createInfo.numVertices, createInfo.vertexSize, createInfo.sourceData.owner), - Seele::Metal::Buffer(graphics, createInfo.sourceData.size, createInfo.sourceData.data, false) {} + Seele::Metal::Buffer(graphics, createInfo.sourceData.size, createInfo.sourceData.data, false, createInfo.name) {} VertexBuffer::~VertexBuffer() {} @@ -61,7 +64,7 @@ void VertexBuffer::executePipelineBarrier(Gfx::SeAccessFlags srcAccess, Gfx::SeP IndexBuffer::IndexBuffer(PGraphics graphics, const IndexBufferCreateInfo& createInfo) : Gfx::IndexBuffer(graphics->getFamilyMapping(), createInfo.sourceData.size, createInfo.indexType, createInfo.sourceData.owner), - Seele::Metal::Buffer(graphics, createInfo.sourceData.size, createInfo.sourceData.data, false) {} + Seele::Metal::Buffer(graphics, createInfo.sourceData.size, createInfo.sourceData.data, false, createInfo.name) {} IndexBuffer::~IndexBuffer() {} @@ -77,7 +80,7 @@ void IndexBuffer::executePipelineBarrier(Gfx::SeAccessFlags srcAccess, Gfx::SePi UniformBuffer::UniformBuffer(PGraphics graphics, const UniformBufferCreateInfo& createInfo) : Gfx::UniformBuffer(graphics->getFamilyMapping(), createInfo.sourceData), - Seele::Metal::Buffer(graphics, createInfo.sourceData.size, createInfo.sourceData.data, createInfo.dynamic) {} + Seele::Metal::Buffer(graphics, createInfo.sourceData.size, createInfo.sourceData.data, createInfo.dynamic, createInfo.name) {} UniformBuffer::~UniformBuffer() {} @@ -94,7 +97,7 @@ void UniformBuffer::executePipelineBarrier(Gfx::SeAccessFlags srcAccess, Gfx::Se ShaderBuffer::ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo& createInfo) : Gfx::ShaderBuffer(graphics->getFamilyMapping(), createInfo.numElements, createInfo.sourceData), - Seele::Metal::Buffer(graphics, createInfo.sourceData.size, createInfo.sourceData.data, createInfo.dynamic) {} + Seele::Metal::Buffer(graphics, createInfo.sourceData.size, createInfo.sourceData.data, createInfo.dynamic, createInfo.name) {} ShaderBuffer::~ShaderBuffer() {} @@ -108,4 +111,4 @@ void ShaderBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) { currentOwn void ShaderBuffer::executePipelineBarrier(Gfx::SeAccessFlags srcAccess, Gfx::SePipelineStageFlags srcStage, - Gfx::SeAccessFlags dstAccess, Gfx::SePipelineStageFlags dstStage) {} \ No newline at end of file + Gfx::SeAccessFlags dstAccess, Gfx::SePipelineStageFlags dstStage) {} diff --git a/src/Engine/Graphics/Metal/Command.mm b/src/Engine/Graphics/Metal/Command.mm index 14fd9e5..8d5e910 100644 --- a/src/Engine/Graphics/Metal/Command.mm +++ b/src/Engine/Graphics/Metal/Command.mm @@ -11,6 +11,7 @@ #include "Pipeline.h" #include "Resources.h" #include "Window.h" +#include using namespace Seele; using namespace Seele::Metal; @@ -74,14 +75,46 @@ void RenderCommand::setViewport(Gfx::PViewport viewport) { void RenderCommand::bindPipeline(Gfx::PGraphicsPipeline pipeline) { boundPipeline = pipeline.cast(); encoder->setRenderPipelineState(boundPipeline->getHandle()); + uint64 argBufferSize = 0; + for (auto [_, layout] : boundPipeline->getPipelineLayout()->getLayouts()) { + argBufferSize += 3 * sizeof(uint64) * layout->getBindings().size(); + } + argumentBuffer = boundPipeline->graphics->getDevice()->newBuffer(argBufferSize, MTL::ResourceStorageModeShared); + argumentBuffer->setLabel( + NS::String::string(boundPipeline->getPipelineLayout()->getName().c_str(), NS::ASCIIStringEncoding)); } void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet) { - uint32 parameterIndex = boundPipeline->getPipelineLayout()->findParameter(descriptorSet->getLayout()->getName()); - encoder->setVertexBuffer(descriptorSet.cast()->getBuffer(), 0, parameterIndex); - encoder->setFragmentBuffer(descriptorSet.cast()->getBuffer(), 0, parameterIndex); - encoder->setMeshBuffer(descriptorSet.cast()->getBuffer(), 0, parameterIndex); - encoder->setObjectBuffer(descriptorSet.cast()->getBuffer(), 0, parameterIndex); + auto metalSet = descriptorSet.cast(); + uint32 parameterIndex = boundPipeline->getPipelineLayout()->findParameter(descriptorSet->getLayout()->getName()); + uint64* topLevelTable = (uint64*)argumentBuffer->contents(); + topLevelTable[parameterIndex] = metalSet->getBuffer()->gpuAddress(); + auto bindings = metalSet->getLayout()->getBindings(); + encoder->useResource(metalSet->getBuffer(), MTL::ResourceUsageRead); + for (size_t i = 0; i < bindings.size(); ++i) { + auto binding = bindings[i]; + if (binding.descriptorType == Gfx::SE_DESCRIPTOR_TYPE_SAMPLER) { + continue; + } + MTL::ResourceUsage usage; + switch (binding.access) { + case Gfx::SE_DESCRIPTOR_ACCESS_READ_ONLY_BIT: + if (binding.descriptorType == Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE) { + usage = MTL::ResourceUsageSample; + break; + } else { + usage = MTL::ResourceUsageRead; + break; + } + case Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT: + usage = MTL::ResourceUsageRead | MTL::ResourceUsageWrite; + break; + case Gfx::SE_DESCRIPTOR_ACCESS_WRITE_ONLY_BIT: + usage = MTL::ResourceUsageWrite; + break; + } + encoder->useResource(metalSet->getBoundResources()[i], usage); + } } void RenderCommand::bindDescriptor(const Array& descriptorSets) { @@ -122,6 +155,10 @@ void RenderCommand::drawIndexed(uint32 indexCount, uint32 instanceCount, int32 f void RenderCommand::drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) { // TODO: + std::cout << "Draw" << std::endl; + encoder->setFragmentBuffer(argumentBuffer, 0, 2); + encoder->setMeshBuffer(argumentBuffer, 0, 2); + encoder->setObjectBuffer(argumentBuffer, 0, 2); encoder->drawMeshThreadgroups(MTL::Size(groupX, groupY, groupZ), MTL::Size(128, 1, 1), MTL::Size(32, 1, 1)); } @@ -136,46 +173,46 @@ void ComputeCommand::end() { } void ComputeCommand::bindPipeline(Gfx::PComputePipeline pipeline) { - boundPipeline = pipeline.cast(); - encoder->setComputePipelineState(boundPipeline->getHandle()); - argumentBuffer = boundPipeline->graphics->getDevice()->newBuffer(sizeof(uint64) * boundPipeline->getPipelineLayout()->getLayouts().size(), MTL::ResourceOptionCPUCacheModeDefault); + boundPipeline = pipeline.cast(); + encoder->setComputePipelineState(boundPipeline->getHandle()); + argumentBuffer = boundPipeline->graphics->getDevice()->newBuffer( + sizeof(uint64) * (boundPipeline->getPipelineLayout()->getLayouts().size() + 1), MTL::ResourceStorageModeShared); + argumentBuffer->setLabel( + NS::String::string(pipeline->getPipelineLayout()->getName().c_str(), NS::ASCIIStringEncoding)); } void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet set) { auto metalSet = set.cast(); metalSet->bind(); - uint32 parameterIndex = boundPipeline->getPipelineLayout()->findParameter(set->getLayout()->getName()); - uint64* topLevelTable = (uint64*)argumentBuffer->contents(); - topLevelTable[parameterIndex] = metalSet->getBuffer()->gpuAddress(); - auto bindings =metalSet->getLayout()->getBindings(); - for(size_t i = 0; i < bindings.size(); ++i) - { - auto binding = bindings[i]; - if(binding.descriptorType == Gfx::SE_DESCRIPTOR_TYPE_SAMPLER) - { - continue; - } - MTL::ResourceUsage usage; - switch(binding.access) { - case Gfx::SE_DESCRIPTOR_ACCESS_READ_ONLY_BIT: - if(binding.descriptorType == Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE) - { - usage = MTL::ResourceUsageSample; - break; - }else - { - usage = MTL::ResourceUsageRead; - break; - } - case Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT: - usage = MTL::ResourceUsageRead | MTL::ResourceUsageWrite; - break; - case Gfx::SE_DESCRIPTOR_ACCESS_WRITE_ONLY_BIT: - usage = MTL::ResourceUsageWrite; - break; - } - encoder->useResource(metalSet->getBoundResources()[i], usage); + uint32 parameterIndex = boundPipeline->getPipelineLayout()->findParameter(set->getLayout()->getName()); + uint64* topLevelTable = (uint64*)argumentBuffer->contents(); + topLevelTable[parameterIndex] = metalSet->getBuffer()->gpuAddress(); + auto bindings = metalSet->getLayout()->getBindings(); + encoder->useResource(metalSet->getBuffer(), MTL::ResourceUsageRead); + for (size_t i = 0; i < bindings.size(); ++i) { + auto binding = bindings[i]; + if (binding.descriptorType == Gfx::SE_DESCRIPTOR_TYPE_SAMPLER) { + continue; } + MTL::ResourceUsage usage; + switch (binding.access) { + case Gfx::SE_DESCRIPTOR_ACCESS_READ_ONLY_BIT: + if (binding.descriptorType == Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE) { + usage = MTL::ResourceUsageSample; + break; + } else { + usage = MTL::ResourceUsageRead; + break; + } + case Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT: + usage = MTL::ResourceUsageRead | MTL::ResourceUsageWrite; + break; + case Gfx::SE_DESCRIPTOR_ACCESS_WRITE_ONLY_BIT: + usage = MTL::ResourceUsageWrite; + break; + } + encoder->useResource(metalSet->getBoundResources()[i], usage); + } } void ComputeCommand::bindDescriptor(const Array& sets) { @@ -191,15 +228,15 @@ void ComputeCommand::pushConstants(Gfx::PPipelineLayout, Gfx::SeShaderStageFlags void ComputeCommand::dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) { // TODO - encoder->setBuffer(argumentBuffer, 0, 2); + encoder->setBuffer(argumentBuffer, 0, 2); encoder->dispatchThreadgroups(MTL::Size(threadX, threadY, threadZ), MTL::Size(32, 32, 1)); } CommandQueue::CommandQueue(PGraphics graphics) : graphics(graphics) { queue = graphics->getDevice()->newCommandQueue(); MTL::CommandBufferDescriptor* descriptor = MTL::CommandBufferDescriptor::alloc()->init(); - descriptor->setErrorOptions(MTL::CommandBufferErrorOptionEncoderExecutionStatus); activeCommand = new Command(graphics, queue->commandBuffer(descriptor)); + descriptor->release(); } CommandQueue::~CommandQueue() { queue->release(); } diff --git a/src/Engine/Graphics/Metal/Descriptor.h b/src/Engine/Graphics/Metal/Descriptor.h index d43055b..1edd5e3 100644 --- a/src/Engine/Graphics/Metal/Descriptor.h +++ b/src/Engine/Graphics/Metal/Descriptor.h @@ -13,11 +13,8 @@ public: virtual ~DescriptorLayout(); virtual void create() override; - NS::Array* getArguments() const { return arguments; } - private: PGraphics graphics; - NS::Array* arguments; }; DEFINE_REF(DescriptorLayout) @@ -28,7 +25,6 @@ public: virtual ~DescriptorPool(); virtual Gfx::PDescriptorSet allocateDescriptorSet() override; virtual void reset() override; - constexpr NS::Array* getArguments() const { return layout->getArguments(); } constexpr PDescriptorLayout getLayout() const { return layout; } private: @@ -63,7 +59,7 @@ private: PGraphics graphics; PDescriptorPool owner; MTL::Buffer* buffer = nullptr; - MTL::ArgumentEncoder* encoder; + uint64* argumentBuffer = nullptr; Array boundResources; uint32 bindCount; bool currentlyInUse; @@ -72,10 +68,10 @@ DEFINE_REF(DescriptorSet) class PipelineLayout : public Gfx::PipelineLayout { public: - PipelineLayout(PGraphics graphics, Gfx::PPipelineLayout baseLayout); + PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout); virtual ~PipelineLayout(); virtual void create() override; - + Array> private: PGraphics graphics; }; diff --git a/src/Engine/Graphics/Metal/Descriptor.mm b/src/Engine/Graphics/Metal/Descriptor.mm index 3226d1d..d59a655 100644 --- a/src/Engine/Graphics/Metal/Descriptor.mm +++ b/src/Engine/Graphics/Metal/Descriptor.mm @@ -15,74 +15,11 @@ using namespace Seele; using namespace Seele::Metal; DescriptorLayout::DescriptorLayout(PGraphics graphics, const std::string& name) - : Gfx::DescriptorLayout(name), graphics(graphics), arguments(nullptr) {} + : Gfx::DescriptorLayout(name), graphics(graphics) {} DescriptorLayout::~DescriptorLayout() {} void DescriptorLayout::create() { - if (arguments != nullptr) { - return; - } - Array descriptors; - for (size_t i = 0; i < descriptorBindings.size(); ++i) { - const auto& binding = descriptorBindings[i]; - auto desc = MTL::ArgumentDescriptor::alloc()->init(); - desc->setAccess(cast(binding.access)); - desc->setArrayLength(binding.descriptorCount); - MTL::DataType dataType; - switch (binding.descriptorType) { - case Gfx::SE_DESCRIPTOR_TYPE_SAMPLER: - case Gfx::SE_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: - case Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE: - case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE: - case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: - case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: - 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_EXT: - dataType = MTL::DataTypePointer; - break; - case Gfx::SE_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV: - dataType = MTL::DataTypePrimitiveAccelerationStructure; - break; - default: - throw std::logic_error("Nooo"); - } - desc->setDataType(dataType); - desc->setIndex(i); - if (dataType == MTL::DataTypeTexture) { - switch (binding.textureType) { - case Gfx::SE_IMAGE_VIEW_TYPE_1D: - desc->setTextureType(MTL::TextureType1D); - break; - case Gfx::SE_IMAGE_VIEW_TYPE_2D: - desc->setTextureType(MTL::TextureType2D); - break; - case Gfx::SE_IMAGE_VIEW_TYPE_3D: - desc->setTextureType(MTL::TextureType3D); - break; - case Gfx::SE_IMAGE_VIEW_TYPE_CUBE: - desc->setTextureType(MTL::TextureTypeCube); - break; - case Gfx::SE_IMAGE_VIEW_TYPE_1D_ARRAY: - desc->setTextureType(MTL::TextureType1DArray); - break; - case Gfx::SE_IMAGE_VIEW_TYPE_2D_ARRAY: - desc->setTextureType(MTL::TextureType2DArray); - break; - case Gfx::SE_IMAGE_VIEW_TYPE_CUBE_ARRAY: - desc->setTextureType(MTL::TextureTypeCubeArray); - break; - } - } - descriptors.add(desc); - } - arguments = NS::Array::array(descriptors.data(), descriptors.size()); pool = new DescriptorPool(graphics, this); hash = CRC::Calculate(descriptorBindings.data(), sizeof(Gfx::DescriptorBinding) * descriptorBindings.size(), CRC::CRC_32()); @@ -116,63 +53,72 @@ void DescriptorPool::reset() { DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner) : Gfx::DescriptorSet(owner->getLayout()), graphics(graphics), owner(owner), bindCount(0), currentlyInUse(false) { - if (owner->getArguments()->count() > 0) { - boundResources.resize(owner->getArguments()->count()); - encoder = graphics->getDevice()->newArgumentEncoder(owner->getArguments()); - buffer = graphics->getDevice()->newBuffer(encoder->encodedLength(), MTL::ResourceOptionCPUCacheModeDefault); - encoder->setArgumentBuffer(buffer, 0); - } else - { - buffer = graphics->getDevice()->newBuffer(8, MTL::ResourceOptionCPUCacheModeDefault); - } + 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)); } -DescriptorSet::~DescriptorSet() {} +DescriptorSet::~DescriptorSet() {buffer->release();} void DescriptorSet::writeChanges() {} void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBuffer) { PUniformBuffer metalBuffer = uniformBuffer.cast(); - encoder->setBuffer(metalBuffer->getHandle(), 0, binding); + 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) { PShaderBuffer metalBuffer = uniformBuffer.cast(); - encoder->setBuffer(metalBuffer->getHandle(), 0, binding); + 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(); - encoder->setSamplerState(sampler->getHandle(), binding); + 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) { - encoder->setTexture(base->getTexture(), binding); - + 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{ - encoder->setBuffer(base->getTexture()->buffer(), 0, binding); + 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) { - PTextureBase metalTexture = t.cast(); - encoder->setTexture(metalTexture->getTexture(), binding); - boundResources[binding++] = metalTexture->getTexture(); + updateTexture(binding++, t); } } -PipelineLayout::PipelineLayout(PGraphics graphics, Gfx::PPipelineLayout baseLayout) - : Gfx::PipelineLayout(baseLayout), graphics(graphics) {} +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) { - set->create(); + assert(set->getHash() != 0); uint32 setHash = set->getHash(); layoutHash = CRC::Calculate(&setHash, sizeof(uint32), CRC::CRC_32(), layoutHash); } diff --git a/src/Engine/Graphics/Metal/Graphics.h b/src/Engine/Graphics/Metal/Graphics.h index fbf6d10..aaa30ad 100644 --- a/src/Engine/Graphics/Metal/Graphics.h +++ b/src/Engine/Graphics/Metal/Graphics.h @@ -49,7 +49,7 @@ public: virtual Gfx::OSampler createSampler(const SamplerCreateInfo& createInfo) override; virtual Gfx::ODescriptorLayout createDescriptorLayout(const std::string& name = "") override; - virtual Gfx::OPipelineLayout createPipelineLayout(Gfx::PPipelineLayout baseLayout = nullptr) override; + virtual Gfx::OPipelineLayout createPipelineLayout(const std::string& name = "", Gfx::PPipelineLayout baseLayout = nullptr) override; virtual Gfx::OVertexInput createVertexInput(VertexInputStateCreateInfo createInfo) override; diff --git a/src/Engine/Graphics/Metal/Graphics.mm b/src/Engine/Graphics/Metal/Graphics.mm index 6ad4d08..c095d7b 100644 --- a/src/Engine/Graphics/Metal/Graphics.mm +++ b/src/Engine/Graphics/Metal/Graphics.mm @@ -180,9 +180,9 @@ Gfx::ODescriptorLayout Graphics::createDescriptorLayout(const std::string& name) return new DescriptorLayout(this, name); } -Gfx::OPipelineLayout Graphics::createPipelineLayout(Gfx::PPipelineLayout baseLayout) +Gfx::OPipelineLayout Graphics::createPipelineLayout(const std::string& name, Gfx::PPipelineLayout baseLayout) { - return new PipelineLayout(this, baseLayout); + return new PipelineLayout(this, name, baseLayout); } Gfx::OVertexInput Graphics::createVertexInput(VertexInputStateCreateInfo createInfo) diff --git a/src/Engine/Graphics/Metal/Pipeline.h b/src/Engine/Graphics/Metal/Pipeline.h index c6b16ae..94daa92 100644 --- a/src/Engine/Graphics/Metal/Pipeline.h +++ b/src/Engine/Graphics/Metal/Pipeline.h @@ -19,10 +19,10 @@ public: virtual ~GraphicsPipeline(); constexpr MTL::RenderPipelineState* getHandle() const { return state; } constexpr MTL::PrimitiveType getPrimitive() const { return primitiveType; } -private: PGraphics graphics; MTL::RenderPipelineState* state; MTL::PrimitiveType primitiveType; +private: }; DEFINE_REF(GraphicsPipeline) class ComputePipeline : public Gfx::ComputePipeline { diff --git a/src/Engine/Graphics/Metal/Shader.mm b/src/Engine/Graphics/Metal/Shader.mm index 9251524..2552fce 100644 --- a/src/Engine/Graphics/Metal/Shader.mm +++ b/src/Engine/Graphics/Metal/Shader.mm @@ -25,15 +25,15 @@ Shader::~Shader() { } void Shader::create(const ShaderCreateInfo& createInfo) { - std::cout << "Compiling " << createInfo.name << std::endl; - Map test; - Slang::ComPtr kernelBlob = generateShader(createInfo, SLANG_DXIL, test); + std::cout << "Compiling " << createInfo.name << std::endl; + Map paramMapping; + Slang::ComPtr kernelBlob = generateShader(createInfo, SLANG_DXIL, paramMapping); hash = CRC::Calculate(kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), CRC::CRC_32()); IRCompiler* pCompiler = IRCompilerCreate(); IRCompilerSetMinimumGPUFamily(pCompiler, IRGPUFamilyMetal3); IRCompilerIgnoreRootSignature(pCompiler, true); IRCompilerSetEntryPointName(pCompiler, "main"); - IRCompilerSetValidationFlags(pCompiler, IRCompilerValidationFlagAll); + IRCompilerSetValidationFlags(pCompiler, IRCompilerValidationFlagAll); IRObject* pDXIL = IRObjectCreateFromDXIL((const uint8*)kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), IRBytecodeOwnershipNone); @@ -100,8 +100,8 @@ void Shader::create(const ShaderCreateInfo& createInfo) { descriptor.desc_1_1.pParameters = parameters.data(); IRRootSignature* rootSignature = IRRootSignatureCreateFromDescriptor(&descriptor, &signatureError); assert(rootSignature); - IRCompilerSetGlobalRootSignature(pCompiler, rootSignature); - // Compile DXIL to Metal IR: + // IRCompilerSetGlobalRootSignature(pCompiler, rootSignature); + // Compile DXIL to Metal IR: IRError* pError = nullptr; IRObject* pOutIR = IRCompilerAllocCompileAndLink(pCompiler, NULL, pDXIL, &pError); @@ -131,7 +131,7 @@ void Shader::create(const ShaderCreateInfo& createInfo) { IRMetalLibBinary* pMetallib = IRMetalLibBinaryCreate(); IRObjectGetMetalLibBinary(pOutIR, irStage, pMetallib); dispatch_data_t data = IRMetalLibGetBytecodeData(pMetallib); - + IRShaderReflection* reflection = IRShaderReflectionCreate(); IRObjectGetReflection(pOutIR, irStage, reflection); std::cout << IRShaderReflectionAllocStringAndSerialize(reflection) << std::endl; diff --git a/src/Engine/Graphics/Metal/Texture.mm b/src/Engine/Graphics/Metal/Texture.mm index c80a261..a5cbac1 100644 --- a/src/Engine/Graphics/Metal/Texture.mm +++ b/src/Engine/Graphics/Metal/Texture.mm @@ -21,7 +21,7 @@ TextureBase::TextureBase(PGraphics graphics, MTL::TextureType type, ownsImage(existingImage == nullptr) { if (existingImage == nullptr) { MTL::TextureUsage mtlUsage = 0; - if(usage & Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) + if(usage & Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT || usage & Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) { mtlUsage |= MTL::TextureUsageRenderTarget; } @@ -29,6 +29,10 @@ TextureBase::TextureBase(PGraphics graphics, MTL::TextureType type, { mtlUsage |= MTL::TextureUsageShaderRead; } + if(usage & Gfx::SE_IMAGE_USAGE_STORAGE_BIT) + { + mtlUsage |= MTL::TextureUsageShaderWrite; + } MTL::TextureDescriptor *descriptor = MTL::TextureDescriptor::alloc()->init(); descriptor->setPixelFormat(cast(format)); diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index 0f9c1c2..86ce23e 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -157,7 +157,7 @@ void BasePass::endFrame() void BasePass::publishOutputs() { - basePassLayout = graphics->createPipelineLayout(); + basePassLayout = graphics->createPipelineLayout("BasePassLayout"); basePassLayout->addDescriptorLayout(viewParamsLayout); basePassLayout->addDescriptorLayout(scene->getLightEnvironment()->getDescriptorLayout()); diff --git a/src/Engine/Graphics/RenderPass/DebugPass.cpp b/src/Engine/Graphics/RenderPass/DebugPass.cpp index 627bfbe..83d7ad2 100644 --- a/src/Engine/Graphics/RenderPass/DebugPass.cpp +++ b/src/Engine/Graphics/RenderPass/DebugPass.cpp @@ -119,7 +119,7 @@ void DebugPass::createRenderPass() }; renderPass = graphics->createRenderPass(std::move(layout), dependency, viewport); - pipelineLayout = graphics->createPipelineLayout(); + pipelineLayout = graphics->createPipelineLayout("DebugPassLayout"); pipelineLayout->addDescriptorLayout(viewParamsLayout); pipelineLayout->create(); diff --git a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp index 1ab6bba..1aa25e6 100644 --- a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp +++ b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp @@ -16,7 +16,7 @@ DepthPrepass::DepthPrepass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics, scene) , descriptorSets(3) { - depthPrepassLayout = graphics->createPipelineLayout(); + depthPrepassLayout = graphics->createPipelineLayout("DepthPrepassLayout"); depthPrepassLayout->addDescriptorLayout(viewParamsLayout); if (graphics->supportMeshShading()) { diff --git a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp index 2c3de74..f264efc 100644 --- a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp +++ b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp @@ -114,9 +114,11 @@ void LightCullingPass::publishOutputs() //t_lightGrid cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 6, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT}); + cullingDescriptorLayout->create(); + lightEnv = scene->getLightEnvironment(); - cullingLayout = graphics->createPipelineLayout(); + cullingLayout = graphics->createPipelineLayout("CullingLayout"); cullingLayout->addDescriptorLayout(viewParamsLayout); cullingLayout->addDescriptorLayout(dispatchParamsLayout); cullingLayout->addDescriptorLayout(cullingDescriptorLayout); @@ -213,7 +215,8 @@ void LightCullingPass::setupFrustums() dispatchParamsLayout = graphics->createDescriptorLayout("pDispatchParams"); dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, }); dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 1, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_WRITE_ONLY_BIT }); - frustumLayout = graphics->createPipelineLayout(); + dispatchParamsLayout->create(); + frustumLayout = graphics->createPipelineLayout("FrustumLayout"); frustumLayout->addDescriptorLayout(viewParamsLayout); frustumLayout->addDescriptorLayout(dispatchParamsLayout); Map mapping; diff --git a/src/Engine/Graphics/RenderPass/RenderPass.cpp b/src/Engine/Graphics/RenderPass/RenderPass.cpp index 283cb7b..696d0a4 100644 --- a/src/Engine/Graphics/RenderPass/RenderPass.cpp +++ b/src/Engine/Graphics/RenderPass/RenderPass.cpp @@ -15,6 +15,7 @@ RenderPass::RenderPass(Gfx::PGraphics graphics, PScene scene) .data = (uint8*)&viewParams, }, .dynamic = true, + .name = "viewParamsBuffer", }; viewParamsBuffer = graphics->createUniformBuffer(uniformInitializer); viewParamsLayout->create(); diff --git a/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp b/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp index db4b70c..b32a49b 100644 --- a/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp +++ b/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp @@ -122,7 +122,7 @@ void SkyboxRenderPass::createRenderPass() createInfo.entryPoint = "fragmentMain"; fragmentShader = graphics->createFragmentShader(createInfo); - pipelineLayout = graphics->createPipelineLayout(); + pipelineLayout = graphics->createPipelineLayout("SkyboxLayout"); pipelineLayout->addDescriptorLayout(viewParamsLayout); pipelineLayout->addDescriptorLayout(skyboxDataLayout); pipelineLayout->addDescriptorLayout(textureLayout); diff --git a/src/Engine/Graphics/Shader.cpp b/src/Engine/Graphics/Shader.cpp index aa0838f..da5b592 100644 --- a/src/Engine/Graphics/Shader.cpp +++ b/src/Engine/Graphics/Shader.cpp @@ -77,7 +77,7 @@ void ShaderCompiler::compile() { for (const auto& [matName, mat] : materials) { - OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout); + OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout); layout->addDescriptorLayout(vd->getVertexDataLayout()); layout->addDescriptorLayout(vd->getInstanceDataLayout()); layout->addDescriptorLayout(mat->getDescriptorLayout()); @@ -95,7 +95,7 @@ void ShaderCompiler::compile() } else { - OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout); + OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout); layout->addDescriptorLayout(vd->getVertexDataLayout()); layout->addDescriptorLayout(vd->getInstanceDataLayout()); Map mapping; diff --git a/src/Engine/Graphics/StaticMeshVertexData.cpp b/src/Engine/Graphics/StaticMeshVertexData.cpp index 8a22654..f1d3f4f 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.cpp +++ b/src/Engine/Graphics/StaticMeshVertexData.cpp @@ -162,13 +162,19 @@ void StaticMeshVertexData::resizeBuffers() }, .numElements = verticesAllocated * 3, .dynamic = true, + .name = "Positions", }; positions = graphics->createShaderBuffer(createInfo); + createInfo.name = "Normals"; normals = graphics->createShaderBuffer(createInfo); + createInfo.name = "Tangents"; tangents = graphics->createShaderBuffer(createInfo); + createInfo.name = "BiTangents"; biTangents = graphics->createShaderBuffer(createInfo); + createInfo.name = "Colors"; colors = graphics->createShaderBuffer(createInfo); createInfo.sourceData.size = verticesAllocated * sizeof(Vector2); + createInfo.name = "TexCoords"; createInfo.numElements = verticesAllocated * 2; texCoords = graphics->createShaderBuffer(createInfo); diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index 69df8fe..110c25d 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -108,6 +108,7 @@ void VertexData::createDescriptors() }, .numElements = instanceData.size(), .dynamic = false, + .name = "InstanceBuffer" }); matInst.instanceBuffer->pipelineBarrier( Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, @@ -124,6 +125,7 @@ void VertexData::createDescriptors() }, .numElements = meshes.size(), .dynamic = false, + .name = "MeshDataBuffer" }); matInst.meshDataBuffer->pipelineBarrier( Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, @@ -162,7 +164,11 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array std::memcpy(vertexIndices.data() + vertexOffset, m.uniqueVertices, m.numVertices * sizeof(uint32)); uint32 primitiveOffset = primitiveIndices.size(); primitiveIndices.resize(primitiveOffset + (m.numPrimitives * 3)); - std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout, m.numPrimitives * 3 * sizeof(uint8)); + for(size_t x = 0; x < m.numPrimitives*3; ++x) + { + primitiveIndices[primitiveOffset + x] = m.primitiveLayout[x]; + } + //std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout, m.numPrimitives * 3 * sizeof(uint8)); meshlets.add(MeshletDescription{ .bounding = m.boundingBox.toSphere(), .vertexCount = m.numVertices, @@ -190,6 +196,7 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array .data = (uint8*)indices.data(), }, .indexType = Gfx::SE_INDEX_TYPE_UINT32, + .name = "IndexBuffer", }); meshletBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .sourceData = { @@ -198,6 +205,7 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array }, .numElements = meshlets.size(), .dynamic = false, + .name = "MeshletBuffer" }); vertexIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .sourceData = { @@ -206,14 +214,16 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array }, .numElements = vertexIndices.size(), .dynamic = false, + .name = "VertexIndicesBuffer" }); primitiveIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .sourceData = { - .size = sizeof(uint8) * primitiveIndices.size(), + .size = sizeof(uint32) * primitiveIndices.size(), .data = (uint8*)primitiveIndices.data(), }, .numElements = primitiveIndices.size(), .dynamic = false, + .name = "PrimitiveIndicesBuffer", }); } diff --git a/src/Engine/Graphics/Vulkan/Graphics.h b/src/Engine/Graphics/Vulkan/Graphics.h index efb15b0..2f2e507 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.h +++ b/src/Engine/Graphics/Vulkan/Graphics.h @@ -64,7 +64,7 @@ public: virtual Gfx::OSampler createSampler(const SamplerCreateInfo& createInfo) override; virtual Gfx::ODescriptorLayout createDescriptorLayout(const std::string& name = "") override; - virtual Gfx::OPipelineLayout createPipelineLayout(Gfx::PPipelineLayout baseLayout = nullptr) override; + virtual Gfx::OPipelineLayout createPipelineLayout(const std::string& name = "", Gfx::PPipelineLayout baseLayout = nullptr) override; virtual Gfx::OVertexInput createVertexInput(VertexInputStateCreateInfo createInfo) override; @@ -109,4 +109,4 @@ protected: }; DEFINE_REF(Graphics) } // namespace Vulkan -} // namespace Seele \ No newline at end of file +} // namespace Seele diff --git a/src/Engine/Graphics/slang-compile.cpp b/src/Engine/Graphics/slang-compile.cpp index add3bd3..0164610 100644 --- a/src/Engine/Graphics/slang-compile.cpp +++ b/src/Engine/Graphics/slang-compile.cpp @@ -28,11 +28,11 @@ Slang::ComPtr Seele::generateShader(const ShaderCreateInfo& create } sessionDesc.preprocessorMacroCount = macros.size(); sessionDesc.preprocessorMacros = macros.data(); - slang::TargetDesc vulkan; - vulkan.profile = globalSession->findProfile("sm_6_6"); - vulkan.format = target; + slang::TargetDesc targetDesc; + targetDesc.profile = globalSession->findProfile("sm_6_6"); + targetDesc.format = target; sessionDesc.targetCount = 1; - sessionDesc.targets = &vulkan; + sessionDesc.targets = &targetDesc; StaticArray searchPaths = {"shaders/", "shaders/lib/", "shaders/generated/"}; sessionDesc.searchPaths = searchPaths.data(); sessionDesc.searchPathCount = searchPaths.size(); @@ -92,11 +92,14 @@ Slang::ComPtr Seele::generateShader(const ShaderCreateInfo& create slang::ProgramLayout* signature = specializedComponent->getLayout(0, diagnostics.writeRef()); CHECK_DIAGNOSTICS(); auto entry = signature->findEntryPointByName(createInfo.entryPoint.c_str()); + uint32 offset = 0; + if(target == SLANG_DXIL) + { + offset = 1;// idk why + } for(size_t i = 0; i < signature->getParameterCount(); ++i) { - auto param = signature->getParameterByIndex(i); - paramMapping[param->getName()] = param->getBindingIndex(); - std::cout << param->getName() << " " << param->getBindingIndex() << " " << param->getSemanticIndex() << std::endl; + paramMapping[param->getName()] = offset++; } return kernelBlob; }