Files
Seele/src/Engine/Graphics/Metal/Descriptor.mm
T

179 lines
7.0 KiB
Plaintext
Raw Normal View History

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) {
if(descriptorBindings[i].descriptorType != Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER) {
plainDescriptor = false;
} else {
objects[i] = MTL::ArgumentDescriptor::alloc()->init();
objects[i]->setIndex(flattenedBindingCount);
objects[i]->setAccess(cast(descriptorBindings[i].access));
objects[i]->setArrayLength(descriptorBindings[i].descriptorCount);
objects[i]->setDataType(MTL::DataTypeChar);
2024-11-01 21:04:06 +01:00
objects[i]->setArrayLength(descriptorBindings[i].uniformLength);
2024-09-16 13:00:53 +02:00
}
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-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
boundResources[flattenedIndex] = nullptr;
uniformWrites.add(UniformWriteInfo{
.index = flattenedIndex,
.content = buffer->getContents(),
});
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
boundResources[flattenedIndex] = buffer->getHandle();
bufferWrites.add(BufferWriteInfo{
.index = flattenedIndex,
.buffer = 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>();
boundResources[flattenedIndex] = buffer->getHandle();
bufferWrites.add(BufferWriteInfo{
.index = flattenedIndex,
.buffer = buffer->getHandle(),
});
2024-11-01 21:04:06 +01:00
}
2024-11-01 21:04:06 +01:00
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
boundResources[flattenedIndex] = buffer->getHandle();
bufferWrites.add(BufferWriteInfo{
.index = flattenedIndex,
.buffer = 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>();
boundResources[flattenedIndex] = nullptr;
samplerWrites.add(SamplerWriteInfo{
.index = flattenedIndex,
.sampler = sampler->getHandle(),
});
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>();
boundResources[flattenedIndex] = tex->getImage();
textureWrites.add(TextureWriteInfo{
.index = flattenedIndex,
.texture = 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>();
boundResources[flattenedIndex] = tex->getImage();
textureWrites.add(TextureWriteInfo{
.index = flattenedIndex,
.texture = tex->getImage(),
});
2024-11-01 21:04:06 +01:00
}
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>();
boundResources[flattenedIndex] = tex->getImage();
textureWrites.add(TextureWriteInfo{
.index = flattenedIndex,
.texture = tex->getImage(),
});
2024-11-01 21:04:06 +01:00
}
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
}