Cleanup: move clipping shader lib & define into struct
[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->sh_cfg];
82
83         /* Depth prepass */
84         if (!sh_data->depth) {
85                 sh_data->depth = DRW_shader_create_3D_depth_only(draw_ctx->sh_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->sh_cfg];
96         const RegionView3D *rv3d = draw_ctx->rv3d;
97
98         if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
99                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
100         }
101
102         if (!stl->g_data) {
103                 /* Alloc transient pointers */
104                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
105         }
106
107         {
108                 psl->depth_pass = DRW_pass_create(
109                         "Depth Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE);
110                 stl->g_data->depth_shgrp = DRW_shgroup_create(sh_data->depth, psl->depth_pass);
111                 if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
112                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->depth_shgrp, rv3d);
113                 }
114
115                 psl->depth_pass_cull = DRW_pass_create(
116                         "Depth Pass Cull",
117                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK);
118                 stl->g_data->depth_shgrp_cull = DRW_shgroup_create(sh_data->depth, psl->depth_pass_cull);
119                 if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
120                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->depth_shgrp_cull, rv3d);
121                 }
122         }
123 }
124
125 static void basic_cache_populate(void *vedata, Object *ob)
126 {
127         BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
128
129         /* TODO(fclem) fix selection of smoke domains. */
130
131         if (!DRW_object_is_renderable(ob) || (ob->dt < OB_SOLID)) {
132                 return;
133         }
134
135         const DRWContextState *draw_ctx = DRW_context_state_get();
136         if (ob != draw_ctx->object_edit) {
137                 for (ParticleSystem *psys = ob->particlesystem.first;
138                      psys != NULL;
139                      psys = psys->next)
140                 {
141                         if (!psys_check_enabled(ob, psys, false)) {
142                                 continue;
143                         }
144                         if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
145                                 continue;
146                         }
147                         ParticleSettings *part = psys->part;
148                         const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
149                         if (draw_as == PART_DRAW_PATH) {
150                                 struct GPUBatch *hairs = DRW_cache_particles_get_hair(ob, psys, NULL);
151                                 DRW_shgroup_call_add(stl->g_data->depth_shgrp, hairs, NULL);
152                         }
153                 }
154         }
155
156         /* Make flat object selectable in ortho view if wireframe is enabled. */
157         if ((draw_ctx->v3d->overlay.flag & V3D_OVERLAY_WIREFRAMES) ||
158             (draw_ctx->v3d->shading.type == OB_WIRE) ||
159             (ob->dtx & OB_DRAWWIRE) ||
160             (ob->dt == OB_WIRE))
161         {
162                 int flat_axis = 0;
163                 bool is_flat_object_viewed_from_side = (
164                         (draw_ctx->rv3d->persp == RV3D_ORTHO) &&
165                         DRW_object_is_flat(ob, &flat_axis) &&
166                         DRW_object_axis_orthogonal_to_view(ob, flat_axis));
167
168                 if (is_flat_object_viewed_from_side) {
169                         /* Avoid losing flat objects when in ortho views (see T56549) */
170                         struct GPUBatch *geom = DRW_cache_object_all_edges_get(ob);
171                         DRW_shgroup_call_object_add(stl->g_data->depth_shgrp, geom, ob);
172                         return;
173                 }
174         }
175
176         struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
177         if (geom) {
178                 const bool do_cull = (draw_ctx->v3d && (draw_ctx->v3d->shading.flag & V3D_SHADING_BACKFACE_CULLING));
179                 /* Depth Prepass */
180                 DRW_shgroup_call_add((do_cull) ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp, geom, ob->obmat);
181         }
182 }
183
184 static void basic_cache_finish(void *vedata)
185 {
186         BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
187
188         UNUSED_VARS(stl);
189 }
190
191 static void basic_draw_scene(void *vedata)
192 {
193         BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl;
194
195         DRW_draw_pass(psl->depth_pass);
196         DRW_draw_pass(psl->depth_pass_cull);
197 }
198
199 static void basic_engine_free(void)
200 {
201         /* all shaders are builtin */
202 }
203
204 static const DrawEngineDataSize basic_data_size = DRW_VIEWPORT_DATA_SIZE(BASIC_Data);
205
206 DrawEngineType draw_engine_basic_type = {
207         NULL, NULL,
208         N_("Basic"),
209         &basic_data_size,
210         &basic_engine_init,
211         &basic_engine_free,
212         &basic_cache_init,
213         &basic_cache_populate,
214         &basic_cache_finish,
215         NULL,
216         &basic_draw_scene,
217         NULL,
218         NULL,
219         NULL,
220 };
221
222 /* Note: currently unused, we may want to register so we can see this when debugging the view. */
223
224 RenderEngineType DRW_engine_viewport_basic_type = {
225         NULL, NULL,
226         BASIC_ENGINE, N_("Basic"), RE_INTERNAL,
227         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
228         &draw_engine_basic_type,
229         {NULL, NULL, NULL},
230 };
231
232
233 #undef BASIC_ENGINE