migracio a OpenGL 3.3 Core Profile completada
This commit is contained in:
@@ -1,212 +0,0 @@
|
|||||||
/*
|
|
||||||
crt-pi - A Raspberry Pi friendly CRT shader.
|
|
||||||
Modernized to OpenGL 3.3 Core Profile
|
|
||||||
|
|
||||||
Copyright (C) 2015-2016 davej
|
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify it
|
|
||||||
under the terms of the GNU General Public License as published by the Free
|
|
||||||
Software Foundation; either version 2 of the License, or (at your option)
|
|
||||||
any later version.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Haven't put these as parameters as it would slow the code down.
|
|
||||||
#define SCANLINES
|
|
||||||
#define MULTISAMPLE
|
|
||||||
#define GAMMA
|
|
||||||
//#define FAKE_GAMMA
|
|
||||||
//#define CURVATURE
|
|
||||||
//#define SHARPER
|
|
||||||
// MASK_TYPE: 0 = none, 1 = green/magenta, 2 = trinitron(ish)
|
|
||||||
#define MASK_TYPE 2
|
|
||||||
|
|
||||||
#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
|
|
||||||
|
|
||||||
#if defined(VERTEX)
|
|
||||||
// ============================================================================
|
|
||||||
// VERTEX SHADER
|
|
||||||
// ============================================================================
|
|
||||||
#version 330 core
|
|
||||||
|
|
||||||
// Inputs (desde VAO)
|
|
||||||
layout(location = 0) in vec2 aPosition;
|
|
||||||
layout(location = 1) in vec2 aTexCoord;
|
|
||||||
|
|
||||||
// Outputs al fragment shader
|
|
||||||
out vec2 vTexCoord;
|
|
||||||
out float vFilterWidth;
|
|
||||||
#if defined(CURVATURE)
|
|
||||||
out vec2 vScreenScale;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Uniforms
|
|
||||||
uniform vec2 TextureSize;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
#if defined(CURVATURE)
|
|
||||||
vScreenScale = vec2(1.0, 1.0);
|
|
||||||
#endif
|
|
||||||
// Calcula filterWidth dinámicamente basándose en la altura de la textura
|
|
||||||
vFilterWidth = (768.0 / TextureSize.y) / 3.0;
|
|
||||||
|
|
||||||
// Pasar coordenadas de textura (invertir Y para SDL)
|
|
||||||
vTexCoord = vec2(aTexCoord.x, 1.0 - aTexCoord.y) * 1.0001;
|
|
||||||
|
|
||||||
// Posición del vértice (ya en espacio de clip [-1, 1])
|
|
||||||
gl_Position = vec4(aPosition, 0.0, 1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined(FRAGMENT)
|
|
||||||
// ============================================================================
|
|
||||||
// FRAGMENT SHADER
|
|
||||||
// ============================================================================
|
|
||||||
#version 330 core
|
|
||||||
|
|
||||||
// Inputs desde vertex shader
|
|
||||||
in vec2 vTexCoord;
|
|
||||||
in float vFilterWidth;
|
|
||||||
#if defined(CURVATURE)
|
|
||||||
in vec2 vScreenScale;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Output
|
|
||||||
out vec4 FragColor;
|
|
||||||
|
|
||||||
// Uniforms
|
|
||||||
uniform sampler2D Texture;
|
|
||||||
uniform vec2 TextureSize;
|
|
||||||
|
|
||||||
#if defined(CURVATURE)
|
|
||||||
vec2 Distort(vec2 coord)
|
|
||||||
{
|
|
||||||
vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y);
|
|
||||||
vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION);
|
|
||||||
coord *= vScreenScale;
|
|
||||||
coord -= vec2(0.5);
|
|
||||||
float rsq = coord.x * coord.x + coord.y * coord.y;
|
|
||||||
coord += coord * (CURVATURE_DISTORTION * rsq);
|
|
||||||
coord *= barrelScale;
|
|
||||||
if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
|
|
||||||
coord = vec2(-1.0);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
coord += vec2(0.5);
|
|
||||||
coord /= vScreenScale;
|
|
||||||
}
|
|
||||||
return coord;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
float CalcScanLineWeight(float dist)
|
|
||||||
{
|
|
||||||
return max(1.0 - dist * dist * SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
float CalcScanLine(float dy)
|
|
||||||
{
|
|
||||||
float scanLineWeight = CalcScanLineWeight(dy);
|
|
||||||
#if defined(MULTISAMPLE)
|
|
||||||
scanLineWeight += CalcScanLineWeight(dy - vFilterWidth);
|
|
||||||
scanLineWeight += CalcScanLineWeight(dy + vFilterWidth);
|
|
||||||
scanLineWeight *= 0.3333333;
|
|
||||||
#endif
|
|
||||||
return scanLineWeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
#if defined(CURVATURE)
|
|
||||||
vec2 texcoord = Distort(vTexCoord);
|
|
||||||
if (texcoord.x < 0.0) {
|
|
||||||
FragColor = vec4(0.0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
vec2 texcoord = vTexCoord;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
vec2 texcoordInPixels = texcoord * TextureSize;
|
|
||||||
|
|
||||||
#if defined(SHARPER)
|
|
||||||
vec2 tempCoord = floor(texcoordInPixels) + 0.5;
|
|
||||||
vec2 coord = tempCoord / TextureSize;
|
|
||||||
vec2 deltas = texcoordInPixels - tempCoord;
|
|
||||||
float scanLineWeight = CalcScanLine(deltas.y);
|
|
||||||
vec2 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;
|
|
||||||
vec2 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);
|
|
||||||
float signY = sign(dy);
|
|
||||||
dy = dy * dy;
|
|
||||||
dy = dy * dy;
|
|
||||||
dy *= 8.0;
|
|
||||||
dy /= TextureSize.y;
|
|
||||||
dy *= signY;
|
|
||||||
vec2 tc = vec2(texcoord.x, yCoord + dy);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// texture() en vez de texture2D() (OpenGL 3.3+)
|
|
||||||
vec3 colour = texture(Texture, tc).rgb;
|
|
||||||
|
|
||||||
#if defined(SCANLINES)
|
|
||||||
#if defined(GAMMA)
|
|
||||||
#if defined(FAKE_GAMMA)
|
|
||||||
colour = colour * colour;
|
|
||||||
#else
|
|
||||||
colour = pow(colour, vec3(INPUT_GAMMA));
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
scanLineWeight *= BLOOM_FACTOR;
|
|
||||||
colour *= scanLineWeight;
|
|
||||||
|
|
||||||
#if defined(GAMMA)
|
|
||||||
#if defined(FAKE_GAMMA)
|
|
||||||
colour = sqrt(colour);
|
|
||||||
#else
|
|
||||||
colour = pow(colour, vec3(1.0 / OUTPUT_GAMMA));
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if MASK_TYPE == 0
|
|
||||||
FragColor = vec4(colour, 1.0);
|
|
||||||
#elif MASK_TYPE == 1
|
|
||||||
float whichMask = fract(gl_FragCoord.x * 0.5);
|
|
||||||
vec3 mask;
|
|
||||||
if (whichMask < 0.5)
|
|
||||||
mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
|
|
||||||
else
|
|
||||||
mask = vec3(1.0, MASK_BRIGHTNESS, 1.0);
|
|
||||||
FragColor = vec4(colour * mask, 1.0);
|
|
||||||
#elif MASK_TYPE == 2
|
|
||||||
float whichMask = fract(gl_FragCoord.x * 0.3333333);
|
|
||||||
vec3 mask = vec3(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;
|
|
||||||
FragColor = vec4(colour * mask, 1.0);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -55,10 +55,12 @@ Screen::Screen()
|
|||||||
setDebugInfoEnabled(true);
|
setDebugInfoEnabled(true);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Inicializa los shaders
|
// Inicializa los shaders (solo en plataformas no-macOS por ahora)
|
||||||
|
#ifndef __APPLE__
|
||||||
loadShaders();
|
loadShaders();
|
||||||
shader_backend_ = std::make_unique<Rendering::OpenGLShader>();
|
shader_backend_ = std::make_unique<Rendering::OpenGLShader>();
|
||||||
shader_backend_->init(window_, game_canvas_, vertex_shader_source_, fragment_shader_source_);
|
shader_backend_->init(window_, game_canvas_, vertex_shader_source_, fragment_shader_source_);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -244,6 +246,7 @@ void Screen::loadShaders() {
|
|||||||
|
|
||||||
// Inicializa los shaders
|
// Inicializa los shaders
|
||||||
void Screen::initShaders() {
|
void Screen::initShaders() {
|
||||||
|
#ifndef __APPLE__
|
||||||
if (Options::video.shaders) {
|
if (Options::video.shaders) {
|
||||||
loadShaders();
|
loadShaders();
|
||||||
if (!shader_backend_) {
|
if (!shader_backend_) {
|
||||||
@@ -251,6 +254,12 @@ void Screen::initShaders() {
|
|||||||
}
|
}
|
||||||
shader_backend_->init(window_, game_canvas_, vertex_shader_source_, fragment_shader_source_);
|
shader_backend_->init(window_, game_canvas_, vertex_shader_source_, fragment_shader_source_);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
// En macOS, OpenGL está deprecated y rinde mal
|
||||||
|
// TODO: Implementar backend de Metal para shaders en macOS
|
||||||
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
|
"Shaders no disponibles en macOS (OpenGL deprecated). Usa Metal backend.");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calcula el tamaño de la ventana
|
// Calcula el tamaño de la ventana
|
||||||
|
|||||||
Reference in New Issue
Block a user