DRW: support clipping for object & lamp centers
authorCampbell Barton <ideasman42@gmail.com>
Mon, 28 Jan 2019 13:13:42 +0000 (00:13 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 28 Jan 2019 13:27:43 +0000 (00:27 +1100)
source/blender/draw/intern/draw_builtin_shader.c
source/blender/draw/intern/draw_common.c
source/blender/draw/intern/draw_common.h
source/blender/draw/modes/object_mode.c
source/blender/gpu/shaders/gpu_shader_3D_point_uniform_size_aa_vert.glsl
source/blender/gpu/shaders/gpu_shader_3D_point_uniform_size_outline_aa_vert.glsl
source/blender/gpu/shaders/gpu_shader_instance_screen_aligned_vert.glsl
source/blender/gpu/shaders/gpu_shader_instance_screenspace_variying_color_vert.glsl

index 5606d31..0b3fc45 100644 (file)
@@ -46,7 +46,11 @@ extern char datatoc_common_world_clip_lib_glsl[];
             GPU_SHADER_3D_DEPTH_ONLY, \
             GPU_SHADER_CAMERA, \
             GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE, \
-            GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE)
+            GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE, \
+            GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA, \
+            GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA, \
+            GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR, \
+            GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED)
 
 /* cache of built-in shaders (each is created on first use) */
 static struct {
index 45cfca4..e54ad6c 100644 (file)
@@ -343,15 +343,18 @@ DRWShadingGroup *shgroup_dynlines_dashed_uniform_color(DRWPass *pass, const floa
        return grp;
 }
 
-DRWShadingGroup *shgroup_dynpoints_uniform_color(DRWPass *pass, const float color[4], const float *size)
+DRWShadingGroup *shgroup_dynpoints_uniform_color(
+        DRWPass *pass, const float color[4], const float *size, eDRW_ShaderSlot shader_slot)
 {
-       GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA);
+       GPUShader *sh = DRW_shader_get_builtin_shader(GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA, shader_slot);
 
        DRWShadingGroup *grp = DRW_shgroup_point_batch_create(sh, pass);
        DRW_shgroup_uniform_vec4(grp, "color", color, 1);
        DRW_shgroup_uniform_float(grp, "size", size, 1);
        DRW_shgroup_state_enable(grp, DRW_STATE_POINT);
-
+       if (shader_slot == DRW_SHADER_SLOT_CLIPPED) {
+               DRW_shgroup_world_clip_planes_from_rv3d(grp, DRW_context_state_get()->rv3d);
+       }
        return grp;
 }
 
@@ -376,9 +379,10 @@ DRWShadingGroup *shgroup_groundpoints_uniform_color(DRWPass *pass, const float c
        return grp;
 }
 
