Descriptors now work in metal hopefully

This commit is contained in:
Dynamitos
2024-09-16 13:00:53 +02:00
parent 49e94d3b74
commit 6417ab940d
45 changed files with 435 additions and 476 deletions
+79 -25
View File
@@ -4,6 +4,7 @@
#include "Foundation/NSArray.hpp"
#include "Foundation/NSObject.hpp"
#include "Graphics/Descriptor.h"
#include "Graphics/Enums.h"
#include "Graphics/Initializer.h"
#include "Graphics/Metal/Resources.h"
#include "Graphics/Metal/Shader.h"
@@ -15,8 +16,10 @@
#include "Metal/MTLTexture.hpp"
#include "Texture.h"
#include <CRC.h>
#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/Foundation.h>
#include <iostream>
#include <stdexcept>
using namespace Seele;
using namespace Seele::Metal;
@@ -29,19 +32,68 @@ 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()];
for(uint32 i = 0; i < descriptorBindings.size(); ++i) {
for (uint32 i = 0; i < descriptorBindings.size(); ++i) {
objects[i] = MTL::ArgumentDescriptor::alloc()->init();
objects[i]->setIndex(i);
objects[i]->setAccess(cast(descriptorBindings[i].access));
objects[i]->setArrayLength(descriptorBindings[i].descriptorCount);
objects[i]->setDataType(MTL::DataTypeStruct);
MTL::DataType dataType;
switch (descriptorBindings[i].descriptorType) {
case Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE:
case Gfx::SE_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
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:
dataType = MTL::DataTypePointer;
break;
case Gfx::SE_DESCRIPTOR_TYPE_SAMPLER:
dataType = MTL::DataTypeSampler;
break;
case Gfx::SE_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:
dataType = MTL::DataTypeInstanceAccelerationStructure;
break;
default:
throw new std::logic_error("unknown descriptor type");
}
objects[i]->setDataType(dataType);
MTL::TextureType textureType;
switch (descriptorBindings[i].textureType) {
case Gfx::SE_IMAGE_VIEW_TYPE_1D:
textureType = MTL::TextureType1D;
break;
case Gfx::SE_IMAGE_VIEW_TYPE_2D:
textureType = MTL::TextureType2D;
break;
case Gfx::SE_IMAGE_VIEW_TYPE_3D:
textureType = MTL::TextureType3D;
break;
case Gfx::SE_IMAGE_VIEW_TYPE_CUBE:
textureType = MTL::TextureTypeCube;
break;
case Gfx::SE_IMAGE_VIEW_TYPE_1D_ARRAY:
textureType = MTL::TextureType1DArray;
break;
case Gfx::SE_IMAGE_VIEW_TYPE_2D_ARRAY:
textureType = MTL::TextureType2DArray;
break;
case Gfx::SE_IMAGE_VIEW_TYPE_CUBE_ARRAY:
textureType = MTL::TextureTypeCubeArray;
break;
}
objects[i]->setTextureType(textureType);
}
arguments = NS::Array::array((NS::Object**)objects, descriptorBindings.size());
}
MTL::ArgumentEncoder* DescriptorLayout::createEncoder() {
return graphics->getDevice()->newArgumentEncoder(arguments);
}
MTL::ArgumentEncoder* DescriptorLayout::createEncoder() { return graphics->getDevice()->newArgumentEncoder(arguments); }
DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout) : graphics(graphics), layout(layout) {}
@@ -65,10 +117,10 @@ void DescriptorPool::reset() {}
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);
encoder->setArgumentBuffer(argumentBuffer, 0);
boundResources.resize(owner->getLayout()->getBindings().size());
for (uint32 i = 0; i < boundResources.size(); ++i) {
boundResources[i].resize(owner->getLayout()->getBindings()[i].descriptorCount);
}
}
DescriptorSet::~DescriptorSet() {}
@@ -76,38 +128,40 @@ DescriptorSet::~DescriptorSet() {}
void DescriptorSet::writeChanges() {}
void DescriptorSet::updateBuffer(uint32 binding, Gfx::PUniformBuffer uniformBuffer) {
boundResources[binding][0] = uniformBuffer.cast<UniformBuffer>()->getHandle();
PUniformBuffer buffer = uniformBuffer.cast<UniformBuffer>();
encoder->setBuffer(buffer->getHandle(), 0, binding);
boundResources[binding] = buffer->getHandle();
}
void DescriptorSet::updateBuffer(uint32 binding, Gfx::PShaderBuffer uniformBuffer) {
boundResources[binding][0] = uniformBuffer.cast<ShaderBuffer>()->getHandle();
PShaderBuffer buffer = uniformBuffer.cast<ShaderBuffer>();
encoder->setBuffer(buffer->getHandle(), 0, binding);
boundResources[binding] = buffer->getHandle();
}
void DescriptorSet::updateBuffer(uint32 binding, Gfx::PIndexBuffer uniformBuffer) {
boundResources[binding][0] = uniformBuffer.cast<IndexBuffer>()->getHandle();
}
void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) {
boundResources[binding][0] = uniformBuffer.cast<ShaderBuffer>()->getHandle();
PIndexBuffer buffer = uniformBuffer.cast<IndexBuffer>();
encoder->setBuffer(buffer->getHandle(), 0, binding);
boundResources[binding] = buffer->getHandle();
}
void DescriptorSet::updateSampler(uint32 binding, Gfx::PSampler samplerState) {
boundResources[binding][0] = nullptr; // Samplers are not resources??????
PSampler sampler = samplerState.cast<Sampler>();
encoder->setSamplerState(sampler->getHandle(), binding);
boundResources[binding] = nullptr; // Samplers are not resources??????
}
void DescriptorSet::updateSampler(uint32 binding, uint32 dstArrayIndex, Gfx::PSampler samplerState) {
boundResources[binding][dstArrayIndex] = nullptr;
void DescriptorSet::updateTexture(uint32 binding, Gfx::PTexture texture, Gfx::PSampler sampler) {
PTextureBase base = texture.cast<TextureBase>();
encoder->setTexture(base->getHandle()->texture, binding);
boundResources[binding] = base->getHandle()->texture;
}
void DescriptorSet::updateTexture(uint32 binding, Gfx::PTexture texture, Gfx::PSampler sampler) {}
void DescriptorSet::updateTextureArray(uint32 binding, Array<Gfx::PTexture2D> texture) { assert(false && "TODO"); }
void DescriptorSet::updateTexture(uint32 binding, uint32 dstArrayIndex, Gfx::PTexture texture) {}
void DescriptorSet::updateSamplerArray(uint32 binding, Array<Gfx::PSampler> samplers) { assert(false && "TODO"); }
void DescriptorSet::updateTextureArray(uint32 binding, Array<Gfx::PTexture2D> texture) {}
void DescriptorSet::updateSamplerArray(uint32 binding, Array<Gfx::PSampler> samplers) {}
void DescriptorSet::updateAccelerationStructure(uint32 binding, Gfx::PTopLevelAS as) {}
void DescriptorSet::updateAccelerationStructure(uint32 binding, Gfx::PTopLevelAS as) { assert(false && "TODO"); }
PipelineLayout::PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout)
: Gfx::PipelineLayout(name, baseLayout), graphics(graphics) {}