Skip to content

Commit b7ffd60

Browse files
authored
Merge pull request #160 from scratchcpp/glsl_version_fix
Fix GLSL version on some platforms
2 parents 9482cf6 + d7219ff commit b7ffd60

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

src/global_functions.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ void scratchcpprender::init()
1010

1111
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
1212
format.setSwapInterval(0);
13+
#ifdef Q_OS_MACOS
14+
format.setProfile(QSurfaceFormat::CoreProfile);
15+
format.setVersion(3, 2);
16+
#endif
1317
QSurfaceFormat::setDefaultFormat(format);
1418
}
1519

src/shadermanager.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ static float wrapClamp(float n, float min, float max)
2424
static const QString VERTEX_SHADER_SRC = ":/qt/qml/ScratchCPP/Render/shaders/sprite.vert";
2525
static const QString FRAGMENT_SHADER_SRC = ":/qt/qml/ScratchCPP/Render/shaders/sprite.frag";
2626

27-
#if defined(Q_OS_WASM)
28-
static const QString SHADER_PREFIX = ""; // compiles, but doesn't work?
29-
#elif defined(Q_OS_ANDROID)
30-
static const QString SHADER_PREFIX = "#version 300 es\n";
27+
#ifdef Q_OS_MACOS
28+
static const QString SHADER_PREFIX = "#version 410\n";
3129
#else
32-
static const QString SHADER_PREFIX = "#version 140\n";
30+
static const QString SHADER_PREFIX = "#version 300 es\n";
3331
#endif
3432

3533
static const char *TEXTURE_UNIT_UNIFORM = "u_skin";

src/shaders/sprite.frag

+9-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ uniform vec2 u_skinSize;
3535
uniform float u_mosaic;
3636
#endif // ENABLE_mosaic
3737

38-
varying vec2 v_texCoord;
38+
in vec2 v_texCoord;
39+
out vec4 fragColor;
3940
uniform sampler2D u_skin;
4041

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

150-
gl_FragColor = texture2D(u_skin, texcoord0);
151+
fragColor = texture(u_skin, texcoord0);
151152

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

157158
#ifdef ENABLE_color
158159
{
159-
vec3 hsv = convertRGB2HSV(gl_FragColor.rgb);
160+
vec3 hsv = convertRGB2HSV(fragColor.rgb);
160161

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

170-
gl_FragColor.rgb = convertHSV2RGB(hsv);
171+
fragColor.rgb = convertHSV2RGB(hsv);
171172
}
172173
#endif // ENABLE_color
173174

174175
#ifdef ENABLE_brightness
175-
gl_FragColor.rgb = clamp(gl_FragColor.rgb + vec3(u_brightness), vec3(0), vec3(1));
176+
fragColor.rgb = clamp(fragColor.rgb + vec3(u_brightness), vec3(0), vec3(1));
176177
#endif // ENABLE_brightness
177178

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

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

183184
#ifdef ENABLE_ghost
184-
gl_FragColor *= u_ghost;
185+
fragColor *= u_ghost;
185186
#endif // ENABLE_ghost
186187
}

src/shaders/sprite.vert

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
uniform mat4 u_projectionMatrix;
22
uniform mat4 u_modelMatrix;
3-
attribute vec2 a_position;
4-
attribute vec2 a_texCoord;
3+
in vec2 a_position;
4+
in vec2 a_texCoord;
55

6-
varying vec2 v_texCoord;
6+
out vec2 v_texCoord;
77

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

0 commit comments

Comments
 (0)