2c03caf0ac3641169cec6cc137261841125a85c6
[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_mesh.h"
44 #include "BKE_particle.h"
45 #include "BKE_pointcache.h"
46
47 #include "ED_particle.h"
48
49 #include "GPU_batch.h"
50 #include "GPU_shader.h"
51
52 #include "draw_hair_private.h"
53
54 typedef enum ParticleRefineShader {
55         PART_REFINE_CATMULL_ROM = 0,
56         PART_REFINE_MAX_SHADER,
57 } ParticleRefineShader;
58
59 static GPUShader *g_refine_shaders[PART_REFINE_MAX_SHADER] = {NULL};
60 static DRWPass *g_tf_pass; /* XXX can be a problem with mulitple DRWManager in the future */
61
62 extern char datatoc_common_hair_lib_glsl[];
63 extern char datatoc_common_hair_refine_vert_glsl[];
64
65 static GPUShader *hair_refine_shader_get(ParticleRefineShader sh)
66 {
67         if (g_refine_shaders[sh]) {
68                 return g_refine_shaders[sh];
69         }
70
71         char *vert_with_lib = BLI_string_joinN(datatoc_common_hair_lib_glsl, datatoc_common_hair_refine_vert_glsl);
72
73         const char *var_names[1] = {"outData"};
74
75         g_refine_shaders[sh] = DRW_shader_create_with_transform_feedback(vert_with_lib, NULL, "#define HAIR_PHASE_SUBDIV\n",
76                                                                          GPU_SHADER_TFB_POINTS, var_names, 1);
77
78         MEM_freeN(vert_with_lib);
79
80         return g_refine_shaders[sh];
81 }
82
83 void DRW_hair_init(void)
84 {
85         g_tf_pass = DRW_pass_create("Update Hair Pass", DRW_STATE_TRANS_FEEDBACK);
86 }
87
88 static DRWShadingGroup *drw_shgroup_create_hair_procedural_ex(
89         Object *object, ParticleSystem *psys, ModifierData *md,
90         DRWPass *hair_pass,
91         struct GPUMaterial *gpu_mat, GPUShader *gpu_shader)
92 {
93         /* TODO(fclem): Pass the scene as parameter */
94         const DRWContextState *draw_ctx = DRW_context_state_get();
95         Scene *scene = draw_ctx->scene;
96
97         int subdiv = scene->r.hair_subdiv;
98         int thickness_res = (scene->r.hair_type == SCE_HAIR_SHAPE_STRAND) ? 1 : 2;
99
100         ParticleHairCache *hair_cache;
101         ParticleSettings *part = psys->part;
102         bool need_ft_update = particles_ensure_procedural_data(object, psys, md, &hair_cache, subdiv, thickness_res);
103
104         DRWShadingGroup *shgrp;
105         if (gpu_mat) {
106                 shgrp = DRW_shgroup_material_create(gpu_mat, hair_pass);
107         }
108         else if (gpu_shader) {
109                 shgrp = DRW_shgroup_create(gpu_shader, hair_pass);
110         }
111         else {
112                 BLI_assert(0);
113         }
114
115         /* TODO optimize this. Only bind the ones GPUMaterial needs. */
116         for (int i = 0; i < hair_cache->num_uv_layers; ++i) {
117                 for (int n = 0; hair_cache->uv_layer_names[i][n][0] != '\0'; ++n) {
118                         DRW_shgroup_uniform_texture(shgrp, hair_cache->uv_layer_names[i][n], hair_cache->uv_tex[i]);
119                 }
120         }
121         for (int i = 0; i < hair_cache->num_col_layers; ++i) {
122                 for (int n = 0; hair_cache->col_layer_names[i][n][0] != '\0'; ++n) {
123                         DRW_shgroup_uniform_texture(shgrp, hair_cache->col_layer_names[i][n], hair_cache->col_tex[i]);
124                 }
125         }
126
127         DRW_shgroup_uniform_texture(shgrp, "hairPointBuffer", hair_cache->final[subdiv].proc_tex);
128         DRW_shgroup_uniform_int(shgrp, "hairStrandsRes", &hair_cache->final[subdiv].strands_res, 1);
129         DRW_shgroup_uniform_int_copy(shgrp, "hairThicknessRes", thickness_res);
130         DRW_shgroup_uniform_float(shgrp, "hairRadShape", &part->shape, 1);
131         DRW_shgroup_uniform_float_copy(shgrp, "hairRadRoot", part->rad_root * part->rad_scale * 0.5f);
132         DRW_shgroup_uniform_float_copy(shgrp, "hairRadTip", part->rad_tip * part->rad_scale * 0.5f);
133         DRW_shgroup_uniform_bool_copy(shgrp, "hairCloseTip", (part->shape_flag & PART_SHAPE_CLOSE_TIP) != 0);
134         /* TODO(fclem): Until we have a better way to cull the hair and render with orco, bypass culling test. */
135         DRW_shgroup_call_object_add_no_cull(shgrp, hair_cache->final[subdiv].proc_hairs[thickness_res - 1], object);
136
137         /* Transform Feedback subdiv. */
138         if (need_ft_update) {
139                 int final_points_ct = hair_cache->final[subdiv].strands_res * hair_cache->strands_count;
140                 GPUShader *tf_shader = hair_refine_shader_get(PART_REFINE_CATMULL_ROM);
141                 DRWShadingGroup *tf_shgrp = DRW_shgroup_transform_feedback_create(tf_shader, g_tf_pass,
142                                                                                   hair_cache->final[subdiv].proc_buf);
143                 DRW_shgroup_uniform_texture(tf_shgrp, "hairPointBuffer", hair_cache->point_tex);
144                 DRW_shgroup_uniform_texture(tf_shgrp, "hairStrandBuffer", hair_cache->strand_tex);
145                 DRW_shgroup_uniform_int(tf_shgrp, "hairStrandsRes", &hair_cache->final[subdiv].strands_res, 1);
146                 DRW_shgroup_call_procedural_points_add(tf_shgrp, final_points_ct, NULL);
147         }
148
149         return shgrp;
150 }
151
152 DRWShadingGroup *DRW_shgroup_hair_create(
153         Object *object, ParticleSystem *psys, ModifierData *md,
154         DRWPass *hair_pass,
155         GPUShader *shader)
156 {
157         return drw_shgroup_create_hair_procedural_ex(object, psys, md, hair_pass, NULL, shader);
158 }
159
160 DRWShadingGroup *DRW_shgroup_material_hair_create(
161         Object *object, ParticleSystem *psys, ModifierData *md,
162         DRWPass *hair_pass,
163         struct GPUMaterial *material)
164 {
165         return drw_shgroup_create_hair_procedural_ex(object, psys, md, hair_pass, material, NULL);
166 }
167
168 void DRW_hair_update(void)
169 {
170         DRW_draw_pass(g_tf_pass);
171 }
172
173 void DRW_hair_free(void)
174 {
175         for (int i = 0; i < PART_REFINE_MAX_SHADER; ++i) {
176                 DRW_SHADER_FREE_SAFE(g_refine_shaders[i]);
177         }
178 }