freedreno/a3xx: enable sampling from integer textures
[mesa.git] / src / gallium / drivers / freedreno / a3xx / fd3_draw.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2013 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 #include "util/u_format.h"
34
35 #include "freedreno_state.h"
36 #include "freedreno_resource.h"
37
38 #include "fd3_draw.h"
39 #include "fd3_context.h"
40 #include "fd3_emit.h"
41 #include "fd3_program.h"
42 #include "fd3_format.h"
43 #include "fd3_zsa.h"
44
45
46 static void
47 draw_impl(struct fd_context *ctx, struct fd_ringbuffer *ring,
48 struct fd3_emit *emit)
49 {
50 const struct pipe_draw_info *info = emit->info;
51
52 fd3_emit_state(ctx, ring, emit);
53
54 if (emit->dirty & (FD_DIRTY_VTXBUF | FD_DIRTY_VTXSTATE))
55 fd3_emit_vertex_bufs(ring, emit);
56
57 OUT_PKT0(ring, REG_A3XX_PC_VERTEX_REUSE_BLOCK_CNTL, 1);
58 OUT_RING(ring, 0x0000000b); /* PC_VERTEX_REUSE_BLOCK_CNTL */
59
60 OUT_PKT0(ring, REG_A3XX_VFD_INDEX_MIN, 4);
61 OUT_RING(ring, info->min_index); /* VFD_INDEX_MIN */
62 OUT_RING(ring, info->max_index); /* VFD_INDEX_MAX */
63 OUT_RING(ring, info->start_instance); /* VFD_INSTANCEID_OFFSET */
64 OUT_RING(ring, info->start); /* VFD_INDEX_OFFSET */
65
66 OUT_PKT0(ring, REG_A3XX_PC_RESTART_INDEX, 1);
67 OUT_RING(ring, info->primitive_restart ? /* PC_RESTART_INDEX */
68 info->restart_index : 0xffffffff);
69
70 fd_draw_emit(ctx, ring,
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 fd3_context *fd3_ctx = fd3_context(ctx);
83 struct ir3_shader_key *last_key = &fd3_ctx->last_key;
84
85 if (!ir3_shader_key_equal(last_key, key)) {
86 ctx->dirty |= FD_DIRTY_PROG;
87
88 if (last_key->has_per_samp || key->has_per_samp) {
89 if ((last_key->vsaturate_s != key->vsaturate_s) ||
90 (last_key->vsaturate_t != key->vsaturate_t) ||
91 (last_key->vsaturate_r != key->vsaturate_r) ||
92 (last_key->vinteger_s != key->vinteger_s))
93 ctx->prog.dirty |= FD_SHADER_DIRTY_VP;
94
95 if ((last_key->fsaturate_s != key->fsaturate_s) ||
96 (last_key->fsaturate_t != key->fsaturate_t) ||
97 (last_key->fsaturate_r != key->fsaturate_r) ||
98 (last_key->finteger_s != key->finteger_s))
99 ctx->prog.dirty |= FD_SHADER_DIRTY_FP;
100 }
101
102 if (last_key->color_two_side != key->color_two_side)
103 ctx->prog.dirty |= FD_SHADER_DIRTY_FP;
104
105 if (last_key->half_precision != key->half_precision)
106 ctx->prog.dirty |= FD_SHADER_DIRTY_FP;
107
108 if (last_key->alpha != key->alpha)
109 ctx->prog.dirty |= FD_SHADER_DIRTY_FP;
110
111 fd3_ctx->last_key = *key;
112 }
113 }
114
115 static void
116 fd3_draw_vbo(struct fd_context *ctx, const struct pipe_draw_info *info)
117 {
118 struct fd3_context *fd3_ctx = fd3_context(ctx);
119 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
120 struct fd3_emit emit = {
121 .vtx = &ctx->vtx,
122 .prog = &ctx->prog,
123 .info = info,
124 .key = {
125 /* do binning pass first: */
126 .binning_pass = true,
127 .color_two_side = ctx->rasterizer ? ctx->rasterizer->light_twoside : false,
128 .alpha = util_format_is_alpha(pipe_surface_format(pfb->cbufs[0])),
129 // TODO set .half_precision based on render target format,
130 // ie. float16 and smaller use half, float32 use full..
131 .half_precision = !!(fd_mesa_debug & FD_DBG_FRAGHALF),
132 .has_per_samp = (fd3_ctx->fsaturate || fd3_ctx->vsaturate ||
133 fd3_ctx->vinteger_s || fd3_ctx->finteger_s),
134 .vsaturate_s = fd3_ctx->vsaturate_s,
135 .vsaturate_t = fd3_ctx->vsaturate_t,
136 .vsaturate_r = fd3_ctx->vsaturate_r,
137 .fsaturate_s = fd3_ctx->fsaturate_s,
138 .fsaturate_t = fd3_ctx->fsaturate_t,
139 .fsaturate_r = fd3_ctx->fsaturate_r,
140 .vinteger_s = fd3_ctx->vinteger_s,
141 .finteger_s = fd3_ctx->finteger_s,
142 },
143 .format = pipe_surface_format(pfb->cbufs[0]),
144 .rasterflat = ctx->rasterizer && ctx->rasterizer->flatshade,
145 };
146 unsigned dirty;
147
148 fixup_shader_state(ctx, &emit.key);
149
150 dirty = ctx->dirty;
151 emit.dirty = dirty & ~(FD_DIRTY_BLEND);
152 draw_impl(ctx, ctx->binning_ring, &emit);
153
154 /* and now regular (non-binning) pass: */
155 emit.key.binning_pass = false;
156 emit.dirty = dirty;
157 emit.vp = NULL; /* we changed key so need to refetch vp */
158 draw_impl(ctx, ctx->ring, &emit);
159 }
160
161 /* clear operations ignore viewport state, so we need to reset it
162 * based on framebuffer state:
163 */
164 static void
165 reset_viewport(struct fd_ringbuffer *ring, struct pipe_framebuffer_state *pfb)
166 {
167 float half_width = pfb->width * 0.5f;
168 float half_height = pfb->height * 0.5f;
169
170 OUT_PKT0(ring, REG_A3XX_GRAS_CL_VPORT_XOFFSET, 4);
171 OUT_RING(ring, A3XX_GRAS_CL_VPORT_XOFFSET(half_width - 0.5));
172 OUT_RING(ring, A3XX_GRAS_CL_VPORT_XSCALE(half_width));
173 OUT_RING(ring, A3XX_GRAS_CL_VPORT_YOFFSET(half_height - 0.5));
174 OUT_RING(ring, A3XX_GRAS_CL_VPORT_YSCALE(-half_height));
175 }
176
177 /* binning pass cmds for a clear:
178 * NOTE: newer blob drivers don't use binning for clear, which is probably
179 * preferable since it is low vtx count. However that doesn't seem to
180 * actually work for me. Not sure if it is depending on support for
181 * clear pass (rather than using solid-fill shader), or something else
182 * that newer blob is doing differently. Once that is figured out, we
183 * can remove fd3_clear_binning().
184 */
185 static void
186 fd3_clear_binning(struct fd_context *ctx, unsigned dirty)
187 {
188 struct fd3_context *fd3_ctx = fd3_context(ctx);
189 struct fd_ringbuffer *ring = ctx->binning_ring;
190 struct fd3_emit emit = {
191 .vtx = &fd3_ctx->solid_vbuf_state,
192 .prog = &ctx->solid_prog,
193 .key = {
194 .binning_pass = true,
195 .half_precision = true,
196 },
197 .dirty = dirty,
198 };
199
200 fd3_emit_state(ctx, ring, &emit);
201 fd3_emit_vertex_bufs(ring, &emit);
202 reset_viewport(ring, &ctx->framebuffer);
203
204 OUT_PKT0(ring, REG_A3XX_PC_PRIM_VTX_CNTL, 1);
205 OUT_RING(ring, A3XX_PC_PRIM_VTX_CNTL_STRIDE_IN_VPC(0) |
206 A3XX_PC_PRIM_VTX_CNTL_POLYMODE_FRONT_PTYPE(PC_DRAW_TRIANGLES) |
207 A3XX_PC_PRIM_VTX_CNTL_POLYMODE_BACK_PTYPE(PC_DRAW_TRIANGLES) |
208 A3XX_PC_PRIM_VTX_CNTL_PROVOKING_VTX_LAST);
209 OUT_PKT0(ring, REG_A3XX_VFD_INDEX_MIN, 4);
210 OUT_RING(ring, 0); /* VFD_INDEX_MIN */
211 OUT_RING(ring, 2); /* VFD_INDEX_MAX */
212 OUT_RING(ring, 0); /* VFD_INSTANCEID_OFFSET */
213 OUT_RING(ring, 0); /* VFD_INDEX_OFFSET */
214 OUT_PKT0(ring, REG_A3XX_PC_RESTART_INDEX, 1);
215 OUT_RING(ring, 0xffffffff); /* PC_RESTART_INDEX */
216
217 fd_event_write(ctx, ring, PERFCOUNTER_STOP);
218
219 fd_draw(ctx, ring, DI_PT_RECTLIST, IGNORE_VISIBILITY,
220 DI_SRC_SEL_AUTO_INDEX, 2, INDEX_SIZE_IGN, 0, 0, NULL);
221 }
222
223 static void
224 fd3_clear(struct fd_context *ctx, unsigned buffers,
225 const union pipe_color_union *color, double depth, unsigned stencil)
226 {
227 struct fd3_context *fd3_ctx = fd3_context(ctx);
228 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
229 enum pipe_format format = pipe_surface_format(pfb->cbufs[0]);
230 struct fd_ringbuffer *ring = ctx->ring;
231 unsigned dirty = ctx->dirty;
232 unsigned ce, i;
233 struct fd3_emit emit = {
234 .vtx = &fd3_ctx->solid_vbuf_state,
235 .prog = &ctx->solid_prog,
236 .key = {
237 .half_precision = fd3_half_precision(format),
238 },
239 .format = format,
240 };
241
242 dirty &= FD_DIRTY_FRAMEBUFFER | FD_DIRTY_SCISSOR;
243 dirty |= FD_DIRTY_PROG;
244 emit.dirty = dirty;
245
246 fd3_clear_binning(ctx, dirty);
247
248 /* emit generic state now: */
249 fd3_emit_state(ctx, ring, &emit);
250 reset_viewport(ring, &ctx->framebuffer);
251
252 OUT_PKT0(ring, REG_A3XX_RB_BLEND_ALPHA, 1);
253 OUT_RING(ring, A3XX_RB_BLEND_ALPHA_UINT(0xff) |
254 A3XX_RB_BLEND_ALPHA_FLOAT(1.0));
255
256 OUT_PKT0(ring, REG_A3XX_RB_RENDER_CONTROL, 1);
257 OUT_RINGP(ring, A3XX_RB_RENDER_CONTROL_ALPHA_TEST_FUNC(FUNC_NEVER),
258 &fd3_ctx->rbrc_patches);
259
260 if (buffers & PIPE_CLEAR_DEPTH) {
261 OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
262 OUT_RING(ring, A3XX_RB_DEPTH_CONTROL_Z_WRITE_ENABLE |
263 A3XX_RB_DEPTH_CONTROL_Z_ENABLE |
264 A3XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_ALWAYS));
265
266 fd_wfi(ctx, ring);
267 OUT_PKT0(ring, REG_A3XX_GRAS_CL_VPORT_ZOFFSET, 2);
268 OUT_RING(ring, A3XX_GRAS_CL_VPORT_ZOFFSET(0.0));
269 OUT_RING(ring, A3XX_GRAS_CL_VPORT_ZSCALE(depth));
270 ctx->dirty |= FD_DIRTY_VIEWPORT;
271 } else {
272 OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
273 OUT_RING(ring, A3XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_NEVER));
274 }
275
276 if (buffers & PIPE_CLEAR_STENCIL) {
277 OUT_PKT0(ring, REG_A3XX_RB_STENCILREFMASK, 2);
278 OUT_RING(ring, A3XX_RB_STENCILREFMASK_STENCILREF(stencil) |
279 A3XX_RB_STENCILREFMASK_STENCILMASK(stencil) |
280 A3XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
281 OUT_RING(ring, A3XX_RB_STENCILREFMASK_STENCILREF(0) |
282 A3XX_RB_STENCILREFMASK_STENCILMASK(0) |
283 0xff000000 | // XXX ???
284 A3XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
285
286 OUT_PKT0(ring, REG_A3XX_RB_STENCIL_CONTROL, 1);
287 OUT_RING(ring, A3XX_RB_STENCIL_CONTROL_STENCIL_ENABLE |
288 A3XX_RB_STENCIL_CONTROL_FUNC(FUNC_ALWAYS) |
289 A3XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
290 A3XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_REPLACE) |
291 A3XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP) |
292 A3XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER) |
293 A3XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
294 A3XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
295 A3XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
296 } else {
297 OUT_PKT0(ring, REG_A3XX_RB_STENCILREFMASK, 2);
298 OUT_RING(ring, A3XX_RB_STENCILREFMASK_STENCILREF(0) |
299 A3XX_RB_STENCILREFMASK_STENCILMASK(0) |
300 A3XX_RB_STENCILREFMASK_STENCILWRITEMASK(0));
301 OUT_RING(ring, A3XX_RB_STENCILREFMASK_BF_STENCILREF(0) |
302 A3XX_RB_STENCILREFMASK_BF_STENCILMASK(0) |
303 A3XX_RB_STENCILREFMASK_BF_STENCILWRITEMASK(0));
304
305 OUT_PKT0(ring, REG_A3XX_RB_STENCIL_CONTROL, 1);
306 OUT_RING(ring, A3XX_RB_STENCIL_CONTROL_FUNC(FUNC_NEVER) |
307 A3XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
308 A3XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_KEEP) |
309 A3XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP) |
310 A3XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER) |
311 A3XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
312 A3XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
313 A3XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
314 }
315
316 if (buffers & PIPE_CLEAR_COLOR) {
317 ce = 0xf;
318 } else {
319 ce = 0x0;
320 }
321
322 for (i = 0; i < 4; i++) {
323 OUT_PKT0(ring, REG_A3XX_RB_MRT_CONTROL(i), 1);
324 OUT_RING(ring, A3XX_RB_MRT_CONTROL_ROP_CODE(ROP_COPY) |
325 A3XX_RB_MRT_CONTROL_DITHER_MODE(DITHER_ALWAYS) |
326 A3XX_RB_MRT_CONTROL_COMPONENT_ENABLE(ce));
327
328 OUT_PKT0(ring, REG_A3XX_RB_MRT_BLEND_CONTROL(i), 1);
329 OUT_RING(ring, A3XX_RB_MRT_BLEND_CONTROL_RGB_SRC_FACTOR(FACTOR_ONE) |
330 A3XX_RB_MRT_BLEND_CONTROL_RGB_BLEND_OPCODE(BLEND_DST_PLUS_SRC) |
331 A3XX_RB_MRT_BLEND_CONTROL_RGB_DEST_FACTOR(FACTOR_ZERO) |
332 A3XX_RB_MRT_BLEND_CONTROL_ALPHA_SRC_FACTOR(FACTOR_ONE) |
333 A3XX_RB_MRT_BLEND_CONTROL_ALPHA_BLEND_OPCODE(BLEND_DST_PLUS_SRC) |
334 A3XX_RB_MRT_BLEND_CONTROL_ALPHA_DEST_FACTOR(FACTOR_ZERO));
335 }
336
337 OUT_PKT0(ring, REG_A3XX_GRAS_SU_MODE_CONTROL, 1);
338 OUT_RING(ring, A3XX_GRAS_SU_MODE_CONTROL_LINEHALFWIDTH(0));
339
340 fd3_emit_vertex_bufs(ring, &emit);
341
342 fd3_emit_constant(ring, SB_FRAG_SHADER, 0, 0, 4, color->ui, NULL);
343
344 OUT_PKT0(ring, REG_A3XX_PC_PRIM_VTX_CNTL, 1);
345 OUT_RING(ring, A3XX_PC_PRIM_VTX_CNTL_STRIDE_IN_VPC(0) |
346 A3XX_PC_PRIM_VTX_CNTL_POLYMODE_FRONT_PTYPE(PC_DRAW_TRIANGLES) |
347 A3XX_PC_PRIM_VTX_CNTL_POLYMODE_BACK_PTYPE(PC_DRAW_TRIANGLES) |
348 A3XX_PC_PRIM_VTX_CNTL_PROVOKING_VTX_LAST);
349 OUT_PKT0(ring, REG_A3XX_VFD_INDEX_MIN, 4);
350 OUT_RING(ring, 0); /* VFD_INDEX_MIN */
351 OUT_RING(ring, 2); /* VFD_INDEX_MAX */
352 OUT_RING(ring, 0); /* VFD_INSTANCEID_OFFSET */
353 OUT_RING(ring, 0); /* VFD_INDEX_OFFSET */
354 OUT_PKT0(ring, REG_A3XX_PC_RESTART_INDEX, 1);
355 OUT_RING(ring, 0xffffffff); /* PC_RESTART_INDEX */
356
357 fd_event_write(ctx, ring, PERFCOUNTER_STOP);
358
359 fd_draw(ctx, ring, DI_PT_RECTLIST, USE_VISIBILITY,
360 DI_SRC_SEL_AUTO_INDEX, 2, INDEX_SIZE_IGN, 0, 0, NULL);
361 }
362
363 void
364 fd3_draw_init(struct pipe_context *pctx)
365 {
366 struct fd_context *ctx = fd_context(pctx);
367 ctx->draw_vbo = fd3_draw_vbo;
368 ctx->clear = fd3_clear;
369 }