diff --git a/source/blender/draw/CMakeLists.txt b/source/blender/draw/CMakeLists.txt index 87966f4fac5..90bca4f15ec 100644 --- a/source/blender/draw/CMakeLists.txt +++ b/source/blender/draw/CMakeLists.txt @@ -181,12 +181,12 @@ set(SRC engines/gpencil/gpencil_antialiasing.cc engines/gpencil/gpencil_cache_utils.cc engines/gpencil/gpencil_draw_data.cc - engines/gpencil/gpencil_engine.cc engines/gpencil/gpencil_engine_c.cc engines/gpencil/gpencil_render.cc - engines/gpencil/gpencil_shader.cc engines/gpencil/gpencil_shader_c.cc engines/gpencil/gpencil_shader_fx.cc + engines/grease_pencil_v3/grease_pencil_engine.cc + engines/grease_pencil_v3/grease_pencil_shader.cc engines/select/select_draw_utils.cc engines/select/select_engine.cc engines/select/select_instance.cc @@ -305,14 +305,15 @@ set(SRC engines/eevee_next/eevee_volume.hh engines/eevee_next/eevee_world.hh engines/external/external_engine.h - engines/gpencil/gpencil_antialiasing.hh engines/gpencil/gpencil_engine.h - engines/gpencil/gpencil_layer.hh - engines/gpencil/gpencil_light.hh - engines/gpencil/gpencil_material.hh - engines/gpencil/gpencil_object.hh - engines/gpencil/gpencil_shader.hh - engines/gpencil/gpencil_vfx.hh + engines/grease_pencil_v3/grease_pencil_antialiasing.hh + engines/grease_pencil_v3/grease_pencil_engine.h + engines/grease_pencil_v3/grease_pencil_layer.hh + engines/grease_pencil_v3/grease_pencil_light.hh + engines/grease_pencil_v3/grease_pencil_material.hh + engines/grease_pencil_v3/grease_pencil_object.hh + engines/grease_pencil_v3/grease_pencil_shader.hh + engines/grease_pencil_v3/grease_pencil_vfx.hh engines/image/image_batches.hh engines/image/image_buffer_cache.hh engines/image/image_drawing_mode.hh @@ -714,22 +715,33 @@ set(GLSL_SRC intern/draw_shader_shared.h engines/gpencil/shaders/gpencil_frag.glsl - engines/gpencil/shaders/grease_pencil_frag.glsl engines/gpencil/shaders/gpencil_vert.glsl - engines/gpencil/shaders/grease_pencil_vert.glsl engines/gpencil/shaders/gpencil_antialiasing_frag.glsl engines/gpencil/shaders/gpencil_antialiasing_vert.glsl - engines/gpencil/shaders/gpencil_common_lib.glsl + engines/gpencil/shaders/gpencil_blend_mode.glsl engines/gpencil/shaders/gpencil_layer_blend_frag.glsl engines/gpencil/shaders/gpencil_mask_invert_frag.glsl engines/gpencil/shaders/gpencil_depth_merge_frag.glsl engines/gpencil/shaders/gpencil_depth_merge_vert.glsl - engines/gpencil/shaders/grease_pencil_depth_merge_vert.glsl engines/gpencil/shaders/gpencil_vfx_frag.glsl engines/gpencil/gpencil_defines.h engines/gpencil/gpencil_shader_shared.h + engines/grease_pencil_v3/shaders/grease_pencil_antialiasing_frag.glsl + engines/grease_pencil_v3/shaders/grease_pencil_antialiasing_vert.glsl + engines/grease_pencil_v3/shaders/grease_pencil_blend_mode.glsl + engines/grease_pencil_v3/shaders/grease_pencil_depth_merge_frag.glsl + engines/grease_pencil_v3/shaders/grease_pencil_depth_merge_vert.glsl + engines/grease_pencil_v3/shaders/grease_pencil_frag.glsl + engines/grease_pencil_v3/shaders/grease_pencil_layer_blend_frag.glsl + engines/grease_pencil_v3/shaders/grease_pencil_mask_invert_frag.glsl + engines/grease_pencil_v3/shaders/grease_pencil_vert.glsl + engines/grease_pencil_v3/shaders/grease_pencil_vfx_frag.glsl + + engines/grease_pencil_v3/grease_pencil_defines.hh + engines/grease_pencil_v3/grease_pencil_shader_shared.h + engines/select/shaders/select_id_vert.glsl engines/select/shaders/select_id_frag.glsl engines/select/shaders/select_lib.glsl diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h index ab4d9ea4309..e03733a0ac6 100644 --- a/source/blender/draw/engines/gpencil/gpencil_engine.h +++ b/source/blender/draw/engines/gpencil/gpencil_engine.h @@ -26,7 +26,6 @@ extern "C" { #include "gpencil_shader_shared.h" extern DrawEngineType draw_engine_gpencil_type; -extern DrawEngineType draw_engine_gpencil_next_type; struct GPENCIL_Data; struct GPENCIL_StorageList; diff --git a/source/blender/draw/engines/gpencil/gpencil_shader_c.cc b/source/blender/draw/engines/gpencil/gpencil_shader_c.cc index a5f93d497ee..41482fa9ae1 100644 --- a/source/blender/draw/engines/gpencil/gpencil_shader_c.cc +++ b/source/blender/draw/engines/gpencil/gpencil_shader_c.cc @@ -9,7 +9,7 @@ #include "gpencil_engine.h" -extern "C" char datatoc_gpencil_common_lib_glsl[]; +extern "C" char datatoc_gpencil_blend_mode_glsl[]; extern "C" char datatoc_gpencil_frag_glsl[]; extern "C" char datatoc_gpencil_vert_glsl[]; extern "C" char datatoc_gpencil_antialiasing_frag_glsl[]; diff --git a/source/blender/draw/engines/gpencil/gpencil_shader_shared.h b/source/blender/draw/engines/gpencil/gpencil_shader_shared.h index e1a6f93a629..2bea94f0024 100644 --- a/source/blender/draw/engines/gpencil/gpencil_shader_shared.h +++ b/source/blender/draw/engines/gpencil/gpencil_shader_shared.h @@ -3,16 +3,11 @@ * SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef GPU_SHADER -# pragma once - # include "GPU_shader_shared_utils.h" # ifndef __cplusplus -typedef struct gpScene gpScene; typedef struct gpMaterial gpMaterial; typedef struct gpLight gpLight; -typedef struct gpObject gpObject; -typedef struct gpLayer gpLayer; typedef enum gpMaterialFlag gpMaterialFlag; # ifdef GP_LIGHT typedef enum gpLightType gpLightType; @@ -38,8 +33,6 @@ enum gpMaterialFlag { GP_FILL_TEXTURE_CLIP = (1u << 12u), GP_FILL_GRADIENT_USE = (1u << 13u), GP_FILL_GRADIENT_RADIAL = (1u << 14u), - GP_SHOW_STROKE = (1u << 15u), - GP_SHOW_FILL = (1u << 16u), GP_FILL_FLAGS = (GP_FILL_TEXTURE_USE | GP_FILL_TEXTURE_PREMUL | GP_FILL_TEXTURE_CLIP | GP_FILL_GRADIENT_USE | GP_FILL_GRADIENT_RADIAL | GP_FILL_HOLDOUT), }; @@ -60,12 +53,6 @@ enum gpLightType { # define gpLightType uint #endif -struct gpScene { - float2 render_size; - float2 _pad0; -}; -BLI_STATIC_ASSERT_ALIGN(gpScene, 16) - struct gpMaterial { float4 stroke_color; float4 fill_color; @@ -132,37 +119,6 @@ struct gpLight { BLI_STATIC_ASSERT_ALIGN(gpLight, 16) #endif -struct gpObject { - /** Weather or not to apply lighting to the GPencil object. */ - bool1 is_shadeless; - /** Switch between 2d and 3D stroke order. */ - bool1 stroke_order3d; - /** Offset inside the layer buffer to the first layer data of this object. */ - uint layer_offset; - /** Offset inside the material buffer to the first material data of this object. */ - uint material_offset; - /** Color to multiply to the final mixed color. */ - float4 tint; - /** Color to multiply to the final mixed color. */ - float3 normal; - - float _pad0; -}; -BLI_STATIC_ASSERT_ALIGN(gpObject, 16) - -struct gpLayer { - /** Amount of vertex color to blend with actual material color. */ - float vertex_color_opacity; - /** Thickness change of all the strokes. */ - float thickness_offset; - /** Thickness change of all the strokes. */ - float opacity; - /** Offset to apply to stroke index to be able to insert a currently drawn stroke in between. */ - float stroke_index_offset; - /** Color to multiply to the final mixed color. */ - float4 tint; -}; -BLI_STATIC_ASSERT_ALIGN(gpLayer, 16) #ifndef GPU_SHADER # undef gpMaterialFlag diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_common_lib.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_blend_mode.glsl similarity index 100% rename from source/blender/draw/engines/gpencil/shaders/gpencil_common_lib.glsl rename to source/blender/draw/engines/gpencil/shaders/gpencil_blend_mode.glsl diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_layer_blend_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_layer_blend_frag.glsl index f58858591f8..d00f6cfedfe 100644 --- a/source/blender/draw/engines/gpencil/shaders/gpencil_layer_blend_frag.glsl +++ b/source/blender/draw/engines/gpencil/shaders/gpencil_layer_blend_frag.glsl @@ -2,7 +2,7 @@ * * SPDX-License-Identifier: GPL-2.0-or-later */ -#pragma BLENDER_REQUIRE(gpencil_common_lib.glsl) +#pragma BLENDER_REQUIRE(gpencil_blend_mode.glsl) void main() { diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_vfx_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_vfx_frag.glsl index 0a47aede8a2..f5713663b64 100644 --- a/source/blender/draw/engines/gpencil/shaders/gpencil_vfx_frag.glsl +++ b/source/blender/draw/engines/gpencil/shaders/gpencil_vfx_frag.glsl @@ -2,7 +2,7 @@ * * SPDX-License-Identifier: GPL-2.0-or-later */ -#pragma BLENDER_REQUIRE(gpencil_common_lib.glsl) +#pragma BLENDER_REQUIRE(gpencil_blend_mode.glsl) float gaussian_weight(float x) { diff --git a/source/blender/draw/engines/gpencil/shaders/infos/gpencil_info.hh b/source/blender/draw/engines/gpencil/shaders/infos/gpencil_info.hh index af893352351..f3231e79e50 100644 --- a/source/blender/draw/engines/gpencil/shaders/infos/gpencil_info.hh +++ b/source/blender/draw/engines/gpencil/shaders/infos/gpencil_info.hh @@ -54,41 +54,6 @@ GPU_SHADER_CREATE_INFO(gpencil_geometry) .depth_write(DepthWrite::ANY) .additional_info("draw_gpencil"); -GPU_SHADER_CREATE_INFO(gpencil_geometry_next) - .do_static_compilation(true) - .define("GP_LIGHT") - .typedef_source("gpencil_defines.h") - .sampler(GPENCIL_SCENE_DEPTH_TEX_SLOT, ImageType::DEPTH_2D, "gpSceneDepthTexture") - .sampler(GPENCIL_MASK_TEX_SLOT, ImageType::FLOAT_2D, "gpMaskTexture") - .sampler(GPENCIL_FILL_TEX_SLOT, ImageType::FLOAT_2D, "gpFillTexture") - .sampler(GPENCIL_STROKE_TEX_SLOT, ImageType::FLOAT_2D, "gpStrokeTexture") - .storage_buf(GPENCIL_OBJECT_SLOT, Qualifier::READ, "gpObject", "gp_object[]") - .storage_buf(GPENCIL_LAYER_SLOT, Qualifier::READ, "gpLayer", "gp_layer[]") - .storage_buf(GPENCIL_MATERIAL_SLOT, Qualifier::READ, "gpMaterial", "gp_materials[]") - .storage_buf(GPENCIL_LIGHT_SLOT, Qualifier::READ, "gpLight", "gp_lights[]") - .uniform_buf(GPENCIL_SCENE_SLOT, "gpScene", "gp_scene") - /* Per Scene */ - .define("viewportSize", "gp_scene.render_size") - /* Per Object */ - .define("gpNormal", "gp_object[resource_id].normal") - .define("gpStrokeOrder3d", "gp_object[resource_id].stroke_order3d") - .define("gpMaterialOffset", "gp_object[resource_id].material_offset") - /* Per Layer */ - .define("layer_id", "gp_object[resource_id].layer_offset") /* TODO */ - .define("gpVertexColorOpacity", "gp_layer[layer_id].vertex_color_opacity") - .define("gpLayerTint", "gp_layer[layer_id].tint") - .define("gpLayerOpacity", "gp_layer[layer_id].opacity") - .define("gpStrokeIndexOffset", "gp_layer[layer_id].stroke_index_offset") - .fragment_out(0, Type::VEC4, "fragColor") - .fragment_out(1, Type::VEC4, "revealColor") - .vertex_out(gpencil_geometry_iface) - .vertex_out(gpencil_geometry_flat_iface) - .vertex_out(gpencil_geometry_noperspective_iface) - .vertex_source("grease_pencil_vert.glsl") - .fragment_source("grease_pencil_frag.glsl") - .additional_info("draw_gpencil_new") - .depth_write(DepthWrite::ANY); - /** \} */ /* -------------------------------------------------------------------- */ @@ -126,16 +91,6 @@ GPU_SHADER_CREATE_INFO(gpencil_depth_merge) .depth_write(DepthWrite::ANY) .additional_info("draw_view"); -GPU_SHADER_CREATE_INFO(grease_pencil_depth_merge) - .do_static_compilation(true) - .define("strokeOrder3d", "false") - .sampler(0, ImageType::DEPTH_2D, "depthBuf") - .vertex_in(0, Type::VEC3, "pos") - .vertex_source("grease_pencil_depth_merge_vert.glsl") - .fragment_source("gpencil_depth_merge_frag.glsl") - .depth_write(DepthWrite::ANY) - .additional_info("draw_modelmat_new", "draw_view"); - /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/draw/engines/gpencil/gpencil_antialiasing.hh b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_antialiasing.hh similarity index 98% rename from source/blender/draw/engines/gpencil/gpencil_antialiasing.hh rename to source/blender/draw/engines/grease_pencil_v3/grease_pencil_antialiasing.hh index cdcd2282a3d..fb65a50f172 100644 --- a/source/blender/draw/engines/gpencil/gpencil_antialiasing.hh +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_antialiasing.hh @@ -8,7 +8,6 @@ #pragma once -#include "BKE_gpencil_legacy.h" #include "BKE_image.h" #include "DRW_gpu_wrapper.hh" #include "DRW_render.hh" @@ -16,7 +15,7 @@ #include "draw_manager.hh" #include "draw_pass.hh" -#include "gpencil_shader.hh" +#include "grease_pencil_shader.hh" #include "BLI_smaa_textures.h" diff --git a/source/blender/draw/engines/grease_pencil_v3/grease_pencil_defines.hh b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_defines.hh new file mode 100644 index 00000000000..b94ef809d3b --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_defines.hh @@ -0,0 +1,23 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#define GPENCIL_MATERIAL_BUFFER_LEN 256 + +#define GPENCIL_LIGHT_BUFFER_LEN 128 + +/* High bits are used to pass material ID to fragment shader. */ +#define GPENCIl_MATID_SHIFT 16u + +/* Textures */ +#define GPENCIL_SCENE_DEPTH_TEX_SLOT 2 +#define GPENCIL_MASK_TEX_SLOT 3 +#define GPENCIL_FILL_TEX_SLOT 4 +#define GPENCIL_STROKE_TEX_SLOT 5 +/* SSBOs */ +#define GPENCIL_OBJECT_SLOT 0 +#define GPENCIL_LAYER_SLOT 1 +#define GPENCIL_MATERIAL_SLOT 2 +#define GPENCIL_LIGHT_SLOT 3 +/* UBOs */ +#define GPENCIL_SCENE_SLOT 2 diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.cc b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_engine.cc similarity index 97% rename from source/blender/draw/engines/gpencil/gpencil_engine.cc rename to source/blender/draw/engines/grease_pencil_v3/grease_pencil_engine.cc index 6b8350d7253..2be07b293ea 100644 --- a/source/blender/draw/engines/gpencil/gpencil_engine.cc +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_engine.cc @@ -6,10 +6,6 @@ * \ingroup draw */ -#include "BKE_gpencil_modifier_legacy.h" - -#include "BLI_listbase_wrapper.hh" - #include "DEG_depsgraph_query.hh" #include "DNA_shader_fx_types.h" @@ -30,16 +26,15 @@ #include "draw_pass.hh" #define GP_LIGHT -#include "gpencil_antialiasing.hh" -#include "gpencil_defines.h" -#include "gpencil_engine.h" -#include "gpencil_layer.hh" -#include "gpencil_light.hh" -#include "gpencil_material.hh" -#include "gpencil_object.hh" -#include "gpencil_shader.hh" -#include "gpencil_shader_shared.h" -#include "gpencil_vfx.hh" +#include "grease_pencil_antialiasing.hh" +#include "grease_pencil_engine.h" +#include "grease_pencil_layer.hh" +#include "grease_pencil_light.hh" +#include "grease_pencil_material.hh" +#include "grease_pencil_object.hh" +#include "grease_pencil_shader.hh" +#include "grease_pencil_shader_shared.h" +#include "grease_pencil_vfx.hh" namespace blender::draw::greasepencil { diff --git a/source/blender/draw/engines/grease_pencil_v3/grease_pencil_engine.h b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_engine.h new file mode 100644 index 00000000000..3148ad6af77 --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_engine.h @@ -0,0 +1,25 @@ +/* SPDX-FileCopyrightText: 2024 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup draw + */ + +#pragma once + +#include "DRW_render.hh" +#include "RE_engine.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#include "grease_pencil_defines.hh" +#include "grease_pencil_shader_shared.h" + +extern DrawEngineType draw_engine_gpencil_next_type; + +#ifdef __cplusplus +} +#endif diff --git a/source/blender/draw/engines/gpencil/gpencil_layer.hh b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_layer.hh similarity index 97% rename from source/blender/draw/engines/gpencil/gpencil_layer.hh rename to source/blender/draw/engines/grease_pencil_v3/grease_pencil_layer.hh index f9fe3e06e7d..01a7de085b5 100644 --- a/source/blender/draw/engines/gpencil/gpencil_layer.hh +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_layer.hh @@ -15,6 +15,8 @@ #include "draw_manager.hh" #include "draw_pass.hh" +#include "grease_pencil_shader_shared.h" + namespace blender::draw::greasepencil { using namespace draw; diff --git a/source/blender/draw/engines/gpencil/gpencil_light.hh b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_light.hh similarity index 99% rename from source/blender/draw/engines/gpencil/gpencil_light.hh rename to source/blender/draw/engines/grease_pencil_v3/grease_pencil_light.hh index 7b4f4cc2308..5323e24f756 100644 --- a/source/blender/draw/engines/gpencil/gpencil_light.hh +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_light.hh @@ -8,7 +8,6 @@ #pragma once -#include "BKE_gpencil_legacy.h" #include "BKE_image.h" #include "DRW_gpu_wrapper.hh" #include "DRW_render.hh" diff --git a/source/blender/draw/engines/gpencil/gpencil_material.hh b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_material.hh similarity index 99% rename from source/blender/draw/engines/gpencil/gpencil_material.hh rename to source/blender/draw/engines/grease_pencil_v3/grease_pencil_material.hh index 8b6e4c0336a..b45d81fc2d4 100644 --- a/source/blender/draw/engines/gpencil/gpencil_material.hh +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_material.hh @@ -8,7 +8,6 @@ #pragma once -#include "BKE_gpencil_legacy.h" #include "BKE_image.h" #include "DRW_gpu_wrapper.hh" #include "DRW_render.hh" diff --git a/source/blender/draw/engines/gpencil/gpencil_object.hh b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_object.hh similarity index 96% rename from source/blender/draw/engines/gpencil/gpencil_object.hh rename to source/blender/draw/engines/grease_pencil_v3/grease_pencil_object.hh index dc172957694..766ff5bff15 100644 --- a/source/blender/draw/engines/gpencil/gpencil_object.hh +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_object.hh @@ -18,9 +18,9 @@ #include "draw_manager.hh" #include "draw_pass.hh" -#include "gpencil_layer.hh" -#include "gpencil_material.hh" -#include "gpencil_shader.hh" +#include "grease_pencil_layer.hh" +#include "grease_pencil_material.hh" +#include "grease_pencil_shader.hh" namespace blender::draw::greasepencil { @@ -84,13 +84,13 @@ class ObjectModule { const bool hide_overlay = ((v3d->flag2 & V3D_HIDE_OVERLAYS) != 0); const bool show_onion = ((v3d->gp_flag & V3D_GP_SHOW_ONION_SKIN) != 0); use_onion_ = show_onion && !hide_overlay && !playing; - use_stroke_fill_ = GPENCIL_SIMPLIFY_FILL(scene, playing); - use_vfx_ = GPENCIL_SIMPLIFY_FX(scene, playing); + use_stroke_fill_ = true;/* GPENCIL_SIMPLIFY_FILL(scene, playing); */ + use_vfx_ = playing;/* GPENCIL_SIMPLIFY_FX(scene, playing); */ is_render_ = false; } else { - use_stroke_fill_ = GPENCIL_SIMPLIFY_FILL(scene, false); - use_vfx_ = GPENCIL_SIMPLIFY_FX(scene, false); + use_stroke_fill_ = true;/* GPENCIL_SIMPLIFY_FILL(scene, false); */ + use_vfx_ = true;/* GPENCIL_SIMPLIFY_FX(scene, false); */ } } diff --git a/source/blender/draw/engines/gpencil/gpencil_shader.cc b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_shader.cc similarity index 80% rename from source/blender/draw/engines/gpencil/gpencil_shader.cc rename to source/blender/draw/engines/grease_pencil_v3/grease_pencil_shader.cc index c8e9724489d..474dff6971a 100644 --- a/source/blender/draw/engines/gpencil/gpencil_shader.cc +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_shader.cc @@ -6,7 +6,7 @@ * \ingroup draw */ -#include "gpencil_shader.hh" +#include "grease_pencil_shader.hh" #include @@ -64,35 +64,35 @@ const char *ShaderModule::static_shader_create_info_name_get(eShaderType shader_ { switch (shader_type) { case ANTIALIASING_EDGE_DETECT: - return "gpencil_antialiasing_stage_0"; + return "grease_pencil_antialiasing_stage_0"; case ANTIALIASING_BLEND_WEIGHT: - return "gpencil_antialiasing_stage_1"; + return "grease_pencil_antialiasing_stage_1"; case ANTIALIASING_RESOLVE: - return "gpencil_antialiasing_stage_2"; + return "grease_pencil_antialiasing_stage_2"; case GREASE_PENCIL: - return "gpencil_geometry_next"; + return "grease_pencil_geometry"; case LAYER_BLEND: - return "gpencil_layer_blend"; + return "grease_pencil_layer_blend"; case DEPTH_MERGE: return "grease_pencil_depth_merge"; case MASK_INVERT: - return "gpencil_mask_invert"; + return "grease_pencil_mask_invert"; case FX_COMPOSITE: - return "gpencil_fx_composite"; + return "grease_pencil_fx_composite"; case FX_COLORIZE: - return "gpencil_fx_colorize"; + return "grease_pencil_fx_colorize"; case FX_BLUR: - return "gpencil_fx_blur"; + return "grease_pencil_fx_blur"; case FX_GLOW: - return "gpencil_fx_glow"; + return "grease_pencil_fx_glow"; case FX_PIXEL: - return "gpencil_fx_pixelize"; + return "grease_pencil_fx_pixelize"; case FX_RIM: - return "gpencil_fx_rim"; + return "grease_pencil_fx_rim"; case FX_SHADOW: - return "gpencil_fx_shadow"; + return "grease_pencil_fx_shadow"; case FX_TRANSFORM: - return "gpencil_fx_transform"; + return "grease_pencil_fx_transform"; /* To avoid compiler warning about missing case. */ case MAX_SHADER_TYPE: return ""; diff --git a/source/blender/draw/engines/gpencil/gpencil_shader.hh b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_shader.hh similarity index 100% rename from source/blender/draw/engines/gpencil/gpencil_shader.hh rename to source/blender/draw/engines/grease_pencil_v3/grease_pencil_shader.hh diff --git a/source/blender/draw/engines/grease_pencil_v3/grease_pencil_shader_shared.h b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_shader_shared.h new file mode 100644 index 00000000000..544f037df37 --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_shader_shared.h @@ -0,0 +1,174 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef GPU_SHADER +# pragma once + +# include "GPU_shader_shared_utils.h" + +# ifndef __cplusplus +typedef struct gpScene gpScene; +typedef struct gpMaterial gpMaterial; +typedef struct gpLight gpLight; +typedef struct gpObject gpObject; +typedef struct gpLayer gpLayer; +typedef enum gpMaterialFlag gpMaterialFlag; +# ifdef GP_LIGHT +typedef enum gpLightType gpLightType; +# endif +# endif + +namespace blender::draw::greasepencil { + +#endif + +enum gpMaterialFlag { + GP_FLAG_NONE = 0u, + GP_STROKE_ALIGNMENT_STROKE = 1u, + GP_STROKE_ALIGNMENT_OBJECT = 2u, + GP_STROKE_ALIGNMENT_FIXED = 3u, + GP_STROKE_ALIGNMENT = 0x3u, + GP_STROKE_OVERLAP = (1u << 2u), + GP_STROKE_TEXTURE_USE = (1u << 3u), + GP_STROKE_TEXTURE_STENCIL = (1u << 4u), + GP_STROKE_TEXTURE_PREMUL = (1u << 5u), + GP_STROKE_DOTS = (1u << 6u), + GP_STROKE_HOLDOUT = (1u << 7u), + GP_FILL_HOLDOUT = (1u << 8u), + GP_FILL_TEXTURE_USE = (1u << 10u), + GP_FILL_TEXTURE_PREMUL = (1u << 11u), + GP_FILL_TEXTURE_CLIP = (1u << 12u), + GP_FILL_GRADIENT_USE = (1u << 13u), + GP_FILL_GRADIENT_RADIAL = (1u << 14u), + GP_SHOW_STROKE = (1u << 15u), + GP_SHOW_FILL = (1u << 16u), + GP_FILL_FLAGS = (GP_FILL_TEXTURE_USE | GP_FILL_TEXTURE_PREMUL | GP_FILL_TEXTURE_CLIP | + GP_FILL_GRADIENT_USE | GP_FILL_GRADIENT_RADIAL | GP_FILL_HOLDOUT), +}; + +enum gpLightType { + GP_LIGHT_TYPE_POINT = 0u, + GP_LIGHT_TYPE_SPOT = 1u, + GP_LIGHT_TYPE_SUN = 2u, + GP_LIGHT_TYPE_AMBIENT = 3u, +}; + +#define GP_IS_STROKE_VERTEX_BIT (1 << 30) +#define GP_VERTEX_ID_SHIFT 2 + +/* Avoid compiler funkiness with enum types not being strongly typed in C. */ +#ifndef GPU_SHADER +# define gpMaterialFlag uint +# define gpLightType uint +#endif + +struct gpScene { + float2 render_size; + float2 _pad0; +}; +BLI_STATIC_ASSERT_ALIGN(gpScene, 16) + +struct gpMaterial { + float4 stroke_color; + float4 fill_color; + float4 fill_mix_color; + float4 fill_uv_rot_scale; +#ifndef GPU_SHADER + float2 fill_uv_offset; + float2 alignment_rot; + float stroke_texture_mix; + float stroke_u_scale; + float fill_texture_mix; + gpMaterialFlag flag; +#else + /* Some drivers are completely messing the alignment or the fetches here. + * We are forced to pack these into vec4 otherwise we only get 0.0 as value. */ + /* NOTE(@fclem): This was the case on MacOS OpenGL implementation. + * This might be fixed in newer APIs. */ + float4 packed1; + float4 packed2; +# define _fill_uv_offset packed1.xy +# define _alignment_rot packed1.zw +# define _stroke_texture_mix packed2.x +# define _stroke_u_scale packed2.y +# define _fill_texture_mix packed2.z + /** NOTE(@fclem): Needs floatBitsToUint(). */ +# define _flag packed2.w +#endif +}; +BLI_STATIC_ASSERT_ALIGN(gpMaterial, 16) + +#ifdef GP_LIGHT +struct gpLight { +# ifndef GPU_SHADER + float3 color; + gpLightType type; + float3 right; + float spot_size; + float3 up; + float spot_blend; + float3 forward; + float _pad0; + float3 position; + float _pad1; +# else + /* Some drivers are completely messing the alignment or the fetches here. + * We are forced to pack these into vec4 otherwise we only get 0.0 as value. */ + /* NOTE(@fclem): This was the case on MacOS OpenGL implementation. + * This might be fixed in newer APIs. */ + float4 packed0; + float4 packed1; + float4 packed2; + float4 packed3; + float4 packed4; +# define _color packed0.xyz +# define _type packed0.w +# define _right packed1.xyz +# define _spot_size packed1.w +# define _up packed2.xyz +# define _spot_blend packed2.w +# define _forward packed3.xyz +# define _position packed4.xyz +# endif +}; +BLI_STATIC_ASSERT_ALIGN(gpLight, 16) +#endif + +struct gpObject { + /** Weather or not to apply lighting to the GPencil object. */ + bool1 is_shadeless; + /** Switch between 2d and 3D stroke order. */ + bool1 stroke_order3d; + /** Offset inside the layer buffer to the first layer data of this object. */ + uint layer_offset; + /** Offset inside the material buffer to the first material data of this object. */ + uint material_offset; + /** Color to multiply to the final mixed color. */ + float4 tint; + /** Color to multiply to the final mixed color. */ + float3 normal; + + float _pad0; +}; +BLI_STATIC_ASSERT_ALIGN(gpObject, 16) + +struct gpLayer { + /** Amount of vertex color to blend with actual material color. */ + float vertex_color_opacity; + /** Thickness change of all the strokes. */ + float thickness_offset; + /** Thickness change of all the strokes. */ + float opacity; + /** Offset to apply to stroke index to be able to insert a currently drawn stroke in between. */ + float stroke_index_offset; + /** Color to multiply to the final mixed color. */ + float4 tint; +}; +BLI_STATIC_ASSERT_ALIGN(gpLayer, 16) + +#ifndef GPU_SHADER +} // namespace blender::draw::greasepencil +# undef gpMaterialFlag +# undef gpLightType +#endif diff --git a/source/blender/draw/engines/gpencil/gpencil_vfx.hh b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_vfx.hh similarity index 97% rename from source/blender/draw/engines/gpencil/gpencil_vfx.hh rename to source/blender/draw/engines/grease_pencil_v3/grease_pencil_vfx.hh index 2b22173f974..a5b5ebc0f50 100644 --- a/source/blender/draw/engines/gpencil/gpencil_vfx.hh +++ b/source/blender/draw/engines/grease_pencil_v3/grease_pencil_vfx.hh @@ -8,15 +8,14 @@ #include "BKE_camera.h" #include "BLI_listbase_wrapper.hh" #include "DNA_camera_types.h" -#include "DNA_gpencil_legacy_types.h" #include "DNA_shader_fx_types.h" #include "draw_manager.hh" #include "draw_pass.hh" -#include "gpencil_engine.h" -#include "gpencil_shader.hh" -#include "gpencil_shader_shared.h" +#include "grease_pencil_engine.h" +#include "grease_pencil_shader.hh" +#include "grease_pencil_shader_shared.h" namespace blender::draw::greasepencil { @@ -231,9 +230,8 @@ class VfxModule { /* TODO(fclem): Replace by this->render_size. */ const float *vp_size = DRW_viewport_size_get(); - float world_pixel_scale = 1.0f / GPENCIL_PIXEL_FACTOR; float scale = mat4_to_scale(object->object_to_world); - float distance_factor = world_pixel_scale * scale * winmat[1][1] * vp_size[1] / w; + float distance_factor = scale * winmat[1][1] * vp_size[1] / w; blur_size *= distance_factor; } diff --git a/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_antialiasing_frag.glsl b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_antialiasing_frag.glsl new file mode 100644 index 00000000000..424524c7697 --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_antialiasing_frag.glsl @@ -0,0 +1,47 @@ +/* SPDX-FileCopyrightText: 2020-2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma BLENDER_REQUIRE(gpu_shader_smaa_lib.glsl) + +void main() +{ +#if SMAA_STAGE == 0 + /* Detect edges in color and revealage buffer. */ + out_edges = SMAALumaEdgeDetectionPS(uvs, offset, colorTex); + out_edges = max(out_edges, SMAALumaEdgeDetectionPS(uvs, offset, revealTex)); + /* Discard if there is no edge. */ + if (dot(out_edges, float2(1.0, 1.0)) == 0.0) { + discard; + return; + } + +#elif SMAA_STAGE == 1 + out_weights = SMAABlendingWeightCalculationPS( + uvs, pixcoord, offset, edgesTex, areaTex, searchTex, vec4(0)); + +#elif SMAA_STAGE == 2 + /* Resolve both buffers. */ + if (doAntiAliasing) { + out_color = SMAANeighborhoodBlendingPS(uvs, offset[0], colorTex, blendTex); + out_reveal = SMAANeighborhoodBlendingPS(uvs, offset[0], revealTex, blendTex); + } + else { + out_color = texture(colorTex, uvs); + out_reveal = texture(revealTex, uvs); + } + + /* Revealage, how much light passes through. */ + /* Average for alpha channel. */ + out_reveal.a = clamp(dot(out_reveal.rgb, vec3(0.333334)), 0.0, 1.0); + /* Color buffer is already pre-multiplied. Just add it to the color. */ + /* Add the alpha. */ + out_color.a = 1.0 - out_reveal.a; + + if (onlyAlpha) { + /* Special case in wire-frame X-ray mode. */ + out_color = vec4(0.0); + out_reveal.rgb = out_reveal.aaa; + } +#endif +} diff --git a/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_antialiasing_vert.glsl b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_antialiasing_vert.glsl new file mode 100644 index 00000000000..6620ae3da82 --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_antialiasing_vert.glsl @@ -0,0 +1,22 @@ +/* SPDX-FileCopyrightText: 2020-2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma BLENDER_REQUIRE(gpu_shader_smaa_lib.glsl) + +void main() +{ + int v = gl_VertexID % 3; + float x = -1.0 + float((v & 1) << 2); + float y = -1.0 + float((v & 2) << 1); + gl_Position = vec4(x, y, 1.0, 1.0); + uvs = (gl_Position.xy + 1.0) * 0.5; + +#if SMAA_STAGE == 0 + SMAAEdgeDetectionVS(uvs, offset); +#elif SMAA_STAGE == 1 + SMAABlendingWeightCalculationVS(uvs, pixcoord, offset); +#elif SMAA_STAGE == 2 + SMAANeighborhoodBlendingVS(uvs, offset[0]); +#endif +} diff --git a/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_blend_mode.glsl b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_blend_mode.glsl new file mode 100644 index 00000000000..1a501cd305c --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_blend_mode.glsl @@ -0,0 +1,67 @@ +/* SPDX-FileCopyrightText: 2020-2022 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/* Must match eGPLayerBlendModes */ +#define MODE_REGULAR 0 +#define MODE_HARDLIGHT 1 +#define MODE_ADD 2 +#define MODE_SUB 3 +#define MODE_MULTIPLY 4 +#define MODE_DIVIDE 5 +#define MODE_HARDLIGHT_SECOND_PASS 999 + +void blend_mode_output( + int blend_mode, vec4 color, float opacity, out vec4 frag_color, out vec4 frag_revealage) +{ + switch (blend_mode) { + case MODE_REGULAR: + /* Reminder: Blending func is pre-multiply alpha blend + * `(dst.rgba * (1 - src.a) + src.rgb)`. */ + color *= opacity; + frag_color = color; + frag_revealage = vec4(0.0, 0.0, 0.0, color.a); + break; + case MODE_MULTIPLY: + /* Reminder: Blending func is multiply blend `(dst.rgba * src.rgba)`. */ + color.a *= opacity; + frag_revealage = frag_color = (1.0 - color.a) + color.a * color; + break; + case MODE_DIVIDE: + /* Reminder: Blending func is multiply blend `(dst.rgba * src.rgba)`. */ + color.a *= opacity; + frag_revealage = frag_color = clamp(1.0 / max(vec4(1e-6), 1.0 - color * color.a), 0.0, 1e18); + break; + case MODE_HARDLIGHT: { + /* Reminder: Blending func is multiply blend `(dst.rgba * src.rgba)`. */ + /** + * We need to separate the overlay equation into 2 term (one multiply and one add). + * This is the standard overlay equation (per channel): + * `rtn = (src < 0.5) ? (2.0 * src * dst) : (1.0 - 2.0 * (1.0 - src) * (1.0 - dst));` + * We rewrite the second branch like this: + * `rtn = 1 - 2 * (1 - src) * (1 - dst);` + * `rtn = 1 - 2 (1 - dst + src * dst - src);` + * `rtn = 1 - 2 (1 - dst * (1 - src) - src);` + * `rtn = 1 - 2 + dst * (2 - 2 * src) + 2 * src;` + * `rtn = (- 1 + 2 * src) + dst * (2 - 2 * src);` + */ + color = mix(vec4(0.5), color, color.a * opacity); + vec4 s = step(-0.5, -color); + frag_revealage = frag_color = 2.0 * s + 2.0 * color * (1.0 - s * 2.0); + frag_revealage = max(vec4(0.0), frag_revealage); + break; + } + case MODE_HARDLIGHT_SECOND_PASS: + /* Reminder: Blending func is additive blend `(dst.rgba + src.rgba)`. */ + color = mix(vec4(0.5), color, color.a * opacity); + frag_revealage = frag_color = (-1.0 + 2.0 * color) * step(-0.5, -color); + frag_revealage = max(vec4(0.0), frag_revealage); + break; + case MODE_SUB: + case MODE_ADD: + /* Reminder: Blending func is additive / subtractive blend `(dst.rgba +/- src.rgba)`. */ + frag_color = color * color.a * opacity; + frag_revealage = vec4(0.0); + break; + } +} diff --git a/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_depth_merge_frag.glsl b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_depth_merge_frag.glsl new file mode 100644 index 00000000000..ee2898bbfd7 --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_depth_merge_frag.glsl @@ -0,0 +1,14 @@ +/* SPDX-FileCopyrightText: 2020-2022 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +void main() +{ + float depth = textureLod(depthBuf, gl_FragCoord.xy / vec2(textureSize(depthBuf, 0)), 0).r; + if (strokeOrder3d) { + gl_FragDepth = depth; + } + else { + gl_FragDepth = (depth != 0.0) ? gl_FragCoord.z : 1.0; + } +} diff --git a/source/blender/draw/engines/gpencil/shaders/grease_pencil_depth_merge_vert.glsl b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_depth_merge_vert.glsl similarity index 100% rename from source/blender/draw/engines/gpencil/shaders/grease_pencil_depth_merge_vert.glsl rename to source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_depth_merge_vert.glsl diff --git a/source/blender/draw/engines/gpencil/shaders/grease_pencil_frag.glsl b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_frag.glsl similarity index 100% rename from source/blender/draw/engines/gpencil/shaders/grease_pencil_frag.glsl rename to source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_frag.glsl diff --git a/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_layer_blend_frag.glsl b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_layer_blend_frag.glsl new file mode 100644 index 00000000000..d00f6cfedfe --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_layer_blend_frag.glsl @@ -0,0 +1,23 @@ +/* SPDX-FileCopyrightText: 2020-2022 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma BLENDER_REQUIRE(gpencil_blend_mode.glsl) + +void main() +{ + vec4 color; + + /* Remember, this is associated alpha (aka. pre-multiply). */ + color.rgb = textureLod(colorBuf, uvcoordsvar.xy, 0).rgb; + /* Stroke only render mono-chromatic revealage. We convert to alpha. */ + color.a = 1.0 - textureLod(revealBuf, uvcoordsvar.xy, 0).r; + + float mask = textureLod(maskBuf, uvcoordsvar.xy, 0).r; + mask *= blendOpacity; + + fragColor = vec4(1.0, 0.0, 1.0, 1.0); + fragRevealage = vec4(1.0, 0.0, 1.0, 1.0); + + blend_mode_output(blendMode, color, mask, fragColor, fragRevealage); +} diff --git a/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_mask_invert_frag.glsl b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_mask_invert_frag.glsl new file mode 100644 index 00000000000..7faaa1cf2be --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_mask_invert_frag.glsl @@ -0,0 +1,9 @@ +/* SPDX-FileCopyrightText: 2020-2022 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +void main() +{ + /* Blend mode does the inversion. */ + fragRevealage = fragColor = vec4(1.0); +} diff --git a/source/blender/draw/engines/gpencil/shaders/grease_pencil_vert.glsl b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_vert.glsl similarity index 100% rename from source/blender/draw/engines/gpencil/shaders/grease_pencil_vert.glsl rename to source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_vert.glsl diff --git a/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_vfx_frag.glsl b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_vfx_frag.glsl new file mode 100644 index 00000000000..f5713663b64 --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/shaders/grease_pencil_vfx_frag.glsl @@ -0,0 +1,299 @@ +/* SPDX-FileCopyrightText: 2020-2022 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma BLENDER_REQUIRE(gpencil_blend_mode.glsl) + +float gaussian_weight(float x) +{ + return exp(-x * x / (2.0 * 0.35 * 0.35)); +} + +#if defined(COMPOSITE) + +void main() +{ + if (isFirstPass) { + /* Blend mode is multiply. */ + fragColor.rgb = fragRevealage.rgb = texture(revealBuf, uvcoordsvar.xy).rgb; + fragColor.a = fragRevealage.a = 1.0; + } + else { + /* Blend mode is additive. */ + fragRevealage = vec4(0.0); + fragColor.rgb = texture(colorBuf, uvcoordsvar.xy).rgb; + fragColor.a = 0.0; + } +} + +#elif defined(COLORIZE) + +const mat3 sepia_mat = mat3( + vec3(0.393, 0.349, 0.272), vec3(0.769, 0.686, 0.534), vec3(0.189, 0.168, 0.131)); + +# define MODE_GRAYSCALE 0 +# define MODE_SEPIA 1 +# define MODE_DUOTONE 2 +# define MODE_CUSTOM 3 +# define MODE_TRANSPARENT 4 + +void main() +{ + fragColor = texture(colorBuf, uvcoordsvar.xy); + fragRevealage = texture(revealBuf, uvcoordsvar.xy); + + float luma = dot(fragColor.rgb, vec3(0.2126, 0.7152, 0.723)); + + /* No blending. */ + switch (mode) { + case MODE_GRAYSCALE: + fragColor.rgb = mix(fragColor.rgb, vec3(luma), factor); + break; + case MODE_SEPIA: + fragColor.rgb = mix(fragColor.rgb, sepia_mat * fragColor.rgb, factor); + break; + case MODE_DUOTONE: + fragColor.rgb = luma * ((luma <= factor) ? lowColor : highColor); + break; + case MODE_CUSTOM: + fragColor.rgb = mix(fragColor.rgb, luma * lowColor, factor); + break; + case MODE_TRANSPARENT: + default: + fragColor.rgb *= factor; + fragRevealage.rgb = mix(vec3(1.0), fragRevealage.rgb, factor); + break; + } +} + +#elif defined(BLUR) + +void main() +{ + vec2 pixel_size = 1.0 / vec2(textureSize(revealBuf, 0).xy); + vec2 ofs = offset * pixel_size; + + fragColor = vec4(0.0); + fragRevealage = vec4(0.0); + + /* No blending. */ + float weight_accum = 0.0; + for (int i = -sampCount; i <= sampCount; i++) { + float x = float(i) / float(sampCount); + float weight = gaussian_weight(x); + weight_accum += weight; + vec2 uv = uvcoordsvar.xy + ofs * x; + fragColor.rgb += texture(colorBuf, uv).rgb * weight; + fragRevealage.rgb += texture(revealBuf, uv).rgb * weight; + } + + fragColor /= weight_accum; + fragRevealage /= weight_accum; +} + +#elif defined(TRANSFORM) + +void main() +{ + vec2 uv = (uvcoordsvar.xy - 0.5) * axisFlip + 0.5; + + /* Wave deform. */ + float wave_time = dot(uv, waveDir.xy); + uv += sin(wave_time + wavePhase) * waveOffset; + /* Swirl deform. */ + if (swirlRadius > 0.0) { + vec2 tex_size = vec2(textureSize(colorBuf, 0).xy); + vec2 pix_coord = uv * tex_size - swirlCenter; + float dist = length(pix_coord); + float percent = clamp((swirlRadius - dist) / swirlRadius, 0.0, 1.0); + float theta = percent * percent * swirlAngle; + float s = sin(theta); + float c = cos(theta); + mat2 rot = mat2(vec2(c, -s), vec2(s, c)); + uv = (rot * pix_coord + swirlCenter) / tex_size; + } + + fragColor = texture(colorBuf, uv); + fragRevealage = texture(revealBuf, uv); +} + +#elif defined(GLOW) + +void main() +{ + vec2 pixel_size = 1.0 / vec2(textureSize(revealBuf, 0).xy); + vec2 ofs = offset * pixel_size; + + fragColor = vec4(0.0); + fragRevealage = vec4(0.0); + + float weight_accum = 0.0; + for (int i = -sampCount; i <= sampCount; i++) { + float x = float(i) / float(sampCount); + float weight = gaussian_weight(x); + weight_accum += weight; + vec2 uv = uvcoordsvar.xy + ofs * x; + vec3 col = texture(colorBuf, uv).rgb; + vec3 rev = texture(revealBuf, uv).rgb; + if (threshold.x > -1.0) { + if (threshold.y > -1.0) { + if (any(greaterThan(abs(col - vec3(threshold)), vec3(threshold.w)))) { + weight = 0.0; + } + } + else { + if (dot(col, vec3(1.0 / 3.0)) < threshold.x) { + weight = 0.0; + } + } + } + fragColor.rgb += col * weight; + fragRevealage.rgb += (1.0 - rev) * weight; + } + + if (weight_accum > 0.0) { + fragColor *= glowColor.rgbb / weight_accum; + fragRevealage = fragRevealage / weight_accum; + } + fragRevealage = 1.0 - fragRevealage; + + if (glowUnder) { + if (firstPass) { + /* In first pass we copy the revealage buffer in the alpha channel. + * This let us do the alpha under in second pass. */ + vec3 original_revealage = texture(revealBuf, uvcoordsvar.xy).rgb; + fragRevealage.a = clamp(dot(original_revealage.rgb, vec3(0.333334)), 0.0, 1.0); + } + else { + /* Recover original revealage. */ + fragRevealage.a = texture(revealBuf, uvcoordsvar.xy).a; + } + } + + if (!firstPass) { + fragColor.a = clamp(1.0 - dot(fragRevealage.rgb, vec3(0.333334)), 0.0, 1.0); + fragRevealage.a *= glowColor.a; + blend_mode_output(blendMode, fragColor, fragRevealage.a, fragColor, fragRevealage); + } +} + +#elif defined(RIM) + +void main() +{ + /* Blur revealage buffer. */ + fragRevealage = vec4(0.0); + float weight_accum = 0.0; + for (int i = -sampCount; i <= sampCount; i++) { + float x = float(i) / float(sampCount); + float weight = gaussian_weight(x); + weight_accum += weight; + vec2 uv = uvcoordsvar.xy + blurDir * x + uvOffset; + vec3 col = texture(revealBuf, uv).rgb; + if (any(not(equal(vec2(0.0), floor(uv))))) { + col = vec3(0.0); + } + fragRevealage.rgb += col * weight; + } + fragRevealage /= weight_accum; + + if (isFirstPass) { + /* In first pass we copy the reveal buffer. This let us do alpha masking in second pass. */ + fragColor = texture(revealBuf, uvcoordsvar.xy); + /* Also add the masked color to the reveal buffer. */ + vec3 col = texture(colorBuf, uvcoordsvar.xy).rgb; + if (all(lessThan(abs(col - maskColor), vec3(0.05)))) { + fragColor = vec4(1.0); + } + } + else { + /* Pre-multiply by foreground alpha (alpha mask). */ + float mask = 1.0 - clamp(dot(vec3(0.333334), texture(colorBuf, uvcoordsvar.xy).rgb), 0.0, 1.0); + + /* fragRevealage is blurred shadow. */ + float rim = clamp(dot(vec3(0.333334), fragRevealage.rgb), 0.0, 1.0); + + vec4 color = vec4(rimColor, 1.0); + + blend_mode_output(blendMode, color, rim * mask, fragColor, fragRevealage); + } +} + +#elif defined(SHADOW) + +vec2 compute_uvs(float x) +{ + vec2 uv = uvcoordsvar.xy; + /* Transform UV (loc, rot, scale) */ + uv = uv.x * uvRotX + uv.y * uvRotY + uvOffset; + uv += blurDir * x; + /* Wave deform. */ + float wave_time = dot(uv, waveDir.xy); + uv += sin(wave_time + wavePhase) * waveOffset; + return uv; +} + +void main() +{ + /* Blur revealage buffer. */ + fragRevealage = vec4(0.0); + float weight_accum = 0.0; + for (int i = -sampCount; i <= sampCount; i++) { + float x = float(i) / float(sampCount); + float weight = gaussian_weight(x); + weight_accum += weight; + vec2 uv = compute_uvs(x); + vec3 col = texture(revealBuf, uv).rgb; + if (any(not(equal(vec2(0.0), floor(uv))))) { + col = vec3(1.0); + } + fragRevealage.rgb += col * weight; + } + fragRevealage /= weight_accum; + + /* No blending in first pass, alpha over pre-multiply in second pass. */ + if (isFirstPass) { + /* In first pass we copy the reveal buffer. This let us do alpha under in second pass. */ + fragColor = texture(revealBuf, uvcoordsvar.xy); + } + else { + /* fragRevealage is blurred shadow. */ + float shadow_fac = 1.0 - clamp(dot(vec3(0.333334), fragRevealage.rgb), 0.0, 1.0); + /* Pre-multiply by foreground revealage (alpha under). */ + vec3 original_revealage = texture(colorBuf, uvcoordsvar.xy).rgb; + shadow_fac *= clamp(dot(vec3(0.333334), original_revealage), 0.0, 1.0); + /* Modulate by opacity */ + shadow_fac *= shadowColor.a; + /* Apply shadow color. */ + fragColor.rgb = mix(vec3(0.0), shadowColor.rgb, shadow_fac); + /* Alpha over (mask behind the shadow). */ + fragColor.a = shadow_fac; + + fragRevealage.rgb = original_revealage * (1.0 - shadow_fac); + /* Replace the whole revealage buffer. */ + fragRevealage.a = 1.0; + } +} + +#elif defined(PIXELIZE) + +void main() +{ + vec2 pixel = floor((uvcoordsvar.xy - targetPixelOffset) / targetPixelSize); + vec2 uv = (pixel + 0.5) * targetPixelSize + targetPixelOffset; + + fragColor = vec4(0.0); + fragRevealage = vec4(0.0); + + for (int i = -sampCount; i <= sampCount; i++) { + float x = float(i) / float(sampCount + 1); + vec2 uv_ofs = uv + accumOffset * 0.5 * x; + fragColor += texture(colorBuf, uv_ofs); + fragRevealage += texture(revealBuf, uv_ofs); + } + + fragColor /= float(sampCount) * 2.0 + 1.0; + fragRevealage /= float(sampCount) * 2.0 + 1.0; +} + +#endif diff --git a/source/blender/draw/engines/grease_pencil_v3/shaders/infos/grease_pencil_info.hh b/source/blender/draw/engines/grease_pencil_v3/shaders/infos/grease_pencil_info.hh new file mode 100644 index 00000000000..7e1cbb4ea72 --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/shaders/infos/grease_pencil_info.hh @@ -0,0 +1,152 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "gpu_shader_create_info.hh" + +/* -------------------------------------------------------------------- */ +/** \name Grease Pencil Object rendering + * \{ */ + +GPU_SHADER_INTERFACE_INFO(grease_pencil_geometry_iface, "gp_interp") + .smooth(Type::VEC4, "color_mul") + .smooth(Type::VEC4, "color_add") + .smooth(Type::VEC3, "pos") + .smooth(Type::VEC2, "uv"); +GPU_SHADER_INTERFACE_INFO(grease_pencil_geometry_flat_iface, "gp_interp_flat") + .flat(Type::VEC2, "aspect") + .flat(Type::VEC4, "sspos") + .flat(Type::UINT, "mat_flag") + .flat(Type::FLOAT, "depth"); +GPU_SHADER_INTERFACE_INFO(grease_pencil_geometry_noperspective_iface, "gp_interp_noperspective") + .no_perspective(Type::VEC2, "thickness") + .no_perspective(Type::FLOAT, "hardness"); + +GPU_SHADER_CREATE_INFO(grease_pencil_geometry) + .do_static_compilation(true) + .define("GP_LIGHT") + .typedef_source("grease_pencil_defines.hh") + .sampler(GPENCIL_SCENE_DEPTH_TEX_SLOT, ImageType::DEPTH_2D, "gpSceneDepthTexture") + .sampler(GPENCIL_MASK_TEX_SLOT, ImageType::FLOAT_2D, "gpMaskTexture") + .sampler(GPENCIL_FILL_TEX_SLOT, ImageType::FLOAT_2D, "gpFillTexture") + .sampler(GPENCIL_STROKE_TEX_SLOT, ImageType::FLOAT_2D, "gpStrokeTexture") + .storage_buf(GPENCIL_OBJECT_SLOT, Qualifier::READ, "gpObject", "gp_object[]") + .storage_buf(GPENCIL_LAYER_SLOT, Qualifier::READ, "gpLayer", "gp_layer[]") + .storage_buf(GPENCIL_MATERIAL_SLOT, Qualifier::READ, "gpMaterial", "gp_materials[]") + .storage_buf(GPENCIL_LIGHT_SLOT, Qualifier::READ, "gpLight", "gp_lights[]") + .uniform_buf(GPENCIL_SCENE_SLOT, "gpScene", "gp_scene") + /* Per Scene */ + .define("viewportSize", "gp_scene.render_size") + /* Per Object */ + .define("gpNormal", "gp_object[resource_id].normal") + .define("gpStrokeOrder3d", "gp_object[resource_id].stroke_order3d") + .define("gpMaterialOffset", "gp_object[resource_id].material_offset") + /* Per Layer */ + .define("layer_id", "gp_object[resource_id].layer_offset") /* TODO */ + .define("gpVertexColorOpacity", "gp_layer[layer_id].vertex_color_opacity") + .define("gpLayerTint", "gp_layer[layer_id].tint") + .define("gpLayerOpacity", "gp_layer[layer_id].opacity") + .define("gpStrokeIndexOffset", "gp_layer[layer_id].stroke_index_offset") + .fragment_out(0, Type::VEC4, "fragColor") + .fragment_out(1, Type::VEC4, "revealColor") + .vertex_out(grease_pencil_geometry_iface) + .vertex_out(grease_pencil_geometry_flat_iface) + .vertex_out(grease_pencil_geometry_noperspective_iface) + .vertex_source("grease_pencil_vert.glsl") + .fragment_source("grease_pencil_frag.glsl") + .additional_info("draw_grease_pencil") + .depth_write(DepthWrite::ANY); + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Full-Screen Shaders + * \{ */ + +GPU_SHADER_CREATE_INFO(grease_pencil_layer_blend) + .do_static_compilation(true) + .sampler(0, ImageType::FLOAT_2D, "colorBuf") + .sampler(1, ImageType::FLOAT_2D, "revealBuf") + .sampler(2, ImageType::FLOAT_2D, "maskBuf") + .push_constant(Type::INT, "blendMode") + .push_constant(Type::FLOAT, "blendOpacity") + /* Reminder: This is considered SRC color in blend equations. + * Same operation on all buffers. */ + .fragment_out(0, Type::VEC4, "fragColor") + .fragment_out(1, Type::VEC4, "fragRevealage") + .fragment_source("grease_pencil_layer_blend_frag.glsl") + .additional_info("draw_fullscreen"); + +GPU_SHADER_CREATE_INFO(grease_pencil_mask_invert) + .do_static_compilation(true) + .fragment_out(0, Type::VEC4, "fragColor") + .fragment_out(1, Type::VEC4, "fragRevealage") + .fragment_source("grease_pencil_mask_invert_frag.glsl") + .additional_info("draw_fullscreen"); + +GPU_SHADER_CREATE_INFO(grease_pencil_depth_merge) + .do_static_compilation(true) + .define("strokeOrder3d", "false") + .sampler(0, ImageType::DEPTH_2D, "depthBuf") + .vertex_in(0, Type::VEC3, "pos") + .vertex_source("grease_pencil_depth_merge_vert.glsl") + .fragment_source("grease_pencil_depth_merge_frag.glsl") + .depth_write(DepthWrite::ANY) + .additional_info("draw_modelmat_new", "draw_view"); + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Anti-Aliasing + * \{ */ + +GPU_SHADER_INTERFACE_INFO(grease_pencil_antialiasing_iface, "") + .smooth(Type::VEC2, "uvs") + .smooth(Type::VEC2, "pixcoord") + .smooth(Type::VEC4, "offset[3]"); + +GPU_SHADER_CREATE_INFO(grease_pencil_antialiasing) + .define("SMAA_GLSL_3") + .define("SMAA_RT_METRICS", "viewportMetrics") + .define("SMAA_PRESET_HIGH") + .define("SMAA_LUMA_WEIGHT", "float4(lumaWeight, lumaWeight, lumaWeight, 0.0)") + .define("SMAA_NO_DISCARD") + .vertex_out(grease_pencil_antialiasing_iface) + .push_constant(Type::VEC4, "viewportMetrics") + .push_constant(Type::FLOAT, "lumaWeight") + .vertex_source("grease_pencil_antialiasing_vert.glsl") + .fragment_source("grease_pencil_antialiasing_frag.glsl"); + +GPU_SHADER_CREATE_INFO(grease_pencil_antialiasing_stage_0) + .define("SMAA_STAGE", "0") + .sampler(0, ImageType::FLOAT_2D, "colorTex") + .sampler(1, ImageType::FLOAT_2D, "revealTex") + .fragment_out(0, Type::VEC2, "out_edges") + .additional_info("grease_pencil_antialiasing") + .do_static_compilation(true); + +GPU_SHADER_CREATE_INFO(grease_pencil_antialiasing_stage_1) + .define("SMAA_STAGE", "1") + .sampler(0, ImageType::FLOAT_2D, "edgesTex") + .sampler(1, ImageType::FLOAT_2D, "areaTex") + .sampler(2, ImageType::FLOAT_2D, "searchTex") + .fragment_out(0, Type::VEC4, "out_weights") + .additional_info("grease_pencil_antialiasing") + .do_static_compilation(true); + +GPU_SHADER_CREATE_INFO(grease_pencil_antialiasing_stage_2) + .define("SMAA_STAGE", "2") + .sampler(0, ImageType::FLOAT_2D, "colorTex") + .sampler(1, ImageType::FLOAT_2D, "revealTex") + .sampler(2, ImageType::FLOAT_2D, "blendTex") + .push_constant(Type::FLOAT, "mixFactor") + .push_constant(Type::FLOAT, "taaAccumulatedWeight") + .push_constant(Type::BOOL, "doAntiAliasing") + .push_constant(Type::BOOL, "onlyAlpha") + /* Reminder: Blending func is `fragRevealage * DST + fragColor`. */ + .fragment_out(0, Type::VEC4, "out_color", DualBlend::SRC_0) + .fragment_out(0, Type::VEC4, "out_reveal", DualBlend::SRC_1) + .additional_info("grease_pencil_antialiasing") + .do_static_compilation(true); + +/** \} */ diff --git a/source/blender/draw/engines/grease_pencil_v3/shaders/infos/grease_pencil_vfx_info.hh b/source/blender/draw/engines/grease_pencil_v3/shaders/infos/grease_pencil_vfx_info.hh new file mode 100644 index 00000000000..f3ddde695a3 --- /dev/null +++ b/source/blender/draw/engines/grease_pencil_v3/shaders/infos/grease_pencil_vfx_info.hh @@ -0,0 +1,96 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "gpu_shader_create_info.hh" + +GPU_SHADER_CREATE_INFO(grease_pencil_fx_common) + .sampler(0, ImageType::FLOAT_2D, "colorBuf") + .sampler(1, ImageType::FLOAT_2D, "revealBuf") + /* Reminder: This is considered SRC color in blend equations. + * Same operation on all buffers. */ + .fragment_out(0, Type::VEC4, "fragColor") + .fragment_out(1, Type::VEC4, "fragRevealage") + .fragment_source("grease_pencil_vfx_frag.glsl"); + +GPU_SHADER_CREATE_INFO(grease_pencil_fx_composite) + .do_static_compilation(true) + .define("COMPOSITE") + .push_constant(Type::BOOL, "isFirstPass") + .additional_info("grease_pencil_fx_common", "draw_fullscreen"); + +GPU_SHADER_CREATE_INFO(grease_pencil_fx_colorize) + .do_static_compilation(true) + .define("COLORIZE") + .push_constant(Type::VEC3, "lowColor") + .push_constant(Type::VEC3, "highColor") + .push_constant(Type::FLOAT, "factor") + .push_constant(Type::INT, "mode") + .additional_info("grease_pencil_fx_common", "draw_fullscreen"); + +GPU_SHADER_CREATE_INFO(grease_pencil_fx_blur) + .do_static_compilation(true) + .define("BLUR") + .push_constant(Type::VEC2, "offset") + .push_constant(Type::INT, "sampCount") + .additional_info("grease_pencil_fx_common", "draw_fullscreen"); + +GPU_SHADER_CREATE_INFO(grease_pencil_fx_transform) + .do_static_compilation(true) + .define("TRANSFORM") + .push_constant(Type::VEC2, "axisFlip") + .push_constant(Type::VEC2, "waveDir") + .push_constant(Type::VEC2, "waveOffset") + .push_constant(Type::FLOAT, "wavePhase") + .push_constant(Type::VEC2, "swirlCenter") + .push_constant(Type::FLOAT, "swirlAngle") + .push_constant(Type::FLOAT, "swirlRadius") + .additional_info("grease_pencil_fx_common", "draw_fullscreen"); + +GPU_SHADER_CREATE_INFO(grease_pencil_fx_glow) + .do_static_compilation(true) + .define("GLOW") + .push_constant(Type::VEC4, "glowColor") + .push_constant(Type::VEC2, "offset") + .push_constant(Type::INT, "sampCount") + .push_constant(Type::VEC4, "threshold") + .push_constant(Type::BOOL, "firstPass") + .push_constant(Type::BOOL, "glowUnder") + .push_constant(Type::INT, "blendMode") + .additional_info("grease_pencil_fx_common", "draw_fullscreen"); + +GPU_SHADER_CREATE_INFO(grease_pencil_fx_rim) + .do_static_compilation(true) + .define("RIM") + .push_constant(Type::VEC2, "blurDir") + .push_constant(Type::VEC2, "uvOffset") + .push_constant(Type::VEC3, "rimColor") + .push_constant(Type::VEC3, "maskColor") + .push_constant(Type::INT, "sampCount") + .push_constant(Type::INT, "blendMode") + .push_constant(Type::BOOL, "isFirstPass") + .additional_info("grease_pencil_fx_common", "draw_fullscreen"); + +GPU_SHADER_CREATE_INFO(grease_pencil_fx_shadow) + .do_static_compilation(true) + .define("SHADOW") + .push_constant(Type::VEC4, "shadowColor") + .push_constant(Type::VEC2, "uvRotX") + .push_constant(Type::VEC2, "uvRotY") + .push_constant(Type::VEC2, "uvOffset") + .push_constant(Type::VEC2, "blurDir") + .push_constant(Type::VEC2, "waveDir") + .push_constant(Type::VEC2, "waveOffset") + .push_constant(Type::FLOAT, "wavePhase") + .push_constant(Type::INT, "sampCount") + .push_constant(Type::BOOL, "isFirstPass") + .additional_info("grease_pencil_fx_common", "draw_fullscreen"); + +GPU_SHADER_CREATE_INFO(grease_pencil_fx_pixelize) + .do_static_compilation(true) + .define("PIXELIZE") + .push_constant(Type::VEC2, "targetPixelSize") + .push_constant(Type::VEC2, "targetPixelOffset") + .push_constant(Type::VEC2, "accumOffset") + .push_constant(Type::INT, "sampCount") + .additional_info("grease_pencil_fx_common", "draw_fullscreen"); diff --git a/source/blender/draw/intern/draw_cache_impl_grease_pencil.cc b/source/blender/draw/intern/draw_cache_impl_grease_pencil.cc index e49d894be71..74226a14f59 100644 --- a/source/blender/draw/intern/draw_cache_impl_grease_pencil.cc +++ b/source/blender/draw/intern/draw_cache_impl_grease_pencil.cc @@ -27,8 +27,8 @@ #include "draw_cache_impl.hh" -#include "../engines/gpencil/gpencil_defines.h" -#include "../engines/gpencil/gpencil_shader_shared.h" +#include "../engines/grease_pencil_v3/grease_pencil_defines.hh" +#include "../engines/grease_pencil_v3/grease_pencil_shader_shared.h" namespace blender::draw { diff --git a/source/blender/draw/intern/draw_manager_c.cc b/source/blender/draw/intern/draw_manager_c.cc index e90858a1ea5..e506c20008b 100644 --- a/source/blender/draw/intern/draw_manager_c.cc +++ b/source/blender/draw/intern/draw_manager_c.cc @@ -92,6 +92,7 @@ #include "engines/eevee_next/eevee_engine.h" #include "engines/external/external_engine.h" #include "engines/gpencil/gpencil_engine.h" +#include "engines/grease_pencil_v3/grease_pencil_engine.h" #include "engines/image/image_engine.h" #include "engines/overlay/overlay_engine.h" #include "engines/select/select_engine.hh" diff --git a/source/blender/draw/intern/shaders/draw_view_info.hh b/source/blender/draw/intern/shaders/draw_view_info.hh index f7e0cbf8ce6..8d43e382159 100644 --- a/source/blender/draw/intern/shaders/draw_view_info.hh +++ b/source/blender/draw/intern/shaders/draw_view_info.hh @@ -165,8 +165,8 @@ GPU_SHADER_CREATE_INFO(draw_gpencil) .push_constant(Type::FLOAT, "gpThicknessOffset") .additional_info("draw_modelmat", "draw_object_infos"); -GPU_SHADER_CREATE_INFO(draw_gpencil_new) - .typedef_source("gpencil_shader_shared.h") +GPU_SHADER_CREATE_INFO(draw_grease_pencil) + .typedef_source("grease_pencil_shader_shared.h") .define("DRW_GPENCIL_INFO") .sampler(0, ImageType::FLOAT_BUFFER, "gp_pos_tx") .sampler(1, ImageType::FLOAT_BUFFER, "gp_col_tx") diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index b265fab17b4..7ba85f63287 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -691,6 +691,8 @@ set(SRC_SHADER_CREATE_INFOS ../draw/engines/eevee_next/shaders/infos/eevee_volume_info.hh ../draw/engines/gpencil/shaders/infos/gpencil_info.hh ../draw/engines/gpencil/shaders/infos/gpencil_vfx_info.hh + ../draw/engines/grease_pencil_v3/shaders/infos/grease_pencil_info.hh + ../draw/engines/grease_pencil_v3/shaders/infos/grease_pencil_vfx_info.hh ../draw/engines/overlay/shaders/infos/overlay_antialiasing_info.hh ../draw/engines/overlay/shaders/infos/overlay_armature_info.hh ../draw/engines/overlay/shaders/infos/overlay_background_info.hh