Descriptors now work in metal hopefully

This commit is contained in:
Dynamitos
2024-09-16 13:00:53 +02:00
parent 49e94d3b74
commit 6417ab940d
45 changed files with 435 additions and 476 deletions
+12 -3
View File
@@ -42,8 +42,11 @@ void BufferAllocation::unmap() {}
Buffer::Buffer(PGraphics graphics, uint64 size, Gfx::SeBufferUsageFlags usage, Gfx::QueueType queueType, bool dynamic, std::string name,
bool createCleared, uint32 clearValue)
: graphics(graphics), currentBuffer(0), dynamic(dynamic), createCleared(createCleared), name(name), clearValue(clearValue) {
buffers.add(nullptr);
createBuffer(size, 0);
if(size > 0)
{
buffers.add(nullptr);
createBuffer(size, 0);
}
}
Buffer::~Buffer() {
@@ -101,10 +104,16 @@ void Buffer::copyBuffer(uint64 src, uint64 dest) {
std::memcpy(buffers[dest]->map(), buffers[src]->map(), buffers[src]->size);
}
void Buffer::transferOwnership(Gfx::QueueType newOwner) { getAlloc()->transferOwnership(newOwner); }
void Buffer::transferOwnership(Gfx::QueueType newOwner) {
if(buffers.size() == 0)
return;
getAlloc()->transferOwnership(newOwner);
}
void Buffer::pipelineBarrier(Gfx::SeAccessFlags srcAccess, Gfx::SePipelineStageFlags srcStage, Gfx::SeAccessFlags dstAccess,
Gfx::SePipelineStageFlags dstStage) {
if(buffers.size() == 0)
return;
getAlloc()->pipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
}
+6 -7
View File
@@ -57,8 +57,8 @@ class RenderCommand : public Gfx::RenderCommand {
virtual void setViewport(Gfx::PViewport viewport) override;
virtual void bindPipeline(Gfx::PGraphicsPipeline pipeline) override;
virtual void bindPipeline(Gfx::PRayTracingPipeline pipeline) override;
virtual void bindDescriptor(Gfx::PDescriptorSet descriptorSet, Array<uint32> dynamicOffsets) override;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets, Array<uint32> dynamicOffsets) override;
virtual void bindDescriptor(Gfx::PDescriptorSet descriptorSet) override;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets) override;
virtual void bindVertexBuffer(const Array<Gfx::PVertexBuffer>& buffers) override;
virtual void bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) override;
virtual void pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) override;
@@ -71,9 +71,8 @@ class RenderCommand : public Gfx::RenderCommand {
private:
PGraphicsPipeline boundPipeline;
PIndexBuffer boundIndexBuffer;
MTL::ArgumentEncoder* argumentEncoder;
MTL::Buffer* argumentBuffer;
MTL::RenderCommandEncoder* encoder;
MTL::Buffer* constantsBuffer;
std::string name;
};
DEFINE_REF(RenderCommand)
@@ -83,8 +82,8 @@ class ComputeCommand : public Gfx::ComputeCommand {
virtual ~ComputeCommand();
void end();
virtual void bindPipeline(Gfx::PComputePipeline pipeline) override;
virtual void bindDescriptor(Gfx::PDescriptorSet set, Array<uint32> dynamicOffsets) override;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& sets, Array<uint32> dynamicOffsets) override;
virtual void bindDescriptor(Gfx::PDescriptorSet set) override;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& sets) override;
virtual void pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) override;
virtual void dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) override;
@@ -92,7 +91,7 @@ class ComputeCommand : public Gfx::ComputeCommand {
PComputePipeline boundPipeline;
MTL::CommandBuffer* commandBuffer;
MTL::ComputeCommandEncoder* encoder;
MTL::Buffer* argumentBuffer;
MTL::Buffer* constantsBuffer;
std::string name;
};
DEFINE_REF(ComputeCommand)
+34 -19
View File
@@ -3,6 +3,7 @@
#include "Containers/Array.h"
#include "Descriptor.h"
#include "Enums.h"
#include "Graphics/Graphics.h"
#include "Graphics/Command.h"
#include "Graphics/Enums.h"
#include "Graphics/Graphics.h"
@@ -74,35 +75,51 @@ void RenderCommand::setViewport(Gfx::PViewport viewport) {
void RenderCommand::bindPipeline(Gfx::PGraphicsPipeline pipeline) {
boundPipeline = pipeline.cast<GraphicsPipeline>();
encoder->setRenderPipelineState(boundPipeline->getHandle());
if(boundPipeline->getPipelineLayout()->hasPushConstants()) {
constantsBuffer = boundPipeline->graphics->getDevice()->newBuffer(boundPipeline->getPipelineLayout()->getPushConstantsSize(), 0);
}
}
void RenderCommand::bindPipeline(Gfx::PRayTracingPipeline pipeline) {}
void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet, Array<uint32> offsets) {
void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet) {
auto metalSet = descriptorSet.cast<DescriptorSet>();
metalSet->bind();
uint32 descriptorIndex = boundPipeline->getPipelineLayout()->findParameter(metalSet->getLayout()->getName());
encoder->setVertexBuffer(metalSet->getArgumentBuffer(), 0, descriptorIndex);
encoder->setFragmentBuffer(metalSet->getArgumentBuffer(), 0, descriptorIndex);
encoder->setObjectBuffer(metalSet->getArgumentBuffer(), 0, descriptorIndex);
encoder->setMeshBuffer(metalSet->getArgumentBuffer(), 0, descriptorIndex);
}
void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets, Array<uint32> offsets) {
void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets) {
for (auto set : descriptorSets) {
bindDescriptor(set, offsets);
bindDescriptor(set);
}
}
void RenderCommand::bindVertexBuffer(const Array<Gfx::PVertexBuffer>& buffers) {
uint32 i = 0;
for (auto buffer : buffers) {
encoder->setVertexBuffer(buffer.cast<VertexBuffer>()->getHandle(), 0, METAL_VERTEXBUFFER_OFFSET + i++);
encoder->setVertexBuffer(buffer.cast<VertexBuffer>()->getHandle(), 0, buffer.cast<VertexBuffer>()->getVertexSize(), i++);
}
}
void RenderCommand::bindIndexBuffer(Gfx::PIndexBuffer gfxIndexBuffer) { boundIndexBuffer = gfxIndexBuffer.cast<IndexBuffer>(); }
void RenderCommand::pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) {
std::memcpy(constantsBuffer->contents(), data, size);
uint pushIndex = boundPipeline->getPipelineLayout()->findParameter("pOffsets");
if (stage & Gfx::SE_SHADER_STAGE_VERTEX_BIT) {
encoder->setVertexBytes((char*)data + offset, size, 0);
encoder->setVertexBuffer(constantsBuffer, 0, pushIndex); // TODO: hardcoded
}
if (stage & Gfx::SE_SHADER_STAGE_FRAGMENT_BIT) {
encoder->setFragmentBytes((char*)data + offset, size, 0);
encoder->setFragmentBuffer(constantsBuffer, 0, pushIndex); // TODO: hardcoded
}
if (stage & Gfx::SE_SHADER_STAGE_TASK_BIT_EXT) {
encoder->setObjectBuffer(constantsBuffer, 0, pushIndex); // TODO: hardcoded
}
if (stage & Gfx::SE_SHADER_STAGE_MESH_BIT_EXT) {
encoder->setMeshBuffer(constantsBuffer, 0, pushIndex); // TODO: hardcoded
}
}
@@ -116,11 +133,6 @@ void RenderCommand::drawIndexed(uint32 indexCount, uint32 instanceCount, int32 f
}
void RenderCommand::drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) {
// TODO:
std::cout << "Draw" << std::endl;
encoder->setFragmentBuffer(argumentBuffer, 0, 2);
encoder->setMeshBuffer(argumentBuffer, 0, 2);
encoder->setObjectBuffer(argumentBuffer, 0, 2);
encoder->drawMeshThreadgroups(MTL::Size(groupX, groupY, groupZ), MTL::Size(128, 1, 1), MTL::Size(32, 1, 1));
}
@@ -143,29 +155,32 @@ void ComputeCommand::end() {
void ComputeCommand::bindPipeline(Gfx::PComputePipeline pipeline) {
boundPipeline = pipeline.cast<ComputePipeline>();
encoder->setComputePipelineState(boundPipeline->getHandle());
argumentBuffer = boundPipeline->graphics->getDevice()->newBuffer(
sizeof(uint64) * (boundPipeline->getPipelineLayout()->getLayouts().size() + 1), MTL::ResourceStorageModeShared);
argumentBuffer->setLabel(NS::String::string(pipeline->getPipelineLayout()->getName().c_str(), NS::ASCIIStringEncoding));
if(boundPipeline->getPipelineLayout()->hasPushConstants()) {
constantsBuffer = boundPipeline->graphics->getDevice()->newBuffer(boundPipeline->getPipelineLayout()->getPushConstantsSize(), 0);
}
}
void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet set, Array<uint32> offsets) {
void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet set) {
auto metalSet = set.cast<DescriptorSet>();
metalSet->bind();
uint32 descriptorIndex = boundPipeline->getPipelineLayout()->findParameter(metalSet->getLayout()->getName());
encoder->setBuffer(metalSet->getArgumentBuffer(), 0, descriptorIndex);
}
void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& sets, Array<uint32> offsets) {
void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& sets) {
for (auto& set : sets) {
bindDescriptor(set, offsets);
bindDescriptor(set);
}
}
void ComputeCommand::pushConstants(Gfx::SeShaderStageFlags, uint32 offset, uint32 size, const void* data) {
encoder->setBytes((char*)data + offset, size, 0);
std::memcpy(constantsBuffer->contents(), data, size);
uint pushIndex = boundPipeline->getPipelineLayout()->findParameter("pMipParam");
encoder->setBuffer(constantsBuffer, 0, pushIndex); // TODO: hardcoded
}
void ComputeCommand::dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) {
// TODO
encoder->setBuffer(argumentBuffer, 0, 2);
encoder->dispatchThreadgroups(MTL::Size(threadX, threadY, threadZ), MTL::Size(32, 32, 1));
}
+4 -5
View File
@@ -50,21 +50,20 @@ class DescriptorSet : public Gfx::DescriptorSet, public CommandBoundResource {
virtual void updateBuffer(uint32 binding, Gfx::PUniformBuffer uniformBuffer) override;
virtual void updateBuffer(uint32 binding, Gfx::PShaderBuffer uniformBuffer) override;
virtual void updateBuffer(uint32 binding, Gfx::PIndexBuffer uniformBuffer) override;
virtual void updateBuffer(uint32 binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) override;
virtual void updateSampler(uint32 binding, Gfx::PSampler samplerState) override;
virtual void updateSampler(uint32 binding, uint32 dstArrayIndex, Gfx::PSampler samplerState) override;
virtual void updateTexture(uint32 binding, Gfx::PTexture texture, Gfx::PSampler sampler = nullptr) override;
virtual void updateTexture(uint32 binding, uint32 dstArrayIndex, Gfx::PTexture texture) override;
virtual void updateTextureArray(uint32 binding, Array<Gfx::PTexture2D> texture) override;
virtual void updateSamplerArray(uint32 binding, Array<Gfx::PSampler> samplers) override;
virtual void updateAccelerationStructure(uint32 binding, Gfx::PTopLevelAS as) override;
constexpr const Array<Array<MTL::Resource*>>& getBoundResources() const { return boundResources; }
constexpr const Array<MTL::Resource*>& getBoundResources() const { return boundResources; }
MTL::Buffer* getArgumentBuffer() const { return argumentBuffer; }
private:
PGraphics graphics;
PDescriptorPool owner;
Array<Array<MTL::Resource*>> boundResources;
Array<MTL::Resource*> boundResources;
MTL::ArgumentEncoder* encoder;
MTL::Buffer* argumentBuffer = nullptr;
};
+79 -25
View File
@@ -4,6 +4,7 @@
#include "Foundation/NSArray.hpp"
#include "Foundation/NSObject.hpp"
#include "Graphics/Descriptor.h"
#include "Graphics/Enums.h"
#include "Graphics/Initializer.h"
#include "Graphics/Metal/Resources.h"
#include "Graphics/Metal/Shader.h"
@@ -15,8 +16,10 @@
#include "Metal/MTLTexture.hpp"
#include "Texture.h"
#include <CRC.h>
#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/Foundation.h>
#include <iostream>
#include <stdexcept>
using namespace Seele;
using namespace Seele::Metal;
@@ -29,19 +32,68 @@ void DescriptorLayout::create() {
pool = new DescriptorPool(graphics, this);
hash = CRC::Calculate(descriptorBindings.data(), sizeof(Gfx::DescriptorBinding) * descriptorBindings.size(), CRC::CRC_32());
MTL::ArgumentDescriptor** objects = new MTL::ArgumentDescriptor*[descriptorBindings.size()];
for(uint32 i = 0; i < descriptorBindings.size(); ++i) {
for (uint32 i = 0; i < descriptorBindings.size(); ++i) {
objects[i] = MTL::ArgumentDescriptor::alloc()->init();
objects[i]->setIndex(i);
objects[i]->setAccess(cast(descriptorBindings[i].access));
objects[i]->setArrayLength(descriptorBindings[i].descriptorCount);
objects[i]->setDataType(MTL::DataTypeStruct);
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_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:
dataType = MTL::DataTypePointer;
break;
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);
}
arguments = NS::Array::array((NS::Object**)objects, descriptorBindings.size());
}
MTL::ArgumentEncoder* DescriptorLayout::createEncoder() {
return graphics->getDevice()->newArgumentEncoder(arguments);
}
MTL::ArgumentEncoder* DescriptorLayout::createEncoder() { return graphics->getDevice()->newArgumentEncoder(arguments); }
DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout) : graphics(graphics), layout(layout) {}
@@ -65,10 +117,10 @@ void DescriptorPool::reset() {}
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner)
: Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics), graphics(graphics), owner(owner) {
encoder = owner->getLayout()->createEncoder();
argumentBuffer = graphics->getDevice()->newBuffer(encoder->encodedLength(), 0);
encoder->setArgumentBuffer(argumentBuffer, 0);
boundResources.resize(owner->getLayout()->getBindings().size());
for (uint32 i = 0; i < boundResources.size(); ++i) {
boundResources[i].resize(owner->getLayout()->getBindings()[i].descriptorCount);
}
}
DescriptorSet::~DescriptorSet() {}
@@ -76,38 +128,40 @@ DescriptorSet::~DescriptorSet() {}
void DescriptorSet::writeChanges() {}
void DescriptorSet::updateBuffer(uint32 binding, Gfx::PUniformBuffer uniformBuffer) {
boundResources[binding][0] = uniformBuffer.cast<UniformBuffer>()->getHandle();
PUniformBuffer buffer = uniformBuffer.cast<UniformBuffer>();
encoder->setBuffer(buffer->getHandle(), 0, binding);
boundResources[binding] = buffer->getHandle();
}
void DescriptorSet::updateBuffer(uint32 binding, Gfx::PShaderBuffer uniformBuffer) {
boundResources[binding][0] = uniformBuffer.cast<ShaderBuffer>()->getHandle();
PShaderBuffer buffer = uniformBuffer.cast<ShaderBuffer>();
encoder->setBuffer(buffer->getHandle(), 0, binding);
boundResources[binding] = buffer->getHandle();
}
void DescriptorSet::updateBuffer(uint32 binding, Gfx::PIndexBuffer uniformBuffer) {
boundResources[binding][0] = uniformBuffer.cast<IndexBuffer>()->getHandle();
}
void DescriptorSet::updateBuffer(uint32 binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) {
boundResources[binding][0] = uniformBuffer.cast<ShaderBuffer>()->getHandle();
PIndexBuffer buffer = uniformBuffer.cast<IndexBuffer>();
encoder->setBuffer(buffer->getHandle(), 0, binding);
boundResources[binding] = buffer->getHandle();
}
void DescriptorSet::updateSampler(uint32 binding, Gfx::PSampler samplerState) {
boundResources[binding][0] = nullptr; // Samplers are not resources??????
PSampler sampler = samplerState.cast<Sampler>();
encoder->setSamplerState(sampler->getHandle(), binding);
boundResources[binding] = nullptr; // Samplers are not resources??????
}
void DescriptorSet::updateSampler(uint32 binding, uint32 dstArrayIndex, Gfx::PSampler samplerState) {
boundResources[binding][dstArrayIndex] = nullptr;
void DescriptorSet::updateTexture(uint32 binding, Gfx::PTexture texture, Gfx::PSampler sampler) {
PTextureBase base = texture.cast<TextureBase>();
encoder->setTexture(base->getHandle()->texture, binding);
boundResources[binding] = base->getHandle()->texture;
}
void DescriptorSet::updateTexture(uint32 binding, Gfx::PTexture texture, Gfx::PSampler sampler) {}
void DescriptorSet::updateTextureArray(uint32 binding, Array<Gfx::PTexture2D> texture) { assert(false && "TODO"); }
void DescriptorSet::updateTexture(uint32 binding, uint32 dstArrayIndex, Gfx::PTexture texture) {}
void DescriptorSet::updateSamplerArray(uint32 binding, Array<Gfx::PSampler> samplers) { assert(false && "TODO"); }
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) {}
void DescriptorSet::updateAccelerationStructure(uint32 binding, Gfx::PTopLevelAS as) { assert(false && "TODO"); }
PipelineLayout::PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout)
: Gfx::PipelineLayout(name, baseLayout), graphics(graphics) {}
+4 -4
View File
@@ -121,7 +121,7 @@ Gfx::PGraphicsPipeline Graphics::createGraphicsPipeline(Gfx::MeshPipelineCreateI
Gfx::PComputePipeline Graphics::createComputePipeline(Gfx::ComputePipelineCreateInfo createInfo) {
return cache->createPipeline(std::move(createInfo));
}
Gfx::PRayTracingPipeline Graphics::createRayTracingPipeline(Gfx::RayTracingPipelineCreateInfo createInfo) { return nullptr; }
Gfx::PRayTracingPipeline Graphics::createRayTracingPipeline(Gfx::RayTracingPipelineCreateInfo) { return nullptr; }
Gfx::OSampler Graphics::createSampler(const SamplerCreateInfo& createInfo) { return new Sampler(this, createInfo); }
@@ -143,16 +143,16 @@ Gfx::OTimestampQuery Graphics::createTimestampQuery(uint64 numTimestamps, const
return new TimestampQuery(this, name, numTimestamps);
}
void Graphics::resolveTexture(Gfx::PTexture source, Gfx::PTexture destination) {}
void Graphics::resolveTexture(Gfx::PTexture, Gfx::PTexture) {}
void Graphics::copyTexture(Gfx::PTexture src, Gfx::PTexture dst) {}
void Graphics::copyTexture(Gfx::PTexture, Gfx::PTexture) {}
// Ray Tracing
Gfx::OBottomLevelAS Graphics::createBottomLevelAccelerationStructure(const Gfx::BottomLevelASCreateInfo& createInfo) { return nullptr; }
Gfx::OTopLevelAS Graphics::createTopLevelAccelerationStructure(const Gfx::TopLevelASCreateInfo& createInfo) { return nullptr; }
void Graphics::buildBottomLevelAccelerationStructures(Array<Gfx::PBottomLevelAS> data) {}
void Graphics::buildBottomLevelAccelerationStructures(Array<Gfx::PBottomLevelAS>) {}
Gfx::ORayGenShader Graphics::createRayGenShader(const ShaderCreateInfo& createInfo) {
ORayGenShader shader = new RayGenShader(this);
+145 -179
View File
@@ -14,205 +14,171 @@
#include "Metal/MTLVertexDescriptor.hpp"
#include "Shader.h"
#include "Texture.h"
#include <Foundation/Foundation.h>
#include <iostream>
using namespace Seele;
using namespace Seele::Metal;
PipelineCache::PipelineCache(PGraphics graphics, const std::string &name)
: graphics(graphics), cacheFile(name) {}
PipelineCache::PipelineCache(PGraphics graphics, const std::string& name) : graphics(graphics), cacheFile(name) {}
PipelineCache::~PipelineCache() {}
PGraphicsPipeline
PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo createInfo) {
PPipelineLayout layout =
Gfx::PPipelineLayout(createInfo.pipelineLayout).cast<PipelineLayout>();
PGraphicsPipeline PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo createInfo) {
PPipelineLayout layout = Gfx::PPipelineLayout(createInfo.pipelineLayout).cast<PipelineLayout>();
MTL::RenderPipelineDescriptor *pipelineDescriptor =
MTL::RenderPipelineDescriptor::alloc()->init();
MTL::RenderPipelineDescriptor* pipelineDescriptor = MTL::RenderPipelineDescriptor::alloc()->init();
MTL::VertexDescriptor *vertexDescriptor =
pipelineDescriptor->vertexDescriptor()->init();
MTL::VertexAttributeDescriptorArray *attributes =
vertexDescriptor->attributes()->init();
if (createInfo.vertexInput != nullptr) {
const auto &vertexInfo = createInfo.vertexInput->getInfo();
for (size_t attr = 0; attr < vertexInfo.attributes.size(); ++attr) {
MTL::VertexAttributeDescriptor *attribute =
attributes->object(attr + METAL_VERTEXATTRIBUTE_OFFSET)->init();
attribute->setBufferIndex(vertexInfo.attributes[attr].binding +
METAL_VERTEXBUFFER_OFFSET);
switch (vertexInfo.attributes[attr].format) {
case Gfx::SE_FORMAT_R32G32B32_SFLOAT:
attribute->setFormat(MTL::VertexFormatFloat3);
MTL::VertexDescriptor* vertexDescriptor = pipelineDescriptor->vertexDescriptor()->init();
MTL::VertexAttributeDescriptorArray* attributes = vertexDescriptor->attributes()->init();
if (createInfo.vertexInput != nullptr) {
const auto& vertexInfo = createInfo.vertexInput->getInfo();
for (size_t attr = 0; attr < vertexInfo.attributes.size(); ++attr) {
MTL::VertexAttributeDescriptor* attribute = attributes->object(attr)->init();
attribute->setBufferIndex(vertexInfo.attributes[attr].binding);
switch (vertexInfo.attributes[attr].format) {
case Gfx::SE_FORMAT_R32G32B32_SFLOAT:
attribute->setFormat(MTL::VertexFormatFloat3);
break;
default:
throw std::logic_error("TODO");
}
attribute->setOffset(vertexInfo.attributes[attr].offset);
}
MTL::VertexBufferLayoutDescriptorArray* bufferLayout = vertexDescriptor->layouts()->init();
for (size_t binding = 0; binding < vertexInfo.bindings.size(); ++binding) {
MTL::VertexBufferLayoutDescriptor* buffer = bufferLayout->object(binding)->init();
buffer->setStride(vertexInfo.bindings[binding].stride);
buffer->setStepRate(1);
switch (vertexInfo.bindings[binding].inputRate) {
case Gfx::SE_VERTEX_INPUT_RATE_VERTEX:
buffer->setStepFunction(MTL::VertexStepFunctionPerVertex);
break;
case Gfx::SE_VERTEX_INPUT_RATE_INSTANCE:
buffer->setStepFunction(MTL::VertexStepFunctionPerInstance);
break;
}
}
}
pipelineDescriptor->setVertexDescriptor(vertexDescriptor);
pipelineDescriptor->setVertexFunction(createInfo.vertexShader.cast<VertexShader>()->getFunction());
if (createInfo.fragmentShader != nullptr) {
pipelineDescriptor->setFragmentFunction(createInfo.fragmentShader.cast<FragmentShader>()->getFunction());
}
pipelineDescriptor->setInputPrimitiveTopology(cast(createInfo.topology));
if (createInfo.renderPass->getLayout().depthAttachment.getTexture() != nullptr) {
pipelineDescriptor->setDepthAttachmentPixelFormat(
cast(createInfo.renderPass->getLayout().depthAttachment.getTexture().cast<Texture2D>()->getFormat()));
}
pipelineDescriptor->setAlphaToCoverageEnabled(createInfo.multisampleState.alphaCoverageEnable);
pipelineDescriptor->setAlphaToOneEnabled(createInfo.multisampleState.alphaToOneEnable);
pipelineDescriptor->setRasterSampleCount(createInfo.multisampleState.samples);
pipelineDescriptor->setRasterizationEnabled(!createInfo.rasterizationState.rasterizerDiscardEnable);
uint32 hash = pipelineDescriptor->hash();
if (graphicsPipelines.contains(hash)) {
return graphicsPipelines[hash];
}
MTL::PrimitiveType type = MTL::PrimitiveTypeTriangle;
switch (createInfo.topology) {
case Gfx::SE_PRIMITIVE_TOPOLOGY_POINT_LIST:
type = MTL::PrimitiveTypePoint;
break;
default:
throw std::logic_error("TODO");
}
attribute->setOffset(vertexInfo.attributes[attr].offset);
case Gfx::SE_PRIMITIVE_TOPOLOGY_LINE_LIST:
type = MTL::PrimitiveTypeLine;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_LINE_STRIP:
type = MTL::PrimitiveTypeLineStrip;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
type = MTL::PrimitiveTypeTriangle;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
type = MTL::PrimitiveTypeTriangleStrip;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
type = MTL::PrimitiveTypeTriangle;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
type = MTL::PrimitiveTypeLine;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
type = MTL::PrimitiveTypeLineStrip;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
type = MTL::PrimitiveTypeTriangle;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
type = MTL::PrimitiveTypeTriangleStrip;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_PATCH_LIST:
type = MTL::PrimitiveTypeTriangle;
break;
}
NS::Error* error;
graphicsPipelines[hash] = new GraphicsPipeline(
graphics, type, graphics->getDevice()->newRenderPipelineState(pipelineDescriptor, &error), std::move(createInfo.pipelineLayout));
if (error) {
std::cout << error->localizedDescription()->cString(NS::ASCIIStringEncoding) << std::endl;
assert(false);
}
MTL::VertexBufferLayoutDescriptorArray *bufferLayout =
vertexDescriptor->layouts()->init();
for (size_t binding = 0; binding < vertexInfo.bindings.size(); ++binding) {
MTL::VertexBufferLayoutDescriptor *buffer =
bufferLayout->object(binding + METAL_VERTEXBUFFER_OFFSET)->init();
buffer->setStride(vertexInfo.bindings[binding].stride);
buffer->setStepRate(1);
switch (vertexInfo.bindings[binding].inputRate) {
case Gfx::SE_VERTEX_INPUT_RATE_VERTEX:
buffer->setStepFunction(MTL::VertexStepFunctionPerVertex);
break;
case Gfx::SE_VERTEX_INPUT_RATE_INSTANCE:
buffer->setStepFunction(MTL::VertexStepFunctionPerInstance);
break;
}
pipelineDescriptor->release();
return graphicsPipelines[hash];
}
PGraphicsPipeline PipelineCache::createPipeline(Gfx::MeshPipelineCreateInfo createInfo) {
MTL::MeshRenderPipelineDescriptor* pipelineDescriptor = MTL::MeshRenderPipelineDescriptor::alloc()->init();
pipelineDescriptor->setMeshFunction(createInfo.meshShader.cast<MeshShader>()->getFunction());
if (createInfo.taskShader != nullptr) {
pipelineDescriptor->setObjectFunction(createInfo.taskShader.cast<TaskShader>()->getFunction());
}
}
pipelineDescriptor->setVertexDescriptor(vertexDescriptor);
if (createInfo.fragmentShader != nullptr) {
pipelineDescriptor->setFragmentFunction(createInfo.fragmentShader.cast<FragmentShader>()->getFunction());
}
if (createInfo.renderPass->getLayout().depthAttachment.getTexture() != nullptr) {
pipelineDescriptor->setDepthAttachmentPixelFormat(
cast(createInfo.renderPass->getLayout().depthAttachment.getTexture().cast<Texture2D>()->getFormat()));
}
pipelineDescriptor->setAlphaToCoverageEnabled(createInfo.multisampleState.alphaCoverageEnable);
pipelineDescriptor->setAlphaToOneEnabled(createInfo.multisampleState.alphaToOneEnable);
pipelineDescriptor->setRasterSampleCount(createInfo.multisampleState.samples);
pipelineDescriptor->setRasterizationEnabled(!createInfo.rasterizationState.rasterizerDiscardEnable);
pipelineDescriptor->setVertexFunction(
createInfo.vertexShader.cast<VertexShader>()->getFunction());
if (createInfo.fragmentShader != nullptr) {
pipelineDescriptor->setFragmentFunction(
createInfo.fragmentShader.cast<FragmentShader>()->getFunction());
}
pipelineDescriptor->setInputPrimitiveTopology(cast(createInfo.topology));
if (createInfo.renderPass->getLayout().depthAttachment.getTexture() !=
nullptr) {
pipelineDescriptor->setDepthAttachmentPixelFormat(
cast(createInfo.renderPass->getLayout()
.depthAttachment.getTexture()
.cast<Texture2D>()
->getFormat()));
}
pipelineDescriptor->setAlphaToCoverageEnabled(
createInfo.multisampleState.alphaCoverageEnable);
pipelineDescriptor->setAlphaToOneEnabled(
createInfo.multisampleState.alphaToOneEnable);
pipelineDescriptor->setRasterSampleCount(createInfo.multisampleState.samples);
pipelineDescriptor->setRasterizationEnabled(
!createInfo.rasterizationState.rasterizerDiscardEnable);
uint32 hash = pipelineDescriptor->hash();
uint32 hash = pipelineDescriptor->hash();
if (graphicsPipelines.contains(hash)) {
return graphicsPipelines[hash];
}
NS::Error* error = nullptr;
MTL::AutoreleasedRenderPipelineReflection reflection;
graphicsPipelines[hash] = new GraphicsPipeline(
graphics, MTL::PrimitiveTypeTriangle,
graphics->getDevice()->newRenderPipelineState(pipelineDescriptor, MTL::PipelineOptionNone, &reflection, &error),
std::move(createInfo.pipelineLayout));
if (graphicsPipelines.contains(hash)) {
if (error) {
std::cout << error->debugDescription()->utf8String() << std::endl;
}
pipelineDescriptor->release();
return graphicsPipelines[hash];
}
MTL::PrimitiveType type = MTL::PrimitiveTypeTriangle;
switch (createInfo.topology) {
case Gfx::SE_PRIMITIVE_TOPOLOGY_POINT_LIST:
type = MTL::PrimitiveTypePoint;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_LINE_LIST:
type = MTL::PrimitiveTypeLine;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_LINE_STRIP:
type = MTL::PrimitiveTypeLineStrip;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
type = MTL::PrimitiveTypeTriangle;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
type = MTL::PrimitiveTypeTriangleStrip;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
type = MTL::PrimitiveTypeTriangle;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
type = MTL::PrimitiveTypeLine;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
type = MTL::PrimitiveTypeLineStrip;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
type = MTL::PrimitiveTypeTriangle;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
type = MTL::PrimitiveTypeTriangleStrip;
break;
case Gfx::SE_PRIMITIVE_TOPOLOGY_PATCH_LIST:
type = MTL::PrimitiveTypeTriangle;
break;
}
NS::Error *error;
graphicsPipelines[hash] = new GraphicsPipeline(
graphics, type,
graphics->getDevice()->newRenderPipelineState(pipelineDescriptor, &error),
std::move(createInfo.pipelineLayout));
if (error) {
std::cout << error->localizedDescription()->cString(NS::ASCIIStringEncoding)
<< std::endl;
assert(false);
}
pipelineDescriptor->release();
return graphicsPipelines[hash];
}
PGraphicsPipeline
PipelineCache::createPipeline(Gfx::MeshPipelineCreateInfo createInfo) {
MTL::MeshRenderPipelineDescriptor *pipelineDescriptor =
MTL::MeshRenderPipelineDescriptor::alloc()->init();
PComputePipeline PipelineCache::createPipeline(Gfx::ComputePipelineCreateInfo createInfo) {
PComputeShader shader = createInfo.computeShader.cast<ComputeShader>();
uint32 hash = shader->getShaderHash();
if (computePipelines.contains(hash)) {
return computePipelines[hash];
}
pipelineDescriptor->setMeshFunction(
createInfo.meshShader.cast<MeshShader>()->getFunction());
if (createInfo.taskShader != nullptr) {
pipelineDescriptor->setObjectFunction(
createInfo.taskShader.cast<TaskShader>()->getFunction());
}
if (createInfo.fragmentShader != nullptr) {
pipelineDescriptor->setFragmentFunction(
createInfo.fragmentShader.cast<FragmentShader>()->getFunction());
}
if (createInfo.renderPass->getLayout().depthAttachment.getTexture() !=
nullptr) {
pipelineDescriptor->setDepthAttachmentPixelFormat(
cast(createInfo.renderPass->getLayout()
.depthAttachment.getTexture()
.cast<Texture2D>()
->getFormat()));
}
pipelineDescriptor->setAlphaToCoverageEnabled(
createInfo.multisampleState.alphaCoverageEnable);
pipelineDescriptor->setAlphaToOneEnabled(
createInfo.multisampleState.alphaToOneEnable);
pipelineDescriptor->setRasterSampleCount(createInfo.multisampleState.samples);
pipelineDescriptor->setRasterizationEnabled(
!createInfo.rasterizationState.rasterizerDiscardEnable);
uint32 hash = pipelineDescriptor->hash();
if (graphicsPipelines.contains(hash)) {
return graphicsPipelines[hash];
}
NS::Error *error = nullptr;
MTL::AutoreleasedRenderPipelineReflection reflection;
graphicsPipelines[hash] = new GraphicsPipeline(
graphics, MTL::PrimitiveTypeTriangle,
graphics->getDevice()->newRenderPipelineState(
pipelineDescriptor, MTL::PipelineOptionNone, &reflection, &error),
std::move(createInfo.pipelineLayout));
pipelineDescriptor->release();
return graphicsPipelines[hash];
}
PComputePipeline
PipelineCache::createPipeline(Gfx::ComputePipelineCreateInfo createInfo) {
PComputeShader shader = createInfo.computeShader.cast<ComputeShader>();
uint32 hash = shader->getShaderHash();
if (computePipelines.contains(hash)) {
NS::Error* error;
computePipelines[hash] = new ComputePipeline(graphics, graphics->getDevice()->newComputePipelineState(shader->getFunction(), &error),
std::move(createInfo.pipelineLayout));
assert(!error);
return computePipelines[hash];
}
NS::Error *error;
computePipelines[hash] =
new ComputePipeline(graphics,
graphics->getDevice()->newComputePipelineState(
shader->getFunction(), &error),
std::move(createInfo.pipelineLayout));
assert(!error);
return computePipelines[hash];
}
+1 -7
View File
@@ -40,16 +40,10 @@ class TimestampQuery : public Gfx::TimestampQuery, public QueryPool {
public:
TimestampQuery(PGraphics graphics, const std::string& name, uint32 numTimestamps);
virtual ~TimestampQuery();
virtual void begin() override;
virtual void write(Gfx::SePipelineStageFlagBits stage, const std::string& name = "") override;
virtual void end() override;
virtual Array<Gfx::Timestamp> getResults() override;
virtual Gfx::Timestamp getResult() override;
private:
uint64 wrapping = 0;
uint64 lastMeasure = 0;
uint32 numTimestamps = 0;
uint32 currentTimestamp = 0;
Array<std::string> pendingTimestamps;
};
DEFINE_REF(TimestampQuery)
+15 -27
View File
@@ -4,28 +4,23 @@
#include "Containers/Array.h"
#include "Enums.h"
#include "Graphics.h"
#include "Graphics/Query.h"
#include <vulkan/vulkan_core.h>
using namespace Seele;
using namespace Seele::Metal;
QueryPool::QueryPool(PGraphics graphics, const std::string& name)
: graphics(graphics) {
}
QueryPool::QueryPool(PGraphics graphics, const std::string&) : graphics(graphics) {}
QueryPool::~QueryPool() { }
QueryPool::~QueryPool() {}
void QueryPool::begin() {
}
void QueryPool::begin() {}
void QueryPool::end() {
}
void QueryPool::end() {}
void QueryPool::getQueryResults(Array<uint64>& results) {
}
void QueryPool::getQueryResults(Array<uint64>&) {}
OcclusionQuery::OcclusionQuery(PGraphics graphics, const std::string& name)
: QueryPool(graphics, name) {}
OcclusionQuery::OcclusionQuery(PGraphics graphics, const std::string& name) : QueryPool(graphics, name) {}
OcclusionQuery::~OcclusionQuery() {}
@@ -41,8 +36,7 @@ Gfx::OcclusionResult OcclusionQuery::getResults() {
};
}
PipelineStatisticsQuery::PipelineStatisticsQuery(PGraphics graphics, const std::string& name)
: QueryPool(graphics, name) {}
PipelineStatisticsQuery::PipelineStatisticsQuery(PGraphics graphics, const std::string& name) : QueryPool(graphics, name) {}
PipelineStatisticsQuery::~PipelineStatisticsQuery() {}
@@ -66,21 +60,15 @@ Gfx::PipelineStatisticsResult PipelineStatisticsQuery::getResults() {
};
}
TimestampQuery::TimestampQuery(PGraphics graphics, const std::string& name, uint32 numTimestamps)
: QueryPool(graphics, name), numTimestamps(numTimestamps) {
}
TimestampQuery::TimestampQuery(PGraphics graphics, const std::string& name, uint32) : QueryPool(graphics, name) {}
TimestampQuery::~TimestampQuery() {}
void TimestampQuery::begin() {
currentTimestamp = 0;
}
void TimestampQuery::write(Gfx::SePipelineStageFlagBits, const std::string&) {}
void TimestampQuery::write(Gfx::SePipelineStageFlagBits stage, const std::string& name) {
}
void TimestampQuery::end() {
}
Array<Gfx::Timestamp> TimestampQuery::getResults() {
Gfx::Timestamp TimestampQuery::getResult() {
return Gfx::Timestamp{
.name = "Test",
.time = 0,
};
}
+3 -3
View File
@@ -25,10 +25,10 @@ Shader::~Shader() {
void Shader::create(const ShaderCreateInfo& createInfo) {
auto [kernelBlob, entryPoint] = generateShader(createInfo);
std::ofstream test("test.metal");
test.write((char*)kernelBlob->getBufferPointer(), kernelBlob->getBufferSize());
test.close();
hash = CRC::Calculate(kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), CRC::CRC_32());
std::ofstream test("test.metal");
test.write((const char*)kernelBlob->getBufferPointer(), kernelBlob->getBufferSize());
test.close();
NS::Error* error;
MTL::CompileOptions* options = MTL::CompileOptions::alloc()->init();
library = graphics->getDevice()->newLibrary(NS::String::string((char*)kernelBlob->getBufferPointer(), NS::ASCIIStringEncoding), options,
+4 -1
View File
@@ -88,7 +88,10 @@ void TextureBase::generateMipmaps() { handle->generateMipmaps(); }
Texture2D::Texture2D(PGraphics graphics, const TextureCreateInfo& createInfo, MTL::Texture* existingImage)
: Gfx::Texture2D(graphics->getFamilyMapping()),
TextureBase(graphics, createInfo.elements > 1 ? MTL::TextureType2DArray : MTL::TextureType2D, createInfo, existingImage) {}
TextureBase(graphics,
createInfo.elements > 1 ? (createInfo.samples > 1 ? MTL::TextureType2DMultisampleArray : MTL::TextureType2DArray)
: (createInfo.samples > 1 ? MTL::TextureType2DMultisample : MTL::TextureType2D),
createInfo, existingImage) {}
Texture2D::~Texture2D() {}
+4
View File
@@ -90,6 +90,10 @@ Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo) : graphic
metalLayer.drawableSize = CGSizeMake(createInfo.width, createInfo.height);
metalWindow.contentView.layer = metalLayer;
metalWindow.contentView.wantsLayer = YES;
framebufferFormat = Gfx::SE_FORMAT_R8G8B8A8_UNORM;
drawable = (__bridge CA::MetalDrawable*)[metalLayer nextDrawable];
createBackBuffer();
}
Window::~Window() { glfwDestroyWindow(static_cast<GLFWwindow*>(windowHandle)); }