From f8f9cc2bdfaefefd037caa7b9e7f1ad2711783eb Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Fri, 20 Nov 2026 11:40:01 +0530 Subject: [PATCH 1/2] zink: fetch 10/10/10/2 vertex formats as R32_UINT when the driver lacks them Mali drivers do not list the A2B10G10R10/A2R10G10B10 UNORM, SNORM, USCALED and SSCALED formats as vertex buffer formats, so GL_ARB_vertex_type_2_10_10_10_rev and with it GL 3.3 were missing. Without optimal_keys, such an attribute is now declared as R32_UINT at the same offset and stride, and the vertex shader key records which attributes need unpacking and how (channel order, signedness, normalization). A compile pass replaces the float input with a uint input and unpacks it. Formats the driver supports keep the native path, and with optimal_keys (the only mode that uses pipeline libraries or shader objects) nothing changes. --- src/gallium/drivers/zink/zink_compiler.c | 74 +++++++++++++++++++++ src/gallium/drivers/zink/zink_screen.c | 25 ++++++- src/gallium/drivers/zink/zink_screen.h | 2 + src/gallium/drivers/zink/zink_shader_keys.h | 4 ++ src/gallium/drivers/zink/zink_state.c | 28 ++++++-- src/gallium/drivers/zink/zink_types.h | 5 ++ 6 files changed, 133 insertions(+), 5 deletions(-) diff --git a/src/gallium/drivers/zink/zink_compiler.c b/src/gallium/drivers/zink/zink_compiler.c index 533991413fb..7431cb5469b 100644 --- a/src/gallium/drivers/zink/zink_compiler.c +++ b/src/gallium/drivers/zink/zink_compiler.c @@ -2158,6 +2158,78 @@ decompose_attribs(nir_shader *nir, uint32_t decomposed_attrs, uint32_t decompose return true; } +struct packed_attrib_state { + nir_variable *var; + nir_variable *packed; + bool bgra; + bool is_signed; + bool scaled; +}; + +static bool +lower_packed_attrib(nir_builder *b, nir_intrinsic_instr *intr, void *data) +{ + struct packed_attrib_state *state = data; + if (intr->intrinsic != nir_intrinsic_load_deref || + nir_intrinsic_get_var(intr, 0) != state->var) + return false; + b->cursor = nir_after_instr(&intr->instr); + nir_def *packed = nir_load_deref(b, nir_build_deref_var(b, state->packed)); + nir_def *channels[4]; + for (unsigned i = 0; i < 4; i++) { + unsigned bits = i == 3 ? 2 : 10; + nir_def *field = nir_ishl_imm(b, packed, 32 - i * 10 - bits); + nir_def *v = nir_u2f32(b, nir_ushr_imm(b, field, 32 - bits)); + if (state->is_signed) { + /* sign-extend in float: signed integer extraction gave wrong fields on Mali-G615 r44p1 */ + v = nir_bcsel(b, nir_fge(b, v, nir_imm_float(b, 1 << (bits - 1))), nir_fadd_imm(b, v, -(double)(1 << bits)), v); + if (!state->scaled) + v = nir_fmax(b, nir_fdiv_imm(b, v, (1 << (bits - 1)) - 1), nir_imm_float(b, -1.0)); + channels[i] = v; + } else { + channels[i] = state->scaled ? v : nir_fdiv_imm(b, v, (1 << bits) - 1); + } + } + if (state->bgra) { + nir_def *tmp = channels[0]; + channels[0] = channels[2]; + channels[2] = tmp; + } + nir_def *val = nir_vec(b, channels, intr->def.num_components); + nir_def_replace(&intr->def, val); + return true; +} + +static bool +lower_packed_attribs(nir_shader *nir, const struct zink_vs_key *vs_key) +{ + bool progress = false; + u_foreach_bit(location, vs_key->packed_attrs) { + nir_variable *var = nir_find_variable_with_driver_location(nir, nir_var_shader_in, location); + if (!var || !glsl_type_is_vector_or_scalar(var->type) || + glsl_get_base_type(var->type) != GLSL_TYPE_FLOAT) + continue; + struct packed_attrib_state state = { + .var = var, + .packed = nir_variable_clone(var, nir), + .bgra = (vs_key->packed_attrs_bgra & BITFIELD_BIT(location)) != 0, + .is_signed = (vs_key->packed_attrs_signed & BITFIELD_BIT(location)) != 0, + .scaled = (vs_key->packed_attrs_scaled & BITFIELD_BIT(location)) != 0, + }; + state.packed->name = ralloc_asprintf(nir, "%s_packed", var->name); + state.packed->type = glsl_uint_type(); + nir_shader_add_variable(nir, state.packed); + var->data.mode = nir_var_shader_temp; + nir_shader_intrinsics_pass(nir, lower_packed_attrib, nir_metadata_control_flow, &state); + progress = true; + } + if (progress) { + nir_fixup_deref_modes(nir); + NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_shader_temp, NULL); + } + return progress; +} + static bool rewrite_bo_access_instr(nir_builder *b, nir_instr *instr, void *data) { @@ -4034,6 +4106,8 @@ zink_shader_compile(struct zink_screen *screen, bool can_shobj, struct zink_shad break; default: break; } + if (vs_key->packed_attrs) + NIR_PASS_V(nir, lower_packed_attribs, vs_key); if (decomposed_attrs || decomposed_attrs_without_w) NIR_PASS_V(nir, decompose_attribs, decomposed_attrs, decomposed_attrs_without_w); break; diff --git a/src/gallium/drivers/zink/zink_screen.c b/src/gallium/drivers/zink/zink_screen.c index c9b49e15f61..33bfb435e13 100644 --- a/src/gallium/drivers/zink/zink_screen.c +++ b/src/gallium/drivers/zink/zink_screen.c @@ -1511,7 +1511,8 @@ zink_is_format_supported(struct pipe_screen *pscreen, if (target == PIPE_BUFFER) { if (bind & PIPE_BIND_VERTEX_BUFFER) { - if (!(props.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT)) { + if (!(props.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) && + !zink_screen_emulates_packed_vertex_format(screen, format)) { enum pipe_format new_format = zink_decompose_vertex_format(format); if (!new_format) return false; @@ -2273,6 +2274,28 @@ check_vertex_formats(struct zink_screen *screen) } } +bool +zink_screen_emulates_packed_vertex_format(struct zink_screen *screen, enum pipe_format format) +{ + switch (format) { + case PIPE_FORMAT_R10G10B10A2_UNORM: + case PIPE_FORMAT_B10G10R10A2_UNORM: + case PIPE_FORMAT_R10G10B10A2_SNORM: + case PIPE_FORMAT_B10G10R10A2_SNORM: + case PIPE_FORMAT_R10G10B10A2_USCALED: + case PIPE_FORMAT_B10G10R10A2_USCALED: + case PIPE_FORMAT_R10G10B10A2_SSCALED: + case PIPE_FORMAT_B10G10R10A2_SSCALED: + break; + default: + return false; + } + /* the vertex shader key that does the unpacking only exists without optimal_keys */ + return !screen->optimal_keys && + !(screen->format_props[format].bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) && + (screen->format_props[PIPE_FORMAT_R32_UINT].bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT); +} + static void populate_format_props(struct zink_screen *screen) { diff --git a/src/gallium/drivers/zink/zink_screen.h b/src/gallium/drivers/zink/zink_screen.h index 9991dceb92f..c0a1b824d79 100644 --- a/src/gallium/drivers/zink/zink_screen.h +++ b/src/gallium/drivers/zink/zink_screen.h @@ -75,6 +75,8 @@ zink_screen_check_last_finished(struct zink_screen *screen, uint32_t batch_id) bool zink_screen_init_semaphore(struct zink_screen *screen); +bool +zink_screen_emulates_packed_vertex_format(struct zink_screen *screen, enum pipe_format format); static inline bool zink_screen_handle_vkresult(struct zink_screen *screen, VkResult ret) diff --git a/src/gallium/drivers/zink/zink_shader_keys.h b/src/gallium/drivers/zink/zink_shader_keys.h index 1dab2447fd8..bf570796009 100644 --- a/src/gallium/drivers/zink/zink_shader_keys.h +++ b/src/gallium/drivers/zink/zink_shader_keys.h @@ -53,6 +53,10 @@ struct zink_vs_key { uint8_t decomposed_attrs_without_w; } u8; }; + uint32_t packed_attrs; + uint32_t packed_attrs_bgra; + uint32_t packed_attrs_signed; + uint32_t packed_attrs_scaled; // not hashed unsigned size; }; diff --git a/src/gallium/drivers/zink/zink_state.c b/src/gallium/drivers/zink/zink_state.c index ca317104524..a7a57487100 100644 --- a/src/gallium/drivers/zink/zink_state.c +++ b/src/gallium/drivers/zink/zink_state.c @@ -79,7 +79,17 @@ zink_create_vertex_elements_state(struct pipe_context *pctx, VkFormat format; if (screen->format_props[elem->src_format].bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) format = zink_get_format(screen, elem->src_format); - else { + else if (zink_screen_emulates_packed_vertex_format(screen, elem->src_format)) { + const struct util_format_description *desc = util_format_description(elem->src_format); + format = VK_FORMAT_R32_UINT; + ves->packed_attrs |= BITFIELD_BIT(i); + if (desc->swizzle[0] == PIPE_SWIZZLE_Z) + ves->packed_attrs_bgra |= BITFIELD_BIT(i); + if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) + ves->packed_attrs_signed |= BITFIELD_BIT(i); + if (!desc->channel[0].normalized) + ves->packed_attrs_scaled |= BITFIELD_BIT(i); + } else { enum pipe_format new_format = zink_decompose_vertex_format(elem->src_format); assert(new_format); num_decomposed++; @@ -209,10 +219,14 @@ zink_bind_vertex_elements_state(struct pipe_context *pctx, break; } if (ctx->element_state->decomposed_attrs != decomposed_attrs || - ctx->element_state->decomposed_attrs_without_w != decomposed_attrs_without_w) { + ctx->element_state->decomposed_attrs_without_w != decomposed_attrs_without_w || + ctx->element_state->packed_attrs != vs->packed_attrs || + ctx->element_state->packed_attrs_bgra != vs->packed_attrs_bgra || + ctx->element_state->packed_attrs_signed != vs->packed_attrs_signed || + ctx->element_state->packed_attrs_scaled != vs->packed_attrs_scaled) { unsigned size = MAX2(ctx->element_state->decomposed_attrs_size, ctx->element_state->decomposed_attrs_without_w_size); struct zink_shader_key *key = (struct zink_shader_key *)zink_set_vs_key(ctx); - key->size -= 2 * key->key.vs.size; + memset(&key->key.vs.u32, 0, sizeof(key->key.vs.u32)); switch (size) { case 1: key->key.vs.u8.decomposed_attrs = ctx->element_state->decomposed_attrs; @@ -229,7 +243,13 @@ zink_bind_vertex_elements_state(struct pipe_context *pctx, default: break; } key->key.vs.size = size; - key->size += 2 * size; + key->key.vs.packed_attrs = ctx->element_state->packed_attrs; + key->key.vs.packed_attrs_bgra = ctx->element_state->packed_attrs_bgra; + key->key.vs.packed_attrs_signed = ctx->element_state->packed_attrs_signed; + key->key.vs.packed_attrs_scaled = ctx->element_state->packed_attrs_scaled; + /* the decomposed masks sit behind padding, so hash the whole key once packed attrs are in it */ + key->size = ctx->element_state->packed_attrs ? offsetof(struct zink_vs_key, size) : + sizeof(struct zink_vs_key_base) + 2 * size; } } else { state->element_state = NULL; diff --git a/src/gallium/drivers/zink/zink_types.h b/src/gallium/drivers/zink/zink_types.h index 90978fa27e3..76e9ca42de3 100644 --- a/src/gallium/drivers/zink/zink_types.h +++ b/src/gallium/drivers/zink/zink_types.h @@ -315,6 +315,11 @@ struct zink_vertex_elements_state { unsigned decomposed_attrs_size; uint32_t decomposed_attrs_without_w; unsigned decomposed_attrs_without_w_size; + /* 10/10/10/2 attributes fetched as R32_UINT and unpacked in the vertex shader */ + uint32_t packed_attrs; + uint32_t packed_attrs_bgra; + uint32_t packed_attrs_signed; + uint32_t packed_attrs_scaled; struct zink_vertex_elements_hw_state hw_state; }; From 2bc9b7a4cf516c4bdd3d144f6bcbd8e28b28b945 Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Fri, 20 Nov 2026 11:45:38 +0530 Subject: [PATCH 2/2] zink: blend dual-source color 0 through fbfetch when the driver lacks dualSrcBlend Mali drivers report dualSrcBlend = false, so GL_ARB_blend_func_extended and with it GL 3.3 were missing. They do have coherent framebuffer fetch (VK_EXT_rasterization_order_attachment_access). When the driver has no dualSrcBlend but has coherent fetch, and zink runs without optimal_keys and without dynamic blend state, zink now reports one dual-source render target: - A fragment shader that writes a dual-source color is marked as an fbfetch shader when it is created, so its programs get the input attachment layout, the rasterization-order render pass and per-sample shading for multisampled targets no matter when the blend state arrives. - A blend state that uses src1 factors on color 0 turns hardware blending off for color 0 (write mask kept) and puts the equation into a new part of the fragment shader key. That variant gathers both color 0 outputs, applies alpha-to-coverage (per sample, from the source alpha) and alpha-to-one, and runs nir_lower_blend with the destination read through fbfetch. The blend constant comes from a new push constant. The clamp follows cbuf 0: unorm and sRGB (blended in linear through the sRGB view), snorm, or none for float; integer buffers are not blended. - A shader that writes no dual-source color while such a blend state is bound gets the hardware blend with src1 factors reading zero. Drivers with dualSrcBlend keep the native path. With optimal_keys (and so pipeline libraries and shader objects) or full dynamic blend state, or without coherent fetch, the cap stays 0. --- src/gallium/drivers/zink/zink_compiler.c | 160 +++++++++++++++++++- src/gallium/drivers/zink/zink_compiler.h | 2 + src/gallium/drivers/zink/zink_context.c | 1 + src/gallium/drivers/zink/zink_draw.cpp | 4 + src/gallium/drivers/zink/zink_pipeline.c | 13 +- src/gallium/drivers/zink/zink_program.c | 35 ++++- src/gallium/drivers/zink/zink_program.h | 3 + src/gallium/drivers/zink/zink_screen.c | 6 +- src/gallium/drivers/zink/zink_screen.h | 8 + src/gallium/drivers/zink/zink_shader_keys.h | 18 +++ src/gallium/drivers/zink/zink_state.c | 45 ++++++ src/gallium/drivers/zink/zink_types.h | 9 ++ 12 files changed, 296 insertions(+), 8 deletions(-) diff --git a/src/gallium/drivers/zink/zink_compiler.c b/src/gallium/drivers/zink/zink_compiler.c index 7431cb5469b..f234f82e66a 100644 --- a/src/gallium/drivers/zink/zink_compiler.c +++ b/src/gallium/drivers/zink/zink_compiler.c @@ -37,6 +37,7 @@ #include "compiler/nir/nir_builder.h" #include "compiler/nir/nir_serialize.h" #include "compiler/nir/nir_builtin_builder.h" +#include "compiler/nir/nir_lower_blend.h" #include "nir/tgsi_to_nir.h" #include "tgsi/tgsi_dump.h" @@ -87,7 +88,7 @@ is_clipcull_dist(int location) #define SIZEOF_FIELD(type, field) sizeof(((type *)0)->field) static void -create_gfx_pushconst(nir_shader *nir) +create_gfx_pushconst(nir_shader *nir, bool blend_color) { #define PUSHCONST_MEMBER(member_idx, field) \ fields[member_idx].type = \ @@ -106,9 +107,10 @@ fields[member_idx].offset = offsetof(struct zink_gfx_push_constant, field); PUSHCONST_MEMBER(ZINK_GFX_PUSHCONST_LINE_STIPPLE_PATTERN, line_stipple_pattern); PUSHCONST_MEMBER(ZINK_GFX_PUSHCONST_VIEWPORT_SCALE, viewport_scale); PUSHCONST_MEMBER(ZINK_GFX_PUSHCONST_LINE_WIDTH, line_width); + PUSHCONST_MEMBER(ZINK_GFX_PUSHCONST_BLEND_COLOR, blend_color); pushconst = nir_variable_create(nir, nir_var_mem_push_const, - glsl_struct_type(fields, ZINK_GFX_PUSHCONST_MAX, "struct", false), + glsl_struct_type(fields, blend_color ? ZINK_GFX_PUSHCONST_MAX : ZINK_GFX_PUSHCONST_BLEND_COLOR, "struct", false), "gfx_pushconst"); pushconst->data.location = INT_MAX; //doesn't really matter @@ -1033,6 +1035,147 @@ lower_dual_blend(nir_shader *shader) return progress; } +bool +zink_shader_writes_dual_src(nir_shader *nir) +{ + /* GLSL reaches the driver with lowered io; nothing else has dual-source outputs */ + if (!nir->info.io_lowered) + return false; + nir_foreach_function_impl(impl, nir) { + nir_foreach_block(block, impl) { + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + if (intr->intrinsic == nir_intrinsic_store_output && + nir_intrinsic_io_semantics(intr).dual_source_blend_index) + return true; + } + } + } + return false; +} + +static bool +remove_dual_src_store(nir_builder *b, nir_intrinsic_instr *intr, void *data) +{ + if (intr->intrinsic != nir_intrinsic_store_output || + !nir_intrinsic_io_semantics(intr).dual_source_blend_index) + return false; + nir_instr_remove(&intr->instr); + return true; +} + +static bool +lower_blend_const_to_pushconst(nir_builder *b, nir_intrinsic_instr *intr, void *data) +{ + if (intr->intrinsic != nir_intrinsic_load_blend_const_color_rgba) + return false; + b->cursor = nir_before_instr(&intr->instr); + nir_def_replace(&intr->def, nir_load_push_constant_zink(b, 4, 32, nir_imm_int(b, ZINK_GFX_PUSHCONST_BLEND_COLOR))); + return true; +} + +/* blend color 0 in the shader for drivers without dualSrcBlend: + * - gather both color 0 outputs into temporaries and store them once at the end + * - alpha-to-coverage and alpha-to-one use the source alpha, so apply them here + * - nir_lower_blend reads the destination as a fbfetch output and consumes the dual-source store + */ +static bool +lower_dual_src_blend(nir_shader *nir, const struct zink_fs_dual_blend_key *key, bool ms) +{ + if (!key || !key->enabled) + return nir_shader_intrinsics_pass(nir, remove_dual_src_store, nir_metadata_control_flow, NULL); + + nir_function_impl *impl = nir_shader_get_entrypoint(nir); + nir_builder b = nir_builder_create(impl); + nir_variable *temps[2] = {NULL}; + nir_io_semantics sems[2] = { + {.location = FRAG_RESULT_DATA0, .num_slots = 1}, + {.location = FRAG_RESULT_DATA0, .num_slots = 1, .dual_source_blend_index = 1}, + }; + nir_foreach_block(block, impl) { + nir_foreach_instr_safe(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + if (intr->intrinsic != nir_intrinsic_store_output) + continue; + nir_io_semantics sem = nir_intrinsic_io_semantics(intr); + if (sem.location != FRAG_RESULT_DATA0) + continue; + unsigned idx = sem.dual_source_blend_index; + if (!temps[idx]) { + temps[idx] = nir_local_variable_create(impl, glsl_vec4_type(), idx ? "dual_src_color1" : "dual_src_color0"); + sems[idx] = sem; + } + b.cursor = nir_before_instr(instr); + unsigned comp = nir_intrinsic_component(intr); + nir_def *val = intr->src[0].ssa; + nir_def *undef = nir_undef(&b, 1, 32); + nir_def *channels[4]; + for (unsigned c = 0; c < 4; c++) + channels[c] = c >= comp && c < comp + val->num_components ? nir_channel(&b, val, c - comp) : undef; + nir_store_var(&b, temps[idx], nir_vec(&b, channels, 4), nir_intrinsic_write_mask(intr) << comp); + nir_instr_remove(instr); + } + } + + b.cursor = nir_after_impl(impl); + nir_def *src = temps[0] ? nir_load_var(&b, temps[0]) : nir_undef(&b, 4, 32); + if (key->alpha_to_coverage && ms) { + /* per-sample shading is on with ms fbfetch: keep this sample if alpha covers it */ + nir_def *covered = nir_fmul_imm(&b, nir_fsat(&b, nir_channel(&b, src, 3)), 1 << key->coverage_samples_log2); + nir_def *sample = nir_fadd_imm(&b, nir_u2f32(&b, nir_load_sample_id(&b)), 0.5); + nir_discard_if(&b, nir_flt(&b, covered, sample)); + nir->info.fs.uses_discard = true; + } + if (key->alpha_to_one) + src = nir_vector_insert_imm(&b, src, nir_imm_float(&b, 1.0), 3); + if (temps[1]) + nir_store_output(&b, nir_load_var(&b, temps[1]), nir_imm_int(&b, 0), .src_type = nir_type_float32, .io_semantics = sems[1]); + nir_store_output(&b, src, nir_imm_int(&b, 0), .src_type = nir_type_float32, .io_semantics = sems[0]); + nir_metadata_preserve(impl, nir_metadata_none); + + nir_lower_blend_options opts = {0}; + opts.format[0] = key->format; + opts.rt[0].rgb.func = key->rgb_func; + opts.rt[0].rgb.src_factor = key->rgb_src_factor; + opts.rt[0].rgb.dst_factor = key->rgb_dst_factor; + opts.rt[0].alpha.func = key->alpha_func; + opts.rt[0].alpha.src_factor = key->alpha_src_factor; + opts.rt[0].alpha.dst_factor = key->alpha_dst_factor; + /* the pipeline keeps the color write mask */ + opts.rt[0].colormask = BITFIELD_MASK(4); + /* per-sample shading comes from the fbfetch_ms key, as for any fbfetch shader */ + bool uses_sample_shading = nir->info.fs.uses_sample_shading; + NIR_PASS_V(nir, nir_lower_blend, &opts); + nir->info.fs.uses_sample_shading = uses_sample_shading; + NIR_PASS_V(nir, nir_shader_intrinsics_pass, lower_blend_const_to_pushconst, nir_metadata_control_flow, NULL); + if (key->alpha_to_coverage && ms) + NIR_PASS_V(nir, nir_lower_discard_if, nir_lower_demote_if_to_cf | nir_lower_terminate_if_to_cf); + NIR_PASS_V(nir, nir_lower_vars_to_ssa); + + /* replace the color 0 variables with one vec4 for the blended color and one for the fetch */ + bool have_fetch = false; + nir_foreach_shader_out_variable_safe(var, nir) { + if (var->data.location != FRAG_RESULT_DATA0) + continue; + if (var->data.fb_fetch_output) + have_fetch = true; + else + exec_node_remove(&var->node); + } + nir_variable *color = nir_variable_create(nir, nir_var_shader_out, glsl_vec4_type(), "dual_src_blended"); + color->data.location = FRAG_RESULT_DATA0; + if (nir->info.fs.uses_fbfetch_output && !have_fetch) { + nir_variable *fetch = nir_variable_create(nir, nir_var_shader_out, glsl_vec4_type(), "dual_src_fbfetch"); + fetch->data.location = FRAG_RESULT_DATA0; + fetch->data.fb_fetch_output = true; + } + return true; +} + static bool lower_64bit_pack_instr(nir_builder *b, nir_instr *instr, void *data) { @@ -3500,7 +3643,7 @@ zink_shader_spirv_compile(struct zink_screen *screen, struct zink_shader *zs, st VkPushConstantRange pcr; pcr.stageFlags = VK_SHADER_STAGE_ALL_GRAPHICS; pcr.offset = 0; - pcr.size = sizeof(struct zink_gfx_push_constant); + pcr.size = zink_gfx_push_constant_size(screen); sci.pushConstantRangeCount = 1; sci.pPushConstantRanges = &pcr; @@ -4072,6 +4215,9 @@ zink_shader_compile(struct zink_screen *screen, bool can_shobj, struct zink_shad bool need_optimize = true; bool inlined_uniforms = false; + if (zs->info.stage == MESA_SHADER_FRAGMENT && zs->fs.dual_src_fbfetch) + NIR_PASS_V(nir, lower_dual_src_blend, key ? &zink_fs_key(key)->dual_blend : NULL, + key && zink_fs_key_base(key)->fbfetch_ms); NIR_PASS_V(nir, add_derefs); NIR_PASS_V(nir, nir_lower_fragcolor, nir->info.fs.color_is_dual_source ? 1 : 8); if (key) { @@ -6341,7 +6487,7 @@ zink_shader_init(struct zink_screen *screen, struct zink_shader *zs) } if (nir->info.stage < MESA_SHADER_COMPUTE) - create_gfx_pushconst(nir); + create_gfx_pushconst(nir, screen->emulate_dual_src_blend); if (nir->info.stage == MESA_SHADER_TESS_CTRL || nir->info.stage == MESA_SHADER_TESS_EVAL) @@ -6501,6 +6647,10 @@ zink_shader_init(struct zink_screen *screen, struct zink_shader *zs) if (nir->xfb_info && nir->xfb_info->output_count && nir->info.outputs_written) update_so_info(zs, nir, nir->info.outputs_written, have_psiz); zink_shader_serialize_blob(nir, &zs->blob); + if (nir->info.stage == MESA_SHADER_FRAGMENT && zs->fs.dual_src_fbfetch) { + nir->info.fs.uses_fbfetch_output = true; + nir->info.outputs_read |= BITFIELD64_BIT(FRAG_RESULT_DATA0); + } memcpy(&zs->info, &nir->info, sizeof(nir->info)); } @@ -6729,7 +6879,7 @@ zink_shader_tcs_init(struct zink_screen *screen, struct zink_shader *zs, nir_sha gl_TessLevelOuter->data.location = VARYING_SLOT_TESS_LEVEL_OUTER; gl_TessLevelOuter->data.patch = 1; - create_gfx_pushconst(nir); + create_gfx_pushconst(nir, screen->emulate_dual_src_blend); nir_def *load_inner = nir_load_push_constant_zink(&b, 2, 32, nir_imm_int(&b, ZINK_GFX_PUSHCONST_DEFAULT_INNER_LEVEL)); diff --git a/src/gallium/drivers/zink/zink_compiler.h b/src/gallium/drivers/zink/zink_compiler.h index babd9df8271..a1f94fe1883 100644 --- a/src/gallium/drivers/zink/zink_compiler.h +++ b/src/gallium/drivers/zink/zink_compiler.h @@ -75,6 +75,8 @@ struct zink_shader_object zink_shader_compile_separate(struct zink_screen *screen, struct zink_shader *zs); struct zink_shader * zink_shader_create(struct zink_screen *screen, struct nir_shader *nir); +bool +zink_shader_writes_dual_src(nir_shader *nir); void zink_shader_init(struct zink_screen *screen, struct zink_shader *zs); diff --git a/src/gallium/drivers/zink/zink_context.c b/src/gallium/drivers/zink/zink_context.c index b23665bd804..65d089f4ea8 100644 --- a/src/gallium/drivers/zink/zink_context.c +++ b/src/gallium/drivers/zink/zink_context.c @@ -3741,6 +3741,7 @@ zink_set_framebuffer_state(struct pipe_context *pctx, util_copy_framebuffer_state(&ctx->fb_state, state); ctx->rp_changed |= zink_update_fbfetch(ctx); + zink_update_fs_dual_blend_key(ctx); ctx->transient_attachments = 0; ctx->fb_layer_mismatch = 0; diff --git a/src/gallium/drivers/zink/zink_draw.cpp b/src/gallium/drivers/zink/zink_draw.cpp index 7284016d909..f7f6eddb599 100644 --- a/src/gallium/drivers/zink/zink_draw.cpp +++ b/src/gallium/drivers/zink/zink_draw.cpp @@ -776,6 +776,10 @@ zink_draw(struct pipe_context *pctx, } if (!screen->optimal_keys) { + if (zink_get_fs_key(ctx)->dual_blend.enabled) + VKCTX(CmdPushConstants)(bs->cmdbuf, ctx->curr_program->base.layout, VK_SHADER_STAGE_ALL_GRAPHICS, + offsetof(struct zink_gfx_push_constant, blend_color), sizeof(float) * 4, + ctx->blend_constants); if (zink_get_fs_key(ctx)->lower_line_stipple || zink_get_gs_key(ctx)->lower_gl_point || zink_get_fs_key(ctx)->lower_line_smooth) { diff --git a/src/gallium/drivers/zink/zink_pipeline.c b/src/gallium/drivers/zink/zink_pipeline.c index a5f151fefc7..e0a16db25d6 100644 --- a/src/gallium/drivers/zink/zink_pipeline.c +++ b/src/gallium/drivers/zink/zink_pipeline.c @@ -100,6 +100,8 @@ zink_create_gfx_pipeline(struct zink_screen *screen, VkPipelineColorBlendStateCreateInfo blend_state = {0}; blend_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; + VkPipelineColorBlendAttachmentState attachments[PIPE_MAX_COLOR_BUFS]; + bool fs_blends_dual_src = false; if (state->blend_state) { unsigned num_attachments = state->render_pass ? state->render_pass->state.num_rts : @@ -107,6 +109,15 @@ zink_create_gfx_pipeline(struct zink_screen *screen, if (state->render_pass && state->render_pass->state.have_zsbuf) num_attachments--; blend_state.pAttachments = state->blend_state->attachments; + if (state->blend_state->dual_blend.enabled) { + if (prog->shaders[MESA_SHADER_FRAGMENT] && prog->shaders[MESA_SHADER_FRAGMENT]->fs.dual_src_fbfetch) { + fs_blends_dual_src = true; + } else { + memcpy(attachments, state->blend_state->attachments, sizeof(attachments)); + attachments[0] = state->blend_state->dual_blend_fallback; + blend_state.pAttachments = attachments; + } + } blend_state.attachmentCount = num_attachments; blend_state.logicOpEnable = state->blend_state->logicop_enable; blend_state.logicOp = state->blend_state->logicop_func; @@ -117,7 +128,7 @@ zink_create_gfx_pipeline(struct zink_screen *screen, VkPipelineMultisampleStateCreateInfo ms_state = {0}; ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; ms_state.rasterizationSamples = state->rast_samples + 1; - if (state->blend_state) { + if (state->blend_state && !fs_blends_dual_src) { ms_state.alphaToCoverageEnable = state->blend_state->alpha_to_coverage; if (state->blend_state->alpha_to_one && !screen->info.feats.features.alphaToOne) { static bool warned = false; diff --git a/src/gallium/drivers/zink/zink_program.c b/src/gallium/drivers/zink/zink_program.c index 91dc0281ff8..a33eb9a001b 100644 --- a/src/gallium/drivers/zink/zink_program.c +++ b/src/gallium/drivers/zink/zink_program.c @@ -965,7 +965,7 @@ zink_pipeline_layout_create(struct zink_screen *screen, VkDescriptorSetLayout *d if (!is_compute) { pcr.stageFlags = VK_SHADER_STAGE_ALL_GRAPHICS; pcr.offset = 0; - pcr.size = sizeof(struct zink_gfx_push_constant); + pcr.size = zink_gfx_push_constant_size(screen); plci.pushConstantRangeCount = 1; plci.pPushConstantRanges = &pcr; } @@ -1963,6 +1963,30 @@ zink_update_fs_key_samples(struct zink_context *ctx) } } +void +zink_update_fs_dual_blend_key(struct zink_context *ctx) +{ + if (!zink_screen(ctx->base.screen)->emulate_dual_src_blend) + return; + const struct zink_blend_state *blend = ctx->gfx_pipeline_state.blend_state; + const struct pipe_surface *cbuf = ctx->fb_state.nr_cbufs ? ctx->fb_state.cbufs[0] : NULL; + struct zink_fs_dual_blend_key key = {0}; + /* integer buffers are never blended */ + if (blend && blend->dual_blend.enabled && !(cbuf && util_format_is_pure_integer(cbuf->format))) { + key = blend->dual_blend; + if (cbuf && util_format_is_snorm(cbuf->format)) + key.format = PIPE_FORMAT_R8G8B8A8_SNORM; + else if (cbuf && !util_format_is_unorm(cbuf->format) && !util_format_is_srgb(cbuf->format)) + key.format = PIPE_FORMAT_R32G32B32A32_FLOAT; + else + key.format = PIPE_FORMAT_R8G8B8A8_UNORM; + if (key.alpha_to_coverage) + key.coverage_samples_log2 = util_logbase2(MAX2(ctx->fb_state.samples, 1)); + } + if (memcmp(&zink_get_fs_key(ctx)->dual_blend, &key, sizeof(key))) + zink_set_fs_key(ctx)->dual_blend = key; +} + void zink_update_gs_key_rectangular_line(struct zink_context *ctx) { bool line_rectangular = zink_get_gs_key(ctx)->line_rectangular; @@ -2306,12 +2330,21 @@ zink_create_gfx_shader_state(struct pipe_context *pctx, const struct pipe_shader else nir = (struct nir_shader *)shader->ir.nir; + bool dual_src_fbfetch = screen->emulate_dual_src_blend && nir->info.stage == MESA_SHADER_FRAGMENT && + zink_shader_writes_dual_src(nir); + if (dual_src_fbfetch) { + /* fixed per shader so programs get the fbfetch layout before the blend state is known */ + nir->info.fs.uses_fbfetch_output = true; + nir->info.outputs_read |= BITFIELD64_BIT(FRAG_RESULT_DATA0); + } if (nir->info.stage == MESA_SHADER_FRAGMENT && nir->info.fs.uses_fbfetch_output) zink_descriptor_util_init_fbfetch(zink_context(pctx)); if (nir->info.uses_bindless) zink_descriptors_init_bindless(zink_context(pctx)); struct zink_shader *zs = zink_shader_create(zink_screen(pctx->screen), nir); + if (dual_src_fbfetch) + zs->fs.dual_src_fbfetch = true; if (zink_debug & ZINK_DEBUG_NOBGC) gfx_shader_init_job(zs, screen, 0); else diff --git a/src/gallium/drivers/zink/zink_program.h b/src/gallium/drivers/zink/zink_program.h index 65f9fa66381..2347d90170e 100644 --- a/src/gallium/drivers/zink/zink_program.h +++ b/src/gallium/drivers/zink/zink_program.h @@ -314,6 +314,9 @@ zink_get_tcs_key(const struct zink_context *ctx) void zink_update_fs_key_samples(struct zink_context *ctx); +void +zink_update_fs_dual_blend_key(struct zink_context *ctx); + void zink_update_gs_key_rectangular_line(struct zink_context *ctx); diff --git a/src/gallium/drivers/zink/zink_screen.c b/src/gallium/drivers/zink/zink_screen.c index 33bfb435e13..a796a469220 100644 --- a/src/gallium/drivers/zink/zink_screen.c +++ b/src/gallium/drivers/zink/zink_screen.c @@ -727,7 +727,7 @@ zink_get_param(struct pipe_screen *pscreen, enum pipe_cap param) case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS: if (!screen->info.feats.features.dualSrcBlend) - return 0; + return screen->emulate_dual_src_blend ? 1 : 0; return screen->info.props.limits.maxFragmentDualSrcAttachments; case PIPE_CAP_MAX_RENDER_TARGETS: @@ -3331,6 +3331,10 @@ init_optimal_keys(struct zink_screen *screen) screen->info.have_EXT_shader_object = false; if (screen->info.have_EXT_shader_object) screen->have_full_ds3 = true; + /* dual-source blending through coherent fbfetch needs a fs key and blend state baked into the pipeline */ + screen->emulate_dual_src_blend = !screen->info.feats.features.dualSrcBlend && + screen->info.have_EXT_rasterization_order_attachment_access && + !screen->optimal_keys && !screen->have_full_ds3; } static struct disk_cache * diff --git a/src/gallium/drivers/zink/zink_screen.h b/src/gallium/drivers/zink/zink_screen.h index c0a1b824d79..e0e7781da7e 100644 --- a/src/gallium/drivers/zink/zink_screen.h +++ b/src/gallium/drivers/zink/zink_screen.h @@ -78,6 +78,14 @@ zink_screen_init_semaphore(struct zink_screen *screen); bool zink_screen_emulates_packed_vertex_format(struct zink_screen *screen, enum pipe_format format); +/* the blend color member only exists where dual-source blending is emulated */ +static inline unsigned +zink_gfx_push_constant_size(const struct zink_screen *screen) +{ + return screen->emulate_dual_src_blend ? sizeof(struct zink_gfx_push_constant) : + offsetof(struct zink_gfx_push_constant, blend_color); +} + static inline bool zink_screen_handle_vkresult(struct zink_screen *screen, VkResult ret) { diff --git a/src/gallium/drivers/zink/zink_shader_keys.h b/src/gallium/drivers/zink/zink_shader_keys.h index bf570796009..9a4ef328565 100644 --- a/src/gallium/drivers/zink/zink_shader_keys.h +++ b/src/gallium/drivers/zink/zink_shader_keys.h @@ -94,6 +94,23 @@ struct zink_fs_key_base { uint8_t coord_replace_bits; }; +/* blending of color 0 done in the shader through fbfetch, for drivers without dualSrcBlend */ +struct zink_fs_dual_blend_key { + uint32_t enabled : 1; + uint32_t rgb_func : 3; + uint32_t rgb_src_factor : 5; + uint32_t rgb_dst_factor : 5; + uint32_t alpha_func : 3; + uint32_t alpha_src_factor : 5; + uint32_t alpha_dst_factor : 5; + uint32_t alpha_to_one : 1; + uint32_t alpha_to_coverage : 1; + uint32_t pad : 3; + uint16_t format; /* enum pipe_format standing in for cbuf 0: unorm, snorm or float */ + uint8_t coverage_samples_log2; + uint8_t pad2; +}; + struct zink_fs_key { struct zink_fs_key_base base; /* non-optimal bits after this point */ @@ -102,6 +119,7 @@ struct zink_fs_key { bool lower_point_smooth : 1; bool robust_access : 1; uint16_t pad2 : 12; + struct zink_fs_dual_blend_key dual_blend; }; struct zink_tcs_key { diff --git a/src/gallium/drivers/zink/zink_state.c b/src/gallium/drivers/zink/zink_state.c index a7a57487100..2a23b0c758a 100644 --- a/src/gallium/drivers/zink/zink_state.c +++ b/src/gallium/drivers/zink/zink_state.c @@ -356,10 +356,26 @@ fix_blendfactor(enum pipe_blendfactor f, bool alpha_to_one) return f; } +static enum pipe_blendfactor +src1_as_zero(enum pipe_blendfactor f) +{ + switch (f) { + case PIPE_BLENDFACTOR_SRC1_COLOR: + case PIPE_BLENDFACTOR_SRC1_ALPHA: + return PIPE_BLENDFACTOR_ZERO; + case PIPE_BLENDFACTOR_INV_SRC1_COLOR: + case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: + return PIPE_BLENDFACTOR_ONE; + default: + return f; + } +} + static void * zink_create_blend_state(struct pipe_context *pctx, const struct pipe_blend_state *blend_state) { + struct zink_screen *screen = zink_screen(pctx->screen); struct zink_blend_state *cso = CALLOC_STRUCT(zink_blend_state); if (!cso) return NULL; @@ -424,6 +440,34 @@ zink_create_blend_state(struct pipe_context *pctx, } cso->dual_src_blend = util_blend_state_is_dual(blend_state, 0); + if (screen->emulate_dual_src_blend && cso->dual_src_blend && blend_state->rt[0].blend_enable) { + const struct pipe_rt_blend_state *rt = blend_state->rt; + VkPipelineColorBlendAttachmentState *att = &cso->dual_blend_fallback; + *att = cso->attachments[0]; + att->srcColorBlendFactor = blend_factor(src1_as_zero(fix_blendfactor(rt->rgb_src_factor, cso->alpha_to_one))); + att->dstColorBlendFactor = blend_factor(src1_as_zero(fix_blendfactor(rt->rgb_dst_factor, cso->alpha_to_one))); + att->srcAlphaBlendFactor = blend_factor(src1_as_zero(fix_blendfactor(rt->alpha_src_factor, cso->alpha_to_one))); + att->dstAlphaBlendFactor = blend_factor(src1_as_zero(fix_blendfactor(rt->alpha_dst_factor, cso->alpha_to_one))); + if (cso->logicop_enable) { + cso->attachments[0] = *att; + } else { + cso->attachments[0].blendEnable = VK_FALSE; + cso->attachments[0].srcColorBlendFactor = VK_BLEND_FACTOR_ONE; + cso->attachments[0].dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; + cso->attachments[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; + cso->attachments[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; + cso->dual_blend.enabled = true; + cso->dual_blend.rgb_func = rt->rgb_func; + cso->dual_blend.rgb_src_factor = fix_blendfactor(rt->rgb_src_factor, cso->alpha_to_one); + cso->dual_blend.rgb_dst_factor = fix_blendfactor(rt->rgb_dst_factor, cso->alpha_to_one); + cso->dual_blend.alpha_func = rt->alpha_func; + cso->dual_blend.alpha_src_factor = fix_blendfactor(rt->alpha_src_factor, cso->alpha_to_one); + cso->dual_blend.alpha_dst_factor = fix_blendfactor(rt->alpha_dst_factor, cso->alpha_to_one); + cso->dual_blend.alpha_to_one = cso->alpha_to_one; + cso->dual_blend.alpha_to_coverage = cso->alpha_to_coverage; + } + } + return cso; } @@ -446,6 +490,7 @@ zink_bind_blend_state(struct pipe_context *pctx, void *cso) blend && blend->dual_src_blend && state->blend_state->attachments[0].blendEnable; if (force_dual_color_blend != zink_get_fs_base_key(ctx)->force_dual_color_blend) zink_set_fs_base_key(ctx)->force_dual_color_blend = force_dual_color_blend; + zink_update_fs_dual_blend_key(ctx); ctx->blend_state_changed = true; if (cso && screen->have_full_ds3) { diff --git a/src/gallium/drivers/zink/zink_types.h b/src/gallium/drivers/zink/zink_types.h index 76e9ca42de3..ffecde5d0a4 100644 --- a/src/gallium/drivers/zink/zink_types.h +++ b/src/gallium/drivers/zink/zink_types.h @@ -371,6 +371,10 @@ struct zink_blend_state { uint8_t enables; bool dual_src_blend; + /* without dualSrcBlend: hw blending is off for rt 0 and the fs blends when it writes a dual-source color */ + struct zink_fs_dual_blend_key dual_blend; + /* rt 0 for a fs that writes no dual-source color: src1 factors read it as zero */ + VkPipelineColorBlendAttachmentState dual_blend_fallback; }; struct zink_depth_stencil_alpha_hw_state { @@ -854,6 +858,8 @@ struct zink_shader { */ uint32_t legacy_shadow_mask; nir_variable *fbfetch; //for fs output + /* writes a dual-source color that is blended through fbfetch */ + bool dual_src_fbfetch; } fs; }; }; @@ -971,6 +977,7 @@ struct zink_gfx_push_constant { uint32_t line_stipple_pattern; float viewport_scale[2]; float line_width; + float blend_color[4]; }; /* The order of the enums MUST match the order of the zink_gfx_push_constant @@ -985,6 +992,7 @@ enum zink_gfx_push_constant_member { ZINK_GFX_PUSHCONST_LINE_STIPPLE_PATTERN, ZINK_GFX_PUSHCONST_VIEWPORT_SCALE, ZINK_GFX_PUSHCONST_LINE_WIDTH, + ZINK_GFX_PUSHCONST_BLEND_COLOR, ZINK_GFX_PUSHCONST_MAX }; @@ -1509,6 +1517,7 @@ struct zink_screen { bool have_D32_SFLOAT_S8_UINT; bool have_triangle_fans; bool need_decompose_attrs; + bool emulate_dual_src_blend; bool need_2D_zs; bool need_2D_sparse; bool can_hic_shader_read;