0f0e1b5ffd91b91b2f3ae357d6fbaad052a02f4e
[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 "gallium/auxiliary/util/u_blend.h"
32 #include "util/u_memory.h"
33
34 /*
35 * Implements the command stream portion of programmatic blend shaders.
36 *
37 * On Midgard, common blending operations are accelerated by the fixed-function
38 * blending pipeline. Panfrost supports this fast path via the code in
39 * pan_blending.c. Nevertheless, uncommon blend modes (including some seemingly
40 * simple modes present in ES2) require "blend shaders", a special internal
41 * shader type used for programmable blending.
42 *
43 * Blend shaders operate during the normal blending time, but they bypass the
44 * fixed-function blending pipeline and instead go straight to the Midgard
45 * shader cores. The shaders themselves are essentially just fragment shaders,
46 * making heavy use of uint8 arithmetic to manipulate RGB values for the
47 * framebuffer.
48 *
49 * As is typical with Midgard, shader binaries must be accompanied by
50 * information about the first tag (ORed with the bottom nibble of address,
51 * like usual) and work registers. Work register count is specified in the
52 * blend descriptor, as well as in the coresponding fragment shader's work
53 * count. This suggests that blend shader invocation is tied to fragment shader
54 * execution.
55 *
56 * ---
57 *
58 * As for blend shaders, they use the standard ISA.
59 *
60 * The source pixel colour, including alpha, is preloaded into r0 as a vec4 of
61 * float32.
62 *
63 * The destination pixel colour must be loaded explicitly via load/store ops.
64 * TODO: Investigate.
65 *
66 * They use fragment shader writeout; however, instead of writing a vec4 of
67 * float32 for RGBA encoding, we writeout a vec4 of uint8, using 8-bit imov
68 * instead of 32-bit fmov. The net result is that r0 encodes a single uint32
69 * containing all four channels of the color. Accordingly, the blend shader
70 * epilogue has to scale all four channels by 255 and then type convert to a
71 * uint8.
72 *
73 * ---
74 *
75 * Blend shaders hardcode constants. Naively, this requires recompilation each
76 * time the blend color changes, which is a performance risk. Accordingly, we
77 * 'cheat' a bit: instead of loading the constant, we compile a shader with a
78 * dummy constant, exporting the offset to the immediate in the shader binary,
79 * storing this generic binary and metadata in the CSO itself at CSO create
80 * time.
81 *
82 * We then hot patch in the color into this shader at attachment / color change
83 * time, allowing for CSO create to be the only expensive operation
84 * (compilation).
85 */
86
87 static nir_lower_blend_options
88 nir_make_options(const struct pipe_blend_state *blend, unsigned i)
89 {
90 nir_lower_blend_options options;
91
92 if (blend->logicop_enable) {
93 options.logicop_enable = true;
94 options.logicop_func = blend->logicop_func;
95 return options;
96 }
97
98 options.logicop_enable = false;
99
100 /* If blend is disabled, we just use replace mode */
101
102 nir_lower_blend_channel rgb = {
103 .func = BLEND_FUNC_ADD,
104 .src_factor = BLEND_FACTOR_ZERO,
105 .invert_src_factor = true,
106 .dst_factor = BLEND_FACTOR_ZERO,
107 .invert_dst_factor = false
108 };
109
110 nir_lower_blend_channel alpha = rgb;
111
112 if (blend->rt[i].blend_enable) {
113 rgb.func = util_blend_func_to_shader(blend->rt[i].rgb_func);
114 rgb.src_factor = util_blend_factor_to_shader(blend->rt[i].rgb_src_factor);
115 rgb.dst_factor = util_blend_factor_to_shader(blend->rt[i].rgb_dst_factor);
116 rgb.invert_src_factor = util_blend_factor_is_inverted(blend->rt[i].rgb_src_factor);
117 rgb.invert_dst_factor = util_blend_factor_is_inverted(blend->rt[i].rgb_dst_factor);
118
119 alpha.func = util_blend_func_to_shader(blend->rt[i].alpha_func);
120 alpha.src_factor = util_blend_factor_to_shader(blend->rt[i].alpha_src_factor);
121 alpha.dst_factor = util_blend_factor_to_shader(blend->rt[i].alpha_dst_factor);
122 alpha.invert_src_factor = util_blend_factor_is_inverted(blend->rt[i].alpha_src_factor);
123 alpha.invert_dst_factor = util_blend_factor_is_inverted(blend->rt[i].alpha_dst_factor);
124 }
125
126 options.rgb = rgb;
127 options.alpha = alpha;
128
129 options.colormask = blend->rt[i].colormask;
130
131 return options;
132 }
133
134 struct panfrost_blend_shader
135 panfrost_compile_blend_shader(
136 struct panfrost_context *ctx,
137 struct pipe_blend_state *cso,
138 enum pipe_format format,
139 unsigned rt)
140 {
141 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
142 struct panfrost_blend_shader res;
143
144 res.ctx = ctx;
145
146 /* Build the shader */
147
148 nir_shader *shader = nir_shader_create(NULL, MESA_SHADER_FRAGMENT, &midgard_nir_options, NULL);
149 nir_function *fn = nir_function_create(shader, "main");
150 nir_function_impl *impl = nir_function_impl_create(fn);
151
152 /* Create the blend variables */
153
154 nir_variable *c_src = nir_variable_create(shader, nir_var_shader_in, glsl_vector_type(GLSL_TYPE_FLOAT, 4), "gl_Color");
155 nir_variable *c_out = nir_variable_create(shader, nir_var_shader_out, glsl_vector_type(GLSL_TYPE_FLOAT, 4), "gl_FragColor");
156
157 c_src->data.location = VARYING_SLOT_COL0;
158 c_out->data.location = FRAG_RESULT_COLOR;
159
160 /* Setup nir_builder */
161
162 nir_builder _b;
163 nir_builder *b = &_b;
164 nir_builder_init(b, impl);
165 b->cursor = nir_before_block(nir_start_block(impl));
166
167 /* Setup inputs */
168
169 nir_ssa_def *s_src = nir_load_var(b, c_src);
170
171 /* Build a trivial blend shader */
172 nir_store_var(b, c_out, s_src, 0xFF);
173
174 nir_lower_blend_options options =
175 nir_make_options(cso, rt);
176 NIR_PASS_V(shader, nir_lower_blend, options);
177
178 NIR_PASS_V(shader, nir_lower_framebuffer, format, screen->gpu_id);
179
180 /* Compile the built shader */
181
182 midgard_program program;
183 midgard_compile_shader_nir(shader, &program, true, rt, screen->gpu_id, false);
184
185 /* Allow us to patch later */
186 res.patch_index = program.blend_patch_offset;
187 res.first_tag = program.first_tag;
188 res.size = program.compiled.size;
189 res.buffer = program.compiled.data;
190
191 return res;
192 }