2 * Copyright © 2014-2017 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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, sublicense,
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 next
13 * paragraph) shall be included in all copies or substantial portions of the
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 NONINFRINGEMENT. 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 DEALINGS
25 #include "pipe/p_state.h"
26 #include "util/u_format.h"
27 #include "util/u_framebuffer.h"
28 #include "util/u_inlines.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_half.h"
32 #include "util/u_helpers.h"
33 #include "util/u_upload_mgr.h"
35 #include "v3d_context.h"
36 #include "v3d_tiling.h"
37 #include "broadcom/common/v3d_macros.h"
38 #include "broadcom/compiler/v3d_compiler.h"
39 #include "broadcom/cle/v3dx_pack.h"
42 v3d_generic_cso_state_delete(struct pipe_context
*pctx
, void *hwcso
)
48 v3d_set_blend_color(struct pipe_context
*pctx
,
49 const struct pipe_blend_color
*blend_color
)
51 struct v3d_context
*v3d
= v3d_context(pctx
);
52 v3d
->blend_color
.f
= *blend_color
;
53 for (int i
= 0; i
< 4; i
++) {
54 v3d
->blend_color
.hf
[i
] =
55 util_float_to_half(blend_color
->color
[i
]);
57 v3d
->dirty
|= VC5_DIRTY_BLEND_COLOR
;
61 v3d_set_stencil_ref(struct pipe_context
*pctx
,
62 const struct pipe_stencil_ref
*stencil_ref
)
64 struct v3d_context
*v3d
= v3d_context(pctx
);
65 v3d
->stencil_ref
= *stencil_ref
;
66 v3d
->dirty
|= VC5_DIRTY_STENCIL_REF
;
70 v3d_set_clip_state(struct pipe_context
*pctx
,
71 const struct pipe_clip_state
*clip
)
73 struct v3d_context
*v3d
= v3d_context(pctx
);
75 v3d
->dirty
|= VC5_DIRTY_CLIP
;
79 v3d_set_sample_mask(struct pipe_context
*pctx
, unsigned sample_mask
)
81 struct v3d_context
*v3d
= v3d_context(pctx
);
82 v3d
->sample_mask
= sample_mask
& ((1 << V3D_MAX_SAMPLES
) - 1);
83 v3d
->dirty
|= VC5_DIRTY_SAMPLE_STATE
;
87 v3d_create_rasterizer_state(struct pipe_context
*pctx
,
88 const struct pipe_rasterizer_state
*cso
)
90 struct v3d_rasterizer_state
*so
;
92 so
= CALLOC_STRUCT(v3d_rasterizer_state
);
98 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
101 so
->point_size
= MAX2(cso
->point_size
, .125f
);
103 STATIC_ASSERT(sizeof(so
->depth_offset
) >=
104 cl_packet_length(DEPTH_OFFSET
));
105 v3dx_pack(&so
->depth_offset
, DEPTH_OFFSET
, depth
) {
106 depth
.depth_offset_factor
= cso
->offset_scale
;
107 depth
.depth_offset_units
= cso
->offset_units
;
110 /* The HW treats polygon offset units based on a Z24 buffer, so we
111 * need to scale up offset_units if we're only Z16.
113 v3dx_pack(&so
->depth_offset_z16
, DEPTH_OFFSET
, depth
) {
114 depth
.depth_offset_factor
= cso
->offset_scale
;
115 depth
.depth_offset_units
= cso
->offset_units
* 256.0;
121 /* Blend state is baked into shaders. */
123 v3d_create_blend_state(struct pipe_context
*pctx
,
124 const struct pipe_blend_state
*cso
)
126 struct v3d_blend_state
*so
;
128 so
= CALLOC_STRUCT(v3d_blend_state
);
134 if (cso
->independent_blend_enable
) {
135 for (int i
= 0; i
< V3D_MAX_DRAW_BUFFERS
; i
++) {
136 so
->blend_enables
|= cso
->rt
[i
].blend_enable
<< i
;
138 /* V3D 4.x is when we got independent blend enables. */
139 assert(V3D_VERSION
>= 40 ||
140 cso
->rt
[i
].blend_enable
== cso
->rt
[0].blend_enable
);
143 if (cso
->rt
[0].blend_enable
)
144 so
->blend_enables
= (1 << V3D_MAX_DRAW_BUFFERS
) - 1;
151 translate_stencil_op(enum pipe_stencil_op op
)
154 case PIPE_STENCIL_OP_KEEP
: return V3D_STENCIL_OP_KEEP
;
155 case PIPE_STENCIL_OP_ZERO
: return V3D_STENCIL_OP_ZERO
;
156 case PIPE_STENCIL_OP_REPLACE
: return V3D_STENCIL_OP_REPLACE
;
157 case PIPE_STENCIL_OP_INCR
: return V3D_STENCIL_OP_INCR
;
158 case PIPE_STENCIL_OP_DECR
: return V3D_STENCIL_OP_DECR
;
159 case PIPE_STENCIL_OP_INCR_WRAP
: return V3D_STENCIL_OP_INCWRAP
;
160 case PIPE_STENCIL_OP_DECR_WRAP
: return V3D_STENCIL_OP_DECWRAP
;
161 case PIPE_STENCIL_OP_INVERT
: return V3D_STENCIL_OP_INVERT
;
163 unreachable("bad stencil op");
167 v3d_create_depth_stencil_alpha_state(struct pipe_context
*pctx
,
168 const struct pipe_depth_stencil_alpha_state
*cso
)
170 struct v3d_depth_stencil_alpha_state
*so
;
172 so
= CALLOC_STRUCT(v3d_depth_stencil_alpha_state
);
178 if (cso
->depth
.enabled
) {
179 switch (cso
->depth
.func
) {
181 case PIPE_FUNC_LEQUAL
:
182 so
->ez_state
= VC5_EZ_LT_LE
;
184 case PIPE_FUNC_GREATER
:
185 case PIPE_FUNC_GEQUAL
:
186 so
->ez_state
= VC5_EZ_GT_GE
;
188 case PIPE_FUNC_NEVER
:
189 case PIPE_FUNC_EQUAL
:
190 so
->ez_state
= VC5_EZ_UNDECIDED
;
193 so
->ez_state
= VC5_EZ_DISABLED
;
197 /* If stencil is enabled and it's not a no-op, then it would
200 if (cso
->stencil
[0].enabled
&&
201 (cso
->stencil
[0].zfail_op
!= PIPE_STENCIL_OP_KEEP
||
202 cso
->stencil
[0].func
!= PIPE_FUNC_ALWAYS
||
203 (cso
->stencil
[1].enabled
&&
204 (cso
->stencil
[1].zfail_op
!= PIPE_STENCIL_OP_KEEP
&&
205 cso
->stencil
[1].func
!= PIPE_FUNC_ALWAYS
)))) {
206 so
->ez_state
= VC5_EZ_DISABLED
;
210 const struct pipe_stencil_state
*front
= &cso
->stencil
[0];
211 const struct pipe_stencil_state
*back
= &cso
->stencil
[1];
213 if (front
->enabled
) {
214 STATIC_ASSERT(sizeof(so
->stencil_front
) >=
215 cl_packet_length(STENCIL_CFG
));
216 v3dx_pack(&so
->stencil_front
, STENCIL_CFG
, config
) {
217 config
.front_config
= true;
218 /* If !back->enabled, then the front values should be
219 * used for both front and back-facing primitives.
221 config
.back_config
= !back
->enabled
;
223 config
.stencil_write_mask
= front
->writemask
;
224 config
.stencil_test_mask
= front
->valuemask
;
226 config
.stencil_test_function
= front
->func
;
227 config
.stencil_pass_op
=
228 translate_stencil_op(front
->zpass_op
);
229 config
.depth_test_fail_op
=
230 translate_stencil_op(front
->zfail_op
);
231 config
.stencil_test_fail_op
=
232 translate_stencil_op(front
->fail_op
);
236 STATIC_ASSERT(sizeof(so
->stencil_back
) >=
237 cl_packet_length(STENCIL_CFG
));
238 v3dx_pack(&so
->stencil_back
, STENCIL_CFG
, config
) {
239 config
.front_config
= false;
240 config
.back_config
= true;
242 config
.stencil_write_mask
= back
->writemask
;
243 config
.stencil_test_mask
= back
->valuemask
;
245 config
.stencil_test_function
= back
->func
;
246 config
.stencil_pass_op
=
247 translate_stencil_op(back
->zpass_op
);
248 config
.depth_test_fail_op
=
249 translate_stencil_op(back
->zfail_op
);
250 config
.stencil_test_fail_op
=
251 translate_stencil_op(back
->fail_op
);
259 v3d_set_polygon_stipple(struct pipe_context
*pctx
,
260 const struct pipe_poly_stipple
*stipple
)
262 struct v3d_context
*v3d
= v3d_context(pctx
);
263 v3d
->stipple
= *stipple
;
264 v3d
->dirty
|= VC5_DIRTY_STIPPLE
;
268 v3d_set_scissor_states(struct pipe_context
*pctx
,
270 unsigned num_scissors
,
271 const struct pipe_scissor_state
*scissor
)
273 struct v3d_context
*v3d
= v3d_context(pctx
);
275 v3d
->scissor
= *scissor
;
276 v3d
->dirty
|= VC5_DIRTY_SCISSOR
;
280 v3d_set_viewport_states(struct pipe_context
*pctx
,
282 unsigned num_viewports
,
283 const struct pipe_viewport_state
*viewport
)
285 struct v3d_context
*v3d
= v3d_context(pctx
);
286 v3d
->viewport
= *viewport
;
287 v3d
->dirty
|= VC5_DIRTY_VIEWPORT
;
291 v3d_set_vertex_buffers(struct pipe_context
*pctx
,
292 unsigned start_slot
, unsigned count
,
293 const struct pipe_vertex_buffer
*vb
)
295 struct v3d_context
*v3d
= v3d_context(pctx
);
296 struct v3d_vertexbuf_stateobj
*so
= &v3d
->vertexbuf
;
298 util_set_vertex_buffers_mask(so
->vb
, &so
->enabled_mask
, vb
,
300 so
->count
= util_last_bit(so
->enabled_mask
);
302 v3d
->dirty
|= VC5_DIRTY_VTXBUF
;
306 v3d_blend_state_bind(struct pipe_context
*pctx
, void *hwcso
)
308 struct v3d_context
*v3d
= v3d_context(pctx
);
310 v3d
->dirty
|= VC5_DIRTY_BLEND
;
314 v3d_rasterizer_state_bind(struct pipe_context
*pctx
, void *hwcso
)
316 struct v3d_context
*v3d
= v3d_context(pctx
);
317 v3d
->rasterizer
= hwcso
;
318 v3d
->dirty
|= VC5_DIRTY_RASTERIZER
;
322 v3d_zsa_state_bind(struct pipe_context
*pctx
, void *hwcso
)
324 struct v3d_context
*v3d
= v3d_context(pctx
);
326 v3d
->dirty
|= VC5_DIRTY_ZSA
;
330 v3d_vertex_state_create(struct pipe_context
*pctx
, unsigned num_elements
,
331 const struct pipe_vertex_element
*elements
)
333 struct v3d_context
*v3d
= v3d_context(pctx
);
334 struct v3d_vertex_stateobj
*so
= CALLOC_STRUCT(v3d_vertex_stateobj
);
339 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
340 so
->num_elements
= num_elements
;
342 for (int i
= 0; i
< so
->num_elements
; i
++) {
343 const struct pipe_vertex_element
*elem
= &elements
[i
];
344 const struct util_format_description
*desc
=
345 util_format_description(elem
->src_format
);
346 uint32_t r_size
= desc
->channel
[0].size
;
348 const uint32_t size
=
349 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD
);
351 v3dx_pack(&so
->attrs
[i
* size
],
352 GL_SHADER_STATE_ATTRIBUTE_RECORD
, attr
) {
353 /* vec_size == 0 means 4 */
354 attr
.vec_size
= desc
->nr_channels
& 3;
355 attr
.signed_int_type
= (desc
->channel
[0].type
==
356 UTIL_FORMAT_TYPE_SIGNED
);
358 attr
.normalized_int_type
= desc
->channel
[0].normalized
;
359 attr
.read_as_int_uint
= desc
->channel
[0].pure_integer
;
360 attr
.instance_divisor
= MIN2(elem
->instance_divisor
,
363 switch (desc
->channel
[0].type
) {
364 case UTIL_FORMAT_TYPE_FLOAT
:
366 attr
.type
= ATTRIBUTE_FLOAT
;
368 assert(r_size
== 16);
369 attr
.type
= ATTRIBUTE_HALF_FLOAT
;
373 case UTIL_FORMAT_TYPE_SIGNED
:
374 case UTIL_FORMAT_TYPE_UNSIGNED
:
377 attr
.type
= ATTRIBUTE_INT
;
380 attr
.type
= ATTRIBUTE_SHORT
;
383 attr
.type
= ATTRIBUTE_INT2_10_10_10
;
386 attr
.type
= ATTRIBUTE_BYTE
;
390 "format %s unsupported\n",
392 attr
.type
= ATTRIBUTE_BYTE
;
399 "format %s unsupported\n",
406 /* Set up the default attribute values in case any of the vertex
410 u_upload_alloc(v3d
->state_uploader
, 0,
411 V3D_MAX_VS_INPUTS
* sizeof(float), 16,
412 &so
->defaults_offset
, &so
->defaults
, (void **)&attrs
);
414 for (int i
= 0; i
< V3D_MAX_VS_INPUTS
/ 4; i
++) {
415 attrs
[i
* 4 + 0] = 0;
416 attrs
[i
* 4 + 1] = 0;
417 attrs
[i
* 4 + 2] = 0;
418 if (i
< so
->num_elements
&&
419 util_format_is_pure_integer(so
->pipe
[i
].src_format
)) {
420 attrs
[i
* 4 + 3] = 1;
422 attrs
[i
* 4 + 3] = fui(1.0);
426 u_upload_unmap(v3d
->state_uploader
);
431 v3d_vertex_state_delete(struct pipe_context
*pctx
, void *hwcso
)
433 struct v3d_vertex_stateobj
*so
= hwcso
;
435 pipe_resource_reference(&so
->defaults
, NULL
);
440 v3d_vertex_state_bind(struct pipe_context
*pctx
, void *hwcso
)
442 struct v3d_context
*v3d
= v3d_context(pctx
);
444 v3d
->dirty
|= VC5_DIRTY_VTXSTATE
;
448 v3d_set_constant_buffer(struct pipe_context
*pctx
, uint shader
, uint index
,
449 const struct pipe_constant_buffer
*cb
)
451 struct v3d_context
*v3d
= v3d_context(pctx
);
452 struct v3d_constbuf_stateobj
*so
= &v3d
->constbuf
[shader
];
454 util_copy_constant_buffer(&so
->cb
[index
], cb
);
456 /* Note that the state tracker can unbind constant buffers by
460 so
->enabled_mask
&= ~(1 << index
);
461 so
->dirty_mask
&= ~(1 << index
);
465 so
->enabled_mask
|= 1 << index
;
466 so
->dirty_mask
|= 1 << index
;
467 v3d
->dirty
|= VC5_DIRTY_CONSTBUF
;
471 v3d_set_framebuffer_state(struct pipe_context
*pctx
,
472 const struct pipe_framebuffer_state
*framebuffer
)
474 struct v3d_context
*v3d
= v3d_context(pctx
);
475 struct pipe_framebuffer_state
*cso
= &v3d
->framebuffer
;
479 util_copy_framebuffer_state(cso
, framebuffer
);
481 v3d
->swap_color_rb
= 0;
482 v3d
->blend_dst_alpha_one
= 0;
483 for (int i
= 0; i
< v3d
->framebuffer
.nr_cbufs
; i
++) {
484 struct pipe_surface
*cbuf
= v3d
->framebuffer
.cbufs
[i
];
487 struct v3d_surface
*v3d_cbuf
= v3d_surface(cbuf
);
489 const struct util_format_description
*desc
=
490 util_format_description(cbuf
->format
);
492 /* For BGRA8 formats (DRI window system default format), we
493 * need to swap R and B, since the HW's format is RGBA8. On
494 * V3D 4.1+, the RCL can swap R and B on load/store.
496 if (v3d
->screen
->devinfo
.ver
< 41 && v3d_cbuf
->swap_rb
)
497 v3d
->swap_color_rb
|= 1 << i
;
499 if (desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
500 v3d
->blend_dst_alpha_one
|= 1 << i
;
503 v3d
->dirty
|= VC5_DIRTY_FRAMEBUFFER
;
506 static enum V3DX(Wrap_Mode
)
507 translate_wrap(uint32_t pipe_wrap
, bool using_nearest
)
510 case PIPE_TEX_WRAP_REPEAT
:
511 return V3D_WRAP_MODE_REPEAT
;
512 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
513 return V3D_WRAP_MODE_CLAMP
;
514 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
515 return V3D_WRAP_MODE_MIRROR
;
516 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
517 return V3D_WRAP_MODE_BORDER
;
518 case PIPE_TEX_WRAP_CLAMP
:
519 return (using_nearest
?
520 V3D_WRAP_MODE_CLAMP
:
521 V3D_WRAP_MODE_BORDER
);
523 unreachable("Unknown wrap mode");
529 v3d_create_sampler_state(struct pipe_context
*pctx
,
530 const struct pipe_sampler_state
*cso
)
532 MAYBE_UNUSED
struct v3d_context
*v3d
= v3d_context(pctx
);
533 struct v3d_sampler_state
*so
= CALLOC_STRUCT(v3d_sampler_state
);
538 memcpy(so
, cso
, sizeof(*cso
));
540 bool either_nearest
=
541 (cso
->mag_img_filter
== PIPE_TEX_MIPFILTER_NEAREST
||
542 cso
->min_img_filter
== PIPE_TEX_MIPFILTER_NEAREST
);
544 #if V3D_VERSION >= 40
546 u_upload_alloc(v3d
->state_uploader
, 0,
547 cl_packet_length(SAMPLER_STATE
),
548 32, /* XXX: 8 for unextended samplers. */
549 &so
->sampler_state_offset
,
553 v3dx_pack(map
, SAMPLER_STATE
, sampler
) {
554 sampler
.wrap_i_border
= false;
556 sampler
.wrap_s
= translate_wrap(cso
->wrap_s
, either_nearest
);
557 sampler
.wrap_t
= translate_wrap(cso
->wrap_t
, either_nearest
);
558 sampler
.wrap_r
= translate_wrap(cso
->wrap_r
, either_nearest
);
560 sampler
.fixed_bias
= cso
->lod_bias
;
561 sampler
.depth_compare_function
= cso
->compare_func
;
563 sampler
.min_filter_nearest
=
564 cso
->min_img_filter
== PIPE_TEX_FILTER_NEAREST
;
565 sampler
.mag_filter_nearest
=
566 cso
->mag_img_filter
== PIPE_TEX_FILTER_NEAREST
;
567 sampler
.mip_filter_nearest
=
568 cso
->min_mip_filter
!= PIPE_TEX_MIPFILTER_LINEAR
;
570 sampler
.min_level_of_detail
= MIN2(MAX2(0, cso
->min_lod
),
572 sampler
.max_level_of_detail
= MIN2(cso
->max_lod
, 15);
574 /* If we're not doing inter-miplevel filtering, we need to
575 * clamp the LOD so that we only sample from baselevel.
576 * However, we need to still allow the calculated LOD to be
577 * fractionally over the baselevel, so that the HW can decide
578 * between the min and mag filters.
580 if (cso
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
581 sampler
.min_level_of_detail
=
582 MIN2(sampler
.min_level_of_detail
, 1.0 / 256.0);
583 sampler
.max_level_of_detail
=
584 MIN2(sampler
.max_level_of_detail
, 1.0 / 256.0);
587 if (cso
->max_anisotropy
) {
588 sampler
.anisotropy_enable
= true;
590 if (cso
->max_anisotropy
> 8)
591 sampler
.maximum_anisotropy
= 3;
592 else if (cso
->max_anisotropy
> 4)
593 sampler
.maximum_anisotropy
= 2;
594 else if (cso
->max_anisotropy
> 2)
595 sampler
.maximum_anisotropy
= 1;
598 sampler
.border_color_mode
= V3D_BORDER_COLOR_FOLLOWS
;
599 /* XXX: The border color field is in the TMU blending format
600 * (32, f16, or i16), and we need to customize it based on
603 * XXX: for compat alpha formats, we need the alpha field to
604 * be in the red channel.
606 sampler
.border_color_red
=
607 util_float_to_half(cso
->border_color
.f
[0]);
608 sampler
.border_color_green
=
609 util_float_to_half(cso
->border_color
.f
[1]);
610 sampler
.border_color_blue
=
611 util_float_to_half(cso
->border_color
.f
[2]);
612 sampler
.border_color_alpha
=
613 util_float_to_half(cso
->border_color
.f
[3]);
616 #else /* V3D_VERSION < 40 */
617 v3dx_pack(&so
->p0
, TEXTURE_UNIFORM_PARAMETER_0_CFG_MODE1
, p0
) {
618 p0
.s_wrap_mode
= translate_wrap(cso
->wrap_s
, either_nearest
);
619 p0
.t_wrap_mode
= translate_wrap(cso
->wrap_t
, either_nearest
);
620 p0
.r_wrap_mode
= translate_wrap(cso
->wrap_r
, either_nearest
);
623 v3dx_pack(&so
->texture_shader_state
, TEXTURE_SHADER_STATE
, tex
) {
624 tex
.depth_compare_function
= cso
->compare_func
;
625 tex
.fixed_bias
= cso
->lod_bias
;
627 #endif /* V3D_VERSION < 40 */
632 v3d_sampler_states_bind(struct pipe_context
*pctx
,
633 enum pipe_shader_type shader
, unsigned start
,
634 unsigned nr
, void **hwcso
)
636 struct v3d_context
*v3d
= v3d_context(pctx
);
637 struct v3d_texture_stateobj
*stage_tex
= &v3d
->tex
[shader
];
643 for (i
= 0; i
< nr
; i
++) {
646 stage_tex
->samplers
[i
] = hwcso
[i
];
649 for (; i
< stage_tex
->num_samplers
; i
++) {
650 stage_tex
->samplers
[i
] = NULL
;
653 stage_tex
->num_samplers
= new_nr
;
657 v3d_sampler_state_delete(struct pipe_context
*pctx
,
660 struct pipe_sampler_state
*psampler
= hwcso
;
661 struct v3d_sampler_state
*sampler
= v3d_sampler_state(psampler
);
663 pipe_resource_reference(&sampler
->sampler_state
, NULL
);
667 #if V3D_VERSION >= 40
669 translate_swizzle(unsigned char pipe_swizzle
)
671 switch (pipe_swizzle
) {
680 return 2 + pipe_swizzle
;
682 unreachable("unknown swizzle");
688 v3d_setup_texture_shader_state(struct V3DX(TEXTURE_SHADER_STATE
) *tex
,
689 struct pipe_resource
*prsc
,
690 int base_level
, int last_level
,
691 int first_layer
, int last_layer
)
693 struct v3d_resource
*rsc
= v3d_resource(prsc
);
694 int msaa_scale
= prsc
->nr_samples
> 1 ? 2 : 1;
696 tex
->image_width
= prsc
->width0
* msaa_scale
;
697 tex
->image_height
= prsc
->height0
* msaa_scale
;
699 #if V3D_VERSION >= 40
700 /* On 4.x, the height of a 1D texture is redefined to be the
701 * upper 14 bits of the width (which is only usable with txf).
703 if (prsc
->target
== PIPE_TEXTURE_1D
||
704 prsc
->target
== PIPE_TEXTURE_1D_ARRAY
) {
705 tex
->image_height
= tex
->image_width
>> 14;
709 if (prsc
->target
== PIPE_TEXTURE_3D
) {
710 tex
->image_depth
= prsc
->depth0
;
712 tex
->image_depth
= (last_layer
- first_layer
) + 1;
715 tex
->base_level
= base_level
;
716 #if V3D_VERSION >= 40
717 tex
->max_level
= last_level
;
718 /* Note that we don't have a job to reference the texture's sBO
719 * at state create time, so any time this sampler view is used
720 * we need to add the texture to the job.
722 tex
->texture_base_pointer
=
725 v3d_layer_offset(prsc
, 0, first_layer
));
727 tex
->array_stride_64_byte_aligned
= rsc
->cube_map_stride
/ 64;
729 /* Since other platform devices may produce UIF images even
730 * when they're not big enough for V3D to assume they're UIF,
731 * we force images with level 0 as UIF to be always treated
734 tex
->level_0_is_strictly_uif
=
735 (rsc
->slices
[0].tiling
== VC5_TILING_UIF_XOR
||
736 rsc
->slices
[0].tiling
== VC5_TILING_UIF_NO_XOR
);
737 tex
->level_0_xor_enable
= (rsc
->slices
[0].tiling
== VC5_TILING_UIF_XOR
);
739 if (tex
->level_0_is_strictly_uif
)
740 tex
->level_0_ub_pad
= rsc
->slices
[0].ub_pad
;
742 #if V3D_VERSION >= 40
743 if (tex
->uif_xor_disable
||
744 tex
->level_0_is_strictly_uif
) {
745 tex
->extended
= true;
747 #endif /* V3D_VERSION >= 40 */
750 static struct pipe_sampler_view
*
751 v3d_create_sampler_view(struct pipe_context
*pctx
, struct pipe_resource
*prsc
,
752 const struct pipe_sampler_view
*cso
)
754 struct v3d_context
*v3d
= v3d_context(pctx
);
755 struct v3d_screen
*screen
= v3d
->screen
;
756 struct v3d_sampler_view
*so
= CALLOC_STRUCT(v3d_sampler_view
);
757 struct v3d_resource
*rsc
= v3d_resource(prsc
);
764 pipe_reference(NULL
, &prsc
->reference
);
766 /* Compute the sampler view's swizzle up front. This will be plugged
767 * into either the sampler (for 16-bit returns) or the shader's
768 * texture key (for 32)
770 uint8_t view_swizzle
[4] = {
776 const uint8_t *fmt_swizzle
=
777 v3d_get_format_swizzle(&screen
->devinfo
, so
->base
.format
);
778 util_format_compose_swizzles(fmt_swizzle
, view_swizzle
, so
->swizzle
);
780 so
->base
.texture
= prsc
;
781 so
->base
.reference
.count
= 1;
782 so
->base
.context
= pctx
;
784 if (rsc
->separate_stencil
&&
785 cso
->format
== PIPE_FORMAT_X32_S8X24_UINT
) {
786 rsc
= rsc
->separate_stencil
;
790 /* V3D still doesn't support sampling from raster textures, so we will
791 * have to copy to a temporary tiled texture.
793 if (!rsc
->tiled
&& !(prsc
->target
== PIPE_TEXTURE_1D
||
794 prsc
->target
== PIPE_TEXTURE_1D_ARRAY
)) {
795 struct v3d_resource
*shadow_parent
= rsc
;
796 struct pipe_resource tmpl
= {
797 .target
= prsc
->target
,
798 .format
= prsc
->format
,
799 .width0
= u_minify(prsc
->width0
,
800 cso
->u
.tex
.first_level
),
801 .height0
= u_minify(prsc
->height0
,
802 cso
->u
.tex
.first_level
),
805 .bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
,
806 .last_level
= cso
->u
.tex
.last_level
- cso
->u
.tex
.first_level
,
807 .nr_samples
= prsc
->nr_samples
,
810 /* Create the shadow texture. The rest of the sampler view
811 * setup will use the shadow.
813 prsc
= v3d_resource_create(pctx
->screen
, &tmpl
);
818 rsc
= v3d_resource(prsc
);
820 /* Flag it as needing update of the contents from the parent. */
821 rsc
->writes
= shadow_parent
->writes
- 1;
826 pipe_resource_reference(&so
->texture
, prsc
);
830 #if V3D_VERSION >= 40
831 so
->bo
= v3d_bo_alloc(v3d
->screen
,
832 cl_packet_length(TEXTURE_SHADER_STATE
), "sampler");
833 map
= v3d_bo_map(so
->bo
);
834 #else /* V3D_VERSION < 40 */
835 STATIC_ASSERT(sizeof(so
->texture_shader_state
) >=
836 cl_packet_length(TEXTURE_SHADER_STATE
));
837 map
= &so
->texture_shader_state
;
840 v3dx_pack(map
, TEXTURE_SHADER_STATE
, tex
) {
841 v3d_setup_texture_shader_state(&tex
, prsc
,
842 cso
->u
.tex
.first_level
,
843 cso
->u
.tex
.last_level
,
844 cso
->u
.tex
.first_layer
,
845 cso
->u
.tex
.last_layer
);
847 tex
.srgb
= util_format_is_srgb(cso
->format
);
849 #if V3D_VERSION >= 40
850 tex
.swizzle_r
= translate_swizzle(so
->swizzle
[0]);
851 tex
.swizzle_g
= translate_swizzle(so
->swizzle
[1]);
852 tex
.swizzle_b
= translate_swizzle(so
->swizzle
[2]);
853 tex
.swizzle_a
= translate_swizzle(so
->swizzle
[3]);
856 if (prsc
->nr_samples
> 1 && V3D_VERSION
< 40) {
857 /* Using texture views to reinterpret formats on our
858 * MSAA textures won't work, because we don't lay out
859 * the bits in memory as it's expected -- for example,
860 * RGBA8 and RGB10_A2 are compatible in the
861 * ARB_texture_view spec, but in HW we lay them out as
862 * 32bpp RGBA8 and 64bpp RGBA16F. Just assert for now
865 * We explicitly allow remapping S8Z24 to RGBA8888 for
866 * v3d_blit.c's stencil blits.
868 assert((util_format_linear(cso
->format
) ==
869 util_format_linear(prsc
->format
)) ||
870 (prsc
->format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
&&
871 cso
->format
== PIPE_FORMAT_R8G8B8A8_UNORM
));
872 uint32_t output_image_format
=
873 v3d_get_rt_format(&screen
->devinfo
, cso
->format
);
874 uint32_t internal_type
;
875 uint32_t internal_bpp
;
876 v3d_get_internal_type_bpp_for_output_format(&screen
->devinfo
,
881 switch (internal_type
) {
882 case V3D_INTERNAL_TYPE_8
:
883 tex
.texture_type
= TEXTURE_DATA_FORMAT_RGBA8
;
885 case V3D_INTERNAL_TYPE_16F
:
886 tex
.texture_type
= TEXTURE_DATA_FORMAT_RGBA16F
;
889 unreachable("Bad MSAA texture type");
892 /* sRGB was stored in the tile buffer as linear and
893 * would have been encoded to sRGB on resolved tile
894 * buffer store. Note that this means we would need
895 * shader code if we wanted to read an MSAA sRGB
896 * texture without sRGB decode.
900 tex
.texture_type
= v3d_get_tex_format(&screen
->devinfo
,
909 v3d_sampler_view_destroy(struct pipe_context
*pctx
,
910 struct pipe_sampler_view
*psview
)
912 struct v3d_sampler_view
*sview
= v3d_sampler_view(psview
);
914 v3d_bo_unreference(&sview
->bo
);
915 pipe_resource_reference(&psview
->texture
, NULL
);
916 pipe_resource_reference(&sview
->texture
, NULL
);
921 v3d_set_sampler_views(struct pipe_context
*pctx
,
922 enum pipe_shader_type shader
,
923 unsigned start
, unsigned nr
,
924 struct pipe_sampler_view
**views
)
926 struct v3d_context
*v3d
= v3d_context(pctx
);
927 struct v3d_texture_stateobj
*stage_tex
= &v3d
->tex
[shader
];
933 for (i
= 0; i
< nr
; i
++) {
936 pipe_sampler_view_reference(&stage_tex
->textures
[i
], views
[i
]);
939 for (; i
< stage_tex
->num_textures
; i
++) {
940 pipe_sampler_view_reference(&stage_tex
->textures
[i
], NULL
);
943 stage_tex
->num_textures
= new_nr
;
946 static struct pipe_stream_output_target
*
947 v3d_create_stream_output_target(struct pipe_context
*pctx
,
948 struct pipe_resource
*prsc
,
949 unsigned buffer_offset
,
950 unsigned buffer_size
)
952 struct pipe_stream_output_target
*target
;
954 target
= CALLOC_STRUCT(pipe_stream_output_target
);
958 pipe_reference_init(&target
->reference
, 1);
959 pipe_resource_reference(&target
->buffer
, prsc
);
961 target
->context
= pctx
;
962 target
->buffer_offset
= buffer_offset
;
963 target
->buffer_size
= buffer_size
;
969 v3d_stream_output_target_destroy(struct pipe_context
*pctx
,
970 struct pipe_stream_output_target
*target
)
972 pipe_resource_reference(&target
->buffer
, NULL
);
977 v3d_set_stream_output_targets(struct pipe_context
*pctx
,
978 unsigned num_targets
,
979 struct pipe_stream_output_target
**targets
,
980 const unsigned *offsets
)
982 struct v3d_context
*ctx
= v3d_context(pctx
);
983 struct v3d_streamout_stateobj
*so
= &ctx
->streamout
;
986 assert(num_targets
<= ARRAY_SIZE(so
->targets
));
988 for (i
= 0; i
< num_targets
; i
++) {
989 if (offsets
[i
] != -1)
990 so
->offsets
[i
] = offsets
[i
];
992 pipe_so_target_reference(&so
->targets
[i
], targets
[i
]);
995 for (; i
< so
->num_targets
; i
++)
996 pipe_so_target_reference(&so
->targets
[i
], NULL
);
998 so
->num_targets
= num_targets
;
1000 ctx
->dirty
|= VC5_DIRTY_STREAMOUT
;
1004 v3d_set_shader_buffers(struct pipe_context
*pctx
,
1005 enum pipe_shader_type shader
,
1006 unsigned start
, unsigned count
,
1007 const struct pipe_shader_buffer
*buffers
)
1009 struct v3d_context
*v3d
= v3d_context(pctx
);
1010 struct v3d_ssbo_stateobj
*so
= &v3d
->ssbo
[shader
];
1014 for (unsigned i
= 0; i
< count
; i
++) {
1015 unsigned n
= i
+ start
;
1016 struct pipe_shader_buffer
*buf
= &so
->sb
[n
];
1018 if ((buf
->buffer
== buffers
[i
].buffer
) &&
1019 (buf
->buffer_offset
== buffers
[i
].buffer_offset
) &&
1020 (buf
->buffer_size
== buffers
[i
].buffer_size
))
1025 buf
->buffer_offset
= buffers
[i
].buffer_offset
;
1026 buf
->buffer_size
= buffers
[i
].buffer_size
;
1027 pipe_resource_reference(&buf
->buffer
, buffers
[i
].buffer
);
1030 so
->enabled_mask
|= 1 << n
;
1032 so
->enabled_mask
&= ~(1 << n
);
1035 mask
= ((1 << count
) - 1) << start
;
1037 for (unsigned i
= 0; i
< count
; i
++) {
1038 unsigned n
= i
+ start
;
1039 struct pipe_shader_buffer
*buf
= &so
->sb
[n
];
1041 pipe_resource_reference(&buf
->buffer
, NULL
);
1044 so
->enabled_mask
&= ~mask
;
1047 v3d
->dirty
|= VC5_DIRTY_SSBO
;
1051 v3d_create_image_view_texture_shader_state(struct v3d_context
*v3d
,
1052 struct v3d_shaderimg_stateobj
*so
,
1055 #if V3D_VERSION >= 40
1056 struct v3d_image_view
*iview
= &so
->si
[img
];
1059 u_upload_alloc(v3d
->uploader
, 0, cl_packet_length(TEXTURE_SHADER_STATE
),
1061 &iview
->tex_state_offset
,
1065 struct pipe_resource
*prsc
= iview
->base
.resource
;
1067 v3dx_pack(map
, TEXTURE_SHADER_STATE
, tex
) {
1068 v3d_setup_texture_shader_state(&tex
, prsc
,
1069 iview
->base
.u
.tex
.level
,
1070 iview
->base
.u
.tex
.level
,
1071 iview
->base
.u
.tex
.first_layer
,
1072 iview
->base
.u
.tex
.last_layer
);
1074 tex
.swizzle_r
= translate_swizzle(PIPE_SWIZZLE_X
);
1075 tex
.swizzle_g
= translate_swizzle(PIPE_SWIZZLE_Y
);
1076 tex
.swizzle_b
= translate_swizzle(PIPE_SWIZZLE_Z
);
1077 tex
.swizzle_a
= translate_swizzle(PIPE_SWIZZLE_W
);
1079 tex
.texture_type
= v3d_get_tex_format(&v3d
->screen
->devinfo
,
1080 iview
->base
.format
);
1082 #else /* V3D_VERSION < 40 */
1083 /* V3D 3.x doesn't use support shader image load/store operations on
1084 * textures, so it would get lowered in the shader to general memory
1091 v3d_set_shader_images(struct pipe_context
*pctx
,
1092 enum pipe_shader_type shader
,
1093 unsigned start
, unsigned count
,
1094 const struct pipe_image_view
*images
)
1096 struct v3d_context
*v3d
= v3d_context(pctx
);
1097 struct v3d_shaderimg_stateobj
*so
= &v3d
->shaderimg
[shader
];
1100 for (unsigned i
= 0; i
< count
; i
++) {
1101 unsigned n
= i
+ start
;
1102 struct v3d_image_view
*iview
= &so
->si
[n
];
1104 if ((iview
->base
.resource
== images
[i
].resource
) &&
1105 (iview
->base
.format
== images
[i
].format
) &&
1106 (iview
->base
.access
== images
[i
].access
) &&
1107 !memcmp(&iview
->base
.u
, &images
[i
].u
,
1108 sizeof(iview
->base
.u
)))
1111 util_copy_image_view(&iview
->base
, &images
[i
]);
1113 if (iview
->base
.resource
) {
1114 so
->enabled_mask
|= 1 << n
;
1115 v3d_create_image_view_texture_shader_state(v3d
,
1119 so
->enabled_mask
&= ~(1 << n
);
1120 pipe_resource_reference(&iview
->tex_state
, NULL
);
1124 for (unsigned i
= 0; i
< count
; i
++) {
1125 unsigned n
= i
+ start
;
1126 struct v3d_image_view
*iview
= &so
->si
[n
];
1128 pipe_resource_reference(&iview
->base
.resource
, NULL
);
1129 pipe_resource_reference(&iview
->tex_state
, NULL
);
1133 so
->enabled_mask
= 0;
1135 so
->enabled_mask
&= ~(((1 << count
) - 1) << start
);
1138 v3d
->dirty
|= VC5_DIRTY_SHADER_IMAGE
;
1142 v3dX(state_init
)(struct pipe_context
*pctx
)
1144 pctx
->set_blend_color
= v3d_set_blend_color
;
1145 pctx
->set_stencil_ref
= v3d_set_stencil_ref
;
1146 pctx
->set_clip_state
= v3d_set_clip_state
;
1147 pctx
->set_sample_mask
= v3d_set_sample_mask
;
1148 pctx
->set_constant_buffer
= v3d_set_constant_buffer
;
1149 pctx
->set_framebuffer_state
= v3d_set_framebuffer_state
;
1150 pctx
->set_polygon_stipple
= v3d_set_polygon_stipple
;
1151 pctx
->set_scissor_states
= v3d_set_scissor_states
;
1152 pctx
->set_viewport_states
= v3d_set_viewport_states
;
1154 pctx
->set_vertex_buffers
= v3d_set_vertex_buffers
;
1156 pctx
->create_blend_state
= v3d_create_blend_state
;
1157 pctx
->bind_blend_state
= v3d_blend_state_bind
;
1158 pctx
->delete_blend_state
= v3d_generic_cso_state_delete
;
1160 pctx
->create_rasterizer_state
= v3d_create_rasterizer_state
;
1161 pctx
->bind_rasterizer_state
= v3d_rasterizer_state_bind
;
1162 pctx
->delete_rasterizer_state
= v3d_generic_cso_state_delete
;
1164 pctx
->create_depth_stencil_alpha_state
= v3d_create_depth_stencil_alpha_state
;
1165 pctx
->bind_depth_stencil_alpha_state
= v3d_zsa_state_bind
;
1166 pctx
->delete_depth_stencil_alpha_state
= v3d_generic_cso_state_delete
;
1168 pctx
->create_vertex_elements_state
= v3d_vertex_state_create
;
1169 pctx
->delete_vertex_elements_state
= v3d_vertex_state_delete
;
1170 pctx
->bind_vertex_elements_state
= v3d_vertex_state_bind
;
1172 pctx
->create_sampler_state
= v3d_create_sampler_state
;
1173 pctx
->delete_sampler_state
= v3d_sampler_state_delete
;
1174 pctx
->bind_sampler_states
= v3d_sampler_states_bind
;
1176 pctx
->create_sampler_view
= v3d_create_sampler_view
;
1177 pctx
->sampler_view_destroy
= v3d_sampler_view_destroy
;
1178 pctx
->set_sampler_views
= v3d_set_sampler_views
;
1180 pctx
->set_shader_buffers
= v3d_set_shader_buffers
;
1181 pctx
->set_shader_images
= v3d_set_shader_images
;
1183 pctx
->create_stream_output_target
= v3d_create_stream_output_target
;
1184 pctx
->stream_output_target_destroy
= v3d_stream_output_target_destroy
;
1185 pctx
->set_stream_output_targets
= v3d_set_stream_output_targets
;