Merge remote branch 'origin/opengl-es-v2'
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_context.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 <zack@tungstengraphics.com>
35 * @author Keith Whitwell <keith@tungstengraphics.com>
36 */
37
38 #include "pipe/p_state.h"
39 #include "util/u_memory.h"
40 #include "tgsi/tgsi_parse.h"
41
42 #include "cso_cache/cso_context.h"
43 #include "cso_cache/cso_cache.h"
44 #include "cso_cache/cso_hash.h"
45 #include "cso_context.h"
46
47 struct cso_context {
48 struct pipe_context *pipe;
49 struct cso_cache *cache;
50
51 struct {
52 void *samplers[PIPE_MAX_SAMPLERS];
53 unsigned nr_samplers;
54
55 void *vertex_samplers[PIPE_MAX_VERTEX_SAMPLERS];
56 unsigned nr_vertex_samplers;
57 } hw;
58
59 void *samplers[PIPE_MAX_SAMPLERS];
60 unsigned nr_samplers;
61
62 void *vertex_samplers[PIPE_MAX_VERTEX_SAMPLERS];
63 unsigned nr_vertex_samplers;
64
65 unsigned nr_samplers_saved;
66 void *samplers_saved[PIPE_MAX_SAMPLERS];
67
68 unsigned nr_vertex_samplers_saved;
69 void *vertex_samplers_saved[PIPE_MAX_VERTEX_SAMPLERS];
70
71 struct pipe_texture *textures[PIPE_MAX_SAMPLERS];
72 uint nr_textures;
73
74 struct pipe_texture *vertex_textures[PIPE_MAX_VERTEX_SAMPLERS];
75 uint nr_vertex_textures;
76
77 uint nr_textures_saved;
78 struct pipe_texture *textures_saved[PIPE_MAX_SAMPLERS];
79
80 uint nr_vertex_textures_saved;
81 struct pipe_texture *vertex_textures_saved[PIPE_MAX_SAMPLERS];
82
83 /** Current and saved state.
84 * The saved state is used as a 1-deep stack.
85 */
86 void *blend, *blend_saved;
87 void *depth_stencil, *depth_stencil_saved;
88 void *rasterizer, *rasterizer_saved;
89 void *fragment_shader, *fragment_shader_saved, *geometry_shader;
90 void *vertex_shader, *vertex_shader_saved, *geometry_shader_saved;
91
92 struct pipe_framebuffer_state fb, fb_saved;
93 struct pipe_viewport_state vp, vp_saved;
94 struct pipe_blend_color blend_color;
95 };
96
97
98 static void
99 free_framebuffer_state(struct pipe_framebuffer_state *fb);
100
101
102 static boolean delete_blend_state(struct cso_context *ctx, void *state)
103 {
104 struct cso_blend *cso = (struct cso_blend *)state;
105
106 if (ctx->blend == cso->data)
107 return FALSE;
108
109 if (cso->delete_state)
110 cso->delete_state(cso->context, cso->data);
111 FREE(state);
112 return TRUE;
113 }
114
115 static boolean delete_depth_stencil_state(struct cso_context *ctx, void *state)
116 {
117 struct cso_depth_stencil_alpha *cso = (struct cso_depth_stencil_alpha *)state;
118
119 if (ctx->depth_stencil == cso->data)
120 return FALSE;
121
122 if (cso->delete_state)
123 cso->delete_state(cso->context, cso->data);
124 FREE(state);
125
126 return TRUE;
127 }
128
129 static boolean delete_sampler_state(struct cso_context *ctx, void *state)
130 {
131 struct cso_sampler *cso = (struct cso_sampler *)state;
132 if (cso->delete_state)
133 cso->delete_state(cso->context, cso->data);
134 FREE(state);
135 return TRUE;
136 }
137
138 static boolean delete_rasterizer_state(struct cso_context *ctx, void *state)
139 {
140 struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
141
142 if (ctx->rasterizer == cso->data)
143 return FALSE;
144 if (cso->delete_state)
145 cso->delete_state(cso->context, cso->data);
146 FREE(state);
147 return TRUE;
148 }
149
150 static boolean delete_fs_state(struct cso_context *ctx, void *state)
151 {
152 struct cso_fragment_shader *cso = (struct cso_fragment_shader *)state;
153 if (ctx->fragment_shader == cso->data)
154 return FALSE;
155 if (cso->delete_state)
156 cso->delete_state(cso->context, cso->data);
157 FREE(state);
158 return TRUE;
159 }
160
161 static boolean delete_vs_state(struct cso_context *ctx, void *state)
162 {
163 struct cso_vertex_shader *cso = (struct cso_vertex_shader *)state;
164 if (ctx->vertex_shader == cso->data)
165 return TRUE;
166 if (cso->delete_state)
167 cso->delete_state(cso->context, cso->data);
168 FREE(state);
169 return FALSE;
170 }
171
172
173 static INLINE boolean delete_cso(struct cso_context *ctx,
174 void *state, enum cso_cache_type type)
175 {
176 switch (type) {
177 case CSO_BLEND:
178 return delete_blend_state(ctx, state);
179 break;
180 case CSO_SAMPLER:
181 return delete_sampler_state(ctx, state);
182 break;
183 case CSO_DEPTH_STENCIL_ALPHA:
184 return delete_depth_stencil_state(ctx, state);
185 break;
186 case CSO_RASTERIZER:
187 return delete_rasterizer_state(ctx, state);
188 break;
189 case CSO_FRAGMENT_SHADER:
190 return delete_fs_state(ctx, state);
191 break;
192 case CSO_VERTEX_SHADER:
193 return delete_vs_state(ctx, state);
194 break;
195 default:
196 assert(0);
197 FREE(state);
198 }
199 return FALSE;
200 }
201
202 static INLINE void sanitize_hash(struct cso_hash *hash, enum cso_cache_type type,
203 int max_size, void *user_data)
204 {
205 struct cso_context *ctx = (struct cso_context *)user_data;
206 /* if we're approach the maximum size, remove fourth of the entries
207 * otherwise every subsequent call will go through the same */
208 int hash_size = cso_hash_size(hash);
209 int max_entries = (max_size > hash_size) ? max_size : hash_size;
210 int to_remove = (max_size < max_entries) * max_entries/4;
211 struct cso_hash_iter iter = cso_hash_first_node(hash);
212 if (hash_size > max_size)
213 to_remove += hash_size - max_size;
214 while (to_remove) {
215 /*remove elements until we're good */
216 /*fixme: currently we pick the nodes to remove at random*/
217 void *cso = cso_hash_iter_data(iter);
218 if (delete_cso(ctx, cso, type)) {
219 iter = cso_hash_erase(hash, iter);
220 --to_remove;
221 } else
222 iter = cso_hash_iter_next(iter);
223 }
224 }
225
226
227 struct cso_context *cso_create_context( struct pipe_context *pipe )
228 {
229 struct cso_context *ctx = CALLOC_STRUCT(cso_context);
230 if (ctx == NULL)
231 goto out;
232
233 ctx->cache = cso_cache_create();
234 if (ctx->cache == NULL)
235 goto out;
236 cso_cache_set_sanitize_callback(ctx->cache,
237 sanitize_hash,
238 ctx);
239
240 ctx->pipe = pipe;
241
242 /* Enable for testing: */
243 if (0) cso_set_maximum_cache_size( ctx->cache, 4 );
244
245 return ctx;
246
247 out:
248 cso_destroy_context( ctx );
249 return NULL;
250 }
251
252
253 /**
254 * Prior to context destruction, this function unbinds all state objects.
255 */
256 void cso_release_all( struct cso_context *ctx )
257 {
258 unsigned i;
259
260 if (ctx->pipe) {
261 ctx->pipe->bind_blend_state( ctx->pipe, NULL );
262 ctx->pipe->bind_rasterizer_state( ctx->pipe, NULL );
263 ctx->pipe->bind_fragment_sampler_states( ctx->pipe, 0, NULL );
264 if (ctx->pipe->bind_vertex_sampler_states)
265 ctx->pipe->bind_vertex_sampler_states(ctx->pipe, 0, NULL);
266 ctx->pipe->bind_depth_stencil_alpha_state( ctx->pipe, NULL );
267 ctx->pipe->bind_fs_state( ctx->pipe, NULL );
268 ctx->pipe->bind_vs_state( ctx->pipe, NULL );
269 }
270
271 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
272 pipe_texture_reference(&ctx->textures[i], NULL);
273 pipe_texture_reference(&ctx->textures_saved[i], NULL);
274 }
275
276 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
277 pipe_texture_reference(&ctx->vertex_textures[i], NULL);
278 pipe_texture_reference(&ctx->vertex_textures_saved[i], NULL);
279 }
280
281 free_framebuffer_state(&ctx->fb);
282 free_framebuffer_state(&ctx->fb_saved);
283
284 if (ctx->cache) {
285 cso_cache_delete( ctx->cache );
286 ctx->cache = NULL;
287 }
288 }
289
290
291 void cso_destroy_context( struct cso_context *ctx )
292 {
293 if (ctx) {
294 /*cso_release_all( ctx );*/
295 FREE( ctx );
296 }
297 }
298
299
300 /* Those function will either find the state of the given template
301 * in the cache or they will create a new state from the given
302 * template, insert it in the cache and return it.
303 */
304
305 /*
306 * If the driver returns 0 from the create method then they will assign
307 * the data member of the cso to be the template itself.
308 */
309
310 enum pipe_error cso_set_blend(struct cso_context *ctx,
311 const struct pipe_blend_state *templ)
312 {
313 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_blend_state));
314 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
315 hash_key, CSO_BLEND,
316 (void*)templ);
317 void *handle;
318
319 if (cso_hash_iter_is_null(iter)) {
320 struct cso_blend *cso = MALLOC(sizeof(struct cso_blend));
321 if (!cso)
322 return PIPE_ERROR_OUT_OF_MEMORY;
323
324 memcpy(&cso->state, templ, sizeof(*templ));
325 cso->data = ctx->pipe->create_blend_state(ctx->pipe, &cso->state);
326 cso->delete_state = (cso_state_callback)ctx->pipe->delete_blend_state;
327 cso->context = ctx->pipe;
328
329 iter = cso_insert_state(ctx->cache, hash_key, CSO_BLEND, cso);
330 if (cso_hash_iter_is_null(iter)) {
331 FREE(cso);
332 return PIPE_ERROR_OUT_OF_MEMORY;
333 }
334
335 handle = cso->data;
336 }
337 else {
338 handle = ((struct cso_blend *)cso_hash_iter_data(iter))->data;
339 }
340
341 if (ctx->blend != handle) {
342 ctx->blend = handle;
343 ctx->pipe->bind_blend_state(ctx->pipe, handle);
344 }
345 return PIPE_OK;
346 }
347
348 void cso_save_blend(struct cso_context *ctx)
349 {
350 assert(!ctx->blend_saved);
351 ctx->blend_saved = ctx->blend;
352 }
353
354 void cso_restore_blend(struct cso_context *ctx)
355 {
356 if (ctx->blend != ctx->blend_saved) {
357 ctx->blend = ctx->blend_saved;
358 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend_saved);
359 }
360 ctx->blend_saved = NULL;
361 }
362
363
364
365 enum pipe_error cso_single_sampler(struct cso_context *ctx,
366 unsigned idx,
367 const struct pipe_sampler_state *templ)
368 {
369 void *handle = NULL;
370
371 if (templ != NULL) {
372 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_sampler_state));
373 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
374 hash_key, CSO_SAMPLER,
375 (void*)templ);
376
377 if (cso_hash_iter_is_null(iter)) {
378 struct cso_sampler *cso = MALLOC(sizeof(struct cso_sampler));
379 if (!cso)
380 return PIPE_ERROR_OUT_OF_MEMORY;
381
382 memcpy(&cso->state, templ, sizeof(*templ));
383 cso->data = ctx->pipe->create_sampler_state(ctx->pipe, &cso->state);
384 cso->delete_state = (cso_state_callback)ctx->pipe->delete_sampler_state;
385 cso->context = ctx->pipe;
386
387 iter = cso_insert_state(ctx->cache, hash_key, CSO_SAMPLER, cso);
388 if (cso_hash_iter_is_null(iter)) {
389 FREE(cso);
390 return PIPE_ERROR_OUT_OF_MEMORY;
391 }
392
393 handle = cso->data;
394 }
395 else {
396 handle = ((struct cso_sampler *)cso_hash_iter_data(iter))->data;
397 }
398 }
399
400 ctx->samplers[idx] = handle;
401 return PIPE_OK;
402 }
403
404 enum pipe_error
405 cso_single_vertex_sampler(struct cso_context *ctx,
406 unsigned idx,
407 const struct pipe_sampler_state *templ)
408 {
409 void *handle = NULL;
410
411 if (templ != NULL) {
412 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_sampler_state));
413 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
414 hash_key, CSO_SAMPLER,
415 (void*)templ);
416
417 if (cso_hash_iter_is_null(iter)) {
418 struct cso_sampler *cso = MALLOC(sizeof(struct cso_sampler));
419 if (!cso)
420 return PIPE_ERROR_OUT_OF_MEMORY;
421
422 memcpy(&cso->state, templ, sizeof(*templ));
423 cso->data = ctx->pipe->create_sampler_state(ctx->pipe, &cso->state);
424 cso->delete_state = (cso_state_callback)ctx->pipe->delete_sampler_state;
425 cso->context = ctx->pipe;
426
427 iter = cso_insert_state(ctx->cache, hash_key, CSO_SAMPLER, cso);
428 if (cso_hash_iter_is_null(iter)) {
429 FREE(cso);
430 return PIPE_ERROR_OUT_OF_MEMORY;
431 }
432
433 handle = cso->data;
434 }
435 else {
436 handle = ((struct cso_sampler *)cso_hash_iter_data(iter))->data;
437 }
438 }
439
440 ctx->vertex_samplers[idx] = handle;
441 return PIPE_OK;
442 }
443
444 void cso_single_sampler_done( struct cso_context *ctx )
445 {
446 unsigned i;
447
448 /* find highest non-null sampler */
449 for (i = PIPE_MAX_SAMPLERS; i > 0; i--) {
450 if (ctx->samplers[i - 1] != NULL)
451 break;
452 }
453
454 ctx->nr_samplers = i;
455
456 if (ctx->hw.nr_samplers != ctx->nr_samplers ||
457 memcmp(ctx->hw.samplers,
458 ctx->samplers,
459 ctx->nr_samplers * sizeof(void *)) != 0)
460 {
461 memcpy(ctx->hw.samplers, ctx->samplers, ctx->nr_samplers * sizeof(void *));
462 ctx->hw.nr_samplers = ctx->nr_samplers;
463
464 ctx->pipe->bind_fragment_sampler_states(ctx->pipe, ctx->nr_samplers, ctx->samplers);
465 }
466 }
467
468 void
469 cso_single_vertex_sampler_done(struct cso_context *ctx)
470 {
471 unsigned i;
472
473 /* find highest non-null sampler */
474 for (i = PIPE_MAX_VERTEX_SAMPLERS; i > 0; i--) {
475 if (ctx->vertex_samplers[i - 1] != NULL)
476 break;
477 }
478
479 ctx->nr_vertex_samplers = i;
480
481 if (ctx->hw.nr_vertex_samplers != ctx->nr_vertex_samplers ||
482 memcmp(ctx->hw.vertex_samplers,
483 ctx->vertex_samplers,
484 ctx->nr_vertex_samplers * sizeof(void *)) != 0)
485 {
486 memcpy(ctx->hw.vertex_samplers,
487 ctx->vertex_samplers,
488 ctx->nr_vertex_samplers * sizeof(void *));
489 ctx->hw.nr_vertex_samplers = ctx->nr_vertex_samplers;
490
491 ctx->pipe->bind_vertex_sampler_states(ctx->pipe,
492 ctx->nr_vertex_samplers,
493 ctx->vertex_samplers);
494 }
495 }
496
497 /*
498 * If the function encouters any errors it will return the
499 * last one. Done to always try to set as many samplers
500 * as possible.
501 */
502 enum pipe_error cso_set_samplers( struct cso_context *ctx,
503 unsigned nr,
504 const struct pipe_sampler_state **templates )
505 {
506 unsigned i;
507 enum pipe_error temp, error = PIPE_OK;
508
509 /* TODO: fastpath
510 */
511
512 for (i = 0; i < nr; i++) {
513 temp = cso_single_sampler( ctx, i, templates[i] );
514 if (temp != PIPE_OK)
515 error = temp;
516 }
517
518 for ( ; i < ctx->nr_samplers; i++) {
519 temp = cso_single_sampler( ctx, i, NULL );
520 if (temp != PIPE_OK)
521 error = temp;
522 }
523
524 cso_single_sampler_done( ctx );
525
526 return error;
527 }
528
529 void cso_save_samplers(struct cso_context *ctx)
530 {
531 ctx->nr_samplers_saved = ctx->nr_samplers;
532 memcpy(ctx->samplers_saved, ctx->samplers, sizeof(ctx->samplers));
533 }
534
535 void cso_restore_samplers(struct cso_context *ctx)
536 {
537 ctx->nr_samplers = ctx->nr_samplers_saved;
538 memcpy(ctx->samplers, ctx->samplers_saved, sizeof(ctx->samplers));
539 cso_single_sampler_done( ctx );
540 }
541
542 /*
543 * If the function encouters any errors it will return the
544 * last one. Done to always try to set as many samplers
545 * as possible.
546 */
547 enum pipe_error cso_set_vertex_samplers(struct cso_context *ctx,
548 unsigned nr,
549 const struct pipe_sampler_state **templates)
550 {
551 unsigned i;
552 enum pipe_error temp, error = PIPE_OK;
553
554 /* TODO: fastpath
555 */
556
557 for (i = 0; i < nr; i++) {
558 temp = cso_single_vertex_sampler( ctx, i, templates[i] );
559 if (temp != PIPE_OK)
560 error = temp;
561 }
562
563 for ( ; i < ctx->nr_samplers; i++) {
564 temp = cso_single_vertex_sampler( ctx, i, NULL );
565 if (temp != PIPE_OK)
566 error = temp;
567 }
568
569 cso_single_vertex_sampler_done( ctx );
570
571 return error;
572 }
573
574 void
575 cso_save_vertex_samplers(struct cso_context *ctx)
576 {
577 ctx->nr_vertex_samplers_saved = ctx->nr_vertex_samplers;
578 memcpy(ctx->vertex_samplers_saved, ctx->vertex_samplers, sizeof(ctx->vertex_samplers));
579 }
580
581 void
582 cso_restore_vertex_samplers(struct cso_context *ctx)
583 {
584 ctx->nr_vertex_samplers = ctx->nr_vertex_samplers_saved;
585 memcpy(ctx->vertex_samplers, ctx->vertex_samplers_saved, sizeof(ctx->vertex_samplers));
586 cso_single_vertex_sampler_done(ctx);
587 }
588
589
590 enum pipe_error cso_set_sampler_textures( struct cso_context *ctx,
591 uint count,
592 struct pipe_texture **textures )
593 {
594 uint i;
595
596 ctx->nr_textures = count;
597
598 for (i = 0; i < count; i++)
599 pipe_texture_reference(&ctx->textures[i], textures[i]);
600 for ( ; i < PIPE_MAX_SAMPLERS; i++)
601 pipe_texture_reference(&ctx->textures[i], NULL);
602
603 ctx->pipe->set_fragment_sampler_textures(ctx->pipe, count, textures);
604
605 return PIPE_OK;
606 }
607
608 void cso_save_sampler_textures( struct cso_context *ctx )
609 {
610 uint i;
611
612 ctx->nr_textures_saved = ctx->nr_textures;
613 for (i = 0; i < ctx->nr_textures; i++) {
614 assert(!ctx->textures_saved[i]);
615 pipe_texture_reference(&ctx->textures_saved[i], ctx->textures[i]);
616 }
617 }
618
619 void cso_restore_sampler_textures( struct cso_context *ctx )
620 {
621 uint i;
622
623 ctx->nr_textures = ctx->nr_textures_saved;
624
625 for (i = 0; i < ctx->nr_textures; i++) {
626 pipe_texture_reference(&ctx->textures[i], NULL);
627 ctx->textures[i] = ctx->textures_saved[i];
628 ctx->textures_saved[i] = NULL;
629 }
630 for ( ; i < PIPE_MAX_SAMPLERS; i++)
631 pipe_texture_reference(&ctx->textures[i], NULL);
632
633 ctx->pipe->set_fragment_sampler_textures(ctx->pipe, ctx->nr_textures, ctx->textures);
634
635 ctx->nr_textures_saved = 0;
636 }
637
638
639
640 enum pipe_error
641 cso_set_vertex_sampler_textures(struct cso_context *ctx,
642 uint count,
643 struct pipe_texture **textures)
644 {
645 uint i;
646
647 ctx->nr_vertex_textures = count;
648
649 for (i = 0; i < count; i++) {
650 pipe_texture_reference(&ctx->vertex_textures[i], textures[i]);
651 }
652 for ( ; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
653 pipe_texture_reference(&ctx->vertex_textures[i], NULL);
654 }
655
656 ctx->pipe->set_vertex_sampler_textures(ctx->pipe, count, textures);
657
658 return PIPE_OK;
659 }
660
661 void
662 cso_save_vertex_sampler_textures(struct cso_context *ctx)
663 {
664 uint i;
665
666 ctx->nr_vertex_textures_saved = ctx->nr_vertex_textures;
667 for (i = 0; i < ctx->nr_vertex_textures; i++) {
668 assert(!ctx->vertex_textures_saved[i]);
669 pipe_texture_reference(&ctx->vertex_textures_saved[i], ctx->vertex_textures[i]);
670 }
671 }
672
673 void
674 cso_restore_vertex_sampler_textures(struct cso_context *ctx)
675 {
676 uint i;
677
678 ctx->nr_vertex_textures = ctx->nr_vertex_textures_saved;
679
680 for (i = 0; i < ctx->nr_vertex_textures; i++) {
681 pipe_texture_reference(&ctx->vertex_textures[i], NULL);
682 ctx->vertex_textures[i] = ctx->vertex_textures_saved[i];
683 ctx->vertex_textures_saved[i] = NULL;
684 }
685 for ( ; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
686 pipe_texture_reference(&ctx->vertex_textures[i], NULL);
687 }
688
689 ctx->pipe->set_vertex_sampler_textures(ctx->pipe,
690 ctx->nr_vertex_textures,
691 ctx->vertex_textures);
692
693 ctx->nr_vertex_textures_saved = 0;
694 }
695
696
697
698 enum pipe_error cso_set_depth_stencil_alpha(struct cso_context *ctx,
699 const struct pipe_depth_stencil_alpha_state *templ)
700 {
701 unsigned hash_key = cso_construct_key((void*)templ,
702 sizeof(struct pipe_depth_stencil_alpha_state));
703 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
704 hash_key,
705 CSO_DEPTH_STENCIL_ALPHA,
706 (void*)templ);
707 void *handle;
708
709 if (cso_hash_iter_is_null(iter)) {
710 struct cso_depth_stencil_alpha *cso = MALLOC(sizeof(struct cso_depth_stencil_alpha));
711 if (!cso)
712 return PIPE_ERROR_OUT_OF_MEMORY;
713
714 memcpy(&cso->state, templ, sizeof(*templ));
715 cso->data = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &cso->state);
716 cso->delete_state = (cso_state_callback)ctx->pipe->delete_depth_stencil_alpha_state;
717 cso->context = ctx->pipe;
718
719 iter = cso_insert_state(ctx->cache, hash_key, CSO_DEPTH_STENCIL_ALPHA, cso);
720 if (cso_hash_iter_is_null(iter)) {
721 FREE(cso);
722 return PIPE_ERROR_OUT_OF_MEMORY;
723 }
724
725 handle = cso->data;
726 }
727 else {
728 handle = ((struct cso_depth_stencil_alpha *)cso_hash_iter_data(iter))->data;
729 }
730
731 if (ctx->depth_stencil != handle) {
732 ctx->depth_stencil = handle;
733 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, handle);
734 }
735 return PIPE_OK;
736 }
737
738 void cso_save_depth_stencil_alpha(struct cso_context *ctx)
739 {
740 assert(!ctx->depth_stencil_saved);
741 ctx->depth_stencil_saved = ctx->depth_stencil;
742 }
743
744 void cso_restore_depth_stencil_alpha(struct cso_context *ctx)
745 {
746 if (ctx->depth_stencil != ctx->depth_stencil_saved) {
747 ctx->depth_stencil = ctx->depth_stencil_saved;
748 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, ctx->depth_stencil_saved);
749 }
750 ctx->depth_stencil_saved = NULL;
751 }
752
753
754
755 enum pipe_error cso_set_rasterizer(struct cso_context *ctx,
756 const struct pipe_rasterizer_state *templ)
757 {
758 unsigned hash_key = cso_construct_key((void*)templ,
759 sizeof(struct pipe_rasterizer_state));
760 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
761 hash_key, CSO_RASTERIZER,
762 (void*)templ);
763 void *handle = NULL;
764
765 if (cso_hash_iter_is_null(iter)) {
766 struct cso_rasterizer *cso = MALLOC(sizeof(struct cso_rasterizer));
767 if (!cso)
768 return PIPE_ERROR_OUT_OF_MEMORY;
769
770 memcpy(&cso->state, templ, sizeof(*templ));
771 cso->data = ctx->pipe->create_rasterizer_state(ctx->pipe, &cso->state);
772 cso->delete_state = (cso_state_callback)ctx->pipe->delete_rasterizer_state;
773 cso->context = ctx->pipe;
774
775 iter = cso_insert_state(ctx->cache, hash_key, CSO_RASTERIZER, cso);
776 if (cso_hash_iter_is_null(iter)) {
777 FREE(cso);
778 return PIPE_ERROR_OUT_OF_MEMORY;
779 }
780
781 handle = cso->data;
782 }
783 else {
784 handle = ((struct cso_rasterizer *)cso_hash_iter_data(iter))->data;
785 }
786
787 if (ctx->rasterizer != handle) {
788 ctx->rasterizer = handle;
789 ctx->pipe->bind_rasterizer_state(ctx->pipe, handle);
790 }
791 return PIPE_OK;
792 }
793
794 void cso_save_rasterizer(struct cso_context *ctx)
795 {
796 assert(!ctx->rasterizer_saved);
797 ctx->rasterizer_saved = ctx->rasterizer;
798 }
799
800 void cso_restore_rasterizer(struct cso_context *ctx)
801 {
802 if (ctx->rasterizer != ctx->rasterizer_saved) {
803 ctx->rasterizer = ctx->rasterizer_saved;
804 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rasterizer_saved);
805 }
806 ctx->rasterizer_saved = NULL;
807 }
808
809
810
811 enum pipe_error cso_set_fragment_shader_handle(struct cso_context *ctx,
812 void *handle )
813 {
814 if (ctx->fragment_shader != handle) {
815 ctx->fragment_shader = handle;
816 ctx->pipe->bind_fs_state(ctx->pipe, handle);
817 }
818 return PIPE_OK;
819 }
820
821 void cso_delete_fragment_shader(struct cso_context *ctx, void *handle )
822 {
823 if (handle == ctx->fragment_shader) {
824 /* unbind before deleting */
825 ctx->pipe->bind_fs_state(ctx->pipe, NULL);
826 ctx->fragment_shader = NULL;
827 }
828 ctx->pipe->delete_fs_state(ctx->pipe, handle);
829 }
830
831 /* Not really working:
832 */
833 #if 0
834 enum pipe_error cso_set_fragment_shader(struct cso_context *ctx,
835 const struct pipe_shader_state *templ)
836 {
837 const struct tgsi_token *tokens = templ->tokens;
838 unsigned num_tokens = tgsi_num_tokens(tokens);
839 size_t tokens_size = num_tokens*sizeof(struct tgsi_token);
840 unsigned hash_key = cso_construct_key((void*)tokens, tokens_size);
841 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
842 hash_key,
843 CSO_FRAGMENT_SHADER,
844 (void*)tokens);
845 void *handle = NULL;
846
847 if (cso_hash_iter_is_null(iter)) {
848 struct cso_fragment_shader *cso = MALLOC(sizeof(struct cso_fragment_shader) + tokens_size);
849 struct tgsi_token *cso_tokens = (struct tgsi_token *)((char *)cso + sizeof(*cso));
850
851 if (!cso)
852 return PIPE_ERROR_OUT_OF_MEMORY;
853
854 memcpy(cso_tokens, tokens, tokens_size);
855 cso->state.tokens = cso_tokens;
856 cso->data = ctx->pipe->create_fs_state(ctx->pipe, &cso->state);
857 cso->delete_state = (cso_state_callback)ctx->pipe->delete_fs_state;
858 cso->context = ctx->pipe;
859
860 iter = cso_insert_state(ctx->cache, hash_key, CSO_FRAGMENT_SHADER, cso);
861 if (cso_hash_iter_is_null(iter)) {
862 FREE(cso);
863 return PIPE_ERROR_OUT_OF_MEMORY;
864 }
865
866 handle = cso->data;
867 }
868 else {
869 handle = ((struct cso_fragment_shader *)cso_hash_iter_data(iter))->data;
870 }
871
872 return cso_set_fragment_shader_handle( ctx, handle );
873 }
874 #endif
875
876 void cso_save_fragment_shader(struct cso_context *ctx)
877 {
878 assert(!ctx->fragment_shader_saved);
879 ctx->fragment_shader_saved = ctx->fragment_shader;
880 }
881
882 void cso_restore_fragment_shader(struct cso_context *ctx)
883 {
884 if (ctx->fragment_shader_saved != ctx->fragment_shader) {
885 ctx->pipe->bind_fs_state(ctx->pipe, ctx->fragment_shader_saved);
886 ctx->fragment_shader = ctx->fragment_shader_saved;
887 }
888 ctx->fragment_shader_saved = NULL;
889 }
890
891
892 enum pipe_error cso_set_vertex_shader_handle(struct cso_context *ctx,
893 void *handle )
894 {
895 if (ctx->vertex_shader != handle) {
896 ctx->vertex_shader = handle;
897 ctx->pipe->bind_vs_state(ctx->pipe, handle);
898 }
899 return PIPE_OK;
900 }
901
902 void cso_delete_vertex_shader(struct cso_context *ctx, void *handle )
903 {
904 if (handle == ctx->vertex_shader) {
905 /* unbind before deleting */
906 ctx->pipe->bind_vs_state(ctx->pipe, NULL);
907 ctx->vertex_shader = NULL;
908 }
909 ctx->pipe->delete_vs_state(ctx->pipe, handle);
910 }
911
912
913 /* Not really working:
914 */
915 #if 0
916 enum pipe_error cso_set_vertex_shader(struct cso_context *ctx,
917 const struct pipe_shader_state *templ)
918 {
919 unsigned hash_key = cso_construct_key((void*)templ,
920 sizeof(struct pipe_shader_state));
921 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
922 hash_key, CSO_VERTEX_SHADER,
923 (void*)templ);
924 void *handle = NULL;
925
926 if (cso_hash_iter_is_null(iter)) {
927 struct cso_vertex_shader *cso = MALLOC(sizeof(struct cso_vertex_shader));
928
929 if (!cso)
930 return PIPE_ERROR_OUT_OF_MEMORY;
931
932 memcpy(cso->state, templ, sizeof(*templ));
933 cso->data = ctx->pipe->create_vs_state(ctx->pipe, &cso->state);
934 cso->delete_state = (cso_state_callback)ctx->pipe->delete_vs_state;
935 cso->context = ctx->pipe;
936
937 iter = cso_insert_state(ctx->cache, hash_key, CSO_VERTEX_SHADER, cso);
938 if (cso_hash_iter_is_null(iter)) {
939 FREE(cso);
940 return PIPE_ERROR_OUT_OF_MEMORY;
941 }
942
943 handle = cso->data;
944 }
945 else {
946 handle = ((struct cso_vertex_shader *)cso_hash_iter_data(iter))->data;
947 }
948
949 return cso_set_vertex_shader_handle( ctx, handle );
950 }
951 #endif
952
953
954
955 void cso_save_vertex_shader(struct cso_context *ctx)
956 {
957 assert(!ctx->vertex_shader_saved);
958 ctx->vertex_shader_saved = ctx->vertex_shader;
959 }
960
961 void cso_restore_vertex_shader(struct cso_context *ctx)
962 {
963 if (ctx->vertex_shader_saved != ctx->vertex_shader) {
964 ctx->pipe->bind_vs_state(ctx->pipe, ctx->vertex_shader_saved);
965 ctx->vertex_shader = ctx->vertex_shader_saved;
966 }
967 ctx->vertex_shader_saved = NULL;
968 }
969
970
971 /**
972 * Copy framebuffer state from src to dst with refcounting of surfaces.
973 */
974 static void
975 copy_framebuffer_state(struct pipe_framebuffer_state *dst,
976 const struct pipe_framebuffer_state *src)
977 {
978 uint i;
979
980 dst->width = src->width;
981 dst->height = src->height;
982 dst->nr_cbufs = src->nr_cbufs;
983 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
984 pipe_surface_reference(&dst->cbufs[i], src->cbufs[i]);
985 }
986 pipe_surface_reference(&dst->zsbuf, src->zsbuf);
987 }
988
989
990 static void
991 free_framebuffer_state(struct pipe_framebuffer_state *fb)
992 {
993 uint i;
994
995 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
996 pipe_surface_reference(&fb->cbufs[i], NULL);
997 }
998 pipe_surface_reference(&fb->zsbuf, NULL);
999 }
1000
1001
1002 enum pipe_error cso_set_framebuffer(struct cso_context *ctx,
1003 const struct pipe_framebuffer_state *fb)
1004 {
1005 if (memcmp(&ctx->fb, fb, sizeof(*fb)) != 0) {
1006 copy_framebuffer_state(&ctx->fb, fb);
1007 ctx->pipe->set_framebuffer_state(ctx->pipe, fb);
1008 }
1009 return PIPE_OK;
1010 }
1011
1012 void cso_save_framebuffer(struct cso_context *ctx)
1013 {
1014 copy_framebuffer_state(&ctx->fb_saved, &ctx->fb);
1015 }
1016
1017 void cso_restore_framebuffer(struct cso_context *ctx)
1018 {
1019 if (memcmp(&ctx->fb, &ctx->fb_saved, sizeof(ctx->fb))) {
1020 copy_framebuffer_state(&ctx->fb, &ctx->fb_saved);
1021 ctx->pipe->set_framebuffer_state(ctx->pipe, &ctx->fb);
1022 free_framebuffer_state(&ctx->fb_saved);
1023 }
1024 }
1025
1026
1027 enum pipe_error cso_set_viewport(struct cso_context *ctx,
1028 const struct pipe_viewport_state *vp)
1029 {
1030 if (memcmp(&ctx->vp, vp, sizeof(*vp))) {
1031 ctx->vp = *vp;
1032 ctx->pipe->set_viewport_state(ctx->pipe, vp);
1033 }
1034 return PIPE_OK;
1035 }
1036
1037 void cso_save_viewport(struct cso_context *ctx)
1038 {
1039 ctx->vp_saved = ctx->vp;
1040 }
1041
1042
1043 void cso_restore_viewport(struct cso_context *ctx)
1044 {
1045 if (memcmp(&ctx->vp, &ctx->vp_saved, sizeof(ctx->vp))) {
1046 ctx->vp = ctx->vp_saved;
1047 ctx->pipe->set_viewport_state(ctx->pipe, &ctx->vp);
1048 }
1049 }
1050
1051
1052
1053
1054 enum pipe_error cso_set_blend_color(struct cso_context *ctx,
1055 const struct pipe_blend_color *bc)
1056 {
1057 if (memcmp(&ctx->blend_color, bc, sizeof(ctx->blend_color))) {
1058 ctx->blend_color = *bc;
1059 ctx->pipe->set_blend_color(ctx->pipe, bc);
1060 }
1061 return PIPE_OK;
1062 }
1063
1064 enum pipe_error cso_set_geometry_shader_handle(struct cso_context *ctx,
1065 void *handle)
1066 {
1067 if (ctx->geometry_shader != handle) {
1068 ctx->geometry_shader = handle;
1069 ctx->pipe->bind_gs_state(ctx->pipe, handle);
1070 }
1071 return PIPE_OK;
1072 }
1073
1074 void cso_delete_geometry_shader(struct cso_context *ctx, void *handle)
1075 {
1076 if (handle == ctx->geometry_shader) {
1077 /* unbind before deleting */
1078 ctx->pipe->bind_gs_state(ctx->pipe, NULL);
1079 ctx->geometry_shader = NULL;
1080 }
1081 ctx->pipe->delete_gs_state(ctx->pipe, handle);
1082 }
1083
1084 void cso_save_geometry_shader(struct cso_context *ctx)
1085 {
1086 assert(!ctx->geometry_shader_saved);
1087 ctx->geometry_shader_saved = ctx->geometry_shader;
1088 }
1089
1090 void cso_restore_geometry_shader(struct cso_context *ctx)
1091 {
1092 if (ctx->geometry_shader_saved != ctx->geometry_shader) {
1093 ctx->pipe->bind_gs_state(ctx->pipe, ctx->geometry_shader_saved);
1094 ctx->geometry_shader = ctx->geometry_shader_saved;
1095 }
1096 ctx->geometry_shader_saved = NULL;
1097 }