aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compute/skc/place.cl
blob: 00f16f78430b2a7d516da45a410a219783bb2d31 (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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can
 * be found in the LICENSE file.
 *
 */

//
//
//

#include "device_cl_12_gen9.h"
#include "common.h"
#include "atomic_cl.h"
#include "raster.h"
#include "tile.h"

//
//
//

#define SKC_PLACE_SUBGROUP_MASK      (SKC_PLACE_SUBGROUP_SIZE - 1)
#define SKC_PLACE_SUBGROUP_LAST      (SKC_PLACE_SUBGROUP_SIZE - 1)

//
//
//

#define SKC_PLACE_SMEM_COUNT_TTSK    SKC_MAX_MACRO(SKC_RASTER_NODE_MAX_TTSK,SKC_PLACE_SUBGROUP_SIZE)
#define SKC_PLACE_SMEM_COUNT_TTPK    SKC_RASTER_NODE_MAX_TTPK

//
//
//

#define SKC_PLACE_X                  (SKC_DEVICE_BLOCK_DWORDS / SKC_PLACE_SUBGROUP_SIZE)

//
//
//

#if   ( SKC_PLACE_X == 1 )
#define SKC_PLACE_EXPAND()           SKC_EXPAND_1()
#define SKC_PLACE_EXPAND_I_LAST      0

#elif ( SKC_PLACE_X == 2 )
#define SKC_PLACE_EXPAND()           SKC_EXPAND_2()
#define SKC_PLACE_EXPAND_I_LAST      1

#elif ( SKC_PLACE_X == 4 )
#define SKC_PLACE_EXPAND()           SKC_EXPAND_4()
#define SKC_PLACE_EXPAND_I_LAST      3

#elif ( SKC_PLACE_X == 8 )
#define SKC_PLACE_EXPAND()           SKC_EXPAND_8()
#define SKC_PLACE_EXPAND_I_LAST      7

#elif ( SKC_PLACE_X == 16)
#define SKC_PLACE_EXPAND()           SKC_EXPAND_16()
#define SKC_PLACE_EXPAND_I_LAST      15
#endif

//
// PREFIX STORES THE 64-BIT KEYS WITH TWO 32-BIT SUBGROUP-WIDE
// COALESCED WRITES.  LO FIRST, FOLLOWED BY HI.
//
// THIS SLIGHTLY COMPLICATES LOADING BY THE PLACE KERNEL IF THE
// KERNELS USE DIFFERENT SUBGROUP SIZES.
//
// THE BENEFIT IS THAT THE RASTER RECLAIM KERNEL ONLY HAS TO LOAD THE
// LO WORD OF THE KEY SINCE IT CONTAINS THE BLOCK ID.
//
// NOTE: AT THIS POINT, ONLY INTEL'S HD GRAPHICS ARCHITECTURE UNDER
// OPENCL SUPPORTS SELECTING A SUBGROUP SIZE (8/16/32). VULKAN MAY
// ONLY SUPPORT A SUBGROUP SIZE OF 16.
//

#if    ( SKC_PREFIX_SUBGROUP_SIZE == SKC_PLACE_SUBGROUP_SIZE )

#define SKC_PLACE_STRIDE_H(L)              (L)
#define SKC_PLACE_STRIDE_V_LO(I)           (I * 2 * SKC_PLACE_SUBGROUP_SIZE)
#define SKC_PLACE_STRIDE_V_HI(I)           (SKC_PLACE_STRIDE_V_LO(I) + SKC_PLACE_SUBGROUP_SIZE)

#elif  ( SKC_PREFIX_SUBGROUP_SIZE >  SKC_PLACE_SUBGROUP_SIZE ) // same as above when ratio equals 1

#define SKC_PLACE_SUBGROUP_RATIO           (SKC_PREFIX_SUBGROUP_SIZE / SKC_PLACE_SUBGROUP_SIZE)
#define SKC_PLACE_SUBGROUP_RATIO_MASK      (SKC_PLACE_SUBGROUP_RATIO - 1)
#define SKC_PLACE_SUBGROUP_RATIO_SCALE(I)  ((I / SKC_PLACE_SUBGROUP_RATIO) * 2 * SKC_PLACE_SUBGROUP_RATIO + (I & SKC_PLACE_SUBGROUP_RATIO_MASK))

#define SKC_PLACE_STRIDE_H(L)              (L)
#define SKC_PLACE_STRIDE_V_LO(I)           (SKC_PLACE_SUBGROUP_RATIO_SCALE(I) * SKC_PLACE_SUBGROUP_SIZE)
#define SKC_PLACE_STRIDE_V_HI(I)           (SKC_PLACE_STRIDE_V_LO(I) + SKC_PLACE_SUBGROUP_RATIO * SKC_PLACE_SUBGROUP_SIZE)

#elif  ( SKC_PREFIX_SUBGROUP_SIZE <  SKC_PLACE_SUBGROUP_SIZE ) // same as above when ratio equals 1

#define SKC_PLACE_SUBGROUP_RATIO           (SKC_PLACE_SUBGROUP_SIZE / SKC_PREFIX_SUBGROUP_SIZE)
#define SKC_PLACE_SUBGROUP_RATIO_MASK      (SKC_PLACE_SUBGROUP_SIZE / SKC_PLACE_SUBGROUP_RATIO - 1) // equal to prefix subgroup mask

#define SKC_PLACE_STRIDE_H(L)              (((L) & ~SKC_PLACE_SUBGROUP_RATIO_MASK) * 2 + ((L) & SKC_PLACE_SUBGROUP_RATIO_MASK))
#define SKC_PLACE_STRIDE_V_LO(I)           (I * 2 * SKC_PLACE_SUBGROUP_SIZE)
#define SKC_PLACE_STRIDE_V_HI(I)           (SKC_PLACE_STRIDE_V_LO(I) + SKC_PLACE_SUBGROUP_SIZE / SKC_PLACE_SUBGROUP_RATIO)

#endif

//
// A COARSE COMPILE-TIME GUARD -- WILL ONLY MATTER WHEN SUBGROUP SIZE
// IS EQUAL TO THE RASTER HEADER SIZE (CURRENTLY 8)
//

#define SKC_PLACE_IS_ALL_HEADER_ROW(i)   (((i)+1) * SKC_PLACE_SUBGROUP_SIZE <= SKC_RASTER_HEAD_DWORDS)

#define SKC_PLACE_IS_NOT_HEADER_ROW(i)   ( (i)    * SKC_PLACE_SUBGROUP_SIZE >= SKC_RASTER_HEAD_DWORDS)

#define SKC_PLACE_IS_TRAILING_ROW(i)     (((i)+1) * SKC_PLACE_SUBGROUP_SIZE == SKC_DEVICE_BLOCK_DWORDS)

#define SKC_PLACE_IS_HEADER_ROW_KEY(i)   ((i) * SKC_PLACE_SUBGROUP_SIZE + get_sub_group_local_id() - SKC_RASTER_HEAD_DWORDS < (k))


//
// Note: HEADER_LESS_THAN purposefully wraps unsigned integer to ~UINT_MAX
//
#define SKC_PLACE_HEADER_LESS_THAN(i,k) ((i) * SKC_PLACE_SUBGROUP_SIZE + get_sub_group_local_id() - SKC_RASTER_HEAD_DWORDS < (k))
#define SKC_PLACE_NODE_LESS_THAN(i,k)   ((i) * SKC_PLACE_SUBGROUP_SIZE + get_sub_group_local_id()                          < (k))

//
// TTSK v2:
//
//  0                                       63
//  | TTSB ID | PREFIX |  SPAN   |  X  |  Y  |
//  +---------+--------+---------+-----+-----+
//  |    27   | 1 (=0) | 12 (=0) | 12  | 12  |
//
//
// TTPK v2:
//
//  0                                    63
//  | TTPB ID | PREFIX | SPAN |  X  |  Y  |
//  +---------+--------+------+-----+-----+
//  |    27   | 1 (=1) |  12  | 12  | 12  |
//
//

//
// TTCK (32-BIT COMPARE) v1:
//
//  0                                                           63
//  | PAYLOAD/TTSB/TTPB ID | PREFIX | ESCAPE | LAYER |  X  |  Y  |
//  +----------------------+--------+--------+-------+-----+-----+
//  |          30          |    1   |    1   |   18  |  7  |  7  |
//
//
// TTCK (32-BIT COMPARE) v2:
//
//  0                                                           63
//  | PAYLOAD/TTSB/TTPB ID | PREFIX | ESCAPE | LAYER |  X  |  Y  |
//  +----------------------+--------+--------+-------+-----+-----+
//  |          30          |    1   |    1   |   15  |  9  |  8  |
//
//
// TTCK (64-BIT COMPARE) -- achieves 4K x 4K with an 8x16 tile:
//
//  0                                                           63
//  | PAYLOAD/TTSB/TTPB ID | PREFIX | ESCAPE | LAYER |  X  |  Y  |
//  +----------------------+--------+--------+-------+-----+-----+
//  |          27          |    1   |    1   |   18  |  9  |  8  |
//

union skc_subgroup_smem
{
  skc_uint scratch[SKC_PLACE_SUBGROUP_SIZE]; // will only use SKC_PLACE_SUBGROUP_SIZE

  struct {
    struct {
      skc_ttsk_lo_t sk[SKC_PLACE_SMEM_COUNT_TTSK];
      skc_ttpk_lo_t pk[SKC_PLACE_SMEM_COUNT_TTPK];
    } lo;

    struct {
      skc_ttsk_hi_t sk[SKC_PLACE_SMEM_COUNT_TTSK];
      skc_ttpk_hi_t pk[SKC_PLACE_SMEM_COUNT_TTPK];
    } hi;

    // skc_uint span[SKC_PLACE_SMEM_COUNT_TTPK];
  };

};

//
// scatter scan max
//
static
skc_int_v_t
skc_scatter_scan_max(__local union skc_subgroup_smem  volatile * const smem,
                     skc_int_v_t                                 const iss,
                     skc_int_v_t                                 const ess)
{
  //
  // prefix sums determine which lanes we're going to work on next
  //
  skc_pred_v_t const is_scratch_store = (iss > 0) && (ess < SKC_PLACE_SUBGROUP_SIZE);
  skc_int_v_t  const scratch_idx      = max(ess,0);

  //
  // SIMT
  //

  //
  // zero the volatile smem scratchpad using vector syntax
  //
  smem->scratch[get_sub_group_local_id()] = ( 0 );

  //
  // store source lane at starting lane
  //
  if (is_scratch_store) {
    smem->scratch[scratch_idx] = get_sub_group_local_id();
  }

  //
  // propagate lanes to right using max scan
  //
  skc_int_v_t const scratch = smem->scratch[get_sub_group_local_id()];
  skc_int_v_t const source  = sub_group_scan_inclusive_max(scratch);

  return source;
}

//
//
//

static
skc_bool
skc_xk_clip(union skc_tile_clip const * const tile_clip,
            skc_ttxk_t                * const xk)
{
  //
  // clip the sk and pk keys
  //
  // if fully clipped then return false
  //
  // alternatively -- we can expand all these keys in place
  //
  // alternatively -- keep sk and pk keys segregated because sk
  // represents the vast majority of keys and are easier to process.
  // don't mess with the fastpath!
  //
  return false;
}

//
//
//

static
skc_ttck_t
skc_sk_to_ck(__local union skc_subgroup_smem  volatile * const smem,
             union skc_cmd_place              const    * const cmd,
             skc_uint                                    const sk_idx)
{
  skc_uint const lo = smem->lo.sk[sk_idx]; // assumes prefix bit is 0
  skc_uint const hi = smem->hi.sk[sk_idx];

  skc_ttck_t ck;

  ck.lo = lo | (cmd->layer_id << SKC_TTCK_LO_BITS_ID_PREFIX_ESCAPE); // FIXME -- preshift the layer id

  // FIXME -- x and y should already be clipped and shifted
  skc_uint const x = (cmd->tx + SKC_BFE(hi,SKC_TTXK_HI_BITS_X,SKC_TTXK_HI_OFFSET_X)) << SKC_TTCK_HI_OFFSET_X;
  skc_uint const y = (cmd->ty + SKC_BFE(hi,SKC_TTXK_HI_BITS_Y,SKC_TTXK_HI_OFFSET_Y)) << SKC_TTCK_HI_OFFSET_Y;

  ck.hi = (cmd->layer_id >> SKC_TTCK_HI_SHR_LAYER) | x | y;

  return ck;
}

static
skc_ttck_t
skc_pk_to_ck(__local union skc_subgroup_smem  volatile * const smem,
             union skc_cmd_place              const    * const cmd,
             skc_uint                                    const pk_idx,
             skc_uint                                    const dx)
{
  skc_uint const lo = smem->lo.pk[pk_idx] & SKC_TTXK_LO_MASK_ID_PREFIX; // assumes prefix bit is 1
  skc_uint const hi = smem->hi.pk[pk_idx];

  skc_ttck_t ck;

  ck.lo = lo | (cmd->layer_id << SKC_TTCK_LO_BITS_ID_PREFIX_ESCAPE); // FIXME -- preshift the layer id

  // FIXME -- x and y should already be clipped and shifted
  skc_uint const x = (cmd->tx + dx + SKC_BFE(hi,SKC_TTXK_HI_BITS_X,SKC_TTXK_HI_OFFSET_X)) << SKC_TTCK_HI_OFFSET_X;
  skc_uint const y = (cmd->ty +      SKC_BFE(hi,SKC_TTXK_HI_BITS_Y,SKC_TTXK_HI_OFFSET_Y)) << SKC_TTCK_HI_OFFSET_Y;

  ck.hi = (cmd->layer_id >> SKC_TTCK_HI_SHR_LAYER) | x | y;

  return ck;
}

//
//
//

static
void
skc_ttsk_flush(__global SKC_ATOMIC_UINT         volatile * const place_atomics,
               __global skc_ttck_t                       * const ck_extent,
               __local union skc_subgroup_smem  volatile * const smem,
               union skc_cmd_place              const    * const cmd,
               skc_uint                         const            sk)
{
  //
  // Pretty sure you can never ever have an sk count equal to 0
  //
  skc_uint ck_base = 0;

  // last lane performs the block pool allocation with an atomic increment
  if (get_sub_group_local_id() == 0) {
    ck_base = SKC_ATOMIC_ADD_GLOBAL_RELAXED_DEVICE(place_atomics,sk);
  }

  // broadcast base to all lanes
  ck_base = sub_group_broadcast(ck_base,0);

  // convert sk keys to ck keys
  for (skc_uint ii=get_sub_group_local_id(); ii<sk; ii+=SKC_PLACE_SUBGROUP_SIZE)
    {
      ck_extent[ck_base+ii] = skc_sk_to_ck(smem,cmd,ii);
    }
}

//
//
//

static
skc_int
skc_ttpk_get_span(__local union skc_subgroup_smem  volatile * const smem,
                  skc_uint                                    const idx)
{
  skc_uint const lo      = smem->lo.pk[idx];
  skc_uint const hi      = smem->hi.pk[idx];

  skc_uint const span_lo = lo >> SKC_TTXK_LO_OFFSET_SPAN;
  skc_uint const span_hi = (hi & SKC_BITS_TO_MASK(SKC_TTXK_HI_BITS_SPAN)) << SKC_TTXK_LO_BITS_SPAN;

  return (span_lo | span_hi) + 1;
}

//
//
//

static
void
skc_ttpk_flush(__global SKC_ATOMIC_UINT         volatile * const place_atomics,
               __global skc_ttck_t                       * const ck_extent,
               __local union skc_subgroup_smem  volatile * const smem,
               union skc_cmd_place              const    * const cmd,
               skc_uint                         const            pk)
{
  // bail out if pk queue is empty
  if (pk == 0)
    return;

#if 0
  if (get_sub_group_local_id() == 0)
    printf("%u\n",pk);
#endif

  //
  // FIXME -- this nested loop iterates over the queue processing a
  // subgroup of 64-bit keys at a time.  This is probably not the most
  // efficient approach so investigate how to store and iterate over a
  // wider than subgroup (node-sized) queue of keys.
  //

  // round up so we work with full subgroups
  skc_uint const pk_ru = (pk + SKC_PLACE_SUBGROUP_SIZE - 1) & ~SKC_PLACE_SUBGROUP_MASK;
  skc_uint       ii    = 0;

  // nested loop that expands all ttpk keys
#if (SKC_PLACE_SMEM_COUNT_TTPK > SKC_PLACE_SUBGROUP_SIZE)
  for (; ii<pk_ru; ii+=SKC_PLACE_SUBGROUP_SIZE)
#endif
    {
      skc_uint idx  = ii + get_sub_group_local_id();
      skc_int  span = 0;

      // how many tiles does this ttpk span?
      if (idx < pk)
        span = skc_ttpk_get_span(smem,idx);

      // we need inclusive, exclusive and total
      skc_int iss = sub_group_scan_inclusive_add(span);
      skc_int ess = iss - span;
      skc_int rem = sub_group_broadcast(iss,SKC_PLACE_SUBGROUP_SIZE-1);

      // printf("%u : %u\n",span,iss);
      // continue;

      // atomically allocate space for the pk keys
      skc_uint ck_base = 0;

      // last lane performs the block pool allocation with an atomic increment
      if (get_sub_group_local_id() == 0) {
        ck_base = SKC_ATOMIC_ADD_GLOBAL_RELAXED_DEVICE(place_atomics,rem);
      }

      // broadcast atomically allocated extent base to all lanes
      skc_uint ck_idx = sub_group_broadcast(ck_base,0) + get_sub_group_local_id();

      //
      // FIXME -- this loop would probably be faster if the ttpk keys
      // were held in registers and accessed with shuffles instead of
      // SMEM loads
      //

      //
      // loop until there are no more expanded pk keys
      //
      while (true)
        {
          skc_int const source = skc_scatter_scan_max(smem,iss,ess);
          skc_int const dx     = get_sub_group_local_id() - intel_sub_group_shuffle(ess,source);

          // store valid ck keys to gmem
          if (get_sub_group_local_id() < rem) {
            ck_extent[ck_idx] = skc_pk_to_ck(smem,cmd,ii+source,dx);
          }

          // decrement remainder
          rem -= SKC_PLACE_SUBGROUP_SIZE;

          if (rem <= 0)
            break;

          // increment/decrement indices
          ck_idx += SKC_PLACE_SUBGROUP_SIZE;
          iss    -= SKC_PLACE_SUBGROUP_SIZE;
          ess    -= SKC_PLACE_SUBGROUP_SIZE;
        }
    }
}

//
//
//

static
skc_uint
skc_ballot(skc_uint * const xk, skc_uint const is_xk)
{
#if 0
  //
  // FIXME -- when available, this should use the idiom:
  //
  //   ballot() + lane_mask_less_than_or_equal + popcount()
  //
  // Supported by:
  //
  //   - Vulkan 1.1 / SPIR-V 1.3
  //   - CUDA
  //   - AVX2 (SSE*?)
  //
#else
  //
  // otherwise, emulate with an inclusive scan (yuk)
  //
  skc_uint const prefix = sub_group_scan_inclusive_add(is_xk);

  skc_uint const xk_idx = *xk + prefix - is_xk;

  *xk += sub_group_broadcast(prefix,SKC_PLACE_SUBGROUP_LAST);

#if 0
  printf("< %3u >\n",xk_idx);
#endif

  return xk_idx;
#endif
}

//
//
//
__kernel
SKC_PLACE_KERNEL_ATTRIBS
void
skc_kernel_place(__global skc_bp_elem_t                * const bp_elems,
                 __global SKC_ATOMIC_UINT     volatile * const place_atomics,
                 __global skc_ttck_t                   * const ck_extent,
                 __global union skc_cmd_place const    * const cmds,
                 __global skc_block_id_t               * const map,
                 skc_uint4                               const clip,
                 skc_uint                                const count)
{
  //
  // declare shared memory block
  //
#if ( SKC_PLACE_WORKGROUP_SUBGROUPS == 1 )
  __local union skc_subgroup_smem  volatile                smem[1];
#else
  __local union skc_subgroup_smem  volatile                smem_wg[SKC_PLACE_WORKGROUP_SUBGROUPS];
  __local union skc_subgroup_smem  volatile * const smem = smem_wg + get_sub_group_id();
#endif

  //
  // This is a subgroup-centric kernel
  //
  // Which subgroup in the grid is this?
  //
  // TAKE NOTE: the Intel GEN compiler appears to be recognizing
  // get_group_id(0) as a uniform but the alternative calculation used
  // when there are multiple subgroups per workgroup is not
  // cooperating and driving spillage elsewhere.
  //
  // Test the raster's translated bounds against the composition's
  // tile clip
  //
  // There are 3 cases:
  //
  //   - the raster is completely clipped -> return
  //   - the raster is partially  clipped -> all keys must clipped
  //   - the raster is not        clipped -> no keys are tested
  //
  //
  // There are at least 4 implementations of place and we want to
  // special-case them as much as possible so that, at the least, the
  // fastpath remains fast.
  //
  //  - implement NO CLIP + NO TRANSLATION fastpath -- CAN ATOMICALLY ALLOCATE SK+PK KEYS IN ONE STEP
  //
  //  - implement CLIPPED + NO TRANSLATION path
  //
  //  - implement NO CLIP +    TRANSLATION path
  //
  //  - implement CLIPPED +    TRANSLATION path
  //
  //
  // FIXME/OPTIMIZATION: split scan accumulator into a triple-bin
  // 12:12:8 integer where:
  //
  //  12: ttsk
  //  12: ttpk
  //   8: /dev/null -- clipped or invalid key
  //
  // Three kinds of nodes in a raster's list:
  //
  //  - the head node
  //  - an internal node
  //  - the final node
  //

#if ( SKC_PLACE_WORKGROUP_SUBGROUPS == 1 )
  skc_uint const cmd_idx = get_group_id(0);
#else
  skc_uint const cmd_idx = get_group_id(0) * SKC_PLACE_WORKGROUP_SUBGROUPS + get_sub_group_id();
#endif

  // load command
  union skc_cmd_place const cmd = cmds[cmd_idx];

  // get the raster header from the raster host id -- scalar
  skc_block_id_t            id  = map[cmd.raster_h];

  //
  // load all of the head block ttxk keys into registers
  //
  // FIXME -- this pattern lends itself to using the higher
  // performance Intel GEN block load instructions
  //
  skc_uint const head_id = id * SKC_DEVICE_SUBBLOCK_WORDS + SKC_PLACE_STRIDE_H(get_sub_group_local_id());

#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R)                                 \
  union skc_raster_node_elem const h##I = {                     \
    .u32v2 = { bp_elems[head_id + SKC_PLACE_STRIDE_V_LO(I)],    \
               bp_elems[head_id + SKC_PLACE_STRIDE_V_HI(I)]  }  \
  };

  SKC_PLACE_EXPAND();

  //
  // load raster header counts -- we only need the "nodes" and "keys"
  // words but the keys we loaded are doublewords.
  //
  // FIXME -- this can be made portable with compile-time macro expansion
  //
  skc_uint nodes = sub_group_broadcast(h0.u32v2.lo,1); // SKC_RASTER_HEAD_OFFSET_COUNTS_NODES
  skc_uint keys  = sub_group_broadcast(h0.u32v2.hi,1); // SKC_RASTER_HEAD_OFFSET_COUNTS_KEYS

  //
  //
  //
#if 0
#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R)                                 \
  printf("%5u :  %6u : %3u : %08X . %08X - %08X\n",             \
         nodes,keys,                                            \
         I*SKC_PLACE_SUBGROUP_SIZE + get_sub_group_local_id(),  \
         h##I.u32v2.hi,h##I.u32v2.lo,                           \
         h##I.u32v2.lo & SKC_TTXK_LO_MASK_PREFIX);

  SKC_PLACE_EXPAND();
#endif

  //
#if 0
  if (get_sub_group_local_id() == 0) {
    printf("place: %u / %u / %u\n",head_id,nodes,keys);
  }
#endif

  {
    //
    // classify every key in the header
    //
    // keys: 0 is not a key / 1 is a key
    // skpk: 0 is sk        / 1 is pk
    //
    skc_uint bits_keys = 0;
    skc_uint bits_skpk = 0;

    //
    // calculate bits_keys
    //
#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R)                                         \
    if (!SKC_PLACE_IS_ALL_HEADER_ROW(I)) {                              \
      skc_uint const idx = I * SKC_PLACE_SUBGROUP_SIZE + get_sub_group_local_id() - SKC_RASTER_HEAD_DWORDS; \
      if (idx < keys) {                                                 \
        bits_keys |= (1u << I);                                         \
      }                                                                 \
      if (SKC_PLACE_IS_TRAILING_ROW(I)) {                               \
        if (keys > SKC_RASTER_HEAD_COUNT_KEYS) {                        \
          if (get_sub_group_local_id() == SKC_PLACE_SUBGROUP_LAST) {    \
            bits_keys &= ~(1u << I);                                    \
          }                                                             \
        }                                                               \
      }                                                                 \
    }

    SKC_PLACE_EXPAND();

    //
    // blindly calculate bits_skpk
    //
#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R)                                         \
    if (!SKC_PLACE_IS_ALL_HEADER_ROW(I)) {                              \
      bits_skpk |= (h##I.xk.lo & SKC_TTXK_LO_MASK_PREFIX) >> (SKC_TTXK_LO_OFFSET_PREFIX - I); \
    }

    SKC_PLACE_EXPAND();

#if 0
    printf("%2X : %2X\n",bits_keys,bits_skpk);
#endif

    //
    // next pointer is last element of last row.  save it now because
    // this might be recognized as a subgroup-uniform/scalar.
    //
    id = sub_group_broadcast(SKC_CONCAT(h,SKC_PLACE_EXPAND_I_LAST).next.node,SKC_PLACE_SUBGROUP_LAST);

    //
    // append SK keys first
    //
    skc_uint const bits_sk = bits_keys & ~bits_skpk;
    skc_uint       sk      = 0;

#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R)                 \
    if (!SKC_PLACE_IS_ALL_HEADER_ROW(I)) {      \
      skc_uint is_sk  = (bits_sk >> I) & 1;     \
      skc_uint sk_idx = skc_ballot(&sk,is_sk);  \
      if (is_sk) {                              \
        smem->lo.sk[sk_idx] = h##I.xk.lo;       \
        smem->hi.sk[sk_idx] = h##I.xk.hi;       \
      }                                         \
    }

    SKC_PLACE_EXPAND();

    //
    // append PK keys next
    //
    skc_uint const bits_pk = bits_keys & bits_skpk;
    skc_uint       pk      = 0;

#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R)                 \
    if (!SKC_PLACE_IS_ALL_HEADER_ROW(I)) {      \
      skc_uint is_pk  = (bits_pk >> I) & 1;     \
      skc_uint pk_idx = skc_ballot(&pk,is_pk);  \
      if (is_pk) {                              \
        smem->lo.pk[pk_idx] = h##I.xk.lo;       \
        smem->hi.pk[pk_idx] = h##I.xk.hi;       \
      }                                         \
    }

    SKC_PLACE_EXPAND();

#if 0
    printf("%2u * %2u\n",sk,pk);
#endif
    //
    // flush the keys
    //
    skc_ttsk_flush(place_atomics,ck_extent,smem,&cmd,sk);
    skc_ttpk_flush(place_atomics,ck_extent,smem,&cmd,pk);
  }

  //
  // we're done if there was only a head node
  //
  if (nodes == 0)
    return;

  //
  // decrement keys
  //
  keys -= SKC_RASTER_HEAD_COUNT_KEYS;

  //
  // otherwise, append keys in trailing nodes to smem
  //
  while (true)
    {
      //
      // load all of the node block ttxk keys into registers
      //
      // FIXME -- this pattern lends itself to using the higher
      // performance Intel GEN block load instructions
      //
      skc_uint const node_id = id * SKC_DEVICE_SUBBLOCK_WORDS + SKC_PLACE_STRIDE_H(get_sub_group_local_id());

#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R)                                         \
      union skc_raster_node_elem const n##I = {                         \
        .u32v2 = { bp_elems[node_id + SKC_PLACE_STRIDE_V_LO(I)],        \
                   bp_elems[node_id + SKC_PLACE_STRIDE_V_HI(I)]  }      \
      };

      SKC_PLACE_EXPAND();

#if 0
#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R)                                         \
      printf("%5u :  %6u : %3u : %08X . %08X - %08X\n",                 \
             nodes,keys,                                                \
             I*SKC_PLACE_SUBGROUP_SIZE + get_sub_group_local_id(),      \
             n##I.u32v2.hi,n##I.u32v2.lo,                               \
             n##I.u32v2.lo & SKC_TTXK_LO_MASK_PREFIX);

      SKC_PLACE_EXPAND();
#endif

      //
      // classify every key in the header
      //
      // keys: 0 is not a key / 1 is a key
      // skpk: 0 is sk        / 1 is pk
      //
      skc_uint bits_keys = 0;
      skc_uint bits_skpk = 0;

      //
      // calculate bits_keys
      //
#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R) {                                       \
        skc_uint const idx = I * SKC_PLACE_SUBGROUP_SIZE + get_sub_group_local_id(); \
        if (idx < keys) {                                               \
          bits_keys |= (1u << I);                                       \
        }                                                               \
        if (SKC_PLACE_IS_TRAILING_ROW(I)) {                             \
          if (keys > SKC_RASTER_NODE_COUNT_KEYS) {                      \
            if (get_sub_group_local_id() == SKC_PLACE_SUBGROUP_LAST) {  \
              bits_keys &= ~(1u << I);                                  \
            }                                                           \
          }                                                             \
        }                                                               \
      }

      SKC_PLACE_EXPAND();

      //
      // blindly calculate bits_skpk
      //
#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R) {                                       \
        bits_skpk |= (n##I.xk.lo & SKC_TTXK_LO_MASK_PREFIX) >> (SKC_TTXK_LO_OFFSET_PREFIX - I); \
      }

      SKC_PLACE_EXPAND();

#if 0
      printf("%2X : %2X\n",bits_keys,bits_skpk);
#endif

      //
      // next pointer is last element of last row.  save it now because
      // this might be recognized as a subgroup-uniform/scalar.
      //
      id = sub_group_broadcast(SKC_CONCAT(n,SKC_PLACE_EXPAND_I_LAST).next.node,SKC_PLACE_SUBGROUP_LAST);

      //
      // append SK keys first
      //
      skc_uint const bits_sk = bits_keys & ~bits_skpk;
      skc_uint       sk      = 0;

#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R) {                       \
        skc_uint is_sk  = (bits_sk >> I) & 1;           \
        skc_uint sk_idx = skc_ballot(&sk,is_sk);        \
        if (is_sk) {                                    \
          smem->lo.sk[sk_idx] = n##I.xk.lo;             \
          smem->hi.sk[sk_idx] = n##I.xk.hi;             \
        }                                               \
      }

      SKC_PLACE_EXPAND();

      //
      // append PK keys next
      //
      skc_uint const bits_pk = bits_keys & bits_skpk;
      skc_uint       pk      = 0;

#undef  SKC_EXPAND_X
#define SKC_EXPAND_X(I,S,C,P,R) {                       \
        skc_uint is_pk  = (bits_pk >> I) & 1;           \
        skc_uint pk_idx = skc_ballot(&pk,is_pk);        \
        if (is_pk) {                                    \
          smem->lo.pk[pk_idx] = n##I.xk.lo;             \
          smem->hi.pk[pk_idx] = n##I.xk.hi;             \
        }                                               \
      }

      SKC_PLACE_EXPAND();

#if 0
    printf("%2u * %2u\n",sk,pk);
#endif
      //
      // if total for either the sk or pk queue reaches the
      // highwater mark then flush it to the extent
      //
      skc_ttsk_flush(place_atomics,ck_extent,smem,&cmd,sk);
      skc_ttpk_flush(place_atomics,ck_extent,smem,&cmd,pk);

      //
      // if this was the last node then we're done
      //
      if (--nodes == 0)
        return;

      //
      // otherwise decrement keys
      //
      keys -= SKC_RASTER_NODE_COUNT_KEYS;
    }
}

//
//
//