this sucks, but there is hope

This commit is contained in:
Dynamitos
2024-04-20 21:35:43 +02:00
parent a27e280ab8
commit acc976fe84
30 changed files with 279 additions and 192 deletions
BIN
View File
Binary file not shown.
+4 -4
View File
@@ -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);
}
}
-1
View File
@@ -42,6 +42,5 @@ struct Scene
StructuredBuffer<uint32_t> primitiveIndices;
StructuredBuffer<uint32_t> vertexIndices;
};
layout(set=2)
ParameterBlock<Scene> pScene;
+77
View File
@@ -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
]
}
Binary file not shown.
+2 -2
View File
@@ -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;
+5 -3
View File
@@ -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<std::string, PDescriptorLayout>& getLayouts() const { return descriptorSetLayouts; }
constexpr uint32 findParameter(const std::string& name) const { return parameterMapping[name]; }
void addMapping(Map<std::string, uint32> mapping);
constexpr std::string getName() const {return name;};
protected:
uint32 layoutHash = 0;
Map<std::string, PDescriptorLayout> descriptorSetLayouts;
Map<std::string, uint32> parameterMapping;
Array<SePushConstantRange> pushConstants;
std::string name;
};
DEFINE_REF(PipelineLayout)
} // namespace Gfx
+2 -2
View File
@@ -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
} // namespace Seele
+4 -4
View File
@@ -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
+2 -2
View File
@@ -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
} // namespace Seele
+12 -9
View File
@@ -3,10 +3,12 @@
#include "Graphics/Buffer.h"
#include "Graphics/Enums.h"
#include "Graphics/Initializer.h"
#include <iostream>
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) {}
Gfx::SeAccessFlags dstAccess, Gfx::SePipelineStageFlags dstStage) {}
+78 -41
View File
@@ -11,6 +11,7 @@
#include "Pipeline.h"
#include "Resources.h"
#include "Window.h"
#include <iostream>
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<GraphicsPipeline>();
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<DescriptorSet>()->getBuffer(), 0, parameterIndex);
encoder->setFragmentBuffer(descriptorSet.cast<DescriptorSet>()->getBuffer(), 0, parameterIndex);
encoder->setMeshBuffer(descriptorSet.cast<DescriptorSet>()->getBuffer(), 0, parameterIndex);
encoder->setObjectBuffer(descriptorSet.cast<DescriptorSet>()->getBuffer(), 0, parameterIndex);
auto metalSet = descriptorSet.cast<DescriptorSet>();
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<Gfx::PDescriptorSet>& 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<ComputePipeline>();
encoder->setComputePipelineState(boundPipeline->getHandle());
argumentBuffer = boundPipeline->graphics->getDevice()->newBuffer(sizeof(uint64) * boundPipeline->getPipelineLayout()->getLayouts().size(), MTL::ResourceOptionCPUCacheModeDefault);
boundPipeline = pipeline.cast<ComputePipeline>();
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<DescriptorSet>();
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<Gfx::PDescriptorSet>& 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(); }
+3 -7
View File
@@ -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<MTL::Resource*> 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<Array<uint32>>
private:
PGraphics graphics;
};
+32 -86
View File
@@ -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<NS::Object*> 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<size_t>(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<UniformBuffer>();
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<ShaderBuffer>();
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<Sampler>();
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<TextureBase>();
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<Gfx::PTexture> array) {
for (auto& t : array) {
PTextureBase metalTexture = t.cast<TextureBase>();
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);
}
+1 -1
View File
@@ -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;
+2 -2
View File
@@ -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)
+1 -1
View File
@@ -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 {
+7 -7
View File
@@ -25,15 +25,15 @@ Shader::~Shader() {
}
void Shader::create(const ShaderCreateInfo& createInfo) {
std::cout << "Compiling " << createInfo.name << std::endl;
Map<std::string, uint32> test;
Slang::ComPtr<slang::IBlob> kernelBlob = generateShader(createInfo, SLANG_DXIL, test);
std::cout << "Compiling " << createInfo.name << std::endl;
Map<std::string, uint32> paramMapping;
Slang::ComPtr<slang::IBlob> 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;
+5 -1
View File
@@ -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));
+1 -1
View File
@@ -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());
+1 -1
View File
@@ -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();
@@ -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())
{
@@ -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<std::string, uint32> mapping;
@@ -15,6 +15,7 @@ RenderPass::RenderPass(Gfx::PGraphics graphics, PScene scene)
.data = (uint8*)&viewParams,
},
.dynamic = true,
.name = "viewParamsBuffer",
};
viewParamsBuffer = graphics->createUniformBuffer(uniformInitializer);
viewParamsLayout->create();
@@ -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);
+2 -2
View File
@@ -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<std::string, uint32> mapping;
@@ -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);
+12 -2
View File
@@ -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<uint32> loadedIndices, Array<Meshlet>
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<uint32> loadedIndices, Array<Meshlet>
.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<uint32> loadedIndices, Array<Meshlet>
},
.numElements = meshlets.size(),
.dynamic = false,
.name = "MeshletBuffer"
});
vertexIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
@@ -206,14 +214,16 @@ void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet>
},
.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",
});
}
+2 -2
View File
@@ -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
} // namespace Seele
+10 -7
View File
@@ -28,11 +28,11 @@ Slang::ComPtr<slang::IBlob> 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<const char*, 3> searchPaths = {"shaders/", "shaders/lib/", "shaders/generated/"};
sessionDesc.searchPaths = searchPaths.data();
sessionDesc.searchPathCount = searchPaths.size();
@@ -92,11 +92,14 @@ Slang::ComPtr<slang::IBlob> 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;
}