Drawing API: shgroup_instance and shgroup_instance_alpha clarification
authorDalai Felinto <dfelinto@gmail.com>
Fri, 11 Jan 2019 19:50:45 +0000 (17:50 -0200)
committerDalai Felinto <dfelinto@gmail.com>
Fri, 11 Jan 2019 23:51:15 +0000 (21:51 -0200)
shgroup_instance_alpha was getting a color[4] but would only use the
alpha defined upon creation of the shading group.

This was very limiting since it wouldn't allow for different instances
to have different alpha values.

Patch made with ClĂ©ment Foucault (he made the code of it, while I fixed
all the parts of the code that were relying on shgroup_instance_alpha.

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_variying_size_variying_color_vert.glsl

index 9b969c2..15a8675 100644 (file)
@@ -444,18 +444,18 @@ DRWShadingGroup *shgroup_instance(DRWPass *pass, struct GPUBatch *geom)
        GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE);
 
        DRW_shgroup_instance_format(g_formats.instance_sized, {
-               {"color",               DRW_ATTRIB_FLOAT, 3},
+               {"color",               DRW_ATTRIB_FLOAT, 4},
                {"size",                DRW_ATTRIB_FLOAT, 1},
                {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
        });
 
        DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom, g_formats.instance_sized);
-       DRW_shgroup_uniform_float_copy(grp, "alpha", 1.0f);
+       DRW_shgroup_state_disable(grp, DRW_STATE_BLEND);
 
        return grp;
 }
 
-DRWShadingGroup *shgroup_instance_alpha(DRWPass *pass, struct GPUBatch *geom, float alpha)
+DRWShadingGroup *shgroup_instance_alpha(DRWPass *pass, struct GPUBatch *geom)
 {
        GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE);
 
@@ -466,7 +466,6 @@ DRWShadingGroup *shgroup_instance_alpha(DRWPass *pass, struct GPUBatch *geom, fl
        });
 
        DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom, g_formats.instance_sized);
-       DRW_shgroup_uniform_float_copy(grp, "alpha", alpha);
 
        return grp;
 }
index ca4bf96..b79902b 100644 (file)
@@ -137,7 +137,7 @@ struct DRWShadingGroup *shgroup_instance_screen_aligned(struct DRWPass *pass, st
 struct DRWShadingGroup *shgroup_instance_empty_axes(struct DRWPass *pass, struct GPUBatch *geom);
 struct DRWShadingGroup *shgroup_instance_scaled(struct DRWPass *pass, struct GPUBatch *geom);
 struct DRWShadingGroup *shgroup_instance(struct DRWPass *pass, struct GPUBatch *geom);
-struct DRWShadingGroup *shgroup_instance_alpha(struct DRWPass *pass, struct GPUBatch *geom, float alpha);
+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_distance_lines_instance(struct DRWPass *pass, struct GPUBatch *geom);
index 69de0d7..9046865 100644 (file)
@@ -1368,22 +1368,20 @@ static void OBJECT_cache_init(void *vedata)
                /* Spot shapes */
                state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND | DRW_STATE_CULL_FRONT;
                sgl->spot_shapes = psl->spot_shapes[i] = DRW_pass_create("Spot Shape Pass", state);
-               float cone_spot_alpha = 0.5f;
 
                geom = DRW_cache_lamp_spot_volume_get();
-               sgl->lamp_spot_volume = shgroup_instance_alpha(sgl->spot_shapes, geom, cone_spot_alpha);
+               sgl->lamp_spot_volume = shgroup_instance_alpha(sgl->spot_shapes, geom);
 
                geom = DRW_cache_lamp_spot_square_volume_get();
-               sgl->lamp_spot_volume_rect = shgroup_instance_alpha(sgl->spot_shapes, geom, cone_spot_alpha);
+               sgl->lamp_spot_volume_rect = shgroup_instance_alpha(sgl->spot_shapes, geom);
 
-               cone_spot_alpha = 0.3f;
                geom = DRW_cache_lamp_spot_volume_get();
-               sgl->lamp_spot_volume_outside = shgroup_instance_alpha(sgl->spot_shapes, geom, cone_spot_alpha);
+               sgl->lamp_spot_volume_outside = shgroup_instance_alpha(sgl->spot_shapes, geom);
                DRW_shgroup_state_disable(sgl->lamp_spot_volume_outside, DRW_STATE_CULL_FRONT);
                DRW_shgroup_state_enable(sgl->lamp_spot_volume_outside, DRW_STATE_CULL_BACK);
 
                geom = DRW_cache_lamp_spot_square_volume_get();
-               sgl->lamp_spot_volume_rect_outside = shgroup_instance_alpha(sgl->spot_shapes, geom, cone_spot_alpha);
+               sgl->lamp_spot_volume_rect_outside = shgroup_instance_alpha(sgl->spot_shapes, geom);
                DRW_shgroup_state_disable(sgl->lamp_spot_volume_rect_outside, DRW_STATE_CULL_FRONT);
                DRW_shgroup_state_enable(sgl->lamp_spot_volume_rect_outside, DRW_STATE_CULL_BACK);
        }
@@ -1524,8 +1522,8 @@ static void DRW_shgroup_lamp(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer
        else if (la->type == LA_SPOT) {
                float size[3], sizemat[4][4];
                static float one = 1.0f;
-               float cone_inside[3] = {0.0f, 0.0f, 0.0f};
-               float cone_outside[3] = {1.0f, 1.0f, 1.0f};
+               float cone_inside[4] = {0.0f, 0.0f, 0.0f, 0.5f};
+               float cone_outside[4] = {1.0f, 1.0f, 1.0f, 0.3f};
                float blend = 1.0f - pow2f(la->spotblend);
                size[0] = size[1] = sinf(la->spotsize * 0.5f) * la->dist;
                size[2] = cosf(la->spotsize * 0.5f) * la->dist;
index 1ec158a..0d0413f 100644 (file)
@@ -1,13 +1,12 @@
 
 uniform mat4 ViewProjectionMatrix;
-uniform float alpha;
 
 /* ---- Instantiated Attribs ---- */
 in vec3 pos;
 
 /* ---- Per instance Attribs ---- */
 in mat4 InstanceModelMatrix;
-in vec3 color;
+in vec4 color;
 #ifdef UNIFORM_SCALE
 in float size;
 #else
@@ -19,5 +18,5 @@ flat out vec4 finalColor;
 void main()
 {
        gl_Position = ViewProjectionMatrix * InstanceModelMatrix * vec4(pos * size, 1.0);
-       finalColor = vec4(color, alpha);
+       finalColor = color;
 }