1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 * Wrap the cso cache & hash mechanisms in a simplified
32 * pipe-driver-specific interface.
34 * @author Zack Rusin <zack@tungstengraphics.com>
35 * @author Keith Whitwell <keith@tungstengraphics.com>
38 #include "pipe/p_state.h"
39 #include "util/u_draw.h"
40 #include "util/u_framebuffer.h"
41 #include "util/u_inlines.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.h"
44 #include "util/u_vbuf.h"
45 #include "tgsi/tgsi_parse.h"
47 #include "cso_cache/cso_context.h"
48 #include "cso_cache/cso_cache.h"
49 #include "cso_cache/cso_hash.h"
50 #include "cso_context.h"
54 * Info related to samplers and sampler views.
55 * We have one of these for fragment samplers and another for vertex samplers.
60 void *samplers
[PIPE_MAX_SAMPLERS
];
64 void *samplers
[PIPE_MAX_SAMPLERS
];
67 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
68 unsigned nr_samplers_saved
;
70 struct pipe_sampler_view
*views
[PIPE_MAX_SAMPLERS
];
73 struct pipe_sampler_view
*views_saved
[PIPE_MAX_SAMPLERS
];
74 unsigned nr_views_saved
;
80 struct pipe_context
*pipe
;
81 struct cso_cache
*cache
;
84 boolean has_geometry_shader
;
85 boolean has_streamout
;
87 struct sampler_info samplers
[PIPE_SHADER_TYPES
];
89 uint nr_vertex_buffers
;
90 struct pipe_vertex_buffer vertex_buffers
[PIPE_MAX_ATTRIBS
];
92 uint nr_vertex_buffers_saved
;
93 struct pipe_vertex_buffer vertex_buffers_saved
[PIPE_MAX_ATTRIBS
];
95 unsigned nr_so_targets
;
96 struct pipe_stream_output_target
*so_targets
[PIPE_MAX_SO_BUFFERS
];
98 unsigned nr_so_targets_saved
;
99 struct pipe_stream_output_target
*so_targets_saved
[PIPE_MAX_SO_BUFFERS
];
101 /** Current and saved state.
102 * The saved state is used as a 1-deep stack.
104 void *blend
, *blend_saved
;
105 void *depth_stencil
, *depth_stencil_saved
;
106 void *rasterizer
, *rasterizer_saved
;
107 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
108 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
109 void *velements
, *velements_saved
;
111 struct pipe_clip_state clip
;
112 struct pipe_clip_state clip_saved
;
114 struct pipe_framebuffer_state fb
, fb_saved
;
115 struct pipe_viewport_state vp
, vp_saved
;
116 struct pipe_blend_color blend_color
;
117 unsigned sample_mask
;
118 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
122 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
124 struct cso_blend
*cso
= (struct cso_blend
*)state
;
126 if (ctx
->blend
== cso
->data
)
129 if (cso
->delete_state
)
130 cso
->delete_state(cso
->context
, cso
->data
);
135 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
137 struct cso_depth_stencil_alpha
*cso
=
138 (struct cso_depth_stencil_alpha
*)state
;
140 if (ctx
->depth_stencil
== cso
->data
)
143 if (cso
->delete_state
)
144 cso
->delete_state(cso
->context
, cso
->data
);
150 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
152 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
153 if (cso
->delete_state
)
154 cso
->delete_state(cso
->context
, cso
->data
);
159 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
161 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
163 if (ctx
->rasterizer
== cso
->data
)
165 if (cso
->delete_state
)
166 cso
->delete_state(cso
->context
, cso
->data
);
171 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
174 struct cso_velements
*cso
= (struct cso_velements
*)state
;
176 if (ctx
->velements
== cso
->data
)
179 if (cso
->delete_state
)
180 cso
->delete_state(cso
->context
, cso
->data
);
186 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
187 void *state
, enum cso_cache_type type
)
191 return delete_blend_state(ctx
, state
);
194 return delete_sampler_state(ctx
, state
);
196 case CSO_DEPTH_STENCIL_ALPHA
:
197 return delete_depth_stencil_state(ctx
, state
);
200 return delete_rasterizer_state(ctx
, state
);
203 return delete_vertex_elements(ctx
, state
);
213 sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
214 int max_size
, void *user_data
)
216 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
217 /* if we're approach the maximum size, remove fourth of the entries
218 * otherwise every subsequent call will go through the same */
219 int hash_size
= cso_hash_size(hash
);
220 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
221 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
222 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
223 if (hash_size
> max_size
)
224 to_remove
+= hash_size
- max_size
;
226 /*remove elements until we're good */
227 /*fixme: currently we pick the nodes to remove at random*/
228 void *cso
= cso_hash_iter_data(iter
);
229 if (delete_cso(ctx
, cso
, type
)) {
230 iter
= cso_hash_erase(hash
, iter
);
233 iter
= cso_hash_iter_next(iter
);
237 static void cso_init_vbuf(struct cso_context
*cso
)
239 struct u_vbuf_caps caps
;
241 u_vbuf_get_caps(cso
->pipe
->screen
, &caps
);
243 /* Install u_vbuf if there is anything unsupported. */
244 if (!caps
.buffer_offset_unaligned
||
245 !caps
.buffer_stride_unaligned
||
246 !caps
.velem_src_offset_unaligned
||
247 !caps
.format_fixed32
||
248 !caps
.format_float16
||
249 !caps
.format_float64
||
250 !caps
.format_norm32
||
251 !caps
.format_scaled32
||
252 !caps
.user_vertex_buffers
) {
253 cso
->vbuf
= u_vbuf_create(cso
->pipe
, &caps
);
257 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
259 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
263 assert(PIPE_MAX_SAMPLERS
== PIPE_MAX_VERTEX_SAMPLERS
);
265 ctx
->cache
= cso_cache_create();
266 if (ctx
->cache
== NULL
)
268 cso_cache_set_sanitize_callback(ctx
->cache
,
276 /* Enable for testing: */
277 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
279 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
280 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
281 ctx
->has_geometry_shader
= TRUE
;
283 if (pipe
->screen
->get_param(pipe
->screen
,
284 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
285 ctx
->has_streamout
= TRUE
;
291 cso_destroy_context( ctx
);
296 * Prior to context destruction, this function unbinds all state objects.
298 void cso_release_all( struct cso_context
*ctx
)
303 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
304 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
305 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
306 if (ctx
->pipe
->bind_vertex_sampler_states
)
307 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
308 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
309 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
310 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
311 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
312 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, 0, NULL
);
313 if (ctx
->pipe
->set_vertex_sampler_views
)
314 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, 0, NULL
);
315 if (ctx
->pipe
->set_stream_output_targets
)
316 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, 0);
319 /* free fragment samplers, views */
320 for (shader
= 0; shader
< Elements(ctx
->samplers
); shader
++) {
321 struct sampler_info
*info
= &ctx
->samplers
[shader
];
322 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
323 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
324 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
328 util_unreference_framebuffer_state(&ctx
->fb
);
329 util_unreference_framebuffer_state(&ctx
->fb_saved
);
331 util_copy_vertex_buffers(ctx
->vertex_buffers
,
332 &ctx
->nr_vertex_buffers
,
334 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
335 &ctx
->nr_vertex_buffers_saved
,
338 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
339 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
340 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
344 cso_cache_delete( ctx
->cache
);
351 * Free the CSO context. NOTE: the state tracker should have previously called
354 void cso_destroy_context( struct cso_context
*ctx
)
358 u_vbuf_destroy(ctx
->vbuf
);
364 /* Those function will either find the state of the given template
365 * in the cache or they will create a new state from the given
366 * template, insert it in the cache and return it.
370 * If the driver returns 0 from the create method then they will assign
371 * the data member of the cso to be the template itself.
374 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
375 const struct pipe_blend_state
*templ
)
377 unsigned key_size
, hash_key
;
378 struct cso_hash_iter iter
;
381 key_size
= templ
->independent_blend_enable
?
382 sizeof(struct pipe_blend_state
) :
383 (char *)&(templ
->rt
[1]) - (char *)templ
;
384 hash_key
= cso_construct_key((void*)templ
, key_size
);
385 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
,
386 (void*)templ
, key_size
);
388 if (cso_hash_iter_is_null(iter
)) {
389 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
391 return PIPE_ERROR_OUT_OF_MEMORY
;
393 memset(&cso
->state
, 0, sizeof cso
->state
);
394 memcpy(&cso
->state
, templ
, key_size
);
395 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
396 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
397 cso
->context
= ctx
->pipe
;
399 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
400 if (cso_hash_iter_is_null(iter
)) {
402 return PIPE_ERROR_OUT_OF_MEMORY
;
408 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
411 if (ctx
->blend
!= handle
) {
413 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
418 void cso_save_blend(struct cso_context
*ctx
)
420 assert(!ctx
->blend_saved
);
421 ctx
->blend_saved
= ctx
->blend
;
424 void cso_restore_blend(struct cso_context
*ctx
)
426 if (ctx
->blend
!= ctx
->blend_saved
) {
427 ctx
->blend
= ctx
->blend_saved
;
428 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
430 ctx
->blend_saved
= NULL
;
436 cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
437 const struct pipe_depth_stencil_alpha_state
*templ
)
439 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
440 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
441 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
443 CSO_DEPTH_STENCIL_ALPHA
,
444 (void*)templ
, key_size
);
447 if (cso_hash_iter_is_null(iter
)) {
448 struct cso_depth_stencil_alpha
*cso
=
449 MALLOC(sizeof(struct cso_depth_stencil_alpha
));
451 return PIPE_ERROR_OUT_OF_MEMORY
;
453 memcpy(&cso
->state
, templ
, sizeof(*templ
));
454 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
,
457 (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
458 cso
->context
= ctx
->pipe
;
460 iter
= cso_insert_state(ctx
->cache
, hash_key
,
461 CSO_DEPTH_STENCIL_ALPHA
, cso
);
462 if (cso_hash_iter_is_null(iter
)) {
464 return PIPE_ERROR_OUT_OF_MEMORY
;
470 handle
= ((struct cso_depth_stencil_alpha
*)
471 cso_hash_iter_data(iter
))->data
;
474 if (ctx
->depth_stencil
!= handle
) {
475 ctx
->depth_stencil
= handle
;
476 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
481 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
483 assert(!ctx
->depth_stencil_saved
);
484 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
487 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
489 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
490 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
491 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
,
492 ctx
->depth_stencil_saved
);
494 ctx
->depth_stencil_saved
= NULL
;
499 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
500 const struct pipe_rasterizer_state
*templ
)
502 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
503 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
504 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
507 (void*)templ
, key_size
);
510 if (cso_hash_iter_is_null(iter
)) {
511 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
513 return PIPE_ERROR_OUT_OF_MEMORY
;
515 memcpy(&cso
->state
, templ
, sizeof(*templ
));
516 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
518 (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
519 cso
->context
= ctx
->pipe
;
521 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
522 if (cso_hash_iter_is_null(iter
)) {
524 return PIPE_ERROR_OUT_OF_MEMORY
;
530 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
533 if (ctx
->rasterizer
!= handle
) {
534 ctx
->rasterizer
= handle
;
535 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
540 void cso_save_rasterizer(struct cso_context
*ctx
)
542 assert(!ctx
->rasterizer_saved
);
543 ctx
->rasterizer_saved
= ctx
->rasterizer
;
546 void cso_restore_rasterizer(struct cso_context
*ctx
)
548 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
549 ctx
->rasterizer
= ctx
->rasterizer_saved
;
550 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
552 ctx
->rasterizer_saved
= NULL
;
557 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
560 if (ctx
->fragment_shader
!= handle
) {
561 ctx
->fragment_shader
= handle
;
562 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
567 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
569 if (handle
== ctx
->fragment_shader
) {
570 /* unbind before deleting */
571 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
572 ctx
->fragment_shader
= NULL
;
574 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
577 void cso_save_fragment_shader(struct cso_context
*ctx
)
579 assert(!ctx
->fragment_shader_saved
);
580 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
583 void cso_restore_fragment_shader(struct cso_context
*ctx
)
585 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
586 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
587 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
589 ctx
->fragment_shader_saved
= NULL
;
593 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
596 if (ctx
->vertex_shader
!= handle
) {
597 ctx
->vertex_shader
= handle
;
598 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
603 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
605 if (handle
== ctx
->vertex_shader
) {
606 /* unbind before deleting */
607 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
608 ctx
->vertex_shader
= NULL
;
610 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
613 void cso_save_vertex_shader(struct cso_context
*ctx
)
615 assert(!ctx
->vertex_shader_saved
);
616 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
619 void cso_restore_vertex_shader(struct cso_context
*ctx
)
621 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
622 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
623 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
625 ctx
->vertex_shader_saved
= NULL
;
629 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
630 const struct pipe_framebuffer_state
*fb
)
632 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
633 util_copy_framebuffer_state(&ctx
->fb
, fb
);
634 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
639 void cso_save_framebuffer(struct cso_context
*ctx
)
641 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
644 void cso_restore_framebuffer(struct cso_context
*ctx
)
646 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
647 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
648 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
649 util_unreference_framebuffer_state(&ctx
->fb_saved
);
654 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
655 const struct pipe_viewport_state
*vp
)
657 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
659 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
664 void cso_save_viewport(struct cso_context
*ctx
)
666 ctx
->vp_saved
= ctx
->vp
;
670 void cso_restore_viewport(struct cso_context
*ctx
)
672 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
673 ctx
->vp
= ctx
->vp_saved
;
674 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
679 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
680 const struct pipe_blend_color
*bc
)
682 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
683 ctx
->blend_color
= *bc
;
684 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
689 enum pipe_error
cso_set_sample_mask(struct cso_context
*ctx
,
690 unsigned sample_mask
)
692 if (ctx
->sample_mask
!= sample_mask
) {
693 ctx
->sample_mask
= sample_mask
;
694 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
699 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
700 const struct pipe_stencil_ref
*sr
)
702 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
703 ctx
->stencil_ref
= *sr
;
704 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
709 void cso_save_stencil_ref(struct cso_context
*ctx
)
711 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
715 void cso_restore_stencil_ref(struct cso_context
*ctx
)
717 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
,
718 sizeof(ctx
->stencil_ref
))) {
719 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
720 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
724 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
727 assert(ctx
->has_geometry_shader
|| !handle
);
729 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
730 ctx
->geometry_shader
= handle
;
731 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
736 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
738 if (handle
== ctx
->geometry_shader
) {
739 /* unbind before deleting */
740 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
741 ctx
->geometry_shader
= NULL
;
743 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
746 void cso_save_geometry_shader(struct cso_context
*ctx
)
748 if (!ctx
->has_geometry_shader
) {
752 assert(!ctx
->geometry_shader_saved
);
753 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
756 void cso_restore_geometry_shader(struct cso_context
*ctx
)
758 if (!ctx
->has_geometry_shader
) {
762 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
763 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
764 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
766 ctx
->geometry_shader_saved
= NULL
;
772 clip_state_cpy(struct pipe_clip_state
*dst
,
773 const struct pipe_clip_state
*src
)
775 memcpy(dst
->ucp
, src
->ucp
, sizeof(dst
->ucp
));
779 clip_state_cmp(const struct pipe_clip_state
*a
,
780 const struct pipe_clip_state
*b
)
782 return memcmp(a
->ucp
, b
->ucp
, sizeof(a
->ucp
));
786 cso_set_clip(struct cso_context
*ctx
,
787 const struct pipe_clip_state
*clip
)
789 if (clip_state_cmp(&ctx
->clip
, clip
)) {
790 clip_state_cpy(&ctx
->clip
, clip
);
791 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
796 cso_save_clip(struct cso_context
*ctx
)
798 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
802 cso_restore_clip(struct cso_context
*ctx
)
804 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
805 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
806 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
811 cso_set_vertex_elements(struct cso_context
*ctx
,
813 const struct pipe_vertex_element
*states
)
815 struct u_vbuf
*vbuf
= ctx
->vbuf
;
816 unsigned key_size
, hash_key
;
817 struct cso_hash_iter iter
;
819 struct cso_velems_state velems_state
;
822 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
826 /* Need to include the count into the stored state data too.
827 * Otherwise first few count pipe_vertex_elements could be identical
828 * even if count is different, and there's no guarantee the hash would
829 * be different in that case neither.
831 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
832 velems_state
.count
= count
;
833 memcpy(velems_state
.velems
, states
,
834 sizeof(struct pipe_vertex_element
) * count
);
835 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
836 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
,
837 (void*)&velems_state
, key_size
);
839 if (cso_hash_iter_is_null(iter
)) {
840 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
842 return PIPE_ERROR_OUT_OF_MEMORY
;
844 memcpy(&cso
->state
, &velems_state
, key_size
);
845 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
,
846 &cso
->state
.velems
[0]);
848 (cso_state_callback
) ctx
->pipe
->delete_vertex_elements_state
;
849 cso
->context
= ctx
->pipe
;
851 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
852 if (cso_hash_iter_is_null(iter
)) {
854 return PIPE_ERROR_OUT_OF_MEMORY
;
860 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
863 if (ctx
->velements
!= handle
) {
864 ctx
->velements
= handle
;
865 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
870 void cso_save_vertex_elements(struct cso_context
*ctx
)
872 struct u_vbuf
*vbuf
= ctx
->vbuf
;
875 u_vbuf_save_vertex_elements(vbuf
);
879 assert(!ctx
->velements_saved
);
880 ctx
->velements_saved
= ctx
->velements
;
883 void cso_restore_vertex_elements(struct cso_context
*ctx
)
885 struct u_vbuf
*vbuf
= ctx
->vbuf
;
888 u_vbuf_restore_vertex_elements(vbuf
);
892 if (ctx
->velements
!= ctx
->velements_saved
) {
893 ctx
->velements
= ctx
->velements_saved
;
894 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
896 ctx
->velements_saved
= NULL
;
901 void cso_set_vertex_buffers(struct cso_context
*ctx
,
903 const struct pipe_vertex_buffer
*buffers
)
905 struct u_vbuf
*vbuf
= ctx
->vbuf
;
908 u_vbuf_set_vertex_buffers(vbuf
, count
, buffers
);
912 if (count
!= ctx
->nr_vertex_buffers
||
913 memcmp(buffers
, ctx
->vertex_buffers
,
914 sizeof(struct pipe_vertex_buffer
) * count
) != 0) {
915 util_copy_vertex_buffers(ctx
->vertex_buffers
, &ctx
->nr_vertex_buffers
,
917 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, count
, buffers
);
921 void cso_save_vertex_buffers(struct cso_context
*ctx
)
923 struct u_vbuf
*vbuf
= ctx
->vbuf
;
926 u_vbuf_save_vertex_buffers(vbuf
);
930 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
931 &ctx
->nr_vertex_buffers_saved
,
933 ctx
->nr_vertex_buffers
);
936 void cso_restore_vertex_buffers(struct cso_context
*ctx
)
939 struct u_vbuf
*vbuf
= ctx
->vbuf
;
942 u_vbuf_restore_vertex_buffers(vbuf
);
946 util_copy_vertex_buffers(ctx
->vertex_buffers
,
947 &ctx
->nr_vertex_buffers
,
948 ctx
->vertex_buffers_saved
,
949 ctx
->nr_vertex_buffers_saved
);
951 for (i
= 0; i
< ctx
->nr_vertex_buffers_saved
; i
++) {
952 pipe_resource_reference(&ctx
->vertex_buffers_saved
[i
].buffer
, NULL
);
954 ctx
->nr_vertex_buffers_saved
= 0;
956 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, ctx
->nr_vertex_buffers
,
957 ctx
->vertex_buffers
);
961 /**************** fragment/vertex sampler view state *************************/
963 static enum pipe_error
964 single_sampler(struct cso_context
*ctx
,
965 struct sampler_info
*info
,
967 const struct pipe_sampler_state
*templ
)
972 unsigned key_size
= sizeof(struct pipe_sampler_state
);
973 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
974 struct cso_hash_iter iter
=
975 cso_find_state_template(ctx
->cache
,
976 hash_key
, CSO_SAMPLER
,
977 (void *) templ
, key_size
);
979 if (cso_hash_iter_is_null(iter
)) {
980 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
982 return PIPE_ERROR_OUT_OF_MEMORY
;
984 memcpy(&cso
->state
, templ
, sizeof(*templ
));
985 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
987 (cso_state_callback
) ctx
->pipe
->delete_sampler_state
;
988 cso
->context
= ctx
->pipe
;
990 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
991 if (cso_hash_iter_is_null(iter
)) {
993 return PIPE_ERROR_OUT_OF_MEMORY
;
999 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
1003 info
->samplers
[idx
] = handle
;
1009 cso_single_sampler(struct cso_context
*ctx
,
1010 unsigned shader_stage
,
1012 const struct pipe_sampler_state
*templ
)
1014 return single_sampler(ctx
, &ctx
->samplers
[shader_stage
], idx
, templ
);
1020 single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1022 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1025 /* find highest non-null sampler */
1026 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1027 if (info
->samplers
[i
- 1] != NULL
)
1031 info
->nr_samplers
= i
;
1033 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1034 memcmp(info
->hw
.samplers
,
1036 info
->nr_samplers
* sizeof(void *)) != 0)
1038 memcpy(info
->hw
.samplers
,
1040 info
->nr_samplers
* sizeof(void *));
1041 info
->hw
.nr_samplers
= info
->nr_samplers
;
1043 switch (shader_stage
) {
1044 case PIPE_SHADER_FRAGMENT
:
1045 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
,
1049 case PIPE_SHADER_VERTEX
:
1050 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
1054 case PIPE_SHADER_GEOMETRY
:
1055 ctx
->pipe
->bind_geometry_sampler_states(ctx
->pipe
,
1060 assert(!"bad shader type in single_sampler_done()");
1066 cso_single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1068 single_sampler_done(ctx
, shader_stage
);
1073 * If the function encouters any errors it will return the
1074 * last one. Done to always try to set as many samplers
1078 cso_set_samplers(struct cso_context
*ctx
,
1079 unsigned shader_stage
,
1081 const struct pipe_sampler_state
**templates
)
1083 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1085 enum pipe_error temp
, error
= PIPE_OK
;
1090 for (i
= 0; i
< nr
; i
++) {
1091 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1092 if (temp
!= PIPE_OK
)
1096 for ( ; i
< info
->nr_samplers
; i
++) {
1097 temp
= single_sampler(ctx
, info
, i
, NULL
);
1098 if (temp
!= PIPE_OK
)
1102 single_sampler_done(ctx
, shader_stage
);
1108 cso_save_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1110 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1111 info
->nr_samplers_saved
= info
->nr_samplers
;
1112 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1117 cso_restore_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1119 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1120 info
->nr_samplers
= info
->nr_samplers_saved
;
1121 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1122 single_sampler_done(ctx
, shader_stage
);
1127 cso_set_sampler_views(struct cso_context
*ctx
,
1128 unsigned shader_stage
,
1130 struct pipe_sampler_view
**views
)
1132 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1135 /* reference new views */
1136 for (i
= 0; i
< count
; i
++) {
1137 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1139 /* unref extra old views, if any */
1140 for (; i
< info
->nr_views
; i
++) {
1141 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1144 info
->nr_views
= count
;
1146 /* bind the new sampler views */
1147 switch (shader_stage
) {
1148 case PIPE_SHADER_FRAGMENT
:
1149 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, count
, info
->views
);
1151 case PIPE_SHADER_VERTEX
:
1152 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, count
, info
->views
);
1154 case PIPE_SHADER_GEOMETRY
:
1155 ctx
->pipe
->set_geometry_sampler_views(ctx
->pipe
, count
, info
->views
);
1158 assert(!"bad shader type in cso_set_sampler_views()");
1164 cso_save_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1166 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1169 info
->nr_views_saved
= info
->nr_views
;
1171 for (i
= 0; i
< info
->nr_views
; i
++) {
1172 assert(!info
->views_saved
[i
]);
1173 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1179 cso_restore_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1181 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1182 unsigned i
, nr_saved
= info
->nr_views_saved
;
1184 for (i
= 0; i
< nr_saved
; i
++) {
1185 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1186 /* move the reference from one pointer to another */
1187 info
->views
[i
] = info
->views_saved
[i
];
1188 info
->views_saved
[i
] = NULL
;
1190 for (; i
< info
->nr_views
; i
++) {
1191 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1194 /* bind the old/saved sampler views */
1195 switch (shader_stage
) {
1196 case PIPE_SHADER_FRAGMENT
:
1197 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1199 case PIPE_SHADER_VERTEX
:
1200 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1202 case PIPE_SHADER_GEOMETRY
:
1203 ctx
->pipe
->set_geometry_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1206 assert(!"bad shader type in cso_restore_sampler_views()");
1209 info
->nr_views
= nr_saved
;
1210 info
->nr_views_saved
= 0;
1215 cso_set_stream_outputs(struct cso_context
*ctx
,
1216 unsigned num_targets
,
1217 struct pipe_stream_output_target
**targets
,
1218 unsigned append_bitmask
)
1220 struct pipe_context
*pipe
= ctx
->pipe
;
1223 if (!ctx
->has_streamout
) {
1224 assert(num_targets
== 0);
1228 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1229 /* Nothing to do. */
1233 /* reference new targets */
1234 for (i
= 0; i
< num_targets
; i
++) {
1235 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1237 /* unref extra old targets, if any */
1238 for (; i
< ctx
->nr_so_targets
; i
++) {
1239 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1242 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1244 ctx
->nr_so_targets
= num_targets
;
1248 cso_save_stream_outputs(struct cso_context
*ctx
)
1252 if (!ctx
->has_streamout
) {
1256 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1258 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1259 assert(!ctx
->so_targets_saved
[i
]);
1260 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1265 cso_restore_stream_outputs(struct cso_context
*ctx
)
1267 struct pipe_context
*pipe
= ctx
->pipe
;
1270 if (!ctx
->has_streamout
) {
1274 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1275 /* Nothing to do. */
1279 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1280 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1281 /* move the reference from one pointer to another */
1282 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1283 ctx
->so_targets_saved
[i
] = NULL
;
1285 for (; i
< ctx
->nr_so_targets
; i
++) {
1286 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1289 /* ~0 means append */
1290 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1291 ctx
->so_targets
, ~0);
1293 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1294 ctx
->nr_so_targets_saved
= 0;
1300 cso_set_index_buffer(struct cso_context
*cso
,
1301 const struct pipe_index_buffer
*ib
)
1303 struct u_vbuf
*vbuf
= cso
->vbuf
;
1306 u_vbuf_set_index_buffer(vbuf
, ib
);
1308 struct pipe_context
*pipe
= cso
->pipe
;
1309 pipe
->set_index_buffer(pipe
, ib
);
1314 cso_draw_vbo(struct cso_context
*cso
,
1315 const struct pipe_draw_info
*info
)
1317 struct u_vbuf
*vbuf
= cso
->vbuf
;
1320 u_vbuf_draw_vbo(vbuf
, info
);
1322 struct pipe_context
*pipe
= cso
->pipe
;
1323 pipe
->draw_vbo(pipe
, info
);
1328 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1330 struct pipe_draw_info info
;
1332 util_draw_init_info(&info
);
1337 info
.min_index
= start
;
1338 info
.max_index
= start
+ count
- 1;
1340 cso_draw_vbo(cso
, &info
);