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_inlines.h"
40 #include "util/u_memory.h"
41 #include "tgsi/tgsi_parse.h"
43 #include "cso_cache/cso_context.h"
44 #include "cso_cache/cso_cache.h"
45 #include "cso_cache/cso_hash.h"
46 #include "cso_context.h"
49 struct pipe_context
*pipe
;
50 struct cso_cache
*cache
;
53 void *samplers
[PIPE_MAX_SAMPLERS
];
56 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
57 unsigned nr_vertex_samplers
;
60 void *samplers
[PIPE_MAX_SAMPLERS
];
63 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
64 unsigned nr_vertex_samplers
;
66 unsigned nr_samplers_saved
;
67 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
69 unsigned nr_vertex_samplers_saved
;
70 void *vertex_samplers_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
72 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
75 struct pipe_texture
*vertex_textures
[PIPE_MAX_VERTEX_SAMPLERS
];
76 uint nr_vertex_textures
;
78 uint nr_textures_saved
;
79 struct pipe_texture
*textures_saved
[PIPE_MAX_SAMPLERS
];
81 uint nr_vertex_textures_saved
;
82 struct pipe_texture
*vertex_textures_saved
[PIPE_MAX_SAMPLERS
];
84 /** Current and saved state.
85 * The saved state is used as a 1-deep stack.
87 void *blend
, *blend_saved
;
88 void *depth_stencil
, *depth_stencil_saved
;
89 void *rasterizer
, *rasterizer_saved
;
90 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
91 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
92 void *velements
, *velements_saved
;
94 struct pipe_clip_state clip
;
95 struct pipe_clip_state clip_saved
;
97 struct pipe_framebuffer_state fb
, fb_saved
;
98 struct pipe_viewport_state vp
, vp_saved
;
99 struct pipe_blend_color blend_color
;
100 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
105 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
108 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
110 struct cso_blend
*cso
= (struct cso_blend
*)state
;
112 if (ctx
->blend
== cso
->data
)
115 if (cso
->delete_state
)
116 cso
->delete_state(cso
->context
, cso
->data
);
121 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
123 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
125 if (ctx
->depth_stencil
== cso
->data
)
128 if (cso
->delete_state
)
129 cso
->delete_state(cso
->context
, cso
->data
);
135 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
137 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
138 if (cso
->delete_state
)
139 cso
->delete_state(cso
->context
, cso
->data
);
144 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
146 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
148 if (ctx
->rasterizer
== cso
->data
)
150 if (cso
->delete_state
)
151 cso
->delete_state(cso
->context
, cso
->data
);
156 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
158 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
159 if (ctx
->fragment_shader
== cso
->data
)
161 if (cso
->delete_state
)
162 cso
->delete_state(cso
->context
, cso
->data
);
167 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
169 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
170 if (ctx
->vertex_shader
== cso
->data
)
172 if (cso
->delete_state
)
173 cso
->delete_state(cso
->context
, cso
->data
);
178 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
181 struct cso_velements
*cso
= (struct cso_velements
*)state
;
183 if (ctx
->velements
== cso
->data
)
186 if (cso
->delete_state
)
187 cso
->delete_state(cso
->context
, cso
->data
);
193 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
194 void *state
, enum cso_cache_type type
)
198 return delete_blend_state(ctx
, state
);
201 return delete_sampler_state(ctx
, state
);
203 case CSO_DEPTH_STENCIL_ALPHA
:
204 return delete_depth_stencil_state(ctx
, state
);
207 return delete_rasterizer_state(ctx
, state
);
209 case CSO_FRAGMENT_SHADER
:
210 return delete_fs_state(ctx
, state
);
212 case CSO_VERTEX_SHADER
:
213 return delete_vs_state(ctx
, state
);
216 return delete_vertex_elements(ctx
, state
);
225 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
226 int max_size
, void *user_data
)
228 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
229 /* if we're approach the maximum size, remove fourth of the entries
230 * otherwise every subsequent call will go through the same */
231 int hash_size
= cso_hash_size(hash
);
232 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
233 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
234 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
235 if (hash_size
> max_size
)
236 to_remove
+= hash_size
- max_size
;
238 /*remove elements until we're good */
239 /*fixme: currently we pick the nodes to remove at random*/
240 void *cso
= cso_hash_iter_data(iter
);
241 if (delete_cso(ctx
, cso
, type
)) {
242 iter
= cso_hash_erase(hash
, iter
);
245 iter
= cso_hash_iter_next(iter
);
250 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
252 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
256 ctx
->cache
= cso_cache_create();
257 if (ctx
->cache
== NULL
)
259 cso_cache_set_sanitize_callback(ctx
->cache
,
265 /* Enable for testing: */
266 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
271 cso_destroy_context( ctx
);
277 * Prior to context destruction, this function unbinds all state objects.
279 void cso_release_all( struct cso_context
*ctx
)
284 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
285 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
286 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
287 if (ctx
->pipe
->bind_vertex_sampler_states
)
288 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
289 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
290 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
291 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
292 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
295 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
296 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
297 pipe_texture_reference(&ctx
->textures_saved
[i
], NULL
);
300 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
301 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
302 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], NULL
);
305 free_framebuffer_state(&ctx
->fb
);
306 free_framebuffer_state(&ctx
->fb_saved
);
309 cso_cache_delete( ctx
->cache
);
315 void cso_destroy_context( struct cso_context
*ctx
)
318 /*cso_release_all( ctx );*/
324 /* Those function will either find the state of the given template
325 * in the cache or they will create a new state from the given
326 * template, insert it in the cache and return it.
330 * If the driver returns 0 from the create method then they will assign
331 * the data member of the cso to be the template itself.
334 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
335 const struct pipe_blend_state
*templ
)
337 unsigned key_size
, hash_key
;
338 struct cso_hash_iter iter
;
341 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
342 (char *)&(templ
->rt
[1]) - (char *)templ
;
343 hash_key
= cso_construct_key((void*)templ
, key_size
);
344 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
346 if (cso_hash_iter_is_null(iter
)) {
347 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
349 return PIPE_ERROR_OUT_OF_MEMORY
;
351 memcpy(&cso
->state
, templ
, key_size
);
352 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
353 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
354 cso
->context
= ctx
->pipe
;
356 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
357 if (cso_hash_iter_is_null(iter
)) {
359 return PIPE_ERROR_OUT_OF_MEMORY
;
365 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
368 if (ctx
->blend
!= handle
) {
370 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
375 void cso_save_blend(struct cso_context
*ctx
)
377 assert(!ctx
->blend_saved
);
378 ctx
->blend_saved
= ctx
->blend
;
381 void cso_restore_blend(struct cso_context
*ctx
)
383 if (ctx
->blend
!= ctx
->blend_saved
) {
384 ctx
->blend
= ctx
->blend_saved
;
385 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
387 ctx
->blend_saved
= NULL
;
392 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
394 const struct pipe_sampler_state
*templ
)
399 unsigned key_size
= sizeof(struct pipe_sampler_state
);
400 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
401 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
402 hash_key
, CSO_SAMPLER
,
403 (void*)templ
, key_size
);
405 if (cso_hash_iter_is_null(iter
)) {
406 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
408 return PIPE_ERROR_OUT_OF_MEMORY
;
410 memcpy(&cso
->state
, templ
, sizeof(*templ
));
411 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
412 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
413 cso
->context
= ctx
->pipe
;
415 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
416 if (cso_hash_iter_is_null(iter
)) {
418 return PIPE_ERROR_OUT_OF_MEMORY
;
424 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
428 ctx
->samplers
[idx
] = handle
;
433 cso_single_vertex_sampler(struct cso_context
*ctx
,
435 const struct pipe_sampler_state
*templ
)
440 unsigned key_size
= sizeof(struct pipe_sampler_state
);
441 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
442 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
443 hash_key
, CSO_SAMPLER
,
444 (void*)templ
, key_size
);
446 if (cso_hash_iter_is_null(iter
)) {
447 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
449 return PIPE_ERROR_OUT_OF_MEMORY
;
451 memcpy(&cso
->state
, templ
, sizeof(*templ
));
452 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
453 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
454 cso
->context
= ctx
->pipe
;
456 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
457 if (cso_hash_iter_is_null(iter
)) {
459 return PIPE_ERROR_OUT_OF_MEMORY
;
465 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
469 ctx
->vertex_samplers
[idx
] = handle
;
473 void cso_single_sampler_done( struct cso_context
*ctx
)
477 /* find highest non-null sampler */
478 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
479 if (ctx
->samplers
[i
- 1] != NULL
)
483 ctx
->nr_samplers
= i
;
485 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
486 memcmp(ctx
->hw
.samplers
,
488 ctx
->nr_samplers
* sizeof(void *)) != 0)
490 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
491 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
493 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
498 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
502 /* find highest non-null sampler */
503 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
504 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
508 ctx
->nr_vertex_samplers
= i
;
510 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
511 memcmp(ctx
->hw
.vertex_samplers
,
512 ctx
->vertex_samplers
,
513 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
515 memcpy(ctx
->hw
.vertex_samplers
,
516 ctx
->vertex_samplers
,
517 ctx
->nr_vertex_samplers
* sizeof(void *));
518 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
520 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
521 ctx
->nr_vertex_samplers
,
522 ctx
->vertex_samplers
);
527 * If the function encouters any errors it will return the
528 * last one. Done to always try to set as many samplers
531 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
533 const struct pipe_sampler_state
**templates
)
536 enum pipe_error temp
, error
= PIPE_OK
;
541 for (i
= 0; i
< nr
; i
++) {
542 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
547 for ( ; i
< ctx
->nr_samplers
; i
++) {
548 temp
= cso_single_sampler( ctx
, i
, NULL
);
553 cso_single_sampler_done( ctx
);
558 void cso_save_samplers(struct cso_context
*ctx
)
560 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
561 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
564 void cso_restore_samplers(struct cso_context
*ctx
)
566 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
567 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
568 cso_single_sampler_done( ctx
);
572 * If the function encouters any errors it will return the
573 * last one. Done to always try to set as many samplers
576 enum pipe_error
cso_set_vertex_samplers(struct cso_context
*ctx
,
578 const struct pipe_sampler_state
**templates
)
581 enum pipe_error temp
, error
= PIPE_OK
;
586 for (i
= 0; i
< nr
; i
++) {
587 temp
= cso_single_vertex_sampler( ctx
, i
, templates
[i
] );
592 for ( ; i
< ctx
->nr_samplers
; i
++) {
593 temp
= cso_single_vertex_sampler( ctx
, i
, NULL
);
598 cso_single_vertex_sampler_done( ctx
);
604 cso_save_vertex_samplers(struct cso_context
*ctx
)
606 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
607 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
611 cso_restore_vertex_samplers(struct cso_context
*ctx
)
613 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
614 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
615 cso_single_vertex_sampler_done(ctx
);
619 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
621 struct pipe_texture
**textures
)
625 ctx
->nr_textures
= count
;
627 for (i
= 0; i
< count
; i
++)
628 pipe_texture_reference(&ctx
->textures
[i
], textures
[i
]);
629 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
630 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
632 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, count
, textures
);
637 void cso_save_sampler_textures( struct cso_context
*ctx
)
641 ctx
->nr_textures_saved
= ctx
->nr_textures
;
642 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
643 assert(!ctx
->textures_saved
[i
]);
644 pipe_texture_reference(&ctx
->textures_saved
[i
], ctx
->textures
[i
]);
648 void cso_restore_sampler_textures( struct cso_context
*ctx
)
652 ctx
->nr_textures
= ctx
->nr_textures_saved
;
654 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
655 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
656 ctx
->textures
[i
] = ctx
->textures_saved
[i
];
657 ctx
->textures_saved
[i
] = NULL
;
659 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
660 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
662 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, ctx
->nr_textures
, ctx
->textures
);
664 ctx
->nr_textures_saved
= 0;
670 cso_set_vertex_sampler_textures(struct cso_context
*ctx
,
672 struct pipe_texture
**textures
)
676 ctx
->nr_vertex_textures
= count
;
678 for (i
= 0; i
< count
; i
++) {
679 pipe_texture_reference(&ctx
->vertex_textures
[i
], textures
[i
]);
681 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
682 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
685 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
, count
, textures
);
691 cso_save_vertex_sampler_textures(struct cso_context
*ctx
)
695 ctx
->nr_vertex_textures_saved
= ctx
->nr_vertex_textures
;
696 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
697 assert(!ctx
->vertex_textures_saved
[i
]);
698 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], ctx
->vertex_textures
[i
]);
703 cso_restore_vertex_sampler_textures(struct cso_context
*ctx
)
707 ctx
->nr_vertex_textures
= ctx
->nr_vertex_textures_saved
;
709 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
710 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
711 ctx
->vertex_textures
[i
] = ctx
->vertex_textures_saved
[i
];
712 ctx
->vertex_textures_saved
[i
] = NULL
;
714 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
715 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
718 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
,
719 ctx
->nr_vertex_textures
,
720 ctx
->vertex_textures
);
722 ctx
->nr_vertex_textures_saved
= 0;
727 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
728 const struct pipe_depth_stencil_alpha_state
*templ
)
730 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
731 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
732 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
734 CSO_DEPTH_STENCIL_ALPHA
,
735 (void*)templ
, key_size
);
738 if (cso_hash_iter_is_null(iter
)) {
739 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
741 return PIPE_ERROR_OUT_OF_MEMORY
;
743 memcpy(&cso
->state
, templ
, sizeof(*templ
));
744 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
745 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
746 cso
->context
= ctx
->pipe
;
748 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
749 if (cso_hash_iter_is_null(iter
)) {
751 return PIPE_ERROR_OUT_OF_MEMORY
;
757 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
760 if (ctx
->depth_stencil
!= handle
) {
761 ctx
->depth_stencil
= handle
;
762 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
767 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
769 assert(!ctx
->depth_stencil_saved
);
770 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
773 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
775 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
776 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
777 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
779 ctx
->depth_stencil_saved
= NULL
;
784 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
785 const struct pipe_rasterizer_state
*templ
)
787 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
788 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
789 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
790 hash_key
, CSO_RASTERIZER
,
791 (void*)templ
, key_size
);
794 if (cso_hash_iter_is_null(iter
)) {
795 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
797 return PIPE_ERROR_OUT_OF_MEMORY
;
799 memcpy(&cso
->state
, templ
, sizeof(*templ
));
800 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
801 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
802 cso
->context
= ctx
->pipe
;
804 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
805 if (cso_hash_iter_is_null(iter
)) {
807 return PIPE_ERROR_OUT_OF_MEMORY
;
813 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
816 if (ctx
->rasterizer
!= handle
) {
817 ctx
->rasterizer
= handle
;
818 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
823 void cso_save_rasterizer(struct cso_context
*ctx
)
825 assert(!ctx
->rasterizer_saved
);
826 ctx
->rasterizer_saved
= ctx
->rasterizer
;
829 void cso_restore_rasterizer(struct cso_context
*ctx
)
831 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
832 ctx
->rasterizer
= ctx
->rasterizer_saved
;
833 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
835 ctx
->rasterizer_saved
= NULL
;
840 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
843 if (ctx
->fragment_shader
!= handle
) {
844 ctx
->fragment_shader
= handle
;
845 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
850 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
852 if (handle
== ctx
->fragment_shader
) {
853 /* unbind before deleting */
854 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
855 ctx
->fragment_shader
= NULL
;
857 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
860 /* Not really working:
863 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
864 const struct pipe_shader_state
*templ
)
866 const struct tgsi_token
*tokens
= templ
->tokens
;
867 unsigned num_tokens
= tgsi_num_tokens(tokens
);
868 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
869 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
870 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
874 sizeof(*templ
)); /* XXX correct? tokens_size? */
877 if (cso_hash_iter_is_null(iter
)) {
878 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
879 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
882 return PIPE_ERROR_OUT_OF_MEMORY
;
884 memcpy(cso_tokens
, tokens
, tokens_size
);
885 cso
->state
.tokens
= cso_tokens
;
886 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
887 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
888 cso
->context
= ctx
->pipe
;
890 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
891 if (cso_hash_iter_is_null(iter
)) {
893 return PIPE_ERROR_OUT_OF_MEMORY
;
899 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
902 return cso_set_fragment_shader_handle( ctx
, handle
);
906 void cso_save_fragment_shader(struct cso_context
*ctx
)
908 assert(!ctx
->fragment_shader_saved
);
909 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
912 void cso_restore_fragment_shader(struct cso_context
*ctx
)
914 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
915 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
916 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
918 ctx
->fragment_shader_saved
= NULL
;
922 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
925 if (ctx
->vertex_shader
!= handle
) {
926 ctx
->vertex_shader
= handle
;
927 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
932 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
934 if (handle
== ctx
->vertex_shader
) {
935 /* unbind before deleting */
936 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
937 ctx
->vertex_shader
= NULL
;
939 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
943 /* Not really working:
946 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
947 const struct pipe_shader_state
*templ
)
949 unsigned hash_key
= cso_construct_key((void*)templ
,
950 sizeof(struct pipe_shader_state
));
951 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
952 hash_key
, CSO_VERTEX_SHADER
,
957 if (cso_hash_iter_is_null(iter
)) {
958 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
961 return PIPE_ERROR_OUT_OF_MEMORY
;
963 memcpy(cso
->state
, templ
, sizeof(*templ
));
964 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
965 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
966 cso
->context
= ctx
->pipe
;
968 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
969 if (cso_hash_iter_is_null(iter
)) {
971 return PIPE_ERROR_OUT_OF_MEMORY
;
977 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
980 return cso_set_vertex_shader_handle( ctx
, handle
);
986 void cso_save_vertex_shader(struct cso_context
*ctx
)
988 assert(!ctx
->vertex_shader_saved
);
989 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
992 void cso_restore_vertex_shader(struct cso_context
*ctx
)
994 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
995 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
996 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
998 ctx
->vertex_shader_saved
= NULL
;
1003 * Copy framebuffer state from src to dst with refcounting of surfaces.
1006 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
1007 const struct pipe_framebuffer_state
*src
)
1011 dst
->width
= src
->width
;
1012 dst
->height
= src
->height
;
1013 dst
->nr_cbufs
= src
->nr_cbufs
;
1014 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1015 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
1017 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
1022 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
1026 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1027 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
1029 pipe_surface_reference(&fb
->zsbuf
, NULL
);
1033 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
1034 const struct pipe_framebuffer_state
*fb
)
1036 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
1037 copy_framebuffer_state(&ctx
->fb
, fb
);
1038 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
1043 void cso_save_framebuffer(struct cso_context
*ctx
)
1045 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
1048 void cso_restore_framebuffer(struct cso_context
*ctx
)
1050 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
1051 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
1052 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
1053 free_framebuffer_state(&ctx
->fb_saved
);
1058 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
1059 const struct pipe_viewport_state
*vp
)
1061 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
1063 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1068 void cso_save_viewport(struct cso_context
*ctx
)
1070 ctx
->vp_saved
= ctx
->vp
;
1074 void cso_restore_viewport(struct cso_context
*ctx
)
1076 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1077 ctx
->vp
= ctx
->vp_saved
;
1078 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1083 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1084 const struct pipe_blend_color
*bc
)
1086 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1087 ctx
->blend_color
= *bc
;
1088 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
1093 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
1094 const struct pipe_stencil_ref
*sr
)
1096 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
1097 ctx
->stencil_ref
= *sr
;
1098 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
1103 void cso_save_stencil_ref(struct cso_context
*ctx
)
1105 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
1109 void cso_restore_stencil_ref(struct cso_context
*ctx
)
1111 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
1112 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
1113 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
1117 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1120 if (ctx
->geometry_shader
!= handle
) {
1121 ctx
->geometry_shader
= handle
;
1122 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1127 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1129 if (handle
== ctx
->geometry_shader
) {
1130 /* unbind before deleting */
1131 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1132 ctx
->geometry_shader
= NULL
;
1134 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1137 void cso_save_geometry_shader(struct cso_context
*ctx
)
1139 assert(!ctx
->geometry_shader_saved
);
1140 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1143 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1145 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1146 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1147 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1149 ctx
->geometry_shader_saved
= NULL
;
1155 clip_state_cpy(struct pipe_clip_state
*dst
,
1156 const struct pipe_clip_state
*src
)
1160 memcpy(dst
->ucp
, src
->ucp
, src
->nr
* sizeof(src
->ucp
[0]));
1165 clip_state_cmp(const struct pipe_clip_state
*a
,
1166 const struct pipe_clip_state
*b
)
1168 if (a
->nr
!= b
->nr
) {
1172 return memcmp(a
->ucp
, b
->ucp
, a
->nr
* sizeof(a
->ucp
[0]));
1178 cso_set_clip(struct cso_context
*ctx
,
1179 const struct pipe_clip_state
*clip
)
1181 if (clip_state_cmp(&ctx
->clip
, clip
)) {
1182 clip_state_cpy(&ctx
->clip
, clip
);
1183 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
1188 cso_save_clip(struct cso_context
*ctx
)
1190 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
1194 cso_restore_clip(struct cso_context
*ctx
)
1196 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
1197 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
1198 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
1202 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
1204 const struct pipe_vertex_element
*states
)
1206 unsigned key_size
, hash_key
;
1207 struct cso_hash_iter iter
;
1209 struct cso_velems_state velems_state
;
1211 /* need to include the count into the stored state data too.
1212 Otherwise first few count pipe_vertex_elements could be identical even if count
1213 is different, and there's no guarantee the hash would be different in that
1215 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
1216 velems_state
.count
= count
;
1217 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
1218 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
1219 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
1221 if (cso_hash_iter_is_null(iter
)) {
1222 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1224 return PIPE_ERROR_OUT_OF_MEMORY
;
1226 memcpy(&cso
->state
, &velems_state
, key_size
);
1227 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
1228 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
1229 cso
->context
= ctx
->pipe
;
1231 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1232 if (cso_hash_iter_is_null(iter
)) {
1234 return PIPE_ERROR_OUT_OF_MEMORY
;
1240 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1243 if (ctx
->velements
!= handle
) {
1244 ctx
->velements
= handle
;
1245 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1250 void cso_save_vertex_elements(struct cso_context
*ctx
)
1252 assert(!ctx
->velements_saved
);
1253 ctx
->velements_saved
= ctx
->velements
;
1256 void cso_restore_vertex_elements(struct cso_context
*ctx
)
1258 if (ctx
->velements
!= ctx
->velements_saved
) {
1259 ctx
->velements
= ctx
->velements_saved
;
1260 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1262 ctx
->velements_saved
= NULL
;