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_tessellation
;
86 boolean has_streamout
;
88 struct sampler_info samplers
[PIPE_SHADER_TYPES
];
90 struct pipe_vertex_buffer aux_vertex_buffer_current
;
91 struct pipe_vertex_buffer aux_vertex_buffer_saved
;
92 unsigned aux_vertex_buffer_index
;
94 struct pipe_constant_buffer aux_constbuf_current
[PIPE_SHADER_TYPES
];
95 struct pipe_constant_buffer aux_constbuf_saved
[PIPE_SHADER_TYPES
];
97 unsigned nr_so_targets
;
98 struct pipe_stream_output_target
*so_targets
[PIPE_MAX_SO_BUFFERS
];
100 unsigned nr_so_targets_saved
;
101 struct pipe_stream_output_target
*so_targets_saved
[PIPE_MAX_SO_BUFFERS
];
103 /** Current and saved state.
104 * The saved state is used as a 1-deep stack.
106 void *blend
, *blend_saved
;
107 void *depth_stencil
, *depth_stencil_saved
;
108 void *rasterizer
, *rasterizer_saved
;
109 void *fragment_shader
, *fragment_shader_saved
;
110 void *vertex_shader
, *vertex_shader_saved
;
111 void *geometry_shader
, *geometry_shader_saved
;
112 void *tessctrl_shader
, *tessctrl_shader_saved
;
113 void *tesseval_shader
, *tesseval_shader_saved
;
114 void *velements
, *velements_saved
;
115 struct pipe_query
*render_condition
, *render_condition_saved
;
116 uint render_condition_mode
, render_condition_mode_saved
;
117 boolean render_condition_cond
, render_condition_cond_saved
;
119 struct pipe_clip_state clip
;
120 struct pipe_clip_state clip_saved
;
122 struct pipe_framebuffer_state fb
, fb_saved
;
123 struct pipe_viewport_state vp
, vp_saved
;
124 struct pipe_blend_color blend_color
;
125 unsigned sample_mask
, sample_mask_saved
;
126 unsigned min_samples
, min_samples_saved
;
127 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
131 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
133 struct cso_blend
*cso
= (struct cso_blend
*)state
;
135 if (ctx
->blend
== cso
->data
)
138 if (cso
->delete_state
)
139 cso
->delete_state(cso
->context
, cso
->data
);
144 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
146 struct cso_depth_stencil_alpha
*cso
=
147 (struct cso_depth_stencil_alpha
*)state
;
149 if (ctx
->depth_stencil
== cso
->data
)
152 if (cso
->delete_state
)
153 cso
->delete_state(cso
->context
, cso
->data
);
159 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
161 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
162 if (cso
->delete_state
)
163 cso
->delete_state(cso
->context
, cso
->data
);
168 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
170 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
172 if (ctx
->rasterizer
== cso
->data
)
174 if (cso
->delete_state
)
175 cso
->delete_state(cso
->context
, cso
->data
);
180 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
183 struct cso_velements
*cso
= (struct cso_velements
*)state
;
185 if (ctx
->velements
== cso
->data
)
188 if (cso
->delete_state
)
189 cso
->delete_state(cso
->context
, cso
->data
);
195 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
196 void *state
, enum cso_cache_type type
)
200 return delete_blend_state(ctx
, state
);
202 return delete_sampler_state(ctx
, state
);
203 case CSO_DEPTH_STENCIL_ALPHA
:
204 return delete_depth_stencil_state(ctx
, state
);
206 return delete_rasterizer_state(ctx
, state
);
208 return delete_vertex_elements(ctx
, state
);
217 sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
218 int max_size
, void *user_data
)
220 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
221 /* if we're approach the maximum size, remove fourth of the entries
222 * otherwise every subsequent call will go through the same */
223 int hash_size
= cso_hash_size(hash
);
224 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
225 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
226 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
227 if (hash_size
> max_size
)
228 to_remove
+= hash_size
- max_size
;
230 /*remove elements until we're good */
231 /*fixme: currently we pick the nodes to remove at random*/
232 void *cso
= cso_hash_iter_data(iter
);
233 if (delete_cso(ctx
, cso
, type
)) {
234 iter
= cso_hash_erase(hash
, iter
);
237 iter
= cso_hash_iter_next(iter
);
241 static void cso_init_vbuf(struct cso_context
*cso
)
243 struct u_vbuf_caps caps
;
245 /* Install u_vbuf if there is anything unsupported. */
246 if (u_vbuf_get_caps(cso
->pipe
->screen
, &caps
)) {
247 cso
->vbuf
= u_vbuf_create(cso
->pipe
, &caps
,
248 cso
->aux_vertex_buffer_index
);
252 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
254 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
258 ctx
->cache
= cso_cache_create();
259 if (ctx
->cache
== NULL
)
261 cso_cache_set_sanitize_callback(ctx
->cache
,
266 ctx
->sample_mask
= ~0;
268 ctx
->aux_vertex_buffer_index
= 0; /* 0 for now */
272 /* Enable for testing: */
273 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
275 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
276 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
277 ctx
->has_geometry_shader
= TRUE
;
279 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
280 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
281 ctx
->has_tessellation
= TRUE
;
283 if (pipe
->screen
->get_param(pipe
->screen
,
284 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
285 ctx
->has_streamout
= TRUE
;
291 cso_destroy_context( ctx
);
296 * Free the CSO context.
298 void cso_destroy_context( struct cso_context
*ctx
)
303 ctx
->pipe
->set_index_buffer(ctx
->pipe
, NULL
);
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
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_FRAGMENT
, 0, NULL
);
332 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
333 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_VERTEX
, 0, NULL
);
334 if (ctx
->has_geometry_shader
) {
335 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
336 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_GEOMETRY
, 0, NULL
);
338 if (ctx
->has_tessellation
) {
339 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, NULL
);
340 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_TESS_CTRL
, 0, NULL
);
341 ctx
->pipe
->bind_tes_state(ctx
->pipe
, NULL
);
342 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_TESS_EVAL
, 0, NULL
);
344 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
346 if (ctx
->has_streamout
)
347 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, NULL
);
350 /* free sampler views for each shader stage */
351 for (shader
= 0; shader
< Elements(ctx
->samplers
); shader
++) {
352 struct sampler_info
*info
= &ctx
->samplers
[shader
];
353 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
354 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
355 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
359 util_unreference_framebuffer_state(&ctx
->fb
);
360 util_unreference_framebuffer_state(&ctx
->fb_saved
);
362 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
, NULL
);
363 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
, NULL
);
365 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
366 pipe_resource_reference(&ctx
->aux_constbuf_current
[i
].buffer
, NULL
);
367 pipe_resource_reference(&ctx
->aux_constbuf_saved
[i
].buffer
, NULL
);
370 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
371 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
372 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
376 cso_cache_delete( ctx
->cache
);
381 u_vbuf_destroy(ctx
->vbuf
);
386 /* Those function will either find the state of the given template
387 * in the cache or they will create a new state from the given
388 * template, insert it in the cache and return it.
392 * If the driver returns 0 from the create method then they will assign
393 * the data member of the cso to be the template itself.
396 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
397 const struct pipe_blend_state
*templ
)
399 unsigned key_size
, hash_key
;
400 struct cso_hash_iter iter
;
403 key_size
= templ
->independent_blend_enable
?
404 sizeof(struct pipe_blend_state
) :
405 (char *)&(templ
->rt
[1]) - (char *)templ
;
406 hash_key
= cso_construct_key((void*)templ
, key_size
);
407 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
,
408 (void*)templ
, key_size
);
410 if (cso_hash_iter_is_null(iter
)) {
411 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
413 return PIPE_ERROR_OUT_OF_MEMORY
;
415 memset(&cso
->state
, 0, sizeof cso
->state
);
416 memcpy(&cso
->state
, templ
, key_size
);
417 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
418 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
419 cso
->context
= ctx
->pipe
;
421 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
422 if (cso_hash_iter_is_null(iter
)) {
424 return PIPE_ERROR_OUT_OF_MEMORY
;
430 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
433 if (ctx
->blend
!= handle
) {
435 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
440 void cso_save_blend(struct cso_context
*ctx
)
442 assert(!ctx
->blend_saved
);
443 ctx
->blend_saved
= ctx
->blend
;
446 void cso_restore_blend(struct cso_context
*ctx
)
448 if (ctx
->blend
!= ctx
->blend_saved
) {
449 ctx
->blend
= ctx
->blend_saved
;
450 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
452 ctx
->blend_saved
= NULL
;
458 cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
459 const struct pipe_depth_stencil_alpha_state
*templ
)
461 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
462 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
463 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
465 CSO_DEPTH_STENCIL_ALPHA
,
466 (void*)templ
, key_size
);
469 if (cso_hash_iter_is_null(iter
)) {
470 struct cso_depth_stencil_alpha
*cso
=
471 MALLOC(sizeof(struct cso_depth_stencil_alpha
));
473 return PIPE_ERROR_OUT_OF_MEMORY
;
475 memcpy(&cso
->state
, templ
, sizeof(*templ
));
476 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
,
479 (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
480 cso
->context
= ctx
->pipe
;
482 iter
= cso_insert_state(ctx
->cache
, hash_key
,
483 CSO_DEPTH_STENCIL_ALPHA
, cso
);
484 if (cso_hash_iter_is_null(iter
)) {
486 return PIPE_ERROR_OUT_OF_MEMORY
;
492 handle
= ((struct cso_depth_stencil_alpha
*)
493 cso_hash_iter_data(iter
))->data
;
496 if (ctx
->depth_stencil
!= handle
) {
497 ctx
->depth_stencil
= handle
;
498 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
503 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
505 assert(!ctx
->depth_stencil_saved
);
506 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
509 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
511 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
512 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
513 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
,
514 ctx
->depth_stencil_saved
);
516 ctx
->depth_stencil_saved
= NULL
;
521 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
522 const struct pipe_rasterizer_state
*templ
)
524 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
525 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
526 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
529 (void*)templ
, key_size
);
532 if (cso_hash_iter_is_null(iter
)) {
533 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
535 return PIPE_ERROR_OUT_OF_MEMORY
;
537 memcpy(&cso
->state
, templ
, sizeof(*templ
));
538 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
540 (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
541 cso
->context
= ctx
->pipe
;
543 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
544 if (cso_hash_iter_is_null(iter
)) {
546 return PIPE_ERROR_OUT_OF_MEMORY
;
552 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
555 if (ctx
->rasterizer
!= handle
) {
556 ctx
->rasterizer
= handle
;
557 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
562 void cso_save_rasterizer(struct cso_context
*ctx
)
564 assert(!ctx
->rasterizer_saved
);
565 ctx
->rasterizer_saved
= ctx
->rasterizer
;
568 void cso_restore_rasterizer(struct cso_context
*ctx
)
570 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
571 ctx
->rasterizer
= ctx
->rasterizer_saved
;
572 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
574 ctx
->rasterizer_saved
= NULL
;
578 void cso_set_fragment_shader_handle(struct cso_context
*ctx
, void *handle
)
580 if (ctx
->fragment_shader
!= handle
) {
581 ctx
->fragment_shader
= handle
;
582 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
586 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
588 if (handle
== ctx
->fragment_shader
) {
589 /* unbind before deleting */
590 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
591 ctx
->fragment_shader
= NULL
;
593 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
596 void cso_save_fragment_shader(struct cso_context
*ctx
)
598 assert(!ctx
->fragment_shader_saved
);
599 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
602 void cso_restore_fragment_shader(struct cso_context
*ctx
)
604 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
605 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
606 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
608 ctx
->fragment_shader_saved
= NULL
;
612 void cso_set_vertex_shader_handle(struct cso_context
*ctx
, void *handle
)
614 if (ctx
->vertex_shader
!= handle
) {
615 ctx
->vertex_shader
= handle
;
616 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
620 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
622 if (handle
== ctx
->vertex_shader
) {
623 /* unbind before deleting */
624 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
625 ctx
->vertex_shader
= NULL
;
627 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
630 void cso_save_vertex_shader(struct cso_context
*ctx
)
632 assert(!ctx
->vertex_shader_saved
);
633 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
636 void cso_restore_vertex_shader(struct cso_context
*ctx
)
638 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
639 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
640 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
642 ctx
->vertex_shader_saved
= NULL
;
646 void cso_set_framebuffer(struct cso_context
*ctx
,
647 const struct pipe_framebuffer_state
*fb
)
649 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
650 util_copy_framebuffer_state(&ctx
->fb
, fb
);
651 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
655 void cso_save_framebuffer(struct cso_context
*ctx
)
657 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
660 void cso_restore_framebuffer(struct cso_context
*ctx
)
662 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
663 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
664 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
665 util_unreference_framebuffer_state(&ctx
->fb_saved
);
670 void cso_set_viewport(struct cso_context
*ctx
,
671 const struct pipe_viewport_state
*vp
)
673 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
675 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, vp
);
679 void cso_save_viewport(struct cso_context
*ctx
)
681 ctx
->vp_saved
= ctx
->vp
;
685 void cso_restore_viewport(struct cso_context
*ctx
)
687 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
688 ctx
->vp
= ctx
->vp_saved
;
689 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, &ctx
->vp
);
694 void cso_set_blend_color(struct cso_context
*ctx
,
695 const struct pipe_blend_color
*bc
)
697 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
698 ctx
->blend_color
= *bc
;
699 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
703 void cso_set_sample_mask(struct cso_context
*ctx
, unsigned sample_mask
)
705 if (ctx
->sample_mask
!= sample_mask
) {
706 ctx
->sample_mask
= sample_mask
;
707 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
711 void cso_save_sample_mask(struct cso_context
*ctx
)
713 ctx
->sample_mask_saved
= ctx
->sample_mask
;
716 void cso_restore_sample_mask(struct cso_context
*ctx
)
718 cso_set_sample_mask(ctx
, ctx
->sample_mask_saved
);
721 void cso_set_min_samples(struct cso_context
*ctx
, unsigned min_samples
)
723 if (ctx
->min_samples
!= min_samples
&& ctx
->pipe
->set_min_samples
) {
724 ctx
->min_samples
= min_samples
;
725 ctx
->pipe
->set_min_samples(ctx
->pipe
, min_samples
);
729 void cso_save_min_samples(struct cso_context
*ctx
)
731 ctx
->min_samples_saved
= ctx
->min_samples
;
734 void cso_restore_min_samples(struct cso_context
*ctx
)
736 cso_set_min_samples(ctx
, ctx
->min_samples_saved
);
739 void cso_set_stencil_ref(struct cso_context
*ctx
,
740 const struct pipe_stencil_ref
*sr
)
742 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
743 ctx
->stencil_ref
= *sr
;
744 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
748 void cso_save_stencil_ref(struct cso_context
*ctx
)
750 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
754 void cso_restore_stencil_ref(struct cso_context
*ctx
)
756 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
,
757 sizeof(ctx
->stencil_ref
))) {
758 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
759 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
763 void cso_set_render_condition(struct cso_context
*ctx
,
764 struct pipe_query
*query
,
765 boolean condition
, uint mode
)
767 struct pipe_context
*pipe
= ctx
->pipe
;
769 if (ctx
->render_condition
!= query
||
770 ctx
->render_condition_mode
!= mode
||
771 ctx
->render_condition_cond
!= condition
) {
772 pipe
->render_condition(pipe
, query
, condition
, mode
);
773 ctx
->render_condition
= query
;
774 ctx
->render_condition_cond
= condition
;
775 ctx
->render_condition_mode
= mode
;
779 void cso_save_render_condition(struct cso_context
*ctx
)
781 ctx
->render_condition_saved
= ctx
->render_condition
;
782 ctx
->render_condition_cond_saved
= ctx
->render_condition_cond
;
783 ctx
->render_condition_mode_saved
= ctx
->render_condition_mode
;
786 void cso_restore_render_condition(struct cso_context
*ctx
)
788 cso_set_render_condition(ctx
, ctx
->render_condition_saved
,
789 ctx
->render_condition_cond_saved
,
790 ctx
->render_condition_mode_saved
);
793 void cso_set_geometry_shader_handle(struct cso_context
*ctx
, void *handle
)
795 assert(ctx
->has_geometry_shader
|| !handle
);
797 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
798 ctx
->geometry_shader
= handle
;
799 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
803 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
805 if (handle
== ctx
->geometry_shader
) {
806 /* unbind before deleting */
807 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
808 ctx
->geometry_shader
= NULL
;
810 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
813 void cso_save_geometry_shader(struct cso_context
*ctx
)
815 if (!ctx
->has_geometry_shader
) {
819 assert(!ctx
->geometry_shader_saved
);
820 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
823 void cso_restore_geometry_shader(struct cso_context
*ctx
)
825 if (!ctx
->has_geometry_shader
) {
829 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
830 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
831 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
833 ctx
->geometry_shader_saved
= NULL
;
836 void cso_set_tessctrl_shader_handle(struct cso_context
*ctx
, void *handle
)
838 assert(ctx
->has_tessellation
|| !handle
);
840 if (ctx
->has_tessellation
&& ctx
->tessctrl_shader
!= handle
) {
841 ctx
->tessctrl_shader
= handle
;
842 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, handle
);
846 void cso_delete_tessctrl_shader(struct cso_context
*ctx
, void *handle
)
848 if (handle
== ctx
->tessctrl_shader
) {
849 /* unbind before deleting */
850 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, NULL
);
851 ctx
->tessctrl_shader
= NULL
;
853 ctx
->pipe
->delete_tcs_state(ctx
->pipe
, handle
);
856 void cso_save_tessctrl_shader(struct cso_context
*ctx
)
858 if (!ctx
->has_tessellation
) {
862 assert(!ctx
->tessctrl_shader_saved
);
863 ctx
->tessctrl_shader_saved
= ctx
->tessctrl_shader
;
866 void cso_restore_tessctrl_shader(struct cso_context
*ctx
)
868 if (!ctx
->has_tessellation
) {
872 if (ctx
->tessctrl_shader_saved
!= ctx
->tessctrl_shader
) {
873 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, ctx
->tessctrl_shader_saved
);
874 ctx
->tessctrl_shader
= ctx
->tessctrl_shader_saved
;
876 ctx
->tessctrl_shader_saved
= NULL
;
879 void cso_set_tesseval_shader_handle(struct cso_context
*ctx
, void *handle
)
881 assert(ctx
->has_tessellation
|| !handle
);
883 if (ctx
->has_tessellation
&& ctx
->tesseval_shader
!= handle
) {
884 ctx
->tesseval_shader
= handle
;
885 ctx
->pipe
->bind_tes_state(ctx
->pipe
, handle
);
889 void cso_delete_tesseval_shader(struct cso_context
*ctx
, void *handle
)
891 if (handle
== ctx
->tesseval_shader
) {
892 /* unbind before deleting */
893 ctx
->pipe
->bind_tes_state(ctx
->pipe
, NULL
);
894 ctx
->tesseval_shader
= NULL
;
896 ctx
->pipe
->delete_tes_state(ctx
->pipe
, handle
);
899 void cso_save_tesseval_shader(struct cso_context
*ctx
)
901 if (!ctx
->has_tessellation
) {
905 assert(!ctx
->tesseval_shader_saved
);
906 ctx
->tesseval_shader_saved
= ctx
->tesseval_shader
;
909 void cso_restore_tesseval_shader(struct cso_context
*ctx
)
911 if (!ctx
->has_tessellation
) {
915 if (ctx
->tesseval_shader_saved
!= ctx
->tesseval_shader
) {
916 ctx
->pipe
->bind_tes_state(ctx
->pipe
, ctx
->tesseval_shader_saved
);
917 ctx
->tesseval_shader
= ctx
->tesseval_shader_saved
;
919 ctx
->tesseval_shader_saved
= NULL
;
925 clip_state_cpy(struct pipe_clip_state
*dst
,
926 const struct pipe_clip_state
*src
)
928 memcpy(dst
->ucp
, src
->ucp
, sizeof(dst
->ucp
));
932 clip_state_cmp(const struct pipe_clip_state
*a
,
933 const struct pipe_clip_state
*b
)
935 return memcmp(a
->ucp
, b
->ucp
, sizeof(a
->ucp
));
939 cso_set_clip(struct cso_context
*ctx
,
940 const struct pipe_clip_state
*clip
)
942 if (clip_state_cmp(&ctx
->clip
, clip
)) {
943 clip_state_cpy(&ctx
->clip
, clip
);
944 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
949 cso_save_clip(struct cso_context
*ctx
)
951 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
955 cso_restore_clip(struct cso_context
*ctx
)
957 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
958 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
959 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
964 cso_set_vertex_elements(struct cso_context
*ctx
,
966 const struct pipe_vertex_element
*states
)
968 struct u_vbuf
*vbuf
= ctx
->vbuf
;
969 unsigned key_size
, hash_key
;
970 struct cso_hash_iter iter
;
972 struct cso_velems_state velems_state
;
975 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
979 /* Need to include the count into the stored state data too.
980 * Otherwise first few count pipe_vertex_elements could be identical
981 * even if count is different, and there's no guarantee the hash would
982 * be different in that case neither.
984 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
985 velems_state
.count
= count
;
986 memcpy(velems_state
.velems
, states
,
987 sizeof(struct pipe_vertex_element
) * count
);
988 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
989 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
,
990 (void*)&velems_state
, key_size
);
992 if (cso_hash_iter_is_null(iter
)) {
993 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
995 return PIPE_ERROR_OUT_OF_MEMORY
;
997 memcpy(&cso
->state
, &velems_state
, key_size
);
998 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
,
999 &cso
->state
.velems
[0]);
1001 (cso_state_callback
) ctx
->pipe
->delete_vertex_elements_state
;
1002 cso
->context
= ctx
->pipe
;
1004 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1005 if (cso_hash_iter_is_null(iter
)) {
1007 return PIPE_ERROR_OUT_OF_MEMORY
;
1013 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1016 if (ctx
->velements
!= handle
) {
1017 ctx
->velements
= handle
;
1018 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1023 void cso_save_vertex_elements(struct cso_context
*ctx
)
1025 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1028 u_vbuf_save_vertex_elements(vbuf
);
1032 assert(!ctx
->velements_saved
);
1033 ctx
->velements_saved
= ctx
->velements
;
1036 void cso_restore_vertex_elements(struct cso_context
*ctx
)
1038 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1041 u_vbuf_restore_vertex_elements(vbuf
);
1045 if (ctx
->velements
!= ctx
->velements_saved
) {
1046 ctx
->velements
= ctx
->velements_saved
;
1047 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1049 ctx
->velements_saved
= NULL
;
1052 /* vertex buffers */
1054 void cso_set_vertex_buffers(struct cso_context
*ctx
,
1055 unsigned start_slot
, unsigned count
,
1056 const struct pipe_vertex_buffer
*buffers
)
1058 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1061 u_vbuf_set_vertex_buffers(vbuf
, start_slot
, count
, buffers
);
1065 /* Save what's in the auxiliary slot, so that we can save and restore it
1067 if (start_slot
<= ctx
->aux_vertex_buffer_index
&&
1068 start_slot
+count
> ctx
->aux_vertex_buffer_index
) {
1070 const struct pipe_vertex_buffer
*vb
=
1071 buffers
+ (ctx
->aux_vertex_buffer_index
- start_slot
);
1073 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
,
1075 memcpy(&ctx
->aux_vertex_buffer_current
, vb
,
1076 sizeof(struct pipe_vertex_buffer
));
1079 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
,
1081 ctx
->aux_vertex_buffer_current
.user_buffer
= NULL
;
1085 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, start_slot
, count
, buffers
);
1088 void cso_save_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1090 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1093 u_vbuf_save_aux_vertex_buffer_slot(vbuf
);
1097 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
,
1098 ctx
->aux_vertex_buffer_current
.buffer
);
1099 memcpy(&ctx
->aux_vertex_buffer_saved
, &ctx
->aux_vertex_buffer_current
,
1100 sizeof(struct pipe_vertex_buffer
));
1103 void cso_restore_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1105 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1108 u_vbuf_restore_aux_vertex_buffer_slot(vbuf
);
1112 cso_set_vertex_buffers(ctx
, ctx
->aux_vertex_buffer_index
, 1,
1113 &ctx
->aux_vertex_buffer_saved
);
1114 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
, NULL
);
1117 unsigned cso_get_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1119 return ctx
->aux_vertex_buffer_index
;
1123 /**************** fragment/vertex sampler view state *************************/
1125 static enum pipe_error
1126 single_sampler(struct cso_context
*ctx
,
1127 struct sampler_info
*info
,
1129 const struct pipe_sampler_state
*templ
)
1131 void *handle
= NULL
;
1133 if (templ
!= NULL
) {
1134 unsigned key_size
= sizeof(struct pipe_sampler_state
);
1135 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
1136 struct cso_hash_iter iter
=
1137 cso_find_state_template(ctx
->cache
,
1138 hash_key
, CSO_SAMPLER
,
1139 (void *) templ
, key_size
);
1141 if (cso_hash_iter_is_null(iter
)) {
1142 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
1144 return PIPE_ERROR_OUT_OF_MEMORY
;
1146 memcpy(&cso
->state
, templ
, sizeof(*templ
));
1147 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
1149 (cso_state_callback
) ctx
->pipe
->delete_sampler_state
;
1150 cso
->context
= ctx
->pipe
;
1152 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
1153 if (cso_hash_iter_is_null(iter
)) {
1155 return PIPE_ERROR_OUT_OF_MEMORY
;
1161 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
1165 info
->samplers
[idx
] = handle
;
1171 cso_single_sampler(struct cso_context
*ctx
,
1172 unsigned shader_stage
,
1174 const struct pipe_sampler_state
*templ
)
1176 return single_sampler(ctx
, &ctx
->samplers
[shader_stage
], idx
, templ
);
1182 single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1184 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1187 /* find highest non-null sampler */
1188 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1189 if (info
->samplers
[i
- 1] != NULL
)
1193 info
->nr_samplers
= i
;
1195 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1196 memcmp(info
->hw
.samplers
,
1198 info
->nr_samplers
* sizeof(void *)) != 0)
1200 memcpy(info
->hw
.samplers
,
1202 info
->nr_samplers
* sizeof(void *));
1204 /* set remaining slots/pointers to null */
1205 for (i
= info
->nr_samplers
; i
< info
->hw
.nr_samplers
; i
++)
1206 info
->samplers
[i
] = NULL
;
1208 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, shader_stage
, 0,
1209 MAX2(info
->nr_samplers
,
1210 info
->hw
.nr_samplers
),
1213 info
->hw
.nr_samplers
= info
->nr_samplers
;
1218 cso_single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1220 single_sampler_done(ctx
, shader_stage
);
1225 * If the function encouters any errors it will return the
1226 * last one. Done to always try to set as many samplers
1230 cso_set_samplers(struct cso_context
*ctx
,
1231 unsigned shader_stage
,
1233 const struct pipe_sampler_state
**templates
)
1235 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1237 enum pipe_error temp
, error
= PIPE_OK
;
1242 for (i
= 0; i
< nr
; i
++) {
1243 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1244 if (temp
!= PIPE_OK
)
1248 for ( ; i
< info
->nr_samplers
; i
++) {
1249 temp
= single_sampler(ctx
, info
, i
, NULL
);
1250 if (temp
!= PIPE_OK
)
1254 single_sampler_done(ctx
, shader_stage
);
1260 cso_save_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1262 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1263 info
->nr_samplers_saved
= info
->nr_samplers
;
1264 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1269 cso_restore_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1271 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1272 info
->nr_samplers
= info
->nr_samplers_saved
;
1273 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1274 single_sampler_done(ctx
, shader_stage
);
1279 cso_set_sampler_views(struct cso_context
*ctx
,
1280 unsigned shader_stage
,
1282 struct pipe_sampler_view
**views
)
1284 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1286 boolean any_change
= FALSE
;
1288 /* reference new views */
1289 for (i
= 0; i
< count
; i
++) {
1290 any_change
|= info
->views
[i
] != views
[i
];
1291 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1293 /* unref extra old views, if any */
1294 for (; i
< info
->nr_views
; i
++) {
1295 any_change
|= info
->views
[i
] != NULL
;
1296 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1299 /* bind the new sampler views */
1301 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0,
1302 MAX2(info
->nr_views
, count
),
1306 info
->nr_views
= count
;
1311 cso_save_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1313 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1316 info
->nr_views_saved
= info
->nr_views
;
1318 for (i
= 0; i
< info
->nr_views
; i
++) {
1319 assert(!info
->views_saved
[i
]);
1320 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1326 cso_restore_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1328 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1329 unsigned i
, nr_saved
= info
->nr_views_saved
;
1332 for (i
= 0; i
< nr_saved
; i
++) {
1333 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1334 /* move the reference from one pointer to another */
1335 info
->views
[i
] = info
->views_saved
[i
];
1336 info
->views_saved
[i
] = NULL
;
1338 for (; i
< info
->nr_views
; i
++) {
1339 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1342 num
= MAX2(info
->nr_views
, nr_saved
);
1344 /* bind the old/saved sampler views */
1345 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0, num
, info
->views
);
1347 info
->nr_views
= nr_saved
;
1348 info
->nr_views_saved
= 0;
1353 cso_set_stream_outputs(struct cso_context
*ctx
,
1354 unsigned num_targets
,
1355 struct pipe_stream_output_target
**targets
,
1356 const unsigned *offsets
)
1358 struct pipe_context
*pipe
= ctx
->pipe
;
1361 if (!ctx
->has_streamout
) {
1362 assert(num_targets
== 0);
1366 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1367 /* Nothing to do. */
1371 /* reference new targets */
1372 for (i
= 0; i
< num_targets
; i
++) {
1373 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1375 /* unref extra old targets, if any */
1376 for (; i
< ctx
->nr_so_targets
; i
++) {
1377 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1380 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1382 ctx
->nr_so_targets
= num_targets
;
1386 cso_save_stream_outputs(struct cso_context
*ctx
)
1390 if (!ctx
->has_streamout
) {
1394 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1396 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1397 assert(!ctx
->so_targets_saved
[i
]);
1398 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1403 cso_restore_stream_outputs(struct cso_context
*ctx
)
1405 struct pipe_context
*pipe
= ctx
->pipe
;
1407 unsigned offset
[PIPE_MAX_SO_BUFFERS
];
1409 if (!ctx
->has_streamout
) {
1413 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1414 /* Nothing to do. */
1418 assert(ctx
->nr_so_targets_saved
<= PIPE_MAX_SO_BUFFERS
);
1419 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1420 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1421 /* move the reference from one pointer to another */
1422 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1423 ctx
->so_targets_saved
[i
] = NULL
;
1424 /* -1 means append */
1425 offset
[i
] = (unsigned)-1;
1427 for (; i
< ctx
->nr_so_targets
; i
++) {
1428 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1431 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1432 ctx
->so_targets
, offset
);
1434 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1435 ctx
->nr_so_targets_saved
= 0;
1438 /* constant buffers */
1441 cso_set_constant_buffer(struct cso_context
*cso
, unsigned shader_stage
,
1442 unsigned index
, struct pipe_constant_buffer
*cb
)
1444 struct pipe_context
*pipe
= cso
->pipe
;
1446 pipe
->set_constant_buffer(pipe
, shader_stage
, index
, cb
);
1449 util_copy_constant_buffer(&cso
->aux_constbuf_current
[shader_stage
], cb
);
1454 cso_set_constant_buffer_resource(struct cso_context
*cso
,
1455 unsigned shader_stage
,
1457 struct pipe_resource
*buffer
)
1460 struct pipe_constant_buffer cb
;
1462 cb
.buffer_offset
= 0;
1463 cb
.buffer_size
= buffer
->width0
;
1464 cb
.user_buffer
= NULL
;
1465 cso_set_constant_buffer(cso
, shader_stage
, index
, &cb
);
1467 cso_set_constant_buffer(cso
, shader_stage
, index
, NULL
);
1472 cso_save_constant_buffer_slot0(struct cso_context
*cso
,
1473 unsigned shader_stage
)
1475 util_copy_constant_buffer(&cso
->aux_constbuf_saved
[shader_stage
],
1476 &cso
->aux_constbuf_current
[shader_stage
]);
1480 cso_restore_constant_buffer_slot0(struct cso_context
*cso
,
1481 unsigned shader_stage
)
1483 cso_set_constant_buffer(cso
, shader_stage
, 0,
1484 &cso
->aux_constbuf_saved
[shader_stage
]);
1485 pipe_resource_reference(&cso
->aux_constbuf_saved
[shader_stage
].buffer
,
1492 cso_set_index_buffer(struct cso_context
*cso
,
1493 const struct pipe_index_buffer
*ib
)
1495 struct u_vbuf
*vbuf
= cso
->vbuf
;
1498 u_vbuf_set_index_buffer(vbuf
, ib
);
1500 struct pipe_context
*pipe
= cso
->pipe
;
1501 pipe
->set_index_buffer(pipe
, ib
);
1506 cso_draw_vbo(struct cso_context
*cso
,
1507 const struct pipe_draw_info
*info
)
1509 struct u_vbuf
*vbuf
= cso
->vbuf
;
1512 u_vbuf_draw_vbo(vbuf
, info
);
1514 struct pipe_context
*pipe
= cso
->pipe
;
1515 pipe
->draw_vbo(pipe
, info
);
1520 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1522 struct pipe_draw_info info
;
1524 util_draw_init_info(&info
);
1529 info
.min_index
= start
;
1530 info
.max_index
= start
+ count
- 1;
1532 cso_draw_vbo(cso
, &info
);
1536 cso_draw_arrays_instanced(struct cso_context
*cso
, uint mode
,
1537 uint start
, uint count
,
1538 uint start_instance
, uint instance_count
)
1540 struct pipe_draw_info info
;
1542 util_draw_init_info(&info
);
1547 info
.min_index
= start
;
1548 info
.max_index
= start
+ count
- 1;
1549 info
.start_instance
= start_instance
;
1550 info
.instance_count
= instance_count
;
1552 cso_draw_vbo(cso
, &info
);