2
* Copyright (C) 2019-2021 Collabora, Ltd.
3
* Copyright (C) 2019 Alyssa Rosenzweig
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the "Software"),
7
* to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
* and/or sell copies of the Software, and to permit persons to whom the
10
* Software is furnished to do so, subject to the following conditions:
12
* The above copyright notice and this permission notice (including the next
13
* paragraph) shall be included in all copies or substantial portions of the
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28
* Implements the fragment pipeline (blending and writeout) in software, to be
29
* run as a dedicated "blend shader" stage on Midgard/Bifrost, or as a fragment
30
* shader variant on typical GPUs. This pass is useful if hardware lacks
31
* fixed-function blending in part or in full.
34
#include "compiler/nir/nir.h"
35
#include "compiler/nir/nir_builder.h"
36
#include "compiler/nir/nir_format_convert.h"
37
#include "nir_lower_blend.h"
39
/* Given processed factors, combine them per a blend function */
45
nir_ssa_def *src, nir_ssa_def *dst)
49
return nir_fadd(b, src, dst);
50
case BLEND_FUNC_SUBTRACT:
51
return nir_fsub(b, src, dst);
52
case BLEND_FUNC_REVERSE_SUBTRACT:
53
return nir_fsub(b, dst, src);
55
return nir_fmin(b, src, dst);
57
return nir_fmax(b, src, dst);
60
unreachable("Invalid blend function");
63
/* Does this blend function multiply by a blend factor? */
66
nir_blend_factored(enum blend_func func)
70
case BLEND_FUNC_SUBTRACT:
71
case BLEND_FUNC_REVERSE_SUBTRACT:
78
/* Compute a src_alpha_saturate factor */
82
nir_ssa_def *src, nir_ssa_def *dst,
85
nir_ssa_def *Asrc = nir_channel(b, src, 3);
86
nir_ssa_def *Adst = nir_channel(b, dst, 3);
87
nir_ssa_def *one = nir_imm_floatN_t(b, 1.0, src->bit_size);
88
nir_ssa_def *Adsti = nir_fsub(b, one, Adst);
90
return (chan < 3) ? nir_fmin(b, Asrc, Adsti) : one;
93
/* Returns a scalar single factor, unmultiplied */
96
nir_blend_factor_value(
98
nir_ssa_def *src, nir_ssa_def *src1, nir_ssa_def *dst, nir_ssa_def *bconst,
100
enum blend_factor factor)
103
case BLEND_FACTOR_ZERO:
104
return nir_imm_floatN_t(b, 0.0, src->bit_size);
105
case BLEND_FACTOR_SRC_COLOR:
106
return nir_channel(b, src, chan);
107
case BLEND_FACTOR_SRC1_COLOR:
108
return nir_channel(b, src1, chan);
109
case BLEND_FACTOR_DST_COLOR:
110
return nir_channel(b, dst, chan);
111
case BLEND_FACTOR_SRC_ALPHA:
112
return nir_channel(b, src, 3);
113
case BLEND_FACTOR_SRC1_ALPHA:
114
return nir_channel(b, src1, 3);
115
case BLEND_FACTOR_DST_ALPHA:
116
return nir_channel(b, dst, 3);
117
case BLEND_FACTOR_CONSTANT_COLOR:
118
return nir_channel(b, bconst, chan);
119
case BLEND_FACTOR_CONSTANT_ALPHA:
120
return nir_channel(b, bconst, 3);
121
case BLEND_FACTOR_SRC_ALPHA_SATURATE:
122
return nir_alpha_saturate(b, src, dst, chan);
125
unreachable("Invalid blend factor");
131
nir_ssa_def *raw_scalar,
132
nir_ssa_def *src, nir_ssa_def *src1, nir_ssa_def *dst, nir_ssa_def *bconst,
134
enum blend_factor factor,
138
nir_blend_factor_value(b, src, src1, dst, bconst, chan, factor);
141
f = nir_fadd_imm(b, nir_fneg(b, f), 1.0);
143
return nir_fmul(b, raw_scalar, f);
146
/* Given a colormask, "blend" with the destination */
156
nir_channel(b, (mask & (1 << 0)) ? src : dst, 0),
157
nir_channel(b, (mask & (1 << 1)) ? src : dst, 1),
158
nir_channel(b, (mask & (1 << 2)) ? src : dst, 2),
159
nir_channel(b, (mask & (1 << 3)) ? src : dst, 3));
166
nir_ssa_def *src, nir_ssa_def *dst)
169
case PIPE_LOGICOP_CLEAR:
170
return nir_imm_ivec4(b, 0, 0, 0, 0);
171
case PIPE_LOGICOP_NOR:
172
return nir_inot(b, nir_ior(b, src, dst));
173
case PIPE_LOGICOP_AND_INVERTED:
174
return nir_iand(b, nir_inot(b, src), dst);
175
case PIPE_LOGICOP_COPY_INVERTED:
176
return nir_inot(b, src);
177
case PIPE_LOGICOP_AND_REVERSE:
178
return nir_iand(b, src, nir_inot(b, dst));
179
case PIPE_LOGICOP_INVERT:
180
return nir_inot(b, dst);
181
case PIPE_LOGICOP_XOR:
182
return nir_ixor(b, src, dst);
183
case PIPE_LOGICOP_NAND:
184
return nir_inot(b, nir_iand(b, src, dst));
185
case PIPE_LOGICOP_AND:
186
return nir_iand(b, src, dst);
187
case PIPE_LOGICOP_EQUIV:
188
return nir_inot(b, nir_ixor(b, src, dst));
189
case PIPE_LOGICOP_NOOP:
191
case PIPE_LOGICOP_OR_INVERTED:
192
return nir_ior(b, nir_inot(b, src), dst);
193
case PIPE_LOGICOP_COPY:
195
case PIPE_LOGICOP_OR_REVERSE:
196
return nir_ior(b, src, nir_inot(b, dst));
197
case PIPE_LOGICOP_OR:
198
return nir_ior(b, src, dst);
199
case PIPE_LOGICOP_SET:
200
return nir_imm_ivec4(b, ~0, ~0, ~0, ~0);
203
unreachable("Invalid logciop function");
209
nir_lower_blend_options options,
211
nir_ssa_def *src, nir_ssa_def *dst)
213
unsigned bit_size = src->bit_size;
214
const struct util_format_description *format_desc =
215
util_format_description(options.format[rt]);
217
if (bit_size != 32) {
218
src = nir_f2f32(b, src);
219
dst = nir_f2f32(b, dst);
222
assert(src->num_components <= 4);
223
assert(dst->num_components <= 4);
226
for (int i = 0; i < 4; ++i)
227
bits[i] = format_desc->channel[i].size;
229
src = nir_format_float_to_unorm(b, src, bits);
230
dst = nir_format_float_to_unorm(b, dst, bits);
232
nir_ssa_def *out = nir_logicop_func(b, options.logicop_func, src, dst);
235
nir_const_value mask[4];
236
for (int i = 0; i < 4; ++i)
237
mask[i] = nir_const_value_for_int((1u << bits[i]) - 1, 32);
239
out = nir_iand(b, out, nir_build_imm(b, 4, 32, mask));
242
out = nir_format_unorm_to_float(b, out, bits);
245
out = nir_f2f16(b, out);
251
nir_fsat_signed(nir_builder *b, nir_ssa_def *x)
253
return nir_fclamp(b, x, nir_imm_floatN_t(b, -1.0, x->bit_size),
254
nir_imm_floatN_t(b, +1.0, x->bit_size));
257
/* Given a blend state, the source color, and the destination color,
258
* return the blended color
264
nir_lower_blend_options options,
266
nir_ssa_def *src, nir_ssa_def *src1, nir_ssa_def *dst)
268
/* Grab the blend constant ahead of time */
270
if (options.scalar_blend_const) {
272
nir_load_blend_const_color_r_float(b),
273
nir_load_blend_const_color_g_float(b),
274
nir_load_blend_const_color_b_float(b),
275
nir_load_blend_const_color_a_float(b));
277
bconst = nir_load_blend_const_color_rgba(b);
280
if (src->bit_size == 16)
281
bconst = nir_f2f16(b, bconst);
283
/* Fixed-point framebuffers require their inputs clamped. */
284
enum pipe_format format = options.format[rt];
286
/* From section 17.3.6 "Blending" of the OpenGL 4.5 spec:
288
* If the color buffer is fixed-point, the components of the source and
289
* destination values and blend factors are each clamped to [0, 1] or
290
* [-1, 1] respectively for an unsigned normalized or signed normalized
291
* color buffer prior to evaluating the blend equation. If the color
292
* buffer is floating-point, no clamping occurs.
294
if (util_format_is_unorm(format))
295
src = nir_fsat(b, src);
296
else if (util_format_is_snorm(format))
297
src = nir_fsat_signed(b, src);
299
/* DST_ALPHA reads back 1.0 if there is no alpha channel */
300
const struct util_format_description *desc =
301
util_format_description(format);
303
if (desc->nr_channels < 4) {
304
nir_ssa_def *zero = nir_imm_floatN_t(b, 0.0, dst->bit_size);
305
nir_ssa_def *one = nir_imm_floatN_t(b, 1.0, dst->bit_size);
307
dst = nir_vec4(b, nir_channel(b, dst, 0),
308
desc->nr_channels > 1 ? nir_channel(b, dst, 1) : zero,
309
desc->nr_channels > 2 ? nir_channel(b, dst, 2) : zero,
310
desc->nr_channels > 3 ? nir_channel(b, dst, 3) : one);
313
/* We blend per channel and recombine later */
314
nir_ssa_def *channels[4];
316
for (unsigned c = 0; c < 4; ++c) {
317
/* Decide properties based on channel */
318
nir_lower_blend_channel chan =
319
(c < 3) ? options.rt[rt].rgb : options.rt[rt].alpha;
321
nir_ssa_def *psrc = nir_channel(b, src, c);
322
nir_ssa_def *pdst = nir_channel(b, dst, c);
324
if (nir_blend_factored(chan.func)) {
325
psrc = nir_blend_factor(
327
src, src1, dst, bconst, c,
328
chan.src_factor, chan.invert_src_factor);
330
pdst = nir_blend_factor(
332
src, src1, dst, bconst, c,
333
chan.dst_factor, chan.invert_dst_factor);
336
channels[c] = nir_blend_func(b, chan.func, psrc, pdst);
339
return nir_vec(b, channels, 4);
343
nir_lower_blend_instr(nir_builder *b, nir_instr *instr, void *data)
345
nir_lower_blend_options *options = data;
346
if (instr->type != nir_instr_type_intrinsic)
349
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
350
if (intr->intrinsic != nir_intrinsic_store_deref)
353
nir_variable *var = nir_intrinsic_get_var(intr, 0);
354
if (var->data.mode != nir_var_shader_out ||
355
(var->data.location != FRAG_RESULT_COLOR &&
356
var->data.location < FRAG_RESULT_DATA0))
359
/* Determine render target for per-RT blending */
361
(var->data.location == FRAG_RESULT_COLOR) ? 0 :
362
(var->data.location - FRAG_RESULT_DATA0);
364
/* No blend lowering requested on this RT */
365
if (options->format[rt] == PIPE_FORMAT_NONE)
368
b->cursor = nir_before_instr(instr);
370
/* Grab the input color */
371
unsigned src_num_comps = nir_src_num_components(intr->src[1]);
373
nir_pad_vector(b, nir_ssa_for_src(b, intr->src[1], src_num_comps), 4);
375
/* Grab the previous fragment color */
376
var->data.fb_fetch_output = true;
377
b->shader->info.outputs_read |= BITFIELD64_BIT(var->data.location);
378
b->shader->info.fs.uses_fbfetch_output = true;
379
nir_ssa_def *dst = nir_load_var(b, var);
381
/* Blend the two colors per the passed options */
382
nir_ssa_def *blended = src;
384
if (options->logicop_enable) {
385
blended = nir_blend_logicop(b, *options, rt, src, dst);
386
} else if (!util_format_is_pure_integer(options->format[rt])) {
387
assert(!util_format_is_scaled(options->format[rt]));
388
blended = nir_blend(b, *options, rt, src, options->src1, dst);
391
/* Apply a colormask */
392
blended = nir_color_mask(b, options->rt[rt].colormask, blended, dst);
394
if (src_num_comps != 4)
395
blended = nir_channels(b, blended, nir_component_mask(src_num_comps));
397
/* Write out the final color instead of the input */
398
nir_instr_rewrite_src_ssa(instr, &intr->src[1], blended);
403
nir_lower_blend(nir_shader *shader, nir_lower_blend_options options)
405
assert(shader->info.stage == MESA_SHADER_FRAGMENT);
407
nir_shader_instructions_pass(shader, nir_lower_blend_instr,
408
nir_metadata_block_index | nir_metadata_dominance, &options);