2 * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
3 * Copyright (c) 2017-2019 Lima Project
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
26 #include "util/format/u_format.h"
27 #include "util/u_debug.h"
28 #include "util/u_half.h"
29 #include "util/u_helpers.h"
30 #include "util/u_inlines.h"
31 #include "util/u_pack_color.h"
32 #include "util/u_split_draw.h"
33 #include "util/u_upload_mgr.h"
34 #include "util/u_prim.h"
35 #include "util/u_vbuf.h"
36 #include "util/hash_table.h"
38 #include "lima_context.h"
39 #include "lima_screen.h"
40 #include "lima_resource.h"
41 #include "lima_program.h"
44 #include "lima_texture.h"
45 #include "lima_util.h"
48 #include "pan_minmax_cache.h"
50 #include <drm-uapi/lima_drm.h>
53 lima_clip_scissor_to_viewport(struct lima_context
*ctx
)
55 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
56 struct pipe_scissor_state
*cscissor
= &ctx
->clipped_scissor
;
57 int viewport_left
, viewport_right
, viewport_bottom
, viewport_top
;
59 if (ctx
->rasterizer
&& ctx
->rasterizer
->base
.scissor
) {
60 struct pipe_scissor_state
*scissor
= &ctx
->scissor
;
61 cscissor
->minx
= scissor
->minx
;
62 cscissor
->maxx
= scissor
->maxx
;
63 cscissor
->miny
= scissor
->miny
;
64 cscissor
->maxy
= scissor
->maxy
;
67 cscissor
->maxx
= fb
->base
.width
;
69 cscissor
->maxy
= fb
->base
.height
;
72 viewport_left
= MAX2(ctx
->viewport
.left
, 0);
73 cscissor
->minx
= MAX2(cscissor
->minx
, viewport_left
);
74 viewport_right
= MIN2(ctx
->viewport
.right
, fb
->base
.width
);
75 cscissor
->maxx
= MIN2(cscissor
->maxx
, viewport_right
);
76 if (cscissor
->minx
> cscissor
->maxx
)
77 cscissor
->minx
= cscissor
->maxx
;
79 viewport_bottom
= MAX2(ctx
->viewport
.bottom
, 0);
80 cscissor
->miny
= MAX2(cscissor
->miny
, viewport_bottom
);
81 viewport_top
= MIN2(ctx
->viewport
.top
, fb
->base
.height
);
82 cscissor
->maxy
= MIN2(cscissor
->maxy
, viewport_top
);
83 if (cscissor
->miny
> cscissor
->maxy
)
84 cscissor
->miny
= cscissor
->maxy
;
88 lima_is_scissor_zero(struct lima_context
*ctx
)
90 struct pipe_scissor_state
*cscissor
= &ctx
->clipped_scissor
;
92 return cscissor
->minx
== cscissor
->maxx
|| cscissor
->miny
== cscissor
->maxy
;
96 lima_update_job_wb(struct lima_context
*ctx
, unsigned buffers
)
98 struct lima_job
*job
= lima_job_get(ctx
);
99 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
101 /* add to job when the buffer is dirty and resolve is clear (not added before) */
102 if (fb
->base
.nr_cbufs
&& (buffers
& PIPE_CLEAR_COLOR0
) &&
103 !(job
->resolve
& PIPE_CLEAR_COLOR0
)) {
104 struct lima_resource
*res
= lima_resource(fb
->base
.cbufs
[0]->texture
);
105 lima_flush_job_accessing_bo(ctx
, res
->bo
, true);
106 _mesa_hash_table_insert(ctx
->write_jobs
, &res
->base
, job
);
107 lima_job_add_bo(job
, LIMA_PIPE_PP
, res
->bo
, LIMA_SUBMIT_BO_WRITE
);
110 /* add to job when the buffer is dirty and resolve is clear (not added before) */
111 if (fb
->base
.zsbuf
&& (buffers
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
)) &&
112 !(job
->resolve
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
))) {
113 struct lima_resource
*res
= lima_resource(fb
->base
.zsbuf
->texture
);
114 lima_flush_job_accessing_bo(ctx
, res
->bo
, true);
115 _mesa_hash_table_insert(ctx
->write_jobs
, &res
->base
, job
);
116 lima_job_add_bo(job
, LIMA_PIPE_PP
, res
->bo
, LIMA_SUBMIT_BO_WRITE
);
119 job
->resolve
|= buffers
;
123 lima_damage_rect_union(struct pipe_scissor_state
*rect
,
124 unsigned minx
, unsigned maxx
,
125 unsigned miny
, unsigned maxy
)
127 rect
->minx
= MIN2(rect
->minx
, minx
);
128 rect
->miny
= MIN2(rect
->miny
, miny
);
129 rect
->maxx
= MAX2(rect
->maxx
, maxx
);
130 rect
->maxy
= MAX2(rect
->maxy
, maxy
);
134 lima_clear(struct pipe_context
*pctx
, unsigned buffers
, const struct pipe_scissor_state
*scissor_state
,
135 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
137 struct lima_context
*ctx
= lima_context(pctx
);
138 struct lima_job
*job
= lima_job_get(ctx
);
140 /* flush if this job already contains any draw, otherwise multi clear can be
141 * combined into a single job */
142 if (lima_job_has_draw_pending(job
)) {
144 job
= lima_job_get(ctx
);
147 lima_update_job_wb(ctx
, buffers
);
149 /* no need to reload if cleared */
150 if (ctx
->framebuffer
.base
.nr_cbufs
&& (buffers
& PIPE_CLEAR_COLOR0
)) {
151 struct lima_surface
*surf
= lima_surface(ctx
->framebuffer
.base
.cbufs
[0]);
152 surf
->reload
&= ~PIPE_CLEAR_COLOR0
;
155 struct lima_job_clear
*clear
= &job
->clear
;
156 clear
->buffers
= buffers
;
158 if (buffers
& PIPE_CLEAR_COLOR0
) {
160 ((uint32_t)float_to_ubyte(color
->f
[3]) << 24) |
161 ((uint32_t)float_to_ubyte(color
->f
[2]) << 16) |
162 ((uint32_t)float_to_ubyte(color
->f
[1]) << 8) |
163 float_to_ubyte(color
->f
[0]);
166 ((uint64_t)float_to_ushort(color
->f
[3]) << 48) |
167 ((uint64_t)float_to_ushort(color
->f
[2]) << 32) |
168 ((uint64_t)float_to_ushort(color
->f
[1]) << 16) |
169 float_to_ushort(color
->f
[0]);
172 struct lima_surface
*zsbuf
= lima_surface(ctx
->framebuffer
.base
.zsbuf
);
174 if (buffers
& PIPE_CLEAR_DEPTH
) {
175 clear
->depth
= util_pack_z(PIPE_FORMAT_Z24X8_UNORM
, depth
);
177 zsbuf
->reload
&= ~PIPE_CLEAR_DEPTH
;
179 clear
->depth
= 0x00ffffff;
181 if (buffers
& PIPE_CLEAR_STENCIL
) {
182 clear
->stencil
= stencil
;
184 zsbuf
->reload
&= ~PIPE_CLEAR_STENCIL
;
187 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_CLEAR
;
189 lima_damage_rect_union(&job
->damage_rect
,
190 0, ctx
->framebuffer
.base
.width
,
191 0, ctx
->framebuffer
.base
.height
);
194 enum lima_attrib_type
{
195 LIMA_ATTRIB_FLOAT
= 0x000,
196 /* todo: find out what lives here. */
197 LIMA_ATTRIB_I16
= 0x004,
198 LIMA_ATTRIB_U16
= 0x005,
199 LIMA_ATTRIB_I8
= 0x006,
200 LIMA_ATTRIB_U8
= 0x007,
201 LIMA_ATTRIB_I8N
= 0x008,
202 LIMA_ATTRIB_U8N
= 0x009,
203 LIMA_ATTRIB_I16N
= 0x00A,
204 LIMA_ATTRIB_U16N
= 0x00B,
205 /* todo: where is the 32 int */
206 /* todo: find out what lives here. */
207 LIMA_ATTRIB_FIXED
= 0x101
210 static enum lima_attrib_type
211 lima_pipe_format_to_attrib_type(enum pipe_format format
)
213 const struct util_format_description
*desc
= util_format_description(format
);
214 int i
= util_format_get_first_non_void_channel(format
);
215 const struct util_format_channel_description
*c
= desc
->channel
+ i
;
218 case UTIL_FORMAT_TYPE_FLOAT
:
219 return LIMA_ATTRIB_FLOAT
;
220 case UTIL_FORMAT_TYPE_FIXED
:
221 return LIMA_ATTRIB_FIXED
;
222 case UTIL_FORMAT_TYPE_SIGNED
:
225 return LIMA_ATTRIB_I8N
;
227 return LIMA_ATTRIB_I8
;
229 else if (c
->size
== 16) {
231 return LIMA_ATTRIB_I16N
;
233 return LIMA_ATTRIB_I16
;
236 case UTIL_FORMAT_TYPE_UNSIGNED
:
239 return LIMA_ATTRIB_U8N
;
241 return LIMA_ATTRIB_U8
;
243 else if (c
->size
== 16) {
245 return LIMA_ATTRIB_U16N
;
247 return LIMA_ATTRIB_U16
;
252 return LIMA_ATTRIB_FLOAT
;
256 lima_pack_vs_cmd(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
258 struct lima_job
*job
= lima_job_get(ctx
);
260 VS_CMD_BEGIN(&job
->vs_cmd_array
, 24);
262 if (!info
->index_size
) {
263 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_1();
264 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_2();
267 int uniform_size
= ctx
->vs
->uniform_pending_offset
+ ctx
->vs
->constant_size
+ 32;
268 VS_CMD_UNIFORMS_ADDRESS(
269 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_uniform
),
270 align(uniform_size
, 16));
272 VS_CMD_SHADER_ADDRESS(ctx
->vs
->bo
->va
, ctx
->vs
->shader_size
);
273 VS_CMD_SHADER_INFO(ctx
->vs
->prefetch
, ctx
->vs
->shader_size
);
275 int num_outputs
= ctx
->vs
->num_outputs
;
276 int num_attributes
= ctx
->vertex_elements
->num_elements
;
277 VS_CMD_VARYING_ATTRIBUTE_COUNT(num_outputs
, MAX2(1, num_attributes
));
281 VS_CMD_ATTRIBUTES_ADDRESS(
282 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_attribute_info
),
283 MAX2(1, num_attributes
));
285 VS_CMD_VARYINGS_ADDRESS(
286 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_varying_info
),
289 unsigned num
= info
->index_size
? (ctx
->max_index
- ctx
->min_index
+ 1) : info
->count
;
290 VS_CMD_DRAW(num
, info
->index_size
);
294 VS_CMD_ARRAYS_SEMAPHORE_END(info
->index_size
);
300 lima_pack_plbu_cmd(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
302 struct lima_vs_shader_state
*vs
= ctx
->vs
;
303 struct pipe_scissor_state
*cscissor
= &ctx
->clipped_scissor
;
304 struct lima_job
*job
= lima_job_get(ctx
);
305 PLBU_CMD_BEGIN(&job
->plbu_cmd_array
, 32);
307 PLBU_CMD_VIEWPORT_LEFT(fui(ctx
->viewport
.left
));
308 PLBU_CMD_VIEWPORT_RIGHT(fui(ctx
->viewport
.right
));
309 PLBU_CMD_VIEWPORT_BOTTOM(fui(ctx
->viewport
.bottom
));
310 PLBU_CMD_VIEWPORT_TOP(fui(ctx
->viewport
.top
));
312 if (!info
->index_size
)
313 PLBU_CMD_ARRAYS_SEMAPHORE_BEGIN();
315 int cf
= ctx
->rasterizer
->base
.cull_face
;
316 int ccw
= ctx
->rasterizer
->base
.front_ccw
;
318 bool force_point_size
= false;
320 if (cf
!= PIPE_FACE_NONE
) {
321 if (cf
& PIPE_FACE_FRONT
)
322 cull
|= ccw
? 0x00040000 : 0x00020000;
323 if (cf
& PIPE_FACE_BACK
)
324 cull
|= ccw
? 0x00020000 : 0x00040000;
327 /* Specify point size with PLBU command if shader doesn't write */
328 if (info
->mode
== PIPE_PRIM_POINTS
&& ctx
->vs
->point_size_idx
== -1)
329 force_point_size
= true;
331 /* Specify line width with PLBU command for lines */
332 if (info
->mode
> PIPE_PRIM_POINTS
&& info
->mode
< PIPE_PRIM_TRIANGLES
)
333 force_point_size
= true;
335 PLBU_CMD_PRIMITIVE_SETUP(force_point_size
, cull
, info
->index_size
);
337 PLBU_CMD_RSW_VERTEX_ARRAY(
338 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
),
342 * - we should set it only for the first draw that enabled the scissor and for
343 * latter draw only if scissor is dirty
346 assert(cscissor
->minx
< cscissor
->maxx
&& cscissor
->miny
< cscissor
->maxy
);
347 PLBU_CMD_SCISSORS(cscissor
->minx
, cscissor
->maxx
, cscissor
->miny
, cscissor
->maxy
);
349 lima_damage_rect_union(&job
->damage_rect
, cscissor
->minx
, cscissor
->maxx
,
350 cscissor
->miny
, cscissor
->maxy
);
354 PLBU_CMD_DEPTH_RANGE_NEAR(fui(ctx
->viewport
.near
));
355 PLBU_CMD_DEPTH_RANGE_FAR(fui(ctx
->viewport
.far
));
357 if ((info
->mode
== PIPE_PRIM_POINTS
&& ctx
->vs
->point_size_idx
== -1) ||
358 ((info
->mode
>= PIPE_PRIM_LINES
) && (info
->mode
< PIPE_PRIM_TRIANGLES
)))
360 uint32_t v
= info
->mode
== PIPE_PRIM_POINTS
?
361 fui(ctx
->rasterizer
->base
.point_size
) : fui(ctx
->rasterizer
->base
.line_width
);
362 PLBU_CMD_LOW_PRIM_SIZE(v
);
365 if (info
->index_size
) {
366 PLBU_CMD_INDEXED_DEST(ctx
->gp_output
->va
);
367 if (vs
->point_size_idx
!= -1)
368 PLBU_CMD_INDEXED_PT_SIZE(ctx
->gp_output
->va
+ ctx
->gp_output_point_size_offt
);
370 PLBU_CMD_INDICES(ctx
->index_res
->bo
->va
+ info
->start
* info
->index_size
+ ctx
->index_offset
);
373 /* can this make the attribute info static? */
374 PLBU_CMD_DRAW_ARRAYS(info
->mode
, info
->start
, info
->count
);
377 PLBU_CMD_ARRAYS_SEMAPHORE_END();
379 if (info
->index_size
)
380 PLBU_CMD_DRAW_ELEMENTS(info
->mode
, ctx
->min_index
, info
->count
);
386 lima_blend_func(enum pipe_blend_func pipe
)
391 case PIPE_BLEND_SUBTRACT
:
393 case PIPE_BLEND_REVERSE_SUBTRACT
:
404 lima_blend_factor_has_alpha(enum pipe_blendfactor pipe
)
406 /* Bit 4 is set if the blendfactor uses alpha */
408 case PIPE_BLENDFACTOR_SRC_ALPHA
:
409 case PIPE_BLENDFACTOR_DST_ALPHA
:
410 case PIPE_BLENDFACTOR_CONST_ALPHA
:
411 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
412 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
413 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
416 case PIPE_BLENDFACTOR_SRC_COLOR
:
417 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
418 case PIPE_BLENDFACTOR_DST_COLOR
:
419 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
420 case PIPE_BLENDFACTOR_CONST_COLOR
:
421 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
422 case PIPE_BLENDFACTOR_ZERO
:
423 case PIPE_BLENDFACTOR_ONE
:
424 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
427 case PIPE_BLENDFACTOR_SRC1_COLOR
:
428 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
429 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
430 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
431 return -1; /* not supported */
437 lima_blend_factor_is_inv(enum pipe_blendfactor pipe
)
439 /* Bit 3 is set if the blendfactor type is inverted */
441 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
442 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
443 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
444 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
445 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
446 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
447 case PIPE_BLENDFACTOR_ONE
:
450 case PIPE_BLENDFACTOR_SRC_COLOR
:
451 case PIPE_BLENDFACTOR_SRC_ALPHA
:
452 case PIPE_BLENDFACTOR_DST_COLOR
:
453 case PIPE_BLENDFACTOR_DST_ALPHA
:
454 case PIPE_BLENDFACTOR_CONST_COLOR
:
455 case PIPE_BLENDFACTOR_CONST_ALPHA
:
456 case PIPE_BLENDFACTOR_ZERO
:
457 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
460 case PIPE_BLENDFACTOR_SRC1_COLOR
:
461 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
462 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
463 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
464 return -1; /* not supported */
470 lima_blend_factor(enum pipe_blendfactor pipe
)
472 /* Bits 0-2 indicate the blendfactor type */
474 case PIPE_BLENDFACTOR_SRC_COLOR
:
475 case PIPE_BLENDFACTOR_SRC_ALPHA
:
476 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
477 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
480 case PIPE_BLENDFACTOR_DST_COLOR
:
481 case PIPE_BLENDFACTOR_DST_ALPHA
:
482 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
483 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
486 case PIPE_BLENDFACTOR_CONST_COLOR
:
487 case PIPE_BLENDFACTOR_CONST_ALPHA
:
488 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
489 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
492 case PIPE_BLENDFACTOR_ZERO
:
493 case PIPE_BLENDFACTOR_ONE
:
496 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
499 case PIPE_BLENDFACTOR_SRC1_COLOR
:
500 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
501 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
502 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
503 return -1; /* not supported */
509 lima_calculate_alpha_blend(enum pipe_blend_func rgb_func
, enum pipe_blend_func alpha_func
,
510 enum pipe_blendfactor rgb_src_factor
, enum pipe_blendfactor rgb_dst_factor
,
511 enum pipe_blendfactor alpha_src_factor
, enum pipe_blendfactor alpha_dst_factor
)
513 /* PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE has to be changed to PIPE_BLENDFACTOR_ONE
514 * if it is set for alpha_src.
516 if (alpha_src_factor
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
)
517 alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
519 return lima_blend_func(rgb_func
) |
520 (lima_blend_func(alpha_func
) << 3) |
522 (lima_blend_factor(rgb_src_factor
) << 6) |
523 (lima_blend_factor_is_inv(rgb_src_factor
) << 9) |
524 (lima_blend_factor_has_alpha(rgb_src_factor
) << 10) |
526 (lima_blend_factor(rgb_dst_factor
) << 11) |
527 (lima_blend_factor_is_inv(rgb_dst_factor
) << 14) |
528 (lima_blend_factor_has_alpha(rgb_dst_factor
) << 15) |
530 (lima_blend_factor(alpha_src_factor
) << 16) |
531 (lima_blend_factor_is_inv(alpha_src_factor
) << 19) |
533 (lima_blend_factor(alpha_dst_factor
) << 20) |
534 (lima_blend_factor_is_inv(alpha_dst_factor
) << 23) |
535 0x0C000000; /* need to check if this is GLESv1 glAlphaFunc */
539 lima_stencil_op(enum pipe_stencil_op pipe
)
542 case PIPE_STENCIL_OP_KEEP
:
544 case PIPE_STENCIL_OP_ZERO
:
546 case PIPE_STENCIL_OP_REPLACE
:
548 case PIPE_STENCIL_OP_INCR
:
550 case PIPE_STENCIL_OP_DECR
:
552 case PIPE_STENCIL_OP_INCR_WRAP
:
554 case PIPE_STENCIL_OP_DECR_WRAP
:
556 case PIPE_STENCIL_OP_INVERT
:
563 lima_calculate_depth_test(struct pipe_depth_state
*depth
, struct pipe_rasterizer_state
*rst
)
565 int offset_scale
= 0, offset_units
= 0;
566 enum pipe_compare_func func
= (depth
->enabled
? depth
->func
: PIPE_FUNC_ALWAYS
);
568 offset_scale
= CLAMP(rst
->offset_scale
* 4, -128, 127);
569 if (offset_scale
< 0)
570 offset_scale
+= 0x100;
572 offset_units
= CLAMP(rst
->offset_units
* 2, -128, 127);
573 if (offset_units
< 0)
574 offset_units
+= 0x100;
576 return (depth
->enabled
&& depth
->writemask
) |
578 (offset_scale
<< 16) |
579 (offset_units
<< 24) |
580 0x30; /* find out what is this */
584 lima_pack_render_state(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
586 struct lima_fs_shader_state
*fs
= ctx
->fs
;
587 struct lima_render_state
*render
=
588 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_plb_rsw
,
591 bool pixel_kill
= true;
593 /* do hw support RGBA independ blend?
594 * PIPE_CAP_INDEP_BLEND_ENABLE
596 * how to handle the no cbuf only zbuf case?
598 struct pipe_rt_blend_state
*rt
= ctx
->blend
->base
.rt
;
599 render
->blend_color_bg
= float_to_ubyte(ctx
->blend_color
.color
[2]) |
600 (float_to_ubyte(ctx
->blend_color
.color
[1]) << 16);
601 render
->blend_color_ra
= float_to_ubyte(ctx
->blend_color
.color
[0]) |
602 (float_to_ubyte(ctx
->blend_color
.color
[3]) << 16);
604 if (rt
->blend_enable
) {
605 render
->alpha_blend
= lima_calculate_alpha_blend(rt
->rgb_func
, rt
->alpha_func
,
606 rt
->rgb_src_factor
, rt
->rgb_dst_factor
,
607 rt
->alpha_src_factor
, rt
->alpha_dst_factor
);
611 * Special handling for blending disabled.
612 * Binary driver is generating the same alpha_value,
613 * as when we would just enable blending, without changing/setting any blend equation/params.
614 * Normaly in this case mesa would set all rt fields (func/factor) to zero.
616 render
->alpha_blend
= lima_calculate_alpha_blend(PIPE_BLEND_ADD
, PIPE_BLEND_ADD
,
617 PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_ZERO
,
618 PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_ZERO
);
621 render
->alpha_blend
|= (rt
->colormask
& PIPE_MASK_RGBA
) << 28;
623 struct pipe_rasterizer_state
*rst
= &ctx
->rasterizer
->base
;
624 struct pipe_depth_state
*depth
= &ctx
->zsa
->base
.depth
;
625 render
->depth_test
= lima_calculate_depth_test(depth
, rst
);
629 near
= float_to_ushort(ctx
->viewport
.near
);
630 far
= float_to_ushort(ctx
->viewport
.far
);
632 /* Subtract epsilon from 'near' if far == near. Make sure we don't get overflow */
633 if ((far
== near
) && (near
!= 0))
636 /* overlap with plbu? any place can remove one? */
637 render
->depth_range
= near
| (far
<< 16);
639 struct pipe_stencil_state
*stencil
= ctx
->zsa
->base
.stencil
;
640 struct pipe_stencil_ref
*ref
= &ctx
->stencil_ref
;
642 if (stencil
[0].enabled
) { /* stencil is enabled */
643 render
->stencil_front
= stencil
[0].func
|
644 (lima_stencil_op(stencil
[0].fail_op
) << 3) |
645 (lima_stencil_op(stencil
[0].zfail_op
) << 6) |
646 (lima_stencil_op(stencil
[0].zpass_op
) << 9) |
647 (ref
->ref_value
[0] << 16) |
648 (stencil
[0].valuemask
<< 24);
649 render
->stencil_back
= render
->stencil_front
;
650 render
->stencil_test
= (stencil
[0].writemask
& 0xff) | (stencil
[0].writemask
& 0xff) << 8;
651 if (stencil
[1].enabled
) { /* two-side is enabled */
652 render
->stencil_back
= stencil
[1].func
|
653 (lima_stencil_op(stencil
[1].fail_op
) << 3) |
654 (lima_stencil_op(stencil
[1].zfail_op
) << 6) |
655 (lima_stencil_op(stencil
[1].zpass_op
) << 9) |
656 (ref
->ref_value
[1] << 16) |
657 (stencil
[1].valuemask
<< 24);
658 render
->stencil_test
= (stencil
[0].writemask
& 0xff) | (stencil
[1].writemask
& 0xff) << 8;
660 /* TODO: Find out, what (render->stecil_test & 0xffff0000) is.
661 * 0x00ff0000 is probably (float_to_ubyte(alpha->ref_value) << 16)
662 * (render->multi_sample & 0x00000007 is probably the compare function
663 * of glAlphaFunc then.
667 /* Default values, when stencil is disabled:
668 * stencil[0|1].valuemask = 0xff
669 * stencil[0|1].func = PIPE_FUNC_ALWAYS
670 * stencil[0|1].writemask = 0xff
672 render
->stencil_front
= 0xff000007;
673 render
->stencil_back
= 0xff000007;
674 render
->stencil_test
= 0x0000ffff;
677 /* need more investigation */
678 if (info
->mode
== PIPE_PRIM_POINTS
)
679 render
->multi_sample
= 0x0000F007;
680 else if (info
->mode
< PIPE_PRIM_TRIANGLES
)
681 render
->multi_sample
= 0x0000F407;
683 render
->multi_sample
= 0x0000F807;
684 if (ctx
->framebuffer
.base
.samples
)
685 render
->multi_sample
|= 0x68;
687 render
->shader_address
=
688 ctx
->fs
->bo
->va
| (((uint32_t *)ctx
->fs
->bo
->map
)[0] & 0x1F);
690 /* seems not needed */
691 render
->uniforms_address
= 0x00000000;
693 render
->textures_address
= 0x00000000;
695 render
->aux0
= (ctx
->vs
->varying_stride
>> 3);
696 render
->aux1
= 0x00001000;
697 if (ctx
->blend
->base
.dither
)
698 render
->aux1
|= 0x00002000;
700 if (fs
->uses_discard
) {
705 if (rt
->blend_enable
)
708 if ((rt
->colormask
& PIPE_MASK_RGBA
) != PIPE_MASK_RGBA
)
712 render
->aux0
|= 0x300;
715 render
->aux0
|= 0x1000;
717 if (ctx
->tex_stateobj
.num_samplers
) {
718 render
->textures_address
=
719 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_tex_desc
);
720 render
->aux0
|= ctx
->tex_stateobj
.num_samplers
<< 14;
721 render
->aux0
|= 0x20;
724 if (ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].buffer
) {
725 render
->uniforms_address
=
726 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform_array
);
727 uint32_t size
= ctx
->buffer_state
[lima_ctx_buff_pp_uniform
].size
;
730 bits
= util_last_bit(size
>> 3) - 1;
731 bits
+= size
& u_bit_consecutive(0, bits
+ 3) ? 1 : 0;
733 render
->uniforms_address
|= bits
> 0xf ? 0xf : bits
;
735 render
->aux0
|= 0x80;
736 render
->aux1
|= 0x10000;
739 if (ctx
->vs
->num_varyings
) {
740 render
->varying_types
= 0x00000000;
741 render
->varyings_address
= ctx
->gp_output
->va
+
742 ctx
->gp_output_varyings_offt
;
743 for (int i
= 0, index
= 0; i
< ctx
->vs
->num_outputs
; i
++) {
746 if (i
== ctx
->vs
->gl_pos_idx
||
747 i
== ctx
->vs
->point_size_idx
)
750 struct lima_varying_info
*v
= ctx
->vs
->varying
+ i
;
751 if (v
->component_size
== 4)
752 val
= v
->components
> 2 ? 0 : 1;
754 val
= v
->components
> 2 ? 2 : 3;
757 render
->varying_types
|= val
<< (3 * index
);
758 else if (index
== 10) {
759 render
->varying_types
|= val
<< 30;
760 render
->varyings_address
|= val
>> 2;
762 else if (index
== 11)
763 render
->varyings_address
|= val
<< 1;
769 render
->varying_types
= 0x00000000;
770 render
->varyings_address
= 0x00000000;
773 struct lima_job
*job
= lima_job_get(ctx
);
775 lima_dump_command_stream_print(
776 job
->dump
, render
, sizeof(*render
),
777 false, "add render state at va %x\n",
778 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
));
780 lima_dump_rsw_command_stream_print(
781 job
->dump
, render
, sizeof(*render
),
782 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
));
786 lima_update_gp_attribute_info(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
788 struct lima_job
*job
= lima_job_get(ctx
);
789 struct lima_vertex_element_state
*ve
= ctx
->vertex_elements
;
790 struct lima_context_vertex_buffer
*vb
= &ctx
->vertex_buffers
;
792 uint32_t *attribute
=
793 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_attribute_info
,
794 MAX2(1, ve
->num_elements
) * 8);
797 for (int i
= 0; i
< ve
->num_elements
; i
++) {
798 struct pipe_vertex_element
*pve
= ve
->pipe
+ i
;
800 assert(pve
->vertex_buffer_index
< vb
->count
);
801 assert(vb
->enabled_mask
& (1 << pve
->vertex_buffer_index
));
803 struct pipe_vertex_buffer
*pvb
= vb
->vb
+ pve
->vertex_buffer_index
;
804 struct lima_resource
*res
= lima_resource(pvb
->buffer
.resource
);
806 lima_job_add_bo(job
, LIMA_PIPE_GP
, res
->bo
, LIMA_SUBMIT_BO_READ
);
808 unsigned start
= info
->index_size
? (ctx
->min_index
+ info
->index_bias
) : info
->start
;
809 attribute
[n
++] = res
->bo
->va
+ pvb
->buffer_offset
+ pve
->src_offset
810 + start
* pvb
->stride
;
811 attribute
[n
++] = (pvb
->stride
<< 11) |
812 (lima_pipe_format_to_attrib_type(pve
->src_format
) << 2) |
813 (util_format_get_nr_components(pve
->src_format
) - 1);
816 lima_dump_command_stream_print(
817 job
->dump
, attribute
, n
* 4, false, "update attribute info at va %x\n",
818 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_attribute_info
));
822 lima_update_gp_uniform(struct lima_context
*ctx
)
824 struct lima_context_constant_buffer
*ccb
=
825 ctx
->const_buffer
+ PIPE_SHADER_VERTEX
;
826 struct lima_vs_shader_state
*vs
= ctx
->vs
;
828 int size
= vs
->uniform_pending_offset
+ vs
->constant_size
+ 32;
829 void *vs_const_buff
=
830 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_uniform
, size
);
833 memcpy(vs_const_buff
, ccb
->buffer
, ccb
->size
);
835 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
,
836 ctx
->viewport
.transform
.scale
,
837 sizeof(ctx
->viewport
.transform
.scale
));
838 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
+ 16,
839 ctx
->viewport
.transform
.translate
,
840 sizeof(ctx
->viewport
.transform
.translate
));
843 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
+ 32,
844 vs
->constant
, vs
->constant_size
);
846 struct lima_job
*job
= lima_job_get(ctx
);
848 if (lima_debug
& LIMA_DEBUG_GP
) {
849 float *vs_const_buff_f
= vs_const_buff
;
850 printf("gp uniforms:\n");
851 for (int i
= 0; i
< (size
/ sizeof(float)); i
++) {
853 printf("%4d:", i
/ 4);
854 printf(" %8.4f", vs_const_buff_f
[i
]);
861 lima_dump_command_stream_print(
862 job
->dump
, vs_const_buff
, size
, true,
863 "update gp uniform at va %x\n",
864 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_uniform
));
868 lima_update_pp_uniform(struct lima_context
*ctx
)
870 const float *const_buff
= ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].buffer
;
871 size_t const_buff_size
= ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].size
/ sizeof(float);
876 uint16_t *fp16_const_buff
=
877 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_uniform
,
878 const_buff_size
* sizeof(uint16_t));
881 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_uniform_array
, 4);
883 for (int i
= 0; i
< const_buff_size
; i
++)
884 fp16_const_buff
[i
] = util_float_to_half(const_buff
[i
]);
886 *array
= lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform
);
888 struct lima_job
*job
= lima_job_get(ctx
);
890 lima_dump_command_stream_print(
891 job
->dump
, fp16_const_buff
, const_buff_size
* 2,
892 false, "add pp uniform data at va %x\n",
893 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform
));
894 lima_dump_command_stream_print(
895 job
->dump
, array
, 4, false, "add pp uniform info at va %x\n",
896 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform_array
));
900 lima_update_varying(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
902 struct lima_job
*job
= lima_job_get(ctx
);
903 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
904 struct lima_vs_shader_state
*vs
= ctx
->vs
;
905 uint32_t gp_output_size
;
906 unsigned num
= info
->index_size
? (ctx
->max_index
- ctx
->min_index
+ 1) : info
->count
;
909 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_varying_info
,
910 vs
->num_outputs
* 8);
915 for (int i
= 0; i
< vs
->num_outputs
; i
++) {
916 struct lima_varying_info
*v
= vs
->varying
+ i
;
918 if (i
== vs
->gl_pos_idx
||
919 i
== vs
->point_size_idx
)
922 int size
= v
->component_size
* 4;
924 /* does component_size == 2 need to be 16 aligned? */
925 if (v
->component_size
== 4)
926 offset
= align(offset
, 16);
932 vs
->varying_stride
= align(offset
, 16);
934 /* gl_Position is always present, allocate space for it */
935 gp_output_size
= align(4 * 4 * num
, 0x40);
937 /* Allocate space for varyings if there're any */
938 if (vs
->num_varyings
) {
939 ctx
->gp_output_varyings_offt
= gp_output_size
;
940 gp_output_size
+= align(vs
->varying_stride
* num
, 0x40);
943 /* Allocate space for gl_PointSize if it's there */
944 if (vs
->point_size_idx
!= -1) {
945 ctx
->gp_output_point_size_offt
= gp_output_size
;
946 gp_output_size
+= 4 * num
;
949 /* gp_output can be too large for the suballocator, so create a
950 * separate bo for it. The bo cache should prevent performance hit.
952 ctx
->gp_output
= lima_bo_create(screen
, gp_output_size
, 0);
953 assert(ctx
->gp_output
);
954 lima_job_add_bo(job
, LIMA_PIPE_GP
, ctx
->gp_output
, LIMA_SUBMIT_BO_WRITE
);
955 lima_job_add_bo(job
, LIMA_PIPE_PP
, ctx
->gp_output
, LIMA_SUBMIT_BO_READ
);
957 for (int i
= 0; i
< vs
->num_outputs
; i
++) {
958 struct lima_varying_info
*v
= vs
->varying
+ i
;
960 if (i
== vs
->gl_pos_idx
) {
962 varying
[n
++] = ctx
->gp_output
->va
;
963 varying
[n
++] = 0x8020;
964 } else if (i
== vs
->point_size_idx
) {
966 varying
[n
++] = ctx
->gp_output
->va
+ ctx
->gp_output_point_size_offt
;
967 varying
[n
++] = 0x2021;
970 varying
[n
++] = ctx
->gp_output
->va
+ ctx
->gp_output_varyings_offt
+
972 varying
[n
++] = (vs
->varying_stride
<< 11) | (v
->components
- 1) |
973 (v
->component_size
== 2 ? 0x0C : 0);
977 lima_dump_command_stream_print(
978 job
->dump
, varying
, n
* 4, false, "update varying info at va %x\n",
979 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_varying_info
));
983 lima_draw_vbo_update(struct pipe_context
*pctx
,
984 const struct pipe_draw_info
*info
)
986 struct lima_context
*ctx
= lima_context(pctx
);
987 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
988 unsigned buffers
= 0;
990 if (fb
->base
.zsbuf
) {
991 if (ctx
->zsa
->base
.depth
.enabled
)
992 buffers
|= PIPE_CLEAR_DEPTH
;
993 if (ctx
->zsa
->base
.stencil
[0].enabled
||
994 ctx
->zsa
->base
.stencil
[1].enabled
)
995 buffers
|= PIPE_CLEAR_STENCIL
;
998 if (fb
->base
.nr_cbufs
)
999 buffers
|= PIPE_CLEAR_COLOR0
;
1001 lima_update_job_wb(ctx
, buffers
);
1003 lima_update_gp_attribute_info(ctx
, info
);
1005 if ((ctx
->dirty
& LIMA_CONTEXT_DIRTY_CONST_BUFF
&&
1006 ctx
->const_buffer
[PIPE_SHADER_VERTEX
].dirty
) ||
1007 ctx
->dirty
& LIMA_CONTEXT_DIRTY_VIEWPORT
||
1008 ctx
->dirty
& LIMA_CONTEXT_DIRTY_SHADER_VERT
) {
1009 lima_update_gp_uniform(ctx
);
1010 ctx
->const_buffer
[PIPE_SHADER_VERTEX
].dirty
= false;
1013 lima_update_varying(ctx
, info
);
1015 lima_pack_vs_cmd(ctx
, info
);
1017 if (ctx
->dirty
& LIMA_CONTEXT_DIRTY_CONST_BUFF
&&
1018 ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].dirty
) {
1019 lima_update_pp_uniform(ctx
);
1020 ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].dirty
= false;
1023 lima_update_textures(ctx
);
1025 lima_pack_render_state(ctx
, info
);
1026 lima_pack_plbu_cmd(ctx
, info
);
1028 if (ctx
->gp_output
) {
1029 lima_bo_unreference(ctx
->gp_output
); /* held by job */
1030 ctx
->gp_output
= NULL
;
1037 lima_draw_vbo_indexed(struct pipe_context
*pctx
,
1038 const struct pipe_draw_info
*info
)
1040 struct lima_context
*ctx
= lima_context(pctx
);
1041 struct lima_job
*job
= lima_job_get(ctx
);
1042 struct pipe_resource
*indexbuf
= NULL
;
1043 bool needs_indices
= true;
1045 /* Mali Utgard GPU always need min/max index info for index draw,
1046 * compute it if upper layer does not do for us */
1047 if (info
->max_index
!= ~0u) {
1048 ctx
->min_index
= info
->min_index
;
1049 ctx
->max_index
= info
->max_index
;
1050 needs_indices
= false;
1053 if (info
->has_user_indices
) {
1054 util_upload_index_buffer(&ctx
->base
, info
, &indexbuf
, &ctx
->index_offset
, 0x40);
1055 ctx
->index_res
= lima_resource(indexbuf
);
1058 ctx
->index_res
= lima_resource(info
->index
.resource
);
1059 ctx
->index_offset
= 0;
1060 needs_indices
= !panfrost_minmax_cache_get(ctx
->index_res
->index_cache
, info
->start
,
1061 info
->count
, &ctx
->min_index
, &ctx
->max_index
);
1064 if (needs_indices
) {
1065 u_vbuf_get_minmax_index(pctx
, info
, &ctx
->min_index
, &ctx
->max_index
);
1066 if (!info
->has_user_indices
)
1067 panfrost_minmax_cache_add(ctx
->index_res
->index_cache
, info
->start
, info
->count
,
1068 ctx
->min_index
, ctx
->max_index
);
1071 lima_job_add_bo(job
, LIMA_PIPE_GP
, ctx
->index_res
->bo
, LIMA_SUBMIT_BO_READ
);
1072 lima_job_add_bo(job
, LIMA_PIPE_PP
, ctx
->index_res
->bo
, LIMA_SUBMIT_BO_READ
);
1073 lima_draw_vbo_update(pctx
, info
);
1076 pipe_resource_reference(&indexbuf
, NULL
);
1080 lima_draw_vbo_count(struct pipe_context
*pctx
,
1081 const struct pipe_draw_info
*info
)
1083 static const uint32_t max_verts
= 65535;
1085 struct pipe_draw_info local_info
= *info
;
1086 unsigned start
= info
->start
;
1087 unsigned count
= info
->count
;
1090 unsigned this_count
= count
;
1093 u_split_draw(info
, max_verts
, &this_count
, &step
);
1095 local_info
.start
= start
;
1096 local_info
.count
= this_count
;
1098 lima_draw_vbo_update(pctx
, &local_info
);
1106 lima_draw_vbo(struct pipe_context
*pctx
,
1107 const struct pipe_draw_info
*info
)
1109 /* check if draw mode and vertex/index count match,
1110 * otherwise gp will hang */
1111 if (!u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
)) {
1112 debug_printf("draw mode and vertex/index count mismatch\n");
1116 struct lima_context
*ctx
= lima_context(pctx
);
1118 if (!ctx
->vs
|| !ctx
->fs
) {
1119 debug_warn_once("no shader, skip draw\n");
1123 lima_clip_scissor_to_viewport(ctx
);
1124 if (lima_is_scissor_zero(ctx
))
1127 if (!lima_update_vs_state(ctx
) || !lima_update_fs_state(ctx
))
1130 struct lima_job
*job
= lima_job_get(ctx
);
1132 lima_dump_command_stream_print(
1133 job
->dump
, ctx
->vs
->bo
->map
, ctx
->vs
->shader_size
, false,
1134 "add vs at va %x\n", ctx
->vs
->bo
->va
);
1136 lima_dump_command_stream_print(
1137 job
->dump
, ctx
->fs
->bo
->map
, ctx
->fs
->shader_size
, false,
1138 "add fs at va %x\n", ctx
->fs
->bo
->va
);
1140 lima_job_add_bo(job
, LIMA_PIPE_GP
, ctx
->vs
->bo
, LIMA_SUBMIT_BO_READ
);
1141 lima_job_add_bo(job
, LIMA_PIPE_PP
, ctx
->fs
->bo
, LIMA_SUBMIT_BO_READ
);
1143 if (info
->index_size
)
1144 lima_draw_vbo_indexed(pctx
, info
);
1146 lima_draw_vbo_count(pctx
, info
);
1150 lima_draw_init(struct lima_context
*ctx
)
1152 ctx
->base
.clear
= lima_clear
;
1153 ctx
->base
.draw_vbo
= lima_draw_vbo
;