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 samplers
[PIPE_SHADER_TYPES
];
89 struct pipe_vertex_buffer aux_vertex_buffer_current
;
90 struct pipe_vertex_buffer aux_vertex_buffer_saved
;
91 unsigned aux_vertex_buffer_index
;
93 struct pipe_constant_buffer aux_constbuf_current
[PIPE_SHADER_TYPES
];
94 struct pipe_constant_buffer aux_constbuf_saved
[PIPE_SHADER_TYPES
];
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
;
109 void *vertex_shader
, *vertex_shader_saved
;
110 void *geometry_shader
, *geometry_shader_saved
;
111 void *velements
, *velements_saved
;
112 struct pipe_query
*render_condition
, *render_condition_saved
;
113 uint render_condition_mode
, render_condition_mode_saved
;
114 boolean render_condition_cond
, render_condition_cond_saved
;
116 struct pipe_clip_state clip
;
117 struct pipe_clip_state clip_saved
;
119 struct pipe_framebuffer_state fb
, fb_saved
;
120 struct pipe_viewport_state vp
, vp_saved
;
121 struct pipe_blend_color blend_color
;
122 unsigned sample_mask
, sample_mask_saved
;
123 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
127 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
129 struct cso_blend
*cso
= (struct cso_blend
*)state
;
131 if (ctx
->blend
== cso
->data
)
134 if (cso
->delete_state
)
135 cso
->delete_state(cso
->context
, cso
->data
);
140 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
142 struct cso_depth_stencil_alpha
*cso
=
143 (struct cso_depth_stencil_alpha
*)state
;
145 if (ctx
->depth_stencil
== cso
->data
)
148 if (cso
->delete_state
)
149 cso
->delete_state(cso
->context
, cso
->data
);
155 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
157 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
158 if (cso
->delete_state
)
159 cso
->delete_state(cso
->context
, cso
->data
);
164 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
166 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
168 if (ctx
->rasterizer
== cso
->data
)
170 if (cso
->delete_state
)
171 cso
->delete_state(cso
->context
, cso
->data
);
176 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
179 struct cso_velements
*cso
= (struct cso_velements
*)state
;
181 if (ctx
->velements
== cso
->data
)
184 if (cso
->delete_state
)
185 cso
->delete_state(cso
->context
, cso
->data
);
191 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
192 void *state
, enum cso_cache_type type
)
196 return delete_blend_state(ctx
, state
);
198 return delete_sampler_state(ctx
, state
);
199 case CSO_DEPTH_STENCIL_ALPHA
:
200 return delete_depth_stencil_state(ctx
, state
);
202 return delete_rasterizer_state(ctx
, state
);
204 return delete_vertex_elements(ctx
, state
);
213 sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
214 int max_size
, void *user_data
)
216 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
217 /* if we're approach the maximum size, remove fourth of the entries
218 * otherwise every subsequent call will go through the same */
219 int hash_size
= cso_hash_size(hash
);
220 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
221 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
222 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
223 if (hash_size
> max_size
)
224 to_remove
+= hash_size
- max_size
;
226 /*remove elements until we're good */
227 /*fixme: currently we pick the nodes to remove at random*/
228 void *cso
= cso_hash_iter_data(iter
);
229 if (delete_cso(ctx
, cso
, type
)) {
230 iter
= cso_hash_erase(hash
, iter
);
233 iter
= cso_hash_iter_next(iter
);
237 static void cso_init_vbuf(struct cso_context
*cso
)
239 struct u_vbuf_caps caps
;
241 u_vbuf_get_caps(cso
->pipe
->screen
, &caps
);
243 /* Install u_vbuf if there is anything unsupported. */
244 if (!caps
.buffer_offset_unaligned
||
245 !caps
.buffer_stride_unaligned
||
246 !caps
.velem_src_offset_unaligned
||
247 !caps
.format_fixed32
||
248 !caps
.format_float16
||
249 !caps
.format_float64
||
250 !caps
.format_norm32
||
251 !caps
.format_scaled32
||
252 !caps
.user_vertex_buffers
) {
253 cso
->vbuf
= u_vbuf_create(cso
->pipe
, &caps
,
254 cso
->aux_vertex_buffer_index
);
258 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
260 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
264 ctx
->cache
= cso_cache_create();
265 if (ctx
->cache
== NULL
)
267 cso_cache_set_sanitize_callback(ctx
->cache
,
272 ctx
->sample_mask_saved
= ~0;
274 ctx
->aux_vertex_buffer_index
= 0; /* 0 for now */
278 /* Enable for testing: */
279 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
281 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
282 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
283 ctx
->has_geometry_shader
= TRUE
;
285 if (pipe
->screen
->get_param(pipe
->screen
,
286 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
287 ctx
->has_streamout
= TRUE
;
293 cso_destroy_context( ctx
);
298 * Prior to context destruction, this function unbinds all state objects.
300 void cso_release_all( struct cso_context
*ctx
)
305 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
306 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
307 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
308 if (ctx
->pipe
->bind_vertex_sampler_states
)
309 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
310 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
311 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
312 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
313 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
314 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, 0, NULL
);
315 if (ctx
->pipe
->set_vertex_sampler_views
)
316 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, 0, NULL
);
317 if (ctx
->pipe
->set_stream_output_targets
)
318 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, 0);
321 /* free fragment samplers, views */
322 for (shader
= 0; shader
< Elements(ctx
->samplers
); shader
++) {
323 struct sampler_info
*info
= &ctx
->samplers
[shader
];
324 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
325 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
326 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
330 util_unreference_framebuffer_state(&ctx
->fb
);
331 util_unreference_framebuffer_state(&ctx
->fb_saved
);
333 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
, NULL
);
334 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
, NULL
);
336 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
337 pipe_resource_reference(&ctx
->aux_constbuf_current
[i
].buffer
, NULL
);
338 pipe_resource_reference(&ctx
->aux_constbuf_saved
[i
].buffer
, NULL
);
341 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
342 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
343 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
347 cso_cache_delete( ctx
->cache
);
354 * Free the CSO context. NOTE: the state tracker should have previously called
357 void cso_destroy_context( struct cso_context
*ctx
)
361 u_vbuf_destroy(ctx
->vbuf
);
367 /* Those function will either find the state of the given template
368 * in the cache or they will create a new state from the given
369 * template, insert it in the cache and return it.
373 * If the driver returns 0 from the create method then they will assign
374 * the data member of the cso to be the template itself.
377 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
378 const struct pipe_blend_state
*templ
)
380 unsigned key_size
, hash_key
;
381 struct cso_hash_iter iter
;
384 key_size
= templ
->independent_blend_enable
?
385 sizeof(struct pipe_blend_state
) :
386 (char *)&(templ
->rt
[1]) - (char *)templ
;
387 hash_key
= cso_construct_key((void*)templ
, key_size
);
388 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
,
389 (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
;
439 cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
440 const struct pipe_depth_stencil_alpha_state
*templ
)
442 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
443 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
444 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
446 CSO_DEPTH_STENCIL_ALPHA
,
447 (void*)templ
, key_size
);
450 if (cso_hash_iter_is_null(iter
)) {
451 struct cso_depth_stencil_alpha
*cso
=
452 MALLOC(sizeof(struct cso_depth_stencil_alpha
));
454 return PIPE_ERROR_OUT_OF_MEMORY
;
456 memcpy(&cso
->state
, templ
, sizeof(*templ
));
457 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
,
460 (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
461 cso
->context
= ctx
->pipe
;
463 iter
= cso_insert_state(ctx
->cache
, hash_key
,
464 CSO_DEPTH_STENCIL_ALPHA
, cso
);
465 if (cso_hash_iter_is_null(iter
)) {
467 return PIPE_ERROR_OUT_OF_MEMORY
;
473 handle
= ((struct cso_depth_stencil_alpha
*)
474 cso_hash_iter_data(iter
))->data
;
477 if (ctx
->depth_stencil
!= handle
) {
478 ctx
->depth_stencil
= handle
;
479 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
484 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
486 assert(!ctx
->depth_stencil_saved
);
487 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
490 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
492 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
493 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
494 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
,
495 ctx
->depth_stencil_saved
);
497 ctx
->depth_stencil_saved
= NULL
;
502 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
503 const struct pipe_rasterizer_state
*templ
)
505 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
506 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
507 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
510 (void*)templ
, key_size
);
513 if (cso_hash_iter_is_null(iter
)) {
514 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
516 return PIPE_ERROR_OUT_OF_MEMORY
;
518 memcpy(&cso
->state
, templ
, sizeof(*templ
));
519 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
521 (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
522 cso
->context
= ctx
->pipe
;
524 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
525 if (cso_hash_iter_is_null(iter
)) {
527 return PIPE_ERROR_OUT_OF_MEMORY
;
533 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
536 if (ctx
->rasterizer
!= handle
) {
537 ctx
->rasterizer
= handle
;
538 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
543 void cso_save_rasterizer(struct cso_context
*ctx
)
545 assert(!ctx
->rasterizer_saved
);
546 ctx
->rasterizer_saved
= ctx
->rasterizer
;
549 void cso_restore_rasterizer(struct cso_context
*ctx
)
551 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
552 ctx
->rasterizer
= ctx
->rasterizer_saved
;
553 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
555 ctx
->rasterizer_saved
= NULL
;
559 void cso_set_fragment_shader_handle(struct cso_context
*ctx
, void *handle
)
561 if (ctx
->fragment_shader
!= handle
) {
562 ctx
->fragment_shader
= handle
;
563 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
567 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
569 if (handle
== ctx
->fragment_shader
) {
570 /* unbind before deleting */
571 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
572 ctx
->fragment_shader
= NULL
;
574 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
577 void cso_save_fragment_shader(struct cso_context
*ctx
)
579 assert(!ctx
->fragment_shader_saved
);
580 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
583 void cso_restore_fragment_shader(struct cso_context
*ctx
)
585 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
586 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
587 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
589 ctx
->fragment_shader_saved
= NULL
;
593 void cso_set_vertex_shader_handle(struct cso_context
*ctx
, void *handle
)
595 if (ctx
->vertex_shader
!= handle
) {
596 ctx
->vertex_shader
= handle
;
597 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
601 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
603 if (handle
== ctx
->vertex_shader
) {
604 /* unbind before deleting */
605 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
606 ctx
->vertex_shader
= NULL
;
608 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
611 void cso_save_vertex_shader(struct cso_context
*ctx
)
613 assert(!ctx
->vertex_shader_saved
);
614 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
617 void cso_restore_vertex_shader(struct cso_context
*ctx
)
619 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
620 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
621 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
623 ctx
->vertex_shader_saved
= NULL
;
627 void cso_set_framebuffer(struct cso_context
*ctx
,
628 const struct pipe_framebuffer_state
*fb
)
630 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
631 util_copy_framebuffer_state(&ctx
->fb
, fb
);
632 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
636 void cso_save_framebuffer(struct cso_context
*ctx
)
638 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
641 void cso_restore_framebuffer(struct cso_context
*ctx
)
643 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
644 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
645 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
646 util_unreference_framebuffer_state(&ctx
->fb_saved
);
651 void cso_set_viewport(struct cso_context
*ctx
,
652 const struct pipe_viewport_state
*vp
)
654 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
656 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, vp
);
660 void cso_save_viewport(struct cso_context
*ctx
)
662 ctx
->vp_saved
= ctx
->vp
;
666 void cso_restore_viewport(struct cso_context
*ctx
)
668 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
669 ctx
->vp
= ctx
->vp_saved
;
670 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, &ctx
->vp
);
675 void cso_set_blend_color(struct cso_context
*ctx
,
676 const struct pipe_blend_color
*bc
)
678 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
679 ctx
->blend_color
= *bc
;
680 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
684 void cso_set_sample_mask(struct cso_context
*ctx
, 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
);
692 void cso_save_sample_mask(struct cso_context
*ctx
)
694 ctx
->sample_mask_saved
= ctx
->sample_mask
;
697 void cso_restore_sample_mask(struct cso_context
*ctx
)
699 cso_set_sample_mask(ctx
, ctx
->sample_mask_saved
);
702 void cso_set_stencil_ref(struct cso_context
*ctx
,
703 const struct pipe_stencil_ref
*sr
)
705 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
706 ctx
->stencil_ref
= *sr
;
707 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
711 void cso_save_stencil_ref(struct cso_context
*ctx
)
713 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
717 void cso_restore_stencil_ref(struct cso_context
*ctx
)
719 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
,
720 sizeof(ctx
->stencil_ref
))) {
721 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
722 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
726 void cso_set_render_condition(struct cso_context
*ctx
,
727 struct pipe_query
*query
,
728 boolean condition
, uint mode
)
730 struct pipe_context
*pipe
= ctx
->pipe
;
732 if (ctx
->render_condition
!= query
||
733 ctx
->render_condition_mode
!= mode
||
734 ctx
->render_condition_cond
!= condition
) {
735 pipe
->render_condition(pipe
, query
, condition
, mode
);
736 ctx
->render_condition
= query
;
737 ctx
->render_condition_cond
= condition
;
738 ctx
->render_condition_mode
= mode
;
742 void cso_save_render_condition(struct cso_context
*ctx
)
744 ctx
->render_condition_saved
= ctx
->render_condition
;
745 ctx
->render_condition_cond_saved
= ctx
->render_condition_cond
;
746 ctx
->render_condition_mode_saved
= ctx
->render_condition_mode
;
749 void cso_restore_render_condition(struct cso_context
*ctx
)
751 cso_set_render_condition(ctx
, ctx
->render_condition_saved
,
752 ctx
->render_condition_cond_saved
,
753 ctx
->render_condition_mode_saved
);
756 void cso_set_geometry_shader_handle(struct cso_context
*ctx
, void *handle
)
758 assert(ctx
->has_geometry_shader
|| !handle
);
760 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
761 ctx
->geometry_shader
= handle
;
762 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
766 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
768 if (handle
== ctx
->geometry_shader
) {
769 /* unbind before deleting */
770 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
771 ctx
->geometry_shader
= NULL
;
773 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
776 void cso_save_geometry_shader(struct cso_context
*ctx
)
778 if (!ctx
->has_geometry_shader
) {
782 assert(!ctx
->geometry_shader_saved
);
783 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
786 void cso_restore_geometry_shader(struct cso_context
*ctx
)
788 if (!ctx
->has_geometry_shader
) {
792 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
793 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
794 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
796 ctx
->geometry_shader_saved
= NULL
;
802 clip_state_cpy(struct pipe_clip_state
*dst
,
803 const struct pipe_clip_state
*src
)
805 memcpy(dst
->ucp
, src
->ucp
, sizeof(dst
->ucp
));
809 clip_state_cmp(const struct pipe_clip_state
*a
,
810 const struct pipe_clip_state
*b
)
812 return memcmp(a
->ucp
, b
->ucp
, sizeof(a
->ucp
));
816 cso_set_clip(struct cso_context
*ctx
,
817 const struct pipe_clip_state
*clip
)
819 if (clip_state_cmp(&ctx
->clip
, clip
)) {
820 clip_state_cpy(&ctx
->clip
, clip
);
821 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
826 cso_save_clip(struct cso_context
*ctx
)
828 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
832 cso_restore_clip(struct cso_context
*ctx
)
834 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
835 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
836 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
841 cso_set_vertex_elements(struct cso_context
*ctx
,
843 const struct pipe_vertex_element
*states
)
845 struct u_vbuf
*vbuf
= ctx
->vbuf
;
846 unsigned key_size
, hash_key
;
847 struct cso_hash_iter iter
;
849 struct cso_velems_state velems_state
;
852 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
856 /* Need to include the count into the stored state data too.
857 * Otherwise first few count pipe_vertex_elements could be identical
858 * even if count is different, and there's no guarantee the hash would
859 * be different in that case neither.
861 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
862 velems_state
.count
= count
;
863 memcpy(velems_state
.velems
, states
,
864 sizeof(struct pipe_vertex_element
) * count
);
865 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
866 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
,
867 (void*)&velems_state
, key_size
);
869 if (cso_hash_iter_is_null(iter
)) {
870 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
872 return PIPE_ERROR_OUT_OF_MEMORY
;
874 memcpy(&cso
->state
, &velems_state
, key_size
);
875 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
,
876 &cso
->state
.velems
[0]);
878 (cso_state_callback
) ctx
->pipe
->delete_vertex_elements_state
;
879 cso
->context
= ctx
->pipe
;
881 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
882 if (cso_hash_iter_is_null(iter
)) {
884 return PIPE_ERROR_OUT_OF_MEMORY
;
890 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
893 if (ctx
->velements
!= handle
) {
894 ctx
->velements
= handle
;
895 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
900 void cso_save_vertex_elements(struct cso_context
*ctx
)
902 struct u_vbuf
*vbuf
= ctx
->vbuf
;
905 u_vbuf_save_vertex_elements(vbuf
);
909 assert(!ctx
->velements_saved
);
910 ctx
->velements_saved
= ctx
->velements
;
913 void cso_restore_vertex_elements(struct cso_context
*ctx
)
915 struct u_vbuf
*vbuf
= ctx
->vbuf
;
918 u_vbuf_restore_vertex_elements(vbuf
);
922 if (ctx
->velements
!= ctx
->velements_saved
) {
923 ctx
->velements
= ctx
->velements_saved
;
924 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
926 ctx
->velements_saved
= NULL
;
931 void cso_set_vertex_buffers(struct cso_context
*ctx
,
932 unsigned start_slot
, unsigned count
,
933 const struct pipe_vertex_buffer
*buffers
)
935 struct u_vbuf
*vbuf
= ctx
->vbuf
;
938 u_vbuf_set_vertex_buffers(vbuf
, start_slot
, count
, buffers
);
942 /* Save what's in the auxiliary slot, so that we can save and restore it
944 if (start_slot
<= ctx
->aux_vertex_buffer_index
&&
945 start_slot
+count
> ctx
->aux_vertex_buffer_index
) {
947 const struct pipe_vertex_buffer
*vb
=
948 buffers
+ (ctx
->aux_vertex_buffer_index
- start_slot
);
950 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
,
952 memcpy(&ctx
->aux_vertex_buffer_current
, vb
,
953 sizeof(struct pipe_vertex_buffer
));
956 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
,
958 ctx
->aux_vertex_buffer_current
.user_buffer
= NULL
;
962 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, start_slot
, count
, buffers
);
965 void cso_save_aux_vertex_buffer_slot(struct cso_context
*ctx
)
967 struct u_vbuf
*vbuf
= ctx
->vbuf
;
970 u_vbuf_save_aux_vertex_buffer_slot(vbuf
);
974 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
,
975 ctx
->aux_vertex_buffer_current
.buffer
);
976 memcpy(&ctx
->aux_vertex_buffer_saved
, &ctx
->aux_vertex_buffer_current
,
977 sizeof(struct pipe_vertex_buffer
));
980 void cso_restore_aux_vertex_buffer_slot(struct cso_context
*ctx
)
982 struct u_vbuf
*vbuf
= ctx
->vbuf
;
985 u_vbuf_restore_aux_vertex_buffer_slot(vbuf
);
989 cso_set_vertex_buffers(ctx
, ctx
->aux_vertex_buffer_index
, 1,
990 &ctx
->aux_vertex_buffer_saved
);
991 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
, NULL
);
994 unsigned cso_get_aux_vertex_buffer_slot(struct cso_context
*ctx
)
996 return ctx
->aux_vertex_buffer_index
;
1000 /**************** fragment/vertex sampler view state *************************/
1002 static enum pipe_error
1003 single_sampler(struct cso_context
*ctx
,
1004 struct sampler_info
*info
,
1006 const struct pipe_sampler_state
*templ
)
1008 void *handle
= NULL
;
1010 if (templ
!= NULL
) {
1011 unsigned key_size
= sizeof(struct pipe_sampler_state
);
1012 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
1013 struct cso_hash_iter iter
=
1014 cso_find_state_template(ctx
->cache
,
1015 hash_key
, CSO_SAMPLER
,
1016 (void *) templ
, key_size
);
1018 if (cso_hash_iter_is_null(iter
)) {
1019 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
1021 return PIPE_ERROR_OUT_OF_MEMORY
;
1023 memcpy(&cso
->state
, templ
, sizeof(*templ
));
1024 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
1026 (cso_state_callback
) ctx
->pipe
->delete_sampler_state
;
1027 cso
->context
= ctx
->pipe
;
1029 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
1030 if (cso_hash_iter_is_null(iter
)) {
1032 return PIPE_ERROR_OUT_OF_MEMORY
;
1038 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
1042 info
->samplers
[idx
] = handle
;
1048 cso_single_sampler(struct cso_context
*ctx
,
1049 unsigned shader_stage
,
1051 const struct pipe_sampler_state
*templ
)
1053 return single_sampler(ctx
, &ctx
->samplers
[shader_stage
], idx
, templ
);
1059 single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1061 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1064 /* find highest non-null sampler */
1065 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1066 if (info
->samplers
[i
- 1] != NULL
)
1070 info
->nr_samplers
= i
;
1072 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1073 memcmp(info
->hw
.samplers
,
1075 info
->nr_samplers
* sizeof(void *)) != 0)
1077 memcpy(info
->hw
.samplers
,
1079 info
->nr_samplers
* sizeof(void *));
1080 info
->hw
.nr_samplers
= info
->nr_samplers
;
1082 switch (shader_stage
) {
1083 case PIPE_SHADER_FRAGMENT
:
1084 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
,
1088 case PIPE_SHADER_VERTEX
:
1089 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
1093 case PIPE_SHADER_GEOMETRY
:
1094 ctx
->pipe
->bind_geometry_sampler_states(ctx
->pipe
,
1099 assert(!"bad shader type in single_sampler_done()");
1105 cso_single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1107 single_sampler_done(ctx
, shader_stage
);
1112 * If the function encouters any errors it will return the
1113 * last one. Done to always try to set as many samplers
1117 cso_set_samplers(struct cso_context
*ctx
,
1118 unsigned shader_stage
,
1120 const struct pipe_sampler_state
**templates
)
1122 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1124 enum pipe_error temp
, error
= PIPE_OK
;
1129 for (i
= 0; i
< nr
; i
++) {
1130 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1131 if (temp
!= PIPE_OK
)
1135 for ( ; i
< info
->nr_samplers
; i
++) {
1136 temp
= single_sampler(ctx
, info
, i
, NULL
);
1137 if (temp
!= PIPE_OK
)
1141 single_sampler_done(ctx
, shader_stage
);
1147 cso_save_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1149 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1150 info
->nr_samplers_saved
= info
->nr_samplers
;
1151 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1156 cso_restore_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1158 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1159 info
->nr_samplers
= info
->nr_samplers_saved
;
1160 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1161 single_sampler_done(ctx
, shader_stage
);
1166 cso_set_sampler_views(struct cso_context
*ctx
,
1167 unsigned shader_stage
,
1169 struct pipe_sampler_view
**views
)
1171 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1174 /* reference new views */
1175 for (i
= 0; i
< count
; i
++) {
1176 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1178 /* unref extra old views, if any */
1179 for (; i
< info
->nr_views
; i
++) {
1180 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1183 info
->nr_views
= count
;
1185 /* bind the new sampler views */
1186 switch (shader_stage
) {
1187 case PIPE_SHADER_FRAGMENT
:
1188 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, count
, info
->views
);
1190 case PIPE_SHADER_VERTEX
:
1191 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, count
, info
->views
);
1193 case PIPE_SHADER_GEOMETRY
:
1194 ctx
->pipe
->set_geometry_sampler_views(ctx
->pipe
, count
, info
->views
);
1197 assert(!"bad shader type in cso_set_sampler_views()");
1203 cso_save_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1205 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1208 info
->nr_views_saved
= info
->nr_views
;
1210 for (i
= 0; i
< info
->nr_views
; i
++) {
1211 assert(!info
->views_saved
[i
]);
1212 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1218 cso_restore_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1220 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1221 unsigned i
, nr_saved
= info
->nr_views_saved
;
1223 for (i
= 0; i
< nr_saved
; i
++) {
1224 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1225 /* move the reference from one pointer to another */
1226 info
->views
[i
] = info
->views_saved
[i
];
1227 info
->views_saved
[i
] = NULL
;
1229 for (; i
< info
->nr_views
; i
++) {
1230 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1233 /* bind the old/saved sampler views */
1234 switch (shader_stage
) {
1235 case PIPE_SHADER_FRAGMENT
:
1236 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1238 case PIPE_SHADER_VERTEX
:
1239 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1241 case PIPE_SHADER_GEOMETRY
:
1242 ctx
->pipe
->set_geometry_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1245 assert(!"bad shader type in cso_restore_sampler_views()");
1248 info
->nr_views
= nr_saved
;
1249 info
->nr_views_saved
= 0;
1254 cso_set_stream_outputs(struct cso_context
*ctx
,
1255 unsigned num_targets
,
1256 struct pipe_stream_output_target
**targets
,
1257 unsigned append_bitmask
)
1259 struct pipe_context
*pipe
= ctx
->pipe
;
1262 if (!ctx
->has_streamout
) {
1263 assert(num_targets
== 0);
1267 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1268 /* Nothing to do. */
1272 /* reference new targets */
1273 for (i
= 0; i
< num_targets
; i
++) {
1274 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1276 /* unref extra old targets, if any */
1277 for (; i
< ctx
->nr_so_targets
; i
++) {
1278 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1281 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1283 ctx
->nr_so_targets
= num_targets
;
1287 cso_save_stream_outputs(struct cso_context
*ctx
)
1291 if (!ctx
->has_streamout
) {
1295 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1297 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1298 assert(!ctx
->so_targets_saved
[i
]);
1299 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1304 cso_restore_stream_outputs(struct cso_context
*ctx
)
1306 struct pipe_context
*pipe
= ctx
->pipe
;
1309 if (!ctx
->has_streamout
) {
1313 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1314 /* Nothing to do. */
1318 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1319 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1320 /* move the reference from one pointer to another */
1321 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1322 ctx
->so_targets_saved
[i
] = NULL
;
1324 for (; i
< ctx
->nr_so_targets
; i
++) {
1325 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1328 /* ~0 means append */
1329 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1330 ctx
->so_targets
, ~0);
1332 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1333 ctx
->nr_so_targets_saved
= 0;
1336 /* constant buffers */
1339 cso_set_constant_buffer(struct cso_context
*cso
, unsigned shader_stage
,
1340 unsigned index
, struct pipe_constant_buffer
*cb
)
1342 struct pipe_context
*pipe
= cso
->pipe
;
1344 pipe
->set_constant_buffer(pipe
, shader_stage
, index
, cb
);
1347 util_copy_constant_buffer(&cso
->aux_constbuf_current
[shader_stage
], cb
);
1352 cso_set_constant_buffer_resource(struct cso_context
*cso
,
1353 unsigned shader_stage
,
1355 struct pipe_resource
*buffer
)
1358 struct pipe_constant_buffer cb
;
1360 cb
.buffer_offset
= 0;
1361 cb
.buffer_size
= buffer
->width0
;
1362 cb
.user_buffer
= NULL
;
1363 cso_set_constant_buffer(cso
, shader_stage
, index
, &cb
);
1365 cso_set_constant_buffer(cso
, shader_stage
, index
, NULL
);
1370 cso_save_constant_buffer_slot0(struct cso_context
*cso
,
1371 unsigned shader_stage
)
1373 util_copy_constant_buffer(&cso
->aux_constbuf_saved
[shader_stage
],
1374 &cso
->aux_constbuf_current
[shader_stage
]);
1378 cso_restore_constant_buffer_slot0(struct cso_context
*cso
,
1379 unsigned shader_stage
)
1381 cso_set_constant_buffer(cso
, shader_stage
, 0,
1382 &cso
->aux_constbuf_saved
[shader_stage
]);
1383 pipe_resource_reference(&cso
->aux_constbuf_saved
[shader_stage
].buffer
,
1390 cso_set_index_buffer(struct cso_context
*cso
,
1391 const struct pipe_index_buffer
*ib
)
1393 struct u_vbuf
*vbuf
= cso
->vbuf
;
1396 u_vbuf_set_index_buffer(vbuf
, ib
);
1398 struct pipe_context
*pipe
= cso
->pipe
;
1399 pipe
->set_index_buffer(pipe
, ib
);
1404 cso_draw_vbo(struct cso_context
*cso
,
1405 const struct pipe_draw_info
*info
)
1407 struct u_vbuf
*vbuf
= cso
->vbuf
;
1410 u_vbuf_draw_vbo(vbuf
, info
);
1412 struct pipe_context
*pipe
= cso
->pipe
;
1413 pipe
->draw_vbo(pipe
, info
);
1418 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1420 struct pipe_draw_info info
;
1422 util_draw_init_info(&info
);
1427 info
.min_index
= start
;
1428 info
.max_index
= start
+ count
- 1;
1430 cso_draw_vbo(cso
, &info
);