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