Particle Info node support for GLSL mode and the internal render.

With this patch "Particle Info" node from Cycles works in GLSL and BI

Alexander (Blend4Web Team)

Reviewers: psy-fi
Note: moved particle info to object render instance instead of
shadeinput during review - Antony.

Differential Revision: https://developer.blender.org/D1313
This commit is contained in:
Alexander Romanov 2015-07-14 18:48:54 +02:00 committed by Antony Riakiotakis
parent 107e34407d
commit 38940662e5
Notes: blender-bot 2023-02-13 12:03:16 +01:00
Referenced by commit c6049d1371, Fix T46194: Crash rendering particles
15 changed files with 204 additions and 9 deletions

View File

@ -141,6 +141,7 @@ shader_node_categories = [
NodeItem("ShaderNodeTexture"),
NodeItem("ShaderNodeGeometry"),
NodeItem("ShaderNodeExtendedMaterial"),
NodeItem("ShaderNodeParticleInfo"),
NodeItem("NodeGroupInput", poll=group_input_output_item_poll),
]),
ShaderOldNodeCategory("SH_OUTPUT", "Output", items=[

View File

@ -2191,7 +2191,9 @@ static void draw_dupli_objects_color(
}
else {
copy_m4_m4(dob->ob->obmat, dob->mat);
GPU_begin_dupli_object(dob);
draw_object(scene, ar, v3d, &tbase, dflag_dupli);
GPU_end_dupli_object();
}
}

View File

@ -45,6 +45,7 @@ struct Scene;
struct View3D;
struct RegionView3D;
struct SmokeModifierData;
struct DupliObject;
/* OpenGL drawing functions related to shading. These are also
* shared with the game engine, where there were previously
@ -72,6 +73,9 @@ void GPU_end_object_materials(void);
int GPU_enable_material(int nr, void *attribs);
void GPU_disable_material(void);
void GPU_begin_dupli_object(struct DupliObject *dob);
void GPU_end_dupli_object(void);
void GPU_material_diffuse_get(int nr, float diff[4]);
bool GPU_material_use_matcaps_get(void);

View File

@ -62,6 +62,7 @@ typedef struct GPUNode GPUNode;
typedef struct GPUNodeLink GPUNodeLink;
typedef struct GPUMaterial GPUMaterial;
typedef struct GPULamp GPULamp;
typedef struct GPUParticleInfo GPUParticleInfo;
/* Functions to create GPU Materials nodes */
@ -90,6 +91,10 @@ typedef enum GPUBuiltin {
GPU_OBCOLOR = (1 << 6),
GPU_AUTO_BUMPSCALE = (1 << 7),
GPU_CAMERA_TEXCO_FACTORS = (1 << 8),
GPU_PARTICLE_SCALAR_PROPS = (1 << 9),
GPU_PARTICLE_LOCATION = (1 << 10),
GPU_PARTICLE_VELOCITY = (1 << 11),
GPU_PARTICLE_ANG_VELOCITY = (1 << 12),
} GPUBuiltin;
typedef enum GPUOpenGLBuiltin {
@ -189,6 +194,7 @@ GPUNodeLink *GPU_texture(int size, float *pixels);
GPUNodeLink *GPU_dynamic_texture(struct GPUTexture *tex, GPUDynamicType dynamictype, void *data);
GPUNodeLink *GPU_builtin(GPUBuiltin builtin);
GPUNodeLink *GPU_opengl_builtin(GPUOpenGLBuiltin builtin);
void GPU_node_link_set_type(GPUNodeLink *link, GPUType type);
bool GPU_link(GPUMaterial *mat, const char *name, ...);
bool GPU_stack_link(GPUMaterial *mat, const char *name, GPUNodeStack *in, GPUNodeStack *out, ...);
@ -208,7 +214,7 @@ void GPU_materials_free(void);
bool GPU_lamp_override_visible(GPULamp *lamp, struct SceneRenderLayer *srl, struct Material *ma);
void GPU_material_bind(GPUMaterial *material, int oblay, int viewlay, double time, int mipmap, float viewmat[4][4], float viewinv[4][4], float cameraborder[4], bool scenelock);
void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[4][4], float obcol[4], float autobumpscale);
void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[4][4], float obcol[4], float autobumpscale, GPUParticleInfo *pi);
void GPU_material_unbind(GPUMaterial *material);
bool GPU_material_bound(GPUMaterial *material);
struct Scene *GPU_material_scene(GPUMaterial *material);
@ -308,6 +314,14 @@ void GPU_mist_update_values(int type, float start, float dist, float inten, floa
void GPU_horizon_update_color(float color[3]);
void GPU_ambient_update_color(float color[3]);
typedef struct GPUParticleInfo
{
float scalprops[4];
float location[3];
float velocity[3];
float angular_velocity[3];
} GPUParticleInfo;
#ifdef __cplusplus
}
#endif

