cso_context.c revision ed187d39a6e0fd921b2a45a143d88ac4b66eee91
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 "pipe/p_util.h" 40#include "pipe/p_inlines.h" 41#include "tgsi/util/tgsi_parse.h" 42 43#include "cso_cache/cso_context.h" 44#include "cso_cache/cso_cache.h" 45#include "cso_cache/cso_hash.h" 46 47struct 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 } hw; 55 56 void *samplers[PIPE_MAX_SAMPLERS]; 57 unsigned nr_samplers; 58 59 void *samplers_saved[PIPE_MAX_SAMPLERS]; 60 unsigned nr_samplers_saved; 61 62 struct pipe_texture *textures[PIPE_MAX_SAMPLERS]; 63 uint nr_textures; 64 65 struct pipe_texture *textures_saved[PIPE_MAX_SAMPLERS]; 66 uint nr_textures_saved; 67 68 /** Current and saved state. 69 * The saved state is used as a 1-deep stack. 70 */ 71 void *blend, *blend_saved; 72 void *depth_stencil, *depth_stencil_saved; 73 void *rasterizer, *rasterizer_saved; 74 void *fragment_shader, *fragment_shader_saved; 75 void *vertex_shader, *vertex_shader_saved; 76 77 struct pipe_framebuffer_state fb, fb_saved; 78 struct pipe_viewport_state vp, vp_saved; 79 struct pipe_blend_color blend_color; 80}; 81 82 83struct cso_context *cso_create_context( struct pipe_context *pipe ) 84{ 85 struct cso_context *ctx = CALLOC_STRUCT(cso_context); 86 if (ctx == NULL) 87 goto out; 88 89 ctx->cache = cso_cache_create(); 90 if (ctx->cache == NULL) 91 goto out; 92 93 ctx->pipe = pipe; 94 95 /* Enable for testing: */ 96 if (0) cso_set_maximum_cache_size( ctx->cache, 4 ); 97 98 return ctx; 99 100out: 101 cso_destroy_context( ctx ); 102 return NULL; 103} 104 105static void cso_release_all( struct cso_context *ctx ) 106{ 107 if (ctx->pipe) { 108 ctx->pipe->bind_blend_state( ctx->pipe, NULL ); 109 ctx->pipe->bind_rasterizer_state( ctx->pipe, NULL ); 110 ctx->pipe->bind_sampler_states( ctx->pipe, 0, NULL ); 111 ctx->pipe->bind_depth_stencil_alpha_state( ctx->pipe, NULL ); 112 ctx->pipe->bind_fs_state( ctx->pipe, NULL ); 113 ctx->pipe->bind_vs_state( ctx->pipe, NULL ); 114 } 115 116 if (ctx->cache) { 117 cso_cache_delete( ctx->cache ); 118 ctx->cache = NULL; 119 } 120} 121 122 123void cso_destroy_context( struct cso_context *ctx ) 124{ 125 if (ctx) 126 cso_release_all( ctx ); 127 128 FREE( ctx ); 129} 130 131 132/* Those function will either find the state of the given template 133 * in the cache or they will create a new state from the given 134 * template, insert it in the cache and return it. 135 */ 136 137/* 138 * If the driver returns 0 from the create method then they will assign 139 * the data member of the cso to be the template itself. 140 */ 141 142enum pipe_error cso_set_blend(struct cso_context *ctx, 143 const struct pipe_blend_state *templ) 144{ 145 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_blend_state)); 146 struct cso_hash_iter iter = cso_find_state_template(ctx->cache, 147 hash_key, CSO_BLEND, 148 (void*)templ); 149 void *handle; 150 151 if (cso_hash_iter_is_null(iter)) { 152 struct cso_blend *cso = MALLOC(sizeof(struct cso_blend)); 153 if (!cso) 154 return PIPE_ERROR_OUT_OF_MEMORY; 155 156 cso->state = *templ; 157 cso->data = ctx->pipe->create_blend_state(ctx->pipe, &cso->state); 158 cso->delete_state = (cso_state_callback)ctx->pipe->delete_blend_state; 159 cso->context = ctx->pipe; 160 161 iter = cso_insert_state(ctx->cache, hash_key, CSO_BLEND, cso); 162 if (cso_hash_iter_is_null(iter)) { 163 FREE(cso); 164 return PIPE_ERROR_OUT_OF_MEMORY; 165 } 166 167 handle = cso->data; 168 } 169 else { 170 handle = ((struct cso_blend *)cso_hash_iter_data(iter))->data; 171 } 172 173 if (ctx->blend != handle) { 174 ctx->blend = handle; 175 ctx->pipe->bind_blend_state(ctx->pipe, handle); 176 } 177 return PIPE_OK; 178} 179 180void cso_save_blend(struct cso_context *ctx) 181{ 182 assert(!ctx->blend_saved); 183 ctx->blend_saved = ctx->blend; 184} 185 186void cso_restore_blend(struct cso_context *ctx) 187{ 188 if (ctx->blend != ctx->blend_saved) { 189 ctx->blend = ctx->blend_saved; 190 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend_saved); 191 } 192 ctx->blend_saved = NULL; 193} 194 195 196 197enum pipe_error cso_single_sampler(struct cso_context *ctx, 198 unsigned idx, 199 const struct pipe_sampler_state *templ) 200{ 201 void *handle = NULL; 202 203 if (templ != NULL) { 204 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_sampler_state)); 205 struct cso_hash_iter iter = cso_find_state_template(ctx->cache, 206 hash_key, CSO_SAMPLER, 207 (void*)templ); 208 209 if (cso_hash_iter_is_null(iter)) { 210 struct cso_sampler *cso = MALLOC(sizeof(struct cso_sampler)); 211 if (!cso) 212 return PIPE_ERROR_OUT_OF_MEMORY; 213 214 cso->state = *templ; 215 cso->data = ctx->pipe->create_sampler_state(ctx->pipe, &cso->state); 216 cso->delete_state = (cso_state_callback)ctx->pipe->delete_sampler_state; 217 cso->context = ctx->pipe; 218 219 iter = cso_insert_state(ctx->cache, hash_key, CSO_SAMPLER, cso); 220 if (cso_hash_iter_is_null(iter)) { 221 FREE(cso); 222 return PIPE_ERROR_OUT_OF_MEMORY; 223 } 224 225 handle = cso->data; 226 } 227 else { 228 handle = ((struct cso_sampler *)cso_hash_iter_data(iter))->data; 229 } 230 } 231 232 ctx->samplers[idx] = handle; 233 return PIPE_OK; 234} 235 236void cso_single_sampler_done( struct cso_context *ctx ) 237{ 238 unsigned i; 239 240 /* find highest non-null sampler */ 241 for (i = PIPE_MAX_SAMPLERS; i > 0; i--) { 242 if (ctx->samplers[i - 1] != NULL) 243 break; 244 } 245 246 ctx->nr_samplers = i; 247 248 if (ctx->hw.nr_samplers != ctx->nr_samplers || 249 memcmp(ctx->hw.samplers, 250 ctx->samplers, 251 ctx->nr_samplers * sizeof(void *)) != 0) 252 { 253 memcpy(ctx->hw.samplers, ctx->samplers, ctx->nr_samplers * sizeof(void *)); 254 ctx->hw.nr_samplers = ctx->nr_samplers; 255 256 ctx->pipe->bind_sampler_states(ctx->pipe, ctx->nr_samplers, ctx->samplers); 257 } 258} 259 260/* 261 * If the function encouters any errors it will return the 262 * last one. Done to always try to set as many samplers 263 * as possible. 264 */ 265enum pipe_error cso_set_samplers( struct cso_context *ctx, 266 unsigned nr, 267 const struct pipe_sampler_state **templates ) 268{ 269 unsigned i; 270 enum pipe_error temp, error = PIPE_OK; 271 272 /* TODO: fastpath 273 */ 274 275 for (i = 0; i < nr; i++) { 276 temp = cso_single_sampler( ctx, i, templates[i] ); 277 if (temp != PIPE_OK) 278 error = temp; 279 } 280 281 for ( ; i < ctx->nr_samplers; i++) { 282 temp = cso_single_sampler( ctx, i, NULL ); 283 if (temp != PIPE_OK) 284 error = temp; 285 } 286 287 cso_single_sampler_done( ctx ); 288 289 return error; 290} 291 292void cso_save_samplers(struct cso_context *ctx) 293{ 294 ctx->nr_samplers_saved = ctx->nr_samplers; 295 memcpy(ctx->samplers_saved, ctx->samplers, sizeof(ctx->samplers)); 296} 297 298void cso_restore_samplers(struct cso_context *ctx) 299{ 300 ctx->nr_samplers = ctx->nr_samplers_saved; 301 memcpy(ctx->samplers, ctx->samplers_saved, sizeof(ctx->samplers)); 302 cso_single_sampler_done( ctx ); 303} 304 305 306enum pipe_error cso_set_sampler_textures( struct cso_context *ctx, 307 uint count, 308 struct pipe_texture **textures ) 309{ 310 uint i; 311 312 ctx->nr_textures = count; 313 314 for (i = 0; i < count; i++) 315 pipe_texture_reference(&ctx->textures[i], textures[i]); 316 for ( ; i < PIPE_MAX_SAMPLERS; i++) 317 pipe_texture_reference(&ctx->textures[i], NULL); 318 319 ctx->pipe->set_sampler_textures(ctx->pipe, count, textures); 320 321 return PIPE_OK; 322} 323 324void cso_save_sampler_textures( struct cso_context *ctx ) 325{ 326 uint i; 327 328 ctx->nr_textures_saved = ctx->nr_textures; 329 for (i = 0; i < ctx->nr_textures; i++) { 330 assert(!ctx->textures_saved[i]); 331 pipe_texture_reference(&ctx->textures_saved[i], ctx->textures[i]); 332 } 333} 334 335void cso_restore_sampler_textures( struct cso_context *ctx ) 336{ 337 uint i; 338 339 ctx->nr_textures = ctx->nr_textures_saved; 340 341 for (i = 0; i < ctx->nr_textures; i++) { 342 pipe_texture_reference(&ctx->textures[i], NULL); 343 ctx->textures[i] = ctx->textures_saved[i]; 344 ctx->textures_saved[i] = NULL; 345 } 346 for ( ; i < PIPE_MAX_SAMPLERS; i++) 347 pipe_texture_reference(&ctx->textures[i], NULL); 348 349 ctx->pipe->set_sampler_textures(ctx->pipe, ctx->nr_textures, ctx->textures); 350 351 ctx->nr_textures_saved = 0; 352} 353 354 355 356enum pipe_error cso_set_depth_stencil_alpha(struct cso_context *ctx, 357 const struct pipe_depth_stencil_alpha_state *templ) 358{ 359 unsigned hash_key = cso_construct_key((void*)templ, 360 sizeof(struct pipe_depth_stencil_alpha_state)); 361 struct cso_hash_iter iter = cso_find_state_template(ctx->cache, 362 hash_key, 363 CSO_DEPTH_STENCIL_ALPHA, 364 (void*)templ); 365 void *handle; 366 367 if (cso_hash_iter_is_null(iter)) { 368 struct cso_depth_stencil_alpha *cso = MALLOC(sizeof(struct cso_depth_stencil_alpha)); 369 if (!cso) 370 return PIPE_ERROR_OUT_OF_MEMORY; 371 372 cso->state = *templ; 373 cso->data = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &cso->state); 374 cso->delete_state = (cso_state_callback)ctx->pipe->delete_depth_stencil_alpha_state; 375 cso->context = ctx->pipe; 376 377 iter = cso_insert_state(ctx->cache, hash_key, CSO_DEPTH_STENCIL_ALPHA, cso); 378 if (cso_hash_iter_is_null(iter)) { 379 FREE(cso); 380 return PIPE_ERROR_OUT_OF_MEMORY; 381 } 382 383 handle = cso->data; 384 } 385 else { 386 handle = ((struct cso_depth_stencil_alpha *)cso_hash_iter_data(iter))->data; 387 } 388 389 if (ctx->depth_stencil != handle) { 390 ctx->depth_stencil = handle; 391 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, handle); 392 } 393 return PIPE_OK; 394} 395 396void cso_save_depth_stencil_alpha(struct cso_context *ctx) 397{ 398 assert(!ctx->depth_stencil_saved); 399 ctx->depth_stencil_saved = ctx->depth_stencil; 400} 401 402void cso_restore_depth_stencil_alpha(struct cso_context *ctx) 403{ 404 if (ctx->depth_stencil != ctx->depth_stencil_saved) { 405 ctx->depth_stencil = ctx->depth_stencil_saved; 406 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, ctx->depth_stencil_saved); 407 } 408 ctx->depth_stencil_saved = NULL; 409} 410 411 412 413enum pipe_error cso_set_rasterizer(struct cso_context *ctx, 414 const struct pipe_rasterizer_state *templ) 415{ 416 unsigned hash_key = cso_construct_key((void*)templ, 417 sizeof(struct pipe_rasterizer_state)); 418 struct cso_hash_iter iter = cso_find_state_template(ctx->cache, 419 hash_key, CSO_RASTERIZER, 420 (void*)templ); 421 void *handle = NULL; 422 423 if (cso_hash_iter_is_null(iter)) { 424 struct cso_rasterizer *cso = MALLOC(sizeof(struct cso_rasterizer)); 425 if (!cso) 426 return PIPE_ERROR_OUT_OF_MEMORY; 427 428 cso->state = *templ; 429 cso->data = ctx->pipe->create_rasterizer_state(ctx->pipe, &cso->state); 430 cso->delete_state = (cso_state_callback)ctx->pipe->delete_rasterizer_state; 431 cso->context = ctx->pipe; 432 433 iter = cso_insert_state(ctx->cache, hash_key, CSO_RASTERIZER, cso); 434 if (cso_hash_iter_is_null(iter)) { 435 FREE(cso); 436 return PIPE_ERROR_OUT_OF_MEMORY; 437 } 438 439 handle = cso->data; 440 } 441 else { 442 handle = ((struct cso_rasterizer *)cso_hash_iter_data(iter))->data; 443 } 444 445 if (ctx->rasterizer != handle) { 446 ctx->rasterizer = handle; 447 ctx->pipe->bind_rasterizer_state(ctx->pipe, handle); 448 } 449 return PIPE_OK; 450} 451 452void cso_save_rasterizer(struct cso_context *ctx) 453{ 454 assert(!ctx->rasterizer_saved); 455 ctx->rasterizer_saved = ctx->rasterizer; 456} 457 458void cso_restore_rasterizer(struct cso_context *ctx) 459{ 460 if (ctx->rasterizer != ctx->rasterizer_saved) { 461 ctx->rasterizer = ctx->rasterizer_saved; 462 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rasterizer_saved); 463 } 464 ctx->rasterizer_saved = NULL; 465} 466 467enum pipe_error cso_set_fragment_shader_handle(struct cso_context *ctx, 468 void *handle ) 469{ 470 if (ctx->fragment_shader != handle) { 471 ctx->fragment_shader = handle; 472 ctx->pipe->bind_fs_state(ctx->pipe, handle); 473 } 474 return PIPE_OK; 475} 476 477 478/* Not really working: 479 */ 480#if 0 481enum pipe_error cso_set_fragment_shader(struct cso_context *ctx, 482 const struct pipe_shader_state *templ) 483{ 484 const struct tgsi_token *tokens = templ->tokens; 485 unsigned num_tokens = tgsi_num_tokens(tokens); 486 size_t tokens_size = num_tokens*sizeof(struct tgsi_token); 487 unsigned hash_key = cso_construct_key((void*)tokens, tokens_size); 488 struct cso_hash_iter iter = cso_find_state_template(ctx->cache, 489 hash_key, 490 CSO_FRAGMENT_SHADER, 491 (void*)tokens); 492 void *handle = NULL; 493 494 if (cso_hash_iter_is_null(iter)) { 495 struct cso_fragment_shader *cso = MALLOC(sizeof(struct cso_fragment_shader) + tokens_size); 496 struct tgsi_token *cso_tokens = (struct tgsi_token *)((char *)cso + sizeof(*cso)); 497 498 if (!cso) 499 return PIPE_ERROR_OUT_OF_MEMORY; 500 501 memcpy(cso_tokens, tokens, tokens_size); 502 cso->state.tokens = cso_tokens; 503 cso->data = ctx->pipe->create_fs_state(ctx->pipe, &cso->state); 504 cso->delete_state = (cso_state_callback)ctx->pipe->delete_fs_state; 505 cso->context = ctx->pipe; 506 507 iter = cso_insert_state(ctx->cache, hash_key, CSO_FRAGMENT_SHADER, cso); 508 if (cso_hash_iter_is_null(iter)) { 509 FREE(cso); 510 return PIPE_ERROR_OUT_OF_MEMORY; 511 } 512 513 handle = cso->data; 514 } 515 else { 516 handle = ((struct cso_fragment_shader *)cso_hash_iter_data(iter))->data; 517 } 518 519 return cso_set_fragment_shader_handle( ctx, handle ); 520} 521#endif 522 523void cso_save_fragment_shader(struct cso_context *ctx) 524{ 525 assert(!ctx->fragment_shader_saved); 526 ctx->fragment_shader_saved = ctx->fragment_shader; 527} 528 529void cso_restore_fragment_shader(struct cso_context *ctx) 530{ 531 assert(ctx->fragment_shader_saved); 532 if (ctx->fragment_shader_saved != ctx->fragment_shader) { 533 ctx->pipe->bind_fs_state(ctx->pipe, ctx->fragment_shader_saved); 534 ctx->fragment_shader = ctx->fragment_shader_saved; 535 } 536 ctx->fragment_shader_saved = NULL; 537} 538 539 540enum pipe_error cso_set_vertex_shader_handle(struct cso_context *ctx, 541 void *handle ) 542{ 543 if (ctx->vertex_shader != handle) { 544 ctx->vertex_shader = handle; 545 ctx->pipe->bind_vs_state(ctx->pipe, handle); 546 } 547 return PIPE_OK; 548} 549 550 551/* Not really working: 552 */ 553#if 0 554enum pipe_error cso_set_vertex_shader(struct cso_context *ctx, 555 const struct pipe_shader_state *templ) 556{ 557 unsigned hash_key = cso_construct_key((void*)templ, 558 sizeof(struct pipe_shader_state)); 559 struct cso_hash_iter iter = cso_find_state_template(ctx->cache, 560 hash_key, CSO_VERTEX_SHADER, 561 (void*)templ); 562 void *handle = NULL; 563 564 if (cso_hash_iter_is_null(iter)) { 565 struct cso_vertex_shader *cso = MALLOC(sizeof(struct cso_vertex_shader)); 566 567 if (!cso) 568 return PIPE_ERROR_OUT_OF_MEMORY; 569 570 cso->state = *templ; 571 cso->data = ctx->pipe->create_vs_state(ctx->pipe, &cso->state); 572 cso->delete_state = (cso_state_callback)ctx->pipe->delete_vs_state; 573 cso->context = ctx->pipe; 574 575 iter = cso_insert_state(ctx->cache, hash_key, CSO_VERTEX_SHADER, cso); 576 if (cso_hash_iter_is_null(iter)) { 577 FREE(cso); 578 return PIPE_ERROR_OUT_OF_MEMORY; 579 } 580 581 handle = cso->data; 582 } 583 else { 584 handle = ((struct cso_vertex_shader *)cso_hash_iter_data(iter))->data; 585 } 586 587 return cso_set_vertex_shader_handle( ctx, handle ); 588} 589#endif 590 591 592 593void cso_save_vertex_shader(struct cso_context *ctx) 594{ 595 assert(!ctx->vertex_shader_saved); 596 ctx->vertex_shader_saved = ctx->vertex_shader; 597} 598 599void cso_restore_vertex_shader(struct cso_context *ctx) 600{ 601 assert(ctx->vertex_shader_saved); 602 if (ctx->vertex_shader_saved != ctx->vertex_shader) { 603 ctx->pipe->bind_fs_state(ctx->pipe, ctx->vertex_shader_saved); 604 ctx->vertex_shader = ctx->vertex_shader_saved; 605 } 606 ctx->vertex_shader_saved = NULL; 607} 608 609 610 611enum pipe_error cso_set_framebuffer(struct cso_context *ctx, 612 const struct pipe_framebuffer_state *fb) 613{ 614 /* XXX this memcmp() fails to detect buffer size changes */ 615 if (1/*memcmp(&ctx->fb, fb, sizeof(*fb))*/) { 616 ctx->fb = *fb; 617 ctx->pipe->set_framebuffer_state(ctx->pipe, fb); 618 } 619 return PIPE_OK; 620} 621 622void cso_save_framebuffer(struct cso_context *ctx) 623{ 624 ctx->fb_saved = ctx->fb; 625} 626 627void cso_restore_framebuffer(struct cso_context *ctx) 628{ 629 if (memcmp(&ctx->fb, &ctx->fb_saved, sizeof(ctx->fb))) { 630 ctx->fb = ctx->fb_saved; 631 ctx->pipe->set_framebuffer_state(ctx->pipe, &ctx->fb); 632 } 633} 634 635 636enum pipe_error cso_set_viewport(struct cso_context *ctx, 637 const struct pipe_viewport_state *vp) 638{ 639 if (memcmp(&ctx->vp, vp, sizeof(*vp))) { 640 ctx->vp = *vp; 641 ctx->pipe->set_viewport_state(ctx->pipe, vp); 642 } 643 return PIPE_OK; 644} 645 646void cso_save_viewport(struct cso_context *ctx) 647{ 648 ctx->vp_saved = ctx->vp; 649} 650 651 652void cso_restore_viewport(struct cso_context *ctx) 653{ 654 if (memcmp(&ctx->vp, &ctx->vp_saved, sizeof(ctx->vp))) { 655 ctx->vp = ctx->vp_saved; 656 ctx->pipe->set_viewport_state(ctx->pipe, &ctx->vp); 657 } 658} 659 660 661 662 663enum pipe_error cso_set_blend_color(struct cso_context *ctx, 664 const struct pipe_blend_color *bc) 665{ 666 if (memcmp(&ctx->blend_color, bc, sizeof(ctx->blend_color))) { 667 ctx->blend_color = *bc; 668 ctx->pipe->set_blend_color(ctx->pipe, bc); 669 } 670 return PIPE_OK; 671} 672