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 fragment_samplers
;
88 struct sampler_info vertex_samplers
;
90 uint nr_vertex_buffers
;
91 struct pipe_vertex_buffer vertex_buffers
[PIPE_MAX_ATTRIBS
];
93 uint nr_vertex_buffers_saved
;
94 struct pipe_vertex_buffer vertex_buffers_saved
[PIPE_MAX_ATTRIBS
];
96 unsigned nr_so_targets
;
97 struct pipe_stream_output_target
*so_targets
[PIPE_MAX_SO_BUFFERS
];
99 unsigned nr_so_targets_saved
;
100 struct pipe_stream_output_target
*so_targets_saved
[PIPE_MAX_SO_BUFFERS
];
102 /** Current and saved state.
103 * The saved state is used as a 1-deep stack.
105 void *blend
, *blend_saved
;
106 void *depth_stencil
, *depth_stencil_saved
;
107 void *rasterizer
, *rasterizer_saved
;
108 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
109 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
110 void *velements
, *velements_saved
;
112 struct pipe_clip_state clip
;
113 struct pipe_clip_state clip_saved
;
115 struct pipe_framebuffer_state fb
, fb_saved
;
116 struct pipe_viewport_state vp
, vp_saved
;
117 struct pipe_blend_color blend_color
;
118 unsigned sample_mask
;
119 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
123 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
125 struct cso_blend
*cso
= (struct cso_blend
*)state
;
127 if (ctx
->blend
== cso
->data
)
130 if (cso
->delete_state
)
131 cso
->delete_state(cso
->context
, cso
->data
);
136 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
138 struct cso_depth_stencil_alpha
*cso
= (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
);
212 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
213 int max_size
, void *user_data
)
215 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
216 /* if we're approach the maximum size, remove fourth of the entries
217 * otherwise every subsequent call will go through the same */
218 int hash_size
= cso_hash_size(hash
);
219 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
220 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
221 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
222 if (hash_size
> max_size
)
223 to_remove
+= hash_size
- max_size
;
225 /*remove elements until we're good */
226 /*fixme: currently we pick the nodes to remove at random*/
227 void *cso
= cso_hash_iter_data(iter
);
228 if (delete_cso(ctx
, cso
, type
)) {
229 iter
= cso_hash_erase(hash
, iter
);
232 iter
= cso_hash_iter_next(iter
);
237 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
239 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
243 assert(PIPE_MAX_SAMPLERS
== PIPE_MAX_VERTEX_SAMPLERS
);
245 ctx
->cache
= cso_cache_create();
246 if (ctx
->cache
== NULL
)
248 cso_cache_set_sanitize_callback(ctx
->cache
,
254 /* Enable for testing: */
255 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
257 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
258 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
259 ctx
->has_geometry_shader
= TRUE
;
261 if (pipe
->screen
->get_param(pipe
->screen
,
262 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
263 ctx
->has_streamout
= TRUE
;
269 cso_destroy_context( ctx
);
273 void cso_install_vbuf(struct cso_context
*ctx
, struct u_vbuf
*vbuf
)
279 * Prior to context destruction, this function unbinds all state objects.
281 void cso_release_all( struct cso_context
*ctx
)
284 struct sampler_info
*info
;
287 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
288 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
289 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
290 if (ctx
->pipe
->bind_vertex_sampler_states
)
291 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
292 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
293 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
294 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
295 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
296 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, 0, NULL
);
297 if (ctx
->pipe
->set_vertex_sampler_views
)
298 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, 0, NULL
);
299 if (ctx
->pipe
->set_stream_output_targets
)
300 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, 0);
303 /* free fragment samplers, views */
304 info
= &ctx
->fragment_samplers
;
305 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
306 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
307 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
310 /* free vertex samplers, views */
311 info
= &ctx
->vertex_samplers
;
312 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
313 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
314 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
317 util_unreference_framebuffer_state(&ctx
->fb
);
318 util_unreference_framebuffer_state(&ctx
->fb_saved
);
320 util_copy_vertex_buffers(ctx
->vertex_buffers
,
321 &ctx
->nr_vertex_buffers
,
323 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
324 &ctx
->nr_vertex_buffers_saved
,
327 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
328 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
329 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
333 cso_cache_delete( ctx
->cache
);
340 * Free the CSO context. NOTE: the state tracker should have previously called
343 void cso_destroy_context( struct cso_context
*ctx
)
351 /* Those function will either find the state of the given template
352 * in the cache or they will create a new state from the given
353 * template, insert it in the cache and return it.
357 * If the driver returns 0 from the create method then they will assign
358 * the data member of the cso to be the template itself.
361 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
362 const struct pipe_blend_state
*templ
)
364 unsigned key_size
, hash_key
;
365 struct cso_hash_iter iter
;
368 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
369 (char *)&(templ
->rt
[1]) - (char *)templ
;
370 hash_key
= cso_construct_key((void*)templ
, key_size
);
371 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
373 if (cso_hash_iter_is_null(iter
)) {
374 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
376 return PIPE_ERROR_OUT_OF_MEMORY
;
378 memset(&cso
->state
, 0, sizeof cso
->state
);
379 memcpy(&cso
->state
, templ
, key_size
);
380 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
381 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
382 cso
->context
= ctx
->pipe
;
384 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
385 if (cso_hash_iter_is_null(iter
)) {
387 return PIPE_ERROR_OUT_OF_MEMORY
;
393 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
396 if (ctx
->blend
!= handle
) {
398 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
403 void cso_save_blend(struct cso_context
*ctx
)
405 assert(!ctx
->blend_saved
);
406 ctx
->blend_saved
= ctx
->blend
;
409 void cso_restore_blend(struct cso_context
*ctx
)
411 if (ctx
->blend
!= ctx
->blend_saved
) {
412 ctx
->blend
= ctx
->blend_saved
;
413 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
415 ctx
->blend_saved
= NULL
;
420 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
421 const struct pipe_depth_stencil_alpha_state
*templ
)
423 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
424 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
425 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
427 CSO_DEPTH_STENCIL_ALPHA
,
428 (void*)templ
, key_size
);
431 if (cso_hash_iter_is_null(iter
)) {
432 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
434 return PIPE_ERROR_OUT_OF_MEMORY
;
436 memcpy(&cso
->state
, templ
, sizeof(*templ
));
437 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
438 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
439 cso
->context
= ctx
->pipe
;
441 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
442 if (cso_hash_iter_is_null(iter
)) {
444 return PIPE_ERROR_OUT_OF_MEMORY
;
450 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
453 if (ctx
->depth_stencil
!= handle
) {
454 ctx
->depth_stencil
= handle
;
455 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
460 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
462 assert(!ctx
->depth_stencil_saved
);
463 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
466 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
468 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
469 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
470 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
472 ctx
->depth_stencil_saved
= NULL
;
477 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
478 const struct pipe_rasterizer_state
*templ
)
480 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
481 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
482 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
483 hash_key
, CSO_RASTERIZER
,
484 (void*)templ
, key_size
);
487 if (cso_hash_iter_is_null(iter
)) {
488 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
490 return PIPE_ERROR_OUT_OF_MEMORY
;
492 memcpy(&cso
->state
, templ
, sizeof(*templ
));
493 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
494 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
495 cso
->context
= ctx
->pipe
;
497 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
498 if (cso_hash_iter_is_null(iter
)) {
500 return PIPE_ERROR_OUT_OF_MEMORY
;
506 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
509 if (ctx
->rasterizer
!= handle
) {
510 ctx
->rasterizer
= handle
;
511 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
516 void cso_save_rasterizer(struct cso_context
*ctx
)
518 assert(!ctx
->rasterizer_saved
);
519 ctx
->rasterizer_saved
= ctx
->rasterizer
;
522 void cso_restore_rasterizer(struct cso_context
*ctx
)
524 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
525 ctx
->rasterizer
= ctx
->rasterizer_saved
;
526 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
528 ctx
->rasterizer_saved
= NULL
;
533 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
536 if (ctx
->fragment_shader
!= handle
) {
537 ctx
->fragment_shader
= handle
;
538 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
543 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
545 if (handle
== ctx
->fragment_shader
) {
546 /* unbind before deleting */
547 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
548 ctx
->fragment_shader
= NULL
;
550 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
553 void cso_save_fragment_shader(struct cso_context
*ctx
)
555 assert(!ctx
->fragment_shader_saved
);
556 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
559 void cso_restore_fragment_shader(struct cso_context
*ctx
)
561 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
562 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
563 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
565 ctx
->fragment_shader_saved
= NULL
;
569 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
572 if (ctx
->vertex_shader
!= handle
) {
573 ctx
->vertex_shader
= handle
;
574 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
579 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
581 if (handle
== ctx
->vertex_shader
) {
582 /* unbind before deleting */
583 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
584 ctx
->vertex_shader
= NULL
;
586 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
589 void cso_save_vertex_shader(struct cso_context
*ctx
)
591 assert(!ctx
->vertex_shader_saved
);
592 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
595 void cso_restore_vertex_shader(struct cso_context
*ctx
)
597 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
598 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
599 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
601 ctx
->vertex_shader_saved
= NULL
;
605 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
606 const struct pipe_framebuffer_state
*fb
)
608 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
609 util_copy_framebuffer_state(&ctx
->fb
, fb
);
610 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
615 void cso_save_framebuffer(struct cso_context
*ctx
)
617 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
620 void cso_restore_framebuffer(struct cso_context
*ctx
)
622 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
623 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
624 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
625 util_unreference_framebuffer_state(&ctx
->fb_saved
);
630 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
631 const struct pipe_viewport_state
*vp
)
633 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
635 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
640 void cso_save_viewport(struct cso_context
*ctx
)
642 ctx
->vp_saved
= ctx
->vp
;
646 void cso_restore_viewport(struct cso_context
*ctx
)
648 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
649 ctx
->vp
= ctx
->vp_saved
;
650 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
655 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
656 const struct pipe_blend_color
*bc
)
658 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
659 ctx
->blend_color
= *bc
;
660 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
665 enum pipe_error
cso_set_sample_mask(struct cso_context
*ctx
,
666 unsigned sample_mask
)
668 if (ctx
->sample_mask
!= sample_mask
) {
669 ctx
->sample_mask
= sample_mask
;
670 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
675 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
676 const struct pipe_stencil_ref
*sr
)
678 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
679 ctx
->stencil_ref
= *sr
;
680 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
685 void cso_save_stencil_ref(struct cso_context
*ctx
)
687 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
691 void cso_restore_stencil_ref(struct cso_context
*ctx
)
693 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
694 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
695 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
699 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
702 assert(ctx
->has_geometry_shader
|| !handle
);
704 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
705 ctx
->geometry_shader
= handle
;
706 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
711 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
713 if (handle
== ctx
->geometry_shader
) {
714 /* unbind before deleting */
715 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
716 ctx
->geometry_shader
= NULL
;
718 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
721 void cso_save_geometry_shader(struct cso_context
*ctx
)
723 if (!ctx
->has_geometry_shader
) {
727 assert(!ctx
->geometry_shader_saved
);
728 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
731 void cso_restore_geometry_shader(struct cso_context
*ctx
)
733 if (!ctx
->has_geometry_shader
) {
737 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
738 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
739 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
741 ctx
->geometry_shader_saved
= NULL
;
747 clip_state_cpy(struct pipe_clip_state
*dst
,
748 const struct pipe_clip_state
*src
)
750 memcpy(dst
->ucp
, src
->ucp
, sizeof(dst
->ucp
));
754 clip_state_cmp(const struct pipe_clip_state
*a
,
755 const struct pipe_clip_state
*b
)
757 return memcmp(a
->ucp
, b
->ucp
, sizeof(a
->ucp
));
761 cso_set_clip(struct cso_context
*ctx
,
762 const struct pipe_clip_state
*clip
)
764 if (clip_state_cmp(&ctx
->clip
, clip
)) {
765 clip_state_cpy(&ctx
->clip
, clip
);
766 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
771 cso_save_clip(struct cso_context
*ctx
)
773 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
777 cso_restore_clip(struct cso_context
*ctx
)
779 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
780 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
781 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
785 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
787 const struct pipe_vertex_element
*states
)
789 struct u_vbuf
*vbuf
= ctx
->vbuf
;
790 unsigned key_size
, hash_key
;
791 struct cso_hash_iter iter
;
793 struct cso_velems_state velems_state
;
796 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
800 /* need to include the count into the stored state data too.
801 Otherwise first few count pipe_vertex_elements could be identical even if count
802 is different, and there's no guarantee the hash would be different in that
804 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
805 velems_state
.count
= count
;
806 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
807 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
808 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
810 if (cso_hash_iter_is_null(iter
)) {
811 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
813 return PIPE_ERROR_OUT_OF_MEMORY
;
815 memcpy(&cso
->state
, &velems_state
, key_size
);
816 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
817 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
818 cso
->context
= ctx
->pipe
;
820 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
821 if (cso_hash_iter_is_null(iter
)) {
823 return PIPE_ERROR_OUT_OF_MEMORY
;
829 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
832 if (ctx
->velements
!= handle
) {
833 ctx
->velements
= handle
;
834 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
839 void cso_save_vertex_elements(struct cso_context
*ctx
)
841 struct u_vbuf
*vbuf
= ctx
->vbuf
;
844 u_vbuf_save_vertex_elements(vbuf
);
848 assert(!ctx
->velements_saved
);
849 ctx
->velements_saved
= ctx
->velements
;
852 void cso_restore_vertex_elements(struct cso_context
*ctx
)
854 struct u_vbuf
*vbuf
= ctx
->vbuf
;
857 u_vbuf_restore_vertex_elements(vbuf
);
861 if (ctx
->velements
!= ctx
->velements_saved
) {
862 ctx
->velements
= ctx
->velements_saved
;
863 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
865 ctx
->velements_saved
= NULL
;
870 void cso_set_vertex_buffers(struct cso_context
*ctx
,
872 const struct pipe_vertex_buffer
*buffers
)
874 struct u_vbuf
*vbuf
= ctx
->vbuf
;
877 u_vbuf_set_vertex_buffers(vbuf
, count
, buffers
);
881 if (count
!= ctx
->nr_vertex_buffers
||
882 memcmp(buffers
, ctx
->vertex_buffers
,
883 sizeof(struct pipe_vertex_buffer
) * count
) != 0) {
884 util_copy_vertex_buffers(ctx
->vertex_buffers
, &ctx
->nr_vertex_buffers
,
886 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, count
, buffers
);
890 void cso_save_vertex_buffers(struct cso_context
*ctx
)
892 struct u_vbuf
*vbuf
= ctx
->vbuf
;
895 u_vbuf_save_vertex_buffers(vbuf
);
899 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
900 &ctx
->nr_vertex_buffers_saved
,
902 ctx
->nr_vertex_buffers
);
905 void cso_restore_vertex_buffers(struct cso_context
*ctx
)
908 struct u_vbuf
*vbuf
= ctx
->vbuf
;
911 u_vbuf_restore_vertex_buffers(vbuf
);
915 util_copy_vertex_buffers(ctx
->vertex_buffers
,
916 &ctx
->nr_vertex_buffers
,
917 ctx
->vertex_buffers_saved
,
918 ctx
->nr_vertex_buffers_saved
);
920 for (i
= 0; i
< ctx
->nr_vertex_buffers_saved
; i
++) {
921 pipe_resource_reference(&ctx
->vertex_buffers_saved
[i
].buffer
, NULL
);
923 ctx
->nr_vertex_buffers_saved
= 0;
925 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, ctx
->nr_vertex_buffers
,
926 ctx
->vertex_buffers
);
930 /**************** fragment/vertex sampler view state *************************/
932 static enum pipe_error
933 single_sampler(struct cso_context
*ctx
,
934 struct sampler_info
*info
,
936 const struct pipe_sampler_state
*templ
)
941 unsigned key_size
= sizeof(struct pipe_sampler_state
);
942 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
943 struct cso_hash_iter iter
=
944 cso_find_state_template(ctx
->cache
,
945 hash_key
, CSO_SAMPLER
,
946 (void *) templ
, key_size
);
948 if (cso_hash_iter_is_null(iter
)) {
949 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
951 return PIPE_ERROR_OUT_OF_MEMORY
;
953 memcpy(&cso
->state
, templ
, sizeof(*templ
));
954 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
955 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
956 cso
->context
= ctx
->pipe
;
958 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
959 if (cso_hash_iter_is_null(iter
)) {
961 return PIPE_ERROR_OUT_OF_MEMORY
;
967 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
971 info
->samplers
[idx
] = handle
;
977 cso_single_sampler(struct cso_context
*ctx
,
979 const struct pipe_sampler_state
*templ
)
981 return single_sampler(ctx
, &ctx
->fragment_samplers
, idx
, templ
);
985 cso_single_vertex_sampler(struct cso_context
*ctx
,
987 const struct pipe_sampler_state
*templ
)
989 return single_sampler(ctx
, &ctx
->vertex_samplers
, idx
, templ
);
995 single_sampler_done(struct cso_context
*ctx
,
996 struct sampler_info
*info
)
1000 /* find highest non-null sampler */
1001 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1002 if (info
->samplers
[i
- 1] != NULL
)
1006 info
->nr_samplers
= i
;
1008 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1009 memcmp(info
->hw
.samplers
,
1011 info
->nr_samplers
* sizeof(void *)) != 0)
1013 memcpy(info
->hw
.samplers
,
1015 info
->nr_samplers
* sizeof(void *));
1016 info
->hw
.nr_samplers
= info
->nr_samplers
;
1018 if (info
== &ctx
->fragment_samplers
) {
1019 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
,
1023 else if (info
== &ctx
->vertex_samplers
) {
1024 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
1035 cso_single_sampler_done( struct cso_context
*ctx
)
1037 single_sampler_done(ctx
, &ctx
->fragment_samplers
);
1041 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
1043 single_sampler_done(ctx
, &ctx
->vertex_samplers
);
1048 * If the function encouters any errors it will return the
1049 * last one. Done to always try to set as many samplers
1052 static enum pipe_error
1053 set_samplers(struct cso_context
*ctx
,
1054 struct sampler_info
*info
,
1056 const struct pipe_sampler_state
**templates
)
1059 enum pipe_error temp
, error
= PIPE_OK
;
1064 for (i
= 0; i
< nr
; i
++) {
1065 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1066 if (temp
!= PIPE_OK
)
1070 for ( ; i
< info
->nr_samplers
; i
++) {
1071 temp
= single_sampler(ctx
, info
, i
, NULL
);
1072 if (temp
!= PIPE_OK
)
1076 single_sampler_done(ctx
, info
);
1082 cso_set_samplers(struct cso_context
*ctx
,
1084 const struct pipe_sampler_state
**templates
)
1086 return set_samplers(ctx
, &ctx
->fragment_samplers
, nr
, templates
);
1090 cso_set_vertex_samplers(struct cso_context
*ctx
,
1092 const struct pipe_sampler_state
**templates
)
1094 return set_samplers(ctx
, &ctx
->vertex_samplers
, nr
, templates
);
1100 save_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1102 info
->nr_samplers_saved
= info
->nr_samplers
;
1103 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1107 cso_save_samplers(struct cso_context
*ctx
)
1109 save_samplers(ctx
, &ctx
->fragment_samplers
);
1113 cso_save_vertex_samplers(struct cso_context
*ctx
)
1115 save_samplers(ctx
, &ctx
->vertex_samplers
);
1121 restore_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1123 info
->nr_samplers
= info
->nr_samplers_saved
;
1124 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1125 single_sampler_done(ctx
, info
);
1129 cso_restore_samplers(struct cso_context
*ctx
)
1131 restore_samplers(ctx
, &ctx
->fragment_samplers
);
1135 cso_restore_vertex_samplers(struct cso_context
*ctx
)
1137 restore_samplers(ctx
, &ctx
->vertex_samplers
);
1143 set_sampler_views(struct cso_context
*ctx
,
1144 struct sampler_info
*info
,
1145 void (*set_views
)(struct pipe_context
*,
1147 struct pipe_sampler_view
**),
1149 struct pipe_sampler_view
**views
)
1153 /* reference new views */
1154 for (i
= 0; i
< count
; i
++) {
1155 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1157 /* unref extra old views, if any */
1158 for (; i
< info
->nr_views
; i
++) {
1159 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1162 info
->nr_views
= count
;
1164 /* bind the new sampler views */
1165 set_views(ctx
->pipe
, count
, info
->views
);
1169 cso_set_fragment_sampler_views(struct cso_context
*ctx
,
1171 struct pipe_sampler_view
**views
)
1173 set_sampler_views(ctx
, &ctx
->fragment_samplers
,
1174 ctx
->pipe
->set_fragment_sampler_views
,
1179 cso_set_vertex_sampler_views(struct cso_context
*ctx
,
1181 struct pipe_sampler_view
**views
)
1183 set_sampler_views(ctx
, &ctx
->vertex_samplers
,
1184 ctx
->pipe
->set_vertex_sampler_views
,
1191 save_sampler_views(struct cso_context
*ctx
,
1192 struct sampler_info
*info
)
1196 info
->nr_views_saved
= info
->nr_views
;
1198 for (i
= 0; i
< info
->nr_views
; i
++) {
1199 assert(!info
->views_saved
[i
]);
1200 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1205 cso_save_fragment_sampler_views(struct cso_context
*ctx
)
1207 save_sampler_views(ctx
, &ctx
->fragment_samplers
);
1211 cso_save_vertex_sampler_views(struct cso_context
*ctx
)
1213 save_sampler_views(ctx
, &ctx
->vertex_samplers
);
1218 restore_sampler_views(struct cso_context
*ctx
,
1219 struct sampler_info
*info
,
1220 void (*set_views
)(struct pipe_context
*,
1222 struct pipe_sampler_view
**))
1226 for (i
= 0; i
< info
->nr_views_saved
; i
++) {
1227 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1228 /* move the reference from one pointer to another */
1229 info
->views
[i
] = info
->views_saved
[i
];
1230 info
->views_saved
[i
] = NULL
;
1232 for (; i
< info
->nr_views
; i
++) {
1233 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1236 /* bind the old/saved sampler views */
1237 set_views(ctx
->pipe
, info
->nr_views_saved
, info
->views
);
1239 info
->nr_views
= info
->nr_views_saved
;
1240 info
->nr_views_saved
= 0;
1244 cso_restore_fragment_sampler_views(struct cso_context
*ctx
)
1246 restore_sampler_views(ctx
, &ctx
->fragment_samplers
,
1247 ctx
->pipe
->set_fragment_sampler_views
);
1251 cso_restore_vertex_sampler_views(struct cso_context
*ctx
)
1253 restore_sampler_views(ctx
, &ctx
->vertex_samplers
,
1254 ctx
->pipe
->set_vertex_sampler_views
);
1259 cso_set_stream_outputs(struct cso_context
*ctx
,
1260 unsigned num_targets
,
1261 struct pipe_stream_output_target
**targets
,
1262 unsigned append_bitmask
)
1264 struct pipe_context
*pipe
= ctx
->pipe
;
1267 if (!ctx
->has_streamout
) {
1268 assert(num_targets
== 0);
1272 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1273 /* Nothing to do. */
1277 /* reference new targets */
1278 for (i
= 0; i
< num_targets
; i
++) {
1279 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1281 /* unref extra old targets, if any */
1282 for (; i
< ctx
->nr_so_targets
; i
++) {
1283 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1286 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1288 ctx
->nr_so_targets
= num_targets
;
1292 cso_save_stream_outputs(struct cso_context
*ctx
)
1296 if (!ctx
->has_streamout
) {
1300 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1302 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1303 assert(!ctx
->so_targets_saved
[i
]);
1304 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1309 cso_restore_stream_outputs(struct cso_context
*ctx
)
1311 struct pipe_context
*pipe
= ctx
->pipe
;
1314 if (!ctx
->has_streamout
) {
1318 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1319 /* Nothing to do. */
1323 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1324 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1325 /* move the reference from one pointer to another */
1326 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1327 ctx
->so_targets_saved
[i
] = NULL
;
1329 for (; i
< ctx
->nr_so_targets
; i
++) {
1330 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1333 /* ~0 means append */
1334 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1335 ctx
->so_targets
, ~0);
1337 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1338 ctx
->nr_so_targets_saved
= 0;
1344 cso_set_index_buffer(struct cso_context
*cso
,
1345 const struct pipe_index_buffer
*ib
)
1347 struct u_vbuf
*vbuf
= cso
->vbuf
;
1350 u_vbuf_set_index_buffer(vbuf
, ib
);
1352 struct pipe_context
*pipe
= cso
->pipe
;
1353 pipe
->set_index_buffer(pipe
, ib
);
1358 cso_draw_vbo(struct cso_context
*cso
,
1359 const struct pipe_draw_info
*info
)
1361 struct u_vbuf
*vbuf
= cso
->vbuf
;
1364 u_vbuf_draw_vbo(vbuf
, info
);
1366 struct pipe_context
*pipe
= cso
->pipe
;
1367 pipe
->draw_vbo(pipe
, info
);
1372 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1374 struct pipe_draw_info info
;
1376 util_draw_init_info(&info
);
1381 info
.min_index
= start
;
1382 info
.max_index
= start
+ count
- 1;
1384 cso_draw_vbo(cso
, &info
);