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
31 #include "pan_context.h"
32 #include "pan_minmax_cache.h"
33 #include "panfrost-quirks.h"
35 #include "util/macros.h"
36 #include "util/format/u_format.h"
37 #include "util/u_inlines.h"
38 #include "util/u_upload_mgr.h"
39 #include "util/u_memory.h"
40 #include "util/u_vbuf.h"
41 #include "util/half_float.h"
42 #include "util/u_helpers.h"
43 #include "util/format/u_format.h"
44 #include "util/u_prim.h"
45 #include "util/u_prim_restart.h"
46 #include "indices/u_primconvert.h"
47 #include "tgsi/tgsi_parse.h"
48 #include "tgsi/tgsi_from_mesa.h"
49 #include "util/u_math.h"
51 #include "pan_screen.h"
52 #include "pan_blending.h"
53 #include "pan_blend_shaders.h"
54 #include "pan_cmdstream.h"
56 #include "pandecode/decode.h"
58 struct midgard_tiler_descriptor
59 panfrost_emit_midg_tiler(struct panfrost_batch
*batch
, unsigned vertex_count
)
61 struct panfrost_screen
*screen
= pan_screen(batch
->ctx
->base
.screen
);
62 bool hierarchy
= !(screen
->quirks
& MIDGARD_NO_HIER_TILING
);
63 struct midgard_tiler_descriptor t
= {0};
64 unsigned height
= batch
->key
.height
;
65 unsigned width
= batch
->key
.width
;
68 panfrost_choose_hierarchy_mask(width
, height
, vertex_count
, hierarchy
);
70 /* Compute the polygon header size and use that to offset the body */
72 unsigned header_size
= panfrost_tiler_header_size(
73 width
, height
, t
.hierarchy_mask
, hierarchy
);
75 t
.polygon_list_size
= panfrost_tiler_full_size(
76 width
, height
, t
.hierarchy_mask
, hierarchy
);
81 struct panfrost_bo
*tiler_heap
;
83 tiler_heap
= panfrost_batch_get_tiler_heap(batch
);
84 t
.polygon_list
= panfrost_batch_get_polygon_list(batch
,
89 /* Allow the entire tiler heap */
90 t
.heap_start
= tiler_heap
->gpu
;
91 t
.heap_end
= tiler_heap
->gpu
+ tiler_heap
->size
;
93 struct panfrost_bo
*tiler_dummy
;
95 tiler_dummy
= panfrost_batch_get_tiler_dummy(batch
);
96 header_size
= MALI_TILER_MINIMUM_HEADER_SIZE
;
98 /* The tiler is disabled, so don't allow the tiler heap */
99 t
.heap_start
= tiler_dummy
->gpu
;
100 t
.heap_end
= t
.heap_start
;
102 /* Use a dummy polygon list */
103 t
.polygon_list
= tiler_dummy
->gpu
;
105 /* Disable the tiler */
107 t
.hierarchy_mask
|= MALI_TILER_DISABLED
;
109 t
.hierarchy_mask
= MALI_TILER_USER
;
110 t
.polygon_list_size
= MALI_TILER_MINIMUM_HEADER_SIZE
+ 4;
112 /* We don't have a WRITE_VALUE job, so write the polygon list manually */
113 uint32_t *polygon_list_body
= (uint32_t *) (tiler_dummy
->cpu
+ header_size
);
114 polygon_list_body
[0] = 0xa0000000; /* TODO: Just that? */
118 t
.polygon_list_body
=
119 t
.polygon_list
+ header_size
;
126 struct pipe_context
*pipe
,
128 const union pipe_color_union
*color
,
129 double depth
, unsigned stencil
)
131 struct panfrost_context
*ctx
= pan_context(pipe
);
133 /* TODO: panfrost_get_fresh_batch_for_fbo() instantiates a new batch if
134 * the existing batch targeting this FBO has draws. We could probably
135 * avoid that by replacing plain clears by quad-draws with a specific
136 * color/depth/stencil value, thus avoiding the generation of extra
139 struct panfrost_batch
*batch
= panfrost_get_fresh_batch_for_fbo(ctx
);
141 panfrost_batch_add_fbo_bos(batch
);
142 panfrost_batch_clear(batch
, buffers
, color
, depth
, stencil
);
145 /* Reset per-frame context, called on context initialisation as well as after
146 * flushing a frame */
149 panfrost_invalidate_frame(struct panfrost_context
*ctx
)
151 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; ++i
)
152 ctx
->payloads
[i
].postfix
.shared_memory
= 0;
154 /* TODO: When does this need to be handled? */
155 ctx
->active_queries
= true;
158 /* In practice, every field of these payloads should be configurable
159 * arbitrarily, which means these functions are basically catch-all's for
160 * as-of-yet unwavering unknowns */
163 panfrost_emit_vertex_payload(struct panfrost_context
*ctx
)
165 /* 0x2 bit clear on 32-bit T6XX */
167 struct midgard_payload_vertex_tiler payload
= {
168 .gl_enables
= 0x4 | 0x2,
171 /* Vertex and compute are closely coupled, so share a payload */
173 memcpy(&ctx
->payloads
[PIPE_SHADER_VERTEX
], &payload
, sizeof(payload
));
174 memcpy(&ctx
->payloads
[PIPE_SHADER_COMPUTE
], &payload
, sizeof(payload
));
178 panfrost_writes_point_size(struct panfrost_context
*ctx
)
180 assert(ctx
->shader
[PIPE_SHADER_VERTEX
]);
181 struct panfrost_shader_state
*vs
= panfrost_get_shader_state(ctx
, PIPE_SHADER_VERTEX
);
183 return vs
->writes_point_size
&& ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.draw_mode
== MALI_POINTS
;
186 /* Stage the attribute descriptors so we can adjust src_offset
187 * to let BOs align nicely */
190 panfrost_stage_attributes(struct panfrost_context
*ctx
)
192 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
193 struct panfrost_vertex_state
*so
= ctx
->vertex
;
195 size_t sz
= sizeof(struct mali_attr_meta
) * PAN_MAX_ATTRIBUTE
;
196 struct panfrost_transfer transfer
= panfrost_allocate_transient(batch
, sz
);
197 struct mali_attr_meta
*target
= (struct mali_attr_meta
*) transfer
.cpu
;
199 /* Copy as-is for the first pass */
200 memcpy(target
, so
->hw
, sz
);
202 /* Fixup offsets for the second pass. Recall that the hardware
203 * calculates attribute addresses as:
205 * addr = base + (stride * vtx) + src_offset;
207 * However, on Mali, base must be aligned to 64-bytes, so we
210 * base' = base & ~63 = base - (base & 63)
212 * To compensate when using base' (see emit_vertex_data), we have
213 * to adjust src_offset by the masked off piece:
215 * addr' = base' + (stride * vtx) + (src_offset + (base & 63))
216 * = base - (base & 63) + (stride * vtx) + src_offset + (base & 63)
217 * = base + (stride * vtx) + src_offset
223 unsigned start
= ctx
->payloads
[PIPE_SHADER_VERTEX
].offset_start
;
225 for (unsigned i
= 0; i
< so
->num_elements
; ++i
) {
226 unsigned vbi
= so
->pipe
[i
].vertex_buffer_index
;
227 struct pipe_vertex_buffer
*buf
= &ctx
->vertex_buffers
[vbi
];
229 /* Adjust by the masked off bits of the offset. Make sure we
230 * read src_offset from so->hw (which is not GPU visible)
231 * rather than target (which is) due to caching effects */
233 unsigned src_offset
= so
->pipe
[i
].src_offset
;
235 /* BOs aligned to 4k so guaranteed aligned to 64 */
236 src_offset
+= (buf
->buffer_offset
& 63);
238 /* Also, somewhat obscurely per-instance data needs to be
239 * offset in response to a delayed start in an indexed draw */
241 if (so
->pipe
[i
].instance_divisor
&& ctx
->instance_count
> 1 && start
)
242 src_offset
-= buf
->stride
* start
;
244 target
[i
].src_offset
= src_offset
;
247 ctx
->payloads
[PIPE_SHADER_VERTEX
].postfix
.attribute_meta
= transfer
.gpu
;
250 /* Compute number of UBOs active (more specifically, compute the highest UBO
251 * number addressable -- if there are gaps, include them in the count anyway).
252 * We always include UBO #0 in the count, since we *need* uniforms enabled for
256 panfrost_ubo_count(struct panfrost_context
*ctx
, enum pipe_shader_type stage
)
258 unsigned mask
= ctx
->constant_buffer
[stage
].enabled_mask
| 1;
259 return 32 - __builtin_clz(mask
);
262 /* Go through dirty flags and actualise them in the cmdstream. */
265 panfrost_emit_for_draw(struct panfrost_context
*ctx
)
267 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
269 panfrost_batch_add_fbo_bos(batch
);
271 for (int i
= 0; i
<= PIPE_SHADER_FRAGMENT
; ++i
)
272 panfrost_vt_attach_framebuffer(ctx
, &ctx
->payloads
[i
]);
274 panfrost_emit_vertex_data(batch
);
276 /* Varyings emitted for -all- geometry */
277 unsigned total_count
= ctx
->padded_count
* ctx
->instance_count
;
278 panfrost_emit_varying_descriptor(ctx
, total_count
);
280 panfrost_batch_set_requirements(batch
);
282 panfrost_vt_update_rasterizer(ctx
, &ctx
->payloads
[PIPE_SHADER_FRAGMENT
]);
283 panfrost_vt_update_occlusion_query(ctx
, &ctx
->payloads
[PIPE_SHADER_FRAGMENT
]);
285 panfrost_emit_shader_meta(batch
, PIPE_SHADER_VERTEX
,
286 &ctx
->payloads
[PIPE_SHADER_VERTEX
]);
287 panfrost_emit_shader_meta(batch
, PIPE_SHADER_FRAGMENT
,
288 &ctx
->payloads
[PIPE_SHADER_FRAGMENT
]);
290 /* We stage to transient, so always dirty.. */
292 panfrost_stage_attributes(ctx
);
294 for (int i
= 0; i
<= PIPE_SHADER_FRAGMENT
; ++i
) {
295 panfrost_emit_sampler_descriptors(batch
, i
, &ctx
->payloads
[i
]);
296 panfrost_emit_texture_descriptors(batch
, i
, &ctx
->payloads
[i
]);
297 panfrost_emit_const_buf(batch
, i
, &ctx
->payloads
[i
]);
300 /* TODO: Upload the viewport somewhere more appropriate */
302 panfrost_emit_viewport(batch
, &ctx
->payloads
[PIPE_SHADER_FRAGMENT
]);
305 /* Corresponds to exactly one draw, but does not submit anything */
308 panfrost_queue_draw(struct panfrost_context
*ctx
)
310 /* Handle dirty flags now */
311 panfrost_emit_for_draw(ctx
);
313 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
315 panfrost_emit_vertex_tiler_jobs(batch
,
316 &ctx
->payloads
[PIPE_SHADER_VERTEX
],
317 &ctx
->payloads
[PIPE_SHADER_FRAGMENT
]);
318 panfrost_batch_adjust_stack_size(batch
);
321 /* The entire frame is in memory -- send it off to the kernel! */
325 struct pipe_context
*pipe
,
326 struct pipe_fence_handle
**fence
,
329 struct panfrost_context
*ctx
= pan_context(pipe
);
330 struct util_dynarray fences
;
332 /* We must collect the fences before the flush is done, otherwise we'll
333 * lose track of them.
336 util_dynarray_init(&fences
, NULL
);
337 hash_table_foreach(ctx
->batches
, hentry
) {
338 struct panfrost_batch
*batch
= hentry
->data
;
340 panfrost_batch_fence_reference(batch
->out_sync
);
341 util_dynarray_append(&fences
,
342 struct panfrost_batch_fence
*,
347 /* Submit all pending jobs */
348 panfrost_flush_all_batches(ctx
, false);
351 struct panfrost_fence
*f
= panfrost_fence_create(ctx
, &fences
);
352 pipe
->screen
->fence_reference(pipe
->screen
, fence
, NULL
);
353 *fence
= (struct pipe_fence_handle
*)f
;
355 util_dynarray_foreach(&fences
, struct panfrost_batch_fence
*, fence
)
356 panfrost_batch_fence_unreference(*fence
);
358 util_dynarray_fini(&fences
);
361 if (pan_debug
& PAN_DBG_TRACE
)
362 pandecode_next_frame();
365 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
368 g2m_draw_mode(enum pipe_prim_type mode
)
373 DEFINE_CASE(LINE_LOOP
);
374 DEFINE_CASE(LINE_STRIP
);
375 DEFINE_CASE(TRIANGLES
);
376 DEFINE_CASE(TRIANGLE_STRIP
);
377 DEFINE_CASE(TRIANGLE_FAN
);
379 DEFINE_CASE(QUAD_STRIP
);
380 DEFINE_CASE(POLYGON
);
383 unreachable("Invalid draw mode");
390 panfrost_translate_index_size(unsigned size
)
394 return MALI_DRAW_INDEXED_UINT8
;
397 return MALI_DRAW_INDEXED_UINT16
;
400 return MALI_DRAW_INDEXED_UINT32
;
403 unreachable("Invalid index size");
407 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
408 * good for the duration of the draw (transient), could last longer. Also get
409 * the bounds on the index buffer for the range accessed by the draw. We do
410 * these operations together because there are natural optimizations which
411 * require them to be together. */
414 panfrost_get_index_buffer_bounded(struct panfrost_context
*ctx
, const struct pipe_draw_info
*info
, unsigned *min_index
, unsigned *max_index
)
416 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) (info
->index
.resource
);
418 off_t offset
= info
->start
* info
->index_size
;
419 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
422 bool needs_indices
= true;
424 if (info
->max_index
!= ~0u) {
425 *min_index
= info
->min_index
;
426 *max_index
= info
->max_index
;
427 needs_indices
= false;
430 if (!info
->has_user_indices
) {
431 /* Only resources can be directly mapped */
432 panfrost_batch_add_bo(batch
, rsrc
->bo
,
433 PAN_BO_ACCESS_SHARED
|
435 PAN_BO_ACCESS_VERTEX_TILER
);
436 out
= rsrc
->bo
->gpu
+ offset
;
438 /* Check the cache */
439 needs_indices
= !panfrost_minmax_cache_get(rsrc
->index_cache
, info
->start
, info
->count
,
440 min_index
, max_index
);
442 /* Otherwise, we need to upload to transient memory */
443 const uint8_t *ibuf8
= (const uint8_t *) info
->index
.user
;
444 out
= panfrost_upload_transient(batch
, ibuf8
+ offset
, info
->count
* info
->index_size
);
449 u_vbuf_get_minmax_index(&ctx
->base
, info
, min_index
, max_index
);
451 if (!info
->has_user_indices
) {
452 panfrost_minmax_cache_add(rsrc
->index_cache
, info
->start
, info
->count
,
453 *min_index
, *max_index
);
462 panfrost_scissor_culls_everything(struct panfrost_context
*ctx
)
464 const struct pipe_scissor_state
*ss
= &ctx
->scissor
;
466 /* Check if we're scissoring at all */
468 if (!(ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
))
471 return (ss
->minx
== ss
->maxx
) || (ss
->miny
== ss
->maxy
);
474 /* Count generated primitives (when there is no geom/tess shaders) for
475 * transform feedback */
478 panfrost_statistics_record(
479 struct panfrost_context
*ctx
,
480 const struct pipe_draw_info
*info
)
482 if (!ctx
->active_queries
)
485 uint32_t prims
= u_prims_for_vertices(info
->mode
, info
->count
);
486 ctx
->prims_generated
+= prims
;
488 if (!ctx
->streamout
.num_targets
)
491 ctx
->tf_prims_generated
+= prims
;
496 struct pipe_context
*pipe
,
497 const struct pipe_draw_info
*info
)
499 struct panfrost_context
*ctx
= pan_context(pipe
);
501 /* First of all, check the scissor to see if anything is drawn at all.
502 * If it's not, we drop the draw (mostly a conformance issue;
503 * well-behaved apps shouldn't hit this) */
505 if (panfrost_scissor_culls_everything(ctx
))
508 int mode
= info
->mode
;
510 /* Fallback unsupported restart index */
511 unsigned primitive_index
= (1 << (info
->index_size
* 8)) - 1;
513 if (info
->primitive_restart
&& info
->index_size
514 && info
->restart_index
!= primitive_index
) {
515 util_draw_vbo_without_prim_restart(pipe
, info
);
519 /* Fallback for unsupported modes */
521 assert(ctx
->rasterizer
!= NULL
);
523 if (!(ctx
->draw_modes
& (1 << mode
))) {
524 if (mode
== PIPE_PRIM_QUADS
&& info
->count
== 4 && !ctx
->rasterizer
->base
.flatshade
) {
525 mode
= PIPE_PRIM_TRIANGLE_FAN
;
527 if (info
->count
< 4) {
528 /* Degenerate case? */
532 util_primconvert_save_rasterizer_state(ctx
->primconvert
, &ctx
->rasterizer
->base
);
533 util_primconvert_draw_vbo(ctx
->primconvert
, info
);
538 ctx
->payloads
[PIPE_SHADER_VERTEX
].offset_start
= info
->start
;
539 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].offset_start
= info
->start
;
541 /* Now that we have a guaranteed terminating path, find the job.
542 * Assignment commented out to prevent unused warning */
544 /* struct panfrost_batch *batch = */ panfrost_get_batch_for_fbo(ctx
);
546 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.draw_mode
= g2m_draw_mode(mode
);
548 /* Take into account a negative bias */
549 ctx
->vertex_count
= info
->count
+ abs(info
->index_bias
);
550 ctx
->instance_count
= info
->instance_count
;
551 ctx
->active_prim
= info
->mode
;
553 /* For non-indexed draws, they're the same */
554 unsigned vertex_count
= ctx
->vertex_count
;
556 unsigned draw_flags
= 0;
558 /* The draw flags interpret how primitive size is interpreted */
560 if (panfrost_writes_point_size(ctx
))
561 draw_flags
|= MALI_DRAW_VARYING_SIZE
;
563 if (info
->primitive_restart
)
564 draw_flags
|= MALI_DRAW_PRIMITIVE_RESTART_FIXED_INDEX
;
566 /* These doesn't make much sense */
568 draw_flags
|= 0x3000;
570 if (ctx
->rasterizer
&& ctx
->rasterizer
->base
.flatshade_first
)
571 draw_flags
|= MALI_DRAW_FLATSHADE_FIRST
;
573 panfrost_statistics_record(ctx
, info
);
575 if (info
->index_size
) {
576 unsigned min_index
= 0, max_index
= 0;
577 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.indices
=
578 panfrost_get_index_buffer_bounded(ctx
, info
, &min_index
, &max_index
);
580 /* Use the corresponding values */
581 vertex_count
= max_index
- min_index
+ 1;
582 ctx
->payloads
[PIPE_SHADER_VERTEX
].offset_start
= min_index
+ info
->index_bias
;
583 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].offset_start
= min_index
+ info
->index_bias
;
585 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.offset_bias_correction
= -min_index
;
586 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.index_count
= MALI_POSITIVE(info
->count
);
588 draw_flags
|= panfrost_translate_index_size(info
->index_size
);
590 /* Index count == vertex count, if no indexing is applied, as
591 * if it is internally indexed in the expected order */
593 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.offset_bias_correction
= 0;
594 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.index_count
= MALI_POSITIVE(ctx
->vertex_count
);
596 /* Reverse index state */
597 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.indices
= (mali_ptr
) 0;
600 /* Dispatch "compute jobs" for the vertex/tiler pair as (1,
601 * vertex_count, 1) */
603 panfrost_pack_work_groups_fused(
604 &ctx
->payloads
[PIPE_SHADER_VERTEX
].prefix
,
605 &ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
,
606 1, vertex_count
, info
->instance_count
,
609 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].prefix
.unknown_draw
= draw_flags
;
611 /* Encode the padded vertex count */
613 if (info
->instance_count
> 1) {
614 ctx
->padded_count
= panfrost_padded_vertex_count(vertex_count
);
616 unsigned shift
= __builtin_ctz(ctx
->padded_count
);
617 unsigned k
= ctx
->padded_count
>> (shift
+ 1);
619 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_shift
= shift
;
620 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_shift
= shift
;
622 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_odd
= k
;
623 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_odd
= k
;
625 ctx
->padded_count
= vertex_count
;
627 /* Reset instancing state */
628 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_shift
= 0;
629 ctx
->payloads
[PIPE_SHADER_VERTEX
].instance_odd
= 0;
630 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_shift
= 0;
631 ctx
->payloads
[PIPE_SHADER_FRAGMENT
].instance_odd
= 0;
634 /* Fire off the draw itself */
635 panfrost_queue_draw(ctx
);
637 /* Increment transform feedback offsets */
639 for (unsigned i
= 0; i
< ctx
->streamout
.num_targets
; ++i
) {
640 unsigned output_count
= u_stream_outputs_for_vertices(
641 ctx
->active_prim
, ctx
->vertex_count
);
643 ctx
->streamout
.offsets
[i
] += output_count
;
650 panfrost_generic_cso_delete(struct pipe_context
*pctx
, void *hwcso
)
656 panfrost_create_rasterizer_state(
657 struct pipe_context
*pctx
,
658 const struct pipe_rasterizer_state
*cso
)
660 struct panfrost_rasterizer
*so
= CALLOC_STRUCT(panfrost_rasterizer
);
668 panfrost_bind_rasterizer_state(
669 struct pipe_context
*pctx
,
672 struct panfrost_context
*ctx
= pan_context(pctx
);
674 ctx
->rasterizer
= hwcso
;
679 /* Gauranteed with the core GL call, so don't expose ARB_polygon_offset */
680 assert(ctx
->rasterizer
->base
.offset_clamp
== 0.0);
682 /* Point sprites are emulated */
684 struct panfrost_shader_state
*variant
= panfrost_get_shader_state(ctx
, PIPE_SHADER_FRAGMENT
);
686 if (ctx
->rasterizer
->base
.sprite_coord_enable
|| (variant
&& variant
->point_sprite_mask
))
687 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->shader
[PIPE_SHADER_FRAGMENT
]);
691 panfrost_create_vertex_elements_state(
692 struct pipe_context
*pctx
,
693 unsigned num_elements
,
694 const struct pipe_vertex_element
*elements
)
696 struct panfrost_vertex_state
*so
= CALLOC_STRUCT(panfrost_vertex_state
);
698 so
->num_elements
= num_elements
;
699 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
701 for (int i
= 0; i
< num_elements
; ++i
) {
704 enum pipe_format fmt
= elements
[i
].src_format
;
705 const struct util_format_description
*desc
= util_format_description(fmt
);
706 so
->hw
[i
].unknown1
= 0x2;
707 so
->hw
[i
].swizzle
= panfrost_get_default_swizzle(desc
->nr_channels
);
709 so
->hw
[i
].format
= panfrost_find_format(desc
);
712 /* Let's also prepare vertex builtins */
713 so
->hw
[PAN_VERTEX_ID
].format
= MALI_R32UI
;
714 so
->hw
[PAN_VERTEX_ID
].swizzle
= panfrost_get_default_swizzle(1);
715 so
->hw
[PAN_INSTANCE_ID
].format
= MALI_R32UI
;
716 so
->hw
[PAN_INSTANCE_ID
].swizzle
= panfrost_get_default_swizzle(1);
722 panfrost_bind_vertex_elements_state(
723 struct pipe_context
*pctx
,
726 struct panfrost_context
*ctx
= pan_context(pctx
);
731 panfrost_create_shader_state(
732 struct pipe_context
*pctx
,
733 const struct pipe_shader_state
*cso
,
734 enum pipe_shader_type stage
)
736 struct panfrost_shader_variants
*so
= CALLOC_STRUCT(panfrost_shader_variants
);
739 /* Token deep copy to prevent memory corruption */
741 if (cso
->type
== PIPE_SHADER_IR_TGSI
)
742 so
->base
.tokens
= tgsi_dup_tokens(so
->base
.tokens
);
744 /* Precompile for shader-db if we need to */
745 if (unlikely((pan_debug
& PAN_DBG_PRECOMPILE
) && cso
->type
== PIPE_SHADER_IR_NIR
)) {
746 struct panfrost_context
*ctx
= pan_context(pctx
);
748 struct panfrost_shader_state state
;
749 uint64_t outputs_written
;
751 panfrost_shader_compile(ctx
, PIPE_SHADER_IR_NIR
,
753 tgsi_processor_to_shader_stage(stage
),
754 &state
, &outputs_written
);
761 panfrost_delete_shader_state(
762 struct pipe_context
*pctx
,
765 struct panfrost_shader_variants
*cso
= (struct panfrost_shader_variants
*) so
;
767 if (cso
->base
.type
== PIPE_SHADER_IR_TGSI
) {
768 DBG("Deleting TGSI shader leaks duplicated tokens\n");
771 for (unsigned i
= 0; i
< cso
->variant_count
; ++i
) {
772 struct panfrost_shader_state
*shader_state
= &cso
->variants
[i
];
773 panfrost_bo_unreference(shader_state
->bo
);
774 shader_state
->bo
= NULL
;
782 panfrost_create_sampler_state(
783 struct pipe_context
*pctx
,
784 const struct pipe_sampler_state
*cso
)
786 struct panfrost_sampler_state
*so
= CALLOC_STRUCT(panfrost_sampler_state
);
789 panfrost_sampler_desc_init(cso
, &so
->hw
);
795 panfrost_bind_sampler_states(
796 struct pipe_context
*pctx
,
797 enum pipe_shader_type shader
,
798 unsigned start_slot
, unsigned num_sampler
,
801 assert(start_slot
== 0);
803 struct panfrost_context
*ctx
= pan_context(pctx
);
805 /* XXX: Should upload, not just copy? */
806 ctx
->sampler_count
[shader
] = num_sampler
;
807 memcpy(ctx
->samplers
[shader
], sampler
, num_sampler
* sizeof (void *));
811 panfrost_variant_matches(
812 struct panfrost_context
*ctx
,
813 struct panfrost_shader_state
*variant
,
814 enum pipe_shader_type type
)
816 struct pipe_rasterizer_state
*rasterizer
= &ctx
->rasterizer
->base
;
817 struct pipe_alpha_state
*alpha
= &ctx
->depth_stencil
->alpha
;
819 bool is_fragment
= (type
== PIPE_SHADER_FRAGMENT
);
821 if (is_fragment
&& (alpha
->enabled
|| variant
->alpha_state
.enabled
)) {
822 /* Make sure enable state is at least the same */
823 if (alpha
->enabled
!= variant
->alpha_state
.enabled
) {
827 /* Check that the contents of the test are the same */
828 bool same_func
= alpha
->func
== variant
->alpha_state
.func
;
829 bool same_ref
= alpha
->ref_value
== variant
->alpha_state
.ref_value
;
831 if (!(same_func
&& same_ref
)) {
836 if (is_fragment
&& rasterizer
&& (rasterizer
->sprite_coord_enable
|
837 variant
->point_sprite_mask
)) {
838 /* Ensure the same varyings are turned to point sprites */
839 if (rasterizer
->sprite_coord_enable
!= variant
->point_sprite_mask
)
842 /* Ensure the orientation is correct */
844 rasterizer
->sprite_coord_mode
==
845 PIPE_SPRITE_COORD_UPPER_LEFT
;
847 if (variant
->point_sprite_upper_left
!= upper_left
)
851 /* Otherwise, we're good to go */
856 * Fix an uncompiled shader's stream output info, and produce a bitmask
857 * of which VARYING_SLOT_* are captured for stream output.
859 * Core Gallium stores output->register_index as a "slot" number, where
860 * slots are assigned consecutively to all outputs in info->outputs_written.
861 * This naive packing of outputs doesn't work for us - we too have slots,
862 * but the layout is defined by the VUE map, which we won't have until we
863 * compile a specific shader variant. So, we remap these and simply store
864 * VARYING_SLOT_* in our copy's output->register_index fields.
866 * We then produce a bitmask of outputs which are used for SO.
868 * Implementation from iris.
872 update_so_info(struct pipe_stream_output_info
*so_info
,
873 uint64_t outputs_written
)
875 uint64_t so_outputs
= 0;
876 uint8_t reverse_map
[64] = {0};
879 while (outputs_written
)
880 reverse_map
[slot
++] = u_bit_scan64(&outputs_written
);
882 for (unsigned i
= 0; i
< so_info
->num_outputs
; i
++) {
883 struct pipe_stream_output
*output
= &so_info
->output
[i
];
885 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
886 output
->register_index
= reverse_map
[output
->register_index
];
888 so_outputs
|= 1ull << output
->register_index
;
895 panfrost_bind_shader_state(
896 struct pipe_context
*pctx
,
898 enum pipe_shader_type type
)
900 struct panfrost_context
*ctx
= pan_context(pctx
);
901 ctx
->shader
[type
] = hwcso
;
905 /* Match the appropriate variant */
908 struct panfrost_shader_variants
*variants
= (struct panfrost_shader_variants
*) hwcso
;
910 for (unsigned i
= 0; i
< variants
->variant_count
; ++i
) {
911 if (panfrost_variant_matches(ctx
, &variants
->variants
[i
], type
)) {
918 /* No variant matched, so create a new one */
919 variant
= variants
->variant_count
++;
921 if (variants
->variant_count
> variants
->variant_space
) {
922 unsigned old_space
= variants
->variant_space
;
924 variants
->variant_space
*= 2;
925 if (variants
->variant_space
== 0)
926 variants
->variant_space
= 1;
928 /* Arbitrary limit to stop runaway programs from
929 * creating an unbounded number of shader variants. */
930 assert(variants
->variant_space
< 1024);
932 unsigned msize
= sizeof(struct panfrost_shader_state
);
933 variants
->variants
= realloc(variants
->variants
,
934 variants
->variant_space
* msize
);
936 memset(&variants
->variants
[old_space
], 0,
937 (variants
->variant_space
- old_space
) * msize
);
940 struct panfrost_shader_state
*v
=
941 &variants
->variants
[variant
];
943 if (type
== PIPE_SHADER_FRAGMENT
) {
944 v
->alpha_state
= ctx
->depth_stencil
->alpha
;
946 if (ctx
->rasterizer
) {
947 v
->point_sprite_mask
= ctx
->rasterizer
->base
.sprite_coord_enable
;
948 v
->point_sprite_upper_left
=
949 ctx
->rasterizer
->base
.sprite_coord_mode
==
950 PIPE_SPRITE_COORD_UPPER_LEFT
;
955 /* Select this variant */
956 variants
->active_variant
= variant
;
958 struct panfrost_shader_state
*shader_state
= &variants
->variants
[variant
];
959 assert(panfrost_variant_matches(ctx
, shader_state
, type
));
961 /* We finally have a variant, so compile it */
963 if (!shader_state
->compiled
) {
964 uint64_t outputs_written
= 0;
966 panfrost_shader_compile(ctx
, variants
->base
.type
,
967 variants
->base
.type
== PIPE_SHADER_IR_NIR
?
968 variants
->base
.ir
.nir
:
969 variants
->base
.tokens
,
970 tgsi_processor_to_shader_stage(type
),
974 shader_state
->compiled
= true;
976 /* Fixup the stream out information, since what Gallium returns
977 * normally is mildly insane */
979 shader_state
->stream_output
= variants
->base
.stream_output
;
980 shader_state
->so_mask
=
981 update_so_info(&shader_state
->stream_output
, outputs_written
);
986 panfrost_create_vs_state(struct pipe_context
*pctx
, const struct pipe_shader_state
*hwcso
)
988 return panfrost_create_shader_state(pctx
, hwcso
, PIPE_SHADER_VERTEX
);
992 panfrost_create_fs_state(struct pipe_context
*pctx
, const struct pipe_shader_state
*hwcso
)
994 return panfrost_create_shader_state(pctx
, hwcso
, PIPE_SHADER_FRAGMENT
);
998 panfrost_bind_vs_state(struct pipe_context
*pctx
, void *hwcso
)
1000 panfrost_bind_shader_state(pctx
, hwcso
, PIPE_SHADER_VERTEX
);
1004 panfrost_bind_fs_state(struct pipe_context
*pctx
, void *hwcso
)
1006 panfrost_bind_shader_state(pctx
, hwcso
, PIPE_SHADER_FRAGMENT
);
1010 panfrost_set_vertex_buffers(
1011 struct pipe_context
*pctx
,
1012 unsigned start_slot
,
1013 unsigned num_buffers
,
1014 const struct pipe_vertex_buffer
*buffers
)
1016 struct panfrost_context
*ctx
= pan_context(pctx
);
1018 util_set_vertex_buffers_mask(ctx
->vertex_buffers
, &ctx
->vb_mask
, buffers
, start_slot
, num_buffers
);
1022 panfrost_set_constant_buffer(
1023 struct pipe_context
*pctx
,
1024 enum pipe_shader_type shader
, uint index
,
1025 const struct pipe_constant_buffer
*buf
)
1027 struct panfrost_context
*ctx
= pan_context(pctx
);
1028 struct panfrost_constant_buffer
*pbuf
= &ctx
->constant_buffer
[shader
];
1030 util_copy_constant_buffer(&pbuf
->cb
[index
], buf
);
1032 unsigned mask
= (1 << index
);
1034 if (unlikely(!buf
)) {
1035 pbuf
->enabled_mask
&= ~mask
;
1036 pbuf
->dirty_mask
&= ~mask
;
1040 pbuf
->enabled_mask
|= mask
;
1041 pbuf
->dirty_mask
|= mask
;
1045 panfrost_set_stencil_ref(
1046 struct pipe_context
*pctx
,
1047 const struct pipe_stencil_ref
*ref
)
1049 struct panfrost_context
*ctx
= pan_context(pctx
);
1050 ctx
->stencil_ref
= *ref
;
1053 static enum mali_texture_type
1054 panfrost_translate_texture_type(enum pipe_texture_target t
) {
1058 case PIPE_TEXTURE_1D
:
1059 case PIPE_TEXTURE_1D_ARRAY
:
1062 case PIPE_TEXTURE_2D
:
1063 case PIPE_TEXTURE_2D_ARRAY
:
1064 case PIPE_TEXTURE_RECT
:
1067 case PIPE_TEXTURE_3D
:
1070 case PIPE_TEXTURE_CUBE
:
1071 case PIPE_TEXTURE_CUBE_ARRAY
:
1072 return MALI_TEX_CUBE
;
1075 unreachable("Unknown target");
1079 static struct pipe_sampler_view
*
1080 panfrost_create_sampler_view(
1081 struct pipe_context
*pctx
,
1082 struct pipe_resource
*texture
,
1083 const struct pipe_sampler_view
*template)
1085 struct panfrost_screen
*screen
= pan_screen(pctx
->screen
);
1086 struct panfrost_sampler_view
*so
= rzalloc(pctx
, struct panfrost_sampler_view
);
1088 pipe_reference(NULL
, &texture
->reference
);
1090 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) texture
;
1093 so
->base
= *template;
1094 so
->base
.texture
= texture
;
1095 so
->base
.reference
.count
= 1;
1096 so
->base
.context
= pctx
;
1098 unsigned char user_swizzle
[4] = {
1099 template->swizzle_r
,
1100 template->swizzle_g
,
1101 template->swizzle_b
,
1105 /* In the hardware, array_size refers specifically to array textures,
1106 * whereas in Gallium, it also covers cubemaps */
1108 unsigned array_size
= texture
->array_size
;
1110 if (template->target
== PIPE_TEXTURE_CUBE
) {
1111 /* TODO: Cubemap arrays */
1112 assert(array_size
== 6);
1116 enum mali_texture_type type
=
1117 panfrost_translate_texture_type(template->target
);
1119 unsigned size
= panfrost_estimate_texture_size(
1120 template->u
.tex
.first_level
,
1121 template->u
.tex
.last_level
,
1122 template->u
.tex
.first_layer
,
1123 template->u
.tex
.last_layer
,
1124 type
, prsrc
->layout
);
1126 so
->bo
= panfrost_bo_create(screen
, size
, 0);
1128 panfrost_new_texture(
1130 texture
->width0
, texture
->height0
,
1131 texture
->depth0
, array_size
,
1133 type
, prsrc
->layout
,
1134 template->u
.tex
.first_level
,
1135 template->u
.tex
.last_level
,
1136 template->u
.tex
.first_layer
,
1137 template->u
.tex
.last_layer
,
1138 prsrc
->cubemap_stride
,
1139 panfrost_translate_swizzle_4(user_swizzle
),
1143 return (struct pipe_sampler_view
*) so
;
1147 panfrost_set_sampler_views(
1148 struct pipe_context
*pctx
,
1149 enum pipe_shader_type shader
,
1150 unsigned start_slot
, unsigned num_views
,
1151 struct pipe_sampler_view
**views
)
1153 struct panfrost_context
*ctx
= pan_context(pctx
);
1154 unsigned new_nr
= 0;
1157 assert(start_slot
== 0);
1159 for (i
= 0; i
< num_views
; ++i
) {
1162 pipe_sampler_view_reference((struct pipe_sampler_view
**)&ctx
->sampler_views
[shader
][i
],
1166 for (; i
< ctx
->sampler_view_count
[shader
]; i
++) {
1167 pipe_sampler_view_reference((struct pipe_sampler_view
**)&ctx
->sampler_views
[shader
][i
],
1170 ctx
->sampler_view_count
[shader
] = new_nr
;
1174 panfrost_sampler_view_destroy(
1175 struct pipe_context
*pctx
,
1176 struct pipe_sampler_view
*pview
)
1178 struct panfrost_sampler_view
*view
= (struct panfrost_sampler_view
*) pview
;
1180 pipe_resource_reference(&pview
->texture
, NULL
);
1181 panfrost_bo_unreference(view
->bo
);
1186 panfrost_set_shader_buffers(
1187 struct pipe_context
*pctx
,
1188 enum pipe_shader_type shader
,
1189 unsigned start
, unsigned count
,
1190 const struct pipe_shader_buffer
*buffers
,
1191 unsigned writable_bitmask
)
1193 struct panfrost_context
*ctx
= pan_context(pctx
);
1195 util_set_shader_buffers_mask(ctx
->ssbo
[shader
], &ctx
->ssbo_mask
[shader
],
1196 buffers
, start
, count
);
1199 /* Hints that a framebuffer should use AFBC where possible */
1203 struct panfrost_screen
*screen
,
1204 const struct pipe_framebuffer_state
*fb
)
1206 /* AFBC implemenation incomplete; hide it */
1207 if (!(pan_debug
& PAN_DBG_AFBC
)) return;
1209 /* Hint AFBC to the resources bound to each color buffer */
1211 for (unsigned i
= 0; i
< fb
->nr_cbufs
; ++i
) {
1212 struct pipe_surface
*surf
= fb
->cbufs
[i
];
1213 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
1214 panfrost_resource_hint_layout(screen
, rsrc
, MALI_TEXTURE_AFBC
, 1);
1217 /* Also hint it to the depth buffer */
1220 struct panfrost_resource
*rsrc
= pan_resource(fb
->zsbuf
->texture
);
1221 panfrost_resource_hint_layout(screen
, rsrc
, MALI_TEXTURE_AFBC
, 1);
1226 panfrost_set_framebuffer_state(struct pipe_context
*pctx
,
1227 const struct pipe_framebuffer_state
*fb
)
1229 struct panfrost_context
*ctx
= pan_context(pctx
);
1231 panfrost_hint_afbc(pan_screen(pctx
->screen
), fb
);
1232 util_copy_framebuffer_state(&ctx
->pipe_framebuffer
, fb
);
1234 panfrost_invalidate_frame(ctx
);
1238 panfrost_create_depth_stencil_state(struct pipe_context
*pipe
,
1239 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
1241 return mem_dup(depth_stencil
, sizeof(*depth_stencil
));
1245 panfrost_bind_depth_stencil_state(struct pipe_context
*pipe
,
1248 struct panfrost_context
*ctx
= pan_context(pipe
);
1249 struct pipe_depth_stencil_alpha_state
*depth_stencil
= cso
;
1250 ctx
->depth_stencil
= depth_stencil
;
1255 /* Alpha does not exist in the hardware (it's not in ES3), so it's
1256 * emulated in the fragment shader */
1258 if (depth_stencil
->alpha
.enabled
) {
1259 /* We need to trigger a new shader (maybe) */
1260 ctx
->base
.bind_fs_state(&ctx
->base
, ctx
->shader
[PIPE_SHADER_FRAGMENT
]);
1263 /* Bounds test not implemented */
1264 assert(!depth_stencil
->depth
.bounds_test
);
1268 panfrost_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
1274 panfrost_set_sample_mask(struct pipe_context
*pipe
,
1275 unsigned sample_mask
)
1280 panfrost_set_clip_state(struct pipe_context
*pipe
,
1281 const struct pipe_clip_state
*clip
)
1283 //struct panfrost_context *panfrost = pan_context(pipe);
1287 panfrost_set_viewport_states(struct pipe_context
*pipe
,
1288 unsigned start_slot
,
1289 unsigned num_viewports
,
1290 const struct pipe_viewport_state
*viewports
)
1292 struct panfrost_context
*ctx
= pan_context(pipe
);
1294 assert(start_slot
== 0);
1295 assert(num_viewports
== 1);
1297 ctx
->pipe_viewport
= *viewports
;
1301 panfrost_set_scissor_states(struct pipe_context
*pipe
,
1302 unsigned start_slot
,
1303 unsigned num_scissors
,
1304 const struct pipe_scissor_state
*scissors
)
1306 struct panfrost_context
*ctx
= pan_context(pipe
);
1308 assert(start_slot
== 0);
1309 assert(num_scissors
== 1);
1311 ctx
->scissor
= *scissors
;
1315 panfrost_set_polygon_stipple(struct pipe_context
*pipe
,
1316 const struct pipe_poly_stipple
*stipple
)
1318 //struct panfrost_context *panfrost = pan_context(pipe);
1322 panfrost_set_active_query_state(struct pipe_context
*pipe
,
1325 struct panfrost_context
*ctx
= pan_context(pipe
);
1326 ctx
->active_queries
= enable
;
1330 panfrost_destroy(struct pipe_context
*pipe
)
1332 struct panfrost_context
*panfrost
= pan_context(pipe
);
1334 if (panfrost
->blitter
)
1335 util_blitter_destroy(panfrost
->blitter
);
1337 if (panfrost
->blitter_wallpaper
)
1338 util_blitter_destroy(panfrost
->blitter_wallpaper
);
1340 util_unreference_framebuffer_state(&panfrost
->pipe_framebuffer
);
1341 u_upload_destroy(pipe
->stream_uploader
);
1346 static struct pipe_query
*
1347 panfrost_create_query(struct pipe_context
*pipe
,
1351 struct panfrost_query
*q
= rzalloc(pipe
, struct panfrost_query
);
1356 return (struct pipe_query
*) q
;
1360 panfrost_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
1362 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
1365 panfrost_bo_unreference(query
->bo
);
1373 panfrost_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
1375 struct panfrost_context
*ctx
= pan_context(pipe
);
1376 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
1378 switch (query
->type
) {
1379 case PIPE_QUERY_OCCLUSION_COUNTER
:
1380 case PIPE_QUERY_OCCLUSION_PREDICATE
:
1381 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
1382 /* Allocate a bo for the query results to be stored */
1384 query
->bo
= panfrost_bo_create(
1385 pan_screen(ctx
->base
.screen
),
1386 sizeof(unsigned), 0);
1389 unsigned *result
= (unsigned *)query
->bo
->cpu
;
1390 *result
= 0; /* Default to 0 if nothing at all drawn. */
1391 ctx
->occlusion_query
= query
;
1394 /* Geometry statistics are computed in the driver. XXX: geom/tess
1397 case PIPE_QUERY_PRIMITIVES_GENERATED
:
1398 query
->start
= ctx
->prims_generated
;
1400 case PIPE_QUERY_PRIMITIVES_EMITTED
:
1401 query
->start
= ctx
->tf_prims_generated
;
1405 DBG("Skipping query %u\n", query
->type
);
1413 panfrost_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
1415 struct panfrost_context
*ctx
= pan_context(pipe
);
1416 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
1418 switch (query
->type
) {
1419 case PIPE_QUERY_OCCLUSION_COUNTER
:
1420 case PIPE_QUERY_OCCLUSION_PREDICATE
:
1421 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
1422 ctx
->occlusion_query
= NULL
;
1424 case PIPE_QUERY_PRIMITIVES_GENERATED
:
1425 query
->end
= ctx
->prims_generated
;
1427 case PIPE_QUERY_PRIMITIVES_EMITTED
:
1428 query
->end
= ctx
->tf_prims_generated
;
1436 panfrost_get_query_result(struct pipe_context
*pipe
,
1437 struct pipe_query
*q
,
1439 union pipe_query_result
*vresult
)
1441 struct panfrost_query
*query
= (struct panfrost_query
*) q
;
1442 struct panfrost_context
*ctx
= pan_context(pipe
);
1445 switch (query
->type
) {
1446 case PIPE_QUERY_OCCLUSION_COUNTER
:
1447 case PIPE_QUERY_OCCLUSION_PREDICATE
:
1448 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
1450 panfrost_flush_all_batches(ctx
, true);
1452 /* Read back the query results */
1453 unsigned *result
= (unsigned *) query
->bo
->cpu
;
1454 unsigned passed
= *result
;
1456 if (query
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
1457 vresult
->u64
= passed
;
1459 vresult
->b
= !!passed
;
1464 case PIPE_QUERY_PRIMITIVES_GENERATED
:
1465 case PIPE_QUERY_PRIMITIVES_EMITTED
:
1466 panfrost_flush_all_batches(ctx
, true);
1467 vresult
->u64
= query
->end
- query
->start
;
1471 DBG("Skipped query get %u\n", query
->type
);
1478 static struct pipe_stream_output_target
*
1479 panfrost_create_stream_output_target(struct pipe_context
*pctx
,
1480 struct pipe_resource
*prsc
,
1481 unsigned buffer_offset
,
1482 unsigned buffer_size
)
1484 struct pipe_stream_output_target
*target
;
1486 target
= rzalloc(pctx
, struct pipe_stream_output_target
);
1491 pipe_reference_init(&target
->reference
, 1);
1492 pipe_resource_reference(&target
->buffer
, prsc
);
1494 target
->context
= pctx
;
1495 target
->buffer_offset
= buffer_offset
;
1496 target
->buffer_size
= buffer_size
;
1502 panfrost_stream_output_target_destroy(struct pipe_context
*pctx
,
1503 struct pipe_stream_output_target
*target
)
1505 pipe_resource_reference(&target
->buffer
, NULL
);
1506 ralloc_free(target
);
1510 panfrost_set_stream_output_targets(struct pipe_context
*pctx
,
1511 unsigned num_targets
,
1512 struct pipe_stream_output_target
**targets
,
1513 const unsigned *offsets
)
1515 struct panfrost_context
*ctx
= pan_context(pctx
);
1516 struct panfrost_streamout
*so
= &ctx
->streamout
;
1518 assert(num_targets
<= ARRAY_SIZE(so
->targets
));
1520 for (unsigned i
= 0; i
< num_targets
; i
++) {
1521 if (offsets
[i
] != -1)
1522 so
->offsets
[i
] = offsets
[i
];
1524 pipe_so_target_reference(&so
->targets
[i
], targets
[i
]);
1527 for (unsigned i
= 0; i
< so
->num_targets
; i
++)
1528 pipe_so_target_reference(&so
->targets
[i
], NULL
);
1530 so
->num_targets
= num_targets
;
1533 struct pipe_context
*
1534 panfrost_create_context(struct pipe_screen
*screen
, void *priv
, unsigned flags
)
1536 struct panfrost_context
*ctx
= rzalloc(screen
, struct panfrost_context
);
1537 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
1539 gallium
->screen
= screen
;
1541 gallium
->destroy
= panfrost_destroy
;
1543 gallium
->set_framebuffer_state
= panfrost_set_framebuffer_state
;
1545 gallium
->flush
= panfrost_flush
;
1546 gallium
->clear
= panfrost_clear
;
1547 gallium
->draw_vbo
= panfrost_draw_vbo
;
1549 gallium
->set_vertex_buffers
= panfrost_set_vertex_buffers
;
1550 gallium
->set_constant_buffer
= panfrost_set_constant_buffer
;
1551 gallium
->set_shader_buffers
= panfrost_set_shader_buffers
;
1553 gallium
->set_stencil_ref
= panfrost_set_stencil_ref
;
1555 gallium
->create_sampler_view
= panfrost_create_sampler_view
;
1556 gallium
->set_sampler_views
= panfrost_set_sampler_views
;
1557 gallium
->sampler_view_destroy
= panfrost_sampler_view_destroy
;
1559 gallium
->create_rasterizer_state
= panfrost_create_rasterizer_state
;
1560 gallium
->bind_rasterizer_state
= panfrost_bind_rasterizer_state
;
1561 gallium
->delete_rasterizer_state
= panfrost_generic_cso_delete
;
1563 gallium
->create_vertex_elements_state
= panfrost_create_vertex_elements_state
;
1564 gallium
->bind_vertex_elements_state
= panfrost_bind_vertex_elements_state
;
1565 gallium
->delete_vertex_elements_state
= panfrost_generic_cso_delete
;
1567 gallium
->create_fs_state
= panfrost_create_fs_state
;
1568 gallium
->delete_fs_state
= panfrost_delete_shader_state
;
1569 gallium
->bind_fs_state
= panfrost_bind_fs_state
;
1571 gallium
->create_vs_state
= panfrost_create_vs_state
;
1572 gallium
->delete_vs_state
= panfrost_delete_shader_state
;
1573 gallium
->bind_vs_state
= panfrost_bind_vs_state
;
1575 gallium
->create_sampler_state
= panfrost_create_sampler_state
;
1576 gallium
->delete_sampler_state
= panfrost_generic_cso_delete
;
1577 gallium
->bind_sampler_states
= panfrost_bind_sampler_states
;
1579 gallium
->create_depth_stencil_alpha_state
= panfrost_create_depth_stencil_state
;
1580 gallium
->bind_depth_stencil_alpha_state
= panfrost_bind_depth_stencil_state
;
1581 gallium
->delete_depth_stencil_alpha_state
= panfrost_delete_depth_stencil_state
;
1583 gallium
->set_sample_mask
= panfrost_set_sample_mask
;
1585 gallium
->set_clip_state
= panfrost_set_clip_state
;
1586 gallium
->set_viewport_states
= panfrost_set_viewport_states
;
1587 gallium
->set_scissor_states
= panfrost_set_scissor_states
;
1588 gallium
->set_polygon_stipple
= panfrost_set_polygon_stipple
;
1589 gallium
->set_active_query_state
= panfrost_set_active_query_state
;
1591 gallium
->create_query
= panfrost_create_query
;
1592 gallium
->destroy_query
= panfrost_destroy_query
;
1593 gallium
->begin_query
= panfrost_begin_query
;
1594 gallium
->end_query
= panfrost_end_query
;
1595 gallium
->get_query_result
= panfrost_get_query_result
;
1597 gallium
->create_stream_output_target
= panfrost_create_stream_output_target
;
1598 gallium
->stream_output_target_destroy
= panfrost_stream_output_target_destroy
;
1599 gallium
->set_stream_output_targets
= panfrost_set_stream_output_targets
;
1601 panfrost_resource_context_init(gallium
);
1602 panfrost_blend_context_init(gallium
);
1603 panfrost_compute_context_init(gallium
);
1606 gallium
->stream_uploader
= u_upload_create_default(gallium
);
1607 gallium
->const_uploader
= gallium
->stream_uploader
;
1608 assert(gallium
->stream_uploader
);
1610 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
1611 ctx
->draw_modes
= (1 << (PIPE_PRIM_POLYGON
+ 1)) - 1;
1613 ctx
->primconvert
= util_primconvert_create(gallium
, ctx
->draw_modes
);
1615 ctx
->blitter
= util_blitter_create(gallium
);
1616 ctx
->blitter_wallpaper
= util_blitter_create(gallium
);
1618 assert(ctx
->blitter
);
1619 assert(ctx
->blitter_wallpaper
);
1621 /* Prepare for render! */
1623 panfrost_batch_init(ctx
);
1624 panfrost_emit_vertex_payload(ctx
);
1625 panfrost_invalidate_frame(ctx
);