1 /*
2 * Copyright (C) 2013 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 #include "util/format/u_format.h"
32
33 #include "freedreno_state.h"
34 #include "freedreno_resource.h"
35
36 #include "fd3_draw.h"
37 #include "fd3_context.h"
38 #include "fd3_emit.h"
39 #include "fd3_program.h"
40 #include "fd3_format.h"
41 #include "fd3_zsa.h"
42
43 static inline uint32_t
add_sat(uint32_t a,int32_t b)44 add_sat(uint32_t a, int32_t b)
45 {
46 int64_t ret = (uint64_t)a + (int64_t)b;
47 if (ret > ~0U)
48 return ~0U;
49 if (ret < 0)
50 return 0;
51 return (uint32_t)ret;
52 }
53
54 static void
draw_impl(struct fd_context * ctx,struct fd_ringbuffer * ring,struct fd3_emit * emit,unsigned index_offset)55 draw_impl(struct fd_context *ctx, struct fd_ringbuffer *ring,
56 struct fd3_emit *emit, unsigned index_offset)
57 {
58 const struct pipe_draw_info *info = emit->info;
59 enum pc_di_primtype primtype = ctx->primtypes[info->mode];
60
61 fd3_emit_state(ctx, ring, emit);
62
63 if (emit->dirty & (FD_DIRTY_VTXBUF | FD_DIRTY_VTXSTATE))
64 fd3_emit_vertex_bufs(ring, emit);
65
66 OUT_PKT0(ring, REG_A3XX_PC_VERTEX_REUSE_BLOCK_CNTL, 1);
67 OUT_RING(ring, 0x0000000b); /* PC_VERTEX_REUSE_BLOCK_CNTL */
68
69 OUT_PKT0(ring, REG_A3XX_VFD_INDEX_MIN, 4);
70 OUT_RING(ring, add_sat(info->min_index, info->index_bias)); /* VFD_INDEX_MIN */
71 OUT_RING(ring, add_sat(info->max_index, info->index_bias)); /* VFD_INDEX_MAX */
72 OUT_RING(ring, info->start_instance); /* VFD_INSTANCEID_OFFSET */
73 OUT_RING(ring, info->index_size ? info->index_bias : info->start); /* VFD_INDEX_OFFSET */
74
75 OUT_PKT0(ring, REG_A3XX_PC_RESTART_INDEX, 1);
76 OUT_RING(ring, info->primitive_restart ? /* PC_RESTART_INDEX */
77 info->restart_index : 0xffffffff);
78
79 /* points + psize -> spritelist: */
80 if (ctx->rasterizer->point_size_per_vertex &&
81 fd3_emit_get_vp(emit)->writes_psize &&
82 (info->mode == PIPE_PRIM_POINTS))
83 primtype = DI_PT_POINTLIST_PSIZE;
84
85 fd_draw_emit(ctx->batch, ring, primtype,
86 emit->binning_pass ? IGNORE_VISIBILITY : USE_VISIBILITY,
87 info, index_offset);
88 }
89
90 /* fixup dirty shader state in case some "unrelated" (from the state-
91 * tracker's perspective) state change causes us to switch to a
92 * different variant.
93 */
94 static void
fixup_shader_state(struct fd_context * ctx,struct ir3_shader_key * key)95 fixup_shader_state(struct fd_context *ctx, struct ir3_shader_key *key)
96 {
97 struct fd3_context *fd3_ctx = fd3_context(ctx);
98 struct ir3_shader_key *last_key = &fd3_ctx->last_key;
99
100 if (!ir3_shader_key_equal(last_key, key)) {
101 if (ir3_shader_key_changes_fs(last_key, key)) {
102 ctx->dirty_shader[PIPE_SHADER_FRAGMENT] |= FD_DIRTY_SHADER_PROG;
103 ctx->dirty |= FD_DIRTY_PROG;
104 }
105
106 if (ir3_shader_key_changes_vs(last_key, key)) {
107 ctx->dirty_shader[PIPE_SHADER_VERTEX] |= FD_DIRTY_SHADER_PROG;
108 ctx->dirty |= FD_DIRTY_PROG;
109 }
110
111 fd3_ctx->last_key = *key;
112 }
113 }
114
115 static bool
fd3_draw_vbo(struct fd_context * ctx,const struct pipe_draw_info * info,unsigned index_offset)116 fd3_draw_vbo(struct fd_context *ctx, const struct pipe_draw_info *info,
117 unsigned index_offset)
118 {
119 struct fd3_context *fd3_ctx = fd3_context(ctx);
120 struct fd3_emit emit = {
121 .debug = &ctx->debug,
122 .vtx = &ctx->vtx,
123 .prog = &ctx->prog,
124 .info = info,
125 .key = {
126 .color_two_side = ctx->rasterizer->light_twoside,
127 .vclamp_color = ctx->rasterizer->clamp_vertex_color,
128 .fclamp_color = ctx->rasterizer->clamp_fragment_color,
129 .has_per_samp = (fd3_ctx->fsaturate || fd3_ctx->vsaturate),
130 .vsaturate_s = fd3_ctx->vsaturate_s,
131 .vsaturate_t = fd3_ctx->vsaturate_t,
132 .vsaturate_r = fd3_ctx->vsaturate_r,
133 .fsaturate_s = fd3_ctx->fsaturate_s,
134 .fsaturate_t = fd3_ctx->fsaturate_t,
135 .fsaturate_r = fd3_ctx->fsaturate_r,
136 },
137 .rasterflat = ctx->rasterizer->flatshade,
138 .sprite_coord_enable = ctx->rasterizer->sprite_coord_enable,
139 .sprite_coord_mode = ctx->rasterizer->sprite_coord_mode,
140 };
141
142 if (fd3_needs_manual_clipping(ctx->prog.vs, ctx->rasterizer))
143 emit.key.ucp_enables = ctx->rasterizer->clip_plane_enable;
144
145 fixup_shader_state(ctx, &emit.key);
146
147 unsigned dirty = ctx->dirty;
148 const struct ir3_shader_variant *vp = fd3_emit_get_vp(&emit);
149 const struct ir3_shader_variant *fp = fd3_emit_get_fp(&emit);
150
151 /* do regular pass first, since that is more likely to fail compiling: */
152
153 if (!vp || !fp)
154 return false;
155
156 ctx->stats.vs_regs += ir3_shader_halfregs(vp);
157 ctx->stats.fs_regs += ir3_shader_halfregs(fp);
158
159 emit.binning_pass = false;
160 emit.dirty = dirty;
161 draw_impl(ctx, ctx->batch->draw, &emit, index_offset);
162
163 /* and now binning pass: */
164 emit.binning_pass = true;
165 emit.dirty = dirty & ~(FD_DIRTY_BLEND);
166 emit.vs = NULL; /* we changed key so need to refetch vs */
167 emit.fs = NULL;
168 draw_impl(ctx, ctx->batch->binning, &emit, index_offset);
169
170 fd_context_all_clean(ctx);
171
172 return true;
173 }
174
175 void
fd3_draw_init(struct pipe_context * pctx)176 fd3_draw_init(struct pipe_context *pctx)
177 {
178 struct fd_context *ctx = fd_context(pctx);
179 ctx->draw_vbo = fd3_draw_vbo;
180 }
181