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"
45 #include "cso_context.h"
48 struct pipe_context
*pipe
;
49 struct cso_cache
*cache
;
52 void *samplers
[PIPE_MAX_SAMPLERS
];
55 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
56 unsigned nr_vertex_samplers
;
59 void *samplers
[PIPE_MAX_SAMPLERS
];
62 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
63 unsigned nr_vertex_samplers
;
65 unsigned nr_samplers_saved
;
66 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
68 unsigned nr_vertex_samplers_saved
;
69 void *vertex_samplers_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
71 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
74 struct pipe_texture
*vertex_textures
[PIPE_MAX_VERTEX_SAMPLERS
];
75 uint nr_vertex_textures
;
77 uint nr_textures_saved
;
78 struct pipe_texture
*textures_saved
[PIPE_MAX_SAMPLERS
];
80 uint nr_vertex_textures_saved
;
81 struct pipe_texture
*vertex_textures_saved
[PIPE_MAX_SAMPLERS
];
83 /** Current and saved state.
84 * The saved state is used as a 1-deep stack.
86 void *blend
, *blend_saved
;
87 void *depth_stencil
, *depth_stencil_saved
;
88 void *rasterizer
, *rasterizer_saved
;
89 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
90 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
92 struct pipe_framebuffer_state fb
, fb_saved
;
93 struct pipe_viewport_state vp
, vp_saved
;
94 struct pipe_blend_color blend_color
;
99 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
102 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
104 struct cso_blend
*cso
= (struct cso_blend
*)state
;
106 if (ctx
->blend
== cso
->data
)
109 if (cso
->delete_state
)
110 cso
->delete_state(cso
->context
, cso
->data
);
115 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
117 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
119 if (ctx
->depth_stencil
== cso
->data
)
122 if (cso
->delete_state
)
123 cso
->delete_state(cso
->context
, cso
->data
);
129 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
131 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
132 if (cso
->delete_state
)
133 cso
->delete_state(cso
->context
, cso
->data
);
138 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
140 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
142 if (ctx
->rasterizer
== cso
->data
)
144 if (cso
->delete_state
)
145 cso
->delete_state(cso
->context
, cso
->data
);
150 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
152 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
153 if (ctx
->fragment_shader
== cso
->data
)
155 if (cso
->delete_state
)
156 cso
->delete_state(cso
->context
, cso
->data
);
161 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
163 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
164 if (ctx
->vertex_shader
== cso
->data
)
166 if (cso
->delete_state
)
167 cso
->delete_state(cso
->context
, cso
->data
);
173 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
174 void *state
, enum cso_cache_type type
)
178 return delete_blend_state(ctx
, state
);
181 return delete_sampler_state(ctx
, state
);
183 case CSO_DEPTH_STENCIL_ALPHA
:
184 return delete_depth_stencil_state(ctx
, state
);
187 return delete_rasterizer_state(ctx
, state
);
189 case CSO_FRAGMENT_SHADER
:
190 return delete_fs_state(ctx
, state
);
192 case CSO_VERTEX_SHADER
:
193 return delete_vs_state(ctx
, state
);
202 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
203 int max_size
, void *user_data
)
205 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
206 /* if we're approach the maximum size, remove fourth of the entries
207 * otherwise every subsequent call will go through the same */
208 int hash_size
= cso_hash_size(hash
);
209 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
210 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
211 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
212 if (hash_size
> max_size
)
213 to_remove
+= hash_size
- max_size
;
215 /*remove elements until we're good */
216 /*fixme: currently we pick the nodes to remove at random*/
217 void *cso
= cso_hash_iter_data(iter
);
218 if (delete_cso(ctx
, cso
, type
)) {
219 iter
= cso_hash_erase(hash
, iter
);
222 iter
= cso_hash_iter_next(iter
);
227 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
229 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
233 ctx
->cache
= cso_cache_create();
234 if (ctx
->cache
== NULL
)
236 cso_cache_set_sanitize_callback(ctx
->cache
,
242 /* Enable for testing: */
243 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
248 cso_destroy_context( ctx
);
254 * Prior to context destruction, this function unbinds all state objects.
256 void cso_release_all( struct cso_context
*ctx
)
261 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
262 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
263 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
264 if (ctx
->pipe
->bind_vertex_sampler_states
)
265 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
266 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
267 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
268 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
271 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
272 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
273 pipe_texture_reference(&ctx
->textures_saved
[i
], NULL
);
276 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
277 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
278 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], NULL
);
281 free_framebuffer_state(&ctx
->fb
);
282 free_framebuffer_state(&ctx
->fb_saved
);
285 cso_cache_delete( ctx
->cache
);
291 void cso_destroy_context( struct cso_context
*ctx
)
294 /*cso_release_all( ctx );*/
300 /* Those function will either find the state of the given template
301 * in the cache or they will create a new state from the given
302 * template, insert it in the cache and return it.
306 * If the driver returns 0 from the create method then they will assign
307 * the data member of the cso to be the template itself.
310 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
311 const struct pipe_blend_state
*templ
)
313 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_blend_state
));
314 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
319 if (cso_hash_iter_is_null(iter
)) {
320 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
322 return PIPE_ERROR_OUT_OF_MEMORY
;
324 memcpy(&cso
->state
, templ
, sizeof(*templ
));
325 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
326 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
327 cso
->context
= ctx
->pipe
;
329 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
330 if (cso_hash_iter_is_null(iter
)) {
332 return PIPE_ERROR_OUT_OF_MEMORY
;
338 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
341 if (ctx
->blend
!= handle
) {
343 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
348 void cso_save_blend(struct cso_context
*ctx
)
350 assert(!ctx
->blend_saved
);
351 ctx
->blend_saved
= ctx
->blend
;
354 void cso_restore_blend(struct cso_context
*ctx
)
356 if (ctx
->blend
!= ctx
->blend_saved
) {
357 ctx
->blend
= ctx
->blend_saved
;
358 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
360 ctx
->blend_saved
= NULL
;
365 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
367 const struct pipe_sampler_state
*templ
)
372 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_sampler_state
));
373 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
374 hash_key
, CSO_SAMPLER
,
377 if (cso_hash_iter_is_null(iter
)) {
378 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
380 return PIPE_ERROR_OUT_OF_MEMORY
;
382 memcpy(&cso
->state
, templ
, sizeof(*templ
));
383 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
384 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
385 cso
->context
= ctx
->pipe
;
387 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
388 if (cso_hash_iter_is_null(iter
)) {
390 return PIPE_ERROR_OUT_OF_MEMORY
;
396 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
400 ctx
->samplers
[idx
] = handle
;
405 cso_single_vertex_sampler(struct cso_context
*ctx
,
407 const struct pipe_sampler_state
*templ
)
412 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_sampler_state
));
413 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
414 hash_key
, CSO_SAMPLER
,
417 if (cso_hash_iter_is_null(iter
)) {
418 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
420 return PIPE_ERROR_OUT_OF_MEMORY
;
422 memcpy(&cso
->state
, templ
, sizeof(*templ
));
423 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
424 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
425 cso
->context
= ctx
->pipe
;
427 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
428 if (cso_hash_iter_is_null(iter
)) {
430 return PIPE_ERROR_OUT_OF_MEMORY
;
436 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
440 ctx
->vertex_samplers
[idx
] = handle
;
444 void cso_single_sampler_done( struct cso_context
*ctx
)
448 /* find highest non-null sampler */
449 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
450 if (ctx
->samplers
[i
- 1] != NULL
)
454 ctx
->nr_samplers
= i
;
456 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
457 memcmp(ctx
->hw
.samplers
,
459 ctx
->nr_samplers
* sizeof(void *)) != 0)
461 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
462 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
464 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
469 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
473 /* find highest non-null sampler */
474 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
475 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
479 ctx
->nr_vertex_samplers
= i
;
481 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
482 memcmp(ctx
->hw
.vertex_samplers
,
483 ctx
->vertex_samplers
,
484 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
486 memcpy(ctx
->hw
.vertex_samplers
,
487 ctx
->vertex_samplers
,
488 ctx
->nr_vertex_samplers
* sizeof(void *));
489 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
491 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
492 ctx
->nr_vertex_samplers
,
493 ctx
->vertex_samplers
);
498 * If the function encouters any errors it will return the
499 * last one. Done to always try to set as many samplers
502 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
504 const struct pipe_sampler_state
**templates
)
507 enum pipe_error temp
, error
= PIPE_OK
;
512 for (i
= 0; i
< nr
; i
++) {
513 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
518 for ( ; i
< ctx
->nr_samplers
; i
++) {
519 temp
= cso_single_sampler( ctx
, i
, NULL
);
524 cso_single_sampler_done( ctx
);
529 void cso_save_samplers(struct cso_context
*ctx
)
531 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
532 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
535 void cso_restore_samplers(struct cso_context
*ctx
)
537 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
538 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
539 cso_single_sampler_done( ctx
);
543 cso_save_vertex_samplers(struct cso_context
*ctx
)
545 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
546 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
550 cso_restore_vertex_samplers(struct cso_context
*ctx
)
552 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
553 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
554 cso_single_vertex_sampler_done(ctx
);
558 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
560 struct pipe_texture
**textures
)
564 ctx
->nr_textures
= count
;
566 for (i
= 0; i
< count
; i
++)
567 pipe_texture_reference(&ctx
->textures
[i
], textures
[i
]);
568 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
569 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
571 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, count
, textures
);
576 void cso_save_sampler_textures( struct cso_context
*ctx
)
580 ctx
->nr_textures_saved
= ctx
->nr_textures
;
581 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
582 assert(!ctx
->textures_saved
[i
]);
583 pipe_texture_reference(&ctx
->textures_saved
[i
], ctx
->textures
[i
]);
587 void cso_restore_sampler_textures( struct cso_context
*ctx
)
591 ctx
->nr_textures
= ctx
->nr_textures_saved
;
593 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
594 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
595 ctx
->textures
[i
] = ctx
->textures_saved
[i
];
596 ctx
->textures_saved
[i
] = NULL
;
598 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
599 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
601 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, ctx
->nr_textures
, ctx
->textures
);
603 ctx
->nr_textures_saved
= 0;
609 cso_set_vertex_sampler_textures(struct cso_context
*ctx
,
611 struct pipe_texture
**textures
)
615 ctx
->nr_vertex_textures
= count
;
617 for (i
= 0; i
< count
; i
++) {
618 pipe_texture_reference(&ctx
->vertex_textures
[i
], textures
[i
]);
620 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
621 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
624 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
, count
, textures
);
630 cso_save_vertex_sampler_textures(struct cso_context
*ctx
)
634 ctx
->nr_vertex_textures_saved
= ctx
->nr_vertex_textures
;
635 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
636 assert(!ctx
->vertex_textures_saved
[i
]);
637 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], ctx
->vertex_textures
[i
]);
642 cso_restore_vertex_sampler_textures(struct cso_context
*ctx
)
646 ctx
->nr_vertex_textures
= ctx
->nr_vertex_textures_saved
;
648 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
649 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
650 ctx
->vertex_textures
[i
] = ctx
->vertex_textures_saved
[i
];
651 ctx
->vertex_textures_saved
[i
] = NULL
;
653 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
654 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
657 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
,
658 ctx
->nr_vertex_textures
,
659 ctx
->vertex_textures
);
661 ctx
->nr_vertex_textures_saved
= 0;
666 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
667 const struct pipe_depth_stencil_alpha_state
*templ
)
669 unsigned hash_key
= cso_construct_key((void*)templ
,
670 sizeof(struct pipe_depth_stencil_alpha_state
));
671 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
673 CSO_DEPTH_STENCIL_ALPHA
,
677 if (cso_hash_iter_is_null(iter
)) {
678 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
680 return PIPE_ERROR_OUT_OF_MEMORY
;
682 memcpy(&cso
->state
, templ
, sizeof(*templ
));
683 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
684 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
685 cso
->context
= ctx
->pipe
;
687 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
688 if (cso_hash_iter_is_null(iter
)) {
690 return PIPE_ERROR_OUT_OF_MEMORY
;
696 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
699 if (ctx
->depth_stencil
!= handle
) {
700 ctx
->depth_stencil
= handle
;
701 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
706 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
708 assert(!ctx
->depth_stencil_saved
);
709 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
712 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
714 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
715 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
716 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
718 ctx
->depth_stencil_saved
= NULL
;
723 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
724 const struct pipe_rasterizer_state
*templ
)
726 unsigned hash_key
= cso_construct_key((void*)templ
,
727 sizeof(struct pipe_rasterizer_state
));
728 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
729 hash_key
, CSO_RASTERIZER
,
733 if (cso_hash_iter_is_null(iter
)) {
734 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
736 return PIPE_ERROR_OUT_OF_MEMORY
;
738 memcpy(&cso
->state
, templ
, sizeof(*templ
));
739 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
740 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
741 cso
->context
= ctx
->pipe
;
743 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
744 if (cso_hash_iter_is_null(iter
)) {
746 return PIPE_ERROR_OUT_OF_MEMORY
;
752 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
755 if (ctx
->rasterizer
!= handle
) {
756 ctx
->rasterizer
= handle
;
757 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
762 void cso_save_rasterizer(struct cso_context
*ctx
)
764 assert(!ctx
->rasterizer_saved
);
765 ctx
->rasterizer_saved
= ctx
->rasterizer
;
768 void cso_restore_rasterizer(struct cso_context
*ctx
)
770 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
771 ctx
->rasterizer
= ctx
->rasterizer_saved
;
772 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
774 ctx
->rasterizer_saved
= NULL
;
779 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
782 if (ctx
->fragment_shader
!= handle
) {
783 ctx
->fragment_shader
= handle
;
784 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
789 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
791 if (handle
== ctx
->fragment_shader
) {
792 /* unbind before deleting */
793 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
794 ctx
->fragment_shader
= NULL
;
796 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
799 /* Not really working:
802 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
803 const struct pipe_shader_state
*templ
)
805 const struct tgsi_token
*tokens
= templ
->tokens
;
806 unsigned num_tokens
= tgsi_num_tokens(tokens
);
807 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
808 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
809 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
815 if (cso_hash_iter_is_null(iter
)) {
816 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
817 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
820 return PIPE_ERROR_OUT_OF_MEMORY
;
822 memcpy(cso_tokens
, tokens
, tokens_size
);
823 cso
->state
.tokens
= cso_tokens
;
824 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
825 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
826 cso
->context
= ctx
->pipe
;
828 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
829 if (cso_hash_iter_is_null(iter
)) {
831 return PIPE_ERROR_OUT_OF_MEMORY
;
837 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
840 return cso_set_fragment_shader_handle( ctx
, handle
);
844 void cso_save_fragment_shader(struct cso_context
*ctx
)
846 assert(!ctx
->fragment_shader_saved
);
847 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
850 void cso_restore_fragment_shader(struct cso_context
*ctx
)
852 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
853 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
854 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
856 ctx
->fragment_shader_saved
= NULL
;
860 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
863 if (ctx
->vertex_shader
!= handle
) {
864 ctx
->vertex_shader
= handle
;
865 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
870 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
872 if (handle
== ctx
->vertex_shader
) {
873 /* unbind before deleting */
874 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
875 ctx
->vertex_shader
= NULL
;
877 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
881 /* Not really working:
884 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
885 const struct pipe_shader_state
*templ
)
887 unsigned hash_key
= cso_construct_key((void*)templ
,
888 sizeof(struct pipe_shader_state
));
889 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
890 hash_key
, CSO_VERTEX_SHADER
,
894 if (cso_hash_iter_is_null(iter
)) {
895 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
898 return PIPE_ERROR_OUT_OF_MEMORY
;
900 memcpy(cso
->state
, templ
, sizeof(*templ
));
901 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
902 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
903 cso
->context
= ctx
->pipe
;
905 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
906 if (cso_hash_iter_is_null(iter
)) {
908 return PIPE_ERROR_OUT_OF_MEMORY
;
914 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
917 return cso_set_vertex_shader_handle( ctx
, handle
);
923 void cso_save_vertex_shader(struct cso_context
*ctx
)
925 assert(!ctx
->vertex_shader_saved
);
926 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
929 void cso_restore_vertex_shader(struct cso_context
*ctx
)
931 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
932 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
933 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
935 ctx
->vertex_shader_saved
= NULL
;
940 * Copy framebuffer state from src to dst with refcounting of surfaces.
943 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
944 const struct pipe_framebuffer_state
*src
)
948 dst
->width
= src
->width
;
949 dst
->height
= src
->height
;
950 dst
->nr_cbufs
= src
->nr_cbufs
;
951 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
952 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
954 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
959 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
963 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
964 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
966 pipe_surface_reference(&fb
->zsbuf
, NULL
);
970 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
971 const struct pipe_framebuffer_state
*fb
)
973 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
974 copy_framebuffer_state(&ctx
->fb
, fb
);
975 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
980 void cso_save_framebuffer(struct cso_context
*ctx
)
982 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
985 void cso_restore_framebuffer(struct cso_context
*ctx
)
987 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
988 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
989 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
990 free_framebuffer_state(&ctx
->fb_saved
);
995 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
996 const struct pipe_viewport_state
*vp
)
998 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
1000 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1005 void cso_save_viewport(struct cso_context
*ctx
)
1007 ctx
->vp_saved
= ctx
->vp
;
1011 void cso_restore_viewport(struct cso_context
*ctx
)
1013 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1014 ctx
->vp
= ctx
->vp_saved
;
1015 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1022 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1023 const struct pipe_blend_color
*bc
)
1025 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1026 ctx
->blend_color
= *bc
;
1027 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
1032 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1035 if (ctx
->geometry_shader
!= handle
) {
1036 ctx
->geometry_shader
= handle
;
1037 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1042 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1044 if (handle
== ctx
->geometry_shader
) {
1045 /* unbind before deleting */
1046 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1047 ctx
->geometry_shader
= NULL
;
1049 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1052 void cso_save_geometry_shader(struct cso_context
*ctx
)
1054 assert(!ctx
->geometry_shader_saved
);
1055 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1058 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1060 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1061 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1062 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1064 ctx
->geometry_shader_saved
= NULL
;