[Bf-blender-cvs] [5ff5866] blender2.8: Viewport: use depth shader to debug the depth

Dalai Felinto noreply at git.blender.org
Fri Oct 21 22:51:39 CEST 2016


Commit: 5ff586610a47aa26ed388270c9a26487c553f1f1
Author: Dalai Felinto
Date:   Fri Oct 21 20:50:00 2016 +0000
Branches: blender2.8
https://developer.blender.org/rB5ff586610a47aa26ed388270c9a26487c553f1f1

Viewport: use depth shader to debug the depth

At the moment this already shows that the depth is the same after the solid plates and in the very end of drawing, while they should be different. Later on we can adapt this to show different buffers we want to debug.

I am using near=0.1, far=2.0 for my tests. I decided not to make a doversion for near/far because this is for debugging only

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

M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/editors/space_view3d/view3d_draw.c
M	source/blender/gpu/GPU_viewport.h
M	source/blender/gpu/intern/gpu_viewport.c
M	source/blender/makesdna/DNA_view3d_types.h
M	source/blender/makesrna/intern/rna_space.c

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

diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 36ada1e..232b46c 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -3000,6 +3000,19 @@ class VIEW3D_PT_viewport_debug(Panel):
 
         col = layout.column()
         col.label(text="Placeholder for debugging options")
+        col.separator()
+
+        row = col.row()
+        row.active = not view.show_combined_depth
+        row.prop(view, "show_scene_depth")
+        row = col.row()
+        row.active = not view.show_scene_depth
+        row.prop(view, "show_combined_depth")
+
+        row = col.row(align=True)
+        row.active = view.show_scene_depth or view.show_combined_depth
+        row.prop(view, "debug_near")
+        row.prop(view, "debug_far")
 
 
 class VIEW3D_PT_grease_pencil(GreasePencilDataPanel, Panel):
diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c
index 4a2a043..9413159 100644
--- a/source/blender/editors/space_view3d/view3d_draw.c
+++ b/source/blender/editors/space_view3d/view3d_draw.c
@@ -56,6 +56,8 @@
 #include "GPU_immediate.h"
 #include "GPU_viewport.h"
 
+#include "MEM_guardedalloc.h"
+
 #include "UI_interface.h"
 #include "UI_resources.h"
 
@@ -73,9 +75,10 @@ typedef struct DrawData {
 	bool render_border;
 	bool clip_border;
 	bool is_render;
+	GPUViewport *viewport;
 } DrawData;
 
-static void view3d_draw_data_init(const bContext *C, ARegion *ar, DrawData *draw_data)
+static void view3d_draw_data_init(const bContext *C, ARegion *ar, RegionView3D *rv3d, DrawData *draw_data)
 {
 	Scene *scene = CTX_data_scene(C);
 	View3D *v3d = CTX_wm_view3d(C);
@@ -84,6 +87,8 @@ static void view3d_draw_data_init(const bContext *C, ARegion *ar, DrawData *draw
 
 	draw_data->render_border = ED_view3d_calc_render_border(scene, v3d, ar, &draw_data->border_rect);
 	draw_data->clip_border = (draw_data->render_border && !BLI_rcti_compare(&ar->drawrct, &draw_data->border_rect));
+
+	draw_data->viewport = rv3d->viewport;
 }
 
 /* ******************** general functions ***************** */
@@ -266,6 +271,70 @@ static void view3d_stereo3d_setup(Scene *scene, View3D *v3d, ARegion *ar)
 	}
 }
 
