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_is_scissor_zero(struct lima_context
*ctx
)
55 if (!ctx
->rasterizer
|| !ctx
->rasterizer
->base
.scissor
)
58 struct pipe_scissor_state
*scissor
= &ctx
->scissor
;
60 scissor
->minx
== scissor
->maxx
61 && scissor
->miny
== scissor
->maxy
;
65 lima_update_job_wb(struct lima_context
*ctx
, unsigned buffers
)
67 struct lima_job
*job
= lima_job_get(ctx
);
68 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
70 /* add to job when the buffer is dirty and resolve is clear (not added before) */
71 if (fb
->base
.nr_cbufs
&& (buffers
& PIPE_CLEAR_COLOR0
) &&
72 !(job
->resolve
& PIPE_CLEAR_COLOR0
)) {
73 struct lima_resource
*res
= lima_resource(fb
->base
.cbufs
[0]->texture
);
74 lima_flush_job_accessing_bo(ctx
, res
->bo
, true);
75 _mesa_hash_table_insert(ctx
->write_jobs
, &res
->base
, job
);
76 lima_job_add_bo(job
, LIMA_PIPE_PP
, res
->bo
, LIMA_SUBMIT_BO_WRITE
);
79 /* add to job when the buffer is dirty and resolve is clear (not added before) */
80 if (fb
->base
.zsbuf
&& (buffers
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
)) &&
81 !(job
->resolve
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
))) {
82 struct lima_resource
*res
= lima_resource(fb
->base
.zsbuf
->texture
);
83 lima_flush_job_accessing_bo(ctx
, res
->bo
, true);
84 _mesa_hash_table_insert(ctx
->write_jobs
, &res
->base
, job
);
85 lima_job_add_bo(job
, LIMA_PIPE_PP
, res
->bo
, LIMA_SUBMIT_BO_WRITE
);
88 job
->resolve
|= buffers
;
92 lima_damage_rect_union(struct pipe_scissor_state
*rect
,
93 unsigned minx
, unsigned maxx
,
94 unsigned miny
, unsigned maxy
)
96 rect
->minx
= MIN2(rect
->minx
, minx
);
97 rect
->miny
= MIN2(rect
->miny
, miny
);
98 rect
->maxx
= MAX2(rect
->maxx
, maxx
);
99 rect
->maxy
= MAX2(rect
->maxy
, maxy
);
103 lima_clear(struct pipe_context
*pctx
, unsigned buffers
,
104 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
106 struct lima_context
*ctx
= lima_context(pctx
);
107 struct lima_job
*job
= lima_job_get(ctx
);
109 /* flush if this job already contains any draw, otherwise multi clear can be
110 * combined into a single job */
111 if (lima_job_has_draw_pending(job
)) {
113 job
= lima_job_get(ctx
);
116 lima_update_job_wb(ctx
, buffers
);
118 /* no need to reload if cleared */
119 if (ctx
->framebuffer
.base
.nr_cbufs
&& (buffers
& PIPE_CLEAR_COLOR0
)) {
120 struct lima_surface
*surf
= lima_surface(ctx
->framebuffer
.base
.cbufs
[0]);
121 surf
->reload
= false;
124 struct lima_job_clear
*clear
= &job
->clear
;
125 clear
->buffers
= buffers
;
127 if (buffers
& PIPE_CLEAR_COLOR0
) {
129 ((uint32_t)float_to_ubyte(color
->f
[3]) << 24) |
130 ((uint32_t)float_to_ubyte(color
->f
[2]) << 16) |
131 ((uint32_t)float_to_ubyte(color
->f
[1]) << 8) |
132 float_to_ubyte(color
->f
[0]);
135 ((uint64_t)float_to_ushort(color
->f
[3]) << 48) |
136 ((uint64_t)float_to_ushort(color
->f
[2]) << 32) |
137 ((uint64_t)float_to_ushort(color
->f
[1]) << 16) |
138 float_to_ushort(color
->f
[0]);
141 if (buffers
& PIPE_CLEAR_DEPTH
)
142 clear
->depth
= util_pack_z(PIPE_FORMAT_Z24X8_UNORM
, depth
);
144 if (buffers
& PIPE_CLEAR_STENCIL
)
145 clear
->stencil
= stencil
;
147 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_CLEAR
;
149 lima_damage_rect_union(&job
->damage_rect
,
150 0, ctx
->framebuffer
.base
.width
,
151 0, ctx
->framebuffer
.base
.height
);
154 enum lima_attrib_type
{
155 LIMA_ATTRIB_FLOAT
= 0x000,
156 /* todo: find out what lives here. */
157 LIMA_ATTRIB_I16
= 0x004,
158 LIMA_ATTRIB_U16
= 0x005,
159 LIMA_ATTRIB_I8
= 0x006,
160 LIMA_ATTRIB_U8
= 0x007,
161 LIMA_ATTRIB_I8N
= 0x008,
162 LIMA_ATTRIB_U8N
= 0x009,
163 LIMA_ATTRIB_I16N
= 0x00A,
164 LIMA_ATTRIB_U16N
= 0x00B,
165 /* todo: where is the 32 int */
166 /* todo: find out what lives here. */
167 LIMA_ATTRIB_FIXED
= 0x101
170 static enum lima_attrib_type
171 lima_pipe_format_to_attrib_type(enum pipe_format format
)
173 const struct util_format_description
*desc
= util_format_description(format
);
174 int i
= util_format_get_first_non_void_channel(format
);
175 const struct util_format_channel_description
*c
= desc
->channel
+ i
;
178 case UTIL_FORMAT_TYPE_FLOAT
:
179 return LIMA_ATTRIB_FLOAT
;
180 case UTIL_FORMAT_TYPE_FIXED
:
181 return LIMA_ATTRIB_FIXED
;
182 case UTIL_FORMAT_TYPE_SIGNED
:
185 return LIMA_ATTRIB_I8N
;
187 return LIMA_ATTRIB_I8
;
189 else if (c
->size
== 16) {
191 return LIMA_ATTRIB_I16N
;
193 return LIMA_ATTRIB_I16
;
196 case UTIL_FORMAT_TYPE_UNSIGNED
:
199 return LIMA_ATTRIB_U8N
;
201 return LIMA_ATTRIB_U8
;
203 else if (c
->size
== 16) {
205 return LIMA_ATTRIB_U16N
;
207 return LIMA_ATTRIB_U16
;
212 return LIMA_ATTRIB_FLOAT
;
216 lima_pack_vs_cmd(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
218 struct lima_job
*job
= lima_job_get(ctx
);
220 VS_CMD_BEGIN(&job
->vs_cmd_array
, 24);
222 if (!info
->index_size
) {
223 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_1();
224 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_2();
227 int uniform_size
= ctx
->vs
->uniform_pending_offset
+ ctx
->vs
->constant_size
+ 32;
228 VS_CMD_UNIFORMS_ADDRESS(
229 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_uniform
),
230 align(uniform_size
, 16));
232 VS_CMD_SHADER_ADDRESS(ctx
->vs
->bo
->va
, ctx
->vs
->shader_size
);
233 VS_CMD_SHADER_INFO(ctx
->vs
->prefetch
, ctx
->vs
->shader_size
);
235 int num_outputs
= ctx
->vs
->num_outputs
;
236 int num_attributes
= ctx
->vertex_elements
->num_elements
;
237 VS_CMD_VARYING_ATTRIBUTE_COUNT(num_outputs
, MAX2(1, num_attributes
));
241 VS_CMD_ATTRIBUTES_ADDRESS(
242 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_attribute_info
),
243 MAX2(1, num_attributes
));
245 VS_CMD_VARYINGS_ADDRESS(
246 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_varying_info
),
249 unsigned num
= info
->index_size
? (ctx
->max_index
- ctx
->min_index
+ 1) : info
->count
;
250 VS_CMD_DRAW(num
, info
->index_size
);
254 VS_CMD_ARRAYS_SEMAPHORE_END(info
->index_size
);
260 lima_pack_plbu_cmd(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
262 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
263 struct lima_vs_shader_state
*vs
= ctx
->vs
;
264 unsigned minx
, maxx
, miny
, maxy
;
266 /* If it's zero scissor, we skip adding all other commands */
267 if (lima_is_scissor_zero(ctx
))
270 struct lima_job
*job
= lima_job_get(ctx
);
271 PLBU_CMD_BEGIN(&job
->plbu_cmd_array
, 32);
273 PLBU_CMD_VIEWPORT_LEFT(fui(ctx
->viewport
.left
));
274 PLBU_CMD_VIEWPORT_RIGHT(fui(ctx
->viewport
.right
));
275 PLBU_CMD_VIEWPORT_BOTTOM(fui(ctx
->viewport
.bottom
));
276 PLBU_CMD_VIEWPORT_TOP(fui(ctx
->viewport
.top
));
278 if (!info
->index_size
)
279 PLBU_CMD_ARRAYS_SEMAPHORE_BEGIN();
281 int cf
= ctx
->rasterizer
->base
.cull_face
;
282 int ccw
= ctx
->rasterizer
->base
.front_ccw
;
284 bool force_point_size
= false;
286 if (cf
!= PIPE_FACE_NONE
) {
287 if (cf
& PIPE_FACE_FRONT
)
288 cull
|= ccw
? 0x00040000 : 0x00020000;
289 if (cf
& PIPE_FACE_BACK
)
290 cull
|= ccw
? 0x00020000 : 0x00040000;
293 /* Specify point size with PLBU command if shader doesn't write */
294 if (info
->mode
== PIPE_PRIM_POINTS
&& ctx
->vs
->point_size_idx
== -1)
295 force_point_size
= true;
297 /* Specify line width with PLBU command for lines */
298 if (info
->mode
> PIPE_PRIM_POINTS
&& info
->mode
< PIPE_PRIM_TRIANGLES
)
299 force_point_size
= true;
301 PLBU_CMD_PRIMITIVE_SETUP(force_point_size
, cull
, info
->index_size
);
303 PLBU_CMD_RSW_VERTEX_ARRAY(
304 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
),
308 * - we should set it only for the first draw that enabled the scissor and for
309 * latter draw only if scissor is dirty
311 if (ctx
->rasterizer
->base
.scissor
) {
312 struct pipe_scissor_state
*scissor
= &ctx
->scissor
;
313 minx
= scissor
->minx
;
314 maxx
= scissor
->maxx
;
315 miny
= scissor
->miny
;
316 maxy
= scissor
->maxy
;
319 maxx
= fb
->base
.width
;
321 maxy
= fb
->base
.height
;
324 minx
= MAX2(minx
, ctx
->viewport
.left
);
325 maxx
= MIN2(maxx
, ctx
->viewport
.right
);
326 miny
= MAX2(miny
, ctx
->viewport
.bottom
);
327 maxy
= MIN2(maxy
, ctx
->viewport
.top
);
329 PLBU_CMD_SCISSORS(minx
, maxx
, miny
, maxy
);
330 lima_damage_rect_union(&job
->damage_rect
, minx
, maxx
, miny
, maxy
);
334 PLBU_CMD_DEPTH_RANGE_NEAR(fui(ctx
->viewport
.near
));
335 PLBU_CMD_DEPTH_RANGE_FAR(fui(ctx
->viewport
.far
));
337 if ((info
->mode
== PIPE_PRIM_POINTS
&& ctx
->vs
->point_size_idx
== -1) ||
338 ((info
->mode
>= PIPE_PRIM_LINES
) && (info
->mode
< PIPE_PRIM_TRIANGLES
)))
340 uint32_t v
= info
->mode
== PIPE_PRIM_POINTS
?
341 fui(ctx
->rasterizer
->base
.point_size
) : fui(ctx
->rasterizer
->base
.line_width
);
342 PLBU_CMD_LOW_PRIM_SIZE(v
);
345 if (info
->index_size
) {
346 PLBU_CMD_INDEXED_DEST(ctx
->gp_output
->va
);
347 if (vs
->point_size_idx
!= -1)
348 PLBU_CMD_INDEXED_PT_SIZE(ctx
->gp_output
->va
+ ctx
->gp_output_point_size_offt
);
350 PLBU_CMD_INDICES(ctx
->index_res
->bo
->va
+ info
->start
* info
->index_size
+ ctx
->index_offset
);
353 /* can this make the attribute info static? */
354 PLBU_CMD_DRAW_ARRAYS(info
->mode
, info
->start
, info
->count
);
357 PLBU_CMD_ARRAYS_SEMAPHORE_END();
359 if (info
->index_size
)
360 PLBU_CMD_DRAW_ELEMENTS(info
->mode
, ctx
->min_index
, info
->count
);
366 lima_blend_func(enum pipe_blend_func pipe
)
371 case PIPE_BLEND_SUBTRACT
:
373 case PIPE_BLEND_REVERSE_SUBTRACT
:
384 lima_blend_factor_has_alpha(enum pipe_blendfactor pipe
)
386 /* Bit 4 is set if the blendfactor uses alpha */
388 case PIPE_BLENDFACTOR_SRC_ALPHA
:
389 case PIPE_BLENDFACTOR_DST_ALPHA
:
390 case PIPE_BLENDFACTOR_CONST_ALPHA
:
391 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
392 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
393 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
396 case PIPE_BLENDFACTOR_SRC_COLOR
:
397 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
398 case PIPE_BLENDFACTOR_DST_COLOR
:
399 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
400 case PIPE_BLENDFACTOR_CONST_COLOR
:
401 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
402 case PIPE_BLENDFACTOR_ZERO
:
403 case PIPE_BLENDFACTOR_ONE
:
404 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
407 case PIPE_BLENDFACTOR_SRC1_COLOR
:
408 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
409 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
410 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
411 return -1; /* not supported */
417 lima_blend_factor_is_inv(enum pipe_blendfactor pipe
)
419 /* Bit 3 is set if the blendfactor type is inverted */
421 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
422 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
423 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
424 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
425 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
426 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
427 case PIPE_BLENDFACTOR_ONE
:
430 case PIPE_BLENDFACTOR_SRC_COLOR
:
431 case PIPE_BLENDFACTOR_SRC_ALPHA
:
432 case PIPE_BLENDFACTOR_DST_COLOR
:
433 case PIPE_BLENDFACTOR_DST_ALPHA
:
434 case PIPE_BLENDFACTOR_CONST_COLOR
:
435 case PIPE_BLENDFACTOR_CONST_ALPHA
:
436 case PIPE_BLENDFACTOR_ZERO
:
437 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
440 case PIPE_BLENDFACTOR_SRC1_COLOR
:
441 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
442 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
443 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
444 return -1; /* not supported */
450 lima_blend_factor(enum pipe_blendfactor pipe
)
452 /* Bits 0-2 indicate the blendfactor type */
454 case PIPE_BLENDFACTOR_SRC_COLOR
:
455 case PIPE_BLENDFACTOR_SRC_ALPHA
:
456 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
457 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
460 case PIPE_BLENDFACTOR_DST_COLOR
:
461 case PIPE_BLENDFACTOR_DST_ALPHA
:
462 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
463 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
466 case PIPE_BLENDFACTOR_CONST_COLOR
:
467 case PIPE_BLENDFACTOR_CONST_ALPHA
:
468 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
469 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
472 case PIPE_BLENDFACTOR_ZERO
:
473 case PIPE_BLENDFACTOR_ONE
:
476 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
479 case PIPE_BLENDFACTOR_SRC1_COLOR
:
480 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
481 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
482 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
483 return -1; /* not supported */
489 lima_calculate_alpha_blend(enum pipe_blend_func rgb_func
, enum pipe_blend_func alpha_func
,
490 enum pipe_blendfactor rgb_src_factor
, enum pipe_blendfactor rgb_dst_factor
,
491 enum pipe_blendfactor alpha_src_factor
, enum pipe_blendfactor alpha_dst_factor
)
493 /* PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE has to be changed to PIPE_BLENDFACTOR_ONE
494 * if it is set for alpha_src.
496 if (alpha_src_factor
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
)
497 alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
499 return lima_blend_func(rgb_func
) |
500 (lima_blend_func(alpha_func
) << 3) |
502 (lima_blend_factor(rgb_src_factor
) << 6) |
503 (lima_blend_factor_is_inv(rgb_src_factor
) << 9) |
504 (lima_blend_factor_has_alpha(rgb_src_factor
) << 10) |
506 (lima_blend_factor(rgb_dst_factor
) << 11) |
507 (lima_blend_factor_is_inv(rgb_dst_factor
) << 14) |
508 (lima_blend_factor_has_alpha(rgb_dst_factor
) << 15) |
510 (lima_blend_factor(alpha_src_factor
) << 16) |
511 (lima_blend_factor_is_inv(alpha_src_factor
) << 19) |
513 (lima_blend_factor(alpha_dst_factor
) << 20) |
514 (lima_blend_factor_is_inv(alpha_dst_factor
) << 23) |
515 0x0C000000; /* need to check if this is GLESv1 glAlphaFunc */
519 lima_stencil_op(enum pipe_stencil_op pipe
)
522 case PIPE_STENCIL_OP_KEEP
:
524 case PIPE_STENCIL_OP_ZERO
:
526 case PIPE_STENCIL_OP_REPLACE
:
528 case PIPE_STENCIL_OP_INCR
:
530 case PIPE_STENCIL_OP_DECR
:
532 case PIPE_STENCIL_OP_INCR_WRAP
:
534 case PIPE_STENCIL_OP_DECR_WRAP
:
536 case PIPE_STENCIL_OP_INVERT
:
543 lima_calculate_depth_test(struct pipe_depth_state
*depth
, struct pipe_rasterizer_state
*rst
)
545 int offset_scale
= 0, offset_units
= 0;
546 enum pipe_compare_func func
= (depth
->enabled
? depth
->func
: PIPE_FUNC_ALWAYS
);
548 offset_scale
= CLAMP(rst
->offset_scale
* 4, -128, 127);
549 if (offset_scale
< 0)
550 offset_scale
+= 0x100;
552 offset_units
= CLAMP(rst
->offset_units
* 2, -128, 127);
553 if (offset_units
< 0)
554 offset_units
+= 0x100;
556 return (depth
->enabled
&& depth
->writemask
) |
558 (offset_scale
<< 16) |
559 (offset_units
<< 24) |
560 0x30; /* find out what is this */
564 lima_pack_render_state(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
566 struct lima_fs_shader_state
*fs
= ctx
->fs
;
567 struct lima_render_state
*render
=
568 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_plb_rsw
,
571 bool pixel_kill
= true;
573 /* do hw support RGBA independ blend?
574 * PIPE_CAP_INDEP_BLEND_ENABLE
576 * how to handle the no cbuf only zbuf case?
578 struct pipe_rt_blend_state
*rt
= ctx
->blend
->base
.rt
;
579 render
->blend_color_bg
= float_to_ubyte(ctx
->blend_color
.color
[2]) |
580 (float_to_ubyte(ctx
->blend_color
.color
[1]) << 16);
581 render
->blend_color_ra
= float_to_ubyte(ctx
->blend_color
.color
[0]) |
582 (float_to_ubyte(ctx
->blend_color
.color
[3]) << 16);
584 if (rt
->blend_enable
) {
585 render
->alpha_blend
= lima_calculate_alpha_blend(rt
->rgb_func
, rt
->alpha_func
,
586 rt
->rgb_src_factor
, rt
->rgb_dst_factor
,
587 rt
->alpha_src_factor
, rt
->alpha_dst_factor
);
591 * Special handling for blending disabled.
592 * Binary driver is generating the same alpha_value,
593 * as when we would just enable blending, without changing/setting any blend equation/params.
594 * Normaly in this case mesa would set all rt fields (func/factor) to zero.
596 render
->alpha_blend
= lima_calculate_alpha_blend(PIPE_BLEND_ADD
, PIPE_BLEND_ADD
,
597 PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_ZERO
,
598 PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_ZERO
);
601 render
->alpha_blend
|= (rt
->colormask
& PIPE_MASK_RGBA
) << 28;
603 struct pipe_rasterizer_state
*rst
= &ctx
->rasterizer
->base
;
604 struct pipe_depth_state
*depth
= &ctx
->zsa
->base
.depth
;
605 render
->depth_test
= lima_calculate_depth_test(depth
, rst
);
609 near
= float_to_ushort(ctx
->viewport
.near
);
610 far
= float_to_ushort(ctx
->viewport
.far
);
612 /* Subtract epsilon from 'near' if far == near. Make sure we don't get overflow */
613 if ((far
== near
) && (near
!= 0))
616 /* overlap with plbu? any place can remove one? */
617 render
->depth_range
= near
| (far
<< 16);
619 struct pipe_stencil_state
*stencil
= ctx
->zsa
->base
.stencil
;
620 struct pipe_stencil_ref
*ref
= &ctx
->stencil_ref
;
622 if (stencil
[0].enabled
) { /* stencil is enabled */
623 render
->stencil_front
= stencil
[0].func
|
624 (lima_stencil_op(stencil
[0].fail_op
) << 3) |
625 (lima_stencil_op(stencil
[0].zfail_op
) << 6) |
626 (lima_stencil_op(stencil
[0].zpass_op
) << 9) |
627 (ref
->ref_value
[0] << 16) |
628 (stencil
[0].valuemask
<< 24);
629 render
->stencil_back
= render
->stencil_front
;
630 render
->stencil_test
= (stencil
[0].writemask
& 0xff) | (stencil
[0].writemask
& 0xff) << 8;
631 if (stencil
[1].enabled
) { /* two-side is enabled */
632 render
->stencil_back
= stencil
[1].func
|
633 (lima_stencil_op(stencil
[1].fail_op
) << 3) |
634 (lima_stencil_op(stencil
[1].zfail_op
) << 6) |
635 (lima_stencil_op(stencil
[1].zpass_op
) << 9) |
636 (ref
->ref_value
[1] << 16) |
637 (stencil
[1].valuemask
<< 24);
638 render
->stencil_test
= (stencil
[0].writemask
& 0xff) | (stencil
[1].writemask
& 0xff) << 8;
640 /* TODO: Find out, what (render->stecil_test & 0xffff0000) is.
641 * 0x00ff0000 is probably (float_to_ubyte(alpha->ref_value) << 16)
642 * (render->multi_sample & 0x00000007 is probably the compare function
643 * of glAlphaFunc then.
647 /* Default values, when stencil is disabled:
648 * stencil[0|1].valuemask = 0xff
649 * stencil[0|1].func = PIPE_FUNC_ALWAYS
650 * stencil[0|1].writemask = 0xff
652 render
->stencil_front
= 0xff000007;
653 render
->stencil_back
= 0xff000007;
654 render
->stencil_test
= 0x0000ffff;
657 /* need more investigation */
658 if (info
->mode
== PIPE_PRIM_POINTS
)
659 render
->multi_sample
= 0x0000F007;
660 else if (info
->mode
< PIPE_PRIM_TRIANGLES
)
661 render
->multi_sample
= 0x0000F407;
663 render
->multi_sample
= 0x0000F807;
664 if (ctx
->framebuffer
.base
.samples
)
665 render
->multi_sample
|= 0x68;
667 render
->shader_address
=
668 ctx
->fs
->bo
->va
| (((uint32_t *)ctx
->fs
->bo
->map
)[0] & 0x1F);
670 /* seems not needed */
671 render
->uniforms_address
= 0x00000000;
673 render
->textures_address
= 0x00000000;
675 render
->aux0
= (ctx
->vs
->varying_stride
>> 3);
676 render
->aux1
= 0x00001000;
677 if (ctx
->blend
->base
.dither
)
678 render
->aux1
|= 0x00002000;
680 if (fs
->uses_discard
) {
685 if (rt
->blend_enable
)
688 if ((rt
->colormask
& PIPE_MASK_RGBA
) != PIPE_MASK_RGBA
)
692 render
->aux0
|= 0x300;
695 render
->aux0
|= 0x1000;
697 if (ctx
->tex_stateobj
.num_samplers
) {
698 render
->textures_address
=
699 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_tex_desc
);
700 render
->aux0
|= ctx
->tex_stateobj
.num_samplers
<< 14;
701 render
->aux0
|= 0x20;
704 if (ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].buffer
) {
705 render
->uniforms_address
=
706 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform_array
);
707 uint32_t size
= ctx
->buffer_state
[lima_ctx_buff_pp_uniform
].size
;
710 bits
= util_last_bit(size
>> 3) - 1;
711 bits
+= size
& u_bit_consecutive(0, bits
+ 3) ? 1 : 0;
713 render
->uniforms_address
|= bits
> 0xf ? 0xf : bits
;
715 render
->aux0
|= 0x80;
716 render
->aux1
|= 0x10000;
719 if (ctx
->vs
->num_varyings
) {
720 render
->varying_types
= 0x00000000;
721 render
->varyings_address
= ctx
->gp_output
->va
+
722 ctx
->gp_output_varyings_offt
;
723 for (int i
= 0, index
= 0; i
< ctx
->vs
->num_outputs
; i
++) {
726 if (i
== ctx
->vs
->gl_pos_idx
||
727 i
== ctx
->vs
->point_size_idx
)
730 struct lima_varying_info
*v
= ctx
->vs
->varying
+ i
;
731 if (v
->component_size
== 4)
732 val
= v
->components
> 2 ? 0 : 1;
734 val
= v
->components
> 2 ? 2 : 3;
737 render
->varying_types
|= val
<< (3 * index
);
738 else if (index
== 10) {
739 render
->varying_types
|= val
<< 30;
740 render
->varyings_address
|= val
>> 2;
742 else if (index
== 11)
743 render
->varyings_address
|= val
<< 1;
749 render
->varying_types
= 0x00000000;
750 render
->varyings_address
= 0x00000000;
753 struct lima_job
*job
= lima_job_get(ctx
);
755 lima_dump_command_stream_print(
756 job
->dump
, render
, sizeof(*render
),
757 false, "add render state at va %x\n",
758 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
));
760 lima_dump_rsw_command_stream_print(
761 job
->dump
, render
, sizeof(*render
),
762 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
));
766 lima_update_gp_attribute_info(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
768 struct lima_job
*job
= lima_job_get(ctx
);
769 struct lima_vertex_element_state
*ve
= ctx
->vertex_elements
;
770 struct lima_context_vertex_buffer
*vb
= &ctx
->vertex_buffers
;
772 uint32_t *attribute
=
773 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_attribute_info
,
774 MAX2(1, ve
->num_elements
) * 8);
777 for (int i
= 0; i
< ve
->num_elements
; i
++) {
778 struct pipe_vertex_element
*pve
= ve
->pipe
+ i
;
780 assert(pve
->vertex_buffer_index
< vb
->count
);
781 assert(vb
->enabled_mask
& (1 << pve
->vertex_buffer_index
));
783 struct pipe_vertex_buffer
*pvb
= vb
->vb
+ pve
->vertex_buffer_index
;
784 struct lima_resource
*res
= lima_resource(pvb
->buffer
.resource
);
786 lima_job_add_bo(job
, LIMA_PIPE_GP
, res
->bo
, LIMA_SUBMIT_BO_READ
);
788 unsigned start
= info
->index_size
? (ctx
->min_index
+ info
->index_bias
) : info
->start
;
789 attribute
[n
++] = res
->bo
->va
+ pvb
->buffer_offset
+ pve
->src_offset
790 + start
* pvb
->stride
;
791 attribute
[n
++] = (pvb
->stride
<< 11) |
792 (lima_pipe_format_to_attrib_type(pve
->src_format
) << 2) |
793 (util_format_get_nr_components(pve
->src_format
) - 1);
796 lima_dump_command_stream_print(
797 job
->dump
, attribute
, n
* 4, false, "update attribute info at va %x\n",
798 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_attribute_info
));
802 lima_update_gp_uniform(struct lima_context
*ctx
)
804 struct lima_context_constant_buffer
*ccb
=
805 ctx
->const_buffer
+ PIPE_SHADER_VERTEX
;
806 struct lima_vs_shader_state
*vs
= ctx
->vs
;
808 int size
= vs
->uniform_pending_offset
+ vs
->constant_size
+ 32;
809 void *vs_const_buff
=
810 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_uniform
, size
);
813 memcpy(vs_const_buff
, ccb
->buffer
, ccb
->size
);
815 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
,
816 ctx
->viewport
.transform
.scale
,
817 sizeof(ctx
->viewport
.transform
.scale
));
818 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
+ 16,
819 ctx
->viewport
.transform
.translate
,
820 sizeof(ctx
->viewport
.transform
.translate
));
823 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
+ 32,
824 vs
->constant
, vs
->constant_size
);
826 struct lima_job
*job
= lima_job_get(ctx
);
828 if (lima_debug
& LIMA_DEBUG_GP
) {
829 float *vs_const_buff_f
= vs_const_buff
;
830 printf("gp uniforms:\n");
831 for (int i
= 0; i
< (size
/ sizeof(float)); i
++) {
833 printf("%4d:", i
/ 4);
834 printf(" %8.4f", vs_const_buff_f
[i
]);
841 lima_dump_command_stream_print(
842 job
->dump
, vs_const_buff
, size
, true,
843 "update gp uniform at va %x\n",
844 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_uniform
));
848 lima_update_pp_uniform(struct lima_context
*ctx
)
850 const float *const_buff
= ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].buffer
;
851 size_t const_buff_size
= ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].size
/ sizeof(float);
856 uint16_t *fp16_const_buff
=
857 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_uniform
,
858 const_buff_size
* sizeof(uint16_t));
861 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_uniform_array
, 4);
863 for (int i
= 0; i
< const_buff_size
; i
++)
864 fp16_const_buff
[i
] = util_float_to_half(const_buff
[i
]);
866 *array
= lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform
);
868 struct lima_job
*job
= lima_job_get(ctx
);
870 lima_dump_command_stream_print(
871 job
->dump
, fp16_const_buff
, const_buff_size
* 2,
872 false, "add pp uniform data at va %x\n",
873 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform
));
874 lima_dump_command_stream_print(
875 job
->dump
, array
, 4, false, "add pp uniform info at va %x\n",
876 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform_array
));
880 lima_update_varying(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
882 struct lima_job
*job
= lima_job_get(ctx
);
883 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
884 struct lima_vs_shader_state
*vs
= ctx
->vs
;
885 uint32_t gp_output_size
;
886 unsigned num
= info
->index_size
? (ctx
->max_index
- ctx
->min_index
+ 1) : info
->count
;
889 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_varying_info
,
890 vs
->num_outputs
* 8);
895 for (int i
= 0; i
< vs
->num_outputs
; i
++) {
896 struct lima_varying_info
*v
= vs
->varying
+ i
;
898 if (i
== vs
->gl_pos_idx
||
899 i
== vs
->point_size_idx
)
902 int size
= v
->component_size
* 4;
904 /* does component_size == 2 need to be 16 aligned? */
905 if (v
->component_size
== 4)
906 offset
= align(offset
, 16);
912 vs
->varying_stride
= align(offset
, 16);
914 /* gl_Position is always present, allocate space for it */
915 gp_output_size
= align(4 * 4 * num
, 0x40);
917 /* Allocate space for varyings if there're any */
918 if (vs
->num_varyings
) {
919 ctx
->gp_output_varyings_offt
= gp_output_size
;
920 gp_output_size
+= align(vs
->varying_stride
* num
, 0x40);
923 /* Allocate space for gl_PointSize if it's there */
924 if (vs
->point_size_idx
!= -1) {
925 ctx
->gp_output_point_size_offt
= gp_output_size
;
926 gp_output_size
+= 4 * num
;
929 /* gp_output can be too large for the suballocator, so create a
930 * separate bo for it. The bo cache should prevent performance hit.
932 ctx
->gp_output
= lima_bo_create(screen
, gp_output_size
, 0);
933 assert(ctx
->gp_output
);
934 lima_job_add_bo(job
, LIMA_PIPE_GP
, ctx
->gp_output
, LIMA_SUBMIT_BO_WRITE
);
935 lima_job_add_bo(job
, LIMA_PIPE_PP
, ctx
->gp_output
, LIMA_SUBMIT_BO_READ
);
937 for (int i
= 0; i
< vs
->num_outputs
; i
++) {
938 struct lima_varying_info
*v
= vs
->varying
+ i
;
940 if (i
== vs
->gl_pos_idx
) {
942 varying
[n
++] = ctx
->gp_output
->va
;
943 varying
[n
++] = 0x8020;
944 } else if (i
== vs
->point_size_idx
) {
946 varying
[n
++] = ctx
->gp_output
->va
+ ctx
->gp_output_point_size_offt
;
947 varying
[n
++] = 0x2021;
950 varying
[n
++] = ctx
->gp_output
->va
+ ctx
->gp_output_varyings_offt
+
952 varying
[n
++] = (vs
->varying_stride
<< 11) | (v
->components
- 1) |
953 (v
->component_size
== 2 ? 0x0C : 0);
957 lima_dump_command_stream_print(
958 job
->dump
, varying
, n
* 4, false, "update varying info at va %x\n",
959 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_varying_info
));
963 lima_draw_vbo_update(struct pipe_context
*pctx
,
964 const struct pipe_draw_info
*info
)
966 struct lima_context
*ctx
= lima_context(pctx
);
967 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
968 unsigned buffers
= 0;
970 if (fb
->base
.zsbuf
) {
971 if (ctx
->zsa
->base
.depth
.enabled
)
972 buffers
|= PIPE_CLEAR_DEPTH
;
973 if (ctx
->zsa
->base
.stencil
[0].enabled
||
974 ctx
->zsa
->base
.stencil
[1].enabled
)
975 buffers
|= PIPE_CLEAR_STENCIL
;
978 if (fb
->base
.nr_cbufs
)
979 buffers
|= PIPE_CLEAR_COLOR0
;
981 lima_update_job_wb(ctx
, buffers
);
983 lima_update_gp_attribute_info(ctx
, info
);
985 if ((ctx
->dirty
& LIMA_CONTEXT_DIRTY_CONST_BUFF
&&
986 ctx
->const_buffer
[PIPE_SHADER_VERTEX
].dirty
) ||
987 ctx
->dirty
& LIMA_CONTEXT_DIRTY_VIEWPORT
||
988 ctx
->dirty
& LIMA_CONTEXT_DIRTY_SHADER_VERT
) {
989 lima_update_gp_uniform(ctx
);
990 ctx
->const_buffer
[PIPE_SHADER_VERTEX
].dirty
= false;
993 lima_update_varying(ctx
, info
);
995 /* If it's zero scissor, don't build vs cmd list */
996 if (!lima_is_scissor_zero(ctx
))
997 lima_pack_vs_cmd(ctx
, info
);
999 if (ctx
->dirty
& LIMA_CONTEXT_DIRTY_CONST_BUFF
&&
1000 ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].dirty
) {
1001 lima_update_pp_uniform(ctx
);
1002 ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].dirty
= false;
1005 lima_update_textures(ctx
);
1007 lima_pack_render_state(ctx
, info
);
1008 lima_pack_plbu_cmd(ctx
, info
);
1010 if (ctx
->gp_output
) {
1011 lima_bo_unreference(ctx
->gp_output
); /* held by job */
1012 ctx
->gp_output
= NULL
;
1019 lima_draw_vbo_indexed(struct pipe_context
*pctx
,
1020 const struct pipe_draw_info
*info
)
1022 struct lima_context
*ctx
= lima_context(pctx
);
1023 struct lima_job
*job
= lima_job_get(ctx
);
1024 struct pipe_resource
*indexbuf
= NULL
;
1025 bool needs_indices
= true;
1027 /* Mali Utgard GPU always need min/max index info for index draw,
1028 * compute it if upper layer does not do for us */
1029 if (info
->max_index
!= ~0u) {
1030 ctx
->min_index
= info
->min_index
;
1031 ctx
->max_index
= info
->max_index
;
1032 needs_indices
= false;
1035 if (info
->has_user_indices
) {
1036 util_upload_index_buffer(&ctx
->base
, info
, &indexbuf
, &ctx
->index_offset
, 0x40);
1037 ctx
->index_res
= lima_resource(indexbuf
);
1040 ctx
->index_res
= lima_resource(info
->index
.resource
);
1041 ctx
->index_offset
= 0;
1042 needs_indices
= !panfrost_minmax_cache_get(ctx
->index_res
->index_cache
, info
->start
,
1043 info
->count
, &ctx
->min_index
, &ctx
->max_index
);
1046 if (needs_indices
) {
1047 u_vbuf_get_minmax_index(pctx
, info
, &ctx
->min_index
, &ctx
->max_index
);
1048 if (!info
->has_user_indices
)
1049 panfrost_minmax_cache_add(ctx
->index_res
->index_cache
, info
->start
, info
->count
,
1050 ctx
->min_index
, ctx
->max_index
);
1053 lima_job_add_bo(job
, LIMA_PIPE_GP
, ctx
->index_res
->bo
, LIMA_SUBMIT_BO_READ
);
1054 lima_job_add_bo(job
, LIMA_PIPE_PP
, ctx
->index_res
->bo
, LIMA_SUBMIT_BO_READ
);
1055 lima_draw_vbo_update(pctx
, info
);
1058 pipe_resource_reference(&indexbuf
, NULL
);
1062 lima_draw_vbo_count(struct pipe_context
*pctx
,
1063 const struct pipe_draw_info
*info
)
1065 static const uint32_t max_verts
= 65535;
1067 struct pipe_draw_info local_info
= *info
;
1068 unsigned start
= info
->start
;
1069 unsigned count
= info
->count
;
1072 unsigned this_count
= count
;
1075 u_split_draw(info
, max_verts
, &this_count
, &step
);
1077 local_info
.start
= start
;
1078 local_info
.count
= this_count
;
1080 lima_draw_vbo_update(pctx
, &local_info
);
1088 lima_draw_vbo(struct pipe_context
*pctx
,
1089 const struct pipe_draw_info
*info
)
1091 /* check if draw mode and vertex/index count match,
1092 * otherwise gp will hang */
1093 if (!u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
)) {
1094 debug_printf("draw mode and vertex/index count mismatch\n");
1098 struct lima_context
*ctx
= lima_context(pctx
);
1100 if (!ctx
->vs
|| !ctx
->fs
) {
1101 debug_warn_once("no shader, skip draw\n");
1105 if (!lima_update_vs_state(ctx
) || !lima_update_fs_state(ctx
))
1108 struct lima_job
*job
= lima_job_get(ctx
);
1110 lima_dump_command_stream_print(
1111 job
->dump
, ctx
->vs
->bo
->map
, ctx
->vs
->shader_size
, false,
1112 "add vs at va %x\n", ctx
->vs
->bo
->va
);
1114 lima_dump_command_stream_print(
1115 job
->dump
, ctx
->fs
->bo
->map
, ctx
->fs
->shader_size
, false,
1116 "add fs at va %x\n", ctx
->fs
->bo
->va
);
1118 lima_job_add_bo(job
, LIMA_PIPE_GP
, ctx
->vs
->bo
, LIMA_SUBMIT_BO_READ
);
1119 lima_job_add_bo(job
, LIMA_PIPE_PP
, ctx
->fs
->bo
, LIMA_SUBMIT_BO_READ
);
1121 if (info
->index_size
)
1122 lima_draw_vbo_indexed(pctx
, info
);
1124 lima_draw_vbo_count(pctx
, info
);
1128 lima_draw_init(struct lima_context
*ctx
)
1130 ctx
->base
.clear
= lima_clear
;
1131 ctx
->base
.draw_vbo
= lima_draw_vbo
;