From d41e77491a7432f0f6a8ad3f56fc5ae53cb713c5 Mon Sep 17 00:00:00 2001
From: Samuliak <samuliak77@gmail.com>
Date: Tue, 2 Jul 2024 09:02:03 +0200
Subject: [PATCH] rewrite shaders

---
 CMakeLists.txt                         |  4 +--
 include/renderer_mtl/renderer_mtl.hpp  |  7 +++++
 src/core/renderer_mtl/renderer_mtl.cpp |  7 +++++
 src/host_shaders/metal_display.metal   | 16 ------------
 src/host_shaders/metal_shaders.metal   | 36 ++++++++++++++++++++++++++
 5 files changed, 52 insertions(+), 18 deletions(-)
 delete mode 100644 src/host_shaders/metal_display.metal
 create mode 100644 src/host_shaders/metal_shaders.metal

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7d46a6d6..30cb57ed 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -407,7 +407,7 @@ if(ENABLE_METAL AND APPLE)
 
     set(RENDERER_MTL_SOURCE_FILES src/core/renderer_mtl/metal_cpp_impl.cpp
         src/core/renderer_mtl/renderer_mtl.cpp
-        src/host_shaders/metal_display.metal
+        src/host_shaders/metal_shaders.metal
     )
 
     set(HEADER_FILES ${HEADER_FILES} ${RENDERER_MTL_INCLUDE_FILES})
@@ -419,7 +419,7 @@ if(ENABLE_METAL AND APPLE)
         resources_renderer_mtl
         NAMESPACE RendererMTL
         WHENCE "src/host_shaders/"
-        "src/host_shaders/metal_display.metal"
+        "src/host_shaders/metal_shaders.metal"
     )
 
     target_sources(AlberCore PRIVATE ${RENDERER_MTL_SOURCE_FILES})
diff --git a/include/renderer_mtl/renderer_mtl.hpp b/include/renderer_mtl/renderer_mtl.hpp
index 0acf67e7..d4cb1af3 100644
--- a/include/renderer_mtl/renderer_mtl.hpp
+++ b/include/renderer_mtl/renderer_mtl.hpp
@@ -29,4 +29,11 @@ class RendererMTL final : public Renderer {
 
 	MTL::Device* device;
 	MTL::CommandQueue* commandQueue;
+
+	// HACK
+	MTL::Texture* topScreenTexture;
+
+	// 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 25f337af..76cf3011 100644
--- a/src/core/renderer_mtl/renderer_mtl.cpp
+++ b/src/core/renderer_mtl/renderer_mtl.cpp
@@ -36,6 +36,13 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
 	device = MTL::CreateSystemDefaultDevice();
 	metalLayer->setDevice(device);
 	commandQueue = device->newCommandQueue();
+
+	// HACK
+	MTL::TextureDescriptor* descriptor = MTL::TextureDescriptor::texture2DDescriptor(MTL::PixelFormatRGBA8Unorm, 400, 240, false);
+	topScreenTexture = device->newTexture(descriptor);
+
+	// Pipelines
+	// TODO
 }
 
 void RendererMTL::clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control) {
diff --git a/src/host_shaders/metal_display.metal b/src/host_shaders/metal_display.metal
deleted file mode 100644
index 595ab5e4..00000000
--- a/src/host_shaders/metal_display.metal
+++ /dev/null
@@ -1,16 +0,0 @@
-struct VertexOut {
-    float4 position [[position]];
-    float2 uv;
-};
-
-vertex VertexOut vertexMain(uint vid [[vertex_id]]) {
-    VertexOut out;
-	out.uv = float2((vid << 1) & 2, vid & 2);
-	out.position = float4(out.uv * 2.0f + -1.0f, 0.0f, 1.0f);
-
-	return out;
-}
-
-fragment float4 fragmentMain(VertexOut in [[stage_in]], texture2d<float> tex [[texture(0)]], sampler samplr [[sampler(0)]]) {
-    return tex.sample(samplr, in.uv);
-}
diff --git a/src/host_shaders/metal_shaders.metal b/src/host_shaders/metal_shaders.metal
new file mode 100644
index 00000000..d4dc1557
--- /dev/null
+++ b/src/host_shaders/metal_shaders.metal
@@ -0,0 +1,36 @@
+struct DisplayVertexOut {
+	float4 position [[position]];
+	float2 uv;
+};
+
+vertex DisplayVertexOut vertexDisplay(uint vid [[vertex_id]]) {
+	DisplayVertexOut out;
+	out.uv = float2((vid << 1) & 2, vid & 2);
+	out.position = float4(out.uv * 2.0f + -1.0f, 0.0f, 1.0f);
+
+	return out;
+}
+
+fragment float4 fragmentDisplay(DisplayVertexOut in [[stage_in]], texture2d<float> tex [[texture(0)]], sampler samplr [[sampler(0)]]) {
+	return tex.sample(samplr, in.uv);
+}
+
+struct DrawVertexIn {
+	float4 position [[attribute(0)]];
+	float4 color [[attribute(2)]];
+};
+
+struct DrawVertexOut {
+	float4 position [[position]];
+	float4 color;
+};
+
+vertex DrawVertexOut vertexDraw(DrawVertexIn in [[stage_in]]) {
+	DrawVertexOut out;
+	out.position = in.position;
+	out.color = in.color;
+
+	return out;
+}
+
+fragment float4 fragmentDraw(DrawVertexOut in [[stage_in]]) { return in.color; }