cso_context.c 52.1 KB
Newer Older
Keith Whitwell's avatar
Keith Whitwell committed
1 2
/**************************************************************************
 *
Jose Fonseca's avatar
Jose Fonseca committed
3
 * Copyright 2007 VMware, Inc.
Keith Whitwell's avatar
Keith Whitwell committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
Jose Fonseca's avatar
Jose Fonseca committed
21
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
Keith Whitwell's avatar
Keith Whitwell committed
22 23 24 25 26 27
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

José Fonseca's avatar
José Fonseca committed
28 29
 /**
  * @file
30
  *
José Fonseca's avatar
José Fonseca committed
31
  * Wrap the cso cache & hash mechanisms in a simplified
Keith Whitwell's avatar
Keith Whitwell committed
32 33
  * pipe-driver-specific interface.
  *
Jose Fonseca's avatar
Jose Fonseca committed
34 35
  * @author Zack Rusin <zackr@vmware.com>
  * @author Keith Whitwell <keithw@vmware.com>
Keith Whitwell's avatar
Keith Whitwell committed
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"
Keith Whitwell's avatar
Keith Whitwell committed
46 47 48 49

#include "cso_cache/cso_context.h"
#include "cso_cache/cso_cache.h"
#include "cso_cache/cso_hash.h"
50
#include "cso_context.h"
Keith Whitwell's avatar
Keith Whitwell committed
51 52


53
/**
Brian Paul's avatar
Brian Paul committed
54
 * Per-shader sampler information.
55 56 57
 */
struct sampler_info
{
58
   struct cso_sampler *cso_samplers[PIPE_MAX_SAMPLERS];
59 60
   void *samplers[PIPE_MAX_SAMPLERS];
};
61

62

63

64 65 66
struct cso_context {
   struct pipe_context *pipe;
   struct cso_cache *cache;
67

68
   struct u_vbuf *vbuf;
69 70
   struct u_vbuf *vbuf_current;
   bool always_use_vbuf;
71

72
   boolean has_geometry_shader;
73
   boolean has_tessellation;
74
   boolean has_compute_shader;
75
   boolean has_streamout;
76

77 78
   unsigned saved_state;  /**< bitmask of CSO_BIT_x flags */

79 80 81 82 83 84
   struct pipe_sampler_view *fragment_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
   unsigned nr_fragment_views;

   struct pipe_sampler_view *fragment_views_saved[PIPE_MAX_SHADER_SAMPLER_VIEWS];
   unsigned nr_fragment_views_saved;

85
   struct sampler_info fragment_samplers_saved;
86
   struct sampler_info samplers[PIPE_SHADER_TYPES];
87

88 89 90 91 92
   /* Temporary number until cso_single_sampler_done is called.
    * It tracks the highest sampler seen in cso_single_sampler.
    */
   int max_sampler_seen;

93 94
   struct pipe_vertex_buffer vertex_buffer0_current;
   struct pipe_vertex_buffer vertex_buffer0_saved;
95

96 97 98
   struct pipe_constant_buffer aux_constbuf_current[PIPE_SHADER_TYPES];
   struct pipe_constant_buffer aux_constbuf_saved[PIPE_SHADER_TYPES];

99 100 101
   struct pipe_image_view fragment_image0_current;
   struct pipe_image_view fragment_image0_saved;

102 103 104 105 106 107
   unsigned nr_so_targets;
   struct pipe_stream_output_target *so_targets[PIPE_MAX_SO_BUFFERS];

   unsigned nr_so_targets_saved;
   struct pipe_stream_output_target *so_targets_saved[PIPE_MAX_SO_BUFFERS];

108 109 110 111 112 113
   /** Current and saved state.
    * The saved state is used as a 1-deep stack.
    */
   void *blend, *blend_saved;
   void *depth_stencil, *depth_stencil_saved;
   void *rasterizer, *rasterizer_saved;
114 115 116
   void *fragment_shader, *fragment_shader_saved;
   void *vertex_shader, *vertex_shader_saved;
   void *geometry_shader, *geometry_shader_saved;
117 118
   void *tessctrl_shader, *tessctrl_shader_saved;
   void *tesseval_shader, *tesseval_shader_saved;
119
   void *compute_shader;
120
   void *velements, *velements_saved;
121 122
   struct pipe_query *render_condition, *render_condition_saved;
   uint render_condition_mode, render_condition_mode_saved;
123
   boolean render_condition_cond, render_condition_cond_saved;
124 125 126 127

   struct pipe_framebuffer_state fb, fb_saved;
   struct pipe_viewport_state vp, vp_saved;
   struct pipe_blend_color blend_color;
128
   unsigned sample_mask, sample_mask_saved;
129
   unsigned min_samples, min_samples_saved;
130
   struct pipe_stencil_ref stencil_ref, stencil_ref_saved;
Keith Whitwell's avatar
Keith Whitwell committed
131 132
};

