[Bf-blender-cvs] [681661dbed1] master: GPU: Simplify select shaders.

mano-wii noreply at git.blender.org
Fri Mar 15 21:03:00 CET 2019


Commit: 681661dbed121c7b81e9129c57df5eadb03c1009
Author: mano-wii
Date:   Fri Mar 15 16:02:55 2019 -0300
Branches: master
https://developer.blender.org/rB681661dbed121c7b81e9129c57df5eadb03c1009

GPU: Simplify select shaders.

The shaders are: `GPU_SHADER_3D_FLAT_SELECT_ID` and `GPU_SHADER_3D_UNIFORM_SELECT_ID`.
This commit allows the drawing of the mesh select ids to be done on a 32UI format texture.
This simplifies the shader that previously acted on the backbuffer and had to do an uint to rgba conversion.

Differential Revision: https://developer.blender.org/D4350

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

M	source/blender/blenloader/intern/readfile.c
M	source/blender/draw/DRW_engine.h
M	source/blender/draw/intern/draw_manager.c
M	source/blender/editors/include/ED_mesh.h
M	source/blender/editors/include/ED_view3d.h
M	source/blender/editors/mesh/editface.c
M	source/blender/editors/mesh/editmesh_select.c
M	source/blender/editors/mesh/meshtools.c
M	source/blender/editors/physics/particle_edit.c
M	source/blender/editors/sculpt_paint/paint_utils.c
M	source/blender/editors/sculpt_paint/paint_vertex_weight_ops.c
M	source/blender/editors/space_view3d/drawobject.c
M	source/blender/editors/space_view3d/space_view3d.c
M	source/blender/editors/space_view3d/view3d_draw_legacy.c
M	source/blender/editors/space_view3d/view3d_intern.h
M	source/blender/editors/space_view3d/view3d_select.c
M	source/blender/gpu/GPU_draw.h
M	source/blender/gpu/GPU_extensions.h
M	source/blender/gpu/intern/gpu_draw.c
M	source/blender/gpu/intern/gpu_extensions.c
M	source/blender/gpu/shaders/gpu_shader_3D_selection_id_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_selection_id_frag.glsl
M	source/blender/makesdna/DNA_view3d_types.h

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

diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 655b1772130..e9bd8821d30 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -7085,7 +7085,6 @@ static void direct_link_region(FileData *fd, ARegion *ar, int spacetype)
 				rv3d->clipbb = newdataadr(fd, rv3d->clipbb);
 
 				rv3d->depths = NULL;
-				rv3d->gpuoffscreen = NULL;
 				rv3d->render_engine = NULL;
 				rv3d->sms = NULL;
 				rv3d->smooth_timer = NULL;
diff --git a/source/blender/draw/DRW_engine.h b/source/blender/draw/DRW_engine.h
index e261de3c900..68d4ef04f95 100644
--- a/source/blender/draw/DRW_engine.h
+++ b/source/blender/draw/DRW_engine.h
@@ -123,6 +123,10 @@ void DRW_draw_depth_loop(
         struct Depsgraph *depsgraph,
         struct ARegion *ar, struct View3D *v3d);
 
+void DRW_framebuffer_select_id_setup(struct ARegion *ar, const bool clear);
+void DRW_framebuffer_select_id_release(struct ARegion *ar);
+void DRW_framebuffer_select_id_read(const struct rcti *rect, uint *r_buf);
+
 /* grease pencil render */
 bool DRW_render_check_grease_pencil(struct Depsgraph *depsgraph);
 void DRW_render_gpencil(struct RenderEngine *engine, struct Depsgraph *depsgraph);
diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c
index a34d2532bb5..fb30e4391e7 100644
--- a/source/blender/draw/intern/draw_manager.c
+++ b/source/blender/draw/intern/draw_manager.c
@@ -2015,20 +2015,26 @@ void DRW_custom_pipeline(
 }
 
 static struct DRWSelectBuffer {
-	struct GPUFrameBuffer *framebuffer;
+	struct GPUFrameBuffer *framebuffer_depth_only;
+	struct GPUFrameBuffer *framebuffer_select_id;
 	struct GPUTexture *texture_depth;
+	struct GPUTexture *texture_u32;
 } g_select_buffer = {NULL};
 
