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_memory.h"
29 #include "util/u_prim.h"
30 #include "util/u_string.h"
31
32 #include "freedreno_resource.h"
33 #include "freedreno_state.h"
34
35 #include "fd5_context.h"
36 #include "fd5_draw.h"
37 #include "fd5_emit.h"
38 #include "fd5_format.h"
39 #include "fd5_program.h"
40 #include "fd5_zsa.h"
41
42 static void
draw_impl(struct fd_context * ctx,struct fd_ringbuffer * ring,struct fd5_emit * emit,unsigned index_offset)43 draw_impl(struct fd_context *ctx, struct fd_ringbuffer *ring,
44 struct fd5_emit *emit, unsigned index_offset) assert_dt
45 {
46 const struct pipe_draw_info *info = emit->info;
47 enum pc_di_primtype primtype = ctx->screen->primtypes[info->mode];
48
49 fd5_emit_state(ctx, ring, emit);
50
51 if (emit->dirty & (FD_DIRTY_VTXBUF | FD_DIRTY_VTXSTATE))
52 fd5_emit_vertex_bufs(ring, emit);
53
54 OUT_PKT4(ring, REG_A5XX_VFD_INDEX_OFFSET, 2);
55 OUT_RING(ring, info->index_size ? emit->draw->index_bias
56 : emit->draw->start); /* VFD_INDEX_OFFSET */
57 OUT_RING(ring, info->start_instance); /* VFD_INSTANCE_START_OFFSET */
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
62 : 0xffffffff);
63
64 fd5_emit_render_cntl(ctx, false, emit->binning_pass);
65 fd5_draw_emit(ctx->batch, ring, primtype,
66 emit->binning_pass ? IGNORE_VISIBILITY : USE_VISIBILITY, info,
67 emit->indirect, emit->draw, index_offset);
68 }
69
70 static bool
fd5_draw_vbo(struct fd_context * ctx,const struct pipe_draw_info * info,unsigned drawid_offset,const struct pipe_draw_indirect_info * indirect,const struct pipe_draw_start_count_bias * draw,unsigned index_offset)71 fd5_draw_vbo(struct fd_context *ctx, const struct pipe_draw_info *info,
72 unsigned drawid_offset,
73 const struct pipe_draw_indirect_info *indirect,
74 const struct pipe_draw_start_count_bias *draw,
75 unsigned index_offset) in_dt
76 {
77 struct fd5_context *fd5_ctx = fd5_context(ctx);
78 struct fd5_emit emit = {
79 .debug = &ctx->debug,
80 .vtx = &ctx->vtx,
81 .info = info,
82 .drawid_offset = drawid_offset,
83 .indirect = indirect,
84 .draw = draw,
85 .key = {
86 .vs = ctx->prog.vs,
87 .fs = ctx->prog.fs,
88 .key = {
89 .rasterflat = ctx->rasterizer->flatshade,
90 .has_per_samp = fd5_ctx->fastc_srgb || fd5_ctx->vastc_srgb,
91 .vastc_srgb = fd5_ctx->vastc_srgb,
92 .fastc_srgb = fd5_ctx->fastc_srgb,
93 },
94 },
95 .rasterflat = ctx->rasterizer->flatshade,
96 .sprite_coord_enable = ctx->rasterizer->sprite_coord_enable,
97 .sprite_coord_mode = ctx->rasterizer->sprite_coord_mode,
98 };
99
100 ir3_fixup_shader_state(&ctx->base, &emit.key.key);
101
102 unsigned dirty = ctx->dirty;
103
104 emit.prog = fd5_program_state(
105 ir3_cache_lookup(ctx->shader_cache, &emit.key, &ctx->debug));
106
107 /* bail if compile failed: */
108 if (!emit.prog)
109 return false;
110
111 const struct ir3_shader_variant *vp = fd5_emit_get_vp(&emit);
112 const struct ir3_shader_variant *fp = fd5_emit_get_fp(&emit);
113
114 ir3_update_max_tf_vtx(ctx, vp);
115
116 /* do regular pass first: */
117
118 if (unlikely(ctx->stats_users > 0)) {
119 ctx->stats.vs_regs += ir3_shader_halfregs(vp);
120 ctx->stats.fs_regs += ir3_shader_halfregs(fp);
121 }
122
123 /* figure out whether we need to disable LRZ write for binning
124 * pass using draw pass's fp:
125 */
126 emit.no_lrz_write = fp->writes_pos || fp->no_earlyz || fp->has_kill;
127
128 emit.binning_pass = false;
129 emit.dirty = dirty;
130
131 draw_impl(ctx, ctx->batch->draw, &emit, index_offset);
132
133 /* and now binning pass: */
134 emit.binning_pass = true;
135 emit.dirty = dirty & ~(FD_DIRTY_BLEND);
136 emit.vs = NULL; /* we changed key so need to refetch vp */
137 emit.fs = NULL;
138 draw_impl(ctx, ctx->batch->binning, &emit, index_offset);
139
140 if (emit.streamout_mask) {
141 struct fd_ringbuffer *ring = ctx->batch->draw;
142
143 for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
144 if (emit.streamout_mask & (1 << i)) {
145 fd5_event_write(ctx->batch, ring, FLUSH_SO_0 + i, false);
146 }
147 }
148 }
149
150 fd_context_all_clean(ctx);
151
152 return true;
153 }
154
155 static bool
is_z32(enum pipe_format format)156 is_z32(enum pipe_format format)
157 {
158 switch (format) {
159 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
160 case PIPE_FORMAT_Z32_UNORM:
161 case PIPE_FORMAT_Z32_FLOAT:
162 return true;
163 default:
164 return false;
165 }
166 }
167
168 static void
fd5_clear_lrz(struct fd_batch * batch,struct fd_resource * zsbuf,double depth)169 fd5_clear_lrz(struct fd_batch *batch, struct fd_resource *zsbuf, double depth)
170 {
171 struct fd_ringbuffer *ring;
172 uint32_t clear = util_pack_z(PIPE_FORMAT_Z16_UNORM, depth);
173
174 ring = fd_batch_get_prologue(batch);
175
176 OUT_WFI5(ring);
177
178 OUT_PKT4(ring, REG_A5XX_RB_CCU_CNTL, 1);
179 OUT_RING(ring, 0x10000000);
180
181 OUT_PKT4(ring, REG_A5XX_HLSQ_UPDATE_CNTL, 1);
182 OUT_RING(ring, 0x20fffff);
183
184 OUT_PKT4(ring, REG_A5XX_GRAS_SU_CNTL, 1);
185 OUT_RING(ring,
186 A5XX_GRAS_SU_CNTL_LINEHALFWIDTH(0.0) |
187 A5XX_GRAS_SU_CNTL_LINE_MODE(zsbuf->b.b.nr_samples > 1 ?
188 RECTANGULAR : BRESENHAM));
189
190 OUT_PKT4(ring, REG_A5XX_GRAS_CNTL, 1);
191 OUT_RING(ring, 0x00000000);
192
193 OUT_PKT4(ring, REG_A5XX_GRAS_CL_CNTL, 1);
194 OUT_RING(ring, 0x00000181);
195
196 OUT_PKT4(ring, REG_A5XX_GRAS_LRZ_CNTL, 1);
197 OUT_RING(ring, 0x00000000);
198
199 OUT_PKT4(ring, REG_A5XX_RB_MRT_BUF_INFO(0), 5);
200 OUT_RING(ring, A5XX_RB_MRT_BUF_INFO_COLOR_FORMAT(RB5_R16_UNORM) |
201 A5XX_RB_MRT_BUF_INFO_COLOR_TILE_MODE(TILE5_LINEAR) |
202 A5XX_RB_MRT_BUF_INFO_COLOR_SWAP(WZYX));
203 OUT_RING(ring, A5XX_RB_MRT_PITCH(zsbuf->lrz_pitch * 2));
204 OUT_RING(ring, A5XX_RB_MRT_ARRAY_PITCH(fd_bo_size(zsbuf->lrz)));
205 OUT_RELOC(ring, zsbuf->lrz, 0x1000, 0, 0);
206
207 OUT_PKT4(ring, REG_A5XX_RB_RENDER_CNTL, 1);
208 OUT_RING(ring, 0x00000000);
209
210 OUT_PKT4(ring, REG_A5XX_RB_DEST_MSAA_CNTL, 1);
211 OUT_RING(ring, A5XX_RB_DEST_MSAA_CNTL_SAMPLES(MSAA_ONE));
212
213 OUT_PKT4(ring, REG_A5XX_RB_BLIT_CNTL, 1);
214 OUT_RING(ring, A5XX_RB_BLIT_CNTL_BUF(BLIT_MRT0));
215
216 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_CNTL, 1);
217 OUT_RING(ring, A5XX_RB_CLEAR_CNTL_FAST_CLEAR | A5XX_RB_CLEAR_CNTL_MASK(0xf));
218
219 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_COLOR_DW0, 1);
220 OUT_RING(ring, clear); /* RB_CLEAR_COLOR_DW0 */
221
222 OUT_PKT4(ring, REG_A5XX_VSC_RESOLVE_CNTL, 2);
223 OUT_RING(ring, A5XX_VSC_RESOLVE_CNTL_X(zsbuf->lrz_width) |
224 A5XX_VSC_RESOLVE_CNTL_Y(zsbuf->lrz_height));
225 OUT_RING(ring, 0x00000000); // XXX UNKNOWN_0CDE
226
227 OUT_PKT4(ring, REG_A5XX_RB_CNTL, 1);
228 OUT_RING(ring, A5XX_RB_CNTL_BYPASS);
229
230 OUT_PKT4(ring, REG_A5XX_RB_RESOLVE_CNTL_1, 2);
231 OUT_RING(ring, A5XX_RB_RESOLVE_CNTL_1_X(0) | A5XX_RB_RESOLVE_CNTL_1_Y(0));
232 OUT_RING(ring, A5XX_RB_RESOLVE_CNTL_2_X(zsbuf->lrz_width - 1) |
233 A5XX_RB_RESOLVE_CNTL_2_Y(zsbuf->lrz_height - 1));
234
235 fd5_emit_blit(batch, ring);
236 }
237
238 static bool
fd5_clear(struct fd_context * ctx,unsigned buffers,const union pipe_color_union * color,double depth,unsigned stencil)239 fd5_clear(struct fd_context *ctx, unsigned buffers,
240 const union pipe_color_union *color, double depth,
241 unsigned stencil) assert_dt
242 {
243 struct fd_ringbuffer *ring = ctx->batch->draw;
244 struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
245
246 if ((buffers & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) &&
247 is_z32(pfb->zsbuf->format))
248 return false;
249
250 fd5_emit_render_cntl(ctx, true, false);
251
252 if (buffers & PIPE_CLEAR_COLOR) {
253 for (int i = 0; i < pfb->nr_cbufs; i++) {
254 union util_color uc = {0};
255
256 if (!pfb->cbufs[i])
257 continue;
258
259 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
260 continue;
261
262 enum pipe_format pfmt = pfb->cbufs[i]->format;
263
264 // XXX I think RB_CLEAR_COLOR_DWn wants to take into account SWAP??
265 union pipe_color_union swapped;
266 switch (fd5_pipe2swap(pfmt)) {
267 case WZYX:
268 swapped.ui[0] = color->ui[0];
269 swapped.ui[1] = color->ui[1];
270 swapped.ui[2] = color->ui[2];
271 swapped.ui[3] = color->ui[3];
272 break;
273 case WXYZ:
274 swapped.ui[2] = color->ui[0];
275 swapped.ui[1] = color->ui[1];
276 swapped.ui[0] = color->ui[2];
277 swapped.ui[3] = color->ui[3];
278 break;
279 case ZYXW:
280 swapped.ui[3] = color->ui[0];
281 swapped.ui[0] = color->ui[1];
282 swapped.ui[1] = color->ui[2];
283 swapped.ui[2] = color->ui[3];
284 break;
285 case XYZW:
286 swapped.ui[3] = color->ui[0];
287 swapped.ui[2] = color->ui[1];
288 swapped.ui[1] = color->ui[2];
289 swapped.ui[0] = color->ui[3];
290 break;
291 }
292
293 util_pack_color_union(pfmt, &uc, &swapped);
294
295 OUT_PKT4(ring, REG_A5XX_RB_BLIT_CNTL, 1);
296 OUT_RING(ring, A5XX_RB_BLIT_CNTL_BUF(BLIT_MRT0 + i));
297
298 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_CNTL, 1);
299 OUT_RING(ring,
300 A5XX_RB_CLEAR_CNTL_FAST_CLEAR | A5XX_RB_CLEAR_CNTL_MASK(0xf));
301
302 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_COLOR_DW0, 4);
303 OUT_RING(ring, uc.ui[0]); /* RB_CLEAR_COLOR_DW0 */
304 OUT_RING(ring, uc.ui[1]); /* RB_CLEAR_COLOR_DW1 */
305 OUT_RING(ring, uc.ui[2]); /* RB_CLEAR_COLOR_DW2 */
306 OUT_RING(ring, uc.ui[3]); /* RB_CLEAR_COLOR_DW3 */
307
308 fd5_emit_blit(ctx->batch, ring);
309 }
310 }
311
312 if (pfb->zsbuf && (buffers & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
313 uint32_t clear = util_pack_z_stencil(pfb->zsbuf->format, depth, stencil);
314 uint32_t mask = 0;
315
316 if (buffers & PIPE_CLEAR_DEPTH)
317 mask |= 0x1;
318
319 if (buffers & PIPE_CLEAR_STENCIL)
320 mask |= 0x2;
321
322 OUT_PKT4(ring, REG_A5XX_RB_BLIT_CNTL, 1);
323 OUT_RING(ring, A5XX_RB_BLIT_CNTL_BUF(BLIT_ZS));
324
325 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_CNTL, 1);
326 OUT_RING(ring,
327 A5XX_RB_CLEAR_CNTL_FAST_CLEAR | A5XX_RB_CLEAR_CNTL_MASK(mask));
328
329 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_COLOR_DW0, 1);
330 OUT_RING(ring, clear); /* RB_CLEAR_COLOR_DW0 */
331
332 fd5_emit_blit(ctx->batch, ring);
333
334 if (pfb->zsbuf && (buffers & PIPE_CLEAR_DEPTH)) {
335 struct fd_resource *zsbuf = fd_resource(pfb->zsbuf->texture);
336 if (zsbuf->lrz) {
337 zsbuf->lrz_valid = true;
338 fd5_clear_lrz(ctx->batch, zsbuf, depth);
339 }
340 }
341 }
342
343 /* disable fast clear to not interfere w/ gmem->mem, etc.. */
344 OUT_PKT4(ring, REG_A5XX_RB_CLEAR_CNTL, 1);
345 OUT_RING(ring, 0x00000000); /* RB_CLEAR_CNTL */
346
347 return true;
348 }
349
350 void
fd5_draw_init(struct pipe_context * pctx)351 fd5_draw_init(struct pipe_context *pctx) disable_thread_safety_analysis
352 {
353 struct fd_context *ctx = fd_context(pctx);
354 ctx->draw_vbo = fd5_draw_vbo;
355 ctx->clear = fd5_clear;
356 }
357