2024-04-12 09:27:30 +02:00
|
|
|
#include "Descriptor.h"
|
2024-04-13 23:51:38 +02:00
|
|
|
#include "Buffer.h"
|
|
|
|
|
#include "Enums.h"
|
2024-08-28 17:54:14 +02:00
|
|
|
#include "Foundation/NSArray.hpp"
|
2024-08-26 21:49:09 +02:00
|
|
|
#include "Foundation/NSObject.hpp"
|
2024-04-15 13:48:34 +02:00
|
|
|
#include "Graphics/Descriptor.h"
|
2024-09-16 13:00:53 +02:00
|
|
|
#include "Graphics/Enums.h"
|
2024-04-13 23:51:38 +02:00
|
|
|
#include "Graphics/Initializer.h"
|
2024-05-17 18:08:11 +02:00
|
|
|
#include "Graphics/Metal/Resources.h"
|
2024-08-28 17:54:14 +02:00
|
|
|
#include "Graphics/Metal/Shader.h"
|
2024-04-15 13:48:34 +02:00
|
|
|
#include "Metal/MTLArgument.hpp"
|
2024-08-28 17:54:14 +02:00
|
|
|
#include "Metal/MTLArgumentEncoder.hpp"
|
2024-04-15 13:48:34 +02:00
|
|
|
#include "Metal/MTLDevice.hpp"
|
|
|
|
|
#include "Metal/MTLResource.hpp"
|
2024-08-28 17:54:14 +02:00
|
|
|
#include "Metal/MTLStageInputOutputDescriptor.hpp"
|
2024-04-15 13:48:34 +02:00
|
|
|
#include "Metal/MTLTexture.hpp"
|
2024-04-13 23:51:38 +02:00
|
|
|
#include "Texture.h"
|
2024-08-26 21:49:09 +02:00
|
|
|
#include <CRC.h>
|
2024-09-16 13:00:53 +02:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
2024-08-28 17:54:14 +02:00
|
|
|
#include <Foundation/Foundation.h>
|
2024-04-15 13:48:34 +02:00
|
|
|
#include <iostream>
|
2024-09-16 13:00:53 +02:00
|
|
|
#include <stdexcept>
|
2024-04-12 09:27:30 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
using namespace Seele::Metal;
|
|
|
|
|
|
2024-08-28 17:54:14 +02:00
|
|
|
DescriptorLayout::DescriptorLayout(PGraphics graphics, const std::string& name) : Gfx::DescriptorLayout(name), graphics(graphics) {}
|
2024-04-13 23:51:38 +02:00
|
|
|
|
|
|
|
|
DescriptorLayout::~DescriptorLayout() {}
|
|
|
|
|
|
|
|
|
|
void DescriptorLayout::create() {
|
2024-08-26 21:49:09 +02:00
|
|
|
pool = new DescriptorPool(graphics, this);
|
2024-08-28 17:54:14 +02:00
|
|
|
hash = CRC::Calculate(descriptorBindings.data(), sizeof(Gfx::DescriptorBinding) * descriptorBindings.size(), CRC::CRC_32());
|
|
|
|
|
MTL::ArgumentDescriptor** objects = new MTL::ArgumentDescriptor*[descriptorBindings.size()];
|
2024-11-01 21:04:06 +01:00
|
|
|
flattenedBindingCount = 0;
|
2024-09-16 13:00:53 +02:00
|
|
|
for (uint32 i = 0; i < descriptorBindings.size(); ++i) {
|
2024-08-28 17:54:14 +02:00
|
|
|
objects[i] = MTL::ArgumentDescriptor::alloc()->init();
|
2024-11-01 21:04:06 +01:00
|
|
|
objects[i]->setIndex(flattenedBindingCount);
|
2024-08-28 17:54:14 +02:00
|
|
|
objects[i]->setAccess(cast(descriptorBindings[i].access));
|
|
|
|
|
objects[i]->setArrayLength(descriptorBindings[i].descriptorCount);
|
2024-09-16 13:00:53 +02:00
|
|
|
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_STORAGE_BUFFER:
|
|
|
|
|
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
|
|
|
|
|
dataType = MTL::DataTypePointer;
|
|
|
|
|
break;
|
2024-11-01 21:04:06 +01:00
|
|
|
case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
|
|
|
|
|
case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
|
|
|
|
|
case Gfx::SE_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK:
|
|
|
|
|
dataType = MTL::DataTypeChar;
|
|
|
|
|
objects[i]->setArrayLength(descriptorBindings[i].uniformLength);
|
|
|
|
|
break;
|
2024-09-16 13:00:53 +02:00
|
|
|
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);
|
2024-11-01 21:04:06 +01:00
|
|
|
for(uint32 j = 0; j < descriptorBindings[i].descriptorCount; ++j) {
|
|
|
|
|
flattenMap[flattenIndex(i, j)] = flattenedBindingCount++;
|
|
|
|
|
}
|
2024-08-28 17:54:14 +02:00
|
|
|
}
|
|
|
|
|
arguments = NS::Array::array((NS::Object**)objects, descriptorBindings.size());
|
2024-04-12 09:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-16 13:00:53 +02:00
|
|
|
MTL::ArgumentEncoder* DescriptorLayout::createEncoder() { return graphics->getDevice()->newArgumentEncoder(arguments); }
|
2024-08-28 17:54:14 +02:00
|
|
|
|
|
|
|
|
DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout) : graphics(graphics), layout(layout) {}
|
2024-04-13 23:51:38 +02:00
|
|
|
|
|
|
|
|
DescriptorPool::~DescriptorPool() {}
|
|
|
|
|
|
|
|
|
|
Gfx::PDescriptorSet DescriptorPool::allocateDescriptorSet() {
|
2024-08-28 17:54:14 +02:00
|
|
|
for (uint32 setIndex = 0; setIndex < allocatedSets.size(); ++setIndex) {
|
|
|
|
|
if (allocatedSets[setIndex]->isCurrentlyBound()) {
|
|
|
|
|
// Currently in use, skip
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Found set, stop searching
|
|
|
|
|
return PDescriptorSet(allocatedSets[setIndex]);
|
2024-04-13 23:51:38 +02:00
|
|
|
}
|
2024-08-28 17:54:14 +02:00
|
|
|
allocatedSets.add(new DescriptorSet(graphics, this));
|
|
|
|
|
return PDescriptorSet(allocatedSets.back());
|
2024-04-13 23:51:38 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:54:14 +02:00
|
|
|
void DescriptorPool::reset() {}
|
2024-04-13 23:51:38 +02:00
|
|
|
|
|
|
|
|
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner)
|
2024-08-28 17:54:14 +02:00
|
|
|
: Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics), graphics(graphics), owner(owner) {
|
2024-09-16 13:00:53 +02:00
|
|
|
encoder = owner->getLayout()->createEncoder();
|
|
|
|
|
argumentBuffer = graphics->getDevice()->newBuffer(encoder->encodedLength(), 0);
|
2024-11-01 21:04:06 +01:00
|
|
|
std::cout << owner->getLayout()->getName() << ": " << encoder->encodedLength() << std::endl;
|
2024-09-16 13:00:53 +02:00
|
|
|
encoder->setArgumentBuffer(argumentBuffer, 0);
|
2024-11-01 21:04:06 +01:00
|
|
|
boundResources.resize(owner->getLayout()->getTotalBindingCount());
|
2024-04-13 23:51:38 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:54:14 +02:00
|
|
|
DescriptorSet::~DescriptorSet() {}
|
2024-04-13 23:51:38 +02:00
|
|
|
|
|
|
|
|
void DescriptorSet::writeChanges() {}
|
|
|
|
|
|
2024-11-01 21:04:06 +01:00
|
|
|
void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PUniformBuffer uniformBuffer) {
|
|
|
|
|
uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index);
|
2024-09-16 13:00:53 +02:00
|
|
|
PUniformBuffer buffer = uniformBuffer.cast<UniformBuffer>();
|
2024-11-01 21:04:06 +01:00
|
|
|
std::memcpy(encoder->constantData(flattenedIndex), buffer->getContents(), buffer->getSize());
|
|
|
|
|
boundResources[flattenedIndex] = nullptr;
|
2024-04-13 23:51:38 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-01 21:04:06 +01:00
|
|
|
void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) {
|
|
|
|
|
uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index);
|
2024-09-16 13:00:53 +02:00
|
|
|
PShaderBuffer buffer = uniformBuffer.cast<ShaderBuffer>();
|
2024-11-01 21:04:06 +01:00
|
|
|
encoder->setBuffer(buffer->getHandle(), 0, flattenedIndex);
|
|
|
|
|
boundResources[flattenedIndex] = buffer->getHandle();
|
2024-05-17 18:08:11 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-01 21:04:06 +01:00
|
|
|
void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PVertexBuffer uniformBuffer) {
|
|
|
|
|
uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index);
|
|
|
|
|
PVertexBuffer buffer = uniformBuffer.cast<VertexBuffer>();
|
|
|
|
|
encoder->setBuffer(buffer->getHandle(), 0, flattenedIndex);
|
|
|
|
|
boundResources[flattenedIndex] = buffer->getHandle();
|
|
|
|
|
}
|
|
|
|
|
void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PIndexBuffer uniformBuffer) {
|
|
|
|
|
uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index);
|
2024-09-16 13:00:53 +02:00
|
|
|
PIndexBuffer buffer = uniformBuffer.cast<IndexBuffer>();
|
2024-11-01 21:04:06 +01:00
|
|
|
encoder->setBuffer(buffer->getHandle(), 0, flattenedIndex);
|
|
|
|
|
boundResources[flattenedIndex] = buffer->getHandle();
|
2024-08-26 21:49:09 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-01 21:04:06 +01:00
|
|
|
void DescriptorSet::updateSampler(uint32 binding, uint32 index, Gfx::PSampler samplerState) {
|
|
|
|
|
uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index);
|
2024-09-16 13:00:53 +02:00
|
|
|
PSampler sampler = samplerState.cast<Sampler>();
|
2024-11-01 21:04:06 +01:00
|
|
|
encoder->setSamplerState(sampler->getHandle(), flattenedIndex);
|
|
|
|
|
boundResources[flattenedIndex] = nullptr; // Samplers are not resources??????
|
2024-08-28 17:54:14 +02:00
|
|
|
}
|
2024-08-26 21:49:09 +02:00
|
|
|
|
2024-11-01 21:04:06 +01:00
|
|
|
void DescriptorSet::updateTexture(uint32 binding, uint32 index, Gfx::PTexture2D texture) {
|
|
|
|
|
uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index);
|
|
|
|
|
PTextureBase tex = texture.cast<TextureBase>();
|
|
|
|
|
encoder->setTexture(tex->getImage(), flattenedIndex);
|
|
|
|
|
boundResources[flattenedIndex] = tex->getImage();
|
2024-08-28 17:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-01 21:04:06 +01:00
|
|
|
void DescriptorSet::updateTexture(uint32 binding, uint32 index, Gfx::PTexture3D texture) {
|
|
|
|
|
uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index);
|
|
|
|
|
PTextureBase tex = texture.cast<TextureBase>();
|
|
|
|
|
encoder->setTexture(tex->getImage(), flattenedIndex);
|
|
|
|
|
boundResources[flattenedIndex] = tex->getImage();
|
|
|
|
|
}
|
2024-08-28 17:54:14 +02:00
|
|
|
|
2024-11-01 21:04:06 +01:00
|
|
|
void DescriptorSet::updateTexture(uint32 binding, uint32 index, Gfx::PTextureCube texture) {
|
|
|
|
|
uint32 flattenedIndex = owner->getLayout()->getFlattenedIndex(binding, index);
|
|
|
|
|
PTextureBase tex = texture.cast<TextureBase>();
|
|
|
|
|
encoder->setTexture(tex->getImage(), flattenedIndex);
|
|
|
|
|
boundResources[flattenedIndex] = tex->getImage();
|
|
|
|
|
}
|
2024-08-28 17:54:14 +02:00
|
|
|
|
2024-11-01 21:04:06 +01:00
|
|
|
void DescriptorSet::updateAccelerationStructure(uint32 binding, uint32 index, Gfx::PTopLevelAS as) { assert(false && "TODO"); }
|
2024-08-28 17:54:14 +02:00
|
|
|
|
|
|
|
|
PipelineLayout::PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout)
|
2024-04-20 21:35:43 +02:00
|
|
|
: Gfx::PipelineLayout(name, baseLayout), graphics(graphics) {}
|
2024-04-13 23:51:38 +02:00
|
|
|
|
2024-04-15 13:48:34 +02:00
|
|
|
PipelineLayout::~PipelineLayout() {}
|
2024-04-13 23:51:38 +02:00
|
|
|
|
2024-04-15 13:48:34 +02:00
|
|
|
void PipelineLayout::create() {
|
2024-08-28 17:54:14 +02:00
|
|
|
for (auto& [_, set] : descriptorSetLayouts) {
|
|
|
|
|
assert(set->getHash() != 0);
|
|
|
|
|
uint32 setHash = set->getHash();
|
|
|
|
|
layoutHash = CRC::Calculate(&setHash, sizeof(uint32), CRC::CRC_32(), layoutHash);
|
|
|
|
|
}
|
2024-04-19 18:23:36 +02:00
|
|
|
}
|