-static void draw_select_framebuffer_setup(const rcti *rect)
+static void draw_select_framebuffer_depth_only_setup(const rcti *rect)
 {
-	if (g_select_buffer.framebuffer == NULL) {
-		g_select_buffer.framebuffer = GPU_framebuffer_create();
+	float size[2];
+	size[0] = BLI_rcti_size_x(rect);
+	size[1] = BLI_rcti_size_y(rect);
+
+	if (g_select_buffer.framebuffer_depth_only == NULL) {
+		g_select_buffer.framebuffer_depth_only = GPU_framebuffer_create();
+		g_select_buffer.framebuffer_select_id = GPU_framebuffer_create();
 	}
 
-	/* If size mismatch recreate the texture. */
 	if ((g_select_buffer.texture_depth != NULL) &&
-	    ((GPU_texture_width(g_select_buffer.texture_depth) != BLI_rcti_size_x(rect)) ||
-	     (GPU_texture_height(g_select_buffer.texture_depth) != BLI_rcti_size_y(rect))))
+	    ((GPU_texture_width(g_select_buffer.texture_depth) != size[0]) ||
+	     (GPU_texture_height(g_select_buffer.texture_depth) != size[1])))
 	{
 		GPU_texture_free(g_select_buffer.texture_depth);
 		g_select_buffer.texture_depth = NULL;
@@ -2036,13 +2042,50 @@ static void draw_select_framebuffer_setup(const rcti *rect)
 
 	if (g_select_buffer.texture_depth == NULL) {
 		g_select_buffer.texture_depth = GPU_texture_create_2D(
-		        BLI_rcti_size_x(rect), BLI_rcti_size_y(rect), GPU_DEPTH_COMPONENT24, NULL, NULL);
+		        size[0], size[1], GPU_DEPTH_COMPONENT24, NULL, NULL);
 
-		GPU_framebuffer_texture_attach(g_select_buffer.framebuffer, g_select_buffer.texture_depth, 0, 0);
+		GPU_framebuffer_texture_attach(
+		        g_select_buffer.framebuffer_depth_only,
+		        g_select_buffer.texture_depth, 0, 0);
 
-		if (!GPU_framebuffer_check_valid(g_select_buffer.framebuffer, NULL)) {
-			printf("Error invalid selection framebuffer\n");
-		}
+		GPU_framebuffer_texture_attach(
+		        g_select_buffer.framebuffer_select_id,
+		        g_select_buffer.texture_depth, 0, 0);
+
+		GPU_framebuffer_check_valid(
+		        g_select_buffer.framebuffer_depth_only, __func__);
+
+		GPU_framebuffer_check_valid(
+		        g_select_buffer.framebuffer_select_id, __func__);
+	}
+}
+
+static void draw_select_framebuffer_select_id_setup(const rcti *rect)
+{
+	float size[2];
+	size[0] = BLI_rcti_size_x(rect);
+	size[1] = BLI_rcti_size_y(rect);
+
+	draw_select_framebuffer_depth_only_setup(rect);
+
+	if ((g_select_buffer.texture_u32 != NULL) &&
+	    ((GPU_texture_width(g_select_buffer.texture_u32) != size[0]) ||
+	     (GPU_texture_height(g_select_buffer.texture_u32) != size[1])))
+	{
+		GPU_texture_free(g_select_buffer.texture_u32);
+		g_select_buffer.texture_u32 = NULL;
+	}
+
+	if (g_select_buffer.texture_u32 == NULL) {
+		g_select_buffer.texture_u32 = GPU_texture_create_2D(
+		        size[0], size[1], GPU_R32UI, NULL, NULL);
+
+		GPU_framebuffer_texture_attach(
+		        g_select_buffer.framebuffer_select_id,
+		        g_select_buffer.texture_u32, 0, 0);
+
+		GPU_framebuffer_check_valid(
+		        g_select_buffer.framebuffer_select_id, __func__);
 	}
 }
 
@@ -2209,9 +2252,9 @@ void DRW_draw_select_loop(
 	}
 
 	/* Setup framebuffer */
-	draw_select_framebuffer_setup(rect);
-	GPU_framebuffer_bind(g_select_buffer.framebuffer);
-	GPU_framebuffer_clear_depth(g_select_buffer.framebuffer, 1.0f);
+	draw_select_framebuffer_depth_only_setup(rect);
+	GPU_framebuffer_bind(g_select_buffer.framebuffer_depth_only);
+	GPU_framebuffer_clear_depth(g_select_buffer.framebuffer_depth_only, 1.0f);
 
 	/* Start Drawing */
 	DRW_state_reset();
@@ -2315,9 +2358,9 @@ void DRW_draw_depth_loop(
 	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);
-	GPU_framebuffer_clear_depth(g_select_buffer.framebuffer, 1.0f);
+	draw_select_framebuffer_depth_only_setup(&ar->winrct);
+	GPU_framebuffer_bind(g_select_buffer.framebuffer_depth_only);
+	GPU_framebuffer_clear_depth(g_select_buffer.framebuffer_depth_only, 1.0f);
 
 	DST.viewport = viewport;
 	DST.options.is_depth = true;
@@ -2421,6 +2464,67 @@ void DRW_draw_depth_loop(
 	GPU_matrix_pop_projection();
 }
 