Marek Olšák's avatar
Marek Olšák committed
133 134 135 136
struct pipe_context *cso_get_pipe_context(struct cso_context *cso)
{
   return cso->pipe;
}
Keith Whitwell's avatar
Keith Whitwell committed
137

138 139 140 141
static boolean delete_blend_state(struct cso_context *ctx, void *state)
{
   struct cso_blend *cso = (struct cso_blend *)state;

142
   if (ctx->blend == cso->data)
143 144 145 146 147 148 149 150 151 152
      return FALSE;

   if (cso->delete_state)
      cso->delete_state(cso->context, cso->data);
   FREE(state);
   return TRUE;
}

static boolean delete_depth_stencil_state(struct cso_context *ctx, void *state)
{
153 154
   struct cso_depth_stencil_alpha *cso =
      (struct cso_depth_stencil_alpha *)state;
155 156 157 158 159 160 161 162 163 164 165

   if (ctx->depth_stencil == cso->data)
      return FALSE;

   if (cso->delete_state)
      cso->delete_state(cso->context, cso->data);
   FREE(state);

   return TRUE;
}

166
static boolean delete_sampler_state(UNUSED struct cso_context *ctx, void *state)
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
   struct cso_sampler *cso = (struct cso_sampler *)state;
   if (cso->delete_state)
      cso->delete_state(cso->context, cso->data);
   FREE(state);
   return TRUE;
}

