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/format/u_format.h"
29#include "util/u_memory.h"
30#include "util/u_prim.h"
31#include "util/u_string.h"
32
33#include "freedreno_resource.h"
34#include "freedreno_state.h"
35
36#include "fd3_context.h"
37#include "fd3_draw.h"
38#include "fd3_emit.h"
39#include "fd3_format.h"
40#include "fd3_program.h"
41#include "fd3_zsa.h"
42
43static inline uint32_t
44add_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
54static void
55draw_impl(struct fd_context *ctx, struct fd_ringbuffer *ring,
56          struct fd3_emit *emit, unsigned index_offset) assert_dt
57{
58   const struct pipe_draw_info *info = emit->info;
59   enum pc_di_primtype primtype = ctx->screen->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, info->index_bounds_valid
71                     ? add_sat(info->min_index,
72                               info->index_size ? emit->draw->index_bias : 0)
73                     : 0); /* VFD_INDEX_MIN */
74   OUT_RING(ring, info->index_bounds_valid
75                     ? add_sat(info->max_index,
76                               info->index_size ? emit->draw->index_bias : 0)
77                     : ~0);              /* VFD_INDEX_MAX */
78   OUT_RING(ring, info->start_instance); /* VFD_INSTANCEID_OFFSET */
79   OUT_RING(ring, info->index_size ? emit->draw->index_bias
80                                   : emit->draw->start); /* VFD_INDEX_OFFSET */
81
82   OUT_PKT0(ring, REG_A3XX_PC_RESTART_INDEX, 1);
83   OUT_RING(ring, info->primitive_restart ? /* PC_RESTART_INDEX */
84                     info->restart_index
85                                          : 0xffffffff);
86
87   /* points + psize -> spritelist: */
88   if (ctx->rasterizer->point_size_per_vertex &&
89       fd3_emit_get_vp(emit)->writes_psize && (info->mode == PIPE_PRIM_POINTS))
90      primtype = DI_PT_POINTLIST_PSIZE;
91
92   fd_draw_emit(ctx->batch, ring, primtype,
93                emit->binning_pass ? IGNORE_VISIBILITY : USE_VISIBILITY, info,
94                emit->draw, index_offset);
95}
96
97static bool
98fd3_draw_vbo(struct fd_context *ctx, const struct pipe_draw_info *info,
99             unsigned drawid_offset,
100             const struct pipe_draw_indirect_info *indirect,
101             const struct pipe_draw_start_count_bias *draw,
102             unsigned index_offset) in_dt
103{
104   struct fd3_emit emit = {
105      .debug = &ctx->debug,
106      .vtx = &ctx->vtx,
107      .info = info,
108      .drawid_offset = drawid_offset,
109      .indirect = indirect,
110      .draw = draw,
111      .key = {
112         .vs = ctx->prog.vs,
113         .fs = ctx->prog.fs,
114      },
115      .rasterflat = ctx->rasterizer->flatshade,
116      .sprite_coord_enable = ctx->rasterizer->sprite_coord_enable,
117      .sprite_coord_mode = ctx->rasterizer->sprite_coord_mode,
118   };
119
120   if (info->mode != PIPE_PRIM_MAX && !indirect && !info->primitive_restart &&
121       !u_trim_pipe_prim(info->mode, (unsigned *)&draw->count))
122      return false;
123
124   if (fd3_needs_manual_clipping(ir3_get_shader(ctx->prog.vs), ctx->rasterizer))
125      emit.key.key.ucp_enables = ctx->rasterizer->clip_plane_enable;
126
127   ir3_fixup_shader_state(&ctx->base, &emit.key.key);
128
129   unsigned dirty = ctx->dirty;
130
131   emit.prog = fd3_program_state(
132      ir3_cache_lookup(ctx->shader_cache, &emit.key, &ctx->debug));
133
134   /* bail if compile failed: */
135   if (!emit.prog)
136      return false;
137
138   const struct ir3_shader_variant *vp = fd3_emit_get_vp(&emit);
139   const struct ir3_shader_variant *fp = fd3_emit_get_fp(&emit);
140
141   ir3_update_max_tf_vtx(ctx, vp);
142
143   /* do regular pass first: */
144
145   if (unlikely(ctx->stats_users > 0)) {
146      ctx->stats.vs_regs += ir3_shader_halfregs(vp);
147      ctx->stats.fs_regs += ir3_shader_halfregs(fp);
148   }
149
150   emit.binning_pass = false;
151   emit.dirty = dirty;
152   draw_impl(ctx, ctx->batch->draw, &emit, index_offset);
153
154   /* and now binning pass: */
155   emit.binning_pass = true;
156   emit.dirty = dirty & ~(FD_DIRTY_BLEND);
157   emit.vs = NULL; /* we changed key so need to refetch vs */
158   emit.fs = NULL;
159   draw_impl(ctx, ctx->batch->binning, &emit, index_offset);
160
161   fd_context_all_clean(ctx);
162
163   return true;
164}
165
166void
167fd3_draw_init(struct pipe_context *pctx) disable_thread_safety_analysis
168{
169   struct fd_context *ctx = fd_context(pctx);
170   ctx->draw_vbo = fd3_draw_vbo;
171}
172