[Bf-blender-cvs] [d712f1f83af] blender-v2.83-release: GPU: Add Polyline shader (wide line emulation)

Clément Foucault noreply at git.blender.org
Thu Apr 23 23:08:10 CEST 2020


Commit: d712f1f83af881be536ec0d183b7d3025c172684
Author: Clément Foucault
Date:   Thu Apr 23 23:05:39 2020 +0200
Branches: blender-v2.83-release
https://developer.blender.org/rBd712f1f83af881be536ec0d183b7d3025c172684

GPU: Add Polyline shader (wide line emulation)

This new shader is able to emulate smooth wide lines drawing using a
geometry shader.

This shader needs viewportSize and lineWidth uniforms to be set.

There is multiple variants to replace the usage of wide lines for most
shaders.

This patch only fix the gizmo_types files and the navigation gizmo.
Other areas could be fixed afterward, I just limited the patch size.

Fix T57570.

Reviewed By: billreynish

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

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

M	source/blender/editors/gizmo_library/gizmo_types/arrow3d_gizmo.c
M	source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.c
M	source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.c
M	source/blender/editors/gizmo_library/gizmo_types/cage3d_gizmo.c
M	source/blender/editors/gizmo_library/gizmo_types/dial3d_gizmo.c
M	source/blender/editors/gizmo_library/gizmo_types/move3d_gizmo.c
M	source/blender/editors/space_view3d/view3d_gizmo_navigate_type.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_3D_polyline_frag.glsl
A	source/blender/gpu/shaders/gpu_shader_3D_polyline_geom.glsl
A	source/blender/gpu/shaders/gpu_shader_3D_polyline_vert.glsl

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