static boolean delete_rasterizer_state(struct cso_context *ctx, void *state)
{
   struct cso_rasterizer *cso = (struct cso_rasterizer *)state;

   if (ctx->rasterizer == cso->data)
      return FALSE;
   if (cso->delete_state)
      cso->delete_state(cso->context, cso->data);
   FREE(state);
   return TRUE;
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200
static boolean delete_vertex_elements(struct cso_context *ctx,
                                      void *state)
{
   struct cso_velements *cso = (struct cso_velements *)state;

   if (ctx->velements == cso->data)
      return FALSE;

   if (cso->delete_state)
      cso->delete_state(cso->context, cso->data);
   FREE(state);
   return TRUE;
}

201

202
static inline boolean delete_cso(struct cso_context *ctx,
203 204 205 206 207 208 209 210 211 212 213
                                 void *state, enum cso_cache_type type)
{
   switch (type) {
   case CSO_BLEND:
      return delete_blend_state(ctx, state);
   case CSO_SAMPLER:
      return delete_sampler_state(ctx, state);
   case CSO_DEPTH_STENCIL_ALPHA:
      return delete_depth_stencil_state(ctx, state);
   case CSO_RASTERIZER:
      return delete_rasterizer_state(ctx, state);
214 215
   case CSO_VELEMENTS:
      return delete_vertex_elements(ctx, state);
216 217 218 219 220 221 222
   default:
      assert(0);
      FREE(state);
   }
   return FALSE;
}

223
static inline void
224 225
sanitize_hash(struct cso_hash *hash, enum cso_cache_type type,
              int max_size, void *user_data)
226 227 228 229 230 231 232
{
   struct cso_context *ctx = (struct cso_context *)user_data;
   /* if we're approach the maximum size, remove fourth of the entries
    * otherwise every subsequent call will go through the same */
   int hash_size = cso_hash_size(hash);
   int max_entries = (max_size > hash_size) ? max_size : hash_size;
   int to_remove =  (max_size < max_entries) * max_entries/4;
233 234 235 236
   struct cso_hash_iter iter;
   struct cso_sampler **samplers_to_restore = NULL;
   unsigned to_restore = 0;

237 238
   if (hash_size > max_size)
      to_remove += hash_size - max_size;
239 240 241 242 243 244 245 246 247 248 249 250 251 252

   if (to_remove == 0)
      return;

   if (type == CSO_SAMPLER) {
      int i, j;

      samplers_to_restore = MALLOC(PIPE_SHADER_TYPES * PIPE_MAX_SAMPLERS *
                                   sizeof(*samplers_to_restore));

      /* Temporarily remove currently bound sampler states from the hash
       * table, to prevent them from being deleted
       */
      for (i = 0; i < PIPE_SHADER_TYPES; i++) {
253
         for (j = 0; j < PIPE_MAX_SAMPLERS; j++) {
254 255 256 257 258 259 260 261 262
            struct cso_sampler *sampler = ctx->samplers[i].cso_samplers[j];

            if (sampler && cso_hash_take(hash, sampler->hash_key))
               samplers_to_restore[to_restore++] = sampler;
         }
      }
   }

   iter = cso_hash_first_node(hash);
263 264 265 266
   while (to_remove) {
      /*remove elements until we're good */
      /*fixme: currently we pick the nodes to remove at random*/
      void *cso = cso_hash_iter_data(iter);
267 268 269 270

      if (!cso)
         break;

271 272 273 274 275 276
      if (delete_cso(ctx, cso, type)) {
         iter = cso_hash_erase(hash, iter);
         --to_remove;
      } else
         iter = cso_hash_iter_next(iter);
   }
277 278 279 280 281 282 283 284 285 286 287

   if (type == CSO_SAMPLER) {
      /* Put currently bound sampler states back into the hash table */
      while (to_restore--) {
         struct cso_sampler *sampler = samplers_to_restore[to_restore];

         cso_hash_insert(hash, sampler->hash_key, sampler);
      }

      FREE(samplers_to_restore);
   }
288 289
}

290
static void cso_init_vbuf(struct cso_context *cso, unsigned flags)
291 292
{
   struct u_vbuf_caps caps;
293
   bool uses_user_vertex_buffers = !(flags & CSO_NO_USER_VERTEX_BUFFERS);
Rob Clark's avatar
Rob Clark committed
294
   bool needs64b = !(flags & CSO_NO_64B_VERTEX_BUFFERS);
295

Rob Clark's avatar
Rob Clark committed
296
   u_vbuf_get_caps(cso->pipe->screen, &caps, needs64b);
297 298 299 300 301

   /* Enable u_vbuf if needed. */
   if (caps.fallback_always ||
       (uses_user_vertex_buffers &&
        caps.fallback_only_for_user_vbuffers)) {
302
      cso->vbuf = u_vbuf_create(cso->pipe, &caps);
303 304
      cso->vbuf_current = cso->vbuf;
      cso->always_use_vbuf = caps.fallback_always;
305 306
   }
}
307

308
struct cso_context *
309
cso_create_context(struct pipe_context *pipe, unsigned flags)
Keith Whitwell's avatar
Keith Whitwell committed
310 311
{
   struct cso_context *ctx = CALLOC_STRUCT(cso_context);
312
   if (!ctx)
313
      return NULL;
Keith Whitwell's avatar
Keith Whitwell committed
314 315 316 317

   ctx->cache = cso_cache_create();
   if (ctx->cache == NULL)
      goto out;
318 319 320
   cso_cache_set_sanitize_callback(ctx->cache,
                                   sanitize_hash,
                                   ctx);
Keith Whitwell's avatar
Keith Whitwell committed
321 322

   ctx->pipe = pipe;
323
   ctx->sample_mask = ~0;
Keith Whitwell's avatar
Keith Whitwell committed
324

325
   cso_init_vbuf(ctx, flags);
326

327 328 329
   /* Enable for testing: */
   if (0) cso_set_maximum_cache_size( ctx->cache, 4 );

330 331 332 333
   if (pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
                                PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0) {
      ctx->has_geometry_shader = TRUE;
   }
334 335 336 337
   if (pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_TESS_CTRL,
                                PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0) {
      ctx->has_tessellation = TRUE;
   }
338 339
   if (pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_COMPUTE,
                                      PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0) {
340 341 342
      int supported_irs =
         pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_COMPUTE,
                                        PIPE_SHADER_CAP_SUPPORTED_IRS);
343 344
      if (supported_irs & ((1 << PIPE_SHADER_IR_TGSI) |
                           (1 << PIPE_SHADER_IR_NIR))) {
345 346
         ctx->has_compute_shader = TRUE;
      }
347
   }
348 349 350 351
   if (pipe->screen->get_param(pipe->screen,
                               PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0) {
      ctx->has_streamout = TRUE;
   }
352

353
   ctx->max_sampler_seen = -1;
Keith Whitwell's avatar
Keith Whitwell committed
354 355 356
   return ctx;

out:
357
   cso_destroy_context( ctx );
Keith Whitwell's avatar
Keith Whitwell committed
358 359 360
   return NULL;
}

361
/**
362
 * Free the CSO context.
363
 */
364
void cso_destroy_context( struct cso_context *ctx )
365
{
366
   unsigned i;
367

368 369 370
   if (ctx->pipe) {
      ctx->pipe->bind_blend_state( ctx->pipe, NULL );
      ctx->pipe->bind_rasterizer_state( ctx->pipe, NULL );
371

372
      {
373
         static struct pipe_sampler_view *views[PIPE_MAX_SHADER_SAMPLER_VIEWS] = { NULL };
374 375
         static void *zeros[PIPE_MAX_SAMPLERS] = { NULL };
         struct pipe_screen *scr = ctx->pipe->screen;
376
         enum pipe_shader_type sh;
377
         for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
378 379 380 381 382 383 384 385 386 387 388
            int maxsam = scr->get_shader_param(scr, sh,
                                               PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS);
            int maxview = scr->get_shader_param(scr, sh,
                                                PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS);
            assert(maxsam <= PIPE_MAX_SAMPLERS);
            assert(maxview <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
            if (maxsam > 0) {
               ctx->pipe->bind_sampler_states(ctx->pipe, sh, 0, maxsam, zeros);
            }
            if (maxview > 0) {
               ctx->pipe->set_sampler_views(ctx->pipe, sh, 0, maxview, views);
389 390 391 392
            }
         }
      }

393 394
      ctx->pipe->bind_depth_stencil_alpha_state( ctx->pipe, NULL );
      ctx->pipe->bind_fs_state( ctx->pipe, NULL );
395
      ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_FRAGMENT, 0, NULL);
396
      ctx->pipe->bind_vs_state( ctx->pipe, NULL );
397
      ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_VERTEX, 0, NULL);
398 399
      if (ctx->has_geometry_shader) {
         ctx->pipe->bind_gs_state(ctx->pipe, NULL);
400
         ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_GEOMETRY, 0, NULL);
401 402 403
      }
      if (ctx->has_tessellation) {
         ctx->pipe->bind_tcs_state(ctx->pipe, NULL);
404
         ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_TESS_CTRL, 0, NULL);
