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_job
*batch
= panfrost_get_job_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_job_get_polygon_list(batch
,
81 header_size
+ t
.polygon_list_size
);
84 /* Allow the entire tiler heap */
85 t
.heap_start
= ctx
->tiler_heap
.bo
->gpu
;
87 ctx
->tiler_heap
.bo
->gpu
+ ctx
->tiler_heap
.bo
->size
;
89 /* The tiler is disabled, so don't allow the tiler heap */
90 t
.heap_start
= ctx
->tiler_heap
.bo
->gpu
;
91 t
.heap_end
= t
.heap_start
;
93 /* Use a dummy polygon list */
94 t
.polygon_list
= ctx
->tiler_dummy
.bo
->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
.bo
->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
.bo
->gpu
,
147 .tiler
= panfrost_emit_midg_tiler(ctx
,
148 width
, height
, vertex_count
)
154 /* Are we currently rendering to the screen (rather than an FBO)? */
157 panfrost_is_scanout(struct panfrost_context
*ctx
)
159 /* If there is no color buffer, it's an FBO */
160 if (ctx
->pipe_framebuffer
.nr_cbufs
!= 1)
163 /* If we're too early that no framebuffer was sent, it's scanout */
164 if (!ctx
->pipe_framebuffer
.cbufs
[0])
167 return ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
168 ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
169 ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;
174 struct pipe_context
*pipe
,
176 const union pipe_color_union
*color
,
177 double depth
, unsigned stencil
)
179 struct panfrost_context
*ctx
= pan_context(pipe
);
180 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
182 panfrost_job_clear(ctx
, job
, buffers
, color
, depth
, stencil
);
186 panfrost_attach_vt_mfbd(struct panfrost_context
*ctx
)
188 struct bifrost_framebuffer mfbd
= panfrost_emit_mfbd(ctx
, ~0);
190 return panfrost_upload_transient(ctx
, &mfbd
, sizeof(mfbd
)) | MALI_MFBD
;
194 panfrost_attach_vt_sfbd(struct panfrost_context
*ctx
)
196 struct mali_single_framebuffer sfbd
= panfrost_emit_sfbd(ctx
, ~0);
198 return panfrost_upload_transient(ctx
, &sfbd
, sizeof(sfbd
)) | MALI_SFBD
;
202 panfrost_attach_vt_framebuffer(struct panfrost_context
*ctx
)
204 /* Skip the attach if we can */
206 if (ctx
->payloads
[PIPE_SHADER_VERTEX
].postfix
.framebuffer
) {
207 assert(ctx
->payloads
[PIPE_SHADER_FRAGMENT
].postfix
.framebuffer
);
211 struct panfrost_screen
*screen
= pan_screen(ctx
->base
.screen
);
212 mali_ptr framebuffer
= screen
->require_sfbd
?
213 panfrost_attach_vt_sfbd(ctx
) :
214 panfrost_attach_vt_mfbd(ctx
);
216 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; ++i
)
217 ctx
->payloads
[i
].postfix
.framebuffer
= framebuffer
;
220 /* Reset per-frame context, called on context initialisation as well as after
221 * flushing a frame */
224 panfrost_invalidate_frame(struct panfrost_context
*ctx
)
226 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; ++i
)
227 ctx
->payloads
[i
].postfix
.framebuffer
= 0;
230 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
233 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
| PAN_DIRTY_TEXTURES
;
235 /* TODO: When does this need to be handled? */
236 ctx
->active_queries
= true;
239 /* In practice, every field of these payloads should be configurable
240 * arbitrarily, which means these functions are basically catch-all's for
241 * as-of-yet unwavering unknowns */
244 panfrost_emit_vertex_payload(struct panfrost_context
*ctx
)
246 /* 0x2 bit clear on 32-bit T6XX */
248 struct midgard_payload_vertex_tiler payload
= {
249 .gl_enables
= 0x4 | 0x2,
252 /* Vertex and compute are closely coupled, so share a payload */
254 memcpy(&ctx
->payloads
[PIPE_SHADER_VERTEX
], &payload
, sizeof(payload
));
255 memcpy(&ctx
->payloads
[PIPE_SHADER_COMPUTE
], &payload
, sizeof(payload
));
259 panfrost_emit_tiler_payload(struct panfrost_context
*ctx
)
261 struct midgard_payload_vertex_tiler payload
= {
263 .zero1
= 0xffff, /* Why is this only seen on test-quad-textured? */
267 memcpy(&ctx
->payloads
[PIPE_SHADER_FRAGMENT
], &payload
, sizeof(payload
));
271 translate_tex_wrap(enum pipe_tex_wrap w
)
274 case PIPE_TEX_WRAP_REPEAT
:
275 return MALI_WRAP_REPEAT
;
277 /* TODO: lower GL_CLAMP? */
278 case PIPE_TEX_WRAP_CLAMP
:
279 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
280 return MALI_WRAP_CLAMP_TO_EDGE
;
282 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
283 return MALI_WRAP_CLAMP_TO_BORDER
;
285 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
286 return MALI_WRAP_MIRRORED_REPEAT
;
289 unreachable("Invalid wrap");
294 panfrost_translate_compare_func(enum pipe_compare_func in
)
297 case PIPE_FUNC_NEVER
:
298 return MALI_FUNC_NEVER
;
301 return MALI_FUNC_LESS
;
303 case PIPE_FUNC_EQUAL
:
304 return MALI_FUNC_EQUAL
;
306 case PIPE_FUNC_LEQUAL
:
307 return MALI_FUNC_LEQUAL
;
309 case PIPE_FUNC_GREATER
:
310 return MALI_FUNC_GREATER
;
312 case PIPE_FUNC_NOTEQUAL
:
313 return MALI_FUNC_NOTEQUAL
;
315 case PIPE_FUNC_GEQUAL
:
316 return MALI_FUNC_GEQUAL
;
318 case PIPE_FUNC_ALWAYS
:
319 return MALI_FUNC_ALWAYS
;
322 unreachable("Invalid func");
327 panfrost_translate_alt_compare_func(enum pipe_compare_func in
)
330 case PIPE_FUNC_NEVER
:
331 return MALI_ALT_FUNC_NEVER
;
334 return MALI_ALT_FUNC_LESS
;
336 case PIPE_FUNC_EQUAL
:
337 return MALI_ALT_FUNC_EQUAL
;
339 case PIPE_FUNC_LEQUAL
:
340 return MALI_ALT_FUNC_LEQUAL
;
342 case PIPE_FUNC_GREATER
:
343 return MALI_ALT_FUNC_GREATER
;
345 case PIPE_FUNC_NOTEQUAL
:
346 return MALI_ALT_FUNC_NOTEQUAL
;
348 case PIPE_FUNC_GEQUAL
:
349 return MALI_ALT_FUNC_GEQUAL
;
351 case PIPE_FUNC_ALWAYS
:
352 return MALI_ALT_FUNC_ALWAYS
;
355 unreachable("Invalid alt func");
360 panfrost_translate_stencil_op(enum pipe_stencil_op in
)
363 case PIPE_STENCIL_OP_KEEP
:
364 return MALI_STENCIL_KEEP
;
366 case PIPE_STENCIL_OP_ZERO
:
367 return MALI_STENCIL_ZERO
;
369 case PIPE_STENCIL_OP_REPLACE
:
370 return MALI_STENCIL_REPLACE
;
372 case PIPE_STENCIL_OP_INCR
:
373 return MALI_STENCIL_INCR
;
375 case PIPE_STENCIL_OP_DECR
:
376 return MALI_STENCIL_DECR
;
378 case PIPE_STENCIL_OP_INCR_WRAP
:
379 return MALI_STENCIL_INCR_WRAP
;
381 case PIPE_STENCIL_OP_DECR_WRAP
:
382 return MALI_STENCIL_DECR_WRAP
;
384 case PIPE_STENCIL_OP_INVERT
:
385 return MALI_STENCIL_INVERT
;
388 unreachable("Invalid stencil op");
393 panfrost_make_stencil_state(const struct pipe_stencil_state
*in
, struct mali_stencil_test
*out
)
395 out
->ref
= 0; /* Gallium gets it from elsewhere */
397 out
->mask
= in
->valuemask
;
398 out
->func
= panfrost_translate_compare_func(in
->func
);
399 out
->sfail
= panfrost_translate_stencil_op(in
->fail_op
);
400 out
->dpfail
= panfrost_translate_stencil_op(in
->zfail_op
);
401 out
->dppass
= panfrost_translate_stencil_op(in
->zpass_op
);
405 panfrost_default_shader_backend(struct panfrost_context
*ctx
)
407 struct mali_shader_meta shader
= {
408 .alpha_coverage
= ~MALI_ALPHA_COVERAGE(0.000000),
410 .unknown2_3
= MALI_DEPTH_FUNC(MALI_FUNC_ALWAYS
) | 0x3010,
411 .unknown2_4
= MALI_NO_MSAA
| 0x4e0,
414 /* unknown2_4 has 0x10 bit set on T6XX. We don't know why this is
415 * required (independent of 32-bit/64-bit descriptors), or why it's not
416 * used on later GPU revisions. Otherwise, all shader jobs fault on
417 * these earlier chips (perhaps this is a chicken bit of some kind).
418 * More investigation is needed. */
421 shader
.unknown2_4
|= 0x10;
424 struct pipe_stencil_state default_stencil
= {
426 .func
= PIPE_FUNC_ALWAYS
,
427 .fail_op
= MALI_STENCIL_KEEP
,
428 .zfail_op
= MALI_STENCIL_KEEP
,
429 .zpass_op
= MALI_STENCIL_KEEP
,
434 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_front
);
435 shader
.stencil_mask_front
= default_stencil
.writemask
;
437 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_back
);
438 shader
.stencil_mask_back
= default_stencil
.writemask
;
440 if (default_stencil
.enabled
)
441 shader
.unknown2_4
|= MALI_STENCIL_TEST
;
443 memcpy(&ctx
->fragment_shader_core
, &shader
, sizeof(shader
));
446 /* Generates a vertex/tiler job. This is, in some sense, the heart of the
447 * graphics command stream. It should be called once per draw, accordding to
448 * presentations. Set is_tiler for "tiler" jobs (fragment shader jobs, but in
449 * Mali parlance, "fragment" refers to framebuffer writeout). Clear it for
452 struct panfrost_transfer
453 panfrost_vertex_tiler_job(struct panfrost_context
*ctx
, bool is_tiler
)
455 struct mali_job_descriptor_header job
= {
456 .job_type
= is_tiler
? JOB_TYPE_TILER
: JOB_TYPE_VERTEX
,
457 .job_descriptor_size
= 1,
460 struct midgard_payload_vertex_tiler
*payload
= is_tiler
? &ctx
->payloads
[PIPE_SHADER_FRAGMENT
] : &ctx
->payloads
[PIPE_SHADER_VERTEX
];
462 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(job
) + sizeof(*payload
));
463 memcpy(transfer
.cpu
, &job
, sizeof(job
));
464 memcpy(transfer
.cpu
+ sizeof(job
), payload
, sizeof(*payload
));
469 panfrost_vertex_buffer_address(struct panfrost_context
*ctx
, unsigned i
)
471 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[i
];
472 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
.resource
);
474 return rsrc
->bo
->gpu
+ buf
->buffer_offset
;
478 panfrost_writes_point_size(struct panfrost_context
*ctx
)
480 assert(ctx
->shader
[PIPE_SHADER_VERTEX
]);
481 struct panfrost_shader_state
*vs
= &ctx
->shader
[PIPE_SHADER_VERTEX
]->variants
[ctx
->shader
[PIPE_SHADER_VERTEX
]->active_variant
];
483 return vs
->writes_point_size
&& ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.draw_mode
== MALI_POINTS
;
486 /* Stage the attribute descriptors so we can adjust src_offset
487 * to let BOs align nicely */
490 panfrost_stage_attributes(struct panfrost_context
*ctx
)
492 struct panfrost_vertex_state
*so
= ctx
->vertex
;
494 size_t sz
= sizeof(struct mali_attr_meta
) * so
->num_elements
;
495 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sz
);
496 struct mali_attr_meta
*target
= (struct mali_attr_meta
*) transfer
.cpu
;
498 /* Copy as-is for the first pass */
499 memcpy(target
, so
->hw
, sz
);
501 /* Fixup offsets for the second pass. Recall that the hardware
502 * calculates attribute addresses as:
504 * addr = base + (stride * vtx) + src_offset;
506 * However, on Mali, base must be aligned to 64-bytes, so we
509 * base' = base & ~63 = base - (base & 63)
511 * To compensate when using base' (see emit_vertex_data), we have
512 * to adjust src_offset by the masked off piece:
514 * addr' = base' + (stride * vtx) + (src_offset + (base & 63))
515 * = base - (base & 63) + (stride * vtx) + src_offset + (base & 63)
516 * = base + (stride * vtx) + src_offset
522 unsigned start
= ctx
->payloads
[PIPE_SHADER_VERTEX
].offset_start
;
524 for (unsigned i
= 0; i
< so
->num_elements
; ++i
) {
525 unsigned vbi
= so
->pipe
[i
].vertex_buffer_index
;
526 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[vbi
];
527 mali_ptr addr
= panfrost_vertex_buffer_address(ctx
, vbi
);
529 /* Adjust by the masked off bits of the offset */
530 target
[i
].src_offset
+= (addr
& 63);
532 /* Also, somewhat obscurely per-instance data needs to be
533 * offset in response to a delayed start in an indexed draw */
535 if (so
->pipe
[i
].instance_divisor
&& ctx
->instance_count
> 1 && start
) {
536 target
[i
].src_offset
-= buf
->stride
* start
;
542 ctx
->payloads
[PIPE_SHADER_VERTEX
].postfix
.attribute_meta
= transfer
.gpu
;
546 panfrost_upload_sampler_descriptors(struct panfrost_context
*ctx
)
548 size_t desc_size
= sizeof(struct mali_sampler_descriptor
);
550 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
553 if (ctx
->sampler_count
[t
] && ctx
->sampler_view_count
[t
]) {
554 size_t transfer_size
= desc_size
* ctx
->sampler_count
[t
];
556 struct panfrost_transfer transfer
=
557 panfrost_allocate_transient(ctx
, transfer_size
);
559 struct mali_sampler_descriptor
*desc
=
560 (struct mali_sampler_descriptor
*) transfer
.cpu
;
562 for (int i
= 0; i
< ctx
->sampler_count
[t
]; ++i
)
563 desc
[i
] = ctx
->samplers
[t
][i
]->hw
;
565 upload
= transfer
.gpu
;
568 ctx
->payloads
[t
].postfix
.sampler_descriptor
= upload
;
573 panfrost_layout_for_texture(struct panfrost_resource
*rsrc
, bool manual_stride
)
575 /* TODO: other linear depth textures */
576 bool is_depth
= rsrc
->base
.format
== PIPE_FORMAT_Z32_UNORM
;
578 unsigned usage2_layout
= 0x10;
580 switch (rsrc
->layout
) {
582 usage2_layout
|= 0x8 | 0x4;
585 usage2_layout
|= 0x1;
588 usage2_layout
|= is_depth
? 0x1 : 0x2;
596 usage2_layout
|= MALI_TEX_MANUAL_STRIDE
;
598 return usage2_layout
;
603 struct panfrost_context
*ctx
,
604 struct panfrost_sampler_view
*view
)
609 struct pipe_sampler_view
*pview
= &view
->base
;
610 struct panfrost_resource
*rsrc
= pan_resource(pview
->texture
);
612 /* Do we interleave an explicit stride with every element? */
614 bool has_manual_stride
= view
->manual_stride
;
616 /* For easy access */
618 assert(pview
->target
!= PIPE_BUFFER
);
619 unsigned first_level
= pview
->u
.tex
.first_level
;
620 unsigned last_level
= pview
->u
.tex
.last_level
;
621 unsigned first_layer
= pview
->u
.tex
.first_layer
;
622 unsigned last_layer
= pview
->u
.tex
.last_layer
;
624 /* Lower-bit is set when sampling from colour AFBC */
625 bool is_afbc
= rsrc
->layout
== PAN_AFBC
;
626 bool is_zs
= rsrc
->base
.bind
& PIPE_BIND_DEPTH_STENCIL
;
627 unsigned afbc_bit
= (is_afbc
&& !is_zs
) ? 1 : 0;
629 /* Add the BO to the job so it's retained until the job is done. */
630 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
631 panfrost_job_add_bo(job
, rsrc
->bo
);
633 /* Add the usage flags in, since they can change across the CSO
634 * lifetime due to layout switches */
636 view
->hw
.format
.usage2
= panfrost_layout_for_texture(rsrc
, has_manual_stride
);
638 /* Inject the addresses in, interleaving mip levels, cube faces, and
639 * strides in that order */
643 for (unsigned l
= first_level
; l
<= last_level
; ++l
) {
644 for (unsigned f
= first_layer
; f
<= last_layer
; ++f
) {
646 view
->hw
.payload
[idx
++] =
647 panfrost_get_texture_address(rsrc
, l
, f
) + afbc_bit
;
649 if (has_manual_stride
) {
650 view
->hw
.payload
[idx
++] =
651 rsrc
->slices
[l
].stride
;
656 return panfrost_upload_transient(ctx
, &view
->hw
,
657 sizeof(struct mali_texture_descriptor
));
661 panfrost_upload_texture_descriptors(struct panfrost_context
*ctx
)
663 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
664 mali_ptr trampoline
= 0;
666 if (ctx
->sampler_view_count
[t
]) {
667 uint64_t trampolines
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
669 for (int i
= 0; i
< ctx
->sampler_view_count
[t
]; ++i
)
671 panfrost_upload_tex(ctx
, ctx
->sampler_views
[t
][i
]);
673 trampoline
= panfrost_upload_transient(ctx
, trampolines
, sizeof(uint64_t) * ctx
->sampler_view_count
[t
]);
676 ctx
->payloads
[t
].postfix
.texture_trampoline
= trampoline
;
680 struct sysval_uniform
{
689 static void panfrost_upload_viewport_scale_sysval(struct panfrost_context
*ctx
,
690 struct sysval_uniform
*uniform
)
692 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
694 uniform
->f
[0] = vp
->scale
[0];
695 uniform
->f
[1] = vp
->scale
[1];
696 uniform
->f
[2] = vp
->scale
[2];
699 static void panfrost_upload_viewport_offset_sysval(struct panfrost_context
*ctx
,
700 struct sysval_uniform
*uniform
)
702 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
704 uniform
->f
[0] = vp
->translate
[0];
705 uniform
->f
[1] = vp
->translate
[1];
706 uniform
->f
[2] = vp
->translate
[2];
709 static void panfrost_upload_txs_sysval(struct panfrost_context
*ctx
,
710 enum pipe_shader_type st
,
711 unsigned int sysvalid
,
712 struct sysval_uniform
*uniform
)
714 unsigned texidx
= PAN_SYSVAL_ID_TO_TXS_TEX_IDX(sysvalid
);
715 unsigned dim
= PAN_SYSVAL_ID_TO_TXS_DIM(sysvalid
);
716 bool is_array
= PAN_SYSVAL_ID_TO_TXS_IS_ARRAY(sysvalid
);
717 struct pipe_sampler_view
*tex
= &ctx
->sampler_views
[st
][texidx
]->base
;
720 uniform
->i
[0] = u_minify(tex
->texture
->width0
, tex
->u
.tex
.first_level
);
723 uniform
->i
[1] = u_minify(tex
->texture
->height0
,
724 tex
->u
.tex
.first_level
);
727 uniform
->i
[2] = u_minify(tex
->texture
->depth0
,
728 tex
->u
.tex
.first_level
);
731 uniform
->i
[dim
] = tex
->texture
->array_size
;
734 static void panfrost_upload_ssbo_sysval(
735 struct panfrost_context
*ctx
,
736 enum pipe_shader_type st
,
738 struct sysval_uniform
*uniform
)
740 assert(ctx
->ssbo_mask
[st
] & (1 << ssbo_id
));
741 struct pipe_shader_buffer sb
= ctx
->ssbo
[st
][ssbo_id
];
743 /* Compute address */
744 struct panfrost_job
*batch
= panfrost_get_job_for_fbo(ctx
);
745 struct panfrost_bo
*bo
= pan_resource(sb
.buffer
)->bo
;
747 panfrost_job_add_bo(batch
, bo
);
749 /* Upload address and size as sysval */
750 uniform
->du
[0] = bo
->gpu
+ sb
.buffer_offset
;
751 uniform
->u
[2] = sb
.buffer_size
;
754 static void panfrost_upload_num_work_groups_sysval(struct panfrost_context
*ctx
,
755 struct sysval_uniform
*uniform
)
757 uniform
->u
[0] = ctx
->compute_grid
->grid
[0];
758 uniform
->u
[1] = ctx
->compute_grid
->grid
[1];
759 uniform
->u
[2] = ctx
->compute_grid
->grid
[2];
762 static void panfrost_upload_sysvals(struct panfrost_context
*ctx
, void *buf
,
763 struct panfrost_shader_state
*ss
,
764 enum pipe_shader_type st
)
766 struct sysval_uniform
*uniforms
= (void *)buf
;
768 for (unsigned i
= 0; i
< ss
->sysval_count
; ++i
) {
769 int sysval
= ss
->sysval
[i
];
771 switch (PAN_SYSVAL_TYPE(sysval
)) {
772 case PAN_SYSVAL_VIEWPORT_SCALE
:
773 panfrost_upload_viewport_scale_sysval(ctx
, &uniforms
[i
]);
775 case PAN_SYSVAL_VIEWPORT_OFFSET
:
776 panfrost_upload_viewport_offset_sysval(ctx
, &uniforms
[i
]);
778 case PAN_SYSVAL_TEXTURE_SIZE
:
779 panfrost_upload_txs_sysval(ctx
, st
, PAN_SYSVAL_ID(sysval
),
782 case PAN_SYSVAL_SSBO
:
783 panfrost_upload_ssbo_sysval(ctx
, st
, PAN_SYSVAL_ID(sysval
),
786 case PAN_SYSVAL_NUM_WORK_GROUPS
:
787 panfrost_upload_num_work_groups_sysval(ctx
, &uniforms
[i
]);
797 panfrost_map_constant_buffer_cpu(struct panfrost_constant_buffer
*buf
, unsigned index
)
799 struct pipe_constant_buffer
*cb
= &buf
->cb
[index
];
800 struct panfrost_resource
*rsrc
= pan_resource(cb
->buffer
);
803 return rsrc
->bo
->cpu
;
804 else if (cb
->user_buffer
)
805 return cb
->user_buffer
;
807 unreachable("No constant buffer");
811 panfrost_map_constant_buffer_gpu(
812 struct panfrost_context
*ctx
,
813 struct panfrost_constant_buffer
*buf
,
816 struct pipe_constant_buffer
*cb
= &buf
->cb
[index
];
817 struct panfrost_resource
*rsrc
= pan_resource(cb
->buffer
);
820 return rsrc
->bo
->gpu
;
821 else if (cb
->user_buffer
)
822 return panfrost_upload_transient(ctx
, cb
->user_buffer
, cb
->buffer_size
);
824 unreachable("No constant buffer");
827 /* Compute number of UBOs active (more specifically, compute the highest UBO
828 * number addressable -- if there are gaps, include them in the count anyway).
829 * We always include UBO #0 in the count, since we *need* uniforms enabled for
833 panfrost_ubo_count(struct panfrost_context
*ctx
, enum pipe_shader_type stage
)
835 unsigned mask
= ctx
->constant_buffer
[stage
].enabled_mask
| 1;
836 return 32 - __builtin_clz(mask
);
839 /* Fixes up a shader state with current state, returning a GPU address to the
843 panfrost_patch_shader_state(
844 struct panfrost_context
*ctx
,
845 struct panfrost_shader_state
*ss
,
846 enum pipe_shader_type stage
,
849 ss
->tripipe
->texture_count
= ctx
->sampler_view_count
[stage
];
850 ss
->tripipe
->sampler_count
= ctx
->sampler_count
[stage
];
852 ss
->tripipe
->midgard1
.flags
= 0x220;
854 unsigned ubo_count
= panfrost_ubo_count(ctx
, stage
);
855 ss
->tripipe
->midgard1
.uniform_buffer_count
= ubo_count
;
857 /* We can't reuse over frames; that's not safe. The descriptor must be
858 * transient uploaded */
861 return panfrost_upload_transient(ctx
,
863 sizeof(struct mali_shader_meta
));
866 /* If we don't need an upload, don't bother */
872 panfrost_patch_shader_state_compute(
873 struct panfrost_context
*ctx
,
874 enum pipe_shader_type stage
,
877 struct panfrost_shader_variants
*all
= ctx
->shader
[stage
];
880 ctx
->payloads
[stage
].postfix
._shader_upper
= 0;
884 struct panfrost_shader_state
*s
= &all
->variants
[all
->active_variant
];
886 ctx
->payloads
[stage
].postfix
._shader_upper
=
887 panfrost_patch_shader_state(ctx
, s
, stage
, should_upload
) >> 4;
890 /* Go through dirty flags and actualise them in the cmdstream. */
893 panfrost_emit_for_draw(struct panfrost_context
*ctx
, bool with_vertex_data
)
895 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
896 struct panfrost_screen
*screen
= pan_screen(ctx
->base
.screen
);
898 panfrost_attach_vt_framebuffer(ctx
);
900 if (with_vertex_data
) {
901 panfrost_emit_vertex_data(job
);
903 /* Varyings emitted for -all- geometry */
904 unsigned total_count
= ctx
->padded_count
* ctx
->instance_count
;
905 panfrost_emit_varying_descriptor(ctx
, total_count
);
908 bool msaa
= ctx
->rasterizer
->base
.multisample
;
910 if (ctx
->dirty
& PAN_DIRTY_RASTERIZER
) {
911 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].gl_enables
= ctx
->rasterizer
->tiler_gl_enables
;
913 /* TODO: Sample size */
914 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_HAS_MSAA
, msaa
);
915 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_MSAA
, !msaa
);
918 panfrost_job_set_requirements(ctx
, job
);
920 if (ctx
->occlusion_query
) {
921 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].gl_enables
|= MALI_OCCLUSION_QUERY
| MALI_OCCLUSION_PRECISE
;
922 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].postfix
.occlusion_counter
= ctx
->occlusion_query
->transfer
.gpu
;
925 panfrost_patch_shader_state_compute(ctx
, PIPE_SHADER_VERTEX
, true);
926 panfrost_patch_shader_state_compute(ctx
, PIPE_SHADER_COMPUTE
, true);
928 if (ctx
->dirty
& (PAN_DIRTY_RASTERIZER
| PAN_DIRTY_VS
)) {
929 /* Check if we need to link the gl_PointSize varying */
930 if (!panfrost_writes_point_size(ctx
)) {
931 /* If the size is constant, write it out. Otherwise,
932 * don't touch primitive_size (since we would clobber
933 * the pointer there) */
935 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].primitive_size
.constant
= ctx
->rasterizer
->base
.line_width
;
939 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
940 if (ctx
->shader
[PIPE_SHADER_FRAGMENT
])
941 ctx
->dirty
|= PAN_DIRTY_FS
;
943 if (ctx
->dirty
& PAN_DIRTY_FS
) {
944 assert(ctx
->shader
[PIPE_SHADER_FRAGMENT
]);
945 struct panfrost_shader_state
*variant
= &ctx
->shader
[PIPE_SHADER_FRAGMENT
]->variants
[ctx
->shader
[PIPE_SHADER_FRAGMENT
]->active_variant
];
947 panfrost_patch_shader_state(ctx
, variant
, PIPE_SHADER_FRAGMENT
, false);
949 panfrost_job_add_bo(job
, variant
->bo
);
951 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
954 COPY(attribute_count
);
959 COPY(midgard1
.uniform_count
);
960 COPY(midgard1
.uniform_buffer_count
);
961 COPY(midgard1
.work_count
);
962 COPY(midgard1
.flags
);
963 COPY(midgard1
.unknown2
);
967 /* Get blending setup */
968 unsigned rt_count
= MAX2(ctx
->pipe_framebuffer
.nr_cbufs
, 1);
970 struct panfrost_blend_final blend
[PIPE_MAX_COLOR_BUFS
];
972 for (unsigned c
= 0; c
< rt_count
; ++c
)
973 blend
[c
] = panfrost_get_blend_for_context(ctx
, c
);
975 /* If there is a blend shader, work registers are shared. XXX: opt */
977 for (unsigned c
= 0; c
< rt_count
; ++c
) {
978 if (blend
[c
].is_shader
)
979 ctx
->fragment_shader_core
.midgard1
.work_count
= 16;
982 /* Set late due to depending on render state */
983 unsigned flags
= ctx
->fragment_shader_core
.midgard1
.flags
;
985 /* Depending on whether it's legal to in the given shader, we
986 * try to enable early-z testing (or forward-pixel kill?) */
988 if (!variant
->can_discard
)
989 flags
|= MALI_EARLY_Z
;
991 /* Any time texturing is used, derivatives are implicitly
992 * calculated, so we need to enable helper invocations */
994 if (variant
->helper_invocations
)
995 flags
|= MALI_HELPER_INVOCATIONS
;
997 ctx
->fragment_shader_core
.midgard1
.flags
= flags
;
999 /* Assign the stencil refs late */
1001 unsigned front_ref
= ctx
->stencil_ref
.ref_value
[0];
1002 unsigned back_ref
= ctx
->stencil_ref
.ref_value
[1];
1003 bool back_enab
= ctx
->depth_stencil
->stencil
[1].enabled
;
1005 ctx
->fragment_shader_core
.stencil_front
.ref
= front_ref
;
1006 ctx
->fragment_shader_core
.stencil_back
.ref
= back_enab
? back_ref
: front_ref
;
1008 /* CAN_DISCARD should be set if the fragment shader possibly
1009 * contains a 'discard' instruction. It is likely this is
1010 * related to optimizations related to forward-pixel kill, as
1011 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
1012 * thing?" by Peter Harris
1015 if (variant
->can_discard
) {
1016 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
1017 ctx
->fragment_shader_core
.midgard1
.flags
|= 0x400;
1020 /* Even on MFBD, the shader descriptor gets blend shaders. It's
1021 * *also* copied to the blend_meta appended (by convention),
1022 * but this is the field actually read by the hardware. (Or
1023 * maybe both are read...?) */
1025 if (blend
[0].is_shader
) {
1026 ctx
->fragment_shader_core
.blend
.shader
=
1027 blend
[0].shader
.bo
->gpu
| blend
[0].shader
.first_tag
;
1029 ctx
->fragment_shader_core
.blend
.shader
= 0;
1032 if (screen
->require_sfbd
) {
1033 /* When only a single render target platform is used, the blend
1034 * information is inside the shader meta itself. We
1035 * additionally need to signal CAN_DISCARD for nontrivial blend
1036 * modes (so we're able to read back the destination buffer) */
1038 if (!blend
[0].is_shader
) {
1039 ctx
->fragment_shader_core
.blend
.equation
=
1040 *blend
[0].equation
.equation
;
1041 ctx
->fragment_shader_core
.blend
.constant
=
1042 blend
[0].equation
.constant
;
1045 if (!blend
[0].no_blending
) {
1046 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
1050 size_t size
= sizeof(struct mali_shader_meta
) + (sizeof(struct midgard_blend_rt
) * rt_count
);
1051 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
1052 memcpy(transfer
.cpu
, &ctx
->fragment_shader_core
, sizeof(struct mali_shader_meta
));
1054 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].postfix
._shader_upper
= (transfer
.gpu
) >> 4;
1056 if (!screen
->require_sfbd
) {
1057 /* Additional blend descriptor tacked on for jobs using MFBD */
1059 struct midgard_blend_rt rts
[4];
1061 for (unsigned i
= 0; i
< rt_count
; ++i
) {
1062 unsigned blend_count
= 0x200;
1064 if (blend
[i
].is_shader
) {
1065 /* For a blend shader, the bottom nibble corresponds to
1066 * the number of work registers used, which signals the
1067 * -existence- of a blend shader */
1069 assert(blend
[i
].shader
.work_count
>= 2);
1070 blend_count
|= MIN2(blend
[i
].shader
.work_count
, 3);
1072 /* Otherwise, the bottom bit simply specifies if
1073 * blending (anything other than REPLACE) is enabled */
1075 if (!blend
[i
].no_blending
)
1081 (ctx
->pipe_framebuffer
.nr_cbufs
> i
) &&
1082 (ctx
->pipe_framebuffer
.cbufs
[i
]) &&
1083 util_format_is_srgb(ctx
->pipe_framebuffer
.cbufs
[i
]->format
);
1085 rts
[i
].flags
= blend_count
;
1088 rts
[i
].flags
|= MALI_BLEND_SRGB
;
1090 if (!ctx
->blend
->base
.dither
)
1091 rts
[i
].flags
|= MALI_BLEND_NO_DITHER
;
1093 /* TODO: sRGB in blend shaders is currently
1094 * unimplemented. Contact me (Alyssa) if you're
1095 * interested in working on this. We have
1096 * native Midgard ops for helping here, but
1097 * they're not well-understood yet. */
1099 assert(!(is_srgb
&& blend
[i
].is_shader
));
1101 if (blend
[i
].is_shader
) {
1102 rts
[i
].blend
.shader
= blend
[i
].shader
.bo
->gpu
| blend
[i
].shader
.first_tag
;
1104 rts
[i
].blend
.equation
= *blend
[i
].equation
.equation
;
1105 rts
[i
].blend
.constant
= blend
[i
].equation
.constant
;
1109 memcpy(transfer
.cpu
+ sizeof(struct mali_shader_meta
), rts
, sizeof(rts
[0]) * rt_count
);
1113 /* We stage to transient, so always dirty.. */
1115 panfrost_stage_attributes(ctx
);
1117 if (ctx
->dirty
& PAN_DIRTY_SAMPLERS
)
1118 panfrost_upload_sampler_descriptors(ctx
);
1120 if (ctx
->dirty
& PAN_DIRTY_TEXTURES
)
1121 panfrost_upload_texture_descriptors(ctx
);
1123 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
1125 for (int i
= 0; i
< PIPE_SHADER_TYPES
; ++i
) {
1126 struct panfrost_shader_variants
*all
= ctx
->shader
[i
];
1131 struct panfrost_constant_buffer
*buf
= &ctx
->constant_buffer
[i
];
1133 struct panfrost_shader_state
*ss
= &all
->variants
[all
->active_variant
];
1135 /* Uniforms are implicitly UBO #0 */
1136 bool has_uniforms
= buf
->enabled_mask
& (1 << 0);
1138 /* Allocate room for the sysval and the uniforms */
1139 size_t sys_size
= sizeof(float) * 4 * ss
->sysval_count
;
1140 size_t uniform_size
= has_uniforms
? (buf
->cb
[0].buffer_size
) : 0;
1141 size_t size
= sys_size
+ uniform_size
;
1142 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
1144 /* Upload sysvals requested by the shader */
1145 panfrost_upload_sysvals(ctx
, transfer
.cpu
, ss
, i
);
1147 /* Upload uniforms */
1149 const void *cpu
= panfrost_map_constant_buffer_cpu(buf
, 0);
1150 memcpy(transfer
.cpu
+ sys_size
, cpu
, uniform_size
);
1154 ctx
->shader
[i
]->variants
[ctx
->shader
[i
]->active_variant
].uniform_count
;
1156 struct mali_vertex_tiler_postfix
*postfix
=
1157 &ctx
->payloads
[i
].postfix
;
1159 /* Next up, attach UBOs. UBO #0 is the uniforms we just
1162 unsigned ubo_count
= panfrost_ubo_count(ctx
, i
);
1163 assert(ubo_count
>= 1);
1165 size_t sz
= sizeof(struct mali_uniform_buffer_meta
) * ubo_count
;
1166 struct mali_uniform_buffer_meta ubos
[PAN_MAX_CONST_BUFFERS
];
1168 /* Upload uniforms as a UBO */
1169 ubos
[0].size
= MALI_POSITIVE((2 + uniform_count
));
1170 ubos
[0].ptr
= transfer
.gpu
>> 2;
1172 /* The rest are honest-to-goodness UBOs */
1174 for (unsigned ubo
= 1; ubo
< ubo_count
; ++ubo
) {
1175 size_t sz
= buf
->cb
[ubo
].buffer_size
;
1177 bool enabled
= buf
->enabled_mask
& (1 << ubo
);
1178 bool empty
= sz
== 0;
1180 if (!enabled
|| empty
) {
1181 /* Stub out disabled UBOs to catch accesses */
1184 ubos
[ubo
].ptr
= 0xDEAD0000;
1188 mali_ptr gpu
= panfrost_map_constant_buffer_gpu(ctx
, buf
, ubo
);
1190 unsigned bytes_per_field
= 16;
1191 unsigned aligned
= ALIGN_POT(sz
, bytes_per_field
);
1192 unsigned fields
= aligned
/ bytes_per_field
;
1194 ubos
[ubo
].size
= MALI_POSITIVE(fields
);
1195 ubos
[ubo
].ptr
= gpu
>> 2;
1198 mali_ptr ubufs
= panfrost_upload_transient(ctx
, ubos
, sz
);
1199 postfix
->uniforms
= transfer
.gpu
;
1200 postfix
->uniform_buffers
= ubufs
;
1202 buf
->dirty_mask
= 0;
1205 /* TODO: Upload the viewport somewhere more appropriate */
1207 /* Clip bounds are encoded as floats. The viewport itself is encoded as
1208 * (somewhat) asymmetric ints. */
1209 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
1211 struct mali_viewport view
= {
1212 /* By default, do no viewport clipping, i.e. clip to (-inf,
1213 * inf) in each direction. Clipping to the viewport in theory
1214 * should work, but in practice causes issues when we're not
1215 * explicitly trying to scissor */
1217 .clip_minx
= -INFINITY
,
1218 .clip_miny
= -INFINITY
,
1219 .clip_maxx
= INFINITY
,
1220 .clip_maxy
= INFINITY
,
1226 /* Always scissor to the viewport by default. */
1227 float vp_minx
= (int) (vp
->translate
[0] - fabsf(vp
->scale
[0]));
1228 float vp_maxx
= (int) (vp
->translate
[0] + fabsf(vp
->scale
[0]));
1230 float vp_miny
= (int) (vp
->translate
[1] - fabsf(vp
->scale
[1]));
1231 float vp_maxy
= (int) (vp
->translate
[1] + fabsf(vp
->scale
[1]));
1233 /* Apply the scissor test */
1235 unsigned minx
, miny
, maxx
, maxy
;
1237 if (ss
&& ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
) {
1238 minx
= MAX2(ss
->minx
, vp_minx
);
1239 miny
= MAX2(ss
->miny
, vp_miny
);
1240 maxx
= MIN2(ss
->maxx
, vp_maxx
);
1241 maxy
= MIN2(ss
->maxy
, vp_maxy
);
1249 /* Hardware needs the min/max to be strictly ordered, so flip if we
1250 * need to. The viewport transformation in the vertex shader will
1251 * handle the negatives if we don't */
1265 /* Clamp everything positive, just in case */
1267 maxx
= MAX2(0, maxx
);
1268 maxy
= MAX2(0, maxy
);
1269 minx
= MAX2(0, minx
);
1270 miny
= MAX2(0, miny
);
1272 /* Clamp to the framebuffer size as a last check */
1274 minx
= MIN2(ctx
->pipe_framebuffer
.width
, minx
);
1275 maxx
= MIN2(ctx
->pipe_framebuffer
.width
, maxx
);
1277 miny
= MIN2(ctx
->pipe_framebuffer
.height
, miny
);
1278 maxy
= MIN2(ctx
->pipe_framebuffer
.height
, maxy
);
1280 /* Update the job, unless we're doing wallpapering (whose lack of
1281 * scissor we can ignore, since if we "miss" a tile of wallpaper, it'll
1282 * just... be faster :) */
1284 if (!ctx
->wallpaper_batch
)
1285 panfrost_job_union_scissor(job
, minx
, miny
, maxx
, maxy
);
1289 view
.viewport0
[0] = minx
;
1290 view
.viewport1
[0] = MALI_POSITIVE(maxx
);
1292 view
.viewport0
[1] = miny
;
1293 view
.viewport1
[1] = MALI_POSITIVE(maxy
);
1295 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].postfix
.viewport
=
1296 panfrost_upload_transient(ctx
,
1298 sizeof(struct mali_viewport
));
1303 /* Corresponds to exactly one draw, but does not submit anything */
1306 panfrost_queue_draw(struct panfrost_context
*ctx
)
1308 /* Handle dirty flags now */
1309 panfrost_emit_for_draw(ctx
, true);
1311 /* If rasterizer discard is enable, only submit the vertex */
1313 bool rasterizer_discard
= ctx
->rasterizer
1314 && ctx
->rasterizer
->base
.rasterizer_discard
;
1316 struct panfrost_transfer vertex
= panfrost_vertex_tiler_job(ctx
, false);
1317 struct panfrost_transfer tiler
;
1319 if (!rasterizer_discard
)
1320 tiler
= panfrost_vertex_tiler_job(ctx
, true);
1322 struct panfrost_job
*batch
= panfrost_get_job_for_fbo(ctx
);
1324 if (rasterizer_discard
)
1325 panfrost_scoreboard_queue_vertex_job(batch
, vertex
, FALSE
);
1326 else if (ctx
->wallpaper_batch
)
1327 panfrost_scoreboard_queue_fused_job_prepend(batch
, vertex
, tiler
);
1329 panfrost_scoreboard_queue_fused_job(batch
, vertex
, tiler
);
1332 /* The entire frame is in memory -- send it off to the kernel! */
1335 panfrost_submit_frame(struct panfrost_context
*ctx
, bool flush_immediate
,
1336 struct pipe_fence_handle
**fence
,
1337 struct panfrost_job
*job
)
1339 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
1340 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
1342 panfrost_job_submit(ctx
, job
);
1344 /* If visual, we can stall a frame */
1346 if (!flush_immediate
)
1347 panfrost_drm_force_flush_fragment(ctx
, fence
);
1349 screen
->last_fragment_flushed
= false;
1350 screen
->last_job
= job
;
1352 /* If readback, flush now (hurts the pipelined performance) */
1353 if (flush_immediate
)
1354 panfrost_drm_force_flush_fragment(ctx
, fence
);
1358 panfrost_draw_wallpaper(struct pipe_context
*pipe
)
1360 struct panfrost_context
*ctx
= pan_context(pipe
);
1362 /* Nothing to reload? TODO: MRT wallpapers */
1363 if (ctx
->pipe_framebuffer
.cbufs
[0] == NULL
)
1366 /* Check if the buffer has any content on it worth preserving */
1368 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[0];
1369 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
1370 unsigned level
= surf
->u
.tex
.level
;
1372 if (!rsrc
->slices
[level
].initialized
)
1375 /* Save the batch */
1376 struct panfrost_job
*batch
= panfrost_get_job_for_fbo(ctx
);
1378 ctx
->wallpaper_batch
= batch
;
1380 /* Clamp the rendering area to the damage extent. The
1381 * KHR_partial_update() spec states that trying to render outside of
1382 * the damage region is "undefined behavior", so we should be safe.
1384 unsigned damage_width
= (rsrc
->damage
.extent
.maxx
- rsrc
->damage
.extent
.minx
);
1385 unsigned damage_height
= (rsrc
->damage
.extent
.maxy
- rsrc
->damage
.extent
.miny
);
1387 if (damage_width
&& damage_height
) {
1388 panfrost_job_intersection_scissor(batch
, rsrc
->damage
.extent
.minx
,
1389 rsrc
->damage
.extent
.miny
,
1390 rsrc
->damage
.extent
.maxx
,
1391 rsrc
->damage
.extent
.maxy
);
1394 /* FIXME: Looks like aligning on a tile is not enough, but
1395 * aligning on twice the tile size seems to works. We don't
1396 * know exactly what happens here but this deserves extra
1397 * investigation to figure it out.
1399 batch
->minx
= batch
->minx
& ~((MALI_TILE_LENGTH
* 2) - 1);
1400 batch
->miny
= batch
->miny
& ~((MALI_TILE_LENGTH
* 2) - 1);
1401 batch
->maxx
= MIN2(ALIGN_POT(batch
->maxx
, MALI_TILE_LENGTH
* 2),
1403 batch
->maxy
= MIN2(ALIGN_POT(batch
->maxy
, MALI_TILE_LENGTH
* 2),
1404 rsrc
->base
.height0
);
1406 struct pipe_scissor_state damage
;
1407 struct pipe_box rects
[4];
1409 /* Clamp the damage box to the rendering area. */
1410 damage
.minx
= MAX2(batch
->minx
, rsrc
->damage
.biggest_rect
.x
);
1411 damage
.miny
= MAX2(batch
->miny
, rsrc
->damage
.biggest_rect
.y
);
1412 damage
.maxx
= MIN2(batch
->maxx
,
1413 rsrc
->damage
.biggest_rect
.x
+
1414 rsrc
->damage
.biggest_rect
.width
);
1415 damage
.maxy
= MIN2(batch
->maxy
,
1416 rsrc
->damage
.biggest_rect
.y
+
1417 rsrc
->damage
.biggest_rect
.height
);
1419 /* One damage rectangle means we can end up with at most 4 reload
1421 * 1: left region, only exists if damage.x > 0
1422 * 2: right region, only exists if damage.x + damage.width < fb->width
1423 * 3: top region, only exists if damage.y > 0. The intersection with
1424 * the left and right regions are dropped
1425 * 4: bottom region, only exists if damage.y + damage.height < fb->height.
1426 * The intersection with the left and right regions are dropped
1428 * ____________________________
1435 * |_______|___________|______|
1437 u_box_2d(batch
->minx
, batch
->miny
, damage
.minx
- batch
->minx
,
1438 batch
->maxy
- batch
->miny
, &rects
[0]);
1439 u_box_2d(damage
.maxx
, batch
->miny
, batch
->maxx
- damage
.maxx
,
1440 batch
->maxy
- batch
->miny
, &rects
[1]);
1441 u_box_2d(damage
.minx
, batch
->miny
, damage
.maxx
- damage
.minx
,
1442 damage
.miny
- batch
->miny
, &rects
[2]);
1443 u_box_2d(damage
.minx
, damage
.maxy
, damage
.maxx
- damage
.minx
,
1444 batch
->maxy
- damage
.maxy
, &rects
[3]);
1446 for (unsigned i
= 0; i
< 4; i
++) {
1447 /* Width and height are always >= 0 even if width is declared as a
1448 * signed integer: u_box_2d() helper takes unsigned args and
1449 * panfrost_set_damage_region() is taking care of clamping
1452 if (!rects
[i
].width
|| !rects
[i
].height
)
1455 /* Blit the wallpaper in */
1456 panfrost_blit_wallpaper(ctx
, &rects
[i
]);
1458 ctx
->wallpaper_batch
= NULL
;
1463 struct pipe_context
*pipe
,
1464 struct pipe_fence_handle
**fence
,
1467 struct panfrost_context
*ctx
= pan_context(pipe
);
1468 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
1470 /* Nothing to do! */
1471 if (!job
->last_job
.gpu
&& !job
->clear
) return;
1473 if (!job
->clear
&& job
->last_tiler
.gpu
)
1474 panfrost_draw_wallpaper(&ctx
->base
);
1476 /* Whether to stall the pipeline for immediately correct results. Since
1477 * pipelined rendering is quite broken right now (to be fixed by the
1478 * panfrost_job refactor, just take the perf hit for correctness) */
1479 bool flush_immediate
= /*flags & PIPE_FLUSH_END_OF_FRAME*/true;
1481 /* Submit the frame itself */
1482 panfrost_submit_frame(ctx
, flush_immediate
, fence
, job
);
1484 /* Prepare for the next frame */
1485 panfrost_invalidate_frame(ctx
);
1488 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1491 g2m_draw_mode(enum pipe_prim_type mode
)
1494 DEFINE_CASE(POINTS
);
1496 DEFINE_CASE(LINE_LOOP
);
1497 DEFINE_CASE(LINE_STRIP
);
1498 DEFINE_CASE(TRIANGLES
);
1499 DEFINE_CASE(TRIANGLE_STRIP
);
1500 DEFINE_CASE(TRIANGLE_FAN
);
1502 DEFINE_CASE(QUAD_STRIP
);
1503 DEFINE_CASE(POLYGON
);
1506 unreachable("Invalid draw mode");
1513 panfrost_translate_index_size(unsigned size
)
1517 return MALI_DRAW_INDEXED_UINT8
;
1520 return MALI_DRAW_INDEXED_UINT16
;
1523 return MALI_DRAW_INDEXED_UINT32
;
1526 unreachable("Invalid index size");
1530 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1531 * good for the duration of the draw (transient), could last longer */
1534 panfrost_get_index_buffer_mapped(struct panfrost_context
*ctx
, const struct pipe_draw_info
*info
)
1536 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (info
->index
.resource
);
1538 off_t offset
= info
->start
* info
->index_size
;
1539 struct panfrost_job
*batch
= panfrost_get_job_for_fbo(ctx
);
1541 if (!info
->has_user_indices
) {
1542 /* Only resources can be directly mapped */
1543 panfrost_job_add_bo(batch
, rsrc
->bo
);
1544 return rsrc
->bo
->gpu
+ offset
;
1546 /* Otherwise, we need to upload to transient memory */
1547 const uint8_t *ibuf8
= (const uint8_t *) info
->index
.user
;
1548 return panfrost_upload_transient(ctx
, ibuf8
+ offset
, info
->count
* info
->index_size
);
1553 panfrost_scissor_culls_everything(struct panfrost_context
*ctx
)
1555 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
1557 /* Check if we're scissoring at all */
1559 if (!(ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
))
1562 return (ss
->minx
== ss
->maxx
) || (ss
->miny
== ss
->maxy
);
1565 /* Count generated primitives (when there is no geom/tess shaders) for
1566 * transform feedback */
1569 panfrost_statistics_record(
1570 struct panfrost_context
*ctx
,
1571 const struct pipe_draw_info
*info
)
1573 if (!ctx
->active_queries
)
1576 uint32_t prims
= u_prims_for_vertices(info
->mode
, info
->count
);
1577 ctx
->prims_generated
+= prims
;
1579 if (ctx
->streamout
.num_targets
<= 0)
1582 ctx
->tf_prims_generated
+= prims
;
1587 struct pipe_context
*pipe
,
1588 const struct pipe_draw_info
*info
)
1590 struct panfrost_context
*ctx
= pan_context(pipe
);
1592 /* First of all, check the scissor to see if anything is drawn at all.
1593 * If it's not, we drop the draw (mostly a conformance issue;
1594 * well-behaved apps shouldn't hit this) */
1596 if (panfrost_scissor_culls_everything(ctx
))
1599 ctx
->payloads
[PIPE_SHADER_VERTEX
].offset_start
= info
->start
;
1600 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].offset_start
= info
->start
;
1602 int mode
= info
->mode
;
1604 /* Fallback unsupported restart index */
1605 unsigned primitive_index
= (1 << (info
->index_size
* 8)) - 1;
1607 if (info
->primitive_restart
&& info
->index_size
1608 && info
->restart_index
!= primitive_index
) {
1609 util_draw_vbo_without_prim_restart(pipe
, info
);
1613 /* Fallback for unsupported modes */
1615 if (!(ctx
->draw_modes
& (1 << mode
))) {
1616 if (mode
== PIPE_PRIM_QUADS
&& info
->count
== 4 && ctx
->rasterizer
&& !ctx
->rasterizer
->base
.flatshade
) {
1617 mode
= PIPE_PRIM_TRIANGLE_FAN
;
1619 if (info
->count
< 4) {
1620 /* Degenerate case? */
1624 util_primconvert_save_rasterizer_state(ctx
->primconvert
, &ctx
->rasterizer
->base
);
1625 util_primconvert_draw_vbo(ctx
->primconvert
, info
);
1630 /* Now that we have a guaranteed terminating path, find the job.
1631 * Assignment commented out to prevent unused warning */
1633 /* struct panfrost_job *job = */ panfrost_get_job_for_fbo(ctx
);
1635 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.draw_mode
= g2m_draw_mode(mode
);
1637 /* Take into account a negative bias */
1638 ctx
->vertex_count
= info
->count
+ abs(info
->index_bias
);
1639 ctx
->instance_count
= info
->instance_count
;
1640 ctx
->active_prim
= info
->mode
;
1642 /* For non-indexed draws, they're the same */
1643 unsigned vertex_count
= ctx
->vertex_count
;
1645 unsigned draw_flags
= 0;
1647 /* The draw flags interpret how primitive size is interpreted */
1649 if (panfrost_writes_point_size(ctx
))
1650 draw_flags
|= MALI_DRAW_VARYING_SIZE
;
1652 if (info
->primitive_restart
)
1653 draw_flags
|= MALI_DRAW_PRIMITIVE_RESTART_FIXED_INDEX
;
1655 /* For higher amounts of vertices (greater than what fits in a 16-bit
1656 * short), the other value is needed, otherwise there will be bizarre
1657 * rendering artefacts. It's not clear what these values mean yet. This
1658 * change is also needed for instancing and sometimes points (perhaps
1659 * related to dynamically setting gl_PointSize) */
1661 bool is_points
= mode
== PIPE_PRIM_POINTS
;
1662 bool many_verts
= ctx
->vertex_count
> 0xFFFF;
1663 bool instanced
= ctx
->instance_count
> 1;
1665 draw_flags
|= (is_points
|| many_verts
|| instanced
) ? 0x3000 : 0x18000;
1667 /* This doesn't make much sense */
1668 if (mode
== PIPE_PRIM_LINE_STRIP
) {
1669 draw_flags
|= 0x800;
1672 panfrost_statistics_record(ctx
, info
);
1674 if (info
->index_size
) {
1675 /* Calculate the min/max index used so we can figure out how
1676 * many times to invoke the vertex shader */
1678 /* Fetch / calculate index bounds */
1679 unsigned min_index
= 0, max_index
= 0;
1681 if (info
->max_index
== ~0u) {
1682 u_vbuf_get_minmax_index(pipe
, info
, &min_index
, &max_index
);
1684 min_index
= info
->min_index
;
1685 max_index
= info
->max_index
;
1688 /* Use the corresponding values */
1689 vertex_count
= max_index
- min_index
+ 1;
1690 ctx
->payloads
[PIPE_SHADER_VERTEX
].offset_start
= min_index
+ info
->index_bias
;
1691 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].offset_start
= min_index
+ info
->index_bias
;
1693 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.offset_bias_correction
= -min_index
;
1694 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.index_count
= MALI_POSITIVE(info
->count
);
1696 //assert(!info->restart_index); /* TODO: Research */
1698 draw_flags
|= panfrost_translate_index_size(info
->index_size
);
1699 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.indices
= panfrost_get_index_buffer_mapped(ctx
, info
);
1701 /* Index count == vertex count, if no indexing is applied, as
1702 * if it is internally indexed in the expected order */
1704 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.offset_bias_correction
= 0;
1705 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.index_count
= MALI_POSITIVE(ctx
->vertex_count
);
1707 /* Reverse index state */
1708 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.indices
= (u64
) NULL
;
1711 /* Dispatch "compute jobs" for the vertex/tiler pair as (1,
1712 * vertex_count, 1) */
1714 panfrost_pack_work_groups_fused(
1715 &ctx
->payloads
[PIPE_SHADER_VERTEX
].prefix
,
1716 &ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
,
1717 1, vertex_count
, info
->instance_count
,
1720 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.unknown_draw
= draw_flags
;
1722 /* Encode the padded vertex count */
1724 if (info
->instance_count
> 1) {
1725 /* Triangles have non-even vertex counts so they change how
1726 * padding works internally */
1729 mode
== PIPE_PRIM_TRIANGLES
||
1730 mode
== PIPE_PRIM_TRIANGLE_STRIP
||
1731 mode
== PIPE_PRIM_TRIANGLE_FAN
;
1733 struct pan_shift_odd so
=
1734 panfrost_padded_vertex_count(vertex_count
, !is_triangle
);
1736 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_shift
= so
.shift
;
1737 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_shift
= so
.shift
;
1739 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_odd
= so
.odd
;
1740 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_odd
= so
.odd
;
1742 ctx
->padded_count
= pan_expand_shift_odd(so
);
1744 ctx
->padded_count
= ctx
->vertex_count
;
1746 /* Reset instancing state */
1747 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_shift
= 0;
1748 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_odd
= 0;
1749 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_shift
= 0;
1750 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_odd
= 0;
1753 /* Fire off the draw itself */
1754 panfrost_queue_draw(ctx
);
1756 /* Increment transform feedback offsets */
1758 for (unsigned i
= 0; i
< ctx
->streamout
.num_targets
; ++i
) {
1759 unsigned output_count
= u_stream_outputs_for_vertices(
1760 ctx
->active_prim
, ctx
->vertex_count
);
1762 ctx
->streamout
.offsets
[i
] += output_count
;
1769 panfrost_generic_cso_delete(struct pipe_context
*pctx
, void *hwcso
)
1775 panfrost_create_rasterizer_state(
1776 struct pipe_context
*pctx
,
1777 const struct pipe_rasterizer_state
*cso
)
1779 struct panfrost_rasterizer
*so
= CALLOC_STRUCT(panfrost_rasterizer
);
1783 /* Bitmask, unknown meaning of the start value. 0x105 on 32-bit T6XX */
1784 so
->tiler_gl_enables
= 0x7;
1787 so
->tiler_gl_enables
|= MALI_FRONT_CCW_TOP
;
1789 if (cso
->cull_face
& PIPE_FACE_FRONT
)
1790 so
->tiler_gl_enables
|= MALI_CULL_FACE_FRONT
;
1792 if (cso
->cull_face
& PIPE_FACE_BACK
)
1793 so
->tiler_gl_enables
|= MALI_CULL_FACE_BACK
;
1799 panfrost_bind_rasterizer_state(
1800 struct pipe_context
*pctx
,
1803 struct panfrost_context
*ctx
= pan_context(pctx
);
1805 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1809 ctx
->rasterizer
= hwcso
;
1810 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
1812 ctx
->fragment_shader_core
.depth_units
= ctx
->rasterizer
->base
.offset_units
;
1813 ctx
->fragment_shader_core
.depth_factor
= ctx
->rasterizer
->base
.offset_scale
;
1815 /* Gauranteed with the core GL call, so don't expose ARB_polygon_offset */
1816 assert(ctx
->rasterizer
->base
.offset_clamp
== 0.0);
1818 /* XXX: Which bit is which? Does this maybe allow offseting not-tri? */
1820 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_DEPTH_RANGE_A
, ctx
->rasterizer
->base
.offset_tri
);
1821 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_DEPTH_RANGE_B
, ctx
->rasterizer
->base
.offset_tri
);
1823 /* Point sprites are emulated */
1825 struct panfrost_shader_state
*variant
=
1826 ctx
->shader
[PIPE_SHADER_FRAGMENT
] ? &ctx
->shader
[PIPE_SHADER_FRAGMENT
]->variants
[ctx
->shader
[PIPE_SHADER_FRAGMENT
]->active_variant
] : NULL
;
1828 if (ctx
->rasterizer
->base
.sprite_coord_enable
|| (variant
&& variant
->point_sprite_mask
))
1829 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->shader
[PIPE_SHADER_FRAGMENT
]);
1833 panfrost_create_vertex_elements_state(
1834 struct pipe_context
*pctx
,
1835 unsigned num_elements
,
1836 const struct pipe_vertex_element
*elements
)
1838 struct panfrost_vertex_state
*so
= CALLOC_STRUCT(panfrost_vertex_state
);
1840 so
->num_elements
= num_elements
;
1841 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
1843 for (int i
= 0; i
< num_elements
; ++i
) {
1844 so
->hw
[i
].index
= i
;
1846 enum pipe_format fmt
= elements
[i
].src_format
;
1847 const struct util_format_description
*desc
= util_format_description(fmt
);
1848 so
->hw
[i
].unknown1
= 0x2;
1849 so
->hw
[i
].swizzle
= panfrost_get_default_swizzle(desc
->nr_channels
);
1851 so
->hw
[i
].format
= panfrost_find_format(desc
);
1853 /* The field itself should probably be shifted over */
1854 so
->hw
[i
].src_offset
= elements
[i
].src_offset
;
1861 panfrost_bind_vertex_elements_state(
1862 struct pipe_context
*pctx
,
1865 struct panfrost_context
*ctx
= pan_context(pctx
);
1867 ctx
->vertex
= hwcso
;
1868 ctx
->dirty
|= PAN_DIRTY_VERTEX
;
1872 panfrost_create_shader_state(
1873 struct pipe_context
*pctx
,
1874 const struct pipe_shader_state
*cso
)
1876 struct panfrost_shader_variants
*so
= CALLOC_STRUCT(panfrost_shader_variants
);
1879 /* Token deep copy to prevent memory corruption */
1881 if (cso
->type
== PIPE_SHADER_IR_TGSI
)
1882 so
->base
.tokens
= tgsi_dup_tokens(so
->base
.tokens
);
1888 panfrost_delete_shader_state(
1889 struct pipe_context
*pctx
,
1892 struct panfrost_shader_variants
*cso
= (struct panfrost_shader_variants
*) so
;
1894 if (cso
->base
.type
== PIPE_SHADER_IR_TGSI
) {
1895 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1898 for (unsigned i
= 0; i
< cso
->variant_count
; ++i
) {
1899 struct panfrost_shader_state
*shader_state
= &cso
->variants
[i
];
1900 panfrost_bo_unreference(pctx
->screen
, shader_state
->bo
);
1901 shader_state
->bo
= NULL
;
1908 panfrost_create_sampler_state(
1909 struct pipe_context
*pctx
,
1910 const struct pipe_sampler_state
*cso
)
1912 struct panfrost_sampler_state
*so
= CALLOC_STRUCT(panfrost_sampler_state
);
1915 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1917 bool min_nearest
= cso
->min_img_filter
== PIPE_TEX_FILTER_NEAREST
;
1918 bool mag_nearest
= cso
->mag_img_filter
== PIPE_TEX_FILTER_NEAREST
;
1919 bool mip_linear
= cso
->min_mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
;
1921 unsigned min_filter
= min_nearest
? MALI_SAMP_MIN_NEAREST
: 0;
1922 unsigned mag_filter
= mag_nearest
? MALI_SAMP_MAG_NEAREST
: 0;
1923 unsigned mip_filter
= mip_linear
?
1924 (MALI_SAMP_MIP_LINEAR_1
| MALI_SAMP_MIP_LINEAR_2
) : 0;
1925 unsigned normalized
= cso
->normalized_coords
? MALI_SAMP_NORM_COORDS
: 0;
1927 struct mali_sampler_descriptor sampler_descriptor
= {
1928 .filter_mode
= min_filter
| mag_filter
| mip_filter
| normalized
,
1929 .wrap_s
= translate_tex_wrap(cso
->wrap_s
),
1930 .wrap_t
= translate_tex_wrap(cso
->wrap_t
),
1931 .wrap_r
= translate_tex_wrap(cso
->wrap_r
),
1932 .compare_func
= panfrost_translate_alt_compare_func(cso
->compare_func
),
1934 cso
->border_color
.f
[0],
1935 cso
->border_color
.f
[1],
1936 cso
->border_color
.f
[2],
1937 cso
->border_color
.f
[3]
1939 .min_lod
= FIXED_16(cso
->min_lod
),
1940 .max_lod
= FIXED_16(cso
->max_lod
),
1941 .seamless_cube_map
= cso
->seamless_cube_map
,
1944 /* If necessary, we disable mipmapping in the sampler descriptor by
1945 * clamping the LOD as tight as possible (from 0 to epsilon,
1946 * essentially -- remember these are fixed point numbers, so
1949 if (cso
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
)
1950 sampler_descriptor
.max_lod
= sampler_descriptor
.min_lod
;
1952 /* Enforce that there is something in the middle by adding epsilon*/
1954 if (sampler_descriptor
.min_lod
== sampler_descriptor
.max_lod
)
1955 sampler_descriptor
.max_lod
++;
1958 assert(sampler_descriptor
.max_lod
> sampler_descriptor
.min_lod
);
1960 so
->hw
= sampler_descriptor
;
1966 panfrost_bind_sampler_states(
1967 struct pipe_context
*pctx
,
1968 enum pipe_shader_type shader
,
1969 unsigned start_slot
, unsigned num_sampler
,
1972 assert(start_slot
== 0);
1974 struct panfrost_context
*ctx
= pan_context(pctx
);
1976 /* XXX: Should upload, not just copy? */
1977 ctx
->sampler_count
[shader
] = num_sampler
;
1978 memcpy(ctx
->samplers
[shader
], sampler
, num_sampler
* sizeof (void *));
1980 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
;
1984 panfrost_variant_matches(
1985 struct panfrost_context
*ctx
,
1986 struct panfrost_shader_state
*variant
,
1987 enum pipe_shader_type type
)
1989 struct pipe_rasterizer_state
*rasterizer
= &ctx
->rasterizer
->base
;
1990 struct pipe_alpha_state
*alpha
= &ctx
->depth_stencil
->alpha
;
1992 bool is_fragment
= (type
== PIPE_SHADER_FRAGMENT
);
1994 if (is_fragment
&& (alpha
->enabled
|| variant
->alpha_state
.enabled
)) {
1995 /* Make sure enable state is at least the same */
1996 if (alpha
->enabled
!= variant
->alpha_state
.enabled
) {
2000 /* Check that the contents of the test are the same */
2001 bool same_func
= alpha
->func
== variant
->alpha_state
.func
;
2002 bool same_ref
= alpha
->ref_value
== variant
->alpha_state
.ref_value
;
2004 if (!(same_func
&& same_ref
)) {
2009 if (is_fragment
&& rasterizer
&& (rasterizer
->sprite_coord_enable
|
2010 variant
->point_sprite_mask
)) {
2011 /* Ensure the same varyings are turned to point sprites */
2012 if (rasterizer
->sprite_coord_enable
!= variant
->point_sprite_mask
)
2015 /* Ensure the orientation is correct */
2017 rasterizer
->sprite_coord_mode
==
2018 PIPE_SPRITE_COORD_UPPER_LEFT
;
2020 if (variant
->point_sprite_upper_left
!= upper_left
)
2024 /* Otherwise, we're good to go */
2029 * Fix an uncompiled shader's stream output info, and produce a bitmask
2030 * of which VARYING_SLOT_* are captured for stream output.
2032 * Core Gallium stores output->register_index as a "slot" number, where
2033 * slots are assigned consecutively to all outputs in info->outputs_written.
2034 * This naive packing of outputs doesn't work for us - we too have slots,
2035 * but the layout is defined by the VUE map, which we won't have until we
2036 * compile a specific shader variant. So, we remap these and simply store
2037 * VARYING_SLOT_* in our copy's output->register_index fields.
2039 * We then produce a bitmask of outputs which are used for SO.
2041 * Implementation from iris.
2045 update_so_info(struct pipe_stream_output_info
*so_info
,
2046 uint64_t outputs_written
)
2048 uint64_t so_outputs
= 0;
2049 uint8_t reverse_map
[64] = {};
2052 while (outputs_written
)
2053 reverse_map
[slot
++] = u_bit_scan64(&outputs_written
);
2055 for (unsigned i
= 0; i
< so_info
->num_outputs
; i
++) {
2056 struct pipe_stream_output
*output
= &so_info
->output
[i
];
2058 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
2059 output
->register_index
= reverse_map
[output
->register_index
];
2061 so_outputs
|= 1ull << output
->register_index
;
2068 panfrost_bind_shader_state(
2069 struct pipe_context
*pctx
,
2071 enum pipe_shader_type type
)
2073 struct panfrost_context
*ctx
= pan_context(pctx
);
2075 ctx
->shader
[type
] = hwcso
;
2077 if (type
== PIPE_SHADER_FRAGMENT
)
2078 ctx
->dirty
|= PAN_DIRTY_FS
;
2080 ctx
->dirty
|= PAN_DIRTY_VS
;
2084 /* Match the appropriate variant */
2086 signed variant
= -1;
2087 struct panfrost_shader_variants
*variants
= (struct panfrost_shader_variants
*) hwcso
;
2089 for (unsigned i
= 0; i
< variants
->variant_count
; ++i
) {
2090 if (panfrost_variant_matches(ctx
, &variants
->variants
[i
], type
)) {
2096 if (variant
== -1) {
2097 /* No variant matched, so create a new one */
2098 variant
= variants
->variant_count
++;
2099 assert(variants
->variant_count
< MAX_SHADER_VARIANTS
);
2101 struct panfrost_shader_state
*v
=
2102 &variants
->variants
[variant
];
2104 if (type
== PIPE_SHADER_FRAGMENT
) {
2105 v
->alpha_state
= ctx
->depth_stencil
->alpha
;
2107 if (ctx
->rasterizer
) {
2108 v
->point_sprite_mask
= ctx
->rasterizer
->base
.sprite_coord_enable
;
2109 v
->point_sprite_upper_left
=
2110 ctx
->rasterizer
->base
.sprite_coord_mode
==
2111 PIPE_SPRITE_COORD_UPPER_LEFT
;
2115 variants
->variants
[variant
].tripipe
= calloc(1, sizeof(struct mali_shader_meta
));
2119 /* Select this variant */
2120 variants
->active_variant
= variant
;
2122 struct panfrost_shader_state
*shader_state
= &variants
->variants
[variant
];
2123 assert(panfrost_variant_matches(ctx
, shader_state
, type
));
2125 /* We finally have a variant, so compile it */
2127 if (!shader_state
->compiled
) {
2128 uint64_t outputs_written
= 0;
2130 panfrost_shader_compile(ctx
, shader_state
->tripipe
,
2131 variants
->base
.type
,
2132 variants
->base
.type
== PIPE_SHADER_IR_NIR
?
2133 variants
->base
.ir
.nir
:
2134 variants
->base
.tokens
,
2135 tgsi_processor_to_shader_stage(type
), shader_state
,
2138 shader_state
->compiled
= true;
2140 /* Fixup the stream out information, since what Gallium returns
2141 * normally is mildly insane */
2143 shader_state
->stream_output
= variants
->base
.stream_output
;
2144 shader_state
->so_mask
=
2145 update_so_info(&shader_state
->stream_output
, outputs_written
);
2150 panfrost_bind_vs_state(struct pipe_context
*pctx
, void *hwcso
)
2152 panfrost_bind_shader_state(pctx
, hwcso
, PIPE_SHADER_VERTEX
);
2156 panfrost_bind_fs_state(struct pipe_context
*pctx
, void *hwcso
)
2158 panfrost_bind_shader_state(pctx
, hwcso
, PIPE_SHADER_FRAGMENT
);
2162 panfrost_set_vertex_buffers(
2163 struct pipe_context
*pctx
,
2164 unsigned start_slot
,
2165 unsigned num_buffers
,
2166 const struct pipe_vertex_buffer
*buffers
)
2168 struct panfrost_context
*ctx
= pan_context(pctx
);
2170 util_set_vertex_buffers_mask(ctx
->vertex_buffers
, &ctx
->vb_mask
, buffers
, start_slot
, num_buffers
);
2174 panfrost_set_constant_buffer(
2175 struct pipe_context
*pctx
,
2176 enum pipe_shader_type shader
, uint index
,
2177 const struct pipe_constant_buffer
*buf
)
2179 struct panfrost_context
*ctx
= pan_context(pctx
);
2180 struct panfrost_constant_buffer
*pbuf
= &ctx
->constant_buffer
[shader
];
2182 util_copy_constant_buffer(&pbuf
->cb
[index
], buf
);
2184 unsigned mask
= (1 << index
);
2186 if (unlikely(!buf
)) {
2187 pbuf
->enabled_mask
&= ~mask
;
2188 pbuf
->dirty_mask
&= ~mask
;
2192 pbuf
->enabled_mask
|= mask
;
2193 pbuf
->dirty_mask
|= mask
;
2197 panfrost_set_stencil_ref(
2198 struct pipe_context
*pctx
,
2199 const struct pipe_stencil_ref
*ref
)
2201 struct panfrost_context
*ctx
= pan_context(pctx
);
2202 ctx
->stencil_ref
= *ref
;
2204 /* Shader core dirty */
2205 ctx
->dirty
|= PAN_DIRTY_FS
;
2208 static enum mali_texture_type
2209 panfrost_translate_texture_type(enum pipe_texture_target t
) {
2213 case PIPE_TEXTURE_1D
:
2214 case PIPE_TEXTURE_1D_ARRAY
:
2217 case PIPE_TEXTURE_2D
:
2218 case PIPE_TEXTURE_2D_ARRAY
:
2219 case PIPE_TEXTURE_RECT
:
2222 case PIPE_TEXTURE_3D
:
2225 case PIPE_TEXTURE_CUBE
:
2226 case PIPE_TEXTURE_CUBE_ARRAY
:
2227 return MALI_TEX_CUBE
;
2230 unreachable("Unknown target");
2234 static struct pipe_sampler_view
*
2235 panfrost_create_sampler_view(
2236 struct pipe_context
*pctx
,
2237 struct pipe_resource
*texture
,
2238 const struct pipe_sampler_view
*template)
2240 struct panfrost_sampler_view
*so
= rzalloc(pctx
, struct panfrost_sampler_view
);
2241 int bytes_per_pixel
= util_format_get_blocksize(texture
->format
);
2243 pipe_reference(NULL
, &texture
->reference
);
2245 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) texture
;
2248 so
->base
= *template;
2249 so
->base
.texture
= texture
;
2250 so
->base
.reference
.count
= 1;
2251 so
->base
.context
= pctx
;
2253 /* sampler_views correspond to texture descriptors, minus the texture
2254 * (data) itself. So, we serialise the descriptor here and cache it for
2257 /* TODO: Detect from format better */
2258 const struct util_format_description
*desc
= util_format_description(prsrc
->base
.format
);
2260 unsigned char user_swizzle
[4] = {
2261 template->swizzle_r
,
2262 template->swizzle_g
,
2263 template->swizzle_b
,
2267 enum mali_format format
= panfrost_find_format(desc
);
2269 /* Check if we need to set a custom stride by computing the "expected"
2270 * stride and comparing it to what the BO actually wants. Only applies
2271 * to linear textures, since tiled/compressed textures have strict
2272 * alignment requirements for their strides as it is */
2274 unsigned first_level
= template->u
.tex
.first_level
;
2275 unsigned last_level
= template->u
.tex
.last_level
;
2277 if (prsrc
->layout
== PAN_LINEAR
) {
2278 for (unsigned l
= first_level
; l
<= last_level
; ++l
) {
2279 unsigned actual_stride
= prsrc
->slices
[l
].stride
;
2280 unsigned width
= u_minify(texture
->width0
, l
);
2281 unsigned comp_stride
= width
* bytes_per_pixel
;
2283 if (comp_stride
!= actual_stride
) {
2284 so
->manual_stride
= true;
2290 /* In the hardware, array_size refers specifically to array textures,
2291 * whereas in Gallium, it also covers cubemaps */
2293 unsigned array_size
= texture
->array_size
;
2295 if (template->target
== PIPE_TEXTURE_CUBE
) {
2296 /* TODO: Cubemap arrays */
2297 assert(array_size
== 6);
2301 struct mali_texture_descriptor texture_descriptor
= {
2302 .width
= MALI_POSITIVE(u_minify(texture
->width0
, first_level
)),
2303 .height
= MALI_POSITIVE(u_minify(texture
->height0
, first_level
)),
2304 .depth
= MALI_POSITIVE(u_minify(texture
->depth0
, first_level
)),
2305 .array_size
= MALI_POSITIVE(array_size
),
2309 .swizzle
= panfrost_translate_swizzle_4(desc
->swizzle
),
2312 .srgb
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
,
2313 .type
= panfrost_translate_texture_type(template->target
),
2316 .swizzle
= panfrost_translate_swizzle_4(user_swizzle
)
2319 texture_descriptor
.levels
= last_level
- first_level
;
2321 so
->hw
= texture_descriptor
;
2323 return (struct pipe_sampler_view
*) so
;
2327 panfrost_set_sampler_views(
2328 struct pipe_context
*pctx
,
2329 enum pipe_shader_type shader
,
2330 unsigned start_slot
, unsigned num_views
,
2331 struct pipe_sampler_view
**views
)
2333 struct panfrost_context
*ctx
= pan_context(pctx
);
2335 assert(start_slot
== 0);
2337 unsigned new_nr
= 0;
2338 for (unsigned i
= 0; i
< num_views
; ++i
) {
2343 ctx
->sampler_view_count
[shader
] = new_nr
;
2344 memcpy(ctx
->sampler_views
[shader
], views
, num_views
* sizeof (void *));
2346 ctx
->dirty
|= PAN_DIRTY_TEXTURES
;
2350 panfrost_sampler_view_destroy(
2351 struct pipe_context
*pctx
,
2352 struct pipe_sampler_view
*view
)
2354 pipe_resource_reference(&view
->texture
, NULL
);
2359 panfrost_set_shader_buffers(
2360 struct pipe_context
*pctx
,
2361 enum pipe_shader_type shader
,
2362 unsigned start
, unsigned count
,
2363 const struct pipe_shader_buffer
*buffers
,
2364 unsigned writable_bitmask
)
2366 struct panfrost_context
*ctx
= pan_context(pctx
);
2368 util_set_shader_buffers_mask(ctx
->ssbo
[shader
], &ctx
->ssbo_mask
[shader
],
2369 buffers
, start
, count
);
2372 /* Hints that a framebuffer should use AFBC where possible */
2376 struct panfrost_screen
*screen
,
2377 const struct pipe_framebuffer_state
*fb
)
2379 /* AFBC implemenation incomplete; hide it */
2380 if (!(pan_debug
& PAN_DBG_AFBC
)) return;
2382 /* Hint AFBC to the resources bound to each color buffer */
2384 for (unsigned i
= 0; i
< fb
->nr_cbufs
; ++i
) {
2385 struct pipe_surface
*surf
= fb
->cbufs
[i
];
2386 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
2387 panfrost_resource_hint_layout(screen
, rsrc
, PAN_AFBC
, 1);
2390 /* Also hint it to the depth buffer */
2393 struct panfrost_resource
*rsrc
= pan_resource(fb
->zsbuf
->texture
);
2394 panfrost_resource_hint_layout(screen
, rsrc
, PAN_AFBC
, 1);
2399 panfrost_set_framebuffer_state(struct pipe_context
*pctx
,
2400 const struct pipe_framebuffer_state
*fb
)
2402 struct panfrost_context
*ctx
= pan_context(pctx
);
2404 /* Flush when switching framebuffers, but not if the framebuffer
2405 * state is being restored by u_blitter
2408 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
2409 bool is_scanout
= panfrost_is_scanout(ctx
);
2410 bool has_draws
= job
->last_job
.gpu
;
2412 /* Bail out early when the current and new states are the same. */
2413 if (util_framebuffer_state_equal(&ctx
->pipe_framebuffer
, fb
))
2416 /* The wallpaper logic sets a new FB state before doing the blit and
2417 * restore the old one when it's done. Those FB states are reported to
2418 * be different because the surface they are pointing to are different,
2419 * but those surfaces actually point to the same cbufs/zbufs. In that
2420 * case we definitely don't want new FB descs to be emitted/attached
2421 * since the job is expected to be flushed just after the blit is done,
2422 * so let's just copy the new state and return here.
2424 if (ctx
->wallpaper_batch
) {
2425 util_copy_framebuffer_state(&ctx
->pipe_framebuffer
, fb
);
2429 if (!is_scanout
|| has_draws
)
2430 panfrost_flush(pctx
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2432 assert(!ctx
->payloads
[PIPE_SHADER_VERTEX
].postfix
.framebuffer
&&
2433 !ctx
->payloads
[PIPE_SHADER_FRAGMENT
].postfix
.framebuffer
);
2435 /* Invalidate the FBO job cache since we've just been assigned a new
2440 util_copy_framebuffer_state(&ctx
->pipe_framebuffer
, fb
);
2442 /* Given that we're rendering, we'd love to have compression */
2443 struct panfrost_screen
*screen
= pan_screen(ctx
->base
.screen
);
2445 panfrost_hint_afbc(screen
, &ctx
->pipe_framebuffer
);
2446 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; ++i
)
2447 ctx
->payloads
[i
].postfix
.framebuffer
= 0;
2451 panfrost_create_depth_stencil_state(struct pipe_context
*pipe
,
2452 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
2454 return mem_dup(depth_stencil
, sizeof(*depth_stencil
));
2458 panfrost_bind_depth_stencil_state(struct pipe_context
*pipe
,
2461 struct panfrost_context
*ctx
= pan_context(pipe
);
2462 struct pipe_depth_stencil_alpha_state
*depth_stencil
= cso
;
2463 ctx
->depth_stencil
= depth_stencil
;
2468 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2469 * emulated in the fragment shader */
2471 if (depth_stencil
->alpha
.enabled
) {
2472 /* We need to trigger a new shader (maybe) */
2473 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->shader
[PIPE_SHADER_FRAGMENT
]);
2477 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_STENCIL_TEST
, depth_stencil
->stencil
[0].enabled
);
2479 panfrost_make_stencil_state(&depth_stencil
->stencil
[0], &ctx
->fragment_shader_core
.stencil_front
);
2480 ctx
->fragment_shader_core
.stencil_mask_front
= depth_stencil
->stencil
[0].writemask
;
2482 /* If back-stencil is not enabled, use the front values */
2483 bool back_enab
= ctx
->depth_stencil
->stencil
[1].enabled
;
2484 unsigned back_index
= back_enab
? 1 : 0;
2486 panfrost_make_stencil_state(&depth_stencil
->stencil
[back_index
], &ctx
->fragment_shader_core
.stencil_back
);
2487 ctx
->fragment_shader_core
.stencil_mask_back
= depth_stencil
->stencil
[back_index
].writemask
;
2489 /* Depth state (TODO: Refactor) */
2490 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_DEPTH_TEST
, depth_stencil
->depth
.enabled
);
2492 int func
= depth_stencil
->depth
.enabled
? depth_stencil
->depth
.func
: PIPE_FUNC_ALWAYS
;
2494 ctx
->fragment_shader_core
.unknown2_3
&= ~MALI_DEPTH_FUNC_MASK
;
2495 ctx
->fragment_shader_core
.unknown2_3
|= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func
));
2497 /* Bounds test not implemented */
2498 assert(!depth_stencil
->depth
.bounds_test
);
2500 ctx
->dirty
|= PAN_DIRTY_FS
;
2504 panfrost_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
2510 panfrost_set_sample_mask(struct pipe_context
*pipe
,
2511 unsigned sample_mask
)
2516 panfrost_set_clip_state(struct pipe_context
*pipe
,
2517 const struct pipe_clip_state
*clip
)
2519 //struct panfrost_context *panfrost = pan_context(pipe);
2523 panfrost_set_viewport_states(struct pipe_context
*pipe
,
2524 unsigned start_slot
,
2525 unsigned num_viewports
,
2526 const struct pipe_viewport_state
*viewports
)
2528 struct panfrost_context
*ctx
= pan_context(pipe
);
2530 assert(start_slot
== 0);
2531 assert(num_viewports
== 1);
2533 ctx
->pipe_viewport
= *viewports
;
2537 panfrost_set_scissor_states(struct pipe_context
*pipe
,
2538 unsigned start_slot
,
2539 unsigned num_scissors
,
2540 const struct pipe_scissor_state
*scissors
)
2542 struct panfrost_context
*ctx
= pan_context(pipe
);
2544 assert(start_slot
== 0);
2545 assert(num_scissors
== 1);
2547 ctx
->scissor
= *scissors
;
2551 panfrost_set_polygon_stipple(struct pipe_context
*pipe
,
2552 const struct pipe_poly_stipple
*stipple
)
2554 //struct panfrost_context *panfrost = pan_context(pipe);
2558 panfrost_set_active_query_state(struct pipe_context
*pipe
,
2561 struct panfrost_context
*ctx
= pan_context(pipe
);
2562 ctx
->active_queries
= enable
;
2566 panfrost_destroy(struct pipe_context
*pipe
)
2568 struct panfrost_context
*panfrost
= pan_context(pipe
);
2569 struct panfrost_screen
*screen
= pan_screen(pipe
->screen
);
2571 if (panfrost
->blitter
)
2572 util_blitter_destroy(panfrost
->blitter
);
2574 if (panfrost
->blitter_wallpaper
)
2575 util_blitter_destroy(panfrost
->blitter_wallpaper
);
2577 panfrost_drm_free_slab(screen
, &panfrost
->scratchpad
);
2578 panfrost_drm_free_slab(screen
, &panfrost
->tiler_heap
);
2579 panfrost_drm_free_slab(screen
, &panfrost
->tiler_dummy
);
2584 static struct pipe_query
*
2585 panfrost_create_query(struct pipe_context
*pipe
,
2589 struct panfrost_query
*q
= rzalloc(pipe
, struct panfrost_query
);
2594 return (struct pipe_query
*) q
;
2598 panfrost_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2604 panfrost_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2606 struct panfrost_context
*ctx
= pan_context(pipe
);
2607 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2609 switch (query
->type
) {
2610 case PIPE_QUERY_OCCLUSION_COUNTER
:
2611 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2612 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2613 /* Allocate a word for the query results to be stored */
2614 query
->transfer
= panfrost_allocate_transient(ctx
, sizeof(unsigned));
2615 ctx
->occlusion_query
= query
;
2618 /* Geometry statistics are computed in the driver. XXX: geom/tess
2621 case PIPE_QUERY_PRIMITIVES_GENERATED
:
2622 query
->start
= ctx
->prims_generated
;
2624 case PIPE_QUERY_PRIMITIVES_EMITTED
:
2625 query
->start
= ctx
->tf_prims_generated
;
2629 fprintf(stderr
, "Skipping query %d\n", query
->type
);
2637 panfrost_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2639 struct panfrost_context
*ctx
= pan_context(pipe
);
2640 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2642 switch (query
->type
) {
2643 case PIPE_QUERY_OCCLUSION_COUNTER
:
2644 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2645 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2646 ctx
->occlusion_query
= NULL
;
2648 case PIPE_QUERY_PRIMITIVES_GENERATED
:
2649 query
->end
= ctx
->prims_generated
;
2651 case PIPE_QUERY_PRIMITIVES_EMITTED
:
2652 query
->end
= ctx
->tf_prims_generated
;
2660 panfrost_get_query_result(struct pipe_context
*pipe
,
2661 struct pipe_query
*q
,
2663 union pipe_query_result
*vresult
)
2665 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2668 switch (query
->type
) {
2669 case PIPE_QUERY_OCCLUSION_COUNTER
:
2670 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2671 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2673 panfrost_flush(pipe
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2675 /* Read back the query results */
2676 unsigned *result
= (unsigned *) query
->transfer
.cpu
;
2677 unsigned passed
= *result
;
2679 if (query
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
2680 vresult
->u64
= passed
;
2682 vresult
->b
= !!passed
;
2687 case PIPE_QUERY_PRIMITIVES_GENERATED
:
2688 case PIPE_QUERY_PRIMITIVES_EMITTED
:
2689 panfrost_flush(pipe
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2690 vresult
->u64
= query
->end
- query
->start
;
2694 DBG("Skipped query get %d\n", query
->type
);
2701 static struct pipe_stream_output_target
*
2702 panfrost_create_stream_output_target(struct pipe_context
*pctx
,
2703 struct pipe_resource
*prsc
,
2704 unsigned buffer_offset
,
2705 unsigned buffer_size
)
2707 struct pipe_stream_output_target
*target
;
2709 target
= rzalloc(pctx
, struct pipe_stream_output_target
);
2714 pipe_reference_init(&target
->reference
, 1);
2715 pipe_resource_reference(&target
->buffer
, prsc
);
2717 target
->context
= pctx
;
2718 target
->buffer_offset
= buffer_offset
;
2719 target
->buffer_size
= buffer_size
;
2725 panfrost_stream_output_target_destroy(struct pipe_context
*pctx
,
2726 struct pipe_stream_output_target
*target
)
2728 pipe_resource_reference(&target
->buffer
, NULL
);
2729 ralloc_free(target
);
2733 panfrost_set_stream_output_targets(struct pipe_context
*pctx
,
2734 unsigned num_targets
,
2735 struct pipe_stream_output_target
**targets
,
2736 const unsigned *offsets
)
2738 struct panfrost_context
*ctx
= pan_context(pctx
);
2739 struct panfrost_streamout
*so
= &ctx
->streamout
;
2741 assert(num_targets
<= ARRAY_SIZE(so
->targets
));
2743 for (unsigned i
= 0; i
< num_targets
; i
++) {
2744 if (offsets
[i
] != -1)
2745 so
->offsets
[i
] = offsets
[i
];
2747 pipe_so_target_reference(&so
->targets
[i
], targets
[i
]);
2750 for (unsigned i
= 0; i
< so
->num_targets
; i
++)
2751 pipe_so_target_reference(&so
->targets
[i
], NULL
);
2753 so
->num_targets
= num_targets
;
2757 panfrost_setup_hardware(struct panfrost_context
*ctx
)
2759 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2760 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
2762 panfrost_drm_allocate_slab(screen
, &ctx
->scratchpad
, 64*4, false, 0, 0, 0);
2763 panfrost_drm_allocate_slab(screen
, &ctx
->tiler_heap
, 4096, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2764 panfrost_drm_allocate_slab(screen
, &ctx
->tiler_dummy
, 1, false, PAN_ALLOCATE_INVISIBLE
, 0, 0);
2767 /* New context creation, which also does hardware initialisation since I don't
2768 * know the better way to structure this :smirk: */
2770 struct pipe_context
*
2771 panfrost_create_context(struct pipe_screen
*screen
, void *priv
, unsigned flags
)
2773 struct panfrost_context
*ctx
= rzalloc(screen
, struct panfrost_context
);
2774 struct panfrost_screen
*pscreen
= pan_screen(screen
);
2775 memset(ctx
, 0, sizeof(*ctx
));
2776 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2778 ctx
->is_t6xx
= pscreen
->gpu_id
< 0x0700; /* Literally, "earlier than T700" */
2780 gallium
->screen
= screen
;
2782 gallium
->destroy
= panfrost_destroy
;
2784 gallium
->set_framebuffer_state
= panfrost_set_framebuffer_state
;
2786 gallium
->flush
= panfrost_flush
;
2787 gallium
->clear
= panfrost_clear
;
2788 gallium
->draw_vbo
= panfrost_draw_vbo
;
2790 gallium
->set_vertex_buffers
= panfrost_set_vertex_buffers
;
2791 gallium
->set_constant_buffer
= panfrost_set_constant_buffer
;
2792 gallium
->set_shader_buffers
= panfrost_set_shader_buffers
;
2794 gallium
->set_stencil_ref
= panfrost_set_stencil_ref
;
2796 gallium
->create_sampler_view
= panfrost_create_sampler_view
;
2797 gallium
->set_sampler_views
= panfrost_set_sampler_views
;
2798 gallium
->sampler_view_destroy
= panfrost_sampler_view_destroy
;
2800 gallium
->create_rasterizer_state
= panfrost_create_rasterizer_state
;
2801 gallium
->bind_rasterizer_state
= panfrost_bind_rasterizer_state
;
2802 gallium
->delete_rasterizer_state
= panfrost_generic_cso_delete
;
2804 gallium
->create_vertex_elements_state
= panfrost_create_vertex_elements_state
;
2805 gallium
->bind_vertex_elements_state
= panfrost_bind_vertex_elements_state
;
2806 gallium
->delete_vertex_elements_state
= panfrost_generic_cso_delete
;
2808 gallium
->create_fs_state
= panfrost_create_shader_state
;
2809 gallium
->delete_fs_state
= panfrost_delete_shader_state
;
2810 gallium
->bind_fs_state
= panfrost_bind_fs_state
;
2812 gallium
->create_vs_state
= panfrost_create_shader_state
;
2813 gallium
->delete_vs_state
= panfrost_delete_shader_state
;
2814 gallium
->bind_vs_state
= panfrost_bind_vs_state
;
2816 gallium
->create_sampler_state
= panfrost_create_sampler_state
;
2817 gallium
->delete_sampler_state
= panfrost_generic_cso_delete
;
2818 gallium
->bind_sampler_states
= panfrost_bind_sampler_states
;
2820 gallium
->create_depth_stencil_alpha_state
= panfrost_create_depth_stencil_state
;
2821 gallium
->bind_depth_stencil_alpha_state
= panfrost_bind_depth_stencil_state
;
2822 gallium
->delete_depth_stencil_alpha_state
= panfrost_delete_depth_stencil_state
;
2824 gallium
->set_sample_mask
= panfrost_set_sample_mask
;
2826 gallium
->set_clip_state
= panfrost_set_clip_state
;
2827 gallium
->set_viewport_states
= panfrost_set_viewport_states
;
2828 gallium
->set_scissor_states
= panfrost_set_scissor_states
;
2829 gallium
->set_polygon_stipple
= panfrost_set_polygon_stipple
;
2830 gallium
->set_active_query_state
= panfrost_set_active_query_state
;
2832 gallium
->create_query
= panfrost_create_query
;
2833 gallium
->destroy_query
= panfrost_destroy_query
;
2834 gallium
->begin_query
= panfrost_begin_query
;
2835 gallium
->end_query
= panfrost_end_query
;
2836 gallium
->get_query_result
= panfrost_get_query_result
;
2838 gallium
->create_stream_output_target
= panfrost_create_stream_output_target
;
2839 gallium
->stream_output_target_destroy
= panfrost_stream_output_target_destroy
;
2840 gallium
->set_stream_output_targets
= panfrost_set_stream_output_targets
;
2842 panfrost_resource_context_init(gallium
);
2843 panfrost_blend_context_init(gallium
);
2844 panfrost_compute_context_init(gallium
);
2846 panfrost_drm_init_context(ctx
);
2848 panfrost_setup_hardware(ctx
);
2851 gallium
->stream_uploader
= u_upload_create_default(gallium
);
2852 gallium
->const_uploader
= gallium
->stream_uploader
;
2853 assert(gallium
->stream_uploader
);
2855 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2856 ctx
->draw_modes
= (1 << (PIPE_PRIM_POLYGON
+ 1)) - 1;
2858 ctx
->primconvert
= util_primconvert_create(gallium
, ctx
->draw_modes
);
2860 ctx
->blitter
= util_blitter_create(gallium
);
2861 ctx
->blitter_wallpaper
= util_blitter_create(gallium
);
2863 assert(ctx
->blitter
);
2864 assert(ctx
->blitter_wallpaper
);
2866 /* Prepare for render! */
2868 panfrost_job_init(ctx
);
2869 panfrost_emit_vertex_payload(ctx
);
2870 panfrost_emit_tiler_payload(ctx
);
2871 panfrost_invalidate_frame(ctx
);
2872 panfrost_default_shader_backend(ctx
);