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