Skip to content

Fix GLSL version on some platforms #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/global_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ void scratchcpprender::init()

QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setSwapInterval(0);
#ifdef Q_OS_MACOS
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(3, 2);
#endif
QSurfaceFormat::setDefaultFormat(format);
}

Expand Down
8 changes: 3 additions & 5 deletions src/shadermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ static float wrapClamp(float n, float min, float max)
static const QString VERTEX_SHADER_SRC = ":/qt/qml/ScratchCPP/Render/shaders/sprite.vert";
static const QString FRAGMENT_SHADER_SRC = ":/qt/qml/ScratchCPP/Render/shaders/sprite.frag";

#if defined(Q_OS_WASM)
static const QString SHADER_PREFIX = ""; // compiles, but doesn't work?
#elif defined(Q_OS_ANDROID)
static const QString SHADER_PREFIX = "#version 300 es\n";
#ifdef Q_OS_MACOS
static const QString SHADER_PREFIX = "#version 410\n";
#else
static const QString SHADER_PREFIX = "#version 140\n";
static const QString SHADER_PREFIX = "#version 300 es\n";
#endif

static const char *TEXTURE_UNIT_UNIFORM = "u_skin";
Expand Down
17 changes: 9 additions & 8 deletions src/shaders/sprite.frag
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ uniform vec2 u_skinSize;
uniform float u_mosaic;
#endif // ENABLE_mosaic

varying vec2 v_texCoord;
in vec2 v_texCoord;
out vec4 fragColor;
uniform sampler2D u_skin;

// Add this to divisors to prevent division by 0, which results in NaNs propagating through calculations.
Expand Down Expand Up @@ -147,16 +148,16 @@ void main()
}
#endif // ENABLE_fisheye

gl_FragColor = texture2D(u_skin, texcoord0);
fragColor = texture(u_skin, texcoord0);

#if defined(ENABLE_color) || defined(ENABLE_brightness)
// Divide premultiplied alpha values for proper color processing
// Add epsilon to avoid dividing by 0 for fully transparent pixels
gl_FragColor.rgb = clamp(gl_FragColor.rgb / (gl_FragColor.a + epsilon), 0.0, 1.0);
fragColor.rgb = clamp(fragColor.rgb / (fragColor.a + epsilon), 0.0, 1.0);

#ifdef ENABLE_color
{
vec3 hsv = convertRGB2HSV(gl_FragColor.rgb);
vec3 hsv = convertRGB2HSV(fragColor.rgb);

// Force grayscale values to be slightly saturated
const float minLightness = 0.11 / 2.0;
Expand All @@ -167,20 +168,20 @@ void main()
hsv.x = mod(hsv.x + u_color, 1.0);
if (hsv.x < 0.0) hsv.x += 1.0;

gl_FragColor.rgb = convertHSV2RGB(hsv);
fragColor.rgb = convertHSV2RGB(hsv);
}
#endif // ENABLE_color

#ifdef ENABLE_brightness
gl_FragColor.rgb = clamp(gl_FragColor.rgb + vec3(u_brightness), vec3(0), vec3(1));
fragColor.rgb = clamp(fragColor.rgb + vec3(u_brightness), vec3(0), vec3(1));
#endif // ENABLE_brightness

// Re-multiply color values
gl_FragColor.rgb *= gl_FragColor.a + epsilon;
fragColor.rgb *= fragColor.a + epsilon;

#endif // defined(ENABLE_color) || defined(ENABLE_brightness)

#ifdef ENABLE_ghost
gl_FragColor *= u_ghost;
fragColor *= u_ghost;
#endif // ENABLE_ghost
}
6 changes: 3 additions & 3 deletions src/shaders/sprite.vert
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
uniform mat4 u_projectionMatrix;
uniform mat4 u_modelMatrix;
attribute vec2 a_position;
attribute vec2 a_texCoord;
in vec2 a_position;
in vec2 a_texCoord;

varying vec2 v_texCoord;
out vec2 v_texCoord;

void main() {
gl_Position = u_projectionMatrix * u_modelMatrix * vec4(a_position, 0, 1);
Expand Down