2 * © Copyright 2018 Alyssa Rosenzweig
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include "pan_context.h"
29 #include "pan_format.h"
31 #include "util/macros.h"
32 #include "util/u_format.h"
33 #include "util/u_inlines.h"
34 #include "util/u_upload_mgr.h"
35 #include "util/u_memory.h"
36 #include "util/u_vbuf.h"
37 #include "util/half_float.h"
38 #include "util/u_helpers.h"
39 #include "util/u_format.h"
40 #include "util/u_prim_restart.h"
41 #include "indices/u_primconvert.h"
42 #include "tgsi/tgsi_parse.h"
43 #include "util/u_math.h"
45 #include "pan_screen.h"
46 #include "pan_blending.h"
47 #include "pan_blend_shaders.h"
49 #include "pan_tiler.h"
51 /* Do not actually send anything to the GPU; merely generate the cmdstream as fast as possible. Disables framebuffer writes */
54 static enum mali_job_type
55 panfrost_job_type_for_pipe(enum pipe_shader_type type
)
58 case PIPE_SHADER_VERTEX
:
59 return JOB_TYPE_VERTEX
;
61 case PIPE_SHADER_FRAGMENT
:
62 /* Note: JOB_TYPE_FRAGMENT is different.
63 * JOB_TYPE_FRAGMENT actually executes the
64 * fragment shader, but JOB_TYPE_TILER is how you
66 return JOB_TYPE_TILER
;
68 case PIPE_SHADER_GEOMETRY
:
69 return JOB_TYPE_GEOMETRY
;
71 case PIPE_SHADER_COMPUTE
:
72 return JOB_TYPE_COMPUTE
;
75 unreachable("Unsupported shader stage");
79 /* Framebuffer descriptor */
82 panfrost_set_framebuffer_resolution(struct mali_single_framebuffer
*fb
, int w
, int h
)
84 fb
->width
= MALI_POSITIVE(w
);
85 fb
->height
= MALI_POSITIVE(h
);
87 /* No idea why this is needed, but it's how resolution_check is
88 * calculated. It's not clear to us yet why the hardware wants this.
89 * The formula itself was discovered mostly by manual bruteforce and
90 * aggressive algebraic simplification. */
92 fb
->tiler_resolution_check
= ((w
+ h
) / 3) << 4;
95 struct mali_single_framebuffer
96 panfrost_emit_sfbd(struct panfrost_context
*ctx
, unsigned vertex_count
)
98 struct mali_single_framebuffer framebuffer
= {
100 .format
= 0x30000000,
101 .clear_flags
= 0x1000,
102 .unknown_address_0
= ctx
->scratchpad
.bo
->gpu
,
103 .tiler_polygon_list
= ctx
->tiler_polygon_list
.bo
->gpu
,
104 .tiler_polygon_list_body
= ctx
->tiler_polygon_list
.bo
->gpu
+ 40960,
105 .tiler_hierarchy_mask
= 0xF0,
107 .tiler_heap_free
= ctx
->tiler_heap
.bo
->gpu
,
108 .tiler_heap_end
= ctx
->tiler_heap
.bo
->gpu
+ ctx
->tiler_heap
.bo
->size
,
111 panfrost_set_framebuffer_resolution(&framebuffer
, ctx
->pipe_framebuffer
.width
, ctx
->pipe_framebuffer
.height
);
116 struct bifrost_framebuffer
117 panfrost_emit_mfbd(struct panfrost_context
*ctx
, unsigned vertex_count
)
119 unsigned width
= ctx
->pipe_framebuffer
.width
;
120 unsigned height
= ctx
->pipe_framebuffer
.height
;
122 struct bifrost_framebuffer framebuffer
= {
123 .width1
= MALI_POSITIVE(width
),
124 .height1
= MALI_POSITIVE(height
),
125 .width2
= MALI_POSITIVE(width
),
126 .height2
= MALI_POSITIVE(height
),
131 .rt_count_1
= MALI_POSITIVE(1),
136 .scratchpad
= ctx
->scratchpad
.bo
->gpu
,
139 framebuffer
.tiler_hierarchy_mask
=
140 panfrost_choose_hierarchy_mask(width
, height
, vertex_count
);
142 /* Compute the polygon header size and use that to offset the body */
144 unsigned header_size
= panfrost_tiler_header_size(
145 width
, height
, framebuffer
.tiler_hierarchy_mask
);
147 unsigned body_size
= panfrost_tiler_body_size(
148 width
, height
, framebuffer
.tiler_hierarchy_mask
);
152 unsigned total_size
= header_size
+ body_size
;
154 if (framebuffer
.tiler_hierarchy_mask
) {
155 assert(ctx
->tiler_polygon_list
.bo
->size
>= total_size
);
157 /* Specify allocated tiler structures */
158 framebuffer
.tiler_polygon_list
= ctx
->tiler_polygon_list
.bo
->gpu
;
160 /* Allow the entire tiler heap */
161 framebuffer
.tiler_heap_start
= ctx
->tiler_heap
.bo
->gpu
;
162 framebuffer
.tiler_heap_end
=
163 ctx
->tiler_heap
.bo
->gpu
+ ctx
->tiler_heap
.bo
->size
;
165 /* The tiler is disabled, so don't allow the tiler heap */
166 framebuffer
.tiler_heap_start
= ctx
->tiler_heap
.bo
->gpu
;
167 framebuffer
.tiler_heap_end
= framebuffer
.tiler_heap_start
;
169 /* Use a dummy polygon list */
170 framebuffer
.tiler_polygon_list
= ctx
->tiler_dummy
.bo
->gpu
;
172 /* Also, set a "tiler disabled?" flag? */
173 framebuffer
.tiler_hierarchy_mask
|= 0x1000;
176 framebuffer
.tiler_polygon_list_body
=
177 framebuffer
.tiler_polygon_list
+ header_size
;
179 framebuffer
.tiler_polygon_list_size
=
180 header_size
+ body_size
;
187 /* Are we currently rendering to the screen (rather than an FBO)? */
190 panfrost_is_scanout(struct panfrost_context
*ctx
)
192 /* If there is no color buffer, it's an FBO */
193 if (!ctx
->pipe_framebuffer
.nr_cbufs
)
196 /* If we're too early that no framebuffer was sent, it's scanout */
197 if (!ctx
->pipe_framebuffer
.cbufs
[0])
200 return ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
201 ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
202 ctx
->pipe_framebuffer
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;
207 struct pipe_context
*pipe
,
209 const union pipe_color_union
*color
,
210 double depth
, unsigned stencil
)
212 struct panfrost_context
*ctx
= pan_context(pipe
);
213 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
215 panfrost_job_clear(ctx
, job
, buffers
, color
, depth
, stencil
);
219 panfrost_attach_vt_mfbd(struct panfrost_context
*ctx
)
221 return panfrost_upload_transient(ctx
, &ctx
->vt_framebuffer_mfbd
, sizeof(ctx
->vt_framebuffer_mfbd
)) | MALI_MFBD
;
225 panfrost_attach_vt_sfbd(struct panfrost_context
*ctx
)
227 return panfrost_upload_transient(ctx
, &ctx
->vt_framebuffer_sfbd
, sizeof(ctx
->vt_framebuffer_sfbd
)) | MALI_SFBD
;
231 panfrost_attach_vt_framebuffer(struct panfrost_context
*ctx
)
233 mali_ptr framebuffer
= ctx
->require_sfbd
?
234 panfrost_attach_vt_sfbd(ctx
) :
235 panfrost_attach_vt_mfbd(ctx
);
237 ctx
->payload_vertex
.postfix
.framebuffer
= framebuffer
;
238 ctx
->payload_tiler
.postfix
.framebuffer
= framebuffer
;
241 /* Reset per-frame context, called on context initialisation as well as after
242 * flushing a frame */
245 panfrost_invalidate_frame(struct panfrost_context
*ctx
)
247 unsigned transient_count
= ctx
->transient_pools
[ctx
->cmdstream_i
].entry_index
*ctx
->transient_pools
[0].entry_size
+ ctx
->transient_pools
[ctx
->cmdstream_i
].entry_offset
;
248 DBG("Uploaded transient %d bytes\n", transient_count
);
250 /* Rotate cmdstream */
251 if ((++ctx
->cmdstream_i
) == (sizeof(ctx
->transient_pools
) / sizeof(ctx
->transient_pools
[0])))
252 ctx
->cmdstream_i
= 0;
254 if (ctx
->require_sfbd
)
255 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
, ~0);
257 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
, ~0);
259 /* Reset varyings allocated */
260 ctx
->varying_height
= 0;
262 /* The transient cmdstream is dirty every frame; the only bits worth preserving
263 * (textures, shaders, etc) are in other buffers anyways */
265 ctx
->transient_pools
[ctx
->cmdstream_i
].entry_index
= 0;
266 ctx
->transient_pools
[ctx
->cmdstream_i
].entry_offset
= 0;
268 /* Regenerate payloads */
269 panfrost_attach_vt_framebuffer(ctx
);
272 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
275 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
| PAN_DIRTY_TEXTURES
;
278 /* In practice, every field of these payloads should be configurable
279 * arbitrarily, which means these functions are basically catch-all's for
280 * as-of-yet unwavering unknowns */
283 panfrost_emit_vertex_payload(struct panfrost_context
*ctx
)
285 struct midgard_payload_vertex_tiler payload
= {
286 .gl_enables
= 0x4 | (ctx
->is_t6xx
? 0 : 0x2),
289 memcpy(&ctx
->payload_vertex
, &payload
, sizeof(payload
));
293 panfrost_emit_tiler_payload(struct panfrost_context
*ctx
)
295 struct midgard_payload_vertex_tiler payload
= {
297 .zero1
= 0xffff, /* Why is this only seen on test-quad-textured? */
301 memcpy(&ctx
->payload_tiler
, &payload
, sizeof(payload
));
305 translate_tex_wrap(enum pipe_tex_wrap w
)
308 case PIPE_TEX_WRAP_REPEAT
:
309 return MALI_WRAP_REPEAT
;
311 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
312 return MALI_WRAP_CLAMP_TO_EDGE
;
314 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
315 return MALI_WRAP_CLAMP_TO_BORDER
;
317 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
318 return MALI_WRAP_MIRRORED_REPEAT
;
321 unreachable("Invalid wrap");
326 translate_tex_filter(enum pipe_tex_filter f
)
329 case PIPE_TEX_FILTER_NEAREST
:
332 case PIPE_TEX_FILTER_LINEAR
:
336 unreachable("Invalid filter");
341 translate_mip_filter(enum pipe_tex_mipfilter f
)
343 return (f
== PIPE_TEX_MIPFILTER_LINEAR
) ? MALI_MIP_LINEAR
: 0;
347 panfrost_translate_compare_func(enum pipe_compare_func in
)
350 case PIPE_FUNC_NEVER
:
351 return MALI_FUNC_NEVER
;
354 return MALI_FUNC_LESS
;
356 case PIPE_FUNC_EQUAL
:
357 return MALI_FUNC_EQUAL
;
359 case PIPE_FUNC_LEQUAL
:
360 return MALI_FUNC_LEQUAL
;
362 case PIPE_FUNC_GREATER
:
363 return MALI_FUNC_GREATER
;
365 case PIPE_FUNC_NOTEQUAL
:
366 return MALI_FUNC_NOTEQUAL
;
368 case PIPE_FUNC_GEQUAL
:
369 return MALI_FUNC_GEQUAL
;
371 case PIPE_FUNC_ALWAYS
:
372 return MALI_FUNC_ALWAYS
;
375 unreachable("Invalid func");
380 panfrost_translate_alt_compare_func(enum pipe_compare_func in
)
383 case PIPE_FUNC_NEVER
:
384 return MALI_ALT_FUNC_NEVER
;
387 return MALI_ALT_FUNC_LESS
;
389 case PIPE_FUNC_EQUAL
:
390 return MALI_ALT_FUNC_EQUAL
;
392 case PIPE_FUNC_LEQUAL
:
393 return MALI_ALT_FUNC_LEQUAL
;
395 case PIPE_FUNC_GREATER
:
396 return MALI_ALT_FUNC_GREATER
;
398 case PIPE_FUNC_NOTEQUAL
:
399 return MALI_ALT_FUNC_NOTEQUAL
;
401 case PIPE_FUNC_GEQUAL
:
402 return MALI_ALT_FUNC_GEQUAL
;
404 case PIPE_FUNC_ALWAYS
:
405 return MALI_ALT_FUNC_ALWAYS
;
408 unreachable("Invalid alt func");
413 panfrost_translate_stencil_op(enum pipe_stencil_op in
)
416 case PIPE_STENCIL_OP_KEEP
:
417 return MALI_STENCIL_KEEP
;
419 case PIPE_STENCIL_OP_ZERO
:
420 return MALI_STENCIL_ZERO
;
422 case PIPE_STENCIL_OP_REPLACE
:
423 return MALI_STENCIL_REPLACE
;
425 case PIPE_STENCIL_OP_INCR
:
426 return MALI_STENCIL_INCR
;
428 case PIPE_STENCIL_OP_DECR
:
429 return MALI_STENCIL_DECR
;
431 case PIPE_STENCIL_OP_INCR_WRAP
:
432 return MALI_STENCIL_INCR_WRAP
;
434 case PIPE_STENCIL_OP_DECR_WRAP
:
435 return MALI_STENCIL_DECR_WRAP
;
437 case PIPE_STENCIL_OP_INVERT
:
438 return MALI_STENCIL_INVERT
;
441 unreachable("Invalid stencil op");
446 panfrost_make_stencil_state(const struct pipe_stencil_state
*in
, struct mali_stencil_test
*out
)
448 out
->ref
= 0; /* Gallium gets it from elsewhere */
450 out
->mask
= in
->valuemask
;
451 out
->func
= panfrost_translate_compare_func(in
->func
);
452 out
->sfail
= panfrost_translate_stencil_op(in
->fail_op
);
453 out
->dpfail
= panfrost_translate_stencil_op(in
->zfail_op
);
454 out
->dppass
= panfrost_translate_stencil_op(in
->zpass_op
);
458 panfrost_default_shader_backend(struct panfrost_context
*ctx
)
460 struct mali_shader_meta shader
= {
461 .alpha_coverage
= ~MALI_ALPHA_COVERAGE(0.000000),
463 .unknown2_3
= MALI_DEPTH_FUNC(MALI_FUNC_ALWAYS
) | 0x3010,
464 .unknown2_4
= MALI_NO_MSAA
| 0x4e0,
468 shader
.unknown2_4
|= 0x10;
471 struct pipe_stencil_state default_stencil
= {
473 .func
= PIPE_FUNC_ALWAYS
,
474 .fail_op
= MALI_STENCIL_KEEP
,
475 .zfail_op
= MALI_STENCIL_KEEP
,
476 .zpass_op
= MALI_STENCIL_KEEP
,
481 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_front
);
482 shader
.stencil_mask_front
= default_stencil
.writemask
;
484 panfrost_make_stencil_state(&default_stencil
, &shader
.stencil_back
);
485 shader
.stencil_mask_back
= default_stencil
.writemask
;
487 if (default_stencil
.enabled
)
488 shader
.unknown2_4
|= MALI_STENCIL_TEST
;
490 memcpy(&ctx
->fragment_shader_core
, &shader
, sizeof(shader
));
493 /* Generates a vertex/tiler job. This is, in some sense, the heart of the
494 * graphics command stream. It should be called once per draw, accordding to
495 * presentations. Set is_tiler for "tiler" jobs (fragment shader jobs, but in
496 * Mali parlance, "fragment" refers to framebuffer writeout). Clear it for
499 struct panfrost_transfer
500 panfrost_vertex_tiler_job(struct panfrost_context
*ctx
, bool is_tiler
)
502 struct mali_job_descriptor_header job
= {
503 .job_type
= is_tiler
? JOB_TYPE_TILER
: JOB_TYPE_VERTEX
,
505 .job_descriptor_size
= 1,
509 struct midgard_payload_vertex_tiler
*payload
= is_tiler
? &ctx
->payload_tiler
: &ctx
->payload_vertex
;
511 /* There's some padding hacks on 32-bit */
518 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sizeof(job
) + sizeof(*payload
));
520 memcpy(transfer
.cpu
, &job
, sizeof(job
));
521 memcpy(transfer
.cpu
+ sizeof(job
) - offset
, payload
, sizeof(*payload
));
526 panfrost_emit_varyings(
527 struct panfrost_context
*ctx
,
528 union mali_attr
*slot
,
532 mali_ptr varying_address
= ctx
->varying_mem
.bo
->gpu
+ ctx
->varying_height
;
534 /* Fill out the descriptor */
535 slot
->elements
= varying_address
| MALI_ATTR_LINEAR
;
536 slot
->stride
= stride
;
537 slot
->size
= stride
* count
;
539 ctx
->varying_height
+= ALIGN(slot
->size
, 64);
540 assert(ctx
->varying_height
< ctx
->varying_mem
.bo
->size
);
542 return varying_address
;
546 panfrost_emit_point_coord(union mali_attr
*slot
)
548 slot
->elements
= MALI_VARYING_POINT_COORD
| MALI_ATTR_LINEAR
;
549 slot
->stride
= slot
->size
= 0;
553 panfrost_emit_varying_descriptor(
554 struct panfrost_context
*ctx
,
555 unsigned vertex_count
)
557 /* Load the shaders */
559 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
560 struct panfrost_shader_state
*fs
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
561 unsigned int num_gen_varyings
= 0;
563 /* Allocate the varying descriptor */
565 size_t vs_size
= sizeof(struct mali_attr_meta
) * vs
->tripipe
->varying_count
;
566 size_t fs_size
= sizeof(struct mali_attr_meta
) * fs
->tripipe
->varying_count
;
568 struct panfrost_transfer trans
= panfrost_allocate_transient(ctx
,
572 * Assign ->src_offset now that we know about all the general purpose
573 * varyings that will be used by the fragment and vertex shaders.
575 for (unsigned i
= 0; i
< vs
->tripipe
->varying_count
; i
++) {
577 * General purpose varyings have ->index set to 0, skip other
580 if (vs
->varyings
[i
].index
)
583 vs
->varyings
[i
].src_offset
= 16 * (num_gen_varyings
++);
586 for (unsigned i
= 0; i
< fs
->tripipe
->varying_count
; i
++) {
589 /* If we have a point sprite replacement, handle that here. We
590 * have to translate location first. TODO: Flip y in shader.
591 * We're already keying ... just time crunch .. */
593 unsigned loc
= fs
->varyings_loc
[i
];
595 (loc
>= VARYING_SLOT_VAR0
) ? (loc
- VARYING_SLOT_VAR0
) :
596 (loc
== VARYING_SLOT_PNTC
) ? 8 :
599 if (~pnt_loc
&& fs
->point_sprite_mask
& (1 << pnt_loc
)) {
600 /* gl_PointCoord index by convention */
601 fs
->varyings
[i
].index
= 3;
602 fs
->reads_point_coord
= true;
604 /* Swizzle out the z/w to 0/1 */
605 fs
->varyings
[i
].format
= MALI_RG16F
;
606 fs
->varyings
[i
].swizzle
=
607 panfrost_get_default_swizzle(2);
612 if (fs
->varyings
[i
].index
)
616 * Re-use the VS general purpose varying pos if it exists,
617 * create a new one otherwise.
619 for (j
= 0; j
< vs
->tripipe
->varying_count
; j
++) {
620 if (fs
->varyings_loc
[i
] == vs
->varyings_loc
[j
])
624 if (j
< vs
->tripipe
->varying_count
)
625 fs
->varyings
[i
].src_offset
= vs
->varyings
[j
].src_offset
;
627 fs
->varyings
[i
].src_offset
= 16 * (num_gen_varyings
++);
630 memcpy(trans
.cpu
, vs
->varyings
, vs_size
);
631 memcpy(trans
.cpu
+ vs_size
, fs
->varyings
, fs_size
);
633 ctx
->payload_vertex
.postfix
.varying_meta
= trans
.gpu
;
634 ctx
->payload_tiler
.postfix
.varying_meta
= trans
.gpu
+ vs_size
;
636 /* Buffer indices must be in this order per our convention */
637 union mali_attr varyings
[PIPE_MAX_ATTRIBS
];
640 panfrost_emit_varyings(ctx
, &varyings
[idx
++], num_gen_varyings
* 16,
643 /* fp32 vec4 gl_Position */
644 ctx
->payload_tiler
.postfix
.position_varying
=
645 panfrost_emit_varyings(ctx
, &varyings
[idx
++],
646 sizeof(float) * 4, vertex_count
);
649 if (vs
->writes_point_size
|| fs
->reads_point_coord
) {
650 /* fp16 vec1 gl_PointSize */
651 ctx
->payload_tiler
.primitive_size
.pointer
=
652 panfrost_emit_varyings(ctx
, &varyings
[idx
++],
656 if (fs
->reads_point_coord
) {
657 /* Special descriptor */
658 panfrost_emit_point_coord(&varyings
[idx
++]);
661 mali_ptr varyings_p
= panfrost_upload_transient(ctx
, &varyings
, idx
* sizeof(union mali_attr
));
662 ctx
->payload_vertex
.postfix
.varyings
= varyings_p
;
663 ctx
->payload_tiler
.postfix
.varyings
= varyings_p
;
667 panfrost_vertex_buffer_address(struct panfrost_context
*ctx
, unsigned i
)
669 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[i
];
670 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (buf
->buffer
.resource
);
672 return rsrc
->bo
->gpu
+ buf
->buffer_offset
;
676 panfrost_writes_point_size(struct panfrost_context
*ctx
)
679 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
681 return vs
->writes_point_size
&& ctx
->payload_tiler
.prefix
.draw_mode
== MALI_POINTS
;
684 /* Stage the attribute descriptors so we can adjust src_offset
685 * to let BOs align nicely */
688 panfrost_stage_attributes(struct panfrost_context
*ctx
)
690 struct panfrost_vertex_state
*so
= ctx
->vertex
;
692 size_t sz
= sizeof(struct mali_attr_meta
) * so
->num_elements
;
693 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, sz
);
694 struct mali_attr_meta
*target
= (struct mali_attr_meta
*) transfer
.cpu
;
696 /* Copy as-is for the first pass */
697 memcpy(target
, so
->hw
, sz
);
699 /* Fixup offsets for the second pass. Recall that the hardware
700 * calculates attribute addresses as:
702 * addr = base + (stride * vtx) + src_offset;
704 * However, on Mali, base must be aligned to 64-bytes, so we
707 * base' = base & ~63 = base - (base & 63)
709 * To compensate when using base' (see emit_vertex_data), we have
710 * to adjust src_offset by the masked off piece:
712 * addr' = base' + (stride * vtx) + (src_offset + (base & 63))
713 * = base - (base & 63) + (stride * vtx) + src_offset + (base & 63)
714 * = base + (stride * vtx) + src_offset
720 unsigned start
= ctx
->payload_vertex
.draw_start
;
722 for (unsigned i
= 0; i
< so
->num_elements
; ++i
) {
723 unsigned vbi
= so
->pipe
[i
].vertex_buffer_index
;
724 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[vbi
];
725 mali_ptr addr
= panfrost_vertex_buffer_address(ctx
, vbi
);
727 /* Adjust by the masked off bits of the offset */
728 target
[i
].src_offset
+= (addr
& 63);
730 /* Also, somewhat obscurely per-instance data needs to be
731 * offset in response to a delayed start in an indexed draw */
733 if (so
->pipe
[i
].instance_divisor
&& ctx
->instance_count
> 1 && start
) {
734 target
[i
].src_offset
-= buf
->stride
* start
;
740 ctx
->payload_vertex
.postfix
.attribute_meta
= transfer
.gpu
;
744 panfrost_upload_sampler_descriptors(struct panfrost_context
*ctx
)
746 size_t desc_size
= sizeof(struct mali_sampler_descriptor
);
748 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
751 if (ctx
->sampler_count
[t
] && ctx
->sampler_view_count
[t
]) {
752 size_t transfer_size
= desc_size
* ctx
->sampler_count
[t
];
754 struct panfrost_transfer transfer
=
755 panfrost_allocate_transient(ctx
, transfer_size
);
757 struct mali_sampler_descriptor
*desc
=
758 (struct mali_sampler_descriptor
*) transfer
.cpu
;
760 for (int i
= 0; i
< ctx
->sampler_count
[t
]; ++i
)
761 desc
[i
] = ctx
->samplers
[t
][i
]->hw
;
763 upload
= transfer
.gpu
;
766 if (t
== PIPE_SHADER_FRAGMENT
)
767 ctx
->payload_tiler
.postfix
.sampler_descriptor
= upload
;
768 else if (t
== PIPE_SHADER_VERTEX
)
769 ctx
->payload_vertex
.postfix
.sampler_descriptor
= upload
;
777 struct panfrost_context
*ctx
,
778 struct panfrost_sampler_view
*view
)
781 return (mali_ptr
) NULL
;
783 struct pipe_sampler_view
*pview
= &view
->base
;
784 struct panfrost_resource
*rsrc
= pan_resource(pview
->texture
);
786 /* Do we interleave an explicit stride with every element? */
788 bool has_manual_stride
=
789 view
->hw
.format
.usage2
& MALI_TEX_MANUAL_STRIDE
;
791 /* For easy access */
793 assert(pview
->target
!= PIPE_BUFFER
);
794 unsigned first_level
= pview
->u
.tex
.first_level
;
795 unsigned last_level
= pview
->u
.tex
.last_level
;
796 unsigned first_layer
= pview
->u
.tex
.first_layer
;
797 unsigned last_layer
= pview
->u
.tex
.last_layer
;
799 /* Lower-bit is set when sampling from colour AFBC */
800 bool is_afbc
= rsrc
->layout
== PAN_AFBC
;
801 bool is_zs
= rsrc
->base
.bind
& PIPE_BIND_DEPTH_STENCIL
;
802 unsigned afbc_bit
= (is_afbc
&& !is_zs
) ? 1 : 0;
804 /* Add the BO to the job so it's retained until the job is done. */
805 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
806 panfrost_job_add_bo(job
, rsrc
->bo
);
808 /* Inject the addresses in, interleaving mip levels, cube faces, and
809 * strides in that order */
813 for (unsigned l
= first_level
; l
<= last_level
; ++l
) {
814 for (unsigned f
= first_layer
; f
<= last_layer
; ++f
) {
816 view
->hw
.payload
[idx
++] =
817 panfrost_get_texture_address(rsrc
, l
, f
) + afbc_bit
;
819 if (has_manual_stride
) {
820 view
->hw
.payload
[idx
++] =
821 rsrc
->slices
[l
].stride
;
826 return panfrost_upload_transient(ctx
, &view
->hw
,
827 sizeof(struct mali_texture_descriptor
));
831 panfrost_upload_texture_descriptors(struct panfrost_context
*ctx
)
833 for (int t
= 0; t
<= PIPE_SHADER_FRAGMENT
; ++t
) {
834 mali_ptr trampoline
= 0;
836 if (ctx
->sampler_view_count
[t
]) {
837 uint64_t trampolines
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
839 for (int i
= 0; i
< ctx
->sampler_view_count
[t
]; ++i
)
841 panfrost_upload_tex(ctx
, ctx
->sampler_views
[t
][i
]);
843 trampoline
= panfrost_upload_transient(ctx
, trampolines
, sizeof(uint64_t) * ctx
->sampler_view_count
[t
]);
846 if (t
== PIPE_SHADER_FRAGMENT
)
847 ctx
->payload_tiler
.postfix
.texture_trampoline
= trampoline
;
848 else if (t
== PIPE_SHADER_VERTEX
)
849 ctx
->payload_vertex
.postfix
.texture_trampoline
= trampoline
;
855 struct sysval_uniform
{
863 static void panfrost_upload_viewport_scale_sysval(struct panfrost_context
*ctx
,
864 struct sysval_uniform
*uniform
)
866 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
868 uniform
->f
[0] = vp
->scale
[0];
869 uniform
->f
[1] = vp
->scale
[1];
870 uniform
->f
[2] = vp
->scale
[2];
873 static void panfrost_upload_viewport_offset_sysval(struct panfrost_context
*ctx
,
874 struct sysval_uniform
*uniform
)
876 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
878 uniform
->f
[0] = vp
->translate
[0];
879 uniform
->f
[1] = vp
->translate
[1];
880 uniform
->f
[2] = vp
->translate
[2];
883 static void panfrost_upload_txs_sysval(struct panfrost_context
*ctx
,
884 enum pipe_shader_type st
,
885 unsigned int sysvalid
,
886 struct sysval_uniform
*uniform
)
888 unsigned texidx
= PAN_SYSVAL_ID_TO_TXS_TEX_IDX(sysvalid
);
889 unsigned dim
= PAN_SYSVAL_ID_TO_TXS_DIM(sysvalid
);
890 bool is_array
= PAN_SYSVAL_ID_TO_TXS_IS_ARRAY(sysvalid
);
891 struct pipe_sampler_view
*tex
= &ctx
->sampler_views
[st
][texidx
]->base
;
894 uniform
->i
[0] = u_minify(tex
->texture
->width0
, tex
->u
.tex
.first_level
);
897 uniform
->i
[1] = u_minify(tex
->texture
->height0
,
898 tex
->u
.tex
.first_level
);
901 uniform
->i
[2] = u_minify(tex
->texture
->depth0
,
902 tex
->u
.tex
.first_level
);
905 uniform
->i
[dim
] = tex
->texture
->array_size
;
908 static void panfrost_upload_sysvals(struct panfrost_context
*ctx
, void *buf
,
909 struct panfrost_shader_state
*ss
,
910 enum pipe_shader_type st
)
912 struct sysval_uniform
*uniforms
= (void *)buf
;
914 for (unsigned i
= 0; i
< ss
->sysval_count
; ++i
) {
915 int sysval
= ss
->sysval
[i
];
917 switch (PAN_SYSVAL_TYPE(sysval
)) {
918 case PAN_SYSVAL_VIEWPORT_SCALE
:
919 panfrost_upload_viewport_scale_sysval(ctx
, &uniforms
[i
]);
921 case PAN_SYSVAL_VIEWPORT_OFFSET
:
922 panfrost_upload_viewport_offset_sysval(ctx
, &uniforms
[i
]);
924 case PAN_SYSVAL_TEXTURE_SIZE
:
925 panfrost_upload_txs_sysval(ctx
, st
, PAN_SYSVAL_ID(sysval
),
935 panfrost_map_constant_buffer_cpu(struct panfrost_constant_buffer
*buf
, unsigned index
)
937 struct pipe_constant_buffer
*cb
= &buf
->cb
[index
];
938 struct panfrost_resource
*rsrc
= pan_resource(cb
->buffer
);
941 return rsrc
->bo
->cpu
;
942 else if (cb
->user_buffer
)
943 return cb
->user_buffer
;
945 unreachable("No constant buffer");
949 panfrost_map_constant_buffer_gpu(
950 struct panfrost_context
*ctx
,
951 struct panfrost_constant_buffer
*buf
,
954 struct pipe_constant_buffer
*cb
= &buf
->cb
[index
];
955 struct panfrost_resource
*rsrc
= pan_resource(cb
->buffer
);
958 return rsrc
->bo
->gpu
;
959 else if (cb
->user_buffer
)
960 return panfrost_upload_transient(ctx
, cb
->user_buffer
, cb
->buffer_size
);
962 unreachable("No constant buffer");
965 /* Compute number of UBOs active (more specifically, compute the highest UBO
966 * number addressable -- if there are gaps, include them in the count anyway).
967 * We always include UBO #0 in the count, since we *need* uniforms enabled for
971 panfrost_ubo_count(struct panfrost_context
*ctx
, enum pipe_shader_type stage
)
973 unsigned mask
= ctx
->constant_buffer
[stage
].enabled_mask
| 1;
974 return 32 - __builtin_clz(mask
);
977 /* Fixes up a shader state with current state, returning a GPU address to the
981 panfrost_patch_shader_state(
982 struct panfrost_context
*ctx
,
983 struct panfrost_shader_state
*ss
,
984 enum pipe_shader_type stage
)
986 ss
->tripipe
->texture_count
= ctx
->sampler_view_count
[stage
];
987 ss
->tripipe
->sampler_count
= ctx
->sampler_count
[stage
];
989 ss
->tripipe
->midgard1
.flags
= 0x220;
991 unsigned ubo_count
= panfrost_ubo_count(ctx
, stage
);
992 ss
->tripipe
->midgard1
.uniform_buffer_count
= ubo_count
;
994 return ss
->tripipe_gpu
;
997 /* Go through dirty flags and actualise them in the cmdstream. */
1000 panfrost_emit_for_draw(struct panfrost_context
*ctx
, bool with_vertex_data
)
1002 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
1004 if (with_vertex_data
) {
1005 panfrost_emit_vertex_data(job
);
1007 /* Varyings emitted for -all- geometry */
1008 unsigned total_count
= ctx
->padded_count
* ctx
->instance_count
;
1009 panfrost_emit_varying_descriptor(ctx
, total_count
);
1012 bool msaa
= ctx
->rasterizer
->base
.multisample
;
1014 if (ctx
->dirty
& PAN_DIRTY_RASTERIZER
) {
1015 ctx
->payload_tiler
.gl_enables
= ctx
->rasterizer
->tiler_gl_enables
;
1017 /* TODO: Sample size */
1018 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_HAS_MSAA
, msaa
);
1019 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_MSAA
, !msaa
);
1022 panfrost_job_set_requirements(ctx
, job
);
1024 if (ctx
->occlusion_query
) {
1025 ctx
->payload_tiler
.gl_enables
|= MALI_OCCLUSION_QUERY
| MALI_OCCLUSION_PRECISE
;
1026 ctx
->payload_tiler
.postfix
.occlusion_counter
= ctx
->occlusion_query
->transfer
.gpu
;
1029 if (ctx
->dirty
& PAN_DIRTY_VS
) {
1032 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
1034 ctx
->payload_vertex
.postfix
._shader_upper
=
1035 panfrost_patch_shader_state(ctx
, vs
, PIPE_SHADER_VERTEX
) >> 4;
1038 if (ctx
->dirty
& (PAN_DIRTY_RASTERIZER
| PAN_DIRTY_VS
)) {
1039 /* Check if we need to link the gl_PointSize varying */
1040 if (!panfrost_writes_point_size(ctx
)) {
1041 /* If the size is constant, write it out. Otherwise,
1042 * don't touch primitive_size (since we would clobber
1043 * the pointer there) */
1045 ctx
->payload_tiler
.primitive_size
.constant
= ctx
->rasterizer
->base
.line_width
;
1049 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
1051 ctx
->dirty
|= PAN_DIRTY_FS
;
1053 if (ctx
->dirty
& PAN_DIRTY_FS
) {
1055 struct panfrost_shader_state
*variant
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
1057 panfrost_patch_shader_state(ctx
, variant
, PIPE_SHADER_FRAGMENT
);
1059 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
1062 COPY(attribute_count
);
1063 COPY(varying_count
);
1064 COPY(texture_count
);
1065 COPY(sampler_count
);
1066 COPY(sampler_count
);
1067 COPY(midgard1
.uniform_count
);
1068 COPY(midgard1
.uniform_buffer_count
);
1069 COPY(midgard1
.work_count
);
1070 COPY(midgard1
.flags
);
1071 COPY(midgard1
.unknown2
);
1074 /* If there is a blend shader, work registers are shared */
1076 if (ctx
->blend
->has_blend_shader
)
1077 ctx
->fragment_shader_core
.midgard1
.work_count
= /*MAX2(ctx->fragment_shader_core.midgard1.work_count, ctx->blend->blend_work_count)*/16;
1079 /* Set late due to depending on render state */
1080 unsigned flags
= ctx
->fragment_shader_core
.midgard1
.flags
;
1082 /* Depending on whether it's legal to in the given shader, we
1083 * try to enable early-z testing (or forward-pixel kill?) */
1085 if (!variant
->can_discard
)
1086 flags
|= MALI_EARLY_Z
;
1088 /* Any time texturing is used, derivatives are implicitly
1089 * calculated, so we need to enable helper invocations */
1091 if (ctx
->sampler_view_count
[PIPE_SHADER_FRAGMENT
])
1092 flags
|= MALI_HELPER_INVOCATIONS
;
1094 ctx
->fragment_shader_core
.midgard1
.flags
= flags
;
1096 /* Assign the stencil refs late */
1097 ctx
->fragment_shader_core
.stencil_front
.ref
= ctx
->stencil_ref
.ref_value
[0];
1098 ctx
->fragment_shader_core
.stencil_back
.ref
= ctx
->stencil_ref
.ref_value
[1];
1100 /* CAN_DISCARD should be set if the fragment shader possibly
1101 * contains a 'discard' instruction. It is likely this is
1102 * related to optimizations related to forward-pixel kill, as
1103 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
1104 * thing?" by Peter Harris
1107 if (variant
->can_discard
) {
1108 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
1109 ctx
->fragment_shader_core
.midgard1
.flags
|= 0x400;
1112 /* Check if we're using the default blend descriptor (fast path) */
1115 !ctx
->blend
->has_blend_shader
&&
1116 (ctx
->blend
->equation
.rgb_mode
== 0x122) &&
1117 (ctx
->blend
->equation
.alpha_mode
== 0x122) &&
1118 (ctx
->blend
->equation
.color_mask
== 0xf);
1120 /* Even on MFBD, the shader descriptor gets blend shaders. It's
1121 * *also* copied to the blend_meta appended (by convention),
1122 * but this is the field actually read by the hardware. (Or
1123 * maybe both are read...?) */
1125 if (ctx
->blend
->has_blend_shader
) {
1126 ctx
->fragment_shader_core
.blend
.shader
= ctx
->blend
->blend_shader
;
1128 ctx
->fragment_shader_core
.blend
.shader
= 0;
1131 if (ctx
->require_sfbd
) {
1132 /* When only a single render target platform is used, the blend
1133 * information is inside the shader meta itself. We
1134 * additionally need to signal CAN_DISCARD for nontrivial blend
1135 * modes (so we're able to read back the destination buffer) */
1137 if (!ctx
->blend
->has_blend_shader
) {
1138 ctx
->fragment_shader_core
.blend
.equation
= ctx
->blend
->equation
;
1139 ctx
->fragment_shader_core
.blend
.constant
= ctx
->blend
->constant
;
1143 ctx
->fragment_shader_core
.unknown2_3
|= MALI_CAN_DISCARD
;
1147 size_t size
= sizeof(struct mali_shader_meta
) + sizeof(struct midgard_blend_rt
);
1148 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
1149 memcpy(transfer
.cpu
, &ctx
->fragment_shader_core
, sizeof(struct mali_shader_meta
));
1151 ctx
->payload_tiler
.postfix
._shader_upper
= (transfer
.gpu
) >> 4;
1153 if (!ctx
->require_sfbd
) {
1154 /* Additional blend descriptor tacked on for jobs using MFBD */
1156 unsigned blend_count
= 0x200;
1158 if (ctx
->blend
->has_blend_shader
) {
1159 /* For a blend shader, the bottom nibble corresponds to
1160 * the number of work registers used, which signals the
1161 * -existence- of a blend shader */
1163 assert(ctx
->blend
->blend_work_count
>= 2);
1164 blend_count
|= MIN2(ctx
->blend
->blend_work_count
, 3);
1166 /* Otherwise, the bottom bit simply specifies if
1167 * blending (anything other than REPLACE) is enabled */
1174 struct midgard_blend_rt rts
[4];
1178 for (unsigned i
= 0; i
< 1; ++i
) {
1180 (ctx
->pipe_framebuffer
.nr_cbufs
> i
) &&
1181 util_format_is_srgb(ctx
->pipe_framebuffer
.cbufs
[i
]->format
);
1183 rts
[i
].flags
= blend_count
;
1186 rts
[i
].flags
|= MALI_BLEND_SRGB
;
1188 /* TODO: sRGB in blend shaders is currently
1189 * unimplemented. Contact me (Alyssa) if you're
1190 * interested in working on this. We have
1191 * native Midgard ops for helping here, but
1192 * they're not well-understood yet. */
1194 assert(!(is_srgb
&& ctx
->blend
->has_blend_shader
));
1196 if (ctx
->blend
->has_blend_shader
) {
1197 rts
[i
].blend
.shader
= ctx
->blend
->blend_shader
;
1199 rts
[i
].blend
.equation
= ctx
->blend
->equation
;
1200 rts
[i
].blend
.constant
= ctx
->blend
->constant
;
1204 memcpy(transfer
.cpu
+ sizeof(struct mali_shader_meta
), rts
, sizeof(rts
[0]) * 1);
1208 /* We stage to transient, so always dirty.. */
1209 panfrost_stage_attributes(ctx
);
1211 if (ctx
->dirty
& PAN_DIRTY_SAMPLERS
)
1212 panfrost_upload_sampler_descriptors(ctx
);
1214 if (ctx
->dirty
& PAN_DIRTY_TEXTURES
)
1215 panfrost_upload_texture_descriptors(ctx
);
1217 const struct pipe_viewport_state
*vp
= &ctx
->pipe_viewport
;
1219 for (int i
= 0; i
<= PIPE_SHADER_FRAGMENT
; ++i
) {
1220 struct panfrost_constant_buffer
*buf
= &ctx
->constant_buffer
[i
];
1222 struct panfrost_shader_state
*vs
= &ctx
->vs
->variants
[ctx
->vs
->active_variant
];
1223 struct panfrost_shader_state
*fs
= &ctx
->fs
->variants
[ctx
->fs
->active_variant
];
1224 struct panfrost_shader_state
*ss
= (i
== PIPE_SHADER_FRAGMENT
) ? fs
: vs
;
1226 /* Uniforms are implicitly UBO #0 */
1227 bool has_uniforms
= buf
->enabled_mask
& (1 << 0);
1229 /* Allocate room for the sysval and the uniforms */
1230 size_t sys_size
= sizeof(float) * 4 * ss
->sysval_count
;
1231 size_t uniform_size
= has_uniforms
? (buf
->cb
[0].buffer_size
) : 0;
1232 size_t size
= sys_size
+ uniform_size
;
1233 struct panfrost_transfer transfer
= panfrost_allocate_transient(ctx
, size
);
1235 /* Upload sysvals requested by the shader */
1236 panfrost_upload_sysvals(ctx
, transfer
.cpu
, ss
, i
);
1238 /* Upload uniforms */
1240 const void *cpu
= panfrost_map_constant_buffer_cpu(buf
, 0);
1241 memcpy(transfer
.cpu
+ sys_size
, cpu
, uniform_size
);
1244 int uniform_count
= 0;
1246 struct mali_vertex_tiler_postfix
*postfix
;
1249 case PIPE_SHADER_VERTEX
:
1250 uniform_count
= ctx
->vs
->variants
[ctx
->vs
->active_variant
].uniform_count
;
1251 postfix
= &ctx
->payload_vertex
.postfix
;
1254 case PIPE_SHADER_FRAGMENT
:
1255 uniform_count
= ctx
->fs
->variants
[ctx
->fs
->active_variant
].uniform_count
;
1256 postfix
= &ctx
->payload_tiler
.postfix
;
1260 unreachable("Invalid shader stage\n");
1263 /* Next up, attach UBOs. UBO #0 is the uniforms we just
1266 unsigned ubo_count
= panfrost_ubo_count(ctx
, i
);
1267 assert(ubo_count
>= 1);
1269 size_t sz
= sizeof(struct mali_uniform_buffer_meta
) * ubo_count
;
1270 struct mali_uniform_buffer_meta
*ubos
= calloc(sz
, 1);
1272 /* Upload uniforms as a UBO */
1273 ubos
[0].size
= MALI_POSITIVE((2 + uniform_count
));
1274 ubos
[0].ptr
= transfer
.gpu
>> 2;
1276 /* The rest are honest-to-goodness UBOs */
1278 for (unsigned ubo
= 1; ubo
< ubo_count
; ++ubo
) {
1279 size_t sz
= buf
->cb
[ubo
].buffer_size
;
1281 bool enabled
= buf
->enabled_mask
& (1 << ubo
);
1282 bool empty
= sz
== 0;
1284 if (!enabled
|| empty
) {
1285 /* Stub out disabled UBOs to catch accesses */
1288 ubos
[ubo
].ptr
= 0xDEAD0000;
1292 mali_ptr gpu
= panfrost_map_constant_buffer_gpu(ctx
, buf
, ubo
);
1294 unsigned bytes_per_field
= 16;
1295 unsigned aligned
= ALIGN(sz
, bytes_per_field
);
1296 unsigned fields
= aligned
/ bytes_per_field
;
1298 ubos
[ubo
].size
= MALI_POSITIVE(fields
);
1299 ubos
[ubo
].ptr
= gpu
>> 2;
1302 mali_ptr ubufs
= panfrost_upload_transient(ctx
, ubos
, sz
);
1303 postfix
->uniforms
= transfer
.gpu
;
1304 postfix
->uniform_buffers
= ubufs
;
1306 buf
->dirty_mask
= 0;
1309 /* TODO: Upload the viewport somewhere more appropriate */
1311 /* Clip bounds are encoded as floats. The viewport itself is encoded as
1312 * (somewhat) asymmetric ints. */
1313 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
1315 struct mali_viewport view
= {
1316 /* By default, do no viewport clipping, i.e. clip to (-inf,
1317 * inf) in each direction. Clipping to the viewport in theory
1318 * should work, but in practice causes issues when we're not
1319 * explicitly trying to scissor */
1330 /* Always scissor to the viewport by default. */
1331 int minx
= (int) (vp
->translate
[0] - vp
->scale
[0]);
1332 int maxx
= (int) (vp
->translate
[0] + vp
->scale
[0]);
1334 int miny
= (int) (vp
->translate
[1] - vp
->scale
[1]);
1335 int maxy
= (int) (vp
->translate
[1] + vp
->scale
[1]);
1337 /* Apply the scissor test */
1339 if (ss
&& ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
) {
1346 /* Hardware needs the min/max to be strictly ordered, so flip if we
1347 * need to. The viewport transformation in the vertex shader will
1348 * handle the negatives if we don't */
1362 /* Clamp everything positive, just in case */
1364 maxx
= MAX2(0, maxx
);
1365 maxy
= MAX2(0, maxy
);
1366 minx
= MAX2(0, minx
);
1367 miny
= MAX2(0, miny
);
1369 /* Clamp to the framebuffer size as a last check */
1371 minx
= MIN2(ctx
->pipe_framebuffer
.width
, minx
);
1372 maxx
= MIN2(ctx
->pipe_framebuffer
.width
, maxx
);
1374 miny
= MIN2(ctx
->pipe_framebuffer
.height
, miny
);
1375 maxy
= MIN2(ctx
->pipe_framebuffer
.height
, maxy
);
1377 /* Update the job, unless we're doing wallpapering (whose lack of
1378 * scissor we can ignore, since if we "miss" a tile of wallpaper, it'll
1379 * just... be faster :) */
1381 if (!ctx
->wallpaper_batch
)
1382 panfrost_job_union_scissor(job
, minx
, miny
, maxx
, maxy
);
1386 view
.viewport0
[0] = minx
;
1387 view
.viewport1
[0] = MALI_POSITIVE(maxx
);
1389 view
.viewport0
[1] = miny
;
1390 view
.viewport1
[1] = MALI_POSITIVE(maxy
);
1392 ctx
->payload_tiler
.postfix
.viewport
=
1393 panfrost_upload_transient(ctx
,
1395 sizeof(struct mali_viewport
));
1400 /* Corresponds to exactly one draw, but does not submit anything */
1403 panfrost_queue_draw(struct panfrost_context
*ctx
)
1405 /* Handle dirty flags now */
1406 panfrost_emit_for_draw(ctx
, true);
1408 /* If rasterizer discard is enable, only submit the vertex */
1410 bool rasterizer_discard
= ctx
->rasterizer
1411 && ctx
->rasterizer
->base
.rasterizer_discard
;
1413 struct panfrost_transfer vertex
= panfrost_vertex_tiler_job(ctx
, false);
1414 struct panfrost_transfer tiler
;
1416 if (!rasterizer_discard
)
1417 tiler
= panfrost_vertex_tiler_job(ctx
, true);
1419 struct panfrost_job
*batch
= panfrost_get_job_for_fbo(ctx
);
1421 if (rasterizer_discard
)
1422 panfrost_scoreboard_queue_vertex_job(batch
, vertex
, FALSE
);
1423 else if (ctx
->wallpaper_batch
)
1424 panfrost_scoreboard_queue_fused_job_prepend(batch
, vertex
, tiler
);
1426 panfrost_scoreboard_queue_fused_job(batch
, vertex
, tiler
);
1429 /* The entire frame is in memory -- send it off to the kernel! */
1432 panfrost_submit_frame(struct panfrost_context
*ctx
, bool flush_immediate
,
1433 struct pipe_fence_handle
**fence
,
1434 struct panfrost_job
*job
)
1436 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
1437 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
1441 panfrost_job_submit(ctx
, job
);
1443 /* If visual, we can stall a frame */
1445 if (!flush_immediate
)
1446 panfrost_drm_force_flush_fragment(ctx
, fence
);
1448 screen
->last_fragment_flushed
= false;
1449 screen
->last_job
= job
;
1451 /* If readback, flush now (hurts the pipelined performance) */
1452 if (flush_immediate
)
1453 panfrost_drm_force_flush_fragment(ctx
, fence
);
1458 panfrost_draw_wallpaper(struct pipe_context
*pipe
)
1460 struct panfrost_context
*ctx
= pan_context(pipe
);
1462 /* Nothing to reload? */
1463 if (ctx
->pipe_framebuffer
.cbufs
[0] == NULL
)
1466 /* Check if the buffer has any content on it worth preserving */
1468 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[0];
1469 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
1470 unsigned level
= surf
->u
.tex
.level
;
1472 if (!rsrc
->slices
[level
].initialized
)
1475 /* Save the batch */
1476 struct panfrost_job
*batch
= panfrost_get_job_for_fbo(ctx
);
1478 ctx
->wallpaper_batch
= batch
;
1479 panfrost_blit_wallpaper(ctx
);
1480 ctx
->wallpaper_batch
= NULL
;
1485 struct pipe_context
*pipe
,
1486 struct pipe_fence_handle
**fence
,
1489 struct panfrost_context
*ctx
= pan_context(pipe
);
1490 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
1492 /* Nothing to do! */
1493 if (!job
->last_job
.gpu
&& !job
->clear
) return;
1496 panfrost_draw_wallpaper(&ctx
->base
);
1498 /* Whether to stall the pipeline for immediately correct results. Since
1499 * pipelined rendering is quite broken right now (to be fixed by the
1500 * panfrost_job refactor, just take the perf hit for correctness) */
1501 bool flush_immediate
= /*flags & PIPE_FLUSH_END_OF_FRAME*/true;
1503 /* Submit the frame itself */
1504 panfrost_submit_frame(ctx
, flush_immediate
, fence
, job
);
1506 /* Prepare for the next frame */
1507 panfrost_invalidate_frame(ctx
);
1510 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1513 g2m_draw_mode(enum pipe_prim_type mode
)
1516 DEFINE_CASE(POINTS
);
1518 DEFINE_CASE(LINE_LOOP
);
1519 DEFINE_CASE(LINE_STRIP
);
1520 DEFINE_CASE(TRIANGLES
);
1521 DEFINE_CASE(TRIANGLE_STRIP
);
1522 DEFINE_CASE(TRIANGLE_FAN
);
1524 DEFINE_CASE(QUAD_STRIP
);
1525 DEFINE_CASE(POLYGON
);
1528 unreachable("Invalid draw mode");
1535 panfrost_translate_index_size(unsigned size
)
1539 return MALI_DRAW_INDEXED_UINT8
;
1542 return MALI_DRAW_INDEXED_UINT16
;
1545 return MALI_DRAW_INDEXED_UINT32
;
1548 unreachable("Invalid index size");
1552 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1553 * good for the duration of the draw (transient), could last longer */
1556 panfrost_get_index_buffer_mapped(struct panfrost_context
*ctx
, const struct pipe_draw_info
*info
)
1558 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (info
->index
.resource
);
1560 off_t offset
= info
->start
* info
->index_size
;
1561 struct panfrost_job
*batch
= panfrost_get_job_for_fbo(ctx
);
1563 if (!info
->has_user_indices
) {
1564 /* Only resources can be directly mapped */
1565 panfrost_job_add_bo(batch
, rsrc
->bo
);
1566 return rsrc
->bo
->gpu
+ offset
;
1568 /* Otherwise, we need to upload to transient memory */
1569 const uint8_t *ibuf8
= (const uint8_t *) info
->index
.user
;
1570 return panfrost_upload_transient(ctx
, ibuf8
+ offset
, info
->count
* info
->index_size
);
1575 panfrost_scissor_culls_everything(struct panfrost_context
*ctx
)
1577 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
1579 /* Check if we're scissoring at all */
1581 if (!(ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
))
1584 return (ss
->minx
== ss
->maxx
) || (ss
->miny
== ss
->maxy
);
1589 struct pipe_context
*pipe
,
1590 const struct pipe_draw_info
*info
)
1592 struct panfrost_context
*ctx
= pan_context(pipe
);
1594 /* First of all, check the scissor to see if anything is drawn at all.
1595 * If it's not, we drop the draw (mostly a conformance issue;
1596 * well-behaved apps shouldn't hit this) */
1598 if (panfrost_scissor_culls_everything(ctx
))
1601 ctx
->payload_vertex
.draw_start
= info
->start
;
1602 ctx
->payload_tiler
.draw_start
= info
->start
;
1604 int mode
= info
->mode
;
1606 /* Fallback unsupported restart index */
1607 unsigned primitive_index
= (1 << (info
->index_size
* 8)) - 1;
1609 if (info
->primitive_restart
&& info
->index_size
1610 && info
->restart_index
!= primitive_index
) {
1611 util_draw_vbo_without_prim_restart(pipe
, info
);
1615 /* Fallback for unsupported modes */
1617 if (!(ctx
->draw_modes
& (1 << mode
))) {
1618 if (mode
== PIPE_PRIM_QUADS
&& info
->count
== 4 && ctx
->rasterizer
&& !ctx
->rasterizer
->base
.flatshade
) {
1619 mode
= PIPE_PRIM_TRIANGLE_FAN
;
1621 if (info
->count
< 4) {
1622 /* Degenerate case? */
1626 util_primconvert_save_rasterizer_state(ctx
->primconvert
, &ctx
->rasterizer
->base
);
1627 util_primconvert_draw_vbo(ctx
->primconvert
, info
);
1632 /* Now that we have a guaranteed terminating path, find the job.
1633 * Assignment commented out to prevent unused warning */
1635 /* struct panfrost_job *job = */ panfrost_get_job_for_fbo(ctx
);
1637 ctx
->payload_tiler
.prefix
.draw_mode
= g2m_draw_mode(mode
);
1639 ctx
->vertex_count
= info
->count
;
1640 ctx
->instance_count
= info
->instance_count
;
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 if (info
->index_size
) {
1673 /* Calculate the min/max index used so we can figure out how
1674 * many times to invoke the vertex shader */
1676 /* Fetch / calculate index bounds */
1677 unsigned min_index
= 0, max_index
= 0;
1679 if (info
->max_index
== ~0u) {
1680 u_vbuf_get_minmax_index(pipe
, info
, &min_index
, &max_index
);
1682 min_index
= info
->min_index
;
1683 max_index
= info
->max_index
;
1686 /* Use the corresponding values */
1687 vertex_count
= max_index
- min_index
+ 1;
1688 ctx
->payload_vertex
.draw_start
= min_index
;
1689 ctx
->payload_tiler
.draw_start
= min_index
;
1691 ctx
->payload_tiler
.prefix
.negative_start
= -min_index
;
1692 ctx
->payload_tiler
.prefix
.index_count
= MALI_POSITIVE(info
->count
);
1694 //assert(!info->restart_index); /* TODO: Research */
1695 assert(!info
->index_bias
);
1697 draw_flags
|= panfrost_translate_index_size(info
->index_size
);
1698 ctx
->payload_tiler
.prefix
.indices
= panfrost_get_index_buffer_mapped(ctx
, info
);
1700 /* Index count == vertex count, if no indexing is applied, as
1701 * if it is internally indexed in the expected order */
1703 ctx
->payload_tiler
.prefix
.negative_start
= 0;
1704 ctx
->payload_tiler
.prefix
.index_count
= MALI_POSITIVE(ctx
->vertex_count
);
1706 /* Reverse index state */
1707 ctx
->payload_tiler
.prefix
.indices
= (uintptr_t) NULL
;
1710 /* Dispatch "compute jobs" for the vertex/tiler pair as (1,
1711 * vertex_count, 1) */
1713 panfrost_pack_work_groups_fused(
1714 &ctx
->payload_vertex
.prefix
,
1715 &ctx
->payload_tiler
.prefix
,
1716 1, vertex_count
, info
->instance_count
,
1719 ctx
->payload_tiler
.prefix
.unknown_draw
= draw_flags
;
1721 /* Encode the padded vertex count */
1723 if (info
->instance_count
> 1) {
1724 /* Triangles have non-even vertex counts so they change how
1725 * padding works internally */
1728 mode
== PIPE_PRIM_TRIANGLES
||
1729 mode
== PIPE_PRIM_TRIANGLE_STRIP
||
1730 mode
== PIPE_PRIM_TRIANGLE_FAN
;
1732 struct pan_shift_odd so
=
1733 panfrost_padded_vertex_count(vertex_count
, !is_triangle
);
1735 ctx
->payload_vertex
.instance_shift
= so
.shift
;
1736 ctx
->payload_tiler
.instance_shift
= so
.shift
;
1738 ctx
->payload_vertex
.instance_odd
= so
.odd
;
1739 ctx
->payload_tiler
.instance_odd
= so
.odd
;
1741 ctx
->padded_count
= pan_expand_shift_odd(so
);
1743 ctx
->padded_count
= ctx
->vertex_count
;
1745 /* Reset instancing state */
1746 ctx
->payload_vertex
.instance_shift
= 0;
1747 ctx
->payload_vertex
.instance_odd
= 0;
1748 ctx
->payload_tiler
.instance_shift
= 0;
1749 ctx
->payload_tiler
.instance_odd
= 0;
1752 /* Fire off the draw itself */
1753 panfrost_queue_draw(ctx
);
1759 panfrost_generic_cso_delete(struct pipe_context
*pctx
, void *hwcso
)
1765 panfrost_create_rasterizer_state(
1766 struct pipe_context
*pctx
,
1767 const struct pipe_rasterizer_state
*cso
)
1769 struct panfrost_context
*ctx
= pan_context(pctx
);
1770 struct panfrost_rasterizer
*so
= CALLOC_STRUCT(panfrost_rasterizer
);
1774 /* Bitmask, unknown meaning of the start value */
1775 so
->tiler_gl_enables
= ctx
->is_t6xx
? 0x105 : 0x7;
1778 so
->tiler_gl_enables
|= MALI_FRONT_CCW_TOP
;
1780 if (cso
->cull_face
& PIPE_FACE_FRONT
)
1781 so
->tiler_gl_enables
|= MALI_CULL_FACE_FRONT
;
1783 if (cso
->cull_face
& PIPE_FACE_BACK
)
1784 so
->tiler_gl_enables
|= MALI_CULL_FACE_BACK
;
1790 panfrost_bind_rasterizer_state(
1791 struct pipe_context
*pctx
,
1794 struct panfrost_context
*ctx
= pan_context(pctx
);
1796 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1800 ctx
->rasterizer
= hwcso
;
1801 ctx
->dirty
|= PAN_DIRTY_RASTERIZER
;
1803 /* Point sprites are emulated */
1805 struct panfrost_shader_state
*variant
=
1806 ctx
->fs
? &ctx
->fs
->variants
[ctx
->fs
->active_variant
] : NULL
;
1808 if (ctx
->rasterizer
->base
.sprite_coord_enable
|| (variant
&& variant
->point_sprite_mask
))
1809 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->fs
);
1813 panfrost_create_vertex_elements_state(
1814 struct pipe_context
*pctx
,
1815 unsigned num_elements
,
1816 const struct pipe_vertex_element
*elements
)
1818 struct panfrost_vertex_state
*so
= CALLOC_STRUCT(panfrost_vertex_state
);
1820 so
->num_elements
= num_elements
;
1821 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
1823 /* XXX: What the cornball? This is totally, 100%, unapologetically
1824 * nonsense. And yet it somehow fixes a regression in -bshadow
1825 * (previously, we allocated the descriptor here... a newer commit
1826 * removed that allocation, and then memory corruption led to
1827 * shader_meta getting overwritten in bad ways and then the whole test
1828 * case falling apart . TODO: LOOK INTO PLEASE XXX XXX BAD XXX XXX XXX
1830 panfrost_allocate_chunk(pan_context(pctx
), 0, HEAP_DESCRIPTOR
);
1832 for (int i
= 0; i
< num_elements
; ++i
) {
1833 so
->hw
[i
].index
= i
;
1835 enum pipe_format fmt
= elements
[i
].src_format
;
1836 const struct util_format_description
*desc
= util_format_description(fmt
);
1837 so
->hw
[i
].unknown1
= 0x2;
1838 so
->hw
[i
].swizzle
= panfrost_get_default_swizzle(desc
->nr_channels
);
1840 so
->hw
[i
].format
= panfrost_find_format(desc
);
1842 /* The field itself should probably be shifted over */
1843 so
->hw
[i
].src_offset
= elements
[i
].src_offset
;
1850 panfrost_bind_vertex_elements_state(
1851 struct pipe_context
*pctx
,
1854 struct panfrost_context
*ctx
= pan_context(pctx
);
1856 ctx
->vertex
= hwcso
;
1857 ctx
->dirty
|= PAN_DIRTY_VERTEX
;
1861 panfrost_create_shader_state(
1862 struct pipe_context
*pctx
,
1863 const struct pipe_shader_state
*cso
)
1865 struct panfrost_shader_variants
*so
= CALLOC_STRUCT(panfrost_shader_variants
);
1868 /* Token deep copy to prevent memory corruption */
1870 if (cso
->type
== PIPE_SHADER_IR_TGSI
)
1871 so
->base
.tokens
= tgsi_dup_tokens(so
->base
.tokens
);
1877 panfrost_delete_shader_state(
1878 struct pipe_context
*pctx
,
1881 struct panfrost_shader_variants
*cso
= (struct panfrost_shader_variants
*) so
;
1883 if (cso
->base
.type
== PIPE_SHADER_IR_TGSI
) {
1884 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1891 panfrost_create_sampler_state(
1892 struct pipe_context
*pctx
,
1893 const struct pipe_sampler_state
*cso
)
1895 struct panfrost_sampler_state
*so
= CALLOC_STRUCT(panfrost_sampler_state
);
1898 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1900 struct mali_sampler_descriptor sampler_descriptor
= {
1901 .filter_mode
= MALI_TEX_MIN(translate_tex_filter(cso
->min_img_filter
))
1902 | MALI_TEX_MAG(translate_tex_filter(cso
->mag_img_filter
))
1903 | translate_mip_filter(cso
->min_mip_filter
)
1906 .wrap_s
= translate_tex_wrap(cso
->wrap_s
),
1907 .wrap_t
= translate_tex_wrap(cso
->wrap_t
),
1908 .wrap_r
= translate_tex_wrap(cso
->wrap_r
),
1909 .compare_func
= panfrost_translate_alt_compare_func(cso
->compare_func
),
1911 cso
->border_color
.f
[0],
1912 cso
->border_color
.f
[1],
1913 cso
->border_color
.f
[2],
1914 cso
->border_color
.f
[3]
1916 .min_lod
= FIXED_16(cso
->min_lod
),
1917 .max_lod
= FIXED_16(cso
->max_lod
),
1918 .seamless_cube_map
= cso
->seamless_cube_map
,
1921 /* If necessary, we disable mipmapping in the sampler descriptor by
1922 * clamping the LOD as tight as possible (from 0 to epsilon,
1923 * essentially -- remember these are fixed point numbers, so
1926 if (cso
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
)
1927 sampler_descriptor
.max_lod
= sampler_descriptor
.min_lod
;
1929 /* Enforce that there is something in the middle by adding epsilon*/
1931 if (sampler_descriptor
.min_lod
== sampler_descriptor
.max_lod
)
1932 sampler_descriptor
.max_lod
++;
1935 assert(sampler_descriptor
.max_lod
> sampler_descriptor
.min_lod
);
1937 so
->hw
= sampler_descriptor
;
1943 panfrost_bind_sampler_states(
1944 struct pipe_context
*pctx
,
1945 enum pipe_shader_type shader
,
1946 unsigned start_slot
, unsigned num_sampler
,
1949 assert(start_slot
== 0);
1951 struct panfrost_context
*ctx
= pan_context(pctx
);
1953 /* XXX: Should upload, not just copy? */
1954 ctx
->sampler_count
[shader
] = num_sampler
;
1955 memcpy(ctx
->samplers
[shader
], sampler
, num_sampler
* sizeof (void *));
1957 ctx
->dirty
|= PAN_DIRTY_SAMPLERS
;
1961 panfrost_variant_matches(
1962 struct panfrost_context
*ctx
,
1963 struct panfrost_shader_state
*variant
,
1964 enum pipe_shader_type type
)
1966 struct pipe_rasterizer_state
*rasterizer
= &ctx
->rasterizer
->base
;
1967 struct pipe_alpha_state
*alpha
= &ctx
->depth_stencil
->alpha
;
1969 bool is_fragment
= (type
== PIPE_SHADER_FRAGMENT
);
1971 if (is_fragment
&& (alpha
->enabled
|| variant
->alpha_state
.enabled
)) {
1972 /* Make sure enable state is at least the same */
1973 if (alpha
->enabled
!= variant
->alpha_state
.enabled
) {
1977 /* Check that the contents of the test are the same */
1978 bool same_func
= alpha
->func
== variant
->alpha_state
.func
;
1979 bool same_ref
= alpha
->ref_value
== variant
->alpha_state
.ref_value
;
1981 if (!(same_func
&& same_ref
)) {
1986 if (is_fragment
&& rasterizer
&& (rasterizer
->sprite_coord_enable
|
1987 variant
->point_sprite_mask
)) {
1988 /* Ensure the same varyings are turned to point sprites */
1989 if (rasterizer
->sprite_coord_enable
!= variant
->point_sprite_mask
)
1992 /* Ensure the orientation is correct */
1994 rasterizer
->sprite_coord_mode
==
1995 PIPE_SPRITE_COORD_UPPER_LEFT
;
1997 if (variant
->point_sprite_upper_left
!= upper_left
)
2001 /* Otherwise, we're good to go */
2006 panfrost_bind_shader_state(
2007 struct pipe_context
*pctx
,
2009 enum pipe_shader_type type
)
2011 struct panfrost_context
*ctx
= pan_context(pctx
);
2013 if (type
== PIPE_SHADER_FRAGMENT
) {
2015 ctx
->dirty
|= PAN_DIRTY_FS
;
2017 assert(type
== PIPE_SHADER_VERTEX
);
2019 ctx
->dirty
|= PAN_DIRTY_VS
;
2024 /* Match the appropriate variant */
2026 signed variant
= -1;
2027 struct panfrost_shader_variants
*variants
= (struct panfrost_shader_variants
*) hwcso
;
2029 for (unsigned i
= 0; i
< variants
->variant_count
; ++i
) {
2030 if (panfrost_variant_matches(ctx
, &variants
->variants
[i
], type
)) {
2036 if (variant
== -1) {
2037 /* No variant matched, so create a new one */
2038 variant
= variants
->variant_count
++;
2039 assert(variants
->variant_count
< MAX_SHADER_VARIANTS
);
2041 struct panfrost_shader_state
*v
=
2042 &variants
->variants
[variant
];
2046 if (type
== PIPE_SHADER_FRAGMENT
) {
2047 v
->alpha_state
= ctx
->depth_stencil
->alpha
;
2049 if (ctx
->rasterizer
) {
2050 v
->point_sprite_mask
= ctx
->rasterizer
->base
.sprite_coord_enable
;
2051 v
->point_sprite_upper_left
=
2052 ctx
->rasterizer
->base
.sprite_coord_mode
==
2053 PIPE_SPRITE_COORD_UPPER_LEFT
;
2057 /* Allocate the mapped descriptor ahead-of-time. */
2058 struct panfrost_context
*ctx
= pan_context(pctx
);
2059 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, sizeof(struct mali_shader_meta
), HEAP_DESCRIPTOR
);
2061 variants
->variants
[variant
].tripipe
= (struct mali_shader_meta
*) transfer
.cpu
;
2062 variants
->variants
[variant
].tripipe_gpu
= transfer
.gpu
;
2066 /* Select this variant */
2067 variants
->active_variant
= variant
;
2069 struct panfrost_shader_state
*shader_state
= &variants
->variants
[variant
];
2070 assert(panfrost_variant_matches(ctx
, shader_state
, type
));
2072 /* We finally have a variant, so compile it */
2074 if (!shader_state
->compiled
) {
2075 panfrost_shader_compile(ctx
, shader_state
->tripipe
, NULL
,
2076 panfrost_job_type_for_pipe(type
), shader_state
);
2078 shader_state
->compiled
= true;
2083 panfrost_bind_vs_state(struct pipe_context
*pctx
, void *hwcso
)
2085 panfrost_bind_shader_state(pctx
, hwcso
, PIPE_SHADER_VERTEX
);
2089 panfrost_bind_fs_state(struct pipe_context
*pctx
, void *hwcso
)
2091 panfrost_bind_shader_state(pctx
, hwcso
, PIPE_SHADER_FRAGMENT
);
2095 panfrost_set_vertex_buffers(
2096 struct pipe_context
*pctx
,
2097 unsigned start_slot
,
2098 unsigned num_buffers
,
2099 const struct pipe_vertex_buffer
*buffers
)
2101 struct panfrost_context
*ctx
= pan_context(pctx
);
2103 util_set_vertex_buffers_mask(ctx
->vertex_buffers
, &ctx
->vb_mask
, buffers
, start_slot
, num_buffers
);
2107 panfrost_set_constant_buffer(
2108 struct pipe_context
*pctx
,
2109 enum pipe_shader_type shader
, uint index
,
2110 const struct pipe_constant_buffer
*buf
)
2112 struct panfrost_context
*ctx
= pan_context(pctx
);
2113 struct panfrost_constant_buffer
*pbuf
= &ctx
->constant_buffer
[shader
];
2115 util_copy_constant_buffer(&pbuf
->cb
[index
], buf
);
2117 unsigned mask
= (1 << index
);
2119 if (unlikely(!buf
)) {
2120 pbuf
->enabled_mask
&= ~mask
;
2121 pbuf
->dirty_mask
&= ~mask
;
2125 pbuf
->enabled_mask
|= mask
;
2126 pbuf
->dirty_mask
|= mask
;
2130 panfrost_set_stencil_ref(
2131 struct pipe_context
*pctx
,
2132 const struct pipe_stencil_ref
*ref
)
2134 struct panfrost_context
*ctx
= pan_context(pctx
);
2135 ctx
->stencil_ref
= *ref
;
2137 /* Shader core dirty */
2138 ctx
->dirty
|= PAN_DIRTY_FS
;
2141 static enum mali_texture_type
2142 panfrost_translate_texture_type(enum pipe_texture_target t
)
2146 case PIPE_TEXTURE_1D
:
2147 case PIPE_TEXTURE_1D_ARRAY
:
2150 case PIPE_TEXTURE_2D
:
2151 case PIPE_TEXTURE_2D_ARRAY
:
2152 case PIPE_TEXTURE_RECT
:
2155 case PIPE_TEXTURE_3D
:
2158 case PIPE_TEXTURE_CUBE
:
2159 case PIPE_TEXTURE_CUBE_ARRAY
:
2160 return MALI_TEX_CUBE
;
2163 unreachable("Unknown target");
2167 static struct pipe_sampler_view
*
2168 panfrost_create_sampler_view(
2169 struct pipe_context
*pctx
,
2170 struct pipe_resource
*texture
,
2171 const struct pipe_sampler_view
*template)
2173 struct panfrost_sampler_view
*so
= rzalloc(pctx
, struct panfrost_sampler_view
);
2174 int bytes_per_pixel
= util_format_get_blocksize(texture
->format
);
2176 pipe_reference(NULL
, &texture
->reference
);
2178 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) texture
;
2181 so
->base
= *template;
2182 so
->base
.texture
= texture
;
2183 so
->base
.reference
.count
= 1;
2184 so
->base
.context
= pctx
;
2186 /* sampler_views correspond to texture descriptors, minus the texture
2187 * (data) itself. So, we serialise the descriptor here and cache it for
2190 /* TODO: Detect from format better */
2191 const struct util_format_description
*desc
= util_format_description(prsrc
->base
.format
);
2193 unsigned char user_swizzle
[4] = {
2194 template->swizzle_r
,
2195 template->swizzle_g
,
2196 template->swizzle_b
,
2200 enum mali_format format
= panfrost_find_format(desc
);
2202 bool is_depth
= desc
->format
== PIPE_FORMAT_Z32_UNORM
;
2204 unsigned usage2_layout
= 0x10;
2206 switch (prsrc
->layout
) {
2208 usage2_layout
|= 0x8 | 0x4;
2211 usage2_layout
|= 0x1;
2214 usage2_layout
|= is_depth
? 0x1 : 0x2;
2221 /* Check if we need to set a custom stride by computing the "expected"
2222 * stride and comparing it to what the BO actually wants. Only applies
2223 * to linear textures, since tiled/compressed textures have strict
2224 * alignment requirements for their strides as it is */
2226 unsigned first_level
= template->u
.tex
.first_level
;
2227 unsigned last_level
= template->u
.tex
.last_level
;
2229 if (prsrc
->layout
== PAN_LINEAR
) {
2230 for (unsigned l
= first_level
; l
<= last_level
; ++l
) {
2231 unsigned actual_stride
= prsrc
->slices
[l
].stride
;
2232 unsigned width
= u_minify(texture
->width0
, l
);
2233 unsigned comp_stride
= width
* bytes_per_pixel
;
2235 if (comp_stride
!= actual_stride
) {
2236 usage2_layout
|= MALI_TEX_MANUAL_STRIDE
;
2242 /* In the hardware, array_size refers specifically to array textures,
2243 * whereas in Gallium, it also covers cubemaps */
2245 unsigned array_size
= texture
->array_size
;
2247 if (template->target
== PIPE_TEXTURE_CUBE
) {
2248 /* TODO: Cubemap arrays */
2249 assert(array_size
== 6);
2253 struct mali_texture_descriptor texture_descriptor
= {
2254 .width
= MALI_POSITIVE(u_minify(texture
->width0
, first_level
)),
2255 .height
= MALI_POSITIVE(u_minify(texture
->height0
, first_level
)),
2256 .depth
= MALI_POSITIVE(u_minify(texture
->depth0
, first_level
)),
2257 .array_size
= MALI_POSITIVE(array_size
),
2261 .swizzle
= panfrost_translate_swizzle_4(desc
->swizzle
),
2264 .srgb
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
,
2265 .type
= panfrost_translate_texture_type(template->target
),
2267 .usage2
= usage2_layout
2270 .swizzle
= panfrost_translate_swizzle_4(user_swizzle
)
2273 texture_descriptor
.nr_mipmap_levels
= last_level
- first_level
;
2275 so
->hw
= texture_descriptor
;
2277 return (struct pipe_sampler_view
*) so
;
2281 panfrost_set_sampler_views(
2282 struct pipe_context
*pctx
,
2283 enum pipe_shader_type shader
,
2284 unsigned start_slot
, unsigned num_views
,
2285 struct pipe_sampler_view
**views
)
2287 struct panfrost_context
*ctx
= pan_context(pctx
);
2289 assert(start_slot
== 0);
2291 unsigned new_nr
= 0;
2292 for (unsigned i
= 0; i
< num_views
; ++i
) {
2297 ctx
->sampler_view_count
[shader
] = new_nr
;
2298 memcpy(ctx
->sampler_views
[shader
], views
, num_views
* sizeof (void *));
2300 ctx
->dirty
|= PAN_DIRTY_TEXTURES
;
2304 panfrost_sampler_view_destroy(
2305 struct pipe_context
*pctx
,
2306 struct pipe_sampler_view
*view
)
2308 pipe_resource_reference(&view
->texture
, NULL
);
2313 panfrost_set_framebuffer_state(struct pipe_context
*pctx
,
2314 const struct pipe_framebuffer_state
*fb
)
2316 struct panfrost_context
*ctx
= pan_context(pctx
);
2318 /* Flush when switching framebuffers, but not if the framebuffer
2319 * state is being restored by u_blitter
2322 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
2323 bool is_scanout
= panfrost_is_scanout(ctx
);
2324 bool has_draws
= job
->last_job
.gpu
;
2326 if (!ctx
->wallpaper_batch
&& (!is_scanout
|| has_draws
)) {
2327 panfrost_flush(pctx
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2330 ctx
->pipe_framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
2331 ctx
->pipe_framebuffer
.samples
= fb
->samples
;
2332 ctx
->pipe_framebuffer
.layers
= fb
->layers
;
2333 ctx
->pipe_framebuffer
.width
= fb
->width
;
2334 ctx
->pipe_framebuffer
.height
= fb
->height
;
2336 for (int i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2337 struct pipe_surface
*cb
= i
< fb
->nr_cbufs
? fb
->cbufs
[i
] : NULL
;
2339 /* check if changing cbuf */
2340 if (ctx
->pipe_framebuffer
.cbufs
[i
] == cb
) continue;
2342 if (cb
&& (i
!= 0)) {
2343 DBG("XXX: Multiple render targets not supported before t7xx!\n");
2348 pipe_surface_reference(&ctx
->pipe_framebuffer
.cbufs
[i
], cb
);
2353 if (ctx
->require_sfbd
)
2354 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
, ~0);
2356 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
, ~0);
2358 panfrost_attach_vt_framebuffer(ctx
);
2362 struct pipe_surface
*zb
= fb
->zsbuf
;
2364 if (ctx
->pipe_framebuffer
.zsbuf
!= zb
) {
2365 pipe_surface_reference(&ctx
->pipe_framebuffer
.zsbuf
, zb
);
2368 if (ctx
->require_sfbd
)
2369 ctx
->vt_framebuffer_sfbd
= panfrost_emit_sfbd(ctx
, ~0);
2371 ctx
->vt_framebuffer_mfbd
= panfrost_emit_mfbd(ctx
, ~0);
2373 panfrost_attach_vt_framebuffer(ctx
);
2380 panfrost_create_blend_state(struct pipe_context
*pipe
,
2381 const struct pipe_blend_state
*blend
)
2383 struct panfrost_context
*ctx
= pan_context(pipe
);
2384 struct panfrost_blend_state
*so
= rzalloc(ctx
, struct panfrost_blend_state
);
2387 /* TODO: The following features are not yet implemented */
2388 assert(!blend
->logicop_enable
);
2389 assert(!blend
->alpha_to_coverage
);
2390 assert(!blend
->alpha_to_one
);
2392 /* Compile the blend state, first as fixed-function if we can */
2394 if (panfrost_make_fixed_blend_mode(&blend
->rt
[0], so
, blend
->rt
[0].colormask
, &ctx
->blend_color
))
2397 /* If we can't, compile a blend shader instead */
2399 panfrost_make_blend_shader(ctx
, so
, &ctx
->blend_color
);
2405 panfrost_bind_blend_state(struct pipe_context
*pipe
,
2408 struct panfrost_context
*ctx
= pan_context(pipe
);
2409 struct pipe_blend_state
*blend
= (struct pipe_blend_state
*) cso
;
2410 struct panfrost_blend_state
*pblend
= (struct panfrost_blend_state
*) cso
;
2411 ctx
->blend
= pblend
;
2416 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_NO_DITHER
, !blend
->dither
);
2418 /* TODO: Attach color */
2420 /* Shader itself is not dirty, but the shader core is */
2421 ctx
->dirty
|= PAN_DIRTY_FS
;
2425 panfrost_delete_blend_state(struct pipe_context
*pipe
,
2428 struct panfrost_blend_state
*so
= (struct panfrost_blend_state
*) blend
;
2430 if (so
->has_blend_shader
) {
2431 DBG("Deleting blend state leak blend shaders bytecode\n");
2438 panfrost_set_blend_color(struct pipe_context
*pipe
,
2439 const struct pipe_blend_color
*blend_color
)
2441 struct panfrost_context
*ctx
= pan_context(pipe
);
2443 /* If blend_color is we're unbinding, so ctx->blend_color is now undefined -> nothing to do */
2446 ctx
->blend_color
= *blend_color
;
2448 /* The blend mode depends on the blend constant color, due to the
2449 * fixed/programmable split. So, we're forced to regenerate the blend
2452 /* TODO: Attach color */
2457 panfrost_create_depth_stencil_state(struct pipe_context
*pipe
,
2458 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
2460 return mem_dup(depth_stencil
, sizeof(*depth_stencil
));
2464 panfrost_bind_depth_stencil_state(struct pipe_context
*pipe
,
2467 struct panfrost_context
*ctx
= pan_context(pipe
);
2468 struct pipe_depth_stencil_alpha_state
*depth_stencil
= cso
;
2469 ctx
->depth_stencil
= depth_stencil
;
2474 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2475 * emulated in the fragment shader */
2477 if (depth_stencil
->alpha
.enabled
) {
2478 /* We need to trigger a new shader (maybe) */
2479 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->fs
);
2483 SET_BIT(ctx
->fragment_shader_core
.unknown2_4
, MALI_STENCIL_TEST
, depth_stencil
->stencil
[0].enabled
); /* XXX: which one? */
2485 panfrost_make_stencil_state(&depth_stencil
->stencil
[0], &ctx
->fragment_shader_core
.stencil_front
);
2486 ctx
->fragment_shader_core
.stencil_mask_front
= depth_stencil
->stencil
[0].writemask
;
2488 panfrost_make_stencil_state(&depth_stencil
->stencil
[1], &ctx
->fragment_shader_core
.stencil_back
);
2489 ctx
->fragment_shader_core
.stencil_mask_back
= depth_stencil
->stencil
[1].writemask
;
2491 /* Depth state (TODO: Refactor) */
2492 SET_BIT(ctx
->fragment_shader_core
.unknown2_3
, MALI_DEPTH_TEST
, depth_stencil
->depth
.enabled
);
2494 int func
= depth_stencil
->depth
.enabled
? depth_stencil
->depth
.func
: PIPE_FUNC_ALWAYS
;
2496 ctx
->fragment_shader_core
.unknown2_3
&= ~MALI_DEPTH_FUNC_MASK
;
2497 ctx
->fragment_shader_core
.unknown2_3
|= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func
));
2499 /* Bounds test not implemented */
2500 assert(!depth_stencil
->depth
.bounds_test
);
2502 ctx
->dirty
|= PAN_DIRTY_FS
;
2506 panfrost_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
2512 panfrost_set_sample_mask(struct pipe_context
*pipe
,
2513 unsigned sample_mask
)
2518 panfrost_set_clip_state(struct pipe_context
*pipe
,
2519 const struct pipe_clip_state
*clip
)
2521 //struct panfrost_context *panfrost = pan_context(pipe);
2525 panfrost_set_viewport_states(struct pipe_context
*pipe
,
2526 unsigned start_slot
,
2527 unsigned num_viewports
,
2528 const struct pipe_viewport_state
*viewports
)
2530 struct panfrost_context
*ctx
= pan_context(pipe
);
2532 assert(start_slot
== 0);
2533 assert(num_viewports
== 1);
2535 ctx
->pipe_viewport
= *viewports
;
2539 panfrost_set_scissor_states(struct pipe_context
*pipe
,
2540 unsigned start_slot
,
2541 unsigned num_scissors
,
2542 const struct pipe_scissor_state
*scissors
)
2544 struct panfrost_context
*ctx
= pan_context(pipe
);
2546 assert(start_slot
== 0);
2547 assert(num_scissors
== 1);
2549 ctx
->scissor
= *scissors
;
2553 panfrost_set_polygon_stipple(struct pipe_context
*pipe
,
2554 const struct pipe_poly_stipple
*stipple
)
2556 //struct panfrost_context *panfrost = pan_context(pipe);
2560 panfrost_set_active_query_state(struct pipe_context
*pipe
,
2563 //struct panfrost_context *panfrost = pan_context(pipe);
2567 panfrost_destroy(struct pipe_context
*pipe
)
2569 struct panfrost_context
*panfrost
= pan_context(pipe
);
2570 struct panfrost_screen
*screen
= pan_screen(pipe
->screen
);
2572 if (panfrost
->blitter
)
2573 util_blitter_destroy(panfrost
->blitter
);
2575 if (panfrost
->blitter_wallpaper
)
2576 util_blitter_destroy(panfrost
->blitter_wallpaper
);
2578 panfrost_drm_free_slab(screen
, &panfrost
->scratchpad
);
2579 panfrost_drm_free_slab(screen
, &panfrost
->varying_mem
);
2580 panfrost_drm_free_slab(screen
, &panfrost
->shaders
);
2581 panfrost_drm_free_slab(screen
, &panfrost
->tiler_heap
);
2582 panfrost_drm_free_slab(screen
, &panfrost
->tiler_polygon_list
);
2583 panfrost_drm_free_slab(screen
, &panfrost
->tiler_dummy
);
2585 for (int i
= 0; i
< ARRAY_SIZE(panfrost
->transient_pools
); ++i
) {
2586 struct panfrost_memory_entry
*entry
;
2587 entry
= panfrost
->transient_pools
[i
].entries
[0];
2588 pb_slab_free(&screen
->slabs
, (struct pb_slab_entry
*)entry
);
2594 static struct pipe_query
*
2595 panfrost_create_query(struct pipe_context
*pipe
,
2599 struct panfrost_query
*q
= rzalloc(pipe
, struct panfrost_query
);
2604 return (struct pipe_query
*) q
;
2608 panfrost_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2614 panfrost_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2616 struct panfrost_context
*ctx
= pan_context(pipe
);
2617 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2619 switch (query
->type
) {
2620 case PIPE_QUERY_OCCLUSION_COUNTER
:
2621 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2622 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
2624 /* Allocate a word for the query results to be stored */
2625 query
->transfer
= panfrost_allocate_chunk(ctx
, sizeof(unsigned), HEAP_DESCRIPTOR
);
2627 ctx
->occlusion_query
= query
;
2633 DBG("Skipping query %d\n", query
->type
);
2641 panfrost_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
2643 struct panfrost_context
*ctx
= pan_context(pipe
);
2644 ctx
->occlusion_query
= NULL
;
2649 panfrost_get_query_result(struct pipe_context
*pipe
,
2650 struct pipe_query
*q
,
2652 union pipe_query_result
*vresult
)
2655 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
2657 /* We need to flush out the jobs to actually run the counter, TODO
2658 * check wait, TODO wallpaper after if needed */
2660 panfrost_flush(pipe
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
2662 switch (query
->type
) {
2663 case PIPE_QUERY_OCCLUSION_COUNTER
:
2664 case PIPE_QUERY_OCCLUSION_PREDICATE
:
2665 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
: {
2666 /* Read back the query results */
2667 unsigned *result
= (unsigned *) query
->transfer
.cpu
;
2668 unsigned passed
= *result
;
2670 if (query
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
2671 vresult
->u64
= passed
;
2673 vresult
->b
= !!passed
;
2679 DBG("Skipped query get %d\n", query
->type
);
2686 static struct pipe_stream_output_target
*
2687 panfrost_create_stream_output_target(struct pipe_context
*pctx
,
2688 struct pipe_resource
*prsc
,
2689 unsigned buffer_offset
,
2690 unsigned buffer_size
)
2692 struct pipe_stream_output_target
*target
;
2694 target
= rzalloc(pctx
, struct pipe_stream_output_target
);
2699 pipe_reference_init(&target
->reference
, 1);
2700 pipe_resource_reference(&target
->buffer
, prsc
);
2702 target
->context
= pctx
;
2703 target
->buffer_offset
= buffer_offset
;
2704 target
->buffer_size
= buffer_size
;
2710 panfrost_stream_output_target_destroy(struct pipe_context
*pctx
,
2711 struct pipe_stream_output_target
*target
)
2713 pipe_resource_reference(&target
->buffer
, NULL
);
2714 ralloc_free(target
);
2718 panfrost_set_stream_output_targets(struct pipe_context
*pctx
,
2719 unsigned num_targets
,
2720 struct pipe_stream_output_target
**targets
,
2721 const unsigned *offsets
)
2727 panfrost_setup_hardware(struct panfrost_context
*ctx
)
2729 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2730 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
2732 for (int i
= 0; i
< ARRAY_SIZE(ctx
->transient_pools
); ++i
) {
2733 /* Allocate the beginning of the transient pool */
2734 int entry_size
= (1 << 22); /* 4MB */
2736 ctx
->transient_pools
[i
].entry_size
= entry_size
;
2737 ctx
->transient_pools
[i
].entry_count
= 1;
2739 ctx
->transient_pools
[i
].entries
[0] = (struct panfrost_memory_entry
*) pb_slab_alloc(&screen
->slabs
, entry_size
, HEAP_TRANSIENT
);
2742 panfrost_drm_allocate_slab(screen
, &ctx
->scratchpad
, 64, false, 0, 0, 0);
2743 panfrost_drm_allocate_slab(screen
, &ctx
->varying_mem
, 16384, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_COHERENT_LOCAL
, 0, 0);
2744 panfrost_drm_allocate_slab(screen
, &ctx
->shaders
, 4096, true, PAN_ALLOCATE_EXECUTE
, 0, 0);
2745 panfrost_drm_allocate_slab(screen
, &ctx
->tiler_heap
, 32768, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2746 panfrost_drm_allocate_slab(screen
, &ctx
->tiler_polygon_list
, 128*128, false, PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_GROWABLE
, 1, 128);
2747 panfrost_drm_allocate_slab(screen
, &ctx
->tiler_dummy
, 1, false, PAN_ALLOCATE_INVISIBLE
, 0, 0);
2750 /* New context creation, which also does hardware initialisation since I don't
2751 * know the better way to structure this :smirk: */
2753 struct pipe_context
*
2754 panfrost_create_context(struct pipe_screen
*screen
, void *priv
, unsigned flags
)
2756 struct panfrost_context
*ctx
= rzalloc(screen
, struct panfrost_context
);
2757 struct panfrost_screen
*pscreen
= pan_screen(screen
);
2758 memset(ctx
, 0, sizeof(*ctx
));
2759 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
2762 gpu_id
= panfrost_drm_query_gpu_version(pscreen
);
2764 ctx
->is_t6xx
= gpu_id
<= 0x0750; /* For now, this flag means T760 or less */
2765 ctx
->require_sfbd
= gpu_id
< 0x0750; /* T760 is the first to support MFBD */
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
;
2780 gallium
->set_stencil_ref
= panfrost_set_stencil_ref
;
2782 gallium
->create_sampler_view
= panfrost_create_sampler_view
;
2783 gallium
->set_sampler_views
= panfrost_set_sampler_views
;
2784 gallium
->sampler_view_destroy
= panfrost_sampler_view_destroy
;
2786 gallium
->create_rasterizer_state
= panfrost_create_rasterizer_state
;
2787 gallium
->bind_rasterizer_state
= panfrost_bind_rasterizer_state
;
2788 gallium
->delete_rasterizer_state
= panfrost_generic_cso_delete
;
2790 gallium
->create_vertex_elements_state
= panfrost_create_vertex_elements_state
;
2791 gallium
->bind_vertex_elements_state
= panfrost_bind_vertex_elements_state
;
2792 gallium
->delete_vertex_elements_state
= panfrost_generic_cso_delete
;
2794 gallium
->create_fs_state
= panfrost_create_shader_state
;
2795 gallium
->delete_fs_state
= panfrost_delete_shader_state
;
2796 gallium
->bind_fs_state
= panfrost_bind_fs_state
;
2798 gallium
->create_vs_state
= panfrost_create_shader_state
;
2799 gallium
->delete_vs_state
= panfrost_delete_shader_state
;
2800 gallium
->bind_vs_state
= panfrost_bind_vs_state
;
2802 gallium
->create_sampler_state
= panfrost_create_sampler_state
;
2803 gallium
->delete_sampler_state
= panfrost_generic_cso_delete
;
2804 gallium
->bind_sampler_states
= panfrost_bind_sampler_states
;
2806 gallium
->create_blend_state
= panfrost_create_blend_state
;
2807 gallium
->bind_blend_state
= panfrost_bind_blend_state
;
2808 gallium
->delete_blend_state
= panfrost_delete_blend_state
;
2810 gallium
->set_blend_color
= panfrost_set_blend_color
;
2812 gallium
->create_depth_stencil_alpha_state
= panfrost_create_depth_stencil_state
;
2813 gallium
->bind_depth_stencil_alpha_state
= panfrost_bind_depth_stencil_state
;
2814 gallium
->delete_depth_stencil_alpha_state
= panfrost_delete_depth_stencil_state
;
2816 gallium
->set_sample_mask
= panfrost_set_sample_mask
;
2818 gallium
->set_clip_state
= panfrost_set_clip_state
;
2819 gallium
->set_viewport_states
= panfrost_set_viewport_states
;
2820 gallium
->set_scissor_states
= panfrost_set_scissor_states
;
2821 gallium
->set_polygon_stipple
= panfrost_set_polygon_stipple
;
2822 gallium
->set_active_query_state
= panfrost_set_active_query_state
;
2824 gallium
->create_query
= panfrost_create_query
;
2825 gallium
->destroy_query
= panfrost_destroy_query
;
2826 gallium
->begin_query
= panfrost_begin_query
;
2827 gallium
->end_query
= panfrost_end_query
;
2828 gallium
->get_query_result
= panfrost_get_query_result
;
2830 gallium
->create_stream_output_target
= panfrost_create_stream_output_target
;
2831 gallium
->stream_output_target_destroy
= panfrost_stream_output_target_destroy
;
2832 gallium
->set_stream_output_targets
= panfrost_set_stream_output_targets
;
2834 panfrost_resource_context_init(gallium
);
2836 panfrost_drm_init_context(ctx
);
2838 panfrost_setup_hardware(ctx
);
2841 gallium
->stream_uploader
= u_upload_create_default(gallium
);
2842 gallium
->const_uploader
= gallium
->stream_uploader
;
2843 assert(gallium
->stream_uploader
);
2845 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2846 ctx
->draw_modes
= (1 << (PIPE_PRIM_POLYGON
+ 1)) - 1;
2848 ctx
->primconvert
= util_primconvert_create(gallium
, ctx
->draw_modes
);
2850 ctx
->blitter
= util_blitter_create(gallium
);
2851 ctx
->blitter_wallpaper
= util_blitter_create(gallium
);
2853 assert(ctx
->blitter
);
2854 assert(ctx
->blitter_wallpaper
);
2856 /* Prepare for render! */
2858 panfrost_job_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
);