WIP: Metal shader backend para macOS
- Shaders MSL portados desde GLSL (vertex + fragment) - Estructura básica de MetalShader class - Device, command queue, pipeline y buffers creados - CMakeLists.txt actualizado con Metal frameworks - assets.txt incluye shaders .metal como opcionales PENDIENTE: - Implementar render() loop completo - Obtener MTLTexture desde SDL_Texture - Crear sampler state - Testing en macOS real Ver METAL_BACKEND_NOTES.md para detalles de implementación.
This commit is contained in:
152
data/shaders/crtpi_fragment.metal
Normal file
152
data/shaders/crtpi_fragment.metal
Normal file
@@ -0,0 +1,152 @@
|
||||
//
|
||||
// CRT-Pi Fragment Shader - Metal Shading Language
|
||||
// Portado desde GLSL a MSL para macOS
|
||||
//
|
||||
|
||||
#include <metal_stdlib>
|
||||
using namespace metal;
|
||||
|
||||
// Configuración (equivalente a los #define en GLSL)
|
||||
constant bool SCANLINES = true;
|
||||
constant bool MULTISAMPLE = true;
|
||||
constant bool GAMMA = true;
|
||||
constant bool FAKE_GAMMA = false;
|
||||
constant bool CURVATURE = false;
|
||||
constant bool SHARPER = false;
|
||||
constant int MASK_TYPE = 2; // 0=none, 1=green/magenta, 2=trinitron
|
||||
|
||||
constant float CURVATURE_X = 0.05;
|
||||
constant float CURVATURE_Y = 0.1;
|
||||
constant float MASK_BRIGHTNESS = 0.80;
|
||||
constant float SCANLINE_WEIGHT = 6.0;
|
||||
constant float SCANLINE_GAP_BRIGHTNESS = 0.12;
|
||||
constant float BLOOM_FACTOR = 3.5;
|
||||
constant float INPUT_GAMMA = 2.4;
|
||||
constant float OUTPUT_GAMMA = 2.2;
|
||||
|
||||
// Estructura de entrada (salida del vertex shader)
|
||||
struct VertexOut {
|
||||
float4 position [[position]];
|
||||
float2 texCoord;
|
||||
float filterWidth;
|
||||
// float2 screenScale; // Solo si CURVATURE está activo
|
||||
};
|
||||
|
||||
// Uniforms
|
||||
struct Uniforms {
|
||||
float2 textureSize;
|
||||
};
|
||||
|
||||
// Función para calcular el peso de la scanline
|
||||
float CalcScanLineWeight(float dist) {
|
||||
return max(1.0 - dist * dist * SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
|
||||
}
|
||||
|
||||
// Función para calcular scanline con multisampling
|
||||
float CalcScanLine(float dy, float filterWidth) {
|
||||
float scanLineWeight = CalcScanLineWeight(dy);
|
||||
if (MULTISAMPLE) {
|
||||
scanLineWeight += CalcScanLineWeight(dy - filterWidth);
|
||||
scanLineWeight += CalcScanLineWeight(dy + filterWidth);
|
||||
scanLineWeight *= 0.3333333;
|
||||
}
|
||||
return scanLineWeight;
|
||||
}
|
||||
|
||||
// Entry point del fragment shader
|
||||
fragment float4 fragment_main(VertexOut in [[stage_in]],
|
||||
texture2d<float> colorTexture [[texture(0)]],
|
||||
constant Uniforms& uniforms [[buffer(1)]],
|
||||
sampler textureSampler [[sampler(0)]]) {
|
||||
|
||||
float2 texcoord = in.texCoord;
|
||||
|
||||
// Si CURVATURE estuviera activo, aquí iría la función Distort()
|
||||
|
||||
float2 texcoordInPixels = texcoord * uniforms.textureSize;
|
||||
|
||||
float2 tc;
|
||||
float scanLineWeight;
|
||||
|
||||
if (!SHARPER) {
|
||||
// Modo normal (no SHARPER)
|
||||
float tempY = floor(texcoordInPixels.y) + 0.5;
|
||||
float yCoord = tempY / uniforms.textureSize.y;
|
||||
float dy = texcoordInPixels.y - tempY;
|
||||
scanLineWeight = CalcScanLine(dy, in.filterWidth);
|
||||
float signY = sign(dy);
|
||||
dy = dy * dy;
|
||||
dy = dy * dy;
|
||||
dy *= 8.0;
|
||||
dy /= uniforms.textureSize.y;
|
||||
dy *= signY;
|
||||
tc = float2(texcoord.x, yCoord + dy);
|
||||
} else {
|
||||
// Modo SHARPER
|
||||
float2 tempCoord = floor(texcoordInPixels) + 0.5;
|
||||
float2 coord = tempCoord / uniforms.textureSize;
|
||||
float2 deltas = texcoordInPixels - tempCoord;
|
||||
scanLineWeight = CalcScanLine(deltas.y, in.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 /= uniforms.textureSize;
|
||||
deltas *= signs;
|
||||
tc = coord + deltas;
|
||||
}
|
||||
|
||||
// Muestrear textura (texture() en GLSL = sample() en MSL)
|
||||
float3 colour = colorTexture.sample(textureSampler, tc).rgb;
|
||||
|
||||
if (SCANLINES) {
|
||||
if (GAMMA) {
|
||||
if (FAKE_GAMMA) {
|
||||
colour = colour * colour;
|
||||
} else {
|
||||
colour = pow(colour, float3(INPUT_GAMMA));
|
||||
}
|
||||
}
|
||||
scanLineWeight *= BLOOM_FACTOR;
|
||||
colour *= scanLineWeight;
|
||||
|
||||
if (GAMMA) {
|
||||
if (FAKE_GAMMA) {
|
||||
colour = sqrt(colour);
|
||||
} else {
|
||||
colour = pow(colour, float3(1.0 / OUTPUT_GAMMA));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Aplicar máscara CRT
|
||||
if (MASK_TYPE == 0) {
|
||||
return float4(colour, 1.0);
|
||||
} else if (MASK_TYPE == 1) {
|
||||
// Máscara verde/magenta
|
||||
float whichMask = fract(in.position.x * 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);
|
||||
}
|
||||
return float4(colour * mask, 1.0);
|
||||
} else if (MASK_TYPE == 2) {
|
||||
// Máscara trinitron
|
||||
float whichMask = fract(in.position.x * 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;
|
||||
}
|
||||
return float4(colour * mask, 1.0);
|
||||
}
|
||||
|
||||
return float4(colour, 1.0);
|
||||
}
|
||||
46
data/shaders/crtpi_vertex.metal
Normal file
46
data/shaders/crtpi_vertex.metal
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// CRT-Pi Vertex Shader - Metal Shading Language
|
||||
// Portado desde GLSL a MSL para macOS
|
||||
//
|
||||
|
||||
#include <metal_stdlib>
|
||||
using namespace metal;
|
||||
|
||||
// Estructura de entrada del vertex shader (desde el buffer de vértices)
|
||||
struct VertexIn {
|
||||
float2 position [[attribute(0)]]; // Posición del vértice
|
||||
float2 texCoord [[attribute(1)]]; // Coordenadas de textura
|
||||
};
|
||||
|
||||
// Estructura de salida del vertex shader (entrada al fragment shader)
|
||||
struct VertexOut {
|
||||
float4 position [[position]]; // Posición en clip space
|
||||
float2 texCoord; // Coordenadas de textura
|
||||
float filterWidth; // Ancho del filtro calculado
|
||||
// float2 screenScale; // Solo si CURVATURE está activo
|
||||
};
|
||||
|
||||
// Uniforms (constantes del shader)
|
||||
struct Uniforms {
|
||||
float2 textureSize; // Tamaño de la textura (width, height)
|
||||
};
|
||||
|
||||
// Entry point del vertex shader
|
||||
vertex VertexOut vertex_main(VertexIn in [[stage_in]],
|
||||
constant Uniforms& uniforms [[buffer(1)]]) {
|
||||
VertexOut out;
|
||||
|
||||
// Posición del vértice (ya está en espacio de clip [-1, 1])
|
||||
out.position = float4(in.position, 0.0, 1.0);
|
||||
|
||||
// Pasar coordenadas de textura (invertir Y para SDL, igual que en GLSL)
|
||||
out.texCoord = float2(in.texCoord.x, 1.0 - in.texCoord.y) * 1.0001;
|
||||
|
||||
// Calcular filterWidth dinámicamente basándose en la altura de la textura
|
||||
out.filterWidth = (768.0 / uniforms.textureSize.y) / 3.0;
|
||||
|
||||
// Si CURVATURE estuviera activo:
|
||||
// out.screenScale = float2(1.0, 1.0);
|
||||
|
||||
return out;
|
||||
}
|
||||
Reference in New Issue
Block a user