summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/svga/svga_state_sampler.c
blob: 5f314fd6c8661280b051a9892b3235460a641f2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
 * Copyright (c) 2013-2024 Broadcom. All Rights Reserved.
 * The term “Broadcom” refers to Broadcom Inc.
 * and/or its subsidiaries.
 * SPDX-License-Identifier: MIT
 */


/**
 * VGPU10 sampler and sampler view functions.
 */


#include "pipe/p_defines.h"
#include "util/u_bitmask.h"
#include "util/format/u_format.h"
#include "util/u_inlines.h"
#include "util/u_math.h"
#include "util/u_memory.h"

#include "svga_cmd.h"
#include "svga_context.h"
#include "svga_format.h"
#include "svga_resource_buffer.h"
#include "svga_resource_texture.h"
#include "svga_sampler_view.h"
#include "svga_shader.h"
#include "svga_state.h"
#include "svga_surface.h"
#include "svga3d_surfacedefs.h"

/** Get resource handle for a texture or buffer */
static inline struct svga_winsys_surface *
svga_resource_handle(struct pipe_resource *res)
{
   if (res->target == PIPE_BUFFER) {
      return svga_buffer(res)->handle;
   }
   else {
      return svga_texture(res)->handle;
   }
}


/**
 * This helper function returns TRUE if the specified resource collides with
 * any of the resources bound to any of the currently bound sampler views.
 */
bool
svga_check_sampler_view_resource_collision(const struct svga_context *svga,
                                           const struct svga_winsys_surface *res,
                                           enum pipe_shader_type shader)
{
   struct pipe_screen *screen = svga->pipe.screen;
   unsigned i;

   if (svga_screen(screen)->debug.no_surface_view) {
      return false;
   }

   if (!svga_curr_shader_use_samplers(svga, shader))
      return false;

   for (i = 0; i < svga->curr.num_sampler_views[shader]; i++) {
      struct svga_pipe_sampler_view *sv =
         svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);

      if (sv && res == svga_resource_handle(sv->base.texture)) {
         return true;
      }
   }

   return false;
}


/**
 * Check if there are any resources that are both bound to a render target
 * and bound as a shader resource for the given type of shader.
 */
bool
svga_check_sampler_framebuffer_resource_collision(struct svga_context *svga,
                                                  enum pipe_shader_type shader)
{
   struct svga_surface *surf;
   unsigned i;

   for (i = 0; i < svga->curr.framebuffer.nr_cbufs; i++) {
      surf = svga_surface(svga->curr.framebuffer.cbufs[i]);
      if (surf &&
          svga_check_sampler_view_resource_collision(svga, surf->handle,
                                                     shader)) {
         return true;
      }
   }

   surf = svga_surface(svga->curr.framebuffer.zsbuf);
   if (surf &&
       svga_check_sampler_view_resource_collision(svga, surf->handle, shader)) {
      return true;
   }

   return false;
}


/**
 * Create a DX ShaderResourceSamplerView for the given pipe_sampler_view,
 * if needed.
 */
enum pipe_error
svga_validate_pipe_sampler_view(struct svga_context *svga,
                                struct svga_pipe_sampler_view *sv)
{
   enum pipe_error ret = PIPE_OK;

