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