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