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_draw.h"
40 #include "util/u_framebuffer.h"
41 #include "util/u_inlines.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.h"
44 #include "util/u_vbuf.h"
45 #include "tgsi/tgsi_parse.h"
47 #include "cso_cache/cso_context.h"
48 #include "cso_cache/cso_cache.h"
49 #include "cso_cache/cso_hash.h"
50 #include "cso_context.h"
54 * Info related to samplers and sampler views.
55 * We have one of these for fragment samplers and another for vertex samplers.
60 void *samplers
[PIPE_MAX_SAMPLERS
];
64 void *samplers
[PIPE_MAX_SAMPLERS
];
67 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
68 unsigned nr_samplers_saved
;
70 struct pipe_sampler_view
*views
[PIPE_MAX_SAMPLERS
];
73 struct pipe_sampler_view
*views_saved
[PIPE_MAX_SAMPLERS
];
74 unsigned nr_views_saved
;
80 struct pipe_context
*pipe
;
81 struct cso_cache
*cache
;
84 boolean has_geometry_shader
;
85 boolean has_streamout
;
87 struct sampler_info fragment_samplers
;
88 struct sampler_info vertex_samplers
;
90 uint nr_vertex_buffers
;
91 struct pipe_vertex_buffer vertex_buffers
[PIPE_MAX_ATTRIBS
];
93 uint nr_vertex_buffers_saved
;
94 struct pipe_vertex_buffer vertex_buffers_saved
[PIPE_MAX_ATTRIBS
];
96 unsigned nr_so_targets
;
97 struct pipe_stream_output_target
*so_targets
[PIPE_MAX_SO_BUFFERS
];
99 unsigned nr_so_targets_saved
;
100 struct pipe_stream_output_target
*so_targets_saved
[PIPE_MAX_SO_BUFFERS
];
102 /** Current and saved state.
103 * The saved state is used as a 1-deep stack.
105 void *blend
, *blend_saved
;
106 void *depth_stencil
, *depth_stencil_saved
;
107 void *rasterizer
, *rasterizer_saved
;
108 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
109 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
110 void *velements
, *velements_saved
;
112 struct pipe_clip_state clip
;
113 struct pipe_clip_state clip_saved
;
115 struct pipe_framebuffer_state fb
, fb_saved
;
116 struct pipe_viewport_state vp
, vp_saved
;
117 struct pipe_blend_color blend_color
;
118 unsigned sample_mask
;
119 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
123 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
125 struct cso_blend
*cso
= (struct cso_blend
*)state
;
127 if (ctx
->blend
== cso
->data
)
130 if (cso
->delete_state
)
131 cso
->delete_state(cso
->context
, cso
->data
);
136 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
138 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
140 if (ctx
->depth_stencil
== cso
->data
)
143 if (cso
->delete_state
)
144 cso
->delete_state(cso
->context
, cso
->data
);
150 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
152 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
153 if (cso
->delete_state
)
154 cso
->delete_state(cso
->context
, cso
->data
);
159 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
161 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
163 if (ctx
->rasterizer
== cso
->data
)
165 if (cso
->delete_state
)
166 cso
->delete_state(cso
->context
, cso
->data
);
171 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
174 struct cso_velements
*cso
= (struct cso_velements
*)state
;
176 if (ctx
->velements
== cso
->data
)
179 if (cso
->delete_state
)
180 cso
->delete_state(cso
->context
, cso
->data
);
186 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
187 void *state
, enum cso_cache_type type
)
191 return delete_blend_state(ctx
, state
);
194 return delete_sampler_state(ctx
, state
);
196 case CSO_DEPTH_STENCIL_ALPHA
:
197 return delete_depth_stencil_state(ctx
, state
);
200 return delete_rasterizer_state(ctx
, state
);
203 return delete_vertex_elements(ctx
, state
);
212 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
213 int max_size
, void *user_data
)
215 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
216 /* if we're approach the maximum size, remove fourth of the entries
217 * otherwise every subsequent call will go through the same */
218 int hash_size
= cso_hash_size(hash
);
219 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
220 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
221 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
222 if (hash_size
> max_size
)
223 to_remove
+= hash_size
- max_size
;
225 /*remove elements until we're good */
226 /*fixme: currently we pick the nodes to remove at random*/
227 void *cso
= cso_hash_iter_data(iter
);
228 if (delete_cso(ctx
, cso
, type
)) {
229 iter
= cso_hash_erase(hash
, iter
);
232 iter
= cso_hash_iter_next(iter
);
236 static void cso_init_vbuf(struct cso_context
*cso
)
238 struct u_vbuf_caps caps
;
240 u_vbuf_get_caps(cso
->pipe
->screen
, &caps
);
242 /* Install u_vbuf if there is anything unsupported. */
243 if (!caps
.buffer_offset_unaligned
||
244 !caps
.buffer_stride_unaligned
||
245 !caps
.velem_src_offset_unaligned
||
246 !caps
.format_fixed32
||
247 !caps
.format_float16
||
248 !caps
.format_float64
||
249 !caps
.format_norm32
||
250 !caps
.format_scaled32
||
251 !caps
.user_vertex_buffers
) {
252 cso
->vbuf
= u_vbuf_create(cso
->pipe
, &caps
);
256 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
258 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
262 assert(PIPE_MAX_SAMPLERS
== PIPE_MAX_VERTEX_SAMPLERS
);
264 ctx
->cache
= cso_cache_create();
265 if (ctx
->cache
== NULL
)
267 cso_cache_set_sanitize_callback(ctx
->cache
,
275 /* Enable for testing: */
276 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
278 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
279 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
280 ctx
->has_geometry_shader
= TRUE
;
282 if (pipe
->screen
->get_param(pipe
->screen
,
283 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
284 ctx
->has_streamout
= TRUE
;
290 cso_destroy_context( ctx
);
295 * Prior to context destruction, this function unbinds all state objects.
297 void cso_release_all( struct cso_context
*ctx
)
300 struct sampler_info
*info
;
303 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
304 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
305 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
306 if (ctx
->pipe
->bind_vertex_sampler_states
)
307 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
308 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
309 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
310 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
311 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
312 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, 0, NULL
);
313 if (ctx
->pipe
->set_vertex_sampler_views
)
314 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, 0, NULL
);
315 if (ctx
->pipe
->set_stream_output_targets
)
316 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, 0);
319 /* free fragment samplers, views */
320 info
= &ctx
->fragment_samplers
;
321 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
322 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
323 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
326 /* free vertex samplers, views */
327 info
= &ctx
->vertex_samplers
;
328 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
329 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
330 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
333 util_unreference_framebuffer_state(&ctx
->fb
);
334 util_unreference_framebuffer_state(&ctx
->fb_saved
);
336 util_copy_vertex_buffers(ctx
->vertex_buffers
,
337 &ctx
->nr_vertex_buffers
,
339 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
340 &ctx
->nr_vertex_buffers_saved
,
343 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
344 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
345 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
349 cso_cache_delete( ctx
->cache
);
356 * Free the CSO context. NOTE: the state tracker should have previously called
359 void cso_destroy_context( struct cso_context
*ctx
)
363 u_vbuf_destroy(ctx
->vbuf
);
369 /* Those function will either find the state of the given template
370 * in the cache or they will create a new state from the given
371 * template, insert it in the cache and return it.
375 * If the driver returns 0 from the create method then they will assign
376 * the data member of the cso to be the template itself.
379 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
380 const struct pipe_blend_state
*templ
)
382 unsigned key_size
, hash_key
;
383 struct cso_hash_iter iter
;
386 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
387 (char *)&(templ
->rt
[1]) - (char *)templ
;
388 hash_key
= cso_construct_key((void*)templ
, key_size
);
389 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
391 if (cso_hash_iter_is_null(iter
)) {
392 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
394 return PIPE_ERROR_OUT_OF_MEMORY
;
396 memset(&cso
->state
, 0, sizeof cso
->state
);
397 memcpy(&cso
->state
, templ
, key_size
);
398 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
399 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
400 cso
->context
= ctx
->pipe
;
402 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
403 if (cso_hash_iter_is_null(iter
)) {
405 return PIPE_ERROR_OUT_OF_MEMORY
;
411 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
414 if (ctx
->blend
!= handle
) {
416 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
421 void cso_save_blend(struct cso_context
*ctx
)
423 assert(!ctx
->blend_saved
);
424 ctx
->blend_saved
= ctx
->blend
;
427 void cso_restore_blend(struct cso_context
*ctx
)
429 if (ctx
->blend
!= ctx
->blend_saved
) {
430 ctx
->blend
= ctx
->blend_saved
;
431 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
433 ctx
->blend_saved
= NULL
;
438 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
439 const struct pipe_depth_stencil_alpha_state
*templ
)
441 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
442 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
443 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
445 CSO_DEPTH_STENCIL_ALPHA
,
446 (void*)templ
, key_size
);
449 if (cso_hash_iter_is_null(iter
)) {
450 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
452 return PIPE_ERROR_OUT_OF_MEMORY
;
454 memcpy(&cso
->state
, templ
, sizeof(*templ
));
455 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
456 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
457 cso
->context
= ctx
->pipe
;
459 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
460 if (cso_hash_iter_is_null(iter
)) {
462 return PIPE_ERROR_OUT_OF_MEMORY
;
468 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
471 if (ctx
->depth_stencil
!= handle
) {
472 ctx
->depth_stencil
= handle
;
473 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
478 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
480 assert(!ctx
->depth_stencil_saved
);
481 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
484 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
486 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
487 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
488 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
490 ctx
->depth_stencil_saved
= NULL
;
495 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
496 const struct pipe_rasterizer_state
*templ
)
498 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
499 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
500 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
501 hash_key
, CSO_RASTERIZER
,
502 (void*)templ
, key_size
);
505 if (cso_hash_iter_is_null(iter
)) {
506 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
508 return PIPE_ERROR_OUT_OF_MEMORY
;
510 memcpy(&cso
->state
, templ
, sizeof(*templ
));
511 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
512 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
513 cso
->context
= ctx
->pipe
;
515 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
516 if (cso_hash_iter_is_null(iter
)) {
518 return PIPE_ERROR_OUT_OF_MEMORY
;
524 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
527 if (ctx
->rasterizer
!= handle
) {
528 ctx
->rasterizer
= handle
;
529 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
534 void cso_save_rasterizer(struct cso_context
*ctx
)
536 assert(!ctx
->rasterizer_saved
);
537 ctx
->rasterizer_saved
= ctx
->rasterizer
;
540 void cso_restore_rasterizer(struct cso_context
*ctx
)
542 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
543 ctx
->rasterizer
= ctx
->rasterizer_saved
;
544 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
546 ctx
->rasterizer_saved
= NULL
;
551 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
554 if (ctx
->fragment_shader
!= handle
) {
555 ctx
->fragment_shader
= handle
;
556 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
561 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
563 if (handle
== ctx
->fragment_shader
) {
564 /* unbind before deleting */
565 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
566 ctx
->fragment_shader
= NULL
;
568 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
571 void cso_save_fragment_shader(struct cso_context
*ctx
)
573 assert(!ctx
->fragment_shader_saved
);
574 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
577 void cso_restore_fragment_shader(struct cso_context
*ctx
)
579 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
580 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
581 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
583 ctx
->fragment_shader_saved
= NULL
;
587 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
590 if (ctx
->vertex_shader
!= handle
) {
591 ctx
->vertex_shader
= handle
;
592 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
597 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
599 if (handle
== ctx
->vertex_shader
) {
600 /* unbind before deleting */
601 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
602 ctx
->vertex_shader
= NULL
;
604 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
607 void cso_save_vertex_shader(struct cso_context
*ctx
)
609 assert(!ctx
->vertex_shader_saved
);
610 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
613 void cso_restore_vertex_shader(struct cso_context
*ctx
)
615 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
616 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
617 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
619 ctx
->vertex_shader_saved
= NULL
;
623 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
624 const struct pipe_framebuffer_state
*fb
)
626 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
627 util_copy_framebuffer_state(&ctx
->fb
, fb
);
628 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
633 void cso_save_framebuffer(struct cso_context
*ctx
)
635 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
638 void cso_restore_framebuffer(struct cso_context
*ctx
)
640 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
641 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
642 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
643 util_unreference_framebuffer_state(&ctx
->fb_saved
);
648 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
649 const struct pipe_viewport_state
*vp
)
651 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
653 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
658 void cso_save_viewport(struct cso_context
*ctx
)
660 ctx
->vp_saved
= ctx
->vp
;
664 void cso_restore_viewport(struct cso_context
*ctx
)
666 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
667 ctx
->vp
= ctx
->vp_saved
;
668 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
673 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
674 const struct pipe_blend_color
*bc
)
676 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
677 ctx
->blend_color
= *bc
;
678 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
683 enum pipe_error
cso_set_sample_mask(struct cso_context
*ctx
,
684 unsigned sample_mask
)
686 if (ctx
->sample_mask
!= sample_mask
) {
687 ctx
->sample_mask
= sample_mask
;
688 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
693 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
694 const struct pipe_stencil_ref
*sr
)
696 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
697 ctx
->stencil_ref
= *sr
;
698 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
703 void cso_save_stencil_ref(struct cso_context
*ctx
)
705 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
709 void cso_restore_stencil_ref(struct cso_context
*ctx
)
711 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
712 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
713 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
717 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
720 assert(ctx
->has_geometry_shader
|| !handle
);
722 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
723 ctx
->geometry_shader
= handle
;
724 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
729 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
731 if (handle
== ctx
->geometry_shader
) {
732 /* unbind before deleting */
733 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
734 ctx
->geometry_shader
= NULL
;
736 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
739 void cso_save_geometry_shader(struct cso_context
*ctx
)
741 if (!ctx
->has_geometry_shader
) {
745 assert(!ctx
->geometry_shader_saved
);
746 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
749 void cso_restore_geometry_shader(struct cso_context
*ctx
)
751 if (!ctx
->has_geometry_shader
) {
755 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
756 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
757 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
759 ctx
->geometry_shader_saved
= NULL
;
765 clip_state_cpy(struct pipe_clip_state
*dst
,
766 const struct pipe_clip_state
*src
)
768 memcpy(dst
->ucp
, src
->ucp
, sizeof(dst
->ucp
));
772 clip_state_cmp(const struct pipe_clip_state
*a
,
773 const struct pipe_clip_state
*b
)
775 return memcmp(a
->ucp
, b
->ucp
, sizeof(a
->ucp
));
779 cso_set_clip(struct cso_context
*ctx
,
780 const struct pipe_clip_state
*clip
)
782 if (clip_state_cmp(&ctx
->clip
, clip
)) {
783 clip_state_cpy(&ctx
->clip
, clip
);
784 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
789 cso_save_clip(struct cso_context
*ctx
)
791 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
795 cso_restore_clip(struct cso_context
*ctx
)
797 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
798 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
799 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
803 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
805 const struct pipe_vertex_element
*states
)
807 struct u_vbuf
*vbuf
= ctx
->vbuf
;
808 unsigned key_size
, hash_key
;
809 struct cso_hash_iter iter
;
811 struct cso_velems_state velems_state
;
814 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
818 /* need to include the count into the stored state data too.
819 Otherwise first few count pipe_vertex_elements could be identical even if count
820 is different, and there's no guarantee the hash would be different in that
822 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
823 velems_state
.count
= count
;
824 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
825 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
826 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
828 if (cso_hash_iter_is_null(iter
)) {
829 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
831 return PIPE_ERROR_OUT_OF_MEMORY
;
833 memcpy(&cso
->state
, &velems_state
, key_size
);
834 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
835 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
836 cso
->context
= ctx
->pipe
;
838 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
839 if (cso_hash_iter_is_null(iter
)) {
841 return PIPE_ERROR_OUT_OF_MEMORY
;
847 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
850 if (ctx
->velements
!= handle
) {
851 ctx
->velements
= handle
;
852 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
857 void cso_save_vertex_elements(struct cso_context
*ctx
)
859 struct u_vbuf
*vbuf
= ctx
->vbuf
;
862 u_vbuf_save_vertex_elements(vbuf
);
866 assert(!ctx
->velements_saved
);
867 ctx
->velements_saved
= ctx
->velements
;
870 void cso_restore_vertex_elements(struct cso_context
*ctx
)
872 struct u_vbuf
*vbuf
= ctx
->vbuf
;
875 u_vbuf_restore_vertex_elements(vbuf
);
879 if (ctx
->velements
!= ctx
->velements_saved
) {
880 ctx
->velements
= ctx
->velements_saved
;
881 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
883 ctx
->velements_saved
= NULL
;
888 void cso_set_vertex_buffers(struct cso_context
*ctx
,
890 const struct pipe_vertex_buffer
*buffers
)
892 struct u_vbuf
*vbuf
= ctx
->vbuf
;
895 u_vbuf_set_vertex_buffers(vbuf
, count
, buffers
);
899 if (count
!= ctx
->nr_vertex_buffers
||
900 memcmp(buffers
, ctx
->vertex_buffers
,
901 sizeof(struct pipe_vertex_buffer
) * count
) != 0) {
902 util_copy_vertex_buffers(ctx
->vertex_buffers
, &ctx
->nr_vertex_buffers
,
904 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, count
, buffers
);
908 void cso_save_vertex_buffers(struct cso_context
*ctx
)
910 struct u_vbuf
*vbuf
= ctx
->vbuf
;
913 u_vbuf_save_vertex_buffers(vbuf
);
917 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
918 &ctx
->nr_vertex_buffers_saved
,
920 ctx
->nr_vertex_buffers
);
923 void cso_restore_vertex_buffers(struct cso_context
*ctx
)
926 struct u_vbuf
*vbuf
= ctx
->vbuf
;
929 u_vbuf_restore_vertex_buffers(vbuf
);
933 util_copy_vertex_buffers(ctx
->vertex_buffers
,
934 &ctx
->nr_vertex_buffers
,
935 ctx
->vertex_buffers_saved
,
936 ctx
->nr_vertex_buffers_saved
);
938 for (i
= 0; i
< ctx
->nr_vertex_buffers_saved
; i
++) {
939 pipe_resource_reference(&ctx
->vertex_buffers_saved
[i
].buffer
, NULL
);
941 ctx
->nr_vertex_buffers_saved
= 0;
943 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, ctx
->nr_vertex_buffers
,
944 ctx
->vertex_buffers
);
948 /**************** fragment/vertex sampler view state *************************/
950 static enum pipe_error
951 single_sampler(struct cso_context
*ctx
,
952 struct sampler_info
*info
,
954 const struct pipe_sampler_state
*templ
)
959 unsigned key_size
= sizeof(struct pipe_sampler_state
);
960 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
961 struct cso_hash_iter iter
=
962 cso_find_state_template(ctx
->cache
,
963 hash_key
, CSO_SAMPLER
,
964 (void *) templ
, key_size
);
966 if (cso_hash_iter_is_null(iter
)) {
967 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
969 return PIPE_ERROR_OUT_OF_MEMORY
;
971 memcpy(&cso
->state
, templ
, sizeof(*templ
));
972 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
973 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
974 cso
->context
= ctx
->pipe
;
976 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
977 if (cso_hash_iter_is_null(iter
)) {
979 return PIPE_ERROR_OUT_OF_MEMORY
;
985 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
989 info
->samplers
[idx
] = handle
;
995 cso_single_sampler(struct cso_context
*ctx
,
997 const struct pipe_sampler_state
*templ
)
999 return single_sampler(ctx
, &ctx
->fragment_samplers
, idx
, templ
);
1003 cso_single_vertex_sampler(struct cso_context
*ctx
,
1005 const struct pipe_sampler_state
*templ
)
1007 return single_sampler(ctx
, &ctx
->vertex_samplers
, idx
, templ
);
1013 single_sampler_done(struct cso_context
*ctx
,
1014 struct sampler_info
*info
)
1018 /* find highest non-null sampler */
1019 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1020 if (info
->samplers
[i
- 1] != NULL
)
1024 info
->nr_samplers
= i
;
1026 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1027 memcmp(info
->hw
.samplers
,
1029 info
->nr_samplers
* sizeof(void *)) != 0)
1031 memcpy(info
->hw
.samplers
,
1033 info
->nr_samplers
* sizeof(void *));
1034 info
->hw
.nr_samplers
= info
->nr_samplers
;
1036 if (info
== &ctx
->fragment_samplers
) {
1037 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
,
1041 else if (info
== &ctx
->vertex_samplers
) {
1042 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
1053 cso_single_sampler_done( struct cso_context
*ctx
)
1055 single_sampler_done(ctx
, &ctx
->fragment_samplers
);
1059 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
1061 single_sampler_done(ctx
, &ctx
->vertex_samplers
);
1066 * If the function encouters any errors it will return the
1067 * last one. Done to always try to set as many samplers
1070 static enum pipe_error
1071 set_samplers(struct cso_context
*ctx
,
1072 struct sampler_info
*info
,
1074 const struct pipe_sampler_state
**templates
)
1077 enum pipe_error temp
, error
= PIPE_OK
;
1082 for (i
= 0; i
< nr
; i
++) {
1083 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1084 if (temp
!= PIPE_OK
)
1088 for ( ; i
< info
->nr_samplers
; i
++) {
1089 temp
= single_sampler(ctx
, info
, i
, NULL
);
1090 if (temp
!= PIPE_OK
)
1094 single_sampler_done(ctx
, info
);
1100 cso_set_samplers(struct cso_context
*ctx
,
1102 const struct pipe_sampler_state
**templates
)
1104 return set_samplers(ctx
, &ctx
->fragment_samplers
, nr
, templates
);
1108 cso_set_vertex_samplers(struct cso_context
*ctx
,
1110 const struct pipe_sampler_state
**templates
)
1112 return set_samplers(ctx
, &ctx
->vertex_samplers
, nr
, templates
);
1118 save_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1120 info
->nr_samplers_saved
= info
->nr_samplers
;
1121 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1125 cso_save_samplers(struct cso_context
*ctx
)
1127 save_samplers(ctx
, &ctx
->fragment_samplers
);
1131 cso_save_vertex_samplers(struct cso_context
*ctx
)
1133 save_samplers(ctx
, &ctx
->vertex_samplers
);
1139 restore_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1141 info
->nr_samplers
= info
->nr_samplers_saved
;
1142 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1143 single_sampler_done(ctx
, info
);
1147 cso_restore_samplers(struct cso_context
*ctx
)
1149 restore_samplers(ctx
, &ctx
->fragment_samplers
);
1153 cso_restore_vertex_samplers(struct cso_context
*ctx
)
1155 restore_samplers(ctx
, &ctx
->vertex_samplers
);
1161 set_sampler_views(struct cso_context
*ctx
,
1162 struct sampler_info
*info
,
1163 void (*set_views
)(struct pipe_context
*,
1165 struct pipe_sampler_view
**),
1167 struct pipe_sampler_view
**views
)
1171 /* reference new views */
1172 for (i
= 0; i
< count
; i
++) {
1173 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1175 /* unref extra old views, if any */
1176 for (; i
< info
->nr_views
; i
++) {
1177 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1180 info
->nr_views
= count
;
1182 /* bind the new sampler views */
1183 set_views(ctx
->pipe
, count
, info
->views
);
1187 cso_set_fragment_sampler_views(struct cso_context
*ctx
,
1189 struct pipe_sampler_view
**views
)
1191 set_sampler_views(ctx
, &ctx
->fragment_samplers
,
1192 ctx
->pipe
->set_fragment_sampler_views
,
1197 cso_set_vertex_sampler_views(struct cso_context
*ctx
,
1199 struct pipe_sampler_view
**views
)
1201 set_sampler_views(ctx
, &ctx
->vertex_samplers
,
1202 ctx
->pipe
->set_vertex_sampler_views
,
1209 save_sampler_views(struct cso_context
*ctx
,
1210 struct sampler_info
*info
)
1214 info
->nr_views_saved
= info
->nr_views
;
1216 for (i
= 0; i
< info
->nr_views
; i
++) {
1217 assert(!info
->views_saved
[i
]);
1218 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1223 cso_save_fragment_sampler_views(struct cso_context
*ctx
)
1225 save_sampler_views(ctx
, &ctx
->fragment_samplers
);
1229 cso_save_vertex_sampler_views(struct cso_context
*ctx
)
1231 save_sampler_views(ctx
, &ctx
->vertex_samplers
);
1236 restore_sampler_views(struct cso_context
*ctx
,
1237 struct sampler_info
*info
,
1238 void (*set_views
)(struct pipe_context
*,
1240 struct pipe_sampler_view
**))
1244 for (i
= 0; i
< info
->nr_views_saved
; i
++) {
1245 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1246 /* move the reference from one pointer to another */
1247 info
->views
[i
] = info
->views_saved
[i
];
1248 info
->views_saved
[i
] = NULL
;
1250 for (; i
< info
->nr_views
; i
++) {
1251 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1254 /* bind the old/saved sampler views */
1255 set_views(ctx
->pipe
, info
->nr_views_saved
, info
->views
);
1257 info
->nr_views
= info
->nr_views_saved
;
1258 info
->nr_views_saved
= 0;
1262 cso_restore_fragment_sampler_views(struct cso_context
*ctx
)
1264 restore_sampler_views(ctx
, &ctx
->fragment_samplers
,
1265 ctx
->pipe
->set_fragment_sampler_views
);
1269 cso_restore_vertex_sampler_views(struct cso_context
*ctx
)
1271 restore_sampler_views(ctx
, &ctx
->vertex_samplers
,
1272 ctx
->pipe
->set_vertex_sampler_views
);
1277 cso_set_stream_outputs(struct cso_context
*ctx
,
1278 unsigned num_targets
,
1279 struct pipe_stream_output_target
**targets
,
1280 unsigned append_bitmask
)
1282 struct pipe_context
*pipe
= ctx
->pipe
;
1285 if (!ctx
->has_streamout
) {
1286 assert(num_targets
== 0);
1290 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1291 /* Nothing to do. */
1295 /* reference new targets */
1296 for (i
= 0; i
< num_targets
; i
++) {
1297 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1299 /* unref extra old targets, if any */
1300 for (; i
< ctx
->nr_so_targets
; i
++) {
1301 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1304 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1306 ctx
->nr_so_targets
= num_targets
;
1310 cso_save_stream_outputs(struct cso_context
*ctx
)
1314 if (!ctx
->has_streamout
) {
1318 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1320 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1321 assert(!ctx
->so_targets_saved
[i
]);
1322 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1327 cso_restore_stream_outputs(struct cso_context
*ctx
)
1329 struct pipe_context
*pipe
= ctx
->pipe
;
1332 if (!ctx
->has_streamout
) {
1336 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1337 /* Nothing to do. */
1341 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1342 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1343 /* move the reference from one pointer to another */
1344 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1345 ctx
->so_targets_saved
[i
] = NULL
;
1347 for (; i
< ctx
->nr_so_targets
; i
++) {
1348 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1351 /* ~0 means append */
1352 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1353 ctx
->so_targets
, ~0);
1355 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1356 ctx
->nr_so_targets_saved
= 0;
1362 cso_set_index_buffer(struct cso_context
*cso
,
1363 const struct pipe_index_buffer
*ib
)
1365 struct u_vbuf
*vbuf
= cso
->vbuf
;
1368 u_vbuf_set_index_buffer(vbuf
, ib
);
1370 struct pipe_context
*pipe
= cso
->pipe
;
1371 pipe
->set_index_buffer(pipe
, ib
);
1376 cso_draw_vbo(struct cso_context
*cso
,
1377 const struct pipe_draw_info
*info
)
1379 struct u_vbuf
*vbuf
= cso
->vbuf
;
1382 u_vbuf_draw_vbo(vbuf
, info
);
1384 struct pipe_context
*pipe
= cso
->pipe
;
1385 pipe
->draw_vbo(pipe
, info
);
1390 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1392 struct pipe_draw_info info
;
1394 util_draw_init_info(&info
);
1399 info
.min_index
= start
;
1400 info
.max_index
= start
+ count
- 1;
1402 cso_draw_vbo(cso
, &info
);