freedreno/a5xx: fix clear for uint/sint formats
[mesa.git] / src / gallium / drivers / freedreno / a5xx / fd5_draw.c
1 /*
2 * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
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:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "pipe/p_state.h"
28 #include "util/u_string.h"
29 #include "util/u_memory.h"
30 #include "util/u_prim.h"
31
32 #include "freedreno_state.h"
33 #include "freedreno_resource.h"
34
35 #include "fd5_draw.h"
36 #include "fd5_context.h"
37 #include "fd5_emit.h"
38 #include "fd5_program.h"
39 #include "fd5_format.h"
40 #include "fd5_zsa.h"
41
42
43 static void
44 draw_impl(struct fd_context *ctx, struct fd_ringbuffer *ring,
45 struct fd5_emit *emit)
46 {
47 const struct pipe_draw_info *info = emit->info;
48 enum pc_di_primtype primtype = ctx->primtypes[info->mode];
49
50 fd5_emit_state(ctx, ring, emit);
51
52 if (emit->dirty & (FD_DIRTY_VTXBUF | FD_DIRTY_VTXSTATE))
53 fd5_emit_vertex_bufs(ring, emit);
54
55 OUT_PKT4(ring, REG_A5XX_VFD_INDEX_OFFSET, 2);
56 OUT_RING(ring, info->indexed ? info->index_bias : info->start); /* VFD_INDEX_OFFSET */
57 OUT_RING(ring, info->start_instance); /* ??? UNKNOWN_2209 */
58
59 OUT_PKT4(ring, REG_A5XX_PC_RESTART_INDEX, 1);
60 OUT_RING(ring, info->primitive_restart ? /* PC_RESTART_INDEX */
61 info->restart_index : 0xffffffff);
62
63 /* points + psize -> spritelist: */
64 if (ctx->rasterizer->point_size_per_vertex &&
65 fd5_emit_get_vp(emit)->writes_psize &&
66 (info->mode == PIPE_PRIM_POINTS))
67 primtype = DI_PT_POINTLIST_PSIZE;
68
69 fd5_emit_render_cntl(ctx, false);
70 fd5_draw_emit(ctx->batch, ring, primtype,
71 emit->key.binning_pass ? IGNORE_VISIBILITY : USE_VISIBILITY,
72 info);
73 }
74
75 /* fixup dirty shader state in case some "unrelated" (from the state-
76 * tracker's perspective) state change causes us to switch to a
77 * different variant.
78 */
79 static void
80 fixup_shader_state(struct fd_context *ctx, struct ir3_shader_key *key)
81 {
82 struct fd5_context *fd5_ctx = fd5_context(ctx);
83 struct ir3_shader_key *last_key = &fd5_ctx->last_key;
84
85 if (!ir3_shader_key_equal(last_key, key)) {
86 if (last_key->has_per_samp || key->has_per_samp) {
87 if ((last_key->vsaturate_s != key->vsaturate_s) ||
88 (last_key->vsaturate_t != key->vsaturate_t) ||
89 (last_key->vsaturate_r != key->vsaturate_r) ||
90 (last_key->vastc_srgb != key->vastc_srgb))
91 ctx->dirty |= FD_SHADER_DIRTY_VP;
92
93 if ((last_key->fsaturate_s != key->fsaturate_s) ||
94 (last_key->fsaturate_t != key->fsaturate_t) ||
95 (last_key->fsaturate_r != key->fsaturate_r) ||
96 (last_key->fastc_srgb != key->fastc_srgb))
97 ctx->dirty |= FD_SHADER_DIRTY_FP;
98 }
99
100 if (last_key->vclamp_color != key->vclamp_color)
101 ctx->dirty |= FD_SHADER_DIRTY_VP;
102
103 if (last_key->fclamp_color != key->fclamp_color)
104 ctx->dirty |= FD_SHADER_DIRTY_FP;
105
106 if (last_key->color_two_side != key->color_two_side)
107 ctx->dirty |= FD_SHADER_DIRTY_FP;
108
109 if (last_key->half_precision != key->half_precision)
110 ctx->dirty |= FD_SHADER_DIRTY_FP;
111
112 if (last_key->rasterflat != key->rasterflat)
113 ctx->dirty |= FD_SHADER_DIRTY_FP;
114
115 if (last_key->ucp_enables != key->ucp_enables)
116 ctx->dirty |= FD_SHADER_DIRTY_FP | FD_SHADER_DIRTY_VP;
117
118 fd5_ctx->last_key = *key;
119 }
120 }
121
122 static bool
123 fd5_draw_vbo(struct fd_context *ctx, const struct pipe_draw_info *info)
124 {
125 struct fd5_context *fd5_ctx = fd5_context(ctx);
126 struct fd5_emit emit = {
127 .debug = &ctx->debug,
128 .vtx = &ctx->vtx,
129 .prog = &ctx->prog,
130 .info = info,
131 .key = {
132 .color_two_side = ctx->rasterizer->light_twoside,
133 .vclamp_color = ctx->rasterizer->clamp_vertex_color,
134 .fclamp_color = ctx->rasterizer->clamp_fragment_color,
135 .rasterflat = ctx->rasterizer->flatshade,
136 .half_precision = ctx->in_blit &&
137 fd_half_precision(&ctx->batch->framebuffer),
138 .ucp_enables = ctx->rasterizer->clip_plane_enable,
139 .has_per_samp = (fd5_ctx->fsaturate || fd5_ctx->vsaturate ||
140 fd5_ctx->fastc_srgb || fd5_ctx->vastc_srgb),
141 .vsaturate_s = fd5_ctx->vsaturate_s,
142 .vsaturate_t = fd5_ctx->vsaturate_t,
143 .vsaturate_r = fd5_ctx->vsaturate_r,
144 .fsaturate_s = fd5_ctx->fsaturate_s,
145 .fsaturate_t = fd5_ctx->fsaturate_t,
146 .fsaturate_r = fd5_ctx->fsaturate_r,
147 .vastc_srgb = fd5_ctx->vastc_srgb,
148 .fastc_srgb = fd5_ctx->fastc_srgb,
149 },
150 .rasterflat = ctx->rasterizer->flatshade,
151 .sprite_coord_enable = ctx->rasterizer->sprite_coord_enable,
152 .sprite_coord_mode = ctx->rasterizer->sprite_coord_mode,
153 };
154
155 fixup_shader_state(ctx, &emit.key);
156
157 unsigned dirty = ctx->dirty;
158
159 /* do regular pass first, since that is more likely to fail compiling: */
160
161 if (!(fd5_emit_get_vp(&emit) && fd5_emit_get_fp(&emit)))
162 return false;
163
164 emit.key.binning_pass = false;
165 emit.dirty = dirty;
166
167 draw_impl(ctx, ctx->batch->draw, &emit);
168
169 // /* and now binning pass: */
170 // emit.key.binning_pass = true;
171 // emit.dirty = dirty & ~(FD_DIRTY_BLEND);
172 // emit.vp = NULL; /* we changed key so need to refetch vp */
173 // emit.fp = NULL;
174 // draw_impl(ctx, ctx->batch->binning, &emit);
175
176 if (emit.streamout_mask) {
177 struct fd_ringbuffer *ring = ctx->batch->draw;
178
179 for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
180 if (emit.streamout_mask & (1 << i)) {
181 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
182 OUT_RING(ring, FLUSH_SO_0 + i);
183 }
184 }
185 }
186
187 return true;
188 }
189
190 static void
191 fd5_clear(struct fd_context *ctx, unsigned buffers,
192 const union pipe_color_union *color, double depth, unsigned stencil)
193 {
194 struct fd_ringbuffer *ring = ctx->batch->draw;
195 struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
196 struct pipe_scissor_state *scissor = fd_context_get_scissor(ctx);
197
198 /* TODO handle scissor.. or fallback to slow-clear? */
199
200 ctx->batch->max_scissor.minx = MIN2(ctx->batch->max_scissor.minx, scissor->minx);
201 ctx->batch->max_scissor.miny = MIN2(ctx->batch->max_scissor.miny, scissor->miny);
202 ctx->batch->max_scissor.maxx = MAX2(ctx->batch->max_scissor.maxx, scissor->maxx);
203 ctx->batch->max_scissor.maxy = MAX2(ctx->batch->max_scissor.maxy, scissor->maxy);
204
205 fd5_emit_render_cntl(ctx, true);
206
207 if (buffers & PIPE_CLEAR_COLOR) {
208 for (int i = 0; i < pfb->nr_cbufs; i++) {
209 union util_color uc = {0};
210
211 if (!pfb->cbufs[i])
212 continue;
213
214 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
215 continue;
216
217 enum pipe_format pfmt = pfb->cbufs[i]->format;
218
219 // XXX I think RB_CLEAR_COLOR_DWn wants to take into account SWAP??
220 union pipe_color_union swapped;
221 switch (fd5_pipe2swap(pfmt)) {
222 case WZYX:
223 swapped.ui[0] = color->ui[0];
224 swapped.ui[1] = color->ui[1];
225 swapped.ui[2] = color->ui[2];
226 swapped.ui[3] = color->ui[3];
227 break;
228 case WXYZ:
229 swapped.ui[2] = color->ui[0];
230 swapped.ui[1] = color->ui[1];
231 swapped.ui[0] = color->ui[2];
232 swapped.ui[3] = color->ui[3];
233 break;
234 case ZYXW:
235 swapped.ui[3] = color->ui[0];
236 swapped.ui[0] = color->ui[1];
237 swapped.ui[1] = color->ui[2];
238 swapped.ui[2] = color->ui[3];
239 break;
240 case XYZW:
241 swapped.ui[3] = color->ui[0];
242 swapped.ui[2] = color->ui[1];
243 swapped.ui[1] = color->ui[2];
244 swapped.ui[0] = color->ui[3];
245 break;
246 }
247
248 if (util_format_is_pure_uint(pfmt)) {
249 util_format_write_4ui(pfmt, swapped.ui, 0, &uc, 0, 0, 0, 1, 1);
250 } else if (util_format_is_pure_sint(pfmt)) {
251 util_format_write_4i(pfmt, swapped.i, 0, &uc, 0, 0, 0, 1, 1);
252 } else {
253 util_pack_color(swapped.f, pfmt, &uc);
254 }
255
256 OUT_PKT4(ring, REG_A5XX_RB_BLIT_CNTL, 1);
257 OUT_RING(ring, A5XX_RB_BLIT_CNTL_BUF(BLIT_MRT0 + i));
258
259 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_CNTL, 1);
260 OUT_RING(ring, A5XX_RB_CLEAR_CNTL_FAST_CLEAR |
261 A5XX_RB_CLEAR_CNTL_MASK(0xf));
262
263 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_COLOR_DW0, 4);
264 OUT_RING(ring, uc.ui[0]); /* RB_CLEAR_COLOR_DW0 */
265 OUT_RING(ring, uc.ui[1]); /* RB_CLEAR_COLOR_DW1 */
266 OUT_RING(ring, uc.ui[2]); /* RB_CLEAR_COLOR_DW2 */
267 OUT_RING(ring, uc.ui[3]); /* RB_CLEAR_COLOR_DW3 */
268
269 fd5_emit_blit(ctx, ring);
270 }
271 }
272
273 if (pfb->zsbuf && (buffers & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
274 uint32_t clear =
275 util_pack_z_stencil(pfb->zsbuf->format, depth, stencil);
276 uint32_t mask = 0;
277
278 if (buffers & PIPE_CLEAR_DEPTH)
279 mask |= 0x1;
280
281 if (buffers & PIPE_CLEAR_STENCIL)
282 mask |= 0x2;
283
284 OUT_PKT4(ring, REG_A5XX_RB_BLIT_CNTL, 1);
285 OUT_RING(ring, A5XX_RB_BLIT_CNTL_BUF(BLIT_ZS));
286
287 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_CNTL, 1);
288 OUT_RING(ring, A5XX_RB_CLEAR_CNTL_FAST_CLEAR |
289 A5XX_RB_CLEAR_CNTL_MASK(mask));
290
291 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_COLOR_DW0, 1);
292 OUT_RING(ring, clear); /* RB_CLEAR_COLOR_DW0 */
293
294 fd5_emit_blit(ctx, ring);
295 }
296
297 /* disable fast clear to not interfere w/ gmem->mem, etc.. */
298 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_CNTL, 1);
299 OUT_RING(ring, 0x00000000); /* RB_CLEAR_CNTL */
300 }
301
302 void
303 fd5_draw_init(struct pipe_context *pctx)
304 {
305 struct fd_context *ctx = fd_context(pctx);
306 ctx->draw_vbo = fd5_draw_vbo;
307 ctx->clear = fd5_clear;
308 }