diff options
author | Hans Goudey <h.goudey@me.com> | 2022-07-26 16:37:08 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-07-26 16:37:38 +0300 |
commit | 1998269b109f65373336087da7f9751a3c3317f3 (patch) | |
tree | 5aa1123e516fd2874fa6626dcba193aec88cacde /source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_attributes.cc | |
parent | 5945a90df99b919229c876c2fd294f035e24831a (diff) |
Refactor: Extract color attributes as generic attributes
Previously there was a special extraction process for "vertex colors"
that copied the color data to the GPU with a special format. Instead,
this patch replaces this with use of the generic attribute extraction.
This reduces the number of code paths, allowing easier optimization
in the future.
To make it possible to use the generic extraction system for attributes
but also assign aliases for use by shaders, some changes are necessary.
First, the GPU material attribute can now store whether it actually refers
to the default color attribute, rather than a specific name. This replaces
the hack to use `CD_MCOL` in the color attribute shader node. Second,
the extraction code checks the names against the default and active
names and assigns aliases if the request corresponds to a special active
attribute. Finally, support for byte color attributes was added to the
generic attribute extraction.
Differential Revision: https://developer.blender.org/D15205
Diffstat (limited to 'source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_attributes.cc')
-rw-r--r-- | source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_attributes.cc | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_attributes.cc b/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_attributes.cc index b98af3be17f..7f16837022c 100644 --- a/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_attributes.cc +++ b/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_attributes.cc @@ -9,6 +9,7 @@ #include <functional> +#include "BLI_color.hh" #include "BLI_math_vec_types.hh" #include "BLI_string.h" @@ -74,6 +75,18 @@ template<> struct AttributeTypeConverter<MPropCol, gpuMeshCol> { } }; +template<> struct AttributeTypeConverter<ColorGeometry4b, gpuMeshCol> { + static gpuMeshCol convert_value(ColorGeometry4b value) + { + gpuMeshCol result; + result.r = unit_float_to_ushort_clamp(BLI_color_from_srgb_table[value.r]); + result.g = unit_float_to_ushort_clamp(BLI_color_from_srgb_table[value.g]); + result.b = unit_float_to_ushort_clamp(BLI_color_from_srgb_table[value.b]); + result.a = unit_float_to_ushort_clamp(value.a * (1.0f / 255.0f)); + return result; + } +}; + /* Return the number of component for the attribute's value type, or 0 if is it unsupported. */ static uint gpu_component_size_for_attribute_type(eCustomDataType type) { @@ -90,6 +103,7 @@ static uint gpu_component_size_for_attribute_type(eCustomDataType type) case CD_PROP_FLOAT3: return 3; case CD_PROP_COLOR: + case CD_PROP_BYTE_COLOR: return 4; default: return 0; @@ -102,6 +116,7 @@ static GPUVertFetchMode get_fetch_mode_for_type(eCustomDataType type) case CD_PROP_INT32: return GPU_FETCH_INT_TO_FLOAT; case CD_PROP_COLOR: + case CD_PROP_BYTE_COLOR: return GPU_FETCH_INT_TO_FLOAT_UNIT; default: return GPU_FETCH_FLOAT; @@ -114,13 +129,15 @@ static GPUVertCompType get_comp_type_for_type(eCustomDataType type) case CD_PROP_INT32: return GPU_COMP_I32; case CD_PROP_COLOR: + case CD_PROP_BYTE_COLOR: return GPU_COMP_U16; default: return GPU_COMP_F32; } } -static void init_vbo_for_attribute(GPUVertBuf *vbo, +static void init_vbo_for_attribute(const MeshRenderData &mr, + GPUVertBuf *vbo, const DRW_AttributeRequest &request, bool build_on_device, uint32_t len) @@ -140,6 +157,13 @@ static void init_vbo_for_attribute(GPUVertBuf *vbo, GPU_vertformat_deinterleave(&format); GPU_vertformat_attr_add(&format, attr_name, comp_type, comp_size, fetch_mode); + if (mr.active_color_name && STREQ(request.attribute_name, mr.active_color_name)) { + GPU_vertformat_alias_add(&format, "ac"); + } + if (mr.default_color_name && STREQ(request.attribute_name, mr.default_color_name)) { + GPU_vertformat_alias_add(&format, "c"); + } + if (build_on_device) { GPU_vertbuf_init_build_on_device(vbo, &format, len); } @@ -262,7 +286,7 @@ static void extract_attr_init( GPUVertBuf *vbo = static_cast<GPUVertBuf *>(buf); - init_vbo_for_attribute(vbo, request, false, static_cast<uint32_t>(mr->loop_len)); + init_vbo_for_attribute(*mr, vbo, request, false, static_cast<uint32_t>(mr->loop_len)); /* TODO(@kevindietrich): float3 is used for scalar attributes as the implicit conversion done by * OpenGL to vec4 for a scalar `s` will produce a `vec4(s, 0, 0, 1)`. However, following the @@ -290,6 +314,9 @@ static void extract_attr_init( case CD_PROP_COLOR: extract_attr_generic<MPropCol, gpuMeshCol>(mr, vbo, request); break; + case CD_PROP_BYTE_COLOR: + extract_attr_generic<ColorGeometry4b, gpuMeshCol>(mr, vbo, request); + break; default: BLI_assert_unreachable(); } @@ -338,12 +365,15 @@ static void extract_attr_init_subdiv(const DRWSubdivCache *subdiv_cache, case CD_PROP_COLOR: extract_attr_generic<MPropCol, gpuMeshCol>(mr, src_data, request); break; + case CD_PROP_BYTE_COLOR: + extract_attr_generic<ColorGeometry4b, gpuMeshCol>(mr, src_data, request); + break; default: BLI_assert_unreachable(); } GPUVertBuf *dst_buffer = static_cast<GPUVertBuf *>(buffer); - init_vbo_for_attribute(dst_buffer, request, true, subdiv_cache->num_subdiv_loops); + init_vbo_for_attribute(*mr, dst_buffer, request, true, subdiv_cache->num_subdiv_loops); /* Ensure data is uploaded properly. */ GPU_vertbuf_tag_dirty(src_data); @@ -352,7 +382,7 @@ static void extract_attr_init_subdiv(const DRWSubdivCache *subdiv_cache, dst_buffer, static_cast<int>(dimensions), 0, - request.cd_type == CD_PROP_COLOR); + ELEM(request.cd_type, CD_PROP_COLOR, CD_PROP_BYTE_COLOR)); GPU_vertbuf_discard(src_data); } |