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"
43 #include "lima_submit.h"
44 #include "lima_texture.h"
45 #include "lima_util.h"
48 #include <drm-uapi/lima_drm.h>
51 lima_is_scissor_zero(struct lima_context
*ctx
)
53 if (!ctx
->rasterizer
|| !ctx
->rasterizer
->base
.scissor
)
56 struct pipe_scissor_state
*scissor
= &ctx
->scissor
;
58 scissor
->minx
== scissor
->maxx
59 && scissor
->miny
== scissor
->maxy
;
63 lima_update_submit_wb(struct lima_context
*ctx
, unsigned buffers
)
65 struct lima_submit
*submit
= lima_submit_get(ctx
);
66 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
68 /* add to submit when the buffer is dirty and resolve is clear (not added before) */
69 if (fb
->base
.nr_cbufs
&& (buffers
& PIPE_CLEAR_COLOR0
) &&
70 !(submit
->resolve
& PIPE_CLEAR_COLOR0
)) {
71 struct lima_resource
*res
= lima_resource(fb
->base
.cbufs
[0]->texture
);
72 lima_flush_submit_accessing_bo(ctx
, res
->bo
, true);
73 _mesa_hash_table_insert(ctx
->write_submits
, &res
->base
, submit
);
74 lima_submit_add_bo(submit
, LIMA_PIPE_PP
, res
->bo
, LIMA_SUBMIT_BO_WRITE
);
77 /* add to submit when the buffer is dirty and resolve is clear (not added before) */
78 if (fb
->base
.zsbuf
&& (buffers
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
)) &&
79 !(submit
->resolve
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
))) {
80 struct lima_resource
*res
= lima_resource(fb
->base
.zsbuf
->texture
);
81 lima_flush_submit_accessing_bo(ctx
, res
->bo
, true);
82 _mesa_hash_table_insert(ctx
->write_submits
, &res
->base
, submit
);
83 lima_submit_add_bo(submit
, LIMA_PIPE_PP
, res
->bo
, LIMA_SUBMIT_BO_WRITE
);
86 submit
->resolve
|= buffers
;
90 lima_damage_rect_union(struct pipe_scissor_state
*rect
,
91 unsigned minx
, unsigned maxx
,
92 unsigned miny
, unsigned maxy
)
94 rect
->minx
= MIN2(rect
->minx
, minx
);
95 rect
->miny
= MIN2(rect
->miny
, miny
);
96 rect
->maxx
= MAX2(rect
->maxx
, maxx
);
97 rect
->maxy
= MAX2(rect
->maxy
, maxy
);
101 lima_clear(struct pipe_context
*pctx
, unsigned buffers
,
102 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
104 struct lima_context
*ctx
= lima_context(pctx
);
105 struct lima_submit
*submit
= lima_submit_get(ctx
);
107 /* flush if this submit already contains any draw, otherwise multi clear can be
108 * combined into a single submit */
109 if (lima_submit_has_draw_pending(submit
)) {
110 lima_do_submit(submit
);
111 submit
= lima_submit_get(ctx
);
114 lima_update_submit_wb(ctx
, buffers
);
116 /* no need to reload if cleared */
117 if (ctx
->framebuffer
.base
.nr_cbufs
&& (buffers
& PIPE_CLEAR_COLOR0
)) {
118 struct lima_surface
*surf
= lima_surface(ctx
->framebuffer
.base
.cbufs
[0]);
119 surf
->reload
= false;
122 struct lima_submit_clear
*clear
= &submit
->clear
;
123 clear
->buffers
= buffers
;
125 if (buffers
& PIPE_CLEAR_COLOR0
) {
127 ((uint32_t)float_to_ubyte(color
->f
[3]) << 24) |
128 ((uint32_t)float_to_ubyte(color
->f
[2]) << 16) |
129 ((uint32_t)float_to_ubyte(color
->f
[1]) << 8) |
130 float_to_ubyte(color
->f
[0]);
133 ((uint64_t)float_to_ushort(color
->f
[3]) << 48) |
134 ((uint64_t)float_to_ushort(color
->f
[2]) << 32) |
135 ((uint64_t)float_to_ushort(color
->f
[1]) << 16) |
136 float_to_ushort(color
->f
[0]);
139 if (buffers
& PIPE_CLEAR_DEPTH
)
140 clear
->depth
= util_pack_z(PIPE_FORMAT_Z24X8_UNORM
, depth
);
142 if (buffers
& PIPE_CLEAR_STENCIL
)
143 clear
->stencil
= stencil
;
145 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_CLEAR
;
147 lima_damage_rect_union(&submit
->damage_rect
,
148 0, ctx
->framebuffer
.base
.width
,
149 0, ctx
->framebuffer
.base
.height
);
152 enum lima_attrib_type
{
153 LIMA_ATTRIB_FLOAT
= 0x000,
154 /* todo: find out what lives here. */
155 LIMA_ATTRIB_I16
= 0x004,
156 LIMA_ATTRIB_U16
= 0x005,
157 LIMA_ATTRIB_I8
= 0x006,
158 LIMA_ATTRIB_U8
= 0x007,
159 LIMA_ATTRIB_I8N
= 0x008,
160 LIMA_ATTRIB_U8N
= 0x009,
161 LIMA_ATTRIB_I16N
= 0x00A,
162 LIMA_ATTRIB_U16N
= 0x00B,
163 /* todo: where is the 32 int */
164 /* todo: find out what lives here. */
165 LIMA_ATTRIB_FIXED
= 0x101
168 static enum lima_attrib_type
169 lima_pipe_format_to_attrib_type(enum pipe_format format
)
171 const struct util_format_description
*desc
= util_format_description(format
);
172 int i
= util_format_get_first_non_void_channel(format
);
173 const struct util_format_channel_description
*c
= desc
->channel
+ i
;
176 case UTIL_FORMAT_TYPE_FLOAT
:
177 return LIMA_ATTRIB_FLOAT
;
178 case UTIL_FORMAT_TYPE_FIXED
:
179 return LIMA_ATTRIB_FIXED
;
180 case UTIL_FORMAT_TYPE_SIGNED
:
183 return LIMA_ATTRIB_I8N
;
185 return LIMA_ATTRIB_I8
;
187 else if (c
->size
== 16) {
189 return LIMA_ATTRIB_I16N
;
191 return LIMA_ATTRIB_I16
;
194 case UTIL_FORMAT_TYPE_UNSIGNED
:
197 return LIMA_ATTRIB_U8N
;
199 return LIMA_ATTRIB_U8
;
201 else if (c
->size
== 16) {
203 return LIMA_ATTRIB_U16N
;
205 return LIMA_ATTRIB_U16
;
210 return LIMA_ATTRIB_FLOAT
;
214 lima_pack_vs_cmd(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
216 struct lima_submit
*submit
= lima_submit_get(ctx
);
218 VS_CMD_BEGIN(&submit
->vs_cmd_array
, 24);
220 if (!info
->index_size
) {
221 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_1();
222 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_2();
225 int uniform_size
= ctx
->vs
->uniform_pending_offset
+ ctx
->vs
->constant_size
+ 32;
226 VS_CMD_UNIFORMS_ADDRESS(
227 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_uniform
),
228 align(uniform_size
, 16));
230 VS_CMD_SHADER_ADDRESS(ctx
->vs
->bo
->va
, ctx
->vs
->shader_size
);
231 VS_CMD_SHADER_INFO(ctx
->vs
->prefetch
, ctx
->vs
->shader_size
);
233 int num_outputs
= ctx
->vs
->num_outputs
;
234 int num_attributes
= ctx
->vertex_elements
->num_elements
;
235 VS_CMD_VARYING_ATTRIBUTE_COUNT(num_outputs
, MAX2(1, num_attributes
));
239 VS_CMD_ATTRIBUTES_ADDRESS(
240 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_attribute_info
),
241 MAX2(1, num_attributes
));
243 VS_CMD_VARYINGS_ADDRESS(
244 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_varying_info
),
247 unsigned num
= info
->index_size
? (ctx
->max_index
- ctx
->min_index
+ 1) : info
->count
;
248 VS_CMD_DRAW(num
, info
->index_size
);
252 VS_CMD_ARRAYS_SEMAPHORE_END(info
->index_size
);
258 lima_pack_plbu_cmd(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
260 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
261 struct lima_vs_shader_state
*vs
= ctx
->vs
;
262 unsigned minx
, maxx
, miny
, maxy
;
264 /* If it's zero scissor, we skip adding all other commands */
265 if (lima_is_scissor_zero(ctx
))
268 struct lima_submit
*submit
= lima_submit_get(ctx
);
269 PLBU_CMD_BEGIN(&submit
->plbu_cmd_array
, 32);
271 PLBU_CMD_VIEWPORT_LEFT(fui(ctx
->viewport
.left
));
272 PLBU_CMD_VIEWPORT_RIGHT(fui(ctx
->viewport
.right
));
273 PLBU_CMD_VIEWPORT_BOTTOM(fui(ctx
->viewport
.bottom
));
274 PLBU_CMD_VIEWPORT_TOP(fui(ctx
->viewport
.top
));
276 if (!info
->index_size
)
277 PLBU_CMD_ARRAYS_SEMAPHORE_BEGIN();
279 int cf
= ctx
->rasterizer
->base
.cull_face
;
280 int ccw
= ctx
->rasterizer
->base
.front_ccw
;
282 bool force_point_size
= false;
284 if (cf
!= PIPE_FACE_NONE
) {
285 if (cf
& PIPE_FACE_FRONT
)
286 cull
|= ccw
? 0x00040000 : 0x00020000;
287 if (cf
& PIPE_FACE_BACK
)
288 cull
|= ccw
? 0x00020000 : 0x00040000;
291 /* Specify point size with PLBU command if shader doesn't write */
292 if (info
->mode
== PIPE_PRIM_POINTS
&& ctx
->vs
->point_size_idx
== -1)
293 force_point_size
= true;
295 /* Specify line width with PLBU command for lines */
296 if (info
->mode
> PIPE_PRIM_POINTS
&& info
->mode
< PIPE_PRIM_TRIANGLES
)
297 force_point_size
= true;
299 PLBU_CMD_PRIMITIVE_SETUP(force_point_size
, cull
, info
->index_size
);
301 PLBU_CMD_RSW_VERTEX_ARRAY(
302 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
),
306 * - we should set it only for the first draw that enabled the scissor and for
307 * latter draw only if scissor is dirty
309 if (ctx
->rasterizer
->base
.scissor
) {
310 struct pipe_scissor_state
*scissor
= &ctx
->scissor
;
311 minx
= scissor
->minx
;
312 maxx
= scissor
->maxx
;
313 miny
= scissor
->miny
;
314 maxy
= scissor
->maxy
;
317 maxx
= fb
->base
.width
;
319 maxy
= fb
->base
.height
;
322 minx
= MAX2(minx
, ctx
->viewport
.left
);
323 maxx
= MIN2(maxx
, ctx
->viewport
.right
);
324 miny
= MAX2(miny
, ctx
->viewport
.bottom
);
325 maxy
= MIN2(maxy
, ctx
->viewport
.top
);
327 PLBU_CMD_SCISSORS(minx
, maxx
, miny
, maxy
);
328 lima_damage_rect_union(&submit
->damage_rect
, minx
, maxx
, miny
, maxy
);
332 PLBU_CMD_DEPTH_RANGE_NEAR(fui(ctx
->viewport
.near
));
333 PLBU_CMD_DEPTH_RANGE_FAR(fui(ctx
->viewport
.far
));
335 if ((info
->mode
== PIPE_PRIM_POINTS
&& ctx
->vs
->point_size_idx
== -1) ||
336 ((info
->mode
>= PIPE_PRIM_LINES
) && (info
->mode
< PIPE_PRIM_TRIANGLES
)))
338 uint32_t v
= info
->mode
== PIPE_PRIM_POINTS
?
339 fui(ctx
->rasterizer
->base
.point_size
) : fui(ctx
->rasterizer
->base
.line_width
);
340 PLBU_CMD_LOW_PRIM_SIZE(v
);
343 if (info
->index_size
) {
344 PLBU_CMD_INDEXED_DEST(ctx
->gp_output
->va
);
345 if (vs
->point_size_idx
!= -1)
346 PLBU_CMD_INDEXED_PT_SIZE(ctx
->gp_output
->va
+ ctx
->gp_output_point_size_offt
);
348 PLBU_CMD_INDICES(ctx
->index_res
->bo
->va
+ info
->start
* info
->index_size
+ ctx
->index_offset
);
351 /* can this make the attribute info static? */
352 PLBU_CMD_DRAW_ARRAYS(info
->mode
, info
->start
, info
->count
);
355 PLBU_CMD_ARRAYS_SEMAPHORE_END();
357 if (info
->index_size
)
358 PLBU_CMD_DRAW_ELEMENTS(info
->mode
, ctx
->min_index
, info
->count
);
364 lima_blend_func(enum pipe_blend_func pipe
)
369 case PIPE_BLEND_SUBTRACT
:
371 case PIPE_BLEND_REVERSE_SUBTRACT
:
382 lima_blend_factor_has_alpha(enum pipe_blendfactor pipe
)
384 /* Bit 4 is set if the blendfactor uses alpha */
386 case PIPE_BLENDFACTOR_SRC_ALPHA
:
387 case PIPE_BLENDFACTOR_DST_ALPHA
:
388 case PIPE_BLENDFACTOR_CONST_ALPHA
:
389 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
390 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
391 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
394 case PIPE_BLENDFACTOR_SRC_COLOR
:
395 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
396 case PIPE_BLENDFACTOR_DST_COLOR
:
397 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
398 case PIPE_BLENDFACTOR_CONST_COLOR
:
399 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
400 case PIPE_BLENDFACTOR_ZERO
:
401 case PIPE_BLENDFACTOR_ONE
:
402 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
405 case PIPE_BLENDFACTOR_SRC1_COLOR
:
406 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
407 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
408 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
409 return -1; /* not supported */
415 lima_blend_factor_is_inv(enum pipe_blendfactor pipe
)
417 /* Bit 3 is set if the blendfactor type is inverted */
419 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
420 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
421 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
422 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
423 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
424 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
425 case PIPE_BLENDFACTOR_ONE
:
428 case PIPE_BLENDFACTOR_SRC_COLOR
:
429 case PIPE_BLENDFACTOR_SRC_ALPHA
:
430 case PIPE_BLENDFACTOR_DST_COLOR
:
431 case PIPE_BLENDFACTOR_DST_ALPHA
:
432 case PIPE_BLENDFACTOR_CONST_COLOR
:
433 case PIPE_BLENDFACTOR_CONST_ALPHA
:
434 case PIPE_BLENDFACTOR_ZERO
:
435 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
438 case PIPE_BLENDFACTOR_SRC1_COLOR
:
439 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
440 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
441 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
442 return -1; /* not supported */
448 lima_blend_factor(enum pipe_blendfactor pipe
)
450 /* Bits 0-2 indicate the blendfactor type */
452 case PIPE_BLENDFACTOR_SRC_COLOR
:
453 case PIPE_BLENDFACTOR_SRC_ALPHA
:
454 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
455 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
458 case PIPE_BLENDFACTOR_DST_COLOR
:
459 case PIPE_BLENDFACTOR_DST_ALPHA
:
460 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
461 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
464 case PIPE_BLENDFACTOR_CONST_COLOR
:
465 case PIPE_BLENDFACTOR_CONST_ALPHA
:
466 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
467 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
470 case PIPE_BLENDFACTOR_ZERO
:
471 case PIPE_BLENDFACTOR_ONE
:
474 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
477 case PIPE_BLENDFACTOR_SRC1_COLOR
:
478 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
479 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
480 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
481 return -1; /* not supported */
487 lima_calculate_alpha_blend(enum pipe_blend_func rgb_func
, enum pipe_blend_func alpha_func
,
488 enum pipe_blendfactor rgb_src_factor
, enum pipe_blendfactor rgb_dst_factor
,
489 enum pipe_blendfactor alpha_src_factor
, enum pipe_blendfactor alpha_dst_factor
)
491 /* PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE has to be changed to PIPE_BLENDFACTOR_ONE
492 * if it is set for alpha_src.
494 if (alpha_src_factor
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
)
495 alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
497 return lima_blend_func(rgb_func
) |
498 (lima_blend_func(alpha_func
) << 3) |
500 (lima_blend_factor(rgb_src_factor
) << 6) |
501 (lima_blend_factor_is_inv(rgb_src_factor
) << 9) |
502 (lima_blend_factor_has_alpha(rgb_src_factor
) << 10) |
504 (lima_blend_factor(rgb_dst_factor
) << 11) |
505 (lima_blend_factor_is_inv(rgb_dst_factor
) << 14) |
506 (lima_blend_factor_has_alpha(rgb_dst_factor
) << 15) |
508 (lima_blend_factor(alpha_src_factor
) << 16) |
509 (lima_blend_factor_is_inv(alpha_src_factor
) << 19) |
511 (lima_blend_factor(alpha_dst_factor
) << 20) |
512 (lima_blend_factor_is_inv(alpha_dst_factor
) << 23) |
513 0x0C000000; /* need to check if this is GLESv1 glAlphaFunc */
517 lima_stencil_op(enum pipe_stencil_op pipe
)
520 case PIPE_STENCIL_OP_KEEP
:
522 case PIPE_STENCIL_OP_ZERO
:
524 case PIPE_STENCIL_OP_REPLACE
:
526 case PIPE_STENCIL_OP_INCR
:
528 case PIPE_STENCIL_OP_DECR
:
530 case PIPE_STENCIL_OP_INCR_WRAP
:
532 case PIPE_STENCIL_OP_DECR_WRAP
:
534 case PIPE_STENCIL_OP_INVERT
:
541 lima_calculate_depth_test(struct pipe_depth_state
*depth
, struct pipe_rasterizer_state
*rst
)
543 int offset_scale
= 0, offset_units
= 0;
544 enum pipe_compare_func func
= (depth
->enabled
? depth
->func
: PIPE_FUNC_ALWAYS
);
546 offset_scale
= CLAMP(rst
->offset_scale
* 4, -128, 127);
547 if (offset_scale
< 0)
548 offset_scale
+= 0x100;
550 offset_units
= CLAMP(rst
->offset_units
* 2, -128, 127);
551 if (offset_units
< 0)
552 offset_units
+= 0x100;
554 return (depth
->enabled
&& depth
->writemask
) |
556 (offset_scale
<< 16) |
557 (offset_units
<< 24) |
558 0x30; /* find out what is this */
562 lima_pack_render_state(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
564 struct lima_fs_shader_state
*fs
= ctx
->fs
;
565 struct lima_render_state
*render
=
566 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_plb_rsw
,
569 bool pixel_kill
= true;
571 /* do hw support RGBA independ blend?
572 * PIPE_CAP_INDEP_BLEND_ENABLE
574 * how to handle the no cbuf only zbuf case?
576 struct pipe_rt_blend_state
*rt
= ctx
->blend
->base
.rt
;
577 render
->blend_color_bg
= float_to_ubyte(ctx
->blend_color
.color
[2]) |
578 (float_to_ubyte(ctx
->blend_color
.color
[1]) << 16);
579 render
->blend_color_ra
= float_to_ubyte(ctx
->blend_color
.color
[0]) |
580 (float_to_ubyte(ctx
->blend_color
.color
[3]) << 16);
582 if (rt
->blend_enable
) {
583 render
->alpha_blend
= lima_calculate_alpha_blend(rt
->rgb_func
, rt
->alpha_func
,
584 rt
->rgb_src_factor
, rt
->rgb_dst_factor
,
585 rt
->alpha_src_factor
, rt
->alpha_dst_factor
);
589 * Special handling for blending disabled.
590 * Binary driver is generating the same alpha_value,
591 * as when we would just enable blending, without changing/setting any blend equation/params.
592 * Normaly in this case mesa would set all rt fields (func/factor) to zero.
594 render
->alpha_blend
= lima_calculate_alpha_blend(PIPE_BLEND_ADD
, PIPE_BLEND_ADD
,
595 PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_ZERO
,
596 PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_ZERO
);
599 render
->alpha_blend
|= (rt
->colormask
& PIPE_MASK_RGBA
) << 28;
601 struct pipe_rasterizer_state
*rst
= &ctx
->rasterizer
->base
;
602 struct pipe_depth_state
*depth
= &ctx
->zsa
->base
.depth
;
603 render
->depth_test
= lima_calculate_depth_test(depth
, rst
);
607 near
= float_to_ushort(ctx
->viewport
.near
);
608 far
= float_to_ushort(ctx
->viewport
.far
);
610 /* Subtract epsilon from 'near' if far == near. Make sure we don't get overflow */
611 if ((far
== near
) && (near
!= 0))
614 /* overlap with plbu? any place can remove one? */
615 render
->depth_range
= near
| (far
<< 16);
617 struct pipe_stencil_state
*stencil
= ctx
->zsa
->base
.stencil
;
618 struct pipe_stencil_ref
*ref
= &ctx
->stencil_ref
;
620 if (stencil
[0].enabled
) { /* stencil is enabled */
621 render
->stencil_front
= stencil
[0].func
|
622 (lima_stencil_op(stencil
[0].fail_op
) << 3) |
623 (lima_stencil_op(stencil
[0].zfail_op
) << 6) |
624 (lima_stencil_op(stencil
[0].zpass_op
) << 9) |
625 (ref
->ref_value
[0] << 16) |
626 (stencil
[0].valuemask
<< 24);
627 render
->stencil_back
= render
->stencil_front
;
628 render
->stencil_test
= (stencil
[0].writemask
& 0xff) | (stencil
[0].writemask
& 0xff) << 8;
629 if (stencil
[1].enabled
) { /* two-side is enabled */
630 render
->stencil_back
= stencil
[1].func
|
631 (lima_stencil_op(stencil
[1].fail_op
) << 3) |
632 (lima_stencil_op(stencil
[1].zfail_op
) << 6) |
633 (lima_stencil_op(stencil
[1].zpass_op
) << 9) |
634 (ref
->ref_value
[1] << 16) |
635 (stencil
[1].valuemask
<< 24);
636 render
->stencil_test
= (stencil
[0].writemask
& 0xff) | (stencil
[1].writemask
& 0xff) << 8;
638 /* TODO: Find out, what (render->stecil_test & 0xffff0000) is.
639 * 0x00ff0000 is probably (float_to_ubyte(alpha->ref_value) << 16)
640 * (render->multi_sample & 0x00000007 is probably the compare function
641 * of glAlphaFunc then.
645 /* Default values, when stencil is disabled:
646 * stencil[0|1].valuemask = 0xff
647 * stencil[0|1].func = PIPE_FUNC_ALWAYS
648 * stencil[0|1].writemask = 0xff
650 render
->stencil_front
= 0xff000007;
651 render
->stencil_back
= 0xff000007;
652 render
->stencil_test
= 0x0000ffff;
655 /* need more investigation */
656 if (info
->mode
== PIPE_PRIM_POINTS
)
657 render
->multi_sample
= 0x0000F007;
658 else if (info
->mode
< PIPE_PRIM_TRIANGLES
)
659 render
->multi_sample
= 0x0000F407;
661 render
->multi_sample
= 0x0000F807;
662 if (ctx
->framebuffer
.base
.samples
)
663 render
->multi_sample
|= 0x68;
665 render
->shader_address
=
666 ctx
->fs
->bo
->va
| (((uint32_t *)ctx
->fs
->bo
->map
)[0] & 0x1F);
668 /* seems not needed */
669 render
->uniforms_address
= 0x00000000;
671 render
->textures_address
= 0x00000000;
673 render
->aux0
= (ctx
->vs
->varying_stride
>> 3);
674 render
->aux1
= 0x00001000;
675 if (ctx
->blend
->base
.dither
)
676 render
->aux1
|= 0x00002000;
678 if (fs
->uses_discard
) {
683 if (rt
->blend_enable
)
686 if ((rt
->colormask
& PIPE_MASK_RGBA
) != PIPE_MASK_RGBA
)
690 render
->aux0
|= 0x300;
693 render
->aux0
|= 0x1000;
695 if (ctx
->tex_stateobj
.num_samplers
) {
696 render
->textures_address
=
697 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_tex_desc
);
698 render
->aux0
|= ctx
->tex_stateobj
.num_samplers
<< 14;
699 render
->aux0
|= 0x20;
702 if (ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].buffer
) {
703 render
->uniforms_address
=
704 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform_array
);
705 uint32_t size
= ctx
->buffer_state
[lima_ctx_buff_pp_uniform
].size
;
708 bits
= util_last_bit(size
>> 3) - 1;
709 bits
+= size
& u_bit_consecutive(0, bits
+ 3) ? 1 : 0;
711 render
->uniforms_address
|= bits
> 0xf ? 0xf : bits
;
713 render
->aux0
|= 0x80;
714 render
->aux1
|= 0x10000;
717 if (ctx
->vs
->num_varyings
) {
718 render
->varying_types
= 0x00000000;
719 render
->varyings_address
= ctx
->gp_output
->va
+
720 ctx
->gp_output_varyings_offt
;
721 for (int i
= 0, index
= 0; i
< ctx
->vs
->num_outputs
; i
++) {
724 if (i
== ctx
->vs
->gl_pos_idx
||
725 i
== ctx
->vs
->point_size_idx
)
728 struct lima_varying_info
*v
= ctx
->vs
->varying
+ i
;
729 if (v
->component_size
== 4)
730 val
= v
->components
> 2 ? 0 : 1;
732 val
= v
->components
> 2 ? 2 : 3;
735 render
->varying_types
|= val
<< (3 * index
);
736 else if (index
== 10) {
737 render
->varying_types
|= val
<< 30;
738 render
->varyings_address
|= val
>> 2;
740 else if (index
== 11)
741 render
->varyings_address
|= val
<< 1;
747 render
->varying_types
= 0x00000000;
748 render
->varyings_address
= 0x00000000;
751 struct lima_submit
*submit
= lima_submit_get(ctx
);
753 lima_dump_command_stream_print(
754 submit
->dump
, render
, sizeof(*render
),
755 false, "add render state at va %x\n",
756 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
));
758 lima_dump_rsw_command_stream_print(
759 submit
->dump
, render
, sizeof(*render
),
760 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
));
764 lima_update_gp_attribute_info(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
766 struct lima_submit
*submit
= lima_submit_get(ctx
);
767 struct lima_vertex_element_state
*ve
= ctx
->vertex_elements
;
768 struct lima_context_vertex_buffer
*vb
= &ctx
->vertex_buffers
;
770 uint32_t *attribute
=
771 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_attribute_info
,
772 MAX2(1, ve
->num_elements
) * 8);
775 for (int i
= 0; i
< ve
->num_elements
; i
++) {
776 struct pipe_vertex_element
*pve
= ve
->pipe
+ i
;
778 assert(pve
->vertex_buffer_index
< vb
->count
);
779 assert(vb
->enabled_mask
& (1 << pve
->vertex_buffer_index
));
781 struct pipe_vertex_buffer
*pvb
= vb
->vb
+ pve
->vertex_buffer_index
;
782 struct lima_resource
*res
= lima_resource(pvb
->buffer
.resource
);
784 lima_submit_add_bo(submit
, LIMA_PIPE_GP
, res
->bo
, LIMA_SUBMIT_BO_READ
);
786 unsigned start
= info
->index_size
? (ctx
->min_index
+ info
->index_bias
) : info
->start
;
787 attribute
[n
++] = res
->bo
->va
+ pvb
->buffer_offset
+ pve
->src_offset
788 + start
* pvb
->stride
;
789 attribute
[n
++] = (pvb
->stride
<< 11) |
790 (lima_pipe_format_to_attrib_type(pve
->src_format
) << 2) |
791 (util_format_get_nr_components(pve
->src_format
) - 1);
794 lima_dump_command_stream_print(
795 submit
->dump
, attribute
, n
* 4, false, "update attribute info at va %x\n",
796 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_attribute_info
));
800 lima_update_gp_uniform(struct lima_context
*ctx
)
802 struct lima_context_constant_buffer
*ccb
=
803 ctx
->const_buffer
+ PIPE_SHADER_VERTEX
;
804 struct lima_vs_shader_state
*vs
= ctx
->vs
;
806 int size
= vs
->uniform_pending_offset
+ vs
->constant_size
+ 32;
807 void *vs_const_buff
=
808 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_uniform
, size
);
811 memcpy(vs_const_buff
, ccb
->buffer
, ccb
->size
);
813 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
,
814 ctx
->viewport
.transform
.scale
,
815 sizeof(ctx
->viewport
.transform
.scale
));
816 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
+ 16,
817 ctx
->viewport
.transform
.translate
,
818 sizeof(ctx
->viewport
.transform
.translate
));
821 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
+ 32,
822 vs
->constant
, vs
->constant_size
);
824 struct lima_submit
*submit
= lima_submit_get(ctx
);
826 lima_dump_command_stream_print(
827 submit
->dump
, vs_const_buff
, size
, true,
828 "update gp uniform at va %x\n",
829 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_uniform
));
833 lima_update_pp_uniform(struct lima_context
*ctx
)
835 const float *const_buff
= ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].buffer
;
836 size_t const_buff_size
= ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].size
/ sizeof(float);
841 uint16_t *fp16_const_buff
=
842 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_uniform
,
843 const_buff_size
* sizeof(uint16_t));
846 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_uniform_array
, 4);
848 for (int i
= 0; i
< const_buff_size
; i
++)
849 fp16_const_buff
[i
] = util_float_to_half(const_buff
[i
]);
851 *array
= lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform
);
853 struct lima_submit
*submit
= lima_submit_get(ctx
);
855 lima_dump_command_stream_print(
856 submit
->dump
, fp16_const_buff
, const_buff_size
* 2,
857 false, "add pp uniform data at va %x\n",
858 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform
));
859 lima_dump_command_stream_print(
860 submit
->dump
, array
, 4, false, "add pp uniform info at va %x\n",
861 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform_array
));
865 lima_update_varying(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
867 struct lima_submit
*submit
= lima_submit_get(ctx
);
868 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
869 struct lima_vs_shader_state
*vs
= ctx
->vs
;
870 uint32_t gp_output_size
;
871 unsigned num
= info
->index_size
? (ctx
->max_index
- ctx
->min_index
+ 1) : info
->count
;
874 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_varying_info
,
875 vs
->num_outputs
* 8);
880 for (int i
= 0; i
< vs
->num_outputs
; i
++) {
881 struct lima_varying_info
*v
= vs
->varying
+ i
;
883 if (i
== vs
->gl_pos_idx
||
884 i
== vs
->point_size_idx
)
887 int size
= v
->component_size
* 4;
889 /* does component_size == 2 need to be 16 aligned? */
890 if (v
->component_size
== 4)
891 offset
= align(offset
, 16);
897 vs
->varying_stride
= align(offset
, 16);
899 /* gl_Position is always present, allocate space for it */
900 gp_output_size
= align(4 * 4 * num
, 0x40);
902 /* Allocate space for varyings if there're any */
903 if (vs
->num_varyings
) {
904 ctx
->gp_output_varyings_offt
= gp_output_size
;
905 gp_output_size
+= align(vs
->varying_stride
* num
, 0x40);
908 /* Allocate space for gl_PointSize if it's there */
909 if (vs
->point_size_idx
!= -1) {
910 ctx
->gp_output_point_size_offt
= gp_output_size
;
911 gp_output_size
+= 4 * num
;
914 /* gp_output can be too large for the suballocator, so create a
915 * separate bo for it. The bo cache should prevent performance hit.
917 ctx
->gp_output
= lima_bo_create(screen
, gp_output_size
, 0);
918 assert(ctx
->gp_output
);
919 lima_submit_add_bo(submit
, LIMA_PIPE_GP
, ctx
->gp_output
, LIMA_SUBMIT_BO_WRITE
);
920 lima_submit_add_bo(submit
, LIMA_PIPE_PP
, ctx
->gp_output
, LIMA_SUBMIT_BO_READ
);
922 for (int i
= 0; i
< vs
->num_outputs
; i
++) {
923 struct lima_varying_info
*v
= vs
->varying
+ i
;
925 if (i
== vs
->gl_pos_idx
) {
927 varying
[n
++] = ctx
->gp_output
->va
;
928 varying
[n
++] = 0x8020;
929 } else if (i
== vs
->point_size_idx
) {
931 varying
[n
++] = ctx
->gp_output
->va
+ ctx
->gp_output_point_size_offt
;
932 varying
[n
++] = 0x2021;
935 varying
[n
++] = ctx
->gp_output
->va
+ ctx
->gp_output_varyings_offt
+
937 varying
[n
++] = (vs
->varying_stride
<< 11) | (v
->components
- 1) |
938 (v
->component_size
== 2 ? 0x0C : 0);
942 lima_dump_command_stream_print(
943 submit
->dump
, varying
, n
* 4, false, "update varying info at va %x\n",
944 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_varying_info
));
948 lima_draw_vbo_update(struct pipe_context
*pctx
,
949 const struct pipe_draw_info
*info
)
951 struct lima_context
*ctx
= lima_context(pctx
);
952 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
953 unsigned buffers
= 0;
955 if (fb
->base
.zsbuf
) {
956 if (ctx
->zsa
->base
.depth
.enabled
)
957 buffers
|= PIPE_CLEAR_DEPTH
;
958 if (ctx
->zsa
->base
.stencil
[0].enabled
||
959 ctx
->zsa
->base
.stencil
[1].enabled
)
960 buffers
|= PIPE_CLEAR_STENCIL
;
963 if (fb
->base
.nr_cbufs
)
964 buffers
|= PIPE_CLEAR_COLOR0
;
966 lima_update_submit_wb(ctx
, buffers
);
968 lima_update_gp_attribute_info(ctx
, info
);
970 if ((ctx
->dirty
& LIMA_CONTEXT_DIRTY_CONST_BUFF
&&
971 ctx
->const_buffer
[PIPE_SHADER_VERTEX
].dirty
) ||
972 ctx
->dirty
& LIMA_CONTEXT_DIRTY_VIEWPORT
||
973 ctx
->dirty
& LIMA_CONTEXT_DIRTY_SHADER_VERT
) {
974 lima_update_gp_uniform(ctx
);
975 ctx
->const_buffer
[PIPE_SHADER_VERTEX
].dirty
= false;
978 lima_update_varying(ctx
, info
);
980 /* If it's zero scissor, don't build vs cmd list */
981 if (!lima_is_scissor_zero(ctx
))
982 lima_pack_vs_cmd(ctx
, info
);
984 if (ctx
->dirty
& LIMA_CONTEXT_DIRTY_CONST_BUFF
&&
985 ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].dirty
) {
986 lima_update_pp_uniform(ctx
);
987 ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].dirty
= false;
990 lima_update_textures(ctx
);
992 lima_pack_render_state(ctx
, info
);
993 lima_pack_plbu_cmd(ctx
, info
);
995 if (ctx
->gp_output
) {
996 lima_bo_unreference(ctx
->gp_output
); /* held by submit */
997 ctx
->gp_output
= NULL
;
1004 lima_draw_vbo_indexed(struct pipe_context
*pctx
,
1005 const struct pipe_draw_info
*info
)
1007 struct lima_context
*ctx
= lima_context(pctx
);
1008 struct lima_submit
*submit
= lima_submit_get(ctx
);
1009 struct pipe_resource
*indexbuf
= NULL
;
1011 /* Mali Utgard GPU always need min/max index info for index draw,
1012 * compute it if upper layer does not do for us */
1013 if (info
->max_index
== ~0u)
1014 u_vbuf_get_minmax_index(pctx
, info
, &ctx
->min_index
, &ctx
->max_index
);
1016 ctx
->min_index
= info
->min_index
;
1017 ctx
->max_index
= info
->max_index
;
1020 if (info
->has_user_indices
) {
1021 util_upload_index_buffer(&ctx
->base
, info
, &indexbuf
, &ctx
->index_offset
, 0x40);
1022 ctx
->index_res
= lima_resource(indexbuf
);
1025 ctx
->index_res
= lima_resource(info
->index
.resource
);
1026 ctx
->index_offset
= 0;
1029 lima_submit_add_bo(submit
, LIMA_PIPE_GP
, ctx
->index_res
->bo
, LIMA_SUBMIT_BO_READ
);
1030 lima_submit_add_bo(submit
, LIMA_PIPE_PP
, ctx
->index_res
->bo
, LIMA_SUBMIT_BO_READ
);
1031 lima_draw_vbo_update(pctx
, info
);
1034 pipe_resource_reference(&indexbuf
, NULL
);
1038 lima_draw_vbo_count(struct pipe_context
*pctx
,
1039 const struct pipe_draw_info
*info
)
1041 static const uint32_t max_verts
= 65535;
1043 struct pipe_draw_info local_info
= *info
;
1044 unsigned start
= info
->start
;
1045 unsigned count
= info
->count
;
1048 unsigned this_count
= count
;
1051 u_split_draw(info
, max_verts
, &this_count
, &step
);
1053 local_info
.start
= start
;
1054 local_info
.count
= this_count
;
1056 lima_draw_vbo_update(pctx
, &local_info
);
1064 lima_draw_vbo(struct pipe_context
*pctx
,
1065 const struct pipe_draw_info
*info
)
1067 /* check if draw mode and vertex/index count match,
1068 * otherwise gp will hang */
1069 if (!u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
)) {
1070 debug_printf("draw mode and vertex/index count mismatch\n");
1074 struct lima_context
*ctx
= lima_context(pctx
);
1076 if (!ctx
->vs
|| !ctx
->fs
) {
1077 debug_warn_once("no shader, skip draw\n");
1081 if (!lima_update_vs_state(ctx
) || !lima_update_fs_state(ctx
))
1084 struct lima_submit
*submit
= lima_submit_get(ctx
);
1086 lima_dump_command_stream_print(
1087 submit
->dump
, ctx
->vs
->bo
->map
, ctx
->vs
->shader_size
, false,
1088 "add vs at va %x\n", ctx
->vs
->bo
->va
);
1090 lima_dump_command_stream_print(
1091 submit
->dump
, ctx
->fs
->bo
->map
, ctx
->fs
->shader_size
, false,
1092 "add fs at va %x\n", ctx
->fs
->bo
->va
);
1094 lima_submit_add_bo(submit
, LIMA_PIPE_GP
, ctx
->vs
->bo
, LIMA_SUBMIT_BO_READ
);
1095 lima_submit_add_bo(submit
, LIMA_PIPE_PP
, ctx
->fs
->bo
, LIMA_SUBMIT_BO_READ
);
1097 if (info
->index_size
)
1098 lima_draw_vbo_indexed(pctx
, info
);
1100 lima_draw_vbo_count(pctx
, info
);
1104 lima_draw_init(struct lima_context
*ctx
)
1106 ctx
->base
.clear
= lima_clear
;
1107 ctx
->base
.draw_vbo
= lima_draw_vbo
;