From 4bc19e8e43312825da93b140aa1cf0c7c8a64fb6 Mon Sep 17 00:00:00 2001
From: Samuliak <samuliak77@gmail.com>
Date: Tue, 2 Jul 2024 10:05:59 +0200
Subject: [PATCH] fix: issues with drawing

---
 include/renderer_mtl/renderer_mtl.hpp  |  3 +++
 src/core/renderer_mtl/renderer_mtl.cpp | 28 ++++++++++++++++++++------
 src/host_shaders/metal_shaders.metal   |  2 +-
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/include/renderer_mtl/renderer_mtl.hpp b/include/renderer_mtl/renderer_mtl.hpp
index bedde3f3..c33df63e 100644
--- a/include/renderer_mtl/renderer_mtl.hpp
+++ b/include/renderer_mtl/renderer_mtl.hpp
@@ -33,6 +33,9 @@ class RendererMTL final : public Renderer {
 	// HACK
 	MTL::Texture* topScreenTexture;
 
+	// Helpers
+	MTL::SamplerState* basicSampler;
+
 	// Pipelines
 	MTL::RenderPipelineState* displayPipeline;
 	MTL::RenderPipelineState* drawPipeline;
diff --git a/src/core/renderer_mtl/renderer_mtl.cpp b/src/core/renderer_mtl/renderer_mtl.cpp
index 8759446d..bb743005 100644
--- a/src/core/renderer_mtl/renderer_mtl.cpp
+++ b/src/core/renderer_mtl/renderer_mtl.cpp
@@ -28,11 +28,15 @@ void RendererMTL::display() {
 	MTL::RenderPassDescriptor* renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init();
 	MTL::RenderPassColorAttachmentDescriptor* colorAttachment = renderPassDescriptor->colorAttachments()->object(0);
 	colorAttachment->setTexture(drawable->texture());
-	colorAttachment->setLoadAction(MTL::LoadActionClear);
-	colorAttachment->setClearColor(MTL::ClearColor{1.0f, 0.0f, 0.0f, 1.0f});
+	colorAttachment->setLoadAction(MTL::LoadActionDontCare);
 	colorAttachment->setStoreAction(MTL::StoreActionStore);
 
 	MTL::RenderCommandEncoder* renderCommandEncoder = commandBuffer->renderCommandEncoder(renderPassDescriptor);
+	renderCommandEncoder->setRenderPipelineState(displayPipeline);
+	renderCommandEncoder->setFragmentTexture(topScreenTexture, 0);
+	renderCommandEncoder->setFragmentSamplerState(basicSampler, 0);
+	renderCommandEncoder->drawPrimitives(MTL::PrimitiveTypeTriangleStrip, NS::UInteger(0), NS::UInteger(4));
+
 	renderCommandEncoder->endEncoding();
 
 	commandBuffer->presentDrawable(drawable);
@@ -49,9 +53,18 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
 	commandQueue = device->newCommandQueue();
 
 	// HACK
-	MTL::TextureDescriptor* descriptor = MTL::TextureDescriptor::texture2DDescriptor(MTL::PixelFormatRGBA8Unorm, 400, 240, false);
+	MTL::TextureDescriptor* descriptor = MTL::TextureDescriptor::alloc()->init();
+	descriptor->setTextureType(MTL::TextureType2D);
+	descriptor->setPixelFormat(MTL::PixelFormatRGBA8Unorm);
+	descriptor->setWidth(400);
+	descriptor->setHeight(240);
+	descriptor->setUsage(MTL::TextureUsageRenderTarget | MTL::TextureUsageShaderRead);
 	topScreenTexture = device->newTexture(descriptor);
 
+	// Helpers
+	MTL::SamplerDescriptor* samplerDescriptor = MTL::SamplerDescriptor::alloc()->init();
+	basicSampler = device->newSamplerState(samplerDescriptor);
+
 	// -------- Pipelines --------
 
 	// Load shaders
@@ -73,8 +86,8 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
 	displayPipelineDescriptor->setVertexFunction(vertexDisplayFunction);
 	displayPipelineDescriptor->setFragmentFunction(fragmentDisplayFunction);
 	// HACK
-	auto* colorAttachment = displayPipelineDescriptor->colorAttachments()->object(0);
-	colorAttachment->setPixelFormat(topScreenTexture->pixelFormat());
+	auto* displayColorAttachment = displayPipelineDescriptor->colorAttachments()->object(0);
+	displayColorAttachment->setPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm_sRGB);
 
 	error = nullptr;
 	displayPipeline = device->newRenderPipelineState(displayPipelineDescriptor, &error);
@@ -90,7 +103,8 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
 	drawPipelineDescriptor->setVertexFunction(vertexDrawFunction);
 	drawPipelineDescriptor->setFragmentFunction(fragmentDrawFunction);
 	// HACK
-	colorAttachment->setPixelFormat(MTL::PixelFormatRGBA8Unorm);
+	auto* drawColorAttachment = drawPipelineDescriptor->colorAttachments()->object(0);
+	drawColorAttachment->setPixelFormat(topScreenTexture->pixelFormat());
 
 	// Vertex descriptor
 	// TODO: add all attributes
@@ -131,6 +145,8 @@ void RendererMTL::textureCopy(u32 inputAddr, u32 outputAddr, u32 totalBytes, u32
 void RendererMTL::drawVertices(PICA::PrimType primType, std::span<const PICA::Vertex> vertices) {
 	createCommandBufferIfNeeded();
 
+	std::cout << "DRAWING " << vertices.size() << " VERTICES" << std::endl;
+
 	// TODO: don't begin a new render pass every time
 	MTL::RenderPassDescriptor* renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init();
 	MTL::RenderPassColorAttachmentDescriptor* colorAttachment = renderPassDescriptor->colorAttachments()->object(0);
diff --git a/src/host_shaders/metal_shaders.metal b/src/host_shaders/metal_shaders.metal
index 011bc98e..38bab7d3 100644
--- a/src/host_shaders/metal_shaders.metal
+++ b/src/host_shaders/metal_shaders.metal
@@ -30,7 +30,7 @@ struct DrawVertexOut {
 
 vertex DrawVertexOut vertexDraw(DrawVertexIn in [[stage_in]]) {
 	DrawVertexOut out;
-	out.position = in.position;
+	out.position = float4(in.position.xy, 0.0, 1.0);  // HACK
 	out.color = in.color;
 
 	return out;