Extraer shaders a archivos .metal separados para mejor organización

- Crear directorio data/shaders/ para organizar todos los shaders MSL
- Extraer shaders embebidos a archivos individuales:
  * background.metal - Shader de fondo degradado
  * triangle.metal - Shader de triángulo multicolor
  * sprite.metal - Shader de sprites con vertex color tinting
  * crt.metal - Shader CRT post-processing completo
- Modificar main.cpp para cargar shaders desde archivos:
  * Usar stringWithContentsOfFile para leer código fuente
  * Compilar dinámicamente con newLibraryWithSource
  * Manejo robusto de errores de lectura y compilación
- Eliminar 351 líneas de strings embebidos de main.cpp
- Mantener funcionalidad completa: CRT + sprites + fondo + triángulo

Beneficios:
- Shaders editables sin recompilar ejecutable
- Mejor organización y mantenimiento del código
- Syntax highlighting completo en editores
- Reutilización de shaders en otros proyectos
- Desarrollo más ágil de efectos visuales

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-28 18:40:28 +02:00
parent 909635d76d
commit 7cccedb5fb
5 changed files with 309 additions and 351 deletions

View File

@@ -0,0 +1,40 @@
#include <metal_stdlib>
using namespace metal;
struct VertexOut {
float4 position [[position]];
float4 color;
};
vertex VertexOut background_vertex_main(uint vertexID [[vertex_id]]) {
VertexOut out;
// Quad de pantalla completa en coordenadas NDC
float2 positions[6] = {
float2(-1.0, -1.0), // Bottom left
float2( 1.0, -1.0), // Bottom right
float2(-1.0, 1.0), // Top left
float2( 1.0, -1.0), // Bottom right
float2( 1.0, 1.0), // Top right
float2(-1.0, 1.0) // Top left
};
// Gradiente de púrpura oscuro arriba a azul cyan abajo
float4 colors[6] = {
float4(0.2, 0.6, 0.8, 1.0), // Bottom left - cyan claro
float4(0.2, 0.6, 0.8, 1.0), // Bottom right - cyan claro
float4(0.3, 0.1, 0.5, 1.0), // Top left - púrpura oscuro
float4(0.2, 0.6, 0.8, 1.0), // Bottom right - cyan claro
float4(0.3, 0.1, 0.5, 1.0), // Top right - púrpura oscuro
float4(0.3, 0.1, 0.5, 1.0) // Top left - púrpura oscuro
};
out.position = float4(positions[vertexID], 0.0, 1.0);
out.color = colors[vertexID];
return out;
}
fragment float4 background_fragment_main(VertexOut in [[stage_in]]) {
return in.color;
}

158
data/shaders/crt.metal Normal file
View File

