gallium: remove pipe_index_buffer and set_index_buffer
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_context.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 *
31 * Wrap the cso cache & hash mechanisms in a simplified
32 * pipe-driver-specific interface.
33 *
34 * @author Zack Rusin <zackr@vmware.com>
35 * @author Keith Whitwell <keithw@vmware.com>
36 */
37
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"
46
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"
51
52
53 /**
54 * Per-shader sampler information.
55 */
56 struct sampler_info
57 {
58 struct cso_sampler *cso_samplers[PIPE_MAX_SAMPLERS];
59 void *samplers[PIPE_MAX_SAMPLERS];
60 unsigned nr_samplers;
61 };
62
63
64
65 struct cso_context {
66 struct pipe_context *pipe;
67 struct cso_cache *cache;
68 struct u_vbuf *vbuf;
69
70 boolean has_geometry_shader;
71 boolean has_tessellation;
72 boolean has_compute_shader;
73 boolean has_streamout;
74
75 unsigned saved_state; /**< bitmask of CSO_BIT_x flags */
76
77 struct pipe_sampler_view *fragment_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
78 unsigned nr_fragment_views;
79
80 struct pipe_sampler_view *fragment_views_saved[PIPE_MAX_SHADER_SAMPLER_VIEWS];
81 unsigned nr_fragment_views_saved;
82
83 struct sampler_info fragment_samplers_saved;
84 struct sampler_info samplers[PIPE_SHADER_TYPES];
85
86 struct pipe_vertex_buffer aux_vertex_buffer_current;
87 struct pipe_vertex_buffer aux_vertex_buffer_saved;
88 unsigned aux_vertex_buffer_index;
89
90 struct pipe_constant_buffer aux_constbuf_current[PIPE_SHADER_TYPES];
91 struct pipe_constant_buffer aux_constbuf_saved[PIPE_SHADER_TYPES];
92
93 struct pipe_image_view fragment_image0_current;
94 struct pipe_image_view fragment_image0_saved;
95
96 unsigned nr_so_targets;
97 struct pipe_stream_output_target *so_targets[PIPE_MAX_SO_BUFFERS];
98
99 unsigned nr_so_targets_saved;
100 struct pipe_stream_output_target *so_targets_saved[PIPE_MAX_SO_BUFFERS];
101
102 /** Current and saved state.
103 * The saved state is used as a 1-deep stack.
104 */
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 *tessctrl_shader, *tessctrl_shader_saved;
112 void *tesseval_shader, *tesseval_shader_saved;
113 void *compute_shader;
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;
118
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 unsigned min_samples, min_samples_saved;
124 struct pipe_stencil_ref stencil_ref, stencil_ref_saved;
125 };
126
127
128 static boolean delete_blend_state(struct cso_context *ctx, void *state)
129 {
130 struct cso_blend *cso = (struct cso_blend *)state;
131
132 if (ctx->blend == cso->data)
133 return FALSE;
134
135 if (cso->delete_state)
136 cso->delete_state(cso->context, cso->data);
137 FREE(state);
138 return TRUE;
139 }
140
141 static boolean delete_depth_stencil_state(struct cso_context *ctx, void *state)
142 {
143 struct cso_depth_stencil_alpha *cso =
144 (struct cso_depth_stencil_alpha *)state;
145
146 if (ctx->depth_stencil == cso->data)
147 return FALSE;
148
149 if (cso->delete_state)
150 cso->delete_state(cso->context, cso->data);
151 FREE(state);
152
153 return TRUE;
154 }
155
156 static boolean delete_sampler_state(struct cso_context *ctx, void *state)
157 {
158 struct cso_sampler *cso = (struct cso_sampler *)state;
159 if (cso->delete_state)
160 cso->delete_state(cso->context, cso->data);
161 FREE(state);
162 return TRUE;
163 }
164
165 static boolean delete_rasterizer_state(struct cso_context *ctx, void *state)
166 {
167 struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
168
169 if (ctx->rasterizer == cso->data)
170 return FALSE;
171 if (cso->delete_state)
172 cso->delete_state(cso->context, cso->data);
173 FREE(state);
174 return TRUE;
175 }
176
177 static boolean delete_vertex_elements(struct cso_context *ctx,
178 void *state)
179 {
180 struct cso_velements *cso = (struct cso_velements *)state;
181
182 if (ctx->velements == cso->data)
183 return FALSE;
184
185 if (cso->delete_state)
186 cso->delete_state(cso->context, cso->data);
187 FREE(state);
188 return TRUE;
189 }
190
191
192 static inline boolean delete_cso(struct cso_context *ctx,
193 void *state, enum cso_cache_type type)
194 {
195 switch (type) {
196 case CSO_BLEND:
197 return delete_blend_state(ctx, state);
198 case CSO_SAMPLER:
199 return delete_sampler_state(ctx, state);
200 case CSO_DEPTH_STENCIL_ALPHA:
201 return delete_depth_stencil_state(ctx, state);
202 case CSO_RASTERIZER:
203 return delete_rasterizer_state(ctx, state);
204 case CSO_VELEMENTS:
205 return delete_vertex_elements(ctx, state);
206 default:
207 assert(0);
208 FREE(state);
209 }
210 return FALSE;
211 }
212
213 static inline void
214 sanitize_hash(struct cso_hash *hash, enum cso_cache_type type,
215 int max_size, void *user_data)
216 {
217 struct cso_context *ctx = (struct cso_context *)user_data;
218 /* if we're approach the maximum size, remove fourth of the entries
219 * otherwise every subsequent call will go through the same */
220 int hash_size = cso_hash_size(hash);
221 int max_entries = (max_size > hash_size) ? max_size : hash_size;
222 int to_remove = (max_size < max_entries) * max_entries/4;
223 struct cso_hash_iter iter;
224 struct cso_sampler **samplers_to_restore = NULL;
225 unsigned to_restore = 0;
226
227 if (hash_size > max_size)
228 to_remove += hash_size - max_size;
229
230 if (to_remove == 0)
231 return;
232
233 if (type == CSO_SAMPLER) {
234 int i, j;
235
236 samplers_to_restore = MALLOC(PIPE_SHADER_TYPES * PIPE_MAX_SAMPLERS *
237 sizeof(*samplers_to_restore));
238
239 /* Temporarily remove currently bound sampler states from the hash
240 * table, to prevent them from being deleted
241 */
242 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
243 for (j = 0; j < ctx->samplers[i].nr_samplers; j++) {
244 struct cso_sampler *sampler = ctx->samplers[i].cso_samplers[j];
245
246 if (sampler && cso_hash_take(hash, sampler->hash_key))
247 samplers_to_restore[to_restore++] = sampler;
248 }
249 }
250 }
251
252 iter = cso_hash_first_node(hash);
253 while (to_remove) {
254 /*remove elements until we're good */
255 /*fixme: currently we pick the nodes to remove at random*/
256 void *cso = cso_hash_iter_data(iter);
257
258 if (!cso)
259 break;
260
261 if (delete_cso(ctx, cso, type)) {
262 iter = cso_hash_erase(hash, iter);
263 --to_remove;
264 } else
265 iter = cso_hash_iter_next(iter);
266 }
267
268 if (type == CSO_SAMPLER) {
269 /* Put currently bound sampler states back into the hash table */
270 while (to_restore--) {
271 struct cso_sampler *sampler = samplers_to_restore[to_restore];
272
273 cso_hash_insert(hash, sampler->hash_key, sampler);
274 }
275
276 FREE(samplers_to_restore);
277 }
278 }
279
280 static void cso_init_vbuf(struct cso_context *cso, unsigned flags)
281 {
282 struct u_vbuf_caps caps;
283
284 /* Install u_vbuf if there is anything unsupported. */
285 if (u_vbuf_get_caps(cso->pipe->screen, &caps, flags)) {
286 cso->vbuf = u_vbuf_create(cso->pipe, &caps,
287 cso->aux_vertex_buffer_index);
288 }
289 }
290
291 struct cso_context *
292 cso_create_context(struct pipe_context *pipe, unsigned u_vbuf_flags)
293 {
294 struct cso_context *ctx = CALLOC_STRUCT(cso_context);
295 if (!ctx)
296 return NULL;
297
298 ctx->cache = cso_cache_create();
299 if (ctx->cache == NULL)
300 goto out;
301 cso_cache_set_sanitize_callback(ctx->cache,
302 sanitize_hash,
303 ctx);
304
305 ctx->pipe = pipe;
306 ctx->sample_mask = ~0;
307
308 ctx->aux_vertex_buffer_index = 0; /* 0 for now */
309
310 cso_init_vbuf(ctx, u_vbuf_flags);
311
312 /* Enable for testing: */
313 if (0) cso_set_maximum_cache_size( ctx->cache, 4 );
314
315 if (pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
316 PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0) {
317 ctx->has_geometry_shader = TRUE;
318 }
319 if (pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_TESS_CTRL,
320 PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0) {
321 ctx->has_tessellation = TRUE;
322 }
323 if (pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_COMPUTE,
324 PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0) {
325 int supported_irs =
326 pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_COMPUTE,
327 PIPE_SHADER_CAP_SUPPORTED_IRS);
328 if (supported_irs & (1 << PIPE_SHADER_IR_TGSI)) {
329 ctx->has_compute_shader = TRUE;
330 }
331 }
332 if (pipe->screen->get_param(pipe->screen,
333 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0) {
334 ctx->has_streamout = TRUE;
335 }
336
337 return ctx;
338
339 out:
340 cso_destroy_context( ctx );
341 return NULL;
342 }
343
344 /**
345 * Free the CSO context.
346 */
347 void cso_destroy_context( struct cso_context *ctx )
348 {
349 unsigned i;
350
351 if (ctx->pipe) {
352 ctx->pipe->bind_blend_state( ctx->pipe, NULL );
353 ctx->pipe->bind_rasterizer_state( ctx->pipe, NULL );
354
355 {
356 static struct pipe_sampler_view *views[PIPE_MAX_SHADER_SAMPLER_VIEWS] = { NULL };
357 static void *zeros[PIPE_MAX_SAMPLERS] = { NULL };
358 struct pipe_screen *scr = ctx->pipe->screen;
359 enum pipe_shader_type sh;
360 for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
361 int maxsam = scr->get_shader_param(scr, sh,
362 PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS);
363 int maxview = scr->get_shader_param(scr, sh,
364 PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS);
365 assert(maxsam <= PIPE_MAX_SAMPLERS);
366 assert(maxview <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
367 if (maxsam > 0) {
368 ctx->pipe->bind_sampler_states(ctx->pipe, sh, 0, maxsam, zeros);
369 }
370 if (maxview > 0) {
371 ctx->pipe->set_sampler_views(ctx->pipe, sh, 0, maxview, views);
372 }
373 }
374 }
375
376 ctx->pipe->bind_depth_stencil_alpha_state( ctx->pipe, NULL );
377 ctx->pipe->bind_fs_state( ctx->pipe, NULL );
378 ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_FRAGMENT, 0, NULL);
379 ctx->pipe->bind_vs_state( ctx->pipe, NULL );
380 ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_VERTEX, 0, NULL);
381 if (ctx->has_geometry_shader) {
382 ctx->pipe->bind_gs_state(ctx->pipe, NULL);
383 ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_GEOMETRY, 0, NULL);
384 }
385 if (ctx->has_tessellation) {
386 ctx->pipe->bind_tcs_state(ctx->pipe, NULL);
387 ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_TESS_CTRL, 0, NULL);
388 ctx->pipe->bind_tes_state(ctx->pipe, NULL);
389 ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_TESS_EVAL, 0, NULL);
390 }
391 if (ctx->has_compute_shader) {
392 ctx->pipe->bind_compute_state(ctx->pipe, NULL);
393 ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_COMPUTE, 0, NULL);
394 }
395 ctx->pipe->bind_vertex_elements_state( ctx->pipe, NULL );
396
397 if (ctx->has_streamout)
398 ctx->pipe->set_stream_output_targets(ctx->pipe, 0, NULL, NULL);
399 }
400
401 for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
402 pipe_sampler_view_reference(&ctx->fragment_views[i], NULL);
403 pipe_sampler_view_reference(&ctx->fragment_views_saved[i], NULL);
404 }
405
406 util_unreference_framebuffer_state(&ctx->fb);
407 util_unreference_framebuffer_state(&ctx->fb_saved);
408
409 pipe_vertex_buffer_unreference(&ctx->aux_vertex_buffer_current);
410 pipe_vertex_buffer_unreference(&ctx->aux_vertex_buffer_saved);
411
412 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
413 pipe_resource_reference(&ctx->aux_constbuf_current[i].buffer, NULL);
414 pipe_resource_reference(&ctx->aux_constbuf_saved[i].buffer, NULL);
415 }
416
417 pipe_resource_reference(&ctx->fragment_image0_current.resource, NULL);
418 pipe_resource_reference(&ctx->fragment_image0_saved.resource, NULL);
419
420 for (i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
421 pipe_so_target_reference(&ctx->so_targets[i], NULL);
422 pipe_so_target_reference(&ctx->so_targets_saved[i], NULL);
423 }
424
425 if (ctx->cache) {
426 cso_cache_delete( ctx->cache );
427 ctx->cache = NULL;
428 }
429
430 if (ctx->vbuf)
431 u_vbuf_destroy(ctx->vbuf);
432 FREE( ctx );
433 }
434
435
436 /* Those function will either find the state of the given template
437 * in the cache or they will create a new state from the given
438 * template, insert it in the cache and return it.
439 */
440
441 /*
442 * If the driver returns 0 from the create method then they will assign
443 * the data member of the cso to be the template itself.
444 */
445
446 enum pipe_error cso_set_blend(struct cso_context *ctx,
447 const struct pipe_blend_state *templ)
448 {
449 unsigned key_size, hash_key;
450 struct cso_hash_iter iter;
451 void *handle;
452
453 key_size = templ->independent_blend_enable ?
454 sizeof(struct pipe_blend_state) :
455 (char *)&(templ->rt[1]) - (char *)templ;
456 hash_key = cso_construct_key((void*)templ, key_size);
457 iter = cso_find_state_template(ctx->cache, hash_key, CSO_BLEND,
458 (void*)templ, key_size);
459
460 if (cso_hash_iter_is_null(iter)) {
461 struct cso_blend *cso = MALLOC(sizeof(struct cso_blend));
462 if (!cso)
463 return PIPE_ERROR_OUT_OF_MEMORY;
464
465 memset(&cso->state, 0, sizeof cso->state);
466 memcpy(&cso->state, templ, key_size);
467 cso->data = ctx->pipe->create_blend_state(ctx->pipe, &cso->state);
468 cso->delete_state = (cso_state_callback)ctx->pipe->delete_blend_state;
469 cso->context = ctx->pipe;
470
471 iter = cso_insert_state(ctx->cache, hash_key, CSO_BLEND, cso);
472 if (cso_hash_iter_is_null(iter)) {
473 FREE(cso);
474 return PIPE_ERROR_OUT_OF_MEMORY;
475 }
476
477 handle = cso->data;
478 }
479 else {
480 handle = ((struct cso_blend *)cso_hash_iter_data(iter))->data;
481 }
482
483 if (ctx->blend != handle) {
484 ctx->blend = handle;
485 ctx->pipe->bind_blend_state(ctx->pipe, handle);
486 }
487 return PIPE_OK;
488 }
489
490 static void
491 cso_save_blend(struct cso_context *ctx)
492 {
493 assert(!ctx->blend_saved);
494 ctx->blend_saved = ctx->blend;
495 }
496
497 static void
498 cso_restore_blend(struct cso_context *ctx)
499 {
500 if (ctx->blend != ctx->blend_saved) {
501 ctx->blend = ctx->blend_saved;
502 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend_saved);
503 }
504 ctx->blend_saved = NULL;
505 }
506
507
508
509 enum pipe_error
510 cso_set_depth_stencil_alpha(struct cso_context *ctx,
511 const struct pipe_depth_stencil_alpha_state *templ)
512 {
513 unsigned key_size = sizeof(struct pipe_depth_stencil_alpha_state);
514 unsigned hash_key = cso_construct_key((void*)templ, key_size);
515 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
516 hash_key,
517 CSO_DEPTH_STENCIL_ALPHA,
518 (void*)templ, key_size);
519 void *handle;
520
521 if (cso_hash_iter_is_null(iter)) {
522 struct cso_depth_stencil_alpha *cso =
523 MALLOC(sizeof(struct cso_depth_stencil_alpha));
524 if (!cso)
525 return PIPE_ERROR_OUT_OF_MEMORY;
526
527 memcpy(&cso->state, templ, sizeof(*templ));
528 cso->data = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe,
529 &cso->state);
530 cso->delete_state =
531 (cso_state_callback)ctx->pipe->delete_depth_stencil_alpha_state;
532 cso->context = ctx->pipe;
533
534 iter = cso_insert_state(ctx->cache, hash_key,
535 CSO_DEPTH_STENCIL_ALPHA, cso);
536 if (cso_hash_iter_is_null(iter)) {
537 FREE(cso);
538 return PIPE_ERROR_OUT_OF_MEMORY;
539 }
540
541 handle = cso->data;
542 }
543 else {
544 handle = ((struct cso_depth_stencil_alpha *)
545 cso_hash_iter_data(iter))->data;
546 }
547
548 if (ctx->depth_stencil != handle) {
549 ctx->depth_stencil = handle;
550 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, handle);
551 }
552 return PIPE_OK;
553 }
554
555 static void
556 cso_save_depth_stencil_alpha(struct cso_context *ctx)
557 {
558 assert(!ctx->depth_stencil_saved);
559 ctx->depth_stencil_saved = ctx->depth_stencil;
560 }
561
562 static void
563 cso_restore_depth_stencil_alpha(struct cso_context *ctx)
564 {
565 if (ctx->depth_stencil != ctx->depth_stencil_saved) {
566 ctx->depth_stencil = ctx->depth_stencil_saved;
567 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe,
568 ctx->depth_stencil_saved);
569 }
570 ctx->depth_stencil_saved = NULL;
571 }
572
573
574
575 enum pipe_error cso_set_rasterizer(struct cso_context *ctx,
576 const struct pipe_rasterizer_state *templ)
577 {
578 unsigned key_size = sizeof(struct pipe_rasterizer_state);
579 unsigned hash_key = cso_construct_key((void*)templ, key_size);
580 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
581 hash_key,
582 CSO_RASTERIZER,
583 (void*)templ, key_size);
584 void *handle = NULL;
585
586 if (cso_hash_iter_is_null(iter)) {
587 struct cso_rasterizer *cso = MALLOC(sizeof(struct cso_rasterizer));
588 if (!cso)
589 return PIPE_ERROR_OUT_OF_MEMORY;
590
591 memcpy(&cso->state, templ, sizeof(*templ));
592 cso->data = ctx->pipe->create_rasterizer_state(ctx->pipe, &cso->state);
593 cso->delete_state =
594 (cso_state_callback)ctx->pipe->delete_rasterizer_state;
595 cso->context = ctx->pipe;
596
597 iter = cso_insert_state(ctx->cache, hash_key, CSO_RASTERIZER, cso);
598 if (cso_hash_iter_is_null(iter)) {
599 FREE(cso);
600 return PIPE_ERROR_OUT_OF_MEMORY;
601 }
602
603 handle = cso->data;
604 }
605 else {
606 handle = ((struct cso_rasterizer *)cso_hash_iter_data(iter))->data;
607 }
608
609 if (ctx->rasterizer != handle) {
610 ctx->rasterizer = handle;
611 ctx->pipe->bind_rasterizer_state(ctx->pipe, handle);
612 }
613 return PIPE_OK;
614 }
615
616 static void
617 cso_save_rasterizer(struct cso_context *ctx)
618 {
619 assert(!ctx->rasterizer_saved);
620 ctx->rasterizer_saved = ctx->rasterizer;
621 }
622
623 static void
624 cso_restore_rasterizer(struct cso_context *ctx)
625 {
626 if (ctx->rasterizer != ctx->rasterizer_saved) {
627 ctx->rasterizer = ctx->rasterizer_saved;
628 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rasterizer_saved);
629 }
630 ctx->rasterizer_saved = NULL;
631 }
632
633
634 void cso_set_fragment_shader_handle(struct cso_context *ctx, void *handle )
635 {
636 if (ctx->fragment_shader != handle) {
637 ctx->fragment_shader = handle;
638 ctx->pipe->bind_fs_state(ctx->pipe, handle);
639 }
640 }
641
642 void cso_delete_fragment_shader(struct cso_context *ctx, void *handle )
643 {
644 if (handle == ctx->fragment_shader) {
645 /* unbind before deleting */
646 ctx->pipe->bind_fs_state(ctx->pipe, NULL);
647 ctx->fragment_shader = NULL;
648 }
649 ctx->pipe->delete_fs_state(ctx->pipe, handle);
650 }
651
652 static void
653 cso_save_fragment_shader(struct cso_context *ctx)
654 {
655 assert(!ctx->fragment_shader_saved);
656 ctx->fragment_shader_saved = ctx->fragment_shader;
657 }
658
659 static void
660 cso_restore_fragment_shader(struct cso_context *ctx)
661 {
662 if (ctx->fragment_shader_saved != ctx->fragment_shader) {
663 ctx->pipe->bind_fs_state(ctx->pipe, ctx->fragment_shader_saved);
664 ctx->fragment_shader = ctx->fragment_shader_saved;
665 }
666 ctx->fragment_shader_saved = NULL;
667 }
668
669
670 void cso_set_vertex_shader_handle(struct cso_context *ctx, void *handle)
671 {
672 if (ctx->vertex_shader != handle) {
673 ctx->vertex_shader = handle;
674 ctx->pipe->bind_vs_state(ctx->pipe, handle);
675 }
676 }
677
678 void cso_delete_vertex_shader(struct cso_context *ctx, void *handle )
679 {
680 if (handle == ctx->vertex_shader) {
681 /* unbind before deleting */
682 ctx->pipe->bind_vs_state(ctx->pipe, NULL);
683 ctx->vertex_shader = NULL;
684 }
685 ctx->pipe->delete_vs_state(ctx->pipe, handle);
686 }
687
688 static void
689 cso_save_vertex_shader(struct cso_context *ctx)
690 {
691 assert(!ctx->vertex_shader_saved);
692 ctx->vertex_shader_saved = ctx->vertex_shader;
693 }
694
695 static void
696 cso_restore_vertex_shader(struct cso_context *ctx)
697 {
698 if (ctx->vertex_shader_saved != ctx->vertex_shader) {
699 ctx->pipe->bind_vs_state(ctx->pipe, ctx->vertex_shader_saved);
700 ctx->vertex_shader = ctx->vertex_shader_saved;
701 }
702 ctx->vertex_shader_saved = NULL;
703 }
704
705
706 void cso_set_framebuffer(struct cso_context *ctx,
707 const struct pipe_framebuffer_state *fb)
708 {
709 if (memcmp(&ctx->fb, fb, sizeof(*fb)) != 0) {
710 util_copy_framebuffer_state(&ctx->fb, fb);
711 ctx->pipe->set_framebuffer_state(ctx->pipe, fb);
712 }
713 }
714
715 static void
716 cso_save_framebuffer(struct cso_context *ctx)
717 {
718 util_copy_framebuffer_state(&ctx->fb_saved, &ctx->fb);
719 }
720
721 static void
722 cso_restore_framebuffer(struct cso_context *ctx)
723 {
724 if (memcmp(&ctx->fb, &ctx->fb_saved, sizeof(ctx->fb))) {
725 util_copy_framebuffer_state(&ctx->fb, &ctx->fb_saved);
726 ctx->pipe->set_framebuffer_state(ctx->pipe, &ctx->fb);
727 util_unreference_framebuffer_state(&ctx->fb_saved);
728 }
729 }
730
731
732 void cso_set_viewport(struct cso_context *ctx,
733 const struct pipe_viewport_state *vp)
734 {
735 if (memcmp(&ctx->vp, vp, sizeof(*vp))) {
736 ctx->vp = *vp;
737 ctx->pipe->set_viewport_states(ctx->pipe, 0, 1, vp);
738 }
739 }
740
741 /**
742 * Setup viewport state for given width and height (position is always (0,0)).
743 * Invert the Y axis if 'invert' is true.
744 */
745 void
746 cso_set_viewport_dims(struct cso_context *ctx,
747 float width, float height, boolean invert)
748 {
749 struct pipe_viewport_state vp;
750 vp.scale[0] = width * 0.5f;
751 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
752 vp.scale[2] = 0.5f;
753 vp.translate[0] = 0.5f * width;
754 vp.translate[1] = 0.5f * height;
755 vp.translate[2] = 0.5f;
756 cso_set_viewport(ctx, &vp);
757 }
758
759 static void
760 cso_save_viewport(struct cso_context *ctx)
761 {
762 ctx->vp_saved = ctx->vp;
763 }
764
765
766 static void
767 cso_restore_viewport(struct cso_context *ctx)
768 {
769 if (memcmp(&ctx->vp, &ctx->vp_saved, sizeof(ctx->vp))) {
770 ctx->vp = ctx->vp_saved;
771 ctx->pipe->set_viewport_states(ctx->pipe, 0, 1, &ctx->vp);
772 }
773 }
774
775
776 void cso_set_blend_color(struct cso_context *ctx,
777 const struct pipe_blend_color *bc)
778 {
779 if (memcmp(&ctx->blend_color, bc, sizeof(ctx->blend_color))) {
780 ctx->blend_color = *bc;
781 ctx->pipe->set_blend_color(ctx->pipe, bc);
782 }
783 }
784
785 void cso_set_sample_mask(struct cso_context *ctx, unsigned sample_mask)
786 {
787 if (ctx->sample_mask != sample_mask) {
788 ctx->sample_mask = sample_mask;
789 ctx->pipe->set_sample_mask(ctx->pipe, sample_mask);
790 }
791 }
792
793 static void
794 cso_save_sample_mask(struct cso_context *ctx)
795 {
796 ctx->sample_mask_saved = ctx->sample_mask;
797 }
798
799 static void
800 cso_restore_sample_mask(struct cso_context *ctx)
801 {
802 cso_set_sample_mask(ctx, ctx->sample_mask_saved);
803 }
804
805 void cso_set_min_samples(struct cso_context *ctx, unsigned min_samples)
806 {
807 if (ctx->min_samples != min_samples && ctx->pipe->set_min_samples) {
808 ctx->min_samples = min_samples;
809 ctx->pipe->set_min_samples(ctx->pipe, min_samples);
810 }
811 }
812
813 static void
814 cso_save_min_samples(struct cso_context *ctx)
815 {
816 ctx->min_samples_saved = ctx->min_samples;
817 }
818
819 static void
820 cso_restore_min_samples(struct cso_context *ctx)
821 {
822 cso_set_min_samples(ctx, ctx->min_samples_saved);
823 }
824
825 void cso_set_stencil_ref(struct cso_context *ctx,
826 const struct pipe_stencil_ref *sr)
827 {
828 if (memcmp(&ctx->stencil_ref, sr, sizeof(ctx->stencil_ref))) {
829 ctx->stencil_ref = *sr;
830 ctx->pipe->set_stencil_ref(ctx->pipe, sr);
831 }
832 }
833
834 static void
835 cso_save_stencil_ref(struct cso_context *ctx)
836 {
837 ctx->stencil_ref_saved = ctx->stencil_ref;
838 }
839
840
841 static void
842 cso_restore_stencil_ref(struct cso_context *ctx)
843 {
844 if (memcmp(&ctx->stencil_ref, &ctx->stencil_ref_saved,
845 sizeof(ctx->stencil_ref))) {
846 ctx->stencil_ref = ctx->stencil_ref_saved;
847 ctx->pipe->set_stencil_ref(ctx->pipe, &ctx->stencil_ref);
848 }
849 }
850
851 void cso_set_render_condition(struct cso_context *ctx,
852 struct pipe_query *query,
853 boolean condition,
854 enum pipe_render_cond_flag mode)
855 {
856 struct pipe_context *pipe = ctx->pipe;
857
858 if (ctx->render_condition != query ||
859 ctx->render_condition_mode != mode ||
860 ctx->render_condition_cond != condition) {
861 pipe->render_condition(pipe, query, condition, mode);
862 ctx->render_condition = query;
863 ctx->render_condition_cond = condition;
864 ctx->render_condition_mode = mode;
865 }
866 }
867
868 static void
869 cso_save_render_condition(struct cso_context *ctx)
870 {
871 ctx->render_condition_saved = ctx->render_condition;
872 ctx->render_condition_cond_saved = ctx->render_condition_cond;
873 ctx->render_condition_mode_saved = ctx->render_condition_mode;
874 }
875
876 static void
877 cso_restore_render_condition(struct cso_context *ctx)
878 {
879 cso_set_render_condition(ctx, ctx->render_condition_saved,
880 ctx->render_condition_cond_saved,
881 ctx->render_condition_mode_saved);
882 }
883
884 void cso_set_geometry_shader_handle(struct cso_context *ctx, void *handle)
885 {
886 assert(ctx->has_geometry_shader || !handle);
887
888 if (ctx->has_geometry_shader && ctx->geometry_shader != handle) {
889 ctx->geometry_shader = handle;
890 ctx->pipe->bind_gs_state(ctx->pipe, handle);
891 }
892 }
893
894 void cso_delete_geometry_shader(struct cso_context *ctx, void *handle)
895 {
896 if (handle == ctx->geometry_shader) {
897 /* unbind before deleting */
898 ctx->pipe->bind_gs_state(ctx->pipe, NULL);
899 ctx->geometry_shader = NULL;
900 }
901 ctx->pipe->delete_gs_state(ctx->pipe, handle);
902 }
903
904 static void
905 cso_save_geometry_shader(struct cso_context *ctx)
906 {
907 if (!ctx->has_geometry_shader) {
908 return;
909 }
910
911 assert(!ctx->geometry_shader_saved);
912 ctx->geometry_shader_saved = ctx->geometry_shader;
913 }
914
915 static void
916 cso_restore_geometry_shader(struct cso_context *ctx)
917 {
918 if (!ctx->has_geometry_shader) {
919 return;
920 }
921
922 if (ctx->geometry_shader_saved != ctx->geometry_shader) {
923 ctx->pipe->bind_gs_state(ctx->pipe, ctx->geometry_shader_saved);
924 ctx->geometry_shader = ctx->geometry_shader_saved;
925 }
926 ctx->geometry_shader_saved = NULL;
927 }
928
929 void cso_set_tessctrl_shader_handle(struct cso_context *ctx, void *handle)
930 {
931 assert(ctx->has_tessellation || !handle);
932
933 if (ctx->has_tessellation && ctx->tessctrl_shader != handle) {
934 ctx->tessctrl_shader = handle;
935 ctx->pipe->bind_tcs_state(ctx->pipe, handle);
936 }
937 }
938
939 void cso_delete_tessctrl_shader(struct cso_context *ctx, void *handle)
940 {
941 if (handle == ctx->tessctrl_shader) {
942 /* unbind before deleting */
943 ctx->pipe->bind_tcs_state(ctx->pipe, NULL);
944 ctx->tessctrl_shader = NULL;
945 }
946 ctx->pipe->delete_tcs_state(ctx->pipe, handle);
947 }
948
949 static void
950 cso_save_tessctrl_shader(struct cso_context *ctx)
951 {
952 if (!ctx->has_tessellation) {
953 return;
954 }
955
956 assert(!ctx->tessctrl_shader_saved);
957 ctx->tessctrl_shader_saved = ctx->tessctrl_shader;
958 }
959
960 static void
961 cso_restore_tessctrl_shader(struct cso_context *ctx)
962 {
963 if (!ctx->has_tessellation) {
964 return;
965 }
966
967 if (ctx->tessctrl_shader_saved != ctx->tessctrl_shader) {
968 ctx->pipe->bind_tcs_state(ctx->pipe, ctx->tessctrl_shader_saved);
969 ctx->tessctrl_shader = ctx->tessctrl_shader_saved;
970 }
971 ctx->tessctrl_shader_saved = NULL;
972 }
973
974 void cso_set_tesseval_shader_handle(struct cso_context *ctx, void *handle)
975 {
976 assert(ctx->has_tessellation || !handle);
977
978 if (ctx->has_tessellation && ctx->tesseval_shader != handle) {
979 ctx->tesseval_shader = handle;
980 ctx->pipe->bind_tes_state(ctx->pipe, handle);
981 }
982 }
983
984 void cso_delete_tesseval_shader(struct cso_context *ctx, void *handle)
985 {
986 if (handle == ctx->tesseval_shader) {
987 /* unbind before deleting */
988 ctx->pipe->bind_tes_state(ctx->pipe, NULL);
989 ctx->tesseval_shader = NULL;
990 }
991 ctx->pipe->delete_tes_state(ctx->pipe, handle);
992 }
993
994 static void
995 cso_save_tesseval_shader(struct cso_context *ctx)
996 {
997 if (!ctx->has_tessellation) {
998 return;
999 }
1000
1001 assert(!ctx->tesseval_shader_saved);
1002 ctx->tesseval_shader_saved = ctx->tesseval_shader;
1003 }
1004
1005 static void
1006 cso_restore_tesseval_shader(struct cso_context *ctx)
1007 {
1008 if (!ctx->has_tessellation) {
1009 return;
1010 }
1011
1012 if (ctx->tesseval_shader_saved != ctx->tesseval_shader) {
1013 ctx->pipe->bind_tes_state(ctx->pipe, ctx->tesseval_shader_saved);
1014 ctx->tesseval_shader = ctx->tesseval_shader_saved;
1015 }
1016 ctx->tesseval_shader_saved = NULL;
1017 }
1018
1019 void cso_set_compute_shader_handle(struct cso_context *ctx, void *handle)
1020 {
1021 assert(ctx->has_compute_shader || !handle);
1022
1023 if (ctx->has_compute_shader && ctx->compute_shader != handle) {
1024 ctx->compute_shader = handle;
1025 ctx->pipe->bind_compute_state(ctx->pipe, handle);
1026 }
1027 }
1028
1029 void cso_delete_compute_shader(struct cso_context *ctx, void *handle)
1030 {
1031 if (handle == ctx->compute_shader) {
1032 /* unbind before deleting */
1033 ctx->pipe->bind_compute_state(ctx->pipe, NULL);
1034 ctx->compute_shader = NULL;
1035 }
1036 ctx->pipe->delete_compute_state(ctx->pipe, handle);
1037 }
1038
1039 enum pipe_error
1040 cso_set_vertex_elements(struct cso_context *ctx,
1041 unsigned count,
1042 const struct pipe_vertex_element *states)
1043 {
1044 struct u_vbuf *vbuf = ctx->vbuf;
1045 unsigned key_size, hash_key;
1046 struct cso_hash_iter iter;
1047 void *handle;
1048 struct cso_velems_state velems_state;
1049
1050 if (vbuf) {
1051 u_vbuf_set_vertex_elements(vbuf, count, states);
1052 return PIPE_OK;
1053 }
1054
1055 /* Need to include the count into the stored state data too.
1056 * Otherwise first few count pipe_vertex_elements could be identical
1057 * even if count is different, and there's no guarantee the hash would
1058 * be different in that case neither.
1059 */
1060 key_size = sizeof(struct pipe_vertex_element) * count + sizeof(unsigned);
1061 velems_state.count = count;
1062 memcpy(velems_state.velems, states,
1063 sizeof(struct pipe_vertex_element) * count);
1064 hash_key = cso_construct_key((void*)&velems_state, key_size);
1065 iter = cso_find_state_template(ctx->cache, hash_key, CSO_VELEMENTS,
1066 (void*)&velems_state, key_size);
1067
1068 if (cso_hash_iter_is_null(iter)) {
1069 struct cso_velements *cso = MALLOC(sizeof(struct cso_velements));
1070 if (!cso)
1071 return PIPE_ERROR_OUT_OF_MEMORY;
1072
1073 memcpy(&cso->state, &velems_state, key_size);
1074 cso->data = ctx->pipe->create_vertex_elements_state(ctx->pipe, count,
1075 &cso->state.velems[0]);
1076 cso->delete_state =
1077 (cso_state_callback) ctx->pipe->delete_vertex_elements_state;
1078 cso->context = ctx->pipe;
1079
1080 iter = cso_insert_state(ctx->cache, hash_key, CSO_VELEMENTS, cso);
1081 if (cso_hash_iter_is_null(iter)) {
1082 FREE(cso);
1083 return PIPE_ERROR_OUT_OF_MEMORY;
1084 }
1085
1086 handle = cso->data;
1087 }
1088 else {
1089 handle = ((struct cso_velements *)cso_hash_iter_data(iter))->data;
1090 }
1091
1092 if (ctx->velements != handle) {
1093 ctx->velements = handle;
1094 ctx->pipe->bind_vertex_elements_state(ctx->pipe, handle);
1095 }
1096 return PIPE_OK;
1097 }
1098
1099 static void
1100 cso_save_vertex_elements(struct cso_context *ctx)
1101 {
1102 struct u_vbuf *vbuf = ctx->vbuf;
1103
1104 if (vbuf) {
1105 u_vbuf_save_vertex_elements(vbuf);
1106 return;
1107 }
1108
1109 assert(!ctx->velements_saved);
1110 ctx->velements_saved = ctx->velements;
1111 }
1112
1113 static void
1114 cso_restore_vertex_elements(struct cso_context *ctx)
1115 {
1116 struct u_vbuf *vbuf = ctx->vbuf;
1117
1118 if (vbuf) {
1119 u_vbuf_restore_vertex_elements(vbuf);
1120 return;
1121 }
1122
1123 if (ctx->velements != ctx->velements_saved) {
1124 ctx->velements = ctx->velements_saved;
1125 ctx->pipe->bind_vertex_elements_state(ctx->pipe, ctx->velements_saved);
1126 }
1127 ctx->velements_saved = NULL;
1128 }
1129
1130 /* vertex buffers */
1131
1132 void cso_set_vertex_buffers(struct cso_context *ctx,
1133 unsigned start_slot, unsigned count,
1134 const struct pipe_vertex_buffer *buffers)
1135 {
1136 struct u_vbuf *vbuf = ctx->vbuf;
1137
1138 if (vbuf) {
1139 u_vbuf_set_vertex_buffers(vbuf, start_slot, count, buffers);
1140 return;
1141 }
1142
1143 /* Save what's in the auxiliary slot, so that we can save and restore it
1144 * for meta ops. */
1145 if (start_slot <= ctx->aux_vertex_buffer_index &&
1146 start_slot+count > ctx->aux_vertex_buffer_index) {
1147 if (buffers) {
1148 const struct pipe_vertex_buffer *vb =
1149 buffers + (ctx->aux_vertex_buffer_index - start_slot);
1150
1151 pipe_vertex_buffer_reference(&ctx->aux_vertex_buffer_current, vb);
1152 } else {
1153 pipe_vertex_buffer_unreference(&ctx->aux_vertex_buffer_current);
1154 }
1155 }
1156
1157 ctx->pipe->set_vertex_buffers(ctx->pipe, start_slot, count, buffers);
1158 }
1159
1160 static void
1161 cso_save_aux_vertex_buffer_slot(struct cso_context *ctx)
1162 {
1163 struct u_vbuf *vbuf = ctx->vbuf;
1164
1165 if (vbuf) {
1166 u_vbuf_save_aux_vertex_buffer_slot(vbuf);
1167 return;
1168 }
1169
1170 pipe_vertex_buffer_reference(&ctx->aux_vertex_buffer_saved,
1171 &ctx->aux_vertex_buffer_current);
1172 }
1173
1174 static void
1175 cso_restore_aux_vertex_buffer_slot(struct cso_context *ctx)
1176 {
1177 struct u_vbuf *vbuf = ctx->vbuf;
1178
1179 if (vbuf) {
1180 u_vbuf_restore_aux_vertex_buffer_slot(vbuf);
1181 return;
1182 }
1183
1184 cso_set_vertex_buffers(ctx, ctx->aux_vertex_buffer_index, 1,
1185 &ctx->aux_vertex_buffer_saved);
1186 pipe_vertex_buffer_unreference(&ctx->aux_vertex_buffer_saved);
1187 }
1188
1189 unsigned cso_get_aux_vertex_buffer_slot(struct cso_context *ctx)
1190 {
1191 return ctx->aux_vertex_buffer_index;
1192 }
1193
1194
1195
1196 enum pipe_error
1197 cso_single_sampler(struct cso_context *ctx, enum pipe_shader_type shader_stage,
1198 unsigned idx, const struct pipe_sampler_state *templ)
1199 {
1200 if (templ) {
1201 unsigned key_size = sizeof(struct pipe_sampler_state);
1202 unsigned hash_key = cso_construct_key((void*)templ, key_size);
1203 struct cso_sampler *cso;
1204 struct cso_hash_iter iter =
1205 cso_find_state_template(ctx->cache,
1206 hash_key, CSO_SAMPLER,
1207 (void *) templ, key_size);
1208
1209 if (cso_hash_iter_is_null(iter)) {
1210 cso = MALLOC(sizeof(struct cso_sampler));
1211 if (!cso)
1212 return PIPE_ERROR_OUT_OF_MEMORY;
1213
1214 memcpy(&cso->state, templ, sizeof(*templ));
1215 cso->data = ctx->pipe->create_sampler_state(ctx->pipe, &cso->state);
1216 cso->delete_state =
1217 (cso_state_callback) ctx->pipe->delete_sampler_state;
1218 cso->context = ctx->pipe;
1219 cso->hash_key = hash_key;
1220
1221 iter = cso_insert_state(ctx->cache, hash_key, CSO_SAMPLER, cso);
1222 if (cso_hash_iter_is_null(iter)) {
1223 FREE(cso);
1224 return PIPE_ERROR_OUT_OF_MEMORY;
1225 }
1226 }
1227 else {
1228 cso = cso_hash_iter_data(iter);
1229 }
1230
1231 ctx->samplers[shader_stage].cso_samplers[idx] = cso;
1232 ctx->samplers[shader_stage].samplers[idx] = cso->data;
1233 } else {
1234 ctx->samplers[shader_stage].cso_samplers[idx] = NULL;
1235 ctx->samplers[shader_stage].samplers[idx] = NULL;
1236 }
1237
1238 return PIPE_OK;
1239 }
1240
1241
1242 /**
1243 * Send staged sampler state to the driver.
1244 */
1245 void
1246 cso_single_sampler_done(struct cso_context *ctx,
1247 enum pipe_shader_type shader_stage)
1248 {
1249 struct sampler_info *info = &ctx->samplers[shader_stage];
1250 const unsigned old_nr_samplers = info->nr_samplers;
1251 unsigned i;
1252
1253 /* find highest non-null sampler */
1254 for (i = PIPE_MAX_SAMPLERS; i > 0; i--) {
1255 if (info->samplers[i - 1] != NULL)
1256 break;
1257 }
1258
1259 info->nr_samplers = i;
1260 ctx->pipe->bind_sampler_states(ctx->pipe, shader_stage, 0,
1261 MAX2(old_nr_samplers, info->nr_samplers),
1262 info->samplers);
1263 }
1264
1265
1266 /*
1267 * If the function encouters any errors it will return the
1268 * last one. Done to always try to set as many samplers
1269 * as possible.
1270 */
1271 enum pipe_error
1272 cso_set_samplers(struct cso_context *ctx,
1273 enum pipe_shader_type shader_stage,
1274 unsigned nr,
1275 const struct pipe_sampler_state **templates)
1276 {
1277 struct sampler_info *info = &ctx->samplers[shader_stage];
1278 unsigned i;
1279 enum pipe_error temp, error = PIPE_OK;
1280
1281 for (i = 0; i < nr; i++) {
1282 temp = cso_single_sampler(ctx, shader_stage, i, templates[i]);
1283 if (temp != PIPE_OK)
1284 error = temp;
1285 }
1286
1287 for ( ; i < info->nr_samplers; i++) {
1288 temp = cso_single_sampler(ctx, shader_stage, i, NULL);
1289 if (temp != PIPE_OK)
1290 error = temp;
1291 }
1292
1293 cso_single_sampler_done(ctx, shader_stage);
1294
1295 return error;
1296 }
1297
1298 static void
1299 cso_save_fragment_samplers(struct cso_context *ctx)
1300 {
1301 struct sampler_info *info = &ctx->samplers[PIPE_SHADER_FRAGMENT];
1302 struct sampler_info *saved = &ctx->fragment_samplers_saved;
1303
1304 saved->nr_samplers = info->nr_samplers;
1305 memcpy(saved->cso_samplers, info->cso_samplers, info->nr_samplers *
1306 sizeof(*info->cso_samplers));
1307 memcpy(saved->samplers, info->samplers, info->nr_samplers *
1308 sizeof(*info->samplers));
1309 }
1310
1311
1312 static void
1313 cso_restore_fragment_samplers(struct cso_context *ctx)
1314 {
1315 struct sampler_info *info = &ctx->samplers[PIPE_SHADER_FRAGMENT];
1316 struct sampler_info *saved = &ctx->fragment_samplers_saved;
1317 int delta = (int)info->nr_samplers - saved->nr_samplers;
1318
1319 memcpy(info->cso_samplers, saved->cso_samplers,
1320 saved->nr_samplers * sizeof(*info->cso_samplers));
1321 memcpy(info->samplers, saved->samplers,
1322 saved->nr_samplers * sizeof(*info->samplers));
1323
1324 if (delta > 0) {
1325 memset(&info->cso_samplers[saved->nr_samplers], 0,
1326 delta * sizeof(*info->cso_samplers));
1327 memset(&info->samplers[saved->nr_samplers], 0,
1328 delta * sizeof(*info->samplers));
1329 }
1330
1331 cso_single_sampler_done(ctx, PIPE_SHADER_FRAGMENT);
1332 }
1333
1334
1335 void
1336 cso_set_sampler_views(struct cso_context *ctx,
1337 enum pipe_shader_type shader_stage,
1338 unsigned count,
1339 struct pipe_sampler_view **views)
1340 {
1341 if (shader_stage == PIPE_SHADER_FRAGMENT) {
1342 unsigned i;
1343 boolean any_change = FALSE;
1344
1345 /* reference new views */
1346 for (i = 0; i < count; i++) {
1347 any_change |= ctx->fragment_views[i] != views[i];
1348 pipe_sampler_view_reference(&ctx->fragment_views[i], views[i]);
1349 }
1350 /* unref extra old views, if any */
1351 for (; i < ctx->nr_fragment_views; i++) {
1352 any_change |= ctx->fragment_views[i] != NULL;
1353 pipe_sampler_view_reference(&ctx->fragment_views[i], NULL);
1354 }
1355
1356 /* bind the new sampler views */
1357 if (any_change) {
1358 ctx->pipe->set_sampler_views(ctx->pipe, shader_stage, 0,
1359 MAX2(ctx->nr_fragment_views, count),
1360 ctx->fragment_views);
1361 }
1362
1363 ctx->nr_fragment_views = count;
1364 }
1365 else
1366 ctx->pipe->set_sampler_views(ctx->pipe, shader_stage, 0, count, views);
1367 }
1368
1369
1370 static void
1371 cso_save_fragment_sampler_views(struct cso_context *ctx)
1372 {
1373 unsigned i;
1374
1375 ctx->nr_fragment_views_saved = ctx->nr_fragment_views;
1376
1377 for (i = 0; i < ctx->nr_fragment_views; i++) {
1378 assert(!ctx->fragment_views_saved[i]);
1379 pipe_sampler_view_reference(&ctx->fragment_views_saved[i],
1380 ctx->fragment_views[i]);
1381 }
1382 }
1383
1384
1385 static void
1386 cso_restore_fragment_sampler_views(struct cso_context *ctx)
1387 {
1388 unsigned i, nr_saved = ctx->nr_fragment_views_saved;
1389 unsigned num;
1390
1391 for (i = 0; i < nr_saved; i++) {
1392 pipe_sampler_view_reference(&ctx->fragment_views[i], NULL);
1393 /* move the reference from one pointer to another */
1394 ctx->fragment_views[i] = ctx->fragment_views_saved[i];
1395 ctx->fragment_views_saved[i] = NULL;
1396 }
1397 for (; i < ctx->nr_fragment_views; i++) {
1398 pipe_sampler_view_reference(&ctx->fragment_views[i], NULL);
1399 }
1400
1401 num = MAX2(ctx->nr_fragment_views, nr_saved);
1402
1403 /* bind the old/saved sampler views */
1404 ctx->pipe->set_sampler_views(ctx->pipe, PIPE_SHADER_FRAGMENT, 0, num,
1405 ctx->fragment_views);
1406
1407 ctx->nr_fragment_views = nr_saved;
1408 ctx->nr_fragment_views_saved = 0;
1409 }
1410
1411
1412 void
1413 cso_set_shader_images(struct cso_context *ctx,
1414 enum pipe_shader_type shader_stage,
1415 unsigned start, unsigned count,
1416 struct pipe_image_view *images)
1417 {
1418 if (shader_stage == PIPE_SHADER_FRAGMENT && start == 0 && count >= 1) {
1419 util_copy_image_view(&ctx->fragment_image0_current, &images[0]);
1420 }
1421
1422 ctx->pipe->set_shader_images(ctx->pipe, shader_stage, start, count, images);
1423 }
1424
1425
1426 static void
1427 cso_save_fragment_image0(struct cso_context *ctx)
1428 {
1429 util_copy_image_view(&ctx->fragment_image0_saved,
1430 &ctx->fragment_image0_current);
1431 }
1432
1433
1434 static void
1435 cso_restore_fragment_image0(struct cso_context *ctx)
1436 {
1437 cso_set_shader_images(ctx, PIPE_SHADER_FRAGMENT, 0, 1,
1438 &ctx->fragment_image0_saved);
1439 }
1440
1441
1442 void
1443 cso_set_stream_outputs(struct cso_context *ctx,
1444 unsigned num_targets,
1445 struct pipe_stream_output_target **targets,
1446 const unsigned *offsets)
1447 {
1448 struct pipe_context *pipe = ctx->pipe;
1449 uint i;
1450
1451 if (!ctx->has_streamout) {
1452 assert(num_targets == 0);
1453 return;
1454 }
1455
1456 if (ctx->nr_so_targets == 0 && num_targets == 0) {
1457 /* Nothing to do. */
1458 return;
1459 }
1460
1461 /* reference new targets */
1462 for (i = 0; i < num_targets; i++) {
1463 pipe_so_target_reference(&ctx->so_targets[i], targets[i]);
1464 }
1465 /* unref extra old targets, if any */
1466 for (; i < ctx->nr_so_targets; i++) {
1467 pipe_so_target_reference(&ctx->so_targets[i], NULL);
1468 }
1469
1470 pipe->set_stream_output_targets(pipe, num_targets, targets,
1471 offsets);
1472 ctx->nr_so_targets = num_targets;
1473 }
1474
1475 static void
1476 cso_save_stream_outputs(struct cso_context *ctx)
1477 {
1478 uint i;
1479
1480 if (!ctx->has_streamout) {
1481 return;
1482 }
1483
1484 ctx->nr_so_targets_saved = ctx->nr_so_targets;
1485
1486 for (i = 0; i < ctx->nr_so_targets; i++) {
1487 assert(!ctx->so_targets_saved[i]);
1488 pipe_so_target_reference(&ctx->so_targets_saved[i], ctx->so_targets[i]);
1489 }
1490 }
1491
1492 static void
1493 cso_restore_stream_outputs(struct cso_context *ctx)
1494 {
1495 struct pipe_context *pipe = ctx->pipe;
1496 uint i;
1497 unsigned offset[PIPE_MAX_SO_BUFFERS];
1498
1499 if (!ctx->has_streamout) {
1500 return;
1501 }
1502
1503 if (ctx->nr_so_targets == 0 && ctx->nr_so_targets_saved == 0) {
1504 /* Nothing to do. */
1505 return;
1506 }
1507
1508 assert(ctx->nr_so_targets_saved <= PIPE_MAX_SO_BUFFERS);
1509 for (i = 0; i < ctx->nr_so_targets_saved; i++) {
1510 pipe_so_target_reference(&ctx->so_targets[i], NULL);
1511 /* move the reference from one pointer to another */
1512 ctx->so_targets[i] = ctx->so_targets_saved[i];
1513 ctx->so_targets_saved[i] = NULL;
1514 /* -1 means append */
1515 offset[i] = (unsigned)-1;
1516 }
1517 for (; i < ctx->nr_so_targets; i++) {
1518 pipe_so_target_reference(&ctx->so_targets[i], NULL);
1519 }
1520
1521 pipe->set_stream_output_targets(pipe, ctx->nr_so_targets_saved,
1522 ctx->so_targets, offset);
1523
1524 ctx->nr_so_targets = ctx->nr_so_targets_saved;
1525 ctx->nr_so_targets_saved = 0;
1526 }
1527
1528 /* constant buffers */
1529
1530 void
1531 cso_set_constant_buffer(struct cso_context *cso,
1532 enum pipe_shader_type shader_stage,
1533 unsigned index, struct pipe_constant_buffer *cb)
1534 {
1535 struct pipe_context *pipe = cso->pipe;
1536
1537 pipe->set_constant_buffer(pipe, shader_stage, index, cb);
1538
1539 if (index == 0) {
1540 util_copy_constant_buffer(&cso->aux_constbuf_current[shader_stage], cb);
1541 }
1542 }
1543
1544 void
1545 cso_set_constant_buffer_resource(struct cso_context *cso,
1546 enum pipe_shader_type shader_stage,
1547 unsigned index,
1548 struct pipe_resource *buffer)
1549 {
1550 if (buffer) {
1551 struct pipe_constant_buffer cb;
1552 cb.buffer = buffer;
1553 cb.buffer_offset = 0;
1554 cb.buffer_size = buffer->width0;
1555 cb.user_buffer = NULL;
1556 cso_set_constant_buffer(cso, shader_stage, index, &cb);
1557 } else {
1558 cso_set_constant_buffer(cso, shader_stage, index, NULL);
1559 }
1560 }
1561
1562 void
1563 cso_save_constant_buffer_slot0(struct cso_context *cso,
1564 enum pipe_shader_type shader_stage)
1565 {
1566 util_copy_constant_buffer(&cso->aux_constbuf_saved[shader_stage],
1567 &cso->aux_constbuf_current[shader_stage]);
1568 }
1569
1570 void
1571 cso_restore_constant_buffer_slot0(struct cso_context *cso,
1572 enum pipe_shader_type shader_stage)
1573 {
1574 cso_set_constant_buffer(cso, shader_stage, 0,
1575 &cso->aux_constbuf_saved[shader_stage]);
1576 pipe_resource_reference(&cso->aux_constbuf_saved[shader_stage].buffer,
1577 NULL);
1578 }
1579
1580
1581 /**
1582 * Save all the CSO state items specified by the state_mask bitmask
1583 * of CSO_BIT_x flags.
1584 */
1585 void
1586 cso_save_state(struct cso_context *cso, unsigned state_mask)
1587 {
1588 assert(cso->saved_state == 0);
1589
1590 cso->saved_state = state_mask;
1591
1592 if (state_mask & CSO_BIT_AUX_VERTEX_BUFFER_SLOT)
1593 cso_save_aux_vertex_buffer_slot(cso);
1594 if (state_mask & CSO_BIT_BLEND)
1595 cso_save_blend(cso);
1596 if (state_mask & CSO_BIT_DEPTH_STENCIL_ALPHA)
1597 cso_save_depth_stencil_alpha(cso);
1598 if (state_mask & CSO_BIT_FRAGMENT_SAMPLERS)
1599 cso_save_fragment_samplers(cso);
1600 if (state_mask & CSO_BIT_FRAGMENT_SAMPLER_VIEWS)
1601 cso_save_fragment_sampler_views(cso);
1602 if (state_mask & CSO_BIT_FRAGMENT_SHADER)
1603 cso_save_fragment_shader(cso);
1604 if (state_mask & CSO_BIT_FRAMEBUFFER)
1605 cso_save_framebuffer(cso);
1606 if (state_mask & CSO_BIT_GEOMETRY_SHADER)
1607 cso_save_geometry_shader(cso);
1608 if (state_mask & CSO_BIT_MIN_SAMPLES)
1609 cso_save_min_samples(cso);
1610 if (state_mask & CSO_BIT_RASTERIZER)
1611 cso_save_rasterizer(cso);
1612 if (state_mask & CSO_BIT_RENDER_CONDITION)
1613 cso_save_render_condition(cso);
1614 if (state_mask & CSO_BIT_SAMPLE_MASK)
1615 cso_save_sample_mask(cso);
1616 if (state_mask & CSO_BIT_STENCIL_REF)
1617 cso_save_stencil_ref(cso);
1618 if (state_mask & CSO_BIT_STREAM_OUTPUTS)
1619 cso_save_stream_outputs(cso);
1620 if (state_mask & CSO_BIT_TESSCTRL_SHADER)
1621 cso_save_tessctrl_shader(cso);
1622 if (state_mask & CSO_BIT_TESSEVAL_SHADER)
1623 cso_save_tesseval_shader(cso);
1624 if (state_mask & CSO_BIT_VERTEX_ELEMENTS)
1625 cso_save_vertex_elements(cso);
1626 if (state_mask & CSO_BIT_VERTEX_SHADER)
1627 cso_save_vertex_shader(cso);
1628 if (state_mask & CSO_BIT_VIEWPORT)
1629 cso_save_viewport(cso);
1630 if (state_mask & CSO_BIT_PAUSE_QUERIES)
1631 cso->pipe->set_active_query_state(cso->pipe, false);
1632 if (state_mask & CSO_BIT_FRAGMENT_IMAGE0)
1633 cso_save_fragment_image0(cso);
1634 }
1635
1636
1637 /**
1638 * Restore the state which was saved by cso_save_state().
1639 */
1640 void
1641 cso_restore_state(struct cso_context *cso)
1642 {
1643 unsigned state_mask = cso->saved_state;
1644
1645 assert(state_mask);
1646
1647 if (state_mask & CSO_BIT_AUX_VERTEX_BUFFER_SLOT)
1648 cso_restore_aux_vertex_buffer_slot(cso);
1649 if (state_mask & CSO_BIT_BLEND)
1650 cso_restore_blend(cso);
1651 if (state_mask & CSO_BIT_DEPTH_STENCIL_ALPHA)
1652 cso_restore_depth_stencil_alpha(cso);
1653 if (state_mask & CSO_BIT_FRAGMENT_SAMPLERS)
1654 cso_restore_fragment_samplers(cso);
1655 if (state_mask & CSO_BIT_FRAGMENT_SAMPLER_VIEWS)
1656 cso_restore_fragment_sampler_views(cso);
1657 if (state_mask & CSO_BIT_FRAGMENT_SHADER)
1658 cso_restore_fragment_shader(cso);
1659 if (state_mask & CSO_BIT_FRAMEBUFFER)
1660 cso_restore_framebuffer(cso);
1661 if (state_mask & CSO_BIT_GEOMETRY_SHADER)
1662 cso_restore_geometry_shader(cso);
1663 if (state_mask & CSO_BIT_MIN_SAMPLES)
1664 cso_restore_min_samples(cso);
1665 if (state_mask & CSO_BIT_RASTERIZER)
1666 cso_restore_rasterizer(cso);
1667 if (state_mask & CSO_BIT_RENDER_CONDITION)
1668 cso_restore_render_condition(cso);
1669 if (state_mask & CSO_BIT_SAMPLE_MASK)
1670 cso_restore_sample_mask(cso);
1671 if (state_mask & CSO_BIT_STENCIL_REF)
1672 cso_restore_stencil_ref(cso);
1673 if (state_mask & CSO_BIT_STREAM_OUTPUTS)
1674 cso_restore_stream_outputs(cso);
1675 if (state_mask & CSO_BIT_TESSCTRL_SHADER)
1676 cso_restore_tessctrl_shader(cso);
1677 if (state_mask & CSO_BIT_TESSEVAL_SHADER)
1678 cso_restore_tesseval_shader(cso);
1679 if (state_mask & CSO_BIT_VERTEX_ELEMENTS)
1680 cso_restore_vertex_elements(cso);
1681 if (state_mask & CSO_BIT_VERTEX_SHADER)
1682 cso_restore_vertex_shader(cso);
1683 if (state_mask & CSO_BIT_VIEWPORT)
1684 cso_restore_viewport(cso);
1685 if (state_mask & CSO_BIT_PAUSE_QUERIES)
1686 cso->pipe->set_active_query_state(cso->pipe, true);
1687 if (state_mask & CSO_BIT_FRAGMENT_IMAGE0)
1688 cso_restore_fragment_image0(cso);
1689
1690 cso->saved_state = 0;
1691 }
1692
1693
1694
1695 /* drawing */
1696
1697 void
1698 cso_draw_vbo(struct cso_context *cso,
1699 const struct pipe_draw_info *info)
1700 {
1701 struct u_vbuf *vbuf = cso->vbuf;
1702
1703 if (vbuf) {
1704 u_vbuf_draw_vbo(vbuf, info);
1705 } else {
1706 struct pipe_context *pipe = cso->pipe;
1707 pipe->draw_vbo(pipe, info);
1708 }
1709 }
1710
1711 void
1712 cso_draw_arrays(struct cso_context *cso, uint mode, uint start, uint count)
1713 {
1714 struct pipe_draw_info info;
1715
1716 util_draw_init_info(&info);
1717
1718 info.mode = mode;
1719 info.start = start;
1720 info.count = count;
1721 info.min_index = start;
1722 info.max_index = start + count - 1;
1723
1724 cso_draw_vbo(cso, &info);
1725 }
1726
1727 void
1728 cso_draw_arrays_instanced(struct cso_context *cso, uint mode,
1729 uint start, uint count,
1730 uint start_instance, uint instance_count)
1731 {
1732 struct pipe_draw_info info;
1733
1734 util_draw_init_info(&info);
1735
1736 info.mode = mode;
1737 info.start = start;
1738 info.count = count;
1739 info.min_index = start;
1740 info.max_index = start + count - 1;
1741 info.start_instance = start_instance;
1742 info.instance_count = instance_count;
1743
1744 cso_draw_vbo(cso, &info);
1745 }