   if (sv->id == SVGA3D_INVALID_ID) {
      struct svga_screen *ss = svga_screen(svga->pipe.screen);
      struct pipe_resource *texture = sv->base.texture;
      struct svga_winsys_surface *surface;
      SVGA3dSurfaceFormat format;
      SVGA3dResourceType resourceDim;
      SVGA3dShaderResourceViewDesc viewDesc;
      enum pipe_format viewFormat = sv->base.format;
      enum pipe_texture_target target = sv->base.target;

      /* vgpu10 cannot create a BGRX view for a BGRA resource, so force it to
       * create a BGRA view (and vice versa).
       */
      if (viewFormat == PIPE_FORMAT_B8G8R8X8_UNORM &&
          svga_texture_device_format_has_alpha(texture)) {
         viewFormat = PIPE_FORMAT_B8G8R8A8_UNORM;
      }
      else if (viewFormat == PIPE_FORMAT_B8G8R8A8_UNORM &&
               !svga_texture_device_format_has_alpha(texture)) {
         viewFormat = PIPE_FORMAT_B8G8R8X8_UNORM;
      }

      if (target == PIPE_BUFFER) {
         unsigned pf_flags;
         assert(texture->target == PIPE_BUFFER);
         svga_translate_texture_buffer_view_format(viewFormat,
                                                   &format,
                                                   &pf_flags);
         surface = svga_buffer_handle(svga, texture, PIPE_BIND_SAMPLER_VIEW);
      }
      else {
         format = svga_translate_format(ss, viewFormat,
                                        PIPE_BIND_SAMPLER_VIEW);

         /* Convert the format to a sampler-friendly format, if needed */
         format = svga_sampler_format(format);

         surface = svga_texture(texture)->handle;
      }

      assert(format != SVGA3D_FORMAT_INVALID);

      if (target == PIPE_BUFFER) {
         unsigned elem_size = util_format_get_blocksize(sv->base.format);

         viewDesc.buffer.firstElement = sv->base.u.buf.offset / elem_size;
         viewDesc.buffer.numElements = sv->base.u.buf.size / elem_size;
      }
      else {
         viewDesc.tex.mostDetailedMip = sv->base.u.tex.first_level;
         viewDesc.tex.firstArraySlice = sv->base.u.tex.first_layer;
         viewDesc.tex.mipLevels = (sv->base.u.tex.last_level -
                                   sv->base.u.tex.first_level + 1);
      }

      /* arraySize in viewDesc specifies the number of array slices in a
       * texture array. For 3D texture, last_layer in
       * pipe_sampler_view specifies the last slice of the texture
       * which is different from the last slice in a texture array,
       * hence we need to set arraySize to 1 explicitly.
       */
      viewDesc.tex.arraySize =
         (target == PIPE_TEXTURE_3D || target == PIPE_BUFFER) ? 1 :
            (sv->base.u.tex.last_layer - sv->base.u.tex.first_layer + 1);

      switch (target) {
      case PIPE_BUFFER:
         resourceDim = SVGA3D_RESOURCE_BUFFER;
         break;
      case PIPE_TEXTURE_1D:
      case PIPE_TEXTURE_1D_ARRAY:
         resourceDim = SVGA3D_RESOURCE_TEXTURE1D;
         break;
      case PIPE_TEXTURE_RECT:
      case PIPE_TEXTURE_2D:
      case PIPE_TEXTURE_2D_ARRAY:
         resourceDim = SVGA3D_RESOURCE_TEXTURE2D;
         break;
      case PIPE_TEXTURE_3D:
         resourceDim = SVGA3D_RESOURCE_TEXTURE3D;
         break;
      case PIPE_TEXTURE_CUBE:
      case PIPE_TEXTURE_CUBE_ARRAY:
         resourceDim = SVGA3D_RESOURCE_TEXTURECUBE;
         break;

      default:
         assert(!"Unexpected texture type");
         resourceDim = SVGA3D_RESOURCE_TEXTURE2D;
      }

      sv->id = util_bitmask_add(svga->sampler_view_id_bm);

      ret = SVGA3D_vgpu10_DefineShaderResourceView(svga->swc,
                                                   sv->id,
                                                   surface,
                                                   format,
                                                   resourceDim,
                                                   &viewDesc);
      if (ret != PIPE_OK) {
         util_bitmask_clear(svga->sampler_view_id_bm, sv->id);
         sv->id = SVGA3D_INVALID_ID;
      }
   }

   return ret;
}