405
         ctx->pipe->bind_tes_state(ctx->pipe, NULL);
406
         ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_TESS_EVAL, 0, NULL);
407
      }
408 409 410 411
      if (ctx->has_compute_shader) {
         ctx->pipe->bind_compute_state(ctx->pipe, NULL);
         ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_COMPUTE, 0, NULL);
      }
412
      ctx->pipe->bind_vertex_elements_state( ctx->pipe, NULL );
413

414
      if (ctx->has_streamout)
415
         ctx->pipe->set_stream_output_targets(ctx->pipe, 0, NULL, NULL);
416 417
   }

418
   for (i = 0; i < ctx->nr_fragment_views; i++) {
419
      pipe_sampler_view_reference(&ctx->fragment_views[i], NULL);
420 421
   }
   for (i = 0; i < ctx->nr_fragment_views_saved; i++) {
422
      pipe_sampler_view_reference(&ctx->fragment_views_saved[i], NULL);
423 424
   }

425 426
   util_unreference_framebuffer_state(&ctx->fb);
   util_unreference_framebuffer_state(&ctx->fb_saved);
427

428 429
   pipe_vertex_buffer_unreference(&ctx->vertex_buffer0_current);
   pipe_vertex_buffer_unreference(&ctx->vertex_buffer0_saved);
430

431 432 433 434 435
   for (i = 0; i < PIPE_SHADER_TYPES; i++) {
      pipe_resource_reference(&ctx->aux_constbuf_current[i].buffer, NULL);
      pipe_resource_reference(&ctx->aux_constbuf_saved[i].buffer, NULL);
   }

436 437 438
   pipe_resource_reference(&ctx->fragment_image0_current.resource, NULL);
   pipe_resource_reference(&ctx->fragment_image0_saved.resource, NULL);

439 440 441 442 443
   for (i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
      pipe_so_target_reference(&ctx->so_targets[i], NULL);
      pipe_so_target_reference(&ctx->so_targets_saved[i], NULL);
   }

444 445 446 447 448
   if (ctx->cache) {
      cso_cache_delete( ctx->cache );
      ctx->cache = NULL;
   }

449 450 451
   if (ctx->vbuf)
      u_vbuf_destroy(ctx->vbuf);
   FREE( ctx );
Keith Whitwell's avatar
Keith Whitwell committed
452 453 454 455 456 457 458 459 460 461 462 463 464
}


/* Those function will either find the state of the given template
 * in the cache or they will create a new state from the given
 * template, insert it in the cache and return it.
 */

/*
 * If the driver returns 0 from the create method then they will assign
 * the data member of the cso to be the template itself.
 */

465 466
enum pipe_error cso_set_blend(struct cso_context *ctx,
                              const struct pipe_blend_state *templ)
