DRW: support clipping for camera objects
authorCampbell Barton <ideasman42@gmail.com>
Thu, 24 Jan 2019 20:13:02 +0000 (07:13 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 24 Jan 2019 20:13:02 +0000 (07:13 +1100)
source/blender/draw/intern/DRW_render.h
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_instance_camera_vert.glsl

index 395d9483deb4f651b17eb7c4c1dfe6210caee703..884b35c4acb28c7f9d7e626d562b9a5712e6717a 100644 (file)
@@ -94,16 +94,6 @@ typedef struct BoundSphere {
        float center[3], radius;
 } BoundSphere;
 
-/**
- * Support selecting shaders with different options compiled in.
- * Needed for clipping support because it means using a separate set of shaders.
- */
-typedef enum eDRW_ShaderSlot {
-       DRW_SHADER_SLOT_DEFAULT = 0,
-       DRW_SHADER_SLOT_CLIPPED = 1,
-} eDRW_ShaderSlot;
-#define DRW_SHADER_SLOT_LEN 2
-
 /* declare members as empty (unused) */
 typedef char DRWViewportEmptyList;
 
index 5c0c005cb9db4e0b28e6aba2d04c1e0dda748f0c..6a70b91d67a98060303369d2040502847d690f7d 100644 (file)
@@ -44,6 +44,8 @@ extern char datatoc_drw_shader_3D_smooth_color_vert_glsl[];
 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
 extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];
 extern char datatoc_gpu_shader_depth_only_frag_glsl[];
+extern char datatoc_gpu_shader_flat_color_frag_glsl[];
+extern char datatoc_gpu_shader_instance_camera_vert_glsl[];
 
 /* cache of built-in shaders (each is created on first use) */
 static struct {
@@ -88,6 +90,15 @@ static GPUShader *drw_shader_get_builtin_shader_clipped(eGPUBuiltinShader shader
                                .frag = (const char *[]){datatoc_gpu_shader_depth_only_frag_glsl, NULL},
                                .defs = (const char *[]){world_clip_def, NULL}});
                        break;
+               case GPU_SHADER_CAMERA:
+                       if (r_test_only) {
+                               break;
+                       }
+                       shader = DRW_shader_create_from_arrays({
+                               .vert = (const char *[]){world_clip_lib, datatoc_gpu_shader_instance_camera_vert_glsl, NULL},
+                               .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
+                               .defs = (const char *[]){world_clip_def, NULL}});
+                       break;
                default:
                        /* Unsupported, caller asserts. */
                        if (r_test_only) {
index 437e6f63095745a6244d82a870297f94b2be9150..e7a129071b673de9db99fa56bc61ac0883fa853b 100644 (file)
@@ -38,6 +38,7 @@
 #include "BKE_colorband.h"
 
 #include "draw_common.h"
+#include "draw_builtin_shader.h"
 
 #if 0
 #define UI_COLOR_RGB_FROM_U8(r, g, b, v4) \
@@ -515,9 +516,9 @@ DRWShadingGroup *shgroup_instance_outline(DRWPass *pass, struct GPUBatch *geom,
        return grp;
 }
 
-DRWShadingGroup *shgroup_camera_instance(DRWPass *pass, struct GPUBatch *geom)
+DRWShadingGroup *shgroup_camera_instance(DRWPass *pass, struct GPUBatch *geom, eDRW_ShaderSlot shader_slot)
 {
-       GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_CAMERA);
+       GPUShader *sh_inst = DRW_shader_get_builtin_shader(GPU_SHADER_CAMERA, shader_slot);
 
        DRW_shgroup_instance_format(g_formats.instance_camera, {
                {"color",               DRW_ATTRIB_FLOAT, 3},
@@ -528,7 +529,9 @@ DRWShadingGroup *shgroup_camera_instance(DRWPass *pass, struct GPUBatch *geom)
        });
 
        DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom, g_formats.instance_camera);
-
+       if (shader_slot == DRW_SHADER_SLOT_CLIPPED) {
+               DRW_shgroup_world_clip_planes_from_rv3d(grp, DRW_context_state_get()->rv3d);
+       }
        return grp;
 }
 
