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