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