From 085779707f4116eb28f91dcb1f9c8ea58a959424 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 5 Nov 2017 17:13:17 +0100 Subject: Uploaded shaders for GLSL 120 --- examples/shaders/resources/shaders/glsl120/base.fs | 24 +++++++++ examples/shaders/resources/shaders/glsl120/base.vs | 26 ++++++++++ .../shaders/resources/shaders/glsl120/bloom.fs | 39 +++++++++++++++ examples/shaders/resources/shaders/glsl120/blur.fs | 34 +++++++++++++ .../resources/shaders/glsl120/cross_hatching.fs | 47 ++++++++++++++++++ .../resources/shaders/glsl120/cross_stitching.fs | 57 ++++++++++++++++++++++ .../resources/shaders/glsl120/distortion.fs | 54 ++++++++++++++++++++ .../resources/shaders/glsl120/dream_vision.fs | 37 ++++++++++++++ .../shaders/resources/shaders/glsl120/fisheye.fs | 43 ++++++++++++++++ .../shaders/resources/shaders/glsl120/grayscale.fs | 25 ++++++++++ .../shaders/resources/shaders/glsl120/pixelizer.fs | 32 ++++++++++++ .../resources/shaders/glsl120/posterization.fs | 29 +++++++++++ .../shaders/resources/shaders/glsl120/predator.fs | 31 ++++++++++++ .../shaders/resources/shaders/glsl120/scanlines.fs | 44 +++++++++++++++++ .../shaders/resources/shaders/glsl120/sobel.fs | 40 +++++++++++++++ .../shaders/resources/shaders/glsl120/swirl.fs | 46 +++++++++++++++++ 16 files changed, 608 insertions(+) create mode 100644 examples/shaders/resources/shaders/glsl120/base.fs create mode 100644 examples/shaders/resources/shaders/glsl120/base.vs create mode 100644 examples/shaders/resources/shaders/glsl120/bloom.fs create mode 100644 examples/shaders/resources/shaders/glsl120/blur.fs create mode 100644 examples/shaders/resources/shaders/glsl120/cross_hatching.fs create mode 100644 examples/shaders/resources/shaders/glsl120/cross_stitching.fs create mode 100644 examples/shaders/resources/shaders/glsl120/distortion.fs create mode 100644 examples/shaders/resources/shaders/glsl120/dream_vision.fs create mode 100644 examples/shaders/resources/shaders/glsl120/fisheye.fs create mode 100644 examples/shaders/resources/shaders/glsl120/grayscale.fs create mode 100644 examples/shaders/resources/shaders/glsl120/pixelizer.fs create mode 100644 examples/shaders/resources/shaders/glsl120/posterization.fs create mode 100644 examples/shaders/resources/shaders/glsl120/predator.fs create mode 100644 examples/shaders/resources/shaders/glsl120/scanlines.fs create mode 100644 examples/shaders/resources/shaders/glsl120/sobel.fs create mode 100644 examples/shaders/resources/shaders/glsl120/swirl.fs (limited to 'examples/shaders') diff --git a/examples/shaders/resources/shaders/glsl120/base.fs b/examples/shaders/resources/shaders/glsl120/base.fs new file mode 100644 index 00000000..9c685eb9 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/base.fs @@ -0,0 +1,24 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables +uniform vec2 resolution = vec2(800, 450); + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture2D(texture0, fragTexCoord); + + // NOTE: Implement here your fragment shader code + + gl_FragColor = texelColor*colDiffuse; +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/base.vs b/examples/shaders/resources/shaders/glsl120/base.vs new file mode 100644 index 00000000..674153d8 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/base.vs @@ -0,0 +1,26 @@ +#version 120 + +// Input vertex attributes +attribute vec3 vertexPosition; +attribute vec2 vertexTexCoord; +attribute vec3 vertexNormal; +attribute vec4 vertexColor; + +// Input uniform values +uniform mat4 mvp; + +// Output vertex attributes (to fragment shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// NOTE: Add here your custom variables + +void main() +{ + // Send vertex attributes to fragment shader + fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + + // Calculate final vertex position + gl_Position = mvp*vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/bloom.fs b/examples/shaders/resources/shaders/glsl120/bloom.fs new file mode 100644 index 00000000..cd9f8cbe --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/bloom.fs @@ -0,0 +1,39 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +const vec2 size = vec2(800, 450); // render size +const float samples = 5.0; // pixels per axis; higher = bigger glow, worse performance +const float quality = 2.5; // lower = smaller glow, better quality + +void main() +{ + vec4 sum = vec4(0); + vec2 sizeFactor = vec2(1)/size*quality; + + // Texel color fetching from texture sampler + vec4 source = texture2D(texture0, fragTexCoord); + + const int range = 2; // should be = (samples - 1)/2; + + for (int x = -range; x <= range; x++) + { + for (int y = -range; y <= range; y++) + { + sum += texture2D(texture0, fragTexCoord + vec2(x, y)*sizeFactor); + } + } + + // Calculate final fragment color + gl_FragColor = ((sum/(samples*samples)) + source)*colDiffuse; +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/blur.fs b/examples/shaders/resources/shaders/glsl120/blur.fs new file mode 100644 index 00000000..e2b04ad1 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/blur.fs @@ -0,0 +1,34 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +// NOTE: Render size values must be passed from code +const float renderWidth = 800.0; +const float renderHeight = 450.0; + +vec3 offset = vec3(0.0, 1.3846153846, 3.2307692308); +vec3 weight = vec3(0.2270270270, 0.3162162162, 0.0702702703); + +void main() +{ + // Texel color fetching from texture sampler + vec3 tc = texture2D(texture0, fragTexCoord).rgb*weight.x; + + tc += texture2D(texture0, fragTexCoord + vec2(offset.y)/renderWidth, 0.0).rgb*weight.y; + tc += texture2D(texture0, fragTexCoord - vec2(offset.y)/renderWidth, 0.0).rgb*weight.y; + + tc += texture2D(texture0, fragTexCoord + vec2(offset.z)/renderWidth, 0.0).rgb*weight.z; + tc += texture2D(texture0, fragTexCoord - vec2(offset.z)/renderWidth, 0.0).rgb*weight.z; + + gl_FragColor = vec4(tc, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/cross_hatching.fs b/examples/shaders/resources/shaders/glsl120/cross_hatching.fs new file mode 100644 index 00000000..17d9c6a3 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/cross_hatching.fs @@ -0,0 +1,47 @@ +# version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +float hatchOffsetY = 5.0; +float lumThreshold01 = 0.9; +float lumThreshold02 = 0.7; +float lumThreshold03 = 0.5; +float lumThreshold04 = 0.3; + +void main() +{ + vec3 tc = vec3(1.0, 1.0, 1.0); + float lum = length(texture2D(texture0, fragTexCoord).rgb); + + if (lum < lumThreshold01) + { + if (mod(gl_FragCoord.x + gl_FragCoord.y, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0); + } + + if (lum < lumThreshold02) + { + if (mod(gl_FragCoord .x - gl_FragCoord .y, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0); + } + + if (lum < lumThreshold03) + { + if (mod(gl_FragCoord .x + gl_FragCoord .y - hatchOffsetY, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0); + } + + if (lum < lumThreshold04) + { + if (mod(gl_FragCoord .x - gl_FragCoord .y - hatchOffsetY, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0); + } + + gl_FragColor = vec4(tc, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/cross_stitching.fs b/examples/shaders/resources/shaders/glsl120/cross_stitching.fs new file mode 100644 index 00000000..fe60675a --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/cross_stitching.fs @@ -0,0 +1,57 @@ +# version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +// NOTE: Render size values must be passed from code +const float renderWidth = 800.0; +const float renderHeight = 450.0; + +float stitchingSize = 6.0; +int invert = 0; + +vec4 PostFX(sampler2D tex, vec2 uv) +{ + vec4 c = vec4(0.0); + float size = stitchingSize; + vec2 cPos = uv * vec2(renderWidth, renderHeight); + vec2 tlPos = floor(cPos / vec2(size, size)); + tlPos *= size; + + int remX = int(mod(cPos.x, size)); + int remY = int(mod(cPos.y, size)); + + if (remX == 0 && remY == 0) tlPos = cPos; + + vec2 blPos = tlPos; + blPos.y += (size - 1.0); + + if ((remX == remY) || (((int(cPos.x) - int(blPos.x)) == (int(blPos.y) - int(cPos.y))))) + { + if (invert == 1) c = vec4(0.2, 0.15, 0.05, 1.0); + else c = texture2D(tex, tlPos * vec2(1.0/renderWidth, 1.0/renderHeight)) * 1.4; + } + else + { + if (invert == 1) c = texture2D(tex, tlPos * vec2(1.0/renderWidth, 1.0/renderHeight)) * 1.4; + else c = vec4(0.0, 0.0, 0.0, 1.0); + } + + return c; +} + +void main() +{ + vec3 tc = PostFX(texture0, fragTexCoord).rgb; + + gl_FragColor = vec4(tc, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/distortion.fs b/examples/shaders/resources/shaders/glsl120/distortion.fs new file mode 100644 index 00000000..568c50bf --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/distortion.fs @@ -0,0 +1,54 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; + +// Input uniform values +uniform sampler2D texture0; + +// NOTE: Default parameters for Oculus Rift DK2 device +const vec2 LeftLensCenter = vec2(0.2863248, 0.5); +const vec2 RightLensCenter = vec2(0.7136753, 0.5); +const vec2 LeftScreenCenter = vec2(0.25, 0.5); +const vec2 RightScreenCenter = vec2(0.75, 0.5); +const vec2 Scale = vec2(0.25, 0.45); +const vec2 ScaleIn = vec2(4.0, 2.5); +const vec4 HmdWarpParam = vec4(1.0, 0.22, 0.24, 0.0); +const vec4 ChromaAbParam = vec4(0.996, -0.004, 1.014, 0.0); + +void main() +{ + // The following two variables need to be set per eye + vec2 LensCenter = fragTexCoord.x < 0.5 ? LeftLensCenter : RightLensCenter; + vec2 ScreenCenter = fragTexCoord.x < 0.5 ? LeftScreenCenter : RightScreenCenter; + + // Scales input texture coordinates for distortion: vec2 HmdWarp(vec2 fragTexCoord, vec2 LensCenter) + vec2 theta = (fragTexCoord - LensCenter)*ScaleIn; // Scales to [-1, 1] + float rSq = theta.x*theta.x + theta.y*theta.y; + vec2 theta1 = theta*(HmdWarpParam.x + HmdWarpParam.y*rSq + HmdWarpParam.z*rSq*rSq + HmdWarpParam.w*rSq*rSq*rSq); + //vec2 tc = LensCenter + Scale*theta1; + + // Detect whether blue texture coordinates are out of range since these will scaled out the furthest + vec2 thetaBlue = theta1*(ChromaAbParam.z + ChromaAbParam.w*rSq); + vec2 tcBlue = LensCenter + Scale*thetaBlue; + + if (any(bvec2(clamp(tcBlue, ScreenCenter - vec2(0.25, 0.5), ScreenCenter + vec2(0.25, 0.5)) - tcBlue))) gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); + else + { + // Do blue texture lookup + float blue = texture2D(texture0, tcBlue).b; + + // Do green lookup (no scaling) + vec2 tcGreen = LensCenter + Scale*theta1; + float green = texture2D(texture0, tcGreen).g; + + // Do red scale and lookup + vec2 thetaRed = theta1*(ChromaAbParam.x + ChromaAbParam.y*rSq); + vec2 tcRed = LensCenter + Scale*thetaRed; + float red = texture2D(texture0, tcRed).r; + + gl_FragColor = vec4(red, green, blue, 1.0); + } +} diff --git a/examples/shaders/resources/shaders/glsl120/dream_vision.fs b/examples/shaders/resources/shaders/glsl120/dream_vision.fs new file mode 100644 index 00000000..93cec8fb --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/dream_vision.fs @@ -0,0 +1,37 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +void main() +{ + vec4 color = texture2D(texture0, fragTexCoord); + + color += texture2D(texture0, fragTexCoord + 0.001); + color += texture2D(texture0, fragTexCoord + 0.003); + color += texture2D(texture0, fragTexCoord + 0.005); + color += texture2D(texture0, fragTexCoord + 0.007); + color += texture2D(texture0, fragTexCoord + 0.009); + color += texture2D(texture0, fragTexCoord + 0.011); + + color += texture2D(texture0, fragTexCoord - 0.001); + color += texture2D(texture0, fragTexCoord - 0.003); + color += texture2D(texture0, fragTexCoord - 0.005); + color += texture2D(texture0, fragTexCoord - 0.007); + color += texture2D(texture0, fragTexCoord - 0.009); + color += texture2D(texture0, fragTexCoord - 0.011); + + color.rgb = vec3((color.r + color.g + color.b)/3.0); + color = color/9.5; + + gl_FragColor = color; +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/fisheye.fs b/examples/shaders/resources/shaders/glsl120/fisheye.fs new file mode 100644 index 00000000..afac3d4a --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/fisheye.fs @@ -0,0 +1,43 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +const float PI = 3.1415926535; + +void main() +{ + float aperture = 178.0; + float apertureHalf = 0.5 * aperture * (PI / 180.0); + float maxFactor = sin(apertureHalf); + + vec2 uv = vec2(0.0); + vec2 xy = 2.0 * fragTexCoord.xy - 1.0; + float d = length(xy); + + if (d < (2.0 - maxFactor)) + { + d = length(xy * maxFactor); + float z = sqrt(1.0 - d * d); + float r = atan(d, z) / PI; + float phi = atan(xy.y, xy.x); + + uv.x = r * cos(phi) + 0.5; + uv.y = r * sin(phi) + 0.5; + } + else + { + uv = fragTexCoord.xy; + } + + gl_FragColor = texture2D(texture0, uv); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/grayscale.fs b/examples/shaders/resources/shaders/glsl120/grayscale.fs new file mode 100644 index 00000000..470a4222 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/grayscale.fs @@ -0,0 +1,25 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture2D(texture0, fragTexCoord)*colDiffuse*fragColor; + + // Convert texel color to grayscale using NTSC conversion weights + float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114)); + + // Calculate final fragment color + gl_FragColor = vec4(gray, gray, gray, texelColor.a); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/pixelizer.fs b/examples/shaders/resources/shaders/glsl120/pixelizer.fs new file mode 100644 index 00000000..cea6b5e0 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/pixelizer.fs @@ -0,0 +1,32 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +// NOTE: Render size values must be passed from code +const float renderWidth = 800.0; +const float renderHeight = 450.0; + +float pixelWidth = 5.0; +float pixelHeight = 5.0; + +void main() +{ + float dx = pixelWidth*(1.0/renderWidth); + float dy = pixelHeight*(1.0/renderHeight); + + vec2 coord = vec2(dx*floor(fragTexCoord.x/dx), dy*floor(fragTexCoord.y/dy)); + + vec3 tc = texture2D(texture0, coord).rgb; + + gl_FragColor = vec4(tc, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/posterization.fs b/examples/shaders/resources/shaders/glsl120/posterization.fs new file mode 100644 index 00000000..a9a5f246 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/posterization.fs @@ -0,0 +1,29 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +float gamma = 0.6; +float numColors = 8.0; + +void main() +{ + vec3 color = texture2D(texture0, fragTexCoord.xy).rgb; + + color = pow(color, vec3(gamma, gamma, gamma)); + color = color*numColors; + color = floor(color); + color = color/numColors; + color = pow(color, vec3(1.0/gamma)); + + gl_FragColor = vec4(color, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/predator.fs b/examples/shaders/resources/shaders/glsl120/predator.fs new file mode 100644 index 00000000..c471f89d --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/predator.fs @@ -0,0 +1,31 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +void main() +{ + vec3 color = texture2D(texture0, fragTexCoord).rgb; + vec3 colors[3]; + colors[0] = vec3(0.0, 0.0, 1.0); + colors[1] = vec3(1.0, 1.0, 0.0); + colors[2] = vec3(1.0, 0.0, 0.0); + + float lum = (color.r + color.g + color.b)/3.0; + + vec3 tc = vec3(0.0, 0.0, 0.0); + + if (lum < 0.5) tc = mix(colors[0], colors[1], lum/0.5); + else tc = mix(colors[1], colors[2], (lum - 0.5)/0.5); + + gl_FragColor = vec4(tc, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/scanlines.fs b/examples/shaders/resources/shaders/glsl120/scanlines.fs new file mode 100644 index 00000000..f279f347 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/scanlines.fs @@ -0,0 +1,44 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +float offset = 0.0; +float frequency = 450.0/3.0; + +uniform float time; + +void main() +{ +/* + // Scanlines method 1 + float tval = 0; //time + vec2 uv = 0.5 + (fragTexCoord - 0.5)*(0.9 + 0.01*sin(0.5*tval)); + + vec4 color = texture2D(texture0, fragTexCoord); + + color = clamp(color*0.5 + 0.5*color*color*1.2, 0.0, 1.0); + color *= 0.5 + 0.5*16.0*uv.x*uv.y*(1.0 - uv.x)*(1.0 - uv.y); + color *= vec4(0.8, 1.0, 0.7, 1); + color *= 0.9 + 0.1*sin(10.0*tval + uv.y*1000.0); + color *= 0.97 + 0.03*sin(110.0*tval); + + fragColor = color; +*/ + // Scanlines method 2 + float globalPos = (fragTexCoord.y + offset) * frequency; + float wavePos = cos((fract(globalPos) - 0.5)*3.14); + + vec4 color = texture2D(texture0, fragTexCoord); + + gl_FragColor = mix(vec4(0.0, 0.3, 0.0, 0.0), color, wavePos); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/sobel.fs b/examples/shaders/resources/shaders/glsl120/sobel.fs new file mode 100644 index 00000000..128ca423 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/sobel.fs @@ -0,0 +1,40 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables +vec2 resolution = vec2(800.0, 450.0); + +void main() +{ + float x = 1.0/resolution.x; + float y = 1.0/resolution.y; + + vec4 horizEdge = vec4(0.0); + horizEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y))*1.0; + horizEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y ))*2.0; + horizEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y))*1.0; + horizEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y))*1.0; + horizEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y ))*2.0; + horizEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y))*1.0; + + vec4 vertEdge = vec4(0.0); + vertEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y))*1.0; + vertEdge -= texture2D(texture0, vec2(fragTexCoord.x , fragTexCoord.y - y))*2.0; + vertEdge -= texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y))*1.0; + vertEdge += texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y))*1.0; + vertEdge += texture2D(texture0, vec2(fragTexCoord.x , fragTexCoord.y + y))*2.0; + vertEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y))*1.0; + + vec3 edge = sqrt((horizEdge.rgb*horizEdge.rgb) + (vertEdge.rgb*vertEdge.rgb)); + + gl_FragColor = vec4(edge, texture2D(texture0, fragTexCoord).a); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl120/swirl.fs b/examples/shaders/resources/shaders/glsl120/swirl.fs new file mode 100644 index 00000000..7cb24e2a --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/swirl.fs @@ -0,0 +1,46 @@ +#version 120 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +// NOTE: Render size values should be passed from code +const float renderWidth = 800; +const float renderHeight = 450; + +float radius = 250.0; +float angle = 0.8; + +uniform vec2 center; + +void main() +{ + vec2 texSize = vec2(renderWidth, renderHeight); + vec2 tc = fragTexCoord*texSize; + tc -= center; + + float dist = length(tc); + + if (dist < radius) + { + float percent = (radius - dist)/radius; + float theta = percent*percent*angle*8.0; + float s = sin(theta); + float c = cos(theta); + + tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c))); + } + + tc += center; + vec4 color = texture2D(texture0, tc/texSize)*colDiffuse*fragColor;; + + gl_FragColor = vec4(color.rgb, 1.0);; +} \ No newline at end of file -- cgit v1.2.3 From 4fe8e05a7fcaa7223e41414407b9de251ba42676 Mon Sep 17 00:00:00 2001 From: Ray San Date: Mon, 6 Nov 2017 13:49:33 +0100 Subject: Review GLSL 120 shaders Not tested, could have errors --- examples/shaders/resources/shaders/glsl120/base.fs | 2 -- examples/shaders/resources/shaders/glsl120/bloom.fs | 2 -- examples/shaders/resources/shaders/glsl120/blur.fs | 2 -- examples/shaders/resources/shaders/glsl120/cross_hatching.fs | 2 -- examples/shaders/resources/shaders/glsl120/cross_stitching.fs | 2 -- examples/shaders/resources/shaders/glsl120/distortion.fs | 2 -- examples/shaders/resources/shaders/glsl120/dream_vision.fs | 2 -- examples/shaders/resources/shaders/glsl120/fisheye.fs | 2 -- examples/shaders/resources/shaders/glsl120/grayscale.fs | 2 -- examples/shaders/resources/shaders/glsl120/pixelizer.fs | 2 -- examples/shaders/resources/shaders/glsl120/posterization.fs | 2 -- examples/shaders/resources/shaders/glsl120/predator.fs | 2 -- examples/shaders/resources/shaders/glsl120/scanlines.fs | 2 -- examples/shaders/resources/shaders/glsl120/sobel.fs | 2 -- examples/shaders/resources/shaders/glsl120/swirl.fs | 2 -- 15 files changed, 30 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/resources/shaders/glsl120/base.fs b/examples/shaders/resources/shaders/glsl120/base.fs index 9c685eb9..18799f95 100644 --- a/examples/shaders/resources/shaders/glsl120/base.fs +++ b/examples/shaders/resources/shaders/glsl120/base.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/bloom.fs b/examples/shaders/resources/shaders/glsl120/bloom.fs index cd9f8cbe..c28836b0 100644 --- a/examples/shaders/resources/shaders/glsl120/bloom.fs +++ b/examples/shaders/resources/shaders/glsl120/bloom.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/blur.fs b/examples/shaders/resources/shaders/glsl120/blur.fs index e2b04ad1..99cddfc9 100644 --- a/examples/shaders/resources/shaders/glsl120/blur.fs +++ b/examples/shaders/resources/shaders/glsl120/blur.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/cross_hatching.fs b/examples/shaders/resources/shaders/glsl120/cross_hatching.fs index 17d9c6a3..46514120 100644 --- a/examples/shaders/resources/shaders/glsl120/cross_hatching.fs +++ b/examples/shaders/resources/shaders/glsl120/cross_hatching.fs @@ -1,7 +1,5 @@ # version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/cross_stitching.fs b/examples/shaders/resources/shaders/glsl120/cross_stitching.fs index fe60675a..dee4617e 100644 --- a/examples/shaders/resources/shaders/glsl120/cross_stitching.fs +++ b/examples/shaders/resources/shaders/glsl120/cross_stitching.fs @@ -1,7 +1,5 @@ # version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/distortion.fs b/examples/shaders/resources/shaders/glsl120/distortion.fs index 568c50bf..f9d7f1e5 100644 --- a/examples/shaders/resources/shaders/glsl120/distortion.fs +++ b/examples/shaders/resources/shaders/glsl120/distortion.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; diff --git a/examples/shaders/resources/shaders/glsl120/dream_vision.fs b/examples/shaders/resources/shaders/glsl120/dream_vision.fs index 93cec8fb..4ca2a863 100644 --- a/examples/shaders/resources/shaders/glsl120/dream_vision.fs +++ b/examples/shaders/resources/shaders/glsl120/dream_vision.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/fisheye.fs b/examples/shaders/resources/shaders/glsl120/fisheye.fs index afac3d4a..6f376ba3 100644 --- a/examples/shaders/resources/shaders/glsl120/fisheye.fs +++ b/examples/shaders/resources/shaders/glsl120/fisheye.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/grayscale.fs b/examples/shaders/resources/shaders/glsl120/grayscale.fs index 470a4222..4de60d22 100644 --- a/examples/shaders/resources/shaders/glsl120/grayscale.fs +++ b/examples/shaders/resources/shaders/glsl120/grayscale.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/pixelizer.fs b/examples/shaders/resources/shaders/glsl120/pixelizer.fs index cea6b5e0..6f741faf 100644 --- a/examples/shaders/resources/shaders/glsl120/pixelizer.fs +++ b/examples/shaders/resources/shaders/glsl120/pixelizer.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/posterization.fs b/examples/shaders/resources/shaders/glsl120/posterization.fs index a9a5f246..475b93a1 100644 --- a/examples/shaders/resources/shaders/glsl120/posterization.fs +++ b/examples/shaders/resources/shaders/glsl120/posterization.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/predator.fs b/examples/shaders/resources/shaders/glsl120/predator.fs index c471f89d..2999cc8d 100644 --- a/examples/shaders/resources/shaders/glsl120/predator.fs +++ b/examples/shaders/resources/shaders/glsl120/predator.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/scanlines.fs b/examples/shaders/resources/shaders/glsl120/scanlines.fs index f279f347..929c79eb 100644 --- a/examples/shaders/resources/shaders/glsl120/scanlines.fs +++ b/examples/shaders/resources/shaders/glsl120/scanlines.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/sobel.fs b/examples/shaders/resources/shaders/glsl120/sobel.fs index 128ca423..a3f3f2bf 100644 --- a/examples/shaders/resources/shaders/glsl120/sobel.fs +++ b/examples/shaders/resources/shaders/glsl120/sobel.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; diff --git a/examples/shaders/resources/shaders/glsl120/swirl.fs b/examples/shaders/resources/shaders/glsl120/swirl.fs index 7cb24e2a..0618e013 100644 --- a/examples/shaders/resources/shaders/glsl120/swirl.fs +++ b/examples/shaders/resources/shaders/glsl120/swirl.fs @@ -1,7 +1,5 @@ #version 120 -precision mediump float; - // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; -- cgit v1.2.3 From 7ef604fbf5c3ba03d72b1559656e209f929e442d Mon Sep 17 00:00:00 2001 From: Wilhem Barbier Date: Thu, 9 Nov 2017 20:47:22 +0100 Subject: Store the default shaders --- examples/shaders/shaders_postprocessing.c | 28 ++++---- src/rlgl.c | 115 +++++++++++++----------------- 2 files changed, 62 insertions(+), 81 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_postprocessing.c b/examples/shaders/shaders_postprocessing.c index 4aac5f91..f6ed8205 100644 --- a/examples/shaders/shaders_postprocessing.c +++ b/examples/shaders/shaders_postprocessing.c @@ -20,10 +20,8 @@ #if defined(PLATFORM_DESKTOP) #define GLSL_VERSION 330 - #define DEFAULT_VERTEX_SHADER "resources/shaders/glsl330/base.vs" #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB #define GLSL_VERSION 100 - #define DEFAULT_VERTEX_SHADER "resources/shaders/glsl100/base.vs" #endif #define MAX_POSTPRO_SHADERS 12 @@ -85,18 +83,18 @@ int main() // NOTE 2: We load the correct shader depending on GLSL version Shader shaders[MAX_POSTPRO_SHADERS]; - shaders[FX_GRAYSCALE] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); - shaders[FX_POSTERIZATION] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/posterization.fs", GLSL_VERSION)); - shaders[FX_DREAM_VISION] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/dream_vision.fs", GLSL_VERSION)); - shaders[FX_PIXELIZER] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/pixelizer.fs", GLSL_VERSION)); - shaders[FX_CROSS_HATCHING] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/cross_hatching.fs", GLSL_VERSION)); - shaders[FX_CROSS_STITCHING] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/cross_stitching.fs", GLSL_VERSION)); - shaders[FX_PREDATOR_VIEW] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/predator.fs", GLSL_VERSION)); - shaders[FX_SCANLINES] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/scanlines.fs", GLSL_VERSION)); - shaders[FX_FISHEYE] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/fisheye.fs", GLSL_VERSION)); - shaders[FX_SOBEL] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/sobel.fs", GLSL_VERSION)); - shaders[FX_BLOOM] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/bloom.fs", GLSL_VERSION)); - shaders[FX_BLUR] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/blur.fs", GLSL_VERSION)); + shaders[FX_GRAYSCALE] = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); + shaders[FX_POSTERIZATION] = LoadShader(0, FormatText("resources/shaders/glsl%i/posterization.fs", GLSL_VERSION)); + shaders[FX_DREAM_VISION] = LoadShader(0, FormatText("resources/shaders/glsl%i/dream_vision.fs", GLSL_VERSION)); + shaders[FX_PIXELIZER] = LoadShader(0, FormatText("resources/shaders/glsl%i/pixelizer.fs", GLSL_VERSION)); + shaders[FX_CROSS_HATCHING] = LoadShader(0, FormatText("resources/shaders/glsl%i/cross_hatching.fs", GLSL_VERSION)); + shaders[FX_CROSS_STITCHING] = LoadShader(0, FormatText("resources/shaders/glsl%i/cross_stitching.fs", GLSL_VERSION)); + shaders[FX_PREDATOR_VIEW] = LoadShader(0, FormatText("resources/shaders/glsl%i/predator.fs", GLSL_VERSION)); + shaders[FX_SCANLINES] = LoadShader(0, FormatText("resources/shaders/glsl%i/scanlines.fs", GLSL_VERSION)); + shaders[FX_FISHEYE] = LoadShader(0, FormatText("resources/shaders/glsl%i/fisheye.fs", GLSL_VERSION)); + shaders[FX_SOBEL] = LoadShader(0, FormatText("resources/shaders/glsl%i/sobel.fs", GLSL_VERSION)); + shaders[FX_BLOOM] = LoadShader(0, FormatText("resources/shaders/glsl%i/bloom.fs", GLSL_VERSION)); + shaders[FX_BLUR] = LoadShader(0, FormatText("resources/shaders/glsl%i/blur.fs", GLSL_VERSION)); int currentShader = FX_GRAYSCALE; @@ -177,4 +175,4 @@ int main() //-------------------------------------------------------------------------------------- return 0; -} \ No newline at end of file +} diff --git a/src/rlgl.c b/src/rlgl.c index 2dd5c544..c5c9493d 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -267,6 +267,10 @@ static Vector3 *tempBuffer; static int tempBufferCount = 0; static bool useTempBuffer = false; +// Shaders +static unsigned int defaultVertexShader; +static unsigned int defaultFragmentShader; + // Shader Programs static Shader defaultShader; // Basic shader, support vertex color and diffuse texture static Shader currentShader; // Shader to be used on rendering (by default, defaultShader) @@ -324,7 +328,8 @@ static int screenHeight; // Default framebuffer height //---------------------------------------------------------------------------------- #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) static void LoadTextureCompressed(unsigned char *data, int width, int height, int compressedFormat, int mipmapCount); -static unsigned int LoadShaderProgram(const char *vShaderStr, const char *fShaderStr); // Load custom shader strings and return program id +static unsigned int LoadShaderPartial(const char *shaderStr, int type); // Load custom shader and return shader id +static unsigned int LoadShaderProgram(unsigned int vertexShader, unsigned int fragmentShader); // Load custom shader strings and return program id static Shader LoadShaderDefault(void); // Load default shader (just vertex positioning and texture coloring) static void SetShaderDefaultLocations(Shader *shader); // Bind default shader locations (attributes and uniforms) @@ -2359,22 +2364,33 @@ Shader LoadShader(char *vsFileName, char *fsFileName) for (int i = 0; i < MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1; #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - // Shaders loading from external text file - char *vShaderStr = LoadText(vsFileName); - char *fShaderStr = LoadText(fsFileName); - - if ((vShaderStr != NULL) && (fShaderStr != NULL)) - { - shader.id = LoadShaderProgram(vShaderStr, fShaderStr); - // After shader loading, we TRY to set default location names - if (shader.id > 0) SetShaderDefaultLocations(&shader); + unsigned int vertexShader, fragmentShader; - // Shader strings must be freed + if (vsFileName == NULL) { + vertexShader = defaultVertexShader; + } else { + char *vShaderStr = LoadText(vsFileName); + vertexShader = LoadShaderPartial(vShaderStr, GL_VERTEX_SHADER); free(vShaderStr); + } + + if (fsFileName == NULL) { + fragmentShader = defaultVertexShader; + } else { + char* fShaderStr = LoadText(fsFileName); + fragmentShader = LoadShaderPartial(fShaderStr, GL_FRAGMENT_SHADER); free(fShaderStr); } + shader.id = LoadShaderProgram(vertexShader, fragmentShader); + + // After shader loading, we TRY to set default location names + if (shader.id > 0) SetShaderDefaultLocations(&shader); + + if (vertexShader != defaultVertexShader) glDeleteShader(vertexShader); + if (fragmentShader != defaultFragmentShader) glDeleteShader(fragmentShader); + if (shader.id == 0) { TraceLog(LOG_WARNING, "Custom shader could not be loaded"); @@ -3127,45 +3143,28 @@ static void LoadTextureCompressed(unsigned char *data, int width, int height, in } } -// Load custom shader strings and return program id -static unsigned int LoadShaderProgram(const char *vShaderStr, const char *fShaderStr) +static unsigned int LoadShaderPartial(const char *shaderStr, int type) { - unsigned int program = 0; - -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - GLuint vertexShader; - GLuint fragmentShader; - - vertexShader = glCreateShader(GL_VERTEX_SHADER); - fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - - const char *pvs = vShaderStr; - const char *pfs = fShaderStr; - - glShaderSource(vertexShader, 1, &pvs, NULL); - glShaderSource(fragmentShader, 1, &pfs, NULL); + unsigned int shader = glCreateShader(type); + glShaderSource(shader, 1, &shaderStr, NULL); GLint success = 0; - - glCompileShader(vertexShader); - - glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); + glCompileShader(shader); + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); if (success != GL_TRUE) { - TraceLog(LOG_WARNING, "[VSHDR ID %i] Failed to compile vertex shader...", vertexShader); - + TraceLog(LOG_WARNING, "[VSHDR ID %i] Failed to compile shader...", shader); int maxLength = 0; int length; - - glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength); + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); #ifdef _MSC_VER char *log = malloc(maxLength); #else char log[maxLength]; #endif - glGetShaderInfoLog(vertexShader, maxLength, &length, log); + glGetShaderInfoLog(shader, maxLength, &length, log); TraceLog(LOG_INFO, "%s", log); @@ -3173,36 +3172,19 @@ static unsigned int LoadShaderProgram(const char *vShaderStr, const char *fShade free(log); #endif } - else TraceLog(LOG_INFO, "[VSHDR ID %i] Vertex shader compiled successfully", vertexShader); - - glCompileShader(fragmentShader); - - glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); - - if (success != GL_TRUE) - { - TraceLog(LOG_WARNING, "[FSHDR ID %i] Failed to compile fragment shader...", fragmentShader); - - int maxLength = 0; - int length; - - glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength); + else TraceLog(LOG_INFO, "[VSHDR ID %i] Shader compiled successfully", shader); -#ifdef _MSC_VER - char *log = malloc(maxLength); -#else - char log[maxLength]; -#endif - glGetShaderInfoLog(fragmentShader, maxLength, &length, log); + return shader; +} - TraceLog(LOG_INFO, "%s", log); +// Load custom shader strings and return program id +static unsigned int LoadShaderProgram(unsigned int vertexShader, unsigned int fragmentShader) +{ + unsigned int program = 0; -#ifdef _MSC_VER - free(log); -#endif - } - else TraceLog(LOG_INFO, "[FSHDR ID %i] Fragment shader compiled successfully", fragmentShader); +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + GLint success = 0; program = glCreateProgram(); glAttachShader(program, vertexShader); @@ -3250,9 +3232,6 @@ static unsigned int LoadShaderProgram(const char *vShaderStr, const char *fShade program = 0; } else TraceLog(LOG_INFO, "[SHDR ID %i] Shader program loaded successfully", program); - - glDeleteShader(vertexShader); - glDeleteShader(fragmentShader); #endif return program; } @@ -3326,7 +3305,9 @@ static Shader LoadShaderDefault(void) #endif "} \n"; - shader.id = LoadShaderProgram(vDefaultShaderStr, fDefaultShaderStr); + defaultVertexShader = LoadShaderPartial(vDefaultShaderStr, GL_VERTEX_SHADER); + defaultFragmentShader = LoadShaderPartial(fDefaultShaderStr, GL_FRAGMENT_SHADER); + shader.id = LoadShaderProgram(defaultVertexShader, defaultFragmentShader); if (shader.id > 0) { @@ -3388,6 +3369,8 @@ static void UnloadShaderDefault(void) { glUseProgram(0); + glDeleteShader(defaultVertexShader); + glDeleteShader(defaultFragmentShader); //glDetachShader(defaultShader, vertexShader); //glDetachShader(defaultShader, fragmentShader); //glDeleteShader(vertexShader); // Already deleted on shader compilation -- cgit v1.2.3 From 09228752ce2695baf79cbc2edccc60fabb38f783 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 12 Nov 2017 10:33:44 +0100 Subject: Review default shaders usage on loading --- examples/shaders/shaders_postprocessing.c | 1 + src/rlgl.c | 82 ++++++++++++++++--------------- src/shader_distortion.h | 4 +- 3 files changed, 45 insertions(+), 42 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_postprocessing.c b/examples/shaders/shaders_postprocessing.c index f6ed8205..b8b5f6ae 100644 --- a/examples/shaders/shaders_postprocessing.c +++ b/examples/shaders/shaders_postprocessing.c @@ -83,6 +83,7 @@ int main() // NOTE 2: We load the correct shader depending on GLSL version Shader shaders[MAX_POSTPRO_SHADERS]; + // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader shaders[FX_GRAYSCALE] = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); shaders[FX_POSTERIZATION] = LoadShader(0, FormatText("resources/shaders/glsl%i/posterization.fs", GLSL_VERSION)); shaders[FX_DREAM_VISION] = LoadShader(0, FormatText("resources/shaders/glsl%i/dream_vision.fs", GLSL_VERSION)); diff --git a/src/rlgl.c b/src/rlgl.c index c5c9493d..518b0470 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -268,10 +268,8 @@ static int tempBufferCount = 0; static bool useTempBuffer = false; // Shaders -static unsigned int defaultVertexShader; -static unsigned int defaultFragmentShader; - -// Shader Programs +static unsigned int defaultVShaderId; // Default vertex shader id (used by default shader program) +static unsigned int defaultFShaderId; // Default fragment shader Id (used by default shader program) static Shader defaultShader; // Basic shader, support vertex color and diffuse texture static Shader currentShader; // Shader to be used on rendering (by default, defaultShader) @@ -328,8 +326,9 @@ static int screenHeight; // Default framebuffer height //---------------------------------------------------------------------------------- #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) static void LoadTextureCompressed(unsigned char *data, int width, int height, int compressedFormat, int mipmapCount); -static unsigned int LoadShaderPartial(const char *shaderStr, int type); // Load custom shader and return shader id -static unsigned int LoadShaderProgram(unsigned int vertexShader, unsigned int fragmentShader); // Load custom shader strings and return program id + +static unsigned int CompileShader(const char *shaderStr, int type); // Compile custom shader and return shader id +static unsigned int LoadShaderProgram(unsigned int vShaderId, unsigned int fShaderId); // Load custom shader program static Shader LoadShaderDefault(void); // Load default shader (just vertex positioning and texture coloring) static void SetShaderDefaultLocations(Shader *shader); // Bind default shader locations (attributes and uniforms) @@ -2356,6 +2355,7 @@ char *LoadText(const char *fileName) } // Load shader from files and bind default locations +// NOTE: If shader string is NULL, using default vertex/fragment shaders Shader LoadShader(char *vsFileName, char *fsFileName) { Shader shader = { 0 }; @@ -2365,31 +2365,28 @@ Shader LoadShader(char *vsFileName, char *fsFileName) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - unsigned int vertexShader, fragmentShader; + unsigned int vertexShaderId, fragmentShaderId; - if (vsFileName == NULL) { - vertexShader = defaultVertexShader; - } else { + if (vsFileName == NULL) vertexShaderId = defaultVShaderId; + else + { char *vShaderStr = LoadText(vsFileName); - vertexShader = LoadShaderPartial(vShaderStr, GL_VERTEX_SHADER); + vertexShaderId = CompileShader(vShaderStr, GL_VERTEX_SHADER); free(vShaderStr); } - if (fsFileName == NULL) { - fragmentShader = defaultVertexShader; - } else { + if (fsFileName == NULL) fragmentShaderId = defaultVShaderId; + else + { char* fShaderStr = LoadText(fsFileName); - fragmentShader = LoadShaderPartial(fShaderStr, GL_FRAGMENT_SHADER); + fragmentShaderId = CompileShader(fShaderStr, GL_FRAGMENT_SHADER); free(fShaderStr); } - shader.id = LoadShaderProgram(vertexShader, fragmentShader); - - // After shader loading, we TRY to set default location names - if (shader.id > 0) SetShaderDefaultLocations(&shader); + shader.id = LoadShaderProgram(vertexShaderId, fragmentShaderId); - if (vertexShader != defaultVertexShader) glDeleteShader(vertexShader); - if (fragmentShader != defaultFragmentShader) glDeleteShader(fragmentShader); + if (vertexShaderId != defaultVShaderId) glDeleteShader(vertexShaderId); + if (fragmentShaderId != defaultFShaderId) glDeleteShader(fragmentShaderId); if (shader.id == 0) { @@ -2397,6 +2394,8 @@ Shader LoadShader(char *vsFileName, char *fsFileName) shader = defaultShader; } + // After shader loading, we TRY to set default location names + if (shader.id > 0) SetShaderDefaultLocations(&shader); // Get available shader uniforms // NOTE: This information is useful for debug... @@ -2421,7 +2420,6 @@ Shader LoadShader(char *vsFileName, char *fsFileName) TraceLog(LOG_DEBUG, "[SHDR ID %i] Active uniform [%s] set at location: %i", shader.id, name, location); } - #endif return shader; @@ -2929,7 +2927,10 @@ void InitVrSimulator(VrDeviceInfo info) #if defined(SUPPORT_DISTORTION_SHADER) // Load distortion shader (initialized by default with Oculus Rift CV1 parameters) - vrConfig.distortionShader.id = LoadShaderProgram(vDistortionShaderStr, fDistortionShaderStr); + unsigned int vertexShaderId = CompileShader(distortionVShaderStr, GL_VERTEX_SHADER); + unsigned int fragmentShaderId = CompileShader(distortionFShaderStr, GL_FRAGMENT_SHADER); + + vrConfig.distortionShader.id = LoadShaderProgram(vertexShaderId, fragmentShaderId); if (vrConfig.distortionShader.id > 0) SetShaderDefaultLocations(&vrConfig.distortionShader); #endif @@ -3143,7 +3144,8 @@ static void LoadTextureCompressed(unsigned char *data, int width, int height, in } } -static unsigned int LoadShaderPartial(const char *shaderStr, int type) +// Compile custom shader and return shader id +static unsigned int CompileShader(const char *shaderStr, int type) { unsigned int shader = glCreateShader(type); glShaderSource(shader, 1, &shaderStr, NULL); @@ -3154,7 +3156,7 @@ static unsigned int LoadShaderPartial(const char *shaderStr, int type) if (success != GL_TRUE) { - TraceLog(LOG_WARNING, "[VSHDR ID %i] Failed to compile shader...", shader); + TraceLog(LOG_WARNING, "[SHDR ID %i] Failed to compile shader...", shader); int maxLength = 0; int length; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); @@ -3172,13 +3174,13 @@ static unsigned int LoadShaderPartial(const char *shaderStr, int type) free(log); #endif } - else TraceLog(LOG_INFO, "[VSHDR ID %i] Shader compiled successfully", shader); + else TraceLog(LOG_INFO, "[SHDR ID %i] Shader compiled successfully", shader); return shader; } // Load custom shader strings and return program id -static unsigned int LoadShaderProgram(unsigned int vertexShader, unsigned int fragmentShader) +static unsigned int LoadShaderProgram(unsigned int vShaderId, unsigned int fShaderId) { unsigned int program = 0; @@ -3187,8 +3189,8 @@ static unsigned int LoadShaderProgram(unsigned int vertexShader, unsigned int fr GLint success = 0; program = glCreateProgram(); - glAttachShader(program, vertexShader); - glAttachShader(program, fragmentShader); + glAttachShader(program, vShaderId); + glAttachShader(program, fShaderId); // NOTE: Default attribute shader locations must be binded before linking glBindAttribLocation(program, 0, DEFAULT_ATTRIB_POSITION_NAME); @@ -3247,7 +3249,7 @@ static Shader LoadShaderDefault(void) for (int i = 0; i < MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1; // Vertex shader directly defined, no external file required - char vDefaultShaderStr[] = + char defaultVShaderStr[] = #if defined(GRAPHICS_API_OPENGL_21) "#version 120 \n" #elif defined(GRAPHICS_API_OPENGL_ES2) @@ -3276,7 +3278,7 @@ static Shader LoadShaderDefault(void) "} \n"; // Fragment shader directly defined, no external file required - char fDefaultShaderStr[] = + char defaultFShaderStr[] = #if defined(GRAPHICS_API_OPENGL_21) "#version 120 \n" #elif defined(GRAPHICS_API_OPENGL_ES2) @@ -3305,9 +3307,11 @@ static Shader LoadShaderDefault(void) #endif "} \n"; - defaultVertexShader = LoadShaderPartial(vDefaultShaderStr, GL_VERTEX_SHADER); - defaultFragmentShader = LoadShaderPartial(fDefaultShaderStr, GL_FRAGMENT_SHADER); - shader.id = LoadShaderProgram(defaultVertexShader, defaultFragmentShader); + // NOTE: Compiled vertex/fragment shaders are kept for re-use + defaultVShaderId = CompileShader(defaultVShaderStr, GL_VERTEX_SHADER); // Compile default vertex shader + defaultFShaderId = CompileShader(defaultFShaderStr, GL_FRAGMENT_SHADER); // Compile default fragment shader + + shader.id = LoadShaderProgram(defaultVShaderId, defaultFShaderId); if (shader.id > 0) { @@ -3369,12 +3373,10 @@ static void UnloadShaderDefault(void) { glUseProgram(0); - glDeleteShader(defaultVertexShader); - glDeleteShader(defaultFragmentShader); - //glDetachShader(defaultShader, vertexShader); - //glDetachShader(defaultShader, fragmentShader); - //glDeleteShader(vertexShader); // Already deleted on shader compilation - //glDeleteShader(fragmentShader); // Already deleted on shader compilation + glDetachShader(defaultShader.id, defaultVShaderId); + glDetachShader(defaultShader.id, defaultFShaderId); + glDeleteShader(defaultVShaderId); + glDeleteShader(defaultFShaderId); glDeleteProgram(defaultShader.id); } diff --git a/src/shader_distortion.h b/src/shader_distortion.h index 7a2c994b..3b37c630 100644 --- a/src/shader_distortion.h +++ b/src/shader_distortion.h @@ -1,6 +1,6 @@ // Vertex shader definition to embed, no external file required -static const char vDistortionShaderStr[] = +static const char distortionVShaderStr[] = #if defined(GRAPHICS_API_OPENGL_21) "#version 120 \n" #elif defined(GRAPHICS_API_OPENGL_ES2) @@ -29,7 +29,7 @@ static const char vDistortionShaderStr[] = "} \n"; // Fragment shader definition to embed, no external file required -static const char fDistortionShaderStr[] = +static const char distortionFShaderStr[] = #if defined(GRAPHICS_API_OPENGL_21) "#version 120 \n" #elif defined(GRAPHICS_API_OPENGL_ES2) -- cgit v1.2.3