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"
37 #include "lima_context.h"
38 #include "lima_screen.h"
39 #include "lima_resource.h"
40 #include "lima_program.h"
42 #include "lima_submit.h"
43 #include "lima_texture.h"
44 #include "lima_util.h"
47 #include <drm-uapi/lima_drm.h>
50 lima_is_scissor_zero(struct lima_context
*ctx
)
52 if (!ctx
->rasterizer
|| !ctx
->rasterizer
->base
.scissor
)
55 struct pipe_scissor_state
*scissor
= &ctx
->scissor
;
57 scissor
->minx
== scissor
->maxx
58 && scissor
->miny
== scissor
->maxy
;
62 lima_update_submit_wb(struct lima_context
*ctx
, unsigned buffers
)
64 struct lima_submit
*submit
= lima_submit_get(ctx
);
65 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
67 /* add to submit when the buffer is dirty and resolve is clear (not added before) */
68 if (fb
->base
.nr_cbufs
&& (buffers
& PIPE_CLEAR_COLOR0
) &&
69 !(ctx
->resolve
& PIPE_CLEAR_COLOR0
)) {
70 struct lima_resource
*res
= lima_resource(fb
->base
.cbufs
[0]->texture
);
71 lima_submit_add_bo(submit
, LIMA_PIPE_PP
, res
->bo
, LIMA_SUBMIT_BO_WRITE
);
74 /* add to submit when the buffer is dirty and resolve is clear (not added before) */
75 if (fb
->base
.zsbuf
&& (buffers
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
)) &&
76 !(ctx
->resolve
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
))) {
77 struct lima_resource
*res
= lima_resource(fb
->base
.zsbuf
->texture
);
78 lima_submit_add_bo(submit
, LIMA_PIPE_PP
, res
->bo
, LIMA_SUBMIT_BO_WRITE
);
81 ctx
->resolve
|= buffers
;
85 lima_damage_rect_union(struct lima_context
*ctx
, unsigned minx
, unsigned maxx
, unsigned miny
, unsigned maxy
)
87 ctx
->damage_rect
.minx
= MIN2(ctx
->damage_rect
.minx
, minx
);
88 ctx
->damage_rect
.miny
= MIN2(ctx
->damage_rect
.miny
, miny
);
89 ctx
->damage_rect
.maxx
= MAX2(ctx
->damage_rect
.maxx
, maxx
);
90 ctx
->damage_rect
.maxy
= MAX2(ctx
->damage_rect
.maxy
, maxy
);
94 lima_clear(struct pipe_context
*pctx
, unsigned buffers
,
95 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
97 struct lima_context
*ctx
= lima_context(pctx
);
101 lima_update_submit_wb(ctx
, buffers
);
103 /* no need to reload if cleared */
104 if (ctx
->framebuffer
.base
.nr_cbufs
&& (buffers
& PIPE_CLEAR_COLOR0
)) {
105 struct lima_surface
*surf
= lima_surface(ctx
->framebuffer
.base
.cbufs
[0]);
106 surf
->reload
= false;
109 struct lima_context_clear
*clear
= &ctx
->clear
;
110 clear
->buffers
= buffers
;
112 if (buffers
& PIPE_CLEAR_COLOR0
) {
114 ((uint32_t)float_to_ubyte(color
->f
[3]) << 24) |
115 ((uint32_t)float_to_ubyte(color
->f
[2]) << 16) |
116 ((uint32_t)float_to_ubyte(color
->f
[1]) << 8) |
117 float_to_ubyte(color
->f
[0]);
120 ((uint64_t)float_to_ushort(color
->f
[3]) << 48) |
121 ((uint64_t)float_to_ushort(color
->f
[2]) << 32) |
122 ((uint64_t)float_to_ushort(color
->f
[1]) << 16) |
123 float_to_ushort(color
->f
[0]);
126 if (buffers
& PIPE_CLEAR_DEPTH
)
127 clear
->depth
= util_pack_z(PIPE_FORMAT_Z24X8_UNORM
, depth
);
129 if (buffers
& PIPE_CLEAR_STENCIL
)
130 clear
->stencil
= stencil
;
132 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_CLEAR
;
134 lima_damage_rect_union(ctx
, 0, ctx
->framebuffer
.base
.width
,
135 0, ctx
->framebuffer
.base
.height
);
138 enum lima_attrib_type
{
139 LIMA_ATTRIB_FLOAT
= 0x000,
140 /* todo: find out what lives here. */
141 LIMA_ATTRIB_I16
= 0x004,
142 LIMA_ATTRIB_U16
= 0x005,
143 LIMA_ATTRIB_I8
= 0x006,
144 LIMA_ATTRIB_U8
= 0x007,
145 LIMA_ATTRIB_I8N
= 0x008,
146 LIMA_ATTRIB_U8N
= 0x009,
147 LIMA_ATTRIB_I16N
= 0x00A,
148 LIMA_ATTRIB_U16N
= 0x00B,
149 /* todo: where is the 32 int */
150 /* todo: find out what lives here. */
151 LIMA_ATTRIB_FIXED
= 0x101
154 static enum lima_attrib_type
155 lima_pipe_format_to_attrib_type(enum pipe_format format
)
157 const struct util_format_description
*desc
= util_format_description(format
);
158 int i
= util_format_get_first_non_void_channel(format
);
159 const struct util_format_channel_description
*c
= desc
->channel
+ i
;
162 case UTIL_FORMAT_TYPE_FLOAT
:
163 return LIMA_ATTRIB_FLOAT
;
164 case UTIL_FORMAT_TYPE_FIXED
:
165 return LIMA_ATTRIB_FIXED
;
166 case UTIL_FORMAT_TYPE_SIGNED
:
169 return LIMA_ATTRIB_I8N
;
171 return LIMA_ATTRIB_I8
;
173 else if (c
->size
== 16) {
175 return LIMA_ATTRIB_I16N
;
177 return LIMA_ATTRIB_I16
;
180 case UTIL_FORMAT_TYPE_UNSIGNED
:
183 return LIMA_ATTRIB_U8N
;
185 return LIMA_ATTRIB_U8
;
187 else if (c
->size
== 16) {
189 return LIMA_ATTRIB_U16N
;
191 return LIMA_ATTRIB_U16
;
196 return LIMA_ATTRIB_FLOAT
;
200 lima_pack_vs_cmd(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
202 VS_CMD_BEGIN(&ctx
->vs_cmd_array
, 24);
204 if (!info
->index_size
) {
205 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_1();
206 VS_CMD_ARRAYS_SEMAPHORE_BEGIN_2();
209 int uniform_size
= ctx
->vs
->uniform_pending_offset
+ ctx
->vs
->constant_size
+ 32;
210 VS_CMD_UNIFORMS_ADDRESS(
211 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_uniform
),
212 align(uniform_size
, 16));
214 VS_CMD_SHADER_ADDRESS(ctx
->vs
->bo
->va
, ctx
->vs
->shader_size
);
215 VS_CMD_SHADER_INFO(ctx
->vs
->prefetch
, ctx
->vs
->shader_size
);
217 int num_outputs
= ctx
->vs
->num_outputs
;
218 int num_attributes
= ctx
->vertex_elements
->num_elements
;
219 VS_CMD_VARYING_ATTRIBUTE_COUNT(num_outputs
, MAX2(1, num_attributes
));
223 VS_CMD_ATTRIBUTES_ADDRESS(
224 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_attribute_info
),
225 MAX2(1, num_attributes
));
227 VS_CMD_VARYINGS_ADDRESS(
228 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_varying_info
),
231 unsigned num
= info
->index_size
? (ctx
->max_index
- ctx
->min_index
+ 1) : info
->count
;
232 VS_CMD_DRAW(num
, info
->index_size
);
236 VS_CMD_ARRAYS_SEMAPHORE_END(info
->index_size
);
242 lima_pack_plbu_cmd(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
244 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
245 struct lima_vs_shader_state
*vs
= ctx
->vs
;
246 unsigned minx
, maxx
, miny
, maxy
;
248 /* If it's zero scissor, we skip adding all other commands */
249 if (lima_is_scissor_zero(ctx
))
252 PLBU_CMD_BEGIN(&ctx
->plbu_cmd_array
, 32);
254 PLBU_CMD_VIEWPORT_LEFT(fui(ctx
->viewport
.left
));
255 PLBU_CMD_VIEWPORT_RIGHT(fui(ctx
->viewport
.right
));
256 PLBU_CMD_VIEWPORT_BOTTOM(fui(ctx
->viewport
.bottom
));
257 PLBU_CMD_VIEWPORT_TOP(fui(ctx
->viewport
.top
));
259 if (!info
->index_size
)
260 PLBU_CMD_ARRAYS_SEMAPHORE_BEGIN();
262 int cf
= ctx
->rasterizer
->base
.cull_face
;
263 int ccw
= ctx
->rasterizer
->base
.front_ccw
;
265 bool force_point_size
= false;
267 if (cf
!= PIPE_FACE_NONE
) {
268 if (cf
& PIPE_FACE_FRONT
)
269 cull
|= ccw
? 0x00040000 : 0x00020000;
270 if (cf
& PIPE_FACE_BACK
)
271 cull
|= ccw
? 0x00020000 : 0x00040000;
274 /* Specify point size with PLBU command if shader doesn't write */
275 if (info
->mode
== PIPE_PRIM_POINTS
&& ctx
->vs
->point_size_idx
== -1)
276 force_point_size
= true;
278 /* Specify line width with PLBU command for lines */
279 if (info
->mode
> PIPE_PRIM_POINTS
&& info
->mode
< PIPE_PRIM_TRIANGLES
)
280 force_point_size
= true;
282 PLBU_CMD_PRIMITIVE_SETUP(force_point_size
, cull
, info
->index_size
);
284 PLBU_CMD_RSW_VERTEX_ARRAY(
285 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
),
289 * - we should set it only for the first draw that enabled the scissor and for
290 * latter draw only if scissor is dirty
292 if (ctx
->rasterizer
->base
.scissor
) {
293 struct pipe_scissor_state
*scissor
= &ctx
->scissor
;
294 minx
= scissor
->minx
;
295 maxx
= scissor
->maxx
;
296 miny
= scissor
->miny
;
297 maxy
= scissor
->maxy
;
300 maxx
= fb
->base
.width
;
302 maxy
= fb
->base
.height
;
305 minx
= MAX2(minx
, ctx
->viewport
.left
);
306 maxx
= MIN2(maxx
, ctx
->viewport
.right
);
307 miny
= MAX2(miny
, ctx
->viewport
.bottom
);
308 maxy
= MIN2(maxy
, ctx
->viewport
.top
);
310 PLBU_CMD_SCISSORS(minx
, maxx
, miny
, maxy
);
311 lima_damage_rect_union(ctx
, minx
, maxx
, miny
, maxy
);
315 PLBU_CMD_DEPTH_RANGE_NEAR(fui(ctx
->viewport
.near
));
316 PLBU_CMD_DEPTH_RANGE_FAR(fui(ctx
->viewport
.far
));
318 if ((info
->mode
== PIPE_PRIM_POINTS
&& ctx
->vs
->point_size_idx
== -1) ||
319 ((info
->mode
>= PIPE_PRIM_LINES
) && (info
->mode
< PIPE_PRIM_TRIANGLES
)))
321 uint32_t v
= info
->mode
== PIPE_PRIM_POINTS
?
322 fui(ctx
->rasterizer
->base
.point_size
) : fui(ctx
->rasterizer
->base
.line_width
);
323 PLBU_CMD_LOW_PRIM_SIZE(v
);
326 if (info
->index_size
) {
327 PLBU_CMD_INDEXED_DEST(ctx
->gp_output
->va
);
328 if (vs
->point_size_idx
!= -1)
329 PLBU_CMD_INDEXED_PT_SIZE(ctx
->gp_output
->va
+ ctx
->gp_output_point_size_offt
);
331 PLBU_CMD_INDICES(ctx
->index_res
->bo
->va
+ info
->start
* info
->index_size
+ ctx
->index_offset
);
334 /* can this make the attribute info static? */
335 PLBU_CMD_DRAW_ARRAYS(info
->mode
, info
->start
, info
->count
);
338 PLBU_CMD_ARRAYS_SEMAPHORE_END();
340 if (info
->index_size
)
341 PLBU_CMD_DRAW_ELEMENTS(info
->mode
, ctx
->min_index
, info
->count
);
347 lima_blend_func(enum pipe_blend_func pipe
)
352 case PIPE_BLEND_SUBTRACT
:
354 case PIPE_BLEND_REVERSE_SUBTRACT
:
365 lima_blend_factor_has_alpha(enum pipe_blendfactor pipe
)
367 /* Bit 4 is set if the blendfactor uses alpha */
369 case PIPE_BLENDFACTOR_SRC_ALPHA
:
370 case PIPE_BLENDFACTOR_DST_ALPHA
:
371 case PIPE_BLENDFACTOR_CONST_ALPHA
:
372 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
373 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
374 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
377 case PIPE_BLENDFACTOR_SRC_COLOR
:
378 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
379 case PIPE_BLENDFACTOR_DST_COLOR
:
380 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
381 case PIPE_BLENDFACTOR_CONST_COLOR
:
382 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
383 case PIPE_BLENDFACTOR_ZERO
:
384 case PIPE_BLENDFACTOR_ONE
:
385 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
388 case PIPE_BLENDFACTOR_SRC1_COLOR
:
389 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
390 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
391 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
392 return -1; /* not supported */
398 lima_blend_factor_is_inv(enum pipe_blendfactor pipe
)
400 /* Bit 3 is set if the blendfactor type is inverted */
402 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
403 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
404 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
405 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
406 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
407 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
408 case PIPE_BLENDFACTOR_ONE
:
411 case PIPE_BLENDFACTOR_SRC_COLOR
:
412 case PIPE_BLENDFACTOR_SRC_ALPHA
:
413 case PIPE_BLENDFACTOR_DST_COLOR
:
414 case PIPE_BLENDFACTOR_DST_ALPHA
:
415 case PIPE_BLENDFACTOR_CONST_COLOR
:
416 case PIPE_BLENDFACTOR_CONST_ALPHA
:
417 case PIPE_BLENDFACTOR_ZERO
:
418 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
421 case PIPE_BLENDFACTOR_SRC1_COLOR
:
422 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
423 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
424 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
425 return -1; /* not supported */
431 lima_blend_factor(enum pipe_blendfactor pipe
)
433 /* Bits 0-2 indicate the blendfactor type */
435 case PIPE_BLENDFACTOR_SRC_COLOR
:
436 case PIPE_BLENDFACTOR_SRC_ALPHA
:
437 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
438 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
441 case PIPE_BLENDFACTOR_DST_COLOR
:
442 case PIPE_BLENDFACTOR_DST_ALPHA
:
443 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
444 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
447 case PIPE_BLENDFACTOR_CONST_COLOR
:
448 case PIPE_BLENDFACTOR_CONST_ALPHA
:
449 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
450 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
453 case PIPE_BLENDFACTOR_ZERO
:
454 case PIPE_BLENDFACTOR_ONE
:
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_calculate_alpha_blend(enum pipe_blend_func rgb_func
, enum pipe_blend_func alpha_func
,
471 enum pipe_blendfactor rgb_src_factor
, enum pipe_blendfactor rgb_dst_factor
,
472 enum pipe_blendfactor alpha_src_factor
, enum pipe_blendfactor alpha_dst_factor
)
474 /* PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE has to be changed to PIPE_BLENDFACTOR_ONE
475 * if it is set for alpha_src.
477 if (alpha_src_factor
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
)
478 alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
480 return lima_blend_func(rgb_func
) |
481 (lima_blend_func(alpha_func
) << 3) |
483 (lima_blend_factor(rgb_src_factor
) << 6) |
484 (lima_blend_factor_is_inv(rgb_src_factor
) << 9) |
485 (lima_blend_factor_has_alpha(rgb_src_factor
) << 10) |
487 (lima_blend_factor(rgb_dst_factor
) << 11) |
488 (lima_blend_factor_is_inv(rgb_dst_factor
) << 14) |
489 (lima_blend_factor_has_alpha(rgb_dst_factor
) << 15) |
491 (lima_blend_factor(alpha_src_factor
) << 16) |
492 (lima_blend_factor_is_inv(alpha_src_factor
) << 19) |
494 (lima_blend_factor(alpha_dst_factor
) << 20) |
495 (lima_blend_factor_is_inv(alpha_dst_factor
) << 23) |
496 0x0C000000; /* need to check if this is GLESv1 glAlphaFunc */
500 lima_stencil_op(enum pipe_stencil_op pipe
)
503 case PIPE_STENCIL_OP_KEEP
:
505 case PIPE_STENCIL_OP_ZERO
:
507 case PIPE_STENCIL_OP_REPLACE
:
509 case PIPE_STENCIL_OP_INCR
:
511 case PIPE_STENCIL_OP_DECR
:
513 case PIPE_STENCIL_OP_INCR_WRAP
:
515 case PIPE_STENCIL_OP_DECR_WRAP
:
517 case PIPE_STENCIL_OP_INVERT
:
524 lima_calculate_depth_test(struct pipe_depth_state
*depth
, struct pipe_rasterizer_state
*rst
)
526 int offset_scale
= 0, offset_units
= 0;
527 enum pipe_compare_func func
= (depth
->enabled
? depth
->func
: PIPE_FUNC_ALWAYS
);
529 offset_scale
= CLAMP(rst
->offset_scale
* 4, -128, 127);
530 if (offset_scale
< 0)
531 offset_scale
+= 0x100;
533 offset_units
= CLAMP(rst
->offset_units
* 2, -128, 127);
534 if (offset_units
< 0)
535 offset_units
+= 0x100;
537 return (depth
->enabled
&& depth
->writemask
) |
539 (offset_scale
<< 16) |
540 (offset_units
<< 24) |
541 0x30; /* find out what is this */
545 lima_pack_render_state(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
547 struct lima_fs_shader_state
*fs
= ctx
->fs
;
548 struct lima_render_state
*render
=
549 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_plb_rsw
,
552 bool pixel_kill
= true;
554 /* do hw support RGBA independ blend?
555 * PIPE_CAP_INDEP_BLEND_ENABLE
557 * how to handle the no cbuf only zbuf case?
559 struct pipe_rt_blend_state
*rt
= ctx
->blend
->base
.rt
;
560 render
->blend_color_bg
= float_to_ubyte(ctx
->blend_color
.color
[2]) |
561 (float_to_ubyte(ctx
->blend_color
.color
[1]) << 16);
562 render
->blend_color_ra
= float_to_ubyte(ctx
->blend_color
.color
[0]) |
563 (float_to_ubyte(ctx
->blend_color
.color
[3]) << 16);
565 if (rt
->blend_enable
) {
566 render
->alpha_blend
= lima_calculate_alpha_blend(rt
->rgb_func
, rt
->alpha_func
,
567 rt
->rgb_src_factor
, rt
->rgb_dst_factor
,
568 rt
->alpha_src_factor
, rt
->alpha_dst_factor
);
572 * Special handling for blending disabled.
573 * Binary driver is generating the same alpha_value,
574 * as when we would just enable blending, without changing/setting any blend equation/params.
575 * Normaly in this case mesa would set all rt fields (func/factor) to zero.
577 render
->alpha_blend
= lima_calculate_alpha_blend(PIPE_BLEND_ADD
, PIPE_BLEND_ADD
,
578 PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_ZERO
,
579 PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_ZERO
);
582 render
->alpha_blend
|= (rt
->colormask
& PIPE_MASK_RGBA
) << 28;
584 struct pipe_rasterizer_state
*rst
= &ctx
->rasterizer
->base
;
585 struct pipe_depth_state
*depth
= &ctx
->zsa
->base
.depth
;
586 render
->depth_test
= lima_calculate_depth_test(depth
, rst
);
590 near
= float_to_ushort(ctx
->viewport
.near
);
591 far
= float_to_ushort(ctx
->viewport
.far
);
593 /* Subtract epsilon from 'near' if far == near. Make sure we don't get overflow */
594 if ((far
== near
) && (near
!= 0))
597 /* overlap with plbu? any place can remove one? */
598 render
->depth_range
= near
| (far
<< 16);
600 struct pipe_stencil_state
*stencil
= ctx
->zsa
->base
.stencil
;
601 struct pipe_stencil_ref
*ref
= &ctx
->stencil_ref
;
603 if (stencil
[0].enabled
) { /* stencil is enabled */
604 render
->stencil_front
= stencil
[0].func
|
605 (lima_stencil_op(stencil
[0].fail_op
) << 3) |
606 (lima_stencil_op(stencil
[0].zfail_op
) << 6) |
607 (lima_stencil_op(stencil
[0].zpass_op
) << 9) |
608 (ref
->ref_value
[0] << 16) |
609 (stencil
[0].valuemask
<< 24);
610 render
->stencil_back
= render
->stencil_front
;
611 render
->stencil_test
= (stencil
[0].writemask
& 0xff) | (stencil
[0].writemask
& 0xff) << 8;
612 if (stencil
[1].enabled
) { /* two-side is enabled */
613 render
->stencil_back
= stencil
[1].func
|
614 (lima_stencil_op(stencil
[1].fail_op
) << 3) |
615 (lima_stencil_op(stencil
[1].zfail_op
) << 6) |
616 (lima_stencil_op(stencil
[1].zpass_op
) << 9) |
617 (ref
->ref_value
[1] << 16) |
618 (stencil
[1].valuemask
<< 24);
619 render
->stencil_test
= (stencil
[0].writemask
& 0xff) | (stencil
[1].writemask
& 0xff) << 8;
621 /* TODO: Find out, what (render->stecil_test & 0xffff0000) is.
622 * 0x00ff0000 is probably (float_to_ubyte(alpha->ref_value) << 16)
623 * (render->multi_sample & 0x00000007 is probably the compare function
624 * of glAlphaFunc then.
628 /* Default values, when stencil is disabled:
629 * stencil[0|1].valuemask = 0xff
630 * stencil[0|1].func = PIPE_FUNC_ALWAYS
631 * stencil[0|1].writemask = 0xff
633 render
->stencil_front
= 0xff000007;
634 render
->stencil_back
= 0xff000007;
635 render
->stencil_test
= 0x0000ffff;
638 /* need more investigation */
639 if (info
->mode
== PIPE_PRIM_POINTS
)
640 render
->multi_sample
= 0x0000F007;
641 else if (info
->mode
< PIPE_PRIM_TRIANGLES
)
642 render
->multi_sample
= 0x0000F407;
644 render
->multi_sample
= 0x0000F807;
645 if (ctx
->framebuffer
.base
.samples
)
646 render
->multi_sample
|= 0x68;
648 render
->shader_address
=
649 ctx
->fs
->bo
->va
| (((uint32_t *)ctx
->fs
->bo
->map
)[0] & 0x1F);
651 /* seems not needed */
652 render
->uniforms_address
= 0x00000000;
654 render
->textures_address
= 0x00000000;
656 render
->aux0
= (ctx
->vs
->varying_stride
>> 3);
657 render
->aux1
= 0x00001000;
658 if (ctx
->blend
->base
.dither
)
659 render
->aux1
|= 0x00002000;
661 if (fs
->uses_discard
) {
666 if (rt
->blend_enable
)
669 if ((rt
->colormask
& PIPE_MASK_RGBA
) != PIPE_MASK_RGBA
)
673 render
->aux0
|= 0x300;
676 render
->aux0
|= 0x1000;
678 if (ctx
->tex_stateobj
.num_samplers
) {
679 render
->textures_address
=
680 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_tex_desc
);
681 render
->aux0
|= ctx
->tex_stateobj
.num_samplers
<< 14;
682 render
->aux0
|= 0x20;
685 if (ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].buffer
) {
686 render
->uniforms_address
=
687 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform_array
);
688 uint32_t size
= ctx
->buffer_state
[lima_ctx_buff_pp_uniform
].size
;
691 bits
= util_last_bit(size
>> 3) - 1;
692 bits
+= size
& u_bit_consecutive(0, bits
+ 3) ? 1 : 0;
694 render
->uniforms_address
|= bits
> 0xf ? 0xf : bits
;
696 render
->aux0
|= 0x80;
697 render
->aux1
|= 0x10000;
700 if (ctx
->vs
->num_varyings
) {
701 render
->varying_types
= 0x00000000;
702 render
->varyings_address
= ctx
->gp_output
->va
+
703 ctx
->gp_output_varyings_offt
;
704 for (int i
= 0, index
= 0; i
< ctx
->vs
->num_outputs
; i
++) {
707 if (i
== ctx
->vs
->gl_pos_idx
||
708 i
== ctx
->vs
->point_size_idx
)
711 struct lima_varying_info
*v
= ctx
->vs
->varying
+ i
;
712 if (v
->component_size
== 4)
713 val
= v
->components
> 2 ? 0 : 1;
715 val
= v
->components
> 2 ? 2 : 3;
718 render
->varying_types
|= val
<< (3 * index
);
719 else if (index
== 10) {
720 render
->varying_types
|= val
<< 30;
721 render
->varyings_address
|= val
>> 2;
723 else if (index
== 11)
724 render
->varyings_address
|= val
<< 1;
730 render
->varying_types
= 0x00000000;
731 render
->varyings_address
= 0x00000000;
734 lima_dump_command_stream_print(
735 render
, sizeof(*render
), false, "add render state at va %x\n",
736 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
));
738 lima_dump_rsw_command_stream_print(
739 render
, sizeof(*render
), lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_plb_rsw
));
744 lima_update_gp_attribute_info(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
746 struct lima_submit
*submit
= lima_submit_get(ctx
);
747 struct lima_vertex_element_state
*ve
= ctx
->vertex_elements
;
748 struct lima_context_vertex_buffer
*vb
= &ctx
->vertex_buffers
;
750 uint32_t *attribute
=
751 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_attribute_info
,
752 MAX2(1, ve
->num_elements
) * 8);
755 for (int i
= 0; i
< ve
->num_elements
; i
++) {
756 struct pipe_vertex_element
*pve
= ve
->pipe
+ i
;
758 assert(pve
->vertex_buffer_index
< vb
->count
);
759 assert(vb
->enabled_mask
& (1 << pve
->vertex_buffer_index
));
761 struct pipe_vertex_buffer
*pvb
= vb
->vb
+ pve
->vertex_buffer_index
;
762 struct lima_resource
*res
= lima_resource(pvb
->buffer
.resource
);
764 lima_submit_add_bo(submit
, LIMA_PIPE_GP
, res
->bo
, LIMA_SUBMIT_BO_READ
);
766 unsigned start
= info
->index_size
? (ctx
->min_index
+ info
->index_bias
) : info
->start
;
767 attribute
[n
++] = res
->bo
->va
+ pvb
->buffer_offset
+ pve
->src_offset
768 + start
* pvb
->stride
;
769 attribute
[n
++] = (pvb
->stride
<< 11) |
770 (lima_pipe_format_to_attrib_type(pve
->src_format
) << 2) |
771 (util_format_get_nr_components(pve
->src_format
) - 1);
774 lima_dump_command_stream_print(
775 attribute
, n
* 4, false, "update attribute info at va %x\n",
776 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_attribute_info
));
780 lima_update_gp_uniform(struct lima_context
*ctx
)
782 struct lima_context_constant_buffer
*ccb
=
783 ctx
->const_buffer
+ PIPE_SHADER_VERTEX
;
784 struct lima_vs_shader_state
*vs
= ctx
->vs
;
786 int size
= vs
->uniform_pending_offset
+ vs
->constant_size
+ 32;
787 void *vs_const_buff
=
788 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_uniform
, size
);
791 memcpy(vs_const_buff
, ccb
->buffer
, ccb
->size
);
793 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
,
794 ctx
->viewport
.transform
.scale
,
795 sizeof(ctx
->viewport
.transform
.scale
));
796 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
+ 16,
797 ctx
->viewport
.transform
.translate
,
798 sizeof(ctx
->viewport
.transform
.translate
));
801 memcpy(vs_const_buff
+ vs
->uniform_pending_offset
+ 32,
802 vs
->constant
, vs
->constant_size
);
804 lima_dump_command_stream_print(
805 vs_const_buff
, size
, true,
806 "update gp uniform at va %x\n",
807 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_uniform
));
811 lima_update_pp_uniform(struct lima_context
*ctx
)
813 const float *const_buff
= ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].buffer
;
814 size_t const_buff_size
= ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].size
/ sizeof(float);
819 uint16_t *fp16_const_buff
=
820 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_uniform
,
821 const_buff_size
* sizeof(uint16_t));
824 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_uniform_array
, 4);
826 for (int i
= 0; i
< const_buff_size
; i
++)
827 fp16_const_buff
[i
] = util_float_to_half(const_buff
[i
]);
829 *array
= lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform
);
831 lima_dump_command_stream_print(
832 fp16_const_buff
, const_buff_size
* 2, false, "add pp uniform data at va %x\n",
833 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform
));
834 lima_dump_command_stream_print(
835 array
, 4, false, "add pp uniform info at va %x\n",
836 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_uniform_array
));
840 lima_update_varying(struct lima_context
*ctx
, const struct pipe_draw_info
*info
)
842 struct lima_submit
*submit
= lima_submit_get(ctx
);
843 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
844 struct lima_vs_shader_state
*vs
= ctx
->vs
;
845 uint32_t gp_output_size
;
846 unsigned num
= info
->index_size
? (ctx
->max_index
- ctx
->min_index
+ 1) : info
->count
;
849 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_gp_varying_info
,
850 vs
->num_outputs
* 8);
855 for (int i
= 0; i
< vs
->num_outputs
; i
++) {
856 struct lima_varying_info
*v
= vs
->varying
+ i
;
858 if (i
== vs
->gl_pos_idx
||
859 i
== vs
->point_size_idx
)
862 int size
= v
->component_size
* 4;
864 /* does component_size == 2 need to be 16 aligned? */
865 if (v
->component_size
== 4)
866 offset
= align(offset
, 16);
872 vs
->varying_stride
= align(offset
, 16);
874 /* gl_Position is always present, allocate space for it */
875 gp_output_size
= align(4 * 4 * num
, 0x40);
877 /* Allocate space for varyings if there're any */
878 if (vs
->num_varyings
) {
879 ctx
->gp_output_varyings_offt
= gp_output_size
;
880 gp_output_size
+= align(vs
->varying_stride
* num
, 0x40);
883 /* Allocate space for gl_PointSize if it's there */
884 if (vs
->point_size_idx
!= -1) {
885 ctx
->gp_output_point_size_offt
= gp_output_size
;
886 gp_output_size
+= 4 * num
;
889 /* gp_output can be too large for the suballocator, so create a
890 * separate bo for it. The bo cache should prevent performance hit.
892 ctx
->gp_output
= lima_bo_create(screen
, gp_output_size
, 0);
893 assert(ctx
->gp_output
);
894 lima_submit_add_bo(submit
, LIMA_PIPE_GP
, ctx
->gp_output
, LIMA_SUBMIT_BO_WRITE
);
895 lima_submit_add_bo(submit
, LIMA_PIPE_PP
, ctx
->gp_output
, LIMA_SUBMIT_BO_READ
);
897 for (int i
= 0; i
< vs
->num_outputs
; i
++) {
898 struct lima_varying_info
*v
= vs
->varying
+ i
;
900 if (i
== vs
->gl_pos_idx
) {
902 varying
[n
++] = ctx
->gp_output
->va
;
903 varying
[n
++] = 0x8020;
904 } else if (i
== vs
->point_size_idx
) {
906 varying
[n
++] = ctx
->gp_output
->va
+ ctx
->gp_output_point_size_offt
;
907 varying
[n
++] = 0x2021;
910 varying
[n
++] = ctx
->gp_output
->va
+ ctx
->gp_output_varyings_offt
+
912 varying
[n
++] = (vs
->varying_stride
<< 11) | (v
->components
- 1) |
913 (v
->component_size
== 2 ? 0x0C : 0);
917 lima_dump_command_stream_print(
918 varying
, n
* 4, false, "update varying info at va %x\n",
919 lima_ctx_buff_va(ctx
, lima_ctx_buff_gp_varying_info
));
923 lima_draw_vbo_update(struct pipe_context
*pctx
,
924 const struct pipe_draw_info
*info
)
926 struct lima_context
*ctx
= lima_context(pctx
);
927 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
928 unsigned buffers
= 0;
930 if (fb
->base
.zsbuf
) {
931 if (ctx
->zsa
->base
.depth
.enabled
)
932 buffers
|= PIPE_CLEAR_DEPTH
;
933 if (ctx
->zsa
->base
.stencil
[0].enabled
||
934 ctx
->zsa
->base
.stencil
[1].enabled
)
935 buffers
|= PIPE_CLEAR_STENCIL
;
938 if (fb
->base
.nr_cbufs
)
939 buffers
|= PIPE_CLEAR_COLOR0
;
941 lima_update_submit_wb(ctx
, buffers
);
943 lima_update_gp_attribute_info(ctx
, info
);
945 if ((ctx
->dirty
& LIMA_CONTEXT_DIRTY_CONST_BUFF
&&
946 ctx
->const_buffer
[PIPE_SHADER_VERTEX
].dirty
) ||
947 ctx
->dirty
& LIMA_CONTEXT_DIRTY_VIEWPORT
||
948 ctx
->dirty
& LIMA_CONTEXT_DIRTY_SHADER_VERT
) {
949 lima_update_gp_uniform(ctx
);
950 ctx
->const_buffer
[PIPE_SHADER_VERTEX
].dirty
= false;
953 lima_update_varying(ctx
, info
);
955 /* If it's zero scissor, don't build vs cmd list */
956 if (!lima_is_scissor_zero(ctx
))
957 lima_pack_vs_cmd(ctx
, info
);
959 if (ctx
->dirty
& LIMA_CONTEXT_DIRTY_CONST_BUFF
&&
960 ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].dirty
) {
961 lima_update_pp_uniform(ctx
);
962 ctx
->const_buffer
[PIPE_SHADER_FRAGMENT
].dirty
= false;
965 lima_update_textures(ctx
);
967 lima_pack_render_state(ctx
, info
);
968 lima_pack_plbu_cmd(ctx
, info
);
970 if (ctx
->gp_output
) {
971 lima_bo_unreference(ctx
->gp_output
); /* held by submit */
972 ctx
->gp_output
= NULL
;
979 lima_draw_vbo_indexed(struct pipe_context
*pctx
,
980 const struct pipe_draw_info
*info
)
982 struct lima_context
*ctx
= lima_context(pctx
);
983 struct lima_submit
*submit
= lima_submit_get(ctx
);
984 struct pipe_resource
*indexbuf
= NULL
;
986 /* Mali Utgard GPU always need min/max index info for index draw,
987 * compute it if upper layer does not do for us */
988 if (info
->max_index
== ~0u)
989 u_vbuf_get_minmax_index(pctx
, info
, &ctx
->min_index
, &ctx
->max_index
);
991 ctx
->min_index
= info
->min_index
;
992 ctx
->max_index
= info
->max_index
;
995 if (info
->has_user_indices
) {
996 util_upload_index_buffer(&ctx
->base
, info
, &indexbuf
, &ctx
->index_offset
, 0x40);
997 ctx
->index_res
= lima_resource(indexbuf
);
1000 ctx
->index_res
= lima_resource(info
->index
.resource
);
1001 ctx
->index_offset
= 0;
1004 lima_submit_add_bo(submit
, LIMA_PIPE_GP
, ctx
->index_res
->bo
, LIMA_SUBMIT_BO_READ
);
1005 lima_submit_add_bo(submit
, LIMA_PIPE_PP
, ctx
->index_res
->bo
, LIMA_SUBMIT_BO_READ
);
1006 lima_draw_vbo_update(pctx
, info
);
1009 pipe_resource_reference(&indexbuf
, NULL
);
1013 lima_draw_vbo_count(struct pipe_context
*pctx
,
1014 const struct pipe_draw_info
*info
)
1016 static const uint32_t max_verts
= 65535;
1018 struct pipe_draw_info local_info
= *info
;
1019 unsigned start
= info
->start
;
1020 unsigned count
= info
->count
;
1023 unsigned this_count
= count
;
1026 u_split_draw(info
, max_verts
, &this_count
, &step
);
1028 local_info
.start
= start
;
1029 local_info
.count
= this_count
;
1031 lima_draw_vbo_update(pctx
, &local_info
);
1039 lima_draw_vbo(struct pipe_context
*pctx
,
1040 const struct pipe_draw_info
*info
)
1042 /* check if draw mode and vertex/index count match,
1043 * otherwise gp will hang */
1044 if (!u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
)) {
1045 debug_printf("draw mode and vertex/index count mismatch\n");
1049 struct lima_context
*ctx
= lima_context(pctx
);
1051 if (!ctx
->vs
|| !ctx
->fs
) {
1052 debug_warn_once("no shader, skip draw\n");
1056 if (!lima_update_vs_state(ctx
) || !lima_update_fs_state(ctx
))
1059 struct lima_submit
*submit
= lima_submit_get(ctx
);
1061 lima_dump_command_stream_print(
1062 ctx
->vs
->bo
->map
, ctx
->vs
->shader_size
, false,
1063 "add vs at va %x\n", ctx
->vs
->bo
->va
);
1065 lima_dump_command_stream_print(
1066 ctx
->fs
->bo
->map
, ctx
->fs
->shader_size
, false,
1067 "add fs at va %x\n", ctx
->fs
->bo
->va
);
1069 lima_submit_add_bo(submit
, LIMA_PIPE_GP
, ctx
->vs
->bo
, LIMA_SUBMIT_BO_READ
);
1070 lima_submit_add_bo(submit
, LIMA_PIPE_PP
, ctx
->fs
->bo
, LIMA_SUBMIT_BO_READ
);
1072 if (info
->index_size
)
1073 lima_draw_vbo_indexed(pctx
, info
);
1075 lima_draw_vbo_count(pctx
, info
);
1079 lima_draw_init(struct lima_context
*ctx
)
1081 ctx
->base
.clear
= lima_clear
;
1082 ctx
->base
.draw_vbo
= lima_draw_vbo
;