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