static enum pipe_error
update_sampler_resources(struct svga_context *svga, uint64_t dirty)
{
   enum pipe_error ret = PIPE_OK;
   enum pipe_shader_type shader;

   assert(svga_have_vgpu10(svga));

   for (shader = PIPE_SHADER_VERTEX; shader < PIPE_SHADER_COMPUTE; shader++) {
      SVGA3dShaderResourceViewId ids[PIPE_MAX_SAMPLERS];
      struct svga_winsys_surface *surfaces[PIPE_MAX_SAMPLERS];
      struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
      unsigned count;
      unsigned nviews;
      unsigned i;

      count = svga->curr.num_sampler_views[shader];
      for (i = 0; i < count; i++) {
         struct svga_pipe_sampler_view *sv =
            svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);

         if (sv) {
            surfaces[i] = svga_resource_handle(sv->base.texture);

            ret = svga_validate_pipe_sampler_view(svga, sv);
            if (ret != PIPE_OK)
               return ret;

            assert(sv->id != SVGA3D_INVALID_ID);
            ids[i] = sv->id;
            sampler_views[i] = &sv->base;
         }
         else {
            surfaces[i] = NULL;
            ids[i] = SVGA3D_INVALID_ID;
            sampler_views[i] = NULL;
         }
      }

      for (; i < svga->state.hw_draw.num_sampler_views[shader]; i++) {
         ids[i] = SVGA3D_INVALID_ID;
         surfaces[i] = NULL;
         sampler_views[i] = NULL;
      }

      /* Number of ShaderResources that need to be modified. This includes
       * the one that need to be unbound.
       */
      nviews = MAX2(svga->state.hw_draw.num_sampler_views[shader], count);
      if (nviews > 0) {
         if (count != svga->state.hw_draw.num_sampler_views[shader] ||
             memcmp(sampler_views, svga->state.hw_draw.sampler_views[shader],
                    count * sizeof(sampler_views[0])) != 0) {
            SVGA3dShaderResourceViewId *pIds = ids;
            struct svga_winsys_surface **pSurf = surfaces;
            unsigned numSR = 0;

            /* Loop through the sampler view list to only emit
             * the sampler views that are not already in the
             * corresponding entries in the device's
             * shader resource list.
             */
            for (i = 0; i < nviews; i++) {
                bool emit;

                emit = sampler_views[i] ==
                       svga->state.hw_draw.sampler_views[shader][i];

                if (!emit && i == nviews-1) {
                   /* Include the last sampler view in the next emit
                    * if it is different.
                    */
                   emit = true;
                   numSR++;
                   i++;
                }
 
                if (emit) {
                   /* numSR can only be 0 if the first entry of the list
                    * is the same as the one in the device list.
                    * In this case, * there is nothing to send yet.
                    */
                   if (numSR) {
                      ret = SVGA3D_vgpu10_SetShaderResources(
                               svga->swc,
                               svga_shader_type(shader),
                               i - numSR, /* startView */
                               numSR,
                               pIds,
                               pSurf);

                      if (ret != PIPE_OK)
                         return ret;
                   }
                   pIds += (numSR + 1);
                   pSurf += (numSR + 1);
                   numSR = 0;
                }
                else
                   numSR++;
            }

            /* Save referenced sampler views in the hw draw state.  */
            svga->state.hw_draw.num_sampler_views[shader] = count;
            for (i = 0; i < nviews; i++) {
               pipe_sampler_view_reference(
                  &svga->state.hw_draw.sampler_views[shader][i],
                  sampler_views[i]);
            }
         }
      }
   }

   /* Handle polygon stipple sampler view */
   if (svga->curr.rast->templ.poly_stipple_enable) {
      const unsigned unit =
         svga_fs_variant(svga->state.hw_draw.fs)->pstipple_sampler_unit;
      struct svga_pipe_sampler_view *sv = svga->polygon_stipple.sampler_view;
      struct svga_winsys_surface *surface;

      assert(sv);
      if (!sv) {
         return PIPE_OK;  /* probably out of memory */
      }

      ret = svga_validate_pipe_sampler_view(svga, sv);
      if (ret != PIPE_OK)
         return ret;

      surface = svga_resource_handle(sv->base.texture);
      ret = SVGA3D_vgpu10_SetShaderResources(
               svga->swc,
               svga_shader_type(PIPE_SHADER_FRAGMENT),
               unit, /* startView */
               1,
               &sv->id,
               &surface);
   }
   return ret;
}


struct svga_tracked_state svga_hw_sampler_bindings = {
   "shader resources emit",
   SVGA_NEW_STIPPLE |
   SVGA_NEW_TEXTURE_BINDING,
   update_sampler_resources
};



