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 
30 #include "fd6_context.h"
31 #include "fd6_compute.h"
32 #include "fd6_blend.h"
33 #include "fd6_blitter.h"
34 #include "fd6_draw.h"
35 #include "fd6_emit.h"
36 #include "fd6_gmem.h"
37 #include "fd6_image.h"
38 #include "fd6_program.h"
39 #include "fd6_query.h"
40 #include "fd6_rasterizer.h"
41 #include "fd6_texture.h"
42 #include "fd6_zsa.h"
43 
44 static void
fd6_context_destroy(struct pipe_context * pctx)45 fd6_context_destroy(struct pipe_context *pctx)
46 {
47 	struct fd6_context *fd6_ctx = fd6_context(fd_context(pctx));
48 
49 	u_upload_destroy(fd6_ctx->border_color_uploader);
50 	pipe_resource_reference(&fd6_ctx->border_color_buf, NULL);
51 
52 	fd_context_destroy(pctx);
53 
54 	if (fd6_ctx->vsc_draw_strm)
55 		fd_bo_del(fd6_ctx->vsc_draw_strm);
56 	if (fd6_ctx->vsc_prim_strm)
57 		fd_bo_del(fd6_ctx->vsc_prim_strm);
58 	fd_bo_del(fd6_ctx->control_mem);
59 
60 	fd_context_cleanup_common_vbos(&fd6_ctx->base);
61 
62 	ir3_cache_destroy(fd6_ctx->shader_cache);
63 
64 	fd6_texture_fini(pctx);
65 
66 	free(fd6_ctx);
67 }
68 
69 static const uint8_t primtypes[] = {
70 		[PIPE_PRIM_POINTS]                      = DI_PT_POINTLIST,
71 		[PIPE_PRIM_LINES]                       = DI_PT_LINELIST,
72 		[PIPE_PRIM_LINE_STRIP]                  = DI_PT_LINESTRIP,
73 		[PIPE_PRIM_LINE_LOOP]                   = DI_PT_LINELOOP,
74 		[PIPE_PRIM_TRIANGLES]                   = DI_PT_TRILIST,
75 		[PIPE_PRIM_TRIANGLE_STRIP]              = DI_PT_TRISTRIP,
76 		[PIPE_PRIM_TRIANGLE_FAN]                = DI_PT_TRIFAN,
77 		[PIPE_PRIM_LINES_ADJACENCY]             = DI_PT_LINE_ADJ,
78 		[PIPE_PRIM_LINE_STRIP_ADJACENCY]        = DI_PT_LINESTRIP_ADJ,
79 		[PIPE_PRIM_TRIANGLES_ADJACENCY]         = DI_PT_TRI_ADJ,
80 		[PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY]    = DI_PT_TRISTRIP_ADJ,
81 		[PIPE_PRIM_PATCHES]                     = DI_PT_PATCHES0,
82 		[PIPE_PRIM_MAX]                         = DI_PT_RECTLIST,  /* internal clear blits */
83 };
84 
85 static void *
fd6_vertex_state_create(struct pipe_context * pctx,unsigned num_elements,const struct pipe_vertex_element * elements)86 fd6_vertex_state_create(struct pipe_context *pctx, unsigned num_elements,
87 		const struct pipe_vertex_element *elements)
88 {
89 	struct fd_context *ctx = fd_context(pctx);
90 
91 	struct fd6_vertex_stateobj *state = CALLOC_STRUCT(fd6_vertex_stateobj);
92 	memcpy(state->base.pipe, elements, sizeof(*elements) * num_elements);
93 	state->base.num_elements = num_elements;
94 	state->stateobj =
95 		fd_ringbuffer_new_object(ctx->pipe, 4 * (num_elements * 2 + 1));
96 	struct fd_ringbuffer *ring = state->stateobj;
97 
98 	OUT_PKT4(ring, REG_A6XX_VFD_DECODE(0), 2 * num_elements);
99 	for (int32_t i = 0; i < num_elements; i++) {
100 		const struct pipe_vertex_element *elem = &elements[i];
101 		enum pipe_format pfmt = elem->src_format;
102 		enum a6xx_format fmt = fd6_pipe2vtx(pfmt);
103 		bool isint = util_format_is_pure_integer(pfmt);
104 		debug_assert(fmt != FMT6_NONE);
105 
106 		OUT_RING(ring, A6XX_VFD_DECODE_INSTR_IDX(elem->vertex_buffer_index) |
107 				A6XX_VFD_DECODE_INSTR_OFFSET(elem->src_offset) |
108 				A6XX_VFD_DECODE_INSTR_FORMAT(fmt) |
109 				COND(elem->instance_divisor, A6XX_VFD_DECODE_INSTR_INSTANCED) |
110 				A6XX_VFD_DECODE_INSTR_SWAP(fd6_pipe2swap(pfmt)) |
111 				A6XX_VFD_DECODE_INSTR_UNK30 |
112 				COND(!isint, A6XX_VFD_DECODE_INSTR_FLOAT));
113 		OUT_RING(ring, MAX2(1, elem->instance_divisor)); /* VFD_DECODE[j].STEP_RATE */
114 	}
115 
116 	return state;
117 }
118 
119 static void
fd6_vertex_state_delete(struct pipe_context * pctx,void * hwcso)120 fd6_vertex_state_delete(struct pipe_context *pctx, void *hwcso)
121 {
122 	struct fd6_vertex_stateobj *so = hwcso;
123 
124 	fd_ringbuffer_del(so->stateobj);
125 	FREE(hwcso);
126 }
127 
128 struct pipe_context *
fd6_context_create(struct pipe_screen * pscreen,void * priv,unsigned flags)129 fd6_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
130 {
131 	struct fd_screen *screen = fd_screen(pscreen);
132 	struct fd6_context *fd6_ctx = CALLOC_STRUCT(fd6_context);
133 	struct pipe_context *pctx;
134 
135 	if (!fd6_ctx)
136 		return NULL;
137 
138 	pctx = &fd6_ctx->base.base;
139 	pctx->screen = pscreen;
140 
141 	fd6_ctx->base.dev = fd_device_ref(screen->dev);
142 	fd6_ctx->base.screen = fd_screen(pscreen);
143 
144 	pctx->destroy = fd6_context_destroy;
145 	pctx->create_blend_state = fd6_blend_state_create;
146 	pctx->create_rasterizer_state = fd6_rasterizer_state_create;
147 	pctx->create_depth_stencil_alpha_state = fd6_zsa_state_create;
148 	pctx->create_vertex_elements_state = fd6_vertex_state_create;
149 
150 	fd6_draw_init(pctx);
151 	fd6_compute_init(pctx);
152 	fd6_gmem_init(pctx);
153 	fd6_texture_init(pctx);
154 	fd6_prog_init(pctx);
155 	fd6_emit_init(pctx);
156 	fd6_query_context_init(pctx);
157 
158 	pctx = fd_context_init(&fd6_ctx->base, pscreen, primtypes, priv, flags);
159 	if (!pctx)
160 		return NULL;
161 
162 	/* after fd_context_init() to override set_shader_images() */
163 	fd6_image_init(pctx);
164 
165 	util_blitter_set_texture_multisample(fd6_ctx->base.blitter, true);
166 
167 	pctx->delete_vertex_elements_state = fd6_vertex_state_delete;
168 
169 	/* fd_context_init overwrites delete_rasterizer_state, so set this
170 	 * here. */
171 	pctx->delete_rasterizer_state = fd6_rasterizer_state_delete;
172 	pctx->delete_blend_state = fd6_blend_state_delete;
173 	pctx->delete_depth_stencil_alpha_state = fd6_zsa_state_delete;
174 
175 	/* initial sizes for VSC buffers (or rather the per-pipe sizes
176 	 * which is used to derive entire buffer size:
177 	 */
178 	fd6_ctx->vsc_draw_strm_pitch = 0x440;
179 	fd6_ctx->vsc_prim_strm_pitch = 0x1040;
180 
181 	fd6_ctx->control_mem = fd_bo_new(screen->dev, 0x1000,
182 			DRM_FREEDRENO_GEM_TYPE_KMEM, "control");
183 
184 	fd_context_setup_common_vbos(&fd6_ctx->base);
185 
186 	fd6_blitter_init(pctx);
187 
188 	fd6_ctx->border_color_uploader = u_upload_create(pctx, 4096, 0,
189                                                          PIPE_USAGE_STREAM, 0);
190 
191 	return pctx;
192 }
193