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/half_float.h"
38 #include "indices/u_primconvert.h"
39 #include "tgsi/tgsi_parse.h"
41 #include "pan_screen.h"
42 #include "pan_blending.h"
43 #include "pan_blend_shaders.h"
45 #include "pan_wallpaper.h"
47 static int performance_counter_number
= 0;
48 extern const char *pan_counters_base
;
50 /* Do not actually send anything to the GPU; merely generate the cmdstream as fast as possible. Disables framebuffer writes */
53 /* AFBC is enabled on a per-resource basis (AFBC enabling is theoretically
54 * indepdent between color buffers and depth/stencil). To enable, we allocate
55 * the AFBC metadata buffer and mark that it is enabled. We do -not- actually
56 * edit the fragment job here. This routine should be called ONCE per
57 * AFBC-compressed buffer, rather than on every frame. */
60 panfrost_enable_afbc(struct panfrost_context
*ctx
, struct panfrost_resource
*rsrc
, bool ds
)
62 if (ctx
->require_sfbd
) {
63 DBG("AFBC not supported yet on SFBD\n");
67 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
68 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
69 /* AFBC metadata is 16 bytes per tile */
70 int tile_w
= (rsrc
->base
.width0
+ (MALI_TILE_LENGTH
- 1)) >> MALI_TILE_SHIFT
;
71 int tile_h
= (rsrc
->base
.height0
+ (MALI_TILE_LENGTH
- 1)) >> MALI_TILE_SHIFT
;
72 int bytes_per_pixel
= util_format_get_blocksize(rsrc
->base
.format
);
73 int stride
= bytes_per_pixel
* rsrc
->base
.width0
; /* TODO: Alignment? */
75 stride
*= 2; /* TODO: Should this be carried over? */
76 int main_size
= stride
* rsrc
->base
.height0
;
77 rsrc
->bo
->afbc_metadata_size
= tile_w
* tile_h
* 16;
79 /* Allocate the AFBC slab itself, large enough to hold the above */
80 screen
->driver
->allocate_slab(screen
, &rsrc
->bo
->afbc_slab
,
81 (rsrc
->bo
->afbc_metadata_size
+ main_size
+ 4095) / 4096,
84 rsrc
->bo
->layout
= PAN_AFBC
;
86 /* Compressed textured reads use a tagged pointer to the metadata */
88 rsrc
->bo
->gpu
[0] = rsrc
->bo
->afbc_slab
.gpu
| (ds
? 0 : 1);
89 rsrc
->bo
->cpu
[0] = rsrc
->bo
->afbc_slab
.cpu
;
93 panfrost_enable_checksum(struct panfrost_context
*ctx
, struct panfrost_resource
*rsrc
)
95 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
96 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
97 int tile_w
= (rsrc
->base
.width0
+ (MALI_TILE_LENGTH
- 1)) >> MALI_TILE_SHIFT
;
98 int tile_h
= (rsrc
->base
.height0
+ (MALI_TILE_LENGTH
- 1)) >> MALI_TILE_SHIFT
;
100 /* 8 byte checksum per tile */
101 rsrc
->bo
->checksum_stride
= tile_w
* 8;
102 int pages
= (((rsrc
->bo
->checksum_stride
* tile_h
) + 4095) / 4096);
103 screen
->driver
->allocate_slab(screen
, &rsrc
->bo
->checksum_slab
, pages
, false, 0, 0, 0);
105 rsrc
->bo
->has_checksum
= true;
108 /* Framebuffer descriptor */
111 panfrost_set_framebuffer_resolution(struct mali_single_framebuffer
*fb
, int w
, int h
)
113 fb
->width
= MALI_POSITIVE(w
);
114 fb
->height
= MALI_POSITIVE(h
);
116 /* No idea why this is needed, but it's how resolution_check is
117 * calculated. It's not clear to us yet why the hardware wants this.
118 * The formula itself was discovered mostly by manual bruteforce and
119 * aggressive algebraic simplification. */
121 fb
->resolution_check
= ((w
+ h
) / 3) << 4;
124 struct mali_single_framebuffer
125 panfrost_emit_sfbd(struct panfrost_context
*ctx
)
127 struct mali_single_framebuffer framebuffer
= {
129 .format
= 0x30000000,
130 .clear_flags
= 0x1000,
131 .unknown_address_0
= ctx
->scratchpad
.gpu
,
132 .unknown_address_1
= ctx
->misc_0
.gpu
,
133 .unknown_address_2
= ctx
->misc_0
.gpu
+ 40960,
135 .tiler_heap_free
= ctx
->tiler_heap
.gpu
,
136 .tiler_heap_end
= ctx
->tiler_heap
.gpu
+ ctx
->tiler_heap
.size
,
139 panfrost_set_framebuffer_resolution(&framebuffer
, ctx
->pipe_framebuffer
.width
, ctx
->pipe_framebuffer
.height
);
144 struct bifrost_framebuffer
145 panfrost_emit_mfbd(struct panfrost_context
*ctx
)
147 struct bifrost_framebuffer framebuffer
= {
148 /* It is not yet clear what tiler_meta means or how it's
149 * calculated, but we can tell the lower 32-bits are a
150 * (monotonically increasing?) function of tile count and
151 * geometry complexity; I suspect it defines a memory size of
152 * some kind? for the tiler. It's really unclear at the
153 * moment... but to add to the confusion, the hardware is happy
154 * enough to accept a zero in this field, so we don't even have
155 * to worry about it right now.
157 * The byte (just after the 32-bit mark) is much more
158 * interesting. The higher nibble I've only ever seen as 0xF,
159 * but the lower one I've seen as 0x0 or 0xF, and it's not
160 * obvious what the difference is. But what -is- obvious is
161 * that when the lower nibble is zero, performance is severely
162 * degraded compared to when the lower nibble is set.
163 * Evidently, that nibble enables some sort of fast path,
164 * perhaps relating to caching or tile flush? Regardless, at
165 * this point there's no clear reason not to set it, aside from
166 * substantially increased memory requirements (of the misc_0
169 .tiler_meta
= ((uint64_t) 0xff << 32) | 0x0,
171 .width1
= MALI_POSITIVE(ctx
->pipe_framebuffer
.width
),
172 .height1
= MALI_POSITIVE(ctx
->pipe_framebuffer
.height
),
173 .width2
= MALI_POSITIVE(ctx
->pipe_framebuffer
.width
),
174 .height2
= MALI_POSITIVE(ctx
->pipe_framebuffer
.height
),
179 .rt_count_1
= MALI_POSITIVE(1),
184 /* Corresponds to unknown_address_X of SFBD */
185 .scratchpad
= ctx
->scratchpad
.gpu
,
186 .tiler_scratch_start
= ctx
->misc_0
.gpu
,
188 /* The constant added here is, like the lower word of
189 * tiler_meta, (loosely) another product of framebuffer size
190 * and geometry complexity. It must be sufficiently large for
191 * the tiler_meta fast path to work; if it's too small, there
192 * will be DATA_INVALID_FAULTs. Conversely, it must be less
193 * than the total size of misc_0, or else there's no room. It's
194 * possible this constant configures a partition between two
195 * parts of misc_0? We haven't investigated the functionality,
196 * as these buffers are internally used by the hardware
197 * (presumably by the tiler) but not seemingly touched by the driver
200 .tiler_scratch_middle
= ctx
->misc_0
.gpu
+ 0xf0000,
202 .tiler_heap_start
= ctx
->tiler_heap
.gpu
,
203 .tiler_heap_end
= ctx
->tiler_heap
.gpu
+ ctx
->tiler_heap
.size
,
209 /* Are we currently rendering to the screen (rather than an FBO)? */
212 panfrost_is_scanout(struct panfrost_context
*ctx
)
214 /* If there is no color buffer, it's an FBO */
215 if (!ctx
->pipe_framebuffer
.nr_cbufs
)
218 /* If we're too early that no framebuffer was sent, it's scanout */
219 if (!ctx
->pipe_framebuffer
.cbufs
[0])
222 return ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
223 ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
224 ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;
227 /* Maps float 0.0-1.0 to int 0x00-0xFF */
229 normalised_float_to_u8(float f
)
231 return (uint8_t) (int) (f
* 255.0f
);
236 struct pipe_context
*pipe
,
238 const union pipe_color_union
*color
,
239 double depth
, unsigned stencil
)
241 struct panfrost_context
*ctx
= pan_context(pipe
);
242 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
244 if (buffers
& PIPE_CLEAR_COLOR
) {
245 /* Alpha clear only meaningful without alpha channel, TODO less ad hoc */
246 bool has_alpha
= util_format_has_alpha(ctx
->pipe_framebuffer
.cbufs
[0]->format
);
247 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
249 uint32_t packed_color
=
250 (normalised_float_to_u8(clear_alpha
) << 24) |
251 (normalised_float_to_u8(color
->f
[2]) << 16) |
252 (normalised_float_to_u8(color
->f
[1]) << 8) |
253 (normalised_float_to_u8(color
->f
[0]) << 0);
255 job
->clear_color
= packed_color
;
259 if (buffers
& PIPE_CLEAR_DEPTH
) {
260 job
->clear_depth
= depth
;
263 if (buffers
& PIPE_CLEAR_STENCIL
) {
264 job
->clear_stencil
= stencil
;
267 job
->clear
|= buffers
;
271 panfrost_attach_vt_mfbd(struct panfrost_context
*ctx
)
273 /* MFBD needs a sequential semi-render target upload, but what exactly this is, is beyond me for now */
274 struct bifrost_render_target rts_list
[] = {
279 .framebuffer
= ctx
->misc_0
.gpu
,
284 /* Allocate memory for the three components */
285 int size
= 1024 + sizeof(ctx
->vt_framebuffer_mfbd
) + sizeof(rts_list
);
286 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
288 /* Opaque 1024-block */
289 rts_list
[0].chunknown
.pointer
= transfer
.gpu
;
291 memcpy(transfer
.cpu
+ 1024, &ctx
->vt_framebuffer_mfbd
, sizeof(ctx
->vt_framebuffer_mfbd
));
292 memcpy(transfer
.cpu
+ 1024 + sizeof(ctx
->vt_framebuffer_mfbd
), rts_list
, sizeof(rts_list
));
294 return (transfer
.gpu
+ 1024) | MALI_MFBD
;
298 panfrost_attach_vt_sfbd(struct panfrost_context
*ctx
)
300 return panfrost_upload_transient(ctx
, &ctx
->vt_framebuffer_sfbd
, sizeof(ctx
->vt_framebuffer_sfbd
)) | MALI_SFBD
;
304 panfrost_attach_vt_framebuffer(struct panfrost_context
*ctx
)
306 mali_ptr framebuffer
= ctx
->require_sfbd
?
307 panfrost_attach_vt_sfbd(ctx
) :
308 panfrost_attach_vt_mfbd(ctx
);
310 ctx
->payload_vertex
.postfix
.framebuffer
= framebuffer
;
311 ctx
->payload_tiler
.postfix
.framebuffer
= framebuffer
;
315 panfrost_viewport(struct panfrost_context
*ctx
,
316 float depth_clip_near
,
317 float depth_clip_far
,
318 int viewport_x0
, int viewport_y0
,
319 int viewport_x1
, int viewport_y1
)
321 /* Clip bounds are encoded as floats. The viewport itself is encoded as
322 * (somewhat) asymmetric ints. */
324 struct mali_viewport ret
= {
325 /* By default, do no viewport clipping, i.e. clip to (-inf,
326 * inf) in each direction. Clipping to the viewport in theory
327 * should work, but in practice causes issues when we're not
328 * explicitly trying to scissor */
335 /* We always perform depth clipping (TODO: Can this be disabled?) */
337 .clip_minz
= depth_clip_near
,
338 .clip_maxz
= depth_clip_far
,
340 .viewport0
= { viewport_x0
, viewport_y0
},
341 .viewport1
= { MALI_POSITIVE(viewport_x1
), MALI_POSITIVE(viewport_y1
) },
344 memcpy(ctx
->viewport
, &ret
, sizeof(ret
));
347 /* Reset per-frame context, called on context initialisation as well as after
348 * flushing a frame */
351 panfrost_invalidate_frame(struct panfrost_context
*ctx
)
353 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
;
354 DBG("Uploaded transient %d bytes\n", transient_count
);
356 /* Rotate cmdstream */
357 if ((++ctx
->cmdstream_i
) == (sizeof(ctx
->transient_pools
) / sizeof(ctx
->transient_pools
[0])))
358 ctx
->cmdstream_i
= 0;
360 if (ctx
->require_sfbd
)
361 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
363 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
365 /* Reset varyings allocated */
366 ctx
->varying_height
= 0;
368 /* The transient cmdstream is dirty every frame; the only bits worth preserving
369 * (textures, shaders, etc) are in other buffers anyways */
371 ctx
->transient_pools
[ctx
->cmdstream_i
].entry_index
= 0;
372 ctx
->transient_pools
[ctx
->cmdstream_i
].entry_offset
= 0;
374 /* Regenerate payloads */
375 panfrost_attach_vt_framebuffer(ctx
);
378 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
381 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
| PAN_DIRTY_TEXTURES
;
384 /* In practice, every field of these payloads should be configurable
385 * arbitrarily, which means these functions are basically catch-all's for
386 * as-of-yet unwavering unknowns */
389 panfrost_emit_vertex_payload(struct panfrost_context
*ctx
)
391 struct midgard_payload_vertex_tiler payload
= {
393 .workgroups_z_shift
= 32,
394 .workgroups_x_shift_2
= 0x2,
395 .workgroups_x_shift_3
= 0x5,
397 .gl_enables
= 0x4 | (ctx
->is_t6xx
? 0 : 0x2),
400 memcpy(&ctx
->payload_vertex
, &payload
, sizeof(payload
));
404 panfrost_emit_tiler_payload(struct panfrost_context
*ctx
)
406 struct midgard_payload_vertex_tiler payload
= {
408 .workgroups_z_shift
= 32,
409 .workgroups_x_shift_2
= 0x2,
410 .workgroups_x_shift_3
= 0x6,
412 .zero1
= 0xffff, /* Why is this only seen on test-quad-textured? */
416 /* Reserve the viewport */
417 struct panfrost_transfer t
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_viewport
), HEAP_DESCRIPTOR
);
418 ctx
->viewport
= (struct mali_viewport
*) t
.cpu
;
419 payload
.postfix
.viewport
= t
.gpu
;
421 memcpy(&ctx
->payload_tiler
, &payload
, sizeof(payload
));
425 translate_tex_wrap(enum pipe_tex_wrap w
)
428 case PIPE_TEX_WRAP_REPEAT
:
429 return MALI_WRAP_REPEAT
;
431 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
432 return MALI_WRAP_CLAMP_TO_EDGE
;
434 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
435 return MALI_WRAP_CLAMP_TO_BORDER
;
437 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
438 return MALI_WRAP_MIRRORED_REPEAT
;
447 translate_tex_filter(enum pipe_tex_filter f
)
450 case PIPE_TEX_FILTER_NEAREST
:
453 case PIPE_TEX_FILTER_LINEAR
:
463 translate_mip_filter(enum pipe_tex_mipfilter f
)
465 return (f
== PIPE_TEX_MIPFILTER_LINEAR
) ? MALI_MIP_LINEAR
: 0;
469 panfrost_translate_compare_func(enum pipe_compare_func in
)
472 case PIPE_FUNC_NEVER
:
473 return MALI_FUNC_NEVER
;
476 return MALI_FUNC_LESS
;
478 case PIPE_FUNC_EQUAL
:
479 return MALI_FUNC_EQUAL
;
481 case PIPE_FUNC_LEQUAL
:
482 return MALI_FUNC_LEQUAL
;
484 case PIPE_FUNC_GREATER
:
485 return MALI_FUNC_GREATER
;
487 case PIPE_FUNC_NOTEQUAL
:
488 return MALI_FUNC_NOTEQUAL
;
490 case PIPE_FUNC_GEQUAL
:
491 return MALI_FUNC_GEQUAL
;
493 case PIPE_FUNC_ALWAYS
:
494 return MALI_FUNC_ALWAYS
;
498 return 0; /* Unreachable */
502 panfrost_translate_alt_compare_func(enum pipe_compare_func in
)
505 case PIPE_FUNC_NEVER
:
506 return MALI_ALT_FUNC_NEVER
;
509 return MALI_ALT_FUNC_LESS
;
511 case PIPE_FUNC_EQUAL
:
512 return MALI_ALT_FUNC_EQUAL
;
514 case PIPE_FUNC_LEQUAL
:
515 return MALI_ALT_FUNC_LEQUAL
;
517 case PIPE_FUNC_GREATER
:
518 return MALI_ALT_FUNC_GREATER
;
520 case PIPE_FUNC_NOTEQUAL
:
521 return MALI_ALT_FUNC_NOTEQUAL
;
523 case PIPE_FUNC_GEQUAL
:
524 return MALI_ALT_FUNC_GEQUAL
;
526 case PIPE_FUNC_ALWAYS
:
527 return MALI_ALT_FUNC_ALWAYS
;
531 return 0; /* Unreachable */
535 panfrost_translate_stencil_op(enum pipe_stencil_op in
)
538 case PIPE_STENCIL_OP_KEEP
:
539 return MALI_STENCIL_KEEP
;
541 case PIPE_STENCIL_OP_ZERO
:
542 return MALI_STENCIL_ZERO
;
544 case PIPE_STENCIL_OP_REPLACE
:
545 return MALI_STENCIL_REPLACE
;
547 case PIPE_STENCIL_OP_INCR
:
548 return MALI_STENCIL_INCR
;
550 case PIPE_STENCIL_OP_DECR
:
551 return MALI_STENCIL_DECR
;
553 case PIPE_STENCIL_OP_INCR_WRAP
:
554 return MALI_STENCIL_INCR_WRAP
;
556 case PIPE_STENCIL_OP_DECR_WRAP
:
557 return MALI_STENCIL_DECR_WRAP
;
559 case PIPE_STENCIL_OP_INVERT
:
560 return MALI_STENCIL_INVERT
;
564 return 0; /* Unreachable */
568 panfrost_make_stencil_state(const struct pipe_stencil_state
*in
, struct mali_stencil_test
*out
)
570 out
->ref
= 0; /* Gallium gets it from elsewhere */
572 out
->mask
= in
->valuemask
;
573 out
->func
= panfrost_translate_compare_func(in
->func
);
574 out
->sfail
= panfrost_translate_stencil_op(in
->fail_op
);
575 out
->dpfail
= panfrost_translate_stencil_op(in
->zfail_op
);
576 out
->dppass
= panfrost_translate_stencil_op(in
->zpass_op
);
580 panfrost_default_shader_backend(struct panfrost_context
*ctx
)
582 struct mali_shader_meta shader
= {
583 .alpha_coverage
= ~MALI_ALPHA_COVERAGE(0.000000),
585 .unknown2_3
= MALI_DEPTH_FUNC(MALI_FUNC_ALWAYS
) | 0x3010,
586 .unknown2_4
= MALI_NO_MSAA
| 0x4e0,
590 shader
.unknown2_4
|= 0x10;
593 struct pipe_stencil_state default_stencil
= {
595 .func
= PIPE_FUNC_ALWAYS
,
596 .fail_op
= MALI_STENCIL_KEEP
,
597 .zfail_op
= MALI_STENCIL_KEEP
,
598 .zpass_op
= MALI_STENCIL_KEEP
,
603 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_front
);
604 shader
.stencil_mask_front
= default_stencil
.writemask
;
606 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_back
);
607 shader
.stencil_mask_back
= default_stencil
.writemask
;
609 if (default_stencil
.enabled
)
610 shader
.unknown2_4
|= MALI_STENCIL_TEST
;
612 memcpy(&ctx
->fragment_shader_core
, &shader
, sizeof(shader
));
615 /* Generates a vertex/tiler job. This is, in some sense, the heart of the
616 * graphics command stream. It should be called once per draw, accordding to
617 * presentations. Set is_tiler for "tiler" jobs (fragment shader jobs, but in
618 * Mali parlance, "fragment" refers to framebuffer writeout). Clear it for
621 struct panfrost_transfer
622 panfrost_vertex_tiler_job(struct panfrost_context
*ctx
, bool is_tiler
, bool is_elided_tiler
)
624 /* Each draw call corresponds to two jobs, and we want to offset to leave room for the set-value job */
625 int draw_job_index
= 1 + (2 * ctx
->draw_count
);
627 struct mali_job_descriptor_header job
= {
628 .job_type
= is_tiler
? JOB_TYPE_TILER
: JOB_TYPE_VERTEX
,
629 .job_index
= draw_job_index
+ (is_tiler
? 1 : 0),
631 .job_descriptor_size
= 1,
635 /* Only non-elided tiler jobs have dependencies which are known at this point */
637 if (is_tiler
&& !is_elided_tiler
) {
638 /* Tiler jobs depend on vertex jobs */
640 job
.job_dependency_index_1
= draw_job_index
;
642 /* Tiler jobs also depend on the previous tiler job */
645 job
.job_dependency_index_2
= draw_job_index
- 1;
648 struct midgard_payload_vertex_tiler
*payload
= is_tiler
? &ctx
->payload_tiler
: &ctx
->payload_vertex
;
650 /* There's some padding hacks on 32-bit */
657 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(job
) + sizeof(*payload
));
658 memcpy(transfer
.cpu
, &job
, sizeof(job
));
659 memcpy(transfer
.cpu
+ sizeof(job
) - offset
, payload
, sizeof(*payload
));
663 /* Generates a set value job. It's unclear what exactly this does, why it's
664 * necessary, and when to call it. */
667 panfrost_set_value_job(struct panfrost_context
*ctx
)
669 struct mali_job_descriptor_header job
= {
670 .job_type
= JOB_TYPE_SET_VALUE
,
671 .job_descriptor_size
= 1,
672 .job_index
= 1 + (2 * ctx
->draw_count
),
675 struct mali_payload_set_value payload
= {
676 .out
= ctx
->misc_0
.gpu
,
680 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(job
) + sizeof(payload
));
681 memcpy(transfer
.cpu
, &job
, sizeof(job
));
682 memcpy(transfer
.cpu
+ sizeof(job
), &payload
, sizeof(payload
));
684 ctx
->u_set_value_job
= (struct mali_job_descriptor_header
*) transfer
.cpu
;
685 ctx
->set_value_job
= transfer
.gpu
;
688 /* Emits attributes and varying descriptors, which should be called every draw,
689 * excepting some obscure circumstances */
692 panfrost_emit_vertex_data(struct panfrost_context
*ctx
)
694 /* TODO: Only update the dirtied buffers */
695 union mali_attr attrs
[PIPE_MAX_ATTRIBS
];
696 union mali_attr varyings
[PIPE_MAX_ATTRIBS
];
698 unsigned invocation_count
= MALI_NEGATIVE(ctx
->payload_tiler
.prefix
.invocation_count
);
700 for (int i
= 0; i
< ctx
->vertex_buffer_count
; ++i
) {
701 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[i
];
702 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
.resource
);
704 /* Let's figure out the layout of the attributes in memory so
705 * we can be smart about size computation. The idea is to
706 * figure out the maximum src_offset, which tells us the latest
707 * spot a vertex could start. Meanwhile, we figure out the size
708 * of the attribute memory (assuming interleaved
709 * representation) and tack on the max src_offset for a
710 * reasonably good upper bound on the size.
712 * Proving correctness is left as an exercise to the reader.
715 unsigned max_src_offset
= 0;
717 for (unsigned j
= 0; j
< ctx
->vertex
->num_elements
; ++j
) {
718 if (ctx
->vertex
->pipe
[j
].vertex_buffer_index
!= i
) continue;
719 max_src_offset
= MAX2(max_src_offset
, ctx
->vertex
->pipe
[j
].src_offset
);
722 /* Offset vertex count by draw_start to make sure we upload enough */
723 attrs
[i
].stride
= buf
->stride
;
724 attrs
[i
].size
= buf
->stride
* (ctx
->payload_vertex
.draw_start
+ invocation_count
) + max_src_offset
;
726 /* Vertex elements are -already- GPU-visible, at
727 * rsrc->gpu. However, attribute buffers must be 64 aligned. If
728 * it is not, for now we have to duplicate the buffer. */
730 mali_ptr effective_address
= (rsrc
->bo
->gpu
[0] + buf
->buffer_offset
);
732 if (effective_address
& 0x3F) {
733 attrs
[i
].elements
= panfrost_upload_transient(ctx
, rsrc
->bo
->cpu
[0] + buf
->buffer_offset
, attrs
[i
].size
) | 1;
735 attrs
[i
].elements
= effective_address
| 1;
739 struct panfrost_varyings
*vars
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
].varyings
;
741 for (int i
= 0; i
< vars
->varying_buffer_count
; ++i
) {
742 mali_ptr varying_address
= ctx
->varying_mem
.gpu
+ ctx
->varying_height
;
744 varyings
[i
].elements
= varying_address
| 1;
745 varyings
[i
].stride
= vars
->varyings_stride
[i
];
746 varyings
[i
].size
= vars
->varyings_stride
[i
] * invocation_count
;
748 /* If this varying has to be linked somewhere, do it now. See
749 * pan_assemble.c for the indices. TODO: Use a more generic
750 * linking interface */
754 ctx
->payload_tiler
.postfix
.position_varying
= varying_address
;
757 ctx
->payload_tiler
.primitive_size
.pointer
= varying_address
;
760 /* Varyings appear to need 64-byte alignment */
761 ctx
->varying_height
+= ALIGN(varyings
[i
].size
, 64);
763 /* Ensure that we fit */
764 assert(ctx
->varying_height
< ctx
->varying_mem
.size
);
767 ctx
->payload_vertex
.postfix
.attributes
= panfrost_upload_transient(ctx
, attrs
, ctx
->vertex_buffer_count
* sizeof(union mali_attr
));
769 mali_ptr varyings_p
= panfrost_upload_transient(ctx
, &varyings
, vars
->varying_buffer_count
* sizeof(union mali_attr
));
770 ctx
->payload_vertex
.postfix
.varyings
= varyings_p
;
771 ctx
->payload_tiler
.postfix
.varyings
= varyings_p
;
774 /* Go through dirty flags and actualise them in the cmdstream. */
777 panfrost_emit_for_draw(struct panfrost_context
*ctx
, bool with_vertex_data
)
779 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
781 if (with_vertex_data
) {
782 panfrost_emit_vertex_data(ctx
);
785 bool msaa
= ctx
->rasterizer
->base
.multisample
;
787 if (ctx
->dirty
& PAN_DIRTY_RASTERIZER
) {
788 ctx
->payload_tiler
.gl_enables
= ctx
->rasterizer
->tiler_gl_enables
;
790 /* TODO: Sample size */
791 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_HAS_MSAA
, msaa
);
792 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_MSAA
, !msaa
);
795 /* Enable job requirements at draw-time */
798 job
->requirements
|= PAN_REQ_MSAA
;
800 if (ctx
->depth_stencil
->depth
.writemask
)
801 job
->requirements
|= PAN_REQ_DEPTH_WRITE
;
803 if (ctx
->occlusion_query
) {
804 ctx
->payload_tiler
.gl_enables
|= MALI_OCCLUSION_QUERY
| MALI_OCCLUSION_PRECISE
;
805 ctx
->payload_tiler
.postfix
.occlusion_counter
= ctx
->occlusion_query
->transfer
.gpu
;
808 if (ctx
->dirty
& PAN_DIRTY_VS
) {
811 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
813 /* Late shader descriptor assignments */
814 vs
->tripipe
->texture_count
= ctx
->sampler_view_count
[PIPE_SHADER_VERTEX
];
815 vs
->tripipe
->sampler_count
= ctx
->sampler_count
[PIPE_SHADER_VERTEX
];
818 vs
->tripipe
->midgard1
.unknown1
= 0x2201;
820 ctx
->payload_vertex
.postfix
._shader_upper
= vs
->tripipe_gpu
>> 4;
822 /* Varying descriptor is tied to the vertex shader. Also the
823 * fragment shader, I suppose, but it's generated with the
824 * vertex shader so */
826 struct panfrost_varyings
*varyings
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
].varyings
;
828 ctx
->payload_vertex
.postfix
.varying_meta
= varyings
->varyings_descriptor
;
829 ctx
->payload_tiler
.postfix
.varying_meta
= varyings
->varyings_descriptor_fragment
;
832 if (ctx
->dirty
& (PAN_DIRTY_RASTERIZER
| PAN_DIRTY_VS
)) {
833 /* Check if we need to link the gl_PointSize varying */
835 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
837 bool needs_gl_point_size
= vs
->writes_point_size
&& ctx
->payload_tiler
.prefix
.draw_mode
== MALI_POINTS
;
839 if (!needs_gl_point_size
) {
840 /* If the size is constant, write it out. Otherwise,
841 * don't touch primitive_size (since we would clobber
842 * the pointer there) */
844 ctx
->payload_tiler
.primitive_size
.constant
= ctx
->rasterizer
->base
.line_width
;
847 /* Set the flag for varying (pointer) point size if the shader needs that */
848 SET_BIT(ctx
->payload_tiler
.prefix
.unknown_draw
, MALI_DRAW_VARYING_SIZE
, needs_gl_point_size
);
851 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
853 ctx
->dirty
|= PAN_DIRTY_FS
;
855 if (ctx
->dirty
& PAN_DIRTY_FS
) {
857 struct panfrost_shader_state
*variant
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
859 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
862 COPY(attribute_count
);
864 COPY(midgard1
.uniform_count
);
865 COPY(midgard1
.work_count
);
866 COPY(midgard1
.unknown2
);
869 /* If there is a blend shader, work registers are shared */
871 if (ctx
->blend
->has_blend_shader
)
872 ctx
->fragment_shader_core
.midgard1
.work_count
= /*MAX2(ctx->fragment_shader_core.midgard1.work_count, ctx->blend->blend_work_count)*/16;
874 /* Set late due to depending on render state */
875 /* The one at the end seems to mean "1 UBO" */
876 ctx
->fragment_shader_core
.midgard1
.unknown1
= MALI_NO_ALPHA_TO_COVERAGE
| 0x200 | 0x2201;
878 /* Assign texture/sample count right before upload */
879 ctx
->fragment_shader_core
.texture_count
= ctx
->sampler_view_count
[PIPE_SHADER_FRAGMENT
];
880 ctx
->fragment_shader_core
.sampler_count
= ctx
->sampler_count
[PIPE_SHADER_FRAGMENT
];
882 /* Assign the stencil refs late */
883 ctx
->fragment_shader_core
.stencil_front
.ref
= ctx
->stencil_ref
.ref_value
[0];
884 ctx
->fragment_shader_core
.stencil_back
.ref
= ctx
->stencil_ref
.ref_value
[1];
886 /* CAN_DISCARD should be set if the fragment shader possibly
887 * contains a 'discard' instruction. It is likely this is
888 * related to optimizations related to forward-pixel kill, as
889 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
890 * thing?" by Peter Harris
893 if (variant
->can_discard
) {
894 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
895 ctx
->fragment_shader_core
.midgard1
.unknown1
&= ~MALI_NO_ALPHA_TO_COVERAGE
;
896 ctx
->fragment_shader_core
.midgard1
.unknown1
|= 0x4000;
897 ctx
->fragment_shader_core
.midgard1
.unknown1
= 0x4200;
900 /* Check if we're using the default blend descriptor (fast path) */
903 !ctx
->blend
->has_blend_shader
&&
904 (ctx
->blend
->equation
.rgb_mode
== 0x122) &&
905 (ctx
->blend
->equation
.alpha_mode
== 0x122) &&
906 (ctx
->blend
->equation
.color_mask
== 0xf);
908 if (ctx
->require_sfbd
) {
909 /* When only a single render target platform is used, the blend
910 * information is inside the shader meta itself. We
911 * additionally need to signal CAN_DISCARD for nontrivial blend
912 * modes (so we're able to read back the destination buffer) */
914 if (ctx
->blend
->has_blend_shader
) {
915 ctx
->fragment_shader_core
.blend_shader
= ctx
->blend
->blend_shader
;
917 memcpy(&ctx
->fragment_shader_core
.blend_equation
, &ctx
->blend
->equation
, sizeof(ctx
->blend
->equation
));
921 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
925 size_t size
= sizeof(struct mali_shader_meta
) + sizeof(struct mali_blend_meta
);
926 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
927 memcpy(transfer
.cpu
, &ctx
->fragment_shader_core
, sizeof(struct mali_shader_meta
));
929 ctx
->payload_tiler
.postfix
._shader_upper
= (transfer
.gpu
) >> 4;
931 if (!ctx
->require_sfbd
) {
932 /* Additional blend descriptor tacked on for jobs using MFBD */
934 unsigned blend_count
= 0;
936 if (ctx
->blend
->has_blend_shader
) {
937 /* For a blend shader, the bottom nibble corresponds to
938 * the number of work registers used, which signals the
939 * -existence- of a blend shader */
941 assert(ctx
->blend
->blend_work_count
>= 2);
942 blend_count
|= MIN2(ctx
->blend
->blend_work_count
, 3);
944 /* Otherwise, the bottom bit simply specifies if
945 * blending (anything other than REPLACE) is enabled */
952 /* Second blend equation is always a simple replace */
954 uint64_t replace_magic
= 0xf0122122;
955 struct mali_blend_equation replace_mode
;
956 memcpy(&replace_mode
, &replace_magic
, sizeof(replace_mode
));
958 struct mali_blend_meta blend_meta
[] = {
960 .unk1
= 0x200 | blend_count
,
961 .blend_equation_1
= ctx
->blend
->equation
,
962 .blend_equation_2
= replace_mode
966 if (ctx
->blend
->has_blend_shader
)
967 memcpy(&blend_meta
[0].blend_equation_1
, &ctx
->blend
->blend_shader
, sizeof(ctx
->blend
->blend_shader
));
969 memcpy(transfer
.cpu
+ sizeof(struct mali_shader_meta
), blend_meta
, sizeof(blend_meta
));
973 if (ctx
->dirty
& PAN_DIRTY_VERTEX
) {
974 ctx
->payload_vertex
.postfix
.attribute_meta
= ctx
->vertex
->descriptor_ptr
;
977 if (ctx
->dirty
& PAN_DIRTY_SAMPLERS
) {
978 /* Upload samplers back to back, no padding */
980 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
981 if (!ctx
->sampler_count
[t
]) continue;
983 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(struct mali_sampler_descriptor
) * ctx
->sampler_count
[t
]);
984 struct mali_sampler_descriptor
*desc
= (struct mali_sampler_descriptor
*) transfer
.cpu
;
986 for (int i
= 0; i
< ctx
->sampler_count
[t
]; ++i
) {
987 desc
[i
] = ctx
->samplers
[t
][i
]->hw
;
990 if (t
== PIPE_SHADER_FRAGMENT
)
991 ctx
->payload_tiler
.postfix
.sampler_descriptor
= transfer
.gpu
;
992 else if (t
== PIPE_SHADER_VERTEX
)
993 ctx
->payload_vertex
.postfix
.sampler_descriptor
= transfer
.gpu
;
999 if (ctx
->dirty
& PAN_DIRTY_TEXTURES
) {
1000 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
1002 if (!ctx
->sampler_view_count
[t
]) continue;
1004 uint64_t trampolines
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
1006 for (int i
= 0; i
< ctx
->sampler_view_count
[t
]; ++i
) {
1007 if (!ctx
->sampler_views
[t
][i
])
1010 struct pipe_resource
*tex_rsrc
= ctx
->sampler_views
[t
][i
]->base
.texture
;
1011 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) tex_rsrc
;
1013 /* Inject the address in. */
1014 for (int l
= 0; l
< (tex_rsrc
->last_level
+ 1); ++l
)
1015 ctx
->sampler_views
[t
][i
]->hw
.swizzled_bitmaps
[l
] = rsrc
->bo
->gpu
[l
];
1017 /* Workaround maybe-errata (?) with non-mipmaps */
1018 int s
= ctx
->sampler_views
[t
][i
]->hw
.nr_mipmap_levels
;
1020 if (!rsrc
->bo
->is_mipmap
) {
1022 /* HW ERRATA, not needed after t6XX */
1023 ctx
->sampler_views
[t
][i
]->hw
.swizzled_bitmaps
[1] = rsrc
->bo
->gpu
[0];
1025 ctx
->sampler_views
[t
][i
]->hw
.unknown3A
= 1;
1028 ctx
->sampler_views
[t
][i
]->hw
.nr_mipmap_levels
= 0;
1031 trampolines
[i
] = panfrost_upload_transient(ctx
, &ctx
->sampler_views
[t
][i
]->hw
, sizeof(struct mali_texture_descriptor
));
1034 ctx
->sampler_views
[t
][i
]->hw
.nr_mipmap_levels
= s
;
1037 ctx
->sampler_views
[t
][i
]->hw
.unknown3A
= 0;
1041 mali_ptr trampoline
= panfrost_upload_transient(ctx
, trampolines
, sizeof(uint64_t) * ctx
->sampler_view_count
[t
]);
1043 if (t
== PIPE_SHADER_FRAGMENT
)
1044 ctx
->payload_tiler
.postfix
.texture_trampoline
= trampoline
;
1045 else if (t
== PIPE_SHADER_VERTEX
)
1046 ctx
->payload_vertex
.postfix
.texture_trampoline
= trampoline
;
1052 /* Generate the viewport vector of the form: <width/2, height/2, centerx, centery> */
1053 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
1055 float viewport_vec4
[] = {
1057 fabsf(vp
->scale
[1]),
1060 /* -1.0 * vp->translate[1] */ fabs(1.0 * vp
->scale
[1]) /* XXX */
1063 for (int i
= 0; i
< PIPE_SHADER_TYPES
; ++i
) {
1064 struct panfrost_constant_buffer
*buf
= &ctx
->constant_buffer
[i
];
1066 if (i
== PIPE_SHADER_VERTEX
|| i
== PIPE_SHADER_FRAGMENT
) {
1067 /* It doesn't matter if we don't use all the memory;
1068 * we'd need a dummy UBO anyway. Compute the max */
1070 size_t size
= sizeof(viewport_vec4
) + buf
->size
;
1071 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
1073 /* Keep track how much we've uploaded */
1076 if (i
== PIPE_SHADER_VERTEX
) {
1077 /* Upload viewport */
1078 memcpy(transfer
.cpu
+ offset
, viewport_vec4
, sizeof(viewport_vec4
));
1079 offset
+= sizeof(viewport_vec4
);
1082 /* Upload uniforms */
1083 memcpy(transfer
.cpu
+ offset
, buf
->buffer
, buf
->size
);
1085 int uniform_count
= 0;
1087 struct mali_vertex_tiler_postfix
*postfix
;
1090 case PIPE_SHADER_VERTEX
:
1091 uniform_count
= ctx
->vs
->variants
[ctx
->vs
->active_variant
].uniform_count
;
1092 postfix
= &ctx
->payload_vertex
.postfix
;
1095 case PIPE_SHADER_FRAGMENT
:
1096 uniform_count
= ctx
->fs
->variants
[ctx
->fs
->active_variant
].uniform_count
;
1097 postfix
= &ctx
->payload_tiler
.postfix
;
1101 DBG("Unknown shader stage %d in uniform upload\n", i
);
1105 /* Also attach the same buffer as a UBO for extended access */
1107 struct mali_uniform_buffer_meta uniform_buffers
[] = {
1109 .size
= MALI_POSITIVE((2 + uniform_count
)),
1110 .ptr
= transfer
.gpu
>> 2,
1114 mali_ptr ubufs
= panfrost_upload_transient(ctx
, uniform_buffers
, sizeof(uniform_buffers
));
1115 postfix
->uniforms
= transfer
.gpu
;
1116 postfix
->uniform_buffers
= ubufs
;
1125 /* Corresponds to exactly one draw, but does not submit anything */
1128 panfrost_queue_draw(struct panfrost_context
*ctx
)
1130 /* TODO: Expand the array? */
1131 if (ctx
->draw_count
>= MAX_DRAW_CALLS
) {
1132 DBG("Job buffer overflow, ignoring draw\n");
1136 /* Handle dirty flags now */
1137 panfrost_emit_for_draw(ctx
, true);
1139 struct panfrost_transfer vertex
= panfrost_vertex_tiler_job(ctx
, false, false);
1140 struct panfrost_transfer tiler
= panfrost_vertex_tiler_job(ctx
, true, false);
1142 ctx
->u_vertex_jobs
[ctx
->vertex_job_count
] = (struct mali_job_descriptor_header
*) vertex
.cpu
;
1143 ctx
->vertex_jobs
[ctx
->vertex_job_count
++] = vertex
.gpu
;
1145 ctx
->u_tiler_jobs
[ctx
->tiler_job_count
] = (struct mali_job_descriptor_header
*) tiler
.cpu
;
1146 ctx
->tiler_jobs
[ctx
->tiler_job_count
++] = tiler
.gpu
;
1151 /* At the end of the frame, the vertex and tiler jobs are linked together and
1152 * then the fragment job is plonked at the end. Set value job is first for
1153 * unknown reasons. */
1156 panfrost_link_job_pair(struct mali_job_descriptor_header
*first
, mali_ptr next
)
1158 if (first
->job_descriptor_size
)
1159 first
->next_job_64
= (u64
) (uintptr_t) next
;
1161 first
->next_job_32
= (u32
) (uintptr_t) next
;
1165 panfrost_link_jobs(struct panfrost_context
*ctx
)
1167 if (ctx
->draw_count
) {
1168 /* Generate the set_value_job */
1169 panfrost_set_value_job(ctx
);
1171 /* Have the first vertex job depend on the set value job */
1172 ctx
->u_vertex_jobs
[0]->job_dependency_index_1
= ctx
->u_set_value_job
->job_index
;
1175 panfrost_link_job_pair(ctx
->u_set_value_job
, ctx
->vertex_jobs
[0]);
1178 /* V -> V/T ; T -> T/null */
1179 for (int i
= 0; i
< ctx
->vertex_job_count
; ++i
) {
1180 bool isLast
= (i
+ 1) == ctx
->vertex_job_count
;
1182 panfrost_link_job_pair(ctx
->u_vertex_jobs
[i
], isLast
? ctx
->tiler_jobs
[0] : ctx
->vertex_jobs
[i
+ 1]);
1186 for (int i
= 0; i
< ctx
->tiler_job_count
; ++i
) {
1187 bool isLast
= (i
+ 1) == ctx
->tiler_job_count
;
1188 panfrost_link_job_pair(ctx
->u_tiler_jobs
[i
], isLast
? 0 : ctx
->tiler_jobs
[i
+ 1]);
1192 /* The entire frame is in memory -- send it off to the kernel! */
1195 panfrost_submit_frame(struct panfrost_context
*ctx
, bool flush_immediate
,
1196 struct pipe_fence_handle
**fence
)
1198 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
1199 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
1201 /* Edge case if screen is cleared and nothing else */
1202 bool has_draws
= ctx
->draw_count
> 0;
1204 /* Workaround a bizarre lockup (a hardware errata?) */
1206 flush_immediate
= true;
1208 /* A number of jobs are batched -- this must be linked and cleared */
1209 panfrost_link_jobs(ctx
);
1211 ctx
->draw_count
= 0;
1212 ctx
->vertex_job_count
= 0;
1213 ctx
->tiler_job_count
= 0;
1217 bool is_scanout
= panfrost_is_scanout(ctx
);
1218 int fragment_id
= screen
->driver
->submit_vs_fs_job(ctx
, has_draws
, is_scanout
);
1220 /* If visual, we can stall a frame */
1222 if (!flush_immediate
)
1223 screen
->driver
->force_flush_fragment(ctx
, fence
);
1225 screen
->last_fragment_id
= fragment_id
;
1226 screen
->last_fragment_flushed
= false;
1228 /* If readback, flush now (hurts the pipelined performance) */
1229 if (flush_immediate
)
1230 screen
->driver
->force_flush_fragment(ctx
, fence
);
1232 if (screen
->driver
->dump_counters
&& pan_counters_base
) {
1233 screen
->driver
->dump_counters(screen
);
1236 snprintf(filename
, sizeof(filename
), "%s/frame%d.mdgprf", pan_counters_base
, ++performance_counter_number
);
1237 FILE *fp
= fopen(filename
, "wb");
1238 fwrite(screen
->perf_counters
.cpu
, 4096, sizeof(uint32_t), fp
);
1247 struct pipe_context
*pipe
,
1248 struct pipe_fence_handle
**fence
,
1251 struct panfrost_context
*ctx
= pan_context(pipe
);
1252 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
1254 /* Nothing to do! */
1255 if (!ctx
->draw_count
&& !job
->clear
) return;
1257 /* Whether to stall the pipeline for immediately correct results */
1258 bool flush_immediate
= flags
& PIPE_FLUSH_END_OF_FRAME
;
1260 /* Submit the frame itself */
1261 panfrost_submit_frame(ctx
, flush_immediate
, fence
);
1263 /* Prepare for the next frame */
1264 panfrost_invalidate_frame(ctx
);
1267 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1270 g2m_draw_mode(enum pipe_prim_type mode
)
1273 DEFINE_CASE(POINTS
);
1275 DEFINE_CASE(LINE_LOOP
);
1276 DEFINE_CASE(LINE_STRIP
);
1277 DEFINE_CASE(TRIANGLES
);
1278 DEFINE_CASE(TRIANGLE_STRIP
);
1279 DEFINE_CASE(TRIANGLE_FAN
);
1281 DEFINE_CASE(QUAD_STRIP
);
1282 DEFINE_CASE(POLYGON
);
1285 DBG("Illegal draw mode %d\n", mode
);
1287 return MALI_LINE_LOOP
;
1294 panfrost_translate_index_size(unsigned size
)
1298 return MALI_DRAW_INDEXED_UINT8
;
1301 return MALI_DRAW_INDEXED_UINT16
;
1304 return MALI_DRAW_INDEXED_UINT32
;
1307 DBG("Unknown index size %d\n", size
);
1313 static const uint8_t *
1314 panfrost_get_index_buffer_raw(const struct pipe_draw_info
*info
)
1316 if (info
->has_user_indices
) {
1317 return (const uint8_t *) info
->index
.user
;
1319 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (info
->index
.resource
);
1320 return (const uint8_t *) rsrc
->bo
->cpu
[0];
1324 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1325 * good for the duration of the draw (transient), could last longer */
1328 panfrost_get_index_buffer_mapped(struct panfrost_context
*ctx
, const struct pipe_draw_info
*info
)
1330 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (info
->index
.resource
);
1332 off_t offset
= info
->start
* info
->index_size
;
1334 if (!info
->has_user_indices
) {
1335 /* Only resources can be directly mapped */
1336 return rsrc
->bo
->gpu
[0] + offset
;
1338 /* Otherwise, we need to upload to transient memory */
1339 const uint8_t *ibuf8
= panfrost_get_index_buffer_raw(info
);
1340 return panfrost_upload_transient(ctx
, ibuf8
+ offset
, info
->count
* info
->index_size
);
1344 #define CALCULATE_MIN_MAX_INDEX(T, buffer, start, count) \
1345 for (unsigned _idx = (start); _idx < (start + count); ++_idx) { \
1346 T idx = buffer[_idx]; \
1347 if (idx > max_index) max_index = idx; \
1348 if (idx < min_index) min_index = idx; \
1353 struct pipe_context
*pipe
,
1354 const struct pipe_draw_info
*info
)
1356 struct panfrost_context
*ctx
= pan_context(pipe
);
1358 ctx
->payload_vertex
.draw_start
= info
->start
;
1359 ctx
->payload_tiler
.draw_start
= info
->start
;
1361 int mode
= info
->mode
;
1363 /* Fallback for unsupported modes */
1365 if (!(ctx
->draw_modes
& mode
)) {
1366 if (mode
== PIPE_PRIM_QUADS
&& info
->count
== 4 && ctx
->rasterizer
&& !ctx
->rasterizer
->base
.flatshade
) {
1367 mode
= PIPE_PRIM_TRIANGLE_FAN
;
1369 if (info
->count
< 4) {
1370 /* Degenerate case? */
1374 util_primconvert_save_rasterizer_state(ctx
->primconvert
, &ctx
->rasterizer
->base
);
1375 util_primconvert_draw_vbo(ctx
->primconvert
, info
);
1380 /* Now that we have a guaranteed terminating path, find the job.
1381 * Assignment commented out to prevent unused warning */
1383 /* struct panfrost_job *job = */ panfrost_get_job_for_fbo(ctx
);
1385 ctx
->payload_tiler
.prefix
.draw_mode
= g2m_draw_mode(mode
);
1387 ctx
->vertex_count
= info
->count
;
1389 /* For non-indexed draws, they're the same */
1390 unsigned invocation_count
= ctx
->vertex_count
;
1392 /* For higher amounts of vertices (greater than what fits in a 16-bit
1393 * short), the other value is needed, otherwise there will be bizarre
1394 * rendering artefacts. It's not clear what these values mean yet. */
1396 ctx
->payload_tiler
.prefix
.unknown_draw
&= ~(0x3000 | 0x18000);
1397 ctx
->payload_tiler
.prefix
.unknown_draw
|= (mode
== PIPE_PRIM_POINTS
|| ctx
->vertex_count
> 65535) ? 0x3000 : 0x18000;
1399 if (info
->index_size
) {
1400 /* Calculate the min/max index used so we can figure out how
1401 * many times to invoke the vertex shader */
1403 const uint8_t *ibuf8
= panfrost_get_index_buffer_raw(info
);
1405 int min_index
= INT_MAX
;
1408 if (info
->index_size
== 1) {
1409 CALCULATE_MIN_MAX_INDEX(uint8_t, ibuf8
, info
->start
, info
->count
);
1410 } else if (info
->index_size
== 2) {
1411 const uint16_t *ibuf16
= (const uint16_t *) ibuf8
;
1412 CALCULATE_MIN_MAX_INDEX(uint16_t, ibuf16
, info
->start
, info
->count
);
1413 } else if (info
->index_size
== 4) {
1414 const uint32_t *ibuf32
= (const uint32_t *) ibuf8
;
1415 CALCULATE_MIN_MAX_INDEX(uint32_t, ibuf32
, info
->start
, info
->count
);
1420 /* Make sure we didn't go crazy */
1421 assert(min_index
< INT_MAX
);
1422 assert(max_index
> 0);
1423 assert(max_index
> min_index
);
1425 /* Use the corresponding values */
1426 invocation_count
= max_index
- min_index
+ 1;
1427 ctx
->payload_vertex
.draw_start
= min_index
;
1428 ctx
->payload_tiler
.draw_start
= min_index
;
1430 ctx
->payload_tiler
.prefix
.negative_start
= -min_index
;
1431 ctx
->payload_tiler
.prefix
.index_count
= MALI_POSITIVE(info
->count
);
1433 //assert(!info->restart_index); /* TODO: Research */
1434 assert(!info
->index_bias
);
1435 //assert(!info->min_index); /* TODO: Use value */
1437 ctx
->payload_tiler
.prefix
.unknown_draw
|= panfrost_translate_index_size(info
->index_size
);
1438 ctx
->payload_tiler
.prefix
.indices
= panfrost_get_index_buffer_mapped(ctx
, info
);
1440 /* Index count == vertex count, if no indexing is applied, as
1441 * if it is internally indexed in the expected order */
1443 ctx
->payload_tiler
.prefix
.negative_start
= 0;
1444 ctx
->payload_tiler
.prefix
.index_count
= MALI_POSITIVE(ctx
->vertex_count
);
1446 /* Reverse index state */
1447 ctx
->payload_tiler
.prefix
.unknown_draw
&= ~MALI_DRAW_INDEXED_UINT32
;
1448 ctx
->payload_tiler
.prefix
.indices
= (uintptr_t) NULL
;
1451 ctx
->payload_vertex
.prefix
.invocation_count
= MALI_POSITIVE(invocation_count
);
1452 ctx
->payload_tiler
.prefix
.invocation_count
= MALI_POSITIVE(invocation_count
);
1454 /* Fire off the draw itself */
1455 panfrost_queue_draw(ctx
);
1461 panfrost_generic_cso_delete(struct pipe_context
*pctx
, void *hwcso
)
1467 panfrost_set_scissor(struct panfrost_context
*ctx
)
1469 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
1471 if (ss
&& ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
&& 0) {
1472 ctx
->viewport
->viewport0
[0] = ss
->minx
;
1473 ctx
->viewport
->viewport0
[1] = ss
->miny
;
1474 ctx
->viewport
->viewport1
[0] = MALI_POSITIVE(ss
->maxx
);
1475 ctx
->viewport
->viewport1
[1] = MALI_POSITIVE(ss
->maxy
);
1477 ctx
->viewport
->viewport0
[0] = 0;
1478 ctx
->viewport
->viewport0
[1] = 0;
1479 ctx
->viewport
->viewport1
[0] = MALI_POSITIVE(ctx
->pipe_framebuffer
.width
);
1480 ctx
->viewport
->viewport1
[1] = MALI_POSITIVE(ctx
->pipe_framebuffer
.height
);
1485 panfrost_create_rasterizer_state(
1486 struct pipe_context
*pctx
,
1487 const struct pipe_rasterizer_state
*cso
)
1489 struct panfrost_context
*ctx
= pan_context(pctx
);
1490 struct panfrost_rasterizer
*so
= CALLOC_STRUCT(panfrost_rasterizer
);
1494 /* Bitmask, unknown meaning of the start value */
1495 so
->tiler_gl_enables
= ctx
->is_t6xx
? 0x105 : 0x7;
1497 so
->tiler_gl_enables
|= MALI_FRONT_FACE(
1498 cso
->front_ccw
? MALI_CCW
: MALI_CW
);
1500 if (cso
->cull_face
& PIPE_FACE_FRONT
)
1501 so
->tiler_gl_enables
|= MALI_CULL_FACE_FRONT
;
1503 if (cso
->cull_face
& PIPE_FACE_BACK
)
1504 so
->tiler_gl_enables
|= MALI_CULL_FACE_BACK
;
1510 panfrost_bind_rasterizer_state(
1511 struct pipe_context
*pctx
,
1514 struct panfrost_context
*ctx
= pan_context(pctx
);
1515 struct pipe_rasterizer_state
*cso
= hwcso
;
1517 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1521 /* If scissor test has changed, we'll need to update that now */
1522 bool update_scissor
= !ctx
->rasterizer
|| ctx
->rasterizer
->base
.scissor
!= cso
->scissor
;
1524 ctx
->rasterizer
= hwcso
;
1526 /* Actualise late changes */
1528 panfrost_set_scissor(ctx
);
1530 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
1534 panfrost_create_vertex_elements_state(
1535 struct pipe_context
*pctx
,
1536 unsigned num_elements
,
1537 const struct pipe_vertex_element
*elements
)
1539 struct panfrost_context
*ctx
= pan_context(pctx
);
1540 struct panfrost_vertex_state
*so
= CALLOC_STRUCT(panfrost_vertex_state
);
1542 so
->num_elements
= num_elements
;
1543 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
1545 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_attr_meta
) * num_elements
, HEAP_DESCRIPTOR
);
1546 so
->hw
= (struct mali_attr_meta
*) transfer
.cpu
;
1547 so
->descriptor_ptr
= transfer
.gpu
;
1549 /* Allocate memory for the descriptor state */
1551 for (int i
= 0; i
< num_elements
; ++i
) {
1552 so
->hw
[i
].index
= elements
[i
].vertex_buffer_index
;
1554 enum pipe_format fmt
= elements
[i
].src_format
;
1555 const struct util_format_description
*desc
= util_format_description(fmt
);
1556 so
->hw
[i
].unknown1
= 0x2;
1557 so
->hw
[i
].swizzle
= panfrost_get_default_swizzle(desc
->nr_channels
);
1559 so
->hw
[i
].format
= panfrost_find_format(desc
);
1561 /* The field itself should probably be shifted over */
1562 so
->hw
[i
].src_offset
= elements
[i
].src_offset
;
1569 panfrost_bind_vertex_elements_state(
1570 struct pipe_context
*pctx
,
1573 struct panfrost_context
*ctx
= pan_context(pctx
);
1575 ctx
->vertex
= hwcso
;
1576 ctx
->dirty
|= PAN_DIRTY_VERTEX
;
1580 panfrost_delete_vertex_elements_state(struct pipe_context
*pctx
, void *hwcso
)
1582 struct panfrost_vertex_state
*so
= (struct panfrost_vertex_state
*) hwcso
;
1583 unsigned bytes
= sizeof(struct mali_attr_meta
) * so
->num_elements
;
1584 DBG("Vertex elements delete leaks descriptor (%d bytes)\n", bytes
);
1589 panfrost_create_shader_state(
1590 struct pipe_context
*pctx
,
1591 const struct pipe_shader_state
*cso
)
1593 struct panfrost_shader_variants
*so
= CALLOC_STRUCT(panfrost_shader_variants
);
1596 /* Token deep copy to prevent memory corruption */
1598 if (cso
->type
== PIPE_SHADER_IR_TGSI
)
1599 so
->base
.tokens
= tgsi_dup_tokens(so
->base
.tokens
);
1605 panfrost_delete_shader_state(
1606 struct pipe_context
*pctx
,
1609 struct panfrost_shader_variants
*cso
= (struct panfrost_shader_variants
*) so
;
1611 if (cso
->base
.type
== PIPE_SHADER_IR_TGSI
) {
1612 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1615 unsigned leak
= cso
->variant_count
* sizeof(struct mali_shader_meta
);
1616 DBG("Deleting shader state leaks descriptors (%d bytes), and shader bytecode\n", leak
);
1622 panfrost_create_sampler_state(
1623 struct pipe_context
*pctx
,
1624 const struct pipe_sampler_state
*cso
)
1626 struct panfrost_sampler_state
*so
= CALLOC_STRUCT(panfrost_sampler_state
);
1629 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1631 struct mali_sampler_descriptor sampler_descriptor
= {
1632 .filter_mode
= MALI_TEX_MIN(translate_tex_filter(cso
->min_img_filter
))
1633 | MALI_TEX_MAG(translate_tex_filter(cso
->mag_img_filter
))
1634 | translate_mip_filter(cso
->min_mip_filter
)
1637 .wrap_s
= translate_tex_wrap(cso
->wrap_s
),
1638 .wrap_t
= translate_tex_wrap(cso
->wrap_t
),
1639 .wrap_r
= translate_tex_wrap(cso
->wrap_r
),
1640 .compare_func
= panfrost_translate_alt_compare_func(cso
->compare_func
),
1642 cso
->border_color
.f
[0],
1643 cso
->border_color
.f
[1],
1644 cso
->border_color
.f
[2],
1645 cso
->border_color
.f
[3]
1647 .min_lod
= FIXED_16(0.0),
1648 .max_lod
= FIXED_16(31.0),
1652 so
->hw
= sampler_descriptor
;
1658 panfrost_bind_sampler_states(
1659 struct pipe_context
*pctx
,
1660 enum pipe_shader_type shader
,
1661 unsigned start_slot
, unsigned num_sampler
,
1664 assert(start_slot
== 0);
1666 struct panfrost_context
*ctx
= pan_context(pctx
);
1668 /* XXX: Should upload, not just copy? */
1669 ctx
->sampler_count
[shader
] = num_sampler
;
1670 memcpy(ctx
->samplers
[shader
], sampler
, num_sampler
* sizeof (void *));
1672 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
;
1676 panfrost_variant_matches(struct panfrost_context
*ctx
, struct panfrost_shader_state
*variant
)
1678 struct pipe_alpha_state
*alpha
= &ctx
->depth_stencil
->alpha
;
1680 if (alpha
->enabled
|| variant
->alpha_state
.enabled
) {
1681 /* Make sure enable state is at least the same */
1682 if (alpha
->enabled
!= variant
->alpha_state
.enabled
) {
1686 /* Check that the contents of the test are the same */
1687 bool same_func
= alpha
->func
== variant
->alpha_state
.func
;
1688 bool same_ref
= alpha
->ref_value
== variant
->alpha_state
.ref_value
;
1690 if (!(same_func
&& same_ref
)) {
1694 /* Otherwise, we're good to go */
1699 panfrost_bind_fs_state(
1700 struct pipe_context
*pctx
,
1703 struct panfrost_context
*ctx
= pan_context(pctx
);
1708 /* Match the appropriate variant */
1710 signed variant
= -1;
1712 struct panfrost_shader_variants
*variants
= (struct panfrost_shader_variants
*) hwcso
;
1714 for (unsigned i
= 0; i
< variants
->variant_count
; ++i
) {
1715 if (panfrost_variant_matches(ctx
, &variants
->variants
[i
])) {
1721 if (variant
== -1) {
1722 /* No variant matched, so create a new one */
1723 variant
= variants
->variant_count
++;
1724 assert(variants
->variant_count
< MAX_SHADER_VARIANTS
);
1726 variants
->variants
[variant
].base
= hwcso
;
1727 variants
->variants
[variant
].alpha_state
= ctx
->depth_stencil
->alpha
;
1729 /* Allocate the mapped descriptor ahead-of-time. TODO: Use for FS as well as VS */
1730 struct panfrost_context
*ctx
= pan_context(pctx
);
1731 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_shader_meta
), HEAP_DESCRIPTOR
);
1733 variants
->variants
[variant
].tripipe
= (struct mali_shader_meta
*) transfer
.cpu
;
1734 variants
->variants
[variant
].tripipe_gpu
= transfer
.gpu
;
1738 /* Select this variant */
1739 variants
->active_variant
= variant
;
1741 struct panfrost_shader_state
*shader_state
= &variants
->variants
[variant
];
1742 assert(panfrost_variant_matches(ctx
, shader_state
));
1744 /* Now we have a variant selected, so compile and go */
1746 if (!shader_state
->compiled
) {
1747 panfrost_shader_compile(ctx
, shader_state
->tripipe
, NULL
, JOB_TYPE_TILER
, shader_state
);
1748 shader_state
->compiled
= true;
1752 ctx
->dirty
|= PAN_DIRTY_FS
;
1756 panfrost_bind_vs_state(
1757 struct pipe_context
*pctx
,
1760 struct panfrost_context
*ctx
= pan_context(pctx
);
1765 if (!ctx
->vs
->variants
[0].compiled
) {
1766 ctx
->vs
->variants
[0].base
= hwcso
;
1768 /* TODO DRY from above */
1769 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_shader_meta
), HEAP_DESCRIPTOR
);
1770 ctx
->vs
->variants
[0].tripipe
= (struct mali_shader_meta
*) transfer
.cpu
;
1771 ctx
->vs
->variants
[0].tripipe_gpu
= transfer
.gpu
;
1773 panfrost_shader_compile(ctx
, ctx
->vs
->variants
[0].tripipe
, NULL
, JOB_TYPE_VERTEX
, &ctx
->vs
->variants
[0]);
1774 ctx
->vs
->variants
[0].compiled
= true;
1778 ctx
->dirty
|= PAN_DIRTY_VS
;
1782 panfrost_set_vertex_buffers(
1783 struct pipe_context
*pctx
,
1784 unsigned start_slot
,
1785 unsigned num_buffers
,
1786 const struct pipe_vertex_buffer
*buffers
)
1788 struct panfrost_context
*ctx
= pan_context(pctx
);
1789 assert(num_buffers
<= PIPE_MAX_ATTRIBS
);
1791 /* XXX: Dirty tracking? etc */
1793 size_t sz
= sizeof(buffers
[0]) * num_buffers
;
1794 ctx
->vertex_buffers
= malloc(sz
);
1795 ctx
->vertex_buffer_count
= num_buffers
;
1796 memcpy(ctx
->vertex_buffers
, buffers
, sz
);
1798 if (ctx
->vertex_buffers
) {
1799 free(ctx
->vertex_buffers
);
1800 ctx
->vertex_buffers
= NULL
;
1803 ctx
->vertex_buffer_count
= 0;
1808 panfrost_set_constant_buffer(
1809 struct pipe_context
*pctx
,
1810 enum pipe_shader_type shader
, uint index
,
1811 const struct pipe_constant_buffer
*buf
)
1813 struct panfrost_context
*ctx
= pan_context(pctx
);
1814 struct panfrost_constant_buffer
*pbuf
= &ctx
->constant_buffer
[shader
];
1816 size_t sz
= buf
? buf
->buffer_size
: 0;
1818 /* Free previous buffer */
1825 pbuf
->buffer
= NULL
;
1828 /* If unbinding, we're done */
1833 /* Multiple constant buffers not yet supported */
1838 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
);
1841 cpu
= rsrc
->bo
->cpu
[0];
1842 } else if (buf
->user_buffer
) {
1843 cpu
= buf
->user_buffer
;
1845 DBG("No constant buffer?\n");
1849 /* Copy the constant buffer into the driver context for later upload */
1851 pbuf
->buffer
= malloc(sz
);
1852 memcpy(pbuf
->buffer
, cpu
+ buf
->buffer_offset
, sz
);
1856 panfrost_set_stencil_ref(
1857 struct pipe_context
*pctx
,
1858 const struct pipe_stencil_ref
*ref
)
1860 struct panfrost_context
*ctx
= pan_context(pctx
);
1861 ctx
->stencil_ref
= *ref
;
1863 /* Shader core dirty */
1864 ctx
->dirty
|= PAN_DIRTY_FS
;
1867 static struct pipe_sampler_view
*
1868 panfrost_create_sampler_view(
1869 struct pipe_context
*pctx
,
1870 struct pipe_resource
*texture
,
1871 const struct pipe_sampler_view
*template)
1873 struct panfrost_sampler_view
*so
= CALLOC_STRUCT(panfrost_sampler_view
);
1874 int bytes_per_pixel
= util_format_get_blocksize(texture
->format
);
1876 pipe_reference(NULL
, &texture
->reference
);
1878 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) texture
;
1880 so
->base
= *template;
1881 so
->base
.texture
= texture
;
1882 so
->base
.reference
.count
= 1;
1883 so
->base
.context
= pctx
;
1885 /* sampler_views correspond to texture descriptors, minus the texture
1886 * (data) itself. So, we serialise the descriptor here and cache it for
1889 /* TODO: Other types of textures */
1890 assert(template->target
== PIPE_TEXTURE_2D
);
1892 /* Make sure it's something with which we're familiar */
1893 assert(bytes_per_pixel
>= 1 && bytes_per_pixel
<= 4);
1895 /* TODO: Detect from format better */
1896 const struct util_format_description
*desc
= util_format_description(prsrc
->base
.format
);
1898 unsigned char user_swizzle
[4] = {
1899 template->swizzle_r
,
1900 template->swizzle_g
,
1901 template->swizzle_b
,
1905 enum mali_format format
= panfrost_find_format(desc
);
1907 bool is_depth
= desc
->format
== PIPE_FORMAT_Z32_UNORM
;
1909 unsigned usage2_layout
= 0x10;
1911 switch (prsrc
->bo
->layout
) {
1913 usage2_layout
|= 0x8 | 0x4;
1916 usage2_layout
|= 0x1;
1919 usage2_layout
|= is_depth
? 0x1 : 0x2;
1926 struct mali_texture_descriptor texture_descriptor
= {
1927 .width
= MALI_POSITIVE(texture
->width0
),
1928 .height
= MALI_POSITIVE(texture
->height0
),
1929 .depth
= MALI_POSITIVE(texture
->depth0
),
1933 .swizzle
= panfrost_translate_swizzle_4(desc
->swizzle
),
1937 .is_not_cubemap
= 1,
1939 .usage2
= usage2_layout
1942 .swizzle
= panfrost_translate_swizzle_4(user_swizzle
)
1945 /* TODO: Other base levels require adjusting dimensions / level numbers / etc */
1946 assert (template->u
.tex
.first_level
== 0);
1948 texture_descriptor
.nr_mipmap_levels
= template->u
.tex
.last_level
- template->u
.tex
.first_level
;
1950 so
->hw
= texture_descriptor
;
1952 return (struct pipe_sampler_view
*) so
;
1956 panfrost_set_sampler_views(
1957 struct pipe_context
*pctx
,
1958 enum pipe_shader_type shader
,
1959 unsigned start_slot
, unsigned num_views
,
1960 struct pipe_sampler_view
**views
)
1962 struct panfrost_context
*ctx
= pan_context(pctx
);
1964 assert(start_slot
== 0);
1966 ctx
->sampler_view_count
[shader
] = num_views
;
1967 memcpy(ctx
->sampler_views
[shader
], views
, num_views
* sizeof (void *));
1969 ctx
->dirty
|= PAN_DIRTY_TEXTURES
;
1973 panfrost_sampler_view_destroy(
1974 struct pipe_context
*pctx
,
1975 struct pipe_sampler_view
*views
)
1977 //struct panfrost_context *ctx = pan_context(pctx);
1985 panfrost_set_framebuffer_state(struct pipe_context
*pctx
,
1986 const struct pipe_framebuffer_state
*fb
)
1988 struct panfrost_context
*ctx
= pan_context(pctx
);
1990 /* Flush when switching away from an FBO */
1992 if (!panfrost_is_scanout(ctx
)) {
1993 panfrost_flush(pctx
, NULL
, 0);
1996 ctx
->pipe_framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
1997 ctx
->pipe_framebuffer
.samples
= fb
->samples
;
1998 ctx
->pipe_framebuffer
.layers
= fb
->layers
;
1999 ctx
->pipe_framebuffer
.width
= fb
->width
;
2000 ctx
->pipe_framebuffer
.height
= fb
->height
;
2002 for (int i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2003 struct pipe_surface
*cb
= i
< fb
->nr_cbufs
? fb
->cbufs
[i
] : NULL
;
2005 /* check if changing cbuf */
2006 if (ctx
->pipe_framebuffer
.cbufs
[i
] == cb
) continue;
2008 if (cb
&& (i
!= 0)) {
2009 DBG("XXX: Multiple render targets not supported before t7xx!\n");
2014 pipe_surface_reference(&ctx
->pipe_framebuffer
.cbufs
[i
], cb
);
2019 if (ctx
->require_sfbd
)
2020 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
2022 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
2024 panfrost_attach_vt_framebuffer(ctx
);
2025 panfrost_set_scissor(ctx
);
2027 struct panfrost_resource
*tex
= ((struct panfrost_resource
*) ctx
->pipe_framebuffer
.cbufs
[i
]->texture
);
2028 bool is_scanout
= panfrost_is_scanout(ctx
);
2030 if (!is_scanout
&& tex
->bo
->layout
!= PAN_AFBC
) {
2031 /* The blob is aggressive about enabling AFBC. As such,
2032 * it's pretty much necessary to use it here, since we
2033 * have no traces of non-compressed FBO. */
2035 panfrost_enable_afbc(ctx
, tex
, false);
2038 if (!is_scanout
&& !tex
->bo
->has_checksum
) {
2039 /* Enable transaction elimination if we can */
2040 panfrost_enable_checksum(ctx
, tex
);
2045 struct pipe_surface
*zb
= fb
->zsbuf
;
2047 if (ctx
->pipe_framebuffer
.zsbuf
!= zb
) {
2048 pipe_surface_reference(&ctx
->pipe_framebuffer
.zsbuf
, zb
);
2053 if (ctx
->require_sfbd
)
2054 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
2056 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
2058 panfrost_attach_vt_framebuffer(ctx
);
2059 panfrost_set_scissor(ctx
);
2061 struct panfrost_resource
*tex
= ((struct panfrost_resource
*) ctx
->pipe_framebuffer
.zsbuf
->texture
);
2063 if (tex
->bo
->layout
!= PAN_AFBC
&& !panfrost_is_scanout(ctx
))
2064 panfrost_enable_afbc(ctx
, tex
, true);
2071 panfrost_create_blend_state(struct pipe_context
*pipe
,
2072 const struct pipe_blend_state
*blend
)
2074 struct panfrost_context
*ctx
= pan_context(pipe
);
2075 struct panfrost_blend_state
*so
= CALLOC_STRUCT(panfrost_blend_state
);
2078 /* TODO: The following features are not yet implemented */
2079 assert(!blend
->logicop_enable
);
2080 assert(!blend
->alpha_to_coverage
);
2081 assert(!blend
->alpha_to_one
);
2083 /* Compile the blend state, first as fixed-function if we can */
2085 if (panfrost_make_fixed_blend_mode(&blend
->rt
[0], &so
->equation
, blend
->rt
[0].colormask
, &ctx
->blend_color
))
2088 /* If we can't, compile a blend shader instead */
2090 panfrost_make_blend_shader(ctx
, so
, &ctx
->blend_color
);
2096 panfrost_bind_blend_state(struct pipe_context
*pipe
,
2099 struct panfrost_context
*ctx
= pan_context(pipe
);
2100 struct pipe_blend_state
*blend
= (struct pipe_blend_state
*) cso
;
2101 struct panfrost_blend_state
*pblend
= (struct panfrost_blend_state
*) cso
;
2102 ctx
->blend
= pblend
;
2107 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_DITHER
, !blend
->dither
);
2109 /* TODO: Attach color */
2111 /* Shader itself is not dirty, but the shader core is */
2112 ctx
->dirty
|= PAN_DIRTY_FS
;
2116 panfrost_delete_blend_state(struct pipe_context
*pipe
,
2119 struct panfrost_blend_state
*so
= (struct panfrost_blend_state
*) blend
;
2121 if (so
->has_blend_shader
) {
2122 DBG("Deleting blend state leak blend shaders bytecode\n");
2129 panfrost_set_blend_color(struct pipe_context
*pipe
,
2130 const struct pipe_blend_color
*blend_color
)
2132 struct panfrost_context
*ctx
= pan_context(pipe
);
2134 /* If blend_color is we're unbinding, so ctx->blend_color is now undefined -> nothing to do */
2137 ctx
->blend_color
= *blend_color
;
2139 /* The blend mode depends on the blend constant color, due to the
2140 * fixed/programmable split. So, we're forced to regenerate the blend
2143 /* TODO: Attach color */
2148 panfrost_create_depth_stencil_state(struct pipe_context
*pipe
,
2149 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
2151 return mem_dup(depth_stencil
, sizeof(*depth_stencil
));
2155 panfrost_bind_depth_stencil_state(struct pipe_context
*pipe
,
2158 struct panfrost_context
*ctx
= pan_context(pipe
);
2159 struct pipe_depth_stencil_alpha_state
*depth_stencil
= cso
;
2160 ctx
->depth_stencil
= depth_stencil
;
2165 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2166 * emulated in the fragment shader */
2168 if (depth_stencil
->alpha
.enabled
) {
2169 /* We need to trigger a new shader (maybe) */
2170 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->fs
);
2174 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_STENCIL_TEST
, depth_stencil
->stencil
[0].enabled
); /* XXX: which one? */
2176 panfrost_make_stencil_state(&depth_stencil
->stencil
[0], &ctx
->fragment_shader_core
.stencil_front
);
2177 ctx
->fragment_shader_core
.stencil_mask_front
= depth_stencil
->stencil
[0].writemask
;
2179 panfrost_make_stencil_state(&depth_stencil
->stencil
[1], &ctx
->fragment_shader_core
.stencil_back
);
2180 ctx
->fragment_shader_core
.stencil_mask_back
= depth_stencil
->stencil
[1].writemask
;
2182 /* Depth state (TODO: Refactor) */
2183 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_DEPTH_TEST
, depth_stencil
->depth
.enabled
);
2185 int func
= depth_stencil
->depth
.enabled
? depth_stencil
->depth
.func
: PIPE_FUNC_ALWAYS
;
2187 ctx
->fragment_shader_core
.unknown2_3
&= ~MALI_DEPTH_FUNC_MASK
;
2188 ctx
->fragment_shader_core
.unknown2_3
|= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func
));
2190 /* Bounds test not implemented */
2191 assert(!depth_stencil
->depth
.bounds_test
);
2193 ctx
->dirty
|= PAN_DIRTY_FS
;
2197 panfrost_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
2203 panfrost_set_sample_mask(struct pipe_context
*pipe
,
2204 unsigned sample_mask
)
2209 panfrost_set_clip_state(struct pipe_context
*pipe
,
2210 const struct pipe_clip_state
*clip
)
2212 //struct panfrost_context *panfrost = pan_context(pipe);
2216 panfrost_set_viewport_states(struct pipe_context
*pipe
,
2217 unsigned start_slot
,
2218 unsigned num_viewports
,
2219 const struct pipe_viewport_state
*viewports
)
2221 struct panfrost_context
*ctx
= pan_context(pipe
);
2223 assert(start_slot
== 0);
2224 assert(num_viewports
== 1);
2226 ctx
->pipe_viewport
= *viewports
;
2229 /* TODO: What if not centered? */
2230 float w
= abs(viewports
->scale
[0]) * 2.0;
2231 float h
= abs(viewports
->scale
[1]) * 2.0;
2233 ctx
->viewport
.viewport1
[0] = MALI_POSITIVE((int) w
);
2234 ctx
->viewport
.viewport1
[1] = MALI_POSITIVE((int) h
);
2239 panfrost_set_scissor_states(struct pipe_context
*pipe
,
2240 unsigned start_slot
,
2241 unsigned num_scissors
,
2242 const struct pipe_scissor_state
*scissors
)
2244 struct panfrost_context
*ctx
= pan_context(pipe
);
2246 assert(start_slot
== 0);
2247 assert(num_scissors
== 1);
2249 ctx
->scissor
= *scissors
;
2251 panfrost_set_scissor(ctx
);
2255 panfrost_set_polygon_stipple(struct pipe_context
*pipe
,
2256 const struct pipe_poly_stipple
*stipple
)
2258 //struct panfrost_context *panfrost = pan_context(pipe);
2262 panfrost_set_active_query_state(struct pipe_context
*pipe
,
2265 //struct panfrost_context *panfrost = pan_context(pipe);
2269 panfrost_destroy(struct pipe_context
*pipe
)
2271 struct panfrost_context
*panfrost
= pan_context(pipe
);
2272 struct panfrost_screen
*screen
= pan_screen(pipe
->screen
);
2274 if (panfrost
->blitter
)
2275 util_blitter_destroy(panfrost
->blitter
);
2277 screen
->driver
->free_slab(screen
, &panfrost
->scratchpad
);
2278 screen
->driver
->free_slab(screen
, &panfrost
->varying_mem
);
2279 screen
->driver
->free_slab(screen
, &panfrost
->shaders
);
2280 screen
->driver
->free_slab(screen
, &panfrost
->tiler_heap
);
2281 screen
->driver
->free_slab(screen
, &panfrost
->misc_0
);
2284 static struct pipe_query
*
2285 panfrost_create_query(struct pipe_context
*pipe
,
2289 struct panfrost_query
*q
= CALLOC_STRUCT(panfrost_query
);
2294 return (struct pipe_query
*) q
;
2298 panfrost_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2304 panfrost_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2306 struct panfrost_context
*ctx
= pan_context(pipe
);
2307 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2309 switch (query
->type
) {
2310 case PIPE_QUERY_OCCLUSION_COUNTER
:
2311 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2312 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2314 /* Allocate a word for the query results to be stored */
2315 query
->transfer
= panfrost_allocate_chunk(ctx
, sizeof(unsigned), HEAP_DESCRIPTOR
);
2317 ctx
->occlusion_query
= query
;
2323 DBG("Skipping query %d\n", query
->type
);
2331 panfrost_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2333 struct panfrost_context
*ctx
= pan_context(pipe
);
2334 ctx
->occlusion_query
= NULL
;
2339 panfrost_get_query_result(struct pipe_context
*pipe
,
2340 struct pipe_query
*q
,
2342 union pipe_query_result
*vresult
)
2345 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2347 /* We need to flush out the jobs to actually run the counter, TODO
2348 * check wait, TODO wallpaper after if needed */
2350 panfrost_flush(pipe
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2352 switch (query
->type
) {
2353 case PIPE_QUERY_OCCLUSION_COUNTER
:
2354 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2355 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
: {
2356 /* Read back the query results */
2357 unsigned *result
= (unsigned *) query
->transfer
.cpu
;
2358 unsigned passed
= *result
;
2360 if (query
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
2361 vresult
->u64
= passed
;
2363 vresult
->b
= !!passed
;
2369 DBG("Skipped query get %d\n", query
->type
);
2377 panfrost_setup_hardware(struct panfrost_context
*ctx
)
2379 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2380 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
2382 for (int i
= 0; i
< ARRAY_SIZE(ctx
->transient_pools
); ++i
) {
2383 /* Allocate the beginning of the transient pool */
2384 int entry_size
= (1 << 22); /* 4MB */
2386 ctx
->transient_pools
[i
].entry_size
= entry_size
;
2387 ctx
->transient_pools
[i
].entry_count
= 1;
2389 ctx
->transient_pools
[i
].entries
[0] = (struct panfrost_memory_entry
*) pb_slab_alloc(&screen
->slabs
, entry_size
, HEAP_TRANSIENT
);
2392 screen
->driver
->allocate_slab(screen
, &ctx
->scratchpad
, 64, false, 0, 0, 0);
2393 screen
->driver
->allocate_slab(screen
, &ctx
->varying_mem
, 16384, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_COHERENT_LOCAL
, 0, 0);
2394 screen
->driver
->allocate_slab(screen
, &ctx
->shaders
, 4096, true, PAN_ALLOCATE_EXECUTE
, 0, 0);
2395 screen
->driver
->allocate_slab(screen
, &ctx
->tiler_heap
, 32768, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2396 screen
->driver
->allocate_slab(screen
, &ctx
->misc_0
, 128*128, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2400 /* New context creation, which also does hardware initialisation since I don't
2401 * know the better way to structure this :smirk: */
2403 struct pipe_context
*
2404 panfrost_create_context(struct pipe_screen
*screen
, void *priv
, unsigned flags
)
2406 struct panfrost_context
*ctx
= CALLOC_STRUCT(panfrost_context
);
2407 struct panfrost_screen
*pscreen
= pan_screen(screen
);
2408 memset(ctx
, 0, sizeof(*ctx
));
2409 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2412 gpu_id
= pscreen
->driver
->query_gpu_version(pscreen
);
2414 ctx
->is_t6xx
= gpu_id
<= 0x0750; /* For now, this flag means T760 or less */
2415 ctx
->require_sfbd
= gpu_id
< 0x0750; /* T760 is the first to support MFBD */
2417 gallium
->screen
= screen
;
2419 gallium
->destroy
= panfrost_destroy
;
2421 gallium
->set_framebuffer_state
= panfrost_set_framebuffer_state
;
2423 gallium
->flush
= panfrost_flush
;
2424 gallium
->clear
= panfrost_clear
;
2425 gallium
->draw_vbo
= panfrost_draw_vbo
;
2427 gallium
->set_vertex_buffers
= panfrost_set_vertex_buffers
;
2428 gallium
->set_constant_buffer
= panfrost_set_constant_buffer
;
2430 gallium
->set_stencil_ref
= panfrost_set_stencil_ref
;
2432 gallium
->create_sampler_view
= panfrost_create_sampler_view
;
2433 gallium
->set_sampler_views
= panfrost_set_sampler_views
;
2434 gallium
->sampler_view_destroy
= panfrost_sampler_view_destroy
;
2436 gallium
->create_rasterizer_state
= panfrost_create_rasterizer_state
;
2437 gallium
->bind_rasterizer_state
= panfrost_bind_rasterizer_state
;
2438 gallium
->delete_rasterizer_state
= panfrost_generic_cso_delete
;
2440 gallium
->create_vertex_elements_state
= panfrost_create_vertex_elements_state
;
2441 gallium
->bind_vertex_elements_state
= panfrost_bind_vertex_elements_state
;
2442 gallium
->delete_vertex_elements_state
= panfrost_delete_vertex_elements_state
;
2444 gallium
->create_fs_state
= panfrost_create_shader_state
;
2445 gallium
->delete_fs_state
= panfrost_delete_shader_state
;
2446 gallium
->bind_fs_state
= panfrost_bind_fs_state
;
2448 gallium
->create_vs_state
= panfrost_create_shader_state
;
2449 gallium
->delete_vs_state
= panfrost_delete_shader_state
;
2450 gallium
->bind_vs_state
= panfrost_bind_vs_state
;
2452 gallium
->create_sampler_state
= panfrost_create_sampler_state
;
2453 gallium
->delete_sampler_state
= panfrost_generic_cso_delete
;
2454 gallium
->bind_sampler_states
= panfrost_bind_sampler_states
;
2456 gallium
->create_blend_state
= panfrost_create_blend_state
;
2457 gallium
->bind_blend_state
= panfrost_bind_blend_state
;
2458 gallium
->delete_blend_state
= panfrost_delete_blend_state
;
2460 gallium
->set_blend_color
= panfrost_set_blend_color
;
2462 gallium
->create_depth_stencil_alpha_state
= panfrost_create_depth_stencil_state
;
2463 gallium
->bind_depth_stencil_alpha_state
= panfrost_bind_depth_stencil_state
;
2464 gallium
->delete_depth_stencil_alpha_state
= panfrost_delete_depth_stencil_state
;
2466 gallium
->set_sample_mask
= panfrost_set_sample_mask
;
2468 gallium
->set_clip_state
= panfrost_set_clip_state
;
2469 gallium
->set_viewport_states
= panfrost_set_viewport_states
;
2470 gallium
->set_scissor_states
= panfrost_set_scissor_states
;
2471 gallium
->set_polygon_stipple
= panfrost_set_polygon_stipple
;
2472 gallium
->set_active_query_state
= panfrost_set_active_query_state
;
2474 gallium
->create_query
= panfrost_create_query
;
2475 gallium
->destroy_query
= panfrost_destroy_query
;
2476 gallium
->begin_query
= panfrost_begin_query
;
2477 gallium
->end_query
= panfrost_end_query
;
2478 gallium
->get_query_result
= panfrost_get_query_result
;
2480 panfrost_resource_context_init(gallium
);
2482 pscreen
->driver
->init_context(ctx
);
2484 panfrost_setup_hardware(ctx
);
2487 gallium
->stream_uploader
= u_upload_create_default(gallium
);
2488 gallium
->const_uploader
= gallium
->stream_uploader
;
2489 assert(gallium
->stream_uploader
);
2491 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2492 ctx
->draw_modes
= (1 << (PIPE_PRIM_POLYGON
+ 1)) - 1;
2494 ctx
->primconvert
= util_primconvert_create(gallium
, ctx
->draw_modes
);
2496 ctx
->blitter
= util_blitter_create(gallium
);
2497 assert(ctx
->blitter
);
2499 /* Prepare for render! */
2501 panfrost_job_init(ctx
);
2502 panfrost_emit_vertex_payload(ctx
);
2503 panfrost_emit_tiler_payload(ctx
);
2504 panfrost_invalidate_frame(ctx
);
2505 panfrost_viewport(ctx
, 0.0, 1.0, 0, 0, ctx
->pipe_framebuffer
.width
, ctx
->pipe_framebuffer
.height
);
2506 panfrost_default_shader_backend(ctx
);
2507 panfrost_generate_space_filler_indices();