2024-04-10 12:53:55 +02:00
|
|
|
#pragma once
|
2024-05-17 18:08:11 +02:00
|
|
|
#include "Buffer.h"
|
2024-08-26 21:49:09 +02:00
|
|
|
#include "Foundation/NSArray.hpp"
|
2024-08-28 17:54:14 +02:00
|
|
|
#include "Foundation/NSObject.hpp"
|
2024-04-10 12:53:55 +02:00
|
|
|
#include "Graphics/Descriptor.h"
|
2024-04-10 15:50:20 +02:00
|
|
|
#include "Graphics/Initializer.h"
|
2025-02-14 00:39:53 +01:00
|
|
|
#include "Graphics/Metal/Command.h"
|
2024-05-17 18:08:11 +02:00
|
|
|
#include "Graphics/Metal/Resources.h"
|
2025-02-14 00:39:53 +01:00
|
|
|
#include "Metal/MTLAccelerationStructure.hpp"
|
2024-08-26 21:49:09 +02:00
|
|
|
#include "Metal/MTLArgumentEncoder.hpp"
|
|
|
|
|
#include "Metal/MTLLibrary.hpp"
|
2024-08-28 17:54:14 +02:00
|
|
|
#include "Metal/MTLResource.hpp"
|
2024-04-10 15:50:20 +02:00
|
|
|
#include "MinimalEngine.h"
|
2024-04-10 12:53:55 +02:00
|
|
|
|
|
|
|
|
namespace Seele {
|
|
|
|
|
namespace Metal {
|
|
|
|
|
DECLARE_REF(Graphics)
|
2024-04-12 09:27:30 +02:00
|
|
|
class DescriptorLayout : public Gfx::DescriptorLayout {
|
2024-06-09 12:20:04 +02:00
|
|
|
public:
|
|
|
|
|
DescriptorLayout(PGraphics graphics, const std::string& name);
|
|
|
|
|
virtual ~DescriptorLayout();
|
|
|
|
|
virtual void create() override;
|
2024-08-28 17:54:14 +02:00
|
|
|
MTL::ArgumentEncoder* createEncoder();
|
2024-11-01 21:04:06 +01:00
|
|
|
uint32 getFlattenedIndex(uint32 binding, uint32 arrayIndex) const { return flattenMap.at(flattenIndex(binding, arrayIndex)); }
|
|
|
|
|
constexpr uint32 getTotalBindingCount() const { return flattenedBindingCount; }
|
2025-02-14 00:39:53 +01:00
|
|
|
constexpr bool isPlainDescriptor() const { return plainDescriptor; }
|
2024-11-01 21:04:06 +01:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
private:
|
2024-11-01 21:04:06 +01:00
|
|
|
constexpr uint64 flattenIndex(uint32 binding, uint32 arrayIndex) const { return uint64(arrayIndex) << 32 | binding; }
|
2024-06-09 12:20:04 +02:00
|
|
|
PGraphics graphics;
|
2024-08-28 17:54:14 +02:00
|
|
|
NS::Array* arguments;
|
2024-11-01 21:04:06 +01:00
|
|
|
Map<uint64, uint32> flattenMap;
|
|
|
|
|
uint32 flattenedBindingCount = 0;
|
2025-02-14 00:39:53 +01:00
|
|
|
// descriptor sets containing only uniform data are not actually argument buffers, so they need to be
|
|
|
|
|
// handled separately
|
|
|
|
|
bool plainDescriptor = true;
|
2024-04-10 12:53:55 +02:00
|
|
|
};
|
2024-04-13 23:51:38 +02:00
|
|
|
DEFINE_REF(DescriptorLayout)
|
|
|
|
|
|
|
|
|
|
DECLARE_REF(DescriptorSet)
|
|
|
|
|
class DescriptorPool : public Gfx::DescriptorPool {
|
2024-06-09 12:20:04 +02:00
|
|
|
public:
|
|
|
|
|
DescriptorPool(PGraphics graphics, PDescriptorLayout layout);
|
|
|
|
|
virtual ~DescriptorPool();
|
|
|
|
|
virtual Gfx::PDescriptorSet allocateDescriptorSet() override;
|
|
|
|
|
virtual void reset() override;
|
|
|
|
|
constexpr PDescriptorLayout getLayout() const { return layout; }
|
2024-04-12 09:27:30 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
private:
|
|
|
|
|
PGraphics graphics;
|
|
|
|
|
PDescriptorLayout layout;
|
|
|
|
|
Array<ODescriptorSet> allocatedSets;
|
2024-04-10 15:50:20 +02:00
|
|
|
};
|
2024-04-13 23:51:38 +02:00
|
|
|
DEFINE_REF(DescriptorPool)
|
2024-05-17 18:08:11 +02:00
|
|
|
class DescriptorSet : public Gfx::DescriptorSet, public CommandBoundResource {
|
2024-06-09 12:20:04 +02:00
|
|
|
public:
|
|
|
|
|
DescriptorSet(PGraphics graphics, PDescriptorPool owner);
|
|
|
|
|
virtual ~DescriptorSet();
|
|
|
|
|
virtual void writeChanges() override;
|
2024-11-01 21:04:06 +01:00
|
|
|
virtual void updateBuffer(uint32 binding, uint32 index, Gfx::PUniformBuffer uniformBuffer) override;
|
|
|
|
|
virtual void updateBuffer(uint32 binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) override;
|
|
|
|
|
virtual void updateBuffer(uint32 binding, uint32 index, Gfx::PVertexBuffer uniformBuffer) override;
|
|
|
|
|
virtual void updateBuffer(uint32 binding, uint32 index, Gfx::PIndexBuffer uniformBuffer) override;
|
|
|
|
|
virtual void updateSampler(uint32 binding, uint32 index, Gfx::PSampler samplerState) override;
|
|
|
|
|
virtual void updateTexture(uint32 binding, uint32 index, Gfx::PTexture2D texture) override;
|
|
|
|
|
virtual void updateTexture(uint32 binding, uint32 index, Gfx::PTexture3D texture) override;
|
|
|
|
|
virtual void updateTexture(uint32 binding, uint32 index, Gfx::PTextureCube texture) override;
|
|
|
|
|
virtual void updateAccelerationStructure(uint32 binding, uint32 index, Gfx::PTopLevelAS as) override;
|
|
|
|
|
|
2024-09-16 13:00:53 +02:00
|
|
|
constexpr const Array<MTL::Resource*>& getBoundResources() const { return boundResources; }
|
2025-02-14 00:39:53 +01:00
|
|
|
constexpr bool isPlainDescriptor() const { return owner->getLayout()->isPlainDescriptor(); }
|
|
|
|
|
constexpr MTL::ArgumentEncoder* createEncoder() const { return owner->getLayout()->createEncoder(); }
|
2024-04-14 11:35:37 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
private:
|
|
|
|
|
PGraphics graphics;
|
|
|
|
|
PDescriptorPool owner;
|
2024-09-16 13:00:53 +02:00
|
|
|
Array<MTL::Resource*> boundResources;
|
2024-08-28 17:54:14 +02:00
|
|
|
MTL::Buffer* argumentBuffer = nullptr;
|
2025-02-14 00:39:53 +01:00
|
|
|
MTL::ArgumentEncoder* encoder = nullptr;
|
|
|
|
|
|
|
|
|
|
struct UniformWriteInfo
|
|
|
|
|
{
|
|
|
|
|
uint32 index;
|
|
|
|
|
Array<uint8> content;
|
|
|
|
|
void apply(MTL::ArgumentEncoder* encoder) const { std::memcpy(encoder->constantData(index), content.data(), content.size()); }
|
|
|
|
|
};
|
|
|
|
|
Array<UniformWriteInfo> uniformWrites;
|
|
|
|
|
struct BufferWriteInfo
|
|
|
|
|
{
|
|
|
|
|
uint32 index;
|
|
|
|
|
MTL::Buffer* buffer;
|
|
|
|
|
void apply(MTL::ArgumentEncoder* encoder) const { encoder->setBuffer(buffer, 0, 2); }
|
|
|
|
|
};
|
|
|
|
|
Array<BufferWriteInfo> bufferWrites;
|
|
|
|
|
struct TextureWriteInfo
|
|
|
|
|
{
|
|
|
|
|
uint32 index;
|
|
|
|
|
MTL::Texture* texture;
|
|
|
|
|
void apply(MTL::ArgumentEncoder* encoder) const { encoder->setTexture(texture, index); }
|
|
|
|
|
};
|
|
|
|
|
Array<TextureWriteInfo> textureWrites;
|
|
|
|
|
struct SamplerWriteInfo
|
|
|
|
|
{
|
|
|
|
|
uint32 index;
|
|
|
|
|
MTL::SamplerState* sampler;
|
|
|
|
|
void apply(MTL::ArgumentEncoder* encoder) const { encoder->setSamplerState(sampler, index); }
|
|
|
|
|
};
|
|
|
|
|
Array<SamplerWriteInfo> samplerWrites;
|
|
|
|
|
struct AccelerationStructureWriteInfo
|
|
|
|
|
{
|
|
|
|
|
uint32 index;
|
|
|
|
|
MTL::AccelerationStructure* accelerationStructure;
|
|
|
|
|
void apply(MTL::ArgumentEncoder* encoder) const { encoder->setAccelerationStructure(accelerationStructure, index); }
|
|
|
|
|
};
|
|
|
|
|
Array<AccelerationStructureWriteInfo> accelerationWrites;
|
|
|
|
|
|
|
|
|
|
friend class RenderCommand;
|
|
|
|
|
friend class ComputeCommand;
|
2024-04-10 15:50:20 +02:00
|
|
|
};
|
|
|
|
|
DEFINE_REF(DescriptorSet)
|
|
|
|
|
|
2024-04-13 23:51:38 +02:00
|
|
|
class PipelineLayout : public Gfx::PipelineLayout {
|
2024-06-09 12:20:04 +02:00
|
|
|
public:
|
|
|
|
|
PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout);
|
|
|
|
|
virtual ~PipelineLayout();
|
|
|
|
|
virtual void create() override;
|
2024-05-17 18:08:11 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
private:
|
|
|
|
|
PGraphics graphics;
|
2024-04-10 12:53:55 +02:00
|
|
|
};
|
2024-04-13 23:51:38 +02:00
|
|
|
DEFINE_REF(PipelineLayout)
|
2024-04-12 09:27:30 +02:00
|
|
|
} // namespace Metal
|
2024-04-19 18:23:36 +02:00
|
|
|
} // namespace Seele
|