1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <zackr@vmware.com>
35 * @author Keith Whitwell <keithw@vmware.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_SHADER_SAMPLER_VIEWS
];
73 struct pipe_sampler_view
*views_saved
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
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
= ~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
);
309 static struct pipe_sampler_view
*views
[PIPE_MAX_SHADER_SAMPLER_VIEWS
] = { NULL
};
310 static void *zeros
[PIPE_MAX_SAMPLERS
] = { NULL
};
311 struct pipe_screen
*scr
= ctx
->pipe
->screen
;
313 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
314 int maxsam
= scr
->get_shader_param(scr
, sh
,
315 PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS
);
316 int maxview
= scr
->get_shader_param(scr
, sh
,
317 PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS
);
318 assert(maxsam
<= PIPE_MAX_SAMPLERS
);
319 assert(maxview
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
321 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, sh
, 0, maxsam
, zeros
);
324 ctx
->pipe
->set_sampler_views(ctx
->pipe
, sh
, 0, maxview
, views
);
329 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
330 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
331 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
332 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
334 if (ctx
->pipe
->set_stream_output_targets
)
335 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, 0);
338 /* free fragment sampler views */
339 for (shader
= 0; shader
< Elements(ctx
->samplers
); shader
++) {
340 struct sampler_info
*info
= &ctx
->samplers
[shader
];
341 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
342 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
343 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
347 util_unreference_framebuffer_state(&ctx
->fb
);
348 util_unreference_framebuffer_state(&ctx
->fb_saved
);
350 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
, NULL
);
351 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
, NULL
);
353 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
354 pipe_resource_reference(&ctx
->aux_constbuf_current
[i
].buffer
, NULL
);
355 pipe_resource_reference(&ctx
->aux_constbuf_saved
[i
].buffer
, NULL
);
358 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
359 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
360 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
364 cso_cache_delete( ctx
->cache
);
371 * Free the CSO context. NOTE: the state tracker should have previously called
374 void cso_destroy_context( struct cso_context
*ctx
)
378 u_vbuf_destroy(ctx
->vbuf
);
384 /* Those function will either find the state of the given template
385 * in the cache or they will create a new state from the given
386 * template, insert it in the cache and return it.
390 * If the driver returns 0 from the create method then they will assign
391 * the data member of the cso to be the template itself.
394 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
395 const struct pipe_blend_state
*templ
)
397 unsigned key_size
, hash_key
;
398 struct cso_hash_iter iter
;
401 key_size
= templ
->independent_blend_enable
?
402 sizeof(struct pipe_blend_state
) :
403 (char *)&(templ
->rt
[1]) - (char *)templ
;
404 hash_key
= cso_construct_key((void*)templ
, key_size
);
405 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
,
406 (void*)templ
, key_size
);
408 if (cso_hash_iter_is_null(iter
)) {
409 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
411 return PIPE_ERROR_OUT_OF_MEMORY
;
413 memset(&cso
->state
, 0, sizeof cso
->state
);
414 memcpy(&cso
->state
, templ
, key_size
);
415 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
416 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
417 cso
->context
= ctx
->pipe
;
419 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
420 if (cso_hash_iter_is_null(iter
)) {
422 return PIPE_ERROR_OUT_OF_MEMORY
;
428 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
431 if (ctx
->blend
!= handle
) {
433 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
438 void cso_save_blend(struct cso_context
*ctx
)
440 assert(!ctx
->blend_saved
);
441 ctx
->blend_saved
= ctx
->blend
;
444 void cso_restore_blend(struct cso_context
*ctx
)
446 if (ctx
->blend
!= ctx
->blend_saved
) {
447 ctx
->blend
= ctx
->blend_saved
;
448 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
450 ctx
->blend_saved
= NULL
;
456 cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
457 const struct pipe_depth_stencil_alpha_state
*templ
)
459 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
460 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
461 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
463 CSO_DEPTH_STENCIL_ALPHA
,
464 (void*)templ
, key_size
);
467 if (cso_hash_iter_is_null(iter
)) {
468 struct cso_depth_stencil_alpha
*cso
=
469 MALLOC(sizeof(struct cso_depth_stencil_alpha
));
471 return PIPE_ERROR_OUT_OF_MEMORY
;
473 memcpy(&cso
->state
, templ
, sizeof(*templ
));
474 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
,
477 (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
478 cso
->context
= ctx
->pipe
;
480 iter
= cso_insert_state(ctx
->cache
, hash_key
,
481 CSO_DEPTH_STENCIL_ALPHA
, cso
);
482 if (cso_hash_iter_is_null(iter
)) {
484 return PIPE_ERROR_OUT_OF_MEMORY
;
490 handle
= ((struct cso_depth_stencil_alpha
*)
491 cso_hash_iter_data(iter
))->data
;
494 if (ctx
->depth_stencil
!= handle
) {
495 ctx
->depth_stencil
= handle
;
496 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
501 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
503 assert(!ctx
->depth_stencil_saved
);
504 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
507 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
509 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
510 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
511 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
,
512 ctx
->depth_stencil_saved
);
514 ctx
->depth_stencil_saved
= NULL
;
519 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
520 const struct pipe_rasterizer_state
*templ
)
522 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
523 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
524 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
527 (void*)templ
, key_size
);
530 if (cso_hash_iter_is_null(iter
)) {
531 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
533 return PIPE_ERROR_OUT_OF_MEMORY
;
535 memcpy(&cso
->state
, templ
, sizeof(*templ
));
536 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
538 (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
539 cso
->context
= ctx
->pipe
;
541 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
542 if (cso_hash_iter_is_null(iter
)) {
544 return PIPE_ERROR_OUT_OF_MEMORY
;
550 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
553 if (ctx
->rasterizer
!= handle
) {
554 ctx
->rasterizer
= handle
;
555 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
560 void cso_save_rasterizer(struct cso_context
*ctx
)
562 assert(!ctx
->rasterizer_saved
);
563 ctx
->rasterizer_saved
= ctx
->rasterizer
;
566 void cso_restore_rasterizer(struct cso_context
*ctx
)
568 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
569 ctx
->rasterizer
= ctx
->rasterizer_saved
;
570 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
572 ctx
->rasterizer_saved
= NULL
;
576 void cso_set_fragment_shader_handle(struct cso_context
*ctx
, void *handle
)
578 if (ctx
->fragment_shader
!= handle
) {
579 ctx
->fragment_shader
= handle
;
580 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
584 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
586 if (handle
== ctx
->fragment_shader
) {
587 /* unbind before deleting */
588 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
589 ctx
->fragment_shader
= NULL
;
591 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
594 void cso_save_fragment_shader(struct cso_context
*ctx
)
596 assert(!ctx
->fragment_shader_saved
);
597 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
600 void cso_restore_fragment_shader(struct cso_context
*ctx
)
602 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
603 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
604 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
606 ctx
->fragment_shader_saved
= NULL
;
610 void cso_set_vertex_shader_handle(struct cso_context
*ctx
, void *handle
)
612 if (ctx
->vertex_shader
!= handle
) {
613 ctx
->vertex_shader
= handle
;
614 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
618 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
620 if (handle
== ctx
->vertex_shader
) {
621 /* unbind before deleting */
622 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
623 ctx
->vertex_shader
= NULL
;
625 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
628 void cso_save_vertex_shader(struct cso_context
*ctx
)
630 assert(!ctx
->vertex_shader_saved
);
631 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
634 void cso_restore_vertex_shader(struct cso_context
*ctx
)
636 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
637 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
638 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
640 ctx
->vertex_shader_saved
= NULL
;
644 void cso_set_framebuffer(struct cso_context
*ctx
,
645 const struct pipe_framebuffer_state
*fb
)
647 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
648 util_copy_framebuffer_state(&ctx
->fb
, fb
);
649 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
653 void cso_save_framebuffer(struct cso_context
*ctx
)
655 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
658 void cso_restore_framebuffer(struct cso_context
*ctx
)
660 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
661 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
662 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
663 util_unreference_framebuffer_state(&ctx
->fb_saved
);
668 void cso_set_viewport(struct cso_context
*ctx
,
669 const struct pipe_viewport_state
*vp
)
671 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
673 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, vp
);
677 void cso_save_viewport(struct cso_context
*ctx
)
679 ctx
->vp_saved
= ctx
->vp
;
683 void cso_restore_viewport(struct cso_context
*ctx
)
685 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
686 ctx
->vp
= ctx
->vp_saved
;
687 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, &ctx
->vp
);
692 void cso_set_blend_color(struct cso_context
*ctx
,
693 const struct pipe_blend_color
*bc
)
695 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
696 ctx
->blend_color
= *bc
;
697 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
701 void cso_set_sample_mask(struct cso_context
*ctx
, unsigned sample_mask
)
703 if (ctx
->sample_mask
!= sample_mask
) {
704 ctx
->sample_mask
= sample_mask
;
705 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
709 void cso_save_sample_mask(struct cso_context
*ctx
)
711 ctx
->sample_mask_saved
= ctx
->sample_mask
;
714 void cso_restore_sample_mask(struct cso_context
*ctx
)
716 cso_set_sample_mask(ctx
, ctx
->sample_mask_saved
);
719 void cso_set_stencil_ref(struct cso_context
*ctx
,
720 const struct pipe_stencil_ref
*sr
)
722 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
723 ctx
->stencil_ref
= *sr
;
724 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
728 void cso_save_stencil_ref(struct cso_context
*ctx
)
730 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
734 void cso_restore_stencil_ref(struct cso_context
*ctx
)
736 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
,
737 sizeof(ctx
->stencil_ref
))) {
738 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
739 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
743 void cso_set_render_condition(struct cso_context
*ctx
,
744 struct pipe_query
*query
,
745 boolean condition
, uint mode
)
747 struct pipe_context
*pipe
= ctx
->pipe
;
749 if (ctx
->render_condition
!= query
||
750 ctx
->render_condition_mode
!= mode
||
751 ctx
->render_condition_cond
!= condition
) {
752 pipe
->render_condition(pipe
, query
, condition
, mode
);
753 ctx
->render_condition
= query
;
754 ctx
->render_condition_cond
= condition
;
755 ctx
->render_condition_mode
= mode
;
759 void cso_save_render_condition(struct cso_context
*ctx
)
761 ctx
->render_condition_saved
= ctx
->render_condition
;
762 ctx
->render_condition_cond_saved
= ctx
->render_condition_cond
;
763 ctx
->render_condition_mode_saved
= ctx
->render_condition_mode
;
766 void cso_restore_render_condition(struct cso_context
*ctx
)
768 cso_set_render_condition(ctx
, ctx
->render_condition_saved
,
769 ctx
->render_condition_cond_saved
,
770 ctx
->render_condition_mode_saved
);
773 void cso_set_geometry_shader_handle(struct cso_context
*ctx
, void *handle
)
775 assert(ctx
->has_geometry_shader
|| !handle
);
777 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
778 ctx
->geometry_shader
= handle
;
779 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
783 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
785 if (handle
== ctx
->geometry_shader
) {
786 /* unbind before deleting */
787 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
788 ctx
->geometry_shader
= NULL
;
790 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
793 void cso_save_geometry_shader(struct cso_context
*ctx
)
795 if (!ctx
->has_geometry_shader
) {
799 assert(!ctx
->geometry_shader_saved
);
800 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
803 void cso_restore_geometry_shader(struct cso_context
*ctx
)
805 if (!ctx
->has_geometry_shader
) {
809 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
810 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
811 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
813 ctx
->geometry_shader_saved
= NULL
;
819 clip_state_cpy(struct pipe_clip_state
*dst
,
820 const struct pipe_clip_state
*src
)
822 memcpy(dst
->ucp
, src
->ucp
, sizeof(dst
->ucp
));
826 clip_state_cmp(const struct pipe_clip_state
*a
,
827 const struct pipe_clip_state
*b
)
829 return memcmp(a
->ucp
, b
->ucp
, sizeof(a
->ucp
));
833 cso_set_clip(struct cso_context
*ctx
,
834 const struct pipe_clip_state
*clip
)
836 if (clip_state_cmp(&ctx
->clip
, clip
)) {
837 clip_state_cpy(&ctx
->clip
, clip
);
838 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
843 cso_save_clip(struct cso_context
*ctx
)
845 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
849 cso_restore_clip(struct cso_context
*ctx
)
851 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
852 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
853 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
858 cso_set_vertex_elements(struct cso_context
*ctx
,
860 const struct pipe_vertex_element
*states
)
862 struct u_vbuf
*vbuf
= ctx
->vbuf
;
863 unsigned key_size
, hash_key
;
864 struct cso_hash_iter iter
;
866 struct cso_velems_state velems_state
;
869 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
873 /* Need to include the count into the stored state data too.
874 * Otherwise first few count pipe_vertex_elements could be identical
875 * even if count is different, and there's no guarantee the hash would
876 * be different in that case neither.
878 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
879 velems_state
.count
= count
;
880 memcpy(velems_state
.velems
, states
,
881 sizeof(struct pipe_vertex_element
) * count
);
882 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
883 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
,
884 (void*)&velems_state
, key_size
);
886 if (cso_hash_iter_is_null(iter
)) {
887 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
889 return PIPE_ERROR_OUT_OF_MEMORY
;
891 memcpy(&cso
->state
, &velems_state
, key_size
);
892 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
,
893 &cso
->state
.velems
[0]);
895 (cso_state_callback
) ctx
->pipe
->delete_vertex_elements_state
;
896 cso
->context
= ctx
->pipe
;
898 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
899 if (cso_hash_iter_is_null(iter
)) {
901 return PIPE_ERROR_OUT_OF_MEMORY
;
907 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
910 if (ctx
->velements
!= handle
) {
911 ctx
->velements
= handle
;
912 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
917 void cso_save_vertex_elements(struct cso_context
*ctx
)
919 struct u_vbuf
*vbuf
= ctx
->vbuf
;
922 u_vbuf_save_vertex_elements(vbuf
);
926 assert(!ctx
->velements_saved
);
927 ctx
->velements_saved
= ctx
->velements
;
930 void cso_restore_vertex_elements(struct cso_context
*ctx
)
932 struct u_vbuf
*vbuf
= ctx
->vbuf
;
935 u_vbuf_restore_vertex_elements(vbuf
);
939 if (ctx
->velements
!= ctx
->velements_saved
) {
940 ctx
->velements
= ctx
->velements_saved
;
941 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
943 ctx
->velements_saved
= NULL
;
948 void cso_set_vertex_buffers(struct cso_context
*ctx
,
949 unsigned start_slot
, unsigned count
,
950 const struct pipe_vertex_buffer
*buffers
)
952 struct u_vbuf
*vbuf
= ctx
->vbuf
;
955 u_vbuf_set_vertex_buffers(vbuf
, start_slot
, count
, buffers
);
959 /* Save what's in the auxiliary slot, so that we can save and restore it
961 if (start_slot
<= ctx
->aux_vertex_buffer_index
&&
962 start_slot
+count
> ctx
->aux_vertex_buffer_index
) {
964 const struct pipe_vertex_buffer
*vb
=
965 buffers
+ (ctx
->aux_vertex_buffer_index
- start_slot
);
967 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
,
969 memcpy(&ctx
->aux_vertex_buffer_current
, vb
,
970 sizeof(struct pipe_vertex_buffer
));
973 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
,
975 ctx
->aux_vertex_buffer_current
.user_buffer
= NULL
;
979 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, start_slot
, count
, buffers
);
982 void cso_save_aux_vertex_buffer_slot(struct cso_context
*ctx
)
984 struct u_vbuf
*vbuf
= ctx
->vbuf
;
987 u_vbuf_save_aux_vertex_buffer_slot(vbuf
);
991 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
,
992 ctx
->aux_vertex_buffer_current
.buffer
);
993 memcpy(&ctx
->aux_vertex_buffer_saved
, &ctx
->aux_vertex_buffer_current
,
994 sizeof(struct pipe_vertex_buffer
));
997 void cso_restore_aux_vertex_buffer_slot(struct cso_context
*ctx
)
999 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1002 u_vbuf_restore_aux_vertex_buffer_slot(vbuf
);
1006 cso_set_vertex_buffers(ctx
, ctx
->aux_vertex_buffer_index
, 1,
1007 &ctx
->aux_vertex_buffer_saved
);
1008 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
, NULL
);
1011 unsigned cso_get_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1013 return ctx
->aux_vertex_buffer_index
;
1017 /**************** fragment/vertex sampler view state *************************/
1019 static enum pipe_error
1020 single_sampler(struct cso_context
*ctx
,
1021 struct sampler_info
*info
,
1023 const struct pipe_sampler_state
*templ
)
1025 void *handle
= NULL
;
1027 if (templ
!= NULL
) {
1028 unsigned key_size
= sizeof(struct pipe_sampler_state
);
1029 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
1030 struct cso_hash_iter iter
=
1031 cso_find_state_template(ctx
->cache
,
1032 hash_key
, CSO_SAMPLER
,
1033 (void *) templ
, key_size
);
1035 if (cso_hash_iter_is_null(iter
)) {
1036 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
1038 return PIPE_ERROR_OUT_OF_MEMORY
;
1040 memcpy(&cso
->state
, templ
, sizeof(*templ
));
1041 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
1043 (cso_state_callback
) ctx
->pipe
->delete_sampler_state
;
1044 cso
->context
= ctx
->pipe
;
1046 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
1047 if (cso_hash_iter_is_null(iter
)) {
1049 return PIPE_ERROR_OUT_OF_MEMORY
;
1055 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
1059 info
->samplers
[idx
] = handle
;
1065 cso_single_sampler(struct cso_context
*ctx
,
1066 unsigned shader_stage
,
1068 const struct pipe_sampler_state
*templ
)
1070 return single_sampler(ctx
, &ctx
->samplers
[shader_stage
], idx
, templ
);
1076 single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1078 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1081 /* find highest non-null sampler */
1082 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1083 if (info
->samplers
[i
- 1] != NULL
)
1087 info
->nr_samplers
= i
;
1089 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1090 memcmp(info
->hw
.samplers
,
1092 info
->nr_samplers
* sizeof(void *)) != 0)
1094 memcpy(info
->hw
.samplers
,
1096 info
->nr_samplers
* sizeof(void *));
1098 /* set remaining slots/pointers to null */
1099 for (i
= info
->nr_samplers
; i
< info
->hw
.nr_samplers
; i
++)
1100 info
->samplers
[i
] = NULL
;
1102 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, shader_stage
, 0,
1103 MAX2(info
->nr_samplers
,
1104 info
->hw
.nr_samplers
),
1107 info
->hw
.nr_samplers
= info
->nr_samplers
;
1112 cso_single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1114 single_sampler_done(ctx
, shader_stage
);
1119 * If the function encouters any errors it will return the
1120 * last one. Done to always try to set as many samplers
1124 cso_set_samplers(struct cso_context
*ctx
,
1125 unsigned shader_stage
,
1127 const struct pipe_sampler_state
**templates
)
1129 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1131 enum pipe_error temp
, error
= PIPE_OK
;
1136 for (i
= 0; i
< nr
; i
++) {
1137 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1138 if (temp
!= PIPE_OK
)
1142 for ( ; i
< info
->nr_samplers
; i
++) {
1143 temp
= single_sampler(ctx
, info
, i
, NULL
);
1144 if (temp
!= PIPE_OK
)
1148 single_sampler_done(ctx
, shader_stage
);
1154 cso_save_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1156 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1157 info
->nr_samplers_saved
= info
->nr_samplers
;
1158 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1163 cso_restore_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1165 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1166 info
->nr_samplers
= info
->nr_samplers_saved
;
1167 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1168 single_sampler_done(ctx
, shader_stage
);
1173 cso_set_sampler_views(struct cso_context
*ctx
,
1174 unsigned shader_stage
,
1176 struct pipe_sampler_view
**views
)
1178 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1181 /* reference new views */
1182 for (i
= 0; i
< count
; i
++) {
1183 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1185 /* unref extra old views, if any */
1186 for (; i
< info
->nr_views
; i
++) {
1187 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1190 info
->nr_views
= count
;
1192 /* bind the new sampler views */
1193 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0, count
,
1199 cso_save_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1201 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1204 info
->nr_views_saved
= info
->nr_views
;
1206 for (i
= 0; i
< info
->nr_views
; i
++) {
1207 assert(!info
->views_saved
[i
]);
1208 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1214 cso_restore_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1216 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1217 unsigned i
, nr_saved
= info
->nr_views_saved
;
1220 for (i
= 0; i
< nr_saved
; i
++) {
1221 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1222 /* move the reference from one pointer to another */
1223 info
->views
[i
] = info
->views_saved
[i
];
1224 info
->views_saved
[i
] = NULL
;
1226 for (; i
< info
->nr_views
; i
++) {
1227 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1230 num
= MAX2(info
->nr_views
, nr_saved
);
1232 /* bind the old/saved sampler views */
1233 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0, num
, info
->views
);
1235 info
->nr_views
= nr_saved
;
1236 info
->nr_views_saved
= 0;
1241 cso_set_stream_outputs(struct cso_context
*ctx
,
1242 unsigned num_targets
,
1243 struct pipe_stream_output_target
**targets
,
1244 unsigned append_bitmask
)
1246 struct pipe_context
*pipe
= ctx
->pipe
;
1249 if (!ctx
->has_streamout
) {
1250 assert(num_targets
== 0);
1254 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1255 /* Nothing to do. */
1259 /* reference new targets */
1260 for (i
= 0; i
< num_targets
; i
++) {
1261 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1263 /* unref extra old targets, if any */
1264 for (; i
< ctx
->nr_so_targets
; i
++) {
1265 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1268 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1270 ctx
->nr_so_targets
= num_targets
;
1274 cso_save_stream_outputs(struct cso_context
*ctx
)
1278 if (!ctx
->has_streamout
) {
1282 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1284 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1285 assert(!ctx
->so_targets_saved
[i
]);
1286 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1291 cso_restore_stream_outputs(struct cso_context
*ctx
)
1293 struct pipe_context
*pipe
= ctx
->pipe
;
1296 if (!ctx
->has_streamout
) {
1300 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1301 /* Nothing to do. */
1305 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1306 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1307 /* move the reference from one pointer to another */
1308 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1309 ctx
->so_targets_saved
[i
] = NULL
;
1311 for (; i
< ctx
->nr_so_targets
; i
++) {
1312 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1315 /* ~0 means append */
1316 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1317 ctx
->so_targets
, ~0);
1319 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1320 ctx
->nr_so_targets_saved
= 0;
1323 /* constant buffers */
1326 cso_set_constant_buffer(struct cso_context
*cso
, unsigned shader_stage
,
1327 unsigned index
, struct pipe_constant_buffer
*cb
)
1329 struct pipe_context
*pipe
= cso
->pipe
;
1331 pipe
->set_constant_buffer(pipe
, shader_stage
, index
, cb
);
1334 util_copy_constant_buffer(&cso
->aux_constbuf_current
[shader_stage
], cb
);
1339 cso_set_constant_buffer_resource(struct cso_context
*cso
,
1340 unsigned shader_stage
,
1342 struct pipe_resource
*buffer
)
1345 struct pipe_constant_buffer cb
;
1347 cb
.buffer_offset
= 0;
1348 cb
.buffer_size
= buffer
->width0
;
1349 cb
.user_buffer
= NULL
;
1350 cso_set_constant_buffer(cso
, shader_stage
, index
, &cb
);
1352 cso_set_constant_buffer(cso
, shader_stage
, index
, NULL
);
1357 cso_save_constant_buffer_slot0(struct cso_context
*cso
,
1358 unsigned shader_stage
)
1360 util_copy_constant_buffer(&cso
->aux_constbuf_saved
[shader_stage
],
1361 &cso
->aux_constbuf_current
[shader_stage
]);
1365 cso_restore_constant_buffer_slot0(struct cso_context
*cso
,
1366 unsigned shader_stage
)
1368 cso_set_constant_buffer(cso
, shader_stage
, 0,
1369 &cso
->aux_constbuf_saved
[shader_stage
]);
1370 pipe_resource_reference(&cso
->aux_constbuf_saved
[shader_stage
].buffer
,
1377 cso_set_index_buffer(struct cso_context
*cso
,
1378 const struct pipe_index_buffer
*ib
)
1380 struct u_vbuf
*vbuf
= cso
->vbuf
;
1383 u_vbuf_set_index_buffer(vbuf
, ib
);
1385 struct pipe_context
*pipe
= cso
->pipe
;
1386 pipe
->set_index_buffer(pipe
, ib
);
1391 cso_draw_vbo(struct cso_context
*cso
,
1392 const struct pipe_draw_info
*info
)
1394 struct u_vbuf
*vbuf
= cso
->vbuf
;
1397 u_vbuf_draw_vbo(vbuf
, info
);
1399 struct pipe_context
*pipe
= cso
->pipe
;
1400 pipe
->draw_vbo(pipe
, info
);
1405 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1407 struct pipe_draw_info info
;
1409 util_draw_init_info(&info
);
1414 info
.min_index
= start
;
1415 info
.max_index
= start
+ count
- 1;
1417 cso_draw_vbo(cso
, &info
);
1421 cso_draw_arrays_instanced(struct cso_context
*cso
, uint mode
,
1422 uint start
, uint count
,
1423 uint start_instance
, uint instance_count
)
1425 struct pipe_draw_info info
;
1427 util_draw_init_info(&info
);
1432 info
.min_index
= start
;
1433 info
.max_index
= start
+ count
- 1;
1434 info
.start_instance
= start_instance
;
1435 info
.instance_count
= instance_count
;
1437 cso_draw_vbo(cso
, &info
);