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