[Bf-blender-cvs] [0f97718c10e] blender2.8: DRW: Add option to only resolve framebuffer colors without depth test

Clément Foucault noreply at git.blender.org
Mon Jul 30 14:21:02 CEST 2018


Commit: 0f97718c10e8c2a441f58a51eb923b1dfa3698d4
Author: Clément Foucault
Date:   Mon Jul 30 13:56:22 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB0f97718c10e8c2a441f58a51eb923b1dfa3698d4

DRW: Add option to only resolve framebuffer colors without depth test

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

M	source/blender/draw/intern/DRW_render.h
M	source/blender/draw/intern/draw_manager.c
M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/intern/gpu_shader.c
M	source/blender/gpu/shaders/gpu_shader_image_multisample_resolve_frag.glsl

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

diff --git a/source/blender/draw/intern/DRW_render.h b/source/blender/draw/intern/DRW_render.h
index cd2db3cfd0a..d2c44cfef2a 100644
--- a/source/blender/draw/intern/DRW_render.h
+++ b/source/blender/draw/intern/DRW_render.h
@@ -120,7 +120,16 @@ typedef char DRWViewportEmptyList;
 	if (dfbl->multisample_fb != NULL) { \
 		DRW_stats_query_start("Multisample Resolve"); \
 		GPU_framebuffer_bind(dfbl->default_fb); \
-		DRW_multisamples_resolve(dtxl->multisample_depth, dtxl->multisample_color); \
+		DRW_multisamples_resolve(dtxl->multisample_depth, dtxl->multisample_color, true); \
+		DRW_stats_query_end(); \
+	} \
+}
+
+#define MULTISAMPLE_SYNC_DISABLE_NO_DEPTH(dfbl, dtxl) { \
+	if (dfbl->multisample_fb != NULL) { \
+		DRW_stats_query_start("Multisample Resolve"); \
+		GPU_framebuffer_bind(dfbl->default_fb); \
+		DRW_multisamples_resolve(dtxl->multisample_depth, dtxl->multisample_color, false); \
 		DRW_stats_query_end(); \
 	} \
 }
