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.
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.
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.
16 * Copyright 2016, Blender Foundation.
19 /** \file \ingroup draw
22 #include "DRW_engine.h"
23 #include "DRW_render.h"
25 #include "DNA_curve_types.h"
26 #include "DNA_view3d_types.h"
28 #include "BKE_object.h"
30 /* If builtin shaders are needed */
31 #include "GPU_shader.h"
33 #include "draw_common.h"
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. */
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[];
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[];
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 */
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;
63 typedef struct EDIT_CURVE_StorageList {
64 struct CustomStruct *block;
65 struct EDIT_CURVE_PrivateData *g_data;
66 } EDIT_CURVE_StorageList;
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;
76 /* *********** STATIC *********** */
79 typedef struct EDIT_CURVE_Shaders {
81 GPUShader *wire_normals_sh;
82 GPUShader *overlay_edge_sh; /* handles and nurbs control cage */
83 GPUShader *overlay_vert_sh;
87 EDIT_CURVE_Shaders sh_data[GPU_SHADER_CFG_LEN];
88 } e_data = {NULL}; /* Engine data */
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;
95 DRWShadingGroup *overlay_edge_shgrp;
96 DRWShadingGroup *overlay_vert_shgrp;
99 } EDIT_CURVE_PrivateData; /* Transient data */
101 /* *********** FUNCTIONS *********** */
103 /* Init Textures, Framebuffers, Storage and Shaders.
104 * It is called for every frames.
106 static void EDIT_CURVE_engine_init(void *UNUSED(vedata))
108 const DRWContextState *draw_ctx = DRW_context_state_get();
109 EDIT_CURVE_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
111 if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
112 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
115 const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[draw_ctx->sh_cfg];
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);
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},
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},
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},
149 /* Here init all passes and shading groups
150 * Assume that all Passes are NULL */
151 static void EDIT_CURVE_cache_init(void *vedata)
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];
161 /* Alloc transient pointers */
162 stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__);
165 stl->g_data->show_handles = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_CU_HANDLES) != 0;
168 DRWShadingGroup *grp;
170 /* Center-Line (wire) */
171 psl->wire_pass = DRW_pass_create(
173 DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE);
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);
180 stl->g_data->wire_shgrp = grp;
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);
189 stl->g_data->wire_normals_shgrp = grp;
191 psl->overlay_edge_pass = DRW_pass_create(
192 "Curve Handle Overlay",
193 DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
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);
202 stl->g_data->overlay_edge_shgrp = grp;
205 psl->overlay_vert_pass = DRW_pass_create(
206 "Curve Vert Overlay",
207 DRW_STATE_WRITE_COLOR | DRW_STATE_POINT);
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);
214 stl->g_data->overlay_vert_shgrp = grp;
218 /* Add geometry to shadingGroups. Execute for each objects */
219 static void EDIT_CURVE_cache_populate(void *vedata, Object *ob)
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;
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;
231 geom = DRW_cache_curve_edge_wire_get(ob);
232 DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
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);
240 geom = DRW_cache_curve_edge_overlay_get(ob);
242 DRW_shgroup_call_add(stl->g_data->overlay_edge_shgrp, geom, ob->obmat);
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);
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);
255 geom = DRW_cache_curve_vert_overlay_get(ob, false);
256 DRW_shgroup_call_add(stl->g_data->overlay_vert_shgrp, geom, ob->obmat);
261 /* Draw time ! Control rendering pipeline from here */
262 static void EDIT_CURVE_draw_scene(void *vedata)
264 EDIT_CURVE_PassList *psl = ((EDIT_CURVE_Data *)vedata)->psl;
266 /* Default framebuffer and texture */
267 DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
268 DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
270 if (!DRW_pass_is_empty(psl->wire_pass)) {
271 MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
273 DRW_draw_pass(psl->wire_pass);
275 MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
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);
282 DRW_state_clip_planes_reset();
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)
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]);
301 static const DrawEngineDataSize EDIT_CURVE_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_CURVE_Data);
303 DrawEngineType draw_engine_edit_curve_type = {
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,
312 NULL, /* draw_background but not needed by mode engines */
313 &EDIT_CURVE_draw_scene,