panfrost: Don't advertise MSAA 2x
[mesa.git] / src / gallium / drivers / panfrost / pan_blend_shaders.c
1 /*
2 * © Copyright 2018 Alyssa Rosenzweig
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 */
24
25 #include <stdio.h>
26 #include "pan_blend_shaders.h"
27 #include "pan_util.h"
28 #include "midgard/midgard_compile.h"
29 #include "compiler/nir/nir_builder.h"
30 #include "nir/nir_lower_blend.h"
31 #include "panfrost/util/pan_lower_framebuffer.h"
32 #include "gallium/auxiliary/util/u_blend.h"
33 #include "util/u_memory.h"
34
35 /*
36 * Implements the command stream portion of programmatic blend shaders.
37 *
38 * On Midgard, common blending operations are accelerated by the fixed-function
39 * blending pipeline. Panfrost supports this fast path via the code in
40 * pan_blending.c. Nevertheless, uncommon blend modes (including some seemingly
41 * simple modes present in ES2) require "blend shaders", a special internal
42 * shader type used for programmable blending.
43 *
44 * Blend shaders operate during the normal blending time, but they bypass the
45 * fixed-function blending pipeline and instead go straight to the Midgard
46 * shader cores. The shaders themselves are essentially just fragment shaders,
47 * making heavy use of uint8 arithmetic to manipulate RGB values for the
48 * framebuffer.
49 *
50 * As is typical with Midgard, shader binaries must be accompanied by
51 * information about the first tag (ORed with the bottom nibble of address,
52 * like usual) and work registers. Work register count is assumed to be less
53 * than or equal to the coresponding fragment shader's work count. This
54 * suggests that blend shader invocation is tied to fragment shader
55 * execution.
56 *
57 * The shaders themselves use the standard ISA. The source pixel colour,
58 * including alpha, is preloaded into r0 as a vec4 of float32. The destination
59 * pixel colour must be loaded explicitly via load/store ops, possibly
60 * performing conversions in software. The blended colour must be stored with a
61 * fragment writeout in the correct framebuffer format, either in software or
62 * via conversion opcodes on the load/store pipe.
63 *
64 * Blend shaders hardcode constants. Naively, this requires recompilation each
65 * time the blend color changes, which is a performance risk. Accordingly, we
66 * 'cheat' a bit: instead of loading the constant, we compile a shader with a
67 * dummy constant, exporting the offset to the immediate in the shader binary,
68 * storing this generic binary and metadata in the CSO itself at CSO create
69 * time.
70 *
71 * We then hot patch in the color into this shader at attachment / color change
72 * time, allowing for CSO create to be the only expensive operation
73 * (compilation).
74 */
75
76 static nir_lower_blend_options
77 nir_make_options(const struct pipe_blend_state *blend, unsigned i)
78 {
79 nir_lower_blend_options options = { 0 };
80
81 if (blend->logicop_enable) {
82 options.logicop_enable = true;
83 options.logicop_func = blend->logicop_func;
84 return options;
85 }
86
87 options.logicop_enable = false;
88
89 if (!blend->independent_blend_enable)
90 i = 0;
91
92 /* If blend is disabled, we just use replace mode */
93
94 nir_lower_blend_channel rgb = {
95 .func = BLEND_FUNC_ADD,
96 .src_factor = BLEND_FACTOR_ZERO,
97 .invert_src_factor = true,
98 .dst_factor = BLEND_FACTOR_ZERO,
99 .invert_dst_factor = false
100 };
101
102 nir_lower_blend_channel alpha = rgb;
103
104 if (blend->rt[i].blend_enable) {
105 rgb.func = util_blend_func_to_shader(blend->rt[i].rgb_func);
106 rgb.src_factor = util_blend_factor_to_shader(blend->rt[i].rgb_src_factor);
107 rgb.dst_factor = util_blend_factor_to_shader(blend->rt[i].rgb_dst_factor);
108 rgb.invert_src_factor = util_blend_factor_is_inverted(blend->rt[i].rgb_src_factor);
109 rgb.invert_dst_factor = util_blend_factor_is_inverted(blend->rt[i].rgb_dst_factor);
110
111 alpha.func = util_blend_func_to_shader(blend->rt[i].alpha_func);
112 alpha.src_factor = util_blend_factor_to_shader(blend->rt[i].alpha_src_factor);
113 alpha.dst_factor = util_blend_factor_to_shader(blend->rt[i].alpha_dst_factor);
114 alpha.invert_src_factor = util_blend_factor_is_inverted(blend->rt[i].alpha_src_factor);
115 alpha.invert_dst_factor = util_blend_factor_is_inverted(blend->rt[i].alpha_dst_factor);
116 }
117
118 options.rgb = rgb;
119 options.alpha = alpha;
120
121 options.colormask = blend->rt[i].colormask;
122
123 return options;
124 }
125
126 struct panfrost_blend_shader
127 panfrost_compile_blend_shader(
128 struct panfrost_context *ctx,
129 struct pipe_blend_state *cso,
130 enum pipe_format format,
131 unsigned rt)
132 {
133 struct panfrost_device *dev = pan_device(ctx->base.screen);
134 struct panfrost_blend_shader res;
135
136 res.ctx = ctx;
137
138 /* Build the shader */
139
140 nir_shader *shader = nir_shader_create(NULL, MESA_SHADER_FRAGMENT, &midgard_nir_options, NULL);
141 nir_function *fn = nir_function_create(shader, "main");
142 nir_function_impl *impl = nir_function_impl_create(fn);
143
144 const struct util_format_description *format_desc =
145 util_format_description(format);
146
147 nir_alu_type T = pan_unpacked_type_for_format(format_desc);
148 enum glsl_base_type g =
149 (T == nir_type_float16) ? GLSL_TYPE_FLOAT16 :
150 (T == nir_type_float32) ? GLSL_TYPE_FLOAT :
151 (T == nir_type_int8) ? GLSL_TYPE_INT8 :
152 (T == nir_type_int16) ? GLSL_TYPE_INT16 :
153 (T == nir_type_int32) ? GLSL_TYPE_INT :
154 (T == nir_type_uint8) ? GLSL_TYPE_UINT8 :
155 (T == nir_type_uint16) ? GLSL_TYPE_UINT16 :
156 (T == nir_type_uint32) ? GLSL_TYPE_UINT :
157 GLSL_TYPE_FLOAT;
158
159 /* Create the blend variables */
160
161 nir_variable *c_src = nir_variable_create(shader, nir_var_shader_in, glsl_vector_type(GLSL_TYPE_FLOAT, 4), "gl_Color");
162 nir_variable *c_out = nir_variable_create(shader, nir_var_shader_out, glsl_vector_type(g, 4), "gl_FragColor");
163
164 c_src->data.location = VARYING_SLOT_COL0;
165 c_out->data.location = FRAG_RESULT_COLOR;
166
167 /* Setup nir_builder */
168
169 nir_builder _b;
170 nir_builder *b = &_b;
171 nir_builder_init(b, impl);
172 b->cursor = nir_before_block(nir_start_block(impl));
173
174 /* Setup inputs */
175
176 nir_ssa_def *s_src = nir_load_var(b, c_src);
177
178 if (T == nir_type_float16)
179 s_src = nir_f2f16(b, s_src);
180 else if (T == nir_type_int16)
181 s_src = nir_i2i16(b, s_src);
182 else if (T == nir_type_uint16)
183 s_src = nir_u2u16(b, s_src);
184 else if (T == nir_type_int8)
185 s_src = nir_i2i8(b, s_src);
186 else if (T == nir_type_uint8)
187 s_src = nir_u2u8(b, s_src);
188
189 /* Build a trivial blend shader */
190 nir_store_var(b, c_out, s_src, 0xFF);
191
192 nir_lower_blend_options options =
193 nir_make_options(cso, rt);
194 options.format = format;
195
196 if (T == nir_type_float16)
197 options.half = true;
198
199 NIR_PASS_V(shader, nir_lower_blend, options);
200 NIR_PASS_V(shader, pan_lower_framebuffer, format_desc, dev->quirks);
201
202 /* Compile the built shader */
203
204 panfrost_program program;
205 midgard_compile_shader_nir(shader, &program, true, rt, dev->gpu_id, false);
206
207 /* Allow us to patch later */
208 res.patch_index = program.blend_patch_offset;
209 res.first_tag = program.first_tag;
210 res.size = program.compiled.size;
211 res.buffer = program.compiled.data;
212
213 return res;
214 }