Fix T56657: Crash when creating a collection instance containing probes
[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
52 /* *********** LISTS *********** */
53
54 typedef struct PARTICLE_PassList {
55         struct DRWPass *psys_edit_pass;
56 } PARTICLE_PassList;
57
58 typedef struct PARTICLE_FramebufferList {
59         struct GPUFrameBuffer *fb;
60 } PARTICLE_FramebufferList;
61
62 typedef struct PARTICLE_TextureList {
63         struct GPUTexture *texture;
64 } PARTICLE_TextureList;
65
66 typedef struct PARTICLE_StorageList {
67         struct CustomStruct *block;
68         struct PARTICLE_PrivateData *g_data;
69 } PARTICLE_StorageList;
70
71 typedef struct PARTICLE_Data {
72         void *engine_type; /* Required */
73         PARTICLE_FramebufferList *fbl;
74         PARTICLE_TextureList *txl;
75         PARTICLE_PassList *psl;
76         PARTICLE_StorageList *stl;
77 } PARTICLE_Data;
78
79 /* *********** STATIC *********** */
80
81 static struct {
82         struct GPUShader *strands_shader;
83         struct GPUShader *points_shader;
84 } e_data = {NULL}; /* Engine data */
85
86 typedef struct PARTICLE_PrivateData {
87         DRWShadingGroup *strands_group;
88         DRWShadingGroup *inner_points_group;
89         DRWShadingGroup *tip_points_group;
90 } PARTICLE_PrivateData; /* Transient data */
91
92 /* *********** FUNCTIONS *********** */
93
94 static void particle_engine_init(void *UNUSED(vedata))
95 {
96         if (!e_data.strands_shader) {
97                 e_data.strands_shader = DRW_shader_create(
98                         datatoc_particle_strand_vert_glsl,
99                         NULL,
100                         datatoc_particle_strand_frag_glsl,
101                         "");
102         }
103         if (!e_data.points_shader) {
104                 e_data.points_shader = GPU_shader_get_builtin_shader(
105                         GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
106         }
107 }
108
109 static void particle_cache_init(void *vedata)
110 {
111         PARTICLE_PassList *psl = ((PARTICLE_Data *)vedata)->psl;
112         PARTICLE_StorageList *stl = ((PARTICLE_Data *)vedata)->stl;
113
114         if (!stl->g_data) {
115                 /* Alloc transient pointers */
116                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
117         }
118
119         /* Create a pass */
120         psl->psys_edit_pass = DRW_pass_create("PSys Edit Pass",
121                                               (DRW_STATE_WRITE_COLOR |
122                                                DRW_STATE_WRITE_DEPTH |
123                                                DRW_STATE_DEPTH_LESS_EQUAL |
124                                                DRW_STATE_WIRE |
125                                                DRW_STATE_POINT));
126
127         stl->g_data->strands_group = DRW_shgroup_create(
128                 e_data.strands_shader, psl->psys_edit_pass);
129         stl->g_data->inner_points_group = DRW_shgroup_create(
130                 e_data.points_shader, psl->psys_edit_pass);
131         stl->g_data->tip_points_group = DRW_shgroup_create(
132                 e_data.points_shader, psl->psys_edit_pass);
133
134         static float size = 5.0f;
135         static float outline_width = 1.0f;
136         DRW_shgroup_uniform_float(stl->g_data->inner_points_group, "size", &size, 1);
137         DRW_shgroup_uniform_float(stl->g_data->inner_points_group, "outlineWidth", &outline_width, 1);
138         DRW_shgroup_uniform_float(stl->g_data->tip_points_group, "size", &size, 1);
139         DRW_shgroup_uniform_float(stl->g_data->tip_points_group, "outlineWidth", &outline_width, 1);
140 }
141
142 static void particle_edit_cache_populate(void *vedata,
143                                          Object *object,
144                                          ParticleSystem *psys,
145                                          PTCacheEdit *edit)
146 {
147         PARTICLE_StorageList *stl = ((PARTICLE_Data *)vedata)->stl;
148         const DRWContextState *draw_ctx = DRW_context_state_get();
149         ParticleEditSettings *pset = PE_settings(draw_ctx->scene);
150         {
151                 struct GPUBatch *strands =
152                         DRW_cache_particles_get_edit_strands(object, psys, edit);
153                 DRW_shgroup_call_add(stl->g_data->strands_group, strands, NULL);
154         }
155         if (pset->selectmode == SCE_SELECT_POINT) {
156                 struct GPUBatch *points =
157                         DRW_cache_particles_get_edit_inner_points(object, psys, edit);
158                 DRW_shgroup_call_add(stl->g_data->inner_points_group, points, NULL);
159         }
160         if (ELEM(pset->selectmode, SCE_SELECT_POINT, SCE_SELECT_END)) {
161                 struct GPUBatch *points =
162                         DRW_cache_particles_get_edit_tip_points(object, psys, edit);
163                 DRW_shgroup_call_add(stl->g_data->tip_points_group, points, NULL);
164         }
165 }
166
167 static void particle_cache_populate(void *vedata, Object *object)
168 {
169         if (object->mode != OB_MODE_PARTICLE_EDIT) {
170                 return;
171         }
172         const DRWContextState *draw_ctx = DRW_context_state_get();
173         Scene *scene_orig = (Scene *)DEG_get_original_id(&draw_ctx->scene->id);
174         /* Usually the edit structure is created by Particle Edit Mode Toggle
175          * operator, but sometimes it's invoked after tagging hair as outdated
176          * (for example, when toggling edit mode). That makes it impossible to
177          * create edit structure for until after next dependency graph evaluation.
178          *
179          * Ideally, the edit structure will be created here already via some
180          * dependency graph callback or so, but currently trying to make it nicer
181          * only causes bad level calls and breaks design from the past.
182          */
183         Object *object_orig = DEG_get_original_object(object);
184         PTCacheEdit *edit = PE_create_current(
185                 draw_ctx->depsgraph, scene_orig, object_orig);
186         if (edit == NULL) {
187                 /* Happens when trying to edit particles in EMITTER mode without
188                  * having them cached.
189                  */
190                 return;
191         }
192         /* NOTE: We need to pass evaluated particle system, which we need
193          * to find first.
194          */
195         ParticleSystem *psys = object->particlesystem.first;
196         ParticleSystem *psys_orig = object_orig->particlesystem.first;
197         while (psys_orig != NULL) {
198                 if (PE_get_current_from_psys(psys_orig) == edit) {
199                         break;
200                 }
201                 psys = psys->next;
202                 psys_orig = psys_orig->next;
203         }
204         if (psys == NULL) {
205                 printf("Error getting evaluated particle system for edit.\n");
206                 return;
207         }
208         particle_edit_cache_populate(vedata, object, psys, edit);
209 }
210
211 /* Optional: Post-cache_populate callback */
212 static void particle_cache_finish(void *UNUSED(vedata))
213 {
214 }
215
216 /* Draw time ! Control rendering pipeline from here */
217 static void particle_draw_scene(void *vedata)
218 {
219
220         PARTICLE_PassList *psl = ((PARTICLE_Data *)vedata)->psl;
221
222         DRW_draw_pass(psl->psys_edit_pass);
223 }
224
225 static void particle_engine_free(void)
226 {
227         DRW_SHADER_FREE_SAFE(e_data.strands_shader);
228 }
229
230 static const DrawEngineDataSize particle_data_size =
231       DRW_VIEWPORT_DATA_SIZE(PARTICLE_Data);
232
233 DrawEngineType draw_engine_particle_type = {
234         NULL, NULL,
235         N_("Particle Mode"),
236         &particle_data_size,
237         &particle_engine_init,
238         &particle_engine_free,
239         &particle_cache_init,
240         &particle_cache_populate,
241         &particle_cache_finish,
242         NULL, /* draw_background but not needed by mode engines */
243         &particle_draw_scene,
244         NULL,
245         NULL,
246 };