2 * © Copyright 2018 Alyssa Rosenzweig
3 * Copyright © 2014-2017 Broadcom
4 * Copyright (C) 2017 Intel Corporation
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include "pan_context.h"
31 #include "pan_format.h"
33 #include "util/macros.h"
34 #include "util/u_format.h"
35 #include "util/u_inlines.h"
36 #include "util/u_upload_mgr.h"
37 #include "util/u_memory.h"
38 #include "util/u_vbuf.h"
39 #include "util/half_float.h"
40 #include "util/u_helpers.h"
41 #include "util/u_format.h"
42 #include "util/u_prim.h"
43 #include "util/u_prim_restart.h"
44 #include "indices/u_primconvert.h"
45 #include "tgsi/tgsi_parse.h"
46 #include "tgsi/tgsi_from_mesa.h"
47 #include "util/u_math.h"
49 #include "pan_screen.h"
50 #include "pan_blending.h"
51 #include "pan_blend_shaders.h"
54 /* Framebuffer descriptor */
56 static struct midgard_tiler_descriptor
57 panfrost_emit_midg_tiler(
58 struct panfrost_context
*ctx
,
61 unsigned vertex_count
)
63 struct midgard_tiler_descriptor t
= {};
64 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
67 panfrost_choose_hierarchy_mask(width
, height
, vertex_count
);
69 /* Compute the polygon header size and use that to offset the body */
71 unsigned header_size
= panfrost_tiler_header_size(
72 width
, height
, t
.hierarchy_mask
);
74 t
.polygon_list_size
= panfrost_tiler_full_size(
75 width
, height
, t
.hierarchy_mask
);
79 if (t
.hierarchy_mask
) {
80 t
.polygon_list
= panfrost_batch_get_polygon_list(batch
,
85 /* Allow the entire tiler heap */
86 t
.heap_start
= ctx
->tiler_heap
->gpu
;
87 t
.heap_end
= ctx
->tiler_heap
->gpu
+ ctx
->tiler_heap
->size
;
89 /* The tiler is disabled, so don't allow the tiler heap */
90 t
.heap_start
= ctx
->tiler_heap
->gpu
;
91 t
.heap_end
= t
.heap_start
;
93 /* Use a dummy polygon list */
94 t
.polygon_list
= ctx
->tiler_dummy
->gpu
;
96 /* Disable the tiler */
97 t
.hierarchy_mask
|= MALI_TILER_DISABLED
;
100 t
.polygon_list_body
=
101 t
.polygon_list
+ header_size
;
106 struct mali_single_framebuffer
107 panfrost_emit_sfbd(struct panfrost_context
*ctx
, unsigned vertex_count
)
109 unsigned width
= ctx
->pipe_framebuffer
.width
;
110 unsigned height
= ctx
->pipe_framebuffer
.height
;
112 struct mali_single_framebuffer framebuffer
= {
113 .width
= MALI_POSITIVE(width
),
114 .height
= MALI_POSITIVE(height
),
116 .format
= 0x30000000,
117 .clear_flags
= 0x1000,
118 .unknown_address_0
= ctx
->scratchpad
->gpu
,
119 .tiler
= panfrost_emit_midg_tiler(ctx
,
120 width
, height
, vertex_count
),
126 struct bifrost_framebuffer
127 panfrost_emit_mfbd(struct panfrost_context
*ctx
, unsigned vertex_count
)
129 unsigned width
= ctx
->pipe_framebuffer
.width
;
130 unsigned height
= ctx
->pipe_framebuffer
.height
;
132 struct bifrost_framebuffer framebuffer
= {
133 .unk0
= 0x1e5, /* 1e4 if no spill */
134 .width1
= MALI_POSITIVE(width
),
135 .height1
= MALI_POSITIVE(height
),
136 .width2
= MALI_POSITIVE(width
),
137 .height2
= MALI_POSITIVE(height
),
141 .rt_count_1
= MALI_POSITIVE(ctx
->pipe_framebuffer
.nr_cbufs
),
146 .scratchpad
= ctx
->scratchpad
->gpu
,
147 .tiler
= panfrost_emit_midg_tiler(ctx
,
148 width
, height
, vertex_count
)
156 struct pipe_context
*pipe
,
158 const union pipe_color_union
*color
,
159 double depth
, unsigned stencil
)
161 struct panfrost_context
*ctx
= pan_context(pipe
);
162 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
164 panfrost_batch_clear(batch
, buffers
, color
, depth
, stencil
);
168 panfrost_attach_vt_mfbd(struct panfrost_context
*ctx
)
170 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
171 struct bifrost_framebuffer mfbd
= panfrost_emit_mfbd(ctx
, ~0);
173 return panfrost_upload_transient(batch
, &mfbd
, sizeof(mfbd
)) | MALI_MFBD
;
177 panfrost_attach_vt_sfbd(struct panfrost_context
*ctx
)
179 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
180 struct mali_single_framebuffer sfbd
= panfrost_emit_sfbd(ctx
, ~0);
182 return panfrost_upload_transient(batch
, &sfbd
, sizeof(sfbd
)) | MALI_SFBD
;
186 panfrost_attach_vt_framebuffer(struct panfrost_context
*ctx
)
188 /* Skip the attach if we can */
190 if (ctx
->payloads
[PIPE_SHADER_VERTEX
].postfix
.framebuffer
) {
191 assert(ctx
->payloads
[PIPE_SHADER_FRAGMENT
].postfix
.framebuffer
);
195 struct panfrost_screen
*screen
= pan_screen(ctx
->base
.screen
);
196 mali_ptr framebuffer
= screen
->require_sfbd
?
197 panfrost_attach_vt_sfbd(ctx
) :
198 panfrost_attach_vt_mfbd(ctx
);
200 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; ++i
)
201 ctx
->payloads
[i
].postfix
.framebuffer
= framebuffer
;
204 /* Reset per-frame context, called on context initialisation as well as after
205 * flushing a frame */
208 panfrost_invalidate_frame(struct panfrost_context
*ctx
)
210 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; ++i
)
211 ctx
->payloads
[i
].postfix
.framebuffer
= 0;
214 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
217 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
| PAN_DIRTY_TEXTURES
;
219 /* TODO: When does this need to be handled? */
220 ctx
->active_queries
= true;
223 /* In practice, every field of these payloads should be configurable
224 * arbitrarily, which means these functions are basically catch-all's for
225 * as-of-yet unwavering unknowns */
228 panfrost_emit_vertex_payload(struct panfrost_context
*ctx
)
230 /* 0x2 bit clear on 32-bit T6XX */
232 struct midgard_payload_vertex_tiler payload
= {
233 .gl_enables
= 0x4 | 0x2,
236 /* Vertex and compute are closely coupled, so share a payload */
238 memcpy(&ctx
->payloads
[PIPE_SHADER_VERTEX
], &payload
, sizeof(payload
));
239 memcpy(&ctx
->payloads
[PIPE_SHADER_COMPUTE
], &payload
, sizeof(payload
));
243 panfrost_emit_tiler_payload(struct panfrost_context
*ctx
)
245 struct midgard_payload_vertex_tiler payload
= {
247 .zero1
= 0xffff, /* Why is this only seen on test-quad-textured? */
251 memcpy(&ctx
->payloads
[PIPE_SHADER_FRAGMENT
], &payload
, sizeof(payload
));
255 translate_tex_wrap(enum pipe_tex_wrap w
)
258 case PIPE_TEX_WRAP_REPEAT
:
259 return MALI_WRAP_REPEAT
;
261 /* TODO: lower GL_CLAMP? */
262 case PIPE_TEX_WRAP_CLAMP
:
263 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
264 return MALI_WRAP_CLAMP_TO_EDGE
;
266 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
267 return MALI_WRAP_CLAMP_TO_BORDER
;
269 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
270 return MALI_WRAP_MIRRORED_REPEAT
;
273 unreachable("Invalid wrap");
278 panfrost_translate_compare_func(enum pipe_compare_func in
)
281 case PIPE_FUNC_NEVER
:
282 return MALI_FUNC_NEVER
;
285 return MALI_FUNC_LESS
;
287 case PIPE_FUNC_EQUAL
:
288 return MALI_FUNC_EQUAL
;
290 case PIPE_FUNC_LEQUAL
:
291 return MALI_FUNC_LEQUAL
;
293 case PIPE_FUNC_GREATER
:
294 return MALI_FUNC_GREATER
;
296 case PIPE_FUNC_NOTEQUAL
:
297 return MALI_FUNC_NOTEQUAL
;
299 case PIPE_FUNC_GEQUAL
:
300 return MALI_FUNC_GEQUAL
;
302 case PIPE_FUNC_ALWAYS
:
303 return MALI_FUNC_ALWAYS
;
306 unreachable("Invalid func");
311 panfrost_translate_alt_compare_func(enum pipe_compare_func in
)
314 case PIPE_FUNC_NEVER
:
315 return MALI_ALT_FUNC_NEVER
;
318 return MALI_ALT_FUNC_LESS
;
320 case PIPE_FUNC_EQUAL
:
321 return MALI_ALT_FUNC_EQUAL
;
323 case PIPE_FUNC_LEQUAL
:
324 return MALI_ALT_FUNC_LEQUAL
;
326 case PIPE_FUNC_GREATER
:
327 return MALI_ALT_FUNC_GREATER
;
329 case PIPE_FUNC_NOTEQUAL
:
330 return MALI_ALT_FUNC_NOTEQUAL
;
332 case PIPE_FUNC_GEQUAL
:
333 return MALI_ALT_FUNC_GEQUAL
;
335 case PIPE_FUNC_ALWAYS
:
336 return MALI_ALT_FUNC_ALWAYS
;
339 unreachable("Invalid alt func");
344 panfrost_translate_stencil_op(enum pipe_stencil_op in
)
347 case PIPE_STENCIL_OP_KEEP
:
348 return MALI_STENCIL_KEEP
;
350 case PIPE_STENCIL_OP_ZERO
:
351 return MALI_STENCIL_ZERO
;
353 case PIPE_STENCIL_OP_REPLACE
:
354 return MALI_STENCIL_REPLACE
;
356 case PIPE_STENCIL_OP_INCR
:
357 return MALI_STENCIL_INCR
;
359 case PIPE_STENCIL_OP_DECR
:
360 return MALI_STENCIL_DECR
;
362 case PIPE_STENCIL_OP_INCR_WRAP
:
363 return MALI_STENCIL_INCR_WRAP
;
365 case PIPE_STENCIL_OP_DECR_WRAP
:
366 return MALI_STENCIL_DECR_WRAP
;
368 case PIPE_STENCIL_OP_INVERT
:
369 return MALI_STENCIL_INVERT
;
372 unreachable("Invalid stencil op");
377 panfrost_make_stencil_state(const struct pipe_stencil_state
*in
, struct mali_stencil_test
*out
)
379 out
->ref
= 0; /* Gallium gets it from elsewhere */
381 out
->mask
= in
->valuemask
;
382 out
->func
= panfrost_translate_compare_func(in
->func
);
383 out
->sfail
= panfrost_translate_stencil_op(in
->fail_op
);
384 out
->dpfail
= panfrost_translate_stencil_op(in
->zfail_op
);
385 out
->dppass
= panfrost_translate_stencil_op(in
->zpass_op
);
389 panfrost_default_shader_backend(struct panfrost_context
*ctx
)
391 struct mali_shader_meta shader
= {
392 .alpha_coverage
= ~MALI_ALPHA_COVERAGE(0.000000),
394 .unknown2_3
= MALI_DEPTH_FUNC(MALI_FUNC_ALWAYS
) | 0x3010,
395 .unknown2_4
= MALI_NO_MSAA
| 0x4e0,
398 /* unknown2_4 has 0x10 bit set on T6XX. We don't know why this is
399 * required (independent of 32-bit/64-bit descriptors), or why it's not
400 * used on later GPU revisions. Otherwise, all shader jobs fault on
401 * these earlier chips (perhaps this is a chicken bit of some kind).
402 * More investigation is needed. */
405 shader
.unknown2_4
|= 0x10;
408 struct pipe_stencil_state default_stencil
= {
410 .func
= PIPE_FUNC_ALWAYS
,
411 .fail_op
= MALI_STENCIL_KEEP
,
412 .zfail_op
= MALI_STENCIL_KEEP
,
413 .zpass_op
= MALI_STENCIL_KEEP
,
418 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_front
);
419 shader
.stencil_mask_front
= default_stencil
.writemask
;
421 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_back
);
422 shader
.stencil_mask_back
= default_stencil
.writemask
;
424 if (default_stencil
.enabled
)
425 shader
.unknown2_4
|= MALI_STENCIL_TEST
;
427 memcpy(&ctx
->fragment_shader_core
, &shader
, sizeof(shader
));
430 /* Generates a vertex/tiler job. This is, in some sense, the heart of the
431 * graphics command stream. It should be called once per draw, accordding to
432 * presentations. Set is_tiler for "tiler" jobs (fragment shader jobs, but in
433 * Mali parlance, "fragment" refers to framebuffer writeout). Clear it for
436 struct panfrost_transfer
437 panfrost_vertex_tiler_job(struct panfrost_context
*ctx
, bool is_tiler
)
439 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
440 struct mali_job_descriptor_header job
= {
441 .job_type
= is_tiler
? JOB_TYPE_TILER
: JOB_TYPE_VERTEX
,
442 .job_descriptor_size
= 1,
445 struct midgard_payload_vertex_tiler
*payload
= is_tiler
? &ctx
->payloads
[PIPE_SHADER_FRAGMENT
] : &ctx
->payloads
[PIPE_SHADER_VERTEX
];
447 struct panfrost_transfer transfer
= panfrost_allocate_transient(batch
, sizeof(job
) + sizeof(*payload
));
448 memcpy(transfer
.cpu
, &job
, sizeof(job
));
449 memcpy(transfer
.cpu
+ sizeof(job
), payload
, sizeof(*payload
));
454 panfrost_vertex_buffer_address(struct panfrost_context
*ctx
, unsigned i
)
456 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[i
];
457 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
.resource
);
459 return rsrc
->bo
->gpu
+ buf
->buffer_offset
;
463 panfrost_writes_point_size(struct panfrost_context
*ctx
)
465 assert(ctx
->shader
[PIPE_SHADER_VERTEX
]);
466 struct panfrost_shader_state
*vs
= &ctx
->shader
[PIPE_SHADER_VERTEX
]->variants
[ctx
->shader
[PIPE_SHADER_VERTEX
]->active_variant
];
468 return vs
->writes_point_size
&& ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.draw_mode
== MALI_POINTS
;
471 /* Stage the attribute descriptors so we can adjust src_offset
472 * to let BOs align nicely */
475 panfrost_stage_attributes(struct panfrost_context
*ctx
)
477 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
478 struct panfrost_vertex_state
*so
= ctx
->vertex
;
480 size_t sz
= sizeof(struct mali_attr_meta
) * so
->num_elements
;
481 struct panfrost_transfer transfer
= panfrost_allocate_transient(batch
, sz
);
482 struct mali_attr_meta
*target
= (struct mali_attr_meta
*) transfer
.cpu
;
484 /* Copy as-is for the first pass */
485 memcpy(target
, so
->hw
, sz
);
487 /* Fixup offsets for the second pass. Recall that the hardware
488 * calculates attribute addresses as:
490 * addr = base + (stride * vtx) + src_offset;
492 * However, on Mali, base must be aligned to 64-bytes, so we
495 * base' = base & ~63 = base - (base & 63)
497 * To compensate when using base' (see emit_vertex_data), we have
498 * to adjust src_offset by the masked off piece:
500 * addr' = base' + (stride * vtx) + (src_offset + (base & 63))
501 * = base - (base & 63) + (stride * vtx) + src_offset + (base & 63)
502 * = base + (stride * vtx) + src_offset
508 unsigned start
= ctx
->payloads
[PIPE_SHADER_VERTEX
].offset_start
;
510 for (unsigned i
= 0; i
< so
->num_elements
; ++i
) {
511 unsigned vbi
= so
->pipe
[i
].vertex_buffer_index
;
512 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[vbi
];
513 mali_ptr addr
= panfrost_vertex_buffer_address(ctx
, vbi
);
515 /* Adjust by the masked off bits of the offset */
516 target
[i
].src_offset
+= (addr
& 63);
518 /* Also, somewhat obscurely per-instance data needs to be
519 * offset in response to a delayed start in an indexed draw */
521 if (so
->pipe
[i
].instance_divisor
&& ctx
->instance_count
> 1 && start
) {
522 target
[i
].src_offset
-= buf
->stride
* start
;
528 ctx
->payloads
[PIPE_SHADER_VERTEX
].postfix
.attribute_meta
= transfer
.gpu
;
532 panfrost_upload_sampler_descriptors(struct panfrost_context
*ctx
)
534 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
535 size_t desc_size
= sizeof(struct mali_sampler_descriptor
);
537 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
540 if (ctx
->sampler_count
[t
] && ctx
->sampler_view_count
[t
]) {
541 size_t transfer_size
= desc_size
* ctx
->sampler_count
[t
];
543 struct panfrost_transfer transfer
=
544 panfrost_allocate_transient(batch
, transfer_size
);
546 struct mali_sampler_descriptor
*desc
=
547 (struct mali_sampler_descriptor
*) transfer
.cpu
;
549 for (int i
= 0; i
< ctx
->sampler_count
[t
]; ++i
)
550 desc
[i
] = ctx
->samplers
[t
][i
]->hw
;
552 upload
= transfer
.gpu
;
555 ctx
->payloads
[t
].postfix
.sampler_descriptor
= upload
;
559 static enum mali_texture_layout
560 panfrost_layout_for_texture(struct panfrost_resource
*rsrc
)
562 /* TODO: other linear depth textures */
563 bool is_depth
= rsrc
->base
.format
== PIPE_FORMAT_Z32_UNORM
;
565 switch (rsrc
->layout
) {
567 return MALI_TEXTURE_AFBC
;
570 return MALI_TEXTURE_TILED
;
572 return is_depth
? MALI_TEXTURE_TILED
: MALI_TEXTURE_LINEAR
;
574 unreachable("Invalid texture layout");
580 struct panfrost_context
*ctx
,
581 struct panfrost_sampler_view
*view
)
586 struct pipe_sampler_view
*pview
= &view
->base
;
587 struct panfrost_resource
*rsrc
= pan_resource(pview
->texture
);
589 /* Do we interleave an explicit stride with every element? */
591 bool has_manual_stride
= view
->manual_stride
;
593 /* For easy access */
595 bool is_buffer
= pview
->target
== PIPE_BUFFER
;
596 unsigned first_level
= is_buffer
? 0 : pview
->u
.tex
.first_level
;
597 unsigned last_level
= is_buffer
? 0 : pview
->u
.tex
.last_level
;
598 unsigned first_layer
= is_buffer
? 0 : pview
->u
.tex
.first_layer
;
599 unsigned last_layer
= is_buffer
? 0 : pview
->u
.tex
.last_layer
;
601 /* Lower-bit is set when sampling from colour AFBC */
602 bool is_afbc
= rsrc
->layout
== PAN_AFBC
;
603 bool is_zs
= rsrc
->base
.bind
& PIPE_BIND_DEPTH_STENCIL
;
604 unsigned afbc_bit
= (is_afbc
&& !is_zs
) ? 1 : 0;
606 /* Add the BO to the job so it's retained until the job is done. */
607 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
608 panfrost_batch_add_bo(batch
, rsrc
->bo
);
610 /* Add the usage flags in, since they can change across the CSO
611 * lifetime due to layout switches */
613 view
->hw
.format
.layout
= panfrost_layout_for_texture(rsrc
);
614 view
->hw
.format
.manual_stride
= has_manual_stride
;
616 /* Inject the addresses in, interleaving mip levels, cube faces, and
617 * strides in that order */
621 for (unsigned l
= first_level
; l
<= last_level
; ++l
) {
622 for (unsigned f
= first_layer
; f
<= last_layer
; ++f
) {
624 view
->hw
.payload
[idx
++] =
625 panfrost_get_texture_address(rsrc
, l
, f
) + afbc_bit
;
627 if (has_manual_stride
) {
628 view
->hw
.payload
[idx
++] =
629 rsrc
->slices
[l
].stride
;
634 return panfrost_upload_transient(batch
, &view
->hw
,
635 sizeof(struct mali_texture_descriptor
));
639 panfrost_upload_texture_descriptors(struct panfrost_context
*ctx
)
641 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
643 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
644 mali_ptr trampoline
= 0;
646 if (ctx
->sampler_view_count
[t
]) {
647 uint64_t trampolines
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
649 for (int i
= 0; i
< ctx
->sampler_view_count
[t
]; ++i
)
651 panfrost_upload_tex(ctx
, ctx
->sampler_views
[t
][i
]);
653 trampoline
= panfrost_upload_transient(batch
, trampolines
, sizeof(uint64_t) * ctx
->sampler_view_count
[t
]);
656 ctx
->payloads
[t
].postfix
.texture_trampoline
= trampoline
;
660 struct sysval_uniform
{
669 static void panfrost_upload_viewport_scale_sysval(struct panfrost_context
*ctx
,
670 struct sysval_uniform
*uniform
)
672 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
674 uniform
->f
[0] = vp
->scale
[0];
675 uniform
->f
[1] = vp
->scale
[1];
676 uniform
->f
[2] = vp
->scale
[2];
679 static void panfrost_upload_viewport_offset_sysval(struct panfrost_context
*ctx
,
680 struct sysval_uniform
*uniform
)
682 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
684 uniform
->f
[0] = vp
->translate
[0];
685 uniform
->f
[1] = vp
->translate
[1];
686 uniform
->f
[2] = vp
->translate
[2];
689 static void panfrost_upload_txs_sysval(struct panfrost_context
*ctx
,
690 enum pipe_shader_type st
,
691 unsigned int sysvalid
,
692 struct sysval_uniform
*uniform
)
694 unsigned texidx
= PAN_SYSVAL_ID_TO_TXS_TEX_IDX(sysvalid
);
695 unsigned dim
= PAN_SYSVAL_ID_TO_TXS_DIM(sysvalid
);
696 bool is_array
= PAN_SYSVAL_ID_TO_TXS_IS_ARRAY(sysvalid
);
697 struct pipe_sampler_view
*tex
= &ctx
->sampler_views
[st
][texidx
]->base
;
700 uniform
->i
[0] = u_minify(tex
->texture
->width0
, tex
->u
.tex
.first_level
);
703 uniform
->i
[1] = u_minify(tex
->texture
->height0
,
704 tex
->u
.tex
.first_level
);
707 uniform
->i
[2] = u_minify(tex
->texture
->depth0
,
708 tex
->u
.tex
.first_level
);
711 uniform
->i
[dim
] = tex
->texture
->array_size
;
714 static void panfrost_upload_ssbo_sysval(
715 struct panfrost_context
*ctx
,
716 enum pipe_shader_type st
,
718 struct sysval_uniform
*uniform
)
720 assert(ctx
->ssbo_mask
[st
] & (1 << ssbo_id
));
721 struct pipe_shader_buffer sb
= ctx
->ssbo
[st
][ssbo_id
];
723 /* Compute address */
724 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
725 struct panfrost_bo
*bo
= pan_resource(sb
.buffer
)->bo
;
727 panfrost_batch_add_bo(batch
, bo
);
729 /* Upload address and size as sysval */
730 uniform
->du
[0] = bo
->gpu
+ sb
.buffer_offset
;
731 uniform
->u
[2] = sb
.buffer_size
;
734 static void panfrost_upload_num_work_groups_sysval(struct panfrost_context
*ctx
,
735 struct sysval_uniform
*uniform
)
737 uniform
->u
[0] = ctx
->compute_grid
->grid
[0];
738 uniform
->u
[1] = ctx
->compute_grid
->grid
[1];
739 uniform
->u
[2] = ctx
->compute_grid
->grid
[2];
742 static void panfrost_upload_sysvals(struct panfrost_context
*ctx
, void *buf
,
743 struct panfrost_shader_state
*ss
,
744 enum pipe_shader_type st
)
746 struct sysval_uniform
*uniforms
= (void *)buf
;
748 for (unsigned i
= 0; i
< ss
->sysval_count
; ++i
) {
749 int sysval
= ss
->sysval
[i
];
751 switch (PAN_SYSVAL_TYPE(sysval
)) {
752 case PAN_SYSVAL_VIEWPORT_SCALE
:
753 panfrost_upload_viewport_scale_sysval(ctx
, &uniforms
[i
]);
755 case PAN_SYSVAL_VIEWPORT_OFFSET
:
756 panfrost_upload_viewport_offset_sysval(ctx
, &uniforms
[i
]);
758 case PAN_SYSVAL_TEXTURE_SIZE
:
759 panfrost_upload_txs_sysval(ctx
, st
, PAN_SYSVAL_ID(sysval
),
762 case PAN_SYSVAL_SSBO
:
763 panfrost_upload_ssbo_sysval(ctx
, st
, PAN_SYSVAL_ID(sysval
),
766 case PAN_SYSVAL_NUM_WORK_GROUPS
:
767 panfrost_upload_num_work_groups_sysval(ctx
, &uniforms
[i
]);
777 panfrost_map_constant_buffer_cpu(struct panfrost_constant_buffer
*buf
, unsigned index
)
779 struct pipe_constant_buffer
*cb
= &buf
->cb
[index
];
780 struct panfrost_resource
*rsrc
= pan_resource(cb
->buffer
);
783 return rsrc
->bo
->cpu
;
784 else if (cb
->user_buffer
)
785 return cb
->user_buffer
;
787 unreachable("No constant buffer");
791 panfrost_map_constant_buffer_gpu(
792 struct panfrost_context
*ctx
,
793 struct panfrost_constant_buffer
*buf
,
796 struct pipe_constant_buffer
*cb
= &buf
->cb
[index
];
797 struct panfrost_resource
*rsrc
= pan_resource(cb
->buffer
);
800 return rsrc
->bo
->gpu
;
801 } else if (cb
->user_buffer
) {
802 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
803 return panfrost_upload_transient(batch
, cb
->user_buffer
, cb
->buffer_size
);
805 unreachable("No constant buffer");
809 /* Compute number of UBOs active (more specifically, compute the highest UBO
810 * number addressable -- if there are gaps, include them in the count anyway).
811 * We always include UBO #0 in the count, since we *need* uniforms enabled for
815 panfrost_ubo_count(struct panfrost_context
*ctx
, enum pipe_shader_type stage
)
817 unsigned mask
= ctx
->constant_buffer
[stage
].enabled_mask
| 1;
818 return 32 - __builtin_clz(mask
);
821 /* Fixes up a shader state with current state, returning a GPU address to the
825 panfrost_patch_shader_state(
826 struct panfrost_context
*ctx
,
827 struct panfrost_shader_state
*ss
,
828 enum pipe_shader_type stage
,
831 ss
->tripipe
->texture_count
= ctx
->sampler_view_count
[stage
];
832 ss
->tripipe
->sampler_count
= ctx
->sampler_count
[stage
];
834 ss
->tripipe
->midgard1
.flags
= 0x220;
836 unsigned ubo_count
= panfrost_ubo_count(ctx
, stage
);
837 ss
->tripipe
->midgard1
.uniform_buffer_count
= ubo_count
;
839 /* We can't reuse over frames; that's not safe. The descriptor must be
840 * transient uploaded */
843 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
845 return panfrost_upload_transient(batch
, ss
->tripipe
,
846 sizeof(struct mali_shader_meta
));
849 /* If we don't need an upload, don't bother */
855 panfrost_patch_shader_state_compute(
856 struct panfrost_context
*ctx
,
857 enum pipe_shader_type stage
,
860 struct panfrost_shader_variants
*all
= ctx
->shader
[stage
];
863 ctx
->payloads
[stage
].postfix
._shader_upper
= 0;
867 struct panfrost_shader_state
*s
= &all
->variants
[all
->active_variant
];
869 ctx
->payloads
[stage
].postfix
._shader_upper
=
870 panfrost_patch_shader_state(ctx
, s
, stage
, should_upload
) >> 4;
873 /* Go through dirty flags and actualise them in the cmdstream. */
876 panfrost_emit_for_draw(struct panfrost_context
*ctx
, bool with_vertex_data
)
878 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
879 struct panfrost_screen
*screen
= pan_screen(ctx
->base
.screen
);
881 panfrost_attach_vt_framebuffer(ctx
);
883 if (with_vertex_data
) {
884 panfrost_emit_vertex_data(batch
);
886 /* Varyings emitted for -all- geometry */
887 unsigned total_count
= ctx
->padded_count
* ctx
->instance_count
;
888 panfrost_emit_varying_descriptor(ctx
, total_count
);
891 bool msaa
= ctx
->rasterizer
->base
.multisample
;
893 if (ctx
->dirty
& PAN_DIRTY_RASTERIZER
) {
894 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].gl_enables
= ctx
->rasterizer
->tiler_gl_enables
;
896 /* TODO: Sample size */
897 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_HAS_MSAA
, msaa
);
898 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_MSAA
, !msaa
);
901 panfrost_batch_set_requirements(batch
);
903 if (ctx
->occlusion_query
) {
904 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].gl_enables
|= MALI_OCCLUSION_QUERY
| MALI_OCCLUSION_PRECISE
;
905 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].postfix
.occlusion_counter
= ctx
->occlusion_query
->transfer
.gpu
;
908 panfrost_patch_shader_state_compute(ctx
, PIPE_SHADER_VERTEX
, true);
909 panfrost_patch_shader_state_compute(ctx
, PIPE_SHADER_COMPUTE
, true);
911 if (ctx
->dirty
& (PAN_DIRTY_RASTERIZER
| PAN_DIRTY_VS
)) {
912 /* Check if we need to link the gl_PointSize varying */
913 if (!panfrost_writes_point_size(ctx
)) {
914 /* If the size is constant, write it out. Otherwise,
915 * don't touch primitive_size (since we would clobber
916 * the pointer there) */
918 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].primitive_size
.constant
= ctx
->rasterizer
->base
.line_width
;
922 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
923 if (ctx
->shader
[PIPE_SHADER_FRAGMENT
])
924 ctx
->dirty
|= PAN_DIRTY_FS
;
926 if (ctx
->dirty
& PAN_DIRTY_FS
) {
927 assert(ctx
->shader
[PIPE_SHADER_FRAGMENT
]);
928 struct panfrost_shader_state
*variant
= &ctx
->shader
[PIPE_SHADER_FRAGMENT
]->variants
[ctx
->shader
[PIPE_SHADER_FRAGMENT
]->active_variant
];
930 panfrost_patch_shader_state(ctx
, variant
, PIPE_SHADER_FRAGMENT
, false);
932 panfrost_batch_add_bo(batch
, variant
->bo
);
934 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
937 COPY(attribute_count
);
941 COPY(midgard1
.uniform_count
);
942 COPY(midgard1
.uniform_buffer_count
);
943 COPY(midgard1
.work_count
);
944 COPY(midgard1
.flags
);
945 COPY(midgard1
.unknown2
);
949 /* Get blending setup */
950 unsigned rt_count
= MAX2(ctx
->pipe_framebuffer
.nr_cbufs
, 1);
952 struct panfrost_blend_final blend
[PIPE_MAX_COLOR_BUFS
];
954 for (unsigned c
= 0; c
< rt_count
; ++c
)
955 blend
[c
] = panfrost_get_blend_for_context(ctx
, c
);
957 /* If there is a blend shader, work registers are shared. XXX: opt */
959 for (unsigned c
= 0; c
< rt_count
; ++c
) {
960 if (blend
[c
].is_shader
)
961 ctx
->fragment_shader_core
.midgard1
.work_count
= 16;
964 /* Set late due to depending on render state */
965 unsigned flags
= ctx
->fragment_shader_core
.midgard1
.flags
;
967 /* Depending on whether it's legal to in the given shader, we
968 * try to enable early-z testing (or forward-pixel kill?) */
970 if (!variant
->can_discard
)
971 flags
|= MALI_EARLY_Z
;
973 /* Any time texturing is used, derivatives are implicitly
974 * calculated, so we need to enable helper invocations */
976 if (variant
->helper_invocations
)
977 flags
|= MALI_HELPER_INVOCATIONS
;
979 ctx
->fragment_shader_core
.midgard1
.flags
= flags
;
981 /* Assign the stencil refs late */
983 unsigned front_ref
= ctx
->stencil_ref
.ref_value
[0];
984 unsigned back_ref
= ctx
->stencil_ref
.ref_value
[1];
985 bool back_enab
= ctx
->depth_stencil
->stencil
[1].enabled
;
987 ctx
->fragment_shader_core
.stencil_front
.ref
= front_ref
;
988 ctx
->fragment_shader_core
.stencil_back
.ref
= back_enab
? back_ref
: front_ref
;
990 /* CAN_DISCARD should be set if the fragment shader possibly
991 * contains a 'discard' instruction. It is likely this is
992 * related to optimizations related to forward-pixel kill, as
993 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
994 * thing?" by Peter Harris
997 if (variant
->can_discard
) {
998 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
999 ctx
->fragment_shader_core
.midgard1
.flags
|= 0x400;
1002 /* Even on MFBD, the shader descriptor gets blend shaders. It's
1003 * *also* copied to the blend_meta appended (by convention),
1004 * but this is the field actually read by the hardware. (Or
1005 * maybe both are read...?) */
1007 if (blend
[0].is_shader
) {
1008 ctx
->fragment_shader_core
.blend
.shader
=
1009 blend
[0].shader
.bo
->gpu
| blend
[0].shader
.first_tag
;
1011 ctx
->fragment_shader_core
.blend
.shader
= 0;
1014 if (screen
->require_sfbd
) {
1015 /* When only a single render target platform is used, the blend
1016 * information is inside the shader meta itself. We
1017 * additionally need to signal CAN_DISCARD for nontrivial blend
1018 * modes (so we're able to read back the destination buffer) */
1020 if (!blend
[0].is_shader
) {
1021 ctx
->fragment_shader_core
.blend
.equation
=
1022 *blend
[0].equation
.equation
;
1023 ctx
->fragment_shader_core
.blend
.constant
=
1024 blend
[0].equation
.constant
;
1027 if (!blend
[0].no_blending
) {
1028 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
1032 size_t size
= sizeof(struct mali_shader_meta
) + (sizeof(struct midgard_blend_rt
) * rt_count
);
1033 struct panfrost_transfer transfer
= panfrost_allocate_transient(batch
, size
);
1034 memcpy(transfer
.cpu
, &ctx
->fragment_shader_core
, sizeof(struct mali_shader_meta
));
1036 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].postfix
._shader_upper
= (transfer
.gpu
) >> 4;
1038 if (!screen
->require_sfbd
) {
1039 /* Additional blend descriptor tacked on for jobs using MFBD */
1041 struct midgard_blend_rt rts
[4];
1043 for (unsigned i
= 0; i
< rt_count
; ++i
) {
1044 unsigned blend_count
= 0x200;
1046 if (blend
[i
].is_shader
) {
1047 /* For a blend shader, the bottom nibble corresponds to
1048 * the number of work registers used, which signals the
1049 * -existence- of a blend shader */
1051 assert(blend
[i
].shader
.work_count
>= 2);
1052 blend_count
|= MIN2(blend
[i
].shader
.work_count
, 3);
1054 /* Otherwise, the bottom bit simply specifies if
1055 * blending (anything other than REPLACE) is enabled */
1057 if (!blend
[i
].no_blending
)
1063 (ctx
->pipe_framebuffer
.nr_cbufs
> i
) &&
1064 (ctx
->pipe_framebuffer
.cbufs
[i
]) &&
1065 util_format_is_srgb(ctx
->pipe_framebuffer
.cbufs
[i
]->format
);
1067 rts
[i
].flags
= blend_count
;
1070 rts
[i
].flags
|= MALI_BLEND_SRGB
;
1072 if (!ctx
->blend
->base
.dither
)
1073 rts
[i
].flags
|= MALI_BLEND_NO_DITHER
;
1075 /* TODO: sRGB in blend shaders is currently
1076 * unimplemented. Contact me (Alyssa) if you're
1077 * interested in working on this. We have
1078 * native Midgard ops for helping here, but
1079 * they're not well-understood yet. */
1081 assert(!(is_srgb
&& blend
[i
].is_shader
));
1083 if (blend
[i
].is_shader
) {
1084 rts
[i
].blend
.shader
= blend
[i
].shader
.bo
->gpu
| blend
[i
].shader
.first_tag
;
1086 rts
[i
].blend
.equation
= *blend
[i
].equation
.equation
;
1087 rts
[i
].blend
.constant
= blend
[i
].equation
.constant
;
1091 memcpy(transfer
.cpu
+ sizeof(struct mali_shader_meta
), rts
, sizeof(rts
[0]) * rt_count
);
1095 /* We stage to transient, so always dirty.. */
1097 panfrost_stage_attributes(ctx
);
1099 if (ctx
->dirty
& PAN_DIRTY_SAMPLERS
)
1100 panfrost_upload_sampler_descriptors(ctx
);
1102 if (ctx
->dirty
& PAN_DIRTY_TEXTURES
)
1103 panfrost_upload_texture_descriptors(ctx
);
1105 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
1107 for (int i
= 0; i
< PIPE_SHADER_TYPES
; ++i
) {
1108 struct panfrost_shader_variants
*all
= ctx
->shader
[i
];
1113 struct panfrost_constant_buffer
*buf
= &ctx
->constant_buffer
[i
];
1115 struct panfrost_shader_state
*ss
= &all
->variants
[all
->active_variant
];
1117 /* Uniforms are implicitly UBO #0 */
1118 bool has_uniforms
= buf
->enabled_mask
& (1 << 0);
1120 /* Allocate room for the sysval and the uniforms */
1121 size_t sys_size
= sizeof(float) * 4 * ss
->sysval_count
;
1122 size_t uniform_size
= has_uniforms
? (buf
->cb
[0].buffer_size
) : 0;
1123 size_t size
= sys_size
+ uniform_size
;
1124 struct panfrost_transfer transfer
= panfrost_allocate_transient(batch
, size
);
1126 /* Upload sysvals requested by the shader */
1127 panfrost_upload_sysvals(ctx
, transfer
.cpu
, ss
, i
);
1129 /* Upload uniforms */
1131 const void *cpu
= panfrost_map_constant_buffer_cpu(buf
, 0);
1132 memcpy(transfer
.cpu
+ sys_size
, cpu
, uniform_size
);
1136 ctx
->shader
[i
]->variants
[ctx
->shader
[i
]->active_variant
].uniform_count
;
1138 struct mali_vertex_tiler_postfix
*postfix
=
1139 &ctx
->payloads
[i
].postfix
;
1141 /* Next up, attach UBOs. UBO #0 is the uniforms we just
1144 unsigned ubo_count
= panfrost_ubo_count(ctx
, i
);
1145 assert(ubo_count
>= 1);
1147 size_t sz
= sizeof(struct mali_uniform_buffer_meta
) * ubo_count
;
1148 struct mali_uniform_buffer_meta ubos
[PAN_MAX_CONST_BUFFERS
];
1150 /* Upload uniforms as a UBO */
1151 ubos
[0].size
= MALI_POSITIVE((2 + uniform_count
));
1152 ubos
[0].ptr
= transfer
.gpu
>> 2;
1154 /* The rest are honest-to-goodness UBOs */
1156 for (unsigned ubo
= 1; ubo
< ubo_count
; ++ubo
) {
1157 size_t usz
= buf
->cb
[ubo
].buffer_size
;
1159 bool enabled
= buf
->enabled_mask
& (1 << ubo
);
1160 bool empty
= usz
== 0;
1162 if (!enabled
|| empty
) {
1163 /* Stub out disabled UBOs to catch accesses */
1166 ubos
[ubo
].ptr
= 0xDEAD0000;
1170 mali_ptr gpu
= panfrost_map_constant_buffer_gpu(ctx
, buf
, ubo
);
1172 unsigned bytes_per_field
= 16;
1173 unsigned aligned
= ALIGN_POT(usz
, bytes_per_field
);
1174 unsigned fields
= aligned
/ bytes_per_field
;
1176 ubos
[ubo
].size
= MALI_POSITIVE(fields
);
1177 ubos
[ubo
].ptr
= gpu
>> 2;
1180 mali_ptr ubufs
= panfrost_upload_transient(batch
, ubos
, sz
);
1181 postfix
->uniforms
= transfer
.gpu
;
1182 postfix
->uniform_buffers
= ubufs
;
1184 buf
->dirty_mask
= 0;
1187 /* TODO: Upload the viewport somewhere more appropriate */
1189 /* Clip bounds are encoded as floats. The viewport itself is encoded as
1190 * (somewhat) asymmetric ints. */
1191 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
1193 struct mali_viewport view
= {
1194 /* By default, do no viewport clipping, i.e. clip to (-inf,
1195 * inf) in each direction. Clipping to the viewport in theory
1196 * should work, but in practice causes issues when we're not
1197 * explicitly trying to scissor */
1199 .clip_minx
= -INFINITY
,
1200 .clip_miny
= -INFINITY
,
1201 .clip_maxx
= INFINITY
,
1202 .clip_maxy
= INFINITY
,
1205 /* Always scissor to the viewport by default. */
1206 float vp_minx
= (int) (vp
->translate
[0] - fabsf(vp
->scale
[0]));
1207 float vp_maxx
= (int) (vp
->translate
[0] + fabsf(vp
->scale
[0]));
1209 float vp_miny
= (int) (vp
->translate
[1] - fabsf(vp
->scale
[1]));
1210 float vp_maxy
= (int) (vp
->translate
[1] + fabsf(vp
->scale
[1]));
1212 float minz
= (vp
->translate
[2] - fabsf(vp
->scale
[2]));
1213 float maxz
= (vp
->translate
[2] + fabsf(vp
->scale
[2]));
1215 /* Apply the scissor test */
1217 unsigned minx
, miny
, maxx
, maxy
;
1219 if (ss
&& ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
) {
1220 minx
= MAX2(ss
->minx
, vp_minx
);
1221 miny
= MAX2(ss
->miny
, vp_miny
);
1222 maxx
= MIN2(ss
->maxx
, vp_maxx
);
1223 maxy
= MIN2(ss
->maxy
, vp_maxy
);
1231 /* Hardware needs the min/max to be strictly ordered, so flip if we
1232 * need to. The viewport transformation in the vertex shader will
1233 * handle the negatives if we don't */
1236 unsigned temp
= miny
;
1242 unsigned temp
= minx
;
1253 /* Clamp to the framebuffer size as a last check */
1255 minx
= MIN2(ctx
->pipe_framebuffer
.width
, minx
);
1256 maxx
= MIN2(ctx
->pipe_framebuffer
.width
, maxx
);
1258 miny
= MIN2(ctx
->pipe_framebuffer
.height
, miny
);
1259 maxy
= MIN2(ctx
->pipe_framebuffer
.height
, maxy
);
1261 /* Update the job, unless we're doing wallpapering (whose lack of
1262 * scissor we can ignore, since if we "miss" a tile of wallpaper, it'll
1263 * just... be faster :) */
1265 if (!ctx
->wallpaper_batch
)
1266 panfrost_batch_union_scissor(batch
, minx
, miny
, maxx
, maxy
);
1270 view
.viewport0
[0] = minx
;
1271 view
.viewport1
[0] = MALI_POSITIVE(maxx
);
1273 view
.viewport0
[1] = miny
;
1274 view
.viewport1
[1] = MALI_POSITIVE(maxy
);
1276 view
.clip_minz
= minz
;
1277 view
.clip_maxz
= maxz
;
1279 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].postfix
.viewport
=
1280 panfrost_upload_transient(batch
,
1282 sizeof(struct mali_viewport
));
1287 /* Corresponds to exactly one draw, but does not submit anything */
1290 panfrost_queue_draw(struct panfrost_context
*ctx
)
1292 /* Handle dirty flags now */
1293 panfrost_emit_for_draw(ctx
, true);
1295 /* If rasterizer discard is enable, only submit the vertex */
1297 bool rasterizer_discard
= ctx
->rasterizer
1298 && ctx
->rasterizer
->base
.rasterizer_discard
;
1300 struct panfrost_transfer vertex
= panfrost_vertex_tiler_job(ctx
, false);
1301 struct panfrost_transfer tiler
;
1303 if (!rasterizer_discard
)
1304 tiler
= panfrost_vertex_tiler_job(ctx
, true);
1306 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
1308 if (rasterizer_discard
)
1309 panfrost_scoreboard_queue_vertex_job(batch
, vertex
, FALSE
);
1310 else if (ctx
->wallpaper_batch
)
1311 panfrost_scoreboard_queue_fused_job_prepend(batch
, vertex
, tiler
);
1313 panfrost_scoreboard_queue_fused_job(batch
, vertex
, tiler
);
1316 /* The entire frame is in memory -- send it off to the kernel! */
1319 panfrost_submit_frame(struct panfrost_context
*ctx
, bool flush_immediate
,
1320 struct pipe_fence_handle
**fence
,
1321 struct panfrost_batch
*batch
)
1323 panfrost_batch_submit(batch
);
1325 /* If visual, we can stall a frame */
1327 if (!flush_immediate
)
1328 panfrost_drm_force_flush_fragment(ctx
, fence
);
1330 ctx
->last_fragment_flushed
= false;
1331 ctx
->last_batch
= batch
;
1333 /* If readback, flush now (hurts the pipelined performance) */
1334 if (flush_immediate
)
1335 panfrost_drm_force_flush_fragment(ctx
, fence
);
1339 panfrost_draw_wallpaper(struct pipe_context
*pipe
)
1341 struct panfrost_context
*ctx
= pan_context(pipe
);
1343 /* Nothing to reload? TODO: MRT wallpapers */
1344 if (ctx
->pipe_framebuffer
.cbufs
[0] == NULL
)
1347 /* Check if the buffer has any content on it worth preserving */
1349 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[0];
1350 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
1351 unsigned level
= surf
->u
.tex
.level
;
1353 if (!rsrc
->slices
[level
].initialized
)
1356 /* Save the batch */
1357 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
1359 ctx
->wallpaper_batch
= batch
;
1361 /* Clamp the rendering area to the damage extent. The
1362 * KHR_partial_update() spec states that trying to render outside of
1363 * the damage region is "undefined behavior", so we should be safe.
1365 unsigned damage_width
= (rsrc
->damage
.extent
.maxx
- rsrc
->damage
.extent
.minx
);
1366 unsigned damage_height
= (rsrc
->damage
.extent
.maxy
- rsrc
->damage
.extent
.miny
);
1368 if (damage_width
&& damage_height
) {
1369 panfrost_batch_intersection_scissor(batch
,
1370 rsrc
->damage
.extent
.minx
,
1371 rsrc
->damage
.extent
.miny
,
1372 rsrc
->damage
.extent
.maxx
,
1373 rsrc
->damage
.extent
.maxy
);
1376 /* FIXME: Looks like aligning on a tile is not enough, but
1377 * aligning on twice the tile size seems to works. We don't
1378 * know exactly what happens here but this deserves extra
1379 * investigation to figure it out.
1381 batch
->minx
= batch
->minx
& ~((MALI_TILE_LENGTH
* 2) - 1);
1382 batch
->miny
= batch
->miny
& ~((MALI_TILE_LENGTH
* 2) - 1);
1383 batch
->maxx
= MIN2(ALIGN_POT(batch
->maxx
, MALI_TILE_LENGTH
* 2),
1385 batch
->maxy
= MIN2(ALIGN_POT(batch
->maxy
, MALI_TILE_LENGTH
* 2),
1386 rsrc
->base
.height0
);
1388 struct pipe_scissor_state damage
;
1389 struct pipe_box rects
[4];
1391 /* Clamp the damage box to the rendering area. */
1392 damage
.minx
= MAX2(batch
->minx
, rsrc
->damage
.biggest_rect
.x
);
1393 damage
.miny
= MAX2(batch
->miny
, rsrc
->damage
.biggest_rect
.y
);
1394 damage
.maxx
= MIN2(batch
->maxx
,
1395 rsrc
->damage
.biggest_rect
.x
+
1396 rsrc
->damage
.biggest_rect
.width
);
1397 damage
.maxy
= MIN2(batch
->maxy
,
1398 rsrc
->damage
.biggest_rect
.y
+
1399 rsrc
->damage
.biggest_rect
.height
);
1401 /* One damage rectangle means we can end up with at most 4 reload
1403 * 1: left region, only exists if damage.x > 0
1404 * 2: right region, only exists if damage.x + damage.width < fb->width
1405 * 3: top region, only exists if damage.y > 0. The intersection with
1406 * the left and right regions are dropped
1407 * 4: bottom region, only exists if damage.y + damage.height < fb->height.
1408 * The intersection with the left and right regions are dropped
1410 * ____________________________
1417 * |_______|___________|______|
1419 u_box_2d(batch
->minx
, batch
->miny
, damage
.minx
- batch
->minx
,
1420 batch
->maxy
- batch
->miny
, &rects
[0]);
1421 u_box_2d(damage
.maxx
, batch
->miny
, batch
->maxx
- damage
.maxx
,
1422 batch
->maxy
- batch
->miny
, &rects
[1]);
1423 u_box_2d(damage
.minx
, batch
->miny
, damage
.maxx
- damage
.minx
,
1424 damage
.miny
- batch
->miny
, &rects
[2]);
1425 u_box_2d(damage
.minx
, damage
.maxy
, damage
.maxx
- damage
.minx
,
1426 batch
->maxy
- damage
.maxy
, &rects
[3]);
1428 for (unsigned i
= 0; i
< 4; i
++) {
1429 /* Width and height are always >= 0 even if width is declared as a
1430 * signed integer: u_box_2d() helper takes unsigned args and
1431 * panfrost_set_damage_region() is taking care of clamping
1434 if (!rects
[i
].width
|| !rects
[i
].height
)
1437 /* Blit the wallpaper in */
1438 panfrost_blit_wallpaper(ctx
, &rects
[i
]);
1440 ctx
->wallpaper_batch
= NULL
;
1445 struct pipe_context
*pipe
,
1446 struct pipe_fence_handle
**fence
,
1449 struct panfrost_context
*ctx
= pan_context(pipe
);
1450 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
1452 /* Nothing to do! */
1453 if (!batch
->last_job
.gpu
&& !batch
->clear
) return;
1455 if (!batch
->clear
&& batch
->last_tiler
.gpu
)
1456 panfrost_draw_wallpaper(&ctx
->base
);
1458 /* Whether to stall the pipeline for immediately correct results. Since
1459 * pipelined rendering is quite broken right now (to be fixed by the
1460 * panfrost_job refactor, just take the perf hit for correctness) */
1461 bool flush_immediate
= /*flags & PIPE_FLUSH_END_OF_FRAME*/true;
1463 /* Submit the frame itself */
1464 panfrost_submit_frame(ctx
, flush_immediate
, fence
, batch
);
1466 /* Prepare for the next frame */
1467 panfrost_invalidate_frame(ctx
);
1470 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1473 g2m_draw_mode(enum pipe_prim_type mode
)
1476 DEFINE_CASE(POINTS
);
1478 DEFINE_CASE(LINE_LOOP
);
1479 DEFINE_CASE(LINE_STRIP
);
1480 DEFINE_CASE(TRIANGLES
);
1481 DEFINE_CASE(TRIANGLE_STRIP
);
1482 DEFINE_CASE(TRIANGLE_FAN
);
1484 DEFINE_CASE(QUAD_STRIP
);
1485 DEFINE_CASE(POLYGON
);
1488 unreachable("Invalid draw mode");
1495 panfrost_translate_index_size(unsigned size
)
1499 return MALI_DRAW_INDEXED_UINT8
;
1502 return MALI_DRAW_INDEXED_UINT16
;
1505 return MALI_DRAW_INDEXED_UINT32
;
1508 unreachable("Invalid index size");
1512 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1513 * good for the duration of the draw (transient), could last longer */
1516 panfrost_get_index_buffer_mapped(struct panfrost_context
*ctx
, const struct pipe_draw_info
*info
)
1518 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (info
->index
.resource
);
1520 off_t offset
= info
->start
* info
->index_size
;
1521 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
1523 if (!info
->has_user_indices
) {
1524 /* Only resources can be directly mapped */
1525 panfrost_batch_add_bo(batch
, rsrc
->bo
);
1526 return rsrc
->bo
->gpu
+ offset
;
1528 /* Otherwise, we need to upload to transient memory */
1529 const uint8_t *ibuf8
= (const uint8_t *) info
->index
.user
;
1530 return panfrost_upload_transient(batch
, ibuf8
+ offset
, info
->count
* info
->index_size
);
1535 panfrost_scissor_culls_everything(struct panfrost_context
*ctx
)
1537 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
1539 /* Check if we're scissoring at all */
1541 if (!(ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
))
1544 return (ss
->minx
== ss
->maxx
) || (ss
->miny
== ss
->maxy
);
1547 /* Count generated primitives (when there is no geom/tess shaders) for
1548 * transform feedback */
1551 panfrost_statistics_record(
1552 struct panfrost_context
*ctx
,
1553 const struct pipe_draw_info
*info
)
1555 if (!ctx
->active_queries
)
1558 uint32_t prims
= u_prims_for_vertices(info
->mode
, info
->count
);
1559 ctx
->prims_generated
+= prims
;
1561 if (!ctx
->streamout
.num_targets
)
1564 ctx
->tf_prims_generated
+= prims
;
1569 struct pipe_context
*pipe
,
1570 const struct pipe_draw_info
*info
)
1572 struct panfrost_context
*ctx
= pan_context(pipe
);
1574 /* First of all, check the scissor to see if anything is drawn at all.
1575 * If it's not, we drop the draw (mostly a conformance issue;
1576 * well-behaved apps shouldn't hit this) */
1578 if (panfrost_scissor_culls_everything(ctx
))
1581 ctx
->payloads
[PIPE_SHADER_VERTEX
].offset_start
= info
->start
;
1582 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].offset_start
= info
->start
;
1584 int mode
= info
->mode
;
1586 /* Fallback unsupported restart index */
1587 unsigned primitive_index
= (1 << (info
->index_size
* 8)) - 1;
1589 if (info
->primitive_restart
&& info
->index_size
1590 && info
->restart_index
!= primitive_index
) {
1591 util_draw_vbo_without_prim_restart(pipe
, info
);
1595 /* Fallback for unsupported modes */
1597 assert(ctx
->rasterizer
!= NULL
);
1599 if (!(ctx
->draw_modes
& (1 << mode
))) {
1600 if (mode
== PIPE_PRIM_QUADS
&& info
->count
== 4 && !ctx
->rasterizer
->base
.flatshade
) {
1601 mode
= PIPE_PRIM_TRIANGLE_FAN
;
1603 if (info
->count
< 4) {
1604 /* Degenerate case? */
1608 util_primconvert_save_rasterizer_state(ctx
->primconvert
, &ctx
->rasterizer
->base
);
1609 util_primconvert_draw_vbo(ctx
->primconvert
, info
);
1614 /* Now that we have a guaranteed terminating path, find the job.
1615 * Assignment commented out to prevent unused warning */
1617 /* struct panfrost_batch *batch = */ panfrost_get_batch_for_fbo(ctx
);
1619 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.draw_mode
= g2m_draw_mode(mode
);
1621 /* Take into account a negative bias */
1622 ctx
->vertex_count
= info
->count
+ abs(info
->index_bias
);
1623 ctx
->instance_count
= info
->instance_count
;
1624 ctx
->active_prim
= info
->mode
;
1626 /* For non-indexed draws, they're the same */
1627 unsigned vertex_count
= ctx
->vertex_count
;
1629 unsigned draw_flags
= 0;
1631 /* The draw flags interpret how primitive size is interpreted */
1633 if (panfrost_writes_point_size(ctx
))
1634 draw_flags
|= MALI_DRAW_VARYING_SIZE
;
1636 if (info
->primitive_restart
)
1637 draw_flags
|= MALI_DRAW_PRIMITIVE_RESTART_FIXED_INDEX
;
1639 /* For higher amounts of vertices (greater than what fits in a 16-bit
1640 * short), the other value is needed, otherwise there will be bizarre
1641 * rendering artefacts. It's not clear what these values mean yet. This
1642 * change is also needed for instancing and sometimes points (perhaps
1643 * related to dynamically setting gl_PointSize) */
1645 bool is_points
= mode
== PIPE_PRIM_POINTS
;
1646 bool many_verts
= ctx
->vertex_count
> 0xFFFF;
1647 bool instanced
= ctx
->instance_count
> 1;
1649 draw_flags
|= (is_points
|| many_verts
|| instanced
) ? 0x3000 : 0x18000;
1651 /* This doesn't make much sense */
1652 if (mode
== PIPE_PRIM_LINE_STRIP
) {
1653 draw_flags
|= 0x800;
1656 panfrost_statistics_record(ctx
, info
);
1658 if (info
->index_size
) {
1659 /* Calculate the min/max index used so we can figure out how
1660 * many times to invoke the vertex shader */
1662 /* Fetch / calculate index bounds */
1663 unsigned min_index
= 0, max_index
= 0;
1665 if (info
->max_index
== ~0u) {
1666 u_vbuf_get_minmax_index(pipe
, info
, &min_index
, &max_index
);
1668 min_index
= info
->min_index
;
1669 max_index
= info
->max_index
;
1672 /* Use the corresponding values */
1673 vertex_count
= max_index
- min_index
+ 1;
1674 ctx
->payloads
[PIPE_SHADER_VERTEX
].offset_start
= min_index
+ info
->index_bias
;
1675 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].offset_start
= min_index
+ info
->index_bias
;
1677 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.offset_bias_correction
= -min_index
;
1678 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.index_count
= MALI_POSITIVE(info
->count
);
1680 //assert(!info->restart_index); /* TODO: Research */
1682 draw_flags
|= panfrost_translate_index_size(info
->index_size
);
1683 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.indices
= panfrost_get_index_buffer_mapped(ctx
, info
);
1685 /* Index count == vertex count, if no indexing is applied, as
1686 * if it is internally indexed in the expected order */
1688 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.offset_bias_correction
= 0;
1689 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.index_count
= MALI_POSITIVE(ctx
->vertex_count
);
1691 /* Reverse index state */
1692 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.indices
= (u64
) NULL
;
1695 /* Dispatch "compute jobs" for the vertex/tiler pair as (1,
1696 * vertex_count, 1) */
1698 panfrost_pack_work_groups_fused(
1699 &ctx
->payloads
[PIPE_SHADER_VERTEX
].prefix
,
1700 &ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
,
1701 1, vertex_count
, info
->instance_count
,
1704 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.unknown_draw
= draw_flags
;
1706 /* Encode the padded vertex count */
1708 if (info
->instance_count
> 1) {
1709 /* Triangles have non-even vertex counts so they change how
1710 * padding works internally */
1713 mode
== PIPE_PRIM_TRIANGLES
||
1714 mode
== PIPE_PRIM_TRIANGLE_STRIP
||
1715 mode
== PIPE_PRIM_TRIANGLE_FAN
;
1717 struct pan_shift_odd so
=
1718 panfrost_padded_vertex_count(vertex_count
, !is_triangle
);
1720 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_shift
= so
.shift
;
1721 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_shift
= so
.shift
;
1723 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_odd
= so
.odd
;
1724 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_odd
= so
.odd
;
1726 ctx
->padded_count
= pan_expand_shift_odd(so
);
1728 ctx
->padded_count
= ctx
->vertex_count
;
1730 /* Reset instancing state */
1731 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_shift
= 0;
1732 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_odd
= 0;
1733 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_shift
= 0;
1734 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_odd
= 0;
1737 /* Fire off the draw itself */
1738 panfrost_queue_draw(ctx
);
1740 /* Increment transform feedback offsets */
1742 for (unsigned i
= 0; i
< ctx
->streamout
.num_targets
; ++i
) {
1743 unsigned output_count
= u_stream_outputs_for_vertices(
1744 ctx
->active_prim
, ctx
->vertex_count
);
1746 ctx
->streamout
.offsets
[i
] += output_count
;
1753 panfrost_generic_cso_delete(struct pipe_context
*pctx
, void *hwcso
)
1759 panfrost_create_rasterizer_state(
1760 struct pipe_context
*pctx
,
1761 const struct pipe_rasterizer_state
*cso
)
1763 struct panfrost_rasterizer
*so
= CALLOC_STRUCT(panfrost_rasterizer
);
1767 /* Bitmask, unknown meaning of the start value. 0x105 on 32-bit T6XX */
1768 so
->tiler_gl_enables
= 0x7;
1771 so
->tiler_gl_enables
|= MALI_FRONT_CCW_TOP
;
1773 if (cso
->cull_face
& PIPE_FACE_FRONT
)
1774 so
->tiler_gl_enables
|= MALI_CULL_FACE_FRONT
;
1776 if (cso
->cull_face
& PIPE_FACE_BACK
)
1777 so
->tiler_gl_enables
|= MALI_CULL_FACE_BACK
;
1783 panfrost_bind_rasterizer_state(
1784 struct pipe_context
*pctx
,
1787 struct panfrost_context
*ctx
= pan_context(pctx
);
1789 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1793 ctx
->rasterizer
= hwcso
;
1794 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
1796 ctx
->fragment_shader_core
.depth_units
= ctx
->rasterizer
->base
.offset_units
;
1797 ctx
->fragment_shader_core
.depth_factor
= ctx
->rasterizer
->base
.offset_scale
;
1799 /* Gauranteed with the core GL call, so don't expose ARB_polygon_offset */
1800 assert(ctx
->rasterizer
->base
.offset_clamp
== 0.0);
1802 /* XXX: Which bit is which? Does this maybe allow offseting not-tri? */
1804 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_DEPTH_RANGE_A
, ctx
->rasterizer
->base
.offset_tri
);
1805 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_DEPTH_RANGE_B
, ctx
->rasterizer
->base
.offset_tri
);
1807 /* Point sprites are emulated */
1809 struct panfrost_shader_state
*variant
=
1810 ctx
->shader
[PIPE_SHADER_FRAGMENT
] ? &ctx
->shader
[PIPE_SHADER_FRAGMENT
]->variants
[ctx
->shader
[PIPE_SHADER_FRAGMENT
]->active_variant
] : NULL
;
1812 if (ctx
->rasterizer
->base
.sprite_coord_enable
|| (variant
&& variant
->point_sprite_mask
))
1813 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->shader
[PIPE_SHADER_FRAGMENT
]);
1817 panfrost_create_vertex_elements_state(
1818 struct pipe_context
*pctx
,
1819 unsigned num_elements
,
1820 const struct pipe_vertex_element
*elements
)
1822 struct panfrost_vertex_state
*so
= CALLOC_STRUCT(panfrost_vertex_state
);
1824 so
->num_elements
= num_elements
;
1825 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
1827 for (int i
= 0; i
< num_elements
; ++i
) {
1828 so
->hw
[i
].index
= i
;
1830 enum pipe_format fmt
= elements
[i
].src_format
;
1831 const struct util_format_description
*desc
= util_format_description(fmt
);
1832 so
->hw
[i
].unknown1
= 0x2;
1833 so
->hw
[i
].swizzle
= panfrost_get_default_swizzle(desc
->nr_channels
);
1835 so
->hw
[i
].format
= panfrost_find_format(desc
);
1837 /* The field itself should probably be shifted over */
1838 so
->hw
[i
].src_offset
= elements
[i
].src_offset
;
1845 panfrost_bind_vertex_elements_state(
1846 struct pipe_context
*pctx
,
1849 struct panfrost_context
*ctx
= pan_context(pctx
);
1851 ctx
->vertex
= hwcso
;
1852 ctx
->dirty
|= PAN_DIRTY_VERTEX
;
1856 panfrost_create_shader_state(
1857 struct pipe_context
*pctx
,
1858 const struct pipe_shader_state
*cso
)
1860 struct panfrost_shader_variants
*so
= CALLOC_STRUCT(panfrost_shader_variants
);
1863 /* Token deep copy to prevent memory corruption */
1865 if (cso
->type
== PIPE_SHADER_IR_TGSI
)
1866 so
->base
.tokens
= tgsi_dup_tokens(so
->base
.tokens
);
1872 panfrost_delete_shader_state(
1873 struct pipe_context
*pctx
,
1876 struct panfrost_shader_variants
*cso
= (struct panfrost_shader_variants
*) so
;
1878 if (cso
->base
.type
== PIPE_SHADER_IR_TGSI
) {
1879 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1882 for (unsigned i
= 0; i
< cso
->variant_count
; ++i
) {
1883 struct panfrost_shader_state
*shader_state
= &cso
->variants
[i
];
1884 panfrost_bo_unreference(pctx
->screen
, shader_state
->bo
);
1885 shader_state
->bo
= NULL
;
1892 panfrost_create_sampler_state(
1893 struct pipe_context
*pctx
,
1894 const struct pipe_sampler_state
*cso
)
1896 struct panfrost_sampler_state
*so
= CALLOC_STRUCT(panfrost_sampler_state
);
1899 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1901 bool min_nearest
= cso
->min_img_filter
== PIPE_TEX_FILTER_NEAREST
;
1902 bool mag_nearest
= cso
->mag_img_filter
== PIPE_TEX_FILTER_NEAREST
;
1903 bool mip_linear
= cso
->min_mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
;
1905 unsigned min_filter
= min_nearest
? MALI_SAMP_MIN_NEAREST
: 0;
1906 unsigned mag_filter
= mag_nearest
? MALI_SAMP_MAG_NEAREST
: 0;
1907 unsigned mip_filter
= mip_linear
?
1908 (MALI_SAMP_MIP_LINEAR_1
| MALI_SAMP_MIP_LINEAR_2
) : 0;
1909 unsigned normalized
= cso
->normalized_coords
? MALI_SAMP_NORM_COORDS
: 0;
1911 struct mali_sampler_descriptor sampler_descriptor
= {
1912 .filter_mode
= min_filter
| mag_filter
| mip_filter
| normalized
,
1913 .wrap_s
= translate_tex_wrap(cso
->wrap_s
),
1914 .wrap_t
= translate_tex_wrap(cso
->wrap_t
),
1915 .wrap_r
= translate_tex_wrap(cso
->wrap_r
),
1916 .compare_func
= panfrost_translate_alt_compare_func(cso
->compare_func
),
1918 cso
->border_color
.f
[0],
1919 cso
->border_color
.f
[1],
1920 cso
->border_color
.f
[2],
1921 cso
->border_color
.f
[3]
1923 .min_lod
= FIXED_16(cso
->min_lod
),
1924 .max_lod
= FIXED_16(cso
->max_lod
),
1925 .seamless_cube_map
= cso
->seamless_cube_map
,
1928 /* If necessary, we disable mipmapping in the sampler descriptor by
1929 * clamping the LOD as tight as possible (from 0 to epsilon,
1930 * essentially -- remember these are fixed point numbers, so
1933 if (cso
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
)
1934 sampler_descriptor
.max_lod
= sampler_descriptor
.min_lod
;
1936 /* Enforce that there is something in the middle by adding epsilon*/
1938 if (sampler_descriptor
.min_lod
== sampler_descriptor
.max_lod
)
1939 sampler_descriptor
.max_lod
++;
1942 assert(sampler_descriptor
.max_lod
> sampler_descriptor
.min_lod
);
1944 so
->hw
= sampler_descriptor
;
1950 panfrost_bind_sampler_states(
1951 struct pipe_context
*pctx
,
1952 enum pipe_shader_type shader
,
1953 unsigned start_slot
, unsigned num_sampler
,
1956 assert(start_slot
== 0);
1958 struct panfrost_context
*ctx
= pan_context(pctx
);
1960 /* XXX: Should upload, not just copy? */
1961 ctx
->sampler_count
[shader
] = num_sampler
;
1962 memcpy(ctx
->samplers
[shader
], sampler
, num_sampler
* sizeof (void *));
1964 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
;
1968 panfrost_variant_matches(
1969 struct panfrost_context
*ctx
,
1970 struct panfrost_shader_state
*variant
,
1971 enum pipe_shader_type type
)
1973 struct pipe_rasterizer_state
*rasterizer
= &ctx
->rasterizer
->base
;
1974 struct pipe_alpha_state
*alpha
= &ctx
->depth_stencil
->alpha
;
1976 bool is_fragment
= (type
== PIPE_SHADER_FRAGMENT
);
1978 if (is_fragment
&& (alpha
->enabled
|| variant
->alpha_state
.enabled
)) {
1979 /* Make sure enable state is at least the same */
1980 if (alpha
->enabled
!= variant
->alpha_state
.enabled
) {
1984 /* Check that the contents of the test are the same */
1985 bool same_func
= alpha
->func
== variant
->alpha_state
.func
;
1986 bool same_ref
= alpha
->ref_value
== variant
->alpha_state
.ref_value
;
1988 if (!(same_func
&& same_ref
)) {
1993 if (is_fragment
&& rasterizer
&& (rasterizer
->sprite_coord_enable
|
1994 variant
->point_sprite_mask
)) {
1995 /* Ensure the same varyings are turned to point sprites */
1996 if (rasterizer
->sprite_coord_enable
!= variant
->point_sprite_mask
)
1999 /* Ensure the orientation is correct */
2001 rasterizer
->sprite_coord_mode
==
2002 PIPE_SPRITE_COORD_UPPER_LEFT
;
2004 if (variant
->point_sprite_upper_left
!= upper_left
)
2008 /* Otherwise, we're good to go */
2013 * Fix an uncompiled shader's stream output info, and produce a bitmask
2014 * of which VARYING_SLOT_* are captured for stream output.
2016 * Core Gallium stores output->register_index as a "slot" number, where
2017 * slots are assigned consecutively to all outputs in info->outputs_written.
2018 * This naive packing of outputs doesn't work for us - we too have slots,
2019 * but the layout is defined by the VUE map, which we won't have until we
2020 * compile a specific shader variant. So, we remap these and simply store
2021 * VARYING_SLOT_* in our copy's output->register_index fields.
2023 * We then produce a bitmask of outputs which are used for SO.
2025 * Implementation from iris.
2029 update_so_info(struct pipe_stream_output_info
*so_info
,
2030 uint64_t outputs_written
)
2032 uint64_t so_outputs
= 0;
2033 uint8_t reverse_map
[64] = {};
2036 while (outputs_written
)
2037 reverse_map
[slot
++] = u_bit_scan64(&outputs_written
);
2039 for (unsigned i
= 0; i
< so_info
->num_outputs
; i
++) {
2040 struct pipe_stream_output
*output
= &so_info
->output
[i
];
2042 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
2043 output
->register_index
= reverse_map
[output
->register_index
];
2045 so_outputs
|= 1ull << output
->register_index
;
2052 panfrost_bind_shader_state(
2053 struct pipe_context
*pctx
,
2055 enum pipe_shader_type type
)
2057 struct panfrost_context
*ctx
= pan_context(pctx
);
2059 ctx
->shader
[type
] = hwcso
;
2061 if (type
== PIPE_SHADER_FRAGMENT
)
2062 ctx
->dirty
|= PAN_DIRTY_FS
;
2064 ctx
->dirty
|= PAN_DIRTY_VS
;
2068 /* Match the appropriate variant */
2070 signed variant
= -1;
2071 struct panfrost_shader_variants
*variants
= (struct panfrost_shader_variants
*) hwcso
;
2073 for (unsigned i
= 0; i
< variants
->variant_count
; ++i
) {
2074 if (panfrost_variant_matches(ctx
, &variants
->variants
[i
], type
)) {
2080 if (variant
== -1) {
2081 /* No variant matched, so create a new one */
2082 variant
= variants
->variant_count
++;
2083 assert(variants
->variant_count
< MAX_SHADER_VARIANTS
);
2085 struct panfrost_shader_state
*v
=
2086 &variants
->variants
[variant
];
2088 if (type
== PIPE_SHADER_FRAGMENT
) {
2089 v
->alpha_state
= ctx
->depth_stencil
->alpha
;
2091 if (ctx
->rasterizer
) {
2092 v
->point_sprite_mask
= ctx
->rasterizer
->base
.sprite_coord_enable
;
2093 v
->point_sprite_upper_left
=
2094 ctx
->rasterizer
->base
.sprite_coord_mode
==
2095 PIPE_SPRITE_COORD_UPPER_LEFT
;
2099 variants
->variants
[variant
].tripipe
= calloc(1, sizeof(struct mali_shader_meta
));
2103 /* Select this variant */
2104 variants
->active_variant
= variant
;
2106 struct panfrost_shader_state
*shader_state
= &variants
->variants
[variant
];
2107 assert(panfrost_variant_matches(ctx
, shader_state
, type
));
2109 /* We finally have a variant, so compile it */
2111 if (!shader_state
->compiled
) {
2112 uint64_t outputs_written
= 0;
2114 panfrost_shader_compile(ctx
, shader_state
->tripipe
,
2115 variants
->base
.type
,
2116 variants
->base
.type
== PIPE_SHADER_IR_NIR
?
2117 variants
->base
.ir
.nir
:
2118 variants
->base
.tokens
,
2119 tgsi_processor_to_shader_stage(type
), shader_state
,
2122 shader_state
->compiled
= true;
2124 /* Fixup the stream out information, since what Gallium returns
2125 * normally is mildly insane */
2127 shader_state
->stream_output
= variants
->base
.stream_output
;
2128 shader_state
->so_mask
=
2129 update_so_info(&shader_state
->stream_output
, outputs_written
);
2134 panfrost_bind_vs_state(struct pipe_context
*pctx
, void *hwcso
)
2136 panfrost_bind_shader_state(pctx
, hwcso
, PIPE_SHADER_VERTEX
);
2140 panfrost_bind_fs_state(struct pipe_context
*pctx
, void *hwcso
)
2142 panfrost_bind_shader_state(pctx
, hwcso
, PIPE_SHADER_FRAGMENT
);
2146 panfrost_set_vertex_buffers(
2147 struct pipe_context
*pctx
,
2148 unsigned start_slot
,
2149 unsigned num_buffers
,
2150 const struct pipe_vertex_buffer
*buffers
)
2152 struct panfrost_context
*ctx
= pan_context(pctx
);
2154 util_set_vertex_buffers_mask(ctx
->vertex_buffers
, &ctx
->vb_mask
, buffers
, start_slot
, num_buffers
);
2158 panfrost_set_constant_buffer(
2159 struct pipe_context
*pctx
,
2160 enum pipe_shader_type shader
, uint index
,
2161 const struct pipe_constant_buffer
*buf
)
2163 struct panfrost_context
*ctx
= pan_context(pctx
);
2164 struct panfrost_constant_buffer
*pbuf
= &ctx
->constant_buffer
[shader
];
2166 util_copy_constant_buffer(&pbuf
->cb
[index
], buf
);
2168 unsigned mask
= (1 << index
);
2170 if (unlikely(!buf
)) {
2171 pbuf
->enabled_mask
&= ~mask
;
2172 pbuf
->dirty_mask
&= ~mask
;
2176 pbuf
->enabled_mask
|= mask
;
2177 pbuf
->dirty_mask
|= mask
;
2181 panfrost_set_stencil_ref(
2182 struct pipe_context
*pctx
,
2183 const struct pipe_stencil_ref
*ref
)
2185 struct panfrost_context
*ctx
= pan_context(pctx
);
2186 ctx
->stencil_ref
= *ref
;
2188 /* Shader core dirty */
2189 ctx
->dirty
|= PAN_DIRTY_FS
;
2192 static enum mali_texture_type
2193 panfrost_translate_texture_type(enum pipe_texture_target t
) {
2197 case PIPE_TEXTURE_1D
:
2198 case PIPE_TEXTURE_1D_ARRAY
:
2201 case PIPE_TEXTURE_2D
:
2202 case PIPE_TEXTURE_2D_ARRAY
:
2203 case PIPE_TEXTURE_RECT
:
2206 case PIPE_TEXTURE_3D
:
2209 case PIPE_TEXTURE_CUBE
:
2210 case PIPE_TEXTURE_CUBE_ARRAY
:
2211 return MALI_TEX_CUBE
;
2214 unreachable("Unknown target");
2218 static struct pipe_sampler_view
*
2219 panfrost_create_sampler_view(
2220 struct pipe_context
*pctx
,
2221 struct pipe_resource
*texture
,
2222 const struct pipe_sampler_view
*template)
2224 struct panfrost_sampler_view
*so
= rzalloc(pctx
, struct panfrost_sampler_view
);
2225 int bytes_per_pixel
= util_format_get_blocksize(texture
->format
);
2227 pipe_reference(NULL
, &texture
->reference
);
2229 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) texture
;
2232 so
->base
= *template;
2233 so
->base
.texture
= texture
;
2234 so
->base
.reference
.count
= 1;
2235 so
->base
.context
= pctx
;
2237 /* sampler_views correspond to texture descriptors, minus the texture
2238 * (data) itself. So, we serialise the descriptor here and cache it for
2241 const struct util_format_description
*desc
= util_format_description(prsrc
->base
.format
);
2243 unsigned char user_swizzle
[4] = {
2244 template->swizzle_r
,
2245 template->swizzle_g
,
2246 template->swizzle_b
,
2250 enum mali_format format
= panfrost_find_format(desc
);
2252 /* Check if we need to set a custom stride by computing the "expected"
2253 * stride and comparing it to what the BO actually wants. Only applies
2254 * to linear textures, since tiled/compressed textures have strict
2255 * alignment requirements for their strides as it is */
2257 unsigned first_level
= template->u
.tex
.first_level
;
2258 unsigned last_level
= template->u
.tex
.last_level
;
2260 if (prsrc
->layout
== PAN_LINEAR
) {
2261 for (unsigned l
= first_level
; l
<= last_level
; ++l
) {
2262 unsigned actual_stride
= prsrc
->slices
[l
].stride
;
2263 unsigned width
= u_minify(texture
->width0
, l
);
2264 unsigned comp_stride
= width
* bytes_per_pixel
;
2266 if (comp_stride
!= actual_stride
) {
2267 so
->manual_stride
= true;
2273 /* In the hardware, array_size refers specifically to array textures,
2274 * whereas in Gallium, it also covers cubemaps */
2276 unsigned array_size
= texture
->array_size
;
2278 if (template->target
== PIPE_TEXTURE_CUBE
) {
2279 /* TODO: Cubemap arrays */
2280 assert(array_size
== 6);
2284 struct mali_texture_descriptor texture_descriptor
= {
2285 .width
= MALI_POSITIVE(u_minify(texture
->width0
, first_level
)),
2286 .height
= MALI_POSITIVE(u_minify(texture
->height0
, first_level
)),
2287 .depth
= MALI_POSITIVE(u_minify(texture
->depth0
, first_level
)),
2288 .array_size
= MALI_POSITIVE(array_size
),
2291 .swizzle
= panfrost_translate_swizzle_4(desc
->swizzle
),
2293 .srgb
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
,
2294 .type
= panfrost_translate_texture_type(template->target
),
2298 .swizzle
= panfrost_translate_swizzle_4(user_swizzle
)
2301 texture_descriptor
.levels
= last_level
- first_level
;
2303 so
->hw
= texture_descriptor
;
2305 return (struct pipe_sampler_view
*) so
;
2309 panfrost_set_sampler_views(
2310 struct pipe_context
*pctx
,
2311 enum pipe_shader_type shader
,
2312 unsigned start_slot
, unsigned num_views
,
2313 struct pipe_sampler_view
**views
)
2315 struct panfrost_context
*ctx
= pan_context(pctx
);
2317 assert(start_slot
== 0);
2319 unsigned new_nr
= 0;
2320 for (unsigned i
= 0; i
< num_views
; ++i
) {
2325 ctx
->sampler_view_count
[shader
] = new_nr
;
2326 memcpy(ctx
->sampler_views
[shader
], views
, num_views
* sizeof (void *));
2328 ctx
->dirty
|= PAN_DIRTY_TEXTURES
;
2332 panfrost_sampler_view_destroy(
2333 struct pipe_context
*pctx
,
2334 struct pipe_sampler_view
*view
)
2336 pipe_resource_reference(&view
->texture
, NULL
);
2341 panfrost_set_shader_buffers(
2342 struct pipe_context
*pctx
,
2343 enum pipe_shader_type shader
,
2344 unsigned start
, unsigned count
,
2345 const struct pipe_shader_buffer
*buffers
,
2346 unsigned writable_bitmask
)
2348 struct panfrost_context
*ctx
= pan_context(pctx
);
2350 util_set_shader_buffers_mask(ctx
->ssbo
[shader
], &ctx
->ssbo_mask
[shader
],
2351 buffers
, start
, count
);
2354 /* Hints that a framebuffer should use AFBC where possible */
2358 struct panfrost_screen
*screen
,
2359 const struct pipe_framebuffer_state
*fb
)
2361 /* AFBC implemenation incomplete; hide it */
2362 if (!(pan_debug
& PAN_DBG_AFBC
)) return;
2364 /* Hint AFBC to the resources bound to each color buffer */
2366 for (unsigned i
= 0; i
< fb
->nr_cbufs
; ++i
) {
2367 struct pipe_surface
*surf
= fb
->cbufs
[i
];
2368 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
2369 panfrost_resource_hint_layout(screen
, rsrc
, PAN_AFBC
, 1);
2372 /* Also hint it to the depth buffer */
2375 struct panfrost_resource
*rsrc
= pan_resource(fb
->zsbuf
->texture
);
2376 panfrost_resource_hint_layout(screen
, rsrc
, PAN_AFBC
, 1);
2381 panfrost_set_framebuffer_state(struct pipe_context
*pctx
,
2382 const struct pipe_framebuffer_state
*fb
)
2384 struct panfrost_context
*ctx
= pan_context(pctx
);
2386 /* Flush when switching framebuffers, but not if the framebuffer
2387 * state is being restored by u_blitter
2390 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
2391 bool is_scanout
= panfrost_batch_is_scanout(batch
);
2392 bool has_draws
= batch
->last_job
.gpu
;
2394 /* Bail out early when the current and new states are the same. */
2395 if (util_framebuffer_state_equal(&ctx
->pipe_framebuffer
, fb
))
2398 /* The wallpaper logic sets a new FB state before doing the blit and
2399 * restore the old one when it's done. Those FB states are reported to
2400 * be different because the surface they are pointing to are different,
2401 * but those surfaces actually point to the same cbufs/zbufs. In that
2402 * case we definitely don't want new FB descs to be emitted/attached
2403 * since the job is expected to be flushed just after the blit is done,
2404 * so let's just copy the new state and return here.
2406 if (ctx
->wallpaper_batch
) {
2407 util_copy_framebuffer_state(&ctx
->pipe_framebuffer
, fb
);
2411 if (!is_scanout
|| has_draws
)
2412 panfrost_flush(pctx
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2414 assert(!ctx
->payloads
[PIPE_SHADER_VERTEX
].postfix
.framebuffer
&&
2415 !ctx
->payloads
[PIPE_SHADER_FRAGMENT
].postfix
.framebuffer
);
2417 /* Invalidate the FBO job cache since we've just been assigned a new
2422 util_copy_framebuffer_state(&ctx
->pipe_framebuffer
, fb
);
2424 /* Given that we're rendering, we'd love to have compression */
2425 struct panfrost_screen
*screen
= pan_screen(ctx
->base
.screen
);
2427 panfrost_hint_afbc(screen
, &ctx
->pipe_framebuffer
);
2428 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; ++i
)
2429 ctx
->payloads
[i
].postfix
.framebuffer
= 0;
2433 panfrost_create_depth_stencil_state(struct pipe_context
*pipe
,
2434 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
2436 return mem_dup(depth_stencil
, sizeof(*depth_stencil
));
2440 panfrost_bind_depth_stencil_state(struct pipe_context
*pipe
,
2443 struct panfrost_context
*ctx
= pan_context(pipe
);
2444 struct pipe_depth_stencil_alpha_state
*depth_stencil
= cso
;
2445 ctx
->depth_stencil
= depth_stencil
;
2450 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2451 * emulated in the fragment shader */
2453 if (depth_stencil
->alpha
.enabled
) {
2454 /* We need to trigger a new shader (maybe) */
2455 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->shader
[PIPE_SHADER_FRAGMENT
]);
2459 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_STENCIL_TEST
, depth_stencil
->stencil
[0].enabled
);
2461 panfrost_make_stencil_state(&depth_stencil
->stencil
[0], &ctx
->fragment_shader_core
.stencil_front
);
2462 ctx
->fragment_shader_core
.stencil_mask_front
= depth_stencil
->stencil
[0].writemask
;
2464 /* If back-stencil is not enabled, use the front values */
2465 bool back_enab
= ctx
->depth_stencil
->stencil
[1].enabled
;
2466 unsigned back_index
= back_enab
? 1 : 0;
2468 panfrost_make_stencil_state(&depth_stencil
->stencil
[back_index
], &ctx
->fragment_shader_core
.stencil_back
);
2469 ctx
->fragment_shader_core
.stencil_mask_back
= depth_stencil
->stencil
[back_index
].writemask
;
2471 /* Depth state (TODO: Refactor) */
2472 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_DEPTH_TEST
, depth_stencil
->depth
.enabled
);
2474 int func
= depth_stencil
->depth
.enabled
? depth_stencil
->depth
.func
: PIPE_FUNC_ALWAYS
;
2476 ctx
->fragment_shader_core
.unknown2_3
&= ~MALI_DEPTH_FUNC_MASK
;
2477 ctx
->fragment_shader_core
.unknown2_3
|= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func
));
2479 /* Bounds test not implemented */
2480 assert(!depth_stencil
->depth
.bounds_test
);
2482 ctx
->dirty
|= PAN_DIRTY_FS
;
2486 panfrost_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
2492 panfrost_set_sample_mask(struct pipe_context
*pipe
,
2493 unsigned sample_mask
)
2498 panfrost_set_clip_state(struct pipe_context
*pipe
,
2499 const struct pipe_clip_state
*clip
)
2501 //struct panfrost_context *panfrost = pan_context(pipe);
2505 panfrost_set_viewport_states(struct pipe_context
*pipe
,
2506 unsigned start_slot
,
2507 unsigned num_viewports
,
2508 const struct pipe_viewport_state
*viewports
)
2510 struct panfrost_context
*ctx
= pan_context(pipe
);
2512 assert(start_slot
== 0);
2513 assert(num_viewports
== 1);
2515 ctx
->pipe_viewport
= *viewports
;
2519 panfrost_set_scissor_states(struct pipe_context
*pipe
,
2520 unsigned start_slot
,
2521 unsigned num_scissors
,
2522 const struct pipe_scissor_state
*scissors
)
2524 struct panfrost_context
*ctx
= pan_context(pipe
);
2526 assert(start_slot
== 0);
2527 assert(num_scissors
== 1);
2529 ctx
->scissor
= *scissors
;
2533 panfrost_set_polygon_stipple(struct pipe_context
*pipe
,
2534 const struct pipe_poly_stipple
*stipple
)
2536 //struct panfrost_context *panfrost = pan_context(pipe);
2540 panfrost_set_active_query_state(struct pipe_context
*pipe
,
2543 struct panfrost_context
*ctx
= pan_context(pipe
);
2544 ctx
->active_queries
= enable
;
2548 panfrost_destroy(struct pipe_context
*pipe
)
2550 struct panfrost_context
*panfrost
= pan_context(pipe
);
2551 struct panfrost_screen
*screen
= pan_screen(pipe
->screen
);
2553 if (panfrost
->blitter
)
2554 util_blitter_destroy(panfrost
->blitter
);
2556 if (panfrost
->blitter_wallpaper
)
2557 util_blitter_destroy(panfrost
->blitter_wallpaper
);
2559 panfrost_drm_release_bo(screen
, panfrost
->scratchpad
, false);
2560 panfrost_drm_release_bo(screen
, panfrost
->tiler_heap
, false);
2561 panfrost_drm_release_bo(screen
, panfrost
->tiler_dummy
, false);
2566 static struct pipe_query
*
2567 panfrost_create_query(struct pipe_context
*pipe
,
2571 struct panfrost_query
*q
= rzalloc(pipe
, struct panfrost_query
);
2576 return (struct pipe_query
*) q
;
2580 panfrost_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2586 panfrost_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2588 struct panfrost_context
*ctx
= pan_context(pipe
);
2589 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2590 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
2592 switch (query
->type
) {
2593 case PIPE_QUERY_OCCLUSION_COUNTER
:
2594 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2595 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2596 /* Allocate a word for the query results to be stored */
2597 query
->transfer
= panfrost_allocate_transient(batch
, sizeof(unsigned));
2598 ctx
->occlusion_query
= query
;
2601 /* Geometry statistics are computed in the driver. XXX: geom/tess
2604 case PIPE_QUERY_PRIMITIVES_GENERATED
:
2605 query
->start
= ctx
->prims_generated
;
2607 case PIPE_QUERY_PRIMITIVES_EMITTED
:
2608 query
->start
= ctx
->tf_prims_generated
;
2612 fprintf(stderr
, "Skipping query %u\n", query
->type
);
2620 panfrost_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2622 struct panfrost_context
*ctx
= pan_context(pipe
);
2623 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2625 switch (query
->type
) {
2626 case PIPE_QUERY_OCCLUSION_COUNTER
:
2627 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2628 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2629 ctx
->occlusion_query
= NULL
;
2631 case PIPE_QUERY_PRIMITIVES_GENERATED
:
2632 query
->end
= ctx
->prims_generated
;
2634 case PIPE_QUERY_PRIMITIVES_EMITTED
:
2635 query
->end
= ctx
->tf_prims_generated
;
2643 panfrost_get_query_result(struct pipe_context
*pipe
,
2644 struct pipe_query
*q
,
2646 union pipe_query_result
*vresult
)
2648 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2651 switch (query
->type
) {
2652 case PIPE_QUERY_OCCLUSION_COUNTER
:
2653 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2654 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2656 panfrost_flush(pipe
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2658 /* Read back the query results */
2659 unsigned *result
= (unsigned *) query
->transfer
.cpu
;
2660 unsigned passed
= *result
;
2662 if (query
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
2663 vresult
->u64
= passed
;
2665 vresult
->b
= !!passed
;
2670 case PIPE_QUERY_PRIMITIVES_GENERATED
:
2671 case PIPE_QUERY_PRIMITIVES_EMITTED
:
2672 panfrost_flush(pipe
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2673 vresult
->u64
= query
->end
- query
->start
;
2677 DBG("Skipped query get %u\n", query
->type
);
2684 static struct pipe_stream_output_target
*
2685 panfrost_create_stream_output_target(struct pipe_context
*pctx
,
2686 struct pipe_resource
*prsc
,
2687 unsigned buffer_offset
,
2688 unsigned buffer_size
)
2690 struct pipe_stream_output_target
*target
;
2692 target
= rzalloc(pctx
, struct pipe_stream_output_target
);
2697 pipe_reference_init(&target
->reference
, 1);
2698 pipe_resource_reference(&target
->buffer
, prsc
);
2700 target
->context
= pctx
;
2701 target
->buffer_offset
= buffer_offset
;
2702 target
->buffer_size
= buffer_size
;
2708 panfrost_stream_output_target_destroy(struct pipe_context
*pctx
,
2709 struct pipe_stream_output_target
*target
)
2711 pipe_resource_reference(&target
->buffer
, NULL
);
2712 ralloc_free(target
);
2716 panfrost_set_stream_output_targets(struct pipe_context
*pctx
,
2717 unsigned num_targets
,
2718 struct pipe_stream_output_target
**targets
,
2719 const unsigned *offsets
)
2721 struct panfrost_context
*ctx
= pan_context(pctx
);
2722 struct panfrost_streamout
*so
= &ctx
->streamout
;
2724 assert(num_targets
<= ARRAY_SIZE(so
->targets
));
2726 for (unsigned i
= 0; i
< num_targets
; i
++) {
2727 if (offsets
[i
] != -1)
2728 so
->offsets
[i
] = offsets
[i
];
2730 pipe_so_target_reference(&so
->targets
[i
], targets
[i
]);
2733 for (unsigned i
= 0; i
< so
->num_targets
; i
++)
2734 pipe_so_target_reference(&so
->targets
[i
], NULL
);
2736 so
->num_targets
= num_targets
;
2740 panfrost_setup_hardware(struct panfrost_context
*ctx
)
2742 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2743 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
2745 ctx
->scratchpad
= panfrost_drm_create_bo(screen
, 64 * 4 * 4096, 0);
2746 ctx
->tiler_heap
= panfrost_drm_create_bo(screen
, 4096 * 4096,
2747 PAN_ALLOCATE_INVISIBLE
|
2748 PAN_ALLOCATE_GROWABLE
);
2749 ctx
->tiler_dummy
= panfrost_drm_create_bo(screen
, 4096,
2750 PAN_ALLOCATE_INVISIBLE
);
2751 assert(ctx
->scratchpad
&& ctx
->tiler_heap
&& ctx
->tiler_dummy
);
2754 /* New context creation, which also does hardware initialisation since I don't
2755 * know the better way to structure this :smirk: */
2757 struct pipe_context
*
2758 panfrost_create_context(struct pipe_screen
*screen
, void *priv
, unsigned flags
)
2760 struct panfrost_context
*ctx
= rzalloc(screen
, struct panfrost_context
);
2761 struct panfrost_screen
*pscreen
= pan_screen(screen
);
2762 memset(ctx
, 0, sizeof(*ctx
));
2763 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2765 ctx
->is_t6xx
= pscreen
->gpu_id
< 0x0700; /* Literally, "earlier than T700" */
2767 gallium
->screen
= screen
;
2769 gallium
->destroy
= panfrost_destroy
;
2771 gallium
->set_framebuffer_state
= panfrost_set_framebuffer_state
;
2773 gallium
->flush
= panfrost_flush
;
2774 gallium
->clear
= panfrost_clear
;
2775 gallium
->draw_vbo
= panfrost_draw_vbo
;
2777 gallium
->set_vertex_buffers
= panfrost_set_vertex_buffers
;
2778 gallium
->set_constant_buffer
= panfrost_set_constant_buffer
;
2779 gallium
->set_shader_buffers
= panfrost_set_shader_buffers
;
2781 gallium
->set_stencil_ref
= panfrost_set_stencil_ref
;
2783 gallium
->create_sampler_view
= panfrost_create_sampler_view
;
2784 gallium
->set_sampler_views
= panfrost_set_sampler_views
;
2785 gallium
->sampler_view_destroy
= panfrost_sampler_view_destroy
;
2787 gallium
->create_rasterizer_state
= panfrost_create_rasterizer_state
;
2788 gallium
->bind_rasterizer_state
= panfrost_bind_rasterizer_state
;
2789 gallium
->delete_rasterizer_state
= panfrost_generic_cso_delete
;
2791 gallium
->create_vertex_elements_state
= panfrost_create_vertex_elements_state
;
2792 gallium
->bind_vertex_elements_state
= panfrost_bind_vertex_elements_state
;
2793 gallium
->delete_vertex_elements_state
= panfrost_generic_cso_delete
;
2795 gallium
->create_fs_state
= panfrost_create_shader_state
;
2796 gallium
->delete_fs_state
= panfrost_delete_shader_state
;
2797 gallium
->bind_fs_state
= panfrost_bind_fs_state
;
2799 gallium
->create_vs_state
= panfrost_create_shader_state
;
2800 gallium
->delete_vs_state
= panfrost_delete_shader_state
;
2801 gallium
->bind_vs_state
= panfrost_bind_vs_state
;
2803 gallium
->create_sampler_state
= panfrost_create_sampler_state
;
2804 gallium
->delete_sampler_state
= panfrost_generic_cso_delete
;
2805 gallium
->bind_sampler_states
= panfrost_bind_sampler_states
;
2807 gallium
->create_depth_stencil_alpha_state
= panfrost_create_depth_stencil_state
;
2808 gallium
->bind_depth_stencil_alpha_state
= panfrost_bind_depth_stencil_state
;
2809 gallium
->delete_depth_stencil_alpha_state
= panfrost_delete_depth_stencil_state
;
2811 gallium
->set_sample_mask
= panfrost_set_sample_mask
;
2813 gallium
->set_clip_state
= panfrost_set_clip_state
;
2814 gallium
->set_viewport_states
= panfrost_set_viewport_states
;
2815 gallium
->set_scissor_states
= panfrost_set_scissor_states
;
2816 gallium
->set_polygon_stipple
= panfrost_set_polygon_stipple
;
2817 gallium
->set_active_query_state
= panfrost_set_active_query_state
;
2819 gallium
->create_query
= panfrost_create_query
;
2820 gallium
->destroy_query
= panfrost_destroy_query
;
2821 gallium
->begin_query
= panfrost_begin_query
;
2822 gallium
->end_query
= panfrost_end_query
;
2823 gallium
->get_query_result
= panfrost_get_query_result
;
2825 gallium
->create_stream_output_target
= panfrost_create_stream_output_target
;
2826 gallium
->stream_output_target_destroy
= panfrost_stream_output_target_destroy
;
2827 gallium
->set_stream_output_targets
= panfrost_set_stream_output_targets
;
2829 panfrost_resource_context_init(gallium
);
2830 panfrost_blend_context_init(gallium
);
2831 panfrost_compute_context_init(gallium
);
2833 panfrost_drm_init_context(ctx
);
2835 panfrost_setup_hardware(ctx
);
2838 gallium
->stream_uploader
= u_upload_create_default(gallium
);
2839 gallium
->const_uploader
= gallium
->stream_uploader
;
2840 assert(gallium
->stream_uploader
);
2842 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2843 ctx
->draw_modes
= (1 << (PIPE_PRIM_POLYGON
+ 1)) - 1;
2845 ctx
->primconvert
= util_primconvert_create(gallium
, ctx
->draw_modes
);
2847 ctx
->blitter
= util_blitter_create(gallium
);
2848 ctx
->blitter_wallpaper
= util_blitter_create(gallium
);
2850 assert(ctx
->blitter
);
2851 assert(ctx
->blitter_wallpaper
);
2853 ctx
->last_fragment_flushed
= true;
2854 ctx
->last_batch
= NULL
;
2856 /* Prepare for render! */
2858 panfrost_batch_init(ctx
);
2859 panfrost_emit_vertex_payload(ctx
);
2860 panfrost_emit_tiler_payload(ctx
);
2861 panfrost_invalidate_frame(ctx
);
2862 panfrost_default_shader_backend(ctx
);