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
;
851 /* Go through dirty flags and actualise them in the cmdstream. */
854 panfrost_emit_for_draw(struct panfrost_context
*ctx
, bool with_vertex_data
)
856 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
858 if (with_vertex_data
) {
859 panfrost_emit_vertex_data(ctx
, job
);
862 bool msaa
= ctx
->rasterizer
->base
.multisample
;
864 if (ctx
->dirty
& PAN_DIRTY_RASTERIZER
) {
865 ctx
->payload_tiler
.gl_enables
= ctx
->rasterizer
->tiler_gl_enables
;
867 /* TODO: Sample size */
868 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_HAS_MSAA
, msaa
);
869 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_MSAA
, !msaa
);
872 /* Enable job requirements at draw-time */
875 job
->requirements
|= PAN_REQ_MSAA
;
877 if (ctx
->depth_stencil
->depth
.writemask
)
878 job
->requirements
|= PAN_REQ_DEPTH_WRITE
;
880 if (ctx
->occlusion_query
) {
881 ctx
->payload_tiler
.gl_enables
|= MALI_OCCLUSION_QUERY
| MALI_OCCLUSION_PRECISE
;
882 ctx
->payload_tiler
.postfix
.occlusion_counter
= ctx
->occlusion_query
->transfer
.gpu
;
885 if (ctx
->dirty
& PAN_DIRTY_VS
) {
888 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
890 /* Late shader descriptor assignments */
892 vs
->tripipe
->texture_count
= ctx
->sampler_view_count
[PIPE_SHADER_VERTEX
];
893 vs
->tripipe
->sampler_count
= ctx
->sampler_count
[PIPE_SHADER_VERTEX
];
896 vs
->tripipe
->midgard1
.unknown1
= 0x2201;
898 ctx
->payload_vertex
.postfix
._shader_upper
= vs
->tripipe_gpu
>> 4;
901 if (ctx
->dirty
& (PAN_DIRTY_RASTERIZER
| PAN_DIRTY_VS
)) {
902 /* Check if we need to link the gl_PointSize varying */
903 if (!panfrost_writes_point_size(ctx
)) {
904 /* If the size is constant, write it out. Otherwise,
905 * don't touch primitive_size (since we would clobber
906 * the pointer there) */
908 ctx
->payload_tiler
.primitive_size
.constant
= ctx
->rasterizer
->base
.line_width
;
912 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
914 ctx
->dirty
|= PAN_DIRTY_FS
;
916 if (ctx
->dirty
& PAN_DIRTY_FS
) {
918 struct panfrost_shader_state
*variant
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
920 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
923 COPY(attribute_count
);
925 COPY(midgard1
.uniform_count
);
926 COPY(midgard1
.work_count
);
927 COPY(midgard1
.unknown2
);
930 /* If there is a blend shader, work registers are shared */
932 if (ctx
->blend
->has_blend_shader
)
933 ctx
->fragment_shader_core
.midgard1
.work_count
= /*MAX2(ctx->fragment_shader_core.midgard1.work_count, ctx->blend->blend_work_count)*/16;
935 /* Set late due to depending on render state */
936 /* The one at the end seems to mean "1 UBO" */
937 ctx
->fragment_shader_core
.midgard1
.unknown1
= MALI_NO_ALPHA_TO_COVERAGE
| 0x200 | 0x2201;
939 /* Assign texture/sample count right before upload */
940 ctx
->fragment_shader_core
.texture_count
= ctx
->sampler_view_count
[PIPE_SHADER_FRAGMENT
];
941 ctx
->fragment_shader_core
.sampler_count
= ctx
->sampler_count
[PIPE_SHADER_FRAGMENT
];
943 /* Assign the stencil refs late */
944 ctx
->fragment_shader_core
.stencil_front
.ref
= ctx
->stencil_ref
.ref_value
[0];
945 ctx
->fragment_shader_core
.stencil_back
.ref
= ctx
->stencil_ref
.ref_value
[1];
947 /* CAN_DISCARD should be set if the fragment shader possibly
948 * contains a 'discard' instruction. It is likely this is
949 * related to optimizations related to forward-pixel kill, as
950 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
951 * thing?" by Peter Harris
954 if (variant
->can_discard
) {
955 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
956 ctx
->fragment_shader_core
.midgard1
.unknown1
&= ~MALI_NO_ALPHA_TO_COVERAGE
;
957 ctx
->fragment_shader_core
.midgard1
.unknown1
|= 0x4000;
958 ctx
->fragment_shader_core
.midgard1
.unknown1
= 0x4200;
961 /* Check if we're using the default blend descriptor (fast path) */
964 !ctx
->blend
->has_blend_shader
&&
965 (ctx
->blend
->equation
.rgb_mode
== 0x122) &&
966 (ctx
->blend
->equation
.alpha_mode
== 0x122) &&
967 (ctx
->blend
->equation
.color_mask
== 0xf);
969 /* Even on MFBD, the shader descriptor gets blend shaders. It's
970 * *also* copied to the blend_meta appended (by convention),
971 * but this is the field actually read by the hardware. (Or
972 * maybe both are read...?) */
974 if (ctx
->blend
->has_blend_shader
) {
975 ctx
->fragment_shader_core
.blend
.shader
= ctx
->blend
->blend_shader
;
978 if (ctx
->require_sfbd
) {
979 /* When only a single render target platform is used, the blend
980 * information is inside the shader meta itself. We
981 * additionally need to signal CAN_DISCARD for nontrivial blend
982 * modes (so we're able to read back the destination buffer) */
984 if (!ctx
->blend
->has_blend_shader
) {
985 ctx
->fragment_shader_core
.blend
.equation
= ctx
->blend
->equation
;
986 ctx
->fragment_shader_core
.blend
.constant
= ctx
->blend
->constant
;
990 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
994 size_t size
= sizeof(struct mali_shader_meta
) + sizeof(struct midgard_blend_rt
);
995 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
996 memcpy(transfer
.cpu
, &ctx
->fragment_shader_core
, sizeof(struct mali_shader_meta
));
998 ctx
->payload_tiler
.postfix
._shader_upper
= (transfer
.gpu
) >> 4;
1000 if (!ctx
->require_sfbd
) {
1001 /* Additional blend descriptor tacked on for jobs using MFBD */
1003 unsigned blend_count
= 0x200;
1005 if (ctx
->blend
->has_blend_shader
) {
1006 /* For a blend shader, the bottom nibble corresponds to
1007 * the number of work registers used, which signals the
1008 * -existence- of a blend shader */
1010 assert(ctx
->blend
->blend_work_count
>= 2);
1011 blend_count
|= MIN2(ctx
->blend
->blend_work_count
, 3);
1013 /* Otherwise, the bottom bit simply specifies if
1014 * blending (anything other than REPLACE) is enabled */
1021 struct midgard_blend_rt rts
[4];
1025 for (unsigned i
= 0; i
< 1; ++i
) {
1026 rts
[i
].flags
= blend_count
;
1028 if (ctx
->blend
->has_blend_shader
) {
1029 rts
[i
].blend
.shader
= ctx
->blend
->blend_shader
;
1031 rts
[i
].blend
.equation
= ctx
->blend
->equation
;
1032 rts
[i
].blend
.constant
= ctx
->blend
->constant
;
1036 memcpy(transfer
.cpu
+ sizeof(struct mali_shader_meta
), rts
, sizeof(rts
[0]) * 1);
1040 /* We stage to transient, so always dirty.. */
1041 panfrost_stage_attributes(ctx
);
1043 if (ctx
->dirty
& PAN_DIRTY_SAMPLERS
) {
1044 /* Upload samplers back to back, no padding */
1046 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
1047 if (!ctx
->sampler_count
[t
]) continue;
1049 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(struct mali_sampler_descriptor
) * ctx
->sampler_count
[t
]);
1050 struct mali_sampler_descriptor
*desc
= (struct mali_sampler_descriptor
*) transfer
.cpu
;
1052 for (int i
= 0; i
< ctx
->sampler_count
[t
]; ++i
) {
1053 desc
[i
] = ctx
->samplers
[t
][i
]->hw
;
1056 if (t
== PIPE_SHADER_FRAGMENT
)
1057 ctx
->payload_tiler
.postfix
.sampler_descriptor
= transfer
.gpu
;
1058 else if (t
== PIPE_SHADER_VERTEX
)
1059 ctx
->payload_vertex
.postfix
.sampler_descriptor
= transfer
.gpu
;
1065 if (ctx
->dirty
& PAN_DIRTY_TEXTURES
) {
1066 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
1068 if (!ctx
->sampler_view_count
[t
]) continue;
1070 uint64_t trampolines
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
1072 for (int i
= 0; i
< ctx
->sampler_view_count
[t
]; ++i
) {
1073 if (!ctx
->sampler_views
[t
][i
])
1076 struct pipe_resource
*tex_rsrc
= ctx
->sampler_views
[t
][i
]->base
.texture
;
1077 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) tex_rsrc
;
1079 /* Inject the addresses in, interleaving cube
1080 * faces and mip levels appropriately. */
1082 for (int l
= 0; l
<= tex_rsrc
->last_level
; ++l
) {
1083 for (int f
= 0; f
< tex_rsrc
->array_size
; ++f
) {
1084 unsigned idx
= (l
* tex_rsrc
->array_size
) + f
;
1086 ctx
->sampler_views
[t
][i
]->hw
.swizzled_bitmaps
[idx
] =
1088 rsrc
->bo
->slices
[l
].offset
+
1089 f
* rsrc
->bo
->cubemap_stride
;
1093 /* Inject the strides */
1094 unsigned usage2
= ctx
->sampler_views
[t
][i
]->hw
.format
.usage2
;
1096 if (usage2
& MALI_TEX_MANUAL_STRIDE
) {
1097 unsigned idx
= tex_rsrc
->last_level
* tex_rsrc
->array_size
;
1098 idx
+= tex_rsrc
->array_size
;
1100 ctx
->sampler_views
[t
][i
]->hw
.swizzled_bitmaps
[idx
] =
1101 rsrc
->bo
->slices
[0].stride
;
1104 trampolines
[i
] = panfrost_upload_transient(ctx
, &ctx
->sampler_views
[t
][i
]->hw
, sizeof(struct mali_texture_descriptor
));
1107 mali_ptr trampoline
= panfrost_upload_transient(ctx
, trampolines
, sizeof(uint64_t) * ctx
->sampler_view_count
[t
]);
1109 if (t
== PIPE_SHADER_FRAGMENT
)
1110 ctx
->payload_tiler
.postfix
.texture_trampoline
= trampoline
;
1111 else if (t
== PIPE_SHADER_VERTEX
)
1112 ctx
->payload_vertex
.postfix
.texture_trampoline
= trampoline
;
1118 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
1120 for (int i
= 0; i
<= PIPE_SHADER_FRAGMENT
; ++i
) {
1121 struct panfrost_constant_buffer
*buf
= &ctx
->constant_buffer
[i
];
1123 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
1124 struct panfrost_shader_state
*fs
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
1125 struct panfrost_shader_state
*ss
= (i
== PIPE_SHADER_FRAGMENT
) ? fs
: vs
;
1127 /* Allocate room for the sysval and the uniforms */
1128 size_t sys_size
= sizeof(float) * 4 * ss
->sysval_count
;
1129 size_t size
= sys_size
+ buf
->size
;
1130 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
1132 /* Upload sysvals requested by the shader */
1133 float *uniforms
= (float *) transfer
.cpu
;
1134 for (unsigned i
= 0; i
< ss
->sysval_count
; ++i
) {
1135 int sysval
= ss
->sysval
[i
];
1137 if (sysval
== PAN_SYSVAL_VIEWPORT_SCALE
) {
1138 uniforms
[4*i
+ 0] = vp
->scale
[0];
1139 uniforms
[4*i
+ 1] = vp
->scale
[1];
1140 uniforms
[4*i
+ 2] = vp
->scale
[2];
1141 } else if (sysval
== PAN_SYSVAL_VIEWPORT_OFFSET
) {
1142 uniforms
[4*i
+ 0] = vp
->translate
[0];
1143 uniforms
[4*i
+ 1] = vp
->translate
[1];
1144 uniforms
[4*i
+ 2] = vp
->translate
[2];
1150 /* Upload uniforms */
1151 memcpy(transfer
.cpu
+ sys_size
, buf
->buffer
, buf
->size
);
1153 int uniform_count
= 0;
1155 struct mali_vertex_tiler_postfix
*postfix
;
1158 case PIPE_SHADER_VERTEX
:
1159 uniform_count
= ctx
->vs
->variants
[ctx
->vs
->active_variant
].uniform_count
;
1160 postfix
= &ctx
->payload_vertex
.postfix
;
1163 case PIPE_SHADER_FRAGMENT
:
1164 uniform_count
= ctx
->fs
->variants
[ctx
->fs
->active_variant
].uniform_count
;
1165 postfix
= &ctx
->payload_tiler
.postfix
;
1169 unreachable("Invalid shader stage\n");
1172 /* Also attach the same buffer as a UBO for extended access */
1174 struct mali_uniform_buffer_meta uniform_buffers
[] = {
1176 .size
= MALI_POSITIVE((2 + uniform_count
)),
1177 .ptr
= transfer
.gpu
>> 2,
1181 mali_ptr ubufs
= panfrost_upload_transient(ctx
, uniform_buffers
, sizeof(uniform_buffers
));
1182 postfix
->uniforms
= transfer
.gpu
;
1183 postfix
->uniform_buffers
= ubufs
;
1188 /* TODO: Upload the viewport somewhere more appropriate */
1190 /* Clip bounds are encoded as floats. The viewport itself is encoded as
1191 * (somewhat) asymmetric ints. */
1192 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
1194 struct mali_viewport view
= {
1195 /* By default, do no viewport clipping, i.e. clip to (-inf,
1196 * inf) in each direction. Clipping to the viewport in theory
1197 * should work, but in practice causes issues when we're not
1198 * explicitly trying to scissor */
1209 /* Always scissor to the viewport by default. */
1210 view
.viewport0
[0] = (int) (vp
->translate
[0] - vp
->scale
[0]);
1211 view
.viewport1
[0] = MALI_POSITIVE((int) (vp
->translate
[0] + vp
->scale
[0]));
1213 int miny
= (int) (vp
->translate
[1] - vp
->scale
[1]);
1214 int maxy
= (int) (vp
->translate
[1] + vp
->scale
[1]);
1216 if (ss
&& ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
) {
1217 view
.viewport0
[0] = ss
->minx
;
1218 view
.viewport1
[0] = MALI_POSITIVE(ss
->maxx
);
1224 /* Hardware needs the min/max to be strictly ordered, so flip if we
1232 view
.viewport0
[1] = miny
;
1233 view
.viewport1
[1] = MALI_POSITIVE(maxy
);
1235 ctx
->payload_tiler
.postfix
.viewport
=
1236 panfrost_upload_transient(ctx
,
1238 sizeof(struct mali_viewport
));
1243 /* Corresponds to exactly one draw, but does not submit anything */
1246 panfrost_queue_draw(struct panfrost_context
*ctx
)
1248 /* TODO: Expand the array? */
1249 if (ctx
->draw_count
>= MAX_DRAW_CALLS
) {
1250 DBG("Job buffer overflow, ignoring draw\n");
1254 /* Handle dirty flags now */
1255 panfrost_emit_for_draw(ctx
, true);
1257 /* We need a set_value job before any other draw jobs */
1258 if (ctx
->draw_count
== 0)
1259 panfrost_set_value_job(ctx
);
1261 struct panfrost_transfer vertex
= panfrost_vertex_tiler_job(ctx
, false);
1262 ctx
->u_vertex_jobs
[ctx
->vertex_job_count
] = (struct mali_job_descriptor_header
*) vertex
.cpu
;
1263 ctx
->vertex_jobs
[ctx
->vertex_job_count
++] = vertex
.gpu
;
1265 struct panfrost_transfer tiler
= panfrost_vertex_tiler_job(ctx
, true);
1266 ctx
->u_tiler_jobs
[ctx
->tiler_job_count
] = (struct mali_job_descriptor_header
*) tiler
.cpu
;
1267 ctx
->tiler_jobs
[ctx
->tiler_job_count
++] = tiler
.gpu
;
1272 /* The entire frame is in memory -- send it off to the kernel! */
1275 panfrost_submit_frame(struct panfrost_context
*ctx
, bool flush_immediate
,
1276 struct pipe_fence_handle
**fence
,
1277 struct panfrost_job
*job
)
1279 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
1280 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
1282 /* Edge case if screen is cleared and nothing else */
1283 bool has_draws
= ctx
->draw_count
> 0;
1285 /* Workaround a bizarre lockup (a hardware errata?) */
1287 flush_immediate
= true;
1291 bool is_scanout
= panfrost_is_scanout(ctx
);
1292 screen
->driver
->submit_vs_fs_job(ctx
, has_draws
, is_scanout
);
1294 /* If visual, we can stall a frame */
1296 if (!flush_immediate
)
1297 screen
->driver
->force_flush_fragment(ctx
, fence
);
1299 screen
->last_fragment_flushed
= false;
1300 screen
->last_job
= job
;
1302 /* If readback, flush now (hurts the pipelined performance) */
1303 if (flush_immediate
)
1304 screen
->driver
->force_flush_fragment(ctx
, fence
);
1306 if (screen
->driver
->dump_counters
&& pan_counters_base
) {
1307 screen
->driver
->dump_counters(screen
);
1310 snprintf(filename
, sizeof(filename
), "%s/frame%d.mdgprf", pan_counters_base
, ++performance_counter_number
);
1311 FILE *fp
= fopen(filename
, "wb");
1312 fwrite(screen
->perf_counters
.cpu
, 4096, sizeof(uint32_t), fp
);
1320 panfrost_draw_wallpaper(struct pipe_context
*pipe
)
1322 struct panfrost_context
*ctx
= pan_context(pipe
);
1324 /* Nothing to reload? */
1325 if (ctx
->pipe_framebuffer
.cbufs
[0] == NULL
)
1328 /* Blit the wallpaper in */
1329 panfrost_blit_wallpaper(ctx
);
1331 /* We are flushing all queued draws and we know that no more jobs will
1332 * be added until the next frame.
1333 * We also know that the last jobs are the wallpaper jobs, and they
1334 * need to be linked so they execute right after the set_value job.
1337 /* set_value job to wallpaper vertex job */
1338 panfrost_link_job_pair(ctx
->u_set_value_job
, ctx
->vertex_jobs
[ctx
->vertex_job_count
- 1]);
1339 ctx
->u_vertex_jobs
[ctx
->vertex_job_count
- 1]->job_dependency_index_1
= ctx
->u_set_value_job
->job_index
;
1341 /* wallpaper vertex job to first vertex job */
1342 panfrost_link_job_pair(ctx
->u_vertex_jobs
[ctx
->vertex_job_count
- 1], ctx
->vertex_jobs
[0]);
1343 ctx
->u_vertex_jobs
[0]->job_dependency_index_1
= ctx
->u_set_value_job
->job_index
;
1345 /* last vertex job to wallpaper tiler job */
1346 panfrost_link_job_pair(ctx
->u_vertex_jobs
[ctx
->vertex_job_count
- 2], ctx
->tiler_jobs
[ctx
->tiler_job_count
- 1]);
1347 ctx
->u_tiler_jobs
[ctx
->tiler_job_count
- 1]->job_dependency_index_1
= ctx
->u_vertex_jobs
[ctx
->vertex_job_count
- 1]->job_index
;
1348 ctx
->u_tiler_jobs
[ctx
->tiler_job_count
- 1]->job_dependency_index_2
= 0;
1350 /* wallpaper tiler job to first tiler job */
1351 panfrost_link_job_pair(ctx
->u_tiler_jobs
[ctx
->tiler_job_count
- 1], ctx
->tiler_jobs
[0]);
1352 ctx
->u_tiler_jobs
[0]->job_dependency_index_1
= ctx
->u_vertex_jobs
[0]->job_index
;
1353 ctx
->u_tiler_jobs
[0]->job_dependency_index_2
= ctx
->u_tiler_jobs
[ctx
->tiler_job_count
- 1]->job_index
;
1355 /* last tiler job to NULL */
1356 panfrost_link_job_pair(ctx
->u_tiler_jobs
[ctx
->tiler_job_count
- 2], 0);
1361 struct pipe_context
*pipe
,
1362 struct pipe_fence_handle
**fence
,
1365 struct panfrost_context
*ctx
= pan_context(pipe
);
1366 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
1368 /* Nothing to do! */
1369 if (!ctx
->draw_count
&& !job
->clear
) return;
1372 panfrost_draw_wallpaper(&ctx
->base
);
1374 /* Whether to stall the pipeline for immediately correct results */
1375 bool flush_immediate
= flags
& PIPE_FLUSH_END_OF_FRAME
;
1377 /* Submit the frame itself */
1378 panfrost_submit_frame(ctx
, flush_immediate
, fence
, job
);
1380 /* Prepare for the next frame */
1381 panfrost_invalidate_frame(ctx
);
1384 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1387 g2m_draw_mode(enum pipe_prim_type mode
)
1390 DEFINE_CASE(POINTS
);
1392 DEFINE_CASE(LINE_LOOP
);
1393 DEFINE_CASE(LINE_STRIP
);
1394 DEFINE_CASE(TRIANGLES
);
1395 DEFINE_CASE(TRIANGLE_STRIP
);
1396 DEFINE_CASE(TRIANGLE_FAN
);
1398 DEFINE_CASE(QUAD_STRIP
);
1399 DEFINE_CASE(POLYGON
);
1402 unreachable("Invalid draw mode");
1409 panfrost_translate_index_size(unsigned size
)
1413 return MALI_DRAW_INDEXED_UINT8
;
1416 return MALI_DRAW_INDEXED_UINT16
;
1419 return MALI_DRAW_INDEXED_UINT32
;
1422 unreachable("Invalid index size");
1426 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1427 * good for the duration of the draw (transient), could last longer */
1430 panfrost_get_index_buffer_mapped(struct panfrost_context
*ctx
, const struct pipe_draw_info
*info
)
1432 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (info
->index
.resource
);
1434 off_t offset
= info
->start
* info
->index_size
;
1436 if (!info
->has_user_indices
) {
1437 /* Only resources can be directly mapped */
1438 return rsrc
->bo
->gpu
+ offset
;
1440 /* Otherwise, we need to upload to transient memory */
1441 const uint8_t *ibuf8
= (const uint8_t *) info
->index
.user
;
1442 return panfrost_upload_transient(ctx
, ibuf8
+ offset
, info
->count
* info
->index_size
);
1448 struct pipe_context
*pipe
,
1449 const struct pipe_draw_info
*info
)
1451 struct panfrost_context
*ctx
= pan_context(pipe
);
1453 ctx
->payload_vertex
.draw_start
= info
->start
;
1454 ctx
->payload_tiler
.draw_start
= info
->start
;
1456 int mode
= info
->mode
;
1458 /* Fallback for unsupported modes */
1460 if (!(ctx
->draw_modes
& (1 << mode
))) {
1461 if (mode
== PIPE_PRIM_QUADS
&& info
->count
== 4 && ctx
->rasterizer
&& !ctx
->rasterizer
->base
.flatshade
) {
1462 mode
= PIPE_PRIM_TRIANGLE_FAN
;
1464 if (info
->count
< 4) {
1465 /* Degenerate case? */
1469 util_primconvert_save_rasterizer_state(ctx
->primconvert
, &ctx
->rasterizer
->base
);
1470 util_primconvert_draw_vbo(ctx
->primconvert
, info
);
1475 /* Now that we have a guaranteed terminating path, find the job.
1476 * Assignment commented out to prevent unused warning */
1478 /* struct panfrost_job *job = */ panfrost_get_job_for_fbo(ctx
);
1480 ctx
->payload_tiler
.prefix
.draw_mode
= g2m_draw_mode(mode
);
1482 ctx
->vertex_count
= info
->count
;
1484 /* For non-indexed draws, they're the same */
1485 unsigned invocation_count
= ctx
->vertex_count
;
1487 unsigned draw_flags
= 0;
1489 /* The draw flags interpret how primitive size is interpreted */
1491 if (panfrost_writes_point_size(ctx
))
1492 draw_flags
|= MALI_DRAW_VARYING_SIZE
;
1494 /* For higher amounts of vertices (greater than what fits in a 16-bit
1495 * short), the other value is needed, otherwise there will be bizarre
1496 * rendering artefacts. It's not clear what these values mean yet. */
1498 draw_flags
|= (mode
== PIPE_PRIM_POINTS
|| ctx
->vertex_count
> 65535) ? 0x3000 : 0x18000;
1500 if (info
->index_size
) {
1501 /* Calculate the min/max index used so we can figure out how
1502 * many times to invoke the vertex shader */
1504 /* Fetch / calculate index bounds */
1505 unsigned min_index
= 0, max_index
= 0;
1507 if (info
->max_index
== ~0u) {
1508 u_vbuf_get_minmax_index(pipe
, info
, &min_index
, &max_index
);
1510 min_index
= info
->min_index
;
1511 max_index
= info
->max_index
;
1514 /* Use the corresponding values */
1515 invocation_count
= max_index
- min_index
+ 1;
1516 ctx
->payload_vertex
.draw_start
= min_index
;
1517 ctx
->payload_tiler
.draw_start
= min_index
;
1519 ctx
->payload_tiler
.prefix
.negative_start
= -min_index
;
1520 ctx
->payload_tiler
.prefix
.index_count
= MALI_POSITIVE(info
->count
);
1522 //assert(!info->restart_index); /* TODO: Research */
1523 assert(!info
->index_bias
);
1525 draw_flags
|= panfrost_translate_index_size(info
->index_size
);
1526 ctx
->payload_tiler
.prefix
.indices
= panfrost_get_index_buffer_mapped(ctx
, info
);
1528 /* Index count == vertex count, if no indexing is applied, as
1529 * if it is internally indexed in the expected order */
1531 ctx
->payload_tiler
.prefix
.negative_start
= 0;
1532 ctx
->payload_tiler
.prefix
.index_count
= MALI_POSITIVE(ctx
->vertex_count
);
1534 /* Reverse index state */
1535 ctx
->payload_tiler
.prefix
.indices
= (uintptr_t) NULL
;
1538 ctx
->payload_vertex
.prefix
.invocation_count
= MALI_POSITIVE(invocation_count
);
1539 ctx
->payload_tiler
.prefix
.invocation_count
= MALI_POSITIVE(invocation_count
);
1540 ctx
->payload_tiler
.prefix
.unknown_draw
= draw_flags
;
1542 /* Fire off the draw itself */
1543 panfrost_queue_draw(ctx
);
1549 panfrost_generic_cso_delete(struct pipe_context
*pctx
, void *hwcso
)
1555 panfrost_create_rasterizer_state(
1556 struct pipe_context
*pctx
,
1557 const struct pipe_rasterizer_state
*cso
)
1559 struct panfrost_context
*ctx
= pan_context(pctx
);
1560 struct panfrost_rasterizer
*so
= CALLOC_STRUCT(panfrost_rasterizer
);
1564 /* Bitmask, unknown meaning of the start value */
1565 so
->tiler_gl_enables
= ctx
->is_t6xx
? 0x105 : 0x7;
1568 so
->tiler_gl_enables
|= MALI_FRONT_CCW_TOP
;
1570 if (cso
->cull_face
& PIPE_FACE_FRONT
)
1571 so
->tiler_gl_enables
|= MALI_CULL_FACE_FRONT
;
1573 if (cso
->cull_face
& PIPE_FACE_BACK
)
1574 so
->tiler_gl_enables
|= MALI_CULL_FACE_BACK
;
1580 panfrost_bind_rasterizer_state(
1581 struct pipe_context
*pctx
,
1584 struct panfrost_context
*ctx
= pan_context(pctx
);
1586 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1590 ctx
->rasterizer
= hwcso
;
1591 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
1595 panfrost_create_vertex_elements_state(
1596 struct pipe_context
*pctx
,
1597 unsigned num_elements
,
1598 const struct pipe_vertex_element
*elements
)
1600 struct panfrost_vertex_state
*so
= CALLOC_STRUCT(panfrost_vertex_state
);
1602 so
->num_elements
= num_elements
;
1603 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
1605 /* XXX: What the cornball? This is totally, 100%, unapologetically
1606 * nonsense. And yet it somehow fixes a regression in -bshadow
1607 * (previously, we allocated the descriptor here... a newer commit
1608 * removed that allocation, and then memory corruption led to
1609 * shader_meta getting overwritten in bad ways and then the whole test
1610 * case falling apart . TODO: LOOK INTO PLEASE XXX XXX BAD XXX XXX XXX
1612 panfrost_allocate_chunk(pan_context(pctx
), 0, HEAP_DESCRIPTOR
);
1614 for (int i
= 0; i
< num_elements
; ++i
) {
1615 so
->hw
[i
].index
= elements
[i
].vertex_buffer_index
;
1617 enum pipe_format fmt
= elements
[i
].src_format
;
1618 const struct util_format_description
*desc
= util_format_description(fmt
);
1619 so
->hw
[i
].unknown1
= 0x2;
1620 so
->hw
[i
].swizzle
= panfrost_get_default_swizzle(desc
->nr_channels
);
1622 so
->hw
[i
].format
= panfrost_find_format(desc
);
1624 /* The field itself should probably be shifted over */
1625 so
->hw
[i
].src_offset
= elements
[i
].src_offset
;
1632 panfrost_bind_vertex_elements_state(
1633 struct pipe_context
*pctx
,
1636 struct panfrost_context
*ctx
= pan_context(pctx
);
1638 ctx
->vertex
= hwcso
;
1639 ctx
->dirty
|= PAN_DIRTY_VERTEX
;
1643 panfrost_create_shader_state(
1644 struct pipe_context
*pctx
,
1645 const struct pipe_shader_state
*cso
)
1647 struct panfrost_shader_variants
*so
= CALLOC_STRUCT(panfrost_shader_variants
);
1650 /* Token deep copy to prevent memory corruption */
1652 if (cso
->type
== PIPE_SHADER_IR_TGSI
)
1653 so
->base
.tokens
= tgsi_dup_tokens(so
->base
.tokens
);
1659 panfrost_delete_shader_state(
1660 struct pipe_context
*pctx
,
1663 struct panfrost_shader_variants
*cso
= (struct panfrost_shader_variants
*) so
;
1665 if (cso
->base
.type
== PIPE_SHADER_IR_TGSI
) {
1666 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1673 panfrost_create_sampler_state(
1674 struct pipe_context
*pctx
,
1675 const struct pipe_sampler_state
*cso
)
1677 struct panfrost_sampler_state
*so
= CALLOC_STRUCT(panfrost_sampler_state
);
1680 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1682 struct mali_sampler_descriptor sampler_descriptor
= {
1683 .filter_mode
= MALI_TEX_MIN(translate_tex_filter(cso
->min_img_filter
))
1684 | MALI_TEX_MAG(translate_tex_filter(cso
->mag_img_filter
))
1685 | translate_mip_filter(cso
->min_mip_filter
)
1688 .wrap_s
= translate_tex_wrap(cso
->wrap_s
),
1689 .wrap_t
= translate_tex_wrap(cso
->wrap_t
),
1690 .wrap_r
= translate_tex_wrap(cso
->wrap_r
),
1691 .compare_func
= panfrost_translate_alt_compare_func(cso
->compare_func
),
1693 cso
->border_color
.f
[0],
1694 cso
->border_color
.f
[1],
1695 cso
->border_color
.f
[2],
1696 cso
->border_color
.f
[3]
1698 .min_lod
= FIXED_16(cso
->min_lod
),
1699 .max_lod
= FIXED_16(cso
->max_lod
),
1703 so
->hw
= sampler_descriptor
;
1709 panfrost_bind_sampler_states(
1710 struct pipe_context
*pctx
,
1711 enum pipe_shader_type shader
,
1712 unsigned start_slot
, unsigned num_sampler
,
1715 assert(start_slot
== 0);
1717 struct panfrost_context
*ctx
= pan_context(pctx
);
1719 /* XXX: Should upload, not just copy? */
1720 ctx
->sampler_count
[shader
] = num_sampler
;
1721 memcpy(ctx
->samplers
[shader
], sampler
, num_sampler
* sizeof (void *));
1723 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
;
1727 panfrost_variant_matches(struct panfrost_context
*ctx
, struct panfrost_shader_state
*variant
)
1729 struct pipe_alpha_state
*alpha
= &ctx
->depth_stencil
->alpha
;
1731 if (alpha
->enabled
|| variant
->alpha_state
.enabled
) {
1732 /* Make sure enable state is at least the same */
1733 if (alpha
->enabled
!= variant
->alpha_state
.enabled
) {
1737 /* Check that the contents of the test are the same */
1738 bool same_func
= alpha
->func
== variant
->alpha_state
.func
;
1739 bool same_ref
= alpha
->ref_value
== variant
->alpha_state
.ref_value
;
1741 if (!(same_func
&& same_ref
)) {
1745 /* Otherwise, we're good to go */
1750 panfrost_bind_fs_state(
1751 struct pipe_context
*pctx
,
1754 struct panfrost_context
*ctx
= pan_context(pctx
);
1759 /* Match the appropriate variant */
1761 signed variant
= -1;
1763 struct panfrost_shader_variants
*variants
= (struct panfrost_shader_variants
*) hwcso
;
1765 for (unsigned i
= 0; i
< variants
->variant_count
; ++i
) {
1766 if (panfrost_variant_matches(ctx
, &variants
->variants
[i
])) {
1772 if (variant
== -1) {
1773 /* No variant matched, so create a new one */
1774 variant
= variants
->variant_count
++;
1775 assert(variants
->variant_count
< MAX_SHADER_VARIANTS
);
1777 variants
->variants
[variant
].base
= hwcso
;
1778 variants
->variants
[variant
].alpha_state
= ctx
->depth_stencil
->alpha
;
1780 /* Allocate the mapped descriptor ahead-of-time. TODO: Use for FS as well as VS */
1781 struct panfrost_context
*ctx
= pan_context(pctx
);
1782 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_shader_meta
), HEAP_DESCRIPTOR
);
1784 variants
->variants
[variant
].tripipe
= (struct mali_shader_meta
*) transfer
.cpu
;
1785 variants
->variants
[variant
].tripipe_gpu
= transfer
.gpu
;
1789 /* Select this variant */
1790 variants
->active_variant
= variant
;
1792 struct panfrost_shader_state
*shader_state
= &variants
->variants
[variant
];
1793 assert(panfrost_variant_matches(ctx
, shader_state
));
1795 /* Now we have a variant selected, so compile and go */
1797 if (!shader_state
->compiled
) {
1798 panfrost_shader_compile(ctx
, shader_state
->tripipe
, NULL
, JOB_TYPE_TILER
, shader_state
);
1799 shader_state
->compiled
= true;
1803 ctx
->dirty
|= PAN_DIRTY_FS
;
1807 panfrost_bind_vs_state(
1808 struct pipe_context
*pctx
,
1811 struct panfrost_context
*ctx
= pan_context(pctx
);
1816 if (!ctx
->vs
->variants
[0].compiled
) {
1817 ctx
->vs
->variants
[0].base
= hwcso
;
1819 /* TODO DRY from above */
1820 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_shader_meta
), HEAP_DESCRIPTOR
);
1821 ctx
->vs
->variants
[0].tripipe
= (struct mali_shader_meta
*) transfer
.cpu
;
1822 ctx
->vs
->variants
[0].tripipe_gpu
= transfer
.gpu
;
1824 panfrost_shader_compile(ctx
, ctx
->vs
->variants
[0].tripipe
, NULL
, JOB_TYPE_VERTEX
, &ctx
->vs
->variants
[0]);
1825 ctx
->vs
->variants
[0].compiled
= true;
1829 ctx
->dirty
|= PAN_DIRTY_VS
;
1833 panfrost_set_vertex_buffers(
1834 struct pipe_context
*pctx
,
1835 unsigned start_slot
,
1836 unsigned num_buffers
,
1837 const struct pipe_vertex_buffer
*buffers
)
1839 struct panfrost_context
*ctx
= pan_context(pctx
);
1841 util_set_vertex_buffers_mask(ctx
->vertex_buffers
, &ctx
->vb_mask
, buffers
, start_slot
, num_buffers
);
1845 panfrost_set_constant_buffer(
1846 struct pipe_context
*pctx
,
1847 enum pipe_shader_type shader
, uint index
,
1848 const struct pipe_constant_buffer
*buf
)
1850 struct panfrost_context
*ctx
= pan_context(pctx
);
1851 struct panfrost_constant_buffer
*pbuf
= &ctx
->constant_buffer
[shader
];
1853 size_t sz
= buf
? buf
->buffer_size
: 0;
1855 /* Free previous buffer */
1862 pbuf
->buffer
= NULL
;
1865 /* If unbinding, we're done */
1870 /* Multiple constant buffers not yet supported */
1875 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
);
1878 cpu
= rsrc
->bo
->cpu
;
1879 } else if (buf
->user_buffer
) {
1880 cpu
= buf
->user_buffer
;
1882 DBG("No constant buffer?\n");
1886 /* Copy the constant buffer into the driver context for later upload */
1888 pbuf
->buffer
= malloc(sz
);
1889 memcpy(pbuf
->buffer
, cpu
+ buf
->buffer_offset
, sz
);
1893 panfrost_set_stencil_ref(
1894 struct pipe_context
*pctx
,
1895 const struct pipe_stencil_ref
*ref
)
1897 struct panfrost_context
*ctx
= pan_context(pctx
);
1898 ctx
->stencil_ref
= *ref
;
1900 /* Shader core dirty */
1901 ctx
->dirty
|= PAN_DIRTY_FS
;
1904 static struct pipe_sampler_view
*
1905 panfrost_create_sampler_view(
1906 struct pipe_context
*pctx
,
1907 struct pipe_resource
*texture
,
1908 const struct pipe_sampler_view
*template)
1910 struct panfrost_sampler_view
*so
= CALLOC_STRUCT(panfrost_sampler_view
);
1911 int bytes_per_pixel
= util_format_get_blocksize(texture
->format
);
1913 pipe_reference(NULL
, &texture
->reference
);
1915 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) texture
;
1918 so
->base
= *template;
1919 so
->base
.texture
= texture
;
1920 so
->base
.reference
.count
= 1;
1921 so
->base
.context
= pctx
;
1923 /* sampler_views correspond to texture descriptors, minus the texture
1924 * (data) itself. So, we serialise the descriptor here and cache it for
1927 /* Make sure it's something with which we're familiar */
1928 assert(bytes_per_pixel
>= 1 && bytes_per_pixel
<= 4);
1930 /* TODO: Detect from format better */
1931 const struct util_format_description
*desc
= util_format_description(prsrc
->base
.format
);
1933 unsigned char user_swizzle
[4] = {
1934 template->swizzle_r
,
1935 template->swizzle_g
,
1936 template->swizzle_b
,
1940 enum mali_format format
= panfrost_find_format(desc
);
1942 bool is_depth
= desc
->format
== PIPE_FORMAT_Z32_UNORM
;
1944 unsigned usage2_layout
= 0x10;
1946 switch (prsrc
->bo
->layout
) {
1948 usage2_layout
|= 0x8 | 0x4;
1951 usage2_layout
|= 0x1;
1954 usage2_layout
|= is_depth
? 0x1 : 0x2;
1961 /* Check if we need to set a custom stride by computing the "expected"
1962 * stride and comparing it to what the BO actually wants. Only applies
1963 * to linear textures TODO: Mipmap? */
1965 unsigned actual_stride
= prsrc
->bo
->slices
[0].stride
;
1967 if (prsrc
->bo
->layout
== PAN_LINEAR
&&
1968 template->u
.tex
.last_level
== 0 &&
1969 template->u
.tex
.first_level
== 0 &&
1970 (texture
->width0
* bytes_per_pixel
) != actual_stride
) {
1971 usage2_layout
|= MALI_TEX_MANUAL_STRIDE
;
1974 struct mali_texture_descriptor texture_descriptor
= {
1975 .width
= MALI_POSITIVE(texture
->width0
),
1976 .height
= MALI_POSITIVE(texture
->height0
),
1977 .depth
= MALI_POSITIVE(texture
->depth0
),
1981 .swizzle
= panfrost_translate_swizzle_4(desc
->swizzle
),
1985 .is_not_cubemap
= texture
->target
!= PIPE_TEXTURE_CUBE
,
1987 .usage2
= usage2_layout
1990 .swizzle
= panfrost_translate_swizzle_4(user_swizzle
)
1993 /* TODO: Other base levels require adjusting dimensions / level numbers / etc */
1994 assert (template->u
.tex
.first_level
== 0);
1996 /* Disable mipmapping for now to avoid regressions while automipmapping
1997 * is being implemented. TODO: Remove me once automipmaps work */
1999 //texture_descriptor.nr_mipmap_levels = template->u.tex.last_level - template->u.tex.first_level;
2000 texture_descriptor
.nr_mipmap_levels
= 0;
2002 so
->hw
= texture_descriptor
;
2004 return (struct pipe_sampler_view
*) so
;
2008 panfrost_set_sampler_views(
2009 struct pipe_context
*pctx
,
2010 enum pipe_shader_type shader
,
2011 unsigned start_slot
, unsigned num_views
,
2012 struct pipe_sampler_view
**views
)
2014 struct panfrost_context
*ctx
= pan_context(pctx
);
2016 assert(start_slot
== 0);
2018 ctx
->sampler_view_count
[shader
] = num_views
;
2019 memcpy(ctx
->sampler_views
[shader
], views
, num_views
* sizeof (void *));
2021 ctx
->dirty
|= PAN_DIRTY_TEXTURES
;
2025 panfrost_sampler_view_destroy(
2026 struct pipe_context
*pctx
,
2027 struct pipe_sampler_view
*view
)
2029 pipe_resource_reference(&view
->texture
, NULL
);
2034 panfrost_set_framebuffer_state(struct pipe_context
*pctx
,
2035 const struct pipe_framebuffer_state
*fb
)
2037 struct panfrost_context
*ctx
= pan_context(pctx
);
2039 /* Flush when switching away from an FBO, but not if the framebuffer
2040 * state is being restored by u_blitter
2043 if (!panfrost_is_scanout(ctx
) && !ctx
->blitter
->running
) {
2044 panfrost_flush(pctx
, NULL
, 0);
2047 ctx
->pipe_framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
2048 ctx
->pipe_framebuffer
.samples
= fb
->samples
;
2049 ctx
->pipe_framebuffer
.layers
= fb
->layers
;
2050 ctx
->pipe_framebuffer
.width
= fb
->width
;
2051 ctx
->pipe_framebuffer
.height
= fb
->height
;
2053 for (int i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2054 struct pipe_surface
*cb
= i
< fb
->nr_cbufs
? fb
->cbufs
[i
] : NULL
;
2056 /* check if changing cbuf */
2057 if (ctx
->pipe_framebuffer
.cbufs
[i
] == cb
) continue;
2059 if (cb
&& (i
!= 0)) {
2060 DBG("XXX: Multiple render targets not supported before t7xx!\n");
2065 pipe_surface_reference(&ctx
->pipe_framebuffer
.cbufs
[i
], cb
);
2070 if (ctx
->require_sfbd
)
2071 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
2073 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
2075 panfrost_attach_vt_framebuffer(ctx
);
2077 struct panfrost_resource
*tex
= ((struct panfrost_resource
*) ctx
->pipe_framebuffer
.cbufs
[i
]->texture
);
2078 enum pipe_format format
= ctx
->pipe_framebuffer
.cbufs
[i
]->format
;
2079 bool is_scanout
= panfrost_is_scanout(ctx
);
2081 bool can_afbc
= panfrost_format_supports_afbc(format
);
2083 if (!is_scanout
&& tex
->bo
->layout
!= PAN_AFBC
&& can_afbc
)
2084 panfrost_enable_afbc(ctx
, tex
, false);
2086 if (!is_scanout
&& !tex
->bo
->has_checksum
)
2087 panfrost_enable_checksum(ctx
, tex
);
2091 struct pipe_surface
*zb
= fb
->zsbuf
;
2093 if (ctx
->pipe_framebuffer
.zsbuf
!= zb
) {
2094 pipe_surface_reference(&ctx
->pipe_framebuffer
.zsbuf
, zb
);
2099 if (ctx
->require_sfbd
)
2100 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
2102 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
2104 panfrost_attach_vt_framebuffer(ctx
);
2106 /* Keep the depth FBO linear */
2113 panfrost_create_blend_state(struct pipe_context
*pipe
,
2114 const struct pipe_blend_state
*blend
)
2116 struct panfrost_context
*ctx
= pan_context(pipe
);
2117 struct panfrost_blend_state
*so
= CALLOC_STRUCT(panfrost_blend_state
);
2120 /* TODO: The following features are not yet implemented */
2121 assert(!blend
->logicop_enable
);
2122 assert(!blend
->alpha_to_coverage
);
2123 assert(!blend
->alpha_to_one
);
2125 /* Compile the blend state, first as fixed-function if we can */
2127 if (panfrost_make_fixed_blend_mode(&blend
->rt
[0], so
, blend
->rt
[0].colormask
, &ctx
->blend_color
))
2130 /* If we can't, compile a blend shader instead */
2132 panfrost_make_blend_shader(ctx
, so
, &ctx
->blend_color
);
2138 panfrost_bind_blend_state(struct pipe_context
*pipe
,
2141 struct panfrost_context
*ctx
= pan_context(pipe
);
2142 struct pipe_blend_state
*blend
= (struct pipe_blend_state
*) cso
;
2143 struct panfrost_blend_state
*pblend
= (struct panfrost_blend_state
*) cso
;
2144 ctx
->blend
= pblend
;
2149 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_DITHER
, !blend
->dither
);
2151 /* TODO: Attach color */
2153 /* Shader itself is not dirty, but the shader core is */
2154 ctx
->dirty
|= PAN_DIRTY_FS
;
2158 panfrost_delete_blend_state(struct pipe_context
*pipe
,
2161 struct panfrost_blend_state
*so
= (struct panfrost_blend_state
*) blend
;
2163 if (so
->has_blend_shader
) {
2164 DBG("Deleting blend state leak blend shaders bytecode\n");
2171 panfrost_set_blend_color(struct pipe_context
*pipe
,
2172 const struct pipe_blend_color
*blend_color
)
2174 struct panfrost_context
*ctx
= pan_context(pipe
);
2176 /* If blend_color is we're unbinding, so ctx->blend_color is now undefined -> nothing to do */
2179 ctx
->blend_color
= *blend_color
;
2181 /* The blend mode depends on the blend constant color, due to the
2182 * fixed/programmable split. So, we're forced to regenerate the blend
2185 /* TODO: Attach color */
2190 panfrost_create_depth_stencil_state(struct pipe_context
*pipe
,
2191 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
2193 return mem_dup(depth_stencil
, sizeof(*depth_stencil
));
2197 panfrost_bind_depth_stencil_state(struct pipe_context
*pipe
,
2200 struct panfrost_context
*ctx
= pan_context(pipe
);
2201 struct pipe_depth_stencil_alpha_state
*depth_stencil
= cso
;
2202 ctx
->depth_stencil
= depth_stencil
;
2207 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2208 * emulated in the fragment shader */
2210 if (depth_stencil
->alpha
.enabled
) {
2211 /* We need to trigger a new shader (maybe) */
2212 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->fs
);
2216 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_STENCIL_TEST
, depth_stencil
->stencil
[0].enabled
); /* XXX: which one? */
2218 panfrost_make_stencil_state(&depth_stencil
->stencil
[0], &ctx
->fragment_shader_core
.stencil_front
);
2219 ctx
->fragment_shader_core
.stencil_mask_front
= depth_stencil
->stencil
[0].writemask
;
2221 panfrost_make_stencil_state(&depth_stencil
->stencil
[1], &ctx
->fragment_shader_core
.stencil_back
);
2222 ctx
->fragment_shader_core
.stencil_mask_back
= depth_stencil
->stencil
[1].writemask
;
2224 /* Depth state (TODO: Refactor) */
2225 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_DEPTH_TEST
, depth_stencil
->depth
.enabled
);
2227 int func
= depth_stencil
->depth
.enabled
? depth_stencil
->depth
.func
: PIPE_FUNC_ALWAYS
;
2229 ctx
->fragment_shader_core
.unknown2_3
&= ~MALI_DEPTH_FUNC_MASK
;
2230 ctx
->fragment_shader_core
.unknown2_3
|= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func
));
2232 /* Bounds test not implemented */
2233 assert(!depth_stencil
->depth
.bounds_test
);
2235 ctx
->dirty
|= PAN_DIRTY_FS
;
2239 panfrost_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
2245 panfrost_set_sample_mask(struct pipe_context
*pipe
,
2246 unsigned sample_mask
)
2251 panfrost_set_clip_state(struct pipe_context
*pipe
,
2252 const struct pipe_clip_state
*clip
)
2254 //struct panfrost_context *panfrost = pan_context(pipe);
2258 panfrost_set_viewport_states(struct pipe_context
*pipe
,
2259 unsigned start_slot
,
2260 unsigned num_viewports
,
2261 const struct pipe_viewport_state
*viewports
)
2263 struct panfrost_context
*ctx
= pan_context(pipe
);
2265 assert(start_slot
== 0);
2266 assert(num_viewports
== 1);
2268 ctx
->pipe_viewport
= *viewports
;
2272 panfrost_set_scissor_states(struct pipe_context
*pipe
,
2273 unsigned start_slot
,
2274 unsigned num_scissors
,
2275 const struct pipe_scissor_state
*scissors
)
2277 struct panfrost_context
*ctx
= pan_context(pipe
);
2279 assert(start_slot
== 0);
2280 assert(num_scissors
== 1);
2282 ctx
->scissor
= *scissors
;
2286 panfrost_set_polygon_stipple(struct pipe_context
*pipe
,
2287 const struct pipe_poly_stipple
*stipple
)
2289 //struct panfrost_context *panfrost = pan_context(pipe);
2293 panfrost_set_active_query_state(struct pipe_context
*pipe
,
2296 //struct panfrost_context *panfrost = pan_context(pipe);
2300 panfrost_destroy(struct pipe_context
*pipe
)
2302 struct panfrost_context
*panfrost
= pan_context(pipe
);
2303 struct panfrost_screen
*screen
= pan_screen(pipe
->screen
);
2305 if (panfrost
->blitter
)
2306 util_blitter_destroy(panfrost
->blitter
);
2308 screen
->driver
->free_slab(screen
, &panfrost
->scratchpad
);
2309 screen
->driver
->free_slab(screen
, &panfrost
->varying_mem
);
2310 screen
->driver
->free_slab(screen
, &panfrost
->shaders
);
2311 screen
->driver
->free_slab(screen
, &panfrost
->tiler_heap
);
2312 screen
->driver
->free_slab(screen
, &panfrost
->misc_0
);
2315 static struct pipe_query
*
2316 panfrost_create_query(struct pipe_context
*pipe
,
2320 struct panfrost_query
*q
= CALLOC_STRUCT(panfrost_query
);
2325 return (struct pipe_query
*) q
;
2329 panfrost_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2335 panfrost_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2337 struct panfrost_context
*ctx
= pan_context(pipe
);
2338 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2340 switch (query
->type
) {
2341 case PIPE_QUERY_OCCLUSION_COUNTER
:
2342 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2343 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2345 /* Allocate a word for the query results to be stored */
2346 query
->transfer
= panfrost_allocate_chunk(ctx
, sizeof(unsigned), HEAP_DESCRIPTOR
);
2348 ctx
->occlusion_query
= query
;
2354 DBG("Skipping query %d\n", query
->type
);
2362 panfrost_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2364 struct panfrost_context
*ctx
= pan_context(pipe
);
2365 ctx
->occlusion_query
= NULL
;
2370 panfrost_get_query_result(struct pipe_context
*pipe
,
2371 struct pipe_query
*q
,
2373 union pipe_query_result
*vresult
)
2376 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2378 /* We need to flush out the jobs to actually run the counter, TODO
2379 * check wait, TODO wallpaper after if needed */
2381 panfrost_flush(pipe
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2383 switch (query
->type
) {
2384 case PIPE_QUERY_OCCLUSION_COUNTER
:
2385 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2386 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
: {
2387 /* Read back the query results */
2388 unsigned *result
= (unsigned *) query
->transfer
.cpu
;
2389 unsigned passed
= *result
;
2391 if (query
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
2392 vresult
->u64
= passed
;
2394 vresult
->b
= !!passed
;
2400 DBG("Skipped query get %d\n", query
->type
);
2407 static struct pipe_stream_output_target
*
2408 panfrost_create_stream_output_target(struct pipe_context
*pctx
,
2409 struct pipe_resource
*prsc
,
2410 unsigned buffer_offset
,
2411 unsigned buffer_size
)
2413 struct pipe_stream_output_target
*target
;
2415 target
= CALLOC_STRUCT(pipe_stream_output_target
);
2420 pipe_reference_init(&target
->reference
, 1);
2421 pipe_resource_reference(&target
->buffer
, prsc
);
2423 target
->context
= pctx
;
2424 target
->buffer_offset
= buffer_offset
;
2425 target
->buffer_size
= buffer_size
;
2431 panfrost_stream_output_target_destroy(struct pipe_context
*pctx
,
2432 struct pipe_stream_output_target
*target
)
2434 pipe_resource_reference(&target
->buffer
, NULL
);
2439 panfrost_set_stream_output_targets(struct pipe_context
*pctx
,
2440 unsigned num_targets
,
2441 struct pipe_stream_output_target
**targets
,
2442 const unsigned *offsets
)
2448 panfrost_setup_hardware(struct panfrost_context
*ctx
)
2450 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2451 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
2453 for (int i
= 0; i
< ARRAY_SIZE(ctx
->transient_pools
); ++i
) {
2454 /* Allocate the beginning of the transient pool */
2455 int entry_size
= (1 << 22); /* 4MB */
2457 ctx
->transient_pools
[i
].entry_size
= entry_size
;
2458 ctx
->transient_pools
[i
].entry_count
= 1;
2460 ctx
->transient_pools
[i
].entries
[0] = (struct panfrost_memory_entry
*) pb_slab_alloc(&screen
->slabs
, entry_size
, HEAP_TRANSIENT
);
2463 screen
->driver
->allocate_slab(screen
, &ctx
->scratchpad
, 64, false, 0, 0, 0);
2464 screen
->driver
->allocate_slab(screen
, &ctx
->varying_mem
, 16384, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_COHERENT_LOCAL
, 0, 0);
2465 screen
->driver
->allocate_slab(screen
, &ctx
->shaders
, 4096, true, PAN_ALLOCATE_EXECUTE
, 0, 0);
2466 screen
->driver
->allocate_slab(screen
, &ctx
->tiler_heap
, 32768, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2467 screen
->driver
->allocate_slab(screen
, &ctx
->misc_0
, 128*128, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2471 /* New context creation, which also does hardware initialisation since I don't
2472 * know the better way to structure this :smirk: */
2474 struct pipe_context
*
2475 panfrost_create_context(struct pipe_screen
*screen
, void *priv
, unsigned flags
)
2477 struct panfrost_context
*ctx
= CALLOC_STRUCT(panfrost_context
);
2478 struct panfrost_screen
*pscreen
= pan_screen(screen
);
2479 memset(ctx
, 0, sizeof(*ctx
));
2480 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2483 gpu_id
= pscreen
->driver
->query_gpu_version(pscreen
);
2485 ctx
->is_t6xx
= gpu_id
<= 0x0750; /* For now, this flag means T760 or less */
2486 ctx
->require_sfbd
= gpu_id
< 0x0750; /* T760 is the first to support MFBD */
2488 gallium
->screen
= screen
;
2490 gallium
->destroy
= panfrost_destroy
;
2492 gallium
->set_framebuffer_state
= panfrost_set_framebuffer_state
;
2494 gallium
->flush
= panfrost_flush
;
2495 gallium
->clear
= panfrost_clear
;
2496 gallium
->draw_vbo
= panfrost_draw_vbo
;
2498 gallium
->set_vertex_buffers
= panfrost_set_vertex_buffers
;
2499 gallium
->set_constant_buffer
= panfrost_set_constant_buffer
;
2501 gallium
->set_stencil_ref
= panfrost_set_stencil_ref
;
2503 gallium
->create_sampler_view
= panfrost_create_sampler_view
;
2504 gallium
->set_sampler_views
= panfrost_set_sampler_views
;
2505 gallium
->sampler_view_destroy
= panfrost_sampler_view_destroy
;
2507 gallium
->create_rasterizer_state
= panfrost_create_rasterizer_state
;
2508 gallium
->bind_rasterizer_state
= panfrost_bind_rasterizer_state
;
2509 gallium
->delete_rasterizer_state
= panfrost_generic_cso_delete
;
2511 gallium
->create_vertex_elements_state
= panfrost_create_vertex_elements_state
;
2512 gallium
->bind_vertex_elements_state
= panfrost_bind_vertex_elements_state
;
2513 gallium
->delete_vertex_elements_state
= panfrost_generic_cso_delete
;
2515 gallium
->create_fs_state
= panfrost_create_shader_state
;
2516 gallium
->delete_fs_state
= panfrost_delete_shader_state
;
2517 gallium
->bind_fs_state
= panfrost_bind_fs_state
;
2519 gallium
->create_vs_state
= panfrost_create_shader_state
;
2520 gallium
->delete_vs_state
= panfrost_delete_shader_state
;
2521 gallium
->bind_vs_state
= panfrost_bind_vs_state
;
2523 gallium
->create_sampler_state
= panfrost_create_sampler_state
;
2524 gallium
->delete_sampler_state
= panfrost_generic_cso_delete
;
2525 gallium
->bind_sampler_states
= panfrost_bind_sampler_states
;
2527 gallium
->create_blend_state
= panfrost_create_blend_state
;
2528 gallium
->bind_blend_state
= panfrost_bind_blend_state
;
2529 gallium
->delete_blend_state
= panfrost_delete_blend_state
;
2531 gallium
->set_blend_color
= panfrost_set_blend_color
;
2533 gallium
->create_depth_stencil_alpha_state
= panfrost_create_depth_stencil_state
;
2534 gallium
->bind_depth_stencil_alpha_state
= panfrost_bind_depth_stencil_state
;
2535 gallium
->delete_depth_stencil_alpha_state
= panfrost_delete_depth_stencil_state
;
2537 gallium
->set_sample_mask
= panfrost_set_sample_mask
;
2539 gallium
->set_clip_state
= panfrost_set_clip_state
;
2540 gallium
->set_viewport_states
= panfrost_set_viewport_states
;
2541 gallium
->set_scissor_states
= panfrost_set_scissor_states
;
2542 gallium
->set_polygon_stipple
= panfrost_set_polygon_stipple
;
2543 gallium
->set_active_query_state
= panfrost_set_active_query_state
;
2545 gallium
->create_query
= panfrost_create_query
;
2546 gallium
->destroy_query
= panfrost_destroy_query
;
2547 gallium
->begin_query
= panfrost_begin_query
;
2548 gallium
->end_query
= panfrost_end_query
;
2549 gallium
->get_query_result
= panfrost_get_query_result
;
2551 gallium
->create_stream_output_target
= panfrost_create_stream_output_target
;
2552 gallium
->stream_output_target_destroy
= panfrost_stream_output_target_destroy
;
2553 gallium
->set_stream_output_targets
= panfrost_set_stream_output_targets
;
2555 panfrost_resource_context_init(gallium
);
2557 pscreen
->driver
->init_context(ctx
);
2559 panfrost_setup_hardware(ctx
);
2562 gallium
->stream_uploader
= u_upload_create_default(gallium
);
2563 gallium
->const_uploader
= gallium
->stream_uploader
;
2564 assert(gallium
->stream_uploader
);
2566 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2567 ctx
->draw_modes
= (1 << (PIPE_PRIM_POLYGON
+ 1)) - 1;
2569 ctx
->primconvert
= util_primconvert_create(gallium
, ctx
->draw_modes
);
2571 ctx
->blitter
= util_blitter_create(gallium
);
2572 assert(ctx
->blitter
);
2574 /* Prepare for render! */
2576 panfrost_job_init(ctx
);
2577 panfrost_emit_vertex_payload(ctx
);
2578 panfrost_emit_tiler_payload(ctx
);
2579 panfrost_invalidate_frame(ctx
);
2580 panfrost_default_shader_backend(ctx
);
2581 panfrost_generate_space_filler_indices();