Keith Whitwell's avatar
Keith Whitwell committed
467
{
468 469
   unsigned key_size, hash_key;
   struct cso_hash_iter iter;
Keith Whitwell's avatar
Keith Whitwell committed
470 471
   void *handle;

472 473 474
   key_size = templ->independent_blend_enable ?
      sizeof(struct pipe_blend_state) :
      (char *)&(templ->rt[1]) - (char *)templ;
475
   hash_key = cso_construct_key((void*)templ, key_size);
476 477
   iter = cso_find_state_template(ctx->cache, hash_key, CSO_BLEND,
                                  (void*)templ, key_size);
478

Keith Whitwell's avatar
Keith Whitwell committed
479 480
   if (cso_hash_iter_is_null(iter)) {
      struct cso_blend *cso = MALLOC(sizeof(struct cso_blend));
481 482
      if (!cso)
         return PIPE_ERROR_OUT_OF_MEMORY;
Keith Whitwell's avatar
Keith Whitwell committed
483

484
      memset(&cso->state, 0, sizeof cso->state);
485
      memcpy(&cso->state, templ, key_size);
Keith Whitwell's avatar
Keith Whitwell committed
486 487 488 489 490
      cso->data = ctx->pipe->create_blend_state(ctx->pipe, &cso->state);
      cso->delete_state = (cso_state_callback)ctx->pipe->delete_blend_state;
      cso->context = ctx->pipe;

      iter = cso_insert_state(ctx->cache, hash_key, CSO_BLEND, cso);
491 492 493 494 495
      if (cso_hash_iter_is_null(iter)) {
         FREE(cso);
         return PIPE_ERROR_OUT_OF_MEMORY;
      }

Keith Whitwell's avatar
Keith Whitwell committed
496 497 498 499 500 501 502 503 504 505
      handle = cso->data;
   }
   else {
      handle = ((struct cso_blend *)cso_hash_iter_data(iter))->data;
   }

   if (ctx->blend != handle) {
      ctx->blend = handle;
      ctx->pipe->bind_blend_state(ctx->pipe, handle);
   }
506
   return PIPE_OK;
Keith Whitwell's avatar
Keith Whitwell committed
507 508
}

509 510
static void
cso_save_blend(struct cso_context *ctx)
511 512 513 514 515
{
   assert(!ctx->blend_saved);
   ctx->blend_saved = ctx->blend;
}

516 517
static void
cso_restore_blend(struct cso_context *ctx)
Brian's avatar
Brian committed
518
{
519 520 521 522 523
   if (ctx->blend != ctx->blend_saved) {
      ctx->blend = ctx->blend_saved;
      ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend_saved);
   }
   ctx->blend_saved = NULL;
Brian's avatar
Brian committed
524 525 526
}


527

528 529 530
enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context *ctx,
                            const struct pipe_depth_stencil_alpha_state *templ)
Keith Whitwell's avatar
Keith Whitwell committed
531
{
532 533
   unsigned key_size = sizeof(struct pipe_depth_stencil_alpha_state);
   unsigned hash_key = cso_construct_key((void*)templ, key_size);
Keith Whitwell's avatar
Keith Whitwell committed
534
   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
535
                                                       hash_key,
536 537
                                                       CSO_DEPTH_STENCIL_ALPHA,
                                                       (void*)templ, key_size);
Keith Whitwell's avatar
Keith Whitwell committed
538 539 540
   void *handle;

   if (cso_hash_iter_is_null(iter)) {
541 542
      struct cso_depth_stencil_alpha *cso =
         MALLOC(sizeof(struct cso_depth_stencil_alpha));
543 544
      if (!cso)
         return PIPE_ERROR_OUT_OF_MEMORY;
Keith Whitwell's avatar
Keith Whitwell committed
545

546
      memcpy(&cso->state, templ, sizeof(*templ));
547 548 549 550
      cso->data = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe,
                                                              &cso->state);
      cso->delete_state =
         (cso_state_callback)ctx->pipe->delete_depth_stencil_alpha_state;
Keith Whitwell's avatar
Keith Whitwell committed
551 552
      cso->context = ctx->pipe;

553 554
      iter = cso_insert_state(ctx->cache, hash_key,
                              CSO_DEPTH_STENCIL_ALPHA, cso);
555 556 557 558 559
      if (cso_hash_iter_is_null(iter)) {
         FREE(cso);
         return PIPE_ERROR_OUT_OF_MEMORY;
      }

Keith Whitwell's avatar
Keith Whitwell committed
560 561 562
      handle = cso->data;
   }
   else {
563 564
      handle = ((struct cso_depth_stencil_alpha *)
                cso_hash_iter_data(iter))->data;
Keith Whitwell's avatar
Keith Whitwell committed
565 566 567 568 569 570
   }

   if (ctx->depth_stencil != handle) {
      ctx->depth_stencil = handle;
      ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, handle);
   }
571
   return PIPE_OK;
Keith Whitwell's avatar
Keith Whitwell committed
572 573
}

574 575
static void
cso_save_depth_stencil_alpha(struct cso_context *ctx)
Brian's avatar
Brian committed
576
{
577 578 579 580
   assert(!ctx->depth_stencil_saved);
   ctx->depth_stencil_saved = ctx->depth_stencil;
}

