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