[Bf-blender-cvs] [0b98b668c48] temp-drawcontext: DRW: Fix DRW_draw_select_loop and DRW_draw_depth_loop

Clément Foucault noreply at git.blender.org
Tue Feb 20 22:46:07 CET 2018


Commit: 0b98b668c4853f9e497173a319c00edb73ec3693
Author: Clément Foucault
Date:   Tue Feb 20 22:45:55 2018 +0100
Branches: temp-drawcontext
https://developer.blender.org/rB0b98b668c4853f9e497173a319c00edb73ec3693

DRW: Fix DRW_draw_select_loop and DRW_draw_depth_loop

Depth of selection offscreen buffer was uninitialized and selection was behaving badly.

DRW_draw_depth_loop needed to be updated to use the drawmanager context but since the reading is done is quite some places outside of DRW, I prefered to still copy the depth to the backbuffer until we update all operators to use offscreen buffers.

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

M	source/blender/draw/intern/draw_manager.c
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/intern/gpu_shader.c
A	source/blender/gpu/shaders/gpu_shader_image_depth_copy_frag.glsl

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

diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c
index be20871690d..73376535147 100644
--- a/source/blender/draw/intern/draw_manager.c
+++ b/source/blender/draw/intern/draw_manager.c
@@ -3801,6 +3801,7 @@ void DRW_draw_select_loop(
 	/* Setup framebuffer */
 	draw_select_framebuffer_setup(rect);
 	GPU_framebuffer_bind(g_select_buffer.framebuffer);
+	DRW_framebuffer_clear(false, true, false, NULL, 1.0f);
 
 	DST.options.is_select = true;
 
@@ -3865,6 +3866,7 @@ void DRW_draw_select_loop(
 	/* Avoid accidental reuse. */
 	memset(&DST, 0xFF, sizeof(DST));
 #endif
+	GPU_framebuffer_restore();
 
 	/* Cleanup for selection state */
 	GPU_viewport_free(viewport);
@@ -3875,6 +3877,42 @@ void DRW_draw_select_loop(
 #endif  /* USE_GPU_SELECT */
 }
 
+static void draw_depth_texture_to_screen(GPUTexture *texture)
+{
+	const float w = (float)GPU_texture_width(texture);
+	const float h = (float)GPU_texture_height(texture);
+
+	Gwn_VertFormat *format = immVertexFormat();
+	unsigned int texcoord = GWN_vertformat_attr_add(format, "texCoord", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
+	unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
+
+	immBindBuiltinProgram(GPU_SHADER_3D_IMAGE_DEPTH_COPY);
+
+	GPU_texture_bind(texture, 0);
+
+	immUniform1i("image", 0); /* default GL_TEXTURE0 unit */
+
+	immBegin(GWN_PRIM_TRI_STRIP, 4);
+
+	immAttrib2f(texcoord, 0.0f, 0.0f);
+	immVertex2f(pos, 0.0f, 0.0f);
+
+	immAttrib2f(texcoord, 1.0f, 0.0f);
+	immVertex2f(pos, w, 0.0f);
+
+	immAttrib2f(texcoord, 0.0f, 1.0f);
+	immVertex2f(pos, 0.0f, h);
+
+	immAttrib2f(texcoord, 1.0f, 1.0f);
+	immVertex2f(pos, w, h);
+
+	immEnd();
+
+	GPU_texture_unbind(texture);
+
+	immUnbindProgram();
+}
+
 /**
  * object mode select-loop, see: ED_view3d_draw_depth_loop (legacy drawing).
  */
@@ -3887,6 +3925,8 @@ void DRW_draw_depth_loop(
 	ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
 	RegionView3D *rv3d = ar->regiondata;
 
+	DRW_opengl_context_enable();
+
 	/* backup (_never_ use rv3d->viewport) */
 	void *backup_viewport = rv3d->viewport;
 	rv3d->viewport = NULL;
@@ -3897,6 +3937,11 @@ void DRW_draw_depth_loop(
 	struct GPUViewport *viewport = GPU_viewport_create();
 	GPU_viewport_size_set(viewport, (const int[2]){ar->winx, ar->winy});
 
+	/* Setup framebuffer */
+	draw_select_framebuffer_setup(&ar->winrct);
+	GPU_framebuffer_bind(g_select_buffer.framebuffer);
+	DRW_framebuffer_clear(false, true, false, NULL, 1.0f);
+
 	bool cache_is_dirty;
 	DST.viewport = viewport;
 	v3d->zbuf = true;
@@ -3955,6 +4000,25 @@ void DRW_draw_depth_loop(
 	memset(&DST, 0xFF, sizeof(DST));
 #endif
 
+	/* TODO: Reading depth for operators should be done here. */
+
+	GPU_framebuffer_restore();
+	DRW_opengl_context_disable();
+
+	/* XXX Drawing the resulting buffer to the BACK_BUFFER */
+	gpuPushMatrix();
+	gpuPushProjectionMatrix();
+	wmOrtho2_region_pixelspace(ar);
+	gpuLoadIdentity();
+
+	glEnable(GL_DEPTH_TEST); /* Cannot write to depth buffer without testing */
+	glDepthFunc(GL_ALWAYS);
+	draw_depth_texture_to_screen(g_select_buffer.texture_depth);
+	glDepthFunc(GL_LEQUAL);
+
+	gpuPopMatrix();
+	gpuPopProjectionMatrix();
+
 	/* Cleanup for selection state */
 	GPU_viewport_free(viewport);
 	MEM_freeN(viewport);
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 0ac842d90a0..2d404cd2aa7 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -150,6 +150,7 @@ data_to_c_simple(shaders/gpu_shader_image_alpha_color_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_image_color_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_image_rect_modulate_alpha_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_image_depth_linear_frag.glsl SRC)
+data_to_c_simple(shaders/gpu_shader_image_depth_copy_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_image_interlace_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_3D_image_vert.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_3D_vert.glsl SRC)
diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h
index f2119a117e5..5db199f1ca9 100644
--- a/source/blender/gpu/GPU_shader.h
+++ b/source/blender/gpu/GPU_shader.h
@@ -133,6 +133,7 @@ typedef enum GPUBuiltinShader {
 	GPU_SHADER_3D_IMAGE_MODULATE_ALPHA,
 	GPU_SHADER_3D_IMAGE_RECT_MODULATE_ALPHA,
 	GPU_SHADER_3D_IMAGE_DEPTH,
+	GPU_SHADER_3D_IMAGE_DEPTH_COPY,
 	/* stereo 3d */
 	GPU_SHADER_2D_IMAGE_INTERLACE,
 	/* points */
diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c
index 598722d372b..c8c154074e2 100644
--- a/source/blender/gpu/intern/gpu_shader.c
+++ b/source/blender/gpu/intern/gpu_shader.c
@@ -77,6 +77,7 @@ extern char datatoc_gpu_shader_image_mask_uniform_color_frag_glsl[];
 extern char datatoc_gpu_shader_image_modulate_alpha_frag_glsl[];
 extern char datatoc_gpu_shader_image_rect_modulate_alpha_frag_glsl[];
 extern char datatoc_gpu_shader_image_depth_linear_frag_glsl[];
+extern char datatoc_gpu_shader_image_depth_copy_frag_glsl[];
 extern char datatoc_gpu_shader_3D_vert_glsl[];
 extern char datatoc_gpu_shader_3D_normal_vert_glsl[];
 extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[];
@@ -714,6 +715,8 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
 		                                              datatoc_gpu_shader_image_rect_modulate_alpha_frag_glsl },
 		[GPU_SHADER_3D_IMAGE_DEPTH] = { datatoc_gpu_shader_3D_image_vert_glsl,
 		                                datatoc_gpu_shader_image_depth_linear_frag_glsl },
+		[GPU_SHADER_3D_IMAGE_DEPTH_COPY] = { datatoc_gpu_shader_3D_image_vert_glsl,
+		                                     datatoc_gpu_shader_image_depth_copy_frag_glsl },
 
 		[GPU_SHADER_2D_IMAGE_INTERLACE] = { datatoc_gpu_shader_2D_image_vert_glsl,
 		                                    datatoc_gpu_shader_image_interlace_frag_glsl },
diff --git a/source/blender/gpu/shaders/gpu_shader_image_depth_copy_frag.glsl b/source/blender/gpu/shaders/gpu_shader_image_depth_copy_frag.glsl
new file mode 100644
index 00000000000..10f4dfd5a87
--- /dev/null
+++ b/source/blender/gpu/shaders/gpu_shader_image_depth_copy_frag.glsl
@@ -0,0 +1,12 @@
+
+in vec2 texCoord_interp;
+out vec4 fragColor;
+
+uniform sampler2D image;
+
+void main()
+{
+	float depth = texture(image, texCoord_interp).r;
+	fragColor = vec4(depth);
+	gl_FragDepth = depth;
+}



More information about the Bf-blender-cvs mailing list