View File

@ -387,6 +387,14 @@ const char *GPU_builtin_name(GPUBuiltin builtin)
return "unfobautobumpscale";
else if (builtin == GPU_CAMERA_TEXCO_FACTORS)
return "unfcameratexfactors";
else if (builtin == GPU_PARTICLE_SCALAR_PROPS)
return "unfparticlescalarprops";
else if (builtin == GPU_PARTICLE_LOCATION)
return "unfparticleco";
else if (builtin == GPU_PARTICLE_VELOCITY)
return "unfparticlevel";
else if (builtin == GPU_PARTICLE_ANG_VELOCITY)
return "unfparticleangvel";
else
return "";
}

View File

@ -56,6 +56,7 @@
#include "DNA_scene_types.h"
#include "DNA_smoke_types.h"
#include "DNA_view3d_types.h"
#include "DNA_particle_types.h"
#include "MEM_guardedalloc.h"
@ -1411,6 +1412,7 @@ static struct GPUMaterialState {
Material *gmatbuf_fixed[FIXEDMAT];
Material *gboundmat;
Object *gob;
DupliObject *dob;
Scene *gscene;
int glay;
bool gscenelock;
@ -1493,11 +1495,22 @@ static Material *gpu_active_node_material(Material *ma)
return ma;
}
void GPU_begin_dupli_object(DupliObject *dob)
{
GMS.dob = dob;
}
void GPU_end_dupli_object(void)
{
GMS.dob = NULL;
}
void GPU_begin_object_materials(View3D *v3d, RegionView3D *rv3d, Scene *scene, Object *ob, bool glsl, bool *do_alpha_after)
{
Material *ma;
GPUMaterial *gpumat;
GPUBlendMode alphablend;
DupliObject *dob;
int a;
const bool gamma = BKE_scene_check_color_management_enabled(scene);
const bool new_shading_nodes = BKE_scene_use_new_shading_nodes(scene);
@ -1510,7 +1523,10 @@ void GPU_begin_object_materials(View3D *v3d, RegionView3D *rv3d, Scene *scene, O
#endif
/* initialize state */
/* DupliObject must be restored */
dob = GMS.dob;
memset(&GMS, 0, sizeof(GMS));
GMS.dob = dob;
GMS.lastmatnr = -1;
GMS.lastretval = -1;
GMS.lastalphablend = GPU_BLEND_SOLID;
@ -1618,6 +1634,36 @@ void GPU_begin_object_materials(View3D *v3d, RegionView3D *rv3d, Scene *scene, O
GPU_disable_material();
}
int GPU_get_particle_info(GPUParticleInfo *pi)
{
ParticleData *p;
DupliObject *dob = GMS.dob;
int ind;
if(dob->particle_system) {
if(dob->persistent_id[0] < dob->particle_system->totpart)
ind = dob->persistent_id[0];
else {
ind = dob->particle_system->child[dob->persistent_id[0] - dob->particle_system->totpart].parent;
}
if (ind >= 0) {
p = &dob->particle_system->particles[ind];
pi->scalprops[0] = ind;
pi->scalprops[1] = GMS.gscene->r.cfra - p->time;
pi->scalprops[2] = p->lifetime;
pi->scalprops[3] = p->size;
copy_v3_v3(pi->location, p->state.co);
copy_v3_v3(pi->velocity, p->state.vel);
copy_v3_v3(pi->angular_velocity, p->state.ave);
return 1;
}
else return 0;
}
else
return 0;
}
int GPU_enable_material(int nr, void *attribs)
{
GPUVertexAttribs *gattribs = attribs;
@ -1680,14 +1726,20 @@ int GPU_enable_material(int nr, void *attribs)
if (gattribs && GMS.gmatbuf[nr]) {
/* bind glsl material and get attributes */
Material *mat = GMS.gmatbuf[nr];
GPUParticleInfo partile_info;
float auto_bump_scale;
gpumat = GPU_material_from_blender(GMS.gscene, mat);
GPU_material_vertex_attributes(gpumat, gattribs);
if (GMS.dob)
GPU_get_particle_info(&partile_info);
GPU_material_bind(gpumat, GMS.gob->lay, GMS.glay, 1.0, !(GMS.gob->mode & OB_MODE_TEXTURE_PAINT), GMS.gviewmat, GMS.gviewinv, GMS.gviewcamtexcofac, GMS.gscenelock);
auto_bump_scale = GMS.gob->derivedFinal != NULL ? GMS.gob->derivedFinal->auto_bump_scale : 1.0f;
GPU_material_bind_uniforms(gpumat, GMS.gob->obmat, GMS.gob->col, auto_bump_scale);
GPU_material_bind_uniforms(gpumat, GMS.gob->obmat, GMS.gob->col, auto_bump_scale, &partile_info);
GMS.gboundmat = mat;
/* for glsl use alpha blend mode, unless it's set to solid and

View File

@ -112,6 +112,11 @@ struct GPUMaterial {
int obcolloc, obautobumpscaleloc;
int cameratexcofacloc;
int partscalarpropsloc;
int partcoloc;
int partvel;
int partangvel;
ListBase lamps;
bool bound;
};
@ -241,6 +246,14 @@ static int GPU_material_construct_end(GPUMaterial *material, const char *passnam
material->obautobumpscaleloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_AUTO_BUMPSCALE));
if (material->builtins & GPU_CAMERA_TEXCO_FACTORS)
material->cameratexcofacloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_CAMERA_TEXCO_FACTORS));
if (material->builtins & GPU_PARTICLE_SCALAR_PROPS)
material->partscalarpropsloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_PARTICLE_SCALAR_PROPS));
if (material->builtins & GPU_PARTICLE_LOCATION)
material->partcoloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_PARTICLE_LOCATION));
if (material->builtins & GPU_PARTICLE_VELOCITY)
material->partvel = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_PARTICLE_VELOCITY));
if (material->builtins & GPU_PARTICLE_ANG_VELOCITY)
material->partangvel = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_PARTICLE_ANG_VELOCITY));
return 1;
}
@ -367,7 +380,7 @@ void GPU_material_bind(GPUMaterial *material, int oblay, int viewlay, double tim
}
}
void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[4][4], float obcol[4], float autobumpscale)
void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[4][4], float obcol[4], float autobumpscale, GPUParticleInfo* pi)
{
if (material->pass) {
GPUShader *shader = GPU_pass_shader(material->pass);
@ -389,6 +402,19 @@ void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[4][4], float
if (material->builtins & GPU_AUTO_BUMPSCALE) {
GPU_shader_uniform_vector(shader, material->obautobumpscaleloc, 1, 1, &autobumpscale);
}
if (material->builtins & GPU_PARTICLE_SCALAR_PROPS) {
GPU_shader_uniform_vector(shader, material->partscalarpropsloc, 4, 1, pi->scalprops);
}
if (material->builtins & GPU_PARTICLE_LOCATION) {
GPU_shader_uniform_vector(shader, material->partcoloc, 3, 1, pi->location);
}
if (material->builtins & GPU_PARTICLE_VELOCITY) {
GPU_shader_uniform_vector(shader, material->partvel, 3, 1, pi->velocity);
}
if (material->builtins & GPU_PARTICLE_ANG_VELOCITY) {
GPU_shader_uniform_vector(shader, material->partangvel, 3, 1, pi->angular_velocity);
}
}
}

View File

@ -153,6 +153,18 @@ void geom(vec3 co, vec3 nor, mat4 viewinvmat, vec3 attorco, vec2 attuv, vec4 att
frontback = (gl_FrontFacing)? 1.0: 0.0;
}
void particle_info(vec4 sprops, vec3 loc, vec3 vel, vec3 avel, out float index, out float age, out float life_time, out vec3 location, out float size, out vec3 velocity, out vec3 angular_velocity)
{
index = sprops.x;
age = sprops.y;
life_time = sprops.z;
size = sprops.w;
location = loc;
velocity = vel;
angular_velocity = avel;
}
void mapping(vec3 vec, mat4 mat, vec3 minvec, vec3 maxvec, float domin, float domax, out vec3 outvec)
{
outvec = (mat * vec4(vec, 1.0)).xyz;

View File

@ -26,6 +26,7 @@
*/
#include "../node_shader_util.h"
#include "RE_shader_ext.h"
static bNodeSocketTemplate outputs[] = {
{ SOCK_FLOAT, 0, "Index" },
@ -40,6 +41,22 @@ static bNodeSocketTemplate outputs[] = {
{ SOCK_VECTOR, 0, "Angular Velocity" },
{ -1, 0, "" }
};
static void node_shader_exec_particle_info(void *data, int UNUSED(thread), bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), bNodeStack **UNUSED(in), bNodeStack **out)
{
ShadeInput *shi = ((ShaderCallData *)data)->shi;
RE_instance_get_particle_info(shi->obi, out[0]->vec, out[1]->vec, out[2]->vec, out[3]->vec, out[4]->vec, out[5]->vec, out[6]->vec);
}
static int gpu_shader_particle_info(GPUMaterial *mat, bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
{
return GPU_stack_link(mat, "particle_info", in, out,
GPU_builtin(GPU_PARTICLE_SCALAR_PROPS),
GPU_builtin(GPU_PARTICLE_LOCATION),
GPU_builtin(GPU_PARTICLE_VELOCITY),
GPU_builtin(GPU_PARTICLE_ANG_VELOCITY));
}
/* node type definition */
void register_node_type_sh_particle_info(void)
@ -47,8 +64,10 @@ void register_node_type_sh_particle_info(void)
static bNodeType ntype;
sh_node_type_base(&ntype, SH_NODE_PARTICLE_INFO, "Particle Info", NODE_CLASS_INPUT, 0);
node_type_compatibility(&ntype, NODE_NEW_SHADING);
node_type_compatibility(&ntype, NODE_NEW_SHADING | NODE_OLD_SHADING);
node_type_socket_templates(&ntype, NULL, outputs);
node_type_exec(&ntype, NULL, NULL, node_shader_exec_particle_info);
node_type_gpu(&ntype, gpu_shader_particle_info);
nodeRegisterType(&ntype);
}

View File

@ -178,7 +178,7 @@ typedef struct ShadeInput {
int layflag, passflag, combinedflag;
struct Group *light_override;
struct Material *mat_override;
#ifdef RE_RAYCOUNTER
RayCounter raycounter;
#endif
@ -206,6 +206,7 @@ int multitex_nodes(struct Tex *tex, float texvec[3], float dxt[3], float dyt[3],
const short thread, short which_output, struct ShadeInput *shi, struct MTex *mtex,
struct ImagePool *pool);
float RE_lamp_get_data(struct ShadeInput *shi, struct Object *lamp_obj, float col[4], float lv[3], float *dist, float shadow[4]);
void RE_instance_get_particle_info(struct ObjectInstanceRen *obi, float *index, float *age, float *lifetime, float co[3], float *size, float vel[3], float angvel[3]);
/* shaded view and bake */
struct Render;

View File

@ -367,6 +367,14 @@ typedef struct ObjectInstanceRen {
struct RayObject *raytree;
int transform_primitives;
/* Particle info */
float part_index;
float part_age;
float part_lifetime;
float part_size;
float part_co[3];
float part_vel[3];
float part_avel[3];
} ObjectInstanceRen;
/* ------------------------------------------------------------------------- */

View File

@ -69,7 +69,8 @@
#include "DNA_material_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_texture_types.h"
#include "DNA_texture_types.h"
#include "DNA_particle_types.h"
#include "BKE_customdata.h"
#include "BKE_DerivedMesh.h"
@ -1375,6 +1376,38 @@ ObjectInstanceRen *RE_addRenderInstance(Render *re, ObjectRen *obr, Object *ob,
obi->psysindex= psysindex;
obi->lay= lay;
/* Fill particle info */
if (obi->psysindex >= 0) {
int psysindex = 0;
int index;
ParticleSystem *psys;
if (obi->par) {
for (psys = obi->par->particlesystem.first; psys; psys = psys->next) {
if (psysindex == obi->psysindex)
break;
++psysindex;
}
if (psys) {
if (obi->index < psys->totpart)
index = obi->index;
else {
index = psys->child[obi->index - psys->totpart].parent;
}
if (index >= 0) {
ParticleData* p = &psys->particles[index];
obi->part_index = index;
obi->part_size = p->size;
obi->part_age = RE_GetStats(re)->cfra - p->time;
obi->part_lifetime = p->lifetime;
copy_v3_v3(obi->part_co, p->state.co);
copy_v3_v3(obi->part_vel, p->state.vel);
copy_v3_v3(obi->part_avel, p->state.ave);
}
}
}
}
if (mat) {
copy_m4_m4(obi->mat, mat);
copy_m3_m4(mat3, mat);
@ -1388,6 +1421,18 @@ ObjectInstanceRen *RE_addRenderInstance(Render *re, ObjectRen *obr, Object *ob,
return obi;
}
void RE_instance_get_particle_info(struct ObjectInstanceRen *obi, float *index, float *age, float *lifetime, float co[3], float *size, float vel[3], float angvel[3])
{
*index = obi->part_index;
*age = obi->part_age;
*lifetime = obi->part_lifetime;
copy_v3_v3(co, obi->part_co);
*size = obi->part_size;
copy_v3_v3(vel, obi->part_vel);
copy_v3_v3(angvel, obi->part_avel);
}
void RE_makeRenderInstances(Render *re)
{
ObjectInstanceRen *obi, *oldobi;

View File

@ -39,6 +39,7 @@
#include "DNA_lamp_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_material_types.h"
#include "DNA_particle_types.h"
#include "BKE_scene.h"
@ -155,9 +156,10 @@ void shade_input_do_shade(ShadeInput *shi, ShadeResult *shr)
memset(&shi->raycounter, 0, sizeof(shi->raycounter));
#endif
if (shi->mat->nodetree && shi->mat->use_nodes)
if (shi->mat->nodetree && shi->mat->use_nodes) {
compat = ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
}
/* also run this when node shaders fail, due to incompatible shader nodes */
if (compat == false) {
/* copy all relevant material vars, note, keep this synced with render_types.h */

View File

@ -626,6 +626,7 @@ void RE_engine_update_memory_stats(struct RenderEngine *engine, float mem_used,
struct RenderEngine *RE_engine_create(struct RenderEngineType *type) RET_NULL
void RE_engine_frame_set(struct RenderEngine *engine, int frame, float subframe) RET_NONE
void RE_FreePersistentData(void) RET_NONE
void RE_instance_get_particle_info(struct ObjectInstanceRen *obi, float *index, float *age, float *lifetime, float co[3], float *size, float vel[3], float angvel[3]) RET_NONE
/* python */
struct wmOperatorType *WM_operatortype_find(const char *idname, bool quiet) RET_NULL

View File

@ -168,7 +168,7 @@ void BL_BlenderShader::Update(const RAS_MeshSlot & ms, RAS_IRasterizer* rasty )
obcol[0] = obcol[1] = obcol[2] = obcol[3] = 1.0f;
float auto_bump_scale = ms.m_pDerivedMesh!=0 ? ms.m_pDerivedMesh->auto_bump_scale : 1.0f;
GPU_material_bind_uniforms(gpumat, obmat, obcol, auto_bump_scale);
GPU_material_bind_uniforms(gpumat, obmat, obcol, auto_bump_scale, NULL);
mAlphaBlend = GPU_material_alpha_blend(gpumat, obcol);
}