panfrost: Add quirks system to cmdstream
[mesa.git] / src / gallium / drivers / panfrost / pan_blend_cso.c
1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
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 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 *
26 */
27
28 #include <stdio.h>
29 #include "util/u_memory.h"
30 #include "pan_blend_shaders.h"
31 #include "pan_blending.h"
32 #include "pan_bo.h"
33 #include "panfrost-quirks.h"
34
35 /* A given Gallium blend state can be encoded to the hardware in numerous,
36 * dramatically divergent ways due to the interactions of blending with
37 * framebuffer formats. Conceptually, there are two modes:
38 *
39 * - Fixed-function blending (for suitable framebuffer formats, suitable blend
40 * state, and suitable blend constant)
41 *
42 * - Blend shaders (for everything else)
43 *
44 * A given Gallium blend configuration will compile to exactly one
45 * fixed-function blend state, if it compiles to any, although the constant
46 * will vary across runs as that is tracked outside of the Gallium CSO.
47 *
48 * However, that same blend configuration will compile to many different blend
49 * shaders, depending on the framebuffer formats active. The rationale is that
50 * blend shaders override not just fixed-function blending but also
51 * fixed-function format conversion. As such, each blend shader must be
52 * hardcoded to a particular framebuffer format to correctly pack/unpack it. As
53 * a concrete example, to the hardware there is no difference (!) between RG16F
54 * and RG16UI -- both are simply 4-byte-per-pixel chunks. Thus both formats
55 * require a blend shader (even with blending is totally disabled!), required
56 * to do conversion as necessary (if necessary).
57 *
58 * All of this state is encapsulated in the panfrost_blend_state struct
59 * (our subclass of pipe_blend_state).
60 */
61
62 /* Given an initialized CSO and a particular framebuffer format, grab a
63 * blend shader, generating and compiling it if it doesn't exist
64 * (lazy-loading in a way). This routine, when the cache hits, should
65 * befast, suitable for calling every draw to avoid wacky dirty
66 * tracking paths. If the cache hits, boom, done. */
67
68 static struct panfrost_blend_shader *
69 panfrost_get_blend_shader(
70 struct panfrost_context *ctx,
71 struct panfrost_blend_state *blend,
72 enum pipe_format fmt,
73 unsigned rt)
74 {
75 /* Prevent NULL collision issues.. */
76 assert(fmt != 0);
77
78 /* Check the cache */
79 struct hash_table_u64 *shaders = blend->rt[rt].shaders;
80
81 struct panfrost_blend_shader *shader =
82 _mesa_hash_table_u64_search(shaders, fmt);
83
84 if (shader)
85 return shader;
86
87 /* Cache miss. Build one instead, cache it, and go */
88
89 struct panfrost_blend_shader generated =
90 panfrost_compile_blend_shader(ctx, &blend->base, fmt);
91
92 shader = mem_dup(&generated, sizeof(generated));
93 _mesa_hash_table_u64_insert(shaders, fmt, shader);
94 return shader;
95 }
96
97 /* Create a blend CSO. Essentially, try to compile a fixed-function
98 * expression and initialize blend shaders */
99
100 static void *
101 panfrost_create_blend_state(struct pipe_context *pipe,
102 const struct pipe_blend_state *blend)
103 {
104 struct panfrost_context *ctx = pan_context(pipe);
105 struct panfrost_blend_state *so = rzalloc(ctx, struct panfrost_blend_state);
106 so->base = *blend;
107
108 /* TODO: The following features are not yet implemented */
109 assert(!blend->logicop_enable);
110 assert(!blend->alpha_to_coverage);
111 assert(!blend->alpha_to_one);
112
113 for (unsigned c = 0; c < PIPE_MAX_COLOR_BUFS; ++c) {
114 struct panfrost_blend_rt *rt = &so->rt[c];
115
116 /* There are two paths. First, we would like to try a
117 * fixed-function if we can */
118
119 /* Without indep blending, the first RT settings replicate */
120
121 unsigned g =
122 blend->independent_blend_enable ? c : 0;
123
124 rt->has_fixed_function =
125 panfrost_make_fixed_blend_mode(
126 &blend->rt[g],
127 &rt->equation,
128 &rt->constant_mask,
129 blend->rt[g].colormask);
130
131 /* Regardless if that works, we also need to initialize
132 * the blend shaders */
133
134 rt->shaders = _mesa_hash_table_u64_create(so);
135 }
136
137 return so;
138 }
139
140 static void
141 panfrost_bind_blend_state(struct pipe_context *pipe,
142 void *cso)
143 {
144 struct panfrost_context *ctx = pan_context(pipe);
145 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
146 struct pipe_blend_state *blend = (struct pipe_blend_state *) cso;
147 struct panfrost_blend_state *pblend = (struct panfrost_blend_state *) cso;
148 ctx->blend = pblend;
149
150 if (!blend)
151 return;
152
153 if (screen->quirks & MIDGARD_SFBD) {
154 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_NO_DITHER, !blend->dither);
155 }
156
157 /* Shader itself is not dirty, but the shader core is */
158 ctx->dirty |= PAN_DIRTY_FS;
159 }
160
161 static void
162 panfrost_delete_blend_shader(struct hash_entry *entry)
163 {
164 struct panfrost_blend_shader *shader = (struct panfrost_blend_shader *)entry->data;
165 free(shader->buffer);
166 free(shader);
167 }
168
169 static void
170 panfrost_delete_blend_state(struct pipe_context *pipe,
171 void *cso)
172 {
173 struct panfrost_blend_state *blend = (struct panfrost_blend_state *) cso;
174
175 for (unsigned c = 0; c < 4; ++c) {
176 struct panfrost_blend_rt *rt = &blend->rt[c];
177 _mesa_hash_table_u64_clear(rt->shaders, panfrost_delete_blend_shader);
178 }
179 ralloc_free(blend);
180 }
181
182 static void
183 panfrost_set_blend_color(struct pipe_context *pipe,
184 const struct pipe_blend_color *blend_color)
185 {
186 struct panfrost_context *ctx = pan_context(pipe);
187
188 if (blend_color)
189 ctx->blend_color = *blend_color;
190 }
191
192 /* Given a vec4 of constants, reduce it to just a single constant according to
193 * the mask (if we can) */
194
195 static bool
196 panfrost_blend_constant(float *out, float *in, unsigned mask)
197 {
198 /* If there is no components used, it automatically works. Do set a
199 * dummy constant just to avoid reading uninitialized memory. */
200
201 if (!mask) {
202 *out = 0.0;
203 return true;
204 }
205
206 /* Find some starter mask */
207 unsigned first = ffs(mask) - 1;
208 float cons = in[first];
209 mask ^= (1 << first);
210
211 /* Ensure the rest are equal */
212 while (mask) {
213 unsigned i = u_bit_scan(&mask);
214
215 if (in[i] != cons) {
216 *out = 0.0;
217 return false;
218 }
219 }
220
221 /* Otherwise, we're good to go */
222 *out = cons;
223 return true;
224 }
225
226 /* Create a final blend given the context */
227
228 struct panfrost_blend_final
229 panfrost_get_blend_for_context(struct panfrost_context *ctx, unsigned rti)
230 {
231 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
232
233 /* Grab the format, falling back gracefully if called invalidly (which
234 * has to happen for no-color-attachment FBOs, for instance) */
235 struct pipe_framebuffer_state *fb = &ctx->pipe_framebuffer;
236 enum pipe_format fmt = PIPE_FORMAT_R8G8B8A8_UNORM;
237
238 if ((fb->nr_cbufs > rti) && fb->cbufs[rti])
239 fmt = fb->cbufs[rti]->format;
240
241 /* Grab the blend state */
242 struct panfrost_blend_state *blend = ctx->blend;
243 assert(blend);
244
245 struct panfrost_blend_rt *rt = &blend->rt[rti];
246
247 struct panfrost_blend_final final;
248
249 /* First, we'll try a fixed function path */
250 if (rt->has_fixed_function && panfrost_can_fixed_blend(fmt)) {
251 if (panfrost_blend_constant(
252 &final.equation.constant,
253 ctx->blend_color.color,
254 rt->constant_mask)) {
255 /* There's an equation and suitable constant, so we're good to go */
256 final.is_shader = false;
257 final.equation.equation = &rt->equation;
258
259 final.no_blending =
260 (rt->equation.rgb_mode == 0x122) &&
261 (rt->equation.alpha_mode == 0x122) &&
262 (rt->equation.color_mask == 0xf);
263
264 return final;
265 }
266 }
267
268 /* Otherwise, we need to grab a shader */
269 struct panfrost_blend_shader *shader = panfrost_get_blend_shader(ctx, blend, fmt, rti);
270 final.is_shader = true;
271 final.no_blending = false;
272 final.shader.work_count = shader->work_count;
273 final.shader.first_tag = shader->first_tag;
274
275 /* Upload the shader */
276 final.shader.bo = panfrost_batch_create_bo(batch, shader->size,
277 PAN_BO_EXECUTE,
278 PAN_BO_ACCESS_PRIVATE |
279 PAN_BO_ACCESS_READ |
280 PAN_BO_ACCESS_VERTEX_TILER |
281 PAN_BO_ACCESS_FRAGMENT);
282 memcpy(final.shader.bo->cpu, shader->buffer, shader->size);
283
284 if (shader->patch_index) {
285 /* We have to specialize the blend shader to use constants, so
286 * patch in the current constants */
287
288 float *patch = (float *) (final.shader.bo->cpu + shader->patch_index);
289 memcpy(patch, ctx->blend_color.color, sizeof(float) * 4);
290 }
291
292 return final;
293 }
294
295 void
296 panfrost_blend_context_init(struct pipe_context *pipe)
297 {
298 pipe->create_blend_state = panfrost_create_blend_state;
299 pipe->bind_blend_state = panfrost_bind_blend_state;
300 pipe->delete_blend_state = panfrost_delete_blend_state;
301
302 pipe->set_blend_color = panfrost_set_blend_color;
303 }