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