From 006c8384490b12e0c3607bd6612f955340a9663c Mon Sep 17 00:00:00 2001 From: omigamedev Date: Sat, 5 Jan 2019 16:39:29 +0100 Subject: [PATCH] cleanup shaders and move common functions in global definitions to be plugged into the shaders code --- libs/bugtrap-client | 2 +- src/app_shaders.cpp | 467 +++++++++++++++++++++----------------------- 2 files changed, 221 insertions(+), 248 deletions(-) diff --git a/libs/bugtrap-client b/libs/bugtrap-client index 0cb9f1f..42d8932 160000 --- a/libs/bugtrap-client +++ b/libs/bugtrap-client @@ -1 +1 @@ -Subproject commit 0cb9f1fb5c291e7d39771dd9fb680b1da16c9366 +Subproject commit 42d893209b27fce20abe55b28df2c407af713ace diff --git a/src/app_shaders.cpp b/src/app_shaders.cpp index a602480..2cd1c50 100644 --- a/src/app_shaders.cpp +++ b/src/app_shaders.cpp @@ -2,33 +2,94 @@ #include "app.h" #include "shader.h" +#define SHADER_FUNCTION_BLUR \ + "mediump vec4 blur(sampler2D t, mediump vec2 uv){\n"\ + " mediump vec4 sum = texture(t, uv);\n"\ + " sum += textureOffset(t, uv, ivec2(-1, -1));\n"\ + " sum += textureOffset(t, uv, ivec2(-1, 0));\n"\ + " sum += textureOffset(t, uv, ivec2(-1, 1));\n"\ + " sum += textureOffset(t, uv, ivec2( 0, -1));\n"\ + " sum += textureOffset(t, uv, ivec2( 0, 1));\n"\ + " sum += textureOffset(t, uv, ivec2( 1, -1));\n"\ + " sum += textureOffset(t, uv, ivec2( 1, 0));\n"\ + " sum += textureOffset(t, uv, ivec2( 1, 1));\n"\ + " return sum / vec4(9.0);\n"\ + "}\n" + +#define SHADER_FUNCTION_BLEND \ + "mediump vec3 blend_normal(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)"\ + "{ return mix(base.rgb, stroke.rgb, stroke.a/alpha_tot); }\n"\ + "mediump vec3 blend_multiply(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)"\ + "{ return mix(stroke.rgb, mix(base.rgb, base.rgb*stroke.rgb, stroke.a/alpha_tot), base.a/alpha_tot); }\n"\ + "mediump vec3 blend_screen(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)"\ + "{ return mix(stroke.rgb, mix(base.rgb, 1.0-(1.0-base.rgb)*(1.0-stroke.rgb), stroke.a/alpha_tot), base.a/alpha_tot); }\n"\ + "mediump vec3 blend_colorDodge(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)"\ + "{ return mix(stroke.rgb, mix(base.rgb, base.rgb/(1.0-stroke.rgb), stroke.a/alpha_tot), base.a/alpha_tot); }\n"\ + "mediump vec3 blend_overlay(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)"\ + "{ return mix(stroke.rgb, mix(base.rgb, mix(2.0*base.rgb*stroke.rgb, 1.0-2.0*(1.0-base.rgb)*(1.0-stroke.rgb), floor(base.rgb*2.0)), stroke.a/alpha_tot), base.a/alpha_tot); }\n"\ + "mediump vec3 blend(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot, int mode) {\n"\ + " if (mode == 0) return blend_normal(base, stroke, alpha_tot);\n"\ + " else if (mode == 1) return blend_multiply(base, stroke, alpha_tot);\n"\ + " else if (mode == 2) return blend_screen(base, stroke, alpha_tot);\n"\ + " else if (mode == 3) return blend_colorDodge(base, stroke, alpha_tot);\n"\ + " else if (mode == 4) return blend_overlay(base, stroke, alpha_tot);\n"\ + " else return blend_multiply(base, stroke, alpha_tot);\n"\ + "}\n" + +#define SHADER_FUNCTION_HSV \ + "mediump vec3 rgb2hsv(mediump vec3 c) {\n"\ + " mediump vec4 k = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n"\ + " mediump vec4 p = mix(vec4(c.bg, k.wz), vec4(c.gb, k.xy), step(c.b, c.g));\n"\ + " mediump vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n"\ + " mediump float d = q.x - min(q.w, q.y);\n"\ + " mediump float e = 1.0e-10;\n"\ + " return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n"\ + "}\n"\ + "mediump vec3 hsv2rgb(mediump vec3 c) {\n"\ + " mediump vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n"\ + " mediump vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n"\ + " return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n"\ + "}\n" + +// http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/ +#define SHADER_FUNCTION_RAND \ + "highp float rand(mediump vec2 co)\n"\ + "{\n"\ + " highp float a = 12.9898;\n"\ + " highp float b = 78.233;\n"\ + " highp float c = 43758.5453;\n"\ + " highp float dt= dot(co.xy ,vec2(a,b));\n"\ + " highp float sn= mod(dt,3.14);\n"\ + " return fract(sin(sn) * c);\n"\ + "}\n" + void App::initShaders() { static const char* shader_v = SHADER_VERSION - "uniform mat4 mvp;" - "in vec4 pos;" - "in vec2 uvs;" - "out vec2 uv;" - "void main(){" - " uv = uvs;" - " gl_Position = mvp * vec4(pos.xyz, 1.0);" - "}"; + "uniform mat4 mvp;\n" + "in vec4 pos;\n" + "in vec2 uvs;\n" + "out vec2 uv;\n" + "void main() {\n" + " uv = uvs;\n" + " gl_Position = mvp * vec4(pos.xyz, 1.0);\n" + "}\n"; static const char* shader_f = SHADER_VERSION - "uniform sampler2D tex;" - "in mediump vec2 uv;" - "out mediump vec4 frag;" - "void main(){" - " frag = texture(tex, uv);" - "}"; + "uniform sampler2D tex;\n" + "in mediump vec2 uv;\n" + "out mediump vec4 frag;\n" + "void main() {\n" + " frag = texture(tex, uv);\n" + "}\n"; static const char* shader_uv_f = SHADER_VERSION - "in mediump vec2 uv;" - "out mediump vec4 frag;" - "void main(){" - " frag = vec4(uv, 0.0, 1.0);" - "}"; + "in mediump vec2 uv;\n" + "out mediump vec4 frag;\n" + "void main() {\n" + " frag = vec4(uv, 0.0, 1.0);\n" + "}\n"; // TEXTURE ALPHA static const char* shader_alpha_f = SHADER_VERSION @@ -37,7 +98,7 @@ void App::initShaders() "uniform bool highlight;\n" "in mediump vec2 uv;\n" "out mediump vec4 frag;\n" - "void main(){\n" + "void main() {\n" " mediump vec4 c = texture(tex, uv);\n" " frag = highlight ? \n" " vec4(clamp(vec3(0.3) + c.rgb, vec3(0.0), vec3(1.0)), c.a) : \n" @@ -52,7 +113,7 @@ void App::initShaders() "uniform bool highlight;\n" "in mediump vec2 uv;\n" "out mediump vec4 frag;\n" - "void main(){\n" + "void main() {\n" " mediump vec3 rgb = texture(tex, uv).rgb;\n" " mediump float a = texture(tex_alpha, uv).a;\n" " mediump vec4 c = vec4(rgb, a);\n" @@ -68,10 +129,10 @@ void App::initShaders() "uniform mediump vec4 col;\n" "in mediump vec2 uv;\n" "out mediump vec4 frag;\n" - "void main(){\n" + "void main() {\n" " mediump float stroke = 1.0 - texture(tex, uv).r;\n" " frag = vec4(col.rgb, stroke * alpha);\n" - "}"; + "}\n"; // TEXTURE COMP ERASE static const char* shader_comp_erase_f = SHADER_VERSION @@ -85,19 +146,8 @@ void App::initShaders() "uniform bool mask;\n" "in mediump vec2 uv;\n" "out mediump vec4 frag;\n" - "mediump vec4 blur(sampler2D t, mediump vec2 uv){\n" - " mediump vec4 sum = texture(t, uv);\n" - " sum += textureOffset(t, uv, ivec2(-1, -1));\n" - " sum += textureOffset(t, uv, ivec2(-1, 0));\n" - " sum += textureOffset(t, uv, ivec2(-1, 1));\n" - " sum += textureOffset(t, uv, ivec2( 0, -1));\n" - " sum += textureOffset(t, uv, ivec2( 0, 1));\n" - " sum += textureOffset(t, uv, ivec2( 1, -1));\n" - " sum += textureOffset(t, uv, ivec2( 1, 0));\n" - " sum += textureOffset(t, uv, ivec2( 1, 1));\n" - " return sum / vec4(9.0);\n" - "}\n" - "void main(){\n" + SHADER_FUNCTION_BLUR + "void main() {\n" " mediump vec2 uv2 = fragUV2 ? (gl_FragCoord.st / resolution) : uv;\n" " mediump vec4 base = texture(tex, uv2);\n" " mediump vec4 stroke = texture(tex_stroke, uv);\n" @@ -120,47 +170,16 @@ void App::initShaders() "uniform bool fragUV2;\n" "in mediump vec2 uv;\n" "out mediump vec4 frag;\n" - "mediump vec4 blur(sampler2D t, mediump vec2 uv){\n" - " mediump vec4 sum = texture(t, uv);\n" - " sum += textureOffset(t, uv, ivec2(-1, -1));\n" - " sum += textureOffset(t, uv, ivec2(-1, 0));\n" - " sum += textureOffset(t, uv, ivec2(-1, 1));\n" - " sum += textureOffset(t, uv, ivec2( 0, -1));\n" - " sum += textureOffset(t, uv, ivec2( 0, 1));\n" - " sum += textureOffset(t, uv, ivec2( 1, -1));\n" - " sum += textureOffset(t, uv, ivec2( 1, 0));\n" - " sum += textureOffset(t, uv, ivec2( 1, 1));\n" - " return sum / vec4(9.0);\n" - "}\n" - - "mediump vec3 blend_normal(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)" - "{ return mix(base.rgb, stroke.rgb, stroke.a/alpha_tot); }\n" - "mediump vec3 blend_multiply(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)" - "{ return mix(stroke.rgb, mix(base.rgb, base.rgb*stroke.rgb, stroke.a/alpha_tot), base.a/alpha_tot); }\n" - "mediump vec3 blend_screen(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)" - "{ return mix(stroke.rgb, mix(base.rgb, 1.0-(1.0-base.rgb)*(1.0-stroke.rgb), stroke.a/alpha_tot), base.a/alpha_tot); }\n" - "mediump vec3 blend_colorDodge(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)" - "{ return mix(stroke.rgb, mix(base.rgb, base.rgb/(1.0-stroke.rgb), stroke.a/alpha_tot), base.a/alpha_tot); }\n" - "mediump vec3 blend_overlay(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)" - "{ return mix(stroke.rgb, mix(base.rgb, mix(2.0*base.rgb*stroke.rgb, 1.0-2.0*(1.0-base.rgb)*(1.0-stroke.rgb), floor(base.rgb*2.0)), stroke.a/alpha_tot), base.a/alpha_tot); }\n" - - "mediump vec3 blend(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot, int mode) {\n" - " if (mode == 0) return blend_normal(base, stroke, alpha_tot);\n" - " else if (mode == 1) return blend_multiply(base, stroke, alpha_tot);\n" - " else if (mode == 2) return blend_screen(base, stroke, alpha_tot);\n" - " else if (mode == 3) return blend_colorDodge(base, stroke, alpha_tot);\n" - " else if (mode == 4) return blend_overlay(base, stroke, alpha_tot);\n" - " else return blend_multiply(base, stroke, alpha_tot);\n" - "}\n" - - "void main(){\n" + SHADER_FUNCTION_BLUR + SHADER_FUNCTION_BLEND + "void main() {\n" " mediump vec2 uv2 = fragUV2 ? (gl_FragCoord.st / resolution) : uv;\n" " mediump vec4 base = texture(tex, uv2);\n" " mediump vec4 stroke = texture(tex_stroke, uv);\n" " stroke.a = mask ? stroke.a * stroke_alpha * blur(tex_mask, uv2).r : stroke.a * stroke_alpha;\n" " if (!lock && base.a == 0.0) { frag = stroke * vec4(1.0, 1.0, 1.0, alpha); return; }\n" " mediump float contribution = (1.0 - base.a) * stroke.a;\n" - " mediump float alpha_tot = base.a + contribution;" + " mediump float alpha_tot = base.a + contribution;\n" " mediump vec3 rgb = blend(base, stroke, alpha_tot, blend_mode);\n" " frag = vec4(rgb, (lock ? base.a : alpha_tot) * alpha);\n" "}\n"; @@ -168,153 +187,117 @@ void App::initShaders() // TEXTURE ATLAS static const char* shader_atlas_v = SHADER_VERSION - "uniform mat4 mvp;" - "uniform vec2 tof;" - "uniform vec2 tsz;" - "in vec2 pos;" - "in vec2 uvs;" - "out vec2 uv;" - "void main(){" - " uv = tof + uvs * tsz;" - " gl_Position = mvp * vec4(pos, 0.0, 1.0);" - "}"; + "uniform mat4 mvp;\n" + "uniform vec2 tof;\n" + "uniform vec2 tsz;\n" + "in vec2 pos;\n" + "in vec2 uvs;\n" + "out vec2 uv;\n" + "void main() {\n" + " uv = tof + uvs * tsz;\n" + " gl_Position = mvp * vec4(pos, 0.0, 1.0);\n" + "}\n"; static const char* shader_atlas_f = SHADER_VERSION - "uniform sampler2D tex;" - "in mediump vec2 uv;" - "out mediump vec4 frag;" - "void main(){" - " frag = texture(tex, uv);" - "}"; + "uniform sampler2D tex;\n" + "in mediump vec2 uv;\n" + "out mediump vec4 frag;\n" + "void main() {\n" + " frag = texture(tex, uv);\n" + "}\n"; // SOLID COLOR static const char* shader_color_v = SHADER_VERSION - "uniform mat4 mvp;" - "in vec4 pos;" - "void main(){" - " gl_Position = mvp * pos;" - " gl_PointSize = 5.0;" - "}"; + "uniform mat4 mvp;\n" + "in vec4 pos;\n" + "void main() {\n" + " gl_Position = mvp * pos;\n" + " gl_PointSize = 5.0;\n" + "}\n"; static const char* shader_color_f = SHADER_VERSION - "uniform mediump vec4 col;" - "out mediump vec4 frag;" - "void main(){" - " frag = col;" - "}"; + "uniform mediump vec4 col;\n" + "out mediump vec4 frag;\n" + "void main() {\n" + " frag = col;\n" + "}\n"; // COLOR QUAD static const char* shader_color_quad_v = SHADER_VERSION - "uniform mat4 mvp;" - "in vec4 pos;" - "in vec2 uvs;" - "out vec3 uv;" - "void main(){" - " gl_Position = mvp * pos;" - " uv = vec3(uvs, pos.w);" - "}"; + "uniform mat4 mvp;\n" + "in vec4 pos;\n" + "in vec2 uvs;\n" + "out vec3 uv;\n" + "void main() {\n" + " gl_Position = mvp * pos;\n" + " uv = vec3(uvs, pos.w);\n" + "}\n"; static const char* shader_color_quad_f = SHADER_VERSION "uniform mediump vec4 col; // HSV\n" - "in mediump vec3 uv;" - "out mediump vec4 frag;" - "mediump vec3 rgb2hsv(mediump vec3 c) {" - " mediump vec4 k = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);" - " mediump vec4 p = mix(vec4(c.bg, k.wz), vec4(c.gb, k.xy), step(c.b, c.g));" - " mediump vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));" - " mediump float d = q.x - min(q.w, q.y);" - " mediump float e = 1.0e-10;" - " return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);" - "}" - "mediump vec3 hsv2rgb(mediump vec3 c) {" - " mediump vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);" - " mediump vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);" - " return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);" - "}" - "void main() {" - " frag = vec4(hsv2rgb(vec3(col.x, uv.x, 1.0 - uv.y)), 1.0);" - "}"; + "in mediump vec3 uv;\n" + "out mediump vec4 frag;\n" + SHADER_FUNCTION_HSV + "void main() {\n" + " frag = vec4(hsv2rgb(vec3(col.x, uv.x, 1.0 - uv.y)), 1.0);\n" + "}\n"; // COLOR TRI static const char* shader_color_tri_f = SHADER_VERSION - "uniform mediump vec4 col;" // in HSV - "in mediump vec3 uv;" - "out mediump vec4 frag;" - "mediump vec3 rgb2hsv(mediump vec3 c) {" - " mediump vec4 k = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);" - " mediump vec4 p = mix(vec4(c.bg, k.wz), vec4(c.gb, k.xy), step(c.b, c.g));" - " mediump vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));" - " mediump float d = q.x - min(q.w, q.y);" - " mediump float e = 1.0e-10;" - " return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);" - "}" - "mediump vec3 hsv2rgb(mediump vec3 c) {" - " mediump vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);" - " mediump vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);" - " return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);" - "}" - "void main() {" - " mediump float sat = tan(atan(uv.y, uv.x)) * 0.5 + 0.5;" - " frag = vec4(hsv2rgb(vec3(col.r, sat, uv.x)), 1.0);" - "}"; + "uniform mediump vec4 col;\n" // in HSV + "in mediump vec3 uv;\n" + "out mediump vec4 frag;\n" + SHADER_FUNCTION_HSV + "void main() {\n" + " mediump float sat = tan(atan(uv.y, uv.x)) * 0.5 + 0.5;\n" + " frag = vec4(hsv2rgb(vec3(col.r, sat, uv.x)), 1.0);\n" + "}\n"; // HUE static const char* shader_color_hue_v = SHADER_VERSION - "uniform mat4 mvp;" - "in vec4 pos;" - "in vec2 uvs;" - "out vec3 uv;" - "void main(){" - " gl_Position = mvp * pos;" - " uv = vec3(uvs, pos.w);" - "}"; + "uniform mat4 mvp;\n" + "in vec4 pos;\n" + "in vec2 uvs;\n" + "out vec3 uv;\n" + "void main() {\n" + " gl_Position = mvp * pos;\n" + " uv = vec3(uvs, pos.w);\n" + "}\n"; static const char* shader_color_hue_f = SHADER_VERSION - "uniform mediump vec4 col;" - "uniform bool dir;" // 0:horizontal, 1:vertical - "in mediump vec3 uv;" - "out mediump vec4 frag;" - "mediump vec3 rgb2hsv(mediump vec3 c) {" - " mediump vec4 k = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);" - " mediump vec4 p = mix(vec4(c.bg, k.wz), vec4(c.gb, k.xy), step(c.b, c.g));" - " mediump vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));" - " mediump float d = q.x - min(q.w, q.y);" - " mediump float e = 1.0e-10;" - " return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);" - "}" - "mediump vec3 hsv2rgb(mediump vec3 c) {" - " mediump vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);" - " mediump vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);" - " return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);" - "}" - "void main(){" - " frag = vec4(hsv2rgb(vec3(dir?uv.y:uv.x, 1.0, 1.0)), 1.0);" - "}"; + "uniform mediump vec4 col;\n" + "uniform bool dir;\n" // 0:horizontal, 1:vertical + "in mediump vec3 uv;\n" + "out mediump vec4 frag;\n" + SHADER_FUNCTION_HSV + "void main() {\n" + " frag = vec4(hsv2rgb(vec3(dir?uv.y:uv.x, 1.0, 1.0)), 1.0);\n" + "}\n"; // FONT static const char* shader_font_v = SHADER_VERSION - "uniform mat4 mvp;" - "in vec2 pos;" - "in vec2 uvs;" - "out vec2 uv;" - "void main(){" - " uv = uvs;" - " gl_Position = mvp * vec4(pos, 0.0, 1.0);" - "}"; + "uniform mat4 mvp;\n" + "in vec2 pos;\n" + "in vec2 uvs;\n" + "out vec2 uv;\n" + "void main() {\n" + " uv = uvs;\n" + " gl_Position = mvp * vec4(pos, 0.0, 1.0);\n" + "}\n"; static const char* shader_font_f = SHADER_VERSION - "uniform mediump sampler2D tex;" - "uniform mediump vec4 col;" - "in mediump vec2 uv;" - "out mediump vec4 frag;" - "void main(){" - " mediump float a = texture(tex, uv).r;" - " frag = vec4(col.rgb, a);" - "}"; + "uniform mediump sampler2D tex;\n" + "uniform mediump vec4 col;\n" + "in mediump vec2 uv;\n" + "out mediump vec4 frag;\n" + "void main() {\n" + " mediump float a = texture(tex, uv).r;\n" + " frag = vec4(col.rgb, a);\n" + "}\n"; // STROKE static const char* shader_stroke_v = @@ -326,7 +309,7 @@ void App::initShaders() "out vec2 uv;\n" "out vec2 uv_2;\n" "out float q;\n" - "void main(){\n" + "void main() {\n" " uv = uvs;\n" " uv_2 = uvs2;\n" " q = pos.w;\n" @@ -358,18 +341,8 @@ void App::initShaders() #else "out mediump vec4 frag;\n" #endif - // http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/ - "highp float rand(mediump vec2 co)\n" - "{\n" - " highp float a = 12.9898;\n" - " highp float b = 78.233;\n" - " highp float c = 43758.5453;\n" - " highp float dt= dot(co.xy ,vec2(a,b));\n" - " highp float sn= mod(dt,3.14);\n" - " return fract(sin(sn) * c);\n" - "}\n" - //"highp float rand(mediump vec2 co) { return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); }\n" - "void main(){\n" + SHADER_FUNCTION_RAND + "void main() {\n" " mediump vec2 uv2 = gl_FragCoord.st / resolution;\n" " mediump float stencil = 1.0 - (texture(tex_stencil, (uv2+stencil_offset) * 5.0).r) * stencil_alpha;\n" " mediump float brush_alpha = ( 1.0 - texture(tex, uv/q).r ) * alpha;\n" @@ -404,21 +377,21 @@ void App::initShaders() "in vec4 pos;\n" "in vec2 uvs;\n" "out vec2 uv;\n" - "void main(){\n" + "void main() {\n" " uv = uvs;\n" " gl_Position = mvp * vec4(pos.xyz, 1.0);\n" - "}"; + "}\n"; static const char* shader_checkerboard_f = SHADER_VERSION "in mediump vec2 uv;\n" "out mediump vec4 frag;\n" - "void main(){\n" + "void main() {\n" " const mediump vec4 c1 = vec4(1.0, 1.0, 1.0, 1.0);\n" " const mediump vec4 c2 = vec4(0.9, 0.9, 0.9, 1.0);\n" " mediump vec2 c = floor(fract(uv * 10.0) * 2.0);\n" " mediump float alpha = mix(c.x, 1.0 - c.x, c.y);\n" " frag = mix(c1, c2, alpha);\n" - "}"; + "}\n"; static const char* shader_equirect_v = SHADER_VERSION @@ -428,16 +401,16 @@ void App::initShaders() "in vec4 pos;\n" "in vec2 uvs;\n" "out vec2 uv;\n" - "void main(){\n" + "void main() {\n" " uv = (vec2(1.0) - uvs + vec2(0.25,0.0)) * vec2(TWO_PI, PI);\n" " gl_Position = mvp * vec4(pos.xyz, 1.0);\n" - "}"; + "}\n"; static const char* shader_equirect_f = SHADER_VERSION "uniform samplerCube tex;\n" "in highp vec2 uv;\n" "out mediump vec4 frag;\n" - "void main(){\n" + "void main() {\n" " highp float anglex = uv.x;\n" " highp float angley = uv.y;\n" " highp float sx = sin(anglex);\n" @@ -447,59 +420,59 @@ void App::initShaders() " dir.y = cos(angley);\n" " dir.z = sin(angley) * sx;\n" " frag = texture(tex, dir);\n" - "}"; + "}\n"; // STROKE - INSTANCED static const char* shader_stroke_inst_v = SHADER_VERSION - "in vec4 pos;" - "in vec2 uvs;" - "in mat4 a_mvp;" - "in float a_flow;" - "out vec3 uv;" - "out float alpha;" - "void main(){" - " uv = vec3(uvs, pos.w);" - " alpha = a_flow;" - " gl_Position = a_mvp * vec4(pos.xyz, 1.0);" - "}"; + "in vec4 pos;\n" + "in vec2 uvs;\n" + "in mat4 a_mvp;\n" + "in float a_flow;\n" + "out vec3 uv;\n" + "out float alpha;\n" + "void main() {\n" + " uv = vec3(uvs, pos.w);\n" + " alpha = a_flow;\n" + " gl_Position = a_mvp * vec4(pos.xyz, 1.0);\n" + "}\n"; static const char* shader_stroke_inst_f = SHADER_VERSION - "uniform mediump sampler2D tex;" + "uniform mediump sampler2D tex;\n" "uniform mediump sampler2D tex_stencil;\n" - "uniform mediump vec4 col;" + "uniform mediump vec4 col;\n" "uniform mediump vec2 resolution;\n" "uniform mediump vec2 stencil_offset;\n" "uniform mediump float stencil_alpha;\n" - "in mediump float alpha;" - "in mediump vec3 uv;" - "out mediump vec4 frag;" - "void main(){" + "in mediump float alpha;\n" + "in mediump vec3 uv;\n" + "out mediump vec4 frag;\n" + "void main() {\n" " mediump vec2 uv2 = gl_FragCoord.st / resolution;\n" " mediump float stencil = 1.0 - (texture(tex_stencil, (uv2+stencil_offset)).r * 0.9) * stencil_alpha;\n" - " mediump float a = (1.0 - texture(tex, uv.xy).r) * alpha * stencil;" - " frag = vec4(col.rgb, a);" - "}"; + " mediump float a = (1.0 - texture(tex, uv.xy).r) * alpha * stencil;\n" + " frag = vec4(col.rgb, a);\n" + "}\n"; // VERTEX COLOR static const char* shader_vertcol_v = SHADER_VERSION - "uniform mat4 mvp;" - "in vec4 pos;" - "in vec4 col;" - "out vec4 c;" - "void main(){" - " c = col;" - " gl_Position = mvp * pos;" - " gl_PointSize = 5.0;" - "}"; + "uniform mat4 mvp;\n" + "in vec4 pos;\n" + "in vec4 col;\n" + "out vec4 c;\n" + "void main() {\n" + " c = col;\n" + " gl_Position = mvp * pos;\n" + " gl_PointSize = 5.0;\n" + "}\n"; static const char* shader_vertcol_f = SHADER_VERSION - "in mediump vec4 c;" - "out mediump vec4 frag;" - "void main(){" - " frag = c;" - "}"; + "in mediump vec4 c;\n" + "out mediump vec4 frag;\n" + "void main() {\n" + " frag = c;\n" + "}\n"; // LAMBERT static const char* shader_lambert_v = @@ -508,19 +481,19 @@ void App::initShaders() "in vec4 pos;\n" "in vec3 nor;\n" "out vec3 n;\n" - "void main(){\n" + "void main() {\n" " n = nor;\n" " gl_Position = mvp * pos;\n" - "}"; + "}\n"; static const char* shader_lambert_f = SHADER_VERSION "uniform mediump vec3 light_dir;\n" "in mediump vec3 n;\n" "out mediump vec4 frag;\n" - "void main(){\n" + "void main() {\n" " mediump float d = max(0.0, dot(normalize(n), light_dir));\n" " frag = vec4(vec3(d), 1.0);\n" - "}"; + "}\n"; LOG("initializing shaders");