2 * Copyright 2016, Blender Foundation.
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.
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.
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.
18 * Contributor(s): Blender Institute
22 /** \file basic_engine.c
23 * \ingroup draw_engine
25 * Simple engine for drawing color and/or depth.
26 * When we only need simple flat shaders.
29 #include "DRW_render.h"
31 #include "BKE_icons.h"
32 #include "BKE_idprop.h"
34 #include "BKE_particle.h"
36 #include "DNA_particle_types.h"
38 #include "GPU_shader.h"
40 #include "basic_engine.h"
43 #define BASIC_ENGINE "BLENDER_BASIC"
45 /* *********** LISTS *********** */
47 /* GPUViewport.storage
48 * Is freed everytime the viewport engine changes */
49 typedef struct BASIC_StorageList {
50 struct BASIC_PrivateData *g_data;
53 typedef struct BASIC_PassList {
54 struct DRWPass *depth_pass;
55 struct DRWPass *depth_pass_cull;
58 typedef struct BASIC_Data {
60 DRWViewportEmptyList *fbl;
61 DRWViewportEmptyList *txl;
63 BASIC_StorageList *stl;
66 /* *********** STATIC *********** */
70 struct GPUShader *depth_sh;
71 } e_data = {NULL}; /* Engine data */
73 typedef struct BASIC_PrivateData {
74 DRWShadingGroup *depth_shgrp;
75 DRWShadingGroup *depth_shgrp_cull;
76 DRWShadingGroup *depth_shgrp_hair;
77 } BASIC_PrivateData; /* Transient data */
81 static void basic_engine_init(void *UNUSED(vedata))
84 if (!e_data.depth_sh) {
85 e_data.depth_sh = DRW_shader_create_3D_depth_only();
89 static void basic_cache_init(void *vedata)
91 BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl;
92 BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
95 /* Alloc transient pointers */
96 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
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);
104 psl->depth_pass_cull = DRW_pass_create(
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);
111 static void basic_cache_populate(void *vedata, Object *ob)
113 BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
115 /* TODO(fclem) fix selection of smoke domains. */
117 if (!DRW_object_is_renderable(ob) || (ob->dt < OB_SOLID)) {
121 const DRWContextState *draw_ctx = DRW_context_state_get();
122 if (ob != draw_ctx->object_edit) {
123 for (ParticleSystem *psys = ob->particlesystem.first;
127 if (!psys_check_enabled(ob, psys, false)) {
130 if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
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);
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) ||
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));
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);
162 struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
164 const bool do_cull = (draw_ctx->v3d && (draw_ctx->v3d->shading.flag & V3D_SHADING_BACKFACE_CULLING));
166 DRW_shgroup_call_add((do_cull) ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp, geom, ob->obmat);
170 static void basic_cache_finish(void *vedata)
172 BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
177 static void basic_draw_scene(void *vedata)
179 BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl;
181 DRW_draw_pass(psl->depth_pass);
182 DRW_draw_pass(psl->depth_pass_cull);
185 static void basic_engine_free(void)
187 /* all shaders are builtin */
190 static const DrawEngineDataSize basic_data_size = DRW_VIEWPORT_DATA_SIZE(BASIC_Data);
192 DrawEngineType draw_engine_basic_type = {
199 &basic_cache_populate,
208 /* Note: currently unused, we may want to register so we can see this when debugging the view. */
210 RenderEngineType DRW_engine_viewport_basic_type = {
212 BASIC_ENGINE, N_("Basic"), RE_INTERNAL,
213 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
214 &draw_engine_basic_type,