summaryrefslogtreecommitdiff
path: root/drivers/gles3/shaders
diff options
context:
space:
mode:
authorBastiaan Olij <mux213@gmail.com>2018-10-01 06:51:50 +1000
committerBastiaan Olij <mux213@gmail.com>2018-10-02 17:14:43 +1000
commita6df366b238d93cabc73cb37129cb8979287661a (patch)
treeb948aa205acc9d9e3abeff59adfd11360b7c773e /drivers/gles3/shaders
parenta16dc807e535969f041667442b328232bba0cf3e (diff)
Moving lens distortion shader into drivers and adding GLES2 support
Diffstat (limited to 'drivers/gles3/shaders')
-rw-r--r--drivers/gles3/shaders/SCsub1
-rw-r--r--drivers/gles3/shaders/lens_distorted.glsl64
2 files changed, 65 insertions, 0 deletions
diff --git a/drivers/gles3/shaders/SCsub b/drivers/gles3/shaders/SCsub
index f1811fa7b5..27fd1514e7 100644
--- a/drivers/gles3/shaders/SCsub
+++ b/drivers/gles3/shaders/SCsub
@@ -20,3 +20,4 @@ if 'GLES3_GLSL' in env['BUILDERS']:
env.GLES3_GLSL('exposure.glsl');
env.GLES3_GLSL('tonemap.glsl');
env.GLES3_GLSL('particles.glsl');
+ env.GLES3_GLSL('lens_distorted.glsl');
diff --git a/drivers/gles3/shaders/lens_distorted.glsl b/drivers/gles3/shaders/lens_distorted.glsl
new file mode 100644
index 0000000000..7b9d0b347f
--- /dev/null
+++ b/drivers/gles3/shaders/lens_distorted.glsl
@@ -0,0 +1,64 @@
+/* clang-format off */
+[vertex]
+
+layout(location = 0) in highp vec4 vertex_attrib;
+/* clang-format on */
+
+uniform vec2 offset;
+uniform vec2 scale;
+
+out vec2 uv_interp;
+
+void main() {
+
+ uv_interp = vertex_attrib.xy * 2.0 - 1.0;
+
+ vec2 v = vertex_attrib.xy * scale + offset;
+ gl_Position = vec4(v, 0.0, 1.0);
+}
+
+/* clang-format off */
+[fragment]
+
+uniform sampler2D source; //texunit:0
+/* clang-format on */
+
+uniform vec2 eye_center;
+uniform float k1;
+uniform float k2;
+uniform float upscale;
+uniform float aspect_ratio;
+
+in vec2 uv_interp;
+
+layout(location = 0) out vec4 frag_color;
+
+void main() {
+ vec2 coords = uv_interp;
+ vec2 offset = coords - eye_center;
+
+ // take aspect ratio into account
+ offset.y /= aspect_ratio;
+
+ // distort
+ vec2 offset_sq = offset * offset;
+ float radius_sq = offset_sq.x + offset_sq.y;
+ float radius_s4 = radius_sq * radius_sq;
+ float distortion_scale = 1.0 + (k1 * radius_sq) + (k2 * radius_s4);
+ offset *= distortion_scale;
+
+ // reapply aspect ratio
+ offset.y *= aspect_ratio;
+
+ // add our eye center back in
+ coords = offset + eye_center;
+ coords /= upscale;
+
+ // and check our color
+ if (coords.x < -1.0 || coords.y < -1.0 || coords.x > 1.0 || coords.y > 1.0) {
+ frag_color = vec4(0.0, 0.0, 0.0, 1.0);
+ } else {
+ coords = (coords + vec2(1.0)) / vec2(2.0);
+ frag_color = textureLod(source, coords, 0.0);
+ }
+}