+
+/* Set an opengl context to be used with shaders that draw on U32 colors. */
+void DRW_framebuffer_select_id_setup(ARegion *ar, const bool clear)
+{
+	RegionView3D *rv3d = ar->regiondata;
+
+	DRW_opengl_context_enable();
+
+	/* Setup framebuffer */
+	draw_select_framebuffer_select_id_setup(&ar->winrct);
+	GPU_framebuffer_bind(g_select_buffer.framebuffer_select_id);
+
+	/* dithering and AA break color coding, so disable */
+	glDisable(GL_DITHER);
+
+	GPU_depth_test(true);
+
+	if (clear) {
+		GPU_framebuffer_clear_color_depth(
+		        g_select_buffer.framebuffer_select_id, (const float[4]){0.0f}, 1.0f);
+	}
+
+	if (rv3d->rflag & RV3D_CLIPPING) {
+		ED_view3d_clipping_set(rv3d);
+	}
+}
+
+
+/* Ends the context for selection and restoring the previous one. */
+void DRW_framebuffer_select_id_release(ARegion *ar)
+{
+	RegionView3D *rv3d = ar->regiondata;
+
+	if (rv3d->rflag & RV3D_CLIPPING) {
+		ED_view3d_clipping_disable();
+	}
+
+	GPU_framebuffer_restore();
+
+	GPU_depth_test(false);
+	glEnable(GL_DITHER);
+
+	DRW_opengl_context_disable();
+}
+
+
+/* Read a block of pixels from the select frame buffer. */
+void DRW_framebuffer_select_id_read(const rcti *rect, uint *r_buf)
+{
+	DRW_opengl_context_enable();
+	GPU_framebuffer_bind(g_select_buffer.framebuffer_select_id);
+	glReadBuffer(GL_COLOR_ATTACHMENT0);
+
+	glReadPixels(rect->xmin, rect->ymin,
+	             BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
+	             GL_RED_INTEGER, GL_UNSIGNED_INT, r_buf);
+
+	GPU_framebuffer_restore();
+	DRW_opengl_context_disable();
+}
+
 /** \} */
 
 
@@ -2612,8 +2716,10 @@ void DRW_engines_free(void)
 
 	DRW_opengl_context_enable();
 
+	DRW_TEXTURE_FREE_SAFE(g_select_buffer.texture_u32);
 	DRW_TEXTURE_FREE_SAFE(g_select_buffer.texture_depth);
-	GPU_FRAMEBUFFER_FREE_SAFE(g_select_buffer.framebuffer);
+	GPU_FRAMEBUFFER_FREE_SAFE(g_select_buffer.framebuffer_select_id);
+	GPU_FRAMEBUFFER_FREE_SAFE(g_select_buffer.framebuffer_depth_only);
 
 	DRW_hair_free();
 	DRW_shape_cache_free();
diff --git a/source/blender/editors/include/ED_mesh.h b/source/blender/editors/include/ED_mesh.h
index 7318df8adab..e8c5e0dd789 100644
--- a/source/blender/editors/include/ED_mesh.h
+++ b/source/blender/editors/include/ED_mesh.h
@@ -237,7 +237,7 @@ void EDBM_project_snap_verts(struct bContext *C, struct ARegion *ar, struct BMEd
 /* editface.c */
 void paintface_flush_flags(struct bContext *C, struct Object *ob, short flag);
 bool paintface_mouse_select(struct bContext *C, struct Object *ob, const int mval[2], bool extend, bool deselect, bool toggle);
-int  do_paintface_box_select(struct ViewContext *vc, struct rcti *rect, int sel_op);
+int  do_paintface_box_select(struct ViewContext *vc, const struct rcti *rect, int sel_op);
 void paintface_deselect_all_visible(struct bContext *C, struct Object *ob, int action, bool flush_flags);
 void paintface_select_linked(struct bContext *C, struct Object *ob, const int mval[2], const bool select);
 bool paintface_minmax(struct Object *ob, float r_min[3], float r_max[3]);
@@ -365,9 +365,9 @@ int *mesh_get_x_mirror_faces(struct Object *ob, struct BMEditMesh *em, struct Me
 
 int ED_mesh_mirror_get_vert(struct Object *ob, int index);
 
-bool ED_mesh_pick_vert(struct bContext *C,      struct Object *ob, const int mval[2], unsigned int *index, int size, bool use_zbuf);
-bool ED_mesh_pick_face(struct bContext *C,      struct Object *ob, const int mval[2], unsigned int *index, int size);
-bool ED_mesh_pick_face_vert(struct bContext *C, struct Object *ob, const int mval[2], unsigned int *index, int size);
+bool ED_mesh_pick_vert(struct bContext *C,      struct Object *ob, const int mval[2], unsigned int *index, int dist_px, bool use_zbuf);
+bool ED_mesh_pick_face(struct bContext *C,      struct Object *ob, const int mval[2], unsigned int *index, int dist_px);
+bool ED_mesh_pick_face_vert(struct bContext *C, struct Object *ob, const int mval[2], unsigned int *index, int dist_px);
 
 
 struct MDeformVert *ED_mesh_active_dvert_get_em(struct Object *ob, struct BMVert **r_eve);
@@ -377,8 +377,8 @@ struct MDeformVert *ED_mesh_active_dvert_get_only(struct Object *ob);
 void EDBM_mesh_stats_multi(struct Object **objects, const uint objects_len, int totelem[3], int totelem_sel[3]);
 void EDBM_mesh_elem_index_ensure_multi(struct Object

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list