2 * © Copyright 2018 Alyssa Rosenzweig
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include "pan_context.h"
29 #include "pan_swizzle.h"
30 #include "pan_format.h"
32 #include "util/macros.h"
33 #include "util/u_format.h"
34 #include "util/u_inlines.h"
35 #include "util/u_upload_mgr.h"
36 #include "util/u_memory.h"
37 #include "util/u_vbuf.h"
38 #include "util/half_float.h"
39 #include "util/u_helpers.h"
40 #include "util/u_format.h"
41 #include "indices/u_primconvert.h"
42 #include "tgsi/tgsi_parse.h"
43 #include "util/u_math.h"
45 #include "pan_screen.h"
46 #include "pan_blending.h"
47 #include "pan_blend_shaders.h"
50 static int performance_counter_number
= 0;
51 extern const char *pan_counters_base
;
53 /* Do not actually send anything to the GPU; merely generate the cmdstream as fast as possible. Disables framebuffer writes */
57 panfrost_enable_checksum(struct panfrost_context
*ctx
, struct panfrost_resource
*rsrc
)
59 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
60 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
61 int tile_w
= (rsrc
->base
.width0
+ (MALI_TILE_LENGTH
- 1)) >> MALI_TILE_SHIFT
;
62 int tile_h
= (rsrc
->base
.height0
+ (MALI_TILE_LENGTH
- 1)) >> MALI_TILE_SHIFT
;
64 /* 8 byte checksum per tile */
65 rsrc
->bo
->checksum_stride
= tile_w
* 8;
66 int pages
= (((rsrc
->bo
->checksum_stride
* tile_h
) + 4095) / 4096);
67 screen
->driver
->allocate_slab(screen
, &rsrc
->bo
->checksum_slab
, pages
, false, 0, 0, 0);
69 rsrc
->bo
->has_checksum
= true;
72 /* Framebuffer descriptor */
75 panfrost_set_framebuffer_resolution(struct mali_single_framebuffer
*fb
, int w
, int h
)
77 fb
->width
= MALI_POSITIVE(w
);
78 fb
->height
= MALI_POSITIVE(h
);
80 /* No idea why this is needed, but it's how resolution_check is
81 * calculated. It's not clear to us yet why the hardware wants this.
82 * The formula itself was discovered mostly by manual bruteforce and
83 * aggressive algebraic simplification. */
85 fb
->resolution_check
= ((w
+ h
) / 3) << 4;
88 struct mali_single_framebuffer
89 panfrost_emit_sfbd(struct panfrost_context
*ctx
)
91 struct mali_single_framebuffer framebuffer
= {
94 .clear_flags
= 0x1000,
95 .unknown_address_0
= ctx
->scratchpad
.gpu
,
96 .unknown_address_1
= ctx
->misc_0
.gpu
,
97 .unknown_address_2
= ctx
->misc_0
.gpu
+ 40960,
99 .tiler_heap_free
= ctx
->tiler_heap
.gpu
,
100 .tiler_heap_end
= ctx
->tiler_heap
.gpu
+ ctx
->tiler_heap
.size
,
103 panfrost_set_framebuffer_resolution(&framebuffer
, ctx
->pipe_framebuffer
.width
, ctx
->pipe_framebuffer
.height
);
108 struct bifrost_framebuffer
109 panfrost_emit_mfbd(struct panfrost_context
*ctx
)
111 struct bifrost_framebuffer framebuffer
= {
112 /* It is not yet clear what tiler_meta means or how it's
113 * calculated, but we can tell the lower 32-bits are a
114 * (monotonically increasing?) function of tile count and
115 * geometry complexity; I suspect it defines a memory size of
116 * some kind? for the tiler. It's really unclear at the
117 * moment... but to add to the confusion, the hardware is happy
118 * enough to accept a zero in this field, so we don't even have
119 * to worry about it right now.
121 * The byte (just after the 32-bit mark) is much more
122 * interesting. The higher nibble I've only ever seen as 0xF,
123 * but the lower one I've seen as 0x0 or 0xF, and it's not
124 * obvious what the difference is. But what -is- obvious is
125 * that when the lower nibble is zero, performance is severely
126 * degraded compared to when the lower nibble is set.
127 * Evidently, that nibble enables some sort of fast path,
128 * perhaps relating to caching or tile flush? Regardless, at
129 * this point there's no clear reason not to set it, aside from
130 * substantially increased memory requirements (of the misc_0
133 .tiler_meta
= ((uint64_t) 0xff << 32) | 0x0,
135 .width1
= MALI_POSITIVE(ctx
->pipe_framebuffer
.width
),
136 .height1
= MALI_POSITIVE(ctx
->pipe_framebuffer
.height
),
137 .width2
= MALI_POSITIVE(ctx
->pipe_framebuffer
.width
),
138 .height2
= MALI_POSITIVE(ctx
->pipe_framebuffer
.height
),
143 .rt_count_1
= MALI_POSITIVE(1),
148 /* Corresponds to unknown_address_X of SFBD */
149 .scratchpad
= ctx
->scratchpad
.gpu
,
150 .tiler_scratch_start
= ctx
->misc_0
.gpu
,
152 /* The constant added here is, like the lower word of
153 * tiler_meta, (loosely) another product of framebuffer size
154 * and geometry complexity. It must be sufficiently large for
155 * the tiler_meta fast path to work; if it's too small, there
156 * will be DATA_INVALID_FAULTs. Conversely, it must be less
157 * than the total size of misc_0, or else there's no room. It's
158 * possible this constant configures a partition between two
159 * parts of misc_0? We haven't investigated the functionality,
160 * as these buffers are internally used by the hardware
161 * (presumably by the tiler) but not seemingly touched by the driver
164 .tiler_scratch_middle
= ctx
->misc_0
.gpu
+ 0xf0000,
166 .tiler_heap_start
= ctx
->tiler_heap
.gpu
,
167 .tiler_heap_end
= ctx
->tiler_heap
.gpu
+ ctx
->tiler_heap
.size
,
173 /* Are we currently rendering to the screen (rather than an FBO)? */
176 panfrost_is_scanout(struct panfrost_context
*ctx
)
178 /* If there is no color buffer, it's an FBO */
179 if (!ctx
->pipe_framebuffer
.nr_cbufs
)
182 /* If we're too early that no framebuffer was sent, it's scanout */
183 if (!ctx
->pipe_framebuffer
.cbufs
[0])
186 return ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
187 ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
188 ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;
192 pan_pack_color(const union pipe_color_union
*color
, enum pipe_format format
)
194 /* Alpha magicked to 1.0 if there is no alpha */
196 bool has_alpha
= util_format_has_alpha(format
);
197 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
199 /* Packed color depends on the framebuffer format */
201 const struct util_format_description
*desc
=
202 util_format_description(format
);
204 if (util_format_is_rgba8_variant(desc
)) {
205 return (float_to_ubyte(clear_alpha
) << 24) |
206 (float_to_ubyte(color
->f
[2]) << 16) |
207 (float_to_ubyte(color
->f
[1]) << 8) |
208 (float_to_ubyte(color
->f
[0]) << 0);
209 } else if (format
== PIPE_FORMAT_B5G6R5_UNORM
) {
210 /* First, we convert the components to R5, G6, B5 separately */
211 unsigned r5
= CLAMP(color
->f
[0], 0.0, 1.0) * 31.0;
212 unsigned g6
= CLAMP(color
->f
[1], 0.0, 1.0) * 63.0;
213 unsigned b5
= CLAMP(color
->f
[2], 0.0, 1.0) * 31.0;
215 /* Then we pack into a sparse u32. TODO: Why these shifts? */
216 return (b5
<< 25) | (g6
<< 14) | (r5
<< 5);
227 struct pipe_context
*pipe
,
229 const union pipe_color_union
*color
,
230 double depth
, unsigned stencil
)
232 struct panfrost_context
*ctx
= pan_context(pipe
);
233 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
235 if (buffers
& PIPE_CLEAR_COLOR
) {
236 enum pipe_format format
= ctx
->pipe_framebuffer
.cbufs
[0]->format
;
237 job
->clear_color
= pan_pack_color(color
, format
);
240 if (buffers
& PIPE_CLEAR_DEPTH
) {
241 job
->clear_depth
= depth
;
244 if (buffers
& PIPE_CLEAR_STENCIL
) {
245 job
->clear_stencil
= stencil
;
248 job
->clear
|= buffers
;
252 panfrost_attach_vt_mfbd(struct panfrost_context
*ctx
)
254 /* MFBD needs a sequential semi-render target upload, but what exactly this is, is beyond me for now */
255 struct bifrost_render_target rts_list
[] = {
260 .framebuffer
= ctx
->misc_0
.gpu
,
265 /* Allocate memory for the three components */
266 int size
= 1024 + sizeof(ctx
->vt_framebuffer_mfbd
) + sizeof(rts_list
);
267 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
269 /* Opaque 1024-block */
270 rts_list
[0].chunknown
.pointer
= transfer
.gpu
;
272 memcpy(transfer
.cpu
+ 1024, &ctx
->vt_framebuffer_mfbd
, sizeof(ctx
->vt_framebuffer_mfbd
));
273 memcpy(transfer
.cpu
+ 1024 + sizeof(ctx
->vt_framebuffer_mfbd
), rts_list
, sizeof(rts_list
));
275 return (transfer
.gpu
+ 1024) | MALI_MFBD
;
279 panfrost_attach_vt_sfbd(struct panfrost_context
*ctx
)
281 return panfrost_upload_transient(ctx
, &ctx
->vt_framebuffer_sfbd
, sizeof(ctx
->vt_framebuffer_sfbd
)) | MALI_SFBD
;
285 panfrost_attach_vt_framebuffer(struct panfrost_context
*ctx
)
287 mali_ptr framebuffer
= ctx
->require_sfbd
?
288 panfrost_attach_vt_sfbd(ctx
) :
289 panfrost_attach_vt_mfbd(ctx
);
291 ctx
->payload_vertex
.postfix
.framebuffer
= framebuffer
;
292 ctx
->payload_tiler
.postfix
.framebuffer
= framebuffer
;
295 /* Reset per-frame context, called on context initialisation as well as after
296 * flushing a frame */
299 panfrost_invalidate_frame(struct panfrost_context
*ctx
)
301 unsigned transient_count
= ctx
->transient_pools
[ctx
->cmdstream_i
].entry_index
*ctx
->transient_pools
[0].entry_size
+ ctx
->transient_pools
[ctx
->cmdstream_i
].entry_offset
;
302 DBG("Uploaded transient %d bytes\n", transient_count
);
304 /* Rotate cmdstream */
305 if ((++ctx
->cmdstream_i
) == (sizeof(ctx
->transient_pools
) / sizeof(ctx
->transient_pools
[0])))
306 ctx
->cmdstream_i
= 0;
308 if (ctx
->require_sfbd
)
309 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
311 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
313 /* Reset varyings allocated */
314 ctx
->varying_height
= 0;
316 /* The transient cmdstream is dirty every frame; the only bits worth preserving
317 * (textures, shaders, etc) are in other buffers anyways */
319 ctx
->transient_pools
[ctx
->cmdstream_i
].entry_index
= 0;
320 ctx
->transient_pools
[ctx
->cmdstream_i
].entry_offset
= 0;
322 /* Regenerate payloads */
323 panfrost_attach_vt_framebuffer(ctx
);
326 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
329 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
| PAN_DIRTY_TEXTURES
;
331 /* Reset job counters */
333 ctx
->vertex_job_count
= 0;
334 ctx
->tiler_job_count
= 0;
337 /* In practice, every field of these payloads should be configurable
338 * arbitrarily, which means these functions are basically catch-all's for
339 * as-of-yet unwavering unknowns */
342 panfrost_emit_vertex_payload(struct panfrost_context
*ctx
)
344 struct midgard_payload_vertex_tiler payload
= {
346 .workgroups_z_shift
= 32,
347 .workgroups_x_shift_2
= 0x2,
348 .workgroups_x_shift_3
= 0x5,
350 .gl_enables
= 0x4 | (ctx
->is_t6xx
? 0 : 0x2),
353 memcpy(&ctx
->payload_vertex
, &payload
, sizeof(payload
));
357 panfrost_emit_tiler_payload(struct panfrost_context
*ctx
)
359 struct midgard_payload_vertex_tiler payload
= {
361 .workgroups_z_shift
= 32,
362 .workgroups_x_shift_2
= 0x2,
363 .workgroups_x_shift_3
= 0x6,
365 .zero1
= 0xffff, /* Why is this only seen on test-quad-textured? */
369 memcpy(&ctx
->payload_tiler
, &payload
, sizeof(payload
));
373 translate_tex_wrap(enum pipe_tex_wrap w
)
376 case PIPE_TEX_WRAP_REPEAT
:
377 return MALI_WRAP_REPEAT
;
379 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
380 return MALI_WRAP_CLAMP_TO_EDGE
;
382 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
383 return MALI_WRAP_CLAMP_TO_BORDER
;
385 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
386 return MALI_WRAP_MIRRORED_REPEAT
;
389 unreachable("Invalid wrap");
394 translate_tex_filter(enum pipe_tex_filter f
)
397 case PIPE_TEX_FILTER_NEAREST
:
400 case PIPE_TEX_FILTER_LINEAR
:
404 unreachable("Invalid filter");
409 translate_mip_filter(enum pipe_tex_mipfilter f
)
411 return (f
== PIPE_TEX_MIPFILTER_LINEAR
) ? MALI_MIP_LINEAR
: 0;
415 panfrost_translate_compare_func(enum pipe_compare_func in
)
418 case PIPE_FUNC_NEVER
:
419 return MALI_FUNC_NEVER
;
422 return MALI_FUNC_LESS
;
424 case PIPE_FUNC_EQUAL
:
425 return MALI_FUNC_EQUAL
;
427 case PIPE_FUNC_LEQUAL
:
428 return MALI_FUNC_LEQUAL
;
430 case PIPE_FUNC_GREATER
:
431 return MALI_FUNC_GREATER
;
433 case PIPE_FUNC_NOTEQUAL
:
434 return MALI_FUNC_NOTEQUAL
;
436 case PIPE_FUNC_GEQUAL
:
437 return MALI_FUNC_GEQUAL
;
439 case PIPE_FUNC_ALWAYS
:
440 return MALI_FUNC_ALWAYS
;
443 unreachable("Invalid func");
448 panfrost_translate_alt_compare_func(enum pipe_compare_func in
)
451 case PIPE_FUNC_NEVER
:
452 return MALI_ALT_FUNC_NEVER
;
455 return MALI_ALT_FUNC_LESS
;
457 case PIPE_FUNC_EQUAL
:
458 return MALI_ALT_FUNC_EQUAL
;
460 case PIPE_FUNC_LEQUAL
:
461 return MALI_ALT_FUNC_LEQUAL
;
463 case PIPE_FUNC_GREATER
:
464 return MALI_ALT_FUNC_GREATER
;
466 case PIPE_FUNC_NOTEQUAL
:
467 return MALI_ALT_FUNC_NOTEQUAL
;
469 case PIPE_FUNC_GEQUAL
:
470 return MALI_ALT_FUNC_GEQUAL
;
472 case PIPE_FUNC_ALWAYS
:
473 return MALI_ALT_FUNC_ALWAYS
;
476 unreachable("Invalid alt func");
481 panfrost_translate_stencil_op(enum pipe_stencil_op in
)
484 case PIPE_STENCIL_OP_KEEP
:
485 return MALI_STENCIL_KEEP
;
487 case PIPE_STENCIL_OP_ZERO
:
488 return MALI_STENCIL_ZERO
;
490 case PIPE_STENCIL_OP_REPLACE
:
491 return MALI_STENCIL_REPLACE
;
493 case PIPE_STENCIL_OP_INCR
:
494 return MALI_STENCIL_INCR
;
496 case PIPE_STENCIL_OP_DECR
:
497 return MALI_STENCIL_DECR
;
499 case PIPE_STENCIL_OP_INCR_WRAP
:
500 return MALI_STENCIL_INCR_WRAP
;
502 case PIPE_STENCIL_OP_DECR_WRAP
:
503 return MALI_STENCIL_DECR_WRAP
;
505 case PIPE_STENCIL_OP_INVERT
:
506 return MALI_STENCIL_INVERT
;
509 unreachable("Invalid stencil op");
514 panfrost_make_stencil_state(const struct pipe_stencil_state
*in
, struct mali_stencil_test
*out
)
516 out
->ref
= 0; /* Gallium gets it from elsewhere */
518 out
->mask
= in
->valuemask
;
519 out
->func
= panfrost_translate_compare_func(in
->func
);
520 out
->sfail
= panfrost_translate_stencil_op(in
->fail_op
);
521 out
->dpfail
= panfrost_translate_stencil_op(in
->zfail_op
);
522 out
->dppass
= panfrost_translate_stencil_op(in
->zpass_op
);
526 panfrost_default_shader_backend(struct panfrost_context
*ctx
)
528 struct mali_shader_meta shader
= {
529 .alpha_coverage
= ~MALI_ALPHA_COVERAGE(0.000000),
531 .unknown2_3
= MALI_DEPTH_FUNC(MALI_FUNC_ALWAYS
) | 0x3010,
532 .unknown2_4
= MALI_NO_MSAA
| 0x4e0,
536 shader
.unknown2_4
|= 0x10;
539 struct pipe_stencil_state default_stencil
= {
541 .func
= PIPE_FUNC_ALWAYS
,
542 .fail_op
= MALI_STENCIL_KEEP
,
543 .zfail_op
= MALI_STENCIL_KEEP
,
544 .zpass_op
= MALI_STENCIL_KEEP
,
549 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_front
);
550 shader
.stencil_mask_front
= default_stencil
.writemask
;
552 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_back
);
553 shader
.stencil_mask_back
= default_stencil
.writemask
;
555 if (default_stencil
.enabled
)
556 shader
.unknown2_4
|= MALI_STENCIL_TEST
;
558 memcpy(&ctx
->fragment_shader_core
, &shader
, sizeof(shader
));
562 panfrost_link_job_pair(struct mali_job_descriptor_header
*first
, mali_ptr next
)
564 if (first
->job_descriptor_size
)
565 first
->next_job_64
= (u64
) (uintptr_t) next
;
567 first
->next_job_32
= (u32
) (uintptr_t) next
;
570 /* Generates a vertex/tiler job. This is, in some sense, the heart of the
571 * graphics command stream. It should be called once per draw, accordding to
572 * presentations. Set is_tiler for "tiler" jobs (fragment shader jobs, but in
573 * Mali parlance, "fragment" refers to framebuffer writeout). Clear it for
576 struct panfrost_transfer
577 panfrost_vertex_tiler_job(struct panfrost_context
*ctx
, bool is_tiler
)
579 /* Each draw call corresponds to two jobs, and the set-value job is first */
580 int draw_job_index
= 1 + (2 * ctx
->draw_count
) + 1;
582 struct mali_job_descriptor_header job
= {
583 .job_type
= is_tiler
? JOB_TYPE_TILER
: JOB_TYPE_VERTEX
,
584 .job_index
= draw_job_index
+ (is_tiler
? 1 : 0),
586 .job_descriptor_size
= 1,
590 struct midgard_payload_vertex_tiler
*payload
= is_tiler
? &ctx
->payload_tiler
: &ctx
->payload_vertex
;
592 /* There's some padding hacks on 32-bit */
599 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(job
) + sizeof(*payload
));
602 /* Tiler jobs depend on vertex jobs */
604 job
.job_dependency_index_1
= draw_job_index
;
606 /* Tiler jobs also depend on the previous tiler job */
608 if (ctx
->draw_count
) {
609 job
.job_dependency_index_2
= draw_job_index
- 1;
610 /* Previous tiler job points to this tiler job */
611 panfrost_link_job_pair(ctx
->u_tiler_jobs
[ctx
->draw_count
- 1], transfer
.gpu
);
613 /* The only vertex job so far points to first tiler job */
614 panfrost_link_job_pair(ctx
->u_vertex_jobs
[0], transfer
.gpu
);
617 if (ctx
->draw_count
) {
618 /* Previous vertex job points to this vertex job */
619 panfrost_link_job_pair(ctx
->u_vertex_jobs
[ctx
->draw_count
- 1], transfer
.gpu
);
621 /* Last vertex job points to first tiler job */
622 panfrost_link_job_pair(&job
, ctx
->tiler_jobs
[0]);
624 /* Have the first vertex job depend on the set value job */
625 job
.job_dependency_index_1
= ctx
->u_set_value_job
->job_index
;
626 panfrost_link_job_pair(ctx
->u_set_value_job
, transfer
.gpu
);
630 memcpy(transfer
.cpu
, &job
, sizeof(job
));
631 memcpy(transfer
.cpu
+ sizeof(job
) - offset
, payload
, sizeof(*payload
));
635 /* Generates a set value job. It's unclear what exactly this does, why it's
636 * necessary, and when to call it. */
639 panfrost_set_value_job(struct panfrost_context
*ctx
)
641 struct mali_job_descriptor_header job
= {
642 .job_type
= JOB_TYPE_SET_VALUE
,
643 .job_descriptor_size
= 1,
647 struct mali_payload_set_value payload
= {
648 .out
= ctx
->misc_0
.gpu
,
652 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(job
) + sizeof(payload
));
653 memcpy(transfer
.cpu
, &job
, sizeof(job
));
654 memcpy(transfer
.cpu
+ sizeof(job
), &payload
, sizeof(payload
));
656 ctx
->u_set_value_job
= (struct mali_job_descriptor_header
*) transfer
.cpu
;
657 ctx
->set_value_job
= transfer
.gpu
;
661 panfrost_emit_varyings(
662 struct panfrost_context
*ctx
,
663 union mali_attr
*slot
,
667 mali_ptr varying_address
= ctx
->varying_mem
.gpu
+ ctx
->varying_height
;
669 /* Fill out the descriptor */
670 slot
->elements
= varying_address
| MALI_ATTR_LINEAR
;
671 slot
->stride
= stride
;
672 slot
->size
= stride
* count
;
674 ctx
->varying_height
+= ALIGN(slot
->size
, 64);
675 assert(ctx
->varying_height
< ctx
->varying_mem
.size
);
677 return varying_address
;
681 panfrost_emit_point_coord(union mali_attr
*slot
)
683 slot
->elements
= MALI_VARYING_POINT_COORD
| MALI_ATTR_LINEAR
;
684 slot
->stride
= slot
->size
= 0;
688 panfrost_emit_varying_descriptor(
689 struct panfrost_context
*ctx
,
690 unsigned invocation_count
)
692 /* Load the shaders */
694 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
695 struct panfrost_shader_state
*fs
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
697 /* Allocate the varying descriptor */
699 size_t vs_size
= sizeof(struct mali_attr_meta
) * vs
->tripipe
->varying_count
;
700 size_t fs_size
= sizeof(struct mali_attr_meta
) * fs
->tripipe
->varying_count
;
702 struct panfrost_transfer trans
= panfrost_allocate_transient(ctx
,
705 memcpy(trans
.cpu
, vs
->varyings
, vs_size
);
706 memcpy(trans
.cpu
+ vs_size
, fs
->varyings
, fs_size
);
708 ctx
->payload_vertex
.postfix
.varying_meta
= trans
.gpu
;
709 ctx
->payload_tiler
.postfix
.varying_meta
= trans
.gpu
+ vs_size
;
711 /* Buffer indices must be in this order per our convention */
712 union mali_attr varyings
[PIPE_MAX_ATTRIBS
];
715 /* General varyings -- use the VS's, since those are more likely to be
716 * accurate on desktop */
718 panfrost_emit_varyings(ctx
, &varyings
[idx
++],
719 vs
->general_varying_stride
, invocation_count
);
721 /* fp32 vec4 gl_Position */
722 ctx
->payload_tiler
.postfix
.position_varying
=
723 panfrost_emit_varyings(ctx
, &varyings
[idx
++],
724 sizeof(float) * 4, invocation_count
);
727 if (vs
->writes_point_size
|| fs
->reads_point_coord
) {
728 /* fp16 vec1 gl_PointSize */
729 ctx
->payload_tiler
.primitive_size
.pointer
=
730 panfrost_emit_varyings(ctx
, &varyings
[idx
++],
731 2, invocation_count
);
734 if (fs
->reads_point_coord
) {
735 /* Special descriptor */
736 panfrost_emit_point_coord(&varyings
[idx
++]);
739 mali_ptr varyings_p
= panfrost_upload_transient(ctx
, &varyings
, idx
* sizeof(union mali_attr
));
740 ctx
->payload_vertex
.postfix
.varyings
= varyings_p
;
741 ctx
->payload_tiler
.postfix
.varyings
= varyings_p
;
745 panfrost_vertex_buffer_address(struct panfrost_context
*ctx
, unsigned i
)
747 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[i
];
748 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
.resource
);
750 return rsrc
->bo
->gpu
+ buf
->buffer_offset
;
753 /* Emits attributes and varying descriptors, which should be called every draw,
754 * excepting some obscure circumstances */
757 panfrost_emit_vertex_data(struct panfrost_context
*ctx
, struct panfrost_job
*job
)
759 /* Staged mali_attr, and index into them. i =/= k, depending on the
760 * vertex buffer mask */
761 union mali_attr attrs
[PIPE_MAX_ATTRIBS
];
764 unsigned invocation_count
= MALI_NEGATIVE(ctx
->payload_tiler
.prefix
.invocation_count
);
766 for (int i
= 0; i
< ARRAY_SIZE(ctx
->vertex_buffers
); ++i
) {
767 if (!(ctx
->vb_mask
& (1 << i
))) continue;
769 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[i
];
770 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
.resource
);
774 /* Align to 64 bytes by masking off the lower bits. This
775 * will be adjusted back when we fixup the src_offset in
778 mali_ptr addr
= panfrost_vertex_buffer_address(ctx
, i
) & ~63;
780 /* Offset vertex count by draw_start to make sure we upload enough */
781 attrs
[k
].stride
= buf
->stride
;
782 attrs
[k
].size
= rsrc
->base
.width0
;
784 panfrost_job_add_bo(job
, rsrc
->bo
);
785 attrs
[k
].elements
= addr
| MALI_ATTR_LINEAR
;
790 ctx
->payload_vertex
.postfix
.attributes
= panfrost_upload_transient(ctx
, attrs
, k
* sizeof(union mali_attr
));
792 panfrost_emit_varying_descriptor(ctx
, invocation_count
);
796 panfrost_writes_point_size(struct panfrost_context
*ctx
)
799 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
801 return vs
->writes_point_size
&& ctx
->payload_tiler
.prefix
.draw_mode
== MALI_POINTS
;
804 /* Stage the attribute descriptors so we can adjust src_offset
805 * to let BOs align nicely */
808 panfrost_stage_attributes(struct panfrost_context
*ctx
)
810 struct panfrost_vertex_state
*so
= ctx
->vertex
;
812 size_t sz
= sizeof(struct mali_attr_meta
) * so
->num_elements
;
813 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sz
);
814 struct mali_attr_meta
*target
= (struct mali_attr_meta
*) transfer
.cpu
;
816 /* Copy as-is for the first pass */
817 memcpy(target
, so
->hw
, sz
);
819 /* Fixup offsets for the second pass. Recall that the hardware
820 * calculates attribute addresses as:
822 * addr = base + (stride * vtx) + src_offset;
824 * However, on Mali, base must be aligned to 64-bytes, so we
827 * base' = base & ~63 = base - (base & 63)
829 * To compensate when using base' (see emit_vertex_data), we have
830 * to adjust src_offset by the masked off piece:
832 * addr' = base' + (stride * vtx) + (src_offset + (base & 63))
833 * = base - (base & 63) + (stride * vtx) + src_offset + (base & 63)
834 * = base + (stride * vtx) + src_offset
840 for (unsigned i
= 0; i
< so
->num_elements
; ++i
) {
841 unsigned vbi
= so
->pipe
[i
].vertex_buffer_index
;
842 mali_ptr addr
= panfrost_vertex_buffer_address(ctx
, vbi
);
844 /* Adjust by the masked off bits of the offset */
845 target
[i
].src_offset
+= (addr
& 63);
848 ctx
->payload_vertex
.postfix
.attribute_meta
= transfer
.gpu
;
852 panfrost_upload_sampler_descriptors(struct panfrost_context
*ctx
)
854 size_t desc_size
= sizeof(struct mali_sampler_descriptor
);
856 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
857 if (!ctx
->sampler_count
[t
]) continue;
859 size_t transfer_size
= desc_size
* ctx
->sampler_count
[t
];
861 struct panfrost_transfer transfer
=
862 panfrost_allocate_transient(ctx
, transfer_size
);
864 struct mali_sampler_descriptor
*desc
=
865 (struct mali_sampler_descriptor
*) transfer
.cpu
;
867 for (int i
= 0; i
< ctx
->sampler_count
[t
]; ++i
)
868 desc
[i
] = ctx
->samplers
[t
][i
]->hw
;
870 if (t
== PIPE_SHADER_FRAGMENT
)
871 ctx
->payload_tiler
.postfix
.sampler_descriptor
= transfer
.gpu
;
872 else if (t
== PIPE_SHADER_VERTEX
)
873 ctx
->payload_vertex
.postfix
.sampler_descriptor
= transfer
.gpu
;
879 /* Computes the address to a texture at a particular slice */
882 panfrost_get_texture_address(
883 struct panfrost_resource
*rsrc
,
884 unsigned level
, unsigned face
)
886 unsigned level_offset
= rsrc
->bo
->slices
[level
].offset
;
887 unsigned face_offset
= face
* rsrc
->bo
->cubemap_stride
;
889 return rsrc
->bo
->gpu
+ level_offset
+ face_offset
;
895 struct panfrost_context
*ctx
,
896 struct panfrost_sampler_view
*view
)
899 return (mali_ptr
) NULL
;
901 struct pipe_resource
*tex_rsrc
= view
->base
.texture
;
902 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) tex_rsrc
;
904 /* Do we interleave an explicit stride with every element? */
906 bool has_manual_stride
=
907 view
->hw
.format
.usage2
& MALI_TEX_MANUAL_STRIDE
;
909 /* Inject the addresses in, interleaving mip levels, cube faces, and
910 * strides in that order */
914 for (unsigned l
= 0; l
<= tex_rsrc
->last_level
; ++l
) {
915 for (unsigned f
= 0; f
< tex_rsrc
->array_size
; ++f
) {
916 view
->hw
.payload
[idx
++] =
917 panfrost_get_texture_address(rsrc
, l
, f
);
919 if (has_manual_stride
) {
920 view
->hw
.payload
[idx
++] =
921 rsrc
->bo
->slices
[l
].stride
;
926 return panfrost_upload_transient(ctx
, &view
->hw
,
927 sizeof(struct mali_texture_descriptor
));
931 panfrost_upload_texture_descriptors(struct panfrost_context
*ctx
)
933 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
935 if (!ctx
->sampler_view_count
[t
]) continue;
937 uint64_t trampolines
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
939 for (int i
= 0; i
< ctx
->sampler_view_count
[t
]; ++i
)
941 panfrost_upload_tex(ctx
, ctx
->sampler_views
[t
][i
]);
943 mali_ptr trampoline
= panfrost_upload_transient(ctx
, trampolines
, sizeof(uint64_t) * ctx
->sampler_view_count
[t
]);
945 if (t
== PIPE_SHADER_FRAGMENT
)
946 ctx
->payload_tiler
.postfix
.texture_trampoline
= trampoline
;
947 else if (t
== PIPE_SHADER_VERTEX
)
948 ctx
->payload_vertex
.postfix
.texture_trampoline
= trampoline
;
954 /* Go through dirty flags and actualise them in the cmdstream. */
957 panfrost_emit_for_draw(struct panfrost_context
*ctx
, bool with_vertex_data
)
959 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
961 if (with_vertex_data
) {
962 panfrost_emit_vertex_data(ctx
, job
);
965 bool msaa
= ctx
->rasterizer
->base
.multisample
;
967 if (ctx
->dirty
& PAN_DIRTY_RASTERIZER
) {
968 ctx
->payload_tiler
.gl_enables
= ctx
->rasterizer
->tiler_gl_enables
;
970 /* TODO: Sample size */
971 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_HAS_MSAA
, msaa
);
972 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_MSAA
, !msaa
);
975 /* Enable job requirements at draw-time */
978 job
->requirements
|= PAN_REQ_MSAA
;
980 if (ctx
->depth_stencil
->depth
.writemask
)
981 job
->requirements
|= PAN_REQ_DEPTH_WRITE
;
983 if (ctx
->occlusion_query
) {
984 ctx
->payload_tiler
.gl_enables
|= MALI_OCCLUSION_QUERY
| MALI_OCCLUSION_PRECISE
;
985 ctx
->payload_tiler
.postfix
.occlusion_counter
= ctx
->occlusion_query
->transfer
.gpu
;
988 if (ctx
->dirty
& PAN_DIRTY_VS
) {
991 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
993 /* Late shader descriptor assignments */
995 vs
->tripipe
->texture_count
= ctx
->sampler_view_count
[PIPE_SHADER_VERTEX
];
996 vs
->tripipe
->sampler_count
= ctx
->sampler_count
[PIPE_SHADER_VERTEX
];
999 vs
->tripipe
->midgard1
.unknown1
= 0x2201;
1001 ctx
->payload_vertex
.postfix
._shader_upper
= vs
->tripipe_gpu
>> 4;
1004 if (ctx
->dirty
& (PAN_DIRTY_RASTERIZER
| PAN_DIRTY_VS
)) {
1005 /* Check if we need to link the gl_PointSize varying */
1006 if (!panfrost_writes_point_size(ctx
)) {
1007 /* If the size is constant, write it out. Otherwise,
1008 * don't touch primitive_size (since we would clobber
1009 * the pointer there) */
1011 ctx
->payload_tiler
.primitive_size
.constant
= ctx
->rasterizer
->base
.line_width
;
1015 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
1017 ctx
->dirty
|= PAN_DIRTY_FS
;
1019 if (ctx
->dirty
& PAN_DIRTY_FS
) {
1021 struct panfrost_shader_state
*variant
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
1023 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
1026 COPY(attribute_count
);
1027 COPY(varying_count
);
1028 COPY(midgard1
.uniform_count
);
1029 COPY(midgard1
.work_count
);
1030 COPY(midgard1
.unknown2
);
1033 /* If there is a blend shader, work registers are shared */
1035 if (ctx
->blend
->has_blend_shader
)
1036 ctx
->fragment_shader_core
.midgard1
.work_count
= /*MAX2(ctx->fragment_shader_core.midgard1.work_count, ctx->blend->blend_work_count)*/16;
1038 /* Set late due to depending on render state */
1039 /* The one at the end seems to mean "1 UBO" */
1040 ctx
->fragment_shader_core
.midgard1
.unknown1
= MALI_NO_ALPHA_TO_COVERAGE
| 0x200 | 0x2201;
1042 /* Assign texture/sample count right before upload */
1043 ctx
->fragment_shader_core
.texture_count
= ctx
->sampler_view_count
[PIPE_SHADER_FRAGMENT
];
1044 ctx
->fragment_shader_core
.sampler_count
= ctx
->sampler_count
[PIPE_SHADER_FRAGMENT
];
1046 /* Assign the stencil refs late */
1047 ctx
->fragment_shader_core
.stencil_front
.ref
= ctx
->stencil_ref
.ref_value
[0];
1048 ctx
->fragment_shader_core
.stencil_back
.ref
= ctx
->stencil_ref
.ref_value
[1];
1050 /* CAN_DISCARD should be set if the fragment shader possibly
1051 * contains a 'discard' instruction. It is likely this is
1052 * related to optimizations related to forward-pixel kill, as
1053 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
1054 * thing?" by Peter Harris
1057 if (variant
->can_discard
) {
1058 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
1059 ctx
->fragment_shader_core
.midgard1
.unknown1
&= ~MALI_NO_ALPHA_TO_COVERAGE
;
1060 ctx
->fragment_shader_core
.midgard1
.unknown1
|= 0x4000;
1061 ctx
->fragment_shader_core
.midgard1
.unknown1
= 0x4200;
1064 /* Check if we're using the default blend descriptor (fast path) */
1067 !ctx
->blend
->has_blend_shader
&&
1068 (ctx
->blend
->equation
.rgb_mode
== 0x122) &&
1069 (ctx
->blend
->equation
.alpha_mode
== 0x122) &&
1070 (ctx
->blend
->equation
.color_mask
== 0xf);
1072 /* Even on MFBD, the shader descriptor gets blend shaders. It's
1073 * *also* copied to the blend_meta appended (by convention),
1074 * but this is the field actually read by the hardware. (Or
1075 * maybe both are read...?) */
1077 if (ctx
->blend
->has_blend_shader
) {
1078 ctx
->fragment_shader_core
.blend
.shader
= ctx
->blend
->blend_shader
;
1081 if (ctx
->require_sfbd
) {
1082 /* When only a single render target platform is used, the blend
1083 * information is inside the shader meta itself. We
1084 * additionally need to signal CAN_DISCARD for nontrivial blend
1085 * modes (so we're able to read back the destination buffer) */
1087 if (!ctx
->blend
->has_blend_shader
) {
1088 ctx
->fragment_shader_core
.blend
.equation
= ctx
->blend
->equation
;
1089 ctx
->fragment_shader_core
.blend
.constant
= ctx
->blend
->constant
;
1093 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
1097 size_t size
= sizeof(struct mali_shader_meta
) + sizeof(struct midgard_blend_rt
);
1098 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
1099 memcpy(transfer
.cpu
, &ctx
->fragment_shader_core
, sizeof(struct mali_shader_meta
));
1101 ctx
->payload_tiler
.postfix
._shader_upper
= (transfer
.gpu
) >> 4;
1103 if (!ctx
->require_sfbd
) {
1104 /* Additional blend descriptor tacked on for jobs using MFBD */
1106 unsigned blend_count
= 0x200;
1108 if (ctx
->blend
->has_blend_shader
) {
1109 /* For a blend shader, the bottom nibble corresponds to
1110 * the number of work registers used, which signals the
1111 * -existence- of a blend shader */
1113 assert(ctx
->blend
->blend_work_count
>= 2);
1114 blend_count
|= MIN2(ctx
->blend
->blend_work_count
, 3);
1116 /* Otherwise, the bottom bit simply specifies if
1117 * blending (anything other than REPLACE) is enabled */
1124 struct midgard_blend_rt rts
[4];
1128 for (unsigned i
= 0; i
< 1; ++i
) {
1129 rts
[i
].flags
= blend_count
;
1131 if (ctx
->blend
->has_blend_shader
) {
1132 rts
[i
].blend
.shader
= ctx
->blend
->blend_shader
;
1134 rts
[i
].blend
.equation
= ctx
->blend
->equation
;
1135 rts
[i
].blend
.constant
= ctx
->blend
->constant
;
1139 memcpy(transfer
.cpu
+ sizeof(struct mali_shader_meta
), rts
, sizeof(rts
[0]) * 1);
1143 /* We stage to transient, so always dirty.. */
1144 panfrost_stage_attributes(ctx
);
1146 if (ctx
->dirty
& PAN_DIRTY_SAMPLERS
)
1147 panfrost_upload_sampler_descriptors(ctx
);
1149 if (ctx
->dirty
& PAN_DIRTY_TEXTURES
)
1150 panfrost_upload_texture_descriptors(ctx
);
1152 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
1154 for (int i
= 0; i
<= PIPE_SHADER_FRAGMENT
; ++i
) {
1155 struct panfrost_constant_buffer
*buf
= &ctx
->constant_buffer
[i
];
1157 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
1158 struct panfrost_shader_state
*fs
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
1159 struct panfrost_shader_state
*ss
= (i
== PIPE_SHADER_FRAGMENT
) ? fs
: vs
;
1161 /* Allocate room for the sysval and the uniforms */
1162 size_t sys_size
= sizeof(float) * 4 * ss
->sysval_count
;
1163 size_t size
= sys_size
+ buf
->size
;
1164 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
1166 /* Upload sysvals requested by the shader */
1167 float *uniforms
= (float *) transfer
.cpu
;
1168 for (unsigned i
= 0; i
< ss
->sysval_count
; ++i
) {
1169 int sysval
= ss
->sysval
[i
];
1171 if (sysval
== PAN_SYSVAL_VIEWPORT_SCALE
) {
1172 uniforms
[4*i
+ 0] = vp
->scale
[0];
1173 uniforms
[4*i
+ 1] = vp
->scale
[1];
1174 uniforms
[4*i
+ 2] = vp
->scale
[2];
1175 } else if (sysval
== PAN_SYSVAL_VIEWPORT_OFFSET
) {
1176 uniforms
[4*i
+ 0] = vp
->translate
[0];
1177 uniforms
[4*i
+ 1] = vp
->translate
[1];
1178 uniforms
[4*i
+ 2] = vp
->translate
[2];
1184 /* Upload uniforms */
1185 memcpy(transfer
.cpu
+ sys_size
, buf
->buffer
, buf
->size
);
1187 int uniform_count
= 0;
1189 struct mali_vertex_tiler_postfix
*postfix
;
1192 case PIPE_SHADER_VERTEX
:
1193 uniform_count
= ctx
->vs
->variants
[ctx
->vs
->active_variant
].uniform_count
;
1194 postfix
= &ctx
->payload_vertex
.postfix
;
1197 case PIPE_SHADER_FRAGMENT
:
1198 uniform_count
= ctx
->fs
->variants
[ctx
->fs
->active_variant
].uniform_count
;
1199 postfix
= &ctx
->payload_tiler
.postfix
;
1203 unreachable("Invalid shader stage\n");
1206 /* Also attach the same buffer as a UBO for extended access */
1208 struct mali_uniform_buffer_meta uniform_buffers
[] = {
1210 .size
= MALI_POSITIVE((2 + uniform_count
)),
1211 .ptr
= transfer
.gpu
>> 2,
1215 mali_ptr ubufs
= panfrost_upload_transient(ctx
, uniform_buffers
, sizeof(uniform_buffers
));
1216 postfix
->uniforms
= transfer
.gpu
;
1217 postfix
->uniform_buffers
= ubufs
;
1222 /* TODO: Upload the viewport somewhere more appropriate */
1224 /* Clip bounds are encoded as floats. The viewport itself is encoded as
1225 * (somewhat) asymmetric ints. */
1226 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
1228 struct mali_viewport view
= {
1229 /* By default, do no viewport clipping, i.e. clip to (-inf,
1230 * inf) in each direction. Clipping to the viewport in theory
1231 * should work, but in practice causes issues when we're not
1232 * explicitly trying to scissor */
1243 /* Always scissor to the viewport by default. */
1244 view
.viewport0
[0] = (int) (vp
->translate
[0] - vp
->scale
[0]);
1245 view
.viewport1
[0] = MALI_POSITIVE((int) (vp
->translate
[0] + vp
->scale
[0]));
1247 int miny
= (int) (vp
->translate
[1] - vp
->scale
[1]);
1248 int maxy
= (int) (vp
->translate
[1] + vp
->scale
[1]);
1250 if (ss
&& ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
) {
1251 view
.viewport0
[0] = ss
->minx
;
1252 view
.viewport1
[0] = MALI_POSITIVE(ss
->maxx
);
1258 /* Hardware needs the min/max to be strictly ordered, so flip if we
1266 view
.viewport0
[1] = miny
;
1267 view
.viewport1
[1] = MALI_POSITIVE(maxy
);
1269 ctx
->payload_tiler
.postfix
.viewport
=
1270 panfrost_upload_transient(ctx
,
1272 sizeof(struct mali_viewport
));
1277 /* Corresponds to exactly one draw, but does not submit anything */
1280 panfrost_queue_draw(struct panfrost_context
*ctx
)
1282 /* TODO: Expand the array? */
1283 if (ctx
->draw_count
>= MAX_DRAW_CALLS
) {
1284 DBG("Job buffer overflow, ignoring draw\n");
1288 /* Handle dirty flags now */
1289 panfrost_emit_for_draw(ctx
, true);
1291 /* We need a set_value job before any other draw jobs */
1292 if (ctx
->draw_count
== 0)
1293 panfrost_set_value_job(ctx
);
1295 struct panfrost_transfer vertex
= panfrost_vertex_tiler_job(ctx
, false);
1296 ctx
->u_vertex_jobs
[ctx
->vertex_job_count
] = (struct mali_job_descriptor_header
*) vertex
.cpu
;
1297 ctx
->vertex_jobs
[ctx
->vertex_job_count
++] = vertex
.gpu
;
1299 struct panfrost_transfer tiler
= panfrost_vertex_tiler_job(ctx
, true);
1300 ctx
->u_tiler_jobs
[ctx
->tiler_job_count
] = (struct mali_job_descriptor_header
*) tiler
.cpu
;
1301 ctx
->tiler_jobs
[ctx
->tiler_job_count
++] = tiler
.gpu
;
1306 /* The entire frame is in memory -- send it off to the kernel! */
1309 panfrost_submit_frame(struct panfrost_context
*ctx
, bool flush_immediate
,
1310 struct pipe_fence_handle
**fence
,
1311 struct panfrost_job
*job
)
1313 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
1314 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
1316 /* Edge case if screen is cleared and nothing else */
1317 bool has_draws
= ctx
->draw_count
> 0;
1319 /* Workaround a bizarre lockup (a hardware errata?) */
1321 flush_immediate
= true;
1325 bool is_scanout
= panfrost_is_scanout(ctx
);
1326 screen
->driver
->submit_vs_fs_job(ctx
, has_draws
, is_scanout
);
1328 /* If visual, we can stall a frame */
1330 if (!flush_immediate
)
1331 screen
->driver
->force_flush_fragment(ctx
, fence
);
1333 screen
->last_fragment_flushed
= false;
1334 screen
->last_job
= job
;
1336 /* If readback, flush now (hurts the pipelined performance) */
1337 if (flush_immediate
)
1338 screen
->driver
->force_flush_fragment(ctx
, fence
);
1340 if (screen
->driver
->dump_counters
&& pan_counters_base
) {
1341 screen
->driver
->dump_counters(screen
);
1344 snprintf(filename
, sizeof(filename
), "%s/frame%d.mdgprf", pan_counters_base
, ++performance_counter_number
);
1345 FILE *fp
= fopen(filename
, "wb");
1346 fwrite(screen
->perf_counters
.cpu
, 4096, sizeof(uint32_t), fp
);
1354 panfrost_draw_wallpaper(struct pipe_context
*pipe
)
1356 struct panfrost_context
*ctx
= pan_context(pipe
);
1358 /* Nothing to reload? */
1359 if (ctx
->pipe_framebuffer
.cbufs
[0] == NULL
)
1362 /* Blit the wallpaper in */
1363 panfrost_blit_wallpaper(ctx
);
1365 /* We are flushing all queued draws and we know that no more jobs will
1366 * be added until the next frame.
1367 * We also know that the last jobs are the wallpaper jobs, and they
1368 * need to be linked so they execute right after the set_value job.
1371 /* set_value job to wallpaper vertex job */
1372 panfrost_link_job_pair(ctx
->u_set_value_job
, ctx
->vertex_jobs
[ctx
->vertex_job_count
- 1]);
1373 ctx
->u_vertex_jobs
[ctx
->vertex_job_count
- 1]->job_dependency_index_1
= ctx
->u_set_value_job
->job_index
;
1375 /* wallpaper vertex job to first vertex job */
1376 panfrost_link_job_pair(ctx
->u_vertex_jobs
[ctx
->vertex_job_count
- 1], ctx
->vertex_jobs
[0]);
1377 ctx
->u_vertex_jobs
[0]->job_dependency_index_1
= ctx
->u_set_value_job
->job_index
;
1379 /* last vertex job to wallpaper tiler job */
1380 panfrost_link_job_pair(ctx
->u_vertex_jobs
[ctx
->vertex_job_count
- 2], ctx
->tiler_jobs
[ctx
->tiler_job_count
- 1]);
1381 ctx
->u_tiler_jobs
[ctx
->tiler_job_count
- 1]->job_dependency_index_1
= ctx
->u_vertex_jobs
[ctx
->vertex_job_count
- 1]->job_index
;
1382 ctx
->u_tiler_jobs
[ctx
->tiler_job_count
- 1]->job_dependency_index_2
= 0;
1384 /* wallpaper tiler job to first tiler job */
1385 panfrost_link_job_pair(ctx
->u_tiler_jobs
[ctx
->tiler_job_count
- 1], ctx
->tiler_jobs
[0]);
1386 ctx
->u_tiler_jobs
[0]->job_dependency_index_1
= ctx
->u_vertex_jobs
[0]->job_index
;
1387 ctx
->u_tiler_jobs
[0]->job_dependency_index_2
= ctx
->u_tiler_jobs
[ctx
->tiler_job_count
- 1]->job_index
;
1389 /* last tiler job to NULL */
1390 panfrost_link_job_pair(ctx
->u_tiler_jobs
[ctx
->tiler_job_count
- 2], 0);
1395 struct pipe_context
*pipe
,
1396 struct pipe_fence_handle
**fence
,
1399 struct panfrost_context
*ctx
= pan_context(pipe
);
1400 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
1402 /* Nothing to do! */
1403 if (!ctx
->draw_count
&& !job
->clear
) return;
1406 panfrost_draw_wallpaper(&ctx
->base
);
1408 /* Whether to stall the pipeline for immediately correct results */
1409 bool flush_immediate
= flags
& PIPE_FLUSH_END_OF_FRAME
;
1411 /* Submit the frame itself */
1412 panfrost_submit_frame(ctx
, flush_immediate
, fence
, job
);
1414 /* Prepare for the next frame */
1415 panfrost_invalidate_frame(ctx
);
1418 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1421 g2m_draw_mode(enum pipe_prim_type mode
)
1424 DEFINE_CASE(POINTS
);
1426 DEFINE_CASE(LINE_LOOP
);
1427 DEFINE_CASE(LINE_STRIP
);
1428 DEFINE_CASE(TRIANGLES
);
1429 DEFINE_CASE(TRIANGLE_STRIP
);
1430 DEFINE_CASE(TRIANGLE_FAN
);
1432 DEFINE_CASE(QUAD_STRIP
);
1433 DEFINE_CASE(POLYGON
);
1436 unreachable("Invalid draw mode");
1443 panfrost_translate_index_size(unsigned size
)
1447 return MALI_DRAW_INDEXED_UINT8
;
1450 return MALI_DRAW_INDEXED_UINT16
;
1453 return MALI_DRAW_INDEXED_UINT32
;
1456 unreachable("Invalid index size");
1460 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1461 * good for the duration of the draw (transient), could last longer */
1464 panfrost_get_index_buffer_mapped(struct panfrost_context
*ctx
, const struct pipe_draw_info
*info
)
1466 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (info
->index
.resource
);
1468 off_t offset
= info
->start
* info
->index_size
;
1470 if (!info
->has_user_indices
) {
1471 /* Only resources can be directly mapped */
1472 return rsrc
->bo
->gpu
+ offset
;
1474 /* Otherwise, we need to upload to transient memory */
1475 const uint8_t *ibuf8
= (const uint8_t *) info
->index
.user
;
1476 return panfrost_upload_transient(ctx
, ibuf8
+ offset
, info
->count
* info
->index_size
);
1482 struct pipe_context
*pipe
,
1483 const struct pipe_draw_info
*info
)
1485 struct panfrost_context
*ctx
= pan_context(pipe
);
1487 ctx
->payload_vertex
.draw_start
= info
->start
;
1488 ctx
->payload_tiler
.draw_start
= info
->start
;
1490 int mode
= info
->mode
;
1492 /* Fallback for unsupported modes */
1494 if (!(ctx
->draw_modes
& (1 << mode
))) {
1495 if (mode
== PIPE_PRIM_QUADS
&& info
->count
== 4 && ctx
->rasterizer
&& !ctx
->rasterizer
->base
.flatshade
) {
1496 mode
= PIPE_PRIM_TRIANGLE_FAN
;
1498 if (info
->count
< 4) {
1499 /* Degenerate case? */
1503 util_primconvert_save_rasterizer_state(ctx
->primconvert
, &ctx
->rasterizer
->base
);
1504 util_primconvert_draw_vbo(ctx
->primconvert
, info
);
1509 /* Now that we have a guaranteed terminating path, find the job.
1510 * Assignment commented out to prevent unused warning */
1512 /* struct panfrost_job *job = */ panfrost_get_job_for_fbo(ctx
);
1514 ctx
->payload_tiler
.prefix
.draw_mode
= g2m_draw_mode(mode
);
1516 ctx
->vertex_count
= info
->count
;
1518 /* For non-indexed draws, they're the same */
1519 unsigned invocation_count
= ctx
->vertex_count
;
1521 unsigned draw_flags
= 0;
1523 /* The draw flags interpret how primitive size is interpreted */
1525 if (panfrost_writes_point_size(ctx
))
1526 draw_flags
|= MALI_DRAW_VARYING_SIZE
;
1528 /* For higher amounts of vertices (greater than what fits in a 16-bit
1529 * short), the other value is needed, otherwise there will be bizarre
1530 * rendering artefacts. It's not clear what these values mean yet. */
1532 draw_flags
|= (mode
== PIPE_PRIM_POINTS
|| ctx
->vertex_count
> 65535) ? 0x3000 : 0x18000;
1534 if (info
->index_size
) {
1535 /* Calculate the min/max index used so we can figure out how
1536 * many times to invoke the vertex shader */
1538 /* Fetch / calculate index bounds */
1539 unsigned min_index
= 0, max_index
= 0;
1541 if (info
->max_index
== ~0u) {
1542 u_vbuf_get_minmax_index(pipe
, info
, &min_index
, &max_index
);
1544 min_index
= info
->min_index
;
1545 max_index
= info
->max_index
;
1548 /* Use the corresponding values */
1549 invocation_count
= max_index
- min_index
+ 1;
1550 ctx
->payload_vertex
.draw_start
= min_index
;
1551 ctx
->payload_tiler
.draw_start
= min_index
;
1553 ctx
->payload_tiler
.prefix
.negative_start
= -min_index
;
1554 ctx
->payload_tiler
.prefix
.index_count
= MALI_POSITIVE(info
->count
);
1556 //assert(!info->restart_index); /* TODO: Research */
1557 assert(!info
->index_bias
);
1559 draw_flags
|= panfrost_translate_index_size(info
->index_size
);
1560 ctx
->payload_tiler
.prefix
.indices
= panfrost_get_index_buffer_mapped(ctx
, info
);
1562 /* Index count == vertex count, if no indexing is applied, as
1563 * if it is internally indexed in the expected order */
1565 ctx
->payload_tiler
.prefix
.negative_start
= 0;
1566 ctx
->payload_tiler
.prefix
.index_count
= MALI_POSITIVE(ctx
->vertex_count
);
1568 /* Reverse index state */
1569 ctx
->payload_tiler
.prefix
.indices
= (uintptr_t) NULL
;
1572 ctx
->payload_vertex
.prefix
.invocation_count
= MALI_POSITIVE(invocation_count
);
1573 ctx
->payload_tiler
.prefix
.invocation_count
= MALI_POSITIVE(invocation_count
);
1574 ctx
->payload_tiler
.prefix
.unknown_draw
= draw_flags
;
1576 /* Fire off the draw itself */
1577 panfrost_queue_draw(ctx
);
1583 panfrost_generic_cso_delete(struct pipe_context
*pctx
, void *hwcso
)
1589 panfrost_create_rasterizer_state(
1590 struct pipe_context
*pctx
,
1591 const struct pipe_rasterizer_state
*cso
)
1593 struct panfrost_context
*ctx
= pan_context(pctx
);
1594 struct panfrost_rasterizer
*so
= CALLOC_STRUCT(panfrost_rasterizer
);
1598 /* Bitmask, unknown meaning of the start value */
1599 so
->tiler_gl_enables
= ctx
->is_t6xx
? 0x105 : 0x7;
1602 so
->tiler_gl_enables
|= MALI_FRONT_CCW_TOP
;
1604 if (cso
->cull_face
& PIPE_FACE_FRONT
)
1605 so
->tiler_gl_enables
|= MALI_CULL_FACE_FRONT
;
1607 if (cso
->cull_face
& PIPE_FACE_BACK
)
1608 so
->tiler_gl_enables
|= MALI_CULL_FACE_BACK
;
1614 panfrost_bind_rasterizer_state(
1615 struct pipe_context
*pctx
,
1618 struct panfrost_context
*ctx
= pan_context(pctx
);
1620 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1624 ctx
->rasterizer
= hwcso
;
1625 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
1629 panfrost_create_vertex_elements_state(
1630 struct pipe_context
*pctx
,
1631 unsigned num_elements
,
1632 const struct pipe_vertex_element
*elements
)
1634 struct panfrost_vertex_state
*so
= CALLOC_STRUCT(panfrost_vertex_state
);
1636 so
->num_elements
= num_elements
;
1637 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
1639 /* XXX: What the cornball? This is totally, 100%, unapologetically
1640 * nonsense. And yet it somehow fixes a regression in -bshadow
1641 * (previously, we allocated the descriptor here... a newer commit
1642 * removed that allocation, and then memory corruption led to
1643 * shader_meta getting overwritten in bad ways and then the whole test
1644 * case falling apart . TODO: LOOK INTO PLEASE XXX XXX BAD XXX XXX XXX
1646 panfrost_allocate_chunk(pan_context(pctx
), 0, HEAP_DESCRIPTOR
);
1648 for (int i
= 0; i
< num_elements
; ++i
) {
1649 so
->hw
[i
].index
= elements
[i
].vertex_buffer_index
;
1651 enum pipe_format fmt
= elements
[i
].src_format
;
1652 const struct util_format_description
*desc
= util_format_description(fmt
);
1653 so
->hw
[i
].unknown1
= 0x2;
1654 so
->hw
[i
].swizzle
= panfrost_get_default_swizzle(desc
->nr_channels
);
1656 so
->hw
[i
].format
= panfrost_find_format(desc
);
1658 /* The field itself should probably be shifted over */
1659 so
->hw
[i
].src_offset
= elements
[i
].src_offset
;
1666 panfrost_bind_vertex_elements_state(
1667 struct pipe_context
*pctx
,
1670 struct panfrost_context
*ctx
= pan_context(pctx
);
1672 ctx
->vertex
= hwcso
;
1673 ctx
->dirty
|= PAN_DIRTY_VERTEX
;
1677 panfrost_create_shader_state(
1678 struct pipe_context
*pctx
,
1679 const struct pipe_shader_state
*cso
)
1681 struct panfrost_shader_variants
*so
= CALLOC_STRUCT(panfrost_shader_variants
);
1684 /* Token deep copy to prevent memory corruption */
1686 if (cso
->type
== PIPE_SHADER_IR_TGSI
)
1687 so
->base
.tokens
= tgsi_dup_tokens(so
->base
.tokens
);
1693 panfrost_delete_shader_state(
1694 struct pipe_context
*pctx
,
1697 struct panfrost_shader_variants
*cso
= (struct panfrost_shader_variants
*) so
;
1699 if (cso
->base
.type
== PIPE_SHADER_IR_TGSI
) {
1700 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1707 panfrost_create_sampler_state(
1708 struct pipe_context
*pctx
,
1709 const struct pipe_sampler_state
*cso
)
1711 struct panfrost_sampler_state
*so
= CALLOC_STRUCT(panfrost_sampler_state
);
1714 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1716 struct mali_sampler_descriptor sampler_descriptor
= {
1717 .filter_mode
= MALI_TEX_MIN(translate_tex_filter(cso
->min_img_filter
))
1718 | MALI_TEX_MAG(translate_tex_filter(cso
->mag_img_filter
))
1719 | translate_mip_filter(cso
->min_mip_filter
)
1722 .wrap_s
= translate_tex_wrap(cso
->wrap_s
),
1723 .wrap_t
= translate_tex_wrap(cso
->wrap_t
),
1724 .wrap_r
= translate_tex_wrap(cso
->wrap_r
),
1725 .compare_func
= panfrost_translate_alt_compare_func(cso
->compare_func
),
1727 cso
->border_color
.f
[0],
1728 cso
->border_color
.f
[1],
1729 cso
->border_color
.f
[2],
1730 cso
->border_color
.f
[3]
1732 .min_lod
= FIXED_16(cso
->min_lod
),
1733 .max_lod
= FIXED_16(cso
->max_lod
),
1737 so
->hw
= sampler_descriptor
;
1743 panfrost_bind_sampler_states(
1744 struct pipe_context
*pctx
,
1745 enum pipe_shader_type shader
,
1746 unsigned start_slot
, unsigned num_sampler
,
1749 assert(start_slot
== 0);
1751 struct panfrost_context
*ctx
= pan_context(pctx
);
1753 /* XXX: Should upload, not just copy? */
1754 ctx
->sampler_count
[shader
] = num_sampler
;
1755 memcpy(ctx
->samplers
[shader
], sampler
, num_sampler
* sizeof (void *));
1757 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
;
1761 panfrost_variant_matches(struct panfrost_context
*ctx
, struct panfrost_shader_state
*variant
)
1763 struct pipe_alpha_state
*alpha
= &ctx
->depth_stencil
->alpha
;
1765 if (alpha
->enabled
|| variant
->alpha_state
.enabled
) {
1766 /* Make sure enable state is at least the same */
1767 if (alpha
->enabled
!= variant
->alpha_state
.enabled
) {
1771 /* Check that the contents of the test are the same */
1772 bool same_func
= alpha
->func
== variant
->alpha_state
.func
;
1773 bool same_ref
= alpha
->ref_value
== variant
->alpha_state
.ref_value
;
1775 if (!(same_func
&& same_ref
)) {
1779 /* Otherwise, we're good to go */
1784 panfrost_bind_fs_state(
1785 struct pipe_context
*pctx
,
1788 struct panfrost_context
*ctx
= pan_context(pctx
);
1793 /* Match the appropriate variant */
1795 signed variant
= -1;
1797 struct panfrost_shader_variants
*variants
= (struct panfrost_shader_variants
*) hwcso
;
1799 for (unsigned i
= 0; i
< variants
->variant_count
; ++i
) {
1800 if (panfrost_variant_matches(ctx
, &variants
->variants
[i
])) {
1806 if (variant
== -1) {
1807 /* No variant matched, so create a new one */
1808 variant
= variants
->variant_count
++;
1809 assert(variants
->variant_count
< MAX_SHADER_VARIANTS
);
1811 variants
->variants
[variant
].base
= hwcso
;
1812 variants
->variants
[variant
].alpha_state
= ctx
->depth_stencil
->alpha
;
1814 /* Allocate the mapped descriptor ahead-of-time. TODO: Use for FS as well as VS */
1815 struct panfrost_context
*ctx
= pan_context(pctx
);
1816 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_shader_meta
), HEAP_DESCRIPTOR
);
1818 variants
->variants
[variant
].tripipe
= (struct mali_shader_meta
*) transfer
.cpu
;
1819 variants
->variants
[variant
].tripipe_gpu
= transfer
.gpu
;
1823 /* Select this variant */
1824 variants
->active_variant
= variant
;
1826 struct panfrost_shader_state
*shader_state
= &variants
->variants
[variant
];
1827 assert(panfrost_variant_matches(ctx
, shader_state
));
1829 /* Now we have a variant selected, so compile and go */
1831 if (!shader_state
->compiled
) {
1832 panfrost_shader_compile(ctx
, shader_state
->tripipe
, NULL
, JOB_TYPE_TILER
, shader_state
);
1833 shader_state
->compiled
= true;
1837 ctx
->dirty
|= PAN_DIRTY_FS
;
1841 panfrost_bind_vs_state(
1842 struct pipe_context
*pctx
,
1845 struct panfrost_context
*ctx
= pan_context(pctx
);
1850 if (!ctx
->vs
->variants
[0].compiled
) {
1851 ctx
->vs
->variants
[0].base
= hwcso
;
1853 /* TODO DRY from above */
1854 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_shader_meta
), HEAP_DESCRIPTOR
);
1855 ctx
->vs
->variants
[0].tripipe
= (struct mali_shader_meta
*) transfer
.cpu
;
1856 ctx
->vs
->variants
[0].tripipe_gpu
= transfer
.gpu
;
1858 panfrost_shader_compile(ctx
, ctx
->vs
->variants
[0].tripipe
, NULL
, JOB_TYPE_VERTEX
, &ctx
->vs
->variants
[0]);
1859 ctx
->vs
->variants
[0].compiled
= true;
1863 ctx
->dirty
|= PAN_DIRTY_VS
;
1867 panfrost_set_vertex_buffers(
1868 struct pipe_context
*pctx
,
1869 unsigned start_slot
,
1870 unsigned num_buffers
,
1871 const struct pipe_vertex_buffer
*buffers
)
1873 struct panfrost_context
*ctx
= pan_context(pctx
);
1875 util_set_vertex_buffers_mask(ctx
->vertex_buffers
, &ctx
->vb_mask
, buffers
, start_slot
, num_buffers
);
1879 panfrost_set_constant_buffer(
1880 struct pipe_context
*pctx
,
1881 enum pipe_shader_type shader
, uint index
,
1882 const struct pipe_constant_buffer
*buf
)
1884 struct panfrost_context
*ctx
= pan_context(pctx
);
1885 struct panfrost_constant_buffer
*pbuf
= &ctx
->constant_buffer
[shader
];
1887 size_t sz
= buf
? buf
->buffer_size
: 0;
1889 /* Free previous buffer */
1896 pbuf
->buffer
= NULL
;
1899 /* If unbinding, we're done */
1904 /* Multiple constant buffers not yet supported */
1909 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
);
1912 cpu
= rsrc
->bo
->cpu
;
1913 } else if (buf
->user_buffer
) {
1914 cpu
= buf
->user_buffer
;
1916 DBG("No constant buffer?\n");
1920 /* Copy the constant buffer into the driver context for later upload */
1922 pbuf
->buffer
= malloc(sz
);
1923 memcpy(pbuf
->buffer
, cpu
+ buf
->buffer_offset
, sz
);
1927 panfrost_set_stencil_ref(
1928 struct pipe_context
*pctx
,
1929 const struct pipe_stencil_ref
*ref
)
1931 struct panfrost_context
*ctx
= pan_context(pctx
);
1932 ctx
->stencil_ref
= *ref
;
1934 /* Shader core dirty */
1935 ctx
->dirty
|= PAN_DIRTY_FS
;
1938 static struct pipe_sampler_view
*
1939 panfrost_create_sampler_view(
1940 struct pipe_context
*pctx
,
1941 struct pipe_resource
*texture
,
1942 const struct pipe_sampler_view
*template)
1944 struct panfrost_sampler_view
*so
= CALLOC_STRUCT(panfrost_sampler_view
);
1945 int bytes_per_pixel
= util_format_get_blocksize(texture
->format
);
1947 pipe_reference(NULL
, &texture
->reference
);
1949 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) texture
;
1952 so
->base
= *template;
1953 so
->base
.texture
= texture
;
1954 so
->base
.reference
.count
= 1;
1955 so
->base
.context
= pctx
;
1957 /* sampler_views correspond to texture descriptors, minus the texture
1958 * (data) itself. So, we serialise the descriptor here and cache it for
1961 /* Make sure it's something with which we're familiar */
1962 assert(bytes_per_pixel
>= 1 && bytes_per_pixel
<= 4);
1964 /* TODO: Detect from format better */
1965 const struct util_format_description
*desc
= util_format_description(prsrc
->base
.format
);
1967 unsigned char user_swizzle
[4] = {
1968 template->swizzle_r
,
1969 template->swizzle_g
,
1970 template->swizzle_b
,
1974 enum mali_format format
= panfrost_find_format(desc
);
1976 bool is_depth
= desc
->format
== PIPE_FORMAT_Z32_UNORM
;
1978 unsigned usage2_layout
= 0x10;
1980 switch (prsrc
->bo
->layout
) {
1982 usage2_layout
|= 0x8 | 0x4;
1985 usage2_layout
|= 0x1;
1988 usage2_layout
|= is_depth
? 0x1 : 0x2;
1995 /* Check if we need to set a custom stride by computing the "expected"
1996 * stride and comparing it to what the BO actually wants. Only applies
1997 * to linear textures TODO: Mipmap? */
1999 unsigned actual_stride
= prsrc
->bo
->slices
[0].stride
;
2001 if (prsrc
->bo
->layout
== PAN_LINEAR
&&
2002 template->u
.tex
.last_level
== 0 &&
2003 template->u
.tex
.first_level
== 0 &&
2004 (texture
->width0
* bytes_per_pixel
) != actual_stride
) {
2005 usage2_layout
|= MALI_TEX_MANUAL_STRIDE
;
2008 struct mali_texture_descriptor texture_descriptor
= {
2009 .width
= MALI_POSITIVE(texture
->width0
),
2010 .height
= MALI_POSITIVE(texture
->height0
),
2011 .depth
= MALI_POSITIVE(texture
->depth0
),
2015 .swizzle
= panfrost_translate_swizzle_4(desc
->swizzle
),
2019 .is_not_cubemap
= texture
->target
!= PIPE_TEXTURE_CUBE
,
2021 .usage2
= usage2_layout
2024 .swizzle
= panfrost_translate_swizzle_4(user_swizzle
)
2027 /* TODO: Other base levels require adjusting dimensions / level numbers / etc */
2028 assert (template->u
.tex
.first_level
== 0);
2030 /* Disable mipmapping for now to avoid regressions while automipmapping
2031 * is being implemented. TODO: Remove me once automipmaps work */
2033 //texture_descriptor.nr_mipmap_levels = template->u.tex.last_level - template->u.tex.first_level;
2034 texture_descriptor
.nr_mipmap_levels
= 0;
2036 so
->hw
= texture_descriptor
;
2038 return (struct pipe_sampler_view
*) so
;
2042 panfrost_set_sampler_views(
2043 struct pipe_context
*pctx
,
2044 enum pipe_shader_type shader
,
2045 unsigned start_slot
, unsigned num_views
,
2046 struct pipe_sampler_view
**views
)
2048 struct panfrost_context
*ctx
= pan_context(pctx
);
2050 assert(start_slot
== 0);
2052 ctx
->sampler_view_count
[shader
] = num_views
;
2053 memcpy(ctx
->sampler_views
[shader
], views
, num_views
* sizeof (void *));
2055 ctx
->dirty
|= PAN_DIRTY_TEXTURES
;
2059 panfrost_sampler_view_destroy(
2060 struct pipe_context
*pctx
,
2061 struct pipe_sampler_view
*view
)
2063 pipe_resource_reference(&view
->texture
, NULL
);
2068 panfrost_set_framebuffer_state(struct pipe_context
*pctx
,
2069 const struct pipe_framebuffer_state
*fb
)
2071 struct panfrost_context
*ctx
= pan_context(pctx
);
2073 /* Flush when switching away from an FBO, but not if the framebuffer
2074 * state is being restored by u_blitter
2077 if (!panfrost_is_scanout(ctx
) && !ctx
->blitter
->running
) {
2078 panfrost_flush(pctx
, NULL
, 0);
2081 ctx
->pipe_framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
2082 ctx
->pipe_framebuffer
.samples
= fb
->samples
;
2083 ctx
->pipe_framebuffer
.layers
= fb
->layers
;
2084 ctx
->pipe_framebuffer
.width
= fb
->width
;
2085 ctx
->pipe_framebuffer
.height
= fb
->height
;
2087 for (int i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2088 struct pipe_surface
*cb
= i
< fb
->nr_cbufs
? fb
->cbufs
[i
] : NULL
;
2090 /* check if changing cbuf */
2091 if (ctx
->pipe_framebuffer
.cbufs
[i
] == cb
) continue;
2093 if (cb
&& (i
!= 0)) {
2094 DBG("XXX: Multiple render targets not supported before t7xx!\n");
2099 pipe_surface_reference(&ctx
->pipe_framebuffer
.cbufs
[i
], cb
);
2104 if (ctx
->require_sfbd
)
2105 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
2107 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
2109 panfrost_attach_vt_framebuffer(ctx
);
2111 struct panfrost_resource
*tex
= ((struct panfrost_resource
*) ctx
->pipe_framebuffer
.cbufs
[i
]->texture
);
2112 enum pipe_format format
= ctx
->pipe_framebuffer
.cbufs
[i
]->format
;
2113 bool is_scanout
= panfrost_is_scanout(ctx
);
2115 bool can_afbc
= panfrost_format_supports_afbc(format
);
2117 if (!is_scanout
&& tex
->bo
->layout
!= PAN_AFBC
&& can_afbc
)
2118 panfrost_enable_afbc(ctx
, tex
, false);
2120 if (!is_scanout
&& !tex
->bo
->has_checksum
)
2121 panfrost_enable_checksum(ctx
, tex
);
2125 struct pipe_surface
*zb
= fb
->zsbuf
;
2127 if (ctx
->pipe_framebuffer
.zsbuf
!= zb
) {
2128 pipe_surface_reference(&ctx
->pipe_framebuffer
.zsbuf
, zb
);
2133 if (ctx
->require_sfbd
)
2134 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
2136 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
2138 panfrost_attach_vt_framebuffer(ctx
);
2140 /* Keep the depth FBO linear */
2147 panfrost_create_blend_state(struct pipe_context
*pipe
,
2148 const struct pipe_blend_state
*blend
)
2150 struct panfrost_context
*ctx
= pan_context(pipe
);
2151 struct panfrost_blend_state
*so
= CALLOC_STRUCT(panfrost_blend_state
);
2154 /* TODO: The following features are not yet implemented */
2155 assert(!blend
->logicop_enable
);
2156 assert(!blend
->alpha_to_coverage
);
2157 assert(!blend
->alpha_to_one
);
2159 /* Compile the blend state, first as fixed-function if we can */
2161 if (panfrost_make_fixed_blend_mode(&blend
->rt
[0], so
, blend
->rt
[0].colormask
, &ctx
->blend_color
))
2164 /* If we can't, compile a blend shader instead */
2166 panfrost_make_blend_shader(ctx
, so
, &ctx
->blend_color
);
2172 panfrost_bind_blend_state(struct pipe_context
*pipe
,
2175 struct panfrost_context
*ctx
= pan_context(pipe
);
2176 struct pipe_blend_state
*blend
= (struct pipe_blend_state
*) cso
;
2177 struct panfrost_blend_state
*pblend
= (struct panfrost_blend_state
*) cso
;
2178 ctx
->blend
= pblend
;
2183 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_DITHER
, !blend
->dither
);
2185 /* TODO: Attach color */
2187 /* Shader itself is not dirty, but the shader core is */
2188 ctx
->dirty
|= PAN_DIRTY_FS
;
2192 panfrost_delete_blend_state(struct pipe_context
*pipe
,
2195 struct panfrost_blend_state
*so
= (struct panfrost_blend_state
*) blend
;
2197 if (so
->has_blend_shader
) {
2198 DBG("Deleting blend state leak blend shaders bytecode\n");
2205 panfrost_set_blend_color(struct pipe_context
*pipe
,
2206 const struct pipe_blend_color
*blend_color
)
2208 struct panfrost_context
*ctx
= pan_context(pipe
);
2210 /* If blend_color is we're unbinding, so ctx->blend_color is now undefined -> nothing to do */
2213 ctx
->blend_color
= *blend_color
;
2215 /* The blend mode depends on the blend constant color, due to the
2216 * fixed/programmable split. So, we're forced to regenerate the blend
2219 /* TODO: Attach color */
2224 panfrost_create_depth_stencil_state(struct pipe_context
*pipe
,
2225 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
2227 return mem_dup(depth_stencil
, sizeof(*depth_stencil
));
2231 panfrost_bind_depth_stencil_state(struct pipe_context
*pipe
,
2234 struct panfrost_context
*ctx
= pan_context(pipe
);
2235 struct pipe_depth_stencil_alpha_state
*depth_stencil
= cso
;
2236 ctx
->depth_stencil
= depth_stencil
;
2241 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2242 * emulated in the fragment shader */
2244 if (depth_stencil
->alpha
.enabled
) {
2245 /* We need to trigger a new shader (maybe) */
2246 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->fs
);
2250 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_STENCIL_TEST
, depth_stencil
->stencil
[0].enabled
); /* XXX: which one? */
2252 panfrost_make_stencil_state(&depth_stencil
->stencil
[0], &ctx
->fragment_shader_core
.stencil_front
);
2253 ctx
->fragment_shader_core
.stencil_mask_front
= depth_stencil
->stencil
[0].writemask
;
2255 panfrost_make_stencil_state(&depth_stencil
->stencil
[1], &ctx
->fragment_shader_core
.stencil_back
);
2256 ctx
->fragment_shader_core
.stencil_mask_back
= depth_stencil
->stencil
[1].writemask
;
2258 /* Depth state (TODO: Refactor) */
2259 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_DEPTH_TEST
, depth_stencil
->depth
.enabled
);
2261 int func
= depth_stencil
->depth
.enabled
? depth_stencil
->depth
.func
: PIPE_FUNC_ALWAYS
;
2263 ctx
->fragment_shader_core
.unknown2_3
&= ~MALI_DEPTH_FUNC_MASK
;
2264 ctx
->fragment_shader_core
.unknown2_3
|= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func
));
2266 /* Bounds test not implemented */
2267 assert(!depth_stencil
->depth
.bounds_test
);
2269 ctx
->dirty
|= PAN_DIRTY_FS
;
2273 panfrost_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
2279 panfrost_set_sample_mask(struct pipe_context
*pipe
,
2280 unsigned sample_mask
)
2285 panfrost_set_clip_state(struct pipe_context
*pipe
,
2286 const struct pipe_clip_state
*clip
)
2288 //struct panfrost_context *panfrost = pan_context(pipe);
2292 panfrost_set_viewport_states(struct pipe_context
*pipe
,
2293 unsigned start_slot
,
2294 unsigned num_viewports
,
2295 const struct pipe_viewport_state
*viewports
)
2297 struct panfrost_context
*ctx
= pan_context(pipe
);
2299 assert(start_slot
== 0);
2300 assert(num_viewports
== 1);
2302 ctx
->pipe_viewport
= *viewports
;
2306 panfrost_set_scissor_states(struct pipe_context
*pipe
,
2307 unsigned start_slot
,
2308 unsigned num_scissors
,
2309 const struct pipe_scissor_state
*scissors
)
2311 struct panfrost_context
*ctx
= pan_context(pipe
);
2313 assert(start_slot
== 0);
2314 assert(num_scissors
== 1);
2316 ctx
->scissor
= *scissors
;
2320 panfrost_set_polygon_stipple(struct pipe_context
*pipe
,
2321 const struct pipe_poly_stipple
*stipple
)
2323 //struct panfrost_context *panfrost = pan_context(pipe);
2327 panfrost_set_active_query_state(struct pipe_context
*pipe
,
2330 //struct panfrost_context *panfrost = pan_context(pipe);
2334 panfrost_destroy(struct pipe_context
*pipe
)
2336 struct panfrost_context
*panfrost
= pan_context(pipe
);
2337 struct panfrost_screen
*screen
= pan_screen(pipe
->screen
);
2339 if (panfrost
->blitter
)
2340 util_blitter_destroy(panfrost
->blitter
);
2342 screen
->driver
->free_slab(screen
, &panfrost
->scratchpad
);
2343 screen
->driver
->free_slab(screen
, &panfrost
->varying_mem
);
2344 screen
->driver
->free_slab(screen
, &panfrost
->shaders
);
2345 screen
->driver
->free_slab(screen
, &panfrost
->tiler_heap
);
2346 screen
->driver
->free_slab(screen
, &panfrost
->misc_0
);
2349 static struct pipe_query
*
2350 panfrost_create_query(struct pipe_context
*pipe
,
2354 struct panfrost_query
*q
= CALLOC_STRUCT(panfrost_query
);
2359 return (struct pipe_query
*) q
;
2363 panfrost_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2369 panfrost_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2371 struct panfrost_context
*ctx
= pan_context(pipe
);
2372 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2374 switch (query
->type
) {
2375 case PIPE_QUERY_OCCLUSION_COUNTER
:
2376 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2377 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2379 /* Allocate a word for the query results to be stored */
2380 query
->transfer
= panfrost_allocate_chunk(ctx
, sizeof(unsigned), HEAP_DESCRIPTOR
);
2382 ctx
->occlusion_query
= query
;
2388 DBG("Skipping query %d\n", query
->type
);
2396 panfrost_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2398 struct panfrost_context
*ctx
= pan_context(pipe
);
2399 ctx
->occlusion_query
= NULL
;
2404 panfrost_get_query_result(struct pipe_context
*pipe
,
2405 struct pipe_query
*q
,
2407 union pipe_query_result
*vresult
)
2410 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2412 /* We need to flush out the jobs to actually run the counter, TODO
2413 * check wait, TODO wallpaper after if needed */
2415 panfrost_flush(pipe
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2417 switch (query
->type
) {
2418 case PIPE_QUERY_OCCLUSION_COUNTER
:
2419 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2420 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
: {
2421 /* Read back the query results */
2422 unsigned *result
= (unsigned *) query
->transfer
.cpu
;
2423 unsigned passed
= *result
;
2425 if (query
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
2426 vresult
->u64
= passed
;
2428 vresult
->b
= !!passed
;
2434 DBG("Skipped query get %d\n", query
->type
);
2441 static struct pipe_stream_output_target
*
2442 panfrost_create_stream_output_target(struct pipe_context
*pctx
,
2443 struct pipe_resource
*prsc
,
2444 unsigned buffer_offset
,
2445 unsigned buffer_size
)
2447 struct pipe_stream_output_target
*target
;
2449 target
= CALLOC_STRUCT(pipe_stream_output_target
);
2454 pipe_reference_init(&target
->reference
, 1);
2455 pipe_resource_reference(&target
->buffer
, prsc
);
2457 target
->context
= pctx
;
2458 target
->buffer_offset
= buffer_offset
;
2459 target
->buffer_size
= buffer_size
;
2465 panfrost_stream_output_target_destroy(struct pipe_context
*pctx
,
2466 struct pipe_stream_output_target
*target
)
2468 pipe_resource_reference(&target
->buffer
, NULL
);
2473 panfrost_set_stream_output_targets(struct pipe_context
*pctx
,
2474 unsigned num_targets
,
2475 struct pipe_stream_output_target
**targets
,
2476 const unsigned *offsets
)
2482 panfrost_setup_hardware(struct panfrost_context
*ctx
)
2484 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2485 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
2487 for (int i
= 0; i
< ARRAY_SIZE(ctx
->transient_pools
); ++i
) {
2488 /* Allocate the beginning of the transient pool */
2489 int entry_size
= (1 << 22); /* 4MB */
2491 ctx
->transient_pools
[i
].entry_size
= entry_size
;
2492 ctx
->transient_pools
[i
].entry_count
= 1;
2494 ctx
->transient_pools
[i
].entries
[0] = (struct panfrost_memory_entry
*) pb_slab_alloc(&screen
->slabs
, entry_size
, HEAP_TRANSIENT
);
2497 screen
->driver
->allocate_slab(screen
, &ctx
->scratchpad
, 64, false, 0, 0, 0);
2498 screen
->driver
->allocate_slab(screen
, &ctx
->varying_mem
, 16384, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_COHERENT_LOCAL
, 0, 0);
2499 screen
->driver
->allocate_slab(screen
, &ctx
->shaders
, 4096, true, PAN_ALLOCATE_EXECUTE
, 0, 0);
2500 screen
->driver
->allocate_slab(screen
, &ctx
->tiler_heap
, 32768, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2501 screen
->driver
->allocate_slab(screen
, &ctx
->misc_0
, 128*128, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2505 /* New context creation, which also does hardware initialisation since I don't
2506 * know the better way to structure this :smirk: */
2508 struct pipe_context
*
2509 panfrost_create_context(struct pipe_screen
*screen
, void *priv
, unsigned flags
)
2511 struct panfrost_context
*ctx
= CALLOC_STRUCT(panfrost_context
);
2512 struct panfrost_screen
*pscreen
= pan_screen(screen
);
2513 memset(ctx
, 0, sizeof(*ctx
));
2514 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2517 gpu_id
= pscreen
->driver
->query_gpu_version(pscreen
);
2519 ctx
->is_t6xx
= gpu_id
<= 0x0750; /* For now, this flag means T760 or less */
2520 ctx
->require_sfbd
= gpu_id
< 0x0750; /* T760 is the first to support MFBD */
2522 gallium
->screen
= screen
;
2524 gallium
->destroy
= panfrost_destroy
;
2526 gallium
->set_framebuffer_state
= panfrost_set_framebuffer_state
;
2528 gallium
->flush
= panfrost_flush
;
2529 gallium
->clear
= panfrost_clear
;
2530 gallium
->draw_vbo
= panfrost_draw_vbo
;
2532 gallium
->set_vertex_buffers
= panfrost_set_vertex_buffers
;
2533 gallium
->set_constant_buffer
= panfrost_set_constant_buffer
;
2535 gallium
->set_stencil_ref
= panfrost_set_stencil_ref
;
2537 gallium
->create_sampler_view
= panfrost_create_sampler_view
;
2538 gallium
->set_sampler_views
= panfrost_set_sampler_views
;
2539 gallium
->sampler_view_destroy
= panfrost_sampler_view_destroy
;
2541 gallium
->create_rasterizer_state
= panfrost_create_rasterizer_state
;
2542 gallium
->bind_rasterizer_state
= panfrost_bind_rasterizer_state
;
2543 gallium
->delete_rasterizer_state
= panfrost_generic_cso_delete
;
2545 gallium
->create_vertex_elements_state
= panfrost_create_vertex_elements_state
;
2546 gallium
->bind_vertex_elements_state
= panfrost_bind_vertex_elements_state
;
2547 gallium
->delete_vertex_elements_state
= panfrost_generic_cso_delete
;
2549 gallium
->create_fs_state
= panfrost_create_shader_state
;
2550 gallium
->delete_fs_state
= panfrost_delete_shader_state
;
2551 gallium
->bind_fs_state
= panfrost_bind_fs_state
;
2553 gallium
->create_vs_state
= panfrost_create_shader_state
;
2554 gallium
->delete_vs_state
= panfrost_delete_shader_state
;
2555 gallium
->bind_vs_state
= panfrost_bind_vs_state
;
2557 gallium
->create_sampler_state
= panfrost_create_sampler_state
;
2558 gallium
->delete_sampler_state
= panfrost_generic_cso_delete
;
2559 gallium
->bind_sampler_states
= panfrost_bind_sampler_states
;
2561 gallium
->create_blend_state
= panfrost_create_blend_state
;
2562 gallium
->bind_blend_state
= panfrost_bind_blend_state
;
2563 gallium
->delete_blend_state
= panfrost_delete_blend_state
;
2565 gallium
->set_blend_color
= panfrost_set_blend_color
;
2567 gallium
->create_depth_stencil_alpha_state
= panfrost_create_depth_stencil_state
;
2568 gallium
->bind_depth_stencil_alpha_state
= panfrost_bind_depth_stencil_state
;
2569 gallium
->delete_depth_stencil_alpha_state
= panfrost_delete_depth_stencil_state
;
2571 gallium
->set_sample_mask
= panfrost_set_sample_mask
;
2573 gallium
->set_clip_state
= panfrost_set_clip_state
;
2574 gallium
->set_viewport_states
= panfrost_set_viewport_states
;
2575 gallium
->set_scissor_states
= panfrost_set_scissor_states
;
2576 gallium
->set_polygon_stipple
= panfrost_set_polygon_stipple
;
2577 gallium
->set_active_query_state
= panfrost_set_active_query_state
;
2579 gallium
->create_query
= panfrost_create_query
;
2580 gallium
->destroy_query
= panfrost_destroy_query
;
2581 gallium
->begin_query
= panfrost_begin_query
;
2582 gallium
->end_query
= panfrost_end_query
;
2583 gallium
->get_query_result
= panfrost_get_query_result
;
2585 gallium
->create_stream_output_target
= panfrost_create_stream_output_target
;
2586 gallium
->stream_output_target_destroy
= panfrost_stream_output_target_destroy
;
2587 gallium
->set_stream_output_targets
= panfrost_set_stream_output_targets
;
2589 panfrost_resource_context_init(gallium
);
2591 pscreen
->driver
->init_context(ctx
);
2593 panfrost_setup_hardware(ctx
);
2596 gallium
->stream_uploader
= u_upload_create_default(gallium
);
2597 gallium
->const_uploader
= gallium
->stream_uploader
;
2598 assert(gallium
->stream_uploader
);
2600 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2601 ctx
->draw_modes
= (1 << (PIPE_PRIM_POLYGON
+ 1)) - 1;
2603 ctx
->primconvert
= util_primconvert_create(gallium
, ctx
->draw_modes
);
2605 ctx
->blitter
= util_blitter_create(gallium
);
2606 assert(ctx
->blitter
);
2608 /* Prepare for render! */
2610 panfrost_job_init(ctx
);
2611 panfrost_emit_vertex_payload(ctx
);
2612 panfrost_emit_tiler_payload(ctx
);
2613 panfrost_invalidate_frame(ctx
);
2614 panfrost_default_shader_backend(ctx
);
2615 panfrost_generate_space_filler_indices();