2bd2ca23d54c73407f18a6bf7cc851aaad084c9c
[mesa.git] / src / gallium / drivers / freedreno / a4xx / fd4_draw.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_prim.h"
33
34 #include "freedreno_state.h"
35 #include "freedreno_resource.h"
36
37 #include "fd4_draw.h"
38 #include "fd4_context.h"
39 #include "fd4_emit.h"
40 #include "fd4_program.h"
41 #include "fd4_format.h"
42 #include "fd4_zsa.h"
43
44
45 static void
46 draw_impl(struct fd_context *ctx, struct fd_ringbuffer *ring,
47 struct fd4_emit *emit)
48 {
49 const struct pipe_draw_info *info = emit->info;
50
51 if (!(fd4_emit_get_vp(emit) && fd4_emit_get_fp(emit)))
52 return;
53
54 fd4_emit_state(ctx, ring, emit);
55
56 if (emit->dirty & (FD_DIRTY_VTXBUF | FD_DIRTY_VTXSTATE))
57 fd4_emit_vertex_bufs(ring, emit);
58
59 OUT_PKT0(ring, REG_A4XX_VFD_INDEX_OFFSET, 2);
60 OUT_RING(ring, info->indexed ? info->index_bias : info->start); /* VFD_INDEX_OFFSET */
61 OUT_RING(ring, info->start_instance); /* ??? UNKNOWN_2209 */
62
63 OUT_PKT0(ring, REG_A4XX_PC_RESTART_INDEX, 1);
64 OUT_RING(ring, info->primitive_restart ? /* PC_RESTART_INDEX */
65 info->restart_index : 0xffffffff);
66
67 fd4_draw_emit(ctx, ring,
68 emit->key.binning_pass ? IGNORE_VISIBILITY : USE_VISIBILITY,
69 info);
70 }
71
72 /* fixup dirty shader state in case some "unrelated" (from the state-
73 * tracker's perspective) state change causes us to switch to a
74 * different variant.
75 */
76 static void
77 fixup_shader_state(struct fd_context *ctx, struct ir3_shader_key *key)
78 {
79 struct fd4_context *fd4_ctx = fd4_context(ctx);
80 struct ir3_shader_key *last_key = &fd4_ctx->last_key;
81
82 if (!ir3_shader_key_equal(last_key, key)) {
83 ctx->dirty |= FD_DIRTY_PROG;
84
85 if (last_key->has_per_samp || key->has_per_samp) {
86 if ((last_key->vsaturate_s != key->vsaturate_s) ||
87 (last_key->vsaturate_t != key->vsaturate_t) ||
88 (last_key->vsaturate_r != key->vsaturate_r))
89 ctx->prog.dirty |= FD_SHADER_DIRTY_VP;
90
91 if ((last_key->fsaturate_s != key->fsaturate_s) ||
92 (last_key->fsaturate_t != key->fsaturate_t) ||
93 (last_key->fsaturate_r != key->fsaturate_r))
94 ctx->prog.dirty |= FD_SHADER_DIRTY_FP;
95 }
96
97 if (last_key->color_two_side != key->color_two_side)
98 ctx->prog.dirty |= FD_SHADER_DIRTY_FP;
99
100 if (last_key->half_precision != key->half_precision)
101 ctx->prog.dirty |= FD_SHADER_DIRTY_FP;
102
103 if (last_key->rasterflat != key->rasterflat)
104 ctx->prog.dirty |= FD_SHADER_DIRTY_FP;
105
106 fd4_ctx->last_key = *key;
107 }
108 }
109
110 static void
111 fd4_draw_vbo(struct fd_context *ctx, const struct pipe_draw_info *info)
112 {
113 struct fd4_context *fd4_ctx = fd4_context(ctx);
114 struct fd4_emit emit = {
115 .vtx = &ctx->vtx,
116 .prog = &ctx->prog,
117 .info = info,
118 .key = {
119 /* do binning pass first: */
120 .binning_pass = true,
121 .color_two_side = ctx->rasterizer ? ctx->rasterizer->light_twoside : false,
122 .rasterflat = ctx->rasterizer && ctx->rasterizer->flatshade,
123 // TODO set .half_precision based on render target format,
124 // ie. float16 and smaller use half, float32 use full..
125 .half_precision = !!(fd_mesa_debug & FD_DBG_FRAGHALF),
126 .has_per_samp = (fd4_ctx->fsaturate || fd4_ctx->vsaturate),
127 .vsaturate_s = fd4_ctx->vsaturate_s,
128 .vsaturate_t = fd4_ctx->vsaturate_t,
129 .vsaturate_r = fd4_ctx->vsaturate_r,
130 .fsaturate_s = fd4_ctx->fsaturate_s,
131 .fsaturate_t = fd4_ctx->fsaturate_t,
132 .fsaturate_r = fd4_ctx->fsaturate_r,
133 },
134 .rasterflat = ctx->rasterizer && ctx->rasterizer->flatshade,
135 .sprite_coord_enable = ctx->rasterizer ? ctx->rasterizer->sprite_coord_enable : false,
136 .sprite_coord_mode = ctx->rasterizer ? ctx->rasterizer->sprite_coord_mode : false,
137 };
138 unsigned dirty;
139
140 fixup_shader_state(ctx, &emit.key);
141
142 dirty = ctx->dirty;
143 emit.dirty = dirty & ~(FD_DIRTY_BLEND);
144 draw_impl(ctx, ctx->binning_ring, &emit);
145
146 /* and now regular (non-binning) pass: */
147 emit.key.binning_pass = false;
148 emit.dirty = dirty;
149 emit.vp = NULL; /* we changed key so need to refetch vp */
150 draw_impl(ctx, ctx->ring, &emit);
151 }
152
153 /* clear operations ignore viewport state, so we need to reset it
154 * based on framebuffer state:
155 */
156 static void
157 reset_viewport(struct fd_ringbuffer *ring, struct pipe_framebuffer_state *pfb)
158 {
159 float half_width = pfb->width * 0.5f;
160 float half_height = pfb->height * 0.5f;
161
162 OUT_PKT0(ring, REG_A4XX_GRAS_CL_VPORT_XOFFSET_0, 4);
163 OUT_RING(ring, A4XX_GRAS_CL_VPORT_XOFFSET_0(half_width));
164 OUT_RING(ring, A4XX_GRAS_CL_VPORT_XSCALE_0(half_width));
165 OUT_RING(ring, A4XX_GRAS_CL_VPORT_YOFFSET_0(half_height));
166 OUT_RING(ring, A4XX_GRAS_CL_VPORT_YSCALE_0(-half_height));
167 }
168
169 static void
170 fd4_clear(struct fd_context *ctx, unsigned buffers,
171 const union pipe_color_union *color, double depth, unsigned stencil)
172 {
173 struct fd4_context *fd4_ctx = fd4_context(ctx);
174 struct fd_ringbuffer *ring = ctx->ring;
175 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
176 unsigned char mrt_comp[A4XX_MAX_RENDER_TARGETS] = {0};
177 unsigned dirty = ctx->dirty;
178 unsigned i;
179 struct fd4_emit emit = {
180 .vtx = &fd4_ctx->solid_vbuf_state,
181 .prog = &ctx->solid_prog,
182 .key = {
183 .half_precision = fd_half_precision(pfb),
184 },
185 };
186
187 dirty &= FD_DIRTY_FRAMEBUFFER | FD_DIRTY_SCISSOR;
188 dirty |= FD_DIRTY_PROG;
189 emit.dirty = dirty;
190
191 OUT_PKT0(ring, REG_A4XX_PC_PRIM_VTX_CNTL, 1);
192 OUT_RING(ring, A4XX_PC_PRIM_VTX_CNTL_PROVOKING_VTX_LAST);
193
194 /* emit generic state now: */
195 fd4_emit_state(ctx, ring, &emit);
196 reset_viewport(ring, pfb);
197
198 if (buffers & PIPE_CLEAR_DEPTH) {
199 OUT_PKT0(ring, REG_A4XX_RB_DEPTH_CONTROL, 1);
200 OUT_RING(ring, A4XX_RB_DEPTH_CONTROL_Z_WRITE_ENABLE |
201 A4XX_RB_DEPTH_CONTROL_Z_ENABLE |
202 A4XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_ALWAYS));
203
204 fd_wfi(ctx, ring);
205 OUT_PKT0(ring, REG_A4XX_GRAS_CL_VPORT_ZOFFSET_0, 2);
206 OUT_RING(ring, A4XX_GRAS_CL_VPORT_ZOFFSET_0(0.0));
207 OUT_RING(ring, A4XX_GRAS_CL_VPORT_ZSCALE_0(depth));
208 ctx->dirty |= FD_DIRTY_VIEWPORT;
209 } else {
210 OUT_PKT0(ring, REG_A4XX_RB_DEPTH_CONTROL, 1);
211 OUT_RING(ring, A4XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_NEVER));
212 }
213
214 if (buffers & PIPE_CLEAR_STENCIL) {
215 OUT_PKT0(ring, REG_A4XX_RB_STENCILREFMASK, 2);
216 OUT_RING(ring, A4XX_RB_STENCILREFMASK_STENCILREF(stencil) |
217 A4XX_RB_STENCILREFMASK_STENCILMASK(stencil) |
218 A4XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
219 OUT_RING(ring, A4XX_RB_STENCILREFMASK_STENCILREF(0) |
220 A4XX_RB_STENCILREFMASK_STENCILMASK(0) |
221 0xff000000 | // XXX ???
222 A4XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
223
224 OUT_PKT0(ring, REG_A4XX_RB_STENCIL_CONTROL, 2);
225 OUT_RING(ring, A4XX_RB_STENCIL_CONTROL_STENCIL_ENABLE |
226 A4XX_RB_STENCIL_CONTROL_FUNC(FUNC_ALWAYS) |
227 A4XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
228 A4XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_REPLACE) |
229 A4XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP) |
230 A4XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER) |
231 A4XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
232 A4XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
233 A4XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
234 OUT_RING(ring, A4XX_RB_STENCIL_CONTROL2_STENCIL_BUFFER);
235 } else {
236 OUT_PKT0(ring, REG_A4XX_RB_STENCILREFMASK, 2);
237 OUT_RING(ring, A4XX_RB_STENCILREFMASK_STENCILREF(0) |
238 A4XX_RB_STENCILREFMASK_STENCILMASK(0) |
239 A4XX_RB_STENCILREFMASK_STENCILWRITEMASK(0));
240 OUT_RING(ring, A4XX_RB_STENCILREFMASK_BF_STENCILREF(0) |
241 A4XX_RB_STENCILREFMASK_BF_STENCILMASK(0) |
242 A4XX_RB_STENCILREFMASK_BF_STENCILWRITEMASK(0));
243
244 OUT_PKT0(ring, REG_A4XX_RB_STENCIL_CONTROL, 2);
245 OUT_RING(ring, A4XX_RB_STENCIL_CONTROL_FUNC(FUNC_NEVER) |
246 A4XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
247 A4XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_KEEP) |
248 A4XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP) |
249 A4XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER) |
250 A4XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
251 A4XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
252 A4XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
253 OUT_RING(ring, 0x00000000); /* RB_STENCIL_CONTROL2 */
254 }
255
256 if (buffers & PIPE_CLEAR_COLOR) {
257 OUT_PKT0(ring, REG_A4XX_RB_ALPHA_CONTROL, 1);
258 OUT_RING(ring, A4XX_RB_ALPHA_CONTROL_ALPHA_TEST_FUNC(FUNC_NEVER));
259 }
260
261 for (i = 0; i < A4XX_MAX_RENDER_TARGETS; i++) {
262 mrt_comp[i] = (buffers & (PIPE_CLEAR_COLOR0 << i)) ? 0xf : 0x0;
263
264 OUT_PKT0(ring, REG_A4XX_RB_MRT_CONTROL(i), 1);
265 OUT_RING(ring, A4XX_RB_MRT_CONTROL_FASTCLEAR |
266 A4XX_RB_MRT_CONTROL_B11 |
267 A4XX_RB_MRT_CONTROL_COMPONENT_ENABLE(0xf));
268
269 OUT_PKT0(ring, REG_A4XX_RB_MRT_BLEND_CONTROL(i), 1);
270 OUT_RING(ring, A4XX_RB_MRT_BLEND_CONTROL_RGB_SRC_FACTOR(FACTOR_ONE) |
271 A4XX_RB_MRT_BLEND_CONTROL_RGB_BLEND_OPCODE(BLEND_DST_PLUS_SRC) |
272 A4XX_RB_MRT_BLEND_CONTROL_RGB_DEST_FACTOR(FACTOR_ZERO) |
273 A4XX_RB_MRT_BLEND_CONTROL_ALPHA_SRC_FACTOR(FACTOR_ONE) |
274 A4XX_RB_MRT_BLEND_CONTROL_ALPHA_BLEND_OPCODE(BLEND_DST_PLUS_SRC) |
275 A4XX_RB_MRT_BLEND_CONTROL_ALPHA_DEST_FACTOR(FACTOR_ZERO));
276 }
277
278 OUT_PKT0(ring, REG_A4XX_RB_RENDER_COMPONENTS, 1);
279 OUT_RING(ring, A4XX_RB_RENDER_COMPONENTS_RT0(mrt_comp[0]) |
280 A4XX_RB_RENDER_COMPONENTS_RT1(mrt_comp[1]) |
281 A4XX_RB_RENDER_COMPONENTS_RT2(mrt_comp[2]) |
282 A4XX_RB_RENDER_COMPONENTS_RT3(mrt_comp[3]) |
283 A4XX_RB_RENDER_COMPONENTS_RT4(mrt_comp[4]) |
284 A4XX_RB_RENDER_COMPONENTS_RT5(mrt_comp[5]) |
285 A4XX_RB_RENDER_COMPONENTS_RT6(mrt_comp[6]) |
286 A4XX_RB_RENDER_COMPONENTS_RT7(mrt_comp[7]));
287
288 fd4_emit_vertex_bufs(ring, &emit);
289
290 OUT_PKT0(ring, REG_A4XX_GRAS_ALPHA_CONTROL, 1);
291 OUT_RING(ring, 0x0); /* XXX GRAS_ALPHA_CONTROL */
292
293 OUT_PKT0(ring, REG_A4XX_GRAS_CLEAR_CNTL, 1);
294 OUT_RING(ring, 0x00000000);
295
296 /* until fastclear works: */
297 fd4_emit_const(ring, SHADER_FRAGMENT, 0, 0, 4, color->ui, NULL);
298
299 OUT_PKT0(ring, REG_A4XX_VFD_INDEX_OFFSET, 2);
300 OUT_RING(ring, 0); /* VFD_INDEX_OFFSET */
301 OUT_RING(ring, 0); /* ??? UNKNOWN_2209 */
302
303 OUT_PKT0(ring, REG_A4XX_PC_RESTART_INDEX, 1);
304 OUT_RING(ring, 0xffffffff); /* PC_RESTART_INDEX */
305
306 OUT_PKT3(ring, CP_UNKNOWN_1A, 1);
307 OUT_RING(ring, 0x00000001);
308
309 fd4_draw(ctx, ring, DI_PT_RECTLIST, USE_VISIBILITY,
310 DI_SRC_SEL_AUTO_INDEX, 2, 1, INDEX_SIZE_IGN, 0, 0, NULL);
311
312 OUT_PKT3(ring, CP_UNKNOWN_1A, 1);
313 OUT_RING(ring, 0x00000000);
314
315 OUT_PKT0(ring, REG_A4XX_GRAS_CLEAR_CNTL, 1);
316 OUT_RING(ring, A4XX_GRAS_CLEAR_CNTL_NOT_FASTCLEAR);
317
318 OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
319 OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
320 A4XX_GRAS_SC_CONTROL_MSAA_DISABLE |
321 A4XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
322 A4XX_GRAS_SC_CONTROL_RASTER_MODE(0));
323 }
324
325 void
326 fd4_draw_init(struct pipe_context *pctx)
327 {
328 struct fd_context *ctx = fd_context(pctx);
329 ctx->draw_vbo = fd4_draw_vbo;
330 ctx->clear = fd4_clear;
331 }