@@ -0,0 +1,158 @@
#include <metal_stdlib>
using namespace metal;
// Parámetros del CRT shader
#define CURVATURE_X 0.05
#define CURVATURE_Y 0.1
#define MASK_BRIGHTNESS 0.80
#define SCANLINE_WEIGHT 6.0
#define SCANLINE_GAP_BRIGHTNESS 0.12
#define BLOOM_FACTOR 3.5
#define INPUT_GAMMA 2.4
#define OUTPUT_GAMMA 2.2
// Features habilitadas
#define SCANLINES
#define MULTISAMPLE
#define GAMMA
// #define FAKE_GAMMA
// #define CURVATURE
// #define SHARPER
#define MASK_TYPE 2
struct CRTVertexOut {
float4 position [[position]];
float2 texCoord;
};
vertex CRTVertexOut crt_vertex_main(uint vertexID [[vertex_id]]) {
CRTVertexOut out;
// Fullscreen quad con coordenadas de textura
float2 positions[6] = {
float2(-1.0, -1.0), // Bottom left
float2( 1.0, -1.0), // Bottom right
float2(-1.0, 1.0), // Top left
float2( 1.0, -1.0), // Bottom right
float2( 1.0, 1.0), // Top right
float2(-1.0, 1.0) // Top left
};
float2 texCoords[6] = {
float2(0.0, 1.0), // Bottom left
float2(1.0, 1.0), // Bottom right
float2(0.0, 0.0), // Top left
float2(1.0, 1.0), // Bottom right
float2(1.0, 0.0), // Top right
float2(0.0, 0.0) // Top left
};
out.position = float4(positions[vertexID], 0.0, 1.0);
out.texCoord = texCoords[vertexID];
return out;
}
float CalcScanLineWeight(float dist) {
return max(1.0 - dist * dist * SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
}
float CalcScanLine(float dy, float filterWidth) {
float scanLineWeight = CalcScanLineWeight(dy);
#ifdef MULTISAMPLE
scanLineWeight += CalcScanLineWeight(dy - filterWidth);
scanLineWeight += CalcScanLineWeight(dy + filterWidth);
scanLineWeight *= 0.3333333;
#endif
return scanLineWeight;
}
fragment float4 crt_fragment_main(CRTVertexOut in [[stage_in]],
texture2d<float> sceneTexture [[texture(0)]],
sampler sceneSampler [[sampler(0)]]) {
float2 TextureSize = float2(320.0, 240.0); // Resolución virtual del CRT
float filterWidth = (768.0 / 240.0) / 3.0;
float2 texcoord = in.texCoord;
// Convertir a píxeles
float2 texcoordInPixels = texcoord * TextureSize;
#ifdef SHARPER
float2 tempCoord = floor(texcoordInPixels) + 0.5;
float2 coord = tempCoord / TextureSize;
float2 deltas = texcoordInPixels - tempCoord;
float scanLineWeight = CalcScanLine(deltas.y, filterWidth);
float2 signs = sign(deltas);
deltas.x *= 2.0;
deltas = deltas * deltas;
deltas.y = deltas.y * deltas.y;
deltas.x *= 0.5;
deltas.y *= 8.0;
deltas /= TextureSize;
deltas *= signs;
float2 tc = coord + deltas;
#else
float tempY = floor(texcoordInPixels.y) + 0.5;
float yCoord = tempY / TextureSize.y;
float dy = texcoordInPixels.y - tempY;
float scanLineWeight = CalcScanLine(dy, filterWidth);
float signY = sign(dy);
dy = dy * dy;
dy = dy * dy;
dy *= 8.0;
dy /= TextureSize.y;
dy *= signY;
float2 tc = float2(texcoord.x, yCoord + dy);
#endif
// Samplear la textura de escena
float3 colour = sceneTexture.sample(sceneSampler, tc).rgb;
#ifdef SCANLINES
#ifdef GAMMA
#ifdef FAKE_GAMMA
colour = colour * colour;
#else
colour = pow(colour, float3(INPUT_GAMMA));
#endif
#endif
scanLineWeight *= BLOOM_FACTOR;
colour *= scanLineWeight;
#ifdef GAMMA
#ifdef FAKE_GAMMA
colour = sqrt(colour);
#else
colour = pow(colour, float3(1.0 / OUTPUT_GAMMA));
#endif
#endif
#endif
// Shadow mask
#if MASK_TYPE == 0
return float4(colour, 1.0);
#else
#if MASK_TYPE == 1
float whichMask = fract((in.position.x * 1.0001) * 0.5);
float3 mask;
if (whichMask < 0.5) {
mask = float3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
} else {
mask = float3(1.0, MASK_BRIGHTNESS, 1.0);
}
#elif MASK_TYPE == 2
float whichMask = fract((in.position.x * 1.0001) * 0.3333333);
float3 mask = float3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
if (whichMask < 0.3333333) {
mask.x = 1.0;
} else if (whichMask < 0.6666666) {
mask.y = 1.0;
} else {
mask.z = 1.0;
}
#endif
return float4(colour * mask, 1.0);
#endif
}

29
data/shaders/sprite.metal Normal file
View File

@@ -0,0 +1,29 @@
#include <metal_stdlib>
using namespace metal;
struct SpriteVertexIn {
float2 position [[attribute(0)]];
float4 color [[attribute(1)]];
float2 texCoord [[attribute(2)]];
};
struct SpriteVertexOut {
float4 position [[position]];
float4 color;
float2 texCoord;
};
vertex SpriteVertexOut sprite_vertex_main(SpriteVertexIn in [[stage_in]]) {
SpriteVertexOut out;
out.position = float4(in.position, 0.0, 1.0);
out.color = in.color;
out.texCoord = in.texCoord;
return out;
}
fragment float4 sprite_fragment_main(SpriteVertexOut in [[stage_in]],
texture2d<float> spriteTexture [[texture(0)]],
sampler textureSampler [[sampler(0)]]) {
float4 textureColor = spriteTexture.sample(textureSampler, in.texCoord);
return textureColor * in.color; // Multiplicar textura por color de vértice para tinting
}

View File

@@ -0,0 +1,33 @@
#include <metal_stdlib>
using namespace metal;
struct VertexOut {
float4 position [[position]];
float4 color;
};
vertex VertexOut triangle_vertex_main(uint vertexID [[vertex_id]]) {
VertexOut out;
// Triángulo simple en coordenadas normalized device coordinates
float2 positions[3] = {
float2( 0.0, 0.5), // Top
float2(-0.5, -0.5), // Bottom left
float2( 0.5, -0.5) // Bottom right
};
float4 colors[3] = {
float4(1, 0, 0, 1), // Red
float4(0, 1, 0, 1), // Green
float4(0, 0, 1, 1) // Blue
};
out.position = float4(positions[vertexID], 0.0, 1.0);
out.color = colors[vertexID];
return out;
}
fragment float4 triangle_fragment_main(VertexOut in [[stage_in]]) {
return in.color;
}