diff --git a/source/blender/editors/space_node/drawnode.cc b/source/blender/editors/space_node/drawnode.cc index 7c700575593..ea5bfb44a34 100644 --- a/source/blender/editors/space_node/drawnode.cc +++ b/source/blender/editors/space_node/drawnode.cc @@ -1804,47 +1804,15 @@ static float mute_expand_axis[3][2] = {{1.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, -0.0f} /* Is zero initialized because it is static data. */ static struct { - gpu::Batch *batch; /* for batching line together */ - gpu::Batch *batch_single; /* for single line */ - gpu::VertBuf *inst_vbo; - uint p0_id, p1_id, p2_id, p3_id; - uint colid_id, muted_id, start_color_id, end_color_id; - uint dim_factor_id; - uint thickness_id; - uint dash_params_id; - uint has_back_link_id; - GPUVertBufRaw p0_step, p1_step, p2_step, p3_step; - GPUVertBufRaw colid_step, muted_step, start_color_step, end_color_step; - GPUVertBufRaw dim_factor_step; - GPUVertBufRaw thickness_step; - GPUVertBufRaw dash_params_step; - GPUVertBufRaw has_back_link_step; + gpu::Batch *batch; + gpu::StorageBuf *link_buf; uint count; bool enabled; + NodeLinkData data[NODELINK_GROUP_SIZE]; } g_batch_link; static void nodelink_batch_reset() { - GPU_vertbuf_attr_get_raw_data(g_batch_link.inst_vbo, g_batch_link.p0_id, &g_batch_link.p0_step); - GPU_vertbuf_attr_get_raw_data(g_batch_link.inst_vbo, g_batch_link.p1_id, &g_batch_link.p1_step); - GPU_vertbuf_attr_get_raw_data(g_batch_link.inst_vbo, g_batch_link.p2_id, &g_batch_link.p2_step); - GPU_vertbuf_attr_get_raw_data(g_batch_link.inst_vbo, g_batch_link.p3_id, &g_batch_link.p3_step); - GPU_vertbuf_attr_get_raw_data( - g_batch_link.inst_vbo, g_batch_link.colid_id, &g_batch_link.colid_step); - GPU_vertbuf_attr_get_raw_data( - g_batch_link.inst_vbo, g_batch_link.muted_id, &g_batch_link.muted_step); - GPU_vertbuf_attr_get_raw_data( - g_batch_link.inst_vbo, g_batch_link.dim_factor_id, &g_batch_link.dim_factor_step); - GPU_vertbuf_attr_get_raw_data( - g_batch_link.inst_vbo, g_batch_link.thickness_id, &g_batch_link.thickness_step); - GPU_vertbuf_attr_get_raw_data( - g_batch_link.inst_vbo, g_batch_link.dash_params_id, &g_batch_link.dash_params_step); - GPU_vertbuf_attr_get_raw_data( - g_batch_link.inst_vbo, g_batch_link.has_back_link_id, &g_batch_link.has_back_link_step); - GPU_vertbuf_attr_get_raw_data( - g_batch_link.inst_vbo, g_batch_link.start_color_id, &g_batch_link.start_color_step); - GPU_vertbuf_attr_get_raw_data( - g_batch_link.inst_vbo, g_batch_link.end_color_id, &g_batch_link.end_color_step); g_batch_link.count = 0; } @@ -1944,41 +1912,8 @@ static void nodelink_batch_init() g_batch_link.batch = GPU_batch_create_ex(GPU_PRIM_TRI_STRIP, vbo, nullptr, GPU_BATCH_OWNS_VBO); gpu_batch_presets_register(g_batch_link.batch); - g_batch_link.batch_single = GPU_batch_create_ex( - GPU_PRIM_TRI_STRIP, vbo, nullptr, GPU_BATCH_INVALID); - gpu_batch_presets_register(g_batch_link.batch_single); - /* Instances data */ - GPUVertFormat format_inst = {0}; - g_batch_link.p0_id = GPU_vertformat_attr_add( - &format_inst, "P0", blender::gpu::VertAttrType::SFLOAT_32_32); - g_batch_link.p1_id = GPU_vertformat_attr_add( - &format_inst, "P1", blender::gpu::VertAttrType::SFLOAT_32_32); - g_batch_link.p2_id = GPU_vertformat_attr_add( - &format_inst, "P2", blender::gpu::VertAttrType::SFLOAT_32_32); - g_batch_link.p3_id = GPU_vertformat_attr_add( - &format_inst, "P3", blender::gpu::VertAttrType::SFLOAT_32_32); - g_batch_link.colid_id = GPU_vertformat_attr_add( - &format_inst, "colid_doarrow", blender::gpu::VertAttrType::UINT_8_8_8_8); - g_batch_link.start_color_id = GPU_vertformat_attr_add( - &format_inst, "start_color", blender::gpu::VertAttrType::SFLOAT_32_32_32_32); - g_batch_link.end_color_id = GPU_vertformat_attr_add( - &format_inst, "end_color", blender::gpu::VertAttrType::SFLOAT_32_32_32_32); - g_batch_link.muted_id = GPU_vertformat_attr_add( - &format_inst, "domuted", blender::gpu::VertAttrType::UINT_32); - g_batch_link.dim_factor_id = GPU_vertformat_attr_add( - &format_inst, "dim_factor", blender::gpu::VertAttrType::SFLOAT_32); - g_batch_link.thickness_id = GPU_vertformat_attr_add( - &format_inst, "thickness", blender::gpu::VertAttrType::SFLOAT_32); - g_batch_link.dash_params_id = GPU_vertformat_attr_add( - &format_inst, "dash_params", blender::gpu::VertAttrType::SFLOAT_32_32_32); - g_batch_link.has_back_link_id = GPU_vertformat_attr_add( - &format_inst, "has_back_link", blender::gpu::VertAttrType::SINT_32); - g_batch_link.inst_vbo = GPU_vertbuf_create_with_format_ex(format_inst, GPU_USAGE_STREAM); - /* Alloc max count but only draw the range we need. */ - GPU_vertbuf_data_alloc(*g_batch_link.inst_vbo, NODELINK_GROUP_SIZE); - - GPU_batch_instbuf_set(g_batch_link.batch, g_batch_link.inst_vbo, true); + g_batch_link.link_buf = GPU_storagebuf_create(sizeof(NodeLinkData) * NODELINK_GROUP_SIZE); nodelink_batch_reset(); } @@ -2007,7 +1942,7 @@ static void nodelink_batch_draw(const SpaceNode &snode) } GPU_blend(GPU_BLEND_ALPHA); - NodeLinkInstanceData node_link_data; + NodeLinkUniformData node_link_data; UI_GetThemeColor4fv(TH_WIRE_INNER, node_link_data.colors[nodelink_get_color_id(TH_WIRE_INNER)]); UI_GetThemeColor4fv(TH_WIRE, node_link_data.colors[nodelink_get_color_id(TH_WIRE)]); @@ -2016,17 +1951,18 @@ static void nodelink_batch_draw(const SpaceNode &snode) node_link_data.colors[nodelink_get_color_id(TH_EDGE_SELECT)]); UI_GetThemeColor4fv(TH_REDALERT, node_link_data.colors[nodelink_get_color_id(TH_REDALERT)]); node_link_data.aspect = snode.runtime->aspect; - node_link_data.arrowSize = ARROW_SIZE; + node_link_data.arrow_size = ARROW_SIZE; gpu::UniformBuf *ubo = GPU_uniformbuf_create_ex( sizeof(node_link_data), &node_link_data, __func__); - GPU_vertbuf_data_len_set(*g_batch_link.inst_vbo, g_batch_link.count); - GPU_vertbuf_use(g_batch_link.inst_vbo); /* force update. */ + /* TODO(fclem): Update sub. */ + GPU_storagebuf_update(g_batch_link.link_buf, g_batch_link.data); - GPU_batch_program_set_builtin(g_batch_link.batch, GPU_SHADER_2D_NODELINK_INST); - GPU_batch_uniformbuf_bind(g_batch_link.batch, "node_link_data", ubo); - GPU_batch_draw(g_batch_link.batch); + GPU_batch_program_set_builtin(g_batch_link.batch, GPU_SHADER_2D_NODELINK); + GPU_batch_uniformbuf_bind(g_batch_link.batch, "link_uniforms", ubo); + GPU_storagebuf_bind(g_batch_link.link_buf, 0); + GPU_batch_draw_instance_range(g_batch_link.batch, 0, g_batch_link.count); GPU_uniformbuf_unbind(ubo); GPU_uniformbuf_free(ubo); @@ -2036,12 +1972,12 @@ static void nodelink_batch_draw(const SpaceNode &snode) GPU_blend(GPU_BLEND_NONE); } -void nodelink_batch_start(SpaceNode & /*snode*/) +void nodelink_batch_start(const SpaceNode & /*snode*/) { g_batch_link.enabled = true; } -void nodelink_batch_end(SpaceNode &snode) +void nodelink_batch_end(const SpaceNode &snode) { nodelink_batch_draw(snode); g_batch_link.enabled = false; @@ -2056,8 +1992,8 @@ struct NodeLinkDrawConfig { ColorTheme4f end_color; ColorTheme4f outline_color; - bool drawarrow; - bool drawmuted; + bool draw_arrow; + bool draw_muted; bool highlighted; bool has_back_link; @@ -2079,26 +2015,27 @@ static void nodelink_batch_add_link(const SpaceNode &snode, ELEM(draw_config.th_col2, TH_WIRE_INNER, TH_WIRE, TH_ACTIVE, TH_EDGE_SELECT, TH_REDALERT)); BLI_assert(ELEM(draw_config.th_col3, TH_WIRE, TH_REDALERT, -1)); - g_batch_link.count++; - copy_v2_v2((float *)GPU_vertbuf_raw_step(&g_batch_link.p0_step), points[0]); - copy_v2_v2((float *)GPU_vertbuf_raw_step(&g_batch_link.p1_step), points[1]); - copy_v2_v2((float *)GPU_vertbuf_raw_step(&g_batch_link.p2_step), points[2]); - copy_v2_v2((float *)GPU_vertbuf_raw_step(&g_batch_link.p3_step), points[3]); - char *colid = (char *)GPU_vertbuf_raw_step(&g_batch_link.colid_step); + NodeLinkData &data = g_batch_link.data[g_batch_link.count++]; + data.bezier_P0 = points[0]; + data.bezier_P1 = points[1]; + data.bezier_P2 = points[2]; + data.bezier_P3 = points[3]; + + char *colid = reinterpret_cast(&data.color_ids); colid[0] = nodelink_get_color_id(draw_config.th_col1); colid[1] = nodelink_get_color_id(draw_config.th_col2); colid[2] = nodelink_get_color_id(draw_config.th_col3); - colid[3] = draw_config.drawarrow; - copy_v4_v4((float *)GPU_vertbuf_raw_step(&g_batch_link.start_color_step), - draw_config.start_color); - copy_v4_v4((float *)GPU_vertbuf_raw_step(&g_batch_link.end_color_step), draw_config.end_color); - uint32_t *muted = (uint32_t *)GPU_vertbuf_raw_step(&g_batch_link.muted_step); - muted[0] = draw_config.drawmuted; - *(float *)GPU_vertbuf_raw_step(&g_batch_link.dim_factor_step) = draw_config.dim_factor; - *(float *)GPU_vertbuf_raw_step(&g_batch_link.thickness_step) = draw_config.thickness; - float3 dash_params(draw_config.dash_length, draw_config.dash_factor, draw_config.dash_alpha); - copy_v3_v3((float *)GPU_vertbuf_raw_step(&g_batch_link.dash_params_step), dash_params); - *(int *)GPU_vertbuf_raw_step(&g_batch_link.has_back_link_step) = draw_config.has_back_link; + + data.do_muted = draw_config.draw_muted; + data.do_arrow = draw_config.draw_arrow; + data.start_color = float4(draw_config.start_color); + data.end_color = float4(draw_config.end_color); + data.dim_factor = draw_config.dim_factor; + data.thickness = draw_config.thickness; + data.dash_length = draw_config.dash_length; + data.dash_factor = draw_config.dash_factor; + data.dash_alpha = draw_config.dash_alpha; + data.has_back_link = draw_config.has_back_link; if (g_batch_link.count == NODELINK_GROUP_SIZE) { nodelink_batch_draw(snode); @@ -2191,9 +2128,9 @@ static NodeLinkDrawConfig nodelink_get_draw_config(const bContext &C, (field_link ? 0.7f : 1.0f); draw_config.has_back_link = gizmo_link; draw_config.highlighted = link.flag & NODE_LINK_TEMP_HIGHLIGHT; - draw_config.drawarrow = ((link.tonode && link.tonode->is_reroute()) && - (link.fromnode && link.fromnode->is_reroute())); - draw_config.drawmuted = (link.flag & NODE_LINK_MUTED); + draw_config.draw_arrow = ((link.tonode && link.tonode->is_reroute()) && + (link.fromnode && link.fromnode->is_reroute())); + draw_config.draw_muted = (link.flag & NODE_LINK_MUTED); UI_GetThemeColor4fv(th_col3, draw_config.outline_color); @@ -2260,41 +2197,15 @@ static void node_draw_link_bezier_ex(const SpaceNode &snode, nodelink_batch_init(); } - if (g_batch_link.enabled && !draw_config.highlighted && !GPU_node_link_instancing_workaround()) { + if (g_batch_link.enabled && !draw_config.highlighted) { /* Add link to batch. */ nodelink_batch_add_link(snode, points, draw_config); } else { - NodeLinkData node_link_data; - for (const int i : IndexRange(points.size())) { - copy_v2_v2(node_link_data.bezierPts[i], points[i]); - } - - copy_v4_v4(node_link_data.colors[0], draw_config.outline_color); - copy_v4_v4(node_link_data.colors[1], draw_config.start_color); - copy_v4_v4(node_link_data.colors[2], draw_config.end_color); - - node_link_data.doArrow = draw_config.drawarrow; - node_link_data.doMuted = draw_config.drawmuted; - node_link_data.dim_factor = draw_config.dim_factor; - node_link_data.thickness = draw_config.thickness; - node_link_data.dash_params[0] = draw_config.dash_length; - node_link_data.dash_params[1] = draw_config.dash_factor; - node_link_data.dash_params[2] = draw_config.dash_alpha; - node_link_data.has_back_link = draw_config.has_back_link; - node_link_data.aspect = snode.runtime->aspect; - node_link_data.arrowSize = ARROW_SIZE; - - gpu::Batch *batch = g_batch_link.batch_single; - gpu::UniformBuf *ubo = GPU_uniformbuf_create_ex( - sizeof(NodeLinkData), &node_link_data, __func__); - - GPU_batch_program_set_builtin(batch, GPU_SHADER_2D_NODELINK); - GPU_batch_uniformbuf_bind(batch, "node_link_data", ubo); - GPU_batch_draw(batch); - - GPU_uniformbuf_unbind(ubo); - GPU_uniformbuf_free(ubo); + /* Slow path but should eventually not be the majority of them. */ + nodelink_batch_start(snode); + nodelink_batch_add_link(snode, points, draw_config); + nodelink_batch_end(snode); } } diff --git a/source/blender/editors/space_node/node_intern.hh b/source/blender/editors/space_node/node_intern.hh index 4b0ad59bbb2..33f76457f6f 100644 --- a/source/blender/editors/space_node/node_intern.hh +++ b/source/blender/editors/space_node/node_intern.hh @@ -253,8 +253,8 @@ void UI_node_socket_draw_cache_flush(); void nodesocket_batch_start(); void nodesocket_batch_end(); -void nodelink_batch_start(SpaceNode &snode); -void nodelink_batch_end(SpaceNode &snode); +void nodelink_batch_start(const SpaceNode &snode); +void nodelink_batch_end(const SpaceNode &snode); /** * \note this is used for fake links in groups too. diff --git a/source/blender/gpu/GPU_shader_builtin.hh b/source/blender/gpu/GPU_shader_builtin.hh index e2a8bbe909a..eba15a18f5e 100644 --- a/source/blender/gpu/GPU_shader_builtin.hh +++ b/source/blender/gpu/GPU_shader_builtin.hh @@ -66,7 +66,6 @@ enum eGPUBuiltinShader { GPU_SHADER_2D_NODE_SOCKET_INST, /** Draw a node link given an input quadratic Bezier curve. */ GPU_SHADER_2D_NODELINK, - GPU_SHADER_2D_NODELINK_INST, /** Draw round points with per vertex size and color. */ GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR, diff --git a/source/blender/gpu/GPU_shader_shared.hh b/source/blender/gpu/GPU_shader_shared.hh index 5f068f72282..6b2c8e6cfc7 100644 --- a/source/blender/gpu/GPU_shader_shared.hh +++ b/source/blender/gpu/GPU_shader_shared.hh @@ -48,30 +48,37 @@ struct NodeSocketShaderParameters { }; BLI_STATIC_ASSERT_ALIGN(NodeSocketShaderParameters, 16) +/* Per link data. */ struct NodeLinkData { - float4 colors[3]; - /* bezierPts Is actually a float2, but due to std140 each element needs to be aligned to 16 - * bytes. */ - float4 bezierPts[4]; - bool32_t doArrow; - bool32_t doMuted; + float4 start_color; + float4 end_color; + float2 bezier_P0; + float2 bezier_P1; + float2 bezier_P2; + float2 bezier_P3; + uint color_ids; + float dash_length; + float dash_factor; + float dash_alpha; float dim_factor; float thickness; - float4 dash_params; - bool32_t has_back_link; float aspect; - float arrowSize; - float _pad; + bool32_t do_arrow; + bool32_t do_muted; + bool32_t has_back_link; + float _pad0; + float _pad1; }; BLI_STATIC_ASSERT_ALIGN(NodeLinkData, 16) -struct NodeLinkInstanceData { +/* Data common to all links. */ +struct NodeLinkUniformData { float4 colors[6]; float aspect; - float arrowSize; + float arrow_size; float2 _pad; }; -BLI_STATIC_ASSERT_ALIGN(NodeLinkInstanceData, 16) +BLI_STATIC_ASSERT_ALIGN(NodeLinkUniformData, 16) struct GPencilStrokeData { float2 viewport; diff --git a/source/blender/gpu/intern/gpu_shader_builtin.cc b/source/blender/gpu/intern/gpu_shader_builtin.cc index 2c489822a1d..efc64929108 100644 --- a/source/blender/gpu/intern/gpu_shader_builtin.cc +++ b/source/blender/gpu/intern/gpu_shader_builtin.cc @@ -101,8 +101,6 @@ static const char *builtin_shader_create_info_name(eGPUBuiltinShader shader) return "gpu_shader_2D_node_socket_inst"; case GPU_SHADER_2D_NODELINK: return "gpu_shader_2D_nodelink"; - case GPU_SHADER_2D_NODELINK_INST: - return "gpu_shader_2D_nodelink_inst"; case GPU_SHADER_GPENCIL_STROKE: return "gpu_shader_gpencil_stroke"; case GPU_SHADER_SEQUENCER_STRIPS: diff --git a/source/blender/gpu/shaders/gpu_shader_2D_nodelink_frag.glsl b/source/blender/gpu/shaders/gpu_shader_2D_nodelink_frag.glsl index d5ca70ceb58..8401be5848a 100644 --- a/source/blender/gpu/shaders/gpu_shader_2D_nodelink_frag.glsl +++ b/source/blender/gpu/shaders/gpu_shader_2D_nodelink_frag.glsl @@ -10,53 +10,55 @@ FRAGMENT_SHADER_CREATE_INFO(gpu_shader_2D_nodelink) float get_line_alpha(float center, float relative_radius) { - float radius = relative_radius * lineThickness; - float sdf = abs(lineThickness * (lineUV.y - center)); + float radius = relative_radius * interp_flat.line_thickness; + float sdf = abs(interp_flat.line_thickness * (interp.line_uv.y - center)); return smoothstep(radius, radius - ANTIALIAS, sdf); } void main() { float dash_frag_alpha = 1.0f; - if (dashFactor < 1.0f) { - float distance_along_line = lineLength * lineUV.x; + if (interp_flat.dash_factor < 1.0f) { + float distance_along_line = interp_flat.line_length * interp.line_uv.x; - /* Checking if `normalized_distance <= dashFactor` is already enough for a basic + /* Checking if `normalized_distance <= interp.dash_factor` is already enough for a basic * dash, however we want to handle a nice anti-alias. */ - float dash_center = dashLength * dashFactor * 0.5f; + float dash_center = interp_flat.dash_length * interp_flat.dash_factor * 0.5f; float normalized_distance_triangle = - 1.0f - abs((fract((distance_along_line - dash_center) / dashLength)) * 2.0f - 1.0f); - float t = aspect * ANTIALIAS / dashLength; + 1.0f - + abs((fract((distance_along_line - dash_center) / interp_flat.dash_length)) * 2.0f - 1.0f); + float t = interp_flat.aspect * ANTIALIAS / interp_flat.dash_length; float slope = 1.0f / (2.0f * t); - float unclamped_alpha = 1.0f - slope * (normalized_distance_triangle - dashFactor + t); - float alpha = max(dashAlpha, min(unclamped_alpha, 1.0f)); + float unclamped_alpha = 1.0f - + slope * (normalized_distance_triangle - interp_flat.dash_factor + t); + float alpha = max(interp_flat.dash_alpha, min(unclamped_alpha, 1.0f)); dash_frag_alpha = alpha; } - if (isMainLine == 0) { - fragColor = finalColor; - fragColor.a *= get_line_alpha(0.5f, 0.5f) * dash_frag_alpha; + if (interp_flat.is_main_line == 0) { + out_color = interp.final_color; + out_color.a *= get_line_alpha(0.5f, 0.5f) * dash_frag_alpha; return; } - if (hasBackLink == 0) { - fragColor = finalColor; - fragColor.a *= get_line_alpha(0.5f, 0.5f) * dash_frag_alpha; + if (interp_flat.has_back_link == 0) { + out_color = interp.final_color; + out_color.a *= get_line_alpha(0.5f, 0.5f) * dash_frag_alpha; } else { /* Draw two links right next to each other, the main link and the back-link. */ - float4 main_link_color = finalColor; + float4 main_link_color = interp.final_color; main_link_color.a *= get_line_alpha(0.75f, 0.3f); float4 back_link_color = float4(float3(0.8f), 1.0f); back_link_color.a *= get_line_alpha(0.2f, 0.25f); /* Combine both links. */ - fragColor.rgb = main_link_color.rgb * main_link_color.a + + out_color.rgb = main_link_color.rgb * main_link_color.a + back_link_color.rgb * back_link_color.a; - fragColor.a = main_link_color.a * dash_frag_alpha + back_link_color.a; + out_color.a = main_link_color.a * dash_frag_alpha + back_link_color.a; } } diff --git a/source/blender/gpu/shaders/gpu_shader_2D_nodelink_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_nodelink_vert.glsl index fe9b41d685d..61c6c93da45 100644 --- a/source/blender/gpu/shaders/gpu_shader_2D_nodelink_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_2D_nodelink_vert.glsl @@ -12,83 +12,79 @@ * `pos` is the verts position in the curve tangent space */ -#include "gpu_shader_math_vector_lib.glsl" #include "infos/gpu_shader_2D_nodelink_info.hh" VERTEX_SHADER_CREATE_INFO(gpu_shader_2D_nodelink) -#define MID_VERTEX 65 +#include "gpu_shader_attribute_load_lib.glsl" +#include "gpu_shader_math_vector_lib.glsl" void main() { constexpr float start_gradient_threshold = 0.35f; constexpr float end_gradient_threshold = 0.65f; -#ifdef USE_INSTANCE -# define colStart (colid_doarrow[0] < 3u ? start_color : node_link_data.colors[colid_doarrow[0]]) -# define colEnd (colid_doarrow[1] < 3u ? end_color : node_link_data.colors[colid_doarrow[1]]) -# define colShadow node_link_data.colors[colid_doarrow[2]] -# define doArrow (colid_doarrow[3] != 0u) -# define doMuted (domuted[0] != 0) -#else - float2 P0 = node_link_data.bezierPts[0].xy; - float2 P1 = node_link_data.bezierPts[1].xy; - float2 P2 = node_link_data.bezierPts[2].xy; - float2 P3 = node_link_data.bezierPts[3].xy; - bool doArrow = node_link_data.doArrow; - bool doMuted = node_link_data.doMuted; - float dim_factor = node_link_data.dim_factor; - float thickness = node_link_data.thickness; - float3 dash_params = node_link_data.dash_params.xyz; - int has_back_link = node_link_data.has_back_link ? 1 : 0; + const NodeLinkData link = link_data_buf[gl_InstanceID]; - float4 colShadow = node_link_data.colors[0]; - float4 colStart = node_link_data.colors[1]; - float4 colEnd = node_link_data.colors[2]; -#endif + const float2 P0 = link.bezier_P0; + const float2 P1 = link.bezier_P1; + const float2 P2 = link.bezier_P2; + const float2 P3 = link.bezier_P3; - float line_thickness = thickness; - bool is_outline_pass = gl_VertexID < MID_VERTEX; - isMainLine = expand.y == 1.0f && !is_outline_pass ? 1 : 0; + const uint3 color_ids = gpu_attr_decode_uchar4_to_uint4(link.color_ids).xyz; - if ((expand.y == 1.0f) && has_back_link != 0) { + const float4 color_start = (color_ids[0] < 3u) ? link.start_color : + link_uniforms.colors[color_ids[0]]; + const float4 color_end = (color_ids[1] < 3u) ? link.end_color : + link_uniforms.colors[color_ids[1]]; + const float4 color_shadow = link_uniforms.colors[color_ids[2]]; + float line_thickness = link.thickness; + + /* Each instance contains both the outline and the "main" line on top. */ + constexpr int mid_vertex = 65; + bool is_outline_pass = gl_VertexID < mid_vertex; + + interp_flat.line_thickness = line_thickness; + interp_flat.is_main_line = (expand.y == 1.0f && !is_outline_pass) ? 1 : 0; + interp_flat.has_back_link = int(link.has_back_link); + interp_flat.aspect = link_uniforms.aspect; + /* Parameters for the dashed line. */ + interp_flat.dash_length = link.dash_length; + interp_flat.dash_factor = link.dash_factor; + interp_flat.dash_alpha = link.dash_alpha; + /* Approximate line length, no need for real bezier length calculation. */ + interp_flat.line_length = distance(P0, P3); + /* TODO: Incorrect U, this leads to non-uniform dash distribution. */ + interp.line_uv = uv; + + if ((expand.y == 1.0f) && link.has_back_link) { /* Increase width because two links are drawn. */ line_thickness *= 1.7f; } if (is_outline_pass) { /* Outline pass. */ - finalColor = colShadow; + interp.final_color = color_shadow; } else { /* Second pass. */ if (uv.x < start_gradient_threshold) { - finalColor = colStart; + interp.final_color = color_start; } else if (uv.x > end_gradient_threshold) { - finalColor = colEnd; + interp.final_color = color_end; } else { float mixFactor = (uv.x - start_gradient_threshold) / (end_gradient_threshold - start_gradient_threshold); - finalColor = mix(colStart, colEnd, mixFactor); + interp.final_color = mix(color_start, color_end, mixFactor); } line_thickness *= 0.65f; - if (doMuted) { - finalColor[3] = 0.65f; + if (link.do_muted) { + interp.final_color[3] = 0.65f; } } - - aspect = node_link_data.aspect; - /* Parameters for the dashed line. */ - dashLength = dash_params.x; - dashFactor = dash_params.y; - dashAlpha = dash_params.z; - /* Approximate line length, no need for real bezier length calculation. */ - lineLength = distance(P0, P3); - /* TODO: Incorrect U, this leads to non-uniform dash distribution. */ - lineUV = uv; - hasBackLink = has_back_link; + interp.final_color.a *= link.dim_factor; float t = uv.x; float t2 = t * t; @@ -108,7 +104,7 @@ void main() float2 normal = tangent.yx * float2(-1.0f, 1.0f); /* Position vertex on the curve tangent space */ - point += (pos.x * tangent + pos.y * normal) * node_link_data.arrowSize; + point += (pos.x * tangent + pos.y * normal) * link_uniforms.arrow_size; gl_Position = ModelViewProjectionMatrix * float4(point, 0.0f, 1.0f); @@ -119,17 +115,14 @@ void main() ModelViewProjectionMatrix[1].xy * exp_axis.yy; float expand_dist = line_thickness * (uv.y * 2.0f - 1.0f); - lineThickness = line_thickness; - - finalColor[3] *= dim_factor; /* Expand into a line */ - gl_Position.xy += exp_axis * node_link_data.aspect * expand_dist; + gl_Position.xy += exp_axis * link_uniforms.aspect * expand_dist; /* If the link is not muted or is not a reroute arrow the points are squashed to the center of * the line. Magic numbers are defined in `drawnode.cc`. */ - if ((expand.x == 1.0f && !doMuted) || - (expand.y != 1.0f && (pos.x < 0.70f || pos.x > 0.71f) && !doArrow)) + if ((expand.x == 1.0f && !link.do_muted) || + (expand.y != 1.0f && (pos.x < 0.70f || pos.x > 0.71f) && !link.do_arrow)) { gl_Position.xy *= 0.0f; } diff --git a/source/blender/gpu/shaders/infos/gpu_shader_2D_nodelink_info.hh b/source/blender/gpu/shaders/infos/gpu_shader_2D_nodelink_info.hh index 3751f550c38..46a712bc3af 100644 --- a/source/blender/gpu/shaders/infos/gpu_shader_2D_nodelink_info.hh +++ b/source/blender/gpu/shaders/infos/gpu_shader_2D_nodelink_info.hh @@ -15,52 +15,31 @@ #include "gpu_shader_create_info.hh" -GPU_SHADER_INTERFACE_INFO(nodelink_iface) -SMOOTH(float4, finalColor) -SMOOTH(float2, lineUV) -FLAT(float, lineLength) -FLAT(float, lineThickness) -FLAT(float, dashLength) -FLAT(float, dashFactor) -FLAT(int, hasBackLink) -FLAT(float, dashAlpha) -FLAT(int, isMainLine) +GPU_SHADER_NAMED_INTERFACE_INFO(nodelink_iface, interp) +SMOOTH(float4, final_color) +SMOOTH(float2, line_uv) +GPU_SHADER_NAMED_INTERFACE_END(interp) + +GPU_SHADER_NAMED_INTERFACE_INFO(nodelink_iface_flat, interp_flat) +FLAT(float, line_length) +FLAT(float, line_thickness) +FLAT(float, dash_length) +FLAT(float, dash_factor) +FLAT(float, dash_alpha) FLAT(float, aspect) -GPU_SHADER_INTERFACE_END() +FLAT(int, has_back_link) +FLAT(int, is_main_line) +GPU_SHADER_NAMED_INTERFACE_END(interp_flat) GPU_SHADER_CREATE_INFO(gpu_shader_2D_nodelink) VERTEX_IN(0, float2, uv) VERTEX_IN(1, float2, pos) VERTEX_IN(2, float2, expand) VERTEX_OUT(nodelink_iface) -FRAGMENT_OUT(0, float4, fragColor) -UNIFORM_BUF_FREQ(0, NodeLinkData, node_link_data, PASS) -PUSH_CONSTANT(float4x4, ModelViewProjectionMatrix) -VERTEX_SOURCE("gpu_shader_2D_nodelink_vert.glsl") -FRAGMENT_SOURCE("gpu_shader_2D_nodelink_frag.glsl") -TYPEDEF_SOURCE("GPU_shader_shared.hh") -DO_STATIC_COMPILATION() -GPU_SHADER_CREATE_END() - -GPU_SHADER_CREATE_INFO(gpu_shader_2D_nodelink_inst) -VERTEX_IN(0, float2, uv) -VERTEX_IN(1, float2, pos) -VERTEX_IN(2, float2, expand) -VERTEX_IN(3, float2, P0) -VERTEX_IN(4, float2, P1) -VERTEX_IN(5, float2, P2) -VERTEX_IN(6, float2, P3) -VERTEX_IN(7, uint4, colid_doarrow) -VERTEX_IN(8, float4, start_color) -VERTEX_IN(9, float4, end_color) -VERTEX_IN(10, uint2, domuted) -VERTEX_IN(11, float, dim_factor) -VERTEX_IN(12, float, thickness) -VERTEX_IN(13, float3, dash_params) -VERTEX_IN(14, int, has_back_link) -VERTEX_OUT(nodelink_iface) -FRAGMENT_OUT(0, float4, fragColor) -UNIFORM_BUF_FREQ(0, NodeLinkInstanceData, node_link_data, PASS) +VERTEX_OUT(nodelink_iface_flat) +FRAGMENT_OUT(0, float4, out_color) +STORAGE_BUF(0, read, NodeLinkData, link_data_buf[]) +UNIFORM_BUF(0, NodeLinkUniformData, link_uniforms) PUSH_CONSTANT(float4x4, ModelViewProjectionMatrix) VERTEX_SOURCE("gpu_shader_2D_nodelink_vert.glsl") FRAGMENT_SOURCE("gpu_shader_2D_nodelink_frag.glsl")