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
This commit is contained in:
Dalai Felinto 2016-10-21 20:50:00 +00:00
parent deb77c0e74
commit 5ff586610a
6 changed files with 220 additions and 3 deletions

View File

@ -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):

View File

@ -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;

View File

@ -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__

View File

@ -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;
}

View File

@ -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 << 1),
V3D_DEBUG_SHOW_COMBINED_DEPTH = (1 << 2),
};
/* View3D->around */

View File

@ -2777,6 +2777,30 @@ static void rna_def_space_view3d(BlenderRNA *brna)
RNA_def_property_ui_text(prop, "Modern Viewport", "Use modern viewport");
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
prop = RNA_def_property(srna, "show_scene_depth", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "tmp_compat_flag", V3D_DEBUG_SHOW_SCENE_DEPTH);
RNA_def_property_ui_text(prop, "Show Scene Depth", "Debug option to show the depth in the viewport");
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
prop = RNA_def_property(srna, "show_combined_depth", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "tmp_compat_flag", V3D_DEBUG_SHOW_COMBINED_DEPTH);
RNA_def_property_ui_text(prop, "Show Combined Depth", "Debug option to show the depth in the viewport");
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
prop = RNA_def_property(srna, "debug_near", PROP_FLOAT, PROP_DISTANCE);
RNA_def_property_float_sdna(prop, NULL, "debug.znear");
RNA_def_property_ui_text(prop, "Near", "Near distance for depth debugging");
RNA_def_property_range(prop, 1e-6f, FLT_MAX);
RNA_def_property_ui_range(prop, 0.001f, FLT_MAX, 10, 3);
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
prop = RNA_def_property(srna, "debug_far", PROP_FLOAT, PROP_DISTANCE);
RNA_def_property_float_sdna(prop, NULL, "debug.zfar");
RNA_def_property_range(prop, 1e-6f, FLT_MAX);
RNA_def_property_ui_range(prop, 0.001f, FLT_MAX, 10, 3);
RNA_def_property_ui_text(prop, "Far", "Far distance for depth debugging");
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
/* *** Animated *** */
RNA_define_animate_sdna(true);
/* region */