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_math.h"
41 #include "util/u_memory.h"
42 #include "util/u_sampler.h"
43 #include "tgsi/tgsi_parse.h"
45 #include "cso_cache/cso_context.h"
46 #include "cso_cache/cso_cache.h"
47 #include "cso_cache/cso_hash.h"
48 #include "cso_context.h"
51 struct pipe_context
*pipe
;
52 struct cso_cache
*cache
;
55 void *samplers
[PIPE_MAX_SAMPLERS
];
58 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
59 unsigned nr_vertex_samplers
;
62 void *samplers
[PIPE_MAX_SAMPLERS
];
65 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
66 unsigned nr_vertex_samplers
;
68 unsigned nr_samplers_saved
;
69 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
71 unsigned nr_vertex_samplers_saved
;
72 void *vertex_samplers_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
74 uint nr_fragment_sampler_views
;
75 struct pipe_sampler_view
*fragment_sampler_views
[PIPE_MAX_SAMPLERS
];
77 uint nr_vertex_sampler_views
;
78 struct pipe_sampler_view
*vertex_sampler_views
[PIPE_MAX_VERTEX_SAMPLERS
];
80 uint nr_fragment_sampler_views_saved
;
81 struct pipe_sampler_view
*fragment_sampler_views_saved
[PIPE_MAX_SAMPLERS
];
83 uint nr_vertex_sampler_views_saved
;
84 struct pipe_sampler_view
*vertex_sampler_views_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
86 /** Current and saved state.
87 * The saved state is used as a 1-deep stack.
89 void *blend
, *blend_saved
;
90 void *depth_stencil
, *depth_stencil_saved
;
91 void *rasterizer
, *rasterizer_saved
;
92 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
93 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
94 void *velements
, *velements_saved
;
96 struct pipe_clip_state clip
;
97 struct pipe_clip_state clip_saved
;
99 struct pipe_framebuffer_state fb
, fb_saved
;
100 struct pipe_viewport_state vp
, vp_saved
;
101 struct pipe_blend_color blend_color
;
102 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
107 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
110 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
112 struct cso_blend
*cso
= (struct cso_blend
*)state
;
114 if (ctx
->blend
== cso
->data
)
117 if (cso
->delete_state
)
118 cso
->delete_state(cso
->context
, cso
->data
);
123 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
125 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
127 if (ctx
->depth_stencil
== cso
->data
)
130 if (cso
->delete_state
)
131 cso
->delete_state(cso
->context
, cso
->data
);
137 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
139 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
140 if (cso
->delete_state
)
141 cso
->delete_state(cso
->context
, cso
->data
);
146 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
148 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
150 if (ctx
->rasterizer
== cso
->data
)
152 if (cso
->delete_state
)
153 cso
->delete_state(cso
->context
, cso
->data
);
158 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
160 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
161 if (ctx
->fragment_shader
== cso
->data
)
163 if (cso
->delete_state
)
164 cso
->delete_state(cso
->context
, cso
->data
);
169 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
171 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
172 if (ctx
->vertex_shader
== cso
->data
)
174 if (cso
->delete_state
)
175 cso
->delete_state(cso
->context
, cso
->data
);
180 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
183 struct cso_velements
*cso
= (struct cso_velements
*)state
;
185 if (ctx
->velements
== cso
->data
)
188 if (cso
->delete_state
)
189 cso
->delete_state(cso
->context
, cso
->data
);
195 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
196 void *state
, enum cso_cache_type type
)
200 return delete_blend_state(ctx
, state
);
203 return delete_sampler_state(ctx
, state
);
205 case CSO_DEPTH_STENCIL_ALPHA
:
206 return delete_depth_stencil_state(ctx
, state
);
209 return delete_rasterizer_state(ctx
, state
);
211 case CSO_FRAGMENT_SHADER
:
212 return delete_fs_state(ctx
, state
);
214 case CSO_VERTEX_SHADER
:
215 return delete_vs_state(ctx
, state
);
218 return delete_vertex_elements(ctx
, state
);
227 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
228 int max_size
, void *user_data
)
230 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
231 /* if we're approach the maximum size, remove fourth of the entries
232 * otherwise every subsequent call will go through the same */
233 int hash_size
= cso_hash_size(hash
);
234 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
235 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
236 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
237 if (hash_size
> max_size
)
238 to_remove
+= hash_size
- max_size
;
240 /*remove elements until we're good */
241 /*fixme: currently we pick the nodes to remove at random*/
242 void *cso
= cso_hash_iter_data(iter
);
243 if (delete_cso(ctx
, cso
, type
)) {
244 iter
= cso_hash_erase(hash
, iter
);
247 iter
= cso_hash_iter_next(iter
);
252 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
254 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
258 ctx
->cache
= cso_cache_create();
259 if (ctx
->cache
== NULL
)
261 cso_cache_set_sanitize_callback(ctx
->cache
,
267 /* Enable for testing: */
268 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
273 cso_destroy_context( ctx
);
279 * Prior to context destruction, this function unbinds all state objects.
281 void cso_release_all( struct cso_context
*ctx
)
286 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
287 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
288 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
289 if (ctx
->pipe
->bind_vertex_sampler_states
)
290 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
291 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
292 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
293 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
294 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
297 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
298 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], NULL
);
299 pipe_sampler_view_reference(&ctx
->fragment_sampler_views_saved
[i
], NULL
);
302 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
303 pipe_sampler_view_reference(&ctx
->vertex_sampler_views
[i
], NULL
);
304 pipe_sampler_view_reference(&ctx
->vertex_sampler_views_saved
[i
], NULL
);
307 free_framebuffer_state(&ctx
->fb
);
308 free_framebuffer_state(&ctx
->fb_saved
);
311 cso_cache_delete( ctx
->cache
);
317 void cso_destroy_context( struct cso_context
*ctx
)
320 /*cso_release_all( ctx );*/
326 /* Those function will either find the state of the given template
327 * in the cache or they will create a new state from the given
328 * template, insert it in the cache and return it.
332 * If the driver returns 0 from the create method then they will assign
333 * the data member of the cso to be the template itself.
336 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
337 const struct pipe_blend_state
*templ
)
339 unsigned key_size
, hash_key
;
340 struct cso_hash_iter iter
;
343 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
344 (char *)&(templ
->rt
[1]) - (char *)templ
;
345 hash_key
= cso_construct_key((void*)templ
, key_size
);
346 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
348 if (cso_hash_iter_is_null(iter
)) {
349 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
351 return PIPE_ERROR_OUT_OF_MEMORY
;
353 memcpy(&cso
->state
, templ
, key_size
);
354 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
355 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
356 cso
->context
= ctx
->pipe
;
358 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
359 if (cso_hash_iter_is_null(iter
)) {
361 return PIPE_ERROR_OUT_OF_MEMORY
;
367 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
370 if (ctx
->blend
!= handle
) {
372 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
377 void cso_save_blend(struct cso_context
*ctx
)
379 assert(!ctx
->blend_saved
);
380 ctx
->blend_saved
= ctx
->blend
;
383 void cso_restore_blend(struct cso_context
*ctx
)
385 if (ctx
->blend
!= ctx
->blend_saved
) {
386 ctx
->blend
= ctx
->blend_saved
;
387 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
389 ctx
->blend_saved
= NULL
;
394 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
396 const struct pipe_sampler_state
*templ
)
401 unsigned key_size
= sizeof(struct pipe_sampler_state
);
402 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
403 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
404 hash_key
, CSO_SAMPLER
,
405 (void*)templ
, key_size
);
407 if (cso_hash_iter_is_null(iter
)) {
408 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
410 return PIPE_ERROR_OUT_OF_MEMORY
;
412 memcpy(&cso
->state
, templ
, sizeof(*templ
));
413 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
414 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
415 cso
->context
= ctx
->pipe
;
417 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
418 if (cso_hash_iter_is_null(iter
)) {
420 return PIPE_ERROR_OUT_OF_MEMORY
;
426 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
430 ctx
->samplers
[idx
] = handle
;
435 cso_single_vertex_sampler(struct cso_context
*ctx
,
437 const struct pipe_sampler_state
*templ
)
442 unsigned key_size
= sizeof(struct pipe_sampler_state
);
443 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
444 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
445 hash_key
, CSO_SAMPLER
,
446 (void*)templ
, key_size
);
448 if (cso_hash_iter_is_null(iter
)) {
449 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
451 return PIPE_ERROR_OUT_OF_MEMORY
;
453 memcpy(&cso
->state
, templ
, sizeof(*templ
));
454 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
455 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
456 cso
->context
= ctx
->pipe
;
458 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
459 if (cso_hash_iter_is_null(iter
)) {
461 return PIPE_ERROR_OUT_OF_MEMORY
;
467 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
471 ctx
->vertex_samplers
[idx
] = handle
;
475 void cso_single_sampler_done( struct cso_context
*ctx
)
479 /* find highest non-null sampler */
480 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
481 if (ctx
->samplers
[i
- 1] != NULL
)
485 ctx
->nr_samplers
= i
;
487 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
488 memcmp(ctx
->hw
.samplers
,
490 ctx
->nr_samplers
* sizeof(void *)) != 0)
492 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
493 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
495 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
500 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
504 /* find highest non-null sampler */
505 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
506 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
510 ctx
->nr_vertex_samplers
= i
;
512 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
513 memcmp(ctx
->hw
.vertex_samplers
,
514 ctx
->vertex_samplers
,
515 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
517 memcpy(ctx
->hw
.vertex_samplers
,
518 ctx
->vertex_samplers
,
519 ctx
->nr_vertex_samplers
* sizeof(void *));
520 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
522 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
523 ctx
->nr_vertex_samplers
,
524 ctx
->vertex_samplers
);
529 * If the function encouters any errors it will return the
530 * last one. Done to always try to set as many samplers
533 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
535 const struct pipe_sampler_state
**templates
)
538 enum pipe_error temp
, error
= PIPE_OK
;
543 for (i
= 0; i
< nr
; i
++) {
544 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
549 for ( ; i
< ctx
->nr_samplers
; i
++) {
550 temp
= cso_single_sampler( ctx
, i
, NULL
);
555 cso_single_sampler_done( ctx
);
560 void cso_save_samplers(struct cso_context
*ctx
)
562 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
563 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
566 void cso_restore_samplers(struct cso_context
*ctx
)
568 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
569 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
570 cso_single_sampler_done( ctx
);
574 * If the function encouters any errors it will return the
575 * last one. Done to always try to set as many samplers
578 enum pipe_error
cso_set_vertex_samplers(struct cso_context
*ctx
,
580 const struct pipe_sampler_state
**templates
)
583 enum pipe_error temp
, error
= PIPE_OK
;
588 for (i
= 0; i
< nr
; i
++) {
589 temp
= cso_single_vertex_sampler( ctx
, i
, templates
[i
] );
594 for ( ; i
< ctx
->nr_samplers
; i
++) {
595 temp
= cso_single_vertex_sampler( ctx
, i
, NULL
);
600 cso_single_vertex_sampler_done( ctx
);
606 cso_save_vertex_samplers(struct cso_context
*ctx
)
608 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
609 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
613 cso_restore_vertex_samplers(struct cso_context
*ctx
)
615 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
616 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
617 cso_single_vertex_sampler_done(ctx
);
621 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
622 const struct pipe_depth_stencil_alpha_state
*templ
)
624 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
625 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
626 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
628 CSO_DEPTH_STENCIL_ALPHA
,
629 (void*)templ
, key_size
);
632 if (cso_hash_iter_is_null(iter
)) {
633 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
635 return PIPE_ERROR_OUT_OF_MEMORY
;
637 memcpy(&cso
->state
, templ
, sizeof(*templ
));
638 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
639 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
640 cso
->context
= ctx
->pipe
;
642 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
643 if (cso_hash_iter_is_null(iter
)) {
645 return PIPE_ERROR_OUT_OF_MEMORY
;
651 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
654 if (ctx
->depth_stencil
!= handle
) {
655 ctx
->depth_stencil
= handle
;
656 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
661 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
663 assert(!ctx
->depth_stencil_saved
);
664 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
667 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
669 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
670 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
671 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
673 ctx
->depth_stencil_saved
= NULL
;
678 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
679 const struct pipe_rasterizer_state
*templ
)
681 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
682 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
683 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
684 hash_key
, CSO_RASTERIZER
,
685 (void*)templ
, key_size
);
688 if (cso_hash_iter_is_null(iter
)) {
689 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
691 return PIPE_ERROR_OUT_OF_MEMORY
;
693 memcpy(&cso
->state
, templ
, sizeof(*templ
));
694 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
695 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
696 cso
->context
= ctx
->pipe
;
698 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
699 if (cso_hash_iter_is_null(iter
)) {
701 return PIPE_ERROR_OUT_OF_MEMORY
;
707 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
710 if (ctx
->rasterizer
!= handle
) {
711 ctx
->rasterizer
= handle
;
712 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
717 void cso_save_rasterizer(struct cso_context
*ctx
)
719 assert(!ctx
->rasterizer_saved
);
720 ctx
->rasterizer_saved
= ctx
->rasterizer
;
723 void cso_restore_rasterizer(struct cso_context
*ctx
)
725 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
726 ctx
->rasterizer
= ctx
->rasterizer_saved
;
727 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
729 ctx
->rasterizer_saved
= NULL
;
734 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
737 if (ctx
->fragment_shader
!= handle
) {
738 ctx
->fragment_shader
= handle
;
739 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
744 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
746 if (handle
== ctx
->fragment_shader
) {
747 /* unbind before deleting */
748 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
749 ctx
->fragment_shader
= NULL
;
751 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
754 /* Not really working:
757 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
758 const struct pipe_shader_state
*templ
)
760 const struct tgsi_token
*tokens
= templ
->tokens
;
761 unsigned num_tokens
= tgsi_num_tokens(tokens
);
762 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
763 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
764 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
768 sizeof(*templ
)); /* XXX correct? tokens_size? */
771 if (cso_hash_iter_is_null(iter
)) {
772 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
773 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
776 return PIPE_ERROR_OUT_OF_MEMORY
;
778 memcpy(cso_tokens
, tokens
, tokens_size
);
779 cso
->state
.tokens
= cso_tokens
;
780 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
781 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
782 cso
->context
= ctx
->pipe
;
784 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
785 if (cso_hash_iter_is_null(iter
)) {
787 return PIPE_ERROR_OUT_OF_MEMORY
;
793 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
796 return cso_set_fragment_shader_handle( ctx
, handle
);
800 void cso_save_fragment_shader(struct cso_context
*ctx
)
802 assert(!ctx
->fragment_shader_saved
);
803 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
806 void cso_restore_fragment_shader(struct cso_context
*ctx
)
808 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
809 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
810 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
812 ctx
->fragment_shader_saved
= NULL
;
816 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
819 if (ctx
->vertex_shader
!= handle
) {
820 ctx
->vertex_shader
= handle
;
821 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
826 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
828 if (handle
== ctx
->vertex_shader
) {
829 /* unbind before deleting */
830 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
831 ctx
->vertex_shader
= NULL
;
833 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
837 /* Not really working:
840 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
841 const struct pipe_shader_state
*templ
)
843 unsigned hash_key
= cso_construct_key((void*)templ
,
844 sizeof(struct pipe_shader_state
));
845 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
846 hash_key
, CSO_VERTEX_SHADER
,
851 if (cso_hash_iter_is_null(iter
)) {
852 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
855 return PIPE_ERROR_OUT_OF_MEMORY
;
857 memcpy(cso
->state
, templ
, sizeof(*templ
));
858 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
859 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
860 cso
->context
= ctx
->pipe
;
862 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
863 if (cso_hash_iter_is_null(iter
)) {
865 return PIPE_ERROR_OUT_OF_MEMORY
;
871 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
874 return cso_set_vertex_shader_handle( ctx
, handle
);
880 void cso_save_vertex_shader(struct cso_context
*ctx
)
882 assert(!ctx
->vertex_shader_saved
);
883 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
886 void cso_restore_vertex_shader(struct cso_context
*ctx
)
888 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
889 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
890 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
892 ctx
->vertex_shader_saved
= NULL
;
897 * Copy framebuffer state from src to dst with refcounting of surfaces.
900 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
901 const struct pipe_framebuffer_state
*src
)
905 dst
->width
= src
->width
;
906 dst
->height
= src
->height
;
907 dst
->nr_cbufs
= src
->nr_cbufs
;
908 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
909 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
911 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
916 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
920 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
921 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
923 pipe_surface_reference(&fb
->zsbuf
, NULL
);
927 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
928 const struct pipe_framebuffer_state
*fb
)
930 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
931 copy_framebuffer_state(&ctx
->fb
, fb
);
932 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
937 void cso_save_framebuffer(struct cso_context
*ctx
)
939 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
942 void cso_restore_framebuffer(struct cso_context
*ctx
)
944 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
945 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
946 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
947 free_framebuffer_state(&ctx
->fb_saved
);
952 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
953 const struct pipe_viewport_state
*vp
)
955 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
957 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
962 void cso_save_viewport(struct cso_context
*ctx
)
964 ctx
->vp_saved
= ctx
->vp
;
968 void cso_restore_viewport(struct cso_context
*ctx
)
970 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
971 ctx
->vp
= ctx
->vp_saved
;
972 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
977 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
978 const struct pipe_blend_color
*bc
)
980 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
981 ctx
->blend_color
= *bc
;
982 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
987 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
988 const struct pipe_stencil_ref
*sr
)
990 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
991 ctx
->stencil_ref
= *sr
;
992 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
997 void cso_save_stencil_ref(struct cso_context
*ctx
)
999 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
1003 void cso_restore_stencil_ref(struct cso_context
*ctx
)
1005 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
1006 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
1007 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
1011 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1014 if (ctx
->geometry_shader
!= handle
) {
1015 ctx
->geometry_shader
= handle
;
1016 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1021 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1023 if (handle
== ctx
->geometry_shader
) {
1024 /* unbind before deleting */
1025 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1026 ctx
->geometry_shader
= NULL
;
1028 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1031 void cso_save_geometry_shader(struct cso_context
*ctx
)
1033 assert(!ctx
->geometry_shader_saved
);
1034 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1037 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1039 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1040 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1041 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1043 ctx
->geometry_shader_saved
= NULL
;
1049 clip_state_cpy(struct pipe_clip_state
*dst
,
1050 const struct pipe_clip_state
*src
)
1054 memcpy(dst
->ucp
, src
->ucp
, src
->nr
* sizeof(src
->ucp
[0]));
1059 clip_state_cmp(const struct pipe_clip_state
*a
,
1060 const struct pipe_clip_state
*b
)
1062 if (a
->nr
!= b
->nr
) {
1066 return memcmp(a
->ucp
, b
->ucp
, a
->nr
* sizeof(a
->ucp
[0]));
1072 cso_set_clip(struct cso_context
*ctx
,
1073 const struct pipe_clip_state
*clip
)
1075 if (clip_state_cmp(&ctx
->clip
, clip
)) {
1076 clip_state_cpy(&ctx
->clip
, clip
);
1077 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
1082 cso_save_clip(struct cso_context
*ctx
)
1084 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
1088 cso_restore_clip(struct cso_context
*ctx
)
1090 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
1091 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
1092 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
1096 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
1098 const struct pipe_vertex_element
*states
)
1100 unsigned key_size
, hash_key
;
1101 struct cso_hash_iter iter
;
1103 struct cso_velems_state velems_state
;
1105 /* need to include the count into the stored state data too.
1106 Otherwise first few count pipe_vertex_elements could be identical even if count
1107 is different, and there's no guarantee the hash would be different in that
1109 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
1110 velems_state
.count
= count
;
1111 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
1112 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
1113 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
1115 if (cso_hash_iter_is_null(iter
)) {
1116 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1118 return PIPE_ERROR_OUT_OF_MEMORY
;
1120 memcpy(&cso
->state
, &velems_state
, key_size
);
1121 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
1122 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
1123 cso
->context
= ctx
->pipe
;
1125 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1126 if (cso_hash_iter_is_null(iter
)) {
1128 return PIPE_ERROR_OUT_OF_MEMORY
;
1134 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1137 if (ctx
->velements
!= handle
) {
1138 ctx
->velements
= handle
;
1139 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1144 void cso_save_vertex_elements(struct cso_context
*ctx
)
1146 assert(!ctx
->velements_saved
);
1147 ctx
->velements_saved
= ctx
->velements
;
1150 void cso_restore_vertex_elements(struct cso_context
*ctx
)
1152 if (ctx
->velements
!= ctx
->velements_saved
) {
1153 ctx
->velements
= ctx
->velements_saved
;
1154 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1156 ctx
->velements_saved
= NULL
;
1159 /* fragment sampler view state */
1162 cso_set_fragment_sampler_views(struct cso_context
*cso
,
1164 struct pipe_sampler_view
**views
)
1168 for (i
= 0; i
< count
; i
++) {
1169 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], views
[i
]);
1171 for (; i
< cso
->nr_fragment_sampler_views
; i
++) {
1172 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], NULL
);
1175 cso
->pipe
->set_fragment_sampler_views(cso
->pipe
,
1176 MAX2(count
, cso
->nr_fragment_sampler_views
),
1177 cso
->fragment_sampler_views
);
1179 cso
->nr_fragment_sampler_views
= count
;
1183 cso_save_fragment_sampler_views(struct cso_context
*cso
)
1187 cso
->nr_fragment_sampler_views_saved
= cso
->nr_fragment_sampler_views
;
1189 for (i
= 0; i
< cso
->nr_fragment_sampler_views
; i
++) {
1190 assert(!cso
->fragment_sampler_views_saved
[i
]);
1192 pipe_sampler_view_reference(&cso
->fragment_sampler_views_saved
[i
],
1193 cso
->fragment_sampler_views
[i
]);
1198 cso_restore_fragment_sampler_views(struct cso_context
*cso
)
1202 for (i
= 0; i
< cso
->nr_fragment_sampler_views_saved
; i
++) {
1203 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], cso
->fragment_sampler_views_saved
[i
]);
1204 pipe_sampler_view_reference(&cso
->fragment_sampler_views_saved
[i
], NULL
);
1206 for (; i
< cso
->nr_fragment_sampler_views
; i
++) {
1207 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], NULL
);
1210 cso
->pipe
->set_fragment_sampler_views(cso
->pipe
,
1211 MAX2(cso
->nr_fragment_sampler_views
, cso
->nr_fragment_sampler_views_saved
),
1212 cso
->fragment_sampler_views
);
1214 cso
->nr_fragment_sampler_views
= cso
->nr_fragment_sampler_views_saved
;
1215 cso
->nr_fragment_sampler_views_saved
= 0;
1219 /* vertex sampler view state */
1222 cso_set_vertex_sampler_views(struct cso_context
*cso
,
1224 struct pipe_sampler_view
**views
)
1228 for (i
= 0; i
< count
; i
++) {
1229 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], views
[i
]);
1231 for (; i
< cso
->nr_vertex_sampler_views
; i
++) {
1232 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], NULL
);
1235 cso
->pipe
->set_vertex_sampler_views(cso
->pipe
,
1236 MAX2(count
, cso
->nr_vertex_sampler_views
),
1237 cso
->vertex_sampler_views
);
1239 cso
->nr_vertex_sampler_views
= count
;
1243 cso_save_vertex_sampler_views(struct cso_context
*cso
)
1247 cso
->nr_vertex_sampler_views_saved
= cso
->nr_vertex_sampler_views
;
1249 for (i
= 0; i
< cso
->nr_vertex_sampler_views
; i
++) {
1250 assert(!cso
->vertex_sampler_views_saved
[i
]);
1252 pipe_sampler_view_reference(&cso
->vertex_sampler_views_saved
[i
],
1253 cso
->vertex_sampler_views
[i
]);
1258 cso_restore_vertex_sampler_views(struct cso_context
*cso
)
1262 for (i
= 0; i
< cso
->nr_vertex_sampler_views_saved
; i
++) {
1263 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], cso
->vertex_sampler_views_saved
[i
]);
1264 pipe_sampler_view_reference(&cso
->vertex_sampler_views_saved
[i
], NULL
);
1266 for (; i
< cso
->nr_vertex_sampler_views
; i
++) {
1267 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], NULL
);
1270 cso
->pipe
->set_vertex_sampler_views(cso
->pipe
,
1271 MAX2(cso
->nr_vertex_sampler_views
, cso
->nr_vertex_sampler_views_saved
),
1272 cso
->vertex_sampler_views
);
1274 cso
->nr_vertex_sampler_views
= cso
->nr_vertex_sampler_views_saved
;
1275 cso
->nr_vertex_sampler_views_saved
= 0;