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_memory.h"
40 #include "tgsi/tgsi_parse.h"
42 #include "cso_cache/cso_context.h"
43 #include "cso_cache/cso_cache.h"
44 #include "cso_cache/cso_hash.h"
47 struct pipe_context
*pipe
;
48 struct cso_cache
*cache
;
51 void *samplers
[PIPE_MAX_SAMPLERS
];
54 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
55 unsigned nr_vertex_samplers
;
58 void *samplers
[PIPE_MAX_SAMPLERS
];
61 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
62 unsigned nr_vertex_samplers
;
64 unsigned nr_samplers_saved
;
65 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
67 unsigned nr_vertex_samplers_saved
;
68 void *vertex_samplers_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
70 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
73 struct pipe_texture
*vertex_textures
[PIPE_MAX_VERTEX_SAMPLERS
];
74 uint nr_vertex_textures
;
76 uint nr_textures_saved
;
77 struct pipe_texture
*textures_saved
[PIPE_MAX_SAMPLERS
];
79 uint nr_vertex_textures_saved
;
80 struct pipe_texture
*vertex_textures_saved
[PIPE_MAX_SAMPLERS
];
82 /** Current and saved state.
83 * The saved state is used as a 1-deep stack.
85 void *blend
, *blend_saved
;
86 void *depth_stencil
, *depth_stencil_saved
;
87 void *rasterizer
, *rasterizer_saved
;
88 void *fragment_shader
, *fragment_shader_saved
;
89 void *vertex_shader
, *vertex_shader_saved
;
91 struct pipe_framebuffer_state fb
, fb_saved
;
92 struct pipe_viewport_state vp
, vp_saved
;
93 struct pipe_blend_color blend_color
;
98 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
101 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
103 struct cso_blend
*cso
= (struct cso_blend
*)state
;
105 if (ctx
->blend
== cso
->data
)
108 if (cso
->delete_state
)
109 cso
->delete_state(cso
->context
, cso
->data
);
114 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
116 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
118 if (ctx
->depth_stencil
== cso
->data
)
121 if (cso
->delete_state
)
122 cso
->delete_state(cso
->context
, cso
->data
);
128 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
130 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
131 if (cso
->delete_state
)
132 cso
->delete_state(cso
->context
, cso
->data
);
137 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
139 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
141 if (ctx
->rasterizer
== cso
->data
)
143 if (cso
->delete_state
)
144 cso
->delete_state(cso
->context
, cso
->data
);
149 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
151 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
152 if (ctx
->fragment_shader
== cso
->data
)
154 if (cso
->delete_state
)
155 cso
->delete_state(cso
->context
, cso
->data
);
160 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
162 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
163 if (ctx
->vertex_shader
== cso
->data
)
165 if (cso
->delete_state
)
166 cso
->delete_state(cso
->context
, cso
->data
);
172 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
173 void *state
, enum cso_cache_type type
)
177 return delete_blend_state(ctx
, state
);
180 return delete_sampler_state(ctx
, state
);
182 case CSO_DEPTH_STENCIL_ALPHA
:
183 return delete_depth_stencil_state(ctx
, state
);
186 return delete_rasterizer_state(ctx
, state
);
188 case CSO_FRAGMENT_SHADER
:
189 return delete_fs_state(ctx
, state
);
191 case CSO_VERTEX_SHADER
:
192 return delete_vs_state(ctx
, state
);
201 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
202 int max_size
, void *user_data
)
204 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
205 /* if we're approach the maximum size, remove fourth of the entries
206 * otherwise every subsequent call will go through the same */
207 int hash_size
= cso_hash_size(hash
);
208 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
209 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
210 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
211 if (hash_size
> max_size
)
212 to_remove
+= hash_size
- max_size
;
214 /*remove elements until we're good */
215 /*fixme: currently we pick the nodes to remove at random*/
216 void *cso
= cso_hash_iter_data(iter
);
217 if (delete_cso(ctx
, cso
, type
)) {
218 iter
= cso_hash_erase(hash
, iter
);
221 iter
= cso_hash_iter_next(iter
);
226 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
228 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
232 ctx
->cache
= cso_cache_create();
233 if (ctx
->cache
== NULL
)
235 cso_cache_set_sanitize_callback(ctx
->cache
,
241 /* Enable for testing: */
242 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
247 cso_destroy_context( ctx
);
253 * Prior to context destruction, this function unbinds all state objects.
255 void cso_release_all( struct cso_context
*ctx
)
260 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
261 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
262 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
263 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
264 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
265 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
266 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
269 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
270 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
271 pipe_texture_reference(&ctx
->textures_saved
[i
], NULL
);
274 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
275 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
276 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], NULL
);
279 free_framebuffer_state(&ctx
->fb
);
280 free_framebuffer_state(&ctx
->fb_saved
);
283 cso_cache_delete( ctx
->cache
);
289 void cso_destroy_context( struct cso_context
*ctx
)
292 /*cso_release_all( ctx );*/
298 /* Those function will either find the state of the given template
299 * in the cache or they will create a new state from the given
300 * template, insert it in the cache and return it.
304 * If the driver returns 0 from the create method then they will assign
305 * the data member of the cso to be the template itself.
308 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
309 const struct pipe_blend_state
*templ
)
311 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_blend_state
));
312 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
317 if (cso_hash_iter_is_null(iter
)) {
318 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
320 return PIPE_ERROR_OUT_OF_MEMORY
;
322 memcpy(&cso
->state
, templ
, sizeof(*templ
));
323 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
324 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
325 cso
->context
= ctx
->pipe
;
327 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
328 if (cso_hash_iter_is_null(iter
)) {
330 return PIPE_ERROR_OUT_OF_MEMORY
;
336 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
339 if (ctx
->blend
!= handle
) {
341 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
346 void cso_save_blend(struct cso_context
*ctx
)
348 assert(!ctx
->blend_saved
);
349 ctx
->blend_saved
= ctx
->blend
;
352 void cso_restore_blend(struct cso_context
*ctx
)
354 if (ctx
->blend
!= ctx
->blend_saved
) {
355 ctx
->blend
= ctx
->blend_saved
;
356 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
358 ctx
->blend_saved
= NULL
;
363 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
365 const struct pipe_sampler_state
*templ
)
370 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_sampler_state
));
371 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
372 hash_key
, CSO_SAMPLER
,
375 if (cso_hash_iter_is_null(iter
)) {
376 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
378 return PIPE_ERROR_OUT_OF_MEMORY
;
380 memcpy(&cso
->state
, templ
, sizeof(*templ
));
381 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
382 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
383 cso
->context
= ctx
->pipe
;
385 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
386 if (cso_hash_iter_is_null(iter
)) {
388 return PIPE_ERROR_OUT_OF_MEMORY
;
394 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
398 ctx
->samplers
[idx
] = handle
;
403 cso_single_vertex_sampler(struct cso_context
*ctx
,
405 const struct pipe_sampler_state
*templ
)
410 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_sampler_state
));
411 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
412 hash_key
, CSO_SAMPLER
,
415 if (cso_hash_iter_is_null(iter
)) {
416 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
418 return PIPE_ERROR_OUT_OF_MEMORY
;
420 memcpy(&cso
->state
, templ
, sizeof(*templ
));
421 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
422 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
423 cso
->context
= ctx
->pipe
;
425 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
426 if (cso_hash_iter_is_null(iter
)) {
428 return PIPE_ERROR_OUT_OF_MEMORY
;
434 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
438 ctx
->vertex_samplers
[idx
] = handle
;
442 void cso_single_sampler_done( struct cso_context
*ctx
)
446 /* find highest non-null sampler */
447 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
448 if (ctx
->samplers
[i
- 1] != NULL
)
452 ctx
->nr_samplers
= i
;
454 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
455 memcmp(ctx
->hw
.samplers
,
457 ctx
->nr_samplers
* sizeof(void *)) != 0)
459 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
460 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
462 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
467 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
471 /* find highest non-null sampler */
472 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
473 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
477 ctx
->nr_vertex_samplers
= i
;
479 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
480 memcmp(ctx
->hw
.vertex_samplers
,
481 ctx
->vertex_samplers
,
482 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
484 memcpy(ctx
->hw
.vertex_samplers
,
485 ctx
->vertex_samplers
,
486 ctx
->nr_vertex_samplers
* sizeof(void *));
487 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
489 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
490 ctx
->nr_vertex_samplers
,
491 ctx
->vertex_samplers
);
496 * If the function encouters any errors it will return the
497 * last one. Done to always try to set as many samplers
500 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
502 const struct pipe_sampler_state
**templates
)
505 enum pipe_error temp
, error
= PIPE_OK
;
510 for (i
= 0; i
< nr
; i
++) {
511 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
516 for ( ; i
< ctx
->nr_samplers
; i
++) {
517 temp
= cso_single_sampler( ctx
, i
, NULL
);
522 cso_single_sampler_done( ctx
);
527 void cso_save_samplers(struct cso_context
*ctx
)
529 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
530 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
533 void cso_restore_samplers(struct cso_context
*ctx
)
535 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
536 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
537 cso_single_sampler_done( ctx
);
541 cso_save_vertex_samplers(struct cso_context
*ctx
)
543 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
544 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
548 cso_restore_vertex_samplers(struct cso_context
*ctx
)
550 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
551 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
552 cso_single_vertex_sampler_done(ctx
);
556 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
558 struct pipe_texture
**textures
)
562 ctx
->nr_textures
= count
;
564 for (i
= 0; i
< count
; i
++)
565 pipe_texture_reference(&ctx
->textures
[i
], textures
[i
]);
566 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
567 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
569 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, count
, textures
);
574 void cso_save_sampler_textures( struct cso_context
*ctx
)
578 ctx
->nr_textures_saved
= ctx
->nr_textures
;
579 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
580 assert(!ctx
->textures_saved
[i
]);
581 pipe_texture_reference(&ctx
->textures_saved
[i
], ctx
->textures
[i
]);
585 void cso_restore_sampler_textures( struct cso_context
*ctx
)
589 ctx
->nr_textures
= ctx
->nr_textures_saved
;
591 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
592 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
593 ctx
->textures
[i
] = ctx
->textures_saved
[i
];
594 ctx
->textures_saved
[i
] = NULL
;
596 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
597 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
599 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, ctx
->nr_textures
, ctx
->textures
);
601 ctx
->nr_textures_saved
= 0;
607 cso_set_vertex_sampler_textures(struct cso_context
*ctx
,
609 struct pipe_texture
**textures
)
613 ctx
->nr_vertex_textures
= count
;
615 for (i
= 0; i
< count
; i
++) {
616 pipe_texture_reference(&ctx
->vertex_textures
[i
], textures
[i
]);
618 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
619 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
622 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
, count
, textures
);
628 cso_save_vertex_sampler_textures(struct cso_context
*ctx
)
632 ctx
->nr_vertex_textures_saved
= ctx
->nr_vertex_textures
;
633 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
634 assert(!ctx
->vertex_textures_saved
[i
]);
635 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], ctx
->vertex_textures
[i
]);
640 cso_restore_vertex_sampler_textures(struct cso_context
*ctx
)
644 ctx
->nr_vertex_textures
= ctx
->nr_vertex_textures_saved
;
646 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
647 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
648 ctx
->vertex_textures
[i
] = ctx
->vertex_textures_saved
[i
];
649 ctx
->vertex_textures_saved
[i
] = NULL
;
651 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
652 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
655 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
,
656 ctx
->nr_vertex_textures
,
657 ctx
->vertex_textures
);
659 ctx
->nr_vertex_textures_saved
= 0;
664 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
665 const struct pipe_depth_stencil_alpha_state
*templ
)
667 unsigned hash_key
= cso_construct_key((void*)templ
,
668 sizeof(struct pipe_depth_stencil_alpha_state
));
669 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
671 CSO_DEPTH_STENCIL_ALPHA
,
675 if (cso_hash_iter_is_null(iter
)) {
676 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
678 return PIPE_ERROR_OUT_OF_MEMORY
;
680 memcpy(&cso
->state
, templ
, sizeof(*templ
));
681 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
682 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
683 cso
->context
= ctx
->pipe
;
685 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
686 if (cso_hash_iter_is_null(iter
)) {
688 return PIPE_ERROR_OUT_OF_MEMORY
;
694 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
697 if (ctx
->depth_stencil
!= handle
) {
698 ctx
->depth_stencil
= handle
;
699 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
704 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
706 assert(!ctx
->depth_stencil_saved
);
707 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
710 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
712 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
713 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
714 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
716 ctx
->depth_stencil_saved
= NULL
;
721 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
722 const struct pipe_rasterizer_state
*templ
)
724 unsigned hash_key
= cso_construct_key((void*)templ
,
725 sizeof(struct pipe_rasterizer_state
));
726 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
727 hash_key
, CSO_RASTERIZER
,
731 if (cso_hash_iter_is_null(iter
)) {
732 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
734 return PIPE_ERROR_OUT_OF_MEMORY
;
736 memcpy(&cso
->state
, templ
, sizeof(*templ
));
737 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
738 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
739 cso
->context
= ctx
->pipe
;
741 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
742 if (cso_hash_iter_is_null(iter
)) {
744 return PIPE_ERROR_OUT_OF_MEMORY
;
750 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
753 if (ctx
->rasterizer
!= handle
) {
754 ctx
->rasterizer
= handle
;
755 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
760 void cso_save_rasterizer(struct cso_context
*ctx
)
762 assert(!ctx
->rasterizer_saved
);
763 ctx
->rasterizer_saved
= ctx
->rasterizer
;
766 void cso_restore_rasterizer(struct cso_context
*ctx
)
768 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
769 ctx
->rasterizer
= ctx
->rasterizer_saved
;
770 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
772 ctx
->rasterizer_saved
= NULL
;
777 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
780 if (ctx
->fragment_shader
!= handle
) {
781 ctx
->fragment_shader
= handle
;
782 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
787 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
789 if (handle
== ctx
->fragment_shader
) {
790 /* unbind before deleting */
791 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
792 ctx
->fragment_shader
= NULL
;
794 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
797 /* Not really working:
800 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
801 const struct pipe_shader_state
*templ
)
803 const struct tgsi_token
*tokens
= templ
->tokens
;
804 unsigned num_tokens
= tgsi_num_tokens(tokens
);
805 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
806 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
807 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
813 if (cso_hash_iter_is_null(iter
)) {
814 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
815 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
818 return PIPE_ERROR_OUT_OF_MEMORY
;
820 memcpy(cso_tokens
, tokens
, tokens_size
);
821 cso
->state
.tokens
= cso_tokens
;
822 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
823 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
824 cso
->context
= ctx
->pipe
;
826 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
827 if (cso_hash_iter_is_null(iter
)) {
829 return PIPE_ERROR_OUT_OF_MEMORY
;
835 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
838 return cso_set_fragment_shader_handle( ctx
, handle
);
842 void cso_save_fragment_shader(struct cso_context
*ctx
)
844 assert(!ctx
->fragment_shader_saved
);
845 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
848 void cso_restore_fragment_shader(struct cso_context
*ctx
)
850 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
851 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
852 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
854 ctx
->fragment_shader_saved
= NULL
;
858 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
861 if (ctx
->vertex_shader
!= handle
) {
862 ctx
->vertex_shader
= handle
;
863 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
868 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
870 if (handle
== ctx
->vertex_shader
) {
871 /* unbind before deleting */
872 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
873 ctx
->vertex_shader
= NULL
;
875 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
879 /* Not really working:
882 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
883 const struct pipe_shader_state
*templ
)
885 unsigned hash_key
= cso_construct_key((void*)templ
,
886 sizeof(struct pipe_shader_state
));
887 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
888 hash_key
, CSO_VERTEX_SHADER
,
892 if (cso_hash_iter_is_null(iter
)) {
893 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
896 return PIPE_ERROR_OUT_OF_MEMORY
;
898 memcpy(cso
->state
, templ
, sizeof(*templ
));
899 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
900 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
901 cso
->context
= ctx
->pipe
;
903 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
904 if (cso_hash_iter_is_null(iter
)) {
906 return PIPE_ERROR_OUT_OF_MEMORY
;
912 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
915 return cso_set_vertex_shader_handle( ctx
, handle
);
921 void cso_save_vertex_shader(struct cso_context
*ctx
)
923 assert(!ctx
->vertex_shader_saved
);
924 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
927 void cso_restore_vertex_shader(struct cso_context
*ctx
)
929 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
930 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
931 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
933 ctx
->vertex_shader_saved
= NULL
;
938 * Copy framebuffer state from src to dst with refcounting of surfaces.
941 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
942 const struct pipe_framebuffer_state
*src
)
946 dst
->width
= src
->width
;
947 dst
->height
= src
->height
;
948 dst
->nr_cbufs
= src
->nr_cbufs
;
949 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
950 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
952 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
957 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
961 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
962 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
964 pipe_surface_reference(&fb
->zsbuf
, NULL
);
968 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
969 const struct pipe_framebuffer_state
*fb
)
971 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
972 copy_framebuffer_state(&ctx
->fb
, fb
);
973 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
978 void cso_save_framebuffer(struct cso_context
*ctx
)
980 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
983 void cso_restore_framebuffer(struct cso_context
*ctx
)
985 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
986 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
987 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
988 free_framebuffer_state(&ctx
->fb_saved
);
993 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
994 const struct pipe_viewport_state
*vp
)
996 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
998 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1003 void cso_save_viewport(struct cso_context
*ctx
)
1005 ctx
->vp_saved
= ctx
->vp
;
1009 void cso_restore_viewport(struct cso_context
*ctx
)
1011 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1012 ctx
->vp
= ctx
->vp_saved
;
1013 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1020 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1021 const struct pipe_blend_color
*bc
)
1023 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1024 ctx
->blend_color
= *bc
;
1025 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);