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_framebuffer_state fb
, fb_saved
;
95 struct pipe_viewport_state vp
, vp_saved
;
96 struct pipe_blend_color blend_color
;
97 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
102 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
105 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
107 struct cso_blend
*cso
= (struct cso_blend
*)state
;
109 if (ctx
->blend
== cso
->data
)
112 if (cso
->delete_state
)
113 cso
->delete_state(cso
->context
, cso
->data
);
118 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
120 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
122 if (ctx
->depth_stencil
== cso
->data
)
125 if (cso
->delete_state
)
126 cso
->delete_state(cso
->context
, cso
->data
);
132 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
134 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
135 if (cso
->delete_state
)
136 cso
->delete_state(cso
->context
, cso
->data
);
141 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
143 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
145 if (ctx
->rasterizer
== cso
->data
)
147 if (cso
->delete_state
)
148 cso
->delete_state(cso
->context
, cso
->data
);
153 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
155 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
156 if (ctx
->fragment_shader
== cso
->data
)
158 if (cso
->delete_state
)
159 cso
->delete_state(cso
->context
, cso
->data
);
164 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
166 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
167 if (ctx
->vertex_shader
== cso
->data
)
169 if (cso
->delete_state
)
170 cso
->delete_state(cso
->context
, cso
->data
);
175 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
178 struct cso_velements
*cso
= (struct cso_velements
*)state
;
180 if (ctx
->velements
== cso
->data
)
183 if (cso
->delete_state
)
184 cso
->delete_state(cso
->context
, cso
->data
);
190 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
191 void *state
, enum cso_cache_type type
)
195 return delete_blend_state(ctx
, state
);
198 return delete_sampler_state(ctx
, state
);
200 case CSO_DEPTH_STENCIL_ALPHA
:
201 return delete_depth_stencil_state(ctx
, state
);
204 return delete_rasterizer_state(ctx
, state
);
206 case CSO_FRAGMENT_SHADER
:
207 return delete_fs_state(ctx
, state
);
209 case CSO_VERTEX_SHADER
:
210 return delete_vs_state(ctx
, state
);
213 return delete_vertex_elements(ctx
, state
);
222 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
223 int max_size
, void *user_data
)
225 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
226 /* if we're approach the maximum size, remove fourth of the entries
227 * otherwise every subsequent call will go through the same */
228 int hash_size
= cso_hash_size(hash
);
229 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
230 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
231 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
232 if (hash_size
> max_size
)
233 to_remove
+= hash_size
- max_size
;
235 /*remove elements until we're good */
236 /*fixme: currently we pick the nodes to remove at random*/
237 void *cso
= cso_hash_iter_data(iter
);
238 if (delete_cso(ctx
, cso
, type
)) {
239 iter
= cso_hash_erase(hash
, iter
);
242 iter
= cso_hash_iter_next(iter
);
247 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
249 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
253 ctx
->cache
= cso_cache_create();
254 if (ctx
->cache
== NULL
)
256 cso_cache_set_sanitize_callback(ctx
->cache
,
262 /* Enable for testing: */
263 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
268 cso_destroy_context( ctx
);
274 * Prior to context destruction, this function unbinds all state objects.
276 void cso_release_all( struct cso_context
*ctx
)
281 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
282 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
283 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
284 if (ctx
->pipe
->bind_vertex_sampler_states
)
285 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
286 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
287 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
288 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
291 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
292 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
293 pipe_texture_reference(&ctx
->textures_saved
[i
], NULL
);
296 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
297 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
298 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], NULL
);
301 free_framebuffer_state(&ctx
->fb
);
302 free_framebuffer_state(&ctx
->fb_saved
);
305 cso_cache_delete( ctx
->cache
);
311 void cso_destroy_context( struct cso_context
*ctx
)
314 /*cso_release_all( ctx );*/
320 /* Those function will either find the state of the given template
321 * in the cache or they will create a new state from the given
322 * template, insert it in the cache and return it.
326 * If the driver returns 0 from the create method then they will assign
327 * the data member of the cso to be the template itself.
330 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
331 const struct pipe_blend_state
*templ
)
333 unsigned key_size
, hash_key
;
334 struct cso_hash_iter iter
;
337 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
338 (char *)&(templ
->rt
[1]) - (char *)templ
;
339 hash_key
= cso_construct_key((void*)templ
, key_size
);
340 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
342 if (cso_hash_iter_is_null(iter
)) {
343 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
345 return PIPE_ERROR_OUT_OF_MEMORY
;
347 memcpy(&cso
->state
, templ
, key_size
);
348 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
349 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
350 cso
->context
= ctx
->pipe
;
352 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
353 if (cso_hash_iter_is_null(iter
)) {
355 return PIPE_ERROR_OUT_OF_MEMORY
;
361 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
364 if (ctx
->blend
!= handle
) {
366 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
371 void cso_save_blend(struct cso_context
*ctx
)
373 assert(!ctx
->blend_saved
);
374 ctx
->blend_saved
= ctx
->blend
;
377 void cso_restore_blend(struct cso_context
*ctx
)
379 if (ctx
->blend
!= ctx
->blend_saved
) {
380 ctx
->blend
= ctx
->blend_saved
;
381 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
383 ctx
->blend_saved
= NULL
;
388 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
390 const struct pipe_sampler_state
*templ
)
395 unsigned key_size
= sizeof(struct pipe_sampler_state
);
396 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
397 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
398 hash_key
, CSO_SAMPLER
,
399 (void*)templ
, key_size
);
401 if (cso_hash_iter_is_null(iter
)) {
402 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
404 return PIPE_ERROR_OUT_OF_MEMORY
;
406 memcpy(&cso
->state
, templ
, sizeof(*templ
));
407 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
408 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
409 cso
->context
= ctx
->pipe
;
411 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
412 if (cso_hash_iter_is_null(iter
)) {
414 return PIPE_ERROR_OUT_OF_MEMORY
;
420 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
424 ctx
->samplers
[idx
] = handle
;
429 cso_single_vertex_sampler(struct cso_context
*ctx
,
431 const struct pipe_sampler_state
*templ
)
436 unsigned key_size
= sizeof(struct pipe_sampler_state
);
437 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
438 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
439 hash_key
, CSO_SAMPLER
,
440 (void*)templ
, key_size
);
442 if (cso_hash_iter_is_null(iter
)) {
443 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
445 return PIPE_ERROR_OUT_OF_MEMORY
;
447 memcpy(&cso
->state
, templ
, sizeof(*templ
));
448 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
449 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
450 cso
->context
= ctx
->pipe
;
452 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
453 if (cso_hash_iter_is_null(iter
)) {
455 return PIPE_ERROR_OUT_OF_MEMORY
;
461 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
465 ctx
->vertex_samplers
[idx
] = handle
;
469 void cso_single_sampler_done( struct cso_context
*ctx
)
473 /* find highest non-null sampler */
474 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
475 if (ctx
->samplers
[i
- 1] != NULL
)
479 ctx
->nr_samplers
= i
;
481 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
482 memcmp(ctx
->hw
.samplers
,
484 ctx
->nr_samplers
* sizeof(void *)) != 0)
486 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
487 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
489 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
494 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
498 /* find highest non-null sampler */
499 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
500 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
504 ctx
->nr_vertex_samplers
= i
;
506 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
507 memcmp(ctx
->hw
.vertex_samplers
,
508 ctx
->vertex_samplers
,
509 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
511 memcpy(ctx
->hw
.vertex_samplers
,
512 ctx
->vertex_samplers
,
513 ctx
->nr_vertex_samplers
* sizeof(void *));
514 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
516 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
517 ctx
->nr_vertex_samplers
,
518 ctx
->vertex_samplers
);
523 * If the function encouters any errors it will return the
524 * last one. Done to always try to set as many samplers
527 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
529 const struct pipe_sampler_state
**templates
)
532 enum pipe_error temp
, error
= PIPE_OK
;
537 for (i
= 0; i
< nr
; i
++) {
538 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
543 for ( ; i
< ctx
->nr_samplers
; i
++) {
544 temp
= cso_single_sampler( ctx
, i
, NULL
);
549 cso_single_sampler_done( ctx
);
554 void cso_save_samplers(struct cso_context
*ctx
)
556 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
557 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
560 void cso_restore_samplers(struct cso_context
*ctx
)
562 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
563 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
564 cso_single_sampler_done( ctx
);
568 * If the function encouters any errors it will return the
569 * last one. Done to always try to set as many samplers
572 enum pipe_error
cso_set_vertex_samplers(struct cso_context
*ctx
,
574 const struct pipe_sampler_state
**templates
)
577 enum pipe_error temp
, error
= PIPE_OK
;
582 for (i
= 0; i
< nr
; i
++) {
583 temp
= cso_single_vertex_sampler( ctx
, i
, templates
[i
] );
588 for ( ; i
< ctx
->nr_samplers
; i
++) {
589 temp
= cso_single_vertex_sampler( ctx
, i
, NULL
);
594 cso_single_vertex_sampler_done( ctx
);
600 cso_save_vertex_samplers(struct cso_context
*ctx
)
602 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
603 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
607 cso_restore_vertex_samplers(struct cso_context
*ctx
)
609 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
610 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
611 cso_single_vertex_sampler_done(ctx
);
615 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
617 struct pipe_texture
**textures
)
621 ctx
->nr_textures
= count
;
623 for (i
= 0; i
< count
; i
++)
624 pipe_texture_reference(&ctx
->textures
[i
], textures
[i
]);
625 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
626 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
628 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, count
, textures
);
633 void cso_save_sampler_textures( struct cso_context
*ctx
)
637 ctx
->nr_textures_saved
= ctx
->nr_textures
;
638 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
639 assert(!ctx
->textures_saved
[i
]);
640 pipe_texture_reference(&ctx
->textures_saved
[i
], ctx
->textures
[i
]);
644 void cso_restore_sampler_textures( struct cso_context
*ctx
)
648 ctx
->nr_textures
= ctx
->nr_textures_saved
;
650 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
651 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
652 ctx
->textures
[i
] = ctx
->textures_saved
[i
];
653 ctx
->textures_saved
[i
] = NULL
;
655 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
656 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
658 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, ctx
->nr_textures
, ctx
->textures
);
660 ctx
->nr_textures_saved
= 0;
666 cso_set_vertex_sampler_textures(struct cso_context
*ctx
,
668 struct pipe_texture
**textures
)
672 ctx
->nr_vertex_textures
= count
;
674 for (i
= 0; i
< count
; i
++) {
675 pipe_texture_reference(&ctx
->vertex_textures
[i
], textures
[i
]);
677 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
678 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
681 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
, count
, textures
);
687 cso_save_vertex_sampler_textures(struct cso_context
*ctx
)
691 ctx
->nr_vertex_textures_saved
= ctx
->nr_vertex_textures
;
692 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
693 assert(!ctx
->vertex_textures_saved
[i
]);
694 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], ctx
->vertex_textures
[i
]);
699 cso_restore_vertex_sampler_textures(struct cso_context
*ctx
)
703 ctx
->nr_vertex_textures
= ctx
->nr_vertex_textures_saved
;
705 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
706 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
707 ctx
->vertex_textures
[i
] = ctx
->vertex_textures_saved
[i
];
708 ctx
->vertex_textures_saved
[i
] = NULL
;
710 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
711 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
714 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
,
715 ctx
->nr_vertex_textures
,
716 ctx
->vertex_textures
);
718 ctx
->nr_vertex_textures_saved
= 0;
723 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
724 const struct pipe_depth_stencil_alpha_state
*templ
)
726 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
727 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
728 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
730 CSO_DEPTH_STENCIL_ALPHA
,
731 (void*)templ
, key_size
);
734 if (cso_hash_iter_is_null(iter
)) {
735 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
737 return PIPE_ERROR_OUT_OF_MEMORY
;
739 memcpy(&cso
->state
, templ
, sizeof(*templ
));
740 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
741 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
742 cso
->context
= ctx
->pipe
;
744 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
745 if (cso_hash_iter_is_null(iter
)) {
747 return PIPE_ERROR_OUT_OF_MEMORY
;
753 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
756 if (ctx
->depth_stencil
!= handle
) {
757 ctx
->depth_stencil
= handle
;
758 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
763 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
765 assert(!ctx
->depth_stencil_saved
);
766 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
769 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
771 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
772 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
773 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
775 ctx
->depth_stencil_saved
= NULL
;
780 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
781 const struct pipe_rasterizer_state
*templ
)
783 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
784 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
785 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
786 hash_key
, CSO_RASTERIZER
,
787 (void*)templ
, key_size
);
790 if (cso_hash_iter_is_null(iter
)) {
791 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
793 return PIPE_ERROR_OUT_OF_MEMORY
;
795 memcpy(&cso
->state
, templ
, sizeof(*templ
));
796 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
797 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
798 cso
->context
= ctx
->pipe
;
800 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
801 if (cso_hash_iter_is_null(iter
)) {
803 return PIPE_ERROR_OUT_OF_MEMORY
;
809 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
812 if (ctx
->rasterizer
!= handle
) {
813 ctx
->rasterizer
= handle
;
814 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
819 void cso_save_rasterizer(struct cso_context
*ctx
)
821 assert(!ctx
->rasterizer_saved
);
822 ctx
->rasterizer_saved
= ctx
->rasterizer
;
825 void cso_restore_rasterizer(struct cso_context
*ctx
)
827 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
828 ctx
->rasterizer
= ctx
->rasterizer_saved
;
829 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
831 ctx
->rasterizer_saved
= NULL
;
836 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
839 if (ctx
->fragment_shader
!= handle
) {
840 ctx
->fragment_shader
= handle
;
841 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
846 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
848 if (handle
== ctx
->fragment_shader
) {
849 /* unbind before deleting */
850 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
851 ctx
->fragment_shader
= NULL
;
853 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
856 /* Not really working:
859 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
860 const struct pipe_shader_state
*templ
)
862 const struct tgsi_token
*tokens
= templ
->tokens
;
863 unsigned num_tokens
= tgsi_num_tokens(tokens
);
864 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
865 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
866 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
870 sizeof(*templ
)); /* XXX correct? tokens_size? */
873 if (cso_hash_iter_is_null(iter
)) {
874 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
875 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
878 return PIPE_ERROR_OUT_OF_MEMORY
;
880 memcpy(cso_tokens
, tokens
, tokens_size
);
881 cso
->state
.tokens
= cso_tokens
;
882 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
883 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
884 cso
->context
= ctx
->pipe
;
886 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
887 if (cso_hash_iter_is_null(iter
)) {
889 return PIPE_ERROR_OUT_OF_MEMORY
;
895 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
898 return cso_set_fragment_shader_handle( ctx
, handle
);
902 void cso_save_fragment_shader(struct cso_context
*ctx
)
904 assert(!ctx
->fragment_shader_saved
);
905 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
908 void cso_restore_fragment_shader(struct cso_context
*ctx
)
910 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
911 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
912 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
914 ctx
->fragment_shader_saved
= NULL
;
918 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
921 if (ctx
->vertex_shader
!= handle
) {
922 ctx
->vertex_shader
= handle
;
923 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
928 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
930 if (handle
== ctx
->vertex_shader
) {
931 /* unbind before deleting */
932 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
933 ctx
->vertex_shader
= NULL
;
935 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
939 /* Not really working:
942 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
943 const struct pipe_shader_state
*templ
)
945 unsigned hash_key
= cso_construct_key((void*)templ
,
946 sizeof(struct pipe_shader_state
));
947 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
948 hash_key
, CSO_VERTEX_SHADER
,
953 if (cso_hash_iter_is_null(iter
)) {
954 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
957 return PIPE_ERROR_OUT_OF_MEMORY
;
959 memcpy(cso
->state
, templ
, sizeof(*templ
));
960 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
961 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
962 cso
->context
= ctx
->pipe
;
964 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
965 if (cso_hash_iter_is_null(iter
)) {
967 return PIPE_ERROR_OUT_OF_MEMORY
;
973 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
976 return cso_set_vertex_shader_handle( ctx
, handle
);
982 void cso_save_vertex_shader(struct cso_context
*ctx
)
984 assert(!ctx
->vertex_shader_saved
);
985 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
988 void cso_restore_vertex_shader(struct cso_context
*ctx
)
990 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
991 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
992 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
994 ctx
->vertex_shader_saved
= NULL
;
999 * Copy framebuffer state from src to dst with refcounting of surfaces.
1002 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
1003 const struct pipe_framebuffer_state
*src
)
1007 dst
->width
= src
->width
;
1008 dst
->height
= src
->height
;
1009 dst
->nr_cbufs
= src
->nr_cbufs
;
1010 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1011 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
1013 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
1018 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
1022 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1023 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
1025 pipe_surface_reference(&fb
->zsbuf
, NULL
);
1029 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
1030 const struct pipe_framebuffer_state
*fb
)
1032 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
1033 copy_framebuffer_state(&ctx
->fb
, fb
);
1034 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
1039 void cso_save_framebuffer(struct cso_context
*ctx
)
1041 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
1044 void cso_restore_framebuffer(struct cso_context
*ctx
)
1046 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
1047 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
1048 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
1049 free_framebuffer_state(&ctx
->fb_saved
);
1054 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
1055 const struct pipe_viewport_state
*vp
)
1057 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
1059 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1064 void cso_save_viewport(struct cso_context
*ctx
)
1066 ctx
->vp_saved
= ctx
->vp
;
1070 void cso_restore_viewport(struct cso_context
*ctx
)
1072 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1073 ctx
->vp
= ctx
->vp_saved
;
1074 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1079 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1080 const struct pipe_blend_color
*bc
)
1082 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1083 ctx
->blend_color
= *bc
;
1084 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
1089 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
1090 const struct pipe_stencil_ref
*sr
)
1092 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
1093 ctx
->stencil_ref
= *sr
;
1094 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
1099 void cso_save_stencil_ref(struct cso_context
*ctx
)
1101 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
1105 void cso_restore_stencil_ref(struct cso_context
*ctx
)
1107 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
1108 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
1109 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
1113 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1116 if (ctx
->geometry_shader
!= handle
) {
1117 ctx
->geometry_shader
= handle
;
1118 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1123 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1125 if (handle
== ctx
->geometry_shader
) {
1126 /* unbind before deleting */
1127 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1128 ctx
->geometry_shader
= NULL
;
1130 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1133 void cso_save_geometry_shader(struct cso_context
*ctx
)
1135 assert(!ctx
->geometry_shader_saved
);
1136 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1139 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1141 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1142 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1143 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1145 ctx
->geometry_shader_saved
= NULL
;
1148 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
1150 const struct pipe_vertex_element
*states
)
1152 unsigned key_size
, hash_key
;
1153 struct cso_hash_iter iter
;
1155 struct cso_velems_state velems_state
;
1157 /* need to include the count into the stored state data too.
1158 Otherwise first few count pipe_vertex_elements could be identical even if count
1159 is different, and there's no guarantee the hash would be different in that
1161 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
1162 velems_state
.count
= count
;
1163 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
1164 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
1165 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
1167 if (cso_hash_iter_is_null(iter
)) {
1168 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1170 return PIPE_ERROR_OUT_OF_MEMORY
;
1172 memcpy(&cso
->state
, &velems_state
, key_size
);
1173 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
1174 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
1175 cso
->context
= ctx
->pipe
;
1177 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1178 if (cso_hash_iter_is_null(iter
)) {
1180 return PIPE_ERROR_OUT_OF_MEMORY
;
1186 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1189 if (ctx
->velements
!= handle
) {
1190 ctx
->velements
= handle
;
1191 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1196 void cso_save_vertex_elements(struct cso_context
*ctx
)
1198 assert(!ctx
->velements
);
1199 ctx
->velements_saved
= ctx
->velements
;
1202 void cso_restore_vertex_elements(struct cso_context
*ctx
)
1204 if (ctx
->velements
!= ctx
->velements_saved
) {
1205 ctx
->velements
= ctx
->velements_saved
;
1206 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1208 ctx
->velements_saved
= NULL
;