summaryrefslogtreecommitdiff
path: root/drivers/gles3
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
parenta16dc807e535969f041667442b328232bba0cf3e (diff)
Moving lens distortion shader into drivers and adding GLES2 support
Diffstat (limited to 'drivers/gles3')
-rw-r--r--drivers/gles3/rasterizer_canvas_gles3.cpp34
-rw-r--r--drivers/gles3/rasterizer_canvas_gles3.h3
-rw-r--r--drivers/gles3/rasterizer_gles3.cpp20
-rw-r--r--drivers/gles3/rasterizer_gles3.h1
-rw-r--r--drivers/gles3/shaders/SCsub1
-rw-r--r--drivers/gles3/shaders/lens_distorted.glsl64
6 files changed, 123 insertions, 0 deletions
diff --git a/drivers/gles3/rasterizer_canvas_gles3.cpp b/drivers/gles3/rasterizer_canvas_gles3.cpp
index 856c83e297..a9b46baf53 100644
--- a/drivers/gles3/rasterizer_canvas_gles3.cpp
+++ b/drivers/gles3/rasterizer_canvas_gles3.cpp
@@ -1869,6 +1869,39 @@ void RasterizerCanvasGLES3::draw_generic_textured_rect(const Rect2 &p_rect, cons
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
+void RasterizerCanvasGLES3::draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {
+ Vector2 half_size;
+ if (storage->frame.current_rt) {
+ half_size = Vector2(storage->frame.current_rt->width, storage->frame.current_rt->height);
+ } else {
+ half_size = OS::get_singleton()->get_window_size();
+ }
+ half_size *= 0.5;
+ Vector2 offset((p_rect.position.x - half_size.x) / half_size.x, (p_rect.position.y - half_size.y) / half_size.y);
+ Vector2 scale(p_rect.size.x / half_size.x, p_rect.size.y / half_size.y);
+
+ float aspect_ratio = p_rect.size.x / p_rect.size.y;
+
+ // setup our lens shader
+ state.lens_shader.bind();
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::OFFSET, offset);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::SCALE, scale);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::K1, p_k1);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::K2, p_k2);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::EYE_CENTER, p_eye_center);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::UPSCALE, p_oversample);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::ASPECT_RATIO, aspect_ratio);
+
+ glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.canvas_item_ubo);
+ glBindVertexArray(data.canvas_quad_array);
+
+ // and draw
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+ glBindVertexArray(0);
+ glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0);
+}
+
void RasterizerCanvasGLES3::draw_window_margins(int *black_margin, RID *black_image) {
Vector2 window_size = OS::get_singleton()->get_window_size();
@@ -2058,6 +2091,7 @@ void RasterizerCanvasGLES3::initialize() {
state.canvas_shader.init();
state.canvas_shader.set_base_material_tex_index(2);
state.canvas_shadow_shader.init();
+ state.lens_shader.init();
state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows);
state.canvas_shadow_shader.set_conditional(CanvasShadowShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows);
diff --git a/drivers/gles3/rasterizer_canvas_gles3.h b/drivers/gles3/rasterizer_canvas_gles3.h
index bc4ea80328..3f306003b4 100644
--- a/drivers/gles3/rasterizer_canvas_gles3.h
+++ b/drivers/gles3/rasterizer_canvas_gles3.h
@@ -35,6 +35,7 @@
#include "servers/visual/rasterizer.h"
#include "shaders/canvas_shadow.glsl.gen.h"
+#include "shaders/lens_distorted.glsl.gen.h"
class RasterizerSceneGLES3;
@@ -72,6 +73,7 @@ public:
bool canvas_texscreen_used;
CanvasShaderGLES3 canvas_shader;
CanvasShadowShaderGLES3 canvas_shadow_shader;
+ LensDistortedShaderGLES3 lens_shader;
bool using_texture_rect;
bool using_ninepatch;
@@ -141,6 +143,7 @@ public:
virtual void reset_canvas();
void draw_generic_textured_rect(const Rect2 &p_rect, const Rect2 &p_src);
+ void draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample);
void initialize();
void finalize();
diff --git a/drivers/gles3/rasterizer_gles3.cpp b/drivers/gles3/rasterizer_gles3.cpp
index 6f13df621f..a97cd32faa 100644
--- a/drivers/gles3/rasterizer_gles3.cpp
+++ b/drivers/gles3/rasterizer_gles3.cpp
@@ -359,6 +359,26 @@ void RasterizerGLES3::blit_render_target_to_screen(RID p_render_target, const Re
#endif
}
+void RasterizerGLES3::output_lens_distorted_to_screen(RID p_render_target, const Rect2 &p_screen_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {
+ ERR_FAIL_COND(storage->frame.current_rt);
+
+ RasterizerStorageGLES3::RenderTarget *rt = storage->render_target_owner.getornull(p_render_target);
+ ERR_FAIL_COND(!rt);
+
+ glDisable(GL_BLEND);
+
+ // render to our framebuffer
+ glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
+
+ // output our texture
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, rt->color);
+
+ canvas->draw_lens_distortion_rect(p_screen_rect, p_k1, p_k2, p_eye_center, p_oversample);
+
+ glBindTexture(GL_TEXTURE_2D, 0);
+}
+
void RasterizerGLES3::end_frame(bool p_swap_buffers) {
if (OS::get_singleton()->is_layered_allowed()) {
diff --git a/drivers/gles3/rasterizer_gles3.h b/drivers/gles3/rasterizer_gles3.h
index 543011aff3..477e0dfd48 100644
--- a/drivers/gles3/rasterizer_gles3.h
+++ b/drivers/gles3/rasterizer_gles3.h
@@ -59,6 +59,7 @@ public:
virtual void restore_render_target();
virtual void clear_render_target(const Color &p_color);
virtual void blit_render_target_to_screen(RID p_render_target, const Rect2 &p_screen_rect, int p_screen = 0);
+ virtual void output_lens_distorted_to_screen(RID p_render_target, const Rect2 &p_screen_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample);
virtual void end_frame(bool p_swap_buffers);
virtual void finalize();
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);
+ }
+}