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
);
294 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
295 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
296 pipe_texture_reference(&ctx
->textures_saved
[i
], NULL
);
299 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
300 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
301 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], NULL
);
304 free_framebuffer_state(&ctx
->fb
);
305 free_framebuffer_state(&ctx
->fb_saved
);
308 cso_cache_delete( ctx
->cache
);
314 void cso_destroy_context( struct cso_context
*ctx
)
317 /*cso_release_all( ctx );*/
323 /* Those function will either find the state of the given template
324 * in the cache or they will create a new state from the given
325 * template, insert it in the cache and return it.
329 * If the driver returns 0 from the create method then they will assign
330 * the data member of the cso to be the template itself.
333 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
334 const struct pipe_blend_state
*templ
)
336 unsigned key_size
, hash_key
;
337 struct cso_hash_iter iter
;
340 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
341 (char *)&(templ
->rt
[1]) - (char *)templ
;
342 hash_key
= cso_construct_key((void*)templ
, key_size
);
343 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
345 if (cso_hash_iter_is_null(iter
)) {
346 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
348 return PIPE_ERROR_OUT_OF_MEMORY
;
350 memcpy(&cso
->state
, templ
, key_size
);
351 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
352 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
353 cso
->context
= ctx
->pipe
;
355 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
356 if (cso_hash_iter_is_null(iter
)) {
358 return PIPE_ERROR_OUT_OF_MEMORY
;
364 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
367 if (ctx
->blend
!= handle
) {
369 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
374 void cso_save_blend(struct cso_context
*ctx
)
376 assert(!ctx
->blend_saved
);
377 ctx
->blend_saved
= ctx
->blend
;
380 void cso_restore_blend(struct cso_context
*ctx
)
382 if (ctx
->blend
!= ctx
->blend_saved
) {
383 ctx
->blend
= ctx
->blend_saved
;
384 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
386 ctx
->blend_saved
= NULL
;
391 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
393 const struct pipe_sampler_state
*templ
)
398 unsigned key_size
= sizeof(struct pipe_sampler_state
);
399 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
400 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
401 hash_key
, CSO_SAMPLER
,
402 (void*)templ
, key_size
);
404 if (cso_hash_iter_is_null(iter
)) {
405 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
407 return PIPE_ERROR_OUT_OF_MEMORY
;
409 memcpy(&cso
->state
, templ
, sizeof(*templ
));
410 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
411 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
412 cso
->context
= ctx
->pipe
;
414 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
415 if (cso_hash_iter_is_null(iter
)) {
417 return PIPE_ERROR_OUT_OF_MEMORY
;
423 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
427 ctx
->samplers
[idx
] = handle
;
432 cso_single_vertex_sampler(struct cso_context
*ctx
,
434 const struct pipe_sampler_state
*templ
)
439 unsigned key_size
= sizeof(struct pipe_sampler_state
);
440 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
441 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
442 hash_key
, CSO_SAMPLER
,
443 (void*)templ
, key_size
);
445 if (cso_hash_iter_is_null(iter
)) {
446 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
448 return PIPE_ERROR_OUT_OF_MEMORY
;
450 memcpy(&cso
->state
, templ
, sizeof(*templ
));
451 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
452 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
453 cso
->context
= ctx
->pipe
;
455 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
456 if (cso_hash_iter_is_null(iter
)) {
458 return PIPE_ERROR_OUT_OF_MEMORY
;
464 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
468 ctx
->vertex_samplers
[idx
] = handle
;
472 void cso_single_sampler_done( struct cso_context
*ctx
)
476 /* find highest non-null sampler */
477 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
478 if (ctx
->samplers
[i
- 1] != NULL
)
482 ctx
->nr_samplers
= i
;
484 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
485 memcmp(ctx
->hw
.samplers
,
487 ctx
->nr_samplers
* sizeof(void *)) != 0)
489 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
490 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
492 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
497 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
501 /* find highest non-null sampler */
502 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
503 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
507 ctx
->nr_vertex_samplers
= i
;
509 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
510 memcmp(ctx
->hw
.vertex_samplers
,
511 ctx
->vertex_samplers
,
512 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
514 memcpy(ctx
->hw
.vertex_samplers
,
515 ctx
->vertex_samplers
,
516 ctx
->nr_vertex_samplers
* sizeof(void *));
517 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
519 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
520 ctx
->nr_vertex_samplers
,
521 ctx
->vertex_samplers
);
526 * If the function encouters any errors it will return the
527 * last one. Done to always try to set as many samplers
530 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
532 const struct pipe_sampler_state
**templates
)
535 enum pipe_error temp
, error
= PIPE_OK
;
540 for (i
= 0; i
< nr
; i
++) {
541 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
546 for ( ; i
< ctx
->nr_samplers
; i
++) {
547 temp
= cso_single_sampler( ctx
, i
, NULL
);
552 cso_single_sampler_done( ctx
);
557 void cso_save_samplers(struct cso_context
*ctx
)
559 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
560 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
563 void cso_restore_samplers(struct cso_context
*ctx
)
565 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
566 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
567 cso_single_sampler_done( ctx
);
571 * If the function encouters any errors it will return the
572 * last one. Done to always try to set as many samplers
575 enum pipe_error
cso_set_vertex_samplers(struct cso_context
*ctx
,
577 const struct pipe_sampler_state
**templates
)
580 enum pipe_error temp
, error
= PIPE_OK
;
585 for (i
= 0; i
< nr
; i
++) {
586 temp
= cso_single_vertex_sampler( ctx
, i
, templates
[i
] );
591 for ( ; i
< ctx
->nr_samplers
; i
++) {
592 temp
= cso_single_vertex_sampler( ctx
, i
, NULL
);
597 cso_single_vertex_sampler_done( ctx
);
603 cso_save_vertex_samplers(struct cso_context
*ctx
)
605 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
606 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
610 cso_restore_vertex_samplers(struct cso_context
*ctx
)
612 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
613 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
614 cso_single_vertex_sampler_done(ctx
);
618 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
620 struct pipe_texture
**textures
)
624 ctx
->nr_textures
= count
;
626 for (i
= 0; i
< count
; i
++)
627 pipe_texture_reference(&ctx
->textures
[i
], textures
[i
]);
628 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
629 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
631 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, count
, textures
);
636 void cso_save_sampler_textures( struct cso_context
*ctx
)
640 ctx
->nr_textures_saved
= ctx
->nr_textures
;
641 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
642 assert(!ctx
->textures_saved
[i
]);
643 pipe_texture_reference(&ctx
->textures_saved
[i
], ctx
->textures
[i
]);
647 void cso_restore_sampler_textures( struct cso_context
*ctx
)
651 ctx
->nr_textures
= ctx
->nr_textures_saved
;
653 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
654 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
655 ctx
->textures
[i
] = ctx
->textures_saved
[i
];
656 ctx
->textures_saved
[i
] = NULL
;
658 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
659 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
661 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, ctx
->nr_textures
, ctx
->textures
);
663 ctx
->nr_textures_saved
= 0;
669 cso_set_vertex_sampler_textures(struct cso_context
*ctx
,
671 struct pipe_texture
**textures
)
675 ctx
->nr_vertex_textures
= count
;
677 for (i
= 0; i
< count
; i
++) {
678 pipe_texture_reference(&ctx
->vertex_textures
[i
], textures
[i
]);
680 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
681 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
684 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
, count
, textures
);
690 cso_save_vertex_sampler_textures(struct cso_context
*ctx
)
694 ctx
->nr_vertex_textures_saved
= ctx
->nr_vertex_textures
;
695 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
696 assert(!ctx
->vertex_textures_saved
[i
]);
697 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], ctx
->vertex_textures
[i
]);
702 cso_restore_vertex_sampler_textures(struct cso_context
*ctx
)
706 ctx
->nr_vertex_textures
= ctx
->nr_vertex_textures_saved
;
708 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
709 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
710 ctx
->vertex_textures
[i
] = ctx
->vertex_textures_saved
[i
];
711 ctx
->vertex_textures_saved
[i
] = NULL
;
713 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
714 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
717 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
,
718 ctx
->nr_vertex_textures
,
719 ctx
->vertex_textures
);
721 ctx
->nr_vertex_textures_saved
= 0;
726 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
727 const struct pipe_depth_stencil_alpha_state
*templ
)
729 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
730 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
731 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
733 CSO_DEPTH_STENCIL_ALPHA
,
734 (void*)templ
, key_size
);
737 if (cso_hash_iter_is_null(iter
)) {
738 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
740 return PIPE_ERROR_OUT_OF_MEMORY
;
742 memcpy(&cso
->state
, templ
, sizeof(*templ
));
743 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
744 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
745 cso
->context
= ctx
->pipe
;
747 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
748 if (cso_hash_iter_is_null(iter
)) {
750 return PIPE_ERROR_OUT_OF_MEMORY
;
756 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
759 if (ctx
->depth_stencil
!= handle
) {
760 ctx
->depth_stencil
= handle
;
761 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
766 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
768 assert(!ctx
->depth_stencil_saved
);
769 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
772 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
774 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
775 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
776 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
778 ctx
->depth_stencil_saved
= NULL
;
783 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
784 const struct pipe_rasterizer_state
*templ
)
786 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
787 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
788 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
789 hash_key
, CSO_RASTERIZER
,
790 (void*)templ
, key_size
);
793 if (cso_hash_iter_is_null(iter
)) {
794 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
796 return PIPE_ERROR_OUT_OF_MEMORY
;
798 memcpy(&cso
->state
, templ
, sizeof(*templ
));
799 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
800 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
801 cso
->context
= ctx
->pipe
;
803 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
804 if (cso_hash_iter_is_null(iter
)) {
806 return PIPE_ERROR_OUT_OF_MEMORY
;
812 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
815 if (ctx
->rasterizer
!= handle
) {
816 ctx
->rasterizer
= handle
;
817 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
822 void cso_save_rasterizer(struct cso_context
*ctx
)
824 assert(!ctx
->rasterizer_saved
);
825 ctx
->rasterizer_saved
= ctx
->rasterizer
;
828 void cso_restore_rasterizer(struct cso_context
*ctx
)
830 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
831 ctx
->rasterizer
= ctx
->rasterizer_saved
;
832 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
834 ctx
->rasterizer_saved
= NULL
;
839 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
842 if (ctx
->fragment_shader
!= handle
) {
843 ctx
->fragment_shader
= handle
;
844 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
849 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
851 if (handle
== ctx
->fragment_shader
) {
852 /* unbind before deleting */
853 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
854 ctx
->fragment_shader
= NULL
;
856 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
859 /* Not really working:
862 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
863 const struct pipe_shader_state
*templ
)
865 const struct tgsi_token
*tokens
= templ
->tokens
;
866 unsigned num_tokens
= tgsi_num_tokens(tokens
);
867 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
868 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
869 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
873 sizeof(*templ
)); /* XXX correct? tokens_size? */
876 if (cso_hash_iter_is_null(iter
)) {
877 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
878 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
881 return PIPE_ERROR_OUT_OF_MEMORY
;
883 memcpy(cso_tokens
, tokens
, tokens_size
);
884 cso
->state
.tokens
= cso_tokens
;
885 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
886 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
887 cso
->context
= ctx
->pipe
;
889 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
890 if (cso_hash_iter_is_null(iter
)) {
892 return PIPE_ERROR_OUT_OF_MEMORY
;
898 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
901 return cso_set_fragment_shader_handle( ctx
, handle
);
905 void cso_save_fragment_shader(struct cso_context
*ctx
)
907 assert(!ctx
->fragment_shader_saved
);
908 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
911 void cso_restore_fragment_shader(struct cso_context
*ctx
)
913 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
914 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
915 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
917 ctx
->fragment_shader_saved
= NULL
;
921 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
924 if (ctx
->vertex_shader
!= handle
) {
925 ctx
->vertex_shader
= handle
;
926 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
931 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
933 if (handle
== ctx
->vertex_shader
) {
934 /* unbind before deleting */
935 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
936 ctx
->vertex_shader
= NULL
;
938 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
942 /* Not really working:
945 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
946 const struct pipe_shader_state
*templ
)
948 unsigned hash_key
= cso_construct_key((void*)templ
,
949 sizeof(struct pipe_shader_state
));
950 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
951 hash_key
, CSO_VERTEX_SHADER
,
956 if (cso_hash_iter_is_null(iter
)) {
957 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
960 return PIPE_ERROR_OUT_OF_MEMORY
;
962 memcpy(cso
->state
, templ
, sizeof(*templ
));
963 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
964 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
965 cso
->context
= ctx
->pipe
;
967 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
968 if (cso_hash_iter_is_null(iter
)) {
970 return PIPE_ERROR_OUT_OF_MEMORY
;
976 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
979 return cso_set_vertex_shader_handle( ctx
, handle
);
985 void cso_save_vertex_shader(struct cso_context
*ctx
)
987 assert(!ctx
->vertex_shader_saved
);
988 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
991 void cso_restore_vertex_shader(struct cso_context
*ctx
)
993 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
994 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
995 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
997 ctx
->vertex_shader_saved
= NULL
;
1002 * Copy framebuffer state from src to dst with refcounting of surfaces.
1005 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
1006 const struct pipe_framebuffer_state
*src
)
1010 dst
->width
= src
->width
;
1011 dst
->height
= src
->height
;
1012 dst
->nr_cbufs
= src
->nr_cbufs
;
1013 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1014 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
1016 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
1021 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
1025 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1026 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
1028 pipe_surface_reference(&fb
->zsbuf
, NULL
);
1032 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
1033 const struct pipe_framebuffer_state
*fb
)
1035 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
1036 copy_framebuffer_state(&ctx
->fb
, fb
);
1037 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
1042 void cso_save_framebuffer(struct cso_context
*ctx
)
1044 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
1047 void cso_restore_framebuffer(struct cso_context
*ctx
)
1049 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
1050 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
1051 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
1052 free_framebuffer_state(&ctx
->fb_saved
);
1057 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
1058 const struct pipe_viewport_state
*vp
)
1060 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
1062 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1067 void cso_save_viewport(struct cso_context
*ctx
)
1069 ctx
->vp_saved
= ctx
->vp
;
1073 void cso_restore_viewport(struct cso_context
*ctx
)
1075 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1076 ctx
->vp
= ctx
->vp_saved
;
1077 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1082 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1083 const struct pipe_blend_color
*bc
)
1085 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1086 ctx
->blend_color
= *bc
;
1087 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
1092 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
1093 const struct pipe_stencil_ref
*sr
)
1095 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
1096 ctx
->stencil_ref
= *sr
;
1097 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
1102 void cso_save_stencil_ref(struct cso_context
*ctx
)
1104 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
1108 void cso_restore_stencil_ref(struct cso_context
*ctx
)
1110 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
1111 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
1112 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
1116 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1119 if (ctx
->geometry_shader
!= handle
) {
1120 ctx
->geometry_shader
= handle
;
1121 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1126 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1128 if (handle
== ctx
->geometry_shader
) {
1129 /* unbind before deleting */
1130 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1131 ctx
->geometry_shader
= NULL
;
1133 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1136 void cso_save_geometry_shader(struct cso_context
*ctx
)
1138 assert(!ctx
->geometry_shader_saved
);
1139 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1142 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1144 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1145 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1146 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1148 ctx
->geometry_shader_saved
= NULL
;
1154 clip_state_cpy(struct pipe_clip_state
*dst
,
1155 const struct pipe_clip_state
*src
)
1159 memcpy(dst
->ucp
, src
->ucp
, src
->nr
* sizeof(src
->ucp
[0]));
1164 clip_state_cmp(const struct pipe_clip_state
*a
,
1165 const struct pipe_clip_state
*b
)
1167 if (a
->nr
!= b
->nr
) {
1171 return memcmp(a
->ucp
, b
->ucp
, a
->nr
* sizeof(a
->ucp
[0]));
1177 cso_set_clip(struct cso_context
*ctx
,
1178 const struct pipe_clip_state
*clip
)
1180 if (clip_state_cmp(&ctx
->clip
, clip
)) {
1181 clip_state_cpy(&ctx
->clip
, clip
);
1182 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
1187 cso_save_clip(struct cso_context
*ctx
)
1189 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
1193 cso_restore_clip(struct cso_context
*ctx
)
1195 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
1196 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
1197 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
1201 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
1203 const struct pipe_vertex_element
*states
)
1205 unsigned key_size
, hash_key
;
1206 struct cso_hash_iter iter
;
1208 struct cso_velems_state velems_state
;
1210 /* need to include the count into the stored state data too.
1211 Otherwise first few count pipe_vertex_elements could be identical even if count
1212 is different, and there's no guarantee the hash would be different in that
1214 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
1215 velems_state
.count
= count
;
1216 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
1217 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
1218 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
1220 if (cso_hash_iter_is_null(iter
)) {
1221 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1223 return PIPE_ERROR_OUT_OF_MEMORY
;
1225 memcpy(&cso
->state
, &velems_state
, key_size
);
1226 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
1227 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
1228 cso
->context
= ctx
->pipe
;
1230 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1231 if (cso_hash_iter_is_null(iter
)) {
1233 return PIPE_ERROR_OUT_OF_MEMORY
;
1239 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1242 if (ctx
->velements
!= handle
) {
1243 ctx
->velements
= handle
;
1244 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1249 void cso_save_vertex_elements(struct cso_context
*ctx
)
1251 assert(!ctx
->velements_saved
);
1252 ctx
->velements_saved
= ctx
->velements
;
1255 void cso_restore_vertex_elements(struct cso_context
*ctx
)
1257 if (ctx
->velements
!= ctx
->velements_saved
) {
1258 ctx
->velements
= ctx
->velements_saved
;
1259 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1261 ctx
->velements_saved
= NULL
;