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