static enum pipe_error
update_samplers(struct svga_context *svga, uint64_t dirty )
{
   enum pipe_error ret = PIPE_OK;
   enum pipe_shader_type shader;

   assert(svga_have_vgpu10(svga));

   for (shader = PIPE_SHADER_VERTEX; shader < PIPE_SHADER_COMPUTE; shader++) {
      const unsigned count = svga->curr.num_samplers[shader];
      SVGA3dSamplerId ids[PIPE_MAX_SAMPLERS*2];
      unsigned i;
      unsigned nsamplers = 0;
      bool sampler_state_mapping =
         svga_use_sampler_state_mapping(svga, count);

      for (i = 0; i < count; i++) {
         bool fs_shadow = false;
         const struct svga_sampler_state *sampler = svga->curr.sampler[shader][i];

         /* _NEW_FS */
         if (shader == PIPE_SHADER_FRAGMENT) {
            struct svga_fs_variant *fs =
               svga_fs_variant(svga->state.hw_draw.fs);

            if (fs && (fs->fs_shadow_compare_units & (1 << i))) {

               /* Use the alternate sampler state with the compare
                * bit disabled when comparison is done in the shader and
                * sampler state mapping is not enabled.
                */
               fs_shadow = true;
            }
         }

         if (!sampler_state_mapping) {
            if (sampler) {
               SVGA3dSamplerId id = sampler->id[fs_shadow];
               assert(id != SVGA3D_INVALID_ID);
               ids[i] = id;
            }
            else {
               ids[i] = SVGA3D_INVALID_ID;
            }
            nsamplers++;
         }
         else {
            if (sampler) {
               SVGA3dSamplerId id = sampler->id[0];
               assert(id != SVGA3D_INVALID_ID);

               /* Check if the sampler id is already on the ids list */
               unsigned k;
               for (k = 0; k < nsamplers; k++) {
                   if (ids[k] == id)
                      break;
               }

               /* add the id to the list if it is not already on the list */
               if (k == nsamplers) {
                  ids[nsamplers++] = id;

                  if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
                     /*
                      * add the alternate sampler state as well as the shader
                      * might use this alternate sampler state which has comparison
                      * disabled when the comparison is done in the shader.
                      */
                     ids[nsamplers++] = sampler->id[1];
                  }
               }
            }
         }
      }

      for (i = nsamplers; i < svga->state.hw_draw.num_samplers[shader]; i++) {
         ids[i] = SVGA3D_INVALID_ID;
      }

      unsigned nsamplerIds =
         MAX2(nsamplers, svga->state.hw_draw.num_samplers[shader]);

      if (nsamplerIds > 0) {

         if (nsamplers > SVGA3D_DX_MAX_SAMPLERS) {
            debug_warn_once("Too many sampler states");
            nsamplers = SVGA3D_DX_MAX_SAMPLERS;
         }

         if (nsamplers != svga->state.hw_draw.num_samplers[shader] ||
             memcmp(ids, svga->state.hw_draw.samplers[shader],
                    nsamplerIds * sizeof(ids[0])) != 0) {

            /* HW state is really changing */
            ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
                                            nsamplerIds,
                                            0,                       /* start */
                                            svga_shader_type(shader), /* type */
                                            ids);
            if (ret != PIPE_OK)
               return ret;
            memcpy(svga->state.hw_draw.samplers[shader], ids,
                   nsamplerIds * sizeof(ids[0]));
            svga->state.hw_draw.num_samplers[shader] = nsamplers;
         }
      }
   }

   /* Handle polygon stipple sampler texture */
   if (svga->curr.rast->templ.poly_stipple_enable) {
      const unsigned unit =
         svga_fs_variant(svga->state.hw_draw.fs)->pstipple_sampler_state_index;
      struct svga_sampler_state *sampler = svga->polygon_stipple.sampler;

      assert(sampler);
      if (!sampler) {
         return PIPE_OK; /* probably out of memory */
      }

      if (svga->state.hw_draw.samplers[PIPE_SHADER_FRAGMENT][unit]
          != sampler->id[0]) {
         ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
                                         1, /* count */
                                         unit, /* start */
                                         SVGA3D_SHADERTYPE_PS,
                                         &sampler->id[0]);
         if (ret != PIPE_OK)
            return ret;

         /* save the polygon stipple sampler in the hw draw state */
         svga->state.hw_draw.samplers[PIPE_SHADER_FRAGMENT][unit] =
            sampler->id[0];
      }
      svga->state.hw_draw.num_samplers[PIPE_SHADER_FRAGMENT]++;
   }

   return ret;
}


struct svga_tracked_state svga_hw_sampler = {
   "texture sampler emit",
   (SVGA_NEW_FS |
    SVGA_NEW_SAMPLER |
    SVGA_NEW_STIPPLE),
   update_samplers
};


