lima: add missing resolve check for damage and reload
[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/u_math.h"
27 #include "util/format/u_format.h"
28 #include "util/u_debug.h"
29 #include "util/u_half.h"
30 #include "util/u_helpers.h"
31 #include "util/u_inlines.h"
32 #include "util/u_pack_color.h"
33 #include "util/hash_table.h"
34 #include "util/u_split_draw.h"
35 #include "util/u_upload_mgr.h"
36 #include "util/u_prim.h"
37 #include "util/u_vbuf.h"
38
39 #include "lima_context.h"
40 #include "lima_screen.h"
41 #include "lima_resource.h"
42 #include "lima_program.h"
43 #include "lima_bo.h"
44 #include "lima_submit.h"
45 #include "lima_texture.h"
46 #include "lima_util.h"
47 #include "lima_fence.h"
48 #include "lima_format.h"
49
50 #include <drm-uapi/lima_drm.h>
51
52 struct lima_gp_frame_reg {
53 uint32_t vs_cmd_start;
54 uint32_t vs_cmd_end;
55 uint32_t plbu_cmd_start;
56 uint32_t plbu_cmd_end;
57 uint32_t tile_heap_start;
58 uint32_t tile_heap_end;
59 };
60
61 struct lima_pp_frame_reg {
62 uint32_t plbu_array_address;
63 uint32_t render_address;
64 uint32_t unused_0;
65 uint32_t flags;
66 uint32_t clear_value_depth;
67 uint32_t clear_value_stencil;
68 uint32_t clear_value_color;
69 uint32_t clear_value_color_1;
70 uint32_t clear_value_color_2;
71 uint32_t clear_value_color_3;
72 uint32_t width;
73 uint32_t height;
74 uint32_t fragment_stack_address;
75 uint32_t fragment_stack_size;
76 uint32_t unused_1;
77 uint32_t unused_2;
78 uint32_t one;
79 uint32_t supersampled_height;
80 uint32_t dubya;
81 uint32_t onscreen;
82 uint32_t blocking;
83 uint32_t scale;
84 uint32_t foureight;
85 };
86
87 struct lima_pp_wb_reg {
88 uint32_t type;
89 uint32_t address;
90 uint32_t pixel_format;
91 uint32_t downsample_factor;
92 uint32_t pixel_layout;
93 uint32_t pitch;
94 uint32_t mrt_bits;
95 uint32_t mrt_pitch;
96 uint32_t zero;
97 uint32_t unused0;
98 uint32_t unused1;
99 uint32_t unused2;
100 };
101
102 struct lima_render_state {
103 uint32_t blend_color_bg;
104 uint32_t blend_color_ra;
105 uint32_t alpha_blend;
106 uint32_t depth_test;
107 uint32_t depth_range;
108 uint32_t stencil_front;
109 uint32_t stencil_back;
110 uint32_t stencil_test;
111 uint32_t multi_sample;
112 uint32_t shader_address;
113 uint32_t varying_types;
114 uint32_t uniforms_address;
115 uint32_t textures_address;
116 uint32_t aux0;
117 uint32_t aux1;
118 uint32_t varyings_address;
119 };
120
121
122 /* plbu commands */
123 #define PLBU_CMD_BEGIN(array, max) { \
124 int i = 0, max_n = max; \
125 struct util_dynarray *plbu_cmd_array = array; \
126 uint32_t *plbu_cmd = util_dynarray_ensure_cap(plbu_cmd_array, plbu_cmd_array->size + max_n * 4);
127
128 #define PLBU_CMD_END() \
129 assert(i <= max_n); \
130 plbu_cmd_array->size += i * 4; \
131 }
132
133 #define PLBU_CMD(v1, v2) \
134 do { \
135 plbu_cmd[i++] = v1; \
136 plbu_cmd[i++] = v2; \
137 } while (0)
138
139 #define PLBU_CMD_BLOCK_STEP(shift_min, shift_h, shift_w) \
140 PLBU_CMD(((shift_min) << 28) | ((shift_h) << 16) | (shift_w), 0x1000010C)
141 #define PLBU_CMD_TILED_DIMENSIONS(tiled_w, tiled_h) \
142 PLBU_CMD((((tiled_w) - 1) << 24) | (((tiled_h) - 1) << 8), 0x10000109)
143 #define PLBU_CMD_BLOCK_STRIDE(block_w) PLBU_CMD((block_w) & 0xff, 0x30000000)
144 #define PLBU_CMD_ARRAY_ADDRESS(gp_stream, block_num) \
145 PLBU_CMD(gp_stream, 0x28000000 | ((block_num) - 1) | 1)
146 #define PLBU_CMD_VIEWPORT_LEFT(v) PLBU_CMD(v, 0x10000107)
147 #define PLBU_CMD_VIEWPORT_RIGHT(v) PLBU_CMD(v, 0x10000108)
148 #define PLBU_CMD_VIEWPORT_BOTTOM(v) PLBU_CMD(v, 0x10000105)
149 #define PLBU_CMD_VIEWPORT_TOP(v) PLBU_CMD(v, 0x10000106)
150 #define PLBU_CMD_ARRAYS_SEMAPHORE_BEGIN() PLBU_CMD(0x00010002, 0x60000000)
151 #define PLBU_CMD_ARRAYS_SEMAPHORE_END() PLBU_CMD(0x00010001, 0x60000000)
152 #define PLBU_CMD_PRIMITIVE_SETUP(force_point_size, cull, index_size) \
153 PLBU_CMD(0x2200 | ((force_point_size) ? 0x1000 : 0) | \
154 (cull) | ((index_size) << 9), 0x1000010B)
155 #define PLBU_CMD_RSW_VERTEX_ARRAY(rsw, gl_pos) \
156 PLBU_CMD(rsw, 0x80000000 | ((gl_pos) >> 4))
157 #define PLBU_CMD_SCISSORS(minx, maxx, miny, maxy) \
158 PLBU_CMD(((minx) << 30) | ((maxy) - 1) << 15 | (miny), \
159 0x70000000 | ((maxx) - 1) << 13 | ((minx) >> 2))
160 #define PLBU_CMD_UNKNOWN1() PLBU_CMD(0x00000000, 0x1000010A)
161 #define PLBU_CMD_UNKNOWN2() PLBU_CMD(0x00000200, 0x1000010B)
162 #define PLBU_CMD_LOW_PRIM_SIZE(v) PLBU_CMD(v, 0x1000010D)
163 #define PLBU_CMD_DEPTH_RANGE_NEAR(v) PLBU_CMD(v, 0x1000010E)
164 #define PLBU_CMD_DEPTH_RANGE_FAR(v) PLBU_CMD(v, 0x1000010F)
165 #define PLBU_CMD_INDEXED_DEST(gl_pos) PLBU_CMD(gl_pos, 0x10000100)
166 #define PLBU_CMD_INDEXED_PT_SIZE(pt_size) PLBU_CMD(pt_size, 0x10000102)
167 #define PLBU_CMD_INDICES(va) PLBU_CMD(va, 0x10000101)
168 #define PLBU_CMD_DRAW_ARRAYS(mode, start, count) \
169 PLBU_CMD(((count) << 24) | (start), (((mode) & 0x1F) << 16) | ((count) >> 8))
170 #define PLBU_CMD_DRAW_ELEMENTS(mode, start, count) \
171 PLBU_CMD(((count) << 24) | (start), \
172 0x00200000 | (((mode) & 0x1F) << 16) | ((count) >> 8))
173
174 /* vs commands */
175 #define VS_CMD_BEGIN(array, max) { \
176 int i = 0, max_n = max; \
177 struct util_dynarray *vs_cmd_array = array; \
178 uint32_t *vs_cmd = util_dynarray_ensure_cap(vs_cmd_array, vs_cmd_array->size + max_n * 4);
179
180 #define VS_CMD_END() \
181 assert(i <= max_n); \
182 vs_cmd_array->size += i * 4; \
183 }
184
185 #define VS_CMD(v1, v2) \
186 do { \
187 vs_cmd[i++] = v1; \
188 vs_cmd[i++] = v2; \
189 } while (0)
190
191 #define VS_CMD_ARRAYS_SEMAPHORE_BEGIN_1() VS_CMD(0x00028000, 0x50000000)
192 #define VS_CMD_ARRAYS_SEMAPHORE_BEGIN_2() VS_CMD(0x00000001, 0x50000000)
193 #define VS_CMD_ARRAYS_SEMAPHORE_END(index_draw) \
194 VS_CMD((index_draw) ? 0x00018000 : 0x00000000, 0x50000000)
195 #define VS_CMD_UNIFORMS_ADDRESS(addr, size) \
196 VS_CMD(addr, 0x30000000 | ((size) << 12))
197 #define VS_CMD_SHADER_ADDRESS(addr, size) \
198 VS_CMD(addr, 0x40000000 | ((size) << 12))
199 #define VS_CMD_SHADER_INFO(prefetch, size) \
200 VS_CMD(((prefetch) << 20) | ((((size) >> 4) - 1) << 10), 0x10000040)
201 #define VS_CMD_VARYING_ATTRIBUTE_COUNT(nv, na) \
202 VS_CMD((((nv) - 1) << 8) | (((na) - 1) << 24), 0x10000042)
203 #define VS_CMD_UNKNOWN1() VS_CMD(0x00000003, 0x10000041)
204 #define VS_CMD_UNKNOWN2() VS_CMD(0x00000000, 0x60000000)
205 #define VS_CMD_ATTRIBUTES_ADDRESS(addr, na) \
206 VS_CMD(addr, 0x20000000 | ((na) << 17))
207 #define VS_CMD_VARYINGS_ADDRESS(addr, nv) \
208 VS_CMD(addr, 0x20000008 | ((nv) << 17))
209 #define VS_CMD_DRAW(num, index_draw) \
210 VS_CMD(((num) << 24) | ((index_draw) ? 1 : 0), ((num) >> 8))
211
212 static inline bool
213 lima_ctx_dirty(struct lima_context *ctx)
214 {
215 return !!ctx->resolve;
216 }
217
218 static inline struct lima_damage_region *
219 lima_ctx_get_damage(struct lima_context *ctx)
220 {
221 if (!(ctx->framebuffer.base.nr_cbufs && (ctx->resolve & PIPE_CLEAR_COLOR0)))
222 return NULL;
223
224 struct lima_surface *surf = lima_surface(ctx->framebuffer.base.cbufs[0]);
225 struct lima_resource *res = lima_resource(surf->base.texture);
226 return &res->damage;
227 }
228
229 static bool
230 lima_fb_need_reload(struct lima_context *ctx)
231 {
232 /* Depth buffer is always discarded */
233 if (!(ctx->framebuffer.base.nr_cbufs && (ctx->resolve & PIPE_CLEAR_COLOR0)))
234 return false;
235
236 struct lima_surface *surf = lima_surface(ctx->framebuffer.base.cbufs[0]);
237 struct lima_resource *res = lima_resource(surf->base.texture);
238 if (res->damage.region) {
239 /* for EGL_KHR_partial_update, when EGL_EXT_buffer_age is enabled,
240 * we need to reload damage region, otherwise just want to reload
241 * the region not aligned to tile boundary */
242 //if (!res->damage.aligned)
243 // return true;
244 return true;
245 }
246 else if (surf->reload)
247 return true;
248
249 return false;
250 }
251
252 static void
253 lima_pack_reload_plbu_cmd(struct lima_context *ctx)
254 {
255 #define lima_reload_render_state_offset 0x0000
256 #define lima_reload_gl_pos_offset 0x0040
257 #define lima_reload_varying_offset 0x0080
258 #define lima_reload_tex_desc_offset 0x00c0
259 #define lima_reload_tex_array_offset 0x0100
260 #define lima_reload_buffer_size 0x0140
261
262 void *cpu;
263 unsigned offset;
264 struct pipe_resource *pres = NULL;
265 u_upload_alloc(ctx->uploader, 0, lima_reload_buffer_size,
266 0x40, &offset, &pres, &cpu);
267
268 struct lima_resource *res = lima_resource(pres);
269 uint32_t va = res->bo->va + offset;
270
271 struct lima_screen *screen = lima_screen(ctx->base.screen);
272
273 uint32_t reload_shader_first_instr_size =
274 ((uint32_t *)(screen->pp_buffer->map + pp_reload_program_offset))[0] & 0x1f;
275 uint32_t reload_shader_va = screen->pp_buffer->va + pp_reload_program_offset;
276
277 struct lima_render_state reload_render_state = {
278 .alpha_blend = 0xf03b1ad2,
279 .depth_test = 0x0000000e,
280 .depth_range = 0xffff0000,
281 .stencil_front = 0x00000007,
282 .stencil_back = 0x00000007,
283 .multi_sample = 0x0000f007,
284 .shader_address = reload_shader_va | reload_shader_first_instr_size,
285 .varying_types = 0x00000001,
286 .textures_address = va + lima_reload_tex_array_offset,
287 .aux0 = 0x00004021,
288 .varyings_address = va + lima_reload_varying_offset,
289 };
290 memcpy(cpu + lima_reload_render_state_offset, &reload_render_state,
291 sizeof(reload_render_state));
292
293 struct lima_context_framebuffer *fb = &ctx->framebuffer;
294 lima_tex_desc *td = cpu + lima_reload_tex_desc_offset;
295 memset(td, 0, lima_min_tex_desc_size);
296 lima_texture_desc_set_res(ctx, td, fb->base.cbufs[0]->texture, 0, 0);
297 td->unnorm_coords = 1;
298 td->texture_type = LIMA_TEXTURE_TYPE_2D;
299 td->min_img_filter_nearest = 1;
300 td->mag_img_filter_nearest = 1;
301 td->wrap_s_clamp_to_edge = 1;
302 td->wrap_t_clamp_to_edge = 1;
303 td->unknown_2_2 = 0x1;
304
305 uint32_t *ta = cpu + lima_reload_tex_array_offset;
306 ta[0] = va + lima_reload_tex_desc_offset;
307
308 float reload_gl_pos[] = {
309 fb->base.width, 0, 0, 1,
310 0, 0, 0, 1,
311 0, fb->base.height, 0, 1,
312 };
313 memcpy(cpu + lima_reload_gl_pos_offset, reload_gl_pos,
314 sizeof(reload_gl_pos));
315
316 float reload_varying[] = {
317 fb->base.width, 0, 0, 0,
318 0, fb->base.height, 0, 0,
319 };
320 memcpy(cpu + lima_reload_varying_offset, reload_varying,
321 sizeof(reload_varying));
322
323 lima_submit_add_bo(ctx->pp_submit, res->bo, LIMA_SUBMIT_BO_READ);
324 pipe_resource_reference(&pres, NULL);
325
326 PLBU_CMD_BEGIN(&ctx->plbu_cmd_head, 20);
327
328 PLBU_CMD_VIEWPORT_LEFT(0);
329 PLBU_CMD_VIEWPORT_RIGHT(fui(fb->base.width));
330 PLBU_CMD_VIEWPORT_BOTTOM(0);
331 PLBU_CMD_VIEWPORT_TOP(fui(fb->base.height));
332
333 PLBU_CMD_RSW_VERTEX_ARRAY(
334 va + lima_reload_render_state_offset,
335 va + lima_reload_gl_pos_offset);
336
337 PLBU_CMD_UNKNOWN2();
338 PLBU_CMD_UNKNOWN1();
339
340 PLBU_CMD_INDICES(screen->pp_buffer->va + pp_shared_index_offset);
341 PLBU_CMD_INDEXED_DEST(va + lima_reload_gl_pos_offset);
342 PLBU_CMD_DRAW_ELEMENTS(0xf, 0, 3);
343
344 PLBU_CMD_END();
345 }
346
347 static void
348 lima_pack_head_plbu_cmd(struct lima_context *ctx)
349 {
350 struct lima_context_framebuffer *fb = &ctx->framebuffer;
351
352 PLBU_CMD_BEGIN(&ctx->plbu_cmd_head, 10);
353
354 PLBU_CMD_UNKNOWN2();
355 PLBU_CMD_BLOCK_STEP(fb->shift_min, fb->shift_h, fb->shift_w);
356 PLBU_CMD_TILED_DIMENSIONS(fb->tiled_w, fb->tiled_h);
357 PLBU_CMD_BLOCK_STRIDE(fb->block_w);
358
359 PLBU_CMD_ARRAY_ADDRESS(
360 ctx->plb_gp_stream->va + ctx->plb_index * ctx->plb_gp_size,
361 fb->block_w * fb->block_h);
362
363 PLBU_CMD_END();
364
365 if (lima_fb_need_reload(ctx))
366 lima_pack_reload_plbu_cmd(ctx);
367 }
368
369 static bool
370 lima_is_scissor_zero(struct lima_context *ctx)
371 {
372 if (!ctx->rasterizer || !ctx->rasterizer->base.scissor)
373 return false;
374
375 struct pipe_scissor_state *scissor = &ctx->scissor;
376 return
377 scissor->minx == scissor->maxx
378 && scissor->miny == scissor->maxy;
379 }
380
381 static void
382 hilbert_rotate(int n, int *x, int *y, int rx, int ry)
383 {
384 if (ry == 0) {
385 if (rx == 1) {
386 *x = n-1 - *x;
387 *y = n-1 - *y;
388 }
389
390 /* Swap x and y */
391 int t = *x;
392 *x = *y;
393 *y = t;
394 }
395 }
396
397 static void
398 hilbert_coords(int n, int d, int *x, int *y)
399 {
400 int rx, ry, i, t=d;
401
402 *x = *y = 0;
403
404 for (i = 0; (1 << i) < n; i++) {
405
406 rx = 1 & (t / 2);
407 ry = 1 & (t ^ rx);
408
409 hilbert_rotate(1 << i, x, y, rx, ry);
410
411 *x += rx << i;
412 *y += ry << i;
413
414 t /= 4;
415 }
416 }
417
418 static int
419 lima_get_pp_stream_size(int num_pp, int tiled_w, int tiled_h, uint32_t *off)
420 {
421 /* carefully calculate each stream start address:
422 * 1. overflow: each stream size may be different due to
423 * fb->tiled_w * fb->tiled_h can't be divided by num_pp,
424 * extra size should be added to the preceeding stream
425 * 2. alignment: each stream address should be 0x20 aligned
426 */
427 int delta = tiled_w * tiled_h / num_pp * 16 + 16;
428 int remain = tiled_w * tiled_h % num_pp;
429 int offset = 0;
430
431 for (int i = 0; i < num_pp; i++) {
432 off[i] = offset;
433
434 offset += delta;
435 if (remain) {
436 offset += 16;
437 remain--;
438 }
439 offset = align(offset, 0x20);
440 }
441
442 return offset;
443 }
444
445 static bool
446 inside_damage_region(int x, int y, struct lima_damage_region *ds)
447 {
448 if (!ds || !ds->region)
449 return true;
450
451 for (int i = 0; i < ds->num_region; i++) {
452 struct pipe_scissor_state *ss = ds->region + i;
453 if (x >= ss->minx && x < ss->maxx &&
454 y >= ss->miny && y < ss->maxy)
455 return true;
456 }
457
458 return false;
459 }
460
461 static void
462 lima_generate_pp_stream(struct lima_context *ctx, int off_x, int off_y,
463 int tiled_w, int tiled_h)
464 {
465 struct lima_pp_stream_state *ps = &ctx->pp_stream;
466 struct lima_context_framebuffer *fb = &ctx->framebuffer;
467 struct lima_damage_region *damage = lima_ctx_get_damage(ctx);
468 struct lima_screen *screen = lima_screen(ctx->base.screen);
469 int i, num_pp = screen->num_pp;
470
471 /* use hilbert_coords to generates 1D to 2D relationship.
472 * 1D for pp stream index and 2D for plb block x/y on framebuffer.
473 * if multi pp, interleave the 1D index to make each pp's render target
474 * close enough which should result close workload
475 */
476 int max = MAX2(tiled_w, tiled_h);
477 int index = 0;
478 uint32_t *stream[4];
479 int si[4] = {0};
480 int dim = 0;
481 int count = 0;
482
483 /* Don't update count if we get zero rect. We'll just generate
484 * PP stream with just terminators in it.
485 */
486 if ((tiled_w * tiled_h) != 0) {
487 dim = util_logbase2_ceil(max);
488 count = 1 << (dim + dim);
489 }
490
491 for (i = 0; i < num_pp; i++)
492 stream[i] = ps->bo->map + ps->bo_offset + ps->offset[i];
493
494 for (i = 0; i < count; i++) {
495 int x, y;
496 hilbert_coords(max, i, &x, &y);
497 if (x < tiled_w && y < tiled_h) {
498 x += off_x;
499 y += off_y;
500
501 if (!inside_damage_region(x, y, damage))
502 continue;
503
504 int pp = index % num_pp;
505 int offset = ((y >> fb->shift_h) * fb->block_w +
506 (x >> fb->shift_w)) * LIMA_CTX_PLB_BLK_SIZE;
507 int plb_va = ctx->plb[ctx->plb_index]->va + offset;
508
509 stream[pp][si[pp]++] = 0;
510 stream[pp][si[pp]++] = 0xB8000000 | x | (y << 8);
511 stream[pp][si[pp]++] = 0xE0000002 | ((plb_va >> 3) & ~0xE0000003);
512 stream[pp][si[pp]++] = 0xB0000000;
513
514 index++;
515 }
516 }
517
518 for (i = 0; i < num_pp; i++) {
519 stream[i][si[i]++] = 0;
520 stream[i][si[i]++] = 0xBC000000;
521 stream[i][si[i]++] = 0;
522 stream[i][si[i]++] = 0;
523
524 lima_dump_command_stream_print(
525 stream[i], si[i] * 4, false, "pp plb stream %d at va %x\n",
526 i, ps->bo->va + ps->bo_offset + ps->offset[i]);
527 }
528 }
529
530 static void
531 lima_update_damage_pp_stream(struct lima_context *ctx)
532 {
533 struct lima_damage_region *ds = lima_ctx_get_damage(ctx);
534 struct lima_context_framebuffer *fb = &ctx->framebuffer;
535 struct pipe_scissor_state bound;
536
537 if (ds && ds->region) {
538 struct pipe_scissor_state *dbound = &ds->bound;
539 bound.minx = MAX2(dbound->minx, ctx->damage_rect.minx >> 4);
540 bound.miny = MAX2(dbound->miny, ctx->damage_rect.miny >> 4);
541 bound.maxx = MIN2(dbound->maxx, (ctx->damage_rect.maxx + 0xf) >> 4);
542 bound.maxy = MIN2(dbound->maxy, (ctx->damage_rect.maxy + 0xf) >> 4);
543 } else {
544 bound.minx = ctx->damage_rect.minx >> 4;
545 bound.miny = ctx->damage_rect.miny >> 4;
546 bound.maxx = (ctx->damage_rect.maxx + 0xf) >> 4;
547 bound.maxy = (ctx->damage_rect.maxy + 0xf) >> 4;
548 }
549
550 /* Clamp to FB size */
551 bound.minx = MIN2(bound.minx, fb->tiled_w);
552 bound.miny = MIN2(bound.miny, fb->tiled_h);
553 bound.maxx = MIN2(bound.maxx, fb->tiled_w);
554 bound.maxy = MIN2(bound.maxy, fb->tiled_h);
555
556 int tiled_w = bound.maxx - bound.minx;
557 int tiled_h = bound.maxy - bound.miny;
558
559 struct lima_screen *screen = lima_screen(ctx->base.screen);
560 int size = lima_get_pp_stream_size(
561 screen->num_pp, tiled_w, tiled_h, ctx->pp_stream.offset);
562
563 void *cpu;
564 unsigned offset;
565 struct pipe_resource *pres = NULL;
566 u_upload_alloc(ctx->uploader, 0, size, 0x40, &offset, &pres, &cpu);
567
568 struct lima_resource *res = lima_resource(pres);
569 ctx->pp_stream.bo = res->bo;
570 ctx->pp_stream.bo_offset = offset;
571
572 lima_generate_pp_stream(ctx, bound.minx, bound.miny, tiled_w, tiled_h);
573
574 lima_submit_add_bo(ctx->pp_submit, res->bo, LIMA_SUBMIT_BO_READ);
575 pipe_resource_reference(&pres, NULL);
576 }
577
578 static void
579 lima_update_full_pp_stream(struct lima_context *ctx)
580 {
581 struct lima_context_framebuffer *fb = &ctx->framebuffer;
582 struct lima_ctx_plb_pp_stream_key key = {
583 .plb_index = ctx->plb_index,
584 .tiled_w = fb->tiled_w,
585 .tiled_h = fb->tiled_h,
586 };
587
588 struct hash_entry *entry =
589 _mesa_hash_table_search(ctx->plb_pp_stream, &key);
590 struct lima_ctx_plb_pp_stream *s = entry->data;
591
592 if (s->bo) {
593 ctx->pp_stream.bo = s->bo;
594 ctx->pp_stream.bo_offset = 0;
595 memcpy(ctx->pp_stream.offset, s->offset, sizeof(s->offset));
596 }
597 else {
598 struct lima_screen *screen = lima_screen(ctx->base.screen);
599 int size = lima_get_pp_stream_size(
600 screen->num_pp, fb->tiled_w, fb->tiled_h, s->offset);
601 s->bo = lima_bo_create(screen, size, 0);
602 lima_bo_map(s->bo);
603
604 ctx->pp_stream.bo = s->bo;
605 ctx->pp_stream.bo_offset = 0;
606 memcpy(ctx->pp_stream.offset, s->offset, sizeof(s->offset));
607
608 lima_generate_pp_stream(ctx, 0, 0, fb->tiled_w, fb->tiled_h);
609 }
610
611 lima_submit_add_bo(ctx->pp_submit, s->bo, LIMA_SUBMIT_BO_READ);
612 }
613
614 static bool
615 lima_damage_fullscreen(struct lima_context *ctx)
616 {
617 return ctx->damage_rect.minx == 0 &&
618 ctx->damage_rect.miny == 0 &&
619 ctx->damage_rect.maxx == ctx->framebuffer.base.width &&
620 ctx->damage_rect.maxy == ctx->framebuffer.base.height;
621 }
622
623 static void
624 lima_update_pp_stream(struct lima_context *ctx)
625 {
626 struct lima_damage_region *damage = lima_ctx_get_damage(ctx);
627 if ((damage && damage->region) || !lima_damage_fullscreen(ctx))
628 lima_update_damage_pp_stream(ctx);
629 else if (ctx->plb_pp_stream)
630 lima_update_full_pp_stream(ctx);
631 else
632 ctx->pp_stream.bo = NULL;
633 }
634
635 static void
636 lima_update_submit_wb(struct lima_context *ctx, unsigned buffers)
637 {
638 struct lima_context_framebuffer *fb = &ctx->framebuffer;
639
640 /* add to submit when the buffer is dirty and resolve is clear (not added before) */
641 if (fb->base.nr_cbufs && (buffers & PIPE_CLEAR_COLOR0) &&
642 !(ctx->resolve & PIPE_CLEAR_COLOR0)) {
643 struct lima_resource *res = lima_resource(fb->base.cbufs[0]->texture);
644 lima_submit_add_bo(ctx->pp_submit, res->bo, LIMA_SUBMIT_BO_WRITE);
645 }
646
647 /* add to submit when the buffer is dirty and resolve is clear (not added before) */
648 if (fb->base.zsbuf && (buffers & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) &&
649 !(ctx->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
650 struct lima_resource *res = lima_resource(fb->base.zsbuf->texture);
651 lima_submit_add_bo(ctx->pp_submit, res->bo, LIMA_SUBMIT_BO_WRITE);
652 }
653
654 ctx->resolve |= buffers;
655 }
656
657 static void
658 lima_update_submit_bo(struct lima_context *ctx)
659 {
660 lima_submit_add_bo(ctx->gp_submit, ctx->plb_gp_stream, LIMA_SUBMIT_BO_READ);
661 lima_submit_add_bo(ctx->gp_submit, ctx->plb[ctx->plb_index], LIMA_SUBMIT_BO_WRITE);
662 lima_submit_add_bo(ctx->gp_submit, ctx->gp_tile_heap[ctx->plb_index], LIMA_SUBMIT_BO_WRITE);
663
664 lima_dump_command_stream_print(
665 ctx->plb_gp_stream->map + ctx->plb_index * ctx->plb_gp_size,
666 ctx->plb_gp_size, false, "gp plb stream at va %x\n",
667 ctx->plb_gp_stream->va + ctx->plb_index * ctx->plb_gp_size);
668
669 lima_submit_add_bo(ctx->pp_submit, ctx->plb[ctx->plb_index], LIMA_SUBMIT_BO_READ);
670 lima_submit_add_bo(ctx->pp_submit, ctx->gp_tile_heap[ctx->plb_index], LIMA_SUBMIT_BO_READ);
671
672 struct lima_screen *screen = lima_screen(ctx->base.screen);
673 lima_submit_add_bo(ctx->pp_submit, screen->pp_buffer, LIMA_SUBMIT_BO_READ);
674 }
675
676 static void
677 lima_damage_rect_union(struct lima_context *ctx, unsigned minx, unsigned maxx, unsigned miny, unsigned maxy)
678 {
679 ctx->damage_rect.minx = MIN2(ctx->damage_rect.minx, minx);
680 ctx->damage_rect.miny = MIN2(ctx->damage_rect.miny, miny);
681 ctx->damage_rect.maxx = MAX2(ctx->damage_rect.maxx, maxx);
682 ctx->damage_rect.maxy = MAX2(ctx->damage_rect.maxy, maxy);
683 }
684
685 static void
686 lima_clear(struct pipe_context *pctx, unsigned buffers,
687 const union pipe_color_union *color, double depth, unsigned stencil)
688 {
689 struct lima_context *ctx = lima_context(pctx);
690
691 lima_flush(ctx);
692
693 lima_update_submit_wb(ctx, buffers);
694
695 /* no need to reload if cleared */
696 if (ctx->framebuffer.base.nr_cbufs && (buffers & PIPE_CLEAR_COLOR0)) {
697 struct lima_surface *surf = lima_surface(ctx->framebuffer.base.cbufs[0]);
698 surf->reload = false;
699 }
700
701 struct lima_context_clear *clear = &ctx->clear;
702 clear->buffers = buffers;
703
704 if (buffers & PIPE_CLEAR_COLOR0) {
705 clear->color_8pc =
706 ((uint32_t)float_to_ubyte(color->f[3]) << 24) |
707 ((uint32_t)float_to_ubyte(color->f[2]) << 16) |
708 ((uint32_t)float_to_ubyte(color->f[1]) << 8) |
709 float_to_ubyte(color->f[0]);
710
711 clear->color_16pc =
712 ((uint64_t)float_to_ushort(color->f[3]) << 48) |
713 ((uint64_t)float_to_ushort(color->f[2]) << 32) |
714 ((uint64_t)float_to_ushort(color->f[1]) << 16) |
715 float_to_ushort(color->f[0]);
716 }
717
718 if (buffers & PIPE_CLEAR_DEPTH)
719 clear->depth = util_pack_z(PIPE_FORMAT_Z24X8_UNORM, depth);
720
721 if (buffers & PIPE_CLEAR_STENCIL)
722 clear->stencil = stencil;
723
724 ctx->dirty |= LIMA_CONTEXT_DIRTY_CLEAR;
725
726 lima_damage_rect_union(ctx, 0, ctx->framebuffer.base.width,
727 0, ctx->framebuffer.base.height);
728 }
729
730 enum lima_attrib_type {
731 LIMA_ATTRIB_FLOAT = 0x000,
732 /* todo: find out what lives here. */
733 LIMA_ATTRIB_I16 = 0x004,
734 LIMA_ATTRIB_U16 = 0x005,
735 LIMA_ATTRIB_I8 = 0x006,
736 LIMA_ATTRIB_U8 = 0x007,
737 LIMA_ATTRIB_I8N = 0x008,
738 LIMA_ATTRIB_U8N = 0x009,
739 LIMA_ATTRIB_I16N = 0x00A,
740 LIMA_ATTRIB_U16N = 0x00B,
741 /* todo: where is the 32 int */
742 /* todo: find out what lives here. */
743 LIMA_ATTRIB_FIXED = 0x101
744 };
745
746 static enum lima_attrib_type
747 lima_pipe_format_to_attrib_type(enum pipe_format format)
748 {
749 const struct util_format_description *desc = util_format_description(format);
750 int i = util_format_get_first_non_void_channel(format);
751 const struct util_format_channel_description *c = desc->channel + i;
752
753 switch (c->type) {
754 case UTIL_FORMAT_TYPE_FLOAT:
755 return LIMA_ATTRIB_FLOAT;
756 case UTIL_FORMAT_TYPE_FIXED:
757 return LIMA_ATTRIB_FIXED;
758 case UTIL_FORMAT_TYPE_SIGNED:
759 if (c->size == 8) {
760 if (c->normalized)
761 return LIMA_ATTRIB_I8N;
762 else
763 return LIMA_ATTRIB_I8;
764 }
765 else if (c->size == 16) {
766 if (c->normalized)
767 return LIMA_ATTRIB_I16N;
768 else
769 return LIMA_ATTRIB_I16;
770 }
771 break;
772 case UTIL_FORMAT_TYPE_UNSIGNED:
773 if (c->size == 8) {
774 if (c->normalized)
775 return LIMA_ATTRIB_U8N;
776 else
777 return LIMA_ATTRIB_U8;
778 }
779 else if (c->size == 16) {
780 if (c->normalized)
781 return LIMA_ATTRIB_U16N;
782 else
783 return LIMA_ATTRIB_U16;
784 }
785 break;
786 }
787
788 return LIMA_ATTRIB_FLOAT;
789 }
790
791 static void
792 lima_pack_vs_cmd(struct lima_context *ctx, const struct pipe_draw_info *info)
793 {
794 VS_CMD_BEGIN(&ctx->vs_cmd_array, 24);
795
796 if (!info->index_size) {
797 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_1();
798 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_2();
799 }
800
801 int uniform_size = ctx->vs->uniform_pending_offset + ctx->vs->constant_size + 32;
802 VS_CMD_UNIFORMS_ADDRESS(
803 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_uniform),
804 align(uniform_size, 16));
805
806 VS_CMD_SHADER_ADDRESS(ctx->vs->bo->va, ctx->vs->shader_size);
807 VS_CMD_SHADER_INFO(ctx->vs->prefetch, ctx->vs->shader_size);
808
809 int num_outputs = ctx->vs->num_outputs;
810 int num_attributes = ctx->vertex_elements->num_elements;
811 VS_CMD_VARYING_ATTRIBUTE_COUNT(num_outputs, MAX2(1, num_attributes));
812
813 VS_CMD_UNKNOWN1();
814
815 VS_CMD_ATTRIBUTES_ADDRESS(
816 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_attribute_info),
817 MAX2(1, num_attributes));
818
819 VS_CMD_VARYINGS_ADDRESS(
820 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_varying_info),
821 num_outputs);
822
823 unsigned num = info->index_size ? (ctx->max_index - ctx->min_index + 1) : info->count;
824 VS_CMD_DRAW(num, info->index_size);
825
826 VS_CMD_UNKNOWN2();
827
828 VS_CMD_ARRAYS_SEMAPHORE_END(info->index_size);
829
830 VS_CMD_END();
831 }
832
833 static void
834 lima_pack_plbu_cmd(struct lima_context *ctx, const struct pipe_draw_info *info)
835 {
836 struct lima_context_framebuffer *fb = &ctx->framebuffer;
837 struct lima_vs_shader_state *vs = ctx->vs;
838 unsigned minx, maxx, miny, maxy;
839
840 /* If it's zero scissor, we skip adding all other commands */
841 if (lima_is_scissor_zero(ctx))
842 return;
843
844 PLBU_CMD_BEGIN(&ctx->plbu_cmd_array, 32);
845
846 PLBU_CMD_VIEWPORT_LEFT(fui(ctx->viewport.left));
847 PLBU_CMD_VIEWPORT_RIGHT(fui(ctx->viewport.right));
848 PLBU_CMD_VIEWPORT_BOTTOM(fui(ctx->viewport.bottom));
849 PLBU_CMD_VIEWPORT_TOP(fui(ctx->viewport.top));
850
851 if (!info->index_size)
852 PLBU_CMD_ARRAYS_SEMAPHORE_BEGIN();
853
854 int cf = ctx->rasterizer->base.cull_face;
855 int ccw = ctx->rasterizer->base.front_ccw;
856 uint32_t cull = 0;
857 bool force_point_size = false;
858
859 if (cf != PIPE_FACE_NONE) {
860 if (cf & PIPE_FACE_FRONT)
861 cull |= ccw ? 0x00040000 : 0x00020000;
862 if (cf & PIPE_FACE_BACK)
863 cull |= ccw ? 0x00020000 : 0x00040000;
864 }
865
866 /* Specify point size with PLBU command if shader doesn't write */
867 if (info->mode == PIPE_PRIM_POINTS && ctx->vs->point_size_idx == -1)
868 force_point_size = true;
869
870 /* Specify line width with PLBU command for lines */
871 if (info->mode > PIPE_PRIM_POINTS && info->mode < PIPE_PRIM_TRIANGLES)
872 force_point_size = true;
873
874 PLBU_CMD_PRIMITIVE_SETUP(force_point_size, cull, info->index_size);
875
876 PLBU_CMD_RSW_VERTEX_ARRAY(
877 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw),
878 ctx->gp_output->va);
879
880 /* TODO
881 * - we should set it only for the first draw that enabled the scissor and for
882 * latter draw only if scissor is dirty
883 */
884 if (ctx->rasterizer->base.scissor) {
885 struct pipe_scissor_state *scissor = &ctx->scissor;
886 minx = scissor->minx;
887 maxx = scissor->maxx;
888 miny = scissor->miny;
889 maxy = scissor->maxy;
890 } else {
891 minx = 0;
892 maxx = fb->base.width;
893 miny = 0;
894 maxy = fb->base.height;
895 }
896
897 minx = MAX2(minx, ctx->viewport.left);
898 maxx = MIN2(maxx, ctx->viewport.right);
899 miny = MAX2(miny, ctx->viewport.bottom);
900 maxy = MIN2(maxy, ctx->viewport.top);
901
902 PLBU_CMD_SCISSORS(minx, maxx, miny, maxy);
903 lima_damage_rect_union(ctx, minx, maxx, miny, maxy);
904
905 PLBU_CMD_UNKNOWN1();
906
907 PLBU_CMD_DEPTH_RANGE_NEAR(fui(ctx->viewport.near));
908 PLBU_CMD_DEPTH_RANGE_FAR(fui(ctx->viewport.far));
909
910 if ((info->mode == PIPE_PRIM_POINTS && ctx->vs->point_size_idx == -1) ||
911 ((info->mode >= PIPE_PRIM_LINES) && (info->mode < PIPE_PRIM_TRIANGLES)))
912 {
913 uint32_t v = info->mode == PIPE_PRIM_POINTS ?
914 fui(ctx->rasterizer->base.point_size) : fui(ctx->rasterizer->base.line_width);
915 PLBU_CMD_LOW_PRIM_SIZE(v);
916 }
917
918 if (info->index_size) {
919 PLBU_CMD_INDEXED_DEST(ctx->gp_output->va);
920 if (vs->point_size_idx != -1)
921 PLBU_CMD_INDEXED_PT_SIZE(ctx->gp_output->va + ctx->gp_output_point_size_offt);
922
923 PLBU_CMD_INDICES(ctx->index_res->bo->va + info->start * info->index_size + ctx->index_offset);
924 }
925 else {
926 /* can this make the attribute info static? */
927 PLBU_CMD_DRAW_ARRAYS(info->mode, info->start, info->count);
928 }
929
930 PLBU_CMD_ARRAYS_SEMAPHORE_END();
931
932 if (info->index_size)
933 PLBU_CMD_DRAW_ELEMENTS(info->mode, ctx->min_index, info->count);
934
935 PLBU_CMD_END();
936 }
937
938 static int
939 lima_blend_func(enum pipe_blend_func pipe)
940 {
941 switch (pipe) {
942 case PIPE_BLEND_ADD:
943 return 2;
944 case PIPE_BLEND_SUBTRACT:
945 return 0;
946 case PIPE_BLEND_REVERSE_SUBTRACT:
947 return 1;
948 case PIPE_BLEND_MIN:
949 return 4;
950 case PIPE_BLEND_MAX:
951 return 5;
952 }
953 return -1;
954 }
955
956 static int
957 lima_blend_factor_has_alpha(enum pipe_blendfactor pipe)
958 {
959 /* Bit 4 is set if the blendfactor uses alpha */
960 switch (pipe) {
961 case PIPE_BLENDFACTOR_SRC_ALPHA:
962 case PIPE_BLENDFACTOR_DST_ALPHA:
963 case PIPE_BLENDFACTOR_CONST_ALPHA:
964 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
965 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
966 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
967 return 1;
968
969 case PIPE_BLENDFACTOR_SRC_COLOR:
970 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
971 case PIPE_BLENDFACTOR_DST_COLOR:
972 case PIPE_BLENDFACTOR_INV_DST_COLOR:
973 case PIPE_BLENDFACTOR_CONST_COLOR:
974 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
975 case PIPE_BLENDFACTOR_ZERO:
976 case PIPE_BLENDFACTOR_ONE:
977 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
978 return 0;
979
980 case PIPE_BLENDFACTOR_SRC1_COLOR:
981 case PIPE_BLENDFACTOR_SRC1_ALPHA:
982 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
983 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
984 return -1; /* not supported */
985 }
986 return -1;
987 }
988
989 static int
990 lima_blend_factor_is_inv(enum pipe_blendfactor pipe)
991 {
992 /* Bit 3 is set if the blendfactor type is inverted */
993 switch (pipe) {
994 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
995 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
996 case PIPE_BLENDFACTOR_INV_DST_COLOR:
997 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
998 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
999 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
1000 case PIPE_BLENDFACTOR_ONE:
1001 return 1;
1002
1003 case PIPE_BLENDFACTOR_SRC_COLOR:
1004 case PIPE_BLENDFACTOR_SRC_ALPHA:
1005 case PIPE_BLENDFACTOR_DST_COLOR:
1006 case PIPE_BLENDFACTOR_DST_ALPHA:
1007 case PIPE_BLENDFACTOR_CONST_COLOR:
1008 case PIPE_BLENDFACTOR_CONST_ALPHA:
1009 case PIPE_BLENDFACTOR_ZERO:
1010 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
1011 return 0;
1012
1013 case PIPE_BLENDFACTOR_SRC1_COLOR:
1014 case PIPE_BLENDFACTOR_SRC1_ALPHA:
1015 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
1016 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
1017 return -1; /* not supported */
1018 }
1019 return -1;
1020 }
1021
1022 static int
1023 lima_blend_factor(enum pipe_blendfactor pipe)
1024 {
1025 /* Bits 0-2 indicate the blendfactor type */
1026 switch (pipe) {
1027 case PIPE_BLENDFACTOR_SRC_COLOR:
1028 case PIPE_BLENDFACTOR_SRC_ALPHA:
1029 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
1030 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
1031 return 0;
1032
1033 case PIPE_BLENDFACTOR_DST_COLOR:
1034 case PIPE_BLENDFACTOR_DST_ALPHA:
1035 case PIPE_BLENDFACTOR_INV_DST_COLOR:
1036 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
1037 return 1;
1038
1039 case PIPE_BLENDFACTOR_CONST_COLOR:
1040 case PIPE_BLENDFACTOR_CONST_ALPHA:
1041 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
1042 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
1043 return 2;
1044
1045 case PIPE_BLENDFACTOR_ZERO:
1046 case PIPE_BLENDFACTOR_ONE:
1047 return 3;
1048
1049 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
1050 return 4;
1051
1052 case PIPE_BLENDFACTOR_SRC1_COLOR:
1053 case PIPE_BLENDFACTOR_SRC1_ALPHA:
1054 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
1055 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
1056 return -1; /* not supported */
1057 }
1058 return -1;
1059 }
1060
1061 static int
1062 lima_calculate_alpha_blend(enum pipe_blend_func rgb_func, enum pipe_blend_func alpha_func,
1063 enum pipe_blendfactor rgb_src_factor, enum pipe_blendfactor rgb_dst_factor,
1064 enum pipe_blendfactor alpha_src_factor, enum pipe_blendfactor alpha_dst_factor)
1065 {
1066 /* PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE has to be changed to PIPE_BLENDFACTOR_ONE
1067 * if it is set for alpha_src.
1068 */
1069 if (alpha_src_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE)
1070 alpha_src_factor = PIPE_BLENDFACTOR_ONE;
1071
1072 return lima_blend_func(rgb_func) |
1073 (lima_blend_func(alpha_func) << 3) |
1074
1075 (lima_blend_factor(rgb_src_factor) << 6) |
1076 (lima_blend_factor_is_inv(rgb_src_factor) << 9) |
1077 (lima_blend_factor_has_alpha(rgb_src_factor) << 10) |
1078
1079 (lima_blend_factor(rgb_dst_factor) << 11) |
1080 (lima_blend_factor_is_inv(rgb_dst_factor) << 14) |
1081 (lima_blend_factor_has_alpha(rgb_dst_factor) << 15) |
1082
1083 (lima_blend_factor(alpha_src_factor) << 16) |
1084 (lima_blend_factor_is_inv(alpha_src_factor) << 19) |
1085
1086 (lima_blend_factor(alpha_dst_factor) << 20) |
1087 (lima_blend_factor_is_inv(alpha_dst_factor) << 23) |
1088 0x0C000000; /* need to check if this is GLESv1 glAlphaFunc */
1089 }
1090
1091 static int
1092 lima_stencil_op(enum pipe_stencil_op pipe)
1093 {
1094 switch (pipe) {
1095 case PIPE_STENCIL_OP_KEEP:
1096 return 0;
1097 case PIPE_STENCIL_OP_ZERO:
1098 return 2;
1099 case PIPE_STENCIL_OP_REPLACE:
1100 return 1;
1101 case PIPE_STENCIL_OP_INCR:
1102 return 6;
1103 case PIPE_STENCIL_OP_DECR:
1104 return 7;
1105 case PIPE_STENCIL_OP_INCR_WRAP:
1106 return 4;
1107 case PIPE_STENCIL_OP_DECR_WRAP:
1108 return 5;
1109 case PIPE_STENCIL_OP_INVERT:
1110 return 3;
1111 }
1112 return -1;
1113 }
1114
1115 static unsigned
1116 lima_calculate_depth_test(struct pipe_depth_state *depth, struct pipe_rasterizer_state *rst)
1117 {
1118 int offset_scale = 0, offset_units = 0;
1119 enum pipe_compare_func func = (depth->enabled ? depth->func : PIPE_FUNC_ALWAYS);
1120
1121 offset_scale = CLAMP(rst->offset_scale * 4, -128, 127);
1122 if (offset_scale < 0)
1123 offset_scale += 0x100;
1124
1125 offset_units = CLAMP(rst->offset_units * 2, -128, 127);
1126 if (offset_units < 0)
1127 offset_units += 0x100;
1128
1129 return (depth->enabled && depth->writemask) |
1130 ((int)func << 1) |
1131 (offset_scale << 16) |
1132 (offset_units << 24) |
1133 0x30; /* find out what is this */
1134 }
1135
1136 static void
1137 lima_pack_render_state(struct lima_context *ctx, const struct pipe_draw_info *info)
1138 {
1139 struct lima_fs_shader_state *fs = ctx->fs;
1140 struct lima_render_state *render =
1141 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_plb_rsw,
1142 sizeof(*render));
1143 bool early_z = true;
1144 bool pixel_kill = true;
1145
1146 /* do hw support RGBA independ blend?
1147 * PIPE_CAP_INDEP_BLEND_ENABLE
1148 *
1149 * how to handle the no cbuf only zbuf case?
1150 */
1151 struct pipe_rt_blend_state *rt = ctx->blend->base.rt;
1152 render->blend_color_bg = float_to_ubyte(ctx->blend_color.color[2]) |
1153 (float_to_ubyte(ctx->blend_color.color[1]) << 16);
1154 render->blend_color_ra = float_to_ubyte(ctx->blend_color.color[0]) |
1155 (float_to_ubyte(ctx->blend_color.color[3]) << 16);
1156
1157 if (rt->blend_enable) {
1158 render->alpha_blend = lima_calculate_alpha_blend(rt->rgb_func, rt->alpha_func,
1159 rt->rgb_src_factor, rt->rgb_dst_factor,
1160 rt->alpha_src_factor, rt->alpha_dst_factor);
1161 }
1162 else {
1163 /*
1164 * Special handling for blending disabled.
1165 * Binary driver is generating the same alpha_value,
1166 * as when we would just enable blending, without changing/setting any blend equation/params.
1167 * Normaly in this case mesa would set all rt fields (func/factor) to zero.
1168 */
1169 render->alpha_blend = lima_calculate_alpha_blend(PIPE_BLEND_ADD, PIPE_BLEND_ADD,
1170 PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ZERO,
1171 PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ZERO);
1172 }
1173
1174 render->alpha_blend |= (rt->colormask & PIPE_MASK_RGBA) << 28;
1175
1176 struct pipe_rasterizer_state *rst = &ctx->rasterizer->base;
1177 struct pipe_depth_state *depth = &ctx->zsa->base.depth;
1178 render->depth_test = lima_calculate_depth_test(depth, rst);
1179
1180 ushort far, near;
1181
1182 near = float_to_ushort(ctx->viewport.near);
1183 far = float_to_ushort(ctx->viewport.far);
1184
1185 /* Subtract epsilon from 'near' if far == near. Make sure we don't get overflow */
1186 if ((far == near) && (near != 0))
1187 near--;
1188
1189 /* overlap with plbu? any place can remove one? */
1190 render->depth_range = near | (far << 16);
1191
1192 struct pipe_stencil_state *stencil = ctx->zsa->base.stencil;
1193 struct pipe_stencil_ref *ref = &ctx->stencil_ref;
1194
1195 if (stencil[0].enabled) { /* stencil is enabled */
1196 render->stencil_front = stencil[0].func |
1197 (lima_stencil_op(stencil[0].fail_op) << 3) |
1198 (lima_stencil_op(stencil[0].zfail_op) << 6) |
1199 (lima_stencil_op(stencil[0].zpass_op) << 9) |
1200 (ref->ref_value[0] << 16) |
1201 (stencil[0].valuemask << 24);
1202 render->stencil_back = render->stencil_front;
1203 render->stencil_test = (stencil[0].writemask & 0xff) | (stencil[0].writemask & 0xff) << 8;
1204 if (stencil[1].enabled) { /* two-side is enabled */
1205 render->stencil_back = stencil[1].func |
1206 (lima_stencil_op(stencil[1].fail_op) << 3) |
1207 (lima_stencil_op(stencil[1].zfail_op) << 6) |
1208 (lima_stencil_op(stencil[1].zpass_op) << 9) |
1209 (ref->ref_value[1] << 16) |
1210 (stencil[1].valuemask << 24);
1211 render->stencil_test = (stencil[0].writemask & 0xff) | (stencil[1].writemask & 0xff) << 8;
1212 }
1213 /* TODO: Find out, what (render->stecil_test & 0xffff0000) is.
1214 * 0x00ff0000 is probably (float_to_ubyte(alpha->ref_value) << 16)
1215 * (render->multi_sample & 0x00000007 is probably the compare function
1216 * of glAlphaFunc then.
1217 */
1218 }
1219 else {
1220 /* Default values, when stencil is disabled:
1221 * stencil[0|1].valuemask = 0xff
1222 * stencil[0|1].func = PIPE_FUNC_ALWAYS
1223 * stencil[0|1].writemask = 0xff
1224 */
1225 render->stencil_front = 0xff000007;
1226 render->stencil_back = 0xff000007;
1227 render->stencil_test = 0x0000ffff;
1228 }
1229
1230 /* need more investigation */
1231 if (info->mode == PIPE_PRIM_POINTS)
1232 render->multi_sample = 0x0000F007;
1233 else if (info->mode < PIPE_PRIM_TRIANGLES)
1234 render->multi_sample = 0x0000F407;
1235 else
1236 render->multi_sample = 0x0000F807;
1237 if (ctx->framebuffer.base.samples)
1238 render->multi_sample |= 0x68;
1239
1240 render->shader_address =
1241 ctx->fs->bo->va | (((uint32_t *)ctx->fs->bo->map)[0] & 0x1F);
1242
1243 /* seems not needed */
1244 render->uniforms_address = 0x00000000;
1245
1246 render->textures_address = 0x00000000;
1247
1248 render->aux0 = (ctx->vs->varying_stride >> 3);
1249 render->aux1 = 0x00001000;
1250 if (ctx->blend->base.dither)
1251 render->aux1 |= 0x00002000;
1252
1253 if (fs->uses_discard) {
1254 early_z = false;
1255 pixel_kill = false;
1256 }
1257
1258 if (rt->blend_enable)
1259 pixel_kill = false;
1260
1261 if ((rt->colormask & PIPE_MASK_RGBA) != PIPE_MASK_RGBA)
1262 pixel_kill = false;
1263
1264 if (early_z)
1265 render->aux0 |= 0x300;
1266
1267 if (pixel_kill)
1268 render->aux0 |= 0x1000;
1269
1270 if (ctx->tex_stateobj.num_samplers) {
1271 render->textures_address =
1272 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc);
1273 render->aux0 |= ctx->tex_stateobj.num_samplers << 14;
1274 render->aux0 |= 0x20;
1275 }
1276
1277 if (ctx->const_buffer[PIPE_SHADER_FRAGMENT].buffer) {
1278 render->uniforms_address =
1279 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform_array);
1280 uint32_t size = ctx->buffer_state[lima_ctx_buff_pp_uniform].size;
1281 uint32_t bits = 0;
1282 if (size >= 8) {
1283 bits = util_last_bit(size >> 3) - 1;
1284 bits += size & u_bit_consecutive(0, bits + 3) ? 1 : 0;
1285 }
1286 render->uniforms_address |= bits > 0xf ? 0xf : bits;
1287
1288 render->aux0 |= 0x80;
1289 render->aux1 |= 0x10000;
1290 }
1291
1292 if (ctx->vs->num_varyings) {
1293 render->varying_types = 0x00000000;
1294 render->varyings_address = ctx->gp_output->va +
1295 ctx->gp_output_varyings_offt;
1296 for (int i = 0, index = 0; i < ctx->vs->num_outputs; i++) {
1297 int val;
1298
1299 if (i == ctx->vs->gl_pos_idx ||
1300 i == ctx->vs->point_size_idx)
1301 continue;
1302
1303 struct lima_varying_info *v = ctx->vs->varying + i;
1304 if (v->component_size == 4)
1305 val = v->components > 2 ? 0 : 1;
1306 else
1307 val = v->components > 2 ? 2 : 3;
1308
1309 if (index < 10)
1310 render->varying_types |= val << (3 * index);
1311 else if (index == 10) {
1312 render->varying_types |= val << 30;
1313 render->varyings_address |= val >> 2;
1314 }
1315 else if (index == 11)
1316 render->varyings_address |= val << 1;
1317
1318 index++;
1319 }
1320 }
1321 else {
1322 render->varying_types = 0x00000000;
1323 render->varyings_address = 0x00000000;
1324 }
1325
1326 lima_dump_command_stream_print(
1327 render, sizeof(*render), false, "add render state at va %x\n",
1328 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw));
1329
1330 lima_dump_rsw_command_stream_print(
1331 render, sizeof(*render), lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw));
1332
1333 }
1334
1335 static void
1336 lima_update_gp_attribute_info(struct lima_context *ctx, const struct pipe_draw_info *info)
1337 {
1338 struct lima_vertex_element_state *ve = ctx->vertex_elements;
1339 struct lima_context_vertex_buffer *vb = &ctx->vertex_buffers;
1340
1341 uint32_t *attribute =
1342 lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_attribute_info,
1343 MAX2(1, ve->num_elements) * 8);
1344
1345 int n = 0;
1346 for (int i = 0; i < ve->num_elements; i++) {
1347 struct pipe_vertex_element *pve = ve->pipe + i;
1348
1349 assert(pve->vertex_buffer_index < vb->count);
1350 assert(vb->enabled_mask & (1 << pve->vertex_buffer_index));
1351
1352 struct pipe_vertex_buffer *pvb = vb->vb + pve->vertex_buffer_index;
1353 struct lima_resource *res = lima_resource(pvb->buffer.resource);
1354
1355 lima_submit_add_bo(ctx->gp_submit, res->bo, LIMA_SUBMIT_BO_READ);
1356
1357 unsigned start = info->index_size ? (ctx->min_index + info->index_bias) : info->start;
1358 attribute[n++] = res->bo->va + pvb->buffer_offset + pve->src_offset
1359 + start * pvb->stride;
1360 attribute[n++] = (pvb->stride << 11) |
1361 (lima_pipe_format_to_attrib_type(pve->src_format) << 2) |
1362 (util_format_get_nr_components(pve->src_format) - 1);
1363 }
1364
1365 lima_dump_command_stream_print(
1366 attribute, n * 4, false, "update attribute info at va %x\n",
1367 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_attribute_info));
1368 }
1369
1370 static void
1371 lima_update_gp_uniform(struct lima_context *ctx)
1372 {
1373 struct lima_context_constant_buffer *ccb =
1374 ctx->const_buffer + PIPE_SHADER_VERTEX;
1375 struct lima_vs_shader_state *vs = ctx->vs;
1376
1377 int size = vs->uniform_pending_offset + vs->constant_size + 32;
1378 void *vs_const_buff =
1379 lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_uniform, size);
1380
1381 if (ccb->buffer)
1382 memcpy(vs_const_buff, ccb->buffer, ccb->size);
1383
1384 memcpy(vs_const_buff + vs->uniform_pending_offset,
1385 ctx->viewport.transform.scale,
1386 sizeof(ctx->viewport.transform.scale));
1387 memcpy(vs_const_buff + vs->uniform_pending_offset + 16,
1388 ctx->viewport.transform.translate,
1389 sizeof(ctx->viewport.transform.translate));
1390
1391 if (vs->constant)
1392 memcpy(vs_const_buff + vs->uniform_pending_offset + 32,
1393 vs->constant, vs->constant_size);
1394
1395 lima_dump_command_stream_print(
1396 vs_const_buff, size, true,
1397 "update gp uniform at va %x\n",
1398 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_uniform));
1399 }
1400
1401 static void
1402 lima_update_pp_uniform(struct lima_context *ctx)
1403 {
1404 const float *const_buff = ctx->const_buffer[PIPE_SHADER_FRAGMENT].buffer;
1405 size_t const_buff_size = ctx->const_buffer[PIPE_SHADER_FRAGMENT].size / sizeof(float);
1406
1407 if (!const_buff)
1408 return;
1409
1410 uint16_t *fp16_const_buff =
1411 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_uniform,
1412 const_buff_size * sizeof(uint16_t));
1413
1414 uint32_t *array =
1415 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_uniform_array, 4);
1416
1417 for (int i = 0; i < const_buff_size; i++)
1418 fp16_const_buff[i] = util_float_to_half(const_buff[i]);
1419
1420 *array = lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform);
1421
1422 lima_dump_command_stream_print(
1423 fp16_const_buff, const_buff_size * 2, false, "add pp uniform data at va %x\n",
1424 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform));
1425 lima_dump_command_stream_print(
1426 array, 4, false, "add pp uniform info at va %x\n",
1427 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform_array));
1428 }
1429
1430 static void
1431 lima_update_varying(struct lima_context *ctx, const struct pipe_draw_info *info)
1432 {
1433 struct lima_screen *screen = lima_screen(ctx->base.screen);
1434 struct lima_vs_shader_state *vs = ctx->vs;
1435 uint32_t gp_output_size;
1436 unsigned num = info->index_size ? (ctx->max_index - ctx->min_index + 1) : info->count;
1437
1438 uint32_t *varying =
1439 lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_varying_info,
1440 vs->num_outputs * 8);
1441 int n = 0;
1442
1443 int offset = 0;
1444
1445 for (int i = 0; i < vs->num_outputs; i++) {
1446 struct lima_varying_info *v = vs->varying + i;
1447
1448 if (i == vs->gl_pos_idx ||
1449 i == vs->point_size_idx)
1450 continue;
1451
1452 int size = v->component_size * 4;
1453
1454 /* does component_size == 2 need to be 16 aligned? */
1455 if (v->component_size == 4)
1456 offset = align(offset, 16);
1457
1458 v->offset = offset;
1459 offset += size;
1460 }
1461
1462 vs->varying_stride = align(offset, 16);
1463
1464 /* gl_Position is always present, allocate space for it */
1465 gp_output_size = align(4 * 4 * num, 0x40);
1466
1467 /* Allocate space for varyings if there're any */
1468 if (vs->num_varyings) {
1469 ctx->gp_output_varyings_offt = gp_output_size;
1470 gp_output_size += align(vs->varying_stride * num, 0x40);
1471 }
1472
1473 /* Allocate space for gl_PointSize if it's there */
1474 if (vs->point_size_idx != -1) {
1475 ctx->gp_output_point_size_offt = gp_output_size;
1476 gp_output_size += 4 * num;
1477 }
1478
1479 /* gp_output can be too large for the suballocator, so create a
1480 * separate bo for it. The bo cache should prevent performance hit.
1481 */
1482 ctx->gp_output = lima_bo_create(screen, gp_output_size, 0);
1483 assert(ctx->gp_output);
1484 lima_submit_add_bo(ctx->gp_submit, ctx->gp_output, LIMA_SUBMIT_BO_WRITE);
1485 lima_submit_add_bo(ctx->pp_submit, ctx->gp_output, LIMA_SUBMIT_BO_READ);
1486
1487 for (int i = 0; i < vs->num_outputs; i++) {
1488 struct lima_varying_info *v = vs->varying + i;
1489
1490 if (i == vs->gl_pos_idx) {
1491 /* gl_Position */
1492 varying[n++] = ctx->gp_output->va;
1493 varying[n++] = 0x8020;
1494 } else if (i == vs->point_size_idx) {
1495 /* gl_PointSize */
1496 varying[n++] = ctx->gp_output->va + ctx->gp_output_point_size_offt;
1497 varying[n++] = 0x2021;
1498 } else {
1499 /* Varying */
1500 varying[n++] = ctx->gp_output->va + ctx->gp_output_varyings_offt +
1501 v->offset;
1502 varying[n++] = (vs->varying_stride << 11) | (v->components - 1) |
1503 (v->component_size == 2 ? 0x0C : 0);
1504 }
1505 }
1506
1507 lima_dump_command_stream_print(
1508 varying, n * 4, false, "update varying info at va %x\n",
1509 lima_ctx_buff_va(ctx, lima_ctx_buff_gp_varying_info));
1510 }
1511
1512 static void
1513 lima_draw_vbo_update(struct pipe_context *pctx,
1514 const struct pipe_draw_info *info)
1515 {
1516 struct lima_context *ctx = lima_context(pctx);
1517 struct lima_context_framebuffer *fb = &ctx->framebuffer;
1518 unsigned buffers = 0;
1519
1520 if (fb->base.zsbuf) {
1521 if (ctx->zsa->base.depth.enabled)
1522 buffers |= PIPE_CLEAR_DEPTH;
1523 if (ctx->zsa->base.stencil[0].enabled ||
1524 ctx->zsa->base.stencil[1].enabled)
1525 buffers |= PIPE_CLEAR_STENCIL;
1526 }
1527
1528 if (fb->base.nr_cbufs)
1529 buffers |= PIPE_CLEAR_COLOR0;
1530
1531 lima_update_submit_wb(ctx, buffers);
1532
1533 lima_update_gp_attribute_info(ctx, info);
1534
1535 if ((ctx->dirty & LIMA_CONTEXT_DIRTY_CONST_BUFF &&
1536 ctx->const_buffer[PIPE_SHADER_VERTEX].dirty) ||
1537 ctx->dirty & LIMA_CONTEXT_DIRTY_VIEWPORT ||
1538 ctx->dirty & LIMA_CONTEXT_DIRTY_SHADER_VERT) {
1539 lima_update_gp_uniform(ctx);
1540 ctx->const_buffer[PIPE_SHADER_VERTEX].dirty = false;
1541 }
1542
1543 lima_update_varying(ctx, info);
1544
1545 /* If it's zero scissor, don't build vs cmd list */
1546 if (!lima_is_scissor_zero(ctx))
1547 lima_pack_vs_cmd(ctx, info);
1548
1549 if (ctx->dirty & LIMA_CONTEXT_DIRTY_CONST_BUFF &&
1550 ctx->const_buffer[PIPE_SHADER_FRAGMENT].dirty) {
1551 lima_update_pp_uniform(ctx);
1552 ctx->const_buffer[PIPE_SHADER_FRAGMENT].dirty = false;
1553 }
1554
1555 lima_update_textures(ctx);
1556
1557 lima_pack_render_state(ctx, info);
1558 lima_pack_plbu_cmd(ctx, info);
1559
1560 if (ctx->gp_output) {
1561 lima_bo_unreference(ctx->gp_output); /* held by submit */
1562 ctx->gp_output = NULL;
1563 }
1564
1565 ctx->dirty = 0;
1566 }
1567
1568 static void
1569 lima_draw_vbo_indexed(struct pipe_context *pctx,
1570 const struct pipe_draw_info *info)
1571 {
1572 struct lima_context *ctx = lima_context(pctx);
1573 struct pipe_resource *indexbuf = NULL;
1574
1575 /* Mali Utgard GPU always need min/max index info for index draw,
1576 * compute it if upper layer does not do for us */
1577 if (info->max_index == ~0u)
1578 u_vbuf_get_minmax_index(pctx, info, &ctx->min_index, &ctx->max_index);
1579 else {
1580 ctx->min_index = info->min_index;
1581 ctx->max_index = info->max_index;
1582 }
1583
1584 if (info->has_user_indices) {
1585 util_upload_index_buffer(&ctx->base, info, &indexbuf, &ctx->index_offset, 0x40);
1586 ctx->index_res = lima_resource(indexbuf);
1587 }
1588 else {
1589 ctx->index_res = lima_resource(info->index.resource);
1590 ctx->index_offset = 0;
1591 }
1592
1593 lima_submit_add_bo(ctx->gp_submit, ctx->index_res->bo, LIMA_SUBMIT_BO_READ);
1594 lima_submit_add_bo(ctx->pp_submit, ctx->index_res->bo, LIMA_SUBMIT_BO_READ);
1595 lima_draw_vbo_update(pctx, info);
1596
1597 if (indexbuf)
1598 pipe_resource_reference(&indexbuf, NULL);
1599 }
1600
1601 static void
1602 lima_draw_vbo_count(struct pipe_context *pctx,
1603 const struct pipe_draw_info *info)
1604 {
1605 static const uint32_t max_verts = 65535;
1606
1607 struct pipe_draw_info local_info = *info;
1608 unsigned start = info->start;
1609 unsigned count = info->count;
1610
1611 while (count) {
1612 unsigned this_count = count;
1613 unsigned step;
1614
1615 u_split_draw(info, max_verts, &this_count, &step);
1616
1617 local_info.start = start;
1618 local_info.count = this_count;
1619
1620 lima_draw_vbo_update(pctx, &local_info);
1621
1622 count -= step;
1623 start += step;
1624 }
1625 }
1626
1627 static void
1628 lima_draw_vbo(struct pipe_context *pctx,
1629 const struct pipe_draw_info *info)
1630 {
1631 /* check if draw mode and vertex/index count match,
1632 * otherwise gp will hang */
1633 if (!u_trim_pipe_prim(info->mode, (unsigned*)&info->count)) {
1634 debug_printf("draw mode and vertex/index count mismatch\n");
1635 return;
1636 }
1637
1638 struct lima_context *ctx = lima_context(pctx);
1639
1640 if (!ctx->vs || !ctx->fs) {
1641 debug_warn_once("no shader, skip draw\n");
1642 return;
1643 }
1644
1645 if (!lima_update_vs_state(ctx) || !lima_update_fs_state(ctx))
1646 return;
1647
1648 lima_dump_command_stream_print(
1649 ctx->vs->bo->map, ctx->vs->shader_size, false,
1650 "add vs at va %x\n", ctx->vs->bo->va);
1651
1652 lima_dump_command_stream_print(
1653 ctx->fs->bo->map, ctx->fs->shader_size, false,
1654 "add fs at va %x\n", ctx->fs->bo->va);
1655
1656 lima_submit_add_bo(ctx->gp_submit, ctx->vs->bo, LIMA_SUBMIT_BO_READ);
1657 lima_submit_add_bo(ctx->pp_submit, ctx->fs->bo, LIMA_SUBMIT_BO_READ);
1658
1659 if (info->index_size)
1660 lima_draw_vbo_indexed(pctx, info);
1661 else
1662 lima_draw_vbo_count(pctx, info);
1663 }
1664
1665 static void
1666 lima_finish_plbu_cmd(struct lima_context *ctx)
1667 {
1668 int i = 0;
1669 uint32_t *plbu_cmd = util_dynarray_ensure_cap(&ctx->plbu_cmd_array, ctx->plbu_cmd_array.size + 2 * 4);
1670
1671 plbu_cmd[i++] = 0x00000000;
1672 plbu_cmd[i++] = 0x50000000; /* END */
1673
1674 ctx->plbu_cmd_array.size += i * 4;
1675 }
1676
1677 static void
1678 lima_pack_wb_zsbuf_reg(struct lima_context *ctx, uint32_t *wb_reg, int wb_idx)
1679 {
1680 struct lima_context_framebuffer *fb = &ctx->framebuffer;
1681 struct lima_resource *res = lima_resource(fb->base.zsbuf->texture);
1682 int level = fb->base.zsbuf->u.tex.level;
1683 uint32_t format = lima_format_get_pixel(fb->base.zsbuf->format);
1684
1685 struct lima_pp_wb_reg *wb = (void *)wb_reg;
1686 wb[wb_idx].type = 0x01; /* 1 for depth, stencil */
1687 wb[wb_idx].address = res->bo->va + res->levels[level].offset;
1688 wb[wb_idx].pixel_format = format;
1689 if (res->tiled) {
1690 wb[wb_idx].pixel_layout = 0x2;
1691 wb[wb_idx].pitch = fb->tiled_w;
1692 } else {
1693 wb[wb_idx].pixel_layout = 0x0;
1694 wb[wb_idx].pitch = res->levels[level].stride / 8;
1695 }
1696 wb[wb_idx].mrt_bits = 0;
1697 }
1698
1699 static void
1700 lima_pack_wb_cbuf_reg(struct lima_context *ctx, uint32_t *wb_reg, int wb_idx)
1701 {
1702 struct lima_context_framebuffer *fb = &ctx->framebuffer;
1703 struct lima_resource *res = lima_resource(fb->base.cbufs[0]->texture);
1704 int level = fb->base.cbufs[0]->u.tex.level;
1705 unsigned layer = fb->base.cbufs[0]->u.tex.first_layer;
1706 uint32_t format = lima_format_get_pixel(fb->base.cbufs[0]->format);
1707 bool swap_channels = lima_format_get_swap_rb(fb->base.cbufs[0]->format);
1708
1709 struct lima_pp_wb_reg *wb = (void *)wb_reg;
1710 wb[wb_idx].type = 0x02; /* 2 for color buffer */
1711 wb[wb_idx].address = res->bo->va + res->levels[level].offset + layer * res->levels[level].layer_stride;
1712 wb[wb_idx].pixel_format = format;
1713 if (res->tiled) {
1714 wb[wb_idx].pixel_layout = 0x2;
1715 wb[wb_idx].pitch = fb->tiled_w;
1716 } else {
1717 wb[wb_idx].pixel_layout = 0x0;
1718 wb[wb_idx].pitch = res->levels[level].stride / 8;
1719 }
1720 wb[wb_idx].mrt_bits = swap_channels ? 0x4 : 0x0;
1721 }
1722
1723
1724 static void
1725 lima_pack_pp_frame_reg(struct lima_context *ctx, uint32_t *frame_reg,
1726 uint32_t *wb_reg)
1727 {
1728 struct lima_context_framebuffer *fb = &ctx->framebuffer;
1729 struct lima_pp_frame_reg *frame = (void *)frame_reg;
1730 struct lima_screen *screen = lima_screen(ctx->base.screen);
1731 int wb_idx = 0;
1732
1733 frame->render_address = screen->pp_buffer->va + pp_frame_rsw_offset;
1734 frame->flags = 0x02;
1735 frame->clear_value_depth = ctx->clear.depth;
1736 frame->clear_value_stencil = ctx->clear.stencil;
1737 frame->clear_value_color = ctx->clear.color_8pc;
1738 frame->clear_value_color_1 = ctx->clear.color_8pc;
1739 frame->clear_value_color_2 = ctx->clear.color_8pc;
1740 frame->clear_value_color_3 = ctx->clear.color_8pc;
1741 frame->one = 1;
1742
1743 frame->width = fb->base.width - 1;
1744 frame->height = fb->base.height - 1;
1745
1746 /* frame->fragment_stack_address is overwritten per-pp in the kernel
1747 * by the values of pp_frame.fragment_stack_address[i] */
1748
1749 /* These are "stack size" and "stack offset" shifted,
1750 * here they are assumed to be always the same. */
1751 frame->fragment_stack_size = ctx->pp_max_stack_size << 16 | ctx->pp_max_stack_size;
1752
1753 /* related with MSAA and different value when r4p0/r7p0 */
1754 frame->supersampled_height = fb->base.height * 2 - 1;
1755 frame->scale = 0xE0C;
1756
1757 frame->dubya = 0x77;
1758 frame->onscreen = 1;
1759 frame->blocking = (fb->shift_min << 28) | (fb->shift_h << 16) | fb->shift_w;
1760 frame->foureight = 0x8888;
1761
1762 if (fb->base.nr_cbufs && (ctx->resolve & PIPE_CLEAR_COLOR0))
1763 lima_pack_wb_cbuf_reg(ctx, wb_reg, wb_idx++);
1764
1765 if (fb->base.zsbuf &&
1766 (ctx->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)))
1767 lima_pack_wb_zsbuf_reg(ctx, wb_reg, wb_idx++);
1768 }
1769
1770 static void
1771 _lima_flush(struct lima_context *ctx, bool end_of_frame)
1772 {
1773 #define pp_stack_pp_size 0x400
1774
1775 lima_pack_head_plbu_cmd(ctx);
1776 lima_finish_plbu_cmd(ctx);
1777
1778 lima_update_submit_bo(ctx);
1779
1780 int vs_cmd_size = ctx->vs_cmd_array.size;
1781 uint32_t vs_cmd_va = 0;
1782
1783 if (vs_cmd_size) {
1784 void *vs_cmd =
1785 lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_vs_cmd, vs_cmd_size);
1786 memcpy(vs_cmd, util_dynarray_begin(&ctx->vs_cmd_array), vs_cmd_size);
1787 util_dynarray_clear(&ctx->vs_cmd_array);
1788 vs_cmd_va = lima_ctx_buff_va(ctx, lima_ctx_buff_gp_vs_cmd);
1789
1790 lima_dump_command_stream_print(
1791 vs_cmd, vs_cmd_size, false, "flush vs cmd at va %x\n", vs_cmd_va);
1792 lima_dump_vs_command_stream_print(vs_cmd, vs_cmd_size, vs_cmd_va);
1793 }
1794
1795 int plbu_cmd_size = ctx->plbu_cmd_array.size + ctx->plbu_cmd_head.size;
1796 void *plbu_cmd =
1797 lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_plbu_cmd, plbu_cmd_size);
1798 memcpy(plbu_cmd,
1799 util_dynarray_begin(&ctx->plbu_cmd_head),
1800 ctx->plbu_cmd_head.size);
1801 memcpy(plbu_cmd + ctx->plbu_cmd_head.size,
1802 util_dynarray_begin(&ctx->plbu_cmd_array),
1803 ctx->plbu_cmd_array.size);
1804 util_dynarray_clear(&ctx->plbu_cmd_array);
1805 util_dynarray_clear(&ctx->plbu_cmd_head);
1806 uint32_t plbu_cmd_va = lima_ctx_buff_va(ctx, lima_ctx_buff_gp_plbu_cmd);
1807
1808 lima_dump_command_stream_print(
1809 plbu_cmd, plbu_cmd_size, false, "flush plbu cmd at va %x\n", plbu_cmd_va);
1810 lima_dump_plbu_command_stream_print(plbu_cmd, plbu_cmd_size, plbu_cmd_va);
1811
1812 struct lima_screen *screen = lima_screen(ctx->base.screen);
1813 struct drm_lima_gp_frame gp_frame;
1814 struct lima_gp_frame_reg *gp_frame_reg = (void *)gp_frame.frame;
1815 gp_frame_reg->vs_cmd_start = vs_cmd_va;
1816 gp_frame_reg->vs_cmd_end = vs_cmd_va + vs_cmd_size;
1817 gp_frame_reg->plbu_cmd_start = plbu_cmd_va;
1818 gp_frame_reg->plbu_cmd_end = plbu_cmd_va + plbu_cmd_size;
1819 gp_frame_reg->tile_heap_start = ctx->gp_tile_heap[ctx->plb_index]->va;
1820 gp_frame_reg->tile_heap_end = ctx->gp_tile_heap[ctx->plb_index]->va + ctx->gp_tile_heap_size;
1821
1822 lima_dump_command_stream_print(
1823 &gp_frame, sizeof(gp_frame), false, "add gp frame\n");
1824
1825 if (!lima_submit_start(ctx->gp_submit, &gp_frame, sizeof(gp_frame)))
1826 fprintf(stderr, "gp submit error\n");
1827
1828 if (lima_dump_command_stream) {
1829 if (lima_submit_wait(ctx->gp_submit, PIPE_TIMEOUT_INFINITE)) {
1830 if (ctx->gp_output) {
1831 float *pos = lima_bo_map(ctx->gp_output);
1832 lima_dump_command_stream_print(
1833 pos, 4 * 4 * 16, true, "gl_pos dump at va %x\n",
1834 ctx->gp_output->va);
1835 }
1836
1837 uint32_t *plb = lima_bo_map(ctx->plb[ctx->plb_index]);
1838 lima_dump_command_stream_print(
1839 plb, LIMA_CTX_PLB_BLK_SIZE, false, "plb dump at va %x\n",
1840 ctx->plb[ctx->plb_index]->va);
1841 }
1842 else {
1843 fprintf(stderr, "gp submit wait error\n");
1844 exit(1);
1845 }
1846 }
1847
1848 uint32_t pp_stack_va = 0;
1849 if (ctx->pp_max_stack_size) {
1850 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_stack, screen->num_pp *
1851 ctx->pp_max_stack_size * pp_stack_pp_size);
1852 pp_stack_va = lima_ctx_buff_va(ctx, lima_ctx_buff_pp_stack);
1853 }
1854
1855 lima_update_pp_stream(ctx);
1856
1857 struct lima_pp_stream_state *ps = &ctx->pp_stream;
1858 if (screen->gpu_type == DRM_LIMA_PARAM_GPU_ID_MALI400) {
1859 struct drm_lima_m400_pp_frame pp_frame = {0};
1860 lima_pack_pp_frame_reg(ctx, pp_frame.frame, pp_frame.wb);
1861 pp_frame.num_pp = screen->num_pp;
1862
1863 for (int i = 0; i < screen->num_pp; i++) {
1864 pp_frame.plbu_array_address[i] = ps->bo->va + ps->bo_offset + ps->offset[i];
1865 if (ctx->pp_max_stack_size)
1866 pp_frame.fragment_stack_address[i] = pp_stack_va +
1867 ctx->pp_max_stack_size * pp_stack_pp_size * i;
1868 }
1869
1870 lima_dump_command_stream_print(
1871 &pp_frame, sizeof(pp_frame), false, "add pp frame\n");
1872
1873 if (!lima_submit_start(ctx->pp_submit, &pp_frame, sizeof(pp_frame)))
1874 fprintf(stderr, "pp submit error\n");
1875 }
1876 else {
1877 struct drm_lima_m450_pp_frame pp_frame = {0};
1878 lima_pack_pp_frame_reg(ctx, pp_frame.frame, pp_frame.wb);
1879 pp_frame.num_pp = screen->num_pp;
1880
1881 if (ctx->pp_max_stack_size)
1882 for (int i = 0; i < screen->num_pp; i++)
1883 pp_frame.fragment_stack_address[i] = pp_stack_va +
1884 ctx->pp_max_stack_size * pp_stack_pp_size * i;
1885
1886 if (ps->bo) {
1887 for (int i = 0; i < screen->num_pp; i++)
1888 pp_frame.plbu_array_address[i] = ps->bo->va + ps->bo_offset + ps->offset[i];
1889 }
1890 else {
1891 pp_frame.use_dlbu = true;
1892
1893 struct lima_context_framebuffer *fb = &ctx->framebuffer;
1894 pp_frame.dlbu_regs[0] = ctx->plb[ctx->plb_index]->va;
1895 pp_frame.dlbu_regs[1] = ((fb->tiled_h - 1) << 16) | (fb->tiled_w - 1);
1896 unsigned s = util_logbase2(LIMA_CTX_PLB_BLK_SIZE) - 7;
1897 pp_frame.dlbu_regs[2] = (s << 28) | (fb->shift_h << 16) | fb->shift_w;
1898 pp_frame.dlbu_regs[3] = ((fb->tiled_h - 1) << 24) | ((fb->tiled_w - 1) << 16);
1899 }
1900
1901 lima_dump_command_stream_print(
1902 &pp_frame, sizeof(pp_frame), false, "add pp frame\n");
1903
1904 if (!lima_submit_start(ctx->pp_submit, &pp_frame, sizeof(pp_frame)))
1905 fprintf(stderr, "pp submit error\n");
1906 }
1907
1908 if (lima_dump_command_stream) {
1909 if (!lima_submit_wait(ctx->pp_submit, PIPE_TIMEOUT_INFINITE)) {
1910 fprintf(stderr, "pp wait error\n");
1911 exit(1);
1912 }
1913 }
1914
1915 ctx->plb_index = (ctx->plb_index + 1) % lima_ctx_num_plb;
1916
1917 if (ctx->framebuffer.base.nr_cbufs && (ctx->resolve & PIPE_CLEAR_COLOR0)) {
1918 /* Set reload flag for next draw. It'll be unset if buffer is cleared */
1919 struct lima_surface *surf = lima_surface(ctx->framebuffer.base.cbufs[0]);
1920 surf->reload = true;
1921 }
1922
1923 ctx->pp_max_stack_size = 0;
1924
1925 ctx->damage_rect.minx = ctx->damage_rect.miny = 0xffff;
1926 ctx->damage_rect.maxx = ctx->damage_rect.maxy = 0;
1927
1928 ctx->resolve = 0;
1929
1930 lima_dump_file_next();
1931 }
1932
1933 void
1934 lima_flush(struct lima_context *ctx)
1935 {
1936 if (!lima_ctx_dirty(ctx))
1937 return;
1938
1939 _lima_flush(ctx, false);
1940 }
1941
1942 static void
1943 lima_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
1944 unsigned flags)
1945 {
1946 struct lima_context *ctx = lima_context(pctx);
1947 if (lima_ctx_dirty(ctx))
1948 _lima_flush(ctx, flags & PIPE_FLUSH_END_OF_FRAME);
1949
1950 if (fence) {
1951 int fd;
1952 if (lima_submit_get_out_sync(ctx->pp_submit, &fd))
1953 *fence = lima_fence_create(fd);
1954 }
1955 }
1956
1957 void
1958 lima_draw_init(struct lima_context *ctx)
1959 {
1960 ctx->base.clear = lima_clear;
1961 ctx->base.draw_vbo = lima_draw_vbo;
1962 ctx->base.flush = lima_pipe_flush;
1963 }