2018-04-18 13:44:33 +02: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
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** \file workbench_materials.c
|
|
|
|
|
* \ingroup draw_engine
|
|
|
|
|
*/
|
2018-04-19 07:47:03 +02:00
|
|
|
|
2018-04-18 13:44:33 +02:00
|
|
|
#include "workbench_private.h"
|
2018-04-25 10:59:48 +02:00
|
|
|
|
|
|
|
|
#include "BLI_dynstr.h"
|
2018-05-02 12:05:40 +02:00
|
|
|
#include "BLI_alloca.h"
|
2018-04-25 13:00:18 +02:00
|
|
|
|
|
|
|
|
#include "BKE_particle.h"
|
|
|
|
|
|
2018-04-26 08:46:58 +02:00
|
|
|
#include "DNA_modifier_types.h"
|
|
|
|
|
|
2018-04-18 13:44:33 +02:00
|
|
|
#include "GPU_shader.h"
|
|
|
|
|
|
2018-04-25 13:00:18 +02:00
|
|
|
#include "UI_resources.h"
|
|
|
|
|
|
2018-04-18 13:44:33 +02:00
|
|
|
/* *********** STATIC *********** */
|
2018-04-25 10:59:48 +02:00
|
|
|
#define MAX_SHADERS 255
|
2018-04-18 13:44:33 +02:00
|
|
|
static struct {
|
2018-04-25 10:59:48 +02:00
|
|
|
struct GPUShader *prepass_sh_cache[MAX_SHADERS];
|
|
|
|
|
struct GPUShader *composite_sh_cache[MAX_SHADERS];
|
2018-04-19 07:47:03 +02:00
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
struct GPUTexture *object_id_tx; /* ref only, not alloced */
|
2018-04-25 11:54:02 +02:00
|
|
|
struct GPUTexture *color_buffer_tx; /* ref only, not alloced */
|
|
|
|
|
struct GPUTexture *normal_buffer_tx; /* ref only, not alloced */
|
2018-04-18 13:44:33 +02:00
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
int next_object_id;
|
2018-04-28 16:20:19 +02:00
|
|
|
} e_data = {{NULL}};
|
2018-04-18 13:44:33 +02:00
|
|
|
|
|
|
|
|
/* Shaders */
|
2018-04-25 10:59:48 +02:00
|
|
|
extern char datatoc_workbench_prepass_vert_glsl[];
|
|
|
|
|
extern char datatoc_workbench_prepass_frag_glsl[];
|
|
|
|
|
extern char datatoc_workbench_composite_frag_glsl[];
|
2018-04-18 13:44:33 +02:00
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
extern char datatoc_workbench_background_lib_glsl[];
|
2018-04-25 11:30:38 +02:00
|
|
|
extern char datatoc_workbench_common_lib_glsl[];
|
2018-04-25 10:59:48 +02:00
|
|
|
extern char datatoc_workbench_data_lib_glsl[];
|
|
|
|
|
extern char datatoc_workbench_object_overlap_lib_glsl[];
|
|
|
|
|
extern char datatoc_workbench_world_light_lib_glsl[];
|
|
|
|
|
|
|
|
|
|
extern DrawEngineType draw_engine_workbench_solid;
|
|
|
|
|
|
|
|
|
|
#define OBJECT_ID_PASS_ENABLED(wpd) (wpd->drawtype_options & V3D_DRAWOPTION_OBJECT_OVERLAP)
|
|
|
|
|
#define NORMAL_VIEWPORT_PASS_ENABLED(wpd) (wpd->drawtype_lighting & V3D_LIGHTING_STUDIO)
|
|
|
|
|
|
|
|
|
|
static char *workbench_build_defines(WORKBENCH_PrivateData *wpd)
|
2018-04-19 07:47:03 +02:00
|
|
|
{
|
2018-04-25 10:59:48 +02:00
|
|
|
char *str = NULL;
|
2018-04-19 07:47:03 +02:00
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
DynStr *ds = BLI_dynstr_new();
|
|
|
|
|
|
|
|
|
|
if (wpd->drawtype_options & V3D_DRAWOPTION_OBJECT_OVERLAP) {
|
|
|
|
|
BLI_dynstr_appendf(ds, "#define V3D_DRAWOPTION_OBJECT_OVERLAP\n");
|
|
|
|
|
}
|
|
|
|
|
if (wpd->drawtype_lighting & V3D_LIGHTING_STUDIO) {
|
|
|
|
|
BLI_dynstr_appendf(ds, "#define V3D_LIGHTING_STUDIO\n");
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 16:57:18 +02:00
|
|
|
#ifdef WORKBENCH_ENCODE_NORMALS
|
|
|
|
|
BLI_dynstr_appendf(ds, "#define WORKBENCH_ENCODE_NORMALS\n");
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
str = BLI_dynstr_get_cstring(ds);
|
|
|
|
|
BLI_dynstr_free(ds);
|
|
|
|
|
return str;
|
2018-04-18 13:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
static char *workbench_build_composite_frag(WORKBENCH_PrivateData *wpd)
|
2018-04-19 07:47:03 +02:00
|
|
|
{
|
2018-04-25 10:59:48 +02:00
|
|
|
char *str = NULL;
|
2018-04-20 14:38:55 +02:00
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
DynStr *ds = BLI_dynstr_new();
|
|
|
|
|
|
|
|
|
|
BLI_dynstr_append(ds, datatoc_workbench_data_lib_glsl);
|
2018-04-25 11:30:38 +02:00
|
|
|
BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
|
2018-04-25 10:59:48 +02:00
|
|
|
BLI_dynstr_append(ds, datatoc_workbench_background_lib_glsl);
|
|
|
|
|
|
|
|
|
|
if (wpd->drawtype_lighting & V3D_LIGHTING_STUDIO) {
|
|
|
|
|
BLI_dynstr_append(ds, datatoc_workbench_world_light_lib_glsl);
|
2018-04-21 20:42:27 +02:00
|
|
|
}
|
2018-04-25 10:59:48 +02:00
|
|
|
if (wpd->drawtype_options & V3D_DRAWOPTION_OBJECT_OVERLAP) {
|
|
|
|
|
BLI_dynstr_append(ds, datatoc_workbench_object_overlap_lib_glsl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_dynstr_append(ds, datatoc_workbench_composite_frag_glsl);
|
|
|
|
|
|
|
|
|
|
str = BLI_dynstr_get_cstring(ds);
|
|
|
|
|
BLI_dynstr_free(ds);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 11:54:02 +02:00
|
|
|
static char *workbench_build_prepass_frag(void)
|
|
|
|
|
{
|
|
|
|
|
char *str = NULL;
|
|
|
|
|
|
|
|
|
|
DynStr *ds = BLI_dynstr_new();
|
|
|
|
|
|
|
|
|
|
BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
|
|
|
|
|
BLI_dynstr_append(ds, datatoc_workbench_prepass_frag_glsl);
|
|
|
|
|
|
|
|
|
|
str = BLI_dynstr_get_cstring(ds);
|
|
|
|
|
BLI_dynstr_free(ds);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
static int get_shader_index(WORKBENCH_PrivateData *wpd)
|
|
|
|
|
{
|
|
|
|
|
return (wpd->drawtype_options << 2) + wpd->drawtype_lighting;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void select_deferred_shaders(WORKBENCH_PrivateData *wpd)
|
|
|
|
|
{
|
|
|
|
|
int index = get_shader_index(wpd);
|
|
|
|
|
|
|
|
|
|
if (e_data.prepass_sh_cache[index] == NULL) {
|
|
|
|
|
char *defines = workbench_build_defines(wpd);
|
|
|
|
|
char *composite_frag = workbench_build_composite_frag(wpd);
|
2018-04-25 11:54:02 +02:00
|
|
|
char *prepass_frag = workbench_build_prepass_frag();
|
|
|
|
|
e_data.prepass_sh_cache[index] = DRW_shader_create(datatoc_workbench_prepass_vert_glsl, NULL, prepass_frag, defines);
|
2018-04-25 10:59:48 +02:00
|
|
|
e_data.composite_sh_cache[index] = DRW_shader_create_fullscreen(composite_frag, defines);
|
2018-04-25 11:54:02 +02:00
|
|
|
MEM_freeN(prepass_frag);
|
2018-04-25 10:59:48 +02:00
|
|
|
MEM_freeN(composite_frag);
|
2018-04-25 11:54:02 +02:00
|
|
|
MEM_freeN(defines);
|
2018-04-20 14:38:55 +02:00
|
|
|
}
|
2018-04-25 10:59:48 +02:00
|
|
|
|
|
|
|
|
wpd->prepass_sh = e_data.prepass_sh_cache[index];
|
|
|
|
|
wpd->composite_sh = e_data.composite_sh_cache[index];
|
2018-04-18 13:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
/* Functions */
|
|
|
|
|
static uint get_material_hash(WORKBENCH_PrivateData *wpd, WORKBENCH_MaterialData *material_template)
|
2018-04-19 07:47:03 +02:00
|
|
|
{
|
2018-04-25 10:59:48 +02:00
|
|
|
uint input[4];
|
|
|
|
|
float *color = material_template->color;
|
|
|
|
|
input[0] = (uint)(color[0] * 512);
|
|
|
|
|
input[1] = (uint)(color[1] * 512);
|
|
|
|
|
input[2] = (uint)(color[2] * 512);
|
2018-04-18 13:44:33 +02:00
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
/* Only hash object id when needed */
|
|
|
|
|
input[3] = (uint)0;
|
|
|
|
|
if (OBJECT_ID_PASS_ENABLED(wpd)) {
|
|
|
|
|
input[3] = material_template->object_id;
|
2018-04-18 13:44:33 +02:00
|
|
|
}
|
2018-04-25 10:59:48 +02:00
|
|
|
|
|
|
|
|
return BLI_ghashutil_uinthash_v4_murmur(input);
|
2018-04-18 13:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
static void workbench_init_object_data(ObjectEngineData *engine_data)
|
2018-04-19 07:47:03 +02:00
|
|
|
{
|
2018-04-28 09:01:34 +02:00
|
|
|
WORKBENCH_ObjectData *data = (WORKBENCH_ObjectData *)engine_data;
|
2018-04-25 10:59:48 +02:00
|
|
|
data->object_id = e_data.next_object_id++;
|
|
|
|
|
}
|
2018-04-19 10:41:24 +02:00
|
|
|
|
2018-05-02 12:05:40 +02:00
|
|
|
static void get_material_solid_color(WORKBENCH_PrivateData *wpd, WORKBENCH_ObjectData *engine_object_data, Object *ob, Material *mat, float *color, float hsv_saturation, float hsv_value)
|
2018-04-25 10:59:48 +02:00
|
|
|
{
|
2018-04-30 15:50:08 +02:00
|
|
|
static float default_color[] = {1.0f, 1.0f, 1.0f};
|
2018-05-02 12:05:40 +02:00
|
|
|
if (DRW_object_is_paint_mode(ob) || wpd->drawtype_options & V3D_DRAWOPTION_SINGLE_COLOR) {
|
2018-04-30 15:50:08 +02:00
|
|
|
copy_v3_v3(color, default_color);
|
|
|
|
|
}
|
|
|
|
|
else if (wpd->drawtype_options & V3D_DRAWOPTION_RANDOMIZE) {
|
2018-04-25 10:59:48 +02:00
|
|
|
float offset = fmodf(engine_object_data->object_id * M_GOLDEN_RATION_CONJUGATE, 1.0);
|
|
|
|
|
float hsv[3] = {offset, hsv_saturation, hsv_value};
|
|
|
|
|
hsv_to_rgb_v(hsv, color);
|
|
|
|
|
}
|
2018-05-02 12:05:40 +02:00
|
|
|
else if (wpd->drawtype_options & V3D_DRAWOPTION_OBJECT_COLOR) {
|
2018-04-25 10:59:48 +02:00
|
|
|
copy_v3_v3(color, ob->col);
|
|
|
|
|
}
|
2018-05-02 12:05:40 +02:00
|
|
|
else {
|
|
|
|
|
/* V3D_DRAWOPTION_MATERIAL_COLOR */
|
|
|
|
|
if (mat) {
|
|
|
|
|
copy_v3_v3(color, &mat->r);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
copy_v3_v3(color, default_color);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-19 10:41:24 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
void workbench_materials_engine_init(WORKBENCH_Data *vedata)
|
2018-04-19 10:41:24 +02:00
|
|
|
{
|
2018-04-25 10:59:48 +02:00
|
|
|
WORKBENCH_FramebufferList *fbl = vedata->fbl;
|
2018-04-19 10:41:24 +02:00
|
|
|
WORKBENCH_StorageList *stl = vedata->stl;
|
2018-04-25 10:59:48 +02:00
|
|
|
WORKBENCH_PassList *psl = vedata->psl;
|
|
|
|
|
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
|
2018-04-19 10:41:24 +02:00
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
if (!e_data.next_object_id) {
|
|
|
|
|
memset(e_data.prepass_sh_cache, 0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
|
|
|
|
|
memset(e_data.composite_sh_cache, 0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
|
|
|
|
|
e_data.next_object_id = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!stl->g_data) {
|
|
|
|
|
/* Alloc transient pointers */
|
|
|
|
|
stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
const float *viewport_size = DRW_viewport_size_get();
|
|
|
|
|
const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
|
2018-04-30 16:02:24 +02:00
|
|
|
e_data.object_id_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_R32UI, &draw_engine_workbench_solid);
|
|
|
|
|
e_data.color_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8, &draw_engine_workbench_solid);
|
2018-04-25 16:57:18 +02:00
|
|
|
#ifdef WORKBENCH_ENCODE_NORMALS
|
2018-04-30 16:02:24 +02:00
|
|
|
e_data.normal_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RG8, &draw_engine_workbench_solid);
|
2018-04-25 16:57:18 +02:00
|
|
|
#else
|
2018-04-30 16:02:24 +02:00
|
|
|
e_data.normal_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA32F, &draw_engine_workbench_solid);
|
2018-04-25 16:57:18 +02:00
|
|
|
#endif
|
2018-04-25 10:59:48 +02:00
|
|
|
|
|
|
|
|
GPU_framebuffer_ensure_config(&fbl->prepass_fb, {
|
|
|
|
|
GPU_ATTACHMENT_TEXTURE(dtxl->depth),
|
|
|
|
|
GPU_ATTACHMENT_TEXTURE(e_data.object_id_tx),
|
2018-04-25 11:54:02 +02:00
|
|
|
GPU_ATTACHMENT_TEXTURE(e_data.color_buffer_tx),
|
|
|
|
|
GPU_ATTACHMENT_TEXTURE(e_data.normal_buffer_tx),
|
2018-04-25 10:59:48 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Prepass */
|
|
|
|
|
{
|
|
|
|
|
int state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
|
|
|
|
|
psl->prepass_pass = DRW_pass_create("Prepass", state);
|
|
|
|
|
}
|
2018-04-18 13:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
void workbench_materials_engine_free()
|
|
|
|
|
{
|
|
|
|
|
for (int index = 0; index < MAX_SHADERS; index ++) {
|
|
|
|
|
DRW_SHADER_FREE_SAFE(e_data.prepass_sh_cache[index]);
|
|
|
|
|
DRW_SHADER_FREE_SAFE(e_data.composite_sh_cache[index]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-19 10:41:24 +02:00
|
|
|
|
2018-04-19 07:47:03 +02:00
|
|
|
void workbench_materials_cache_init(WORKBENCH_Data *vedata)
|
|
|
|
|
{
|
|
|
|
|
WORKBENCH_StorageList *stl = vedata->stl;
|
|
|
|
|
WORKBENCH_PassList *psl = vedata->psl;
|
|
|
|
|
WORKBENCH_PrivateData *wpd = stl->g_data;
|
2018-04-25 10:59:48 +02:00
|
|
|
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
|
|
|
|
|
DRWShadingGroup *grp;
|
2018-04-25 15:59:15 +02:00
|
|
|
const DRWContextState *draw_ctx = DRW_context_state_get();
|
|
|
|
|
ViewLayer *view_layer = draw_ctx->view_layer;
|
|
|
|
|
IDProperty *props = BKE_view_layer_engine_evaluated_get(view_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
|
2018-04-19 07:47:03 +02:00
|
|
|
|
2018-04-19 12:44:37 +02:00
|
|
|
const DRWContextState *DCS = DRW_context_state_get();
|
|
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
wpd->material_hash = BLI_ghash_ptr_new(__func__);
|
2018-04-19 12:44:37 +02:00
|
|
|
|
|
|
|
|
View3D *v3d = DCS->v3d;
|
|
|
|
|
if (v3d) {
|
2018-04-20 13:40:18 +02:00
|
|
|
wpd->drawtype_lighting = v3d->drawtype_lighting;
|
2018-04-20 14:38:55 +02:00
|
|
|
wpd->drawtype_options = v3d->drawtype_options;
|
2018-04-19 12:44:37 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
wpd->drawtype_lighting = V3D_LIGHTING_STUDIO;
|
2018-04-20 14:38:55 +02:00
|
|
|
wpd->drawtype_options = 0;
|
2018-04-19 12:44:37 +02:00
|
|
|
}
|
2018-04-25 10:59:48 +02:00
|
|
|
|
|
|
|
|
select_deferred_shaders(wpd);
|
|
|
|
|
/* Deferred Mix Pass */
|
|
|
|
|
{
|
|
|
|
|
WORKBENCH_UBO_World *wd = &wpd->world_data;
|
2018-04-28 09:01:34 +02:00
|
|
|
UI_GetThemeColor3fv(UI_GetThemeValue(TH_SHOW_BACK_GRAD) ? TH_LOW_GRAD:TH_HIGH_GRAD, wd->background_color_low);
|
2018-04-25 10:59:48 +02:00
|
|
|
UI_GetThemeColor3fv(TH_HIGH_GRAD, wd->background_color_high);
|
2018-04-25 15:59:15 +02:00
|
|
|
copy_v3_v3(wd->diffuse_light_x_pos, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_x_pos"));
|
|
|
|
|
copy_v3_v3(wd->diffuse_light_x_neg, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_x_neg"));
|
|
|
|
|
copy_v3_v3(wd->diffuse_light_y_pos, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_y_pos"));
|
|
|
|
|
copy_v3_v3(wd->diffuse_light_y_neg, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_y_neg"));
|
|
|
|
|
copy_v3_v3(wd->diffuse_light_z_pos, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_z_pos"));
|
|
|
|
|
copy_v3_v3(wd->diffuse_light_z_neg, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_z_neg"));
|
2018-04-25 10:59:48 +02:00
|
|
|
|
|
|
|
|
psl->composite_pass = DRW_pass_create("Composite", DRW_STATE_WRITE_COLOR);
|
|
|
|
|
grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
|
2018-04-25 11:54:02 +02:00
|
|
|
DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
|
|
|
|
|
DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &e_data.color_buffer_tx);
|
2018-04-25 10:59:48 +02:00
|
|
|
if (OBJECT_ID_PASS_ENABLED(wpd)) {
|
|
|
|
|
DRW_shgroup_uniform_texture_ref(grp, "objectId", &e_data.object_id_tx);
|
|
|
|
|
}
|
|
|
|
|
if (NORMAL_VIEWPORT_PASS_ENABLED(wpd)) {
|
2018-04-25 11:54:02 +02:00
|
|
|
DRW_shgroup_uniform_texture_ref(grp, "normalBuffer", &e_data.normal_buffer_tx);
|
2018-04-25 10:59:48 +02:00
|
|
|
}
|
|
|
|
|
wpd->world_ubo = DRW_uniformbuffer_create(sizeof(WORKBENCH_UBO_World), NULL);
|
|
|
|
|
DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo);
|
|
|
|
|
DRW_uniformbuffer_update(wpd->world_ubo, &wpd->world_data);
|
|
|
|
|
DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
|
|
|
|
|
|
|
|
|
|
DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-04-18 13:44:33 +02:00
|
|
|
}
|
2018-05-02 12:05:40 +02:00
|
|
|
static WORKBENCH_MaterialData *get_or_create_material_data(WORKBENCH_Data *vedata, IDProperty *props, Object *ob, Material *mat)
|
2018-04-19 09:45:52 +02:00
|
|
|
{
|
|
|
|
|
WORKBENCH_StorageList *stl = vedata->stl;
|
|
|
|
|
WORKBENCH_PassList *psl = vedata->psl;
|
|
|
|
|
WORKBENCH_PrivateData *wpd = stl->g_data;
|
2018-04-25 13:00:18 +02:00
|
|
|
WORKBENCH_MaterialData *material;
|
2018-04-28 09:01:34 +02:00
|
|
|
WORKBENCH_ObjectData *engine_object_data = (WORKBENCH_ObjectData *)DRW_object_engine_data_ensure(
|
|
|
|
|
ob, &draw_engine_workbench_solid, sizeof(WORKBENCH_ObjectData), &workbench_init_object_data, NULL);
|
2018-04-25 13:00:18 +02:00
|
|
|
WORKBENCH_MaterialData material_template;
|
|
|
|
|
float color[3];
|
|
|
|
|
const float hsv_saturation = BKE_collection_engine_property_value_get_float(props, "random_object_color_saturation");
|
|
|
|
|
const float hsv_value = BKE_collection_engine_property_value_get_float(props, "random_object_color_value");
|
|
|
|
|
|
|
|
|
|
/* Solid */
|
2018-05-02 12:05:40 +02:00
|
|
|
get_material_solid_color(wpd, engine_object_data, ob, mat, color, hsv_saturation, hsv_value);
|
2018-04-25 13:00:18 +02:00
|
|
|
copy_v3_v3(material_template.color, color);
|
|
|
|
|
material_template.object_id = engine_object_data->object_id;
|
|
|
|
|
unsigned int hash = get_material_hash(wpd, &material_template);
|
|
|
|
|
|
|
|
|
|
material = BLI_ghash_lookup(wpd->material_hash, SET_UINT_IN_POINTER(hash));
|
|
|
|
|
if (material == NULL) {
|
|
|
|
|
material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), __func__);
|
|
|
|
|
material->shgrp = DRW_shgroup_create(wpd->prepass_sh, psl->prepass_pass);
|
|
|
|
|
material->object_id = engine_object_data->object_id;
|
|
|
|
|
copy_v3_v3(material->color, material_template.color);
|
|
|
|
|
DRW_shgroup_uniform_vec3(material->shgrp, "object_color", material->color, 1);
|
|
|
|
|
DRW_shgroup_uniform_int(material->shgrp, "object_id", &material->object_id, 1);
|
|
|
|
|
BLI_ghash_insert(wpd->material_hash, SET_UINT_IN_POINTER(hash), material);
|
|
|
|
|
}
|
|
|
|
|
return material;
|
|
|
|
|
}
|
2018-04-19 09:45:52 +02:00
|
|
|
|
2018-04-25 13:00:18 +02:00
|
|
|
static void workbench_cache_populate_particles(WORKBENCH_Data *vedata, IDProperty *props, Object *ob)
|
|
|
|
|
{
|
|
|
|
|
const DRWContextState *draw_ctx = DRW_context_state_get();
|
|
|
|
|
|
|
|
|
|
if (ob != draw_ctx->object_edit) {
|
2018-04-26 08:46:58 +02:00
|
|
|
for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
|
|
|
|
|
if (md->type == eModifierType_ParticleSystem) {
|
|
|
|
|
ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
|
|
|
|
|
|
|
|
|
|
if (psys_check_enabled(ob, psys, false)) {
|
|
|
|
|
ParticleSettings *part = psys->part;
|
|
|
|
|
int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
|
|
|
|
|
|
|
|
|
|
if (draw_as == PART_DRAW_PATH && !psys->pathcache && !psys->childcache) {
|
|
|
|
|
draw_as = PART_DRAW_DOT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static float mat[4][4];
|
|
|
|
|
unit_m4(mat);
|
|
|
|
|
|
|
|
|
|
if (draw_as == PART_DRAW_PATH) {
|
|
|
|
|
struct Gwn_Batch *geom = DRW_cache_particles_get_hair(psys, NULL);
|
2018-05-02 12:05:40 +02:00
|
|
|
WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, props, ob, NULL);
|
2018-04-26 08:46:58 +02:00
|
|
|
DRW_shgroup_call_add(material->shgrp, geom, mat);
|
|
|
|
|
}
|
2018-04-25 13:00:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void workbench_materials_solid_cache_populate(WORKBENCH_Data *vedata, Object *ob)
|
|
|
|
|
{
|
2018-04-19 09:45:52 +02:00
|
|
|
if (!DRW_object_is_renderable(ob))
|
|
|
|
|
return;
|
|
|
|
|
|
2018-04-25 13:00:18 +02:00
|
|
|
IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
|
|
|
|
|
if (ob->type == OB_MESH) {
|
|
|
|
|
workbench_cache_populate_particles(vedata, props, ob);
|
|
|
|
|
}
|
2018-05-02 12:05:40 +02:00
|
|
|
|
2018-04-19 09:45:52 +02:00
|
|
|
WORKBENCH_MaterialData *material;
|
2018-05-02 12:05:40 +02:00
|
|
|
if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
|
2018-04-25 10:59:48 +02:00
|
|
|
const DRWContextState *draw_ctx = DRW_context_state_get();
|
|
|
|
|
const bool is_active = (ob == draw_ctx->obact);
|
|
|
|
|
const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
|
2018-05-02 12:05:40 +02:00
|
|
|
|
|
|
|
|
if ((vedata->stl->g_data->drawtype_options & V3D_DRAWOPTION_SOLID_COLOR_MASK) != 0 || is_sculpt_mode) {
|
|
|
|
|
/* No material split needed */
|
|
|
|
|
struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
|
|
|
|
|
if (geom) {
|
|
|
|
|
material = get_or_create_material_data(vedata, props, ob, NULL);
|
|
|
|
|
if (is_sculpt_mode) {
|
|
|
|
|
DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
DRW_shgroup_call_object_add(material->shgrp, geom, ob);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else { /* MATERIAL colors */
|
|
|
|
|
const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
|
|
|
|
|
struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
|
|
|
|
|
for (int i = 0; i < materials_len; i ++) {
|
|
|
|
|
gpumat_array[i] = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 17:08:42 +02:00
|
|
|
struct Gwn_Batch **mat_geom = DRW_cache_object_surface_material_get(ob, gpumat_array, materials_len, NULL, NULL, NULL);
|
2018-05-02 12:05:40 +02:00
|
|
|
if (mat_geom) {
|
|
|
|
|
for (int i = 0; i < materials_len; ++i) {
|
|
|
|
|
Material *mat = give_current_material(ob, i + 1);
|
|
|
|
|
material = get_or_create_material_data(vedata, props, ob, mat);
|
|
|
|
|
DRW_shgroup_call_object_add(material->shgrp, mat_geom[i], ob);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-25 10:59:48 +02:00
|
|
|
}
|
2018-04-19 09:45:52 +02:00
|
|
|
}
|
2018-04-25 10:59:48 +02:00
|
|
|
}
|
2018-04-19 09:45:52 +02:00
|
|
|
|
2018-04-25 10:59:48 +02:00
|
|
|
void workbench_materials_cache_finish(WORKBENCH_Data *UNUSED(vedata))
|
|
|
|
|
{
|
2018-04-19 09:45:52 +02:00
|
|
|
}
|
2018-04-25 10:59:48 +02:00
|
|
|
|
|
|
|
|
void workbench_materials_draw_scene(WORKBENCH_Data *vedata)
|
|
|
|
|
{
|
|
|
|
|
WORKBENCH_PassList *psl = vedata->psl;
|
|
|
|
|
WORKBENCH_StorageList *stl = vedata->stl;
|
|
|
|
|
WORKBENCH_FramebufferList *fbl = vedata->fbl;
|
|
|
|
|
WORKBENCH_PrivateData *wpd = stl->g_data;
|
|
|
|
|
DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
|
|
|
|
|
const float clear_depth = 1.0f;
|
|
|
|
|
|
|
|
|
|
GPU_framebuffer_bind(fbl->prepass_fb);
|
|
|
|
|
if (OBJECT_ID_PASS_ENABLED(wpd)) {
|
|
|
|
|
const float clear_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
|
|
|
|
|
GPU_framebuffer_clear_color_depth(fbl->prepass_fb, clear_color, clear_depth);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
GPU_framebuffer_clear_depth(fbl->prepass_fb, clear_depth);
|
|
|
|
|
}
|
|
|
|
|
DRW_draw_pass(psl->prepass_pass);
|
|
|
|
|
|
|
|
|
|
GPU_framebuffer_bind(dfbl->color_only_fb);
|
|
|
|
|
DRW_draw_pass(psl->composite_pass);
|
|
|
|
|
|
|
|
|
|
BLI_ghash_free(wpd->material_hash, NULL, MEM_freeN);
|
|
|
|
|
DRW_UBO_FREE_SAFE(wpd->world_ubo);
|
|
|
|
|
|
|
|
|
|
}
|