create pipeline states

This commit is contained in:
Samuliak 2024-07-02 09:26:52 +02:00
parent 6123a4a604
commit 74c0df8b71
2 changed files with 63 additions and 1 deletions

View file

@ -45,9 +45,68 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
MTL::TextureDescriptor* descriptor = MTL::TextureDescriptor::texture2DDescriptor(MTL::PixelFormatRGBA8Unorm, 400, 240, false); MTL::TextureDescriptor* descriptor = MTL::TextureDescriptor::texture2DDescriptor(MTL::PixelFormatRGBA8Unorm, 400, 240, false);
topScreenTexture = device->newTexture(descriptor); topScreenTexture = device->newTexture(descriptor);
// Pipelines // -------- Pipelines --------
// Load shaders
auto mtlResources = cmrc::RendererMTL::get_filesystem(); auto mtlResources = cmrc::RendererMTL::get_filesystem();
auto shaderSource = mtlResources.open("metal_shaders.metal"); auto shaderSource = mtlResources.open("metal_shaders.metal");
std::string source(shaderSource.begin(), shaderSource.size());
MTL::CompileOptions* compileOptions = MTL::CompileOptions::alloc()->init();
NS::Error* error = nullptr;
MTL::Library* library = device->newLibrary(NS::String::string(source.c_str(), NS::ASCIIStringEncoding), compileOptions, &error);
if (error) {
Helpers::panic("Error loading shaders: %s", error->description()->cString(NS::ASCIIStringEncoding));
}
// Display
MTL::Function* vertexDisplayFunction = library->newFunction(NS::String::string("vertexDisplay", NS::ASCIIStringEncoding));
MTL::Function* fragmentDisplayFunction = library->newFunction(NS::String::string("fragmentDisplay", NS::ASCIIStringEncoding));
MTL::RenderPipelineDescriptor* displayPipelineDescriptor = MTL::RenderPipelineDescriptor::alloc()->init();
displayPipelineDescriptor->setVertexFunction(vertexDisplayFunction);
displayPipelineDescriptor->setFragmentFunction(fragmentDisplayFunction);
// HACK
auto* colorAttachment = displayPipelineDescriptor->colorAttachments()->object(0);
colorAttachment->setPixelFormat(topScreenTexture->pixelFormat());
error = nullptr;
displayPipeline = device->newRenderPipelineState(displayPipelineDescriptor, &error);
if (error) {
Helpers::panic("Error creating display pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding));
}
// Draw
MTL::Function* vertexDrawFunction = library->newFunction(NS::String::string("vertexDraw", NS::ASCIIStringEncoding));
MTL::Function* fragmentDrawFunction = library->newFunction(NS::String::string("fragmentDraw", NS::ASCIIStringEncoding));
MTL::RenderPipelineDescriptor* drawPipelineDescriptor = MTL::RenderPipelineDescriptor::alloc()->init();
drawPipelineDescriptor->setVertexFunction(vertexDrawFunction);
drawPipelineDescriptor->setFragmentFunction(fragmentDrawFunction);
// HACK
colorAttachment->setPixelFormat(MTL::PixelFormatRGBA8Unorm);
// Vertex descriptor
// TODO: add all attributes
MTL::VertexDescriptor* vertexDescriptor = MTL::VertexDescriptor::alloc()->init();
MTL::VertexAttributeDescriptor* positionAttribute = vertexDescriptor->attributes()->object(0);
positionAttribute->setFormat(MTL::VertexFormatFloat4);
positionAttribute->setOffset(0);
positionAttribute->setBufferIndex(0);
MTL::VertexAttributeDescriptor* colorAttribute = vertexDescriptor->attributes()->object(2);
colorAttribute->setFormat(MTL::VertexFormatFloat4);
colorAttribute->setOffset(16);
colorAttribute->setBufferIndex(0);
MTL::VertexBufferLayoutDescriptor* vertexBufferLayout = vertexDescriptor->layouts()->object(0);
vertexBufferLayout->setStride(32);
vertexBufferLayout->setStepFunction(MTL::VertexStepFunctionPerVertex);
vertexBufferLayout->setStepRate(1);
drawPipelineDescriptor->setVertexDescriptor(vertexDescriptor);
error = nullptr;
drawPipeline = device->newRenderPipelineState(drawPipelineDescriptor, &error);
if (error) {
Helpers::panic("Error creating draw pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding));
}
} }
void RendererMTL::clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control) { void RendererMTL::clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control) {

View file

@ -1,3 +1,6 @@
#include <metal_stdlib>
using namespace metal;
struct DisplayVertexOut { struct DisplayVertexOut {
float4 position [[position]]; float4 position [[position]];
float2 uv; float2 uv;