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 * Per-shader sampler information.
58 void *samplers
[PIPE_MAX_SAMPLERS
];
65 struct pipe_context
*pipe
;
66 struct cso_cache
*cache
;
69 boolean has_geometry_shader
;
70 boolean has_tessellation
;
71 boolean has_compute_shader
;
72 boolean has_streamout
;
74 unsigned saved_state
; /**< bitmask of CSO_BIT_x flags */
76 struct pipe_sampler_view
*fragment_views
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
77 unsigned nr_fragment_views
;
79 struct pipe_sampler_view
*fragment_views_saved
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
80 unsigned nr_fragment_views_saved
;
82 void *fragment_samplers_saved
[PIPE_MAX_SAMPLERS
];
83 unsigned nr_fragment_samplers_saved
;
85 struct sampler_info samplers
[PIPE_SHADER_TYPES
];
87 struct pipe_vertex_buffer aux_vertex_buffer_current
;
88 struct pipe_vertex_buffer aux_vertex_buffer_saved
;
89 unsigned aux_vertex_buffer_index
;
91 struct pipe_constant_buffer aux_constbuf_current
[PIPE_SHADER_TYPES
];
92 struct pipe_constant_buffer aux_constbuf_saved
[PIPE_SHADER_TYPES
];
94 struct pipe_image_view fragment_image0_current
;
95 struct pipe_image_view fragment_image0_saved
;
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 *compute_shader
;
115 void *velements
, *velements_saved
;
116 struct pipe_query
*render_condition
, *render_condition_saved
;
117 uint render_condition_mode
, render_condition_mode_saved
;
118 boolean render_condition_cond
, render_condition_cond_saved
;
120 struct pipe_framebuffer_state fb
, fb_saved
;
121 struct pipe_viewport_state vp
, vp_saved
;
122 struct pipe_blend_color blend_color
;
123 unsigned sample_mask
, sample_mask_saved
;
124 unsigned min_samples
, min_samples_saved
;
125 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
129 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
131 struct cso_blend
*cso
= (struct cso_blend
*)state
;
133 if (ctx
->blend
== cso
->data
)
136 if (cso
->delete_state
)
137 cso
->delete_state(cso
->context
, cso
->data
);
142 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
144 struct cso_depth_stencil_alpha
*cso
=
145 (struct cso_depth_stencil_alpha
*)state
;
147 if (ctx
->depth_stencil
== cso
->data
)
150 if (cso
->delete_state
)
151 cso
->delete_state(cso
->context
, cso
->data
);
157 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
159 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
160 if (cso
->delete_state
)
161 cso
->delete_state(cso
->context
, cso
->data
);
166 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
168 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
170 if (ctx
->rasterizer
== cso
->data
)
172 if (cso
->delete_state
)
173 cso
->delete_state(cso
->context
, cso
->data
);
178 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
181 struct cso_velements
*cso
= (struct cso_velements
*)state
;
183 if (ctx
->velements
== cso
->data
)
186 if (cso
->delete_state
)
187 cso
->delete_state(cso
->context
, cso
->data
);
193 static inline boolean
delete_cso(struct cso_context
*ctx
,
194 void *state
, enum cso_cache_type type
)
198 return delete_blend_state(ctx
, state
);
200 return delete_sampler_state(ctx
, state
);
201 case CSO_DEPTH_STENCIL_ALPHA
:
202 return delete_depth_stencil_state(ctx
, state
);
204 return delete_rasterizer_state(ctx
, state
);
206 return delete_vertex_elements(ctx
, state
);
215 sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
216 int max_size
, void *user_data
)
218 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
219 /* if we're approach the maximum size, remove fourth of the entries
220 * otherwise every subsequent call will go through the same */
221 int hash_size
= cso_hash_size(hash
);
222 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
223 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
224 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
225 if (hash_size
> max_size
)
226 to_remove
+= hash_size
- max_size
;
228 /*remove elements until we're good */
229 /*fixme: currently we pick the nodes to remove at random*/
230 void *cso
= cso_hash_iter_data(iter
);
231 if (delete_cso(ctx
, cso
, type
)) {
232 iter
= cso_hash_erase(hash
, iter
);
235 iter
= cso_hash_iter_next(iter
);
239 static void cso_init_vbuf(struct cso_context
*cso
)
241 struct u_vbuf_caps caps
;
243 /* Install u_vbuf if there is anything unsupported. */
244 if (u_vbuf_get_caps(cso
->pipe
->screen
, &caps
)) {
245 cso
->vbuf
= u_vbuf_create(cso
->pipe
, &caps
,
246 cso
->aux_vertex_buffer_index
);
250 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
252 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
256 ctx
->cache
= cso_cache_create();
257 if (ctx
->cache
== NULL
)
259 cso_cache_set_sanitize_callback(ctx
->cache
,
264 ctx
->sample_mask
= ~0;
266 ctx
->aux_vertex_buffer_index
= 0; /* 0 for now */
270 /* Enable for testing: */
271 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
273 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
274 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
275 ctx
->has_geometry_shader
= TRUE
;
277 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
278 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
279 ctx
->has_tessellation
= TRUE
;
281 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_COMPUTE
,
282 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
284 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_COMPUTE
,
285 PIPE_SHADER_CAP_SUPPORTED_IRS
);
286 if (supported_irs
& (1 << PIPE_SHADER_IR_TGSI
)) {
287 ctx
->has_compute_shader
= TRUE
;
290 if (pipe
->screen
->get_param(pipe
->screen
,
291 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
292 ctx
->has_streamout
= TRUE
;
298 cso_destroy_context( ctx
);
303 * Free the CSO context.
305 void cso_destroy_context( struct cso_context
*ctx
)
310 ctx
->pipe
->set_index_buffer(ctx
->pipe
, NULL
);
312 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
313 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
316 static struct pipe_sampler_view
*views
[PIPE_MAX_SHADER_SAMPLER_VIEWS
] = { NULL
};
317 static void *zeros
[PIPE_MAX_SAMPLERS
] = { NULL
};
318 struct pipe_screen
*scr
= ctx
->pipe
->screen
;
319 enum pipe_shader_type sh
;
320 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
321 int maxsam
= scr
->get_shader_param(scr
, sh
,
322 PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS
);
323 int maxview
= scr
->get_shader_param(scr
, sh
,
324 PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS
);
325 assert(maxsam
<= PIPE_MAX_SAMPLERS
);
326 assert(maxview
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
328 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, sh
, 0, maxsam
, zeros
);
331 ctx
->pipe
->set_sampler_views(ctx
->pipe
, sh
, 0, maxview
, views
);
336 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
337 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
338 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_FRAGMENT
, 0, NULL
);
339 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
340 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_VERTEX
, 0, NULL
);
341 if (ctx
->has_geometry_shader
) {
342 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
343 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_GEOMETRY
, 0, NULL
);
345 if (ctx
->has_tessellation
) {
346 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, NULL
);
347 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_TESS_CTRL
, 0, NULL
);
348 ctx
->pipe
->bind_tes_state(ctx
->pipe
, NULL
);
349 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_TESS_EVAL
, 0, NULL
);
351 if (ctx
->has_compute_shader
) {
352 ctx
->pipe
->bind_compute_state(ctx
->pipe
, NULL
);
353 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_COMPUTE
, 0, NULL
);
355 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
357 if (ctx
->has_streamout
)
358 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, NULL
);
361 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
362 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
363 pipe_sampler_view_reference(&ctx
->fragment_views_saved
[i
], NULL
);
366 util_unreference_framebuffer_state(&ctx
->fb
);
367 util_unreference_framebuffer_state(&ctx
->fb_saved
);
369 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
, NULL
);
370 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
, NULL
);
372 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
373 pipe_resource_reference(&ctx
->aux_constbuf_current
[i
].buffer
, NULL
);
374 pipe_resource_reference(&ctx
->aux_constbuf_saved
[i
].buffer
, NULL
);
377 pipe_resource_reference(&ctx
->fragment_image0_current
.resource
, NULL
);
378 pipe_resource_reference(&ctx
->fragment_image0_saved
.resource
, NULL
);
380 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
381 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
382 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
386 cso_cache_delete( ctx
->cache
);
391 u_vbuf_destroy(ctx
->vbuf
);
396 /* Those function will either find the state of the given template
397 * in the cache or they will create a new state from the given
398 * template, insert it in the cache and return it.
402 * If the driver returns 0 from the create method then they will assign
403 * the data member of the cso to be the template itself.
406 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
407 const struct pipe_blend_state
*templ
)
409 unsigned key_size
, hash_key
;
410 struct cso_hash_iter iter
;
413 key_size
= templ
->independent_blend_enable
?
414 sizeof(struct pipe_blend_state
) :
415 (char *)&(templ
->rt
[1]) - (char *)templ
;
416 hash_key
= cso_construct_key((void*)templ
, key_size
);
417 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
,
418 (void*)templ
, key_size
);
420 if (cso_hash_iter_is_null(iter
)) {
421 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
423 return PIPE_ERROR_OUT_OF_MEMORY
;
425 memset(&cso
->state
, 0, sizeof cso
->state
);
426 memcpy(&cso
->state
, templ
, key_size
);
427 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
428 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
429 cso
->context
= ctx
->pipe
;
431 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
432 if (cso_hash_iter_is_null(iter
)) {
434 return PIPE_ERROR_OUT_OF_MEMORY
;
440 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
443 if (ctx
->blend
!= handle
) {
445 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
451 cso_save_blend(struct cso_context
*ctx
)
453 assert(!ctx
->blend_saved
);
454 ctx
->blend_saved
= ctx
->blend
;
458 cso_restore_blend(struct cso_context
*ctx
)
460 if (ctx
->blend
!= ctx
->blend_saved
) {
461 ctx
->blend
= ctx
->blend_saved
;
462 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
464 ctx
->blend_saved
= NULL
;
470 cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
471 const struct pipe_depth_stencil_alpha_state
*templ
)
473 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
474 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
475 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
477 CSO_DEPTH_STENCIL_ALPHA
,
478 (void*)templ
, key_size
);
481 if (cso_hash_iter_is_null(iter
)) {
482 struct cso_depth_stencil_alpha
*cso
=
483 MALLOC(sizeof(struct cso_depth_stencil_alpha
));
485 return PIPE_ERROR_OUT_OF_MEMORY
;
487 memcpy(&cso
->state
, templ
, sizeof(*templ
));
488 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
,
491 (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
492 cso
->context
= ctx
->pipe
;
494 iter
= cso_insert_state(ctx
->cache
, hash_key
,
495 CSO_DEPTH_STENCIL_ALPHA
, cso
);
496 if (cso_hash_iter_is_null(iter
)) {
498 return PIPE_ERROR_OUT_OF_MEMORY
;
504 handle
= ((struct cso_depth_stencil_alpha
*)
505 cso_hash_iter_data(iter
))->data
;
508 if (ctx
->depth_stencil
!= handle
) {
509 ctx
->depth_stencil
= handle
;
510 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
516 cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
518 assert(!ctx
->depth_stencil_saved
);
519 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
523 cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
525 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
526 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
527 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
,
528 ctx
->depth_stencil_saved
);
530 ctx
->depth_stencil_saved
= NULL
;
535 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
536 const struct pipe_rasterizer_state
*templ
)
538 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
539 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
540 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
543 (void*)templ
, key_size
);
546 if (cso_hash_iter_is_null(iter
)) {
547 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
549 return PIPE_ERROR_OUT_OF_MEMORY
;
551 memcpy(&cso
->state
, templ
, sizeof(*templ
));
552 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
554 (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
555 cso
->context
= ctx
->pipe
;
557 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
558 if (cso_hash_iter_is_null(iter
)) {
560 return PIPE_ERROR_OUT_OF_MEMORY
;
566 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
569 if (ctx
->rasterizer
!= handle
) {
570 ctx
->rasterizer
= handle
;
571 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
577 cso_save_rasterizer(struct cso_context
*ctx
)
579 assert(!ctx
->rasterizer_saved
);
580 ctx
->rasterizer_saved
= ctx
->rasterizer
;
584 cso_restore_rasterizer(struct cso_context
*ctx
)
586 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
587 ctx
->rasterizer
= ctx
->rasterizer_saved
;
588 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
590 ctx
->rasterizer_saved
= NULL
;
594 void cso_set_fragment_shader_handle(struct cso_context
*ctx
, void *handle
)
596 if (ctx
->fragment_shader
!= handle
) {
597 ctx
->fragment_shader
= handle
;
598 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
602 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
604 if (handle
== ctx
->fragment_shader
) {
605 /* unbind before deleting */
606 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
607 ctx
->fragment_shader
= NULL
;
609 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
613 cso_save_fragment_shader(struct cso_context
*ctx
)
615 assert(!ctx
->fragment_shader_saved
);
616 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
620 cso_restore_fragment_shader(struct cso_context
*ctx
)
622 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
623 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
624 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
626 ctx
->fragment_shader_saved
= NULL
;
630 void cso_set_vertex_shader_handle(struct cso_context
*ctx
, void *handle
)
632 if (ctx
->vertex_shader
!= handle
) {
633 ctx
->vertex_shader
= handle
;
634 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
638 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
640 if (handle
== ctx
->vertex_shader
) {
641 /* unbind before deleting */
642 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
643 ctx
->vertex_shader
= NULL
;
645 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
649 cso_save_vertex_shader(struct cso_context
*ctx
)
651 assert(!ctx
->vertex_shader_saved
);
652 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
656 cso_restore_vertex_shader(struct cso_context
*ctx
)
658 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
659 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
660 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
662 ctx
->vertex_shader_saved
= NULL
;
666 void cso_set_framebuffer(struct cso_context
*ctx
,
667 const struct pipe_framebuffer_state
*fb
)
669 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
670 util_copy_framebuffer_state(&ctx
->fb
, fb
);
671 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
676 cso_save_framebuffer(struct cso_context
*ctx
)
678 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
682 cso_restore_framebuffer(struct cso_context
*ctx
)
684 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
685 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
686 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
687 util_unreference_framebuffer_state(&ctx
->fb_saved
);
692 void cso_set_viewport(struct cso_context
*ctx
,
693 const struct pipe_viewport_state
*vp
)
695 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
697 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, vp
);
702 * Setup viewport state for given width and height (position is always (0,0)).
703 * Invert the Y axis if 'invert' is true.
706 cso_set_viewport_dims(struct cso_context
*ctx
,
707 float width
, float height
, boolean invert
)
709 struct pipe_viewport_state vp
;
710 vp
.scale
[0] = width
* 0.5f
;
711 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
713 vp
.translate
[0] = 0.5f
* width
;
714 vp
.translate
[1] = 0.5f
* height
;
715 vp
.translate
[2] = 0.5f
;
716 cso_set_viewport(ctx
, &vp
);
720 cso_save_viewport(struct cso_context
*ctx
)
722 ctx
->vp_saved
= ctx
->vp
;
727 cso_restore_viewport(struct cso_context
*ctx
)
729 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
730 ctx
->vp
= ctx
->vp_saved
;
731 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, &ctx
->vp
);
736 void cso_set_blend_color(struct cso_context
*ctx
,
737 const struct pipe_blend_color
*bc
)
739 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
740 ctx
->blend_color
= *bc
;
741 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
745 void cso_set_sample_mask(struct cso_context
*ctx
, unsigned sample_mask
)
747 if (ctx
->sample_mask
!= sample_mask
) {
748 ctx
->sample_mask
= sample_mask
;
749 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
754 cso_save_sample_mask(struct cso_context
*ctx
)
756 ctx
->sample_mask_saved
= ctx
->sample_mask
;
760 cso_restore_sample_mask(struct cso_context
*ctx
)
762 cso_set_sample_mask(ctx
, ctx
->sample_mask_saved
);
765 void cso_set_min_samples(struct cso_context
*ctx
, unsigned min_samples
)
767 if (ctx
->min_samples
!= min_samples
&& ctx
->pipe
->set_min_samples
) {
768 ctx
->min_samples
= min_samples
;
769 ctx
->pipe
->set_min_samples(ctx
->pipe
, min_samples
);
774 cso_save_min_samples(struct cso_context
*ctx
)
776 ctx
->min_samples_saved
= ctx
->min_samples
;
780 cso_restore_min_samples(struct cso_context
*ctx
)
782 cso_set_min_samples(ctx
, ctx
->min_samples_saved
);
785 void cso_set_stencil_ref(struct cso_context
*ctx
,
786 const struct pipe_stencil_ref
*sr
)
788 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
789 ctx
->stencil_ref
= *sr
;
790 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
795 cso_save_stencil_ref(struct cso_context
*ctx
)
797 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
802 cso_restore_stencil_ref(struct cso_context
*ctx
)
804 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
,
805 sizeof(ctx
->stencil_ref
))) {
806 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
807 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
811 void cso_set_render_condition(struct cso_context
*ctx
,
812 struct pipe_query
*query
,
813 boolean condition
, uint mode
)
815 struct pipe_context
*pipe
= ctx
->pipe
;
817 if (ctx
->render_condition
!= query
||
818 ctx
->render_condition_mode
!= mode
||
819 ctx
->render_condition_cond
!= condition
) {
820 pipe
->render_condition(pipe
, query
, condition
, mode
);
821 ctx
->render_condition
= query
;
822 ctx
->render_condition_cond
= condition
;
823 ctx
->render_condition_mode
= mode
;
828 cso_save_render_condition(struct cso_context
*ctx
)
830 ctx
->render_condition_saved
= ctx
->render_condition
;
831 ctx
->render_condition_cond_saved
= ctx
->render_condition_cond
;
832 ctx
->render_condition_mode_saved
= ctx
->render_condition_mode
;
836 cso_restore_render_condition(struct cso_context
*ctx
)
838 cso_set_render_condition(ctx
, ctx
->render_condition_saved
,
839 ctx
->render_condition_cond_saved
,
840 ctx
->render_condition_mode_saved
);
843 void cso_set_geometry_shader_handle(struct cso_context
*ctx
, void *handle
)
845 assert(ctx
->has_geometry_shader
|| !handle
);
847 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
848 ctx
->geometry_shader
= handle
;
849 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
853 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
855 if (handle
== ctx
->geometry_shader
) {
856 /* unbind before deleting */
857 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
858 ctx
->geometry_shader
= NULL
;
860 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
864 cso_save_geometry_shader(struct cso_context
*ctx
)
866 if (!ctx
->has_geometry_shader
) {
870 assert(!ctx
->geometry_shader_saved
);
871 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
875 cso_restore_geometry_shader(struct cso_context
*ctx
)
877 if (!ctx
->has_geometry_shader
) {
881 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
882 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
883 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
885 ctx
->geometry_shader_saved
= NULL
;
888 void cso_set_tessctrl_shader_handle(struct cso_context
*ctx
, void *handle
)
890 assert(ctx
->has_tessellation
|| !handle
);
892 if (ctx
->has_tessellation
&& ctx
->tessctrl_shader
!= handle
) {
893 ctx
->tessctrl_shader
= handle
;
894 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, handle
);
898 void cso_delete_tessctrl_shader(struct cso_context
*ctx
, void *handle
)
900 if (handle
== ctx
->tessctrl_shader
) {
901 /* unbind before deleting */
902 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, NULL
);
903 ctx
->tessctrl_shader
= NULL
;
905 ctx
->pipe
->delete_tcs_state(ctx
->pipe
, handle
);
909 cso_save_tessctrl_shader(struct cso_context
*ctx
)
911 if (!ctx
->has_tessellation
) {
915 assert(!ctx
->tessctrl_shader_saved
);
916 ctx
->tessctrl_shader_saved
= ctx
->tessctrl_shader
;
920 cso_restore_tessctrl_shader(struct cso_context
*ctx
)
922 if (!ctx
->has_tessellation
) {
926 if (ctx
->tessctrl_shader_saved
!= ctx
->tessctrl_shader
) {
927 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, ctx
->tessctrl_shader_saved
);
928 ctx
->tessctrl_shader
= ctx
->tessctrl_shader_saved
;
930 ctx
->tessctrl_shader_saved
= NULL
;
933 void cso_set_tesseval_shader_handle(struct cso_context
*ctx
, void *handle
)
935 assert(ctx
->has_tessellation
|| !handle
);
937 if (ctx
->has_tessellation
&& ctx
->tesseval_shader
!= handle
) {
938 ctx
->tesseval_shader
= handle
;
939 ctx
->pipe
->bind_tes_state(ctx
->pipe
, handle
);
943 void cso_delete_tesseval_shader(struct cso_context
*ctx
, void *handle
)
945 if (handle
== ctx
->tesseval_shader
) {
946 /* unbind before deleting */
947 ctx
->pipe
->bind_tes_state(ctx
->pipe
, NULL
);
948 ctx
->tesseval_shader
= NULL
;
950 ctx
->pipe
->delete_tes_state(ctx
->pipe
, handle
);
954 cso_save_tesseval_shader(struct cso_context
*ctx
)
956 if (!ctx
->has_tessellation
) {
960 assert(!ctx
->tesseval_shader_saved
);
961 ctx
->tesseval_shader_saved
= ctx
->tesseval_shader
;
965 cso_restore_tesseval_shader(struct cso_context
*ctx
)
967 if (!ctx
->has_tessellation
) {
971 if (ctx
->tesseval_shader_saved
!= ctx
->tesseval_shader
) {
972 ctx
->pipe
->bind_tes_state(ctx
->pipe
, ctx
->tesseval_shader_saved
);
973 ctx
->tesseval_shader
= ctx
->tesseval_shader_saved
;
975 ctx
->tesseval_shader_saved
= NULL
;
978 void cso_set_compute_shader_handle(struct cso_context
*ctx
, void *handle
)
980 assert(ctx
->has_compute_shader
|| !handle
);
982 if (ctx
->has_compute_shader
&& ctx
->compute_shader
!= handle
) {
983 ctx
->compute_shader
= handle
;
984 ctx
->pipe
->bind_compute_state(ctx
->pipe
, handle
);
988 void cso_delete_compute_shader(struct cso_context
*ctx
, void *handle
)
990 if (handle
== ctx
->compute_shader
) {
991 /* unbind before deleting */
992 ctx
->pipe
->bind_compute_state(ctx
->pipe
, NULL
);
993 ctx
->compute_shader
= NULL
;
995 ctx
->pipe
->delete_compute_state(ctx
->pipe
, handle
);
999 cso_set_vertex_elements(struct cso_context
*ctx
,
1001 const struct pipe_vertex_element
*states
)
1003 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1004 unsigned key_size
, hash_key
;
1005 struct cso_hash_iter iter
;
1007 struct cso_velems_state velems_state
;
1010 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
1014 /* Need to include the count into the stored state data too.
1015 * Otherwise first few count pipe_vertex_elements could be identical
1016 * even if count is different, and there's no guarantee the hash would
1017 * be different in that case neither.
1019 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
1020 velems_state
.count
= count
;
1021 memcpy(velems_state
.velems
, states
,
1022 sizeof(struct pipe_vertex_element
) * count
);
1023 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
1024 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
,
1025 (void*)&velems_state
, key_size
);
1027 if (cso_hash_iter_is_null(iter
)) {
1028 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1030 return PIPE_ERROR_OUT_OF_MEMORY
;
1032 memcpy(&cso
->state
, &velems_state
, key_size
);
1033 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
,
1034 &cso
->state
.velems
[0]);
1036 (cso_state_callback
) ctx
->pipe
->delete_vertex_elements_state
;
1037 cso
->context
= ctx
->pipe
;
1039 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1040 if (cso_hash_iter_is_null(iter
)) {
1042 return PIPE_ERROR_OUT_OF_MEMORY
;
1048 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1051 if (ctx
->velements
!= handle
) {
1052 ctx
->velements
= handle
;
1053 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1059 cso_save_vertex_elements(struct cso_context
*ctx
)
1061 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1064 u_vbuf_save_vertex_elements(vbuf
);
1068 assert(!ctx
->velements_saved
);
1069 ctx
->velements_saved
= ctx
->velements
;
1073 cso_restore_vertex_elements(struct cso_context
*ctx
)
1075 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1078 u_vbuf_restore_vertex_elements(vbuf
);
1082 if (ctx
->velements
!= ctx
->velements_saved
) {
1083 ctx
->velements
= ctx
->velements_saved
;
1084 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1086 ctx
->velements_saved
= NULL
;
1089 /* vertex buffers */
1091 void cso_set_vertex_buffers(struct cso_context
*ctx
,
1092 unsigned start_slot
, unsigned count
,
1093 const struct pipe_vertex_buffer
*buffers
)
1095 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1098 u_vbuf_set_vertex_buffers(vbuf
, start_slot
, count
, buffers
);
1102 /* Save what's in the auxiliary slot, so that we can save and restore it
1104 if (start_slot
<= ctx
->aux_vertex_buffer_index
&&
1105 start_slot
+count
> ctx
->aux_vertex_buffer_index
) {
1107 const struct pipe_vertex_buffer
*vb
=
1108 buffers
+ (ctx
->aux_vertex_buffer_index
- start_slot
);
1110 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
,
1112 memcpy(&ctx
->aux_vertex_buffer_current
, vb
,
1113 sizeof(struct pipe_vertex_buffer
));
1116 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
,
1118 ctx
->aux_vertex_buffer_current
.user_buffer
= NULL
;
1122 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, start_slot
, count
, buffers
);
1126 cso_save_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1128 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1131 u_vbuf_save_aux_vertex_buffer_slot(vbuf
);
1135 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
,
1136 ctx
->aux_vertex_buffer_current
.buffer
);
1137 memcpy(&ctx
->aux_vertex_buffer_saved
, &ctx
->aux_vertex_buffer_current
,
1138 sizeof(struct pipe_vertex_buffer
));
1142 cso_restore_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1144 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1147 u_vbuf_restore_aux_vertex_buffer_slot(vbuf
);
1151 cso_set_vertex_buffers(ctx
, ctx
->aux_vertex_buffer_index
, 1,
1152 &ctx
->aux_vertex_buffer_saved
);
1153 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
, NULL
);
1156 unsigned cso_get_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1158 return ctx
->aux_vertex_buffer_index
;
1164 cso_single_sampler(struct cso_context
*ctx
, unsigned shader_stage
,
1165 unsigned idx
, const struct pipe_sampler_state
*templ
)
1167 void *handle
= NULL
;
1170 unsigned key_size
= sizeof(struct pipe_sampler_state
);
1171 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
1172 struct cso_hash_iter iter
=
1173 cso_find_state_template(ctx
->cache
,
1174 hash_key
, CSO_SAMPLER
,
1175 (void *) templ
, key_size
);
1177 if (cso_hash_iter_is_null(iter
)) {
1178 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
1180 return PIPE_ERROR_OUT_OF_MEMORY
;
1182 memcpy(&cso
->state
, templ
, sizeof(*templ
));
1183 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
1185 (cso_state_callback
) ctx
->pipe
->delete_sampler_state
;
1186 cso
->context
= ctx
->pipe
;
1188 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
1189 if (cso_hash_iter_is_null(iter
)) {
1191 return PIPE_ERROR_OUT_OF_MEMORY
;
1197 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
1201 ctx
->samplers
[shader_stage
].samplers
[idx
] = handle
;
1207 * Send staged sampler state to the driver.
1210 cso_single_sampler_done(struct cso_context
*ctx
,
1211 enum pipe_shader_type shader_stage
)
1213 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1214 const unsigned old_nr_samplers
= info
->nr_samplers
;
1217 /* find highest non-null sampler */
1218 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1219 if (info
->samplers
[i
- 1] != NULL
)
1223 info
->nr_samplers
= i
;
1224 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, shader_stage
, 0,
1225 MAX2(old_nr_samplers
, info
->nr_samplers
),
1231 * If the function encouters any errors it will return the
1232 * last one. Done to always try to set as many samplers
1236 cso_set_samplers(struct cso_context
*ctx
,
1237 enum pipe_shader_type shader_stage
,
1239 const struct pipe_sampler_state
**templates
)
1241 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1243 enum pipe_error temp
, error
= PIPE_OK
;
1245 for (i
= 0; i
< nr
; i
++) {
1246 temp
= cso_single_sampler(ctx
, shader_stage
, i
, templates
[i
]);
1247 if (temp
!= PIPE_OK
)
1251 for ( ; i
< info
->nr_samplers
; i
++) {
1252 temp
= cso_single_sampler(ctx
, shader_stage
, i
, NULL
);
1253 if (temp
!= PIPE_OK
)
1257 cso_single_sampler_done(ctx
, shader_stage
);
1263 cso_save_fragment_samplers(struct cso_context
*ctx
)
1265 struct sampler_info
*info
= &ctx
->samplers
[PIPE_SHADER_FRAGMENT
];
1267 ctx
->nr_fragment_samplers_saved
= info
->nr_samplers
;
1268 memcpy(ctx
->fragment_samplers_saved
, info
->samplers
,
1269 sizeof(info
->samplers
));
1274 cso_restore_fragment_samplers(struct cso_context
*ctx
)
1276 struct sampler_info
*info
= &ctx
->samplers
[PIPE_SHADER_FRAGMENT
];
1278 info
->nr_samplers
= ctx
->nr_fragment_samplers_saved
;
1279 memcpy(info
->samplers
, ctx
->fragment_samplers_saved
,
1280 sizeof(info
->samplers
));
1281 cso_single_sampler_done(ctx
, PIPE_SHADER_FRAGMENT
);
1286 cso_set_sampler_views(struct cso_context
*ctx
,
1287 enum pipe_shader_type shader_stage
,
1289 struct pipe_sampler_view
**views
)
1291 if (shader_stage
== PIPE_SHADER_FRAGMENT
) {
1293 boolean any_change
= FALSE
;
1295 /* reference new views */
1296 for (i
= 0; i
< count
; i
++) {
1297 any_change
|= ctx
->fragment_views
[i
] != views
[i
];
1298 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], views
[i
]);
1300 /* unref extra old views, if any */
1301 for (; i
< ctx
->nr_fragment_views
; i
++) {
1302 any_change
|= ctx
->fragment_views
[i
] != NULL
;
1303 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
1306 /* bind the new sampler views */
1308 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0,
1309 MAX2(ctx
->nr_fragment_views
, count
),
1310 ctx
->fragment_views
);
1313 ctx
->nr_fragment_views
= count
;
1316 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0, count
, views
);
1321 cso_save_fragment_sampler_views(struct cso_context
*ctx
)
1325 ctx
->nr_fragment_views_saved
= ctx
->nr_fragment_views
;
1327 for (i
= 0; i
< ctx
->nr_fragment_views
; i
++) {
1328 assert(!ctx
->fragment_views_saved
[i
]);
1329 pipe_sampler_view_reference(&ctx
->fragment_views_saved
[i
],
1330 ctx
->fragment_views
[i
]);
1336 cso_restore_fragment_sampler_views(struct cso_context
*ctx
)
1338 unsigned i
, nr_saved
= ctx
->nr_fragment_views_saved
;
1341 for (i
= 0; i
< nr_saved
; i
++) {
1342 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
1343 /* move the reference from one pointer to another */
1344 ctx
->fragment_views
[i
] = ctx
->fragment_views_saved
[i
];
1345 ctx
->fragment_views_saved
[i
] = NULL
;
1347 for (; i
< ctx
->nr_fragment_views
; i
++) {
1348 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
1351 num
= MAX2(ctx
->nr_fragment_views
, nr_saved
);
1353 /* bind the old/saved sampler views */
1354 ctx
->pipe
->set_sampler_views(ctx
->pipe
, PIPE_SHADER_FRAGMENT
, 0, num
,
1355 ctx
->fragment_views
);
1357 ctx
->nr_fragment_views
= nr_saved
;
1358 ctx
->nr_fragment_views_saved
= 0;
1363 cso_set_shader_images(struct cso_context
*ctx
,
1364 enum pipe_shader_type shader_stage
,
1365 unsigned start
, unsigned count
,
1366 struct pipe_image_view
*images
)
1368 if (shader_stage
== PIPE_SHADER_FRAGMENT
&& start
== 0 && count
>= 1) {
1369 util_copy_image_view(&ctx
->fragment_image0_current
, &images
[0]);
1372 ctx
->pipe
->set_shader_images(ctx
->pipe
, shader_stage
, start
, count
, images
);
1377 cso_save_fragment_image0(struct cso_context
*ctx
)
1379 util_copy_image_view(&ctx
->fragment_image0_saved
,
1380 &ctx
->fragment_image0_current
);
1385 cso_restore_fragment_image0(struct cso_context
*ctx
)
1387 cso_set_shader_images(ctx
, PIPE_SHADER_FRAGMENT
, 0, 1,
1388 &ctx
->fragment_image0_saved
);
1393 cso_set_stream_outputs(struct cso_context
*ctx
,
1394 unsigned num_targets
,
1395 struct pipe_stream_output_target
**targets
,
1396 const unsigned *offsets
)
1398 struct pipe_context
*pipe
= ctx
->pipe
;
1401 if (!ctx
->has_streamout
) {
1402 assert(num_targets
== 0);
1406 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1407 /* Nothing to do. */
1411 /* reference new targets */
1412 for (i
= 0; i
< num_targets
; i
++) {
1413 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1415 /* unref extra old targets, if any */
1416 for (; i
< ctx
->nr_so_targets
; i
++) {
1417 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1420 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1422 ctx
->nr_so_targets
= num_targets
;
1426 cso_save_stream_outputs(struct cso_context
*ctx
)
1430 if (!ctx
->has_streamout
) {
1434 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1436 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1437 assert(!ctx
->so_targets_saved
[i
]);
1438 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1443 cso_restore_stream_outputs(struct cso_context
*ctx
)
1445 struct pipe_context
*pipe
= ctx
->pipe
;
1447 unsigned offset
[PIPE_MAX_SO_BUFFERS
];
1449 if (!ctx
->has_streamout
) {
1453 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1454 /* Nothing to do. */
1458 assert(ctx
->nr_so_targets_saved
<= PIPE_MAX_SO_BUFFERS
);
1459 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1460 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1461 /* move the reference from one pointer to another */
1462 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1463 ctx
->so_targets_saved
[i
] = NULL
;
1464 /* -1 means append */
1465 offset
[i
] = (unsigned)-1;
1467 for (; i
< ctx
->nr_so_targets
; i
++) {
1468 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1471 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1472 ctx
->so_targets
, offset
);
1474 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1475 ctx
->nr_so_targets_saved
= 0;
1478 /* constant buffers */
1481 cso_set_constant_buffer(struct cso_context
*cso
, unsigned shader_stage
,
1482 unsigned index
, struct pipe_constant_buffer
*cb
)
1484 struct pipe_context
*pipe
= cso
->pipe
;
1486 pipe
->set_constant_buffer(pipe
, shader_stage
, index
, cb
);
1489 util_copy_constant_buffer(&cso
->aux_constbuf_current
[shader_stage
], cb
);
1494 cso_set_constant_buffer_resource(struct cso_context
*cso
,
1495 unsigned shader_stage
,
1497 struct pipe_resource
*buffer
)
1500 struct pipe_constant_buffer cb
;
1502 cb
.buffer_offset
= 0;
1503 cb
.buffer_size
= buffer
->width0
;
1504 cb
.user_buffer
= NULL
;
1505 cso_set_constant_buffer(cso
, shader_stage
, index
, &cb
);
1507 cso_set_constant_buffer(cso
, shader_stage
, index
, NULL
);
1512 cso_save_constant_buffer_slot0(struct cso_context
*cso
,
1513 unsigned shader_stage
)
1515 util_copy_constant_buffer(&cso
->aux_constbuf_saved
[shader_stage
],
1516 &cso
->aux_constbuf_current
[shader_stage
]);
1520 cso_restore_constant_buffer_slot0(struct cso_context
*cso
,
1521 unsigned shader_stage
)
1523 cso_set_constant_buffer(cso
, shader_stage
, 0,
1524 &cso
->aux_constbuf_saved
[shader_stage
]);
1525 pipe_resource_reference(&cso
->aux_constbuf_saved
[shader_stage
].buffer
,
1531 * Save all the CSO state items specified by the state_mask bitmask
1532 * of CSO_BIT_x flags.
1535 cso_save_state(struct cso_context
*cso
, unsigned state_mask
)
1537 assert(cso
->saved_state
== 0);
1539 cso
->saved_state
= state_mask
;
1541 if (state_mask
& CSO_BIT_AUX_VERTEX_BUFFER_SLOT
)
1542 cso_save_aux_vertex_buffer_slot(cso
);
1543 if (state_mask
& CSO_BIT_BLEND
)
1544 cso_save_blend(cso
);
1545 if (state_mask
& CSO_BIT_DEPTH_STENCIL_ALPHA
)
1546 cso_save_depth_stencil_alpha(cso
);
1547 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLERS
)
1548 cso_save_fragment_samplers(cso
);
1549 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLER_VIEWS
)
1550 cso_save_fragment_sampler_views(cso
);
1551 if (state_mask
& CSO_BIT_FRAGMENT_SHADER
)
1552 cso_save_fragment_shader(cso
);
1553 if (state_mask
& CSO_BIT_FRAMEBUFFER
)
1554 cso_save_framebuffer(cso
);
1555 if (state_mask
& CSO_BIT_GEOMETRY_SHADER
)
1556 cso_save_geometry_shader(cso
);
1557 if (state_mask
& CSO_BIT_MIN_SAMPLES
)
1558 cso_save_min_samples(cso
);
1559 if (state_mask
& CSO_BIT_RASTERIZER
)
1560 cso_save_rasterizer(cso
);
1561 if (state_mask
& CSO_BIT_RENDER_CONDITION
)
1562 cso_save_render_condition(cso
);
1563 if (state_mask
& CSO_BIT_SAMPLE_MASK
)
1564 cso_save_sample_mask(cso
);
1565 if (state_mask
& CSO_BIT_STENCIL_REF
)
1566 cso_save_stencil_ref(cso
);
1567 if (state_mask
& CSO_BIT_STREAM_OUTPUTS
)
1568 cso_save_stream_outputs(cso
);
1569 if (state_mask
& CSO_BIT_TESSCTRL_SHADER
)
1570 cso_save_tessctrl_shader(cso
);
1571 if (state_mask
& CSO_BIT_TESSEVAL_SHADER
)
1572 cso_save_tesseval_shader(cso
);
1573 if (state_mask
& CSO_BIT_VERTEX_ELEMENTS
)
1574 cso_save_vertex_elements(cso
);
1575 if (state_mask
& CSO_BIT_VERTEX_SHADER
)
1576 cso_save_vertex_shader(cso
);
1577 if (state_mask
& CSO_BIT_VIEWPORT
)
1578 cso_save_viewport(cso
);
1579 if (state_mask
& CSO_BIT_PAUSE_QUERIES
)
1580 cso
->pipe
->set_active_query_state(cso
->pipe
, false);
1581 if (state_mask
& CSO_BIT_FRAGMENT_IMAGE0
)
1582 cso_save_fragment_image0(cso
);
1587 * Restore the state which was saved by cso_save_state().
1590 cso_restore_state(struct cso_context
*cso
)
1592 unsigned state_mask
= cso
->saved_state
;
1596 if (state_mask
& CSO_BIT_AUX_VERTEX_BUFFER_SLOT
)
1597 cso_restore_aux_vertex_buffer_slot(cso
);
1598 if (state_mask
& CSO_BIT_BLEND
)
1599 cso_restore_blend(cso
);
1600 if (state_mask
& CSO_BIT_DEPTH_STENCIL_ALPHA
)
1601 cso_restore_depth_stencil_alpha(cso
);
1602 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLERS
)
1603 cso_restore_fragment_samplers(cso
);
1604 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLER_VIEWS
)
1605 cso_restore_fragment_sampler_views(cso
);
1606 if (state_mask
& CSO_BIT_FRAGMENT_SHADER
)
1607 cso_restore_fragment_shader(cso
);
1608 if (state_mask
& CSO_BIT_FRAMEBUFFER
)
1609 cso_restore_framebuffer(cso
);
1610 if (state_mask
& CSO_BIT_GEOMETRY_SHADER
)
1611 cso_restore_geometry_shader(cso
);
1612 if (state_mask
& CSO_BIT_MIN_SAMPLES
)
1613 cso_restore_min_samples(cso
);
1614 if (state_mask
& CSO_BIT_RASTERIZER
)
1615 cso_restore_rasterizer(cso
);
1616 if (state_mask
& CSO_BIT_RENDER_CONDITION
)
1617 cso_restore_render_condition(cso
);
1618 if (state_mask
& CSO_BIT_SAMPLE_MASK
)
1619 cso_restore_sample_mask(cso
);
1620 if (state_mask
& CSO_BIT_STENCIL_REF
)
1621 cso_restore_stencil_ref(cso
);
1622 if (state_mask
& CSO_BIT_STREAM_OUTPUTS
)
1623 cso_restore_stream_outputs(cso
);
1624 if (state_mask
& CSO_BIT_TESSCTRL_SHADER
)
1625 cso_restore_tessctrl_shader(cso
);
1626 if (state_mask
& CSO_BIT_TESSEVAL_SHADER
)
1627 cso_restore_tesseval_shader(cso
);
1628 if (state_mask
& CSO_BIT_VERTEX_ELEMENTS
)
1629 cso_restore_vertex_elements(cso
);
1630 if (state_mask
& CSO_BIT_VERTEX_SHADER
)
1631 cso_restore_vertex_shader(cso
);
1632 if (state_mask
& CSO_BIT_VIEWPORT
)
1633 cso_restore_viewport(cso
);
1634 if (state_mask
& CSO_BIT_PAUSE_QUERIES
)
1635 cso
->pipe
->set_active_query_state(cso
->pipe
, true);
1636 if (state_mask
& CSO_BIT_FRAGMENT_IMAGE0
)
1637 cso_restore_fragment_image0(cso
);
1639 cso
->saved_state
= 0;
1647 cso_set_index_buffer(struct cso_context
*cso
,
1648 const struct pipe_index_buffer
*ib
)
1650 struct u_vbuf
*vbuf
= cso
->vbuf
;
1653 u_vbuf_set_index_buffer(vbuf
, ib
);
1655 struct pipe_context
*pipe
= cso
->pipe
;
1656 pipe
->set_index_buffer(pipe
, ib
);
1661 cso_draw_vbo(struct cso_context
*cso
,
1662 const struct pipe_draw_info
*info
)
1664 struct u_vbuf
*vbuf
= cso
->vbuf
;
1667 u_vbuf_draw_vbo(vbuf
, info
);
1669 struct pipe_context
*pipe
= cso
->pipe
;
1670 pipe
->draw_vbo(pipe
, info
);
1675 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1677 struct pipe_draw_info info
;
1679 util_draw_init_info(&info
);
1684 info
.min_index
= start
;
1685 info
.max_index
= start
+ count
- 1;
1687 cso_draw_vbo(cso
, &info
);
1691 cso_draw_arrays_instanced(struct cso_context
*cso
, uint mode
,
1692 uint start
, uint count
,
1693 uint start_instance
, uint instance_count
)
1695 struct pipe_draw_info info
;
1697 util_draw_init_info(&info
);
1702 info
.min_index
= start
;
1703 info
.max_index
= start
+ count
- 1;
1704 info
.start_instance
= start_instance
;
1705 info
.instance_count
= instance_count
;
1707 cso_draw_vbo(cso
, &info
);