Cleanup: move clipping shader lib & define into struct
[blender.git] / source / blender / draw / modes / edit_curve_mode.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  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file \ingroup draw
20  */
21
22 #include "DRW_engine.h"
23 #include "DRW_render.h"
24
25 #include "DNA_curve_types.h"
26 #include "DNA_view3d_types.h"
27
28 #include "BKE_object.h"
29
30 /* If builtin shaders are needed */
31 #include "GPU_shader.h"
32
33 #include "draw_common.h"
34
35
36 /* If needed, contains all global/Theme colors
37  * Add needed theme colors / values to DRW_globals_update() and update UBO
38  * Not needed for constant color. */
39
40 extern char datatoc_common_globals_lib_glsl[];
41 extern char datatoc_edit_curve_overlay_loosevert_vert_glsl[];
42 extern char datatoc_edit_curve_overlay_normals_vert_glsl[];
43 extern char datatoc_edit_curve_overlay_handle_vert_glsl[];
44 extern char datatoc_edit_curve_overlay_handle_geom_glsl[];
45
46 extern char datatoc_gpu_shader_point_varying_color_frag_glsl[];
47 extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];
48 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
49
50 /* *********** LISTS *********** */
51 /* All lists are per viewport specific datas.
52  * They are all free when viewport changes engines
53  * or is free itself. Use EDIT_CURVE_engine_init() to
54  * initialize most of them and EDIT_CURVE_cache_init()
55  * for EDIT_CURVE_PassList */
56
57 typedef struct EDIT_CURVE_PassList {
58         struct DRWPass *wire_pass;
59         struct DRWPass *overlay_edge_pass;
60         struct DRWPass *overlay_vert_pass;
61 } EDIT_CURVE_PassList;
62
63 typedef struct EDIT_CURVE_StorageList {
64         struct CustomStruct *block;
65         struct EDIT_CURVE_PrivateData *g_data;
66 } EDIT_CURVE_StorageList;
67
68 typedef struct EDIT_CURVE_Data {
69         void *engine_type; /* Required */
70         DRWViewportEmptyList *fbl;
71         DRWViewportEmptyList *txl;
72         EDIT_CURVE_PassList *psl;
73         EDIT_CURVE_StorageList *stl;
74 } EDIT_CURVE_Data;
75
76 /* *********** STATIC *********** */
77
78
79 typedef struct EDIT_CURVE_Shaders {
80         GPUShader *wire_sh;
81         GPUShader *wire_normals_sh;
82         GPUShader *overlay_edge_sh;  /* handles and nurbs control cage */
83         GPUShader *overlay_vert_sh;
84 } EDIT_CURVE_Shaders;
85
86 static struct {
87         EDIT_CURVE_Shaders sh_data[GPU_SHADER_CFG_LEN];
88 } e_data = {NULL}; /* Engine data */
89
90 typedef struct EDIT_CURVE_PrivateData {
91         /* resulting curve as 'wire' for curves (and optionally normals) */
92         DRWShadingGroup *wire_shgrp;
93         DRWShadingGroup *wire_normals_shgrp;
94
95         DRWShadingGroup *overlay_edge_shgrp;
96         DRWShadingGroup *overlay_vert_shgrp;
97
98         int show_handles;
99 } EDIT_CURVE_PrivateData; /* Transient data */
100
101 /* *********** FUNCTIONS *********** */
102
103 /* Init Textures, Framebuffers, Storage and Shaders.
104  * It is called for every frames.
105  * (Optional) */
106 static void EDIT_CURVE_engine_init(void *UNUSED(vedata))
107 {
108         const DRWContextState *draw_ctx = DRW_context_state_get();
109         EDIT_CURVE_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
110
111         if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
112                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
113         }
114
115         const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[draw_ctx->sh_cfg];
116
117         if (!sh_data->wire_sh) {
118                 sh_data->wire_sh = GPU_shader_get_builtin_shader_with_config(
119                         GPU_SHADER_3D_UNIFORM_COLOR, draw_ctx->sh_cfg);
120         }
121
122         if (!sh_data->wire_normals_sh) {
123                 sh_data->wire_normals_sh = GPU_shader_create_from_arrays({
124                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_edit_curve_overlay_normals_vert_glsl, NULL},
125                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
126                         .defs = (const char *[]){sh_cfg_data->def, NULL},
127                 });
128         }
129
130         if (!sh_data->overlay_edge_sh) {
131                 sh_data->overlay_edge_sh = GPU_shader_create_from_arrays({
132                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_edit_curve_overlay_handle_vert_glsl, NULL},
133                         .geom = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_edit_curve_overlay_handle_geom_glsl, NULL},
134                         .frag = (const char *[]){datatoc_gpu_shader_3D_smooth_color_frag_glsl, NULL},
135                         .defs = (const char *[]){sh_cfg_data->def, NULL},
136                 });
137         }
138
139         if (!sh_data->overlay_vert_sh) {
140                 sh_data->overlay_vert_sh = GPU_shader_create_from_arrays({
141                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_edit_curve_overlay_loosevert_vert_glsl, NULL},
142                         .frag = (const char *[]){datatoc_gpu_shader_point_varying_color_frag_glsl, NULL},
143                         .defs = (const char *[]){sh_cfg_data->def, NULL},
144                 });
145
146         }
147 }
148
149 /* Here init all passes and shading groups
150  * Assume that all Passes are NULL */
151 static void EDIT_CURVE_cache_init(void *vedata)
152 {
153         EDIT_CURVE_PassList *psl = ((EDIT_CURVE_Data *)vedata)->psl;
154         EDIT_CURVE_StorageList *stl = ((EDIT_CURVE_Data *)vedata)->stl;
155         const DRWContextState *draw_ctx = DRW_context_state_get();
156         View3D *v3d = draw_ctx->v3d;
157         const RegionView3D *rv3d = draw_ctx->rv3d;
158         EDIT_CURVE_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
159
160         if (!stl->g_data) {
161                 /* Alloc transient pointers */
162                 stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__);
163         }
164
165         stl->g_data->show_handles = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_CU_HANDLES) != 0;
166
167         {
168                 DRWShadingGroup *grp;
169
170                 /* Center-Line (wire) */
171                 psl->wire_pass = DRW_pass_create(
172                         "Curve Wire",
173                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE);
174
175                 grp = DRW_shgroup_create(sh_data->wire_sh, psl->wire_pass);
176                 DRW_shgroup_uniform_vec4(grp, "color", G_draw.block.colorWireEdit, 1);
177                 if (rv3d->rflag & RV3D_CLIPPING) {
178                         DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
179                 }
180                 stl->g_data->wire_shgrp = grp;
181
182
183                 grp = DRW_shgroup_create(sh_data->wire_normals_sh, psl->wire_pass);
184                 DRW_shgroup_uniform_vec4(grp, "color", G_draw.block.colorWireEdit, 1);
185                 DRW_shgroup_uniform_float_copy(grp, "normalSize", v3d->overlay.normals_length);
186                 if (rv3d->rflag & RV3D_CLIPPING) {
187                         DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
188                 }
189                 stl->g_data->wire_normals_shgrp = grp;
190
191                 psl->overlay_edge_pass = DRW_pass_create(
192                         "Curve Handle Overlay",
193                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
194
195                 grp = DRW_shgroup_create(sh_data->overlay_edge_sh, psl->overlay_edge_pass);
196                 DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
197                 DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
198                 DRW_shgroup_uniform_bool(grp, "showCurveHandles", &stl->g_data->show_handles, 1);
199                 if (rv3d->rflag & RV3D_CLIPPING) {
200                         DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
201                 }
202                 stl->g_data->overlay_edge_shgrp = grp;
203
204
205                 psl->overlay_vert_pass = DRW_pass_create(
206                         "Curve Vert Overlay",
207                         DRW_STATE_WRITE_COLOR | DRW_STATE_POINT);
208
209                 grp = DRW_shgroup_create(sh_data->overlay_vert_sh, psl->overlay_vert_pass);
210                 DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
211                 if (rv3d->rflag & RV3D_CLIPPING) {
212                         DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
213                 }
214                 stl->g_data->overlay_vert_shgrp = grp;
215         }
216 }
217
218 /* Add geometry to shadingGroups. Execute for each objects */
219 static void EDIT_CURVE_cache_populate(void *vedata, Object *ob)
220 {
221         EDIT_CURVE_StorageList *stl = ((EDIT_CURVE_Data *)vedata)->stl;
222         const DRWContextState *draw_ctx = DRW_context_state_get();
223         View3D *v3d = draw_ctx->v3d;
224
225         if (ob->type == OB_CURVE) {
226                 if (BKE_object_is_in_editmode(ob)) {
227                         Curve *cu = ob->data;
228                         /* Get geometry cache */
229                         struct GPUBatch *geom;
230
231                         geom = DRW_cache_curve_edge_wire_get(ob);
232                         DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
233
234                         if ((cu->flag & CU_3D) && (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_CU_NORMALS) != 0) {
235                                 static uint instance_len = 2;
236                                 geom = DRW_cache_curve_edge_normal_get(ob);
237                                 DRW_shgroup_call_instances_add(stl->g_data->wire_normals_shgrp, geom, ob->obmat, &instance_len);
238                         }
239
240                         geom = DRW_cache_curve_edge_overlay_get(ob);
241                         if (geom) {
242                                 DRW_shgroup_call_add(stl->g_data->overlay_edge_shgrp, geom, ob->obmat);
243                         }
244
245                         geom = DRW_cache_curve_vert_overlay_get(ob, stl->g_data->show_handles);
246                         DRW_shgroup_call_add(stl->g_data->overlay_vert_shgrp, geom, ob->obmat);
247                 }
248         }
249
250         if (ob->type == OB_SURF) {
251                 if (BKE_object_is_in_editmode(ob)) {
252                         struct GPUBatch *geom = DRW_cache_curve_edge_overlay_get(ob);
253                         DRW_shgroup_call_add(stl->g_data->overlay_edge_shgrp, geom, ob->obmat);
254
255                         geom = DRW_cache_curve_vert_overlay_get(ob, false);
256                         DRW_shgroup_call_add(stl->g_data->overlay_vert_shgrp, geom, ob->obmat);
257                 }
258         }
259 }
260
261 /* Draw time ! Control rendering pipeline from here */
262 static void EDIT_CURVE_draw_scene(void *vedata)
263 {
264         EDIT_CURVE_PassList *psl = ((EDIT_CURVE_Data *)vedata)->psl;
265
266         /* Default framebuffer and texture */
267         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
268         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
269
270         if (!DRW_pass_is_empty(psl->wire_pass)) {
271                 MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
272
273                 DRW_draw_pass(psl->wire_pass);
274
275                 MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
276         }
277
278         /* Thoses passes don't write to depth and are AA'ed using other tricks. */
279         DRW_draw_pass(psl->overlay_edge_pass);
280         DRW_draw_pass(psl->overlay_vert_pass);
281
282         DRW_state_clip_planes_reset();
283 }
284
285 /* Cleanup when destroying the engine.
286  * This is not per viewport ! only when quitting blender.
287  * Mostly used for freeing shaders */
288 static void EDIT_CURVE_engine_free(void)
289 {
290         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
291                 EDIT_CURVE_Shaders *sh_data = &e_data.sh_data[sh_data_index];
292                 /* Don't free builtins. */
293                 sh_data->wire_sh = NULL;
294                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
295                 for (int i = 0; i < (sizeof(EDIT_CURVE_Shaders) / sizeof(GPUShader *)); i++) {
296                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
297                 }
298         }
299 }
300
301 static const DrawEngineDataSize EDIT_CURVE_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_CURVE_Data);
302
303 DrawEngineType draw_engine_edit_curve_type = {
304         NULL, NULL,
305         N_("EditCurveMode"),
306         &EDIT_CURVE_data_size,
307         &EDIT_CURVE_engine_init,
308         &EDIT_CURVE_engine_free,
309         &EDIT_CURVE_cache_init,
310         &EDIT_CURVE_cache_populate,
311         NULL,
312         NULL, /* draw_background but not needed by mode engines */
313         &EDIT_CURVE_draw_scene,
314         NULL,
315         NULL,
316 };