@@ -228,7 +237,7 @@ void DRW_uniformbuffer_free(struct GPUUniformBuffer *ubo);
 void DRW_transform_to_display(struct GPUTexture *tex);
 void DRW_transform_none(struct GPUTexture *tex);
 void DRW_multisamples_resolve(
-        struct GPUTexture *src_depth, struct GPUTexture *src_color);
+        struct GPUTexture *src_depth, struct GPUTexture *src_color, bool use_depth);
 
 /* Shaders */
 struct GPUShader *DRW_shader_create(
diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c
index 0a15fb3a114..87239e7d93e 100644
--- a/source/blender/draw/intern/draw_manager.c
+++ b/source/blender/draw/intern/draw_manager.c
@@ -317,10 +317,14 @@ void DRW_transform_none(GPUTexture *tex)
 /* Use manual multisample resolve pass.
  * Much quicker than blitting back and forth.
  * Assume destination fb is bound*/
-void DRW_multisamples_resolve(GPUTexture *src_depth, GPUTexture *src_color)
+void DRW_multisamples_resolve(GPUTexture *src_depth, GPUTexture *src_color, bool use_depth)
 {
-	drw_state_set(DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_PREMUL |
-	              DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
+	DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_PREMUL;
+
+	if (use_depth) {
+		state |= DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
+	}
+	drw_state_set(state);
 
 	int samples = GPU_texture_samples(src_depth);
 
@@ -330,22 +334,39 @@ void DRW_multisamples_resolve(GPUTexture *src_depth, GPUTexture *src_color)
 	GPUBatch *geom = DRW_cache_fullscreen_quad_get();
 
 	int builtin;
-	switch (samples) {
-		case 2:  builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_2; break;
-		case 4:  builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_4; break;
-		case 8:  builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_8; break;
-		case 16: builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_16; break;
-		default:
-			BLI_assert(0);
-			builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_2;
-			break;
+	if (use_depth) {
+		switch (samples) {
+			case 2:  builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST; break;
+			case 4:  builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_4_DEPTH_TEST; break;
+			case 8:  builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_8_DEPTH_TEST; break;
+			case 16: builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_16_DEPTH_TEST; break;
+			default:
+				BLI_assert("Mulisample count unsupported by blit shader.");
+				builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST;
+				break;
+		}
+	}
+	else {
+		switch (samples) {
+			case 2:  builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_2; break;
+			case 4:  builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_4; break;
+			case 8:  builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_8; break;
+			case 16: builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_16; break;
+			default:
+				BLI_assert("Mulisample count unsupported by blit shader.");
+				builtin = GPU_SHADER_2D_IMAGE_MULTISAMPLE_2;
+				break;
+		}
 	}
 
 	GPU_batch_program_set_builtin(geom, builtin);
 
-	GPU_texture_bind(src_depth, 0);
+	if (use_depth) {
+		GPU_texture_bind(src_depth, 0);
+		GPU_batch_uniform_1i(geom, "depthMulti", 0);
+	}
+
 	GPU_texture_bind(src_color, 1);
-	GPU_batch_uniform_1i(geom, "depthMulti", 0);
 	GPU_batch_uniform_1i(geom, "colorMulti", 1);
 
 	float mat[4][4];
diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h
index b1a05faf863..7f334cec21f 100644
--- a/source/blender/gpu/GPU_shader.h
+++ b/source/blender/gpu/GPU_shader.h
@@ -158,6 +158,10 @@ typedef enum GPUBuiltinShader {
 	GPU_SHADER_2D_IMAGE_MULTISAMPLE_4,
 	GPU_SHADER_2D_IMAGE_MULTISAMPLE_8,
 	GPU_SHADER_2D_IMAGE_MULTISAMPLE_16,
+	GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST,
+	GPU_SHADER_2D_IMAGE_MULTISAMPLE_4_DEPTH_TEST,
+	GPU_SHADER_2D_IMAGE_MULTISAMPLE_8_DEPTH_TEST,
+	GPU_SHADER_2D_IMAGE_MULTISAMPLE_16_DEPTH_TEST,
 	GPU_SHADER_2D_CHECKER,
 	GPU_SHADER_2D_DIAG_STRIPES,
 	/* for simple 3D drawing */
diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c
index 04b43d03c83..3543c73f71d 100644
--- a/source/blender/gpu/intern/gpu_shader.c
+++ b/source/blender/gpu/intern/gpu_shader.c
@@ -738,6 +738,10 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
 		[GPU_SHADER_2D_IMAGE_MULTISAMPLE_4] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
 		[GPU_SHADER_2D_IMAGE_MULTISAMPLE_8] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
 		[GPU_SHADER_2D_IMAGE_MULTISAMPLE_16] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
+		[GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
+		[GPU_SHADER_2D_IMAGE_MULTISAMPLE_4_DEPTH_TEST] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
+		[GPU_SHADER_2D_IMAGE_MULTISAMPLE_8_DEPTH_TEST] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
+		[GPU_SHADER_2D_IMAGE_MULTISAMPLE_16_DEPTH_TEST] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
 
 		[GPU_SHADER_2D_IMAGE_INTERLACE] = { datatoc_gpu_shader_2D_image_vert_glsl,
 		                                    datatoc_gpu_shader_image_interlace_frag_glsl },
@@ -873,15 +877,31 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
 			case GPU_SHADER_2D_IMAGE_MULTISAMPLE_2:
 				defines = "#define SAMPLES 2\n";
 				break;
+			case GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST:
+				defines = "#define SAMPLES 2\n"
+				          "#define USE_DEPTH\n";
+				break;
 			case GPU_SHADER_2D_IMAGE_MULTISAMPLE_4:
 				defines = "#define SAMPLES 4\n";
 				break;
+			case GPU_SHADER_2D_IMAGE_MULTISAMPLE_4_DEPTH_TEST:
+				defines = "#define SAMPLES 4\n"
+				          "#define USE_DEPTH\n";
+				break;
 			case GPU_SHADER_2D_IMAGE_MULTISAMPLE_8:
 				defines = "#define SAMPLES 8\n";
 				break;
+			case GPU_SHADER_2D_IMAGE_MULTISAMPLE_8_DEPTH_TEST:
+				defines = "#define SAMPLES 8\n"
+				          "#define USE_DEPTH\n";
+				break;
 			case GPU_SHADER_2D_IMAGE_MULTISAMPLE_16:
 				defines = "#define SAMPLES 16\n";
 				break;
+			case GPU_SHADER_2D_IMAGE_MULTISAMPLE_16_DEPTH_TEST:
+				defines = "#define SAMPLES 16\n"
+				          "#define USE_DEPTH\n";
+				break;
 			case GPU_SHADER_2D_WIDGET_BASE_INST:
 			case GPU_SHADER_2D_NODELINK_INST:
 				defines = "#define USE_INSTANCE\n";
diff --git a/source/blender/gpu/shaders/gpu_shader_image_multisample_resolve_frag.glsl b/source/blender/gpu/shaders/gpu_shader_image_multisample_resolve_frag.glsl
index 57362c88320..1f59c9dfdbd 100644
--- a/source/blender/gpu/shaders/gpu_shader_image_multisample_resolve_frag.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_image_multisample_resolve_frag.glsl
@@ -8,8 +8,6 @@ out vec4 fragColor;
 #error "Too many samples"
 #endif
 
-// #define USE_DEPTH_WEIGHTING
-
 void main()
 {
 	ivec2 texel = ivec2(gl_FragCoord.xy);
@@ -19,26 +17,26 @@ void main()
 	vec4 d1, d2, d3, d4;
 	vec4 c1, c2, c3, c4, c5, c6, c7, c8;
 	vec4 c9, c10, c11, c12, c13, c14, c15, c16;
-	d1 = d2 = d3 = d4 = vec4(1.0);
+	d1 = d2 = d3 = d4 = vec4(0.5);
 	w1 = w2 = w3 = w4 = vec4(0.0);
 	c1 = c2 = c3 = c4 = c5 = c6 = c7 = c8 = vec4(0.0);
 	c9 = c10 = c11 = c12 = c13 = c14 = c15 = c16 = vec4(0.0);
 
+#ifdef USE_DEPTH
 	/* Depth */
-
 	d1.x = texelFetch(depthMulti, texel, 0).r;
 	d1.y = texelFetch(depthMulti, texel, 1).r;
-#if SAMPLES > 2
+#  if SAMPLES > 2
 	d1.z = texelFetch(depthMulti, texel, 2).r;
 	d1.w = texelFetch(depthMulti, texel, 3).r;
-#endif
-#if SAMPLES > 4
+#  endif
+#  if SAMPLES > 4
 	d2.x = texelFetch(depthMulti, texel, 4).r;
 	d2.y = texelFetch(depthMulti, texel, 5).r;
 	d2.z = texelFetch(depthMulti, texel, 6).r;
 	d2.w = texelFetch(depthMulti, texel, 7).r;
-#endif
-#if SAMPLES > 8
+#  endif
+#  if SAMPLES > 8
 	d3.x = texelFetch(depthMulti, texel, 8).r;
 	d3.y = texelFetch(depthMulti, texel, 9).r;
 	d3.z = texelFetch(depthMulti, texel, 10).r;
@@ -47,6 +45,7 @@ void main()
 	d4.y = texelFetch(depthMulti, texel, 13).r;
 	d4.z = texelFetch(depthMulti, texel, 14).r;
 	d4.w = texelFetch(depthMulti, texel, 15).r;
+#  endif
 #endif
 
 	/* COLOR */
@@ -89,22 +88,26 @@ void main()
 	}
 #endif
 
-#if SAMPLES > 8
-	d1 = min(d1, min(d3, d4));
-#endif
-#if SAMPLES > 4
-	d1 = min(d1, d2);
-#endif
-#if SAMPLES > 2
-	d1.xy = min(d1.xy, d1.zw);
-#endif
-	gl_FragDepth = min(d1.x, d1.y);
-
-#ifdef USE_DEPTH_WEIGHTING
-	c1  *= w1.x; c2  *= w1.y; c3  *= w1.z; c4  *= w1.w;
-	c5  *= w2.x; c6  *= w2.y; c7  *= w2.z; c8  *= w2.w;
-	c9  *= w3.x; c10 *= w3.y; c11 *= w3.z; c12 *= w3.w;
-	c13 *= w4.x; c14 *= w4.y; c15 *= w4.z; c16 *= w4.w;
+#ifdef USE_DEPTH
+	d1 *= 1.0 - step(1.0, d1); /* make far plane depth = 0 */
+#  if SAMPLES > 8
+	d4 *= 1.0 - step(1.0, d4);
+	d3 *= 1.0 - step(1.0, d3);
+	d1 = max(d1, max(d3, d4));
+#  endif
+#  if SAMPLES > 4
+	d2 *= 1.0 - step(1.0, d2);
+	d1 = max(d1, d2);
+	d1 = max(d1, d2);
+#  endif
+#  if SAMPLES > 2
+	d1.xy = max(d1.xy, d1.zw);
+#  endif
+	gl_FragDepth = max(d1.x, d1.y);
+	/* Don't let the 0.0 farplane occlude other things */
+	if (gl_FragDepth == 0.0) {
+		gl_FragDepth = 1.0;
+	}
 #endif
 
 	c1 =  c1 + c2;



More information about the Bf-blender-cvs mailing list