freedreno/layout: layout simplifications and pitch from level 0 pitch
[mesa.git] / src / gallium / drivers / freedreno / a4xx / fd4_draw.c
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
43 static void
44 draw_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 */
78 static void
79 fixup_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
99 static bool
100 fd4_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 .ucp_enables = ctx->rasterizer->clip_plane_enable,
115 .has_per_samp = (fd4_ctx->fsaturate || fd4_ctx->vsaturate ||
116 fd4_ctx->fastc_srgb || fd4_ctx->vastc_srgb),
117 .vsaturate_s = fd4_ctx->vsaturate_s,
118 .vsaturate_t = fd4_ctx->vsaturate_t,
119 .vsaturate_r = fd4_ctx->vsaturate_r,
120 .fsaturate_s = fd4_ctx->fsaturate_s,
121 .fsaturate_t = fd4_ctx->fsaturate_t,
122 .fsaturate_r = fd4_ctx->fsaturate_r,
123 .vastc_srgb = fd4_ctx->vastc_srgb,
124 .fastc_srgb = fd4_ctx->fastc_srgb,
125 },
126 .rasterflat = ctx->rasterizer->flatshade,
127 .sprite_coord_enable = ctx->rasterizer->sprite_coord_enable,
128 .sprite_coord_mode = ctx->rasterizer->sprite_coord_mode,
129 };
130
131 fixup_shader_state(ctx, &emit.key);
132
133 enum fd_dirty_3d_state dirty = ctx->dirty;
134 const struct ir3_shader_variant *vp = fd4_emit_get_vp(&emit);
135 const struct ir3_shader_variant *fp = fd4_emit_get_fp(&emit);
136
137 /* do regular pass first, since that is more likely to fail compiling: */
138
139 if (!vp || !fp)
140 return false;
141
142 ctx->stats.vs_regs += ir3_shader_halfregs(vp);
143 ctx->stats.fs_regs += ir3_shader_halfregs(fp);
144
145 emit.binning_pass = false;
146 emit.dirty = dirty;
147
148 struct fd_ringbuffer *ring = ctx->batch->draw;
149
150 if (ctx->rasterizer->rasterizer_discard) {
151 fd_wfi(ctx->batch, ring);
152 OUT_PKT3(ring, CP_REG_RMW, 3);
153 OUT_RING(ring, REG_A4XX_RB_RENDER_CONTROL);
154 OUT_RING(ring, ~A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE);
155 OUT_RING(ring, A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE);
156 }
157
158 draw_impl(ctx, ctx->batch->draw, &emit, index_offset);
159
160 if (ctx->rasterizer->rasterizer_discard) {
161 fd_wfi(ctx->batch, ring);
162 OUT_PKT3(ring, CP_REG_RMW, 3);
163 OUT_RING(ring, REG_A4XX_RB_RENDER_CONTROL);
164 OUT_RING(ring, ~A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE);
165 OUT_RING(ring, 0);
166 }
167
168 /* and now binning pass: */
169 emit.binning_pass = true;
170 emit.dirty = dirty & ~(FD_DIRTY_BLEND);
171 emit.vs = NULL; /* we changed key so need to refetch vs */
172 emit.fs = NULL;
173 draw_impl(ctx, ctx->batch->binning, &emit, index_offset);
174
175 fd_context_all_clean(ctx);
176
177 return true;
178 }
179
180 void
181 fd4_draw_init(struct pipe_context *pctx)
182 {
183 struct fd_context *ctx = fd_context(pctx);
184 ctx->draw_vbo = fd4_draw_vbo;
185 }