vc4: Move tile state/alloc allocation into the kernel.
[mesa.git] / src / gallium / drivers / vc4 / vc4_draw.c
1 /*
2 * Copyright (c) 2014 Scott Mansell
3 * Copyright © 2014 Broadcom
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
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "util/u_prim.h"
26 #include "util/u_format.h"
27 #include "util/u_pack_color.h"
28 #include "indices/u_primconvert.h"
29
30 #include "vc4_context.h"
31 #include "vc4_resource.h"
32
33 static void
34 vc4_get_draw_cl_space(struct vc4_context *vc4)
35 {
36 /* Binner gets our packet state -- vc4_emit.c contents,
37 * and the primitive itself.
38 */
39 cl_ensure_space(&vc4->bcl, 256);
40
41 /* Nothing for rcl -- that's covered by vc4_context.c */
42
43 /* shader_rec gets up to 12 dwords of reloc handles plus a maximally
44 * sized shader_rec (104 bytes base for 8 vattrs plus 32 bytes of
45 * vattr stride).
46 */
47 cl_ensure_space(&vc4->shader_rec, 12 * sizeof(uint32_t) + 104 + 8 * 32);
48
49 /* Uniforms are covered by vc4_write_uniforms(). */
50
51 /* There could be up to 16 textures per stage, plus misc other
52 * pointers.
53 */
54 cl_ensure_space(&vc4->bo_handles, (2 * 16 + 20) * sizeof(uint32_t));
55 cl_ensure_space(&vc4->bo_pointers,
56 (2 * 16 + 20) * sizeof(struct vc4_bo *));
57 }
58
59 /**
60 * Does the initial bining command list setup for drawing to a given FBO.
61 */
62 static void
63 vc4_start_draw(struct vc4_context *vc4)
64 {
65 if (vc4->needs_flush)
66 return;
67
68 vc4_get_draw_cl_space(vc4);
69
70 uint32_t width = vc4->framebuffer.width;
71 uint32_t height = vc4->framebuffer.height;
72 uint32_t tilew = align(width, 64) / 64;
73 uint32_t tileh = align(height, 64) / 64;
74
75 // Tile state data is 48 bytes per tile, I think it can be thrown away
76 // as soon as binning is finished.
77 cl_u8(&vc4->bcl, VC4_PACKET_TILE_BINNING_MODE_CONFIG);
78 cl_u32(&vc4->bcl, 0); /* tile alloc addr, filled by kernel */
79 cl_u32(&vc4->bcl, 0); /* tile alloc size, filled by kernel */
80 cl_u32(&vc4->bcl, 0); /* tile state addr, filled by kernel */
81 cl_u8(&vc4->bcl, tilew);
82 cl_u8(&vc4->bcl, tileh);
83 cl_u8(&vc4->bcl, 0); /* flags, filled by kernel. */
84
85 /* START_TILE_BINNING resets the statechange counters in the hardware,
86 * which are what is used when a primitive is binned to a tile to
87 * figure out what new state packets need to be written to that tile's
88 * command list.
89 */
90 cl_u8(&vc4->bcl, VC4_PACKET_START_TILE_BINNING);
91
92 /* Reset the current compressed primitives format. This gets modified
93 * by VC4_PACKET_GL_INDEXED_PRIMITIVE and
94 * VC4_PACKET_GL_ARRAY_PRIMITIVE, so it needs to be reset at the start
95 * of every tile.
96 */
97 cl_u8(&vc4->bcl, VC4_PACKET_PRIMITIVE_LIST_FORMAT);
98 cl_u8(&vc4->bcl, (VC4_PRIMITIVE_LIST_FORMAT_16_INDEX |
99 VC4_PRIMITIVE_LIST_FORMAT_TYPE_TRIANGLES));
100
101 vc4->needs_flush = true;
102 vc4->draw_call_queued = true;
103 vc4->draw_width = width;
104 vc4->draw_height = height;
105 }
106
107 static void
108 vc4_update_shadow_textures(struct pipe_context *pctx,
109 struct vc4_texture_stateobj *stage_tex)
110 {
111 for (int i = 0; i < stage_tex->num_textures; i++) {
112 struct pipe_sampler_view *view = stage_tex->textures[i];
113 if (!view)
114 continue;
115 struct vc4_resource *rsc = vc4_resource(view->texture);
116 if (rsc->shadow_parent)
117 vc4_update_shadow_baselevel_texture(pctx, view);
118 }
119 }
120
121 static void
122 vc4_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
123 {
124 struct vc4_context *vc4 = vc4_context(pctx);
125
126 if (info->mode >= PIPE_PRIM_QUADS) {
127 util_primconvert_save_index_buffer(vc4->primconvert, &vc4->indexbuf);
128 util_primconvert_save_rasterizer_state(vc4->primconvert, &vc4->rasterizer->base);
129 util_primconvert_draw_vbo(vc4->primconvert, info);
130 perf_debug("Fallback conversion for %d %s vertices\n",
131 info->count, u_prim_name(info->mode));
132 return;
133 }
134
135 /* Before setting up the draw, do any fixup blits necessary. */
136 vc4_update_shadow_textures(pctx, &vc4->verttex);
137 vc4_update_shadow_textures(pctx, &vc4->fragtex);
138
139 vc4_get_draw_cl_space(vc4);
140
141 struct vc4_vertex_stateobj *vtx = vc4->vtx;
142 struct vc4_vertexbuf_stateobj *vertexbuf = &vc4->vertexbuf;
143
144 if (vc4->prim_mode != info->mode) {
145 vc4->prim_mode = info->mode;
146 vc4->dirty |= VC4_DIRTY_PRIM_MODE;
147 }
148
149 vc4_start_draw(vc4);
150 vc4_update_compiled_shaders(vc4, info->mode);
151
152 vc4_emit_state(pctx);
153 vc4->dirty = 0;
154
155 vc4_write_uniforms(vc4, vc4->prog.fs,
156 &vc4->constbuf[PIPE_SHADER_FRAGMENT],
157 &vc4->fragtex);
158 vc4_write_uniforms(vc4, vc4->prog.vs,
159 &vc4->constbuf[PIPE_SHADER_VERTEX],
160 &vc4->verttex);
161 vc4_write_uniforms(vc4, vc4->prog.cs,
162 &vc4->constbuf[PIPE_SHADER_VERTEX],
163 &vc4->verttex);
164
165 /* The simulator throws a fit if VS or CS don't read an attribute, so
166 * we emit a dummy read.
167 */
168 uint32_t num_elements_emit = MAX2(vtx->num_elements, 1);
169 /* Emit the shader record. */
170 cl_start_shader_reloc(&vc4->shader_rec, 3 + num_elements_emit);
171 cl_u16(&vc4->shader_rec,
172 VC4_SHADER_FLAG_ENABLE_CLIPPING |
173 ((info->mode == PIPE_PRIM_POINTS &&
174 vc4->rasterizer->base.point_size_per_vertex) ?
175 VC4_SHADER_FLAG_VS_POINT_SIZE : 0));
176 cl_u8(&vc4->shader_rec, 0); /* fs num uniforms (unused) */
177 cl_u8(&vc4->shader_rec, vc4->prog.fs->num_inputs);
178 cl_reloc(vc4, &vc4->shader_rec, vc4->prog.fs->bo, 0);
179 cl_u32(&vc4->shader_rec, 0); /* UBO offset written by kernel */
180
181 cl_u16(&vc4->shader_rec, 0); /* vs num uniforms */
182 cl_u8(&vc4->shader_rec, vc4->prog.vs->vattrs_live);
183 cl_u8(&vc4->shader_rec, vc4->prog.vs->vattr_offsets[8]);
184 cl_reloc(vc4, &vc4->shader_rec, vc4->prog.vs->bo, 0);
185 cl_u32(&vc4->shader_rec, 0); /* UBO offset written by kernel */
186
187 cl_u16(&vc4->shader_rec, 0); /* cs num uniforms */
188 cl_u8(&vc4->shader_rec, vc4->prog.cs->vattrs_live);
189 cl_u8(&vc4->shader_rec, vc4->prog.cs->vattr_offsets[8]);
190 cl_reloc(vc4, &vc4->shader_rec, vc4->prog.cs->bo, 0);
191 cl_u32(&vc4->shader_rec, 0); /* UBO offset written by kernel */
192
193 uint32_t max_index = 0xffff;
194 uint32_t vpm_offset = 0;
195 for (int i = 0; i < vtx->num_elements; i++) {
196 struct pipe_vertex_element *elem = &vtx->pipe[i];
197 struct pipe_vertex_buffer *vb =
198 &vertexbuf->vb[elem->vertex_buffer_index];
199 struct vc4_resource *rsc = vc4_resource(vb->buffer);
200 uint32_t offset = vb->buffer_offset + elem->src_offset;
201 uint32_t vb_size = rsc->bo->size - offset;
202 uint32_t elem_size =
203 util_format_get_blocksize(elem->src_format);
204
205 cl_reloc(vc4, &vc4->shader_rec, rsc->bo, offset);
206 cl_u8(&vc4->shader_rec, elem_size - 1);
207 cl_u8(&vc4->shader_rec, vb->stride);
208 cl_u8(&vc4->shader_rec, vc4->prog.vs->vattr_offsets[i]);
209 cl_u8(&vc4->shader_rec, vc4->prog.cs->vattr_offsets[i]);
210
211 vpm_offset += align(elem_size, 4);
212
213 if (vb->stride > 0) {
214 max_index = MIN2(max_index,
215 (vb_size - elem_size) / vb->stride);
216 }
217 }
218
219 if (vtx->num_elements == 0) {
220 assert(num_elements_emit == 1);
221 struct vc4_bo *bo = vc4_bo_alloc(vc4->screen, 4096, "scratch VBO");
222 cl_reloc(vc4, &vc4->shader_rec, bo, 0);
223 cl_u8(&vc4->shader_rec, 16 - 1); /* element size */
224 cl_u8(&vc4->shader_rec, 0); /* stride */
225 cl_u8(&vc4->shader_rec, 0); /* VS VPM offset */
226 cl_u8(&vc4->shader_rec, 0); /* CS VPM offset */
227 vc4_bo_unreference(&bo);
228 }
229
230 /* the actual draw call. */
231 cl_u8(&vc4->bcl, VC4_PACKET_GL_SHADER_STATE);
232 assert(vtx->num_elements <= 8);
233 /* Note that number of attributes == 0 in the packet means 8
234 * attributes. This field also contains the offset into shader_rec.
235 */
236 cl_u32(&vc4->bcl, num_elements_emit & 0x7);
237
238 /* Note that the primitive type fields match with OpenGL/gallium
239 * definitions, up to but not including QUADS.
240 */
241 if (info->indexed) {
242 uint32_t offset = vc4->indexbuf.offset;
243 uint32_t index_size = vc4->indexbuf.index_size;
244 struct pipe_resource *prsc;
245 if (vc4->indexbuf.index_size == 4) {
246 prsc = vc4_get_shadow_index_buffer(pctx, &vc4->indexbuf,
247 info->count, &offset);
248 index_size = 2;
249 } else {
250 prsc = vc4->indexbuf.buffer;
251 }
252 struct vc4_resource *rsc = vc4_resource(prsc);
253
254 cl_start_reloc(&vc4->bcl, 1);
255 cl_u8(&vc4->bcl, VC4_PACKET_GL_INDEXED_PRIMITIVE);
256 cl_u8(&vc4->bcl,
257 info->mode |
258 (index_size == 2 ?
259 VC4_INDEX_BUFFER_U16:
260 VC4_INDEX_BUFFER_U8));
261 cl_u32(&vc4->bcl, info->count);
262 cl_reloc(vc4, &vc4->bcl, rsc->bo, offset);
263 cl_u32(&vc4->bcl, max_index);
264
265 if (vc4->indexbuf.index_size == 4)
266 pipe_resource_reference(&prsc, NULL);
267 } else {
268 cl_u8(&vc4->bcl, VC4_PACKET_GL_ARRAY_PRIMITIVE);
269 cl_u8(&vc4->bcl, info->mode);
270 cl_u32(&vc4->bcl, info->count);
271 cl_u32(&vc4->bcl, info->start);
272 }
273
274 if (vc4->zsa && vc4->zsa->base.depth.enabled) {
275 vc4->resolve |= PIPE_CLEAR_DEPTH;
276 }
277 if (vc4->zsa && vc4->zsa->base.stencil[0].enabled)
278 vc4->resolve |= PIPE_CLEAR_STENCIL;
279 vc4->resolve |= PIPE_CLEAR_COLOR0;
280
281 vc4->shader_rec_count++;
282
283 if (vc4_debug & VC4_DEBUG_ALWAYS_FLUSH)
284 vc4_flush(pctx);
285 }
286
287 static uint32_t
288 pack_rgba(enum pipe_format format, const float *rgba)
289 {
290 union util_color uc;
291 util_pack_color(rgba, format, &uc);
292 if (util_format_get_blocksize(format) == 2)
293 return uc.us;
294 else
295 return uc.ui[0];
296 }
297
298 static void
299 vc4_clear(struct pipe_context *pctx, unsigned buffers,
300 const union pipe_color_union *color, double depth, unsigned stencil)
301 {
302 struct vc4_context *vc4 = vc4_context(pctx);
303
304 /* We can't flag new buffers for clearing once we've queued draws. We
305 * could avoid this by using the 3d engine to clear.
306 */
307 if (vc4->draw_call_queued) {
308 perf_debug("Flushing rendering to process new clear.");
309 vc4_flush(pctx);
310 }
311
312 if (buffers & PIPE_CLEAR_COLOR0) {
313 vc4->clear_color[0] = vc4->clear_color[1] =
314 pack_rgba(vc4->framebuffer.cbufs[0]->format,
315 color->f);
316 }
317
318 if (buffers & PIPE_CLEAR_DEPTH) {
319 /* Though the depth buffer is stored with Z in the high 24,
320 * for this field we just need to store it in the low 24.
321 */
322 vc4->clear_depth = util_pack_z(PIPE_FORMAT_Z24X8_UNORM, depth);
323 }
324
325 if (buffers & PIPE_CLEAR_STENCIL)
326 vc4->clear_stencil = stencil;
327
328 vc4->draw_min_x = 0;
329 vc4->draw_min_y = 0;
330 vc4->draw_max_x = vc4->framebuffer.width;
331 vc4->draw_max_y = vc4->framebuffer.height;
332 vc4->cleared |= buffers;
333 vc4->resolve |= buffers;
334
335 vc4_start_draw(vc4);
336 }
337
338 static void
339 vc4_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
340 const union pipe_color_union *color,
341 unsigned x, unsigned y, unsigned w, unsigned h)
342 {
343 fprintf(stderr, "unimpl: clear RT\n");
344 }
345
346 static void
347 vc4_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
348 unsigned buffers, double depth, unsigned stencil,
349 unsigned x, unsigned y, unsigned w, unsigned h)
350 {
351 fprintf(stderr, "unimpl: clear DS\n");
352 }
353
354 void
355 vc4_draw_init(struct pipe_context *pctx)
356 {
357 pctx->draw_vbo = vc4_draw_vbo;
358 pctx->clear = vc4_clear;
359 pctx->clear_render_target = vc4_clear_render_target;
360 pctx->clear_depth_stencil = vc4_clear_depth_stencil;
361 }