summaryrefslogtreecommitdiffstats
path: root/third_party/rust/gfx-backend-dx11/shaders/copy.hlsl
blob: f8b5d8523b9f7f6dc8a7447131e68f0982adfe74 (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
struct BufferCopy {
    uint4 SrcDst;
};

struct ImageCopy {
    uint4 Src;
    uint4 Dst;
};

struct BufferImageCopy {
    // x=offset, yz=size
    uint4 BufferVars;
    uint4 ImageOffset;
    uint4 ImageExtent;
    uint4 ImageSize;
};

cbuffer CopyConstants : register(b0) {
    BufferCopy BufferCopies;
    ImageCopy ImageCopies;
    BufferImageCopy BufferImageCopies;
};


uint3 GetDestBounds()
{
    return min(
        BufferImageCopies.ImageOffset + BufferImageCopies.ImageExtent,
        BufferImageCopies.ImageSize
    );
}

uint3 GetImageCopyDst(uint3 dispatch_thread_id)
{
    return uint3(ImageCopies.Dst.xy + dispatch_thread_id.xy, ImageCopies.Dst.z);
}

uint3 GetImageCopySrc(uint3 dispatch_thread_id)
{
    return uint3(ImageCopies.Src.xy + dispatch_thread_id.xy, ImageCopies.Src.z);
}

uint3 GetImageDst(uint3 dispatch_thread_id)
{
    return uint3(BufferImageCopies.ImageOffset.xy + dispatch_thread_id.xy, BufferImageCopies.ImageOffset.z);
}

uint3 GetImageSrc(uint3 dispatch_thread_id)
{
    return uint3(BufferImageCopies.ImageOffset.xy + dispatch_thread_id.xy, BufferImageCopies.ImageOffset.z);
}

uint GetBufferDst128(uint3 dispatch_thread_id)
{
    return BufferImageCopies.BufferVars.x + dispatch_thread_id.x * 16 + dispatch_thread_id.y * 16 * max(BufferImageCopies.BufferVars.y, BufferImageCopies.ImageExtent.x);
}
uint GetBufferSrc128(uint3 dispatch_thread_id)
{
    return BufferImageCopies.BufferVars.x + dispatch_thread_id.x * 16 + dispatch_thread_id.y * 16 * max(BufferImageCopies.BufferVars.y, BufferImageCopies.ImageExtent.x);
}

uint GetBufferDst64(uint3 dispatch_thread_id)
{
    return BufferImageCopies.BufferVars.x + dispatch_thread_id.x * 8 + dispatch_thread_id.y * 8 * max(BufferImageCopies.BufferVars.y, BufferImageCopies.ImageExtent.x);
}
uint GetBufferSrc64(uint3 dispatch_thread_id)
{
    return BufferImageCopies.BufferVars.x + dispatch_thread_id.x * 8 + dispatch_thread_id.y * 8 * max(BufferImageCopies.BufferVars.y, BufferImageCopies.ImageExtent.x);
}

uint GetBufferDst32(uint3 dispatch_thread_id)
{
    return BufferImageCopies.BufferVars.x + dispatch_thread_id.x * 4 + dispatch_thread_id.y * 4 * max(BufferImageCopies.BufferVars.y, BufferImageCopies.ImageExtent.x);
}
uint GetBufferSrc32(uint3 dispatch_thread_id)
{
    return BufferImageCopies.BufferVars.x + dispatch_thread_id.x * 4 + dispatch_thread_id.y * 4 * max(BufferImageCopies.BufferVars.y, BufferImageCopies.ImageExtent.x);
}

uint GetBufferDst16(uint3 dispatch_thread_id)
{
    return BufferImageCopies.BufferVars.x + dispatch_thread_id.x * 4 + dispatch_thread_id.y * 2 * max(BufferImageCopies.BufferVars.y, BufferImageCopies.ImageExtent.x);
}
uint GetBufferSrc16(uint3 dispatch_thread_id)
{
    return BufferImageCopies.BufferVars.x + dispatch_thread_id.x * 4 + dispatch_thread_id.y * 2 * max(BufferImageCopies.BufferVars.y, BufferImageCopies.ImageExtent.x);
}

uint GetBufferDst8(uint3 dispatch_thread_id)
{
    return BufferImageCopies.BufferVars.x + dispatch_thread_id.x * 4 + dispatch_thread_id.y * max(BufferImageCopies.BufferVars.y, BufferImageCopies.ImageExtent.x);
}
uint GetBufferSrc8(uint3 dispatch_thread_id)
{
    return BufferImageCopies.BufferVars.x + dispatch_thread_id.x * 4 + dispatch_thread_id.y * max(BufferImageCopies.BufferVars.y, BufferImageCopies.ImageExtent.x);
}


uint4 Uint32ToUint8x4(uint data)
{
    return (data >> uint4(0, 8, 16, 24)) & 0xFF;
}

uint2 Uint32ToUint16x2(uint data)
{
    return (data >> uint2(0, 16)) & 0xFFFF;
}

uint Uint8x4ToUint32(uint4 data)
{
    return dot(min(data, 0xFF), 1 << uint4(0, 8, 16, 24));
}

uint Uint16x2ToUint32(uint2 data)
{
    return dot(min(data, 0xFFFF), 1 << uint2(0, 16));
}

uint2 Uint16ToUint8x2(uint data)
{
    return (data >> uint2(0, 8)) & 0xFF;
}

uint Uint8x2ToUint16(uint2 data)
{
    return dot(min(data, 0xFF), 1 << uint2(0, 8));
}

uint4 Float4ToUint8x4(float4 data)
{
    return uint4(data * 255 + .5f);
}

// Buffers are always R32-aligned
ByteAddressBuffer   BufferCopySrc : register(t0);
RWByteAddressBuffer BufferCopyDst : register(u0);

RWTexture1DArray<uint>  Image1CopyDstR    : register(u0);
RWTexture1DArray<uint2>  Image1CopyDstRg   : register(u0);
RWTexture1DArray<uint4>  Image1CopyDstRgba : register(u0);

Texture2DArray<uint4>   Image2CopySrc     : register(t0);
RWTexture2DArray<uint>  Image2CopyDstR    : register(u0);
RWTexture2DArray<uint2> Image2CopyDstRg   : register(u0);
RWTexture2DArray<uint4> Image2CopyDstRgba : register(u0);

Texture2DArray<float4>  ImageCopy2SrcBgra : register(t0);

// Image<->Image copies
[numthreads(1, 1, 1)]
void cs_copy_image2d_r8g8_image2d_r16(uint3 dispatch_thread_id : SV_DispatchThreadID)
{
    uint3 dst_idx = GetImageCopyDst(dispatch_thread_id);
    uint3 src_idx = GetImageCopySrc(dispatch_thread_id);

    Image2CopyDstR[dst_idx] = Uint8x2ToUint16(Image2CopySrc[src_idx]);
}

[numthreads(1, 1, 1)]
void cs_copy_image2d_r16_image2d_r8g8(uint3 dispatch_thread_id : SV_DispatchThreadID)
{
    uint3 dst_idx = GetImageCopyDst(dispatch_thread_id);
    uint3 src_idx = GetImageCopySrc(dispatch_thread_id);

    Image2CopyDstRg[dst_idx] = Uint16ToUint8x2(Image2CopySrc[src_idx]);
}

[numthreads(1, 1, 1)]
void cs_copy_image2d_r8g8b8a8_image2d_r32(uint3 dispatch_thread_id : SV_DispatchThreadID)
{
    uint3 dst_idx = GetImageCopyDst(dispatch_thread_id);
    uint3 src_idx = GetImageCopySrc(dispatch_thread_id);

    Image2CopyDstR[dst_idx] = Uint8x4ToUint32(Image2CopySrc[src_idx]);
}

[numthreads(1, 1, 1)]
void cs_copy_image2d_r8g8b8a8_image2d_r16g16(uint3 dispatch_thread_id : SV_DispatchThreadID)
{
    uint3 dst_idx = GetImageCopyDst(dispatch_thread_id);
    uint3 src_idx = GetImageCopySrc(dispatch_thread_id);

    Image2CopyDstRg[dst_idx] = Uint32ToUint16x2(Uint8x4ToUint32(Image2CopySrc[src_idx]));
}

[numthreads(1, 1, 1)]
void cs_copy_image2d_r16g16_image2d_r32(uint3 dispatch_thread_id : SV_DispatchThreadID)
{
    uint3 dst_idx = GetImageCopyDst(dispatch_thread_id);
    uint3 src_idx = GetImageCopySrc(dispatch_thread_id);

    Image2CopyDstR[dst_idx] = Uint16x2ToUint32(Image2CopySrc[src_idx]);
}

[numthreads(1, 1, 1)]
void cs_copy_image2d_r16g16_image2d_r8g8b8a8(uint3 dispatch_thread_id : SV_DispatchThreadID)
{
    uint3 dst_idx = GetImageCopyDst(dispatch_thread_id);
    uint3 src_idx = GetImageCopySrc(dispatch_thread_id);

    Image2CopyDstRgba[dst_idx] = Uint32ToUint8x4(Uint16x2ToUint32(Image2CopySrc[src_idx]));
}

[numthreads(1, 1, 1)]
void cs_copy_image2d_r32_image2d_r16g16(uint3 dispatch_thread_id : SV_DispatchThreadID)
{
    uint3 dst_idx = GetImageCopyDst(dispatch_thread_id);
    uint3 src_idx = GetImageCopySrc(dispatch_thread_id);

    Image2CopyDstRg[dst_idx] = Uint32ToUint16x2(Image2CopySrc[src_idx]);
}

[numthreads(1, 1, 1)]
void cs_copy_image2d_r32_image2d_r8g8b8a8(uint3 dispatch_thread_id : SV_DispatchThreadID)
{
    uint3 dst_idx = GetImageCopyDst(dispatch_thread_id);
    uint3 src_idx = GetImageCopySrc(dispatch_thread_id);

    Image2CopyDstRgba[dst_idx] = Uint32ToUint8x4(Image2CopySrc[src_idx]);
}

//#define COPY_1D_NUM_THREAD 64 //TODO
#define COPY_2D_NUM_THREAD_X 8
#define COPY_2D_NUM_THREAD_Y 8

// Buffer<->Image copies

// R32G32B32A32
[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_buffer_image2d_r32g32b32a32(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x || dst_idx.y >= bounds.y) {
        return;
    }

    uint src_idx = GetBufferSrc128(dispatch_thread_id);

    Image2CopyDstRgba[dst_idx] = uint4(
        BufferCopySrc.Load(src_idx),
        BufferCopySrc.Load(src_idx + 1 * 4),
        BufferCopySrc.Load(src_idx + 2 * 4),
        BufferCopySrc.Load(src_idx + 3 * 4)
    );
}

[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_image2d_r32g32b32a32_buffer(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 src_idx = GetImageSrc(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (src_idx.x >= bounds.x || src_idx.y >= bounds.y) {
        return;
    }

    uint4 data = Image2CopySrc[src_idx];
    uint dst_idx = GetBufferDst128(dispatch_thread_id);

    BufferCopyDst.Store(dst_idx,         data.x);
    BufferCopyDst.Store(dst_idx + 1 * 4, data.y);
    BufferCopyDst.Store(dst_idx + 2 * 4, data.z);
    BufferCopyDst.Store(dst_idx + 3 * 4, data.w);
}

// R32G32
[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_buffer_image2d_r32g32(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x || dst_idx.y >= bounds.y) {
        return;
    }

    uint src_idx = GetBufferSrc64(dispatch_thread_id);

    Image2CopyDstRg[dst_idx] = uint2(
        BufferCopySrc.Load(src_idx),
        BufferCopySrc.Load(src_idx + 1 * 4)
    );
}

[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_image2d_r32g32_buffer(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 src_idx = GetImageSrc(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (src_idx.x >= bounds.x || src_idx.y >= bounds.y) {
        return;
    }

    uint2 data = Image2CopySrc[src_idx].rg;
    uint dst_idx = GetBufferDst64(dispatch_thread_id);

    BufferCopyDst.Store(dst_idx        , data.x);
    BufferCopyDst.Store(dst_idx + 1 * 4, data.y);
}

// R16G16B16A16
[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_buffer_image2d_r16g16b16a16(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x || dst_idx.y >= bounds.y) {
        return;
    }

    uint src_idx = GetBufferSrc64(dispatch_thread_id);

    Image2CopyDstRgba[dst_idx] = uint4(
        Uint32ToUint16x2(BufferCopySrc.Load(src_idx)),
        Uint32ToUint16x2(BufferCopySrc.Load(src_idx + 1 * 4))
    );
}

[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_image2d_r16g16b16a16_buffer(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 src_idx = GetImageSrc(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (src_idx.x >= bounds.x || src_idx.y >= bounds.y) {
        return;
    }

    uint4 data = Image2CopySrc[src_idx];
    uint dst_idx = GetBufferDst64(dispatch_thread_id);

    BufferCopyDst.Store(dst_idx,         Uint16x2ToUint32(data.xy));
    BufferCopyDst.Store(dst_idx + 1 * 4, Uint16x2ToUint32(data.zw));
}

// R32
[numthreads(COPY_2D_NUM_THREAD_X, 1, 1)]
void cs_copy_buffer_image1d_r32(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x) {
        return;
    }

    uint src_idx = GetBufferSrc32(dispatch_thread_id);

    Image1CopyDstR[dst_idx.xz] = BufferCopySrc.Load(src_idx);
}
[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_buffer_image2d_r32(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x || dst_idx.y >= bounds.y) {
        return;
    }

    uint src_idx = GetBufferSrc32(dispatch_thread_id);

    Image2CopyDstR[dst_idx] = BufferCopySrc.Load(src_idx);
}

[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_image2d_r32_buffer(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 src_idx = GetImageSrc(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (src_idx.x >= bounds.x || src_idx.y >= bounds.y) {
        return;
    }

    uint dst_idx = GetBufferDst32(dispatch_thread_id);

    BufferCopyDst.Store(dst_idx, Image2CopySrc[src_idx].r);
}

// R16G16
[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_buffer_image2d_r16g16(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x || dst_idx.y >= bounds.y) {
        return;
    }

    uint src_idx = GetBufferSrc32(dispatch_thread_id);

    Image2CopyDstRg[dst_idx] = Uint32ToUint16x2(BufferCopySrc.Load(src_idx));
}

[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_image2d_r16g16_buffer(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 src_idx = GetImageSrc(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (src_idx.x >= bounds.x || src_idx.y >= bounds.y) {
        return;
    }

    uint dst_idx = GetBufferDst32(dispatch_thread_id);

    BufferCopyDst.Store(dst_idx, Uint16x2ToUint32(Image2CopySrc[src_idx].xy));
}

// R8G8B8A8
[numthreads(COPY_2D_NUM_THREAD_X, 1, 1)]
void cs_copy_buffer_image1d_r8g8b8a8(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x) {
        return;
    }

    uint src_idx = GetBufferSrc32(dispatch_thread_id);

    Image1CopyDstRgba[dst_idx.xz] = Uint32ToUint8x4(BufferCopySrc.Load(src_idx));
}
[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_buffer_image2d_r8g8b8a8(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x || dst_idx.y >= bounds.y) {
        return;
    }

    uint src_idx = GetBufferSrc32(dispatch_thread_id);

    Image2CopyDstRgba[dst_idx] = Uint32ToUint8x4(BufferCopySrc.Load(src_idx));
}

[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_image2d_r8g8b8a8_buffer(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 src_idx = GetImageSrc(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (src_idx.x >= bounds.x || src_idx.y >= bounds.y) {
        return;
    }

    uint dst_idx = GetBufferDst32(dispatch_thread_id);

    BufferCopyDst.Store(dst_idx, Uint8x4ToUint32(Image2CopySrc[src_idx]));
}

// B8G8R8A8
[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_image2d_b8g8r8a8_buffer(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 src_idx = GetImageSrc(dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (src_idx.x >= bounds.x || src_idx.y >= bounds.y) {
        return;
    }

    uint dst_idx = GetBufferDst32(dispatch_thread_id);

    BufferCopyDst.Store(dst_idx, Uint8x4ToUint32(Float4ToUint8x4(ImageCopy2SrcBgra[src_idx].bgra)));
}

// R16
[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_buffer_image2d_r16(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(uint3(2, 1, 0) * dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x || dst_idx.y >= bounds.y) {
        return;
    }

    uint src_idx = GetBufferSrc16(dispatch_thread_id);
    uint2 data = Uint32ToUint16x2(BufferCopySrc.Load(src_idx));

    uint remaining_x = bounds.x - dst_idx.x;

    if (remaining_x >= 2) {
        Image2CopyDstR[dst_idx + uint3(1, 0, 0)] = data.y;
    } 
    if (remaining_x >= 1) {
        Image2CopyDstR[dst_idx + uint3(0, 0, 0)] = data.x;
    }
}

[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_image2d_r16_buffer(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 src_idx = GetImageSrc(uint3(2, 1, 0) * dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (src_idx.x >= bounds.x || src_idx.y >= bounds.y) {
        return;
    }

    uint dst_idx = GetBufferDst16(dispatch_thread_id);

    uint upper = Image2CopySrc[src_idx].r;
    uint lower = Image2CopySrc[src_idx + uint3(1, 0, 0)].r;

    BufferCopyDst.Store(dst_idx, Uint16x2ToUint32(uint2(upper, lower)));
}

// R8G8
[numthreads(COPY_2D_NUM_THREAD_X, 1, 1)]
void cs_copy_buffer_image1d_r8g8(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(uint3(2, 1, 0) * dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x) {
        return;
    }

    uint src_idx = GetBufferSrc16(dispatch_thread_id);

    uint4 data = Uint32ToUint8x4(BufferCopySrc.Load(src_idx));

    uint remaining_x = bounds.x - dst_idx.x;

    if (remaining_x >= 2) {
        Image1CopyDstRg[dst_idx.xz + uint2(1, 0)] = data.zw;
    } 
    if (remaining_x >= 1) {
        Image1CopyDstRg[dst_idx.xz + uint2(0, 0)] = data.xy;
    }
}

[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_buffer_image2d_r8g8(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(uint3(2, 1, 0) * dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x || dst_idx.y >= bounds.y) {
        return;
    }

    uint src_idx = GetBufferSrc16(dispatch_thread_id);

    uint4 data = Uint32ToUint8x4(BufferCopySrc.Load(src_idx));

    uint remaining_x = bounds.x - dst_idx.x;

    if (remaining_x >= 2) {
        Image2CopyDstRg[dst_idx + uint3(1, 0, 0)] = data.zw;
    } 
    if (remaining_x >= 1) {
        Image2CopyDstRg[dst_idx + uint3(0, 0, 0)] = data.xy;
    }
}

[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_image2d_r8g8_buffer(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 src_idx = GetImageSrc(uint3(2, 1, 0) * dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (src_idx.x >= bounds.x || src_idx.y >= bounds.y) {
        return;
    }

    uint dst_idx = GetBufferDst16(dispatch_thread_id);

    uint2 lower = Image2CopySrc[src_idx].xy;
    uint2 upper = Image2CopySrc[src_idx + uint3(1, 0, 0)].xy;

    BufferCopyDst.Store(dst_idx, Uint8x4ToUint32(uint4(lower.x, lower.y, upper.x, upper.y)));
}

// R8
[numthreads(COPY_2D_NUM_THREAD_X, 1, 1)]
void cs_copy_buffer_image1d_r8(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(uint3(4, 1, 0) * dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x) {
        return;
    }

    uint src_idx = GetBufferSrc8(dispatch_thread_id);
    uint4 data = Uint32ToUint8x4(BufferCopySrc.Load(src_idx));

    uint remaining_x = bounds.x - dst_idx.x;

    if (remaining_x >= 4) {
        Image1CopyDstR[dst_idx.xz + uint2(3, 0)] = data.w;
    } 
    if (remaining_x >= 3) {
        Image1CopyDstR[dst_idx.xz + uint2(2, 0)] = data.z;
    } 
    if (remaining_x >= 2) {
        Image1CopyDstR[dst_idx.xz + uint2(1, 0)] = data.y;
    } 
    if (remaining_x >= 1) {
        Image1CopyDstR[dst_idx.xz + uint2(0, 0)] = data.x;
    }
}
[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_buffer_image2d_r8(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 dst_idx = GetImageDst(uint3(4, 1, 0) * dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (dst_idx.x >= bounds.x || dst_idx.y >= bounds.y) {
        return;
    }

    uint src_idx = GetBufferSrc8(dispatch_thread_id);
    uint4 data = Uint32ToUint8x4(BufferCopySrc.Load(src_idx));

    uint remaining_x = bounds.x - dst_idx.x;

    if (remaining_x >= 4) {
        Image2CopyDstR[dst_idx + uint3(3, 0, 0)] = data.w;
    } 
    if (remaining_x >= 3) {
        Image2CopyDstR[dst_idx + uint3(2, 0, 0)] = data.z;
    } 
    if (remaining_x >= 2) {
        Image2CopyDstR[dst_idx + uint3(1, 0, 0)] = data.y;
    } 
    if (remaining_x >= 1) {
        Image2CopyDstR[dst_idx + uint3(0, 0, 0)] = data.x;
    }
}
[numthreads(COPY_2D_NUM_THREAD_X, COPY_2D_NUM_THREAD_Y, 1)]
void cs_copy_image2d_r8_buffer(uint3 dispatch_thread_id : SV_DispatchThreadID) {
    uint3 src_idx = GetImageSrc(uint3(4, 1, 0) * dispatch_thread_id);
    uint3 bounds = GetDestBounds();
    if (src_idx.x >= bounds.x || src_idx.y >= bounds.y) {
        return;
    }

    uint dst_idx = GetBufferDst8(dispatch_thread_id);

    BufferCopyDst.Store(dst_idx, Uint8x4ToUint32(uint4(
        Image2CopySrc[src_idx].r,
        Image2CopySrc[src_idx + uint3(1, 0, 0)].r,
        Image2CopySrc[src_idx + uint3(2, 0, 0)].r,
        Image2CopySrc[src_idx + uint3(3, 0, 0)].r
    )));
}