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