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"
44 #include "pan_screen.h"
45 #include "pan_blending.h"
46 #include "pan_blend_shaders.h"
48 #include "pan_wallpaper.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 */
56 /* AFBC is enabled on a per-resource basis (AFBC enabling is theoretically
57 * indepdent between color buffers and depth/stencil). To enable, we allocate
58 * the AFBC metadata buffer and mark that it is enabled. We do -not- actually
59 * edit the fragment job here. This routine should be called ONCE per
60 * AFBC-compressed buffer, rather than on every frame. */
63 panfrost_enable_afbc(struct panfrost_context
*ctx
, struct panfrost_resource
*rsrc
, bool ds
)
65 if (ctx
->require_sfbd
) {
66 DBG("AFBC not supported yet on SFBD\n");
70 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
71 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
72 /* AFBC metadata is 16 bytes per tile */
73 int tile_w
= (rsrc
->base
.width0
+ (MALI_TILE_LENGTH
- 1)) >> MALI_TILE_SHIFT
;
74 int tile_h
= (rsrc
->base
.height0
+ (MALI_TILE_LENGTH
- 1)) >> MALI_TILE_SHIFT
;
75 int bytes_per_pixel
= util_format_get_blocksize(rsrc
->base
.format
);
76 int stride
= bytes_per_pixel
* ALIGN(rsrc
->base
.width0
, 16);
78 stride
*= 2; /* TODO: Should this be carried over? */
79 int main_size
= stride
* rsrc
->base
.height0
;
80 rsrc
->bo
->afbc_metadata_size
= tile_w
* tile_h
* 16;
82 /* Allocate the AFBC slab itself, large enough to hold the above */
83 screen
->driver
->allocate_slab(screen
, &rsrc
->bo
->afbc_slab
,
84 (rsrc
->bo
->afbc_metadata_size
+ main_size
+ 4095) / 4096,
87 rsrc
->bo
->layout
= PAN_AFBC
;
89 /* Compressed textured reads use a tagged pointer to the metadata */
91 rsrc
->bo
->gpu
= rsrc
->bo
->afbc_slab
.gpu
| (ds
? 0 : 1);
92 rsrc
->bo
->cpu
= rsrc
->bo
->afbc_slab
.cpu
;
93 rsrc
->bo
->gem_handle
= rsrc
->bo
->afbc_slab
.gem_handle
;
97 panfrost_enable_checksum(struct panfrost_context
*ctx
, struct panfrost_resource
*rsrc
)
99 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
100 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
101 int tile_w
= (rsrc
->base
.width0
+ (MALI_TILE_LENGTH
- 1)) >> MALI_TILE_SHIFT
;
102 int tile_h
= (rsrc
->base
.height0
+ (MALI_TILE_LENGTH
- 1)) >> MALI_TILE_SHIFT
;
104 /* 8 byte checksum per tile */
105 rsrc
->bo
->checksum_stride
= tile_w
* 8;
106 int pages
= (((rsrc
->bo
->checksum_stride
* tile_h
) + 4095) / 4096);
107 screen
->driver
->allocate_slab(screen
, &rsrc
->bo
->checksum_slab
, pages
, false, 0, 0, 0);
109 rsrc
->bo
->has_checksum
= true;
112 /* Framebuffer descriptor */
115 panfrost_set_framebuffer_resolution(struct mali_single_framebuffer
*fb
, int w
, int h
)
117 fb
->width
= MALI_POSITIVE(w
);
118 fb
->height
= MALI_POSITIVE(h
);
120 /* No idea why this is needed, but it's how resolution_check is
121 * calculated. It's not clear to us yet why the hardware wants this.
122 * The formula itself was discovered mostly by manual bruteforce and
123 * aggressive algebraic simplification. */
125 fb
->resolution_check
= ((w
+ h
) / 3) << 4;
128 struct mali_single_framebuffer
129 panfrost_emit_sfbd(struct panfrost_context
*ctx
)
131 struct mali_single_framebuffer framebuffer
= {
133 .format
= 0x30000000,
134 .clear_flags
= 0x1000,
135 .unknown_address_0
= ctx
->scratchpad
.gpu
,
136 .unknown_address_1
= ctx
->misc_0
.gpu
,
137 .unknown_address_2
= ctx
->misc_0
.gpu
+ 40960,
139 .tiler_heap_free
= ctx
->tiler_heap
.gpu
,
140 .tiler_heap_end
= ctx
->tiler_heap
.gpu
+ ctx
->tiler_heap
.size
,
143 panfrost_set_framebuffer_resolution(&framebuffer
, ctx
->pipe_framebuffer
.width
, ctx
->pipe_framebuffer
.height
);
148 struct bifrost_framebuffer
149 panfrost_emit_mfbd(struct panfrost_context
*ctx
)
151 struct bifrost_framebuffer framebuffer
= {
152 /* It is not yet clear what tiler_meta means or how it's
153 * calculated, but we can tell the lower 32-bits are a
154 * (monotonically increasing?) function of tile count and
155 * geometry complexity; I suspect it defines a memory size of
156 * some kind? for the tiler. It's really unclear at the
157 * moment... but to add to the confusion, the hardware is happy
158 * enough to accept a zero in this field, so we don't even have
159 * to worry about it right now.
161 * The byte (just after the 32-bit mark) is much more
162 * interesting. The higher nibble I've only ever seen as 0xF,
163 * but the lower one I've seen as 0x0 or 0xF, and it's not
164 * obvious what the difference is. But what -is- obvious is
165 * that when the lower nibble is zero, performance is severely
166 * degraded compared to when the lower nibble is set.
167 * Evidently, that nibble enables some sort of fast path,
168 * perhaps relating to caching or tile flush? Regardless, at
169 * this point there's no clear reason not to set it, aside from
170 * substantially increased memory requirements (of the misc_0
173 .tiler_meta
= ((uint64_t) 0xff << 32) | 0x0,
175 .width1
= MALI_POSITIVE(ctx
->pipe_framebuffer
.width
),
176 .height1
= MALI_POSITIVE(ctx
->pipe_framebuffer
.height
),
177 .width2
= MALI_POSITIVE(ctx
->pipe_framebuffer
.width
),
178 .height2
= MALI_POSITIVE(ctx
->pipe_framebuffer
.height
),
183 .rt_count_1
= MALI_POSITIVE(1),
188 /* Corresponds to unknown_address_X of SFBD */
189 .scratchpad
= ctx
->scratchpad
.gpu
,
190 .tiler_scratch_start
= ctx
->misc_0
.gpu
,
192 /* The constant added here is, like the lower word of
193 * tiler_meta, (loosely) another product of framebuffer size
194 * and geometry complexity. It must be sufficiently large for
195 * the tiler_meta fast path to work; if it's too small, there
196 * will be DATA_INVALID_FAULTs. Conversely, it must be less
197 * than the total size of misc_0, or else there's no room. It's
198 * possible this constant configures a partition between two
199 * parts of misc_0? We haven't investigated the functionality,
200 * as these buffers are internally used by the hardware
201 * (presumably by the tiler) but not seemingly touched by the driver
204 .tiler_scratch_middle
= ctx
->misc_0
.gpu
+ 0xf0000,
206 .tiler_heap_start
= ctx
->tiler_heap
.gpu
,
207 .tiler_heap_end
= ctx
->tiler_heap
.gpu
+ ctx
->tiler_heap
.size
,
213 /* Are we currently rendering to the screen (rather than an FBO)? */
216 panfrost_is_scanout(struct panfrost_context
*ctx
)
218 /* If there is no color buffer, it's an FBO */
219 if (!ctx
->pipe_framebuffer
.nr_cbufs
)
222 /* If we're too early that no framebuffer was sent, it's scanout */
223 if (!ctx
->pipe_framebuffer
.cbufs
[0])
226 return ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
227 ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
228 ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;
231 /* Maps float 0.0-1.0 to int 0x00-0xFF */
233 normalised_float_to_u8(float f
)
235 return (uint8_t) (int) (f
* 255.0f
);
240 struct pipe_context
*pipe
,
242 const union pipe_color_union
*color
,
243 double depth
, unsigned stencil
)
245 struct panfrost_context
*ctx
= pan_context(pipe
);
246 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
248 if (buffers
& PIPE_CLEAR_COLOR
) {
249 /* Alpha clear only meaningful without alpha channel, TODO less ad hoc */
250 bool has_alpha
= util_format_has_alpha(ctx
->pipe_framebuffer
.cbufs
[0]->format
);
251 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
253 uint32_t packed_color
=
254 (normalised_float_to_u8(clear_alpha
) << 24) |
255 (normalised_float_to_u8(color
->f
[2]) << 16) |
256 (normalised_float_to_u8(color
->f
[1]) << 8) |
257 (normalised_float_to_u8(color
->f
[0]) << 0);
259 job
->clear_color
= packed_color
;
263 if (buffers
& PIPE_CLEAR_DEPTH
) {
264 job
->clear_depth
= depth
;
267 if (buffers
& PIPE_CLEAR_STENCIL
) {
268 job
->clear_stencil
= stencil
;
271 job
->clear
|= buffers
;
275 panfrost_attach_vt_mfbd(struct panfrost_context
*ctx
)
277 /* MFBD needs a sequential semi-render target upload, but what exactly this is, is beyond me for now */
278 struct bifrost_render_target rts_list
[] = {
283 .framebuffer
= ctx
->misc_0
.gpu
,
288 /* Allocate memory for the three components */
289 int size
= 1024 + sizeof(ctx
->vt_framebuffer_mfbd
) + sizeof(rts_list
);
290 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
292 /* Opaque 1024-block */
293 rts_list
[0].chunknown
.pointer
= transfer
.gpu
;
295 memcpy(transfer
.cpu
+ 1024, &ctx
->vt_framebuffer_mfbd
, sizeof(ctx
->vt_framebuffer_mfbd
));
296 memcpy(transfer
.cpu
+ 1024 + sizeof(ctx
->vt_framebuffer_mfbd
), rts_list
, sizeof(rts_list
));
298 return (transfer
.gpu
+ 1024) | MALI_MFBD
;
302 panfrost_attach_vt_sfbd(struct panfrost_context
*ctx
)
304 return panfrost_upload_transient(ctx
, &ctx
->vt_framebuffer_sfbd
, sizeof(ctx
->vt_framebuffer_sfbd
)) | MALI_SFBD
;
308 panfrost_attach_vt_framebuffer(struct panfrost_context
*ctx
)
310 mali_ptr framebuffer
= ctx
->require_sfbd
?
311 panfrost_attach_vt_sfbd(ctx
) :
312 panfrost_attach_vt_mfbd(ctx
);
314 ctx
->payload_vertex
.postfix
.framebuffer
= framebuffer
;
315 ctx
->payload_tiler
.postfix
.framebuffer
= framebuffer
;
318 /* Reset per-frame context, called on context initialisation as well as after
319 * flushing a frame */
322 panfrost_invalidate_frame(struct panfrost_context
*ctx
)
324 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
;
325 DBG("Uploaded transient %d bytes\n", transient_count
);
327 /* Rotate cmdstream */
328 if ((++ctx
->cmdstream_i
) == (sizeof(ctx
->transient_pools
) / sizeof(ctx
->transient_pools
[0])))
329 ctx
->cmdstream_i
= 0;
331 if (ctx
->require_sfbd
)
332 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
334 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
336 /* Reset varyings allocated */
337 ctx
->varying_height
= 0;
339 /* The transient cmdstream is dirty every frame; the only bits worth preserving
340 * (textures, shaders, etc) are in other buffers anyways */
342 ctx
->transient_pools
[ctx
->cmdstream_i
].entry_index
= 0;
343 ctx
->transient_pools
[ctx
->cmdstream_i
].entry_offset
= 0;
345 /* Regenerate payloads */
346 panfrost_attach_vt_framebuffer(ctx
);
349 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
352 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
| PAN_DIRTY_TEXTURES
;
355 /* In practice, every field of these payloads should be configurable
356 * arbitrarily, which means these functions are basically catch-all's for
357 * as-of-yet unwavering unknowns */
360 panfrost_emit_vertex_payload(struct panfrost_context
*ctx
)
362 struct midgard_payload_vertex_tiler payload
= {
364 .workgroups_z_shift
= 32,
365 .workgroups_x_shift_2
= 0x2,
366 .workgroups_x_shift_3
= 0x5,
368 .gl_enables
= 0x4 | (ctx
->is_t6xx
? 0 : 0x2),
371 memcpy(&ctx
->payload_vertex
, &payload
, sizeof(payload
));
375 panfrost_emit_tiler_payload(struct panfrost_context
*ctx
)
377 struct midgard_payload_vertex_tiler payload
= {
379 .workgroups_z_shift
= 32,
380 .workgroups_x_shift_2
= 0x2,
381 .workgroups_x_shift_3
= 0x6,
383 .zero1
= 0xffff, /* Why is this only seen on test-quad-textured? */
387 memcpy(&ctx
->payload_tiler
, &payload
, sizeof(payload
));
391 translate_tex_wrap(enum pipe_tex_wrap w
)
394 case PIPE_TEX_WRAP_REPEAT
:
395 return MALI_WRAP_REPEAT
;
397 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
398 return MALI_WRAP_CLAMP_TO_EDGE
;
400 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
401 return MALI_WRAP_CLAMP_TO_BORDER
;
403 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
404 return MALI_WRAP_MIRRORED_REPEAT
;
413 translate_tex_filter(enum pipe_tex_filter f
)
416 case PIPE_TEX_FILTER_NEAREST
:
419 case PIPE_TEX_FILTER_LINEAR
:
429 translate_mip_filter(enum pipe_tex_mipfilter f
)
431 return (f
== PIPE_TEX_MIPFILTER_LINEAR
) ? MALI_MIP_LINEAR
: 0;
435 panfrost_translate_compare_func(enum pipe_compare_func in
)
438 case PIPE_FUNC_NEVER
:
439 return MALI_FUNC_NEVER
;
442 return MALI_FUNC_LESS
;
444 case PIPE_FUNC_EQUAL
:
445 return MALI_FUNC_EQUAL
;
447 case PIPE_FUNC_LEQUAL
:
448 return MALI_FUNC_LEQUAL
;
450 case PIPE_FUNC_GREATER
:
451 return MALI_FUNC_GREATER
;
453 case PIPE_FUNC_NOTEQUAL
:
454 return MALI_FUNC_NOTEQUAL
;
456 case PIPE_FUNC_GEQUAL
:
457 return MALI_FUNC_GEQUAL
;
459 case PIPE_FUNC_ALWAYS
:
460 return MALI_FUNC_ALWAYS
;
464 return 0; /* Unreachable */
468 panfrost_translate_alt_compare_func(enum pipe_compare_func in
)
471 case PIPE_FUNC_NEVER
:
472 return MALI_ALT_FUNC_NEVER
;
475 return MALI_ALT_FUNC_LESS
;
477 case PIPE_FUNC_EQUAL
:
478 return MALI_ALT_FUNC_EQUAL
;
480 case PIPE_FUNC_LEQUAL
:
481 return MALI_ALT_FUNC_LEQUAL
;
483 case PIPE_FUNC_GREATER
:
484 return MALI_ALT_FUNC_GREATER
;
486 case PIPE_FUNC_NOTEQUAL
:
487 return MALI_ALT_FUNC_NOTEQUAL
;
489 case PIPE_FUNC_GEQUAL
:
490 return MALI_ALT_FUNC_GEQUAL
;
492 case PIPE_FUNC_ALWAYS
:
493 return MALI_ALT_FUNC_ALWAYS
;
497 return 0; /* Unreachable */
501 panfrost_translate_stencil_op(enum pipe_stencil_op in
)
504 case PIPE_STENCIL_OP_KEEP
:
505 return MALI_STENCIL_KEEP
;
507 case PIPE_STENCIL_OP_ZERO
:
508 return MALI_STENCIL_ZERO
;
510 case PIPE_STENCIL_OP_REPLACE
:
511 return MALI_STENCIL_REPLACE
;
513 case PIPE_STENCIL_OP_INCR
:
514 return MALI_STENCIL_INCR
;
516 case PIPE_STENCIL_OP_DECR
:
517 return MALI_STENCIL_DECR
;
519 case PIPE_STENCIL_OP_INCR_WRAP
:
520 return MALI_STENCIL_INCR_WRAP
;
522 case PIPE_STENCIL_OP_DECR_WRAP
:
523 return MALI_STENCIL_DECR_WRAP
;
525 case PIPE_STENCIL_OP_INVERT
:
526 return MALI_STENCIL_INVERT
;
530 return 0; /* Unreachable */
534 panfrost_make_stencil_state(const struct pipe_stencil_state
*in
, struct mali_stencil_test
*out
)
536 out
->ref
= 0; /* Gallium gets it from elsewhere */
538 out
->mask
= in
->valuemask
;
539 out
->func
= panfrost_translate_compare_func(in
->func
);
540 out
->sfail
= panfrost_translate_stencil_op(in
->fail_op
);
541 out
->dpfail
= panfrost_translate_stencil_op(in
->zfail_op
);
542 out
->dppass
= panfrost_translate_stencil_op(in
->zpass_op
);
546 panfrost_default_shader_backend(struct panfrost_context
*ctx
)
548 struct mali_shader_meta shader
= {
549 .alpha_coverage
= ~MALI_ALPHA_COVERAGE(0.000000),
551 .unknown2_3
= MALI_DEPTH_FUNC(MALI_FUNC_ALWAYS
) | 0x3010,
552 .unknown2_4
= MALI_NO_MSAA
| 0x4e0,
556 shader
.unknown2_4
|= 0x10;
559 struct pipe_stencil_state default_stencil
= {
561 .func
= PIPE_FUNC_ALWAYS
,
562 .fail_op
= MALI_STENCIL_KEEP
,
563 .zfail_op
= MALI_STENCIL_KEEP
,
564 .zpass_op
= MALI_STENCIL_KEEP
,
569 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_front
);
570 shader
.stencil_mask_front
= default_stencil
.writemask
;
572 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_back
);
573 shader
.stencil_mask_back
= default_stencil
.writemask
;
575 if (default_stencil
.enabled
)
576 shader
.unknown2_4
|= MALI_STENCIL_TEST
;
578 memcpy(&ctx
->fragment_shader_core
, &shader
, sizeof(shader
));
581 /* Generates a vertex/tiler job. This is, in some sense, the heart of the
582 * graphics command stream. It should be called once per draw, accordding to
583 * presentations. Set is_tiler for "tiler" jobs (fragment shader jobs, but in
584 * Mali parlance, "fragment" refers to framebuffer writeout). Clear it for
587 struct panfrost_transfer
588 panfrost_vertex_tiler_job(struct panfrost_context
*ctx
, bool is_tiler
, bool is_elided_tiler
)
590 /* Each draw call corresponds to two jobs, and we want to offset to leave room for the set-value job */
591 int draw_job_index
= 1 + (2 * ctx
->draw_count
);
593 struct mali_job_descriptor_header job
= {
594 .job_type
= is_tiler
? JOB_TYPE_TILER
: JOB_TYPE_VERTEX
,
595 .job_index
= draw_job_index
+ (is_tiler
? 1 : 0),
597 .job_descriptor_size
= 1,
601 /* Only non-elided tiler jobs have dependencies which are known at this point */
603 if (is_tiler
&& !is_elided_tiler
) {
604 /* Tiler jobs depend on vertex jobs */
606 job
.job_dependency_index_1
= draw_job_index
;
608 /* Tiler jobs also depend on the previous tiler job */
611 job
.job_dependency_index_2
= draw_job_index
- 1;
614 struct midgard_payload_vertex_tiler
*payload
= is_tiler
? &ctx
->payload_tiler
: &ctx
->payload_vertex
;
616 /* There's some padding hacks on 32-bit */
623 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(job
) + sizeof(*payload
));
624 memcpy(transfer
.cpu
, &job
, sizeof(job
));
625 memcpy(transfer
.cpu
+ sizeof(job
) - offset
, payload
, sizeof(*payload
));
629 /* Generates a set value job. It's unclear what exactly this does, why it's
630 * necessary, and when to call it. */
633 panfrost_set_value_job(struct panfrost_context
*ctx
)
635 struct mali_job_descriptor_header job
= {
636 .job_type
= JOB_TYPE_SET_VALUE
,
637 .job_descriptor_size
= 1,
638 .job_index
= 1 + (2 * ctx
->draw_count
),
641 struct mali_payload_set_value payload
= {
642 .out
= ctx
->misc_0
.gpu
,
646 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(job
) + sizeof(payload
));
647 memcpy(transfer
.cpu
, &job
, sizeof(job
));
648 memcpy(transfer
.cpu
+ sizeof(job
), &payload
, sizeof(payload
));
650 ctx
->u_set_value_job
= (struct mali_job_descriptor_header
*) transfer
.cpu
;
651 ctx
->set_value_job
= transfer
.gpu
;
655 panfrost_emit_varyings(
656 struct panfrost_context
*ctx
,
657 union mali_attr
*slot
,
661 mali_ptr varying_address
= ctx
->varying_mem
.gpu
+ ctx
->varying_height
;
663 /* Fill out the descriptor */
664 slot
->elements
= varying_address
| MALI_ATTR_LINEAR
;
665 slot
->stride
= stride
;
666 slot
->size
= stride
* count
;
668 ctx
->varying_height
+= ALIGN(slot
->size
, 64);
669 assert(ctx
->varying_height
< ctx
->varying_mem
.size
);
671 return varying_address
;
675 panfrost_emit_point_coord(union mali_attr
*slot
)
677 slot
->elements
= MALI_VARYING_POINT_COORD
| MALI_ATTR_LINEAR
;
678 slot
->stride
= slot
->size
= 0;
682 panfrost_emit_varying_descriptor(
683 struct panfrost_context
*ctx
,
684 unsigned invocation_count
)
686 /* Load the shaders */
688 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
689 struct panfrost_shader_state
*fs
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
691 /* Allocate the varying descriptor */
693 size_t vs_size
= sizeof(struct mali_attr_meta
) * vs
->tripipe
->varying_count
;
694 size_t fs_size
= sizeof(struct mali_attr_meta
) * fs
->tripipe
->varying_count
;
696 struct panfrost_transfer trans
= panfrost_allocate_transient(ctx
,
699 memcpy(trans
.cpu
, vs
->varyings
, vs_size
);
700 memcpy(trans
.cpu
+ vs_size
, fs
->varyings
, fs_size
);
702 ctx
->payload_vertex
.postfix
.varying_meta
= trans
.gpu
;
703 ctx
->payload_tiler
.postfix
.varying_meta
= trans
.gpu
+ vs_size
;
705 /* Buffer indices must be in this order per our convention */
706 union mali_attr varyings
[PIPE_MAX_ATTRIBS
];
709 /* General varyings -- use the VS's, since those are more likely to be
710 * accurate on desktop */
712 panfrost_emit_varyings(ctx
, &varyings
[idx
++],
713 vs
->general_varying_stride
, invocation_count
);
715 /* fp32 vec4 gl_Position */
716 ctx
->payload_tiler
.postfix
.position_varying
=
717 panfrost_emit_varyings(ctx
, &varyings
[idx
++],
718 sizeof(float) * 4, invocation_count
);
721 if (vs
->writes_point_size
|| fs
->reads_point_coord
) {
722 /* fp16 vec1 gl_PointSize */
723 ctx
->payload_tiler
.primitive_size
.pointer
=
724 panfrost_emit_varyings(ctx
, &varyings
[idx
++],
725 2, invocation_count
);
728 if (fs
->reads_point_coord
) {
729 /* Special descriptor */
730 panfrost_emit_point_coord(&varyings
[idx
++]);
733 mali_ptr varyings_p
= panfrost_upload_transient(ctx
, &varyings
, idx
* sizeof(union mali_attr
));
734 ctx
->payload_vertex
.postfix
.varyings
= varyings_p
;
735 ctx
->payload_tiler
.postfix
.varyings
= varyings_p
;
738 /* Emits attributes and varying descriptors, which should be called every draw,
739 * excepting some obscure circumstances */
742 panfrost_emit_vertex_data(struct panfrost_context
*ctx
, struct panfrost_job
*job
)
744 /* Staged mali_attr, and index into them. i =/= k, depending on the
745 * vertex buffer mask */
746 union mali_attr attrs
[PIPE_MAX_ATTRIBS
];
749 unsigned invocation_count
= MALI_NEGATIVE(ctx
->payload_tiler
.prefix
.invocation_count
);
751 for (int i
= 0; i
< ARRAY_SIZE(ctx
->vertex_buffers
); ++i
) {
752 if (!(ctx
->vb_mask
& (1 << i
))) continue;
754 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[i
];
755 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
.resource
);
757 /* Let's figure out the layout of the attributes in memory so
758 * we can be smart about size computation. The idea is to
759 * figure out the maximum src_offset, which tells us the latest
760 * spot a vertex could start. Meanwhile, we figure out the size
761 * of the attribute memory (assuming interleaved
762 * representation) and tack on the max src_offset for a
763 * reasonably good upper bound on the size.
765 * Proving correctness is left as an exercise to the reader.
768 unsigned max_src_offset
= 0;
770 for (unsigned j
= 0; j
< ctx
->vertex
->num_elements
; ++j
) {
771 if (ctx
->vertex
->pipe
[j
].vertex_buffer_index
!= i
) continue;
772 max_src_offset
= MAX2(max_src_offset
, ctx
->vertex
->pipe
[j
].src_offset
);
775 /* Offset vertex count by draw_start to make sure we upload enough */
776 attrs
[k
].stride
= buf
->stride
;
777 attrs
[k
].size
= buf
->stride
* (ctx
->payload_vertex
.draw_start
+ invocation_count
) + max_src_offset
;
779 /* Vertex elements are -already- GPU-visible, at
780 * rsrc->gpu. However, attribute buffers must be 64 aligned. If
781 * it is not, for now we have to duplicate the buffer. */
783 mali_ptr effective_address
= rsrc
? (rsrc
->bo
->gpu
+ buf
->buffer_offset
) : 0;
785 if (effective_address
& 63) {
786 attrs
[k
].elements
= panfrost_upload_transient(ctx
, rsrc
->bo
->cpu
+ buf
->buffer_offset
, attrs
[i
].size
) | MALI_ATTR_LINEAR
;
787 } else if (effective_address
) {
788 panfrost_job_add_bo(job
, rsrc
->bo
);
789 attrs
[k
].elements
= effective_address
| MALI_ATTR_LINEAR
;
797 ctx
->payload_vertex
.postfix
.attributes
= panfrost_upload_transient(ctx
, attrs
, k
* sizeof(union mali_attr
));
799 panfrost_emit_varying_descriptor(ctx
, invocation_count
);
803 panfrost_writes_point_size(struct panfrost_context
*ctx
)
806 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
808 return vs
->writes_point_size
&& ctx
->payload_tiler
.prefix
.draw_mode
== MALI_POINTS
;
811 /* Go through dirty flags and actualise them in the cmdstream. */
814 panfrost_emit_for_draw(struct panfrost_context
*ctx
, bool with_vertex_data
)
816 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
818 if (with_vertex_data
) {
819 panfrost_emit_vertex_data(ctx
, job
);
822 bool msaa
= ctx
->rasterizer
->base
.multisample
;
824 if (ctx
->dirty
& PAN_DIRTY_RASTERIZER
) {
825 ctx
->payload_tiler
.gl_enables
= ctx
->rasterizer
->tiler_gl_enables
;
827 /* TODO: Sample size */
828 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_HAS_MSAA
, msaa
);
829 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_MSAA
, !msaa
);
832 /* Enable job requirements at draw-time */
835 job
->requirements
|= PAN_REQ_MSAA
;
837 if (ctx
->depth_stencil
->depth
.writemask
)
838 job
->requirements
|= PAN_REQ_DEPTH_WRITE
;
840 if (ctx
->occlusion_query
) {
841 ctx
->payload_tiler
.gl_enables
|= MALI_OCCLUSION_QUERY
| MALI_OCCLUSION_PRECISE
;
842 ctx
->payload_tiler
.postfix
.occlusion_counter
= ctx
->occlusion_query
->transfer
.gpu
;
845 if (ctx
->dirty
& PAN_DIRTY_VS
) {
848 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
850 /* Late shader descriptor assignments */
852 vs
->tripipe
->texture_count
= ctx
->sampler_view_count
[PIPE_SHADER_VERTEX
];
853 vs
->tripipe
->sampler_count
= ctx
->sampler_count
[PIPE_SHADER_VERTEX
];
856 vs
->tripipe
->midgard1
.unknown1
= 0x2201;
858 ctx
->payload_vertex
.postfix
._shader_upper
= vs
->tripipe_gpu
>> 4;
861 if (ctx
->dirty
& (PAN_DIRTY_RASTERIZER
| PAN_DIRTY_VS
)) {
862 /* Check if we need to link the gl_PointSize varying */
863 if (!panfrost_writes_point_size(ctx
)) {
864 /* If the size is constant, write it out. Otherwise,
865 * don't touch primitive_size (since we would clobber
866 * the pointer there) */
868 ctx
->payload_tiler
.primitive_size
.constant
= ctx
->rasterizer
->base
.line_width
;
872 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
874 ctx
->dirty
|= PAN_DIRTY_FS
;
876 if (ctx
->dirty
& PAN_DIRTY_FS
) {
878 struct panfrost_shader_state
*variant
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
880 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
883 COPY(attribute_count
);
885 COPY(midgard1
.uniform_count
);
886 COPY(midgard1
.work_count
);
887 COPY(midgard1
.unknown2
);
890 /* If there is a blend shader, work registers are shared */
892 if (ctx
->blend
->has_blend_shader
)
893 ctx
->fragment_shader_core
.midgard1
.work_count
= /*MAX2(ctx->fragment_shader_core.midgard1.work_count, ctx->blend->blend_work_count)*/16;
895 /* Set late due to depending on render state */
896 /* The one at the end seems to mean "1 UBO" */
897 ctx
->fragment_shader_core
.midgard1
.unknown1
= MALI_NO_ALPHA_TO_COVERAGE
| 0x200 | 0x2201;
899 /* Assign texture/sample count right before upload */
900 ctx
->fragment_shader_core
.texture_count
= ctx
->sampler_view_count
[PIPE_SHADER_FRAGMENT
];
901 ctx
->fragment_shader_core
.sampler_count
= ctx
->sampler_count
[PIPE_SHADER_FRAGMENT
];
903 /* Assign the stencil refs late */
904 ctx
->fragment_shader_core
.stencil_front
.ref
= ctx
->stencil_ref
.ref_value
[0];
905 ctx
->fragment_shader_core
.stencil_back
.ref
= ctx
->stencil_ref
.ref_value
[1];
907 /* CAN_DISCARD should be set if the fragment shader possibly
908 * contains a 'discard' instruction. It is likely this is
909 * related to optimizations related to forward-pixel kill, as
910 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
911 * thing?" by Peter Harris
914 if (variant
->can_discard
) {
915 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
916 ctx
->fragment_shader_core
.midgard1
.unknown1
&= ~MALI_NO_ALPHA_TO_COVERAGE
;
917 ctx
->fragment_shader_core
.midgard1
.unknown1
|= 0x4000;
918 ctx
->fragment_shader_core
.midgard1
.unknown1
= 0x4200;
921 /* Check if we're using the default blend descriptor (fast path) */
924 !ctx
->blend
->has_blend_shader
&&
925 (ctx
->blend
->equation
.rgb_mode
== 0x122) &&
926 (ctx
->blend
->equation
.alpha_mode
== 0x122) &&
927 (ctx
->blend
->equation
.color_mask
== 0xf);
929 if (ctx
->require_sfbd
) {
930 /* When only a single render target platform is used, the blend
931 * information is inside the shader meta itself. We
932 * additionally need to signal CAN_DISCARD for nontrivial blend
933 * modes (so we're able to read back the destination buffer) */
935 if (ctx
->blend
->has_blend_shader
) {
936 ctx
->fragment_shader_core
.blend_shader
= ctx
->blend
->blend_shader
;
938 memcpy(&ctx
->fragment_shader_core
.blend_equation
, &ctx
->blend
->equation
, sizeof(ctx
->blend
->equation
));
942 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
946 size_t size
= sizeof(struct mali_shader_meta
) + sizeof(struct mali_blend_meta
);
947 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
948 memcpy(transfer
.cpu
, &ctx
->fragment_shader_core
, sizeof(struct mali_shader_meta
));
950 ctx
->payload_tiler
.postfix
._shader_upper
= (transfer
.gpu
) >> 4;
952 if (!ctx
->require_sfbd
) {
953 /* Additional blend descriptor tacked on for jobs using MFBD */
955 unsigned blend_count
= 0;
957 if (ctx
->blend
->has_blend_shader
) {
958 /* For a blend shader, the bottom nibble corresponds to
959 * the number of work registers used, which signals the
960 * -existence- of a blend shader */
962 assert(ctx
->blend
->blend_work_count
>= 2);
963 blend_count
|= MIN2(ctx
->blend
->blend_work_count
, 3);
965 /* Otherwise, the bottom bit simply specifies if
966 * blending (anything other than REPLACE) is enabled */
973 /* Second blend equation is always a simple replace */
975 uint64_t replace_magic
= 0xf0122122;
976 struct mali_blend_equation replace_mode
;
977 memcpy(&replace_mode
, &replace_magic
, sizeof(replace_mode
));
979 struct mali_blend_meta blend_meta
[] = {
981 .unk1
= 0x200 | blend_count
,
982 .blend_equation_1
= ctx
->blend
->equation
,
983 .blend_equation_2
= replace_mode
987 if (ctx
->blend
->has_blend_shader
)
988 memcpy(&blend_meta
[0].blend_equation_1
, &ctx
->blend
->blend_shader
, sizeof(ctx
->blend
->blend_shader
));
990 memcpy(transfer
.cpu
+ sizeof(struct mali_shader_meta
), blend_meta
, sizeof(blend_meta
));
994 if (ctx
->dirty
& PAN_DIRTY_VERTEX
) {
995 ctx
->payload_vertex
.postfix
.attribute_meta
= ctx
->vertex
->descriptor_ptr
;
998 if (ctx
->dirty
& PAN_DIRTY_SAMPLERS
) {
999 /* Upload samplers back to back, no padding */
1001 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
1002 if (!ctx
->sampler_count
[t
]) continue;
1004 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(struct mali_sampler_descriptor
) * ctx
->sampler_count
[t
]);
1005 struct mali_sampler_descriptor
*desc
= (struct mali_sampler_descriptor
*) transfer
.cpu
;
1007 for (int i
= 0; i
< ctx
->sampler_count
[t
]; ++i
) {
1008 desc
[i
] = ctx
->samplers
[t
][i
]->hw
;
1011 if (t
== PIPE_SHADER_FRAGMENT
)
1012 ctx
->payload_tiler
.postfix
.sampler_descriptor
= transfer
.gpu
;
1013 else if (t
== PIPE_SHADER_VERTEX
)
1014 ctx
->payload_vertex
.postfix
.sampler_descriptor
= transfer
.gpu
;
1020 if (ctx
->dirty
& PAN_DIRTY_TEXTURES
) {
1021 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
1023 if (!ctx
->sampler_view_count
[t
]) continue;
1025 uint64_t trampolines
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
1027 for (int i
= 0; i
< ctx
->sampler_view_count
[t
]; ++i
) {
1028 if (!ctx
->sampler_views
[t
][i
])
1031 struct pipe_resource
*tex_rsrc
= ctx
->sampler_views
[t
][i
]->base
.texture
;
1032 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) tex_rsrc
;
1034 /* Inject the addresses in, interleaving cube
1035 * faces and mip levels appropriately. */
1037 for (int l
= 0; l
<= tex_rsrc
->last_level
; ++l
) {
1038 for (int f
= 0; f
< tex_rsrc
->array_size
; ++f
) {
1039 unsigned idx
= (l
* tex_rsrc
->array_size
) + f
;
1041 ctx
->sampler_views
[t
][i
]->hw
.swizzled_bitmaps
[idx
] =
1043 rsrc
->bo
->slices
[l
].offset
+
1044 f
* rsrc
->bo
->cubemap_stride
;
1048 trampolines
[i
] = panfrost_upload_transient(ctx
, &ctx
->sampler_views
[t
][i
]->hw
, sizeof(struct mali_texture_descriptor
));
1051 mali_ptr trampoline
= panfrost_upload_transient(ctx
, trampolines
, sizeof(uint64_t) * ctx
->sampler_view_count
[t
]);
1053 if (t
== PIPE_SHADER_FRAGMENT
)
1054 ctx
->payload_tiler
.postfix
.texture_trampoline
= trampoline
;
1055 else if (t
== PIPE_SHADER_VERTEX
)
1056 ctx
->payload_vertex
.postfix
.texture_trampoline
= trampoline
;
1062 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
1064 /* For flipped-Y buffers (signaled by negative scale), the translate is
1065 * flipped as well */
1067 bool invert_y
= vp
->scale
[1] < 0.0;
1068 float translate_y
= vp
->translate
[1];
1071 translate_y
= ctx
->pipe_framebuffer
.height
- translate_y
;
1073 for (int i
= 0; i
<= PIPE_SHADER_FRAGMENT
; ++i
) {
1074 struct panfrost_constant_buffer
*buf
= &ctx
->constant_buffer
[i
];
1076 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
1077 struct panfrost_shader_state
*fs
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
1078 struct panfrost_shader_state
*ss
= (i
== PIPE_SHADER_FRAGMENT
) ? fs
: vs
;
1080 /* Allocate room for the sysval and the uniforms */
1081 size_t sys_size
= sizeof(float) * 4 * ss
->sysval_count
;
1082 size_t size
= sys_size
+ buf
->size
;
1083 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
1085 /* Upload sysvals requested by the shader */
1086 float *uniforms
= (float *) transfer
.cpu
;
1087 for (unsigned i
= 0; i
< ss
->sysval_count
; ++i
) {
1088 int sysval
= ss
->sysval
[i
];
1090 if (sysval
== PAN_SYSVAL_VIEWPORT_SCALE
) {
1091 uniforms
[4*i
+ 0] = vp
->scale
[0];
1092 uniforms
[4*i
+ 1] = fabsf(vp
->scale
[1]);
1093 uniforms
[4*i
+ 2] = vp
->scale
[2];
1094 } else if (sysval
== PAN_SYSVAL_VIEWPORT_OFFSET
) {
1095 uniforms
[4*i
+ 0] = vp
->translate
[0];
1096 uniforms
[4*i
+ 1] = translate_y
;
1097 uniforms
[4*i
+ 2] = vp
->translate
[2];
1103 /* Upload uniforms */
1104 memcpy(transfer
.cpu
+ sys_size
, buf
->buffer
, buf
->size
);
1106 int uniform_count
= 0;
1108 struct mali_vertex_tiler_postfix
*postfix
;
1111 case PIPE_SHADER_VERTEX
:
1112 uniform_count
= ctx
->vs
->variants
[ctx
->vs
->active_variant
].uniform_count
;
1113 postfix
= &ctx
->payload_vertex
.postfix
;
1116 case PIPE_SHADER_FRAGMENT
:
1117 uniform_count
= ctx
->fs
->variants
[ctx
->fs
->active_variant
].uniform_count
;
1118 postfix
= &ctx
->payload_tiler
.postfix
;
1122 DBG("Unknown shader stage %d in uniform upload\n", i
);
1126 /* Also attach the same buffer as a UBO for extended access */
1128 struct mali_uniform_buffer_meta uniform_buffers
[] = {
1130 .size
= MALI_POSITIVE((2 + uniform_count
)),
1131 .ptr
= transfer
.gpu
>> 2,
1135 mali_ptr ubufs
= panfrost_upload_transient(ctx
, uniform_buffers
, sizeof(uniform_buffers
));
1136 postfix
->uniforms
= transfer
.gpu
;
1137 postfix
->uniform_buffers
= ubufs
;
1142 /* TODO: Upload the viewport somewhere more appropriate */
1144 /* Clip bounds are encoded as floats. The viewport itself is encoded as
1145 * (somewhat) asymmetric ints. */
1146 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
1148 struct mali_viewport view
= {
1149 /* By default, do no viewport clipping, i.e. clip to (-inf,
1150 * inf) in each direction. Clipping to the viewport in theory
1151 * should work, but in practice causes issues when we're not
1152 * explicitly trying to scissor */
1163 /* Always scissor to the viewport by default. */
1164 view
.viewport0
[0] = (int) (vp
->translate
[0] - vp
->scale
[0]);
1165 view
.viewport1
[0] = MALI_POSITIVE((int) (vp
->translate
[0] + vp
->scale
[0]));
1167 view
.viewport0
[1] = (int) (translate_y
- fabs(vp
->scale
[1]));
1168 view
.viewport1
[1] = MALI_POSITIVE((int) (translate_y
+ fabs(vp
->scale
[1])));
1170 if (ss
&& ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
) {
1171 /* Invert scissor if needed */
1172 unsigned miny
= invert_y
?
1173 ctx
->pipe_framebuffer
.height
- ss
->maxy
: ss
->miny
;
1175 unsigned maxy
= invert_y
?
1176 ctx
->pipe_framebuffer
.height
- ss
->miny
: ss
->maxy
;
1178 /* Set the actual scissor */
1179 view
.viewport0
[0] = ss
->minx
;
1180 view
.viewport0
[1] = miny
;
1181 view
.viewport1
[0] = MALI_POSITIVE(ss
->maxx
);
1182 view
.viewport1
[1] = MALI_POSITIVE(maxy
);
1185 ctx
->payload_tiler
.postfix
.viewport
=
1186 panfrost_upload_transient(ctx
,
1188 sizeof(struct mali_viewport
));
1193 /* Corresponds to exactly one draw, but does not submit anything */
1196 panfrost_queue_draw(struct panfrost_context
*ctx
)
1198 /* TODO: Expand the array? */
1199 if (ctx
->draw_count
>= MAX_DRAW_CALLS
) {
1200 DBG("Job buffer overflow, ignoring draw\n");
1204 /* Handle dirty flags now */
1205 panfrost_emit_for_draw(ctx
, true);
1207 struct panfrost_transfer vertex
= panfrost_vertex_tiler_job(ctx
, false, false);
1208 struct panfrost_transfer tiler
= panfrost_vertex_tiler_job(ctx
, true, false);
1210 ctx
->u_vertex_jobs
[ctx
->vertex_job_count
] = (struct mali_job_descriptor_header
*) vertex
.cpu
;
1211 ctx
->vertex_jobs
[ctx
->vertex_job_count
++] = vertex
.gpu
;
1213 ctx
->u_tiler_jobs
[ctx
->tiler_job_count
] = (struct mali_job_descriptor_header
*) tiler
.cpu
;
1214 ctx
->tiler_jobs
[ctx
->tiler_job_count
++] = tiler
.gpu
;
1219 /* At the end of the frame, the vertex and tiler jobs are linked together and
1220 * then the fragment job is plonked at the end. Set value job is first for
1221 * unknown reasons. */
1224 panfrost_link_job_pair(struct mali_job_descriptor_header
*first
, mali_ptr next
)
1226 if (first
->job_descriptor_size
)
1227 first
->next_job_64
= (u64
) (uintptr_t) next
;
1229 first
->next_job_32
= (u32
) (uintptr_t) next
;
1233 panfrost_link_jobs(struct panfrost_context
*ctx
)
1235 if (ctx
->draw_count
) {
1236 /* Generate the set_value_job */
1237 panfrost_set_value_job(ctx
);
1239 /* Have the first vertex job depend on the set value job */
1240 ctx
->u_vertex_jobs
[0]->job_dependency_index_1
= ctx
->u_set_value_job
->job_index
;
1243 panfrost_link_job_pair(ctx
->u_set_value_job
, ctx
->vertex_jobs
[0]);
1246 /* V -> V/T ; T -> T/null */
1247 for (int i
= 0; i
< ctx
->vertex_job_count
; ++i
) {
1248 bool isLast
= (i
+ 1) == ctx
->vertex_job_count
;
1250 panfrost_link_job_pair(ctx
->u_vertex_jobs
[i
], isLast
? ctx
->tiler_jobs
[0] : ctx
->vertex_jobs
[i
+ 1]);
1254 for (int i
= 0; i
< ctx
->tiler_job_count
; ++i
) {
1255 bool isLast
= (i
+ 1) == ctx
->tiler_job_count
;
1256 panfrost_link_job_pair(ctx
->u_tiler_jobs
[i
], isLast
? 0 : ctx
->tiler_jobs
[i
+ 1]);
1260 /* The entire frame is in memory -- send it off to the kernel! */
1263 panfrost_submit_frame(struct panfrost_context
*ctx
, bool flush_immediate
,
1264 struct pipe_fence_handle
**fence
,
1265 struct panfrost_job
*job
)
1267 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
1268 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
1270 /* Edge case if screen is cleared and nothing else */
1271 bool has_draws
= ctx
->draw_count
> 0;
1273 /* Workaround a bizarre lockup (a hardware errata?) */
1275 flush_immediate
= true;
1277 /* A number of jobs are batched -- this must be linked and cleared */
1278 panfrost_link_jobs(ctx
);
1280 ctx
->draw_count
= 0;
1281 ctx
->vertex_job_count
= 0;
1282 ctx
->tiler_job_count
= 0;
1286 bool is_scanout
= panfrost_is_scanout(ctx
);
1287 screen
->driver
->submit_vs_fs_job(ctx
, has_draws
, is_scanout
);
1289 /* If visual, we can stall a frame */
1291 if (!flush_immediate
)
1292 screen
->driver
->force_flush_fragment(ctx
, fence
);
1294 screen
->last_fragment_flushed
= false;
1295 screen
->last_job
= job
;
1297 /* If readback, flush now (hurts the pipelined performance) */
1298 if (flush_immediate
)
1299 screen
->driver
->force_flush_fragment(ctx
, fence
);
1301 if (screen
->driver
->dump_counters
&& pan_counters_base
) {
1302 screen
->driver
->dump_counters(screen
);
1305 snprintf(filename
, sizeof(filename
), "%s/frame%d.mdgprf", pan_counters_base
, ++performance_counter_number
);
1306 FILE *fp
= fopen(filename
, "wb");
1307 fwrite(screen
->perf_counters
.cpu
, 4096, sizeof(uint32_t), fp
);
1316 struct pipe_context
*pipe
,
1317 struct pipe_fence_handle
**fence
,
1320 struct panfrost_context
*ctx
= pan_context(pipe
);
1321 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
1323 /* Nothing to do! */
1324 if (!ctx
->draw_count
&& !job
->clear
) return;
1326 /* Whether to stall the pipeline for immediately correct results */
1327 bool flush_immediate
= flags
& PIPE_FLUSH_END_OF_FRAME
;
1329 /* Submit the frame itself */
1330 panfrost_submit_frame(ctx
, flush_immediate
, fence
, job
);
1332 /* Prepare for the next frame */
1333 panfrost_invalidate_frame(ctx
);
1336 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1339 g2m_draw_mode(enum pipe_prim_type mode
)
1342 DEFINE_CASE(POINTS
);
1344 DEFINE_CASE(LINE_LOOP
);
1345 DEFINE_CASE(LINE_STRIP
);
1346 DEFINE_CASE(TRIANGLES
);
1347 DEFINE_CASE(TRIANGLE_STRIP
);
1348 DEFINE_CASE(TRIANGLE_FAN
);
1350 DEFINE_CASE(QUAD_STRIP
);
1351 DEFINE_CASE(POLYGON
);
1354 DBG("Illegal draw mode %d\n", mode
);
1356 return MALI_LINE_LOOP
;
1363 panfrost_translate_index_size(unsigned size
)
1367 return MALI_DRAW_INDEXED_UINT8
;
1370 return MALI_DRAW_INDEXED_UINT16
;
1373 return MALI_DRAW_INDEXED_UINT32
;
1376 DBG("Unknown index size %d\n", size
);
1382 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1383 * good for the duration of the draw (transient), could last longer */
1386 panfrost_get_index_buffer_mapped(struct panfrost_context
*ctx
, const struct pipe_draw_info
*info
)
1388 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (info
->index
.resource
);
1390 off_t offset
= info
->start
* info
->index_size
;
1392 if (!info
->has_user_indices
) {
1393 /* Only resources can be directly mapped */
1394 return rsrc
->bo
->gpu
+ offset
;
1396 /* Otherwise, we need to upload to transient memory */
1397 const uint8_t *ibuf8
= (const uint8_t *) info
->index
.user
;
1398 return panfrost_upload_transient(ctx
, ibuf8
+ offset
, info
->count
* info
->index_size
);
1404 struct pipe_context
*pipe
,
1405 const struct pipe_draw_info
*info
)
1407 struct panfrost_context
*ctx
= pan_context(pipe
);
1409 ctx
->payload_vertex
.draw_start
= info
->start
;
1410 ctx
->payload_tiler
.draw_start
= info
->start
;
1412 int mode
= info
->mode
;
1414 /* Fallback for unsupported modes */
1416 if (!(ctx
->draw_modes
& (1 << mode
))) {
1417 if (mode
== PIPE_PRIM_QUADS
&& info
->count
== 4 && ctx
->rasterizer
&& !ctx
->rasterizer
->base
.flatshade
) {
1418 mode
= PIPE_PRIM_TRIANGLE_FAN
;
1420 if (info
->count
< 4) {
1421 /* Degenerate case? */
1425 util_primconvert_save_rasterizer_state(ctx
->primconvert
, &ctx
->rasterizer
->base
);
1426 util_primconvert_draw_vbo(ctx
->primconvert
, info
);
1431 /* Now that we have a guaranteed terminating path, find the job.
1432 * Assignment commented out to prevent unused warning */
1434 /* struct panfrost_job *job = */ panfrost_get_job_for_fbo(ctx
);
1436 ctx
->payload_tiler
.prefix
.draw_mode
= g2m_draw_mode(mode
);
1438 ctx
->vertex_count
= info
->count
;
1440 /* For non-indexed draws, they're the same */
1441 unsigned invocation_count
= ctx
->vertex_count
;
1443 unsigned draw_flags
= 0;
1445 /* The draw flags interpret how primitive size is interpreted */
1447 if (panfrost_writes_point_size(ctx
))
1448 draw_flags
|= MALI_DRAW_VARYING_SIZE
;
1450 /* For higher amounts of vertices (greater than what fits in a 16-bit
1451 * short), the other value is needed, otherwise there will be bizarre
1452 * rendering artefacts. It's not clear what these values mean yet. */
1454 draw_flags
|= (mode
== PIPE_PRIM_POINTS
|| ctx
->vertex_count
> 65535) ? 0x3000 : 0x18000;
1456 if (info
->index_size
) {
1457 /* Calculate the min/max index used so we can figure out how
1458 * many times to invoke the vertex shader */
1460 /* Fetch / calculate index bounds */
1461 unsigned min_index
= 0, max_index
= 0;
1463 if (info
->max_index
== ~0u) {
1464 u_vbuf_get_minmax_index(pipe
, info
, &min_index
, &max_index
);
1466 min_index
= info
->min_index
;
1467 max_index
= info
->max_index
;
1470 /* Use the corresponding values */
1471 invocation_count
= max_index
- min_index
+ 1;
1472 ctx
->payload_vertex
.draw_start
= min_index
;
1473 ctx
->payload_tiler
.draw_start
= min_index
;
1475 ctx
->payload_tiler
.prefix
.negative_start
= -min_index
;
1476 ctx
->payload_tiler
.prefix
.index_count
= MALI_POSITIVE(info
->count
);
1478 //assert(!info->restart_index); /* TODO: Research */
1479 assert(!info
->index_bias
);
1481 draw_flags
|= panfrost_translate_index_size(info
->index_size
);
1482 ctx
->payload_tiler
.prefix
.indices
= panfrost_get_index_buffer_mapped(ctx
, info
);
1484 /* Index count == vertex count, if no indexing is applied, as
1485 * if it is internally indexed in the expected order */
1487 ctx
->payload_tiler
.prefix
.negative_start
= 0;
1488 ctx
->payload_tiler
.prefix
.index_count
= MALI_POSITIVE(ctx
->vertex_count
);
1490 /* Reverse index state */
1491 ctx
->payload_tiler
.prefix
.indices
= (uintptr_t) NULL
;
1494 ctx
->payload_vertex
.prefix
.invocation_count
= MALI_POSITIVE(invocation_count
);
1495 ctx
->payload_tiler
.prefix
.invocation_count
= MALI_POSITIVE(invocation_count
);
1496 ctx
->payload_tiler
.prefix
.unknown_draw
= draw_flags
;
1498 /* Fire off the draw itself */
1499 panfrost_queue_draw(ctx
);
1505 panfrost_generic_cso_delete(struct pipe_context
*pctx
, void *hwcso
)
1511 panfrost_create_rasterizer_state(
1512 struct pipe_context
*pctx
,
1513 const struct pipe_rasterizer_state
*cso
)
1515 struct panfrost_context
*ctx
= pan_context(pctx
);
1516 struct panfrost_rasterizer
*so
= CALLOC_STRUCT(panfrost_rasterizer
);
1520 /* Bitmask, unknown meaning of the start value */
1521 so
->tiler_gl_enables
= ctx
->is_t6xx
? 0x105 : 0x7;
1523 so
->tiler_gl_enables
|= MALI_FRONT_FACE(
1524 cso
->front_ccw
? MALI_CCW
: MALI_CW
);
1526 if (cso
->cull_face
& PIPE_FACE_FRONT
)
1527 so
->tiler_gl_enables
|= MALI_CULL_FACE_FRONT
;
1529 if (cso
->cull_face
& PIPE_FACE_BACK
)
1530 so
->tiler_gl_enables
|= MALI_CULL_FACE_BACK
;
1536 panfrost_bind_rasterizer_state(
1537 struct pipe_context
*pctx
,
1540 struct panfrost_context
*ctx
= pan_context(pctx
);
1542 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1546 ctx
->rasterizer
= hwcso
;
1547 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
1551 panfrost_create_vertex_elements_state(
1552 struct pipe_context
*pctx
,
1553 unsigned num_elements
,
1554 const struct pipe_vertex_element
*elements
)
1556 struct panfrost_context
*ctx
= pan_context(pctx
);
1557 struct panfrost_vertex_state
*so
= CALLOC_STRUCT(panfrost_vertex_state
);
1559 so
->num_elements
= num_elements
;
1560 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
1562 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_attr_meta
) * num_elements
, HEAP_DESCRIPTOR
);
1563 so
->hw
= (struct mali_attr_meta
*) transfer
.cpu
;
1564 so
->descriptor_ptr
= transfer
.gpu
;
1566 /* Allocate memory for the descriptor state */
1568 for (int i
= 0; i
< num_elements
; ++i
) {
1569 so
->hw
[i
].index
= elements
[i
].vertex_buffer_index
;
1571 enum pipe_format fmt
= elements
[i
].src_format
;
1572 const struct util_format_description
*desc
= util_format_description(fmt
);
1573 so
->hw
[i
].unknown1
= 0x2;
1574 so
->hw
[i
].swizzle
= panfrost_get_default_swizzle(desc
->nr_channels
);
1576 so
->hw
[i
].format
= panfrost_find_format(desc
);
1578 /* The field itself should probably be shifted over */
1579 so
->hw
[i
].src_offset
= elements
[i
].src_offset
;
1586 panfrost_bind_vertex_elements_state(
1587 struct pipe_context
*pctx
,
1590 struct panfrost_context
*ctx
= pan_context(pctx
);
1592 ctx
->vertex
= hwcso
;
1593 ctx
->dirty
|= PAN_DIRTY_VERTEX
;
1597 panfrost_delete_vertex_elements_state(struct pipe_context
*pctx
, void *hwcso
)
1599 struct panfrost_vertex_state
*so
= (struct panfrost_vertex_state
*) hwcso
;
1600 unsigned bytes
= sizeof(struct mali_attr_meta
) * so
->num_elements
;
1601 DBG("Vertex elements delete leaks descriptor (%d bytes)\n", bytes
);
1606 panfrost_create_shader_state(
1607 struct pipe_context
*pctx
,
1608 const struct pipe_shader_state
*cso
)
1610 struct panfrost_shader_variants
*so
= CALLOC_STRUCT(panfrost_shader_variants
);
1613 /* Token deep copy to prevent memory corruption */
1615 if (cso
->type
== PIPE_SHADER_IR_TGSI
)
1616 so
->base
.tokens
= tgsi_dup_tokens(so
->base
.tokens
);
1622 panfrost_delete_shader_state(
1623 struct pipe_context
*pctx
,
1626 struct panfrost_shader_variants
*cso
= (struct panfrost_shader_variants
*) so
;
1628 if (cso
->base
.type
== PIPE_SHADER_IR_TGSI
) {
1629 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1632 unsigned leak
= cso
->variant_count
* sizeof(struct mali_shader_meta
);
1633 DBG("Deleting shader state leaks descriptors (%d bytes), and shader bytecode\n", leak
);
1639 panfrost_create_sampler_state(
1640 struct pipe_context
*pctx
,
1641 const struct pipe_sampler_state
*cso
)
1643 struct panfrost_sampler_state
*so
= CALLOC_STRUCT(panfrost_sampler_state
);
1646 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1648 struct mali_sampler_descriptor sampler_descriptor
= {
1649 .filter_mode
= MALI_TEX_MIN(translate_tex_filter(cso
->min_img_filter
))
1650 | MALI_TEX_MAG(translate_tex_filter(cso
->mag_img_filter
))
1651 | translate_mip_filter(cso
->min_mip_filter
)
1654 .wrap_s
= translate_tex_wrap(cso
->wrap_s
),
1655 .wrap_t
= translate_tex_wrap(cso
->wrap_t
),
1656 .wrap_r
= translate_tex_wrap(cso
->wrap_r
),
1657 .compare_func
= panfrost_translate_alt_compare_func(cso
->compare_func
),
1659 cso
->border_color
.f
[0],
1660 cso
->border_color
.f
[1],
1661 cso
->border_color
.f
[2],
1662 cso
->border_color
.f
[3]
1664 .min_lod
= FIXED_16(cso
->min_lod
),
1665 .max_lod
= FIXED_16(cso
->max_lod
),
1669 so
->hw
= sampler_descriptor
;
1675 panfrost_bind_sampler_states(
1676 struct pipe_context
*pctx
,
1677 enum pipe_shader_type shader
,
1678 unsigned start_slot
, unsigned num_sampler
,
1681 assert(start_slot
== 0);
1683 struct panfrost_context
*ctx
= pan_context(pctx
);
1685 /* XXX: Should upload, not just copy? */
1686 ctx
->sampler_count
[shader
] = num_sampler
;
1687 memcpy(ctx
->samplers
[shader
], sampler
, num_sampler
* sizeof (void *));
1689 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
;
1693 panfrost_variant_matches(struct panfrost_context
*ctx
, struct panfrost_shader_state
*variant
)
1695 struct pipe_alpha_state
*alpha
= &ctx
->depth_stencil
->alpha
;
1697 if (alpha
->enabled
|| variant
->alpha_state
.enabled
) {
1698 /* Make sure enable state is at least the same */
1699 if (alpha
->enabled
!= variant
->alpha_state
.enabled
) {
1703 /* Check that the contents of the test are the same */
1704 bool same_func
= alpha
->func
== variant
->alpha_state
.func
;
1705 bool same_ref
= alpha
->ref_value
== variant
->alpha_state
.ref_value
;
1707 if (!(same_func
&& same_ref
)) {
1711 /* Otherwise, we're good to go */
1716 panfrost_bind_fs_state(
1717 struct pipe_context
*pctx
,
1720 struct panfrost_context
*ctx
= pan_context(pctx
);
1725 /* Match the appropriate variant */
1727 signed variant
= -1;
1729 struct panfrost_shader_variants
*variants
= (struct panfrost_shader_variants
*) hwcso
;
1731 for (unsigned i
= 0; i
< variants
->variant_count
; ++i
) {
1732 if (panfrost_variant_matches(ctx
, &variants
->variants
[i
])) {
1738 if (variant
== -1) {
1739 /* No variant matched, so create a new one */
1740 variant
= variants
->variant_count
++;
1741 assert(variants
->variant_count
< MAX_SHADER_VARIANTS
);
1743 variants
->variants
[variant
].base
= hwcso
;
1744 variants
->variants
[variant
].alpha_state
= ctx
->depth_stencil
->alpha
;
1746 /* Allocate the mapped descriptor ahead-of-time. TODO: Use for FS as well as VS */
1747 struct panfrost_context
*ctx
= pan_context(pctx
);
1748 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_shader_meta
), HEAP_DESCRIPTOR
);
1750 variants
->variants
[variant
].tripipe
= (struct mali_shader_meta
*) transfer
.cpu
;
1751 variants
->variants
[variant
].tripipe_gpu
= transfer
.gpu
;
1755 /* Select this variant */
1756 variants
->active_variant
= variant
;
1758 struct panfrost_shader_state
*shader_state
= &variants
->variants
[variant
];
1759 assert(panfrost_variant_matches(ctx
, shader_state
));
1761 /* Now we have a variant selected, so compile and go */
1763 if (!shader_state
->compiled
) {
1764 panfrost_shader_compile(ctx
, shader_state
->tripipe
, NULL
, JOB_TYPE_TILER
, shader_state
);
1765 shader_state
->compiled
= true;
1769 ctx
->dirty
|= PAN_DIRTY_FS
;
1773 panfrost_bind_vs_state(
1774 struct pipe_context
*pctx
,
1777 struct panfrost_context
*ctx
= pan_context(pctx
);
1782 if (!ctx
->vs
->variants
[0].compiled
) {
1783 ctx
->vs
->variants
[0].base
= hwcso
;
1785 /* TODO DRY from above */
1786 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_shader_meta
), HEAP_DESCRIPTOR
);
1787 ctx
->vs
->variants
[0].tripipe
= (struct mali_shader_meta
*) transfer
.cpu
;
1788 ctx
->vs
->variants
[0].tripipe_gpu
= transfer
.gpu
;
1790 panfrost_shader_compile(ctx
, ctx
->vs
->variants
[0].tripipe
, NULL
, JOB_TYPE_VERTEX
, &ctx
->vs
->variants
[0]);
1791 ctx
->vs
->variants
[0].compiled
= true;
1795 ctx
->dirty
|= PAN_DIRTY_VS
;
1799 panfrost_set_vertex_buffers(
1800 struct pipe_context
*pctx
,
1801 unsigned start_slot
,
1802 unsigned num_buffers
,
1803 const struct pipe_vertex_buffer
*buffers
)
1805 struct panfrost_context
*ctx
= pan_context(pctx
);
1807 util_set_vertex_buffers_mask(ctx
->vertex_buffers
, &ctx
->vb_mask
, buffers
, start_slot
, num_buffers
);
1811 panfrost_set_constant_buffer(
1812 struct pipe_context
*pctx
,
1813 enum pipe_shader_type shader
, uint index
,
1814 const struct pipe_constant_buffer
*buf
)
1816 struct panfrost_context
*ctx
= pan_context(pctx
);
1817 struct panfrost_constant_buffer
*pbuf
= &ctx
->constant_buffer
[shader
];
1819 size_t sz
= buf
? buf
->buffer_size
: 0;
1821 /* Free previous buffer */
1828 pbuf
->buffer
= NULL
;
1831 /* If unbinding, we're done */
1836 /* Multiple constant buffers not yet supported */
1841 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
);
1844 cpu
= rsrc
->bo
->cpu
;
1845 } else if (buf
->user_buffer
) {
1846 cpu
= buf
->user_buffer
;
1848 DBG("No constant buffer?\n");
1852 /* Copy the constant buffer into the driver context for later upload */
1854 pbuf
->buffer
= malloc(sz
);
1855 memcpy(pbuf
->buffer
, cpu
+ buf
->buffer_offset
, sz
);
1859 panfrost_set_stencil_ref(
1860 struct pipe_context
*pctx
,
1861 const struct pipe_stencil_ref
*ref
)
1863 struct panfrost_context
*ctx
= pan_context(pctx
);
1864 ctx
->stencil_ref
= *ref
;
1866 /* Shader core dirty */
1867 ctx
->dirty
|= PAN_DIRTY_FS
;
1870 static struct pipe_sampler_view
*
1871 panfrost_create_sampler_view(
1872 struct pipe_context
*pctx
,
1873 struct pipe_resource
*texture
,
1874 const struct pipe_sampler_view
*template)
1876 struct panfrost_sampler_view
*so
= CALLOC_STRUCT(panfrost_sampler_view
);
1877 int bytes_per_pixel
= util_format_get_blocksize(texture
->format
);
1879 pipe_reference(NULL
, &texture
->reference
);
1881 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) texture
;
1883 so
->base
= *template;
1884 so
->base
.texture
= texture
;
1885 so
->base
.reference
.count
= 1;
1886 so
->base
.context
= pctx
;
1888 /* sampler_views correspond to texture descriptors, minus the texture
1889 * (data) itself. So, we serialise the descriptor here and cache it for
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
= texture
->target
!= PIPE_TEXTURE_CUBE
,
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 /* Disable mipmapping for now to avoid regressions while automipmapping
1949 * is being implemented. TODO: Remove me once automipmaps work */
1951 //texture_descriptor.nr_mipmap_levels = template->u.tex.last_level - template->u.tex.first_level;
1952 texture_descriptor
.nr_mipmap_levels
= 0;
1954 so
->hw
= texture_descriptor
;
1956 return (struct pipe_sampler_view
*) so
;
1960 panfrost_set_sampler_views(
1961 struct pipe_context
*pctx
,
1962 enum pipe_shader_type shader
,
1963 unsigned start_slot
, unsigned num_views
,
1964 struct pipe_sampler_view
**views
)
1966 struct panfrost_context
*ctx
= pan_context(pctx
);
1968 assert(start_slot
== 0);
1970 ctx
->sampler_view_count
[shader
] = num_views
;
1971 memcpy(ctx
->sampler_views
[shader
], views
, num_views
* sizeof (void *));
1973 ctx
->dirty
|= PAN_DIRTY_TEXTURES
;
1977 panfrost_sampler_view_destroy(
1978 struct pipe_context
*pctx
,
1979 struct pipe_sampler_view
*views
)
1981 //struct panfrost_context *ctx = pan_context(pctx);
1989 panfrost_set_framebuffer_state(struct pipe_context
*pctx
,
1990 const struct pipe_framebuffer_state
*fb
)
1992 struct panfrost_context
*ctx
= pan_context(pctx
);
1994 /* Flush when switching away from an FBO */
1996 if (!panfrost_is_scanout(ctx
)) {
1997 panfrost_flush(pctx
, NULL
, 0);
2000 ctx
->pipe_framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
2001 ctx
->pipe_framebuffer
.samples
= fb
->samples
;
2002 ctx
->pipe_framebuffer
.layers
= fb
->layers
;
2003 ctx
->pipe_framebuffer
.width
= fb
->width
;
2004 ctx
->pipe_framebuffer
.height
= fb
->height
;
2006 for (int i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2007 struct pipe_surface
*cb
= i
< fb
->nr_cbufs
? fb
->cbufs
[i
] : NULL
;
2009 /* check if changing cbuf */
2010 if (ctx
->pipe_framebuffer
.cbufs
[i
] == cb
) continue;
2012 if (cb
&& (i
!= 0)) {
2013 DBG("XXX: Multiple render targets not supported before t7xx!\n");
2018 pipe_surface_reference(&ctx
->pipe_framebuffer
.cbufs
[i
], cb
);
2023 if (ctx
->require_sfbd
)
2024 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
2026 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
2028 panfrost_attach_vt_framebuffer(ctx
);
2030 struct panfrost_resource
*tex
= ((struct panfrost_resource
*) ctx
->pipe_framebuffer
.cbufs
[i
]->texture
);
2031 bool is_scanout
= panfrost_is_scanout(ctx
);
2033 if (!is_scanout
&& tex
->bo
->layout
!= PAN_AFBC
) {
2034 /* The blob is aggressive about enabling AFBC. As such,
2035 * it's pretty much necessary to use it here, since we
2036 * have no traces of non-compressed FBO. */
2038 panfrost_enable_afbc(ctx
, tex
, false);
2041 if (!is_scanout
&& !tex
->bo
->has_checksum
) {
2042 /* Enable transaction elimination if we can */
2043 panfrost_enable_checksum(ctx
, tex
);
2048 struct pipe_surface
*zb
= fb
->zsbuf
;
2050 if (ctx
->pipe_framebuffer
.zsbuf
!= zb
) {
2051 pipe_surface_reference(&ctx
->pipe_framebuffer
.zsbuf
, zb
);
2056 if (ctx
->require_sfbd
)
2057 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
);
2059 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
);
2061 panfrost_attach_vt_framebuffer(ctx
);
2063 /* Keep the depth FBO linear */
2070 panfrost_create_blend_state(struct pipe_context
*pipe
,
2071 const struct pipe_blend_state
*blend
)
2073 struct panfrost_context
*ctx
= pan_context(pipe
);
2074 struct panfrost_blend_state
*so
= CALLOC_STRUCT(panfrost_blend_state
);
2077 /* TODO: The following features are not yet implemented */
2078 assert(!blend
->logicop_enable
);
2079 assert(!blend
->alpha_to_coverage
);
2080 assert(!blend
->alpha_to_one
);
2082 /* Compile the blend state, first as fixed-function if we can */
2084 if (panfrost_make_fixed_blend_mode(&blend
->rt
[0], &so
->equation
, blend
->rt
[0].colormask
, &ctx
->blend_color
))
2087 /* If we can't, compile a blend shader instead */
2089 panfrost_make_blend_shader(ctx
, so
, &ctx
->blend_color
);
2095 panfrost_bind_blend_state(struct pipe_context
*pipe
,
2098 struct panfrost_context
*ctx
= pan_context(pipe
);
2099 struct pipe_blend_state
*blend
= (struct pipe_blend_state
*) cso
;
2100 struct panfrost_blend_state
*pblend
= (struct panfrost_blend_state
*) cso
;
2101 ctx
->blend
= pblend
;
2106 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_DITHER
, !blend
->dither
);
2108 /* TODO: Attach color */
2110 /* Shader itself is not dirty, but the shader core is */
2111 ctx
->dirty
|= PAN_DIRTY_FS
;
2115 panfrost_delete_blend_state(struct pipe_context
*pipe
,
2118 struct panfrost_blend_state
*so
= (struct panfrost_blend_state
*) blend
;
2120 if (so
->has_blend_shader
) {
2121 DBG("Deleting blend state leak blend shaders bytecode\n");
2128 panfrost_set_blend_color(struct pipe_context
*pipe
,
2129 const struct pipe_blend_color
*blend_color
)
2131 struct panfrost_context
*ctx
= pan_context(pipe
);
2133 /* If blend_color is we're unbinding, so ctx->blend_color is now undefined -> nothing to do */
2136 ctx
->blend_color
= *blend_color
;
2138 /* The blend mode depends on the blend constant color, due to the
2139 * fixed/programmable split. So, we're forced to regenerate the blend
2142 /* TODO: Attach color */
2147 panfrost_create_depth_stencil_state(struct pipe_context
*pipe
,
2148 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
2150 return mem_dup(depth_stencil
, sizeof(*depth_stencil
));
2154 panfrost_bind_depth_stencil_state(struct pipe_context
*pipe
,
2157 struct panfrost_context
*ctx
= pan_context(pipe
);
2158 struct pipe_depth_stencil_alpha_state
*depth_stencil
= cso
;
2159 ctx
->depth_stencil
= depth_stencil
;
2164 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2165 * emulated in the fragment shader */
2167 if (depth_stencil
->alpha
.enabled
) {
2168 /* We need to trigger a new shader (maybe) */
2169 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->fs
);
2173 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_STENCIL_TEST
, depth_stencil
->stencil
[0].enabled
); /* XXX: which one? */
2175 panfrost_make_stencil_state(&depth_stencil
->stencil
[0], &ctx
->fragment_shader_core
.stencil_front
);
2176 ctx
->fragment_shader_core
.stencil_mask_front
= depth_stencil
->stencil
[0].writemask
;
2178 panfrost_make_stencil_state(&depth_stencil
->stencil
[1], &ctx
->fragment_shader_core
.stencil_back
);
2179 ctx
->fragment_shader_core
.stencil_mask_back
= depth_stencil
->stencil
[1].writemask
;
2181 /* Depth state (TODO: Refactor) */
2182 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_DEPTH_TEST
, depth_stencil
->depth
.enabled
);
2184 int func
= depth_stencil
->depth
.enabled
? depth_stencil
->depth
.func
: PIPE_FUNC_ALWAYS
;
2186 ctx
->fragment_shader_core
.unknown2_3
&= ~MALI_DEPTH_FUNC_MASK
;
2187 ctx
->fragment_shader_core
.unknown2_3
|= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func
));
2189 /* Bounds test not implemented */
2190 assert(!depth_stencil
->depth
.bounds_test
);
2192 ctx
->dirty
|= PAN_DIRTY_FS
;
2196 panfrost_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
2202 panfrost_set_sample_mask(struct pipe_context
*pipe
,
2203 unsigned sample_mask
)
2208 panfrost_set_clip_state(struct pipe_context
*pipe
,
2209 const struct pipe_clip_state
*clip
)
2211 //struct panfrost_context *panfrost = pan_context(pipe);
2215 panfrost_set_viewport_states(struct pipe_context
*pipe
,
2216 unsigned start_slot
,
2217 unsigned num_viewports
,
2218 const struct pipe_viewport_state
*viewports
)
2220 struct panfrost_context
*ctx
= pan_context(pipe
);
2222 assert(start_slot
== 0);
2223 assert(num_viewports
== 1);
2225 ctx
->pipe_viewport
= *viewports
;
2228 /* TODO: What if not centered? */
2229 float w
= abs(viewports
->scale
[0]) * 2.0;
2230 float h
= abs(viewports
->scale
[1]) * 2.0;
2232 ctx
->viewport
.viewport1
[0] = MALI_POSITIVE((int) w
);
2233 ctx
->viewport
.viewport1
[1] = MALI_POSITIVE((int) h
);
2238 panfrost_set_scissor_states(struct pipe_context
*pipe
,
2239 unsigned start_slot
,
2240 unsigned num_scissors
,
2241 const struct pipe_scissor_state
*scissors
)
2243 struct panfrost_context
*ctx
= pan_context(pipe
);
2245 assert(start_slot
== 0);
2246 assert(num_scissors
== 1);
2248 ctx
->scissor
= *scissors
;
2252 panfrost_set_polygon_stipple(struct pipe_context
*pipe
,
2253 const struct pipe_poly_stipple
*stipple
)
2255 //struct panfrost_context *panfrost = pan_context(pipe);
2259 panfrost_set_active_query_state(struct pipe_context
*pipe
,
2262 //struct panfrost_context *panfrost = pan_context(pipe);
2266 panfrost_destroy(struct pipe_context
*pipe
)
2268 struct panfrost_context
*panfrost
= pan_context(pipe
);
2269 struct panfrost_screen
*screen
= pan_screen(pipe
->screen
);
2271 if (panfrost
->blitter
)
2272 util_blitter_destroy(panfrost
->blitter
);
2274 screen
->driver
->free_slab(screen
, &panfrost
->scratchpad
);
2275 screen
->driver
->free_slab(screen
, &panfrost
->varying_mem
);
2276 screen
->driver
->free_slab(screen
, &panfrost
->shaders
);
2277 screen
->driver
->free_slab(screen
, &panfrost
->tiler_heap
);
2278 screen
->driver
->free_slab(screen
, &panfrost
->misc_0
);
2281 static struct pipe_query
*
2282 panfrost_create_query(struct pipe_context
*pipe
,
2286 struct panfrost_query
*q
= CALLOC_STRUCT(panfrost_query
);
2291 return (struct pipe_query
*) q
;
2295 panfrost_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2301 panfrost_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2303 struct panfrost_context
*ctx
= pan_context(pipe
);
2304 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2306 switch (query
->type
) {
2307 case PIPE_QUERY_OCCLUSION_COUNTER
:
2308 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2309 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2311 /* Allocate a word for the query results to be stored */
2312 query
->transfer
= panfrost_allocate_chunk(ctx
, sizeof(unsigned), HEAP_DESCRIPTOR
);
2314 ctx
->occlusion_query
= query
;
2320 DBG("Skipping query %d\n", query
->type
);
2328 panfrost_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2330 struct panfrost_context
*ctx
= pan_context(pipe
);
2331 ctx
->occlusion_query
= NULL
;
2336 panfrost_get_query_result(struct pipe_context
*pipe
,
2337 struct pipe_query
*q
,
2339 union pipe_query_result
*vresult
)
2342 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2344 /* We need to flush out the jobs to actually run the counter, TODO
2345 * check wait, TODO wallpaper after if needed */
2347 panfrost_flush(pipe
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2349 switch (query
->type
) {
2350 case PIPE_QUERY_OCCLUSION_COUNTER
:
2351 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2352 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
: {
2353 /* Read back the query results */
2354 unsigned *result
= (unsigned *) query
->transfer
.cpu
;
2355 unsigned passed
= *result
;
2357 if (query
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
2358 vresult
->u64
= passed
;
2360 vresult
->b
= !!passed
;
2366 DBG("Skipped query get %d\n", query
->type
);
2373 static struct pipe_stream_output_target
*
2374 panfrost_create_stream_output_target(struct pipe_context
*pctx
,
2375 struct pipe_resource
*prsc
,
2376 unsigned buffer_offset
,
2377 unsigned buffer_size
)
2379 struct pipe_stream_output_target
*target
;
2381 target
= CALLOC_STRUCT(pipe_stream_output_target
);
2386 pipe_reference_init(&target
->reference
, 1);
2387 pipe_resource_reference(&target
->buffer
, prsc
);
2389 target
->context
= pctx
;
2390 target
->buffer_offset
= buffer_offset
;
2391 target
->buffer_size
= buffer_size
;
2397 panfrost_stream_output_target_destroy(struct pipe_context
*pctx
,
2398 struct pipe_stream_output_target
*target
)
2400 pipe_resource_reference(&target
->buffer
, NULL
);
2405 panfrost_set_stream_output_targets(struct pipe_context
*pctx
,
2406 unsigned num_targets
,
2407 struct pipe_stream_output_target
**targets
,
2408 const unsigned *offsets
)
2414 panfrost_setup_hardware(struct panfrost_context
*ctx
)
2416 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2417 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
2419 for (int i
= 0; i
< ARRAY_SIZE(ctx
->transient_pools
); ++i
) {
2420 /* Allocate the beginning of the transient pool */
2421 int entry_size
= (1 << 22); /* 4MB */
2423 ctx
->transient_pools
[i
].entry_size
= entry_size
;
2424 ctx
->transient_pools
[i
].entry_count
= 1;
2426 ctx
->transient_pools
[i
].entries
[0] = (struct panfrost_memory_entry
*) pb_slab_alloc(&screen
->slabs
, entry_size
, HEAP_TRANSIENT
);
2429 screen
->driver
->allocate_slab(screen
, &ctx
->scratchpad
, 64, false, 0, 0, 0);
2430 screen
->driver
->allocate_slab(screen
, &ctx
->varying_mem
, 16384, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_COHERENT_LOCAL
, 0, 0);
2431 screen
->driver
->allocate_slab(screen
, &ctx
->shaders
, 4096, true, PAN_ALLOCATE_EXECUTE
, 0, 0);
2432 screen
->driver
->allocate_slab(screen
, &ctx
->tiler_heap
, 32768, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2433 screen
->driver
->allocate_slab(screen
, &ctx
->misc_0
, 128*128, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2437 /* New context creation, which also does hardware initialisation since I don't
2438 * know the better way to structure this :smirk: */
2440 struct pipe_context
*
2441 panfrost_create_context(struct pipe_screen
*screen
, void *priv
, unsigned flags
)
2443 struct panfrost_context
*ctx
= CALLOC_STRUCT(panfrost_context
);
2444 struct panfrost_screen
*pscreen
= pan_screen(screen
);
2445 memset(ctx
, 0, sizeof(*ctx
));
2446 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2449 gpu_id
= pscreen
->driver
->query_gpu_version(pscreen
);
2451 ctx
->is_t6xx
= gpu_id
<= 0x0750; /* For now, this flag means T760 or less */
2452 ctx
->require_sfbd
= gpu_id
< 0x0750; /* T760 is the first to support MFBD */
2454 gallium
->screen
= screen
;
2456 gallium
->destroy
= panfrost_destroy
;
2458 gallium
->set_framebuffer_state
= panfrost_set_framebuffer_state
;
2460 gallium
->flush
= panfrost_flush
;
2461 gallium
->clear
= panfrost_clear
;
2462 gallium
->draw_vbo
= panfrost_draw_vbo
;
2464 gallium
->set_vertex_buffers
= panfrost_set_vertex_buffers
;
2465 gallium
->set_constant_buffer
= panfrost_set_constant_buffer
;
2467 gallium
->set_stencil_ref
= panfrost_set_stencil_ref
;
2469 gallium
->create_sampler_view
= panfrost_create_sampler_view
;
2470 gallium
->set_sampler_views
= panfrost_set_sampler_views
;
2471 gallium
->sampler_view_destroy
= panfrost_sampler_view_destroy
;
2473 gallium
->create_rasterizer_state
= panfrost_create_rasterizer_state
;
2474 gallium
->bind_rasterizer_state
= panfrost_bind_rasterizer_state
;
2475 gallium
->delete_rasterizer_state
= panfrost_generic_cso_delete
;
2477 gallium
->create_vertex_elements_state
= panfrost_create_vertex_elements_state
;
2478 gallium
->bind_vertex_elements_state
= panfrost_bind_vertex_elements_state
;
2479 gallium
->delete_vertex_elements_state
= panfrost_delete_vertex_elements_state
;
2481 gallium
->create_fs_state
= panfrost_create_shader_state
;
2482 gallium
->delete_fs_state
= panfrost_delete_shader_state
;
2483 gallium
->bind_fs_state
= panfrost_bind_fs_state
;
2485 gallium
->create_vs_state
= panfrost_create_shader_state
;
2486 gallium
->delete_vs_state
= panfrost_delete_shader_state
;
2487 gallium
->bind_vs_state
= panfrost_bind_vs_state
;
2489 gallium
->create_sampler_state
= panfrost_create_sampler_state
;
2490 gallium
->delete_sampler_state
= panfrost_generic_cso_delete
;
2491 gallium
->bind_sampler_states
= panfrost_bind_sampler_states
;
2493 gallium
->create_blend_state
= panfrost_create_blend_state
;
2494 gallium
->bind_blend_state
= panfrost_bind_blend_state
;
2495 gallium
->delete_blend_state
= panfrost_delete_blend_state
;
2497 gallium
->set_blend_color
= panfrost_set_blend_color
;
2499 gallium
->create_depth_stencil_alpha_state
= panfrost_create_depth_stencil_state
;
2500 gallium
->bind_depth_stencil_alpha_state
= panfrost_bind_depth_stencil_state
;
2501 gallium
->delete_depth_stencil_alpha_state
= panfrost_delete_depth_stencil_state
;
2503 gallium
->set_sample_mask
= panfrost_set_sample_mask
;
2505 gallium
->set_clip_state
= panfrost_set_clip_state
;
2506 gallium
->set_viewport_states
= panfrost_set_viewport_states
;
2507 gallium
->set_scissor_states
= panfrost_set_scissor_states
;
2508 gallium
->set_polygon_stipple
= panfrost_set_polygon_stipple
;
2509 gallium
->set_active_query_state
= panfrost_set_active_query_state
;
2511 gallium
->create_query
= panfrost_create_query
;
2512 gallium
->destroy_query
= panfrost_destroy_query
;
2513 gallium
->begin_query
= panfrost_begin_query
;
2514 gallium
->end_query
= panfrost_end_query
;
2515 gallium
->get_query_result
= panfrost_get_query_result
;
2517 gallium
->create_stream_output_target
= panfrost_create_stream_output_target
;
2518 gallium
->stream_output_target_destroy
= panfrost_stream_output_target_destroy
;
2519 gallium
->set_stream_output_targets
= panfrost_set_stream_output_targets
;
2521 panfrost_resource_context_init(gallium
);
2523 pscreen
->driver
->init_context(ctx
);
2525 panfrost_setup_hardware(ctx
);
2528 gallium
->stream_uploader
= u_upload_create_default(gallium
);
2529 gallium
->const_uploader
= gallium
->stream_uploader
;
2530 assert(gallium
->stream_uploader
);
2532 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2533 ctx
->draw_modes
= (1 << (PIPE_PRIM_POLYGON
+ 1)) - 1;
2535 ctx
->primconvert
= util_primconvert_create(gallium
, ctx
->draw_modes
);
2537 ctx
->blitter
= util_blitter_create(gallium
);
2538 assert(ctx
->blitter
);
2540 /* Prepare for render! */
2542 panfrost_job_init(ctx
);
2543 panfrost_emit_vertex_payload(ctx
);
2544 panfrost_emit_tiler_payload(ctx
);
2545 panfrost_invalidate_frame(ctx
);
2546 panfrost_default_shader_backend(ctx
);
2547 panfrost_generate_space_filler_indices();