1 /*
2 * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
3 * Copyright (c) 2017-2019 Lima Project
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, sub license,
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
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the 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 NON-INFRINGEMENT. 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
22 * DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26 #include "util/format/u_format.h"
27 #include "util/u_debug.h"
28 #include "util/u_draw.h"
29 #include "util/half_float.h"
30 #include "util/u_helpers.h"
31 #include "util/u_inlines.h"
32 #include "util/u_pack_color.h"
33 #include "util/u_split_draw.h"
34 #include "util/u_upload_mgr.h"
35 #include "util/u_prim.h"
36 #include "util/u_vbuf.h"
37 #include "util/hash_table.h"
38
39 #include "lima_context.h"
40 #include "lima_screen.h"
41 #include "lima_resource.h"
42 #include "lima_program.h"
43 #include "lima_bo.h"
44 #include "lima_job.h"
45 #include "lima_texture.h"
46 #include "lima_util.h"
47 #include "lima_gpu.h"
48
49 #include "pan_minmax_cache.h"
50
51 #include <drm-uapi/lima_drm.h>
52
53 static void
lima_clip_scissor_to_viewport(struct lima_context * ctx)54 lima_clip_scissor_to_viewport(struct lima_context *ctx)
55 {
56 struct lima_context_framebuffer *fb = &ctx->framebuffer;
57 struct pipe_scissor_state *cscissor = &ctx->clipped_scissor;
58 int viewport_left, viewport_right, viewport_bottom, viewport_top;
59
60 if (ctx->rasterizer && ctx->rasterizer->base.scissor) {
61 struct pipe_scissor_state *scissor = &ctx->scissor;
62 cscissor->minx = scissor->minx;
63 cscissor->maxx = scissor->maxx;
64 cscissor->miny = scissor->miny;
65 cscissor->maxy = scissor->maxy;
66 } else {
67 cscissor->minx = 0;
68 cscissor->maxx = fb->base.width;
69 cscissor->miny = 0;
70 cscissor->maxy = fb->base.height;
71 }
72
73 viewport_left = MAX2(ctx->viewport.left, 0);
74 cscissor->minx = MAX2(cscissor->minx, viewport_left);
75 viewport_right = MIN2(MAX2(ctx->viewport.right, 0), fb->base.width);
76 cscissor->maxx = MIN2(cscissor->maxx, viewport_right);
77 if (cscissor->minx > cscissor->maxx)
78 cscissor->minx = cscissor->maxx;
79
80 viewport_bottom = MAX2(ctx->viewport.bottom, 0);
81 cscissor->miny = MAX2(cscissor->miny, viewport_bottom);
82 viewport_top = MIN2(MAX2(ctx->viewport.top, 0), fb->base.height);
83 cscissor->maxy = MIN2(cscissor->maxy, viewport_top);
84 if (cscissor->miny > cscissor->maxy)
85 cscissor->miny = cscissor->maxy;
86 }
87
88 static void
lima_extend_viewport(struct lima_context * ctx,const struct pipe_draw_info * info)89 lima_extend_viewport(struct lima_context *ctx, const struct pipe_draw_info *info)
90 {
91 /* restore the original values */
92 ctx->ext_viewport.left = ctx->viewport.left;
93 ctx->ext_viewport.right = ctx->viewport.right;
94 ctx->ext_viewport.bottom = ctx->viewport.bottom;
95 ctx->ext_viewport.top = ctx->viewport.top;
96
97 if (info->mode != PIPE_PRIM_LINES)
98 return;
99
100 if (!ctx->rasterizer)
101 return;
102
103 float line_width = ctx->rasterizer->base.line_width;
104
105 if (line_width == 1.0f)
106 return;
107
108 ctx->ext_viewport.left = ctx->viewport.left - line_width / 2;
109 ctx->ext_viewport.right = ctx->viewport.right + line_width / 2;
110 ctx->ext_viewport.bottom = ctx->viewport.bottom - line_width / 2;
111 ctx->ext_viewport.top = ctx->viewport.top + line_width / 2;
112 }
113
114 static bool
lima_is_scissor_zero(struct lima_context * ctx)115 lima_is_scissor_zero(struct lima_context *ctx)
116 {
117 struct pipe_scissor_state *cscissor = &ctx->clipped_scissor;
118
119 return cscissor->minx == cscissor->maxx || cscissor->miny == cscissor->maxy;
120 }
121
122 static void
lima_update_job_wb(struct lima_context * ctx,unsigned buffers)123 lima_update_job_wb(struct lima_context *ctx, unsigned buffers)
124 {
125 struct lima_job *job = lima_job_get(ctx);
126 struct lima_context_framebuffer *fb = &ctx->framebuffer;
127
128 /* add to job when the buffer is dirty and resolve is clear (not added before) */
129 if (fb->base.nr_cbufs && (buffers & PIPE_CLEAR_COLOR0) &&
130 !(job->resolve & PIPE_CLEAR_COLOR0)) {
131 struct lima_resource *res = lima_resource(fb->base.cbufs[0]->texture);
132 lima_flush_job_accessing_bo(ctx, res->bo, true);
133 _mesa_hash_table_insert(ctx->write_jobs, &res->base, job);
134 lima_job_add_bo(job, LIMA_PIPE_PP, res->bo, LIMA_SUBMIT_BO_WRITE);
135 }
136
137 /* add to job when the buffer is dirty and resolve is clear (not added before) */
138 if (fb->base.zsbuf && (buffers & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) &&
139 !(job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
140 struct lima_resource *res = lima_resource(fb->base.zsbuf->texture);
141 lima_flush_job_accessing_bo(ctx, res->bo, true);
142 _mesa_hash_table_insert(ctx->write_jobs, &res->base, job);
143 lima_job_add_bo(job, LIMA_PIPE_PP, res->bo, LIMA_SUBMIT_BO_WRITE);
144 }
145
146 job->resolve |= buffers;
147 }
148
149 static void
lima_clear(struct pipe_context * pctx,unsigned buffers,const struct pipe_scissor_state * scissor_state,const union pipe_color_union * color,double depth,unsigned stencil)150 lima_clear(struct pipe_context *pctx, unsigned buffers, const struct pipe_scissor_state *scissor_state,
151 const union pipe_color_union *color, double depth, unsigned stencil)
152 {
153 struct lima_context *ctx = lima_context(pctx);
154 struct lima_job *job = lima_job_get(ctx);
155
156 /* flush if this job already contains any draw, otherwise multi clear can be
157 * combined into a single job */
158 if (lima_job_has_draw_pending(job)) {
159 lima_do_job(job);
160 job = lima_job_get(ctx);
161 }
162
163 lima_update_job_wb(ctx, buffers);
164
165 /* no need to reload if cleared */
166 if (ctx->framebuffer.base.nr_cbufs && (buffers & PIPE_CLEAR_COLOR0)) {
167 struct lima_surface *surf = lima_surface(ctx->framebuffer.base.cbufs[0]);
168 surf->reload &= ~PIPE_CLEAR_COLOR0;
169 }
170
171 struct lima_job_clear *clear = &job->clear;
172 clear->buffers = buffers;
173
174 if (buffers & PIPE_CLEAR_COLOR0) {
175 clear->color_8pc =
176 ((uint32_t)float_to_ubyte(color->f[3]) << 24) |
177 ((uint32_t)float_to_ubyte(color->f[2]) << 16) |
178 ((uint32_t)float_to_ubyte(color->f[1]) << 8) |
179 float_to_ubyte(color->f[0]);
180
181 clear->color_16pc =
182 ((uint64_t)float_to_ushort(color->f[3]) << 48) |
183 ((uint64_t)float_to_ushort(color->f[2]) << 32) |
184 ((uint64_t)float_to_ushort(color->f[1]) << 16) |
185 float_to_ushort(color->f[0]);
186 }
187
188 struct lima_surface *zsbuf = lima_surface(ctx->framebuffer.base.zsbuf);
189
190 if (buffers & PIPE_CLEAR_DEPTH) {
191 clear->depth = util_pack_z(PIPE_FORMAT_Z24X8_UNORM, depth);
192 if (zsbuf)
193 zsbuf->reload &= ~PIPE_CLEAR_DEPTH;
194 }
195
196 if (buffers & PIPE_CLEAR_STENCIL) {
197 clear->stencil = stencil;
198 if (zsbuf)
199 zsbuf->reload &= ~PIPE_CLEAR_STENCIL;
200 }
201
202 ctx->dirty |= LIMA_CONTEXT_DIRTY_CLEAR;
203
204 lima_damage_rect_union(&job->damage_rect,
205 0, ctx->framebuffer.base.width,
206 0, ctx->framebuffer.base.height);
207 }
208
209 enum lima_attrib_type {
210 LIMA_ATTRIB_FLOAT = 0x000,
211 LIMA_ATTRIB_I32 = 0x001,
212 LIMA_ATTRIB_U32 = 0x002,
213 LIMA_ATTRIB_FP16 = 0x003,
214 LIMA_ATTRIB_I16 = 0x004,
215 LIMA_ATTRIB_U16 = 0x005,
216 LIMA_ATTRIB_I8 = 0x006,
217 LIMA_ATTRIB_U8 = 0x007,
218 LIMA_ATTRIB_I8N = 0x008,
219 LIMA_ATTRIB_U8N = 0x009,
220 LIMA_ATTRIB_I16N = 0x00A,
221 LIMA_ATTRIB_U16N = 0x00B,
222 LIMA_ATTRIB_I32N = 0x00D,
223 LIMA_ATTRIB_U32N = 0x00E,
224 LIMA_ATTRIB_FIXED = 0x101
225 };
226
227 static enum lima_attrib_type
lima_pipe_format_to_attrib_type(enum pipe_format format)228 lima_pipe_format_to_attrib_type(enum pipe_format format)
229 {
230 const struct util_format_description *desc = util_format_description(format);
231 int i = util_format_get_first_non_void_channel(format);
232 const struct util_format_channel_description *c = desc->channel + i;
233
234 switch (c->type) {
235 case UTIL_FORMAT_TYPE_FLOAT:
236 if (c->size == 16)
237 return LIMA_ATTRIB_FP16;
238 else
239 return LIMA_ATTRIB_FLOAT;
240 case UTIL_FORMAT_TYPE_FIXED:
241 return LIMA_ATTRIB_FIXED;
242 case UTIL_FORMAT_TYPE_SIGNED:
243 if (c->size == 8) {
244 if (c->normalized)
245 return LIMA_ATTRIB_I8N;
246 else
247 return LIMA_ATTRIB_I8;
248 }
249 else if (c->size == 16) {
250 if (c->normalized)
251 return LIMA_ATTRIB_I16N;
252 else
253 return LIMA_ATTRIB_I16;
254 }
255 else if (c->size == 32) {
256 if (c->normalized)
257 return LIMA_ATTRIB_I32N;
258 else
259 return LIMA_ATTRIB_I32;
260 }
261 break;
262 case UTIL_FORMAT_TYPE_UNSIGNED:
263 if (c->size == 8) {
264 if (c->normalized)
265 return LIMA_ATTRIB_U8N;
266 else
267 return LIMA_ATTRIB_U8;
268 }
269 else if (c->size == 16) {
270 if (c->normalized)
271 return LIMA_ATTRIB_U16N;
272 else
273 return LIMA_ATTRIB_U16;
274 }
275 else if (c->size == 32) {
276 if (c->normalized)
277 return LIMA_ATTRIB_U32N;
278 else
279 return LIMA_ATTRIB_U32;
280 }
281 break;
282 }
283
284 return LIMA_ATTRIB_FLOAT;
285 }
286
287 static void
lima_pack_vs_cmd(struct lima_context * ctx,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)288 lima_pack_vs_cmd(struct lima_context *ctx, const struct pipe_draw_info *info,
289 const struct pipe_draw_start_count_bias *draw)
290 {
291 struct lima_context_constant_buffer *ccb =
292 ctx->const_buffer + PIPE_SHADER_VERTEX;
293 struct lima_vs_compiled_shader *vs = ctx->vs;
294 struct lima_job *job = lima_job_get(ctx);
295
296 VS_CMD_BEGIN(&job->vs_cmd_array, 24);
297
298 if (!info->index_size) {
299 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_1();
300 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_2();
301 }
302 int uniform_size = MIN2(vs->state.uniform_size, ccb->size);
303
304 int size = uniform_size + vs->state.constant_size + 32;
305 VS_CMD_UNIFORMS_ADDRESS(
306 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_uniform),
307 align(size, 16));
308
309 VS_CMD_SHADER_ADDRESS(ctx->vs->bo->va, ctx->vs->state.shader_size);
310 VS_CMD_SHADER_INFO(ctx->vs->state.prefetch, ctx->vs->state.shader_size);
311
312 int num_outputs = ctx->vs->state.num_outputs;
313 int num_attributes = ctx->vertex_elements->num_elements;
314 VS_CMD_VARYING_ATTRIBUTE_COUNT(num_outputs, MAX2(1, num_attributes));
315
316 VS_CMD_UNKNOWN1();
317
318 VS_CMD_ATTRIBUTES_ADDRESS(
319 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_attribute_info),
320 MAX2(1, num_attributes));
321
322 VS_CMD_VARYINGS_ADDRESS(
323 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_varying_info),
324 num_outputs);
325
326 unsigned num = info->index_size ? (ctx->max_index - ctx->min_index + 1) : draw->count;
327 VS_CMD_DRAW(num, info->index_size);
328
329 VS_CMD_UNKNOWN2();
330
331 VS_CMD_ARRAYS_SEMAPHORE_END(info->index_size);
332
333 VS_CMD_END();
334 }
335
336 static void
lima_pack_plbu_cmd(struct lima_context * ctx,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)337 lima_pack_plbu_cmd(struct lima_context *ctx, const struct pipe_draw_info *info,
338 const struct pipe_draw_start_count_bias *draw)
339 {
340 struct lima_vs_compiled_shader *vs = ctx->vs;
341 struct pipe_scissor_state *cscissor = &ctx->clipped_scissor;
342 struct lima_job *job = lima_job_get(ctx);
343 PLBU_CMD_BEGIN(&job->plbu_cmd_array, 32);
344
345 PLBU_CMD_VIEWPORT_LEFT(fui(ctx->ext_viewport.left));
346 PLBU_CMD_VIEWPORT_RIGHT(fui(ctx->ext_viewport.right));
347 PLBU_CMD_VIEWPORT_BOTTOM(fui(ctx->ext_viewport.bottom));
348 PLBU_CMD_VIEWPORT_TOP(fui(ctx->ext_viewport.top));
349
350 if (!info->index_size)
351 PLBU_CMD_ARRAYS_SEMAPHORE_BEGIN();
352
353 int cf = ctx->rasterizer->base.cull_face;
354 int ccw = ctx->rasterizer->base.front_ccw;
355 uint32_t cull = 0;
356 bool force_point_size = false;
357
358 if (cf != PIPE_FACE_NONE) {
359 if (cf & PIPE_FACE_FRONT)
360 cull |= ccw ? 0x00040000 : 0x00020000;
361 if (cf & PIPE_FACE_BACK)
362 cull |= ccw ? 0x00020000 : 0x00040000;
363 }
364
365 /* Specify point size with PLBU command if shader doesn't write */
366 if (info->mode == PIPE_PRIM_POINTS && ctx->vs->state.point_size_idx == -1)
367 force_point_size = true;
368
369 /* Specify line width with PLBU command for lines */
370 if (info->mode > PIPE_PRIM_POINTS && info->mode < PIPE_PRIM_TRIANGLES)
371 force_point_size = true;
372
373 PLBU_CMD_PRIMITIVE_SETUP(force_point_size, cull, info->index_size);
374
375 PLBU_CMD_RSW_VERTEX_ARRAY(
376 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw),
377 ctx->gp_output->va);
378
379 /* TODO
380 * - we should set it only for the first draw that enabled the scissor and for
381 * latter draw only if scissor is dirty
382 */
383
384 assert(cscissor->minx < cscissor->maxx && cscissor->miny < cscissor->maxy);
385 PLBU_CMD_SCISSORS(cscissor->minx, cscissor->maxx, cscissor->miny, cscissor->maxy);
386
387 lima_damage_rect_union(&job->damage_rect, cscissor->minx, cscissor->maxx,
388 cscissor->miny, cscissor->maxy);
389
390 PLBU_CMD_UNKNOWN1();
391
392 PLBU_CMD_DEPTH_RANGE_NEAR(fui(ctx->viewport.near));
393 PLBU_CMD_DEPTH_RANGE_FAR(fui(ctx->viewport.far));
394
395 if ((info->mode == PIPE_PRIM_POINTS && ctx->vs->state.point_size_idx == -1) ||
396 ((info->mode >= PIPE_PRIM_LINES) && (info->mode < PIPE_PRIM_TRIANGLES)))
397 {
398 uint32_t v = info->mode == PIPE_PRIM_POINTS ?
399 fui(ctx->rasterizer->base.point_size) : fui(ctx->rasterizer->base.line_width);
400 PLBU_CMD_LOW_PRIM_SIZE(v);
401 }
402
403 if (info->index_size) {
404 PLBU_CMD_INDEXED_DEST(ctx->gp_output->va);
405 if (vs->state.point_size_idx != -1)
406 PLBU_CMD_INDEXED_PT_SIZE(ctx->gp_output->va + ctx->gp_output_point_size_offt);
407
408 PLBU_CMD_INDICES(ctx->index_res->bo->va + draw->start * info->index_size + ctx->index_offset);
409 }
410 else {
411 /* can this make the attribute info static? */
412 PLBU_CMD_DRAW_ARRAYS(info->mode, draw->start, draw->count);
413 }
414
415 PLBU_CMD_ARRAYS_SEMAPHORE_END();
416
417 if (info->index_size)
418 PLBU_CMD_DRAW_ELEMENTS(info->mode, ctx->min_index, draw->count);
419
420 PLBU_CMD_END();
421 }
422
423 static int
lima_blend_func(enum pipe_blend_func pipe)424 lima_blend_func(enum pipe_blend_func pipe)
425 {
426 switch (pipe) {
427 case PIPE_BLEND_ADD:
428 return 2;
429 case PIPE_BLEND_SUBTRACT:
430 return 0;
431 case PIPE_BLEND_REVERSE_SUBTRACT:
432 return 1;
433 case PIPE_BLEND_MIN:
434 return 4;
435 case PIPE_BLEND_MAX:
436 return 5;
437 }
438 return -1;
439 }
440
441 static int
lima_blend_factor(enum pipe_blendfactor pipe)442 lima_blend_factor(enum pipe_blendfactor pipe)
443 {
444 /* Bits 0-2 indicate the blendfactor type,
445 * Bit 3 is set if blendfactor is inverted
446 * Bit 4 is set if blendfactor has alpha */
447 switch (pipe) {
448 case PIPE_BLENDFACTOR_SRC_COLOR:
449 return 0 << 4 | 0 << 3 | 0;
450 case PIPE_BLENDFACTOR_SRC_ALPHA:
451 return 1 << 4 | 0 << 3 | 0;
452 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
453 return 0 << 4 | 1 << 3 | 0;
454 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
455 return 1 << 4 | 1 << 3 | 0;
456
457 case PIPE_BLENDFACTOR_DST_COLOR:
458 return 0 << 4 | 0 << 3 | 1;
459 case PIPE_BLENDFACTOR_DST_ALPHA:
460 return 1 << 4 | 0 << 3 | 1;
461 case PIPE_BLENDFACTOR_INV_DST_COLOR:
462 return 0 << 4 | 1 << 3 | 1;
463 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
464 return 1 << 4 | 1 << 3 | 1;
465
466 case PIPE_BLENDFACTOR_CONST_COLOR:
467 return 0 << 4 | 0 << 3 | 2;
468 case PIPE_BLENDFACTOR_CONST_ALPHA:
469 return 1 << 4 | 0 << 3 | 2;
470 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
471 return 0 << 4 | 1 << 3 | 2;
472 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
473 return 1 << 4 | 1 << 3 | 2;
474
475 case PIPE_BLENDFACTOR_ZERO:
476 return 0 << 4 | 0 << 3 | 3;
477 case PIPE_BLENDFACTOR_ONE:
478 return 0 << 4 | 1 << 3 | 3;
479
480 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
481 return 0 << 4 | 0 << 3 | 4;
482
483 case PIPE_BLENDFACTOR_SRC1_COLOR:
484 return 0 << 4 | 0 << 3 | 5;
485 case PIPE_BLENDFACTOR_SRC1_ALPHA:
486 return 1 << 4 | 0 << 3 | 5;
487 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
488 return 0 << 4 | 1 << 3 | 5;
489 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
490 return 1 << 4 | 1 << 3 | 5;
491 }
492 return -1;
493 }
494
495 static int
lima_calculate_alpha_blend(enum pipe_blend_func rgb_func,enum pipe_blend_func alpha_func,enum pipe_blendfactor rgb_src_factor,enum pipe_blendfactor rgb_dst_factor,enum pipe_blendfactor alpha_src_factor,enum pipe_blendfactor alpha_dst_factor)496 lima_calculate_alpha_blend(enum pipe_blend_func rgb_func, enum pipe_blend_func alpha_func,
497 enum pipe_blendfactor rgb_src_factor, enum pipe_blendfactor rgb_dst_factor,
498 enum pipe_blendfactor alpha_src_factor, enum pipe_blendfactor alpha_dst_factor)
499 {
500 /* PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE has to be changed to PIPE_BLENDFACTOR_ONE
501 * if it is set for alpha_src or alpha_dst.
502 */
503 if (alpha_src_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE)
504 alpha_src_factor = PIPE_BLENDFACTOR_ONE;
505
506 if (alpha_dst_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE)
507 alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
508
509 /* MIN and MAX ops actually do OP(As * S + Ad * D, Ad), so
510 * we need to set S to 1 and D to 0 to get correct result */
511 if (alpha_func == PIPE_BLEND_MIN ||
512 alpha_func == PIPE_BLEND_MAX) {
513 alpha_src_factor = PIPE_BLENDFACTOR_ONE;
514 alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
515 }
516
517 /* MIN and MAX ops actually do OP(Cs * S + Cd * D, Cd), so
518 * we need to set S to 1 and D to 0 to get correct result */
519 if (rgb_func == PIPE_BLEND_MIN ||
520 rgb_func == PIPE_BLEND_MAX) {
521 rgb_src_factor = PIPE_BLENDFACTOR_ONE;
522 rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
523 }
524
525 return lima_blend_func(rgb_func) |
526 (lima_blend_func(alpha_func) << 3) |
527 (lima_blend_factor(rgb_src_factor) << 6) |
528 (lima_blend_factor(rgb_dst_factor) << 11) |
529 /* alpha_src and alpha_dst are 4 bit, so need to mask 5th bit */
530 ((lima_blend_factor(alpha_src_factor) & 0xf) << 16) |
531 ((lima_blend_factor(alpha_dst_factor) & 0xf) << 20) |
532 0x0C000000; /* need to check if this is GLESv1 glAlphaFunc */
533 }
534
535 static int
lima_stencil_op(enum pipe_stencil_op pipe)536 lima_stencil_op(enum pipe_stencil_op pipe)
537 {
538 switch (pipe) {
539 case PIPE_STENCIL_OP_KEEP:
540 return 0;
541 case PIPE_STENCIL_OP_ZERO:
542 return 2;
543 case PIPE_STENCIL_OP_REPLACE:
544 return 1;
545 case PIPE_STENCIL_OP_INCR:
546 return 6;
547 case PIPE_STENCIL_OP_DECR:
548 return 7;
549 case PIPE_STENCIL_OP_INCR_WRAP:
550 return 4;
551 case PIPE_STENCIL_OP_DECR_WRAP:
552 return 5;
553 case PIPE_STENCIL_OP_INVERT:
554 return 3;
555 }
556 return -1;
557 }
558
559 static unsigned
lima_calculate_depth_test(struct pipe_depth_stencil_alpha_state * depth,struct pipe_rasterizer_state * rst)560 lima_calculate_depth_test(struct pipe_depth_stencil_alpha_state *depth,
561 struct pipe_rasterizer_state *rst)
562 {
563 int offset_scale = 0, offset_units = 0;
564 enum pipe_compare_func func = (depth->depth_enabled ? depth->depth_func : PIPE_FUNC_ALWAYS);
565
566 offset_scale = CLAMP(rst->offset_scale * 4, -128, 127);
567 if (offset_scale < 0)
568 offset_scale += 0x100;
569
570 offset_units = CLAMP(rst->offset_units * 2, -128, 127);
571 if (offset_units < 0)
572 offset_units += 0x100;
573
574 return (depth->depth_enabled && depth->depth_writemask) |
575 ((int)func << 1) |
576 (offset_scale << 16) |
577 (offset_units << 24);
578 }
579
580 static void
lima_pack_render_state(struct lima_context * ctx,const struct pipe_draw_info * info)581 lima_pack_render_state(struct lima_context *ctx, const struct pipe_draw_info *info)
582 {
583 struct lima_fs_compiled_shader *fs = ctx->fs;
584 struct lima_render_state *render =
585 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_plb_rsw,
586 sizeof(*render));
587 bool early_z = true;
588 bool pixel_kill = true;
589
590 /* do hw support RGBA independ blend?
591 * PIPE_CAP_INDEP_BLEND_ENABLE
592 *
593 * how to handle the no cbuf only zbuf case?
594 */
595 struct pipe_rt_blend_state *rt = ctx->blend->base.rt;
596 render->blend_color_bg = float_to_ubyte(ctx->blend_color.color[2]) |
597 (float_to_ubyte(ctx->blend_color.color[1]) << 16);
598 render->blend_color_ra = float_to_ubyte(ctx->blend_color.color[0]) |
599 (float_to_ubyte(ctx->blend_color.color[3]) << 16);
600
601 if (rt->blend_enable) {
602 render->alpha_blend = lima_calculate_alpha_blend(rt->rgb_func, rt->alpha_func,
603 rt->rgb_src_factor, rt->rgb_dst_factor,
604 rt->alpha_src_factor, rt->alpha_dst_factor);
605 }
606 else {
607 /*
608 * Special handling for blending disabled.
609 * Binary driver is generating the same alpha_value,
610 * as when we would just enable blending, without changing/setting any blend equation/params.
611 * Normaly in this case mesa would set all rt fields (func/factor) to zero.
612 */
613 render->alpha_blend = lima_calculate_alpha_blend(PIPE_BLEND_ADD, PIPE_BLEND_ADD,
614 PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ZERO,
615 PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ZERO);
616 }
617
618 render->alpha_blend |= (rt->colormask & PIPE_MASK_RGBA) << 28;
619
620 struct pipe_rasterizer_state *rst = &ctx->rasterizer->base;
621 render->depth_test = lima_calculate_depth_test(&ctx->zsa->base, rst);
622
623 if (!rst->depth_clip_near || ctx->viewport.near == 0.0f)
624 render->depth_test |= 0x10; /* don't clip depth near */
625 if (!rst->depth_clip_far || ctx->viewport.far == 1.0f)
626 render->depth_test |= 0x20; /* don't clip depth far */
627
628 if (fs->state.frag_depth_reg != -1) {
629 render->depth_test |= (fs->state.frag_depth_reg << 6);
630 /* Shader writes depth */
631 render->depth_test |= 0x801;
632 }
633
634 ushort far, near;
635
636 near = float_to_ushort(ctx->viewport.near);
637 far = float_to_ushort(ctx->viewport.far);
638
639 /* overlap with plbu? any place can remove one? */
640 render->depth_range = near | (far << 16);
641
642 struct pipe_stencil_state *stencil = ctx->zsa->base.stencil;
643 struct pipe_stencil_ref *ref = &ctx->stencil_ref;
644
645 if (stencil[0].enabled) { /* stencil is enabled */
646 render->stencil_front = stencil[0].func |
647 (lima_stencil_op(stencil[0].fail_op) << 3) |
648 (lima_stencil_op(stencil[0].zfail_op) << 6) |
649 (lima_stencil_op(stencil[0].zpass_op) << 9) |
650 (ref->ref_value[0] << 16) |
651 (stencil[0].valuemask << 24);
652 render->stencil_back = render->stencil_front;
653 render->stencil_test = (stencil[0].writemask & 0xff) | (stencil[0].writemask & 0xff) << 8;
654 if (stencil[1].enabled) { /* two-side is enabled */
655 render->stencil_back = stencil[1].func |
656 (lima_stencil_op(stencil[1].fail_op) << 3) |
657 (lima_stencil_op(stencil[1].zfail_op) << 6) |
658 (lima_stencil_op(stencil[1].zpass_op) << 9) |
659 (ref->ref_value[1] << 16) |
660 (stencil[1].valuemask << 24);
661 render->stencil_test = (stencil[0].writemask & 0xff) | (stencil[1].writemask & 0xff) << 8;
662 }
663 /* TODO: Find out, what (render->stecil_test & 0xff000000) is */
664 }
665 else {
666 /* Default values, when stencil is disabled:
667 * stencil[0|1].valuemask = 0xff
668 * stencil[0|1].func = PIPE_FUNC_ALWAYS
669 * stencil[0|1].writemask = 0xff
670 */
671 render->stencil_front = 0xff000007;
672 render->stencil_back = 0xff000007;
673 render->stencil_test = 0x0000ffff;
674 }
675
676 /* need more investigation */
677 if (info->mode == PIPE_PRIM_POINTS)
678 render->multi_sample = 0x00000000;
679 else if (info->mode < PIPE_PRIM_TRIANGLES)
680 render->multi_sample = 0x00000400;
681 else
682 render->multi_sample = 0x00000800;
683 if (ctx->framebuffer.base.samples)
684 render->multi_sample |= 0x68;
685 if (ctx->blend->base.alpha_to_coverage)
686 render->multi_sample |= (1 << 7);
687 if (ctx->blend->base.alpha_to_one)
688 render->multi_sample |= (1 << 8);
689 render->multi_sample |= (ctx->sample_mask << 12);
690
691 /* Set gl_FragColor register, need to specify it 4 times */
692 render->multi_sample |= (fs->state.frag_color0_reg << 28) |
693 (fs->state.frag_color0_reg << 24) |
694 (fs->state.frag_color0_reg << 20) |
695 (fs->state.frag_color0_reg << 16);
696
697 /* alpha test */
698 if (ctx->zsa->base.alpha_enabled) {
699 render->multi_sample |= ctx->zsa->base.alpha_func;
700 render->stencil_test |= float_to_ubyte(ctx->zsa->base.alpha_ref_value) << 16;
701 } else {
702 /* func = PIPE_FUNC_ALWAYS */
703 render->multi_sample |= 0x7;
704 }
705
706 render->shader_address =
707 ctx->fs->bo->va | (((uint32_t *)ctx->fs->bo->map)[0] & 0x1F);
708
709 /* seems not needed */
710 render->uniforms_address = 0x00000000;
711
712 render->textures_address = 0x00000000;
713
714 render->aux0 = (ctx->vs->state.varying_stride >> 3);
715 render->aux1 = 0x00000000;
716 if (ctx->rasterizer->base.front_ccw)
717 render->aux1 = 0x00001000;
718
719 if (ctx->blend->base.dither)
720 render->aux1 |= 0x00002000;
721
722 if (fs->state.uses_discard ||
723 ctx->zsa->base.alpha_enabled ||
724 fs->state.frag_depth_reg != -1 ||
725 ctx->blend->base.alpha_to_coverage) {
726 early_z = false;
727 pixel_kill = false;
728 }
729
730 if (rt->blend_enable)
731 pixel_kill = false;
732
733 if ((rt->colormask & PIPE_MASK_RGBA) != PIPE_MASK_RGBA)
734 pixel_kill = false;
735
736 if (early_z)
737 render->aux0 |= 0x300;
738
739 if (pixel_kill)
740 render->aux0 |= 0x1000;
741
742 if (ctx->tex_stateobj.num_samplers) {
743 render->textures_address =
744 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc);
745 render->aux0 |= ctx->tex_stateobj.num_samplers << 14;
746 render->aux0 |= 0x20;
747 }
748
749 if (ctx->const_buffer[PIPE_SHADER_FRAGMENT].buffer) {
750 render->uniforms_address =
751 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform_array);
752 uint32_t size = ctx->buffer_state[lima_ctx_buff_pp_uniform].size;
753 uint32_t bits = 0;
754 if (size >= 8) {
755 bits = util_last_bit(size >> 3) - 1;
756 bits += size & u_bit_consecutive(0, bits + 3) ? 1 : 0;
757 }
758 render->uniforms_address |= bits > 0xf ? 0xf : bits;
759
760 render->aux0 |= 0x80;
761 render->aux1 |= 0x10000;
762 }
763
764 /* Set secondary output color */
765 if (fs->state.frag_color1_reg != -1)
766 render->aux0 |= (fs->state.frag_color1_reg << 28);
767
768 if (ctx->vs->state.num_varyings) {
769 render->varying_types = 0x00000000;
770 render->varyings_address = ctx->gp_output->va +
771 ctx->gp_output_varyings_offt;
772 for (int i = 0, index = 0; i < ctx->vs->state.num_outputs; i++) {
773 int val;
774
775 if (i == ctx->vs->state.gl_pos_idx ||
776 i == ctx->vs->state.point_size_idx)
777 continue;
778
779 struct lima_varying_info *v = ctx->vs->state.varying + i;
780 if (v->component_size == 4)
781 val = v->components > 2 ? 0 : 1;
782 else
783 val = v->components > 2 ? 2 : 3;
784
785 if (index < 10)
786 render->varying_types |= val << (3 * index);
787 else if (index == 10) {
788 render->varying_types |= val << 30;
789 render->varyings_address |= val >> 2;
790 }
791 else if (index == 11)
792 render->varyings_address |= val << 1;
793
794 index++;
795 }
796 }
797 else {
798 render->varying_types = 0x00000000;
799 render->varyings_address = 0x00000000;
800 }
801
802 struct lima_job *job = lima_job_get(ctx);
803
804 lima_dump_command_stream_print(
805 job->dump, render, sizeof(*render),
806 false, "add render state at va %x\n",
807 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw));
808
809 lima_dump_rsw_command_stream_print(
810 job->dump, render, sizeof(*render),
811 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw));
812 }
813
814 static void
lima_update_gp_attribute_info(struct lima_context * ctx,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)815 lima_update_gp_attribute_info(struct lima_context *ctx, const struct pipe_draw_info *info,
816 const struct pipe_draw_start_count_bias *draw)
817 {
818 struct lima_job *job = lima_job_get(ctx);
819 struct lima_vertex_element_state *ve = ctx->vertex_elements;
820 struct lima_context_vertex_buffer *vb = &ctx->vertex_buffers;
821
822 uint32_t *attribute =
823 lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_attribute_info,
824 MAX2(1, ve->num_elements) * 8);
825
826 int n = 0;
827 for (int i = 0; i < ve->num_elements; i++) {
828 struct pipe_vertex_element *pve = ve->pipe + i;
829
830 assert(pve->vertex_buffer_index < vb->count);
831 assert(vb->enabled_mask & (1 << pve->vertex_buffer_index));
832
833 struct pipe_vertex_buffer *pvb = vb->vb + pve->vertex_buffer_index;
834 struct lima_resource *res = lima_resource(pvb->buffer.resource);
835
836 lima_job_add_bo(job, LIMA_PIPE_GP, res->bo, LIMA_SUBMIT_BO_READ);
837
838 unsigned start = info->index_size ? (ctx->min_index + draw->index_bias) : draw->start;
839 attribute[n++] = res->bo->va + pvb->buffer_offset + pve->src_offset
840 + start * pvb->stride;
841 attribute[n++] = (pvb->stride << 11) |
842 (lima_pipe_format_to_attrib_type(pve->src_format) << 2) |
843 (util_format_get_nr_components(pve->src_format) - 1);
844 }
845
846 lima_dump_command_stream_print(
847 job->dump, attribute, n * 4, false, "update attribute info at va %x\n",
848 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_attribute_info));
849 }
850
851 static void
lima_update_gp_uniform(struct lima_context * ctx)852 lima_update_gp_uniform(struct lima_context *ctx)
853 {
854 struct lima_context_constant_buffer *ccb =
855 ctx->const_buffer + PIPE_SHADER_VERTEX;
856 struct lima_vs_compiled_shader *vs = ctx->vs;
857 int uniform_size = MIN2(vs->state.uniform_size, ccb->size);
858
859 int size = uniform_size + vs->state.constant_size + 32;
860 void *vs_const_buff =
861 lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_uniform, size);
862
863 if (ccb->buffer)
864 memcpy(vs_const_buff, ccb->buffer, uniform_size);
865
866 memcpy(vs_const_buff + uniform_size,
867 ctx->viewport.transform.scale,
868 sizeof(ctx->viewport.transform.scale));
869 memcpy(vs_const_buff + uniform_size + 16,
870 ctx->viewport.transform.translate,
871 sizeof(ctx->viewport.transform.translate));
872
873 if (vs->constant)
874 memcpy(vs_const_buff + uniform_size + 32,
875 vs->constant, vs->state.constant_size);
876
877 struct lima_job *job = lima_job_get(ctx);
878
879 if (lima_debug & LIMA_DEBUG_GP) {
880 float *vs_const_buff_f = vs_const_buff;
881 printf("gp uniforms:\n");
882 for (int i = 0; i < (size / sizeof(float)); i++) {
883 if ((i % 4) == 0)
884 printf("%4d:", i / 4);
885 printf(" %8.4f", vs_const_buff_f[i]);
886 if ((i % 4) == 3)
887 printf("\n");
888 }
889 printf("\n");
890 }
891
892 lima_dump_command_stream_print(
893 job->dump, vs_const_buff, size, true,
894 "update gp uniform at va %x\n",
895 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_uniform));
896 }
897
898 static void
lima_update_pp_uniform(struct lima_context * ctx)899 lima_update_pp_uniform(struct lima_context *ctx)
900 {
901 const float *const_buff = ctx->const_buffer[PIPE_SHADER_FRAGMENT].buffer;
902 size_t const_buff_size = ctx->const_buffer[PIPE_SHADER_FRAGMENT].size / sizeof(float);
903
904 if (!const_buff)
905 return;
906
907 uint16_t *fp16_const_buff =
908 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_uniform,
909 const_buff_size * sizeof(uint16_t));
910
911 uint32_t *array =
912 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_uniform_array, 4);
913
914 for (int i = 0; i < const_buff_size; i++)
915 fp16_const_buff[i] = _mesa_float_to_half(const_buff[i]);
916
917 *array = lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform);
918
919 struct lima_job *job = lima_job_get(ctx);
920
921 lima_dump_command_stream_print(
922 job->dump, fp16_const_buff, const_buff_size * 2,
923 false, "add pp uniform data at va %x\n",
924 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform));
925 lima_dump_command_stream_print(
926 job->dump, array, 4, false, "add pp uniform info at va %x\n",
927 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform_array));
928 }
929
930 static void
lima_update_varying(struct lima_context * ctx,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)931 lima_update_varying(struct lima_context *ctx, const struct pipe_draw_info *info,
932 const struct pipe_draw_start_count_bias *draw)
933 {
934 struct lima_job *job = lima_job_get(ctx);
935 struct lima_screen *screen = lima_screen(ctx->base.screen);
936 struct lima_vs_compiled_shader *vs = ctx->vs;
937 uint32_t gp_output_size;
938 unsigned num = info->index_size ? (ctx->max_index - ctx->min_index + 1) : draw->count;
939
940 uint32_t *varying =
941 lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_varying_info,
942 vs->state.num_outputs * 8);
943 int n = 0;
944
945 int offset = 0;
946
947 for (int i = 0; i < vs->state.num_outputs; i++) {
948 struct lima_varying_info *v = vs->state.varying + i;
949
950 if (i == vs->state.gl_pos_idx ||
951 i == vs->state.point_size_idx)
952 continue;
953
954 int size = v->component_size * 4;
955
956 /* does component_size == 2 need to be 16 aligned? */
957 if (v->component_size == 4)
958 offset = align(offset, 16);
959
960 v->offset = offset;
961 offset += size;
962 }
963
964 vs->state.varying_stride = align(offset, 16);
965
966 /* gl_Position is always present, allocate space for it */
967 gp_output_size = align(4 * 4 * num, 0x40);
968
969 /* Allocate space for varyings if there're any */
970 if (vs->state.num_varyings) {
971 ctx->gp_output_varyings_offt = gp_output_size;
972 gp_output_size += align(vs->state.varying_stride * num, 0x40);
973 }
974
975 /* Allocate space for gl_PointSize if it's there */
976 if (vs->state.point_size_idx != -1) {
977 ctx->gp_output_point_size_offt = gp_output_size;
978 gp_output_size += 4 * num;
979 }
980
981 /* gp_output can be too large for the suballocator, so create a
982 * separate bo for it. The bo cache should prevent performance hit.
983 */
984 ctx->gp_output = lima_bo_create(screen, gp_output_size, 0);
985 assert(ctx->gp_output);
986 lima_job_add_bo(job, LIMA_PIPE_GP, ctx->gp_output, LIMA_SUBMIT_BO_WRITE);
987 lima_job_add_bo(job, LIMA_PIPE_PP, ctx->gp_output, LIMA_SUBMIT_BO_READ);
988
989 for (int i = 0; i < vs->state.num_outputs; i++) {
990 struct lima_varying_info *v = vs->state.varying + i;
991
992 if (i == vs->state.gl_pos_idx) {
993 /* gl_Position */
994 varying[n++] = ctx->gp_output->va;
995 varying[n++] = 0x8020;
996 } else if (i == vs->state.point_size_idx) {
997 /* gl_PointSize */
998 varying[n++] = ctx->gp_output->va + ctx->gp_output_point_size_offt;
999 varying[n++] = 0x2021;
1000 } else {
1001 /* Varying */
1002 varying[n++] = ctx->gp_output->va + ctx->gp_output_varyings_offt +
1003 v->offset;
1004 varying[n++] = (vs->state.varying_stride << 11) | (v->components - 1) |
1005 (v->component_size == 2 ? 0x0C : 0);
1006 }
1007 }
1008
1009 lima_dump_command_stream_print(
1010 job->dump, varying, n * 4, false, "update varying info at va %x\n",
1011 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_varying_info));
1012 }
1013
1014 static void
lima_draw_vbo_update(struct pipe_context * pctx,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)1015 lima_draw_vbo_update(struct pipe_context *pctx,
1016 const struct pipe_draw_info *info,
1017 const struct pipe_draw_start_count_bias *draw)
1018 {
1019 struct lima_context *ctx = lima_context(pctx);
1020 struct lima_context_framebuffer *fb = &ctx->framebuffer;
1021 unsigned buffers = 0;
1022
1023 if (fb->base.zsbuf) {
1024 if (ctx->zsa->base.depth_enabled)
1025 buffers |= PIPE_CLEAR_DEPTH;
1026 if (ctx->zsa->base.stencil[0].enabled ||
1027 ctx->zsa->base.stencil[1].enabled)
1028 buffers |= PIPE_CLEAR_STENCIL;
1029 }
1030
1031 if (fb->base.nr_cbufs)
1032 buffers |= PIPE_CLEAR_COLOR0;
1033
1034 lima_update_job_wb(ctx, buffers);
1035
1036 lima_update_gp_attribute_info(ctx, info, draw);
1037
1038 if ((ctx->dirty & LIMA_CONTEXT_DIRTY_CONST_BUFF &&
1039 ctx->const_buffer[PIPE_SHADER_VERTEX].dirty) ||
1040 ctx->dirty & LIMA_CONTEXT_DIRTY_VIEWPORT ||
1041 ctx->dirty & LIMA_CONTEXT_DIRTY_COMPILED_VS) {
1042 lima_update_gp_uniform(ctx);
1043 ctx->const_buffer[PIPE_SHADER_VERTEX].dirty = false;
1044 }
1045
1046 lima_update_varying(ctx, info, draw);
1047
1048 lima_pack_vs_cmd(ctx, info, draw);
1049
1050 if (ctx->dirty & LIMA_CONTEXT_DIRTY_CONST_BUFF &&
1051 ctx->const_buffer[PIPE_SHADER_FRAGMENT].dirty) {
1052 lima_update_pp_uniform(ctx);
1053 ctx->const_buffer[PIPE_SHADER_FRAGMENT].dirty = false;
1054 }
1055
1056 lima_update_textures(ctx);
1057
1058 lima_pack_render_state(ctx, info);
1059 lima_pack_plbu_cmd(ctx, info, draw);
1060
1061 if (ctx->gp_output) {
1062 lima_bo_unreference(ctx->gp_output); /* held by job */
1063 ctx->gp_output = NULL;
1064 }
1065
1066 ctx->dirty = 0;
1067 }
1068
1069 static void
lima_draw_vbo_indexed(struct pipe_context * pctx,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)1070 lima_draw_vbo_indexed(struct pipe_context *pctx,
1071 const struct pipe_draw_info *info,
1072 const struct pipe_draw_start_count_bias *draw)
1073 {
1074 struct lima_context *ctx = lima_context(pctx);
1075 struct lima_job *job = lima_job_get(ctx);
1076 struct pipe_resource *indexbuf = NULL;
1077 bool needs_indices = true;
1078
1079 /* Mali Utgard GPU always need min/max index info for index draw,
1080 * compute it if upper layer does not do for us */
1081 if (info->index_bounds_valid) {
1082 ctx->min_index = info->min_index;
1083 ctx->max_index = info->max_index;
1084 needs_indices = false;
1085 }
1086
1087 if (info->has_user_indices) {
1088 util_upload_index_buffer(&ctx->base, info, draw, &indexbuf, &ctx->index_offset, 0x40);
1089 ctx->index_res = lima_resource(indexbuf);
1090 }
1091 else {
1092 ctx->index_res = lima_resource(info->index.resource);
1093 ctx->index_offset = 0;
1094 needs_indices = !panfrost_minmax_cache_get(ctx->index_res->index_cache, draw->start,
1095 draw->count, &ctx->min_index, &ctx->max_index);
1096 }
1097
1098 if (needs_indices) {
1099 u_vbuf_get_minmax_index(pctx, info, draw, &ctx->min_index, &ctx->max_index);
1100 if (!info->has_user_indices)
1101 panfrost_minmax_cache_add(ctx->index_res->index_cache, draw->start, draw->count,
1102 ctx->min_index, ctx->max_index);
1103 }
1104
1105 lima_job_add_bo(job, LIMA_PIPE_GP, ctx->index_res->bo, LIMA_SUBMIT_BO_READ);
1106 lima_job_add_bo(job, LIMA_PIPE_PP, ctx->index_res->bo, LIMA_SUBMIT_BO_READ);
1107 lima_draw_vbo_update(pctx, info, draw);
1108
1109 if (indexbuf)
1110 pipe_resource_reference(&indexbuf, NULL);
1111 }
1112
1113 static void
lima_draw_vbo_count(struct pipe_context * pctx,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)1114 lima_draw_vbo_count(struct pipe_context *pctx,
1115 const struct pipe_draw_info *info,
1116 const struct pipe_draw_start_count_bias *draw)
1117 {
1118 static const uint32_t max_verts = 65535;
1119
1120 struct pipe_draw_start_count_bias local_draw = *draw;
1121 unsigned start = draw->start;
1122 unsigned count = draw->count;
1123
1124 while (count) {
1125 unsigned this_count = count;
1126 unsigned step;
1127
1128 u_split_draw(info, max_verts, &this_count, &step);
1129
1130 local_draw.start = start;
1131 local_draw.count = this_count;
1132
1133 lima_draw_vbo_update(pctx, info, &local_draw);
1134
1135 count -= step;
1136 start += step;
1137 }
1138 }
1139
1140 static void
lima_draw_vbo(struct pipe_context * pctx,const struct pipe_draw_info * info,unsigned drawid_offset,const struct pipe_draw_indirect_info * indirect,const struct pipe_draw_start_count_bias * draws,unsigned num_draws)1141 lima_draw_vbo(struct pipe_context *pctx,
1142 const struct pipe_draw_info *info,
1143 unsigned drawid_offset,
1144 const struct pipe_draw_indirect_info *indirect,
1145 const struct pipe_draw_start_count_bias *draws,
1146 unsigned num_draws)
1147 {
1148 if (num_draws > 1) {
1149 util_draw_multi(pctx, info, drawid_offset, indirect, draws, num_draws);
1150 return;
1151 }
1152
1153 /* check if draw mode and vertex/index count match,
1154 * otherwise gp will hang */
1155 if (!u_trim_pipe_prim(info->mode, (unsigned*)&draws[0].count)) {
1156 debug_printf("draw mode and vertex/index count mismatch\n");
1157 return;
1158 }
1159
1160 struct lima_context *ctx = lima_context(pctx);
1161
1162 if (!ctx->uncomp_fs || !ctx->uncomp_vs) {
1163 debug_warn_once("no shader, skip draw\n");
1164 return;
1165 }
1166
1167 lima_clip_scissor_to_viewport(ctx);
1168 if (lima_is_scissor_zero(ctx))
1169 return;
1170
1171 /* extend the viewport in case of line draws with a line_width > 1.0f,
1172 * otherwise use the original values */
1173 lima_extend_viewport(ctx, info);
1174
1175 if (!lima_update_fs_state(ctx) || !lima_update_vs_state(ctx))
1176 return;
1177
1178 struct lima_job *job = lima_job_get(ctx);
1179 job->pp_max_stack_size = MAX2(job->pp_max_stack_size, ctx->fs->state.stack_size);
1180
1181 lima_dump_command_stream_print(
1182 job->dump, ctx->vs->bo->map, ctx->vs->state.shader_size, false,
1183 "add vs at va %x\n", ctx->vs->bo->va);
1184 lima_dump_shader(job->dump, ctx->vs->bo->map, ctx->vs->state.shader_size, false);
1185
1186 lima_dump_command_stream_print(
1187 job->dump, ctx->fs->bo->map, ctx->fs->state.shader_size, false,
1188 "add fs at va %x\n", ctx->fs->bo->va);
1189 lima_dump_shader(job->dump, ctx->fs->bo->map, ctx->fs->state.shader_size, true);
1190
1191 lima_job_add_bo(job, LIMA_PIPE_GP, ctx->vs->bo, LIMA_SUBMIT_BO_READ);
1192 lima_job_add_bo(job, LIMA_PIPE_PP, ctx->fs->bo, LIMA_SUBMIT_BO_READ);
1193
1194 if (info->index_size)
1195 lima_draw_vbo_indexed(pctx, info, &draws[0]);
1196 else
1197 lima_draw_vbo_count(pctx, info, &draws[0]);
1198
1199 job->draws++;
1200 /* Flush job if we hit the limit of draws per job otherwise we may
1201 * hit tile heap size limit */
1202 if (job->draws > MAX_DRAWS_PER_JOB) {
1203 unsigned resolve = job->resolve;
1204 lima_do_job(job);
1205 /* Subsequent job will need to resolve the same buffers */
1206 lima_update_job_wb(ctx, resolve);
1207 }
1208 }
1209
1210 void
lima_draw_init(struct lima_context * ctx)1211 lima_draw_init(struct lima_context *ctx)
1212 {
1213 ctx->base.clear = lima_clear;
1214 ctx->base.draw_vbo = lima_draw_vbo;
1215 }
1216