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