581 582
static void
cso_restore_depth_stencil_alpha(struct cso_context *ctx)
583 584 585
{
   if (ctx->depth_stencil != ctx->depth_stencil_saved) {
      ctx->depth_stencil = ctx->depth_stencil_saved;
586 587
      ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe,
                                                ctx->depth_stencil_saved);
588 589
   }
   ctx->depth_stencil_saved = NULL;
Brian's avatar
Brian committed
590 591
}

Keith Whitwell's avatar
Keith Whitwell committed
592 593


594 595
enum pipe_error cso_set_rasterizer(struct cso_context *ctx,
                                   const struct pipe_rasterizer_state *templ)
Keith Whitwell's avatar
Keith Whitwell committed
596
{
597 598
   unsigned key_size = sizeof(struct pipe_rasterizer_state);
   unsigned hash_key = cso_construct_key((void*)templ, key_size);
Keith Whitwell's avatar
Keith Whitwell committed
599
   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
600 601
                                                       hash_key,
                                                       CSO_RASTERIZER,
602
                                                       (void*)templ, key_size);
Keith Whitwell's avatar
Keith Whitwell committed
603 604
   void *handle = NULL;

605 606 607 608 609
   /* We can't have both point_quad_rasterization (sprites) and point_smooth
    * (round AA points) enabled at the same time.
    */
   assert(!(templ->point_quad_rasterization && templ->point_smooth));

Keith Whitwell's avatar
Keith Whitwell committed
610 611
   if (cso_hash_iter_is_null(iter)) {
      struct cso_rasterizer *cso = MALLOC(sizeof(struct cso_rasterizer));
612 613
      if (!cso)
         return PIPE_ERROR_OUT_OF_MEMORY;
Keith Whitwell's avatar
Keith Whitwell committed
614

615
      memcpy(&cso->state, templ, sizeof(*templ));
Keith Whitwell's avatar
Keith Whitwell committed
616
      cso->data = ctx->pipe->create_rasterizer_state(ctx->pipe, &cso->state);
617 618
      cso->delete_state =
         (cso_state_callback)ctx->pipe->delete_rasterizer_state;
Keith Whitwell's avatar
Keith Whitwell committed
619 620
      cso->context = ctx->pipe;

621 622 623 624 625 626
      iter = cso_insert_state(ctx->cache, hash_key, CSO_RASTERIZER, cso);
      if (cso_hash_iter_is_null(iter)) {
         FREE(cso);
         return PIPE_ERROR_OUT_OF_MEMORY;
      }

Keith Whitwell's avatar
Keith Whitwell committed
627 628 629 630 631 632 633 634 635 636
      handle = cso->data;
   }
   else {
      handle = ((struct cso_rasterizer *)cso_hash_iter_data(iter))->data;
   }

   if (ctx->rasterizer != handle) {
      ctx->rasterizer = handle;
      ctx->pipe->bind_rasterizer_state(ctx->pipe, handle);
   }
637
   return PIPE_OK;
Keith Whitwell's avatar
Keith Whitwell committed
638 639
}

640 641
static void
cso_save_rasterizer(struct cso_context *ctx)
Brian's avatar
Brian committed
642
{
643 644
   assert(!ctx->rasterizer_saved);
   ctx->rasterizer_saved = ctx->rasterizer;
Brian's avatar
Brian committed
645
}
Keith Whitwell's avatar
Keith Whitwell committed
646

647 648
static void
cso_restore_rasterizer(struct cso_context *ctx)
649 650 651 652 653 654 655
{
   if (ctx->rasterizer != ctx->rasterizer_saved) {
      ctx->rasterizer = ctx->rasterizer_saved;
      ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rasterizer_saved);
   }
   ctx->rasterizer_saved = NULL;
}
Keith Whitwell's avatar
Keith Whitwell committed
656

657

658
void cso_set_fragment_shader_handle(struct cso_context *ctx, void *handle )
659 660 661 662 663 664 665
{
   if (ctx->fragment_shader != handle) {
      ctx->fragment_shader = handle;
      ctx->pipe->bind_fs_state(ctx->pipe, handle);
   }
}

666 667
static void
cso_save_fragment_shader(struct cso_context *ctx)
Brian's avatar
Brian committed
668
{
669 670
   assert(!ctx->fragment_shader_saved);
   ctx->fragment_shader_saved = ctx->fragment_shader;
Brian's avatar
Brian committed
671 672
}

673 674
static void
cso_restore_fragment_shader(struct cso_context *ctx)
675 676 677 678 679 680 681 682 683
{
   if (ctx->fragment_shader_saved != ctx->fragment_shader) {
      ctx->pipe->bind_fs_state(ctx->pipe, ctx->fragment_shader_saved);
      ctx->fragment_shader = ctx->fragment_shader_saved;
   }
   ctx->fragment_shader_saved = NULL;
}


