Eevee: Add support/Fix Object Info node

Caveat: Random output does not yet work with instance (dupli) objects.
This commit is contained in:
Clément Foucault 2018-10-09 17:32:13 +02:00
parent 6d6e3869ce
commit e234ce9b22
10 changed files with 62 additions and 24 deletions

View File

@ -1080,23 +1080,23 @@ void EEVEE_materials_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
}
}
#define ADD_SHGROUP_CALL(shgrp, ob, geom, oedata) do { \
#define ADD_SHGROUP_CALL(shgrp, ob, ma, geom, oedata) do { \
if (is_sculpt_mode_draw) { \
DRW_shgroup_call_sculpt_add(shgrp, ob, ob->obmat); \
} \
else { \
if (oedata) { \
DRW_shgroup_call_object_add_with_callback(shgrp, geom, ob, EEVEE_lightprobes_obj_visibility_cb, oedata); \
DRW_shgroup_call_object_add_with_callback(shgrp, geom, ob, ma, EEVEE_lightprobes_obj_visibility_cb, oedata); \
} \
else { \
DRW_shgroup_call_object_add(shgrp, geom, ob); \
DRW_shgroup_call_object_add_ex(shgrp, geom, ob, ma, false); \
} \
} \
} while (0)
#define ADD_SHGROUP_CALL_SAFE(shgrp, ob, geom, oedata) do { \
#define ADD_SHGROUP_CALL_SAFE(shgrp, ob, ma, geom, oedata) do { \
if (shgrp) { \
ADD_SHGROUP_CALL(shgrp, ob, geom, oedata); \
ADD_SHGROUP_CALL(shgrp, ob, ma, geom, oedata); \
} \
} while (0)
@ -1536,11 +1536,11 @@ void EEVEE_materials_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sld
}
/* Shading pass */
ADD_SHGROUP_CALL(shgrp_array[i], ob, mat_geom[i], oedata);
ADD_SHGROUP_CALL(shgrp_array[i], ob, ma, mat_geom[i], oedata);
/* Depth Prepass */
ADD_SHGROUP_CALL_SAFE(shgrp_depth_array[i], ob, mat_geom[i], oedata);
ADD_SHGROUP_CALL_SAFE(shgrp_depth_clip_array[i], ob, mat_geom[i], oedata);
ADD_SHGROUP_CALL_SAFE(shgrp_depth_array[i], ob, ma, mat_geom[i], oedata);
ADD_SHGROUP_CALL_SAFE(shgrp_depth_clip_array[i], ob, ma, mat_geom[i], oedata);
char *name = auto_layer_names;
for (int j = 0; j < auto_layer_count; ++j) {

View File

@ -363,11 +363,12 @@ void DRW_shgroup_call_procedural_points_add(DRWShadingGroup *shgroup, uint point
void DRW_shgroup_call_procedural_lines_add(DRWShadingGroup *shgroup, uint line_count, float (*obmat)[4]);
void DRW_shgroup_call_procedural_triangles_add(DRWShadingGroup *shgroup, uint tria_count, float (*obmat)[4]);
void DRW_shgroup_call_object_procedural_triangles_culled_add(DRWShadingGroup *shgroup, uint tria_count, struct Object *ob);
void DRW_shgroup_call_object_add_ex(DRWShadingGroup *shgroup, struct GPUBatch *geom, struct Object *ob, bool bypass_culling);
#define DRW_shgroup_call_object_add(shgroup, geom, ob) DRW_shgroup_call_object_add_ex(shgroup, geom, ob, false)
#define DRW_shgroup_call_object_add_no_cull(shgroup, geom, ob) DRW_shgroup_call_object_add_ex(shgroup, geom, ob, true)
void DRW_shgroup_call_object_add_ex(
DRWShadingGroup *shgroup, struct GPUBatch *geom, struct Object *ob, struct Material *ma, bool bypass_culling);
#define DRW_shgroup_call_object_add(shgroup, geom, ob) DRW_shgroup_call_object_add_ex(shgroup, geom, ob, NULL, false)
#define DRW_shgroup_call_object_add_no_cull(shgroup, geom, ob) DRW_shgroup_call_object_add_ex(shgroup, geom, ob, NULL, true)
void DRW_shgroup_call_object_add_with_callback(
DRWShadingGroup *shgroup, struct GPUBatch *geom, struct Object *ob,
DRWShadingGroup *shgroup, struct GPUBatch *geom, struct Object *ob, struct Material *ma,
DRWCallVisibilityFn *callback, void *user_data);
/* Used for drawing a batch with instancing without instance attribs. */
void DRW_shgroup_call_instances_add(

View File

@ -101,6 +101,7 @@ enum {
DRW_CALL_NORMALWORLD = (1 << 5),
DRW_CALL_ORCOTEXFAC = (1 << 6),
DRW_CALL_EYEVEC = (1 << 7),
DRW_CALL_OBJECTINFO = (1 << 8),
};
typedef struct DRWCallState {
@ -122,6 +123,7 @@ typedef struct DRWCallState {
float normalview[3][3];
float normalworld[3][3]; /* Not view dependent */
float orcotexfac[2][3]; /* Not view dependent */
float objectinfo[2];
float eyevec[3];
} DRWCallState;
@ -140,6 +142,7 @@ typedef struct DRWCall {
union {
struct { /* type == DRW_CALL_SINGLE */
GPUBatch *geometry;
short ma_index;
} single;
struct { /* type == DRW_CALL_RANGE */
GPUBatch *geometry;
@ -257,6 +260,7 @@ struct DRWShadingGroup {
int orcotexfac;
int eye;
int callid;
int objectinfo;
uint16_t matflag; /* Matrices needed, same as DRWCall.flag */
#ifndef NDEBUG

View File

@ -36,6 +36,7 @@
#include "DNA_mesh_types.h"
#include "DNA_meta_types.h"
#include "BLI_hash.h"
#include "BLI_link_utils.h"
#include "BLI_mempool.h"
@ -344,6 +345,22 @@ static DRWCallState *drw_call_state_create(DRWShadingGroup *shgroup, float (*obm
state->matflag &= ~DRW_CALL_ORCOTEXFAC;
}
if ((state->matflag & DRW_CALL_OBJECTINFO) != 0) {
state->objectinfo[0] = ob ? ob->index : 0;
unsigned int random;
#if 0 /* TODO(fclem) handle dupli objects */
if (GMS.dob) {
random = GMS.dob->random_id;
}
else
#endif
{
random = BLI_hash_int_2d(BLI_hash_string(ob->id.name + 2), 0);
}
state->objectinfo[2] = random * (1.0f / (float)0xFFFFFFFF);
state->matflag &= ~DRW_CALL_OBJECTINFO;
}
return state;
}
@ -440,7 +457,7 @@ void DRW_shgroup_call_object_procedural_triangles_culled_add(DRWShadingGroup *sh
}
/* These calls can be culled and are optimized for redraw */
void DRW_shgroup_call_object_add_ex(DRWShadingGroup *shgroup, GPUBatch *geom, Object *ob, bool bypass_culling)
void DRW_shgroup_call_object_add_ex(DRWShadingGroup *shgroup, GPUBatch *geom, Object *ob, Material *ma, bool bypass_culling)
{
BLI_assert(geom != NULL);
BLI_assert(ELEM(shgroup->type, DRW_SHG_NORMAL, DRW_SHG_FEEDBACK_TRANSFORM));
@ -449,6 +466,7 @@ void DRW_shgroup_call_object_add_ex(DRWShadingGroup *shgroup, GPUBatch *geom, Ob
call->state = drw_call_state_object(shgroup, ob->obmat, ob);
call->type = DRW_CALL_SINGLE;
call->single.geometry = geom;
call->single.ma_index = ma ? ma->index : 0;
#ifdef USE_GPU_SELECT
call->select_id = DST.select_id;
#endif
@ -460,7 +478,7 @@ void DRW_shgroup_call_object_add_ex(DRWShadingGroup *shgroup, GPUBatch *geom, Ob
}
void DRW_shgroup_call_object_add_with_callback(
DRWShadingGroup *shgroup, GPUBatch *geom, Object *ob,
DRWShadingGroup *shgroup, GPUBatch *geom, Object *ob, Material *ma,
DRWCallVisibilityFn *callback, void *user_data)
{
BLI_assert(geom != NULL);
@ -472,6 +490,7 @@ void DRW_shgroup_call_object_add_with_callback(
call->state->user_data = user_data;
call->type = DRW_CALL_SINGLE;
call->single.geometry = geom;
call->single.ma_index = ma ? ma->index : 0;
#ifdef USE_GPU_SELECT
call->select_id = DST.select_id;
#endif
@ -635,6 +654,7 @@ static void drw_shgroup_init(DRWShadingGroup *shgroup, GPUShader *shader)
shgroup->normalview = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_NORMAL);
shgroup->normalworld = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_WORLDNORMAL);
shgroup->orcotexfac = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_ORCO);
shgroup->objectinfo = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_OBJECT_INFO);
shgroup->eye = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_EYE);
shgroup->callid = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_CALLID);
@ -653,6 +673,8 @@ static void drw_shgroup_init(DRWShadingGroup *shgroup, GPUShader *shader)
shgroup->matflag |= DRW_CALL_NORMALWORLD;
if (shgroup->orcotexfac > -1)
shgroup->matflag |= DRW_CALL_ORCOTEXFAC;
if (shgroup->objectinfo > -1)
shgroup->matflag |= DRW_CALL_OBJECTINFO;
if (shgroup->eye > -1)
shgroup->matflag |= DRW_CALL_EYEVEC;
}

View File

@ -801,10 +801,16 @@ static void draw_matrices_model_prepare(DRWCallState *st)
}
}
static void draw_geometry_prepare(DRWShadingGroup *shgroup, DRWCallState *state)
static void draw_geometry_prepare(DRWShadingGroup *shgroup, DRWCall *call)
{
/* step 1 : bind object dependent matrices */
if (state != NULL) {
if (call != NULL) {
DRWCallState *state = call->state;
float objectinfo[3];
objectinfo[0] = state->objectinfo[0];
objectinfo[1] = call->single.ma_index; /* WATCH this is only valid for single drawcalls. */
objectinfo[2] = state->objectinfo[1];
GPU_shader_uniform_vector(shgroup->shader, shgroup->model, 16, 1, (float *)state->model);
GPU_shader_uniform_vector(shgroup->shader, shgroup->modelinverse, 16, 1, (float *)state->modelinverse);
GPU_shader_uniform_vector(shgroup->shader, shgroup->modelview, 16, 1, (float *)state->modelview);
@ -812,6 +818,7 @@ static void draw_geometry_prepare(DRWShadingGroup *shgroup, DRWCallState *state)
GPU_shader_uniform_vector(shgroup->shader, shgroup->modelviewprojection, 16, 1, (float *)state->modelviewprojection);
GPU_shader_uniform_vector(shgroup->shader, shgroup->normalview, 9, 1, (float *)state->normalview);
GPU_shader_uniform_vector(shgroup->shader, shgroup->normalworld, 9, 1, (float *)state->normalworld);
GPU_shader_uniform_vector(shgroup->shader, shgroup->objectinfo, 3, 1, (float *)objectinfo);
GPU_shader_uniform_vector(shgroup->shader, shgroup->orcotexfac, 3, 2, (float *)state->orcotexfac);
GPU_shader_uniform_vector(shgroup->shader, shgroup->eye, 3, 1, (float *)state->eyevec);
}
@ -825,6 +832,7 @@ static void draw_geometry_prepare(DRWShadingGroup *shgroup, DRWCallState *state)
GPU_shader_uniform_vector(shgroup->shader, shgroup->modelview, 16, 1, (float *)DST.view_data.matstate.mat[DRW_MAT_VIEW]);
GPU_shader_uniform_vector(shgroup->shader, shgroup->modelviewinverse, 16, 1, (float *)DST.view_data.matstate.mat[DRW_MAT_VIEWINV]);
GPU_shader_uniform_vector(shgroup->shader, shgroup->modelviewprojection, 16, 1, (float *)DST.view_data.matstate.mat[DRW_MAT_PERS]);
GPU_shader_uniform_vector(shgroup->shader, shgroup->objectinfo, 3, 1, (float *)unitmat);
GPU_shader_uniform_vector(shgroup->shader, shgroup->orcotexfac, 3, 2, (float *)shgroup->instance_orcofac);
}
}
@ -1205,7 +1213,7 @@ static void draw_shgroup(DRWShadingGroup *shgroup, DRWState pass_state)
}
GPU_SELECT_LOAD_IF_PICKSEL_CALL(call);
draw_geometry_prepare(shgroup, call->state);
draw_geometry_prepare(shgroup, call);
switch (call->type) {
case DRW_CALL_SINGLE:

View File

@ -58,6 +58,7 @@ typedef enum {
GPU_UNIFORM_COLOR, /* vec4 color */
GPU_UNIFORM_EYE, /* vec3 eye */
GPU_UNIFORM_CALLID, /* int callId */
GPU_UNIFORM_OBJECT_INFO, /* vec3 objectInfo */
GPU_UNIFORM_CUSTOM, /* custom uniform, not one of the above built-ins */

View File

@ -759,7 +759,7 @@ static void codegen_call_functions(DynStr *ds, ListBase *nodes, GPUOutput *final
BLI_dynstr_append(ds, ";\n");
}
static char *code_generate_fragment(GPUMaterial *material, ListBase *nodes, GPUOutput *output)
static char *code_generate_fragment(GPUMaterial *material, ListBase *nodes, GPUOutput *output, int *rbuiltins)
{
DynStr *ds = BLI_dynstr_new();
char *code;
@ -770,14 +770,13 @@ static char *code_generate_fragment(GPUMaterial *material, ListBase *nodes, GPUO
#endif
codegen_set_unique_ids(nodes);
builtins = codegen_process_uniforms_functions(material, ds, nodes);
*rbuiltins = builtins = codegen_process_uniforms_functions(material, ds, nodes);
if (builtins & GPU_BARYCENTRIC_TEXCO)
BLI_dynstr_append(ds, "\tin vec2 barycentricTexCo;\n");
BLI_dynstr_append(ds, "in vec2 barycentricTexCo;\n");
if (builtins & GPU_BARYCENTRIC_DIST)
BLI_dynstr_append(ds, "\tflat in vec3 barycentricDist;\n");
BLI_dynstr_append(ds, "flat in vec3 barycentricDist;\n");
BLI_dynstr_append(ds, "Closure nodetree_exec(void)\n{\n");
@ -1790,6 +1789,7 @@ GPUPass *GPU_generate_pass(
GPUNodeLink *frag_outlink,
struct GPUVertexAttribs *attribs,
ListBase *nodes,
int *builtins,
const char *vert_code,
const char *geom_code,
const char *frag_lib,
@ -1804,7 +1804,7 @@ GPUPass *GPU_generate_pass(
GPU_nodes_get_vertex_attributes(nodes, attribs);
/* generate code */
char *fragmentgen = code_generate_fragment(material, nodes, frag_outlink->output);
char *fragmentgen = code_generate_fragment(material, nodes, frag_outlink->output, builtins);
/* Cache lookup: Reuse shaders already compiled */
uint32_t hash = gpu_pass_hash(fragmentgen, defines, attribs);

View File

@ -179,7 +179,7 @@ typedef struct GPUPass GPUPass;
GPUPass *GPU_generate_pass(
GPUMaterial *material,
GPUNodeLink *frag_outlink, struct GPUVertexAttribs *attribs,
ListBase *nodes,
ListBase *nodes, int *builtins,
const char *vert_code, const char *geom_code,
const char *frag_lib, const char *defines);

View File

@ -688,6 +688,7 @@ GPUMaterial *GPU_material_from_nodetree(
mat->outlink,
&mat->attribs,
&mat->nodes,
&mat->builtins,
vert_code,
geom_code,
frag_lib,

View File

@ -72,6 +72,7 @@ static const char *BuiltinUniform_name(GPUUniformBuiltin u)
[GPU_UNIFORM_COLOR] = "color",
[GPU_UNIFORM_EYE] = "eye",
[GPU_UNIFORM_CALLID] = "callId",
[GPU_UNIFORM_OBJECT_INFO] = "unfobjectinfo",
[GPU_UNIFORM_CUSTOM] = NULL,
[GPU_NUM_UNIFORMS] = NULL,