2017-05-01 14:55:59 +02:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2019-01-23 11:29:18 +11:00
|
|
|
* Copyright 2017, Blender Foundation.
|
2017-05-01 14:55:59 +02:00
|
|
|
*/
|
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup draw_engine
|
2017-05-01 14:55:59 +02:00
|
|
|
*
|
|
|
|
|
* Base engine for external render engines.
|
|
|
|
|
* We use it for depth and non-mesh objects.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "DRW_render.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_screen_types.h"
|
|
|
|
|
#include "DNA_view3d_types.h"
|
|
|
|
|
|
2019-09-19 17:31:07 +02:00
|
|
|
#include "BKE_object.h"
|
|
|
|
|
|
2017-05-01 14:55:59 +02:00
|
|
|
#include "ED_screen.h"
|
|
|
|
|
|
|
|
|
|
#include "GPU_matrix.h"
|
|
|
|
|
#include "GPU_shader.h"
|
|
|
|
|
#include "GPU_viewport.h"
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
#include "external_engine.h" /* own include */
|
2019-02-23 18:31:45 +11:00
|
|
|
|
2017-05-01 14:55:59 +02:00
|
|
|
/* Shaders */
|
|
|
|
|
|
|
|
|
|
#define EXTERNAL_ENGINE "BLENDER_EXTERNAL"
|
|
|
|
|
|
|
|
|
|
/* *********** LISTS *********** */
|
|
|
|
|
|
|
|
|
|
/* GPUViewport.storage
|
|
|
|
|
* Is freed everytime the viewport engine changes */
|
|
|
|
|
typedef struct EXTERNAL_Storage {
|
2019-04-17 06:17:24 +02:00
|
|
|
int dummy;
|
2017-05-01 14:55:59 +02:00
|
|
|
} EXTERNAL_Storage;
|
|
|
|
|
|
|
|
|
|
typedef struct EXTERNAL_StorageList {
|
2019-04-17 06:17:24 +02:00
|
|
|
struct EXTERNAL_Storage *storage;
|
|
|
|
|
struct EXTERNAL_PrivateData *g_data;
|
2017-05-01 14:55:59 +02:00
|
|
|
} EXTERNAL_StorageList;
|
|
|
|
|
|
|
|
|
|
typedef struct EXTERNAL_FramebufferList {
|
2019-05-01 12:31:59 +02:00
|
|
|
struct GPUFrameBuffer *depth_buffer_fb;
|
2017-05-01 14:55:59 +02:00
|
|
|
} EXTERNAL_FramebufferList;
|
|
|
|
|
|
|
|
|
|
typedef struct EXTERNAL_TextureList {
|
2019-04-17 06:17:24 +02:00
|
|
|
/* default */
|
2019-05-01 12:31:59 +02:00
|
|
|
struct GPUTexture *depth_buffer_tx;
|
2017-05-01 14:55:59 +02:00
|
|
|
} EXTERNAL_TextureList;
|
|
|
|
|
|
|
|
|
|
typedef struct EXTERNAL_PassList {
|
2019-04-17 06:17:24 +02:00
|
|
|
struct DRWPass *depth_pass;
|
2017-05-01 14:55:59 +02:00
|
|
|
} EXTERNAL_PassList;
|
|
|
|
|
|
|
|
|
|
typedef struct EXTERNAL_Data {
|
2019-04-17 06:17:24 +02:00
|
|
|
void *engine_type;
|
|
|
|
|
EXTERNAL_FramebufferList *fbl;
|
|
|
|
|
EXTERNAL_TextureList *txl;
|
|
|
|
|
EXTERNAL_PassList *psl;
|
|
|
|
|
EXTERNAL_StorageList *stl;
|
|
|
|
|
char info[GPU_INFO_SIZE];
|
2017-05-01 14:55:59 +02:00
|
|
|
} EXTERNAL_Data;
|
|
|
|
|
|
|
|
|
|
/* *********** STATIC *********** */
|
|
|
|
|
|
|
|
|
|
static struct {
|
2019-04-17 06:17:24 +02:00
|
|
|
/* Depth Pre Pass */
|
|
|
|
|
struct GPUShader *depth_sh;
|
2017-05-01 14:55:59 +02:00
|
|
|
} e_data = {NULL}; /* Engine data */
|
|
|
|
|
|
|
|
|
|
typedef struct EXTERNAL_PrivateData {
|
2019-04-17 06:17:24 +02:00
|
|
|
DRWShadingGroup *depth_shgrp;
|
2019-05-01 12:31:59 +02:00
|
|
|
|
|
|
|
|
/* Do we need to update the depth or can we reuse the last calculated texture. */
|
2020-01-23 21:38:06 +01:00
|
|
|
bool need_depth;
|
2019-05-01 12:31:59 +02:00
|
|
|
bool update_depth;
|
|
|
|
|
|
2019-05-20 19:35:20 +02:00
|
|
|
float last_persmat[4][4];
|
2017-05-01 14:55:59 +02:00
|
|
|
} EXTERNAL_PrivateData; /* Transient data */
|
|
|
|
|
|
|
|
|
|
/* Functions */
|
|
|
|
|
|
2019-05-01 12:31:59 +02:00
|
|
|
static void external_engine_init(void *vedata)
|
2017-05-01 14:55:59 +02:00
|
|
|
{
|
2019-05-01 12:31:59 +02:00
|
|
|
EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl;
|
|
|
|
|
const DRWContextState *draw_ctx = DRW_context_state_get();
|
2020-03-06 16:56:42 +01:00
|
|
|
ARegion *region = draw_ctx->region;
|
2019-05-01 12:31:59 +02:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
/* Depth prepass */
|
|
|
|
|
if (!e_data.depth_sh) {
|
|
|
|
|
e_data.depth_sh = DRW_shader_create_3d_depth_only(GPU_SHADER_CFG_DEFAULT);
|
|
|
|
|
}
|
2019-05-01 12:31:59 +02:00
|
|
|
|
|
|
|
|
if (!stl->g_data) {
|
|
|
|
|
/* Alloc transient pointers */
|
|
|
|
|
stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
|
2020-01-23 21:38:06 +01:00
|
|
|
stl->g_data->need_depth = true;
|
2019-05-01 12:31:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-23 21:38:06 +01:00
|
|
|
stl->g_data->update_depth = true;
|
|
|
|
|
|
2019-06-02 02:14:56 +02:00
|
|
|
/* Progressive render samples are tagged with no rebuild, in that case we
|
|
|
|
|
* can skip updating the depth buffer */
|
2020-03-06 16:56:42 +01:00
|
|
|
if (region && (region->do_draw & RGN_DRAW_NO_REBUILD)) {
|
2020-01-23 21:38:06 +01:00
|
|
|
stl->g_data->update_depth = false;
|
2019-05-01 12:31:59 +02:00
|
|
|
}
|
2017-05-01 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-29 12:30:55 +01:00
|
|
|
static void external_cache_init(void *vedata)
|
2017-05-01 14:55:59 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
EXTERNAL_PassList *psl = ((EXTERNAL_Data *)vedata)->psl;
|
|
|
|
|
EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl;
|
2019-05-01 12:31:59 +02:00
|
|
|
EXTERNAL_TextureList *txl = ((EXTERNAL_Data *)vedata)->txl;
|
|
|
|
|
EXTERNAL_FramebufferList *fbl = ((EXTERNAL_Data *)vedata)->fbl;
|
2020-01-23 21:38:06 +01:00
|
|
|
const DRWContextState *draw_ctx = DRW_context_state_get();
|
|
|
|
|
const View3D *v3d = draw_ctx->v3d;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-01 12:31:59 +02:00
|
|
|
{
|
|
|
|
|
DRW_texture_ensure_fullscreen_2d(&txl->depth_buffer_tx, GPU_DEPTH24_STENCIL8, 0);
|
|
|
|
|
|
|
|
|
|
GPU_framebuffer_ensure_config(&fbl->depth_buffer_fb,
|
|
|
|
|
{
|
|
|
|
|
GPU_ATTACHMENT_TEXTURE(txl->depth_buffer_tx),
|
|
|
|
|
});
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Depth Pass */
|
|
|
|
|
{
|
|
|
|
|
psl->depth_pass = DRW_pass_create("Depth Pass",
|
|
|
|
|
DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
|
|
|
|
|
stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass);
|
|
|
|
|
}
|
2019-05-01 12:31:59 +02:00
|
|
|
|
|
|
|
|
/* Do not draw depth pass when overlays are turned off. */
|
2020-01-23 21:38:06 +01:00
|
|
|
stl->g_data->need_depth = (v3d->flag2 & V3D_HIDE_OVERLAYS) == 0;
|
2017-05-01 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-29 12:30:55 +01:00
|
|
|
static void external_cache_populate(void *vedata, Object *ob)
|
2017-05-01 14:55:59 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl;
|
2017-05-01 14:55:59 +02:00
|
|
|
|
2019-09-19 17:31:07 +02:00
|
|
|
if (!(DRW_object_is_renderable(ob) &&
|
|
|
|
|
DRW_object_visibility_in_active_context(ob) & OB_VISIBLE_SELF)) {
|
2019-04-17 06:17:24 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2017-05-01 14:55:59 +02:00
|
|
|
|
2020-01-23 21:38:06 +01:00
|
|
|
if (ob->type == OB_GPENCIL) {
|
|
|
|
|
/* Grease Pencil objects need correct depth to do the blending. */
|
|
|
|
|
stl->g_data->need_depth = true;
|
2019-05-01 12:31:59 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 21:38:06 +01:00
|
|
|
struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
|
|
|
|
|
if (geom) {
|
|
|
|
|
/* Depth Prepass */
|
|
|
|
|
DRW_shgroup_call(stl->g_data->depth_shgrp, geom, ob);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2017-05-01 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-29 12:30:55 +01:00
|
|
|
static void external_cache_finish(void *UNUSED(vedata))
|
2017-05-01 14:55:59 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 12:30:55 +01:00
|
|
|
static void external_draw_scene_do(void *vedata)
|
2017-05-01 14:55:59 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const DRWContextState *draw_ctx = DRW_context_state_get();
|
|
|
|
|
Scene *scene = draw_ctx->scene;
|
|
|
|
|
RegionView3D *rv3d = draw_ctx->rv3d;
|
2020-03-06 16:56:42 +01:00
|
|
|
ARegion *region = draw_ctx->region;
|
2020-03-13 17:27:11 +11:00
|
|
|
const RenderEngineType *type;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
|
DRW_state_reset_ex(DRW_STATE_DEFAULT & ~DRW_STATE_DEPTH_LESS_EQUAL);
|
|
|
|
|
|
|
|
|
|
/* Create render engine. */
|
|
|
|
|
if (!rv3d->render_engine) {
|
|
|
|
|
RenderEngineType *engine_type = draw_ctx->engine_type;
|
|
|
|
|
|
|
|
|
|
if (!(engine_type->view_update && engine_type->view_draw)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenderEngine *engine = RE_engine_create_ex(engine_type, true);
|
|
|
|
|
engine->tile_x = scene->r.tilex;
|
|
|
|
|
engine->tile_y = scene->r.tiley;
|
2019-05-16 17:19:05 +02:00
|
|
|
engine_type->view_update(engine, draw_ctx->evil_C, draw_ctx->depsgraph);
|
2019-04-17 06:17:24 +02:00
|
|
|
rv3d->render_engine = engine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Rendered draw. */
|
|
|
|
|
GPU_matrix_push_projection();
|
2019-05-27 16:33:38 +02:00
|
|
|
GPU_matrix_push();
|
2020-03-06 16:56:42 +01:00
|
|
|
ED_region_pixelspace(region);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
|
/* Render result draw. */
|
|
|
|
|
type = rv3d->render_engine->type;
|
2019-05-16 17:19:05 +02:00
|
|
|
type->view_draw(rv3d->render_engine, draw_ctx->evil_C, draw_ctx->depsgraph);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-27 16:33:38 +02:00
|
|
|
GPU_matrix_pop();
|
2019-04-17 06:17:24 +02:00
|
|
|
GPU_matrix_pop_projection();
|
|
|
|
|
|
|
|
|
|
/* Set render info. */
|
|
|
|
|
EXTERNAL_Data *data = vedata;
|
|
|
|
|
if (rv3d->render_engine->text[0] != '\0') {
|
|
|
|
|
BLI_strncpy(data->info, rv3d->render_engine->text, sizeof(data->info));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
data->info[0] = '\0';
|
|
|
|
|
}
|
2017-05-01 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-29 12:30:55 +01:00
|
|
|
static void external_draw_scene(void *vedata)
|
2017-05-01 14:55:59 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const DRWContextState *draw_ctx = DRW_context_state_get();
|
2019-05-01 12:31:59 +02:00
|
|
|
EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl;
|
2019-04-17 06:17:24 +02:00
|
|
|
EXTERNAL_PassList *psl = ((EXTERNAL_Data *)vedata)->psl;
|
2019-05-01 12:31:59 +02:00
|
|
|
EXTERNAL_FramebufferList *fbl = ((EXTERNAL_Data *)vedata)->fbl;
|
|
|
|
|
const DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
|
/* Will be NULL during OpenGL render.
|
|
|
|
|
* OpenGL render is used for quick preview (thumbnails or sequencer preview)
|
|
|
|
|
* where using the rendering engine to preview doesn't make so much sense. */
|
|
|
|
|
if (draw_ctx->evil_C) {
|
2020-02-11 15:18:55 +01:00
|
|
|
float clear_col[4] = {0, 0, 0, 0};
|
|
|
|
|
/* This is to keep compatibility with external engine. */
|
|
|
|
|
/* TODO(fclem) remove it eventually. */
|
|
|
|
|
GPU_framebuffer_bind(dfbl->default_fb);
|
|
|
|
|
GPU_framebuffer_clear_color(dfbl->default_fb, clear_col);
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
external_draw_scene_do(vedata);
|
|
|
|
|
}
|
2019-05-01 12:31:59 +02:00
|
|
|
|
2020-01-23 21:38:06 +01:00
|
|
|
if (stl->g_data->update_depth && stl->g_data->need_depth) {
|
2019-05-01 12:31:59 +02:00
|
|
|
DRW_draw_pass(psl->depth_pass);
|
2020-01-23 21:38:06 +01:00
|
|
|
/* Copy main depth buffer to cached framebuffer. */
|
2019-05-01 12:31:59 +02:00
|
|
|
GPU_framebuffer_blit(dfbl->depth_only_fb, 0, fbl->depth_buffer_fb, 0, GPU_DEPTH_BIT);
|
|
|
|
|
}
|
2020-01-23 21:38:06 +01:00
|
|
|
|
|
|
|
|
/* Copy cached depth buffer to main framebuffer. */
|
|
|
|
|
GPU_framebuffer_blit(fbl->depth_buffer_fb, 0, dfbl->depth_only_fb, 0, GPU_DEPTH_BIT);
|
2019-05-01 12:31:59 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-29 12:30:55 +01:00
|
|
|
static void external_engine_free(void)
|
2017-05-01 14:55:59 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
/* All shaders are builtin. */
|
2017-05-01 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-29 12:30:55 +01:00
|
|
|
static const DrawEngineDataSize external_data_size = DRW_VIEWPORT_DATA_SIZE(EXTERNAL_Data);
|
2017-05-01 14:55:59 +02:00
|
|
|
|
2019-02-23 18:31:45 +11:00
|
|
|
static DrawEngineType draw_engine_external_type = {
|
2019-04-17 06:17:24 +02:00
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
N_("External"),
|
|
|
|
|
&external_data_size,
|
|
|
|
|
&external_engine_init,
|
|
|
|
|
&external_engine_free,
|
|
|
|
|
&external_cache_init,
|
|
|
|
|
&external_cache_populate,
|
|
|
|
|
&external_cache_finish,
|
|
|
|
|
&external_draw_scene,
|
2019-06-02 02:14:56 +02:00
|
|
|
NULL,
|
2019-04-17 06:17:24 +02:00
|
|
|
NULL,
|
|
|
|
|
NULL,
|
2017-05-01 14:55:59 +02:00
|
|
|
};
|
|
|
|
|
|
2019-05-01 10:35:46 +10:00
|
|
|
/* Note: currently unused,
|
|
|
|
|
* we should not register unless we want to see this when debugging the view. */
|
2017-05-01 14:55:59 +02:00
|
|
|
|
|
|
|
|
RenderEngineType DRW_engine_viewport_external_type = {
|
2019-04-17 06:17:24 +02:00
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
EXTERNAL_ENGINE,
|
|
|
|
|
N_("External"),
|
|
|
|
|
RE_INTERNAL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
&draw_engine_external_type,
|
|
|
|
|
{NULL, NULL, NULL},
|
2017-05-01 14:55:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#undef EXTERNAL_ENGINE
|