From 50f596bd34ed8db1e9dcab928b5b97993f9dd63d Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 31 Jul 2022 11:18:15 -0700 Subject: [PATCH] Default shaders use lovrmain; --- etc/shaders/cubemap.frag | 5 ++--- etc/shaders/cubemap.vert | 10 +++------- etc/shaders/equirect.frag | 8 ++++---- etc/shaders/fill.vert | 6 ++---- etc/shaders/font.frag | 4 ++-- etc/shaders/lovr.glsl | 30 +++++++++++++++++++++++++++++- etc/shaders/unlit.frag | 4 ++-- etc/shaders/unlit.vert | 8 ++------ src/modules/graphics/graphics.c | 18 ++---------------- 9 files changed, 48 insertions(+), 45 deletions(-) diff --git a/etc/shaders/cubemap.frag b/etc/shaders/cubemap.frag index b46aa4a4..ca8f87fd 100644 --- a/etc/shaders/cubemap.frag +++ b/etc/shaders/cubemap.frag @@ -5,8 +5,7 @@ #include "lovr.glsl" layout(set = 1, binding = 1) uniform textureCube SkyboxTexture; -layout(location = 0) in vec3 Direction; -void main() { - PixelColors[0] = Color * getPixel(SkyboxTexture, Direction); +vec4 lovrmain() { + return Color * getPixel(SkyboxTexture, Normal); } diff --git a/etc/shaders/cubemap.vert b/etc/shaders/cubemap.vert index 17ad28f7..d5ac3b04 100644 --- a/etc/shaders/cubemap.vert +++ b/etc/shaders/cubemap.vert @@ -4,11 +4,7 @@ #include "lovr.glsl" -layout(location = 0) out vec3 Direction; - -void main() { - Color = PassColor * VertexColor; - +vec4 lovrmain() { const vec2 uvs[6] = vec2[6]( vec2(-1, -1), vec2(-1, +1), @@ -21,6 +17,6 @@ void main() { vec2 uv = uvs[VertexIndex % 6]; vec3 ray = vec3(uv, -1.); mat3 inverseViewOrientation = transpose(mat3(View)); - Direction = normalize(inverseViewOrientation * (InverseProjection * vec4(ray, 1.)).xyz); - Position = vec4(uv, 0, 1); + Normal = normalize(inverseViewOrientation * (InverseProjection * vec4(ray, 1.)).xyz); + return vec4(uv, 0, 1); } diff --git a/etc/shaders/equirect.frag b/etc/shaders/equirect.frag index 7125a138..2525d005 100644 --- a/etc/shaders/equirect.frag +++ b/etc/shaders/equirect.frag @@ -2,16 +2,16 @@ #extension GL_EXT_multiview : require #extension GL_GOOGLE_include_directive : require -#define PI 3.141592653589793238462643383 - #include "lovr.glsl" +#define PI 3.141592653589793238462643383 + layout(location = 0) in vec3 Direction; -void main() { +vec4 lovrmain() { vec3 dir = normalize(Direction); float phi = acos(dir.y); float theta = atan(dir.x, -dir.z); vec2 uv = vec2(.5 + theta / (2 * PI), phi / PI); - PixelColors[0] = Color * getPixel(ColorTexture, uv); + return Color * getPixel(ColorTexture, uv); } diff --git a/etc/shaders/fill.vert b/etc/shaders/fill.vert index 67184570..114b2b5b 100644 --- a/etc/shaders/fill.vert +++ b/etc/shaders/fill.vert @@ -4,11 +4,9 @@ #include "lovr.glsl" -void main() { - Color = PassColor * VertexColor; +vec4 lovrmain() { float x = -1 + float((VertexIndex & 1) << 2); float y = -1 + float((VertexIndex & 2) << 1); UV = vec2(x, y) * .5 + .5; - Position = vec4(x, y, 1., 1.); - PointSize = 1.f; + return vec4(x, y, 1., 1.); } diff --git a/etc/shaders/font.frag b/etc/shaders/font.frag index 00b5fc9b..02f4fee6 100644 --- a/etc/shaders/font.frag +++ b/etc/shaders/font.frag @@ -14,11 +14,11 @@ float median(float r, float g, float b) { return max(min(r, g), min(max(r, g), b)); } -void main() { +vec4 lovrmain() { vec3 msdf = getPixel(ColorTexture).rgb; float sdf = median(msdf.r, msdf.g, msdf.b); float screenPxDistance = screenPxRange() * (sdf - .5); float alpha = clamp(screenPxDistance + .5, 0., 1.); if (alpha <= 0.) discard; - PixelColors[0] = vec4(Color.rgb, Color.a * alpha); + return vec4(Color.rgb, Color.a * alpha); } diff --git a/etc/shaders/lovr.glsl b/etc/shaders/lovr.glsl index bf109fe9..d7a86078 100644 --- a/etc/shaders/lovr.glsl +++ b/etc/shaders/lovr.glsl @@ -111,7 +111,7 @@ layout(location = 12) in vec2 UV; #define WorldFromLocal (Transform) #define DefaultPosition (ClipFromLocal * VertexPosition) -#define DefaultColor (Color * MaterialColor * getPixel(ColorTexture)) +#define DefaultColor (Color * getPixel(ColorTexture)) #endif #ifdef GL_FRAGMENT_SHADER @@ -121,3 +121,31 @@ vec4 getPixel(texture3D t, vec3 uvw) { return texture(sampler3D(t, Sampler), uvw vec4 getPixel(textureCube t, vec3 dir) { return texture(samplerCube(t, Sampler), dir); } vec4 getPixel(texture2DArray t, vec3 uvw) { return texture(sampler2DArray(t, Sampler), uvw); } #endif + +// Entrypoints +#ifndef NO_DEFAULT_MAIN +#ifdef GL_VERTEX_SHADER +vec4 lovrmain(); +void main() { + Color = VertexColor * MaterialColor * PassColor; + Normal = NormalMatrix * VertexNormal; + UV = VertexUV; + PointSize = 1.f; + Position = lovrmain(); +} +#endif + +#ifdef GL_FRAGMENT_SHADER +vec4 lovrmain(); +void main() { + PixelColors[0] = lovrmain(); +} +#endif + +#ifdef GL_COMPUTE_SHADER +vec4 lovrmain(); +void main() { + lovrmain(); +} +#endif +#endif diff --git a/etc/shaders/unlit.frag b/etc/shaders/unlit.frag index 845eb1bf..8495bb0b 100644 --- a/etc/shaders/unlit.frag +++ b/etc/shaders/unlit.frag @@ -4,6 +4,6 @@ #include "lovr.glsl" -void main() { - PixelColors[0] = Color * MaterialColor * getPixel(ColorTexture, UV); +vec4 lovrmain() { + return DefaultColor; } diff --git a/etc/shaders/unlit.vert b/etc/shaders/unlit.vert index 6ba40089..e7e63a25 100644 --- a/etc/shaders/unlit.vert +++ b/etc/shaders/unlit.vert @@ -4,10 +4,6 @@ #include "lovr.glsl" -void main() { - Color = PassColor * VertexColor; - Normal = normalize(NormalMatrix * VertexNormal); - UV = VertexUV; - Position = DefaultPosition; - PointSize = 1.f; +vec4 lovrmain() { + return DefaultPosition; } diff --git a/src/modules/graphics/graphics.c b/src/modules/graphics/graphics.c index 4abe162c..bad90340 100644 --- a/src/modules/graphics/graphics.c +++ b/src/modules/graphics/graphics.c @@ -1333,32 +1333,18 @@ ShaderSource lovrGraphicsCompileShader(ShaderStage stage, ShaderSource* source) "#extension GL_EXT_multiview : require\n" "#extension GL_GOOGLE_include_directive : require\n"; - const char* suffixes[] = { - [STAGE_VERTEX] = "" - "void main() {" - "Color = PassColor * VertexColor;" - "Normal = NormalMatrix * VertexNormal;" - "UV = VertexUV;" - "Position = lovrmain();" - "}", - [STAGE_FRAGMENT] = "void main() { PixelColors[0] = lovrmain(); }", - [STAGE_COMPUTE] = "void main() { lovrmain(); }" - }; - const char* strings[] = { prefix, (const char*) etc_shaders_lovr_glsl, "#line 1\n", - source->code, - suffixes[stage] + source->code }; int lengths[] = { -1, etc_shaders_lovr_glsl_len, -1, - source->size, - -1 + source->size }; const glslang_resource_t* resource = glslang_default_resource();