diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2018-12-05 04:07:23 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2018-12-05 04:51:48 +0300 |
commit | 7ffde286be0774e24ea04b4acaad6c6e4ae16bfe (patch) | |
tree | 2c4a3891df3438b24480651f157e07632f9cf15f /source/blender/draw/engines/workbench/workbench_deferred.c | |
parent | 53685404316e05879bc9ff46837dd224ba3cce4f (diff) |
Workbench: Cleanups and reduce shader variations
Also optimize deferred engine by only outputing material data if needed.
This make the bare flat shading mode (no effects) only a depth prepass.
Diffstat (limited to 'source/blender/draw/engines/workbench/workbench_deferred.c')
-rw-r--r-- | source/blender/draw/engines/workbench/workbench_deferred.c | 90 |
1 files changed, 52 insertions, 38 deletions
diff --git a/source/blender/draw/engines/workbench/workbench_deferred.c b/source/blender/draw/engines/workbench/workbench_deferred.c index 1e401b278be..7d551114f2e 100644 --- a/source/blender/draw/engines/workbench/workbench_deferred.c +++ b/source/blender/draw/engines/workbench/workbench_deferred.c @@ -46,6 +46,7 @@ #include "GPU_shader.h" #include "GPU_texture.h" +#include "GPU_extensions.h" #include "../eevee/eevee_lut.h" /* TODO find somewhere to share blue noise Table */ @@ -58,8 +59,8 @@ #endif static struct { - struct GPUShader *prepass_sh_cache[MAX_SHADERS]; - struct GPUShader *composite_sh_cache[MAX_SHADERS]; + struct GPUShader *prepass_sh_cache[MAX_PREPASS_SHADERS]; + struct GPUShader *composite_sh_cache[MAX_COMPOSITE_SHADERS]; struct GPUShader *cavity_sh[MAX_CAVITY_SHADERS]; struct GPUShader *background_sh[2]; struct GPUShader *ghost_resolve_sh; @@ -215,64 +216,63 @@ static GPUShader *workbench_cavity_shader_get(bool cavity, bool curvature) return *sh; } -static void ensure_deferred_shaders(WORKBENCH_PrivateData *wpd, int index, bool use_textures, bool is_hair) +static GPUShader *ensure_deferred_prepass_shader(WORKBENCH_PrivateData *wpd, bool use_textures, bool is_hair) { + int index = workbench_material_get_prepass_shader_index(wpd, use_textures, is_hair); if (e_data.prepass_sh_cache[index] == NULL) { char *defines = workbench_material_build_defines(wpd, use_textures, is_hair); - char *composite_frag = workbench_build_composite_frag(wpd); char *prepass_vert = workbench_build_prepass_vert(is_hair); char *prepass_frag = workbench_build_prepass_frag(); e_data.prepass_sh_cache[index] = DRW_shader_create( prepass_vert, NULL, prepass_frag, defines); - if (!use_textures && !is_hair) { - e_data.composite_sh_cache[index] = DRW_shader_create_fullscreen(composite_frag, defines); - } MEM_freeN(prepass_vert); MEM_freeN(prepass_frag); + MEM_freeN(defines); + } + return e_data.prepass_sh_cache[index]; +} + +static GPUShader *ensure_deferred_composite_shader(WORKBENCH_PrivateData *wpd) +{ + int index = workbench_material_get_composite_shader_index(wpd); + if (e_data.composite_sh_cache[index] == NULL) { + char *defines = workbench_material_build_defines(wpd, false, false); + char *composite_frag = workbench_build_composite_frag(wpd); + e_data.composite_sh_cache[index] = DRW_shader_create_fullscreen(composite_frag, defines); MEM_freeN(composite_frag); MEM_freeN(defines); } + return e_data.composite_sh_cache[index]; } -static void ensure_background_shaders(const bool use_outline) +static GPUShader *ensure_background_shader(WORKBENCH_PrivateData *wpd) { - if (e_data.background_sh[use_outline] == NULL) { - const char *defines = (use_outline) ? "#define V3D_SHADING_OBJECT_OUTLINE\n" : NULL; + const int index = OBJECT_OUTLINE_ENABLED(wpd) ? 1 : 0; + if (e_data.background_sh[index] == NULL) { + const char *defines = (index) ? "#define V3D_SHADING_OBJECT_OUTLINE\n" : NULL; char *frag = BLI_string_joinN( datatoc_workbench_data_lib_glsl, datatoc_workbench_common_lib_glsl, datatoc_workbench_background_lib_glsl, datatoc_workbench_object_outline_lib_glsl, datatoc_workbench_deferred_background_frag_glsl); - e_data.background_sh[use_outline] = DRW_shader_create_fullscreen(frag, defines); + e_data.background_sh[index] = DRW_shader_create_fullscreen(frag, defines); MEM_freeN(frag); } + return e_data.background_sh[index]; } static void select_deferred_shaders(WORKBENCH_PrivateData *wpd) { - const bool use_outline = OBJECT_OUTLINE_ENABLED(wpd); - int index_solid = workbench_material_get_shader_index(wpd, false, false); - int index_solid_hair = workbench_material_get_shader_index(wpd, false, true); - int index_texture = workbench_material_get_shader_index(wpd, true, false); - int index_texture_hair = workbench_material_get_shader_index(wpd, true, true); - - ensure_deferred_shaders(wpd, index_solid, false, false); - ensure_deferred_shaders(wpd, index_solid_hair, false, true); - ensure_deferred_shaders(wpd, index_texture, true, false); - ensure_deferred_shaders(wpd, index_texture_hair, true, true); - ensure_background_shaders(use_outline); - - wpd->prepass_solid_sh = e_data.prepass_sh_cache[index_solid]; - wpd->prepass_solid_hair_sh = e_data.prepass_sh_cache[index_solid_hair]; - wpd->prepass_texture_sh = e_data.prepass_sh_cache[index_texture]; - wpd->prepass_texture_hair_sh = e_data.prepass_sh_cache[index_texture_hair]; - wpd->composite_sh = e_data.composite_sh_cache[index_solid]; - wpd->background_sh = e_data.background_sh[use_outline]; + wpd->prepass_solid_sh = ensure_deferred_prepass_shader(wpd, false, false); + wpd->prepass_solid_hair_sh = ensure_deferred_prepass_shader(wpd, false, true); + wpd->prepass_texture_sh = ensure_deferred_prepass_shader(wpd, true, false); + wpd->prepass_texture_hair_sh = ensure_deferred_prepass_shader(wpd, true, true); + wpd->composite_sh = ensure_deferred_composite_shader(wpd); + wpd->background_sh = ensure_background_shader(wpd); } - /* Using Hammersley distribution */ static float *create_disk_samples(int num_samples, int num_iterations) { @@ -346,8 +346,8 @@ void workbench_deferred_engine_init(WORKBENCH_Data *vedata) } 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); + memset(e_data.prepass_sh_cache, 0, sizeof(e_data.prepass_sh_cache)); + memset(e_data.composite_sh_cache, 0, sizeof(e_data.composite_sh_cache)); e_data.next_object_id = 1; #ifdef DEBUG_SHADOW_VOLUME const char *shadow_frag = datatoc_workbench_shadow_debug_frag_glsl; @@ -411,9 +411,11 @@ void workbench_deferred_engine_init(WORKBENCH_Data *vedata) e_data.normal_buffer_tx = NULL; e_data.cavity_buffer_tx = NULL; - e_data.color_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8, &draw_engine_workbench_solid); e_data.composite_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], comp_tex_format, &draw_engine_workbench_solid); + if (MATDATA_PASS_ENABLED(wpd) || GPU_unused_fb_slot_workaround()) { + e_data.color_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8, &draw_engine_workbench_solid); + } if (OBJECT_ID_PASS_ENABLED(wpd) || GPU_unused_fb_slot_workaround()) { e_data.object_id_tx = DRW_texture_pool_query_2D(size[0], size[1], id_tex_format, &draw_engine_workbench_solid); } @@ -426,8 +428,8 @@ void workbench_deferred_engine_init(WORKBENCH_Data *vedata) GPU_framebuffer_ensure_config(&fbl->prepass_fb, { GPU_ATTACHMENT_TEXTURE(dtxl->depth), - GPU_ATTACHMENT_TEXTURE(e_data.object_id_tx), GPU_ATTACHMENT_TEXTURE(e_data.color_buffer_tx), + GPU_ATTACHMENT_TEXTURE(e_data.object_id_tx), GPU_ATTACHMENT_TEXTURE(e_data.normal_buffer_tx), }); GPU_framebuffer_ensure_config(&fbl->cavity_fb, { @@ -442,6 +444,11 @@ void workbench_deferred_engine_init(WORKBENCH_Data *vedata) GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(e_data.composite_buffer_tx), }); + + if (!MATDATA_PASS_ENABLED(wpd) && !GPU_unused_fb_slot_workaround()) { + e_data.color_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8, &draw_engine_workbench_solid); + } + GPU_framebuffer_ensure_config(&fbl->effect_fb, { GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(e_data.color_buffer_tx), @@ -543,8 +550,10 @@ static void workbench_setup_ghost_framebuffer(WORKBENCH_FramebufferList *fbl) void workbench_deferred_engine_free(void) { - for (int index = 0; index < MAX_SHADERS; index++) { + for (int index = 0; index < MAX_PREPASS_SHADERS; index++) { DRW_SHADER_FREE_SAFE(e_data.prepass_sh_cache[index]); + } + for (int index = 0; index < MAX_COMPOSITE_SHADERS; index++) { DRW_SHADER_FREE_SAFE(e_data.composite_sh_cache[index]); } for (int index = 0; index < MAX_CAVITY_SHADERS; ++index) { @@ -571,7 +580,12 @@ void workbench_deferred_engine_free(void) static void workbench_composite_uniforms(WORKBENCH_PrivateData *wpd, DRWShadingGroup *grp) { DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo); - DRW_shgroup_uniform_texture_ref(grp, "materialBuffer", &e_data.color_buffer_tx); + if (MATDATA_PASS_ENABLED(wpd)) { + DRW_shgroup_uniform_texture_ref(grp, "materialBuffer", &e_data.color_buffer_tx); + } + else { + DRW_shgroup_uniform_vec3(grp, "materialSingleColor", wpd->shading.single_color, 1); + } if (OBJECT_OUTLINE_ENABLED(wpd)) { DRW_shgroup_uniform_texture_ref(grp, "objectId", &e_data.object_id_tx); } @@ -721,7 +735,7 @@ static WORKBENCH_MaterialData *get_or_create_material_data( workbench_material_copy(material, &material_template); DRW_shgroup_stencil_mask(material->shgrp, (ob->dtx & OB_DRAWXRAY) ? 0x00 : 0xFF); DRW_shgroup_uniform_int(material->shgrp, "object_id", &material->object_id, 1); - workbench_material_shgroup_uniform(wpd, material->shgrp, material, ob, true); + workbench_material_shgroup_uniform(wpd, material->shgrp, material, ob, true, true); BLI_ghash_insert(wpd->material_hash, POINTER_FROM_UINT(hash), material); } @@ -764,7 +778,7 @@ static void workbench_cache_populate_particles(WORKBENCH_Data *vedata, Object *o shader); DRW_shgroup_stencil_mask(shgrp, (ob->dtx & OB_DRAWXRAY) ? 0x00 : 0xFF); DRW_shgroup_uniform_int(shgrp, "object_id", &material->object_id, 1); - workbench_material_shgroup_uniform(wpd, shgrp, material, ob, true); + workbench_material_shgroup_uniform(wpd, shgrp, material, ob, true, true); } } } |