684
void cso_set_vertex_shader_handle(struct cso_context *ctx, void *handle)
685 686 687 688 689 690 691
{
   if (ctx->vertex_shader != handle) {
      ctx->vertex_shader = handle;
      ctx->pipe->bind_vs_state(ctx->pipe, handle);
   }
}

692 693
static void
cso_save_vertex_shader(struct cso_context *ctx)
Brian's avatar
Brian committed
694
{
695 696 697 698
   assert(!ctx->vertex_shader_saved);
   ctx->vertex_shader_saved = ctx->vertex_shader;
}

699 700
static void
cso_restore_vertex_shader(struct cso_context *ctx)
701 702
{
   if (ctx->vertex_shader_saved != ctx->vertex_shader) {
Keith Whitwell's avatar
Keith Whitwell committed
703
      ctx->pipe->bind_vs_state(ctx->pipe, ctx->vertex_shader_saved);
704 705 706 707 708 709
      ctx->vertex_shader = ctx->vertex_shader_saved;
   }
   ctx->vertex_shader_saved = NULL;
}


710 711
void cso_set_framebuffer(struct cso_context *ctx,
                         const struct pipe_framebuffer_state *fb)
712
{
713
   if (memcmp(&ctx->fb, fb, sizeof(*fb)) != 0) {
714
      util_copy_framebuffer_state(&ctx->fb, fb);
715 716 717 718
      ctx->pipe->set_framebuffer_state(ctx->pipe, fb);
   }
}

719 720
static void
cso_save_framebuffer(struct cso_context *ctx)
721
{
722
   util_copy_framebuffer_state(&ctx->fb_saved, &ctx->fb);
723 724
}

725 726
static void
cso_restore_framebuffer(struct cso_context *ctx)
727 728
{
   if (memcmp(&ctx->fb, &ctx->fb_saved, sizeof(ctx->fb))) {
729
      util_copy_framebuffer_state(&ctx->fb, &ctx->fb_saved);
730
      ctx->pipe->set_framebuffer_state(ctx->pipe, &ctx->fb);
731
      util_unreference_framebuffer_state(&ctx->fb_saved);
732 733 734 735
   }
}


736 737
void cso_set_viewport(struct cso_context *ctx,
                      const struct pipe_viewport_state *vp)
738 739 740
{
   if (memcmp(&ctx->vp, vp, sizeof(*vp))) {
      ctx->vp = *vp;
741
      ctx->pipe->set_viewport_states(ctx->pipe, 0, 1, vp);
742 743 744
   }
}

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
/**
 * Setup viewport state for given width and height (position is always (0,0)).
 * Invert the Y axis if 'invert' is true.
 */
void
cso_set_viewport_dims(struct cso_context *ctx,
                      float width, float height, boolean invert)
{
   struct pipe_viewport_state vp;
   vp.scale[0] = width * 0.5f;
   vp.scale[1] = height * (invert ? -0.5f : 0.5f);
   vp.scale[2] = 0.5f;
   vp.translate[0] = 0.5f * width;
   vp.translate[1] = 0.5f * height;
   vp.translate[2] = 0.5f;
760 761 762 763
   vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
   vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
   vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
   vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
764 765 766
   cso_set_viewport(ctx, &vp);
}

767 768
static void
cso_save_viewport(struct cso_context *ctx)
769 770 771 772 773
{
   ctx->vp_saved = ctx->vp;
}


774 775
static void
cso_restore_viewport(struct cso_context *ctx)
776 777 778
{
   if (memcmp(&ctx->vp, &ctx->vp_saved, sizeof(ctx->vp))) {
      ctx->vp = ctx->vp_saved;
779
      ctx->pipe->set_viewport_states(ctx->pipe, 0, 1, &ctx->vp);
780 781 782 783
   }
}


784 785
void cso_set_blend_color(struct cso_context *ctx,
                         const struct pipe_blend_color *bc)
786 787 788 789 790
{
   if (memcmp(&ctx->blend_color, bc, sizeof(ctx->blend_color))) {
      ctx->blend_color = *bc;
      ctx->pipe->set_blend_color(ctx->pipe, bc);
   }
Brian's avatar
Brian committed
791
}
792

793
void cso_set_sample_mask(struct cso_context *ctx, unsigned sample_mask)
794 795 796 797 798 799 800
{
   if (ctx->sample_mask != sample_mask) {
      ctx->sample_mask = sample_mask;
      ctx->pipe->set_sample_mask(ctx->pipe, sample_mask);
   }
}

801 802
static void
cso_save_sample_mask(struct cso_context *ctx)
803 804 805 806
{
   ctx->sample_mask_saved = ctx->sample_mask;
}

807 808
static void
cso_restore_sample_mask(struct cso_context *ctx)
809 810 811 812
{
   cso_set_sample_mask(ctx, ctx->sample_mask_saved);
}

