1 /*
2  * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3  * Copyright © 2018 Google, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Authors:
25  *    Rob Clark <robclark@freedesktop.org>
26  */
27 
28 #include "freedreno_query_acc.h"
29 #include "freedreno_state.h"
30 
31 #include "fd6_blend.h"
32 #include "fd6_blitter.h"
33 #include "fd6_compute.h"
34 #include "fd6_context.h"
35 #include "fd6_draw.h"
36 #include "fd6_emit.h"
37 #include "fd6_gmem.h"
38 #include "fd6_image.h"
39 #include "fd6_program.h"
40 #include "fd6_query.h"
41 #include "fd6_rasterizer.h"
42 #include "fd6_resource.h"
43 #include "fd6_texture.h"
44 #include "fd6_zsa.h"
45 
46 static void
fd6_context_destroy(struct pipe_context * pctx)47 fd6_context_destroy(struct pipe_context *pctx) in_dt
48 {
49    struct fd6_context *fd6_ctx = fd6_context(fd_context(pctx));
50 
51    u_upload_destroy(fd6_ctx->border_color_uploader);
52    pipe_resource_reference(&fd6_ctx->border_color_buf, NULL);
53 
54    if (fd6_ctx->streamout_disable_stateobj)
55       fd_ringbuffer_del(fd6_ctx->streamout_disable_stateobj);
56 
57    fd_context_destroy(pctx);
58 
59    if (fd6_ctx->vsc_draw_strm)
60       fd_bo_del(fd6_ctx->vsc_draw_strm);
61    if (fd6_ctx->vsc_prim_strm)
62       fd_bo_del(fd6_ctx->vsc_prim_strm);
63    fd_bo_del(fd6_ctx->control_mem);
64 
65    fd_context_cleanup_common_vbos(&fd6_ctx->base);
66 
67    fd6_texture_fini(pctx);
68 
69    free(fd6_ctx);
70 }
71 
72 static void *
fd6_vertex_state_create(struct pipe_context * pctx,unsigned num_elements,const struct pipe_vertex_element * elements)73 fd6_vertex_state_create(struct pipe_context *pctx, unsigned num_elements,
74                         const struct pipe_vertex_element *elements)
75 {
76    struct fd_context *ctx = fd_context(pctx);
77 
78    struct fd6_vertex_stateobj *state = CALLOC_STRUCT(fd6_vertex_stateobj);
79    memcpy(state->base.pipe, elements, sizeof(*elements) * num_elements);
80    state->base.num_elements = num_elements;
81    state->stateobj =
82       fd_ringbuffer_new_object(ctx->pipe, 4 * (num_elements * 2 + 1));
83    struct fd_ringbuffer *ring = state->stateobj;
84 
85    OUT_PKT4(ring, REG_A6XX_VFD_DECODE(0), 2 * num_elements);
86    for (int32_t i = 0; i < num_elements; i++) {
87       const struct pipe_vertex_element *elem = &elements[i];
88       enum pipe_format pfmt = elem->src_format;
89       enum a6xx_format fmt = fd6_vertex_format(pfmt);
90       bool isint = util_format_is_pure_integer(pfmt);
91       debug_assert(fmt != FMT6_NONE);
92 
93       OUT_RING(ring, A6XX_VFD_DECODE_INSTR_IDX(elem->vertex_buffer_index) |
94                         A6XX_VFD_DECODE_INSTR_OFFSET(elem->src_offset) |
95                         A6XX_VFD_DECODE_INSTR_FORMAT(fmt) |
96                         COND(elem->instance_divisor,
97                              A6XX_VFD_DECODE_INSTR_INSTANCED) |
98                         A6XX_VFD_DECODE_INSTR_SWAP(fd6_vertex_swap(pfmt)) |
99                         A6XX_VFD_DECODE_INSTR_UNK30 |
100                         COND(!isint, A6XX_VFD_DECODE_INSTR_FLOAT));
101       OUT_RING(ring,
102                MAX2(1, elem->instance_divisor)); /* VFD_DECODE[j].STEP_RATE */
103    }
104 
105    return state;
106 }
107 
108 static void
fd6_vertex_state_delete(struct pipe_context * pctx,void * hwcso)109 fd6_vertex_state_delete(struct pipe_context *pctx, void *hwcso)
110 {
111    struct fd6_vertex_stateobj *so = hwcso;
112 
113    fd_ringbuffer_del(so->stateobj);
114    FREE(hwcso);
115 }
116 
117 static void
validate_surface(struct pipe_context * pctx,struct pipe_surface * psurf)118 validate_surface(struct pipe_context *pctx, struct pipe_surface *psurf)
119    assert_dt
120 {
121    fd6_validate_format(fd_context(pctx), fd_resource(psurf->texture),
122                        psurf->format);
123 }
124 
125 static void
fd6_set_framebuffer_state(struct pipe_context * pctx,const struct pipe_framebuffer_state * pfb)126 fd6_set_framebuffer_state(struct pipe_context *pctx,
127                           const struct pipe_framebuffer_state *pfb)
128    in_dt
129 {
130    if (pfb->zsbuf)
131       validate_surface(pctx, pfb->zsbuf);
132 
133    for (unsigned i = 0; i < pfb->nr_cbufs; i++) {
134       if (!pfb->cbufs[i])
135          continue;
136       validate_surface(pctx, pfb->cbufs[i]);
137    }
138 
139    fd_set_framebuffer_state(pctx, pfb);
140 }
141 
142 
143 static void
setup_state_map(struct fd_context * ctx)144 setup_state_map(struct fd_context *ctx)
145 {
146    STATIC_ASSERT(FD6_GROUP_NON_GROUP < 32);
147 
148    fd_context_add_map(ctx, FD_DIRTY_VTXSTATE, BIT(FD6_GROUP_VTXSTATE));
149    fd_context_add_map(ctx, FD_DIRTY_VTXBUF, BIT(FD6_GROUP_VBO));
150    fd_context_add_map(ctx, FD_DIRTY_ZSA | FD_DIRTY_RASTERIZER,
151                       BIT(FD6_GROUP_ZSA));
152    fd_context_add_map(ctx, FD_DIRTY_ZSA | FD_DIRTY_BLEND | FD_DIRTY_PROG,
153                       BIT(FD6_GROUP_LRZ) | BIT(FD6_GROUP_LRZ_BINNING));
154    fd_context_add_map(ctx, FD_DIRTY_PROG | FD_DIRTY_RASTERIZER_CLIP_PLANE_ENABLE,
155                       BIT(FD6_GROUP_PROG));
156    fd_context_add_map(ctx, FD_DIRTY_RASTERIZER, BIT(FD6_GROUP_RASTERIZER));
157    fd_context_add_map(ctx,
158                       FD_DIRTY_FRAMEBUFFER | FD_DIRTY_RASTERIZER_DISCARD |
159                          FD_DIRTY_PROG | FD_DIRTY_BLEND_DUAL,
160                       BIT(FD6_GROUP_PROG_FB_RAST));
161    fd_context_add_map(ctx, FD_DIRTY_BLEND | FD_DIRTY_SAMPLE_MASK,
162                       BIT(FD6_GROUP_BLEND));
163    fd_context_add_map(ctx, FD_DIRTY_BLEND_COLOR, BIT(FD6_GROUP_BLEND_COLOR));
164    fd_context_add_map(ctx, FD_DIRTY_SSBO | FD_DIRTY_IMAGE | FD_DIRTY_PROG,
165                       BIT(FD6_GROUP_IBO));
166    fd_context_add_map(ctx, FD_DIRTY_PROG,
167                       BIT(FD6_GROUP_VS_TEX) | BIT(FD6_GROUP_HS_TEX) |
168                          BIT(FD6_GROUP_DS_TEX) | BIT(FD6_GROUP_GS_TEX) |
169                          BIT(FD6_GROUP_FS_TEX));
170    fd_context_add_map(ctx, FD_DIRTY_PROG | FD_DIRTY_CONST,
171                       BIT(FD6_GROUP_CONST));
172    fd_context_add_map(ctx, FD_DIRTY_STREAMOUT, BIT(FD6_GROUP_SO));
173 
174    fd_context_add_shader_map(ctx, PIPE_SHADER_VERTEX, FD_DIRTY_SHADER_TEX,
175                              BIT(FD6_GROUP_VS_TEX));
176    fd_context_add_shader_map(ctx, PIPE_SHADER_TESS_CTRL, FD_DIRTY_SHADER_TEX,
177                              BIT(FD6_GROUP_HS_TEX));
178    fd_context_add_shader_map(ctx, PIPE_SHADER_TESS_EVAL, FD_DIRTY_SHADER_TEX,
179                              BIT(FD6_GROUP_DS_TEX));
180    fd_context_add_shader_map(ctx, PIPE_SHADER_GEOMETRY, FD_DIRTY_SHADER_TEX,
181                              BIT(FD6_GROUP_GS_TEX));
182    fd_context_add_shader_map(ctx, PIPE_SHADER_FRAGMENT, FD_DIRTY_SHADER_TEX,
183                              BIT(FD6_GROUP_FS_TEX));
184 
185    /* NOTE: scissor enabled bit is part of rasterizer state, but
186     * fd_rasterizer_state_bind() will mark scissor dirty if needed:
187     */
188    fd_context_add_map(ctx, FD_DIRTY_SCISSOR, BIT(FD6_GROUP_SCISSOR));
189 
190    /* Stuff still emit in IB2
191     *
192     * NOTE: viewport state doesn't seem to change frequently, so possibly
193     * move it into FD6_GROUP_RASTERIZER?
194     */
195    fd_context_add_map(
196       ctx, FD_DIRTY_STENCIL_REF | FD_DIRTY_VIEWPORT | FD_DIRTY_RASTERIZER,
197       BIT(FD6_GROUP_NON_GROUP));
198 }
199 
200 struct pipe_context *
fd6_context_create(struct pipe_screen * pscreen,void * priv,unsigned flags)201 fd6_context_create(struct pipe_screen *pscreen, void *priv,
202                    unsigned flags) disable_thread_safety_analysis
203 {
204    struct fd_screen *screen = fd_screen(pscreen);
205    struct fd6_context *fd6_ctx = CALLOC_STRUCT(fd6_context);
206    struct pipe_context *pctx;
207 
208    if (!fd6_ctx)
209       return NULL;
210 
211    pctx = &fd6_ctx->base.base;
212    pctx->screen = pscreen;
213 
214    fd6_ctx->base.dev = fd_device_ref(screen->dev);
215    fd6_ctx->base.screen = fd_screen(pscreen);
216    fd6_ctx->base.last.key = &fd6_ctx->last_key;
217 
218    pctx->destroy = fd6_context_destroy;
219    pctx->create_blend_state = fd6_blend_state_create;
220    pctx->create_rasterizer_state = fd6_rasterizer_state_create;
221    pctx->create_depth_stencil_alpha_state = fd6_zsa_state_create;
222    pctx->create_vertex_elements_state = fd6_vertex_state_create;
223 
224    fd6_draw_init(pctx);
225    fd6_compute_init(pctx);
226    fd6_gmem_init(pctx);
227    fd6_texture_init(pctx);
228    fd6_prog_init(pctx);
229    fd6_emit_init(pctx);
230    fd6_query_context_init(pctx);
231 
232    setup_state_map(&fd6_ctx->base);
233 
234    pctx = fd_context_init(&fd6_ctx->base, pscreen, priv, flags);
235    if (!pctx)
236       return NULL;
237 
238    pctx->set_framebuffer_state = fd6_set_framebuffer_state;
239 
240    /* after fd_context_init() to override set_shader_images() */
241    fd6_image_init(pctx);
242 
243    util_blitter_set_texture_multisample(fd6_ctx->base.blitter, true);
244 
245    pctx->delete_vertex_elements_state = fd6_vertex_state_delete;
246 
247    /* fd_context_init overwrites delete_rasterizer_state, so set this
248     * here. */
249    pctx->delete_rasterizer_state = fd6_rasterizer_state_delete;
250    pctx->delete_blend_state = fd6_blend_state_delete;
251    pctx->delete_depth_stencil_alpha_state = fd6_zsa_state_delete;
252 
253    /* initial sizes for VSC buffers (or rather the per-pipe sizes
254     * which is used to derive entire buffer size:
255     */
256    fd6_ctx->vsc_draw_strm_pitch = 0x440;
257    fd6_ctx->vsc_prim_strm_pitch = 0x1040;
258 
259    fd6_ctx->control_mem =
260       fd_bo_new(screen->dev, 0x1000, 0, "control");
261 
262    memset(fd_bo_map(fd6_ctx->control_mem), 0, sizeof(struct fd6_control));
263 
264    fd_context_setup_common_vbos(&fd6_ctx->base);
265 
266    fd6_blitter_init(pctx);
267 
268    fd6_ctx->border_color_uploader =
269       u_upload_create(pctx, 4096, 0, PIPE_USAGE_STREAM, 0);
270 
271    return fd_context_init_tc(pctx, flags);
272 }
273