diff --git a/source/blender/editors/gizmo_library/gizmo_types/arrow3d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/arrow3d_gizmo.c
index da6365d724d..e6333d7d3e0 100644
--- a/source/blender/editors/gizmo_library/gizmo_types/arrow3d_gizmo.c
+++ b/source/blender/editors/gizmo_library/gizmo_types/arrow3d_gizmo.c
@@ -88,9 +88,14 @@ static void arrow_draw_geom(const ArrowGizmo3D *arrow, const bool select, const
   const int draw_style = RNA_enum_get(arrow->gizmo.ptr, "draw_style");
   const int draw_options = RNA_enum_get(arrow->gizmo.ptr, "draw_options");
 
-  immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
+  immBindBuiltinProgram(GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR);
+
+  float viewport[4];
+  GPU_viewport_size_get_f(viewport);
+  immUniform2fv("viewportSize", &viewport[2]);
 
   if (draw_style == ED_GIZMO_ARROW_STYLE_CROSS) {
+    immUniform1f("lineWidth", U.pixelsize);
     immUniformColor4fv(color);
 
     immBegin(GPU_PRIM_LINES, 4);
@@ -112,7 +117,7 @@ static void arrow_draw_geom(const ArrowGizmo3D *arrow, const bool select, const
         {-unitx, unity, 0},
     };
 
-    GPU_line_width(arrow->gizmo.line_width);
+    immUniform1f("lineWidth", arrow->gizmo.line_width * U.pixelsize);
     wm_gizmo_vec_draw(color, vec, ARRAY_SIZE(vec), pos, GPU_PRIM_LINE_LOOP);
   }
   else {
@@ -127,7 +132,7 @@ static void arrow_draw_geom(const ArrowGizmo3D *arrow, const bool select, const
     };
 
     if (draw_options & ED_GIZMO_ARROW_DRAW_FLAG_STEM) {
-      GPU_line_width(arrow->gizmo.line_width);
+      immUniform1f("lineWidth", arrow->gizmo.line_width * U.pixelsize);
       wm_gizmo_vec_draw(color, vec, ARRAY_SIZE(vec), pos, GPU_PRIM_LINE_STRIP);
     }
     else {
@@ -160,6 +165,10 @@ static void arrow_draw_geom(const ArrowGizmo3D *arrow, const bool select, const
       /* translate to line end */
       GPU_matrix_translate_3f(0.0f, 0.0f, arrow_length);
 
+      immUnbindProgram();
+      immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
+      immUniformColor4fv(color);
+
       imm_draw_circle_fill_3d(pos, 0.0, 0.0, width, 8);
       imm_draw_cylinder_fill_3d(pos, width, 0.0, len, 8, 1);
     }
diff --git a/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.c
index 11253f01bf2..11b1b98ea12 100644
--- a/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.c
+++ b/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.c
@@ -76,7 +76,8 @@ static void button2d_geom_draw_backdrop(const wmGizmo *gz,
                                         const float fill_alpha,
                                         const bool select)
 {
-  GPU_line_width(gz->line_width);
+  float viewport[4];
+  GPU_viewport_size_get_f(viewport);
 
   GPUVertFormat *format = immVertexFormat();
   uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
@@ -85,10 +86,14 @@ static void button2d_geom_draw_backdrop(const wmGizmo *gz,
   if (color[3] == 1.0 && fill_alpha == 1.0 && select == false) {
     immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
     immUniformColor4fv(color);
-    GPU_polygon_smooth(0);
     imm_draw_circle_fill_2d(pos, 0, 0, 1.0f, CIRCLE_RESOLUTION);
+    immUnbindProgram();
+
+    immBindBuiltinProgram(GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR);
+    immUniform2fv("viewportSize", &viewport[2]);
+    immUniform1f("lineWidth", gz->line_width * U.pixelsize);
+    immUniformColor4fv(color);
     imm_draw_circle_wire_2d(pos, 0, 0, 1.0f, CIRCLE_RESOLUTION);
-    GPU_polygon_smooth(1);
     immUnbindProgram();
   }
   else {
@@ -103,9 +108,10 @@ static void button2d_geom_draw_backdrop(const wmGizmo *gz,
 
     /* Draw outline. */
     if ((fill_alpha != 1.0f) && (select == false)) {
-      immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
+      immBindBuiltinProgram(GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR);
+      immUniform2fv("viewportSize", &viewport[2]);
+      immUniform1f("lineWidth", gz->line_width * U.pixelsize);
       immUniformColor4fv(color);
-      GPU_line_width(gz->line_width);
       imm_draw_circle_wire_2d(pos, 0, 0, 1.0f, CIRCLE_RESOLUTION);
       immUnbindProgram();
     }
@@ -152,12 +158,13 @@ static void button2d_draw_intern(const bContext *C,
   bool is_3d = (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) != 0;
 
   if ((select == false) && (draw_options & ED_GIZMO_BUTTON_SHOW_HELPLINE)) {
-    float matrix_final_no_offset[4][4];
+    float matrix_final_no_offset[4][4], viewport[4];
     WM_gizmo_calc_matrix_final_no_offset(gz, matrix_final_no_offset);
     uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
-    immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
-    immUniformColor4fv(color);
-    GPU_line_width(gz->line_width);
+    immBindBuiltinProgram(GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR);
+    GPU_viewport_size_get_f(viewport);
+    immUniform2fv("viewportSize", &viewport[2]);
+    immUniform1f("lineWidth", gz->line_width * U.pixelsize);
     immUniformColor4fv(color);
     immBegin(GPU_PRIM_LINE_STRIP, 2);
     immVertex3fv(pos, matrix_final[3]);
@@ -197,7 +204,6 @@ static void button2d_draw_intern(const bContext *C,
     if (button->shape_batch[0] != NULL) {
       GPU_line_smooth(true);
       GPU_polygon_smooth(false);
-      GPU_line_width(1.0f);
       for (uint i = 0; i < ARRAY_SIZE(button->shape_batch) && button->shape_batch[i]; i++) {
         /* Invert line color for wire. */
         GPU_batch_program_set_builtin(button->shape_batch[i], GPU_SHADER_2D_UNIFORM_COLOR);
@@ -216,7 +222,7 @@ static void button2d_draw_intern(const bContext *C,
           GPU_batch_uniform_4f(button->shape_batch[i], "color", UNPACK4(color));
         }
 
-        GPU_batch_draw(button->shape_batch[i]);
+        // GPU_batch_draw(button->shape_batch[i]);
 
         if (draw_options & ED_GIZMO_BUTTON_SHOW_OUTLINE) {
           color[0] = 1.0f - color[0];
diff --git a/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.c
index 1b43479eedb..fd24149e9ea 100644
--- a/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.c
+++ b/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.c
@@ -162,13 +162,22 @@ static void gizmo_rect_pivot_from_scale_part(int part, float r_pt[2], bool r_con
  * Useful for 3D views, see: #ED_GIZMO_CAGE2D_STYLE_BOX
  * \{ */
 
-static void cage2d_draw_box_corners(const rctf *r, const float margin[2], const float color[3])
+static void cage2d_draw_box_corners(const rctf *r,
+                                    const float margin[2],
+                                    const float color[3],
+                                    const float line_width)
 {
   uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
 
-  immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
+  immBindBuiltinProgram(GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR);
   immUniformColor3fv(color);
 
+  float viewport[4];
+  GPU_viewport_size_get_f(viewport);
+  immUniform2fv("viewportSize", &viewport[2]);
+
+  immUniform1f("lineWidth", line_width * U.pixelsize);
+
   immBegin(GPU_PRIM_LINES, 16);
 
   immVertex2f(pos, r->xmin, r->ymin + margin[1]);
@@ -445,7 +454,7 @@ static void cage2d_draw_box_interaction(const float color[4],
       .pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT),
       .col = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 3, GPU_FETCH_FLOAT),
   };
-  immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
+  immBindBuiltinProgram(is_solid ? GPU_SHADER_2D_FLAT_COLOR : GPU_SHADER_3D_POLYLINE_FLAT_COLOR);
 
   {
     if (is_solid) {
@@ -459,7 +468,12 @@ static void cage2d_draw_box_interaction(const float color[4],
     }
     else {
       BLI_assert(ELEM(prim_type, GPU_PRIM_LINE_STRIP, GPU_PRIM_LINES));
-      GPU_line_width(line_width + 3.0f);
+
+      float viewport[4];
+      GPU_viewport_size_get_f(viewport);
+      immUniform2fv("viewportSize", &viewport[2]);
+
+      immUniform1f("lineWidth", (line_width * 3.0f) * U.pixelsize);
 
       immBegin(prim_type, verts_len);
       immAttr3f(attr_id.col, 0.0f, 0.0f, 0.0f);
@@ -468,7 +482,7 @@ static void cage2d_draw_box_interaction(const float color[4],
       }
       immEnd();
 
-      GPU_line_width(line_width);
+      immUniform1f("lineWidth", line_width * U.pixelsize);
 
       immBegin(prim_type, verts_len);
       immAttr3fv(attr_id.col, color);
@@ -505,13 +519,19 @@ static void cage2d_draw_circle_wire(const rctf *r,
                                     const float margin[2],
                                     const float color[3],
                                     const int transform_flag,
-                                    const int draw_options)
+                                    const int draw_options,
+                                    const float line_width)
 {
   uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
 
-  immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
+  immBindBuiltinProgram(GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR);
   immUniformColor3fv(color);
 
+  float viewport[4];
+  GPU_viewport_size_get_f(viewport);
+  immUniform2fv("viewportSize", &viewport[2]);
+  immUniform1f("lineWidth", line_width * U.pixelsize);
+
   immBegin(GPU_PRIM_LINE_LOOP, 4);
   immVertex2f(pos, r->xmin, r->ymin);
   immVertex2f(pos, r->xmax, r->ymin);
@@ -662,15 +682,14 @@ static void gizmo_cage2d_draw_intern(wmGizmo *gz,
         .ymax = size_real[1],
     };
     if (draw_style == ED_GIZMO_CAGE2D_STYLE_BOX) {
+      float color[4], black[3] = {0, 0, 0};
+      gizmo_color_get(gz, highlight, color);
+
       /* corner gizmos */
-      GPU_line_width(gz->line_width + 3.0f);
-      cage2d_draw_box_corners(&r, margin, (const float[3]){0, 0, 0});
+      cage2d_draw_box_corners(&r, margin, black, gz->line_width + 3.0f);
 
       /* corner gizmos */
-      float color[4];
-      gizmo_color_get(gz, highlight, color);
-      GPU_line_width(gz->line_width);
-      cage2d_draw_box_corners(&r, margin, color);
+      cage2d_draw_box_corners(&r, margin, color, gz->line_width);
 
       bool show = f

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list