2 * Copyright 2016, Blender Foundation.
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.
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.
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.
18 * Contributor(s): Blender Institute
22 /** \file overlay_mode.c
23 * \ingroup draw_engine
26 #include "DNA_mesh_types.h"
27 #include "DNA_view3d_types.h"
29 #include "BKE_editmesh.h"
30 #include "BKE_object.h"
31 #include "BKE_global.h"
33 #include "GPU_shader.h"
34 #include "GPU_extensions.h"
35 #include "DRW_render.h"
37 #include "draw_mode_engines.h"
40 typedef struct OVERLAY_StorageList {
41 struct OVERLAY_PrivateData *g_data;
42 } OVERLAY_StorageList;
44 typedef struct OVERLAY_PassList {
45 struct DRWPass *face_orientation_pass;
46 struct DRWPass *face_wireframe_pass;
49 typedef struct OVERLAY_Data {
51 DRWViewportEmptyList *fbl;
52 DRWViewportEmptyList *txl;
53 OVERLAY_PassList *psl;
54 OVERLAY_StorageList *stl;
57 typedef struct OVERLAY_PrivateData {
58 DRWShadingGroup *face_orientation_shgrp;
59 DRWShadingGroup *face_wires;
60 DRWShadingGroup *flat_wires;
61 DRWShadingGroup *sculpt_wires;
62 View3DOverlay overlay;
63 float wire_step_param[2];
64 bool ghost_stencil_test;
66 } OVERLAY_PrivateData; /* Transient data */
68 /* *********** STATIC *********** */
70 /* Face orientation shader */
71 struct GPUShader *face_orientation_sh;
72 /* Wireframe shader */
73 struct GPUShader *select_wireframe_sh;
74 struct GPUShader *face_wireframe_sh;
75 struct GPUShader *face_wireframe_sculpt_sh;
79 extern char datatoc_overlay_face_orientation_frag_glsl[];
80 extern char datatoc_overlay_face_orientation_vert_glsl[];
82 extern char datatoc_overlay_face_wireframe_vert_glsl[];
83 extern char datatoc_overlay_face_wireframe_geom_glsl[];
84 extern char datatoc_overlay_face_wireframe_frag_glsl[];
85 extern char datatoc_gpu_shader_depth_only_frag_glsl[];
87 extern struct GlobalsUboStorage ts; /* draw_common.c */
90 static void overlay_engine_init(void *vedata)
92 OVERLAY_Data *data = vedata;
93 OVERLAY_StorageList *stl = data->stl;
96 /* Alloc transient pointers */
97 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
99 stl->g_data->ghost_stencil_test = false;
101 if (!e_data.face_orientation_sh) {
102 /* Face orientation */
103 e_data.face_orientation_sh = DRW_shader_create(
104 datatoc_overlay_face_orientation_vert_glsl, NULL,
105 datatoc_overlay_face_orientation_frag_glsl, NULL);
108 if (!e_data.face_wireframe_sh) {
109 e_data.select_wireframe_sh = DRW_shader_create(
110 datatoc_overlay_face_wireframe_vert_glsl,
111 datatoc_overlay_face_wireframe_geom_glsl,
112 datatoc_gpu_shader_depth_only_frag_glsl,
113 "#define SELECT_EDGES\n");
115 e_data.face_wireframe_sh = DRW_shader_create(
116 datatoc_overlay_face_wireframe_vert_glsl,
118 datatoc_overlay_face_wireframe_frag_glsl,
121 e_data.face_wireframe_sculpt_sh = DRW_shader_create(
122 datatoc_overlay_face_wireframe_vert_glsl,
123 datatoc_overlay_face_wireframe_geom_glsl,
124 datatoc_overlay_face_wireframe_frag_glsl,
125 "#define USE_SCULPT\n");
129 static void overlay_cache_init(void *vedata)
131 OVERLAY_Data *data = vedata;
132 OVERLAY_PassList *psl = data->psl;
133 OVERLAY_StorageList *stl = data->stl;
134 OVERLAY_PrivateData *g_data = stl->g_data;
136 const DRWContextState *DCS = DRW_context_state_get();
138 View3D *v3d = DCS->v3d;
140 g_data->overlay = v3d->overlay;
141 g_data->show_overlays = (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0;
144 memset(&g_data->overlay, 0, sizeof(g_data->overlay));
145 g_data->show_overlays = false;
148 if (g_data->show_overlays == false) {
149 g_data->overlay.flag = 0;
152 if (v3d->shading.type == OB_WIRE) {
153 g_data->overlay.flag |= V3D_OVERLAY_WIREFRAMES;
154 g_data->show_overlays = true;
158 /* Face Orientation Pass */
159 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND;
160 psl->face_orientation_pass = DRW_pass_create("Face Orientation", state);
161 g_data->face_orientation_shgrp = DRW_shgroup_create(
162 e_data.face_orientation_sh, psl->face_orientation_pass);
167 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND | DRW_STATE_FIRST_VERTEX_CONVENTION;
168 float wire_size = max_ff(0.0f, U.pixelsize - 1.0f) * 0.5f;
170 const bool use_select = (DRW_state_is_select() || DRW_state_is_depth());
171 GPUShader *sculpt_wire_sh = use_select ? e_data.select_wireframe_sh : e_data.face_wireframe_sculpt_sh;
172 GPUShader *face_wires_sh = use_select ? e_data.select_wireframe_sh : e_data.face_wireframe_sh;
173 GPUShader *flat_wires_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
175 psl->face_wireframe_pass = DRW_pass_create("Face Wires", state);
177 g_data->flat_wires = DRW_shgroup_create(flat_wires_sh, psl->face_wireframe_pass);
179 g_data->sculpt_wires = DRW_shgroup_create(sculpt_wire_sh, psl->face_wireframe_pass);
181 g_data->face_wires = DRW_shgroup_create(face_wires_sh, psl->face_wireframe_pass);
182 DRW_shgroup_uniform_vec2(g_data->face_wires, "wireStepParam", g_data->wire_step_param, 1);
185 DRW_shgroup_uniform_float_copy(g_data->sculpt_wires, "wireSize", wire_size);
186 DRW_shgroup_uniform_float_copy(g_data->face_wires, "wireSize", wire_size);
189 /* Control aspect of the falloff. */
190 const float sharpness = 4.0f;
191 /* Scale and bias: Adjust with wiredata encoding. (see mesh_batch_cache_create_edges_wireframe_data) */
192 const float decompress = (0xFF / (float)(0xFF - 0x20));
193 g_data->wire_step_param[0] = -sharpness * decompress;
194 g_data->wire_step_param[1] = decompress + sharpness * stl->g_data->overlay.wireframe_threshold;
198 static void overlay_cache_populate(void *vedata, Object *ob)
200 OVERLAY_Data *data = vedata;
201 OVERLAY_StorageList *stl = data->stl;
202 OVERLAY_PrivateData *pd = stl->g_data;
203 const DRWContextState *draw_ctx = DRW_context_state_get();
204 RegionView3D *rv3d = draw_ctx->rv3d;
205 View3D *v3d = draw_ctx->v3d;
207 if ((!pd->show_overlays) ||
208 (ob->dt < OB_WIRE) ||
209 (!DRW_object_is_renderable(ob) && (ob->dt != OB_WIRE)))
214 if (DRW_object_is_renderable(ob) && pd->overlay.flag & V3D_OVERLAY_FACE_ORIENTATION) {
215 struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
217 DRW_shgroup_call_add(pd->face_orientation_shgrp, geom, ob->obmat);
221 if ((pd->overlay.flag & V3D_OVERLAY_WIREFRAMES) ||
222 (v3d->shading.type == OB_WIRE) ||
223 (ob->dtx & OB_DRAWWIRE) ||
226 const bool is_edit_mode = BKE_object_is_in_editmode(ob);
227 bool has_edit_mesh_cage = false;
228 if (ob->type == OB_MESH) {
229 /* TODO: Should be its own function. */
230 Mesh *me = (Mesh *)ob->data;
231 BMEditMesh *embm = me->edit_btmesh;
233 has_edit_mesh_cage = embm->mesh_eval_cage && (embm->mesh_eval_cage != embm->mesh_eval_final);
237 /* Don't do that in edit Mesh mode, unless there is a modifier preview. */
238 if ((((ob != draw_ctx->object_edit) && !is_edit_mode) || has_edit_mesh_cage) ||
241 const bool is_active = (ob == draw_ctx->obact);
242 const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
243 const bool all_wires = (pd->overlay.wireframe_threshold == 1.0f) ||
244 (ob->dtx & OB_DRAW_ALL_EDGES);
245 const bool is_wire = (ob->dt < OB_SOLID);
246 const int stencil_mask = (ob->dtx & OB_DRAWXRAY) ? 0x00 : 0xFF;
247 DRWShadingGroup *shgrp = NULL;
249 float *rim_col = ts.colorWire;
250 if (!is_edit_mode && !is_sculpt_mode && !has_edit_mesh_cage &&
251 ((ob->base_flag & BASE_SELECTED) != 0))
253 rim_col = (ob == draw_ctx->obact) ? ts.colorActive : ts.colorSelect;
254 rim_col = (G.moving & G_TRANSFORM_OBJ) ? ts.colorTransform : rim_col;
257 /* This fixes only the biggest case which is a plane in ortho view. */
259 bool is_flat_object_viewed_from_side = (rv3d->persp == RV3D_ORTHO) &&
260 DRW_object_is_flat(ob, &flat_axis) &&
261 DRW_object_axis_orthogonal_to_view(ob, flat_axis);
263 if (is_flat_object_viewed_from_side && !is_sculpt_mode) {
264 /* Avoid losing flat objects when in ortho views (see T56549) */
265 struct GPUBatch *geom = DRW_cache_object_all_edges_get(ob);
267 shgrp = pd->flat_wires;
268 shgrp = DRW_shgroup_create_sub(shgrp);
269 DRW_shgroup_stencil_mask(shgrp, stencil_mask);
270 DRW_shgroup_call_object_add(shgrp, geom, ob);
271 DRW_shgroup_uniform_vec4(shgrp, "color", rim_col, 1);
275 struct GPUBatch *geom = DRW_cache_object_face_wireframe_get(ob);
276 if (geom || is_sculpt_mode) {
277 shgrp = (is_sculpt_mode) ? pd->sculpt_wires : pd->face_wires;
278 shgrp = DRW_shgroup_create_sub(shgrp);
280 static float all_wires_params[2] = {0.0f, 10.0f}; /* Parameters for all wires */
281 DRW_shgroup_uniform_vec2(shgrp, "wireStepParam", (all_wires)
283 : pd->wire_step_param, 1);
285 if (!(DRW_state_is_select() || DRW_state_is_depth())) {
286 DRW_shgroup_stencil_mask(shgrp, stencil_mask);
287 DRW_shgroup_uniform_vec3(shgrp, "wireColor", ts.colorWire, 1);
288 DRW_shgroup_uniform_vec3(shgrp, "rimColor", rim_col, 1);
291 if (is_sculpt_mode) {
292 DRW_shgroup_call_sculpt_add(shgrp, ob, ob->obmat);
295 DRW_shgroup_call_add(shgrp, geom, ob->obmat);
299 if (is_wire && shgrp != NULL) {
300 /* If object is wireframe, don't try to use stencil test. */
301 DRW_shgroup_state_disable(shgrp, DRW_STATE_STENCIL_EQUAL);
303 if (ob->dtx & OB_DRAWXRAY) {
304 DRW_shgroup_state_disable(shgrp, DRW_STATE_DEPTH_LESS_EQUAL);
307 else if ((ob->dtx & OB_DRAWXRAY) && shgrp != NULL) {
308 pd->ghost_stencil_test = true;
314 static void overlay_cache_finish(void *vedata)
316 OVERLAY_Data *data = vedata;
317 OVERLAY_PassList *psl = data->psl;
318 OVERLAY_StorageList *stl = data->stl;
320 const DRWContextState *ctx = DRW_context_state_get();
321 View3D *v3d = ctx->v3d;
323 /* only in solid mode */
324 if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & XRAY_FLAG(v3d)) == 0) {
325 if (stl->g_data->ghost_stencil_test) {
326 DRW_pass_state_add(psl->face_wireframe_pass, DRW_STATE_STENCIL_EQUAL);
331 static void overlay_draw_scene(void *vedata)
333 OVERLAY_Data *data = vedata;
334 OVERLAY_PassList *psl = data->psl;
335 DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
337 if (DRW_state_is_fbo()) {
338 GPU_framebuffer_bind(dfbl->default_fb);
340 DRW_draw_pass(psl->face_orientation_pass);
341 DRW_draw_pass(psl->face_wireframe_pass);
344 static void overlay_engine_free(void)
346 DRW_SHADER_FREE_SAFE(e_data.face_orientation_sh);
347 DRW_SHADER_FREE_SAFE(e_data.select_wireframe_sh);
348 DRW_SHADER_FREE_SAFE(e_data.face_wireframe_sh);
349 DRW_SHADER_FREE_SAFE(e_data.face_wireframe_sculpt_sh);
352 static const DrawEngineDataSize overlay_data_size = DRW_VIEWPORT_DATA_SIZE(OVERLAY_Data);
354 DrawEngineType draw_engine_overlay_type = {
358 &overlay_engine_init,
359 &overlay_engine_free,
361 &overlay_cache_populate,
362 &overlay_cache_finish,