[Bf-blender-cvs] [4424a8e] HMD_viewport: Use non-fixed screen size for lens distortion shader

Julian Eisel noreply at git.blender.org
Wed Apr 13 00:46:48 CEST 2016


Commit: 4424a8e8448a5aa1146d0717331d83ac9af5b9cb
Author: Julian Eisel
Date:   Wed Apr 13 00:45:14 2016 +0200
Branches: HMD_viewport
https://developer.blender.org/rB4424a8e8448a5aa1146d0717331d83ac9af5b9cb

Use non-fixed screen size for lens distortion shader

Was assuming 1920x1080. Also minor cleanup.

===================================================================

M	source/blender/editors/space_view3d/view3d_draw.c
M	source/blender/gpu/GPU_compositing.h
M	source/blender/gpu/intern/gpu_compositing.c
M	source/blender/gpu/shaders/gpu_shader_fx_lensdistortion_frag.glsl

===================================================================

diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c
index 16e8412..4be6e64 100644
--- a/source/blender/editors/space_view3d/view3d_draw.c
+++ b/source/blender/editors/space_view3d/view3d_draw.c
@@ -3265,7 +3265,7 @@ void ED_view3d_draw_offscreen(
 	if (do_compositing) {
 		if (!winmat)
 			is_persp = rv3d->is_persp;
-		GPU_fx_do_composite_pass(fx, winmat, is_persp, scene, ofs);
+		GPU_fx_do_composite_pass(fx, winmat, is_persp, scene, ofs, &ar->winx);
 	}
 
 	if ((v3d->flag2 & V3D_RENDER_SHADOW) == 0) {
@@ -3906,7 +3906,7 @@ static void view3d_main_region_draw_objects(const bContext *C, Scene *scene, Vie
 
 	/* post process */
 	if (do_compositing) {
-		GPU_fx_do_composite_pass(rv3d->compositor, rv3d->winmat, rv3d->is_persp, scene, NULL);
+		GPU_fx_do_composite_pass(rv3d->compositor, rv3d->winmat, rv3d->is_persp, scene, NULL, &ar->winx);
 	}
 
 	/* Disable back anti-aliasing */
diff --git a/source/blender/gpu/GPU_compositing.h b/source/blender/gpu/GPU_compositing.h
index 48e1b93..2b69ea5 100644
--- a/source/blender/gpu/GPU_compositing.h
+++ b/source/blender/gpu/GPU_compositing.h
@@ -90,7 +90,8 @@ bool GPU_fx_compositor_initialize_passes(
 /* do compositing on the fx passes that have been initialized */
 bool GPU_fx_do_composite_pass(
         GPUFX *fx, float projmat[4][4], bool is_persp,
-        struct Scene *scene, struct GPUOffScreen *ofs);
+        struct Scene *scene, struct GPUOffScreen *ofs,
+        const short region_size[2]);
 
 /* bind new depth buffer for XRay pass */
 void GPU_fx_compositor_setup_XRay_pass(GPUFX *fx, bool do_xray);
diff --git a/source/blender/gpu/intern/gpu_compositing.c b/source/blender/gpu/intern/gpu_compositing.c
index 0af9949..06514c2 100644
--- a/source/blender/gpu/intern/gpu_compositing.c
+++ b/source/blender/gpu/intern/gpu_compositing.c
@@ -731,7 +731,8 @@ void GPU_fx_compositor_XRay_resolve(GPUFX *fx)
 
 bool GPU_fx_do_composite_pass(
         GPUFX *fx, float projmat[4][4], bool is_persp,
-        struct Scene *scene, struct GPUOffScreen *ofs)
+        struct Scene *scene, struct GPUOffScreen *ofs,
+        const short region_size[2])
 {
 	GPUTexture *src, *target;
 	int numslots = 0;
@@ -1291,21 +1292,24 @@ bool GPU_fx_do_composite_pass(
 			}
 		}
 	}
-    /* third pass, Lens Distortion */
-    if(fx->effects & GPU_FX_FLAG_LensDist) {
-        GPUShader *lensdist_shader;
-		lensdist_shader = GPU_shader_get_builtin_fx_shader(GPU_SHADER_FX_LENS_DISTORTION, is_persp);
+
+	/* third pass, Lens Distortion */
+	if(fx->effects & GPU_FX_FLAG_LensDist) {
+		GPUShader *lensdist_shader = GPU_shader_get_builtin_fx_shader(GPU_SHADER_FX_LENS_DISTORTION, is_persp);
+
 		if (lensdist_shader) {
+			const int color_uniform = GPU_shader_get_uniform(lensdist_shader, "warpTexture");
+			const int regionsize_uniform = GPU_shader_get_uniform(lensdist_shader, "RegionSize");
+			const float region_size_f[2] = {UNPACK2(region_size)};
 
-            int color_uniform;
-            color_uniform = GPU_shader_get_uniform(lensdist_shader, "warpTexture");
+			GPU_shader_bind(lensdist_shader);
 
-            GPU_shader_bind(lensdist_shader);
+			GPU_shader_uniform_vector(lensdist_shader, regionsize_uniform, 2, 1, region_size_f);
 
 			GPU_texture_bind(src, numslots++);
 			GPU_shader_uniform_texture(lensdist_shader, color_uniform, src);
 
-            /* draw */
+			/* draw */
 			gpu_fx_bind_render_target(&passes_left, fx, ofs, target);
 
 			glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@@ -1313,7 +1317,7 @@ bool GPU_fx_do_composite_pass(
 			/* disable bindings */
 			GPU_texture_unbind(src);
 
-            /* may not be attached, in that case this just returns */
+			/* may not be attached, in that case this just returns */
 			if (target) {
 				GPU_framebuffer_texture_detach(target);
 				if (ofs) {
@@ -1327,9 +1331,8 @@ bool GPU_fx_do_composite_pass(
 			/* swap here, after src/target have been unbound */
 			SWAP(GPUTexture *, target, src);
 			numslots = 0;
-        }
-
-    }
+		}
+	}
 
 	glDisableClientState(GL_VERTEX_ARRAY);
 	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
diff --git a/source/blender/gpu/shaders/gpu_shader_fx_lensdistortion_frag.glsl b/source/blender/gpu/shaders/gpu_shader_fx_lensdistortion_frag.glsl
index 915a2e1..c32f1aa 100644
--- a/source/blender/gpu/shaders/gpu_shader_fx_lensdistortion_frag.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_fx_lensdistortion_frag.glsl
@@ -1,3 +1,4 @@
+uniform vec2 RegionSize;
 uniform sampler2D warpTexture;
 
 const vec2 LeftLensCenter = vec2(0.25, 0.5);
@@ -6,7 +7,7 @@ const vec2 LeftScreenCenter = vec2(0.25, 0.5);
 const vec2 RightScreenCenter = vec2(0.75, 0.5);
 const vec2 Scale = vec2(0.1469278, 0.2350845);
 const vec2 ScaleIn = vec2(4, 2.5);
-const vec4 HmdWarpParam   = vec4(1, 0.2, 0.1, 0);
+const vec4 HmdWarpParam = vec4(1, 0.2, 0.1, 0);
 const float aberr_r = 0.985;
 const float aberr_b = 1.015;
 // const vec4 HmdWarpParam   = vec4(1, 0.5, 0.05, 0);
@@ -14,36 +15,37 @@ const float aberr_b = 1.015;
 
 void main()
 {
+	vec2 RegionSize_h = RegionSize / 2;
 	// The following two variables need to be set per eye
-	vec2 LensCenter = gl_FragCoord.x < 960 ? LeftLensCenter : RightLensCenter;
-	vec2 ScreenCenter = gl_FragCoord.x < 960 ? LeftScreenCenter : RightScreenCenter;
+	vec2 LensCenter = gl_FragCoord.x < RegionSize_h.x ? LeftLensCenter : RightLensCenter;
+	vec2 ScreenCenter = gl_FragCoord.x < RegionSize_h.x ? LeftScreenCenter : RightScreenCenter;
 
-	vec2 oTexCoord = gl_FragCoord.xy / vec2(1920, 1080);
-    oTexCoord = vec2(oTexCoord.x, 1.0-oTexCoord.y);
+	vec2 oTexCoord = gl_FragCoord.xy / RegionSize;
+	oTexCoord = vec2(oTexCoord.x, 1.0 - oTexCoord.y);
 
 	vec2 theta = (oTexCoord - LensCenter) * ScaleIn; // Scales to [-1, 1]
 	float rSq = theta.x * theta.x + theta.y * theta.y;
 	vec2 rvector = theta * (HmdWarpParam.x + HmdWarpParam.y * rSq +
-		HmdWarpParam.z * rSq * rSq +
-		HmdWarpParam.w * rSq * rSq * rSq);
+	                        HmdWarpParam.z * rSq * rSq +
+	                        HmdWarpParam.w * rSq * rSq * rSq);
 	vec2 tc_r = LensCenter + aberr_r * Scale * rvector;
 	vec2 tc_g = LensCenter +           Scale * rvector;
 	vec2 tc_b = LensCenter + aberr_b * Scale * rvector;
-	tc_r.x = gl_FragCoord.x < 960 ? (2.0 * tc_r.x) : (2.0 * (tc_r.x - 0.5));
-	tc_g.x = gl_FragCoord.x < 960 ? (2.0 * tc_g.x) : (2.0 * (tc_g.x - 0.5));
-	tc_b.x = gl_FragCoord.x < 960 ? (2.0 * tc_b.x) : (2.0 * (tc_b.x - 0.5));
+	tc_r.x = gl_FragCoord.x < RegionSize_h.x ? (2.0 * tc_r.x) : (2.0 * (tc_r.x - 0.5));
+	tc_g.x = gl_FragCoord.x < RegionSize_h.x ? (2.0 * tc_g.x) : (2.0 * (tc_g.x - 0.5));
+	tc_b.x = gl_FragCoord.x < RegionSize_h.x ? (2.0 * tc_b.x) : (2.0 * (tc_b.x - 0.5));
 
 	float rval = 0.0;
 	float gval = 0.0;
 	float bval = 0.0;
 	
-	tc_r.y = (1-tc_r.y);
-	tc_g.y = (1-tc_g.y);
-	tc_b.y = (1-tc_b.y);
+	tc_r.y = (1 - tc_r.y);
+	tc_g.y = (1 - tc_g.y);
+	tc_b.y = (1 - tc_b.y);
 
 	rval = texture2D(warpTexture, tc_r).x;
 	gval = texture2D(warpTexture, tc_g).y;
 	bval = texture2D(warpTexture, tc_b).z;
 
-	gl_FragColor = vec4(rval,gval,bval,1.0);
+	gl_FragColor = vec4(rval, gval, bval, 1.0);
 }




More information about the Bf-blender-cvs mailing list