2017-04-26 17:20:11 +10:00
|
|
|
/*
|
|
|
|
|
* Copyright 2016, Blender Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): Blender Institute
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
/** \file basic_engine.h
|
|
|
|
|
* \ingroup draw_engine
|
|
|
|
|
*
|
|
|
|
|
* Simple engine for drawing color and/or depth.
|
|
|
|
|
* When we only need simple flat shaders.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-04-26 17:20:11 +10:00
|
|
|
#include "DRW_render.h"
|
|
|
|
|
|
|
|
|
|
#include "BKE_icons.h"
|
|
|
|
|
#include "BKE_idprop.h"
|
|
|
|
|
#include "BKE_main.h"
|
|
|
|
|
|
|
|
|
|
#include "GPU_shader.h"
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
#include "basic_engine.h"
|
2017-04-26 17:20:11 +10:00
|
|
|
/* Shaders */
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
#define BASIC_ENGINE "BLENDER_BASIC"
|
2017-04-26 17:20:11 +10:00
|
|
|
|
|
|
|
|
/* we may want this later? */
|
|
|
|
|
#define USE_DEPTH
|
|
|
|
|
|
|
|
|
|
/* *********** LISTS *********** */
|
|
|
|
|
|
|
|
|
|
/* GPUViewport.storage
|
|
|
|
|
* Is freed everytime the viewport engine changes */
|
2017-04-27 02:49:13 +10:00
|
|
|
typedef struct BASIC_Storage {
|
2017-04-26 17:20:11 +10:00
|
|
|
int dummy;
|
2017-04-27 02:49:13 +10:00
|
|
|
} BASIC_Storage;
|
2017-04-26 17:20:11 +10:00
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
typedef struct BASIC_StorageList {
|
|
|
|
|
struct BASIC_Storage *storage;
|
2017-04-26 17:20:11 +10:00
|
|
|
struct g_data *g_data;
|
2017-04-27 02:49:13 +10:00
|
|
|
} BASIC_StorageList;
|
2017-04-26 17:20:11 +10:00
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
typedef struct BASIC_FramebufferList {
|
2017-04-26 17:20:11 +10:00
|
|
|
/* default */
|
|
|
|
|
struct GPUFrameBuffer *default_fb;
|
|
|
|
|
/* engine specific */
|
|
|
|
|
#ifdef USE_DEPTH
|
|
|
|
|
struct GPUFrameBuffer *dupli_depth;
|
|
|
|
|
#endif
|
2017-04-27 02:49:13 +10:00
|
|
|
} BASIC_FramebufferList;
|
2017-04-26 17:20:11 +10:00
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
typedef struct BASIC_TextureList {
|
2017-04-26 17:20:11 +10:00
|
|
|
/* default */
|
|
|
|
|
struct GPUTexture *color;
|
|
|
|
|
#ifdef USE_DEPTH
|
|
|
|
|
struct GPUTexture *depth;
|
|
|
|
|
/* engine specific */
|
|
|
|
|
struct GPUTexture *depth_dup;
|
|
|
|
|
#endif
|
2017-04-27 02:49:13 +10:00
|
|
|
} BASIC_TextureList;
|
2017-04-26 17:20:11 +10:00
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
typedef struct BASIC_PassList {
|
2017-04-26 17:20:11 +10:00
|
|
|
#ifdef USE_DEPTH
|
|
|
|
|
struct DRWPass *depth_pass;
|
2017-04-26 18:08:49 +10:00
|
|
|
struct DRWPass *depth_pass_cull;
|
2017-04-26 17:20:11 +10:00
|
|
|
#endif
|
|
|
|
|
struct DRWPass *color_pass;
|
|
|
|
|
struct g_data *g_data;
|
2017-04-27 02:49:13 +10:00
|
|
|
} BASIC_PassList;
|
2017-04-26 17:20:11 +10:00
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
typedef struct BASIC_Data {
|
2017-04-26 17:20:11 +10:00
|
|
|
void *engine_type;
|
2017-04-27 02:49:13 +10:00
|
|
|
BASIC_FramebufferList *fbl;
|
|
|
|
|
BASIC_TextureList *txl;
|
|
|
|
|
BASIC_PassList *psl;
|
|
|
|
|
BASIC_StorageList *stl;
|
|
|
|
|
} BASIC_Data;
|
2017-04-26 17:20:11 +10:00
|
|
|
|
|
|
|
|
/* *********** STATIC *********** */
|
|
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
|
#ifdef USE_DEPTH
|
|
|
|
|
/* Depth Pre Pass */
|
|
|
|
|
struct GPUShader *depth_sh;
|
|
|
|
|
#endif
|
|
|
|
|
/* Shading Pass */
|
|
|
|
|
struct GPUShader *color_sh;
|
|
|
|
|
} e_data = {NULL}; /* Engine data */
|
|
|
|
|
|
|
|
|
|
typedef struct g_data {
|
2017-04-26 18:08:49 +10:00
|
|
|
#ifdef USE_DEPTH
|
2017-04-26 17:20:11 +10:00
|
|
|
DRWShadingGroup *depth_shgrp;
|
|
|
|
|
DRWShadingGroup *depth_shgrp_cull;
|
2017-04-26 18:08:49 +10:00
|
|
|
#endif
|
|
|
|
|
DRWShadingGroup *color_shgrp;
|
2017-04-26 17:20:11 +10:00
|
|
|
} g_data; /* Transient data */
|
|
|
|
|
|
|
|
|
|
/* Functions */
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
static void BASIC_engine_init(void *vedata)
|
2017-04-26 17:20:11 +10:00
|
|
|
{
|
2017-04-27 02:49:13 +10:00
|
|
|
BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
|
|
|
|
|
BASIC_TextureList *txl = ((BASIC_Data *)vedata)->txl;
|
|
|
|
|
BASIC_FramebufferList *fbl = ((BASIC_Data *)vedata)->fbl;
|
2017-04-26 17:20:11 +10:00
|
|
|
|
2017-04-26 18:08:49 +10:00
|
|
|
#ifdef USE_DEPTH
|
2017-04-26 17:20:11 +10:00
|
|
|
/* Depth prepass */
|
|
|
|
|
if (!e_data.depth_sh) {
|
|
|
|
|
e_data.depth_sh = DRW_shader_create_3D_depth_only();
|
|
|
|
|
}
|
2017-04-26 18:08:49 +10:00
|
|
|
#endif
|
2017-04-26 17:20:11 +10:00
|
|
|
|
|
|
|
|
/* Shading pass */
|
|
|
|
|
if (!e_data.color_sh) {
|
|
|
|
|
e_data.color_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!stl->storage) {
|
2017-04-27 02:49:13 +10:00
|
|
|
stl->storage = MEM_callocN(sizeof(BASIC_Storage), "BASIC_Storage");
|
2017-04-26 17:20:11 +10:00
|
|
|
}
|
|
|
|
|
|
2017-04-26 18:08:49 +10:00
|
|
|
#ifdef USE_DEPTH
|
2017-04-27 02:04:56 +10:00
|
|
|
if (DRW_state_is_fbo()) {
|
2017-04-26 17:20:11 +10:00
|
|
|
const float *viewport_size = DRW_viewport_size_get();
|
|
|
|
|
DRWFboTexture tex = {&txl->depth_dup, DRW_BUF_DEPTH_24, 0};
|
|
|
|
|
DRW_framebuffer_init(&fbl->dupli_depth,
|
|
|
|
|
(int)viewport_size[0], (int)viewport_size[1],
|
|
|
|
|
&tex, 1);
|
|
|
|
|
}
|
2017-04-26 18:08:49 +10:00
|
|
|
#endif
|
2017-04-26 17:20:11 +10:00
|
|
|
}
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
static void BASIC_cache_init(void *vedata)
|
2017-04-26 17:20:11 +10:00
|
|
|
{
|
2017-04-27 02:49:13 +10:00
|
|
|
BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl;
|
|
|
|
|
BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
|
2017-04-26 17:20:11 +10:00
|
|
|
|
|
|
|
|
if (!stl->g_data) {
|
|
|
|
|
/* Alloc transient pointers */
|
|
|
|
|
stl->g_data = MEM_mallocN(sizeof(g_data), "g_data");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef USE_DEPTH
|
|
|
|
|
/* Depth Pass */
|
|
|
|
|
{
|
|
|
|
|
psl->depth_pass = DRW_pass_create("Depth Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
|
|
|
|
|
stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass);
|
2017-04-26 18:08:49 +10:00
|
|
|
|
|
|
|
|
psl->depth_pass_cull = DRW_pass_create(
|
|
|
|
|
"Depth Pass Cull",
|
|
|
|
|
DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_CULL_BACK);
|
|
|
|
|
stl->g_data->depth_shgrp_cull = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass_cull);
|
2017-04-26 17:20:11 +10:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Color Pass */
|
|
|
|
|
{
|
|
|
|
|
psl->color_pass = DRW_pass_create("Color Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL);
|
2017-04-26 18:08:49 +10:00
|
|
|
stl->g_data->color_shgrp = DRW_shgroup_create(e_data.color_sh, psl->color_pass);
|
2017-04-26 17:20:11 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
static void BASIC_cache_populate(void *vedata, Object *ob)
|
2017-04-26 17:20:11 +10:00
|
|
|
{
|
2017-04-27 02:49:13 +10:00
|
|
|
BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
|
2017-04-26 17:20:11 +10:00
|
|
|
|
|
|
|
|
if (!DRW_is_object_renderable(ob))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
struct Batch *geom = DRW_cache_object_surface_get(ob);
|
|
|
|
|
if (geom) {
|
2017-04-26 18:08:49 +10:00
|
|
|
bool do_cull = false; /* TODO (we probably wan't to take this from the viewport?) */
|
|
|
|
|
#ifdef USE_DEPTH
|
2017-04-26 17:20:11 +10:00
|
|
|
/* Depth Prepass */
|
|
|
|
|
DRW_shgroup_call_add((do_cull) ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp, geom, ob->obmat);
|
2017-04-26 18:08:49 +10:00
|
|
|
#endif
|
2017-04-26 17:20:11 +10:00
|
|
|
/* Shading */
|
2017-04-26 18:08:49 +10:00
|
|
|
DRW_shgroup_call_add(stl->g_data->color_shgrp, geom, ob->obmat);
|
2017-04-26 17:20:11 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
static void BASIC_cache_finish(void *vedata)
|
2017-04-26 17:20:11 +10:00
|
|
|
{
|
2017-04-27 02:49:13 +10:00
|
|
|
BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
|
2017-04-26 17:20:11 +10:00
|
|
|
|
|
|
|
|
UNUSED_VARS(stl);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
static void BASIC_draw_scene(void *vedata)
|
2017-04-26 17:20:11 +10:00
|
|
|
{
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl;
|
|
|
|
|
BASIC_FramebufferList *fbl = ((BASIC_Data *)vedata)->fbl;
|
2017-04-26 17:20:11 +10:00
|
|
|
DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
|
|
|
|
|
|
|
|
|
|
#ifdef USE_DEPTH
|
|
|
|
|
/* Pass 1 : Depth pre-pass */
|
|
|
|
|
DRW_draw_pass(psl->depth_pass);
|
2017-04-26 18:08:49 +10:00
|
|
|
DRW_draw_pass(psl->depth_pass_cull);
|
2017-04-26 17:20:11 +10:00
|
|
|
|
|
|
|
|
/* Pass 2 : Duplicate depth */
|
|
|
|
|
/* Unless we go for deferred shading we need this to avoid manual depth test and artifacts */
|
2017-04-27 02:04:56 +10:00
|
|
|
if (DRW_state_is_fbo()) {
|
2017-04-26 17:20:11 +10:00
|
|
|
DRW_framebuffer_blit(dfbl->default_fb, fbl->dupli_depth, true);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Pass 3 : Shading */
|
|
|
|
|
DRW_draw_pass(psl->color_pass);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
static void BASIC_engine_free(void)
|
2017-04-26 17:20:11 +10:00
|
|
|
{
|
|
|
|
|
/* all shaders are builtin */
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
static const DrawEngineDataSize BASIC_data_size = DRW_VIEWPORT_DATA_SIZE(BASIC_Data);
|
2017-04-26 17:20:11 +10:00
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
DrawEngineType draw_engine_basic_type = {
|
2017-04-26 17:20:11 +10:00
|
|
|
NULL, NULL,
|
2017-04-27 02:49:13 +10:00
|
|
|
N_("Basic"),
|
|
|
|
|
&BASIC_data_size,
|
|
|
|
|
&BASIC_engine_init,
|
|
|
|
|
&BASIC_engine_free,
|
|
|
|
|
&BASIC_cache_init,
|
|
|
|
|
&BASIC_cache_populate,
|
|
|
|
|
&BASIC_cache_finish,
|
2017-04-26 17:20:11 +10:00
|
|
|
NULL,
|
2017-04-27 02:49:13 +10:00
|
|
|
&BASIC_draw_scene
|
2017-04-26 17:20:11 +10:00
|
|
|
};
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
/* Note: currently unused, we may want to register so we can see this when debugging the view. */
|
|
|
|
|
|
|
|
|
|
RenderEngineType DRW_engine_viewport_basic_type = {
|
2017-04-26 17:20:11 +10:00
|
|
|
NULL, NULL,
|
2017-04-27 02:49:13 +10:00
|
|
|
BASIC_ENGINE, N_("Basic"), RE_INTERNAL | RE_USE_OGL_PIPELINE,
|
2017-04-26 17:20:11 +10:00
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
2017-04-27 02:49:13 +10:00
|
|
|
&draw_engine_basic_type,
|
2017-04-26 17:20:11 +10:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2017-04-27 02:49:13 +10:00
|
|
|
#undef BASIC_ENGINE
|