813 814 815 816 817 818 819 820
void cso_set_min_samples(struct cso_context *ctx, unsigned min_samples)
{
   if (ctx->min_samples != min_samples && ctx->pipe->set_min_samples) {
      ctx->min_samples = min_samples;
      ctx->pipe->set_min_samples(ctx->pipe, min_samples);
   }
}

821 822
static void
cso_save_min_samples(struct cso_context *ctx)
823 824 825 826
{
   ctx->min_samples_saved = ctx->min_samples;
}

827 828
static void
cso_restore_min_samples(struct cso_context *ctx)
829 830 831 832
{
   cso_set_min_samples(ctx, ctx->min_samples_saved);
}

833 834
void cso_set_stencil_ref(struct cso_context *ctx,
                         const struct pipe_stencil_ref *sr)
835 836 837 838 839 840 841
{
   if (memcmp(&ctx->stencil_ref, sr, sizeof(ctx->stencil_ref))) {
      ctx->stencil_ref = *sr;
      ctx->pipe->set_stencil_ref(ctx->pipe, sr);
   }
}

842 843
static void
cso_save_stencil_ref(struct cso_context *ctx)
844 845 846 847 848
{
   ctx->stencil_ref_saved = ctx->stencil_ref;
}


849 850
static void
cso_restore_stencil_ref(struct cso_context *ctx)
851
{
852 853
   if (memcmp(&ctx->stencil_ref, &ctx->stencil_ref_saved,
              sizeof(ctx->stencil_ref))) {
854 855 856 857 858
      ctx->stencil_ref = ctx->stencil_ref_saved;
      ctx->pipe->set_stencil_ref(ctx->pipe, &ctx->stencil_ref);
   }
}

859
void cso_set_render_condition(struct cso_context *ctx,
860
                              struct pipe_query *query,
861 862
                              boolean condition,
                              enum pipe_render_cond_flag mode)
863 864 865
{
   struct pipe_context *pipe = ctx->pipe;

866 867 868 869
   if (ctx->render_condition != query ||
       ctx->render_condition_mode != mode ||
       ctx->render_condition_cond != condition) {
      pipe->render_condition(pipe, query, condition, mode);
870
      ctx->render_condition = query;
871
      ctx->render_condition_cond = condition;
872 873 874 875
      ctx->render_condition_mode = mode;
   }
}

876 877
static void
cso_save_render_condition(struct cso_context *ctx)
878 879
{
   ctx->render_condition_saved = ctx->render_condition;
880
   ctx->render_condition_cond_saved = ctx->render_condition_cond;
881 882 883
   ctx->render_condition_mode_saved = ctx->render_condition_mode;
}

884 885
static void
cso_restore_render_condition(struct cso_context *ctx)
886 887
{
   cso_set_render_condition(ctx, ctx->render_condition_saved,
888
                            ctx->render_condition_cond_saved,
889 890 891
                            ctx->render_condition_mode_saved);
}

892
void cso_set_geometry_shader_handle(struct cso_context *ctx, void *handle)
893
{
894 895 896
   assert(ctx->has_geometry_shader || !handle);

   if (ctx->has_geometry_shader && ctx->geometry_shader != handle) {
897 898 899 900 901
      ctx->geometry_shader = handle;
      ctx->pipe->bind_gs_state(ctx->pipe, handle);
   }
}

902 903
static void
cso_save_geometry_shader(struct cso_context *ctx)
904
{
905 906 907 908
   if (!ctx->has_geometry_shader) {
      return;
   }

909 910 911 912
   assert(!ctx->geometry_shader_saved);
   ctx->geometry_shader_saved = ctx->geometry_shader;
}

913 914
static void
cso_restore_geometry_shader(struct cso_context *ctx)
915
{
916 917 918 919
   if (!ctx->has_geometry_shader) {
      return;
   }

920 921 922 923 924
   if (ctx->geometry_shader_saved != ctx->geometry_shader) {
      ctx->pipe->bind_gs_state(ctx->pipe, ctx->geometry_shader_saved);
      ctx->geometry_shader = ctx->geometry_shader_saved;
   }
   ctx->geometry_shader_saved = NULL;
925 926 927 928 929 930 931 932 933 934 935 936
}

void cso_set_tessctrl_shader_handle(struct cso_context *ctx, void *handle)
{
   assert(ctx->has_tessellation || !handle);

   if (ctx->has_tessellation && ctx->tessctrl_shader != handle) {
      ctx->tessctrl_shader = handle;
      ctx->pipe->bind_tcs_state(ctx->pipe, handle);
   }
}

937 938
static void
cso_save_tessctrl_shader(struct cso_context *ctx)