2018-05-29 12:11:03 +02:00
|
|
|
/*
|
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2017 by Blender Foundation.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): Blender Foundation, Mike Erwin, Dalai Felinto
|
|
|
|
|
*
|
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** \file draw_hair.c
|
|
|
|
|
* \ingroup draw
|
|
|
|
|
*
|
|
|
|
|
* \brief Contains procedural GPU hair drawing methods.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "DRW_render.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
#include "BLI_string_utils.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
|
#include "DNA_modifier_types.h"
|
|
|
|
|
#include "DNA_particle_types.h"
|
|
|
|
|
#include "DNA_customdata_types.h"
|
|
|
|
|
|
2018-11-26 15:34:10 +01:00
|
|
|
#include "BKE_anim.h"
|
2018-05-29 12:11:03 +02:00
|
|
|
#include "BKE_mesh.h"
|
|
|
|
|
#include "BKE_particle.h"
|
|
|
|
|
#include "BKE_pointcache.h"
|
|
|
|
|
|
|
|
|
|
#include "ED_particle.h"
|
|
|
|
|
|
|
|
|
|
#include "GPU_batch.h"
|
|
|
|
|
#include "GPU_shader.h"
|
|
|
|
|
|
|
|
|
|
#include "draw_hair_private.h"
|
|
|
|
|
|
|
|
|
|
typedef enum ParticleRefineShader {
|
|
|
|
|
PART_REFINE_CATMULL_ROM = 0,
|
|
|
|
|
PART_REFINE_MAX_SHADER,
|
|
|
|
|
} ParticleRefineShader;
|
|
|
|
|
|
|
|
|
|
static GPUShader *g_refine_shaders[PART_REFINE_MAX_SHADER] = {NULL};
|
2018-09-19 18:19:49 +02:00
|
|
|
static DRWPass *g_tf_pass; /* XXX can be a problem with multiple DRWManager in the future */
|
2018-05-29 12:11:03 +02:00
|
|
|
|
|
|
|
|
extern char datatoc_common_hair_lib_glsl[];
|
|
|
|
|
extern char datatoc_common_hair_refine_vert_glsl[];
|
|
|
|
|
|
|
|
|
|
static GPUShader *hair_refine_shader_get(ParticleRefineShader sh)
|
|
|
|
|
{
|
|
|
|
|
if (g_refine_shaders[sh]) {
|
|
|
|
|
return g_refine_shaders[sh];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *vert_with_lib = BLI_string_joinN(datatoc_common_hair_lib_glsl, datatoc_common_hair_refine_vert_glsl);
|
|
|
|
|
|
|
|
|
|
const char *var_names[1] = {"outData"};
|
|
|
|
|
|
|
|
|
|
g_refine_shaders[sh] = DRW_shader_create_with_transform_feedback(vert_with_lib, NULL, "#define HAIR_PHASE_SUBDIV\n",
|
|
|
|
|
GPU_SHADER_TFB_POINTS, var_names, 1);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(vert_with_lib);
|
|
|
|
|
|
|
|
|
|
return g_refine_shaders[sh];
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-02 20:45:03 +02:00
|
|
|
void DRW_hair_init(void)
|
|
|
|
|
{
|
|
|
|
|
g_tf_pass = DRW_pass_create("Update Hair Pass", DRW_STATE_TRANS_FEEDBACK);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 15:34:10 +01:00
|
|
|
typedef struct DRWHairInstanceData{
|
|
|
|
|
DrawData dd;
|
|
|
|
|
|
|
|
|
|
float mat[4][4];
|
|
|
|
|
} DRWHairInstanceData;
|
|
|
|
|
|
2018-05-29 12:11:03 +02:00
|
|
|
static DRWShadingGroup *drw_shgroup_create_hair_procedural_ex(
|
|
|
|
|
Object *object, ParticleSystem *psys, ModifierData *md,
|
2018-06-02 20:45:03 +02:00
|
|
|
DRWPass *hair_pass,
|
2018-05-29 12:11:03 +02:00
|
|
|
struct GPUMaterial *gpu_mat, GPUShader *gpu_shader)
|
|
|
|
|
{
|
|
|
|
|
/* TODO(fclem): Pass the scene as parameter */
|
|
|
|
|
const DRWContextState *draw_ctx = DRW_context_state_get();
|
|
|
|
|
Scene *scene = draw_ctx->scene;
|
2018-11-26 15:34:10 +01:00
|
|
|
static float unit_mat[4][4] = {
|
|
|
|
|
{1, 0, 0, 0},
|
|
|
|
|
{0, 1, 0, 0},
|
|
|
|
|
{0, 0, 1, 0},
|
|
|
|
|
{0, 0, 0, 1},
|
|
|
|
|
};
|
|
|
|
|
float (*dupli_mat)[4];
|
|
|
|
|
Object *dupli_parent = DRW_object_get_dupli_parent(object);
|
|
|
|
|
DupliObject *dupli_object = DRW_object_get_dupli(object);
|
2018-05-29 12:11:03 +02:00
|
|
|
|
|
|
|
|
int subdiv = scene->r.hair_subdiv;
|
|
|
|
|
int thickness_res = (scene->r.hair_type == SCE_HAIR_SHAPE_STRAND) ? 1 : 2;
|
|
|
|
|
|
|
|
|
|
ParticleHairCache *hair_cache;
|
|
|
|
|
ParticleSettings *part = psys->part;
|
|
|
|
|
bool need_ft_update = particles_ensure_procedural_data(object, psys, md, &hair_cache, subdiv, thickness_res);
|
|
|
|
|
|
|
|
|
|
DRWShadingGroup *shgrp;
|
|
|
|
|
if (gpu_mat) {
|
|
|
|
|
shgrp = DRW_shgroup_material_create(gpu_mat, hair_pass);
|
|
|
|
|
}
|
|
|
|
|
else if (gpu_shader) {
|
|
|
|
|
shgrp = DRW_shgroup_create(gpu_shader, hair_pass);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2018-06-05 12:37:50 +02:00
|
|
|
shgrp = NULL;
|
2018-05-29 12:11:03 +02:00
|
|
|
BLI_assert(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TODO optimize this. Only bind the ones GPUMaterial needs. */
|
|
|
|
|
for (int i = 0; i < hair_cache->num_uv_layers; ++i) {
|
2018-06-28 10:35:48 +02:00
|
|
|
for (int n = 0; n < MAX_LAYER_NAME_CT && hair_cache->uv_layer_names[i][n][0] != '\0'; ++n) {
|
2018-05-29 12:11:03 +02:00
|
|
|
DRW_shgroup_uniform_texture(shgrp, hair_cache->uv_layer_names[i][n], hair_cache->uv_tex[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < hair_cache->num_col_layers; ++i) {
|
2018-06-28 10:35:48 +02:00
|
|
|
for (int n = 0; n < MAX_LAYER_NAME_CT && hair_cache->col_layer_names[i][n][0] != '\0'; ++n) {
|
2018-05-29 12:11:03 +02:00
|
|
|
DRW_shgroup_uniform_texture(shgrp, hair_cache->col_layer_names[i][n], hair_cache->col_tex[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 15:34:10 +01:00
|
|
|
if (dupli_parent) {
|
|
|
|
|
DRWHairInstanceData *hair_inst_data = (DRWHairInstanceData *)DRW_drawdata_ensure(
|
|
|
|
|
&object->id, (DrawEngineType *)&drw_shgroup_create_hair_procedural_ex,
|
|
|
|
|
sizeof(DRWHairInstanceData), NULL, NULL);
|
|
|
|
|
dupli_mat = hair_inst_data->mat;
|
|
|
|
|
if (dupli_object->type & OB_DUPLICOLLECTION) {
|
|
|
|
|
copy_m4_m4(dupli_mat, dupli_parent->obmat);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
copy_m4_m4(dupli_mat, dupli_object->ob->obmat);
|
|
|
|
|
invert_m4(dupli_mat);
|
|
|
|
|
mul_m4_m4m4(dupli_mat, object->obmat, dupli_mat);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
dupli_mat = unit_mat;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 12:11:03 +02:00
|
|
|
DRW_shgroup_uniform_texture(shgrp, "hairPointBuffer", hair_cache->final[subdiv].proc_tex);
|
|
|
|
|
DRW_shgroup_uniform_int(shgrp, "hairStrandsRes", &hair_cache->final[subdiv].strands_res, 1);
|
|
|
|
|
DRW_shgroup_uniform_int_copy(shgrp, "hairThicknessRes", thickness_res);
|
|
|
|
|
DRW_shgroup_uniform_float(shgrp, "hairRadShape", &part->shape, 1);
|
2018-11-26 15:34:10 +01:00
|
|
|
DRW_shgroup_uniform_mat4(shgrp, "hairDupliMatrix", dupli_mat);
|
2018-05-29 12:11:03 +02:00
|
|
|
DRW_shgroup_uniform_float_copy(shgrp, "hairRadRoot", part->rad_root * part->rad_scale * 0.5f);
|
|
|
|
|
DRW_shgroup_uniform_float_copy(shgrp, "hairRadTip", part->rad_tip * part->rad_scale * 0.5f);
|
|
|
|
|
DRW_shgroup_uniform_bool_copy(shgrp, "hairCloseTip", (part->shape_flag & PART_SHAPE_CLOSE_TIP) != 0);
|
|
|
|
|
/* TODO(fclem): Until we have a better way to cull the hair and render with orco, bypass culling test. */
|
2018-05-30 19:45:03 +02:00
|
|
|
DRW_shgroup_call_object_add_no_cull(shgrp, hair_cache->final[subdiv].proc_hairs[thickness_res - 1], object);
|
2018-05-29 12:11:03 +02:00
|
|
|
|
|
|
|
|
/* Transform Feedback subdiv. */
|
|
|
|
|
if (need_ft_update) {
|
2018-07-08 12:45:53 +02:00
|
|
|
int final_points_len = hair_cache->final[subdiv].strands_res * hair_cache->strands_len;
|
2018-05-29 12:11:03 +02:00
|
|
|
GPUShader *tf_shader = hair_refine_shader_get(PART_REFINE_CATMULL_ROM);
|
2018-06-02 20:45:03 +02:00
|
|
|
DRWShadingGroup *tf_shgrp = DRW_shgroup_transform_feedback_create(tf_shader, g_tf_pass,
|
2018-05-29 12:11:03 +02:00
|
|
|
hair_cache->final[subdiv].proc_buf);
|
|
|
|
|
DRW_shgroup_uniform_texture(tf_shgrp, "hairPointBuffer", hair_cache->point_tex);
|
|
|
|
|
DRW_shgroup_uniform_texture(tf_shgrp, "hairStrandBuffer", hair_cache->strand_tex);
|
|
|
|
|
DRW_shgroup_uniform_int(tf_shgrp, "hairStrandsRes", &hair_cache->final[subdiv].strands_res, 1);
|
2018-07-08 12:45:53 +02:00
|
|
|
DRW_shgroup_call_procedural_points_add(tf_shgrp, final_points_len, NULL);
|
2018-05-29 12:11:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return shgrp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DRWShadingGroup *DRW_shgroup_hair_create(
|
|
|
|
|
Object *object, ParticleSystem *psys, ModifierData *md,
|
2018-06-02 20:45:03 +02:00
|
|
|
DRWPass *hair_pass,
|
2018-05-29 12:11:03 +02:00
|
|
|
GPUShader *shader)
|
|
|
|
|
{
|
2018-06-02 20:45:03 +02:00
|
|
|
return drw_shgroup_create_hair_procedural_ex(object, psys, md, hair_pass, NULL, shader);
|
2018-05-29 12:11:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DRWShadingGroup *DRW_shgroup_material_hair_create(
|
|
|
|
|
Object *object, ParticleSystem *psys, ModifierData *md,
|
2018-06-02 20:45:03 +02:00
|
|
|
DRWPass *hair_pass,
|
2018-05-29 12:11:03 +02:00
|
|
|
struct GPUMaterial *material)
|
|
|
|
|
{
|
2018-06-02 20:45:03 +02:00
|
|
|
return drw_shgroup_create_hair_procedural_ex(object, psys, md, hair_pass, material, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DRW_hair_update(void)
|
|
|
|
|
{
|
|
|
|
|
DRW_draw_pass(g_tf_pass);
|
2018-05-29 12:11:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DRW_hair_free(void)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < PART_REFINE_MAX_SHADER; ++i) {
|
|
|
|
|
DRW_SHADER_FREE_SAFE(g_refine_shaders[i]);
|
|
|
|
|
}
|
2018-05-30 15:26:37 +02:00
|
|
|
}
|