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