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

121 lines
4.1 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-26 21:49:09 +02:00
#include "Foundation/NSObject.hpp"
2024-04-15 13:48:34 +02:00
#include "Graphics/Descriptor.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-04-15 13:48:34 +02:00
#include "Metal/MTLArgument.hpp"
#include "Metal/MTLDevice.hpp"
#include "Metal/MTLResource.hpp"
#include "Metal/MTLTexture.hpp"
2024-04-13 23:51:38 +02:00
#include "Texture.h"
2024-04-15 13:48:34 +02:00
#include <Foundation/Foundation.h>
2024-08-26 21:49:09 +02:00
#include <CRC.h>
2024-04-15 13:48:34 +02:00
#include <iostream>
2024-04-12 09:27:30 +02:00
using namespace Seele;
using namespace Seele::Metal;
2024-05-17 18:08:11 +02:00
DescriptorLayout::DescriptorLayout(PGraphics graphics, const std::string &name)
2024-04-20 21:35:43 +02:00
: 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);
hash =
CRC::Calculate(descriptorBindings.data(),
sizeof(Gfx::DescriptorBinding) * descriptorBindings.size(),
CRC::CRC_32());
2024-04-12 09:27:30 +02:00
}
2024-05-17 18:08:11 +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() {
for (uint32 setIndex = 0; setIndex < allocatedSets.size(); ++setIndex) {
2024-08-26 21:49:09 +02:00
if (allocatedSets[setIndex]->isCurrentlyBound()) {
2024-04-13 23:51:38 +02:00
// Currently in use, skip
continue;
}
// Found set, stop searching
return PDescriptorSet(allocatedSets[setIndex]);
}
allocatedSets.add(new DescriptorSet(graphics, this));
return PDescriptorSet(allocatedSets.back());
}
void DescriptorPool::reset() {
}
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner)
2024-05-17 18:08:11 +02:00
: Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics),
2024-08-26 21:49:09 +02:00
graphics(graphics), owner(owner) {
2024-04-20 21:35:43 +02:00
boundResources.resize(owner->getLayout()->getBindings().size());
2024-08-26 21:49:09 +02:00
for(uint32 i = 0; i < boundResources.size(); ++i) {
boundResources[i].resize(owner->getLayout()->getBindings()[i].descriptorCount);
}
encoder = owner->getLayout()->createEncoder();
buffer = graphics->getDevice()->newBuffer(encoder->encodedLength(), MTL::ResourceOptionCPUCacheModeDefault);
2024-05-17 18:08:11 +02:00
buffer->setLabel(NS::String::string(owner->getLayout()->getName().c_str(),
NS::ASCIIStringEncoding));
2024-04-13 23:51:38 +02:00
}
2024-05-17 18:08:11 +02:00
DescriptorSet::~DescriptorSet() { buffer->release(); }
2024-04-13 23:51:38 +02:00
void DescriptorSet::writeChanges() {}
2024-08-26 21:49:09 +02:00
void DescriptorSet::updateBuffer(uint32 binding, Gfx::PUniformBuffer uniformBuffer)
{
encoder->setBuffer(uniformBuffer.cast<UniformBuffer>()->getHandle(), 0, binding);
2024-04-13 23:51:38 +02:00
}
2024-08-26 21:49:09 +02:00
void DescriptorSet::updateBuffer(uint32 binding, Gfx::PShaderBuffer uniformBuffer)
{
encoder->setBuffer(uniformBuffer.cast<ShaderBuffer>()->getHandle(), 0, binding);
2024-05-17 18:08:11 +02:00
}
2024-08-26 21:49:09 +02:00
void DescriptorSet::updateBuffer(uint32 binding, Gfx::PIndexBuffer uniformBuffer)
{
encoder->setBuffer(uniformBuffer.cast<IndexBuffer>()->getHandle(), 0, binding);
2024-04-13 23:51:38 +02:00
}
2024-08-26 21:49:09 +02:00
void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PShaderBuffer uniformBuffer)
{
encoder->setBuffer(uniformBuffer.cast<ShaderBuffer>()->getHandle(), 0, binding);
}
void DescriptorSet::updateSampler(uint32 binding, Gfx::PSampler samplerState)
{}
void DescriptorSet::updateSampler(uint32 binding, uint32 dstArrayIndex, Gfx::PSampler samplerState)
{}
void DescriptorSet::updateTexture(uint32 binding, Gfx::PTexture texture, Gfx::PSampler sampler)
{}
void DescriptorSet::updateTexture(uint32 binding, uint32 dstArrayIndex, Gfx::PTexture texture)
{}
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)
{}
2024-05-17 18:08:11 +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-05-17 18:08:11 +02:00
for (auto &[_, set] : descriptorSetLayouts) {
assert(set->getHash() != 0);
2024-04-13 23:51:38 +02:00
uint32 setHash = set->getHash();
2024-05-17 18:08:11 +02:00
layoutHash =
CRC::Calculate(&setHash, sizeof(uint32), CRC::CRC_32(), layoutHash);
2024-04-13 23:51:38 +02:00
}
2024-04-19 18:23:36 +02:00
}