Fix rotate around point under cursor 'seeing' hidden faces in paint modes.
[blender.git] / source / blender / draw / engines / basic / basic_engine.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 basic_engine.c
23  *  \ingroup draw_engine
24  *
25  * Simple engine for drawing color and/or depth.
26  * When we only need simple flat shaders.
27  */
28
29 #include "DRW_render.h"
30
31 #include "BKE_icons.h"
32 #include "BKE_idprop.h"
33 #include "BKE_main.h"
34 #include "BKE_particle.h"
35
36 #include "DNA_particle_types.h"
37
38 #include "GPU_shader.h"
39
40 #include "basic_engine.h"
41 /* Shaders */
42
43 #define BASIC_ENGINE "BLENDER_BASIC"
44
45 /* *********** LISTS *********** */
46
47 /* GPUViewport.storage
48  * Is freed everytime the viewport engine changes */
49 typedef struct BASIC_StorageList {
50         struct BASIC_PrivateData *g_data;
51 } BASIC_StorageList;
52
53 typedef struct BASIC_PassList {
54         struct DRWPass *depth_pass;
55         struct DRWPass *depth_pass_cull;
56 } BASIC_PassList;
57
58 typedef struct BASIC_Data {
59         void *engine_type;
60         DRWViewportEmptyList *fbl;
61         DRWViewportEmptyList *txl;
62         BASIC_PassList *psl;
63         BASIC_StorageList *stl;
64 } BASIC_Data;
65
66 /* *********** STATIC *********** */
67
68 static struct {
69         /* Depth Pre Pass */
70         struct GPUShader *depth_sh;
71 } e_data = {NULL}; /* Engine data */
72
73 typedef struct BASIC_PrivateData {
74         DRWShadingGroup *depth_shgrp;
75         DRWShadingGroup *depth_shgrp_cull;
76         DRWShadingGroup *depth_shgrp_hair;
77 } BASIC_PrivateData; /* Transient data */
78
79 /* Functions */
80
81 static void basic_engine_init(void *UNUSED(vedata))
82 {
83         /* Depth prepass */
84         if (!e_data.depth_sh) {
85                 e_data.depth_sh = DRW_shader_create_3D_depth_only();
86         }
87 }
88
89 static void basic_cache_init(void *vedata)
90 {
91         BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl;
92         BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
93
94         if (!stl->g_data) {
95                 /* Alloc transient pointers */
96                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
97         }
98
99         {
100                 psl->depth_pass = DRW_pass_create(
101                         "Depth Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE);
102                 stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass);
103
104                 psl->depth_pass_cull = DRW_pass_create(
105                         "Depth Pass Cull",
106                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK);
107                 stl->g_data->depth_shgrp_cull = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass_cull);
108         }
109 }
110
111 static void basic_cache_populate(void *vedata, Object *ob)
112 {
113         BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
114
115         /* TODO(fclem) fix selection of smoke domains. */
116
117         if (!DRW_object_is_renderable(ob) || (ob->dt < OB_SOLID)) {
118                 return;
119         }
120
121         const DRWContextState *draw_ctx = DRW_context_state_get();
122         if (ob != draw_ctx->object_edit) {
123                 for (ParticleSystem *psys = ob->particlesystem.first;
124                      psys != NULL;
125                      psys = psys->next)
126                 {
127                         if (!psys_check_enabled(ob, psys, false)) {
128                                 continue;
129                         }
130                         if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
131                                 continue;
132                         }
133                         ParticleSettings *part = psys->part;
134                         const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
135                         if (draw_as == PART_DRAW_PATH) {
136                                 struct GPUBatch *hairs = DRW_cache_particles_get_hair(ob, psys, NULL);
137                                 DRW_shgroup_call_add(stl->g_data->depth_shgrp, hairs, NULL);
138                         }
139                 }
140         }
141
142         /* Make flat object selectable in ortho view if wireframe is enabled. */
143         if ((draw_ctx->v3d->overlay.flag & V3D_OVERLAY_WIREFRAMES) ||
144             (ob->dtx & OB_DRAWWIRE) ||
145             (ob->dt == OB_WIRE))
146         {
147                 int flat_axis = 0;
148                 bool is_flat_object_viewed_from_side = (draw_ctx->rv3d->persp == RV3D_ORTHO) &&
149                                                        DRW_object_is_flat(ob, &flat_axis) &&
150                                                        DRW_object_axis_orthogonal_to_view(ob, flat_axis);
151
152                 if (is_flat_object_viewed_from_side) {
153                         /* Avoid losing flat objects when in ortho views (see T56549) */
154                         struct GPUBatch *geom = DRW_cache_object_wire_outline_get(ob);
155                         DRW_shgroup_call_object_add(stl->g_data->depth_shgrp, geom, ob);
156                         return;
157                 }
158         }
159
160         const bool is_active = (ob == draw_ctx->obact);
161         const bool use_hide = is_active && DRW_object_use_hide_faces(ob);
162
163         struct GPUBatch *geom = DRW_cache_object_surface_get_ex(ob, use_hide);
164         if (geom) {
165                 const bool do_cull = (draw_ctx->v3d && (draw_ctx->v3d->flag2 & V3D_BACKFACE_CULLING));
166                 /* Depth Prepass */
167                 DRW_shgroup_call_add((do_cull) ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp, geom, ob->obmat);
168         }
169 }
170
171 static void basic_cache_finish(void *vedata)
172 {
173         BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
174
175         UNUSED_VARS(stl);
176 }
177
178 static void basic_draw_scene(void *vedata)
179 {
180         BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl;
181
182         DRW_draw_pass(psl->depth_pass);
183         DRW_draw_pass(psl->depth_pass_cull);
184 }
185
186 static void basic_engine_free(void)
187 {
188         /* all shaders are builtin */
189 }
190
191 static const DrawEngineDataSize basic_data_size = DRW_VIEWPORT_DATA_SIZE(BASIC_Data);
192
193 DrawEngineType draw_engine_basic_type = {
194         NULL, NULL,
195         N_("Basic"),
196         &basic_data_size,
197         &basic_engine_init,
198         &basic_engine_free,
199         &basic_cache_init,
200         &basic_cache_populate,
201         &basic_cache_finish,
202         NULL,
203         &basic_draw_scene,
204         NULL,
205         NULL,
206         NULL,
207 };
208
209 /* Note: currently unused, we may want to register so we can see this when debugging the view. */
210
211 RenderEngineType DRW_engine_viewport_basic_type = {
212         NULL, NULL,
213         BASIC_ENGINE, N_("Basic"), RE_INTERNAL,
214         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
215         &draw_engine_basic_type,
216         {NULL, NULL, NULL}
217 };
218
219
220 #undef BASIC_ENGINE