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 "util/u_sampler.h"
42 #include "tgsi/tgsi_parse.h"
44 #include "cso_cache/cso_context.h"
45 #include "cso_cache/cso_cache.h"
46 #include "cso_cache/cso_hash.h"
47 #include "cso_context.h"
50 struct pipe_context
*pipe
;
51 struct cso_cache
*cache
;
54 void *samplers
[PIPE_MAX_SAMPLERS
];
57 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
58 unsigned nr_vertex_samplers
;
61 void *samplers
[PIPE_MAX_SAMPLERS
];
64 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
65 unsigned nr_vertex_samplers
;
67 unsigned nr_samplers_saved
;
68 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
70 unsigned nr_vertex_samplers_saved
;
71 void *vertex_samplers_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
73 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
74 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
77 struct pipe_texture
*vertex_textures
[PIPE_MAX_VERTEX_SAMPLERS
];
78 struct pipe_sampler_view
*vertex_sampler_views
[PIPE_MAX_VERTEX_SAMPLERS
];
79 uint nr_vertex_textures
;
81 uint nr_textures_saved
;
82 struct pipe_texture
*textures_saved
[PIPE_MAX_SAMPLERS
];
83 struct pipe_sampler_view
*sampler_views_saved
[PIPE_MAX_SAMPLERS
];
85 uint nr_vertex_textures_saved
;
86 struct pipe_texture
*vertex_textures_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
87 struct pipe_sampler_view
*vertex_sampler_views_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
89 /** Current and saved state.
90 * The saved state is used as a 1-deep stack.
92 void *blend
, *blend_saved
;
93 void *depth_stencil
, *depth_stencil_saved
;
94 void *rasterizer
, *rasterizer_saved
;
95 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
96 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
97 void *velements
, *velements_saved
;
99 struct pipe_clip_state clip
;
100 struct pipe_clip_state clip_saved
;
102 struct pipe_framebuffer_state fb
, fb_saved
;
103 struct pipe_viewport_state vp
, vp_saved
;
104 struct pipe_blend_color blend_color
;
105 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
110 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
113 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
115 struct cso_blend
*cso
= (struct cso_blend
*)state
;
117 if (ctx
->blend
== cso
->data
)
120 if (cso
->delete_state
)
121 cso
->delete_state(cso
->context
, cso
->data
);
126 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
128 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
130 if (ctx
->depth_stencil
== cso
->data
)
133 if (cso
->delete_state
)
134 cso
->delete_state(cso
->context
, cso
->data
);
140 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
142 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
143 if (cso
->delete_state
)
144 cso
->delete_state(cso
->context
, cso
->data
);
149 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
151 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
153 if (ctx
->rasterizer
== cso
->data
)
155 if (cso
->delete_state
)
156 cso
->delete_state(cso
->context
, cso
->data
);
161 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
163 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
164 if (ctx
->fragment_shader
== cso
->data
)
166 if (cso
->delete_state
)
167 cso
->delete_state(cso
->context
, cso
->data
);
172 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
174 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
175 if (ctx
->vertex_shader
== cso
->data
)
177 if (cso
->delete_state
)
178 cso
->delete_state(cso
->context
, cso
->data
);
183 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
186 struct cso_velements
*cso
= (struct cso_velements
*)state
;
188 if (ctx
->velements
== cso
->data
)
191 if (cso
->delete_state
)
192 cso
->delete_state(cso
->context
, cso
->data
);
198 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
199 void *state
, enum cso_cache_type type
)
203 return delete_blend_state(ctx
, state
);
206 return delete_sampler_state(ctx
, state
);
208 case CSO_DEPTH_STENCIL_ALPHA
:
209 return delete_depth_stencil_state(ctx
, state
);
212 return delete_rasterizer_state(ctx
, state
);
214 case CSO_FRAGMENT_SHADER
:
215 return delete_fs_state(ctx
, state
);
217 case CSO_VERTEX_SHADER
:
218 return delete_vs_state(ctx
, state
);
221 return delete_vertex_elements(ctx
, state
);
230 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
231 int max_size
, void *user_data
)
233 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
234 /* if we're approach the maximum size, remove fourth of the entries
235 * otherwise every subsequent call will go through the same */
236 int hash_size
= cso_hash_size(hash
);
237 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
238 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
239 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
240 if (hash_size
> max_size
)
241 to_remove
+= hash_size
- max_size
;
243 /*remove elements until we're good */
244 /*fixme: currently we pick the nodes to remove at random*/
245 void *cso
= cso_hash_iter_data(iter
);
246 if (delete_cso(ctx
, cso
, type
)) {
247 iter
= cso_hash_erase(hash
, iter
);
250 iter
= cso_hash_iter_next(iter
);
255 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
257 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
261 ctx
->cache
= cso_cache_create();
262 if (ctx
->cache
== NULL
)
264 cso_cache_set_sanitize_callback(ctx
->cache
,
270 /* Enable for testing: */
271 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
276 cso_destroy_context( ctx
);
282 * Prior to context destruction, this function unbinds all state objects.
284 void cso_release_all( struct cso_context
*ctx
)
289 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
290 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
291 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
292 if (ctx
->pipe
->bind_vertex_sampler_states
)
293 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
294 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
295 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
296 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
297 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
300 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
301 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
302 pipe_texture_reference(&ctx
->textures_saved
[i
], NULL
);
303 pipe_sampler_view_reference(&ctx
->sampler_views
[i
], NULL
);
304 pipe_sampler_view_reference(&ctx
->sampler_views_saved
[i
], NULL
);
307 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
308 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
309 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], NULL
);
310 pipe_sampler_view_reference(&ctx
->vertex_sampler_views
[i
], NULL
);
311 pipe_sampler_view_reference(&ctx
->vertex_sampler_views_saved
[i
], NULL
);
314 free_framebuffer_state(&ctx
->fb
);
315 free_framebuffer_state(&ctx
->fb_saved
);
318 cso_cache_delete( ctx
->cache
);
324 void cso_destroy_context( struct cso_context
*ctx
)
327 /*cso_release_all( ctx );*/
333 /* Those function will either find the state of the given template
334 * in the cache or they will create a new state from the given
335 * template, insert it in the cache and return it.
339 * If the driver returns 0 from the create method then they will assign
340 * the data member of the cso to be the template itself.
343 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
344 const struct pipe_blend_state
*templ
)
346 unsigned key_size
, hash_key
;
347 struct cso_hash_iter iter
;
350 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
351 (char *)&(templ
->rt
[1]) - (char *)templ
;
352 hash_key
= cso_construct_key((void*)templ
, key_size
);
353 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
355 if (cso_hash_iter_is_null(iter
)) {
356 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
358 return PIPE_ERROR_OUT_OF_MEMORY
;
360 memcpy(&cso
->state
, templ
, key_size
);
361 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
362 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
363 cso
->context
= ctx
->pipe
;
365 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
366 if (cso_hash_iter_is_null(iter
)) {
368 return PIPE_ERROR_OUT_OF_MEMORY
;
374 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
377 if (ctx
->blend
!= handle
) {
379 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
384 void cso_save_blend(struct cso_context
*ctx
)
386 assert(!ctx
->blend_saved
);
387 ctx
->blend_saved
= ctx
->blend
;
390 void cso_restore_blend(struct cso_context
*ctx
)
392 if (ctx
->blend
!= ctx
->blend_saved
) {
393 ctx
->blend
= ctx
->blend_saved
;
394 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
396 ctx
->blend_saved
= NULL
;
401 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
403 const struct pipe_sampler_state
*templ
)
408 unsigned key_size
= sizeof(struct pipe_sampler_state
);
409 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
410 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
411 hash_key
, CSO_SAMPLER
,
412 (void*)templ
, key_size
);
414 if (cso_hash_iter_is_null(iter
)) {
415 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
417 return PIPE_ERROR_OUT_OF_MEMORY
;
419 memcpy(&cso
->state
, templ
, sizeof(*templ
));
420 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
421 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
422 cso
->context
= ctx
->pipe
;
424 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
425 if (cso_hash_iter_is_null(iter
)) {
427 return PIPE_ERROR_OUT_OF_MEMORY
;
433 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
437 ctx
->samplers
[idx
] = handle
;
442 cso_single_vertex_sampler(struct cso_context
*ctx
,
444 const struct pipe_sampler_state
*templ
)
449 unsigned key_size
= sizeof(struct pipe_sampler_state
);
450 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
451 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
452 hash_key
, CSO_SAMPLER
,
453 (void*)templ
, key_size
);
455 if (cso_hash_iter_is_null(iter
)) {
456 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
458 return PIPE_ERROR_OUT_OF_MEMORY
;
460 memcpy(&cso
->state
, templ
, sizeof(*templ
));
461 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
462 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
463 cso
->context
= ctx
->pipe
;
465 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
466 if (cso_hash_iter_is_null(iter
)) {
468 return PIPE_ERROR_OUT_OF_MEMORY
;
474 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
478 ctx
->vertex_samplers
[idx
] = handle
;
482 void cso_single_sampler_done( struct cso_context
*ctx
)
486 /* find highest non-null sampler */
487 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
488 if (ctx
->samplers
[i
- 1] != NULL
)
492 ctx
->nr_samplers
= i
;
494 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
495 memcmp(ctx
->hw
.samplers
,
497 ctx
->nr_samplers
* sizeof(void *)) != 0)
499 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
500 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
502 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
507 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
511 /* find highest non-null sampler */
512 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
513 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
517 ctx
->nr_vertex_samplers
= i
;
519 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
520 memcmp(ctx
->hw
.vertex_samplers
,
521 ctx
->vertex_samplers
,
522 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
524 memcpy(ctx
->hw
.vertex_samplers
,
525 ctx
->vertex_samplers
,
526 ctx
->nr_vertex_samplers
* sizeof(void *));
527 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
529 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
530 ctx
->nr_vertex_samplers
,
531 ctx
->vertex_samplers
);
536 * If the function encouters any errors it will return the
537 * last one. Done to always try to set as many samplers
540 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
542 const struct pipe_sampler_state
**templates
)
545 enum pipe_error temp
, error
= PIPE_OK
;
550 for (i
= 0; i
< nr
; i
++) {
551 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
556 for ( ; i
< ctx
->nr_samplers
; i
++) {
557 temp
= cso_single_sampler( ctx
, i
, NULL
);
562 cso_single_sampler_done( ctx
);
567 void cso_save_samplers(struct cso_context
*ctx
)
569 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
570 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
573 void cso_restore_samplers(struct cso_context
*ctx
)
575 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
576 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
577 cso_single_sampler_done( ctx
);
581 * If the function encouters any errors it will return the
582 * last one. Done to always try to set as many samplers
585 enum pipe_error
cso_set_vertex_samplers(struct cso_context
*ctx
,
587 const struct pipe_sampler_state
**templates
)
590 enum pipe_error temp
, error
= PIPE_OK
;
595 for (i
= 0; i
< nr
; i
++) {
596 temp
= cso_single_vertex_sampler( ctx
, i
, templates
[i
] );
601 for ( ; i
< ctx
->nr_samplers
; i
++) {
602 temp
= cso_single_vertex_sampler( ctx
, i
, NULL
);
607 cso_single_vertex_sampler_done( ctx
);
613 cso_save_vertex_samplers(struct cso_context
*ctx
)
615 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
616 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
620 cso_restore_vertex_samplers(struct cso_context
*ctx
)
622 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
623 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
624 cso_single_vertex_sampler_done(ctx
);
628 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
630 struct pipe_texture
**textures
)
634 ctx
->nr_textures
= count
;
636 for (i
= 0; i
< count
; i
++) {
637 struct pipe_sampler_view templ
, *view
;
639 u_sampler_view_default_template(&templ
,
641 textures
[i
]->format
);
642 view
= ctx
->pipe
->create_sampler_view(ctx
->pipe
,
646 pipe_texture_reference(&ctx
->textures
[i
], textures
[i
]);
647 pipe_sampler_view_reference(&ctx
->sampler_views
[i
], view
);
649 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++) {
650 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
651 pipe_sampler_view_reference(&ctx
->sampler_views
[i
], NULL
);
654 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
,
661 void cso_save_sampler_textures( struct cso_context
*ctx
)
665 ctx
->nr_textures_saved
= ctx
->nr_textures
;
666 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
667 assert(!ctx
->textures_saved
[i
]);
668 assert(!ctx
->sampler_views_saved
[i
]);
670 pipe_texture_reference(&ctx
->textures_saved
[i
], ctx
->textures
[i
]);
671 pipe_sampler_view_reference(&ctx
->sampler_views_saved
[i
],
672 ctx
->sampler_views
[i
]);
676 void cso_restore_sampler_textures( struct cso_context
*ctx
)
680 ctx
->nr_textures
= ctx
->nr_textures_saved
;
682 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
683 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
684 ctx
->textures
[i
] = ctx
->textures_saved
[i
];
685 ctx
->textures_saved
[i
] = NULL
;
687 pipe_sampler_view_reference(&ctx
->sampler_views
[i
], NULL
);
688 ctx
->sampler_views
[i
] = ctx
->sampler_views_saved
[i
];
689 ctx
->sampler_views_saved
[i
] = NULL
;
691 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++) {
692 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
693 pipe_sampler_view_reference(&ctx
->sampler_views
[i
], NULL
);
696 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
,
700 ctx
->nr_textures_saved
= 0;
706 cso_set_vertex_sampler_textures(struct cso_context
*ctx
,
708 struct pipe_texture
**textures
)
712 ctx
->nr_vertex_textures
= count
;
714 for (i
= 0; i
< count
; i
++) {
715 struct pipe_sampler_view templ
, *view
;
717 u_sampler_view_default_template(&templ
,
719 textures
[i
]->format
);
720 view
= ctx
->pipe
->create_sampler_view(ctx
->pipe
,
724 pipe_texture_reference(&ctx
->vertex_textures
[i
], textures
[i
]);
725 pipe_sampler_view_reference(&ctx
->vertex_sampler_views
[i
], view
);
727 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
728 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
729 pipe_sampler_view_reference(&ctx
->vertex_sampler_views
[i
], NULL
);
732 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
,
734 ctx
->vertex_sampler_views
);
740 cso_save_vertex_sampler_textures(struct cso_context
*ctx
)
744 ctx
->nr_vertex_textures_saved
= ctx
->nr_vertex_textures
;
745 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
746 assert(!ctx
->vertex_textures_saved
[i
]);
747 assert(!ctx
->vertex_sampler_views_saved
[i
]);
749 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], ctx
->vertex_textures
[i
]);
750 pipe_sampler_view_reference(&ctx
->vertex_sampler_views_saved
[i
],
751 ctx
->vertex_sampler_views
[i
]);
756 cso_restore_vertex_sampler_textures(struct cso_context
*ctx
)
760 ctx
->nr_vertex_textures
= ctx
->nr_vertex_textures_saved
;
762 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
763 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
764 ctx
->vertex_textures
[i
] = ctx
->vertex_textures_saved
[i
];
765 ctx
->vertex_textures_saved
[i
] = NULL
;
767 pipe_sampler_view_reference(&ctx
->vertex_sampler_views
[i
], NULL
);
768 ctx
->vertex_sampler_views
[i
] = ctx
->vertex_sampler_views_saved
[i
];
769 ctx
->vertex_sampler_views_saved
[i
] = NULL
;
771 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
772 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
773 pipe_sampler_view_reference(&ctx
->vertex_sampler_views
[i
], NULL
);
776 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
,
777 ctx
->nr_vertex_textures
,
778 ctx
->vertex_sampler_views
);
780 ctx
->nr_vertex_textures_saved
= 0;
785 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
786 const struct pipe_depth_stencil_alpha_state
*templ
)
788 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
789 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
790 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
792 CSO_DEPTH_STENCIL_ALPHA
,
793 (void*)templ
, key_size
);
796 if (cso_hash_iter_is_null(iter
)) {
797 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
799 return PIPE_ERROR_OUT_OF_MEMORY
;
801 memcpy(&cso
->state
, templ
, sizeof(*templ
));
802 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
803 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
804 cso
->context
= ctx
->pipe
;
806 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
807 if (cso_hash_iter_is_null(iter
)) {
809 return PIPE_ERROR_OUT_OF_MEMORY
;
815 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
818 if (ctx
->depth_stencil
!= handle
) {
819 ctx
->depth_stencil
= handle
;
820 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
825 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
827 assert(!ctx
->depth_stencil_saved
);
828 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
831 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
833 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
834 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
835 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
837 ctx
->depth_stencil_saved
= NULL
;
842 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
843 const struct pipe_rasterizer_state
*templ
)
845 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
846 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
847 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
848 hash_key
, CSO_RASTERIZER
,
849 (void*)templ
, key_size
);
852 if (cso_hash_iter_is_null(iter
)) {
853 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
855 return PIPE_ERROR_OUT_OF_MEMORY
;
857 memcpy(&cso
->state
, templ
, sizeof(*templ
));
858 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
859 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
860 cso
->context
= ctx
->pipe
;
862 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
863 if (cso_hash_iter_is_null(iter
)) {
865 return PIPE_ERROR_OUT_OF_MEMORY
;
871 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
874 if (ctx
->rasterizer
!= handle
) {
875 ctx
->rasterizer
= handle
;
876 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
881 void cso_save_rasterizer(struct cso_context
*ctx
)
883 assert(!ctx
->rasterizer_saved
);
884 ctx
->rasterizer_saved
= ctx
->rasterizer
;
887 void cso_restore_rasterizer(struct cso_context
*ctx
)
889 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
890 ctx
->rasterizer
= ctx
->rasterizer_saved
;
891 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
893 ctx
->rasterizer_saved
= NULL
;
898 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
901 if (ctx
->fragment_shader
!= handle
) {
902 ctx
->fragment_shader
= handle
;
903 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
908 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
910 if (handle
== ctx
->fragment_shader
) {
911 /* unbind before deleting */
912 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
913 ctx
->fragment_shader
= NULL
;
915 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
918 /* Not really working:
921 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
922 const struct pipe_shader_state
*templ
)
924 const struct tgsi_token
*tokens
= templ
->tokens
;
925 unsigned num_tokens
= tgsi_num_tokens(tokens
);
926 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
927 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
928 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
932 sizeof(*templ
)); /* XXX correct? tokens_size? */
935 if (cso_hash_iter_is_null(iter
)) {
936 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
937 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
940 return PIPE_ERROR_OUT_OF_MEMORY
;
942 memcpy(cso_tokens
, tokens
, tokens_size
);
943 cso
->state
.tokens
= cso_tokens
;
944 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
945 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
946 cso
->context
= ctx
->pipe
;
948 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
949 if (cso_hash_iter_is_null(iter
)) {
951 return PIPE_ERROR_OUT_OF_MEMORY
;
957 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
960 return cso_set_fragment_shader_handle( ctx
, handle
);
964 void cso_save_fragment_shader(struct cso_context
*ctx
)
966 assert(!ctx
->fragment_shader_saved
);
967 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
970 void cso_restore_fragment_shader(struct cso_context
*ctx
)
972 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
973 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
974 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
976 ctx
->fragment_shader_saved
= NULL
;
980 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
983 if (ctx
->vertex_shader
!= handle
) {
984 ctx
->vertex_shader
= handle
;
985 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
990 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
992 if (handle
== ctx
->vertex_shader
) {
993 /* unbind before deleting */
994 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
995 ctx
->vertex_shader
= NULL
;
997 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
1001 /* Not really working:
1004 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
1005 const struct pipe_shader_state
*templ
)
1007 unsigned hash_key
= cso_construct_key((void*)templ
,
1008 sizeof(struct pipe_shader_state
));
1009 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
1010 hash_key
, CSO_VERTEX_SHADER
,
1013 void *handle
= NULL
;
1015 if (cso_hash_iter_is_null(iter
)) {
1016 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
1019 return PIPE_ERROR_OUT_OF_MEMORY
;
1021 memcpy(cso
->state
, templ
, sizeof(*templ
));
1022 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
1023 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
1024 cso
->context
= ctx
->pipe
;
1026 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
1027 if (cso_hash_iter_is_null(iter
)) {
1029 return PIPE_ERROR_OUT_OF_MEMORY
;
1035 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
1038 return cso_set_vertex_shader_handle( ctx
, handle
);
1044 void cso_save_vertex_shader(struct cso_context
*ctx
)
1046 assert(!ctx
->vertex_shader_saved
);
1047 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
1050 void cso_restore_vertex_shader(struct cso_context
*ctx
)
1052 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
1053 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
1054 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
1056 ctx
->vertex_shader_saved
= NULL
;
1061 * Copy framebuffer state from src to dst with refcounting of surfaces.
1064 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
1065 const struct pipe_framebuffer_state
*src
)
1069 dst
->width
= src
->width
;
1070 dst
->height
= src
->height
;
1071 dst
->nr_cbufs
= src
->nr_cbufs
;
1072 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1073 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
1075 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
1080 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
1084 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1085 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
1087 pipe_surface_reference(&fb
->zsbuf
, NULL
);
1091 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
1092 const struct pipe_framebuffer_state
*fb
)
1094 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
1095 copy_framebuffer_state(&ctx
->fb
, fb
);
1096 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
1101 void cso_save_framebuffer(struct cso_context
*ctx
)
1103 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
1106 void cso_restore_framebuffer(struct cso_context
*ctx
)
1108 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
1109 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
1110 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
1111 free_framebuffer_state(&ctx
->fb_saved
);
1116 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
1117 const struct pipe_viewport_state
*vp
)
1119 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
1121 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1126 void cso_save_viewport(struct cso_context
*ctx
)
1128 ctx
->vp_saved
= ctx
->vp
;
1132 void cso_restore_viewport(struct cso_context
*ctx
)
1134 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1135 ctx
->vp
= ctx
->vp_saved
;
1136 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1141 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1142 const struct pipe_blend_color
*bc
)
1144 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1145 ctx
->blend_color
= *bc
;
1146 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
1151 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
1152 const struct pipe_stencil_ref
*sr
)
1154 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
1155 ctx
->stencil_ref
= *sr
;
1156 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
1161 void cso_save_stencil_ref(struct cso_context
*ctx
)
1163 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
1167 void cso_restore_stencil_ref(struct cso_context
*ctx
)
1169 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
1170 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
1171 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
1175 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1178 if (ctx
->geometry_shader
!= handle
) {
1179 ctx
->geometry_shader
= handle
;
1180 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1185 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1187 if (handle
== ctx
->geometry_shader
) {
1188 /* unbind before deleting */
1189 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1190 ctx
->geometry_shader
= NULL
;
1192 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1195 void cso_save_geometry_shader(struct cso_context
*ctx
)
1197 assert(!ctx
->geometry_shader_saved
);
1198 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1201 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1203 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1204 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1205 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1207 ctx
->geometry_shader_saved
= NULL
;
1213 clip_state_cpy(struct pipe_clip_state
*dst
,
1214 const struct pipe_clip_state
*src
)
1218 memcpy(dst
->ucp
, src
->ucp
, src
->nr
* sizeof(src
->ucp
[0]));
1223 clip_state_cmp(const struct pipe_clip_state
*a
,
1224 const struct pipe_clip_state
*b
)
1226 if (a
->nr
!= b
->nr
) {
1230 return memcmp(a
->ucp
, b
->ucp
, a
->nr
* sizeof(a
->ucp
[0]));
1236 cso_set_clip(struct cso_context
*ctx
,
1237 const struct pipe_clip_state
*clip
)
1239 if (clip_state_cmp(&ctx
->clip
, clip
)) {
1240 clip_state_cpy(&ctx
->clip
, clip
);
1241 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
1246 cso_save_clip(struct cso_context
*ctx
)
1248 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
1252 cso_restore_clip(struct cso_context
*ctx
)
1254 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
1255 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
1256 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
1260 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
1262 const struct pipe_vertex_element
*states
)
1264 unsigned key_size
, hash_key
;
1265 struct cso_hash_iter iter
;
1267 struct cso_velems_state velems_state
;
1269 /* need to include the count into the stored state data too.
1270 Otherwise first few count pipe_vertex_elements could be identical even if count
1271 is different, and there's no guarantee the hash would be different in that
1273 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
1274 velems_state
.count
= count
;
1275 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
1276 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
1277 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
1279 if (cso_hash_iter_is_null(iter
)) {
1280 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1282 return PIPE_ERROR_OUT_OF_MEMORY
;
1284 memcpy(&cso
->state
, &velems_state
, key_size
);
1285 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
1286 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
1287 cso
->context
= ctx
->pipe
;
1289 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1290 if (cso_hash_iter_is_null(iter
)) {
1292 return PIPE_ERROR_OUT_OF_MEMORY
;
1298 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1301 if (ctx
->velements
!= handle
) {
1302 ctx
->velements
= handle
;
1303 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1308 void cso_save_vertex_elements(struct cso_context
*ctx
)
1310 assert(!ctx
->velements_saved
);
1311 ctx
->velements_saved
= ctx
->velements
;
1314 void cso_restore_vertex_elements(struct cso_context
*ctx
)
1316 if (ctx
->velements
!= ctx
->velements_saved
) {
1317 ctx
->velements
= ctx
->velements_saved
;
1318 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1320 ctx
->velements_saved
= NULL
;