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.
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.
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.
16 * Copyright 2016, Blender Foundation.
19 /** \file \ingroup draw_engine
21 * Simple engine for drawing color and/or depth.
22 * When we only need simple flat shaders.
25 #include "DRW_render.h"
27 #include "BKE_particle.h"
29 #include "DNA_particle_types.h"
31 #include "GPU_shader.h"
33 #include "basic_engine.h"
36 #define BASIC_ENGINE "BLENDER_BASIC"
38 /* *********** LISTS *********** */
40 /* GPUViewport.storage
41 * Is freed everytime the viewport engine changes */
42 typedef struct BASIC_StorageList {
43 struct BASIC_PrivateData *g_data;
46 typedef struct BASIC_PassList {
47 struct DRWPass *depth_pass;
48 struct DRWPass *depth_pass_cull;
51 typedef struct BASIC_Data {
53 DRWViewportEmptyList *fbl;
54 DRWViewportEmptyList *txl;
56 BASIC_StorageList *stl;
59 typedef struct BASIC_Shaders {
61 struct GPUShader *depth;
64 /* *********** STATIC *********** */
67 BASIC_Shaders sh_data[GPU_SHADER_CFG_LEN];
68 } e_data = {NULL}; /* Engine data */
70 typedef struct BASIC_PrivateData {
71 DRWShadingGroup *depth_shgrp;
72 DRWShadingGroup *depth_shgrp_cull;
73 DRWShadingGroup *depth_shgrp_hair;
74 } BASIC_PrivateData; /* Transient data */
78 static void basic_engine_init(void *UNUSED(vedata))
80 const DRWContextState *draw_ctx = DRW_context_state_get();
81 BASIC_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
84 if (!sh_data->depth) {
85 sh_data->depth = DRW_shader_create_3D_depth_only(draw_ctx->sh_cfg);
89 static void basic_cache_init(void *vedata)
91 BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl;
92 BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
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 const bool is_clip = (rv3d->rflag & RV3D_CLIPPING) != 0;
100 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
104 /* Alloc transient pointers */
105 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
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);
116 psl->depth_pass_cull = DRW_pass_create(
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);
126 static void basic_cache_populate(void *vedata, Object *ob)
128 BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
130 /* TODO(fclem) fix selection of smoke domains. */
132 if (!DRW_object_is_renderable(ob) || (ob->dt < OB_SOLID)) {
136 const DRWContextState *draw_ctx = DRW_context_state_get();
137 if (ob != draw_ctx->object_edit) {
138 for (ParticleSystem *psys = ob->particlesystem.first;
142 if (!psys_check_enabled(ob, psys, false)) {
145 if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
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);
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) ||
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));
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);
177 struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
179 const bool do_cull = (draw_ctx->v3d && (draw_ctx->v3d->shading.flag & V3D_SHADING_BACKFACE_CULLING));
181 DRW_shgroup_call_add((do_cull) ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp, geom, ob->obmat);
185 static void basic_cache_finish(void *vedata)
187 BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
192 static void basic_draw_scene(void *vedata)
194 BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl;
196 DRW_draw_pass(psl->depth_pass);
197 DRW_draw_pass(psl->depth_pass_cull);
200 static void basic_engine_free(void)
202 /* all shaders are builtin */
205 static const DrawEngineDataSize basic_data_size = DRW_VIEWPORT_DATA_SIZE(BASIC_Data);
207 DrawEngineType draw_engine_basic_type = {
214 &basic_cache_populate,
223 /* Note: currently unused, we may want to register so we can see this when debugging the view. */
225 RenderEngineType DRW_engine_viewport_basic_type = {
227 BASIC_ENGINE, N_("Basic"), RE_INTERNAL,
228 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
229 &draw_engine_basic_type,