static enum pipe_error
update_cs_sampler_resources(struct svga_context *svga, uint64_t dirty)
{
   enum pipe_error ret = PIPE_OK;
   enum pipe_shader_type shader = PIPE_SHADER_COMPUTE;

   assert(svga_have_sm5(svga));

   SVGA3dShaderResourceViewId ids[PIPE_MAX_SAMPLERS];
   struct svga_winsys_surface *surfaces[PIPE_MAX_SAMPLERS];
   struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
   unsigned count;
   unsigned nviews;
   unsigned i;
   struct svga_compute_shader *cs = svga->curr.cs;

   count = svga->curr.num_sampler_views[shader];
   if (!cs || !cs->base.info.uses_samplers)
      count = 0;

   for (i = 0; i < count; i++) {
      struct svga_pipe_sampler_view *sv =
         svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);

      if (sv) {
         surfaces[i] = svga_resource_handle(sv->base.texture);

         ret = svga_validate_pipe_sampler_view(svga, sv);
         if (ret != PIPE_OK)
            return ret;

         assert(sv->id != SVGA3D_INVALID_ID);
         ids[i] = sv->id;
         sampler_views[i] = &sv->base;
      }
      else {
         surfaces[i] = NULL;
         ids[i] = SVGA3D_INVALID_ID;
         sampler_views[i] = NULL;
      }
   }

   for (; i < svga->state.hw_draw.num_sampler_views[shader]; i++) {
      ids[i] = SVGA3D_INVALID_ID;
      surfaces[i] = NULL;
      sampler_views[i] = NULL;
   }

   /* Number of ShaderResources that need to be modified. This includes
    * the one that need to be unbound.
    */
   nviews = MAX2(svga->state.hw_draw.num_sampler_views[shader], count);
   if (nviews > 0) {
      if (count != svga->state.hw_draw.num_sampler_views[shader] ||
          memcmp(sampler_views, svga->state.hw_draw.sampler_views[shader],
                 count * sizeof(sampler_views[0])) != 0) {
         SVGA3dShaderResourceViewId *pIds = ids;
         struct svga_winsys_surface **pSurf = surfaces;
         unsigned numSR = 0;

         /* Loop through the sampler view list to only emit the sampler views
          * that are not already in the corresponding entries in the device's
          * shader resource list.
          */
         for (i = 0; i < nviews; i++) {
            bool emit;

            emit = sampler_views[i] ==
                   svga->state.hw_draw.sampler_views[shader][i];

            if (!emit && i == nviews - 1) {
               /* Include the last sampler view in the next emit
                * if it is different.
                */
               emit = true;
               numSR++;
               i++;
            }

            if (emit) {
               /* numSR can only be 0 if the first entry of the list
                * is the same as the one in the device list.
                * In this case, * there is nothing to send yet.
                */
               if (numSR) {
                  ret = SVGA3D_vgpu10_SetShaderResources(svga->swc,
                           svga_shader_type(shader),
                           i - numSR, /* startView */
                           numSR,
                           pIds,
                           pSurf);

                  if (ret != PIPE_OK)
                     return ret;
               }
               pIds += (numSR + 1);
               pSurf += (numSR + 1);
               numSR = 0;
            }
            else
               numSR++;
         }

         /* Save referenced sampler views in the hw draw state.  */
         svga->state.hw_draw.num_sampler_views[shader] = count;
         for (i = 0; i < nviews; i++) {
            pipe_sampler_view_reference(
               &svga->state.hw_draw.sampler_views[shader][i],
               sampler_views[i]);
         }
      }
   }
   return ret;
}


struct svga_tracked_state svga_hw_cs_sampler_bindings = {
   "cs shader resources emit",
   SVGA_NEW_TEXTURE_BINDING,
   update_cs_sampler_resources
};

static enum pipe_error
update_cs_samplers(struct svga_context *svga, uint64_t dirty )
{
   enum pipe_error ret = PIPE_OK;
   enum pipe_shader_type shader = PIPE_SHADER_COMPUTE;

   assert(svga_have_sm5(svga));

   const unsigned count = svga->curr.num_samplers[shader];
   SVGA3dSamplerId ids[PIPE_MAX_SAMPLERS];
   unsigned i;
   unsigned nsamplers;

   for (i = 0; i < count; i++) {
      if (svga->curr.sampler[shader][i]) {
         ids[i] = svga->curr.sampler[shader][i]->id[0];
         assert(ids[i] != SVGA3D_INVALID_ID);
      }
      else {
         ids[i] = SVGA3D_INVALID_ID;
      }
   }

   for (; i < svga->state.hw_draw.num_samplers[shader]; i++) {
      ids[i] = SVGA3D_INVALID_ID;
   }

   nsamplers = MAX2(svga->state.hw_draw.num_samplers[shader], count);
   if (nsamplers > 0) {
      if (count != svga->state.hw_draw.num_samplers[shader] ||
          memcmp(ids, svga->state.hw_draw.samplers[shader],
                 count * sizeof(ids[0])) != 0) {
         /* HW state is really changing */
         ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
                                         nsamplers,
                                         0,                        /* start */
                                         svga_shader_type(shader), /* type */
                                         ids);
         if (ret != PIPE_OK)
            return ret;

         memcpy(svga->state.hw_draw.samplers[shader], ids,
                nsamplers * sizeof(ids[0]));
         svga->state.hw_draw.num_samplers[shader] = count;
      }
   }

   return ret;
}


struct svga_tracked_state svga_hw_cs_sampler = {
   "texture cs sampler emit",
   (SVGA_NEW_CS |
    SVGA_NEW_SAMPLER),
   update_cs_samplers
};