Merge branch 'master' into blender2.8
[blender.git] / source / blender / draw / intern / draw_hair.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2017 by Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, Mike Erwin, Dalai Felinto
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file draw_hair.c
27  *  \ingroup draw
28  *
29  *  \brief Contains procedural GPU hair drawing methods.
30  */
31
32 #include "DRW_render.h"
33
34 #include "BLI_utildefines.h"
35 #include "BLI_string_utils.h"
36
37 #include "DNA_mesh_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_modifier_types.h"
40 #include "DNA_particle_types.h"
41 #include "DNA_customdata_types.h"
42
43 #include "BKE_anim.h"
44 #include "BKE_mesh.h"
45 #include "BKE_particle.h"
46 #include "BKE_pointcache.h"
47
48 #include "ED_particle.h"
49
50 #include "GPU_batch.h"
51 #include "GPU_shader.h"
52
53 #include "draw_hair_private.h"
54
55 typedef enum ParticleRefineShader {
56         PART_REFINE_CATMULL_ROM = 0,
57         PART_REFINE_MAX_SHADER,
58 } ParticleRefineShader;
59
60 static GPUShader *g_refine_shaders[PART_REFINE_MAX_SHADER] = {NULL};
61 static DRWPass *g_tf_pass; /* XXX can be a problem with multiple DRWManager in the future */
62
63 extern char datatoc_common_hair_lib_glsl[];
64 extern char datatoc_common_hair_refine_vert_glsl[];
65
66 static GPUShader *hair_refine_shader_get(ParticleRefineShader sh)
67 {
68         if (g_refine_shaders[sh]) {
69                 return g_refine_shaders[sh];
70         }
71
72         char *vert_with_lib = BLI_string_joinN(datatoc_common_hair_lib_glsl, datatoc_common_hair_refine_vert_glsl);
73
74         const char *var_names[1] = {"outData"};
75
76         g_refine_shaders[sh] = DRW_shader_create_with_transform_feedback(vert_with_lib, NULL, "#define HAIR_PHASE_SUBDIV\n",
77                                                                          GPU_SHADER_TFB_POINTS, var_names, 1);
78
79         MEM_freeN(vert_with_lib);
80
81         return g_refine_shaders[sh];
82 }
83
84 void DRW_hair_init(void)
85 {
86         g_tf_pass = DRW_pass_create("Update Hair Pass", DRW_STATE_TRANS_FEEDBACK);
87 }
88
89 typedef struct DRWHairInstanceData {
90         DrawData dd;
91
92         float mat[4][4];
93 } DRWHairInstanceData;
94
95 static DRWShadingGroup *drw_shgroup_create_hair_procedural_ex(
96         Object *object, ParticleSystem *psys, ModifierData *md,
97         DRWPass *hair_pass,
98         struct GPUMaterial *gpu_mat, GPUShader *gpu_shader)
99 {
100         /* TODO(fclem): Pass the scene as parameter */
101         const DRWContextState *draw_ctx = DRW_context_state_get();
102         Scene *scene = draw_ctx->scene;
103         static float unit_mat[4][4] = {
104                 {1, 0, 0, 0},
105                 {0, 1, 0, 0},
106                 {0, 0, 1, 0},
107                 {0, 0, 0, 1},
108         };
109         float (*dupli_mat)[4];
110         Object *dupli_parent = DRW_object_get_dupli_parent(object);
111         DupliObject *dupli_object = DRW_object_get_dupli(object);
112
113         int subdiv = scene->r.hair_subdiv;
114         int thickness_res = (scene->r.hair_type == SCE_HAIR_SHAPE_STRAND) ? 1 : 2;
115
116         ParticleHairCache *hair_cache;
117         ParticleSettings *part = psys->part;
118         bool need_ft_update = particles_ensure_procedural_data(object, psys, md, &hair_cache, subdiv, thickness_res);
119
120         DRWShadingGroup *shgrp;
121         if (gpu_mat) {
122                 shgrp = DRW_shgroup_material_create(gpu_mat, hair_pass);
123         }
124         else if (gpu_shader) {
125                 shgrp = DRW_shgroup_create(gpu_shader, hair_pass);
126         }
127         else {
128                 shgrp = NULL;
129                 BLI_assert(0);
130         }
131
132         /* TODO optimize this. Only bind the ones GPUMaterial needs. */
133         for (int i = 0; i < hair_cache->num_uv_layers; ++i) {
134                 for (int n = 0; n < MAX_LAYER_NAME_CT && hair_cache->uv_layer_names[i][n][0] != '\0'; ++n) {
135                         DRW_shgroup_uniform_texture(shgrp, hair_cache->uv_layer_names[i][n], hair_cache->uv_tex[i]);
136                 }
137         }
138         for (int i = 0; i < hair_cache->num_col_layers; ++i) {
139                 for (int n = 0; n < MAX_LAYER_NAME_CT && hair_cache->col_layer_names[i][n][0] != '\0'; ++n) {
140                         DRW_shgroup_uniform_texture(shgrp, hair_cache->col_layer_names[i][n], hair_cache->col_tex[i]);
141                 }
142         }
143
144         if (dupli_parent) {
145                 DRWHairInstanceData *hair_inst_data = (DRWHairInstanceData *)DRW_drawdata_ensure(
146                         &object->id, (DrawEngineType *)&drw_shgroup_create_hair_procedural_ex,
147                         sizeof(DRWHairInstanceData), NULL, NULL);
148                 dupli_mat = hair_inst_data->mat;
149                 if (dupli_object->type & OB_DUPLICOLLECTION) {
150                         copy_m4_m4(dupli_mat, dupli_parent->obmat);
151                 }
152                 else {
153                         copy_m4_m4(dupli_mat, dupli_object->ob->obmat);
154                         invert_m4(dupli_mat);
155                         mul_m4_m4m4(dupli_mat, object->obmat, dupli_mat);
156                 }
157         }
158         else {
159                 dupli_mat = unit_mat;
160         }
161
162         DRW_shgroup_uniform_texture(shgrp, "hairPointBuffer", hair_cache->final[subdiv].proc_tex);
163         DRW_shgroup_uniform_int(shgrp, "hairStrandsRes", &hair_cache->final[subdiv].strands_res, 1);
164         DRW_shgroup_uniform_int_copy(shgrp, "hairThicknessRes", thickness_res);
165         DRW_shgroup_uniform_float(shgrp, "hairRadShape", &part->shape, 1);
166         DRW_shgroup_uniform_mat4(shgrp, "hairDupliMatrix", dupli_mat);
167         DRW_shgroup_uniform_float_copy(shgrp, "hairRadRoot", part->rad_root * part->rad_scale * 0.5f);
168         DRW_shgroup_uniform_float_copy(shgrp, "hairRadTip", part->rad_tip * part->rad_scale * 0.5f);
169         DRW_shgroup_uniform_bool_copy(shgrp, "hairCloseTip", (part->shape_flag & PART_SHAPE_CLOSE_TIP) != 0);
170         /* TODO(fclem): Until we have a better way to cull the hair and render with orco, bypass culling test. */
171         DRW_shgroup_call_object_add_no_cull(shgrp, hair_cache->final[subdiv].proc_hairs[thickness_res - 1], object);
172
173         /* Transform Feedback subdiv. */
174         if (need_ft_update) {
175                 int final_points_len = hair_cache->final[subdiv].strands_res * hair_cache->strands_len;
176                 GPUShader *tf_shader = hair_refine_shader_get(PART_REFINE_CATMULL_ROM);
177                 DRWShadingGroup *tf_shgrp = DRW_shgroup_transform_feedback_create(tf_shader, g_tf_pass,
178                                                                                   hair_cache->final[subdiv].proc_buf);
179                 DRW_shgroup_uniform_texture(tf_shgrp, "hairPointBuffer", hair_cache->point_tex);
180                 DRW_shgroup_uniform_texture(tf_shgrp, "hairStrandBuffer", hair_cache->strand_tex);
181                 DRW_shgroup_uniform_int(tf_shgrp, "hairStrandsRes", &hair_cache->final[subdiv].strands_res, 1);
182                 DRW_shgroup_call_procedural_points_add(tf_shgrp, final_points_len, NULL);
183         }
184
185         return shgrp;
186 }
187
188 DRWShadingGroup *DRW_shgroup_hair_create(
189         Object *object, ParticleSystem *psys, ModifierData *md,
190         DRWPass *hair_pass,
191         GPUShader *shader)
192 {
193         return drw_shgroup_create_hair_procedural_ex(object, psys, md, hair_pass, NULL, shader);
194 }
195
196 DRWShadingGroup *DRW_shgroup_material_hair_create(
197         Object *object, ParticleSystem *psys, ModifierData *md,
198         DRWPass *hair_pass,
199         struct GPUMaterial *material)
200 {
201         return drw_shgroup_create_hair_procedural_ex(object, psys, md, hair_pass, material, NULL);
202 }
203
204 void DRW_hair_update(void)
205 {
206         DRW_draw_pass(g_tf_pass);
207 }
208
209 void DRW_hair_free(void)
210 {
211         for (int i = 0; i < PART_REFINE_MAX_SHADER; ++i) {
212                 DRW_SHADER_FREE_SAFE(g_refine_shaders[i]);
213         }
214 }