758218fe32977bf268f1f5f8cd5ae7f965be9247
[blender.git] / source / blender / draw / modes / particle_mode.c
1 /*
2  * Copyright 2016, Blender Foundation.
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  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file blender/draw/modes/particle_mode.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_engine.h"
27 #include "DRW_render.h"
28
29 #include "DNA_mesh_types.h"
30 #include "DNA_object_types.h"
31 #include "DNA_particle_types.h"
32
33 #include "BKE_particle.h"
34 #include "BKE_pointcache.h"
35
36 #include "GPU_shader.h"
37 #include "GPU_batch.h"
38
39 #include "draw_common.h"
40
41 #include "draw_mode_engines.h"
42
43 #include "ED_particle.h"
44
45 #include "DEG_depsgraph_query.h"
46
47 #include "draw_cache_impl.h"
48
49 extern char datatoc_particle_strand_vert_glsl[];
50 extern char datatoc_particle_strand_frag_glsl[];
51 extern char datatoc_common_globals_lib_glsl[];
52
53 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
54
55 /* *********** LISTS *********** */
56
57 typedef struct PARTICLE_PassList {
58         struct DRWPass *psys_edit_pass;
59 } PARTICLE_PassList;
60
61 typedef struct PARTICLE_FramebufferList {
62         struct GPUFrameBuffer *fb;
63 } PARTICLE_FramebufferList;
64
65 typedef struct PARTICLE_TextureList {
66         struct GPUTexture *texture;
67 } PARTICLE_TextureList;
68
69 typedef struct PARTICLE_StorageList {
70         struct CustomStruct *block;
71         struct PARTICLE_PrivateData *g_data;
72 } PARTICLE_StorageList;
73
74 typedef struct PARTICLE_Data {
75         void *engine_type; /* Required */
76         PARTICLE_FramebufferList *fbl;
77         PARTICLE_TextureList *txl;
78         PARTICLE_PassList *psl;
79         PARTICLE_StorageList *stl;
80 } PARTICLE_Data;
81
82 /* *********** STATIC *********** */
83
84 static struct {
85         struct GPUShader *strands_shader;
86         struct GPUShader *strands_weight_shader;
87         struct GPUShader *points_shader;
88 } e_data = {NULL}; /* Engine data */
89
90 typedef struct PARTICLE_PrivateData {
91         DRWShadingGroup *strands_group;
92         DRWShadingGroup *inner_points_group;
93         DRWShadingGroup *tip_points_group;
94 } PARTICLE_PrivateData; /* Transient data */
95
96 /* *********** FUNCTIONS *********** */
97
98 static void particle_engine_init(void *UNUSED(vedata))
99 {
100         if (!e_data.strands_shader) {
101                 e_data.strands_shader = DRW_shader_create_with_lib(
102                         datatoc_particle_strand_vert_glsl,
103                         NULL,
104                         datatoc_particle_strand_frag_glsl,
105                         datatoc_common_globals_lib_glsl,
106                         "");
107
108                 e_data.strands_weight_shader = DRW_shader_create_with_lib(
109                         datatoc_particle_strand_vert_glsl,
110                         NULL,
111                         datatoc_particle_strand_frag_glsl,
112                         datatoc_common_globals_lib_glsl,
113                         "#define USE_WEIGHT");
114
115                 e_data.points_shader = DRW_shader_create_with_lib(
116                         datatoc_particle_strand_vert_glsl,
117                         NULL,
118                         datatoc_particle_strand_frag_glsl,
119                         datatoc_common_globals_lib_glsl,
120                         "#define USE_POINTS");
121         }
122 }
123
124 static void particle_cache_init(void *vedata)
125 {
126         PARTICLE_PassList *psl = ((PARTICLE_Data *)vedata)->psl;
127         PARTICLE_StorageList *stl = ((PARTICLE_Data *)vedata)->stl;
128         const DRWContextState *draw_ctx = DRW_context_state_get();
129         ParticleEditSettings *pset = PE_settings(draw_ctx->scene);
130         const bool use_weight = (pset->brushtype == PE_BRUSH_WEIGHT);
131
132         if (!stl->g_data) {
133                 /* Alloc transient pointers */
134                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
135         }
136
137         /* Create a pass */
138         psl->psys_edit_pass = DRW_pass_create("PSys Edit Pass",
139                                               (DRW_STATE_WRITE_COLOR |
140                                                DRW_STATE_WRITE_DEPTH |
141                                                DRW_STATE_DEPTH_LESS_EQUAL |
142                                                DRW_STATE_WIRE |
143                                                DRW_STATE_POINT));
144
145         GPUShader *strand_shader = (use_weight) ? e_data.strands_weight_shader : e_data.strands_shader;
146         stl->g_data->strands_group = DRW_shgroup_create(strand_shader, psl->psys_edit_pass);
147         stl->g_data->inner_points_group = DRW_shgroup_create(e_data.points_shader, psl->psys_edit_pass);
148         stl->g_data->tip_points_group = DRW_shgroup_create(e_data.points_shader, psl->psys_edit_pass);
149
150         DRW_shgroup_uniform_block(stl->g_data->strands_group, "globalsBlock", globals_ubo);
151         DRW_shgroup_uniform_block(stl->g_data->inner_points_group, "globalsBlock", globals_ubo);
152         DRW_shgroup_uniform_block(stl->g_data->tip_points_group, "globalsBlock", globals_ubo);
153 }
154
155 static void particle_edit_cache_populate(void *vedata,
156                                          Object *object,
157                                          ParticleSystem *psys,
158                                          PTCacheEdit *edit)
159 {
160         PARTICLE_StorageList *stl = ((PARTICLE_Data *)vedata)->stl;
161         const DRWContextState *draw_ctx = DRW_context_state_get();
162         ParticleEditSettings *pset = PE_settings(draw_ctx->scene);
163         const bool use_weight = (pset->brushtype == PE_BRUSH_WEIGHT);
164         {
165                 struct GPUBatch *strands =
166                         DRW_cache_particles_get_edit_strands(object, psys, edit, use_weight);
167                 DRW_shgroup_call_add(stl->g_data->strands_group, strands, NULL);
168         }
169         if (pset->selectmode == SCE_SELECT_POINT) {
170                 struct GPUBatch *points =
171                         DRW_cache_particles_get_edit_inner_points(object, psys, edit);
172                 DRW_shgroup_call_add(stl->g_data->inner_points_group, points, NULL);
173         }
174         if (ELEM(pset->selectmode, SCE_SELECT_POINT, SCE_SELECT_END)) {
175                 struct GPUBatch *points =
176                         DRW_cache_particles_get_edit_tip_points(object, psys, edit);
177                 DRW_shgroup_call_add(stl->g_data->tip_points_group, points, NULL);
178         }
179 }
180
181 static void particle_cache_populate(void *vedata, Object *object)
182 {
183         if (object->mode != OB_MODE_PARTICLE_EDIT) {
184                 return;
185         }
186         const DRWContextState *draw_ctx = DRW_context_state_get();
187         Scene *scene_orig = (Scene *)DEG_get_original_id(&draw_ctx->scene->id);
188         /* Usually the edit structure is created by Particle Edit Mode Toggle
189          * operator, but sometimes it's invoked after tagging hair as outdated
190          * (for example, when toggling edit mode). That makes it impossible to
191          * create edit structure for until after next dependency graph evaluation.
192          *
193          * Ideally, the edit structure will be created here already via some
194          * dependency graph callback or so, but currently trying to make it nicer
195          * only causes bad level calls and breaks design from the past.
196          */
197         Object *object_orig = DEG_get_original_object(object);
198         PTCacheEdit *edit = PE_create_current(
199                 draw_ctx->depsgraph, scene_orig, object_orig);
200         if (edit == NULL) {
201                 /* Happens when trying to edit particles in EMITTER mode without
202                  * having them cached.
203                  */
204                 return;
205         }
206         /* NOTE: We need to pass evaluated particle system, which we need
207          * to find first.
208          */
209         ParticleSystem *psys = object->particlesystem.first;
210         ParticleSystem *psys_orig = object_orig->particlesystem.first;
211         while (psys_orig != NULL) {
212                 if (PE_get_current_from_psys(psys_orig) == edit) {
213                         break;
214                 }
215                 psys = psys->next;
216                 psys_orig = psys_orig->next;
217         }
218         if (psys == NULL) {
219                 printf("Error getting evaluated particle system for edit.\n");
220                 return;
221         }
222         particle_edit_cache_populate(vedata, object, psys, edit);
223 }
224
225 /* Optional: Post-cache_populate callback */
226 static void particle_cache_finish(void *UNUSED(vedata))
227 {
228 }
229
230 /* Draw time ! Control rendering pipeline from here */
231 static void particle_draw_scene(void *vedata)
232 {
233
234         PARTICLE_PassList *psl = ((PARTICLE_Data *)vedata)->psl;
235
236         DRW_draw_pass(psl->psys_edit_pass);
237 }
238
239 static void particle_engine_free(void)
240 {
241         DRW_SHADER_FREE_SAFE(e_data.strands_shader);
242         DRW_SHADER_FREE_SAFE(e_data.strands_weight_shader);
243         DRW_SHADER_FREE_SAFE(e_data.points_shader);
244 }
245
246 static const DrawEngineDataSize particle_data_size =
247       DRW_VIEWPORT_DATA_SIZE(PARTICLE_Data);
248
249 DrawEngineType draw_engine_particle_type = {
250         NULL, NULL,
251         N_("Particle Mode"),
252         &particle_data_size,
253         &particle_engine_init,
254         &particle_engine_free,
255         &particle_cache_init,
256         &particle_cache_populate,
257         &particle_cache_finish,
258         NULL, /* draw_background but not needed by mode engines */
259         &particle_draw_scene,
260         NULL,
261         NULL,
262 };