+/* ******************** debug ***************** */
+
+static void view3d_draw_debug_store_depth(ARegion *ar, DrawData *draw_data)
+{
+	GPUViewport *viewport = draw_data->viewport;
+	GLint viewport_size[4];
+	glGetIntegerv(GL_VIEWPORT, viewport_size);
+
+	const int x = viewport_size[0];
+	const int y = viewport_size[1];
+	const int w = viewport_size[2];
+	const int h = viewport_size[3];
+
+	if (GPU_viewport_debug_depth_is_valid(viewport)) {
+		if ((GPU_viewport_debug_depth_width(viewport) != w) ||
+			(GPU_viewport_debug_depth_height(viewport) != h))
+		{
+			GPU_viewport_debug_depth_free(viewport);
+		}
+	}
+
+	if (!GPU_viewport_debug_depth_is_valid(viewport)) {
+		char error[256];
+		if (!GPU_viewport_debug_depth_create(viewport, w, h, 0, error)) {
+			fprintf(stderr, "Failed to create depth buffer for debug: %s\n", error);
+			return;
+		}
+	}
+
+	GPU_viewport_debug_depth_store(viewport, x, y);
+}
+
+static void view3d_draw_debug_post_solid(const bContext *C, ARegion *ar, DrawData *draw_data)
+{
+	View3D *v3d = CTX_wm_view3d(C);
+
+	if ((v3d->tmp_compat_flag & V3D_DEBUG_SHOW_SCENE_DEPTH) != 0) {
+		view3d_draw_debug_store_depth(ar, draw_data);
+	}
+}
+
+static void view3d_draw_debug(const bContext *C, ARegion *ar, DrawData *draw_data)
+{
+	View3D *v3d = CTX_wm_view3d(C);
+
+	if ((v3d->tmp_compat_flag & V3D_DEBUG_SHOW_COMBINED_DEPTH) != 0) {
+		/* store */
+		view3d_draw_debug_store_depth(ar, draw_data);
+	}
+
+	if (((v3d->tmp_compat_flag & V3D_DEBUG_SHOW_SCENE_DEPTH) != 0) ||
+		((v3d->tmp_compat_flag & V3D_DEBUG_SHOW_COMBINED_DEPTH) != 0))
+	{
+		/* draw */
+		if (GPU_viewport_debug_depth_is_valid(draw_data->viewport)) {
+			GPU_viewport_debug_depth_draw(draw_data->viewport, v3d->debug.znear, v3d->debug.zfar);
+		}
+	}
+	else {
+		/* cleanup */
+		GPU_viewport_debug_depth_free(draw_data->viewport);
+	}
+}
+
 /* ******************** view border ***************** */
 
 static void view3d_camera_border(
@@ -1481,6 +1550,9 @@ static void view3d_draw_solid_plates(const bContext *C, ARegion *ar, DrawData *d
 	if (draw_data->is_render) {
 		view3d_draw_render_draw(C, scene, ar, v3d, draw_data->clip_border, &draw_data->border_rect);
 	}
+
+	/* debug */
+	view3d_draw_debug_post_solid(C, ar, draw_data);
 }
 
 /**
@@ -1606,6 +1678,7 @@ static void view3d_draw_view(const bContext *C, ARegion *ar, DrawData *draw_data
 	view3d_draw_reference_images(C);
 	view3d_draw_manipulator(C);
 	view3d_draw_region_info(C, ar);
+	view3d_draw_debug(C, ar, draw_data);
 }
 
 void view3d_main_region_draw(const bContext *C, ARegion *ar)
@@ -1625,7 +1698,7 @@ void view3d_main_region_draw(const bContext *C, ARegion *ar)
 	 * before we even call the drawing routine, but let's move on for now (dfelinto)
 	 * but this is a provisory way to start seeing things in the viewport */
 	DrawData draw_data;
-	view3d_draw_data_init(C, ar, &draw_data);
+	view3d_draw_data_init(C, ar, rv3d, &draw_data);
 	view3d_draw_view(C, ar, &draw_data);
 
 	v3d->flag |= V3D_INVALID_BACKBUF;
diff --git a/source/blender/gpu/GPU_viewport.h b/source/blender/gpu/GPU_viewport.h
index 82b537e..e251948 100644
--- a/source/blender/gpu/GPU_viewport.h
+++ b/source/blender/gpu/GPU_viewport.h
@@ -32,10 +32,21 @@
 #ifndef __GPU_VIEWPORT_H__
 #define __GPU_VIEWPORT_H__
 
+#include <stdbool.h>
+
 typedef struct GPUViewport GPUViewport;
 
 GPUViewport *GPU_viewport_create(void);
 
 void GPU_viewport_free(GPUViewport *viewport);
 
+/* debug */
+bool GPU_viewport_debug_depth_create(GPUViewport *viewport, int width, int height, int samples, char err_out[256]);
+void GPU_viewport_debug_depth_free(GPUViewport *viewport);
+void GPU_viewport_debug_depth_store(GPUViewport *viewport, const int x, const int y);
+void GPU_viewport_debug_depth_draw(GPUViewport *viewport, const float znear, const float zfar);
+bool GPU_viewport_debug_depth_is_valid(GPUViewport *viewport);
+int GPU_viewport_debug_depth_width(const GPUViewport *viewport);
+int GPU_viewport_debug_depth_height(const GPUViewport *viewport);
+
 #endif // __GPU_VIEWPORT_H__
diff --git a/source/blender/gpu/intern/gpu_viewport.c b/source/blender/gpu/intern/gpu_viewport.c
index bae2fdc..6cad050 100644
--- a/source/blender/gpu/intern/gpu_viewport.c
+++ b/source/blender/gpu/intern/gpu_viewport.c
@@ -31,12 +31,19 @@
  * System that manages viewport drawing.
  */
 
+#include "GPU_glew.h"
+#include "GPU_immediate.h"
 #include "GPU_viewport.h"
+#include "GPU_texture.h"
 
 #include "MEM_guardedalloc.h"
 
 struct GPUViewport {
 	float pad[4];
+
+	/* debug */
+	GPUTexture *debug_depth;
+	int debug_width, debug_height;
 };
 
 GPUViewport *GPU_viewport_create(void)
@@ -47,6 +54,88 @@ GPUViewport *GPU_viewport_create(void)
 
 void GPU_viewport_free(GPUViewport *viewport)
 {
+	GPU_viewport_debug_depth_free(viewport);
 	MEM_freeN(viewport);
 }
 
+/****************** debug ********************/
+
+bool GPU_viewport_debug_depth_create(GPUViewport *viewport, int width, int height, int samples, char err_out[256])
+{
+	viewport->debug_depth = GPU_texture_create_2D(width, height, NULL, GPU_HDR_HALF_FLOAT, err_out);
+	return (viewport->debug_depth != NULL);
+}
+
+void GPU_viewport_debug_depth_free(GPUViewport *viewport)
+{
+	if (viewport->debug_depth != NULL) {
+		MEM_freeN(viewport->debug_depth);
+		viewport->debug_depth = NULL;
+	}
+}
+
+void GPU_viewport_debug_depth_store(GPUViewport *viewport, const int x, const int y)
+{
+	const int w = GPU_texture_width(viewport->debug_depth);
+	const int h = GPU_texture_height(viewport->debug_depth);
+
+	GPU_texture_bind(viewport->debug_depth, 0);
+	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, x, y, w, h, 0);
+	GPU_texture_unbind(viewport->debug_depth);
+}
+
+void GPU_viewport_debug_depth_draw(GPUViewport *viewport, const float znear, const float zfar)
+{
+	const float w = (float)GPU_texture_width(viewport->debug_depth);
+	const float h = (float)GPU_texture_height(viewport->debug_depth);
+
+	const int activeTex = GL_TEXTURE0;
+	glActiveTexture(activeTex);
+
+	VertexFormat *format = immVertexFormat();
+	unsigned texcoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
+	unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
+
+	immBindBuiltinProgram(GPU_SHADER_3D_IMAGE_DEPTH);
+
+	GPU_texture_bind(viewport->debug_depth, 0);
+
+	immUniform1f("znear", znear);
+	immUniform1f("zfar", zfar);
+	immUniform1i("image", activeTex);
+
+	immBegin(GL_QUADS, 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, 1.0f, 1.0f);
+	immVertex2f(pos, w, h);
+
+	immAttrib2f(texcoord, 0.0f, 1.0f);
+	immVertex2f(pos, 0.0f, h);
+
+	immEnd();
+
+	GPU_texture_unbind(viewport->debug_depth);
+
+	immUnbindProgram();
+}
+
+int GPU_viewport_debug_depth_width(const GPUViewport *viewport)
+{
+	return GPU_texture_width(viewport->debug_depth);
+}
+
+int GPU_viewport_debug_depth_height(const GPUViewport *viewport)
+{
+	return GPU_texture_height(viewport->debug_depth);
+}
+
+bool GPU_viewport_debug_depth_is_valid(GPUViewport *viewport)
+{
+	return viewport->debug_depth != NULL;
+}
diff --git a/source/blender/makesdna/DNA_view3d_types.h b/source/blender/makesdna/DNA_view3d_types.h
index 7e0adf4..3a990a1 100644
--- a/source/blender/makesdna/DNA_view3d_types.h
+++ b/source/blender/makesdna/DNA_view3d_types.h
@@ -66,6 +66,10 @@ struct GPUViewport;
 
 /* The near/far thing is a Win EXCEPTION. Thus, leave near/far in the
  * code, and patch for windows. */
+
+typedef struct View3DDebug {
+	float znear, zfar;
+} View3DDebug;
  
 /* Background Picture in 3D-View */
 typedef struct BGpic {
@@ -249,6 +253,7 @@ typedef struct View3D {
 	short prev_drawtype;
 	short pad1;
 	float pad2;
+	View3DDebug debug;
 } View3D;
 
 
@@ -327,7 +332,9 @@ typedef struct View3D {
 
 /* View3d->tmp_compat_flag */
 enum {
-	V3D_NEW_VIEWPORT      = (1 << 0),
+	V3D_NEW_VIEWPORT              = (1 << 0),
+	V3D_DEBUG_SHOW_SCENE_DEPTH    = (1 <<

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list