Workbench: reuse similar materials
Similar materials will reuse the same shadergroup. Currently using a custom hash function that might select too similar colors into the same material. Reintroduced the workbench_materials.c this file will be responsible for material lookup/creation and shader compilation Fixed a GPUShader mem leak
This commit is contained in:
@@ -105,6 +105,7 @@ set(SRC
|
||||
engines/eevee/eevee_temporal_sampling.c
|
||||
engines/eevee/eevee_volumes.c
|
||||
engines/workbench/workbench_engine.c
|
||||
engines/workbench/workbench_materials.c
|
||||
engines/workbench/solid_flat_mode.c
|
||||
engines/workbench/solid_studio_mode.c
|
||||
engines/external/external_engine.c
|
||||
@@ -208,6 +209,7 @@ data_to_c_simple(engines/eevee/shaders/volumetric_integration_frag.glsl SRC)
|
||||
|
||||
data_to_c_simple(engines/workbench/shaders/solid_flat_frag.glsl SRC)
|
||||
data_to_c_simple(engines/workbench/shaders/solid_studio_frag.glsl SRC)
|
||||
data_to_c_simple(engines/workbench/shaders/workbench_diffuse_lib.glsl SRC)
|
||||
data_to_c_simple(engines/workbench/shaders/workbench_vert.glsl SRC)
|
||||
data_to_c_simple(engines/workbench/shaders/workbench_studio_vert.glsl SRC)
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ out vec4 fragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
float intensity = dot(normal, vec3(0.0, 0.0, 1.0));
|
||||
float intensity = lambert_diffuse(vec3(0.0, 0.0, 1.0), normal);
|
||||
vec3 shaded_color = color * intensity;
|
||||
fragColor = vec4(shaded_color, 1.0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
float lambert_diffuse(vec3 light_direction, vec3 surface_normal) {
|
||||
return max(0.0, dot(light_direction, surface_normal));
|
||||
}
|
||||
@@ -6,7 +6,6 @@ in vec3 nor;
|
||||
|
||||
out vec3 normal;
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
normal = normalize(NormalMatrix * nor);
|
||||
|
||||
@@ -31,38 +31,15 @@
|
||||
#include "GPU_shader.h"
|
||||
|
||||
#include "workbench_private.h"
|
||||
/* Shaders */
|
||||
|
||||
extern char datatoc_solid_flat_frag_glsl[];
|
||||
extern char datatoc_workbench_vert_glsl[];
|
||||
|
||||
/* *********** STATIC *********** */
|
||||
static struct {
|
||||
struct GPUShader *depth_sh;
|
||||
|
||||
/* Shading Pass */
|
||||
struct GPUShader *solid_sh;
|
||||
|
||||
} e_data = {NULL};
|
||||
|
||||
|
||||
/* Functions */
|
||||
|
||||
static void workbench_solid_flat_engine_init(void *UNUSED(vedata))
|
||||
{
|
||||
if (!e_data.depth_sh) {
|
||||
/* Depth pass */
|
||||
e_data.depth_sh = DRW_shader_create_3D_depth_only();
|
||||
|
||||
/* Shading pass */
|
||||
e_data.solid_sh = DRW_shader_create(
|
||||
datatoc_workbench_vert_glsl, NULL, datatoc_solid_flat_frag_glsl, "\n");
|
||||
}
|
||||
workbench_materials_engine_init();
|
||||
}
|
||||
|
||||
static void workbench_solid_flat_cache_init(void *vedata)
|
||||
{
|
||||
|
||||
WORKBENCH_Data * data = (WORKBENCH_Data *)vedata;
|
||||
WORKBENCH_PassList *psl = data->psl;
|
||||
WORKBENCH_StorageList *stl = data->stl;
|
||||
@@ -76,7 +53,6 @@ static void workbench_solid_flat_cache_init(void *vedata)
|
||||
{
|
||||
int state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
|
||||
psl->depth_pass = DRW_pass_create("Depth Pass", state);
|
||||
stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass);
|
||||
}
|
||||
|
||||
/* Solid Pass */
|
||||
@@ -84,13 +60,14 @@ static void workbench_solid_flat_cache_init(void *vedata)
|
||||
int state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL;
|
||||
psl->solid_pass = DRW_pass_create("Solid Pass", state);
|
||||
}
|
||||
|
||||
workbench_materials_cache_init(data);
|
||||
}
|
||||
|
||||
|
||||
static void workbench_solid_flat_cache_populate(void *vedata, Object *ob)
|
||||
{
|
||||
WORKBENCH_Data * data = (WORKBENCH_Data *)vedata;
|
||||
|
||||
WORKBENCH_PassList *psl = data->psl;
|
||||
WORKBENCH_StorageList *stl = data->stl;
|
||||
|
||||
IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
|
||||
@@ -100,26 +77,26 @@ static void workbench_solid_flat_cache_populate(void *vedata, Object *ob)
|
||||
return;
|
||||
|
||||
struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
|
||||
DRWShadingGroup *grp;
|
||||
WORKBENCH_MaterialData *material;
|
||||
if (geom) {
|
||||
/* Depth */
|
||||
DRW_shgroup_call_add(stl->g_data->depth_shgrp, geom, ob->obmat);
|
||||
|
||||
/* Solid */
|
||||
grp = DRW_shgroup_create(e_data.solid_sh, psl->solid_pass);
|
||||
DRW_shgroup_uniform_vec3(grp, "color", color, 1);
|
||||
DRW_shgroup_call_add(grp, geom, ob->obmat);
|
||||
material = workbench_get_or_create_solid_flat_material_data(data, color);
|
||||
DRW_shgroup_call_add(material->shgrp, geom, ob->obmat);
|
||||
}
|
||||
}
|
||||
|
||||
static void workbench_solid_flat_cache_finish(void *UNUSED(vedata))
|
||||
static void workbench_solid_flat_cache_finish(void *vedata)
|
||||
{
|
||||
workbench_materials_cache_finish((WORKBENCH_Data*)vedata);
|
||||
}
|
||||
|
||||
static void workbench_solid_flat_draw_scene(void *vedata)
|
||||
{
|
||||
// WORKBENCH_Data *data = (WORKBENCH_Data *)vedata;
|
||||
WORKBENCH_PassList *psl = ((WORKBENCH_Data *)vedata)->psl;
|
||||
WORKBENCH_Data *data = (WORKBENCH_Data *)vedata;
|
||||
WORKBENCH_PassList *psl = data->psl;
|
||||
|
||||
DRW_draw_pass(psl->depth_pass);
|
||||
DRW_draw_pass(psl->solid_pass);
|
||||
@@ -127,7 +104,7 @@ static void workbench_solid_flat_draw_scene(void *vedata)
|
||||
|
||||
static void workbench_solid_flat_engine_free(void)
|
||||
{
|
||||
DRW_SHADER_FREE_SAFE(e_data.solid_sh);
|
||||
workbench_materials_engine_finish();
|
||||
}
|
||||
|
||||
static const DrawEngineDataSize workbench_data_size = DRW_VIEWPORT_DATA_SIZE(WORKBENCH_Data);
|
||||
|
||||
@@ -31,33 +31,12 @@
|
||||
#include "GPU_shader.h"
|
||||
|
||||
#include "workbench_private.h"
|
||||
/* Shaders */
|
||||
|
||||
extern char datatoc_solid_studio_frag_glsl[];
|
||||
extern char datatoc_workbench_studio_vert_glsl[];
|
||||
|
||||
/* *********** STATIC *********** */
|
||||
static struct {
|
||||
struct GPUShader *depth_sh;
|
||||
|
||||
/* Shading Pass */
|
||||
struct GPUShader *solid_sh;
|
||||
|
||||
} e_data = {NULL};
|
||||
|
||||
|
||||
/* Functions */
|
||||
|
||||
static void workbench_solid_studio_engine_init(void *UNUSED(vedata))
|
||||
{
|
||||
if (!e_data.depth_sh) {
|
||||
/* Depth pass */
|
||||
e_data.depth_sh = DRW_shader_create_3D_depth_only();
|
||||
|
||||
/* Shading pass */
|
||||
e_data.solid_sh = DRW_shader_create(
|
||||
datatoc_workbench_studio_vert_glsl, NULL, datatoc_solid_studio_frag_glsl, "\n");
|
||||
}
|
||||
workbench_materials_engine_init();
|
||||
}
|
||||
|
||||
static void workbench_solid_studio_cache_init(void *vedata)
|
||||
@@ -76,7 +55,6 @@ static void workbench_solid_studio_cache_init(void *vedata)
|
||||
{
|
||||
int state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
|
||||
psl->depth_pass = DRW_pass_create("Depth Pass", state);
|
||||
stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass);
|
||||
}
|
||||
|
||||
/* Solid Pass */
|
||||
@@ -84,13 +62,12 @@ static void workbench_solid_studio_cache_init(void *vedata)
|
||||
int state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL;
|
||||
psl->solid_pass = DRW_pass_create("Solid Pass", state);
|
||||
}
|
||||
workbench_materials_cache_init(data);
|
||||
}
|
||||
|
||||
static void workbench_solid_studio_cache_populate(void *vedata, Object *ob)
|
||||
{
|
||||
WORKBENCH_Data * data = (WORKBENCH_Data *)vedata;
|
||||
|
||||
WORKBENCH_PassList *psl = data->psl;
|
||||
WORKBENCH_StorageList *stl = data->stl;
|
||||
|
||||
IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
|
||||
@@ -100,26 +77,26 @@ static void workbench_solid_studio_cache_populate(void *vedata, Object *ob)
|
||||
return;
|
||||
|
||||
struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
|
||||
DRWShadingGroup *grp;
|
||||
WORKBENCH_MaterialData *material;
|
||||
if (geom) {
|
||||
/* Depth */
|
||||
DRW_shgroup_call_add(stl->g_data->depth_shgrp, geom, ob->obmat);
|
||||
|
||||
/* Solid */
|
||||
grp = DRW_shgroup_create(e_data.solid_sh, psl->solid_pass);
|
||||
DRW_shgroup_uniform_vec3(grp, "color", color, 1);
|
||||
DRW_shgroup_call_add(grp, geom, ob->obmat);
|
||||
material = workbench_get_or_create_solid_studio_material_data(data, color);
|
||||
DRW_shgroup_call_add(material->shgrp, geom, ob->obmat);
|
||||
}
|
||||
}
|
||||
|
||||
static void workbench_solid_studio_cache_finish(void *UNUSED(vedata))
|
||||
static void workbench_solid_studio_cache_finish(void *vedata)
|
||||
{
|
||||
workbench_materials_cache_finish((WORKBENCH_Data*)vedata);
|
||||
}
|
||||
|
||||
static void workbench_solid_studio_draw_scene(void *vedata)
|
||||
{
|
||||
WORKBENCH_Data * data = (WORKBENCH_Data *)vedata;
|
||||
WORKBENCH_PassList *psl = ((WORKBENCH_Data *)vedata)->psl;
|
||||
WORKBENCH_PassList *psl = data->psl;
|
||||
|
||||
DRW_draw_pass(psl->depth_pass);
|
||||
DRW_draw_pass(psl->solid_pass);
|
||||
@@ -127,7 +104,7 @@ static void workbench_solid_studio_draw_scene(void *vedata)
|
||||
|
||||
static void workbench_solid_studio_engine_free(void)
|
||||
{
|
||||
DRW_SHADER_FREE_SAFE(e_data.solid_sh);
|
||||
workbench_materials_engine_finish();
|
||||
}
|
||||
|
||||
static const DrawEngineDataSize workbench_data_size = DRW_VIEWPORT_DATA_SIZE(WORKBENCH_Data);
|
||||
|
||||
128
source/blender/draw/engines/workbench/workbench_materials.c
Normal file
128
source/blender/draw/engines/workbench/workbench_materials.c
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include "workbench_private.h"
|
||||
#include "GPU_shader.h"
|
||||
|
||||
/* *********** STATIC *********** */
|
||||
static struct {
|
||||
struct GPUShader *depth_sh;
|
||||
|
||||
/* Solid flat mode */
|
||||
struct GPUShader *solid_flat_sh;
|
||||
|
||||
/* Solid studio mode */
|
||||
struct GPUShader *solid_studio_sh;
|
||||
|
||||
} e_data = {NULL};
|
||||
|
||||
/* Shaders */
|
||||
extern char datatoc_solid_flat_frag_glsl[];
|
||||
extern char datatoc_solid_studio_frag_glsl[];
|
||||
extern char datatoc_workbench_vert_glsl[];
|
||||
extern char datatoc_workbench_studio_vert_glsl[];
|
||||
extern char datatoc_workbench_diffuse_lib_glsl[];
|
||||
|
||||
/* Functions */
|
||||
static uint get_material_hash(const float color[3]) {
|
||||
uint r = (uint)(color[0] * 512);
|
||||
uint g = (uint)(color[1] * 512);
|
||||
uint b = (uint)(color[2] * 512);
|
||||
|
||||
return r + g * 4096 + b * 4096*4096;
|
||||
}
|
||||
|
||||
WORKBENCH_MaterialData* workbench_get_or_create_solid_flat_material_data(WORKBENCH_Data *vedata, const float color[3]) {
|
||||
WORKBENCH_StorageList *stl = vedata->stl;
|
||||
WORKBENCH_PassList *psl = vedata->psl;
|
||||
WORKBENCH_PrivateData* wpd = stl->g_data;
|
||||
|
||||
uint hash = get_material_hash(color);
|
||||
WORKBENCH_MaterialData *material;
|
||||
|
||||
material = BLI_ghash_lookup(wpd->material_hash, (void *)hash);
|
||||
if (material == NULL) {
|
||||
material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), "WORKBENCH_MaterialData");
|
||||
material->shgrp = DRW_shgroup_create(e_data.solid_flat_sh, psl->solid_pass);
|
||||
material->color[0] = color[0];
|
||||
material->color[1] = color[1];
|
||||
material->color[2] = color[2];
|
||||
DRW_shgroup_uniform_vec3(material->shgrp, "color", material->color, 1);
|
||||
BLI_ghash_insert(wpd->material_hash, (void *)hash, material);
|
||||
}
|
||||
return material;
|
||||
}
|
||||
|
||||
WORKBENCH_MaterialData* workbench_get_or_create_solid_studio_material_data(WORKBENCH_Data *vedata, const float color[3]) {
|
||||
WORKBENCH_StorageList *stl = vedata->stl;
|
||||
WORKBENCH_PassList *psl = vedata->psl;
|
||||
WORKBENCH_PrivateData* wpd = stl->g_data;
|
||||
|
||||
uint hash = get_material_hash(color);
|
||||
WORKBENCH_MaterialData *material;
|
||||
|
||||
material = BLI_ghash_lookup(wpd->material_hash, (void *)hash);
|
||||
if (material == NULL) {
|
||||
material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), "WORKBENCH_MaterialData");
|
||||
material->shgrp = DRW_shgroup_create(e_data.solid_studio_sh, psl->solid_pass);
|
||||
material->color[0] = color[0];
|
||||
material->color[1] = color[1];
|
||||
material->color[2] = color[2];
|
||||
DRW_shgroup_uniform_vec3(material->shgrp, "color", material->color, 1);
|
||||
BLI_ghash_insert(wpd->material_hash, (void *)hash, material);
|
||||
}
|
||||
return material;
|
||||
}
|
||||
|
||||
void workbench_materials_engine_init(void) {
|
||||
if (!e_data.depth_sh) {
|
||||
/* Depth pass */
|
||||
e_data.depth_sh = DRW_shader_create_3D_depth_only();
|
||||
|
||||
/* Solid flat */
|
||||
e_data.solid_flat_sh = DRW_shader_create(datatoc_workbench_vert_glsl, NULL, datatoc_solid_flat_frag_glsl, "\n");
|
||||
e_data.solid_studio_sh = DRW_shader_create(datatoc_workbench_studio_vert_glsl, NULL, datatoc_solid_studio_frag_glsl, datatoc_workbench_diffuse_lib_glsl);
|
||||
}
|
||||
}
|
||||
|
||||
void workbench_materials_engine_finish(void) {
|
||||
DRW_SHADER_FREE_SAFE(e_data.solid_flat_sh);
|
||||
DRW_SHADER_FREE_SAFE(e_data.solid_studio_sh);
|
||||
}
|
||||
|
||||
void workbench_materials_cache_init(WORKBENCH_Data *vedata) {
|
||||
WORKBENCH_StorageList* stl = vedata->stl;
|
||||
WORKBENCH_PassList* psl = vedata->psl;
|
||||
WORKBENCH_PrivateData* wpd = stl->g_data;
|
||||
|
||||
wpd->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass);
|
||||
wpd->material_hash = BLI_ghash_ptr_new("Workbench material_hash");
|
||||
}
|
||||
|
||||
void workbench_materials_cache_finish(WORKBENCH_Data *vedata) {
|
||||
WORKBENCH_StorageList* stl = vedata->stl;
|
||||
WORKBENCH_PrivateData* wpd = stl->g_data;
|
||||
BLI_ghash_free(wpd->material_hash, NULL, MEM_freeN);
|
||||
}
|
||||
@@ -61,12 +61,19 @@ typedef struct WORKBENCH_PrivateData {
|
||||
|
||||
DRWShadingGroup *shadeless_shgrp;
|
||||
|
||||
// Lighting passes
|
||||
DRWShadingGroup *flat_lighting_shgrp;
|
||||
struct GHash *material_hash;
|
||||
} WORKBENCH_PrivateData; /* Transient data */
|
||||
|
||||
typedef struct WORKBENCH_MaterialData {
|
||||
// Solid color
|
||||
float color[3];
|
||||
|
||||
// Linked shgroup for drawing
|
||||
DRWShadingGroup * shgrp;
|
||||
} WORKBENCH_MaterialData;
|
||||
|
||||
/* workbench_materials.c */
|
||||
|
||||
/* workbench_engine.c */
|
||||
void workbench_solid_materials_init(void);
|
||||
void workbench_solid_materials_cache_init(WORKBENCH_Data* vedata);
|
||||
void workbench_solid_materials_cache_populate(WORKBENCH_Data* vedata, Object* ob);
|
||||
@@ -74,5 +81,13 @@ void workbench_solid_materials_cache_finish(WORKBENCH_Data* vedata);
|
||||
void workbench_solid_materials_draw_scene(WORKBENCH_Data* vedata);
|
||||
void workbench_solid_materials_free(void);
|
||||
|
||||
/* workbench_materials.c */
|
||||
void workbench_materials_engine_init(void);
|
||||
void workbench_materials_engine_finish(void);
|
||||
void workbench_materials_cache_init(WORKBENCH_Data *vedata);
|
||||
WORKBENCH_MaterialData* workbench_get_or_create_solid_flat_material_data(WORKBENCH_Data *vedata, const float color[3]);
|
||||
WORKBENCH_MaterialData* workbench_get_or_create_solid_studio_material_data(WORKBENCH_Data *vedata, const float color[3]);
|
||||
void workbench_materials_cache_finish(WORKBENCH_Data *vedata);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user