index 854a8abbea9263038bdf477fc990a6f085c28ea6..2190db8bf311e535ba38ec242648b64e71f235cf 100644 (file)
@@ -39,6 +39,16 @@ struct ModifierData;
 struct ParticleSystem;
 struct PTCacheEdit;
 
+/**
+ * Support selecting shaders with different options compiled in.
+ * Needed for clipping support because it means using a separate set of shaders.
+ */
+typedef enum eDRW_ShaderSlot {
+       DRW_SHADER_SLOT_DEFAULT = 0,
+       DRW_SHADER_SLOT_CLIPPED = 1,
+} eDRW_ShaderSlot;
+#define DRW_SHADER_SLOT_LEN 2
+
 #define UBO_FIRST_COLOR colorWire
 #define UBO_LAST_COLOR colorGridAxisZ
 
@@ -144,7 +154,7 @@ struct DRWShadingGroup *shgroup_instance_scaled(struct DRWPass *pass, struct GPU
 struct DRWShadingGroup *shgroup_instance(struct DRWPass *pass, struct GPUBatch *geom);
 struct DRWShadingGroup *shgroup_instance_alpha(struct DRWPass *pass, struct GPUBatch *geom);
 struct DRWShadingGroup *shgroup_instance_outline(struct DRWPass *pass, struct GPUBatch *geom, int *baseid);
-struct DRWShadingGroup *shgroup_camera_instance(struct DRWPass *pass, struct GPUBatch *geom);
+struct DRWShadingGroup *shgroup_camera_instance(struct DRWPass *pass, struct GPUBatch *geom, eDRW_ShaderSlot shader_slot);
 struct DRWShadingGroup *shgroup_distance_lines_instance(struct DRWPass *pass, struct GPUBatch *geom);
 struct DRWShadingGroup *shgroup_spot_instance(struct DRWPass *pass, struct GPUBatch *geom);
 struct DRWShadingGroup *shgroup_instance_mball_handles(struct DRWPass *pass);
index 0757403ad01b13cd7354fbab35cf346ef861df2d..52a173e433f13c6231019c059b1245e54ffcafe0 100644 (file)
@@ -1243,13 +1243,13 @@ static void OBJECT_cache_init(void *vedata)
 
                /* Camera */
                geom = DRW_cache_camera_get();
-               sgl->camera = shgroup_camera_instance(sgl->non_meshes, geom);
+               sgl->camera = shgroup_camera_instance(sgl->non_meshes, geom, draw_ctx->shader_slot);
 
                geom = DRW_cache_camera_frame_get();
-               sgl->camera_frame = shgroup_camera_instance(sgl->non_meshes, geom);
+               sgl->camera_frame = shgroup_camera_instance(sgl->non_meshes, geom, draw_ctx->shader_slot);
 
                geom = DRW_cache_camera_tria_get();
-               sgl->camera_tria = shgroup_camera_instance(sgl->non_meshes, geom);
+               sgl->camera_tria = shgroup_camera_instance(sgl->non_meshes, geom, draw_ctx->shader_slot);
 
                geom = DRW_cache_plain_axes_get();
                sgl->camera_focus = shgroup_instance(sgl->non_meshes, geom);
index 805ecd6085fd17c6013095625ee3e1cb52071649..073283858389d56cb3f31c22eb386a62effc3ef3 100644 (file)
@@ -1,5 +1,8 @@
 
 uniform mat4 ViewProjectionMatrix;
+#ifdef USE_WORLD_CLIP_PLANES
+uniform mat4 ModelMatrix;
+#endif
 
 /* ---- Instantiated Attribs ---- */
 in float pos;
@@ -47,4 +50,8 @@ void main()
        gl_Position = ViewProjectionMatrix * InstanceModelMatrix * vec4(pPos, 1.0);
 
        finalColor = vec4(color, 1.0);
+
+#ifdef USE_WORLD_CLIP_PLANES
+       world_clip_planes_calc_clip_distance((ModelMatrix * InstanceModelMatrix * vec4(pPos, 1.0)).xyz);
+#endif
 }