-DRWShadingGroup *shgroup_instance_screenspace(DRWPass *pass, struct GPUBatch *geom, const float *size)
+DRWShadingGroup *shgroup_instance_screenspace(
+        DRWPass *pass, struct GPUBatch *geom, const float *size, eDRW_ShaderSlot shader_slot)
 {
-       GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR);
+       GPUShader *sh = DRW_shader_get_builtin_shader(GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR, shader_slot);
 
        DRW_shgroup_instance_format(g_formats.instance_screenspace, {
                {"world_pos", DRW_ATTRIB_FLOAT, 3},
@@ -389,7 +393,9 @@ DRWShadingGroup *shgroup_instance_screenspace(DRWPass *pass, struct GPUBatch *ge
        DRW_shgroup_uniform_float(grp, "size", size, 1);
        DRW_shgroup_uniform_float(grp, "pixel_size", DRW_viewport_pixelsize_get(), 1);
        DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
-
+       if (shader_slot == DRW_SHADER_SLOT_CLIPPED) {
+               DRW_shgroup_world_clip_planes_from_rv3d(grp, DRW_context_state_get()->rv3d);
+       }
        return grp;
 }
 
@@ -423,9 +429,10 @@ DRWShadingGroup *shgroup_instance_wire(DRWPass *pass, struct GPUBatch *geom)
        return grp;
 }
 
-DRWShadingGroup *shgroup_instance_screen_aligned(DRWPass *pass, struct GPUBatch *geom)
+DRWShadingGroup *shgroup_instance_screen_aligned(
+        DRWPass *pass, struct GPUBatch *geom, eDRW_ShaderSlot shader_slot)
 {
-       GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED);
+       GPUShader *sh = DRW_shader_get_builtin_shader(GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED, shader_slot);
 
        DRW_shgroup_instance_format(g_formats.instance_screen_aligned, {
                {"color",               DRW_ATTRIB_FLOAT, 3},
@@ -435,7 +442,9 @@ DRWShadingGroup *shgroup_instance_screen_aligned(DRWPass *pass, struct GPUBatch
 
        DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom, g_formats.instance_screen_aligned);
        DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
-
+       if (shader_slot == DRW_SHADER_SLOT_CLIPPED) {
+               DRW_shgroup_world_clip_planes_from_rv3d(grp, DRW_context_state_get()->rv3d);
+       }
        return grp;
 }
 
index 644e81e..39a8956 100644 (file)
@@ -142,13 +142,13 @@ void DRW_shgroup_world_clip_planes_from_rv3d(struct DRWShadingGroup *shgrp, cons
 
 struct DRWShadingGroup *shgroup_dynlines_flat_color(struct DRWPass *pass);
 struct DRWShadingGroup *shgroup_dynlines_dashed_uniform_color(struct DRWPass *pass, const float color[4]);
-struct DRWShadingGroup *shgroup_dynpoints_uniform_color(struct DRWPass *pass, const float color[4], const float *size);
+struct DRWShadingGroup *shgroup_dynpoints_uniform_color(struct DRWPass *pass, const float color[4], const float *size, eDRW_ShaderSlot shader_slot);
 struct DRWShadingGroup *shgroup_groundlines_uniform_color(struct DRWPass *pass, const float color[4]);
 struct DRWShadingGroup *shgroup_groundpoints_uniform_color(struct DRWPass *pass, const float color[4]);
-struct DRWShadingGroup *shgroup_instance_screenspace(struct DRWPass *pass, struct GPUBatch *geom, const float *size);
+struct DRWShadingGroup *shgroup_instance_screenspace(struct DRWPass *pass, struct GPUBatch *geom, const float *size, eDRW_ShaderSlot shader_slot);
 struct DRWShadingGroup *shgroup_instance_solid(struct DRWPass *pass, struct GPUBatch *geom);
 struct DRWShadingGroup *shgroup_instance_wire(struct DRWPass *pass, struct GPUBatch *geom);
-struct DRWShadingGroup *shgroup_instance_screen_aligned(struct DRWPass *pass, struct GPUBatch *geom);
+struct DRWShadingGroup *shgroup_instance_screen_aligned(struct DRWPass *pass, struct GPUBatch *geom, eDRW_ShaderSlot shader_slot);
 struct DRWShadingGroup *shgroup_instance_empty_axes(struct DRWPass *pass, struct GPUBatch *geom, eDRW_ShaderSlot shader_slot);
 struct DRWShadingGroup *shgroup_instance_scaled(struct DRWPass *pass, struct GPUBatch *geom, eDRW_ShaderSlot shader_slot);
 struct DRWShadingGroup *shgroup_instance(struct DRWPass *pass, struct GPUBatch *geom, eDRW_ShaderSlot shader_slot);
index 8b5c5e9..4c96a5d 100644 (file)
@@ -1210,13 +1210,13 @@ static void OBJECT_cache_init(void *vedata)
                sgl->field_wind = shgroup_instance_scaled(sgl->non_meshes, geom, draw_ctx->shader_slot);
 
                geom = DRW_cache_field_force_get();
-               sgl->field_force = shgroup_instance_screen_aligned(sgl->non_meshes, geom);
+               sgl->field_force = shgroup_instance_screen_aligned(sgl->non_meshes, geom, draw_ctx->shader_slot);
 
                geom = DRW_cache_field_vortex_get();
                sgl->field_vortex = shgroup_instance_scaled(sgl->non_meshes, geom, draw_ctx->shader_slot);
 
                geom = DRW_cache_screenspace_circle_get();
-               sgl->field_curve_sta = shgroup_instance_screen_aligned(sgl->non_meshes, geom);
+               sgl->field_curve_sta = shgroup_instance_screen_aligned(sgl->non_meshes, geom, draw_ctx->shader_slot);
 
                /* Grease Pencil */
                geom = DRW_cache_gpencil_axes_get();
@@ -1229,14 +1229,14 @@ static void OBJECT_cache_init(void *vedata)
                /* Probe */
                static float probeSize = 14.0f;
                geom = DRW_cache_lightprobe_cube_get();
-               sgl->probe_cube = shgroup_instance_screenspace(sgl->non_meshes, geom, &probeSize);
+               sgl->probe_cube = shgroup_instance_screenspace(sgl->non_meshes, geom, &probeSize, draw_ctx->shader_slot);
 
                geom = DRW_cache_lightprobe_grid_get();
-               sgl->probe_grid = shgroup_instance_screenspace(sgl->non_meshes, geom, &probeSize);
+               sgl->probe_grid = shgroup_instance_screenspace(sgl->non_meshes, geom, &probeSize, draw_ctx->shader_slot);
 
                static float probePlanarSize = 20.0f;
                geom = DRW_cache_lightprobe_planar_get();
-               sgl->probe_planar = shgroup_instance_screenspace(sgl->non_meshes, geom, &probePlanarSize);
+               sgl->probe_planar = shgroup_instance_screenspace(sgl->non_meshes, geom, &probePlanarSize, draw_ctx->shader_slot);
 
                /* Camera */
                geom = DRW_cache_camera_get();
@@ -1312,21 +1312,21 @@ static void OBJECT_cache_init(void *vedata)
                geom = DRW_cache_single_line_get();
                sgl->lamp_buflimit = shgroup_distance_lines_instance(sgl->non_meshes, geom);
 
-               sgl->lamp_center = shgroup_dynpoints_uniform_color(sgl->non_meshes, gb->colorLampNoAlpha, &gb->sizeLampCenter);
+               sgl->lamp_center = shgroup_dynpoints_uniform_color(sgl->non_meshes, gb->colorLampNoAlpha, &gb->sizeLampCenter, draw_ctx->shader_slot);
 
                geom = DRW_cache_lamp_get();
-               sgl->lamp_circle = shgroup_instance_screenspace(sgl->non_meshes, geom, &gb->sizeLampCircle);
+               sgl->lamp_circle = shgroup_instance_screenspace(sgl->non_meshes, geom, &gb->sizeLampCircle, draw_ctx->shader_slot);
                geom = DRW_cache_lamp_shadows_get();
-               sgl->lamp_circle_shadow = shgroup_instance_screenspace(sgl->non_meshes, geom, &gb->sizeLampCircleShadow);
+               sgl->lamp_circle_shadow = shgroup_instance_screenspace(sgl->non_meshes, geom, &gb->sizeLampCircleShadow, draw_ctx->shader_slot);
 
                geom = DRW_cache_lamp_sunrays_get();
-               sgl->lamp_sunrays = shgroup_instance_screenspace(sgl->non_meshes, geom, &gb->sizeLampCircle);
+               sgl->lamp_sunrays = shgroup_instance_screenspace(sgl->non_meshes, geom, &gb->sizeLampCircle, draw_ctx->shader_slot);
 
                sgl->lamp_groundline = shgroup_groundlines_uniform_color(sgl->non_meshes, gb->colorLamp);
                sgl->lamp_groundpoint = shgroup_groundpoints_uniform_color(sgl->non_meshes, gb->colorLamp);
 
                geom = DRW_cache_screenspace_circle_get();
-               sgl->lamp_area_sphere = shgroup_instance_screen_aligned(sgl->non_meshes, geom);
+               sgl->lamp_area_sphere = shgroup_instance_screen_aligned(sgl->non_meshes, geom, draw_ctx->shader_slot);
 
                geom = DRW_cache_lamp_area_square_get();
                sgl->lamp_area_square = shgroup_instance(sgl->non_meshes, geom, draw_ctx->shader_slot);
@@ -1364,7 +1364,7 @@ static void OBJECT_cache_init(void *vedata)
                /* Force Field Curve Guide End (here because of stipple) */
                /* TODO port to shader stipple */
                geom = DRW_cache_screenspace_circle_get();
-               sgl->field_curve_end = shgroup_instance_screen_aligned(sgl->non_meshes, geom);
+               sgl->field_curve_end = shgroup_instance_screen_aligned(sgl->non_meshes, geom, draw_ctx->shader_slot);
 
                /* Force Field Limits */
                /* TODO port to shader stipple */
@@ -1407,7 +1407,8 @@ static void OBJECT_cache_init(void *vedata)
                outlineWidth = 1.0f * U.pixelsize;
                size = U.obcenter_dia * U.pixelsize + outlineWidth;
 
-               GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA);
+               GPUShader *sh = DRW_shader_get_builtin_shader(
+                       GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA, draw_ctx->shader_slot);
 
                /* Active */
                grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
@@ -1415,26 +1416,41 @@ static void OBJECT_cache_init(void *vedata)
                DRW_shgroup_uniform_float(grp, "outlineWidth", &outlineWidth, 1);
                DRW_shgroup_uniform_vec4(grp, "color", gb->colorActive, 1);
                DRW_shgroup_uniform_vec4(grp, "outlineColor", gb->colorOutline, 1);
+               if (draw_ctx->shader_slot == DRW_SHADER_SLOT_CLIPPED) {
+                       DRW_shgroup_world_clip_planes_from_rv3d(grp, draw_ctx->rv3d);
+               }
                stl->g_data->center_active = grp;
 
                /* Select */
                grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
                DRW_shgroup_uniform_vec4(grp, "color", gb->colorSelect, 1);
+               if (draw_ctx->shader_slot == DRW_SHADER_SLOT_CLIPPED) {
+                       DRW_shgroup_world_clip_planes_from_rv3d(grp, draw_ctx->rv3d);
+               }
                stl->g_data->center_selected = grp;
 
                /* Deselect */
                grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
                DRW_shgroup_uniform_vec4(grp, "color", gb->colorDeselect, 1);
+               if (draw_ctx->shader_slot == DRW_SHADER_SLOT_CLIPPED) {
+                       DRW_shgroup_world_clip_planes_from_rv3d(grp, draw_ctx->rv3d);
+               }
                stl->g_data->center_deselected = grp;
 
                /* Select (library) */
                grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
                DRW_shgroup_uniform_vec4(grp, "color", gb->colorLibrarySelect, 1);
+               if (draw_ctx->shader_slot == DRW_SHADER_SLOT_CLIPPED) {
+                       DRW_shgroup_world_clip_planes_from_rv3d(grp, draw_ctx->rv3d);
+               }
                stl->g_data->center_selected_lib = grp;
 
                /* Deselect (library) */
                grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
                DRW_shgroup_uniform_vec4(grp, "color", gb->colorLibrary, 1);
+               if (draw_ctx->shader_slot == DRW_SHADER_SLOT_CLIPPED) {
+                       DRW_shgroup_world_clip_planes_from_rv3d(grp, draw_ctx->rv3d);
+               }
                stl->g_data->center_deselected_lib = grp;
        }
 
index ebc945f..f39c10e 100644 (file)
@@ -1,12 +1,16 @@
 
 uniform mat4 ModelViewProjectionMatrix;
+#ifdef USE_WORLD_CLIP_PLANES
+uniform mat4 ModelMatrix;
+#endif
 uniform float size;
 
 in vec3 pos;
 out vec2 radii;
 
 void main() {
-       gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
+       vec4 pos_4d = vec4(pos, 1.0);
+       gl_Position = ModelViewProjectionMatrix * pos_4d;
        gl_PointSize = size;
 
        // calculate concentric radii in pixels
@@ -18,4 +22,8 @@ void main() {
 
        // convert to PointCoord units
        radii /= size;
+
+#ifdef USE_WORLD_CLIP_PLANES
+       world_clip_planes_calc_clip_distance((ModelMatrix * pos_4d).xyz);
+#endif
 }
index 0d6b90c..e14032a 100644 (file)
@@ -1,5 +1,8 @@
 
 uniform mat4 ModelViewProjectionMatrix;
+#ifdef USE_WORLD_CLIP_PLANES
+uniform mat4 ModelMatrix;
+#endif
 uniform float size;
 uniform float outlineWidth;
 
@@ -7,7 +10,8 @@ in vec3 pos;
 out vec4 radii;
 
 void main() {
-       gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
+       vec4 pos_4d = vec4(pos, 1.0);
+       gl_Position = ModelViewProjectionMatrix * pos_4d;
        gl_PointSize = size;
 
        // calculate concentric radii in pixels
@@ -21,4 +25,8 @@ void main() {
 
        // convert to PointCoord units
        radii /= size;
+
+#ifdef USE_WORLD_CLIP_PLANES
+       world_clip_planes_calc_clip_distance((ModelMatrix * pos_4d).xyz);
+#endif
 }
index 429b648..f4f5b26 100644 (file)
@@ -1,5 +1,8 @@
 
 uniform mat4 ViewProjectionMatrix;
+#ifdef USE_WORLD_CLIP_PLANES
+uniform mat4 ModelMatrix;
+#endif
 uniform vec3 screen_vecs[2];
 
 /* ---- Instantiated Attribs ---- */
@@ -27,6 +30,11 @@ void main()
 #endif
 
        vec3 screen_pos = screen_vecs[0].xyz * pos.x + screen_vecs[1].xyz * pos.y;
-       gl_Position = ViewProjectionMatrix * (InstanceModelMatrix * vec4(offset, 1.0) + vec4(screen_pos * size, 0.0));
+       vec4 pos_4d = InstanceModelMatrix * vec4(offset, 1.0) + vec4(screen_pos * size, 0.0);
+       gl_Position = ViewProjectionMatrix * pos_4d;
        finalColor = vec4(color, 1.0);
+
+#ifdef USE_WORLD_CLIP_PLANES
+       world_clip_planes_calc_clip_distance((ModelMatrix * pos_4d).xyz);
+#endif
 }
index 2a4675c..3baea76 100644 (file)
@@ -26,4 +26,8 @@ void main()
        vec3 screen_pos = screen_vecs[0].xyz * pos.x + screen_vecs[1].xyz * pos.y;
        gl_Position = ViewProjectionMatrix * vec4(world_pos + screen_pos * size * pix_size, 1.0);
        finalColor = vec4(color, 1.0);
+
+#ifdef USE_WORLD_CLIP_PLANES
+   world_clip_planes_calc_clip_distance(world_pos);
+#endif
 }