| 1 |
/* |
| 2 |
* Copyright (C) 2026 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "pan_nir.h" |
| 7 |
#include "nir_builder.h" |
| 8 |
|
| 9 |
struct pan_nir_fs_outputs { |
| 10 |
bool has_pan_ops; |
| 11 |
nir_variable *depth; |
| 12 |
nir_variable *stencil; |
| 13 |
nir_variable *coverage; |
| 14 |
nir_variable *color[2]; |
| 15 |
nir_variable *data[8][2]; |
| 16 |
}; |
| 17 |
|
| 18 |
static bool |
| 19 |
gather_output_intrin(nir_builder *b, nir_intrinsic_instr *intrin, void *_data) |
| 20 |
{ |
| 21 |
struct pan_nir_fs_outputs *out = _data; |
| 22 |
|
| 23 |
if (intrin->intrinsic == nir_intrinsic_atest_pan || |
| 24 |
intrin->intrinsic == nir_intrinsic_zs_emit_pan || |
| 25 |
intrin->intrinsic == nir_intrinsic_blend_pan || |
| 26 |
intrin->intrinsic == nir_intrinsic_blend2_pan) |
| 27 |
out->has_pan_ops = true; |
| 28 |
|
| 29 |
if (intrin->intrinsic != nir_intrinsic_store_output) |
| 30 |
return false; |
| 31 |
|
| 32 |
nir_def *data = intrin->src[0].ssa; |
| 33 |
assert(nir_src_as_uint(intrin->src[1]) == 0); |
| 34 |
|
| 35 |
nir_io_semantics io = nir_intrinsic_io_semantics(intrin); |
| 36 |
nir_alu_type src_type = nir_intrinsic_src_type(intrin); |
| 37 |
|
| 38 |
nir_variable **var; |
| 39 |
unsigned num_components = 1; |
| 40 |
const char *name; |
| 41 |
switch (io.location) { |
| 42 |
case FRAG_RESULT_DEPTH: |
| 43 |
var = &out->depth; |
| 44 |
name = "depth_tmp"; |
| 45 |
break; |
| 46 |
case FRAG_RESULT_STENCIL: |
| 47 |
var = &out->stencil; |
| 48 |
name = "stencil_tmp"; |
| 49 |
break; |
| 50 |
case FRAG_RESULT_SAMPLE_MASK: |
| 51 |
var = &out->coverage; |
| 52 |
name = "coverage_tmp"; |
| 53 |
break; |
| 54 |
case FRAG_RESULT_COLOR: |
| 55 |
var = &out->color[io.dual_source_blend_index]; |
| 56 |
num_components = 4; |
| 57 |
name = "color_tmp"; |
| 58 |
break; |
| 59 |
default: { |
| 60 |
assert(io.location >= FRAG_RESULT_DATA0); |
| 61 |
assert(io.location <= FRAG_RESULT_DATA7); |
| 62 |
unsigned slot = io.location - FRAG_RESULT_DATA0; |
| 63 |
var = &out->data[slot][io.dual_source_blend_index]; |
| 64 |
num_components = 4; |
| 65 |
name = "data_tmp"; |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if (*var == NULL) { |
| 71 |
const struct glsl_type *type = |
| 72 |
glsl_vector_type(nir_get_glsl_base_type_for_nir_type(src_type), |
| 73 |
num_components); |
| 74 |
*var = nir_local_variable_create(b->impl, type, name); |
| 75 |
} |
| 76 |
|
| 77 |
b->cursor = nir_after_instr(&intrin->instr); |
| 78 |
|
| 79 |
unsigned component = nir_intrinsic_component(intrin); |
| 80 |
data = nir_shift_channels(b, data, component, num_components); |
| 81 |
nir_component_mask_t write_mask = |
| 82 |
nir_intrinsic_write_mask(intrin) << component; |
| 83 |
|
| 84 |
nir_store_var(b, *var, data, write_mask); |
| 85 |
nir_instr_remove(&intrin->instr); |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
bool |
| 91 |
pan_nir_lower_fs_outputs(nir_shader *shader, bool skip_atest) |
| 92 |
{ |
| 93 |
nir_function_impl *impl = nir_shader_get_entrypoint(shader); |
| 94 |
|
| 95 |
struct pan_nir_fs_outputs out = {}; |
| 96 |
bool has_store = nir_function_intrinsics_pass(impl, gather_output_intrin, |
| 97 |
nir_metadata_control_flow, |
| 98 |
&out); |
| 99 |
assert(!has_store || !out.has_pan_ops); |
| 100 |
if (!has_store && (out.has_pan_ops || skip_atest)) |
| 101 |
return false; |
| 102 |
|
| 103 |
nir_builder builder = nir_builder_create(impl); |
| 104 |
nir_builder *b = &builder; |
| 105 |
|
| 106 |
/* We shouldn't have both gl_FragColor and gl_FragData */ |
| 107 |
assert(!out.color[0] || !out.data[0][0]); |
| 108 |
nir_variable *color0 = out.color[0] ? out.color[0] : out.data[0][0]; |
| 109 |
|
| 110 |
/* Default alpha to 1.0 for the sake of atest */ |
| 111 |
b->cursor = nir_before_impl(impl); |
| 112 |
if (color0) { |
| 113 |
if (glsl_type_is_float(color0->type)) |
| 114 |
nir_store_var(b, color0, nir_imm_vec4(b, 0, 0, 0, 1), 0b1000); |
| 115 |
else if (glsl_type_is_float_16(color0->type)) |
| 116 |
nir_store_var(b, color0, nir_imm_vec4_16(b, 0, 0, 0, 1), 0b1000); |
| 117 |
} |
| 118 |
|
| 119 |
b->cursor = nir_after_impl(impl); |
| 120 |
|
| 121 |
nir_def *coverage; |
| 122 |
if (out.coverage) { |
| 123 |
/* For sample masks coming from the client, we have to AND with the |
| 124 |
* input sample mask in case they include disabled channels. |
| 125 |
*/ |
| 126 |
coverage = nir_iand(b, nir_load_var(b, out.coverage), |
| 127 |
nir_load_sample_mask(b)); |
| 128 |
} else { |
| 129 |
coverage = nir_load_cumulative_coverage_pan(b); |
| 130 |
} |
| 131 |
|
| 132 |
/* From the OpenGL 4.6 core specification: |
| 133 |
* |
| 134 |
* "All alpha values in this section refer only to the alpha component |
| 135 |
* of the fragment shader output linked to color number zero, index |
| 136 |
* zero" |
| 137 |
*/ |
| 138 |
nir_def *alpha; |
| 139 |
if (color0 && glsl_type_is_float_16_32(color0->type)) |
| 140 |
alpha = nir_f2f32(b, nir_channel(b, nir_load_var(b, color0), 3)); |
| 141 |
else |
| 142 |
alpha = nir_imm_float(b, 1.0f); |
| 143 |
|
| 144 |
/* Emit ATEST if we have to, note ATEST requires a floating-point alpha |
| 145 |
* value, but render target #0 might not be floating point. However the |
| 146 |
* alpha value is only used for alpha-to-coverage, a stage which is |
| 147 |
* skipped for pure integer framebuffers, so the issue is moot. |
| 148 |
*/ |
| 149 |
if (!skip_atest) |
| 150 |
coverage = nir_atest_pan(b, coverage, nir_f2f32(b, alpha)); |
| 151 |
|
| 152 |
/* We discard depth/stencil writes if early fragment tests is forced. */ |
| 153 |
if ((out.depth || out.stencil) && !shader->info.fs.early_fragment_tests) { |
| 154 |
/* EMIT_ZS requires an ATEST */ |
| 155 |
assert(!skip_atest); |
| 156 |
|
| 157 |
unsigned flags = 0; |
| 158 |
nir_def *depth, *stencil; |
| 159 |
if (out.depth) { |
| 160 |
depth = nir_load_var(b, out.depth); |
| 161 |
assert(depth->bit_size == 32); |
| 162 |
flags |= BITFIELD_BIT(FRAG_RESULT_DEPTH); |
| 163 |
} else { |
| 164 |
depth = nir_undef(b, 1, 32); |
| 165 |
} |
| 166 |
|
| 167 |
if (out.stencil) { |
| 168 |
stencil = nir_load_var(b, out.stencil); |
| 169 |
assert(stencil->bit_size == 32); |
| 170 |
flags |= BITFIELD_BIT(FRAG_RESULT_STENCIL); |
| 171 |
} else { |
| 172 |
stencil = nir_undef(b, 1, 32); |
| 173 |
} |
| 174 |
|
| 175 |
coverage = nir_zs_emit_pan(b, coverage, depth, stencil, .flags = flags); |
| 176 |
} |
| 177 |
|
| 178 |
for (unsigned i = 0; i < 8; i++) { |
| 179 |
nir_variable **color = out.color[0] ? out.color : out.data[i]; |
| 180 |
if (!color[0]) |
| 181 |
continue; |
| 182 |
|
| 183 |
nir_def *color0 = nir_load_var(b, color[0]); |
| 184 |
nir_alu_type color0_type = |
| 185 |
nir_get_nir_type_for_glsl_base_type(color[0]->type->base_type); |
| 186 |
|
| 187 |
nir_def *desc = nir_load_blend_descriptor_pan(b, .base = i); |
| 188 |
if (color[1]) { |
| 189 |
nir_def *color1 = nir_load_var(b, color[1]); |
| 190 |
nir_alu_type color1_type = |
| 191 |
nir_get_nir_type_for_glsl_base_type(color[1]->type->base_type); |
| 192 |
|
| 193 |
nir_blend2_pan(b, coverage, desc, color0, color1, |
| 194 |
.src_type = color0_type, .dest_type = color1_type, |
| 195 |
.io_semantics.location = FRAG_RESULT_DATA0 + i); |
| 196 |
} else { |
| 197 |
nir_blend_pan(b, coverage, desc, color0, .src_type = color0_type, |
| 198 |
.io_semantics.location = FRAG_RESULT_DATA0 + i); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return nir_progress(true, impl, nir_metadata_control_flow); |
| 203 |
} |