this sucks, but there is hope
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user