Metal is impossible to debug, thanks apple

This commit is contained in:
Dynamitos
2024-04-14 11:35:37 +02:00
parent f5eb12a5de
commit dc2f0eac30
14 changed files with 325 additions and 138 deletions
+120 -10
View File
@@ -1,11 +1,18 @@
#include "PipelineCache.h"
#include "Descriptor.h"
#include "Enums.h"
#include "Foundation/NSError.hpp"
#include "Graphics.h"
#include "Shader.h"
#include "Graphics/Descriptor.h"
#include "Graphics/Enums.h"
#include "Graphics/Metal/Pipeline.h"
#include "Metal/MTLComputePipeline.hpp"
#include "Metal/MTLDevice.hpp"
#include "Metal/MTLRenderCommandEncoder.hpp"
#include "Metal/MTLRenderPipeline.hpp"
#include "Metal/MTLVertexDescriptor.hpp"
#include "Shader.h"
#include "Texture.h"
using namespace Seele;
using namespace Seele::Metal;
@@ -16,15 +23,12 @@ PipelineCache::~PipelineCache() {}
PGraphicsPipeline PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo createInfo) {
PPipelineLayout layout = Gfx::PPipelineLayout(createInfo.pipelineLayout).cast<PipelineLayout>();
uint32 hash = layout->getHash();
MTL::RenderPipelineDescriptor* pipelineDescriptor = MTL::RenderPipelineDescriptor::alloc()->init();
MTL::VertexDescriptor* vertexDescriptor = MTL::VertexDescriptor::alloc()->init();
MTL::VertexAttributeDescriptorArray* attributes = vertexDescriptor->attributes();
const auto& vertexInfo = createInfo.vertexInput->getInfo();
for (size_t attr = 0; attr < vertexInfo.attributes.size(); ++attr) {
MTL::VertexAttributeDescriptor* attribute = MTL::VertexAttributeDescriptor::alloc()->init();
attribute->setBufferIndex(vertexInfo.attributes[attr].binding);
@@ -40,7 +44,6 @@ PGraphicsPipeline PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo cr
}
MTL::VertexBufferLayoutDescriptorArray* bufferLayout = vertexDescriptor->layouts();
for (size_t binding = 0; binding < vertexInfo.bindings.size(); ++binding) {
MTL::VertexBufferLayoutDescriptor* buffer = MTL::VertexBufferLayoutDescriptor::alloc()->init();
buffer->setStride(vertexInfo.bindings[binding].stride);
@@ -55,16 +58,123 @@ PGraphicsPipeline PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo cr
}
bufferLayout->setObject(buffer, binding);
}
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;
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));
assert(!error);
vertexDescriptor->release();
pipelineDescriptor->release();
return graphicsPipelines[hash];
}
PGraphicsPipeline PipelineCache::createPipeline(Gfx::MeshPipelineCreateInfo createInfo) {}
PGraphicsPipeline PipelineCache::createPipeline(Gfx::MeshPipelineCreateInfo createInfo) {
MTL::MeshRenderPipelineDescriptor* pipelineDescriptor = MTL::MeshRenderPipelineDescriptor::alloc()->init();
PComputePipeline PipelineCache::createPipeline(Gfx::ComputePipelineCreateInfo createInfo) {}
pipelineDescriptor->setMeshFunction(createInfo.meshShader.cast<VertexShader>()->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];
}
graphics->getDevice()->newRenderPipelineState(
pipelineDescriptor, MTL::PipelineOptionNone,
[&](MTL::RenderPipelineState* state, MTL::RenderPipelineReflection*, NS::Error* error) {
assert(!error);
graphicsPipelines[hash] =
new GraphicsPipeline(graphics, MTL::PrimitiveTypeLine, state, 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)) {
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];
}