1/*
2 * Copyright (C) 2014 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 "fd4_draw.h"
36#include "fd4_context.h"
37#include "fd4_emit.h"
38#include "fd4_program.h"
39#include "fd4_format.h"
40#include "fd4_zsa.h"
41
42
43static void
44draw_impl(struct fd_context *ctx, struct fd_ringbuffer *ring,
45		struct fd4_emit *emit, unsigned index_offset)
46{
47	const struct pipe_draw_info *info = emit->info;
48	enum pc_di_primtype primtype = ctx->primtypes[info->mode];
49
50	fd4_emit_state(ctx, ring, emit);
51
52	if (emit->dirty & (FD_DIRTY_VTXBUF | FD_DIRTY_VTXSTATE))
53		fd4_emit_vertex_bufs(ring, emit);
54
55	OUT_PKT0(ring, REG_A4XX_VFD_INDEX_OFFSET, 2);
56	OUT_RING(ring, info->index_size ? info->index_bias : info->start); /* VFD_INDEX_OFFSET */
57	OUT_RING(ring, info->start_instance);   /* ??? UNKNOWN_2209 */
58
59	OUT_PKT0(ring, REG_A4XX_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			fd4_emit_get_vp(emit)->writes_psize &&
66			(info->mode == PIPE_PRIM_POINTS))
67		primtype = DI_PT_POINTLIST_PSIZE;
68
69	fd4_draw_emit(ctx->batch, ring, primtype,
70			emit->binning_pass ? IGNORE_VISIBILITY : USE_VISIBILITY,
71			info, index_offset);
72}
73
74/* fixup dirty shader state in case some "unrelated" (from the state-
75 * tracker's perspective) state change causes us to switch to a
76 * different variant.
77 */
78static void
79fixup_shader_state(struct fd_context *ctx, struct ir3_shader_key *key)
80{
81	struct fd4_context *fd4_ctx = fd4_context(ctx);
82	struct ir3_shader_key *last_key = &fd4_ctx->last_key;
83
84	if (!ir3_shader_key_equal(last_key, key)) {
85		if (ir3_shader_key_changes_fs(last_key, key)) {
86			ctx->dirty_shader[PIPE_SHADER_FRAGMENT] |= FD_DIRTY_SHADER_PROG;
87			ctx->dirty |= FD_DIRTY_PROG;
88		}
89
90		if (ir3_shader_key_changes_vs(last_key, key)) {
91			ctx->dirty_shader[PIPE_SHADER_VERTEX] |= FD_DIRTY_SHADER_PROG;
92			ctx->dirty |= FD_DIRTY_PROG;
93		}
94
95		fd4_ctx->last_key = *key;
96	}
97}
98
99static bool
100fd4_draw_vbo(struct fd_context *ctx, const struct pipe_draw_info *info,
101             unsigned index_offset)
102{
103	struct fd4_context *fd4_ctx = fd4_context(ctx);
104	struct fd4_emit emit = {
105		.debug = &ctx->debug,
106		.vtx  = &ctx->vtx,
107		.prog = &ctx->prog,
108		.info = info,
109		.key = {
110			.color_two_side = ctx->rasterizer->light_twoside,
111			.vclamp_color = ctx->rasterizer->clamp_vertex_color,
112			.fclamp_color = ctx->rasterizer->clamp_fragment_color,
113			.rasterflat = ctx->rasterizer->flatshade,
114			.half_precision = ctx->in_blit &&
115					fd_half_precision(&ctx->batch->framebuffer),
116			.ucp_enables = ctx->rasterizer->clip_plane_enable,
117			.has_per_samp = (fd4_ctx->fsaturate || fd4_ctx->vsaturate ||
118					fd4_ctx->fastc_srgb || fd4_ctx->vastc_srgb),
119			.vsaturate_s = fd4_ctx->vsaturate_s,
120			.vsaturate_t = fd4_ctx->vsaturate_t,
121			.vsaturate_r = fd4_ctx->vsaturate_r,
122			.fsaturate_s = fd4_ctx->fsaturate_s,
123			.fsaturate_t = fd4_ctx->fsaturate_t,
124			.fsaturate_r = fd4_ctx->fsaturate_r,
125			.vastc_srgb = fd4_ctx->vastc_srgb,
126			.fastc_srgb = fd4_ctx->fastc_srgb,
127		},
128		.rasterflat = ctx->rasterizer->flatshade,
129		.sprite_coord_enable = ctx->rasterizer->sprite_coord_enable,
130		.sprite_coord_mode = ctx->rasterizer->sprite_coord_mode,
131	};
132
133	fixup_shader_state(ctx, &emit.key);
134
135	enum fd_dirty_3d_state dirty = ctx->dirty;
136	const struct ir3_shader_variant *vp = fd4_emit_get_vp(&emit);
137	const struct ir3_shader_variant *fp = fd4_emit_get_fp(&emit);
138
139	/* do regular pass first, since that is more likely to fail compiling: */
140
141	if (!vp || !fp)
142		return false;
143
144	ctx->stats.vs_regs += ir3_shader_halfregs(vp);
145	ctx->stats.fs_regs += ir3_shader_halfregs(fp);
146
147	emit.binning_pass = false;
148	emit.dirty = dirty;
149
150	struct fd_ringbuffer *ring = ctx->batch->draw;
151
152	if (ctx->rasterizer->rasterizer_discard) {
153		fd_wfi(ctx->batch, ring);
154		OUT_PKT3(ring, CP_REG_RMW, 3);
155		OUT_RING(ring, REG_A4XX_RB_RENDER_CONTROL);
156		OUT_RING(ring, ~A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE);
157		OUT_RING(ring, A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE);
158	}
159
160	draw_impl(ctx, ctx->batch->draw, &emit, index_offset);
161
162	if (ctx->rasterizer->rasterizer_discard) {
163		fd_wfi(ctx->batch, ring);
164		OUT_PKT3(ring, CP_REG_RMW, 3);
165		OUT_RING(ring, REG_A4XX_RB_RENDER_CONTROL);
166		OUT_RING(ring, ~A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE);
167		OUT_RING(ring, 0);
168	}
169
170	/* and now binning pass: */
171	emit.binning_pass = true;
172	emit.dirty = dirty & ~(FD_DIRTY_BLEND);
173	emit.vp = NULL;   /* we changed key so need to refetch vp */
174	emit.fp = NULL;
175	draw_impl(ctx, ctx->batch->binning, &emit, index_offset);
176
177	fd_context_all_clean(ctx);
178
179	return true;
180}
181
182void
183fd4_draw_init(struct pipe_context *pctx)
184{
185	struct fd_context *ctx = fd_context(pctx);
186	ctx->draw_vbo = fd4_draw_vbo;
187}
188