Cleanup: indentation (right shift)
[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             (draw_ctx->v3d->shading.type == OB_WIRE) ||
145             (ob->dtx & OB_DRAWWIRE) ||
146             (ob->dt == OB_WIRE))
147         {
148                 int flat_axis = 0;
149                 bool is_flat_object_viewed_from_side = (
150                         (draw_ctx->rv3d->persp == RV3D_ORTHO) &&
151                         DRW_object_is_flat(ob, &flat_axis) &&
152                         DRW_object_axis_orthogonal_to_view(ob, flat_axis));
153
154                 if (is_flat_object_viewed_from_side) {
155                         /* Avoid losing flat objects when in ortho views (see T56549) */
156                         struct GPUBatch *geom = DRW_cache_object_all_edges_get(ob);
157                         DRW_shgroup_call_object_add(stl->g_data->depth_shgrp, geom, ob);
158                         return;
159                 }
160         }
161
162         struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
163         if (geom) {
164                 const bool do_cull = (draw_ctx->v3d && (draw_ctx->v3d->shading.flag & V3D_SHADING_BACKFACE_CULLING));
165                 /* Depth Prepass */
166                 DRW_shgroup_call_add((do_cull) ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp, geom, ob->obmat);
167         }
168 }
169
170 static void basic_cache_finish(void *vedata)
171 {
172         BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
173
174         UNUSED_VARS(stl);
175 }
176
177 static void basic_draw_scene(void *vedata)
178 {
179         BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl;
180
181         DRW_draw_pass(psl->depth_pass);
182         DRW_draw_pass(psl->depth_pass_cull);
183 }
184
185 static void basic_engine_free(void)
186 {
187         /* all shaders are builtin */
188 }
189
190 static const DrawEngineDataSize basic_data_size = DRW_VIEWPORT_DATA_SIZE(BASIC_Data);
191
192 DrawEngineType draw_engine_basic_type = {
193         NULL, NULL,
194         N_("Basic"),
195         &basic_data_size,
196         &basic_engine_init,
197         &basic_engine_free,
198         &basic_cache_init,
199         &basic_cache_populate,
200         &basic_cache_finish,
201         NULL,
202         &basic_draw_scene,
203         NULL,
204         NULL,
205         NULL,
206 };
207
208 /* Note: currently unused, we may want to register so we can see this when debugging the view. */
209
210 RenderEngineType DRW_engine_viewport_basic_type = {
211         NULL, NULL,
212         BASIC_ENGINE, N_("Basic"), RE_INTERNAL,
213         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
214         &draw_engine_basic_type,
215         {NULL, NULL, NULL}
216 };
217
218
219 #undef BASIC_ENGINE