2 * Copyright © 2014 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_inlines.h"
27 #include "util/u_math.h"
28 #include "util/u_memory.h"
29 #include "util/u_helpers.h"
31 #include "vc4_context.h"
34 vc4_generic_cso_state_create(const void *src
, uint32_t size
)
36 void *dst
= calloc(1, size
);
39 memcpy(dst
, src
, size
);
44 vc4_generic_cso_state_delete(struct pipe_context
*pctx
, void *hwcso
)
50 vc4_set_blend_color(struct pipe_context
*pctx
,
51 const struct pipe_blend_color
*blend_color
)
53 struct vc4_context
*vc4
= vc4_context(pctx
);
54 vc4
->blend_color
= *blend_color
;
55 vc4
->dirty
|= VC4_DIRTY_BLEND_COLOR
;
59 vc4_set_stencil_ref(struct pipe_context
*pctx
,
60 const struct pipe_stencil_ref
*stencil_ref
)
62 struct vc4_context
*vc4
= vc4_context(pctx
);
63 vc4
->stencil_ref
=* stencil_ref
;
64 vc4
->dirty
|= VC4_DIRTY_STENCIL_REF
;
68 vc4_set_clip_state(struct pipe_context
*pctx
,
69 const struct pipe_clip_state
*clip
)
71 struct vc4_context
*vc4
= vc4_context(pctx
);
73 vc4
->dirty
|= VC4_DIRTY_CLIP
;
77 vc4_set_sample_mask(struct pipe_context
*pctx
, unsigned sample_mask
)
79 struct vc4_context
*vc4
= vc4_context(pctx
);
80 vc4
->sample_mask
= (uint16_t)sample_mask
;
81 vc4
->dirty
|= VC4_DIRTY_SAMPLE_MASK
;
85 float_to_187_half(float f
)
91 vc4_create_rasterizer_state(struct pipe_context
*pctx
,
92 const struct pipe_rasterizer_state
*cso
)
94 struct vc4_rasterizer_state
*so
;
96 so
= CALLOC_STRUCT(vc4_rasterizer_state
);
102 if (!(cso
->cull_face
& PIPE_FACE_FRONT
))
103 so
->config_bits
[0] |= VC4_CONFIG_BITS_ENABLE_PRIM_FRONT
;
104 if (!(cso
->cull_face
& PIPE_FACE_BACK
))
105 so
->config_bits
[0] |= VC4_CONFIG_BITS_ENABLE_PRIM_BACK
;
107 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
110 so
->point_size
= MAX2(cso
->point_size
, .125);
113 so
->config_bits
[0] |= VC4_CONFIG_BITS_CW_PRIMITIVES
;
115 if (cso
->offset_tri
) {
116 so
->config_bits
[0] |= VC4_CONFIG_BITS_ENABLE_DEPTH_OFFSET
;
118 so
->offset_units
= float_to_187_half(cso
->offset_units
);
119 so
->offset_factor
= float_to_187_half(cso
->offset_scale
);
125 /* Blend state is baked into shaders. */
127 vc4_create_blend_state(struct pipe_context
*pctx
,
128 const struct pipe_blend_state
*cso
)
130 return vc4_generic_cso_state_create(cso
, sizeof(*cso
));
134 * The TLB_STENCIL_SETUP data has a little bitfield for common writemask
135 * values, so you don't have to do a separate writemask setup.
138 tlb_stencil_setup_writemask(uint8_t mask
)
145 default: return 0xff;
150 tlb_stencil_setup_bits(const struct pipe_stencil_state
*state
,
151 uint8_t writemask_bits
)
153 static const uint8_t op_map
[] = {
154 [PIPE_STENCIL_OP_ZERO
] = 0,
155 [PIPE_STENCIL_OP_KEEP
] = 1,
156 [PIPE_STENCIL_OP_REPLACE
] = 2,
157 [PIPE_STENCIL_OP_INCR
] = 3,
158 [PIPE_STENCIL_OP_DECR
] = 4,
159 [PIPE_STENCIL_OP_INVERT
] = 5,
160 [PIPE_STENCIL_OP_INCR_WRAP
] = 6,
161 [PIPE_STENCIL_OP_DECR_WRAP
] = 7,
165 if (writemask_bits
!= 0xff)
166 bits
|= writemask_bits
<< 28;
167 bits
|= op_map
[state
->zfail_op
] << 25;
168 bits
|= op_map
[state
->zpass_op
] << 22;
169 bits
|= op_map
[state
->fail_op
] << 19;
170 bits
|= state
->func
<< 16;
171 /* Ref is filled in at uniform upload time */
172 bits
|= state
->valuemask
<< 0;
178 vc4_create_depth_stencil_alpha_state(struct pipe_context
*pctx
,
179 const struct pipe_depth_stencil_alpha_state
*cso
)
181 struct vc4_depth_stencil_alpha_state
*so
;
183 so
= CALLOC_STRUCT(vc4_depth_stencil_alpha_state
);
189 /* We always keep the early Z state correct, since a later state using
190 * early Z may want it.
192 so
->config_bits
[2] |= VC4_CONFIG_BITS_EARLY_Z_UPDATE
;
194 if (cso
->depth
.enabled
) {
195 if (cso
->depth
.writemask
) {
196 so
->config_bits
[1] |= VC4_CONFIG_BITS_Z_UPDATE
;
198 so
->config_bits
[1] |= (cso
->depth
.func
<<
199 VC4_CONFIG_BITS_DEPTH_FUNC_SHIFT
);
201 /* We only handle early Z in the < direction because otherwise
202 * we'd have to runtime guess which direction to set in the
205 if ((cso
->depth
.func
== PIPE_FUNC_LESS
||
206 cso
->depth
.func
== PIPE_FUNC_LEQUAL
) &&
207 (!cso
->stencil
[0].enabled
||
208 (cso
->stencil
[0].zfail_op
== PIPE_STENCIL_OP_KEEP
&&
209 (!cso
->stencil
[1].enabled
||
210 cso
->stencil
[1].zfail_op
== PIPE_STENCIL_OP_KEEP
)))) {
211 so
->config_bits
[2] |= VC4_CONFIG_BITS_EARLY_Z
;
214 so
->config_bits
[1] |= (PIPE_FUNC_ALWAYS
<<
215 VC4_CONFIG_BITS_DEPTH_FUNC_SHIFT
);
218 if (cso
->stencil
[0].enabled
) {
219 const struct pipe_stencil_state
*front
= &cso
->stencil
[0];
220 const struct pipe_stencil_state
*back
= &cso
->stencil
[1];
222 uint8_t front_writemask_bits
=
223 tlb_stencil_setup_writemask(front
->writemask
);
224 uint8_t back_writemask
= front
->writemask
;
225 uint8_t back_writemask_bits
= front_writemask_bits
;
227 so
->stencil_uniforms
[0] =
228 tlb_stencil_setup_bits(front
, front_writemask_bits
);
230 back_writemask
= back
->writemask
;
231 back_writemask_bits
=
232 tlb_stencil_setup_writemask(back
->writemask
);
234 so
->stencil_uniforms
[0] |= (1 << 30);
235 so
->stencil_uniforms
[1] =
236 tlb_stencil_setup_bits(back
, back_writemask_bits
);
237 so
->stencil_uniforms
[1] |= (2 << 30);
239 so
->stencil_uniforms
[0] |= (3 << 30);
242 if (front_writemask_bits
== 0xff ||
243 back_writemask_bits
== 0xff) {
244 so
->stencil_uniforms
[2] = (front
->writemask
|
245 (back_writemask
<< 8));
253 vc4_set_polygon_stipple(struct pipe_context
*pctx
,
254 const struct pipe_poly_stipple
*stipple
)
256 struct vc4_context
*vc4
= vc4_context(pctx
);
257 vc4
->stipple
= *stipple
;
258 vc4
->dirty
|= VC4_DIRTY_STIPPLE
;
262 vc4_set_scissor_states(struct pipe_context
*pctx
,
264 unsigned num_scissors
,
265 const struct pipe_scissor_state
*scissor
)
267 struct vc4_context
*vc4
= vc4_context(pctx
);
269 vc4
->scissor
= *scissor
;
270 vc4
->dirty
|= VC4_DIRTY_SCISSOR
;
274 vc4_set_viewport_states(struct pipe_context
*pctx
,
276 unsigned num_viewports
,
277 const struct pipe_viewport_state
*viewport
)
279 struct vc4_context
*vc4
= vc4_context(pctx
);
280 vc4
->viewport
= *viewport
;
281 vc4
->dirty
|= VC4_DIRTY_VIEWPORT
;
285 vc4_set_vertex_buffers(struct pipe_context
*pctx
,
286 unsigned start_slot
, unsigned count
,
287 const struct pipe_vertex_buffer
*vb
)
289 struct vc4_context
*vc4
= vc4_context(pctx
);
290 struct vc4_vertexbuf_stateobj
*so
= &vc4
->vertexbuf
;
292 util_set_vertex_buffers_mask(so
->vb
, &so
->enabled_mask
, vb
,
294 so
->count
= util_last_bit(so
->enabled_mask
);
296 vc4
->dirty
|= VC4_DIRTY_VTXBUF
;
300 vc4_set_index_buffer(struct pipe_context
*pctx
,
301 const struct pipe_index_buffer
*ib
)
303 struct vc4_context
*vc4
= vc4_context(pctx
);
306 assert(!ib
->user_buffer
);
308 if (ib
->index_size
== 4) {
309 struct pipe_resource tmpl
= *ib
->buffer
;
310 assert(tmpl
.format
== PIPE_FORMAT_R8_UNORM
);
311 assert(tmpl
.height0
== 1);
312 tmpl
.width0
= (tmpl
.width0
- ib
->offset
) / 2;
313 struct pipe_resource
*pshadow
=
314 vc4_resource_create(&vc4
->screen
->base
, &tmpl
);
315 struct vc4_resource
*shadow
= vc4_resource(pshadow
);
316 pipe_resource_reference(&shadow
->shadow_parent
, ib
->buffer
);
318 pipe_resource_reference(&vc4
->indexbuf
.buffer
, NULL
);
319 vc4
->indexbuf
.buffer
= pshadow
;
320 vc4
->indexbuf
.index_size
= 2;
322 pipe_resource_reference(&vc4
->indexbuf
.buffer
, ib
->buffer
);
323 vc4
->indexbuf
.index_size
= ib
->index_size
;
325 vc4
->indexbuf
.offset
= ib
->offset
;
327 pipe_resource_reference(&vc4
->indexbuf
.buffer
, NULL
);
330 vc4
->dirty
|= VC4_DIRTY_INDEXBUF
;
334 vc4_blend_state_bind(struct pipe_context
*pctx
, void *hwcso
)
336 struct vc4_context
*vc4
= vc4_context(pctx
);
338 vc4
->dirty
|= VC4_DIRTY_BLEND
;
342 vc4_rasterizer_state_bind(struct pipe_context
*pctx
, void *hwcso
)
344 struct vc4_context
*vc4
= vc4_context(pctx
);
345 struct vc4_rasterizer_state
*rast
= hwcso
;
347 if (vc4
->rasterizer
&& rast
&&
348 vc4
->rasterizer
->base
.flatshade
!= rast
->base
.flatshade
) {
349 vc4
->dirty
|= VC4_DIRTY_FLAT_SHADE_FLAGS
;
352 vc4
->rasterizer
= hwcso
;
353 vc4
->dirty
|= VC4_DIRTY_RASTERIZER
;
357 vc4_zsa_state_bind(struct pipe_context
*pctx
, void *hwcso
)
359 struct vc4_context
*vc4
= vc4_context(pctx
);
361 vc4
->dirty
|= VC4_DIRTY_ZSA
;
365 vc4_vertex_state_create(struct pipe_context
*pctx
, unsigned num_elements
,
366 const struct pipe_vertex_element
*elements
)
368 struct vc4_vertex_stateobj
*so
= CALLOC_STRUCT(vc4_vertex_stateobj
);
373 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
374 so
->num_elements
= num_elements
;
380 vc4_vertex_state_bind(struct pipe_context
*pctx
, void *hwcso
)
382 struct vc4_context
*vc4
= vc4_context(pctx
);
384 vc4
->dirty
|= VC4_DIRTY_VTXSTATE
;
388 vc4_set_constant_buffer(struct pipe_context
*pctx
, uint shader
, uint index
,
389 struct pipe_constant_buffer
*cb
)
391 struct vc4_context
*vc4
= vc4_context(pctx
);
392 struct vc4_constbuf_stateobj
*so
= &vc4
->constbuf
[shader
];
396 /* Note that the state tracker can unbind constant buffers by
400 so
->enabled_mask
&= ~(1 << index
);
401 so
->dirty_mask
&= ~(1 << index
);
406 so
->cb
[index
].buffer_offset
= cb
->buffer_offset
;
407 so
->cb
[index
].buffer_size
= cb
->buffer_size
;
408 so
->cb
[index
].user_buffer
= cb
->user_buffer
;
410 so
->enabled_mask
|= 1 << index
;
411 so
->dirty_mask
|= 1 << index
;
412 vc4
->dirty
|= VC4_DIRTY_CONSTBUF
;
416 vc4_set_framebuffer_state(struct pipe_context
*pctx
,
417 const struct pipe_framebuffer_state
*framebuffer
)
419 struct vc4_context
*vc4
= vc4_context(pctx
);
420 struct pipe_framebuffer_state
*cso
= &vc4
->framebuffer
;
425 for (i
= 0; i
< framebuffer
->nr_cbufs
; i
++)
426 pipe_surface_reference(&cso
->cbufs
[i
], framebuffer
->cbufs
[i
]);
427 for (; i
< vc4
->framebuffer
.nr_cbufs
; i
++)
428 pipe_surface_reference(&cso
->cbufs
[i
], NULL
);
430 cso
->nr_cbufs
= framebuffer
->nr_cbufs
;
432 pipe_surface_reference(&cso
->zsbuf
, framebuffer
->zsbuf
);
434 cso
->width
= framebuffer
->width
;
435 cso
->height
= framebuffer
->height
;
437 /* Nonzero texture mipmap levels are laid out as if they were in
438 * power-of-two-sized spaces. The renderbuffer config infers its
439 * stride from the width parameter, so we need to configure our
440 * framebuffer. Note that if the z/color buffers were mismatched
441 * sizes, we wouldn't be able to do this.
443 if (cso
->cbufs
[0] && cso
->cbufs
[0]->u
.tex
.level
) {
444 struct vc4_resource
*rsc
=
445 vc4_resource(cso
->cbufs
[0]->texture
);
447 (rsc
->slices
[cso
->cbufs
[0]->u
.tex
.level
].stride
/
449 } else if (cso
->zsbuf
&& cso
->zsbuf
->u
.tex
.level
){
450 struct vc4_resource
*rsc
=
451 vc4_resource(cso
->zsbuf
->texture
);
453 (rsc
->slices
[cso
->zsbuf
->u
.tex
.level
].stride
/
457 vc4
->dirty
|= VC4_DIRTY_FRAMEBUFFER
;
460 static struct vc4_texture_stateobj
*
461 vc4_get_stage_tex(struct vc4_context
*vc4
, unsigned shader
)
463 vc4
->dirty
|= VC4_DIRTY_TEXSTATE
;
466 case PIPE_SHADER_FRAGMENT
:
467 vc4
->dirty
|= VC4_DIRTY_FRAGTEX
;
468 return &vc4
->fragtex
;
470 case PIPE_SHADER_VERTEX
:
471 vc4
->dirty
|= VC4_DIRTY_VERTTEX
;
472 return &vc4
->verttex
;
475 fprintf(stderr
, "Unknown shader target %d\n", shader
);
481 vc4_create_sampler_state(struct pipe_context
*pctx
,
482 const struct pipe_sampler_state
*cso
)
484 return vc4_generic_cso_state_create(cso
, sizeof(*cso
));
488 vc4_sampler_states_bind(struct pipe_context
*pctx
,
489 unsigned shader
, unsigned start
,
490 unsigned nr
, void **hwcso
)
492 struct vc4_context
*vc4
= vc4_context(pctx
);
493 struct vc4_texture_stateobj
*stage_tex
= vc4_get_stage_tex(vc4
, shader
);
499 for (i
= 0; i
< nr
; i
++) {
502 stage_tex
->samplers
[i
] = hwcso
[i
];
503 stage_tex
->dirty_samplers
|= (1 << i
);
506 for (; i
< stage_tex
->num_samplers
; i
++) {
507 stage_tex
->samplers
[i
] = NULL
;
508 stage_tex
->dirty_samplers
|= (1 << i
);
511 stage_tex
->num_samplers
= new_nr
;
514 static struct pipe_sampler_view
*
515 vc4_create_sampler_view(struct pipe_context
*pctx
, struct pipe_resource
*prsc
,
516 const struct pipe_sampler_view
*cso
)
518 struct pipe_sampler_view
*so
= malloc(sizeof(*so
));
525 pipe_reference(NULL
, &prsc
->reference
);
527 /* There is no hardware level clamping, and the start address of a
528 * texture may be misaligned, so in that case we have to copy to a
531 if (so
->u
.tex
.first_level
) {
532 struct vc4_resource
*shadow_parent
= vc4_resource(prsc
);
533 struct pipe_resource tmpl
= shadow_parent
->base
.b
;
534 struct vc4_resource
*clone
;
536 tmpl
.width0
= u_minify(tmpl
.width0
, so
->u
.tex
.first_level
);
537 tmpl
.height0
= u_minify(tmpl
.height0
, so
->u
.tex
.first_level
);
538 tmpl
.last_level
= so
->u
.tex
.last_level
- so
->u
.tex
.first_level
;
540 prsc
= vc4_resource_create(pctx
->screen
, &tmpl
);
541 clone
= vc4_resource(prsc
);
542 clone
->shadow_parent
= &shadow_parent
->base
.b
;
543 /* Flag it as needing update of the contents from the parent. */
544 clone
->writes
= shadow_parent
->writes
- 1;
547 so
->reference
.count
= 1;
554 vc4_sampler_view_destroy(struct pipe_context
*pctx
,
555 struct pipe_sampler_view
*view
)
557 pipe_resource_reference(&view
->texture
, NULL
);
562 vc4_set_sampler_views(struct pipe_context
*pctx
, unsigned shader
,
563 unsigned start
, unsigned nr
,
564 struct pipe_sampler_view
**views
)
566 struct vc4_context
*vc4
= vc4_context(pctx
);
567 struct vc4_texture_stateobj
*stage_tex
= vc4_get_stage_tex(vc4
, shader
);
573 vc4
->dirty
|= VC4_DIRTY_TEXSTATE
;
575 for (i
= 0; i
< nr
; i
++) {
578 if (views
[i
]->u
.tex
.first_level
!= 0)
579 vc4_update_shadow_baselevel_texture(pctx
, views
[i
]);
581 pipe_sampler_view_reference(&stage_tex
->textures
[i
], views
[i
]);
582 stage_tex
->dirty_samplers
|= (1 << i
);
585 for (; i
< stage_tex
->num_textures
; i
++) {
586 pipe_sampler_view_reference(&stage_tex
->textures
[i
], NULL
);
587 stage_tex
->dirty_samplers
|= (1 << i
);
590 stage_tex
->num_textures
= new_nr
;
594 vc4_state_init(struct pipe_context
*pctx
)
596 pctx
->set_blend_color
= vc4_set_blend_color
;
597 pctx
->set_stencil_ref
= vc4_set_stencil_ref
;
598 pctx
->set_clip_state
= vc4_set_clip_state
;
599 pctx
->set_sample_mask
= vc4_set_sample_mask
;
600 pctx
->set_constant_buffer
= vc4_set_constant_buffer
;
601 pctx
->set_framebuffer_state
= vc4_set_framebuffer_state
;
602 pctx
->set_polygon_stipple
= vc4_set_polygon_stipple
;
603 pctx
->set_scissor_states
= vc4_set_scissor_states
;
604 pctx
->set_viewport_states
= vc4_set_viewport_states
;
606 pctx
->set_vertex_buffers
= vc4_set_vertex_buffers
;
607 pctx
->set_index_buffer
= vc4_set_index_buffer
;
609 pctx
->create_blend_state
= vc4_create_blend_state
;
610 pctx
->bind_blend_state
= vc4_blend_state_bind
;
611 pctx
->delete_blend_state
= vc4_generic_cso_state_delete
;
613 pctx
->create_rasterizer_state
= vc4_create_rasterizer_state
;
614 pctx
->bind_rasterizer_state
= vc4_rasterizer_state_bind
;
615 pctx
->delete_rasterizer_state
= vc4_generic_cso_state_delete
;
617 pctx
->create_depth_stencil_alpha_state
= vc4_create_depth_stencil_alpha_state
;
618 pctx
->bind_depth_stencil_alpha_state
= vc4_zsa_state_bind
;
619 pctx
->delete_depth_stencil_alpha_state
= vc4_generic_cso_state_delete
;
621 pctx
->create_vertex_elements_state
= vc4_vertex_state_create
;
622 pctx
->delete_vertex_elements_state
= vc4_generic_cso_state_delete
;
623 pctx
->bind_vertex_elements_state
= vc4_vertex_state_bind
;
625 pctx
->create_sampler_state
= vc4_create_sampler_state
;
626 pctx
->delete_sampler_state
= vc4_generic_cso_state_delete
;
627 pctx
->bind_sampler_states
= vc4_sampler_states_bind
;
629 pctx
->create_sampler_view
= vc4_create_sampler_view
;
630 pctx
->sampler_view_destroy
= vc4_sampler_view_destroy
;
631 pctx
->set_sampler_views
= vc4_set_sampler_views
;