-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtexture.cpp
1221 lines (1077 loc) · 32.4 KB
/
texture.cpp
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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "framework.h"
namespace Framework
{
// Texture2D implementation
Texture2D::Texture2D()
: m_dims(0),
m_mipLevels(0),
m_format(DXGI_FORMAT_UNKNOWN)
{
}
void Texture2D::Reset()
{
m_pPack.release();
m_apPixels.clear();
m_dims = int2(0);
m_mipLevels = 0;
m_format = DXGI_FORMAT_UNKNOWN;
m_pTex.release();
m_pSrv.release();
m_pUav.release();
}
void Texture2D::Init(
ID3D11Device * pDevice,
int2 dims,
DXGI_FORMAT format,
int flags /* = TEXFLAG_Default */)
{
ASSERT_ERR(pDevice);
// Always map the format to its typeless version, if possible;
// enables views of other formats to be created if desired
DXGI_FORMAT formatTex = FindTypelessFormat(format);
if (formatTex == DXGI_FORMAT_UNKNOWN)
formatTex = format;
D3D11_TEXTURE2D_DESC texDesc =
{
UINT(dims.x), UINT(dims.y),
UINT((flags & TEXFLAG_Mipmaps) ? CalculateMipCount(dims) : 1),
1,
formatTex,
{ 1, 0 },
D3D11_USAGE_DEFAULT,
D3D11_BIND_SHADER_RESOURCE,
0, 0,
};
if (flags & TEXFLAG_EnableUAV)
{
texDesc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
}
CHECK_D3D(pDevice->CreateTexture2D(&texDesc, nullptr, &m_pTex));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = { format, D3D11_SRV_DIMENSION_TEXTURE2D, };
srvDesc.Texture2D.MipLevels = texDesc.MipLevels;
CHECK_D3D(pDevice->CreateShaderResourceView(m_pTex, &srvDesc, &m_pSrv));
if (flags & TEXFLAG_EnableUAV)
{
D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc = { format, D3D11_UAV_DIMENSION_TEXTURE2D, };
CHECK_D3D(pDevice->CreateUnorderedAccessView(m_pTex, &uavDesc, &m_pUav));
}
m_dims = dims;
m_mipLevels = texDesc.MipLevels;
m_format = format;
}
void Texture2D::UploadToGPU(
ID3D11Device * pDevice,
int flags /* = TEXFLAG_Default */)
{
ASSERT_ERR(pDevice);
ASSERT_ERR(int(m_apPixels.size()) == m_mipLevels);
// Always map the format to its typeless version, if possible;
// enables views of other formats to be created if desired
DXGI_FORMAT formatTex = FindTypelessFormat(m_format);
if (formatTex == DXGI_FORMAT_UNKNOWN)
formatTex = m_format;
D3D11_TEXTURE2D_DESC texDesc =
{
UINT(m_dims.x), UINT(m_dims.y),
UINT(m_mipLevels), 1,
formatTex,
{ 1, 0 },
D3D11_USAGE_DEFAULT,
D3D11_BIND_SHADER_RESOURCE,
0, 0,
};
if (flags & TEXFLAG_EnableUAV)
{
texDesc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
}
std::vector<D3D11_SUBRESOURCE_DATA> aInitialData(m_mipLevels);
for (int i = 0; i < m_mipLevels; ++i)
{
D3D11_SUBRESOURCE_DATA * pInitialData = &aInitialData[i];
pInitialData->pSysMem = m_apPixels[i];
pInitialData->SysMemPitch = CalculateMipDims(m_dims.x, i) * BitsPerPixel(m_format) / 8;
pInitialData->SysMemSlicePitch = 0;
}
CHECK_D3D(pDevice->CreateTexture2D(&texDesc, &aInitialData[0], &m_pTex));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = { m_format, D3D11_SRV_DIMENSION_TEXTURE2D, };
srvDesc.Texture2D.MipLevels = m_mipLevels;
CHECK_D3D(pDevice->CreateShaderResourceView(m_pTex, &srvDesc, &m_pSrv));
if (flags & TEXFLAG_EnableUAV)
{
D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc = { m_format, D3D11_UAV_DIMENSION_TEXTURE2D, };
CHECK_D3D(pDevice->CreateUnorderedAccessView(m_pTex, &uavDesc, &m_pUav));
}
}
void Texture2D::Readback(
ID3D11DeviceContext * pCtx,
int level,
void * pDataOut)
{
ASSERT_ERR(m_pTex);
ASSERT_ERR(pCtx);
ASSERT_ERR(level >= 0 && level < m_mipLevels);
ASSERT_ERR(pDataOut);
comptr<ID3D11Device> pDevice;
pCtx->GetDevice(&pDevice);
int2 mipDims = CalculateMipDims(m_dims, level);
// Create a staging resource
D3D11_TEXTURE2D_DESC texDesc =
{
UINT(mipDims.x), UINT(mipDims.y), 1, 1,
m_format,
{ 1, 0 },
D3D11_USAGE_STAGING,
0,
D3D11_CPU_ACCESS_READ,
0,
};
comptr<ID3D11Texture2D> pTexStaging;
pDevice->CreateTexture2D(&texDesc, nullptr, &pTexStaging);
// Copy the data to the staging resource
pCtx->CopySubresourceRegion(pTexStaging, 0, 0, 0, 0, m_pTex, level, nullptr);
// Map the staging resource
D3D11_MAPPED_SUBRESOURCE mapped = {};
CHECK_D3D(pCtx->Map(pTexStaging, 0, D3D11_MAP_READ, 0, &mapped));
// Copy the data out row by row, in case the pitch is different
int rowSize = mipDims.x * BitsPerPixel(m_format) / 8;
ASSERT_ERR(mapped.RowPitch >= UINT(rowSize));
for (int y = 0; y < mipDims.y; ++y)
{
memcpy(
offsetPtr(pDataOut, y * rowSize),
offsetPtr(mapped.pData, y * mapped.RowPitch),
rowSize);
}
pCtx->Unmap(pTexStaging, 0);
}
// TextureCube implementation
TextureCube::TextureCube()
: m_cubeSize(0),
m_mipLevels(0),
m_format(DXGI_FORMAT_UNKNOWN)
{
}
void TextureCube::Reset()
{
m_pPack.release();
m_apPixels.clear();
m_cubeSize = 0;
m_mipLevels = 0;
m_format = DXGI_FORMAT_UNKNOWN;
m_pTex.release();
m_pSrv.release();
m_pUav.release();
}
void TextureCube::Init(
ID3D11Device * pDevice,
int cubeSize,
DXGI_FORMAT format,
int flags /* = TEXFLAG_Default */)
{
ASSERT_ERR(pDevice);
// Always map the format to its typeless version, if possible;
// enables views of other formats to be created if desired
DXGI_FORMAT formatTex = FindTypelessFormat(format);
if (formatTex == DXGI_FORMAT_UNKNOWN)
formatTex = format;
D3D11_TEXTURE2D_DESC texDesc =
{
UINT(cubeSize), UINT(cubeSize),
UINT((flags & TEXFLAG_Mipmaps) ? CalculateMipCount(cubeSize) : 1),
6,
formatTex,
{ 1, 0 },
D3D11_USAGE_DEFAULT,
D3D11_BIND_SHADER_RESOURCE,
0,
D3D11_RESOURCE_MISC_TEXTURECUBE,
};
if (flags & TEXFLAG_EnableUAV)
{
texDesc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
}
CHECK_D3D(pDevice->CreateTexture2D(&texDesc, nullptr, &m_pTex));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = { format, D3D11_SRV_DIMENSION_TEXTURECUBE, };
srvDesc.TextureCube.MipLevels = texDesc.MipLevels;
CHECK_D3D(pDevice->CreateShaderResourceView(m_pTex, &srvDesc, &m_pSrv));
if (flags & TEXFLAG_EnableUAV)
{
D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc = { format, D3D11_UAV_DIMENSION_TEXTURE2DARRAY, };
uavDesc.Texture2DArray.ArraySize = 6;
CHECK_D3D(pDevice->CreateUnorderedAccessView(m_pTex, &uavDesc, &m_pUav));
}
m_cubeSize = cubeSize;
m_mipLevels = texDesc.MipLevels;
m_format = format;
}
void TextureCube::UploadToGPU(
ID3D11Device * pDevice,
int flags /* = TEXFLAG_Default */)
{
ASSERT_ERR(pDevice);
ASSERT_ERR(int(m_apPixels.size()) == m_mipLevels * 6);
// Always map the format to its typeless version, if possible;
// enables views of other formats to be created if desired
DXGI_FORMAT formatTex = FindTypelessFormat(m_format);
if (formatTex == DXGI_FORMAT_UNKNOWN)
formatTex = m_format;
D3D11_TEXTURE2D_DESC texDesc =
{
UINT(m_cubeSize), UINT(m_cubeSize),
UINT(m_mipLevels), 6,
formatTex,
{ 1, 0 },
D3D11_USAGE_DEFAULT,
D3D11_BIND_SHADER_RESOURCE,
0,
D3D11_RESOURCE_MISC_TEXTURECUBE,
};
if (flags & TEXFLAG_EnableUAV)
{
texDesc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
}
std::vector<D3D11_SUBRESOURCE_DATA> aInitialData(m_mipLevels * 6);
for (int face = 0; face < 6; ++face)
{
for (int level = 0; level < m_mipLevels; ++level)
{
D3D11_SUBRESOURCE_DATA * pInitialData = &aInitialData[face * m_mipLevels + level];
pInitialData->pSysMem = m_apPixels[face * m_mipLevels + level];
pInitialData->SysMemPitch = CalculateMipDims(m_cubeSize, level) * BitsPerPixel(m_format) / 8;
pInitialData->SysMemSlicePitch = 0;
}
}
CHECK_D3D(pDevice->CreateTexture2D(&texDesc, &aInitialData[0], &m_pTex));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = { m_format, D3D11_SRV_DIMENSION_TEXTURECUBE, };
srvDesc.TextureCube.MipLevels = m_mipLevels;
CHECK_D3D(pDevice->CreateShaderResourceView(m_pTex, &srvDesc, &m_pSrv));
if (flags & TEXFLAG_EnableUAV)
{
D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc = { m_format, D3D11_UAV_DIMENSION_TEXTURE2DARRAY, };
uavDesc.Texture2DArray.ArraySize = 6;
CHECK_D3D(pDevice->CreateUnorderedAccessView(m_pTex, &uavDesc, &m_pUav));
}
}
void TextureCube::Readback(
ID3D11DeviceContext * pCtx,
int face,
int level,
void * pDataOut)
{
ASSERT_ERR(m_pTex);
ASSERT_ERR(pCtx);
ASSERT_ERR(face >= 0 && face < 6);
ASSERT_ERR(level >= 0 && level < m_mipLevels);
ASSERT_ERR(pDataOut);
comptr<ID3D11Device> pDevice;
pCtx->GetDevice(&pDevice);
int mipDim = CalculateMipDims(m_cubeSize, level);
// Create a staging resource
D3D11_TEXTURE2D_DESC texDesc =
{
UINT(mipDim), UINT(mipDim), 1, 1,
m_format,
{ 1, 0 },
D3D11_USAGE_STAGING,
0,
D3D11_CPU_ACCESS_READ,
0,
};
comptr<ID3D11Texture2D> pTexStaging;
pDevice->CreateTexture2D(&texDesc, nullptr, &pTexStaging);
// Copy the data to the staging resource
pCtx->CopySubresourceRegion(pTexStaging, 0, 0, 0, 0, m_pTex, face * m_mipLevels + level, nullptr);
// Map the staging resource and copy the data out
D3D11_MAPPED_SUBRESOURCE mapped = {};
CHECK_D3D(pCtx->Map(pTexStaging, 0, D3D11_MAP_READ, 0, &mapped));
// Copy the data out row by row, in case the pitch is different
int rowSize = mipDim * BitsPerPixel(m_format) / 8;
ASSERT_ERR(mapped.RowPitch >= UINT(rowSize));
for (int y = 0; y < mipDim; ++y)
{
memcpy(
offsetPtr(pDataOut, y * rowSize),
offsetPtr(mapped.pData, y * mapped.RowPitch),
rowSize);
}
pCtx->Unmap(pTexStaging, 0);
}
// Texture3D implementation
Texture3D::Texture3D()
: m_dims(0),
m_mipLevels(0),
m_format(DXGI_FORMAT_UNKNOWN)
{
}
void Texture3D::Reset()
{
m_pPack.release();
m_apPixels.clear();
m_dims = int3(0);
m_mipLevels = 0;
m_format = DXGI_FORMAT_UNKNOWN;
m_pTex.release();
m_pSrv.release();
m_pUav.release();
}
void Texture3D::Init(
ID3D11Device * pDevice,
int3 dims,
DXGI_FORMAT format,
int flags /* = TEXFLAG_Default */)
{
ASSERT_ERR(pDevice);
// Always map the format to its typeless version, if possible;
// enables views of other formats to be created if desired
DXGI_FORMAT formatTex = FindTypelessFormat(format);
if (formatTex == DXGI_FORMAT_UNKNOWN)
formatTex = format;
D3D11_TEXTURE3D_DESC texDesc =
{
UINT(dims.x), UINT(dims.y), UINT(dims.z),
UINT((flags & TEXFLAG_Mipmaps) ? CalculateMipCount(dims) : 1),
formatTex,
D3D11_USAGE_DEFAULT,
D3D11_BIND_SHADER_RESOURCE,
0, 0,
};
if (flags & TEXFLAG_EnableUAV)
{
texDesc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
}
CHECK_D3D(pDevice->CreateTexture3D(&texDesc, nullptr, &m_pTex));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = { format, D3D11_SRV_DIMENSION_TEXTURE3D, };
srvDesc.Texture3D.MipLevels = texDesc.MipLevels;
CHECK_D3D(pDevice->CreateShaderResourceView(m_pTex, &srvDesc, &m_pSrv));
if (flags & TEXFLAG_EnableUAV)
{
D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc = { format, D3D11_UAV_DIMENSION_TEXTURE3D, };
uavDesc.Texture3D.WSize = dims.z;
CHECK_D3D(pDevice->CreateUnorderedAccessView(m_pTex, &uavDesc, &m_pUav));
}
m_dims = dims;
m_mipLevels = texDesc.MipLevels;
m_format = format;
}
void Texture3D::UploadToGPU(
ID3D11Device * pDevice,
int flags /* = TEXFLAG_Default */)
{
ASSERT_ERR(pDevice);
ASSERT_ERR(int(m_apPixels.size()) == m_mipLevels);
// Always map the format to its typeless version, if possible;
// enables views of other formats to be created if desired
DXGI_FORMAT formatTex = FindTypelessFormat(m_format);
if (formatTex == DXGI_FORMAT_UNKNOWN)
formatTex = m_format;
D3D11_TEXTURE3D_DESC texDesc =
{
UINT(m_dims.x), UINT(m_dims.y), UINT(m_dims.z),
UINT(m_mipLevels),
formatTex,
D3D11_USAGE_DEFAULT,
D3D11_BIND_SHADER_RESOURCE,
0, 0,
};
if (flags & TEXFLAG_EnableUAV)
{
texDesc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
}
std::vector<D3D11_SUBRESOURCE_DATA> aInitialData(m_mipLevels);
for (int i = 0; i < m_mipLevels; ++i)
{
int3 mipDims = CalculateMipDims(m_dims, i);
D3D11_SUBRESOURCE_DATA * pInitialData = &aInitialData[i];
pInitialData->pSysMem = m_apPixels[i];
pInitialData->SysMemPitch = mipDims.x * BitsPerPixel(m_format) / 8;
pInitialData->SysMemSlicePitch = pInitialData->SysMemPitch * mipDims.y;
}
CHECK_D3D(pDevice->CreateTexture3D(&texDesc, &aInitialData[0], &m_pTex));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = { m_format, D3D11_SRV_DIMENSION_TEXTURE3D, };
srvDesc.Texture3D.MipLevels = m_mipLevels;
CHECK_D3D(pDevice->CreateShaderResourceView(m_pTex, &srvDesc, &m_pSrv));
if (flags & TEXFLAG_EnableUAV)
{
D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc = { m_format, D3D11_UAV_DIMENSION_TEXTURE3D, };
uavDesc.Texture3D.WSize = m_dims.z;
CHECK_D3D(pDevice->CreateUnorderedAccessView(m_pTex, &uavDesc, &m_pUav));
}
}
void Texture3D::Readback(
ID3D11DeviceContext * pCtx,
int level,
void * pDataOut)
{
ASSERT_ERR(m_pTex);
ASSERT_ERR(pCtx);
ASSERT_ERR(level >= 0 && level < m_mipLevels);
ASSERT_ERR(pDataOut);
comptr<ID3D11Device> pDevice;
pCtx->GetDevice(&pDevice);
int3 mipDims = CalculateMipDims(m_dims, level);
// Create a staging resource
D3D11_TEXTURE3D_DESC texDesc =
{
UINT(mipDims.x), UINT(mipDims.y), UINT(mipDims.z), 1,
m_format,
D3D11_USAGE_STAGING,
0,
D3D11_CPU_ACCESS_READ,
0,
};
comptr<ID3D11Texture3D> pTexStaging;
pDevice->CreateTexture3D(&texDesc, nullptr, &pTexStaging);
// Copy the data to the staging resource
pCtx->CopySubresourceRegion(pTexStaging, 0, 0, 0, 0, m_pTex, level, nullptr);
// Map the staging resource and copy the data out
D3D11_MAPPED_SUBRESOURCE mapped = {};
CHECK_D3D(pCtx->Map(pTexStaging, 0, D3D11_MAP_READ, 0, &mapped));
// Copy the data out slice by slice and row by row, in case the pitches are different
int rowSize = mipDims.x * BitsPerPixel(m_format) / 8;
int sliceSize = mipDims.y * rowSize;
ASSERT_ERR(mapped.RowPitch >= UINT(rowSize));
ASSERT_ERR(mapped.DepthPitch >= UINT(sliceSize));
for (int z = 0; z < mipDims.z; ++z)
{
for (int y = 0; y < mipDims.y; ++y)
{
memcpy(
offsetPtr(pDataOut, z * sliceSize + y * rowSize),
offsetPtr(mapped.pData, z * mapped.DepthPitch + y * mapped.RowPitch),
rowSize);
}
}
pCtx->Unmap(pTexStaging, 0);
}
// Texture creation - helper functions
void CreateTexture1x1(
ID3D11Device * pDevice,
rgba color,
Texture2D * pTexOut,
DXGI_FORMAT format /*= DXGI_FORMAT_R8G8B8A8_UNORM_SRGB*/)
{
ASSERT_ERR(pDevice);
ASSERT_ERR(pTexOut);
// Convert floats to 8-bit format
byte4 colorBytes = byte4(round(255.0f * saturate(color)));
// Always map the format to its typeless version, if possible;
// enables views of other formats to be created if desired
DXGI_FORMAT formatTex = FindTypelessFormat(format);
if (formatTex == DXGI_FORMAT_UNKNOWN)
formatTex = format;
D3D11_TEXTURE2D_DESC texDesc =
{
1, 1, 1, 1,
formatTex,
{ 1, 0 },
D3D11_USAGE_DEFAULT,
D3D11_BIND_SHADER_RESOURCE,
0, 0,
};
D3D11_SUBRESOURCE_DATA initialData = { colorBytes, sizeof(colorBytes), };
comptr<ID3D11Texture2D> pTex;
CHECK_D3D(pDevice->CreateTexture2D(&texDesc, &initialData, &pTex));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc =
{
format,
D3D11_SRV_DIMENSION_TEXTURE2D,
};
srvDesc.Texture2D.MipLevels = 1;
comptr<ID3D11ShaderResourceView> pSrv = nullptr;
CHECK_D3D(pDevice->CreateShaderResourceView(pTex, &srvDesc, &pSrv));
pTexOut->m_pTex = pTex;
pTexOut->m_pSrv = pSrv;
pTexOut->m_dims = { 1, 1 };
pTexOut->m_mipLevels = 1;
pTexOut->m_format = format;
}
void CreateTextureCube1x1(
ID3D11Device * pDevice,
rgba color,
TextureCube * pTexOut,
DXGI_FORMAT format /*= DXGI_FORMAT_R8G8B8A8_UNORM_SRGB*/)
{
ASSERT_ERR(pDevice);
ASSERT_ERR(pTexOut);
// Convert floats to 8-bit format
byte4 colorBytes[6] = { byte4(round(255.0f * saturate(color))) };
for (int i = 1; i < dim(colorBytes); ++i)
colorBytes[i] = colorBytes[0];
// Always map the format to its typeless version, if possible;
// enables views of other formats to be created if desired
DXGI_FORMAT formatTex = FindTypelessFormat(format);
if (formatTex == DXGI_FORMAT_UNKNOWN)
formatTex = format;
D3D11_TEXTURE2D_DESC texDesc =
{
1, 1, 1, 6,
formatTex,
{ 1, 0 },
D3D11_USAGE_DEFAULT,
D3D11_BIND_SHADER_RESOURCE,
0,
D3D11_RESOURCE_MISC_TEXTURECUBE,
};
D3D11_SUBRESOURCE_DATA initialData = { colorBytes, sizeof(colorBytes), };
comptr<ID3D11Texture2D> pTex;
CHECK_D3D(pDevice->CreateTexture2D(&texDesc, &initialData, &pTex));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc =
{
format,
D3D11_SRV_DIMENSION_TEXTURECUBE,
};
srvDesc.TextureCube.MipLevels = 1;
comptr<ID3D11ShaderResourceView> pSrv = nullptr;
CHECK_D3D(pDevice->CreateShaderResourceView(pTex, &srvDesc, &pSrv));
pTexOut->m_pTex = pTex;
pTexOut->m_pSrv = pSrv;
pTexOut->m_cubeSize = 1;
pTexOut->m_mipLevels = 1;
pTexOut->m_format = format;
}
void CreateTexture2DFromMemory(
ID3D11Device * pDevice,
int2 dims,
DXGI_FORMAT format,
const void * pPixels,
Texture2D * pTexOut)
{
ASSERT_ERR(pDevice);
ASSERT_ERR(pPixels);
ASSERT_ERR(pTexOut);
// Always map the format to its typeless version, if possible;
// enables views of other formats to be created if desired
DXGI_FORMAT formatTex = FindTypelessFormat(format);
if (formatTex == DXGI_FORMAT_UNKNOWN)
formatTex = format;
D3D11_TEXTURE2D_DESC texDesc =
{
UINT(dims.x), UINT(dims.y), 1, 1,
formatTex,
{ 1, 0 },
D3D11_USAGE_IMMUTABLE,
D3D11_BIND_SHADER_RESOURCE,
0, 0,
};
D3D11_SUBRESOURCE_DATA initialData = { pPixels, UINT(dims.x * BitsPerPixel(format) / 8) };
comptr<ID3D11Texture2D> pTex;
CHECK_D3D(pDevice->CreateTexture2D(&texDesc, &initialData, &pTex));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc =
{
format,
D3D11_SRV_DIMENSION_TEXTURE2D,
};
srvDesc.Texture2D.MipLevels = 1;
comptr<ID3D11ShaderResourceView> pSrv = nullptr;
CHECK_D3D(pDevice->CreateShaderResourceView(pTex, &srvDesc, &pSrv));
pTexOut->m_pTex = pTex;
pTexOut->m_pSrv = pSrv;
pTexOut->m_dims = dims;
pTexOut->m_mipLevels = 1;
pTexOut->m_format = format;
}
// TextureLib implementation
TextureLib::TextureLib()
{
}
Texture2D * TextureLib::Lookup(const std::string & name)
{
auto iter = m_texs.find(name);
if (iter == m_texs.end())
return nullptr;
return &iter->second;
}
void TextureLib::UploadAllToGPU(ID3D11Device * pDevice, int flags /* = TEXFLAG_Default */)
{
for (auto iter = m_texs.begin(), end = m_texs.end(); iter != end; ++iter)
{
iter->second.UploadToGPU(pDevice, flags);
}
}
void TextureLib::Reset()
{
m_texs.clear();
}
// Utility functions
const char * NameOfFormat(DXGI_FORMAT format)
{
static const char * s_names[] =
{
"UNKNOWN",
"R32G32B32A32_TYPELESS",
"R32G32B32A32_FLOAT",
"R32G32B32A32_UINT",
"R32G32B32A32_SINT",
"R32G32B32_TYPELESS",
"R32G32B32_FLOAT",
"R32G32B32_UINT",
"R32G32B32_SINT",
"R16G16B16A16_TYPELESS",
"R16G16B16A16_FLOAT",
"R16G16B16A16_UNORM",
"R16G16B16A16_UINT",
"R16G16B16A16_SNORM",
"R16G16B16A16_SINT",
"R32G32_TYPELESS",
"R32G32_FLOAT",
"R32G32_UINT",
"R32G32_SINT",
"R32G8X24_TYPELESS",
"D32_FLOAT_S8X24_UINT",
"R32_FLOAT_X8X24_TYPELESS",
"X32_TYPELESS_G8X24_UINT",
"R10G10B10A2_TYPELESS",
"R10G10B10A2_UNORM",
"R10G10B10A2_UINT",
"R11G11B10_FLOAT",
"R8G8B8A8_TYPELESS",
"R8G8B8A8_UNORM",
"R8G8B8A8_UNORM_SRGB",
"R8G8B8A8_UINT",
"R8G8B8A8_SNORM",
"R8G8B8A8_SINT",
"R16G16_TYPELESS",
"R16G16_FLOAT",
"R16G16_UNORM",
"R16G16_UINT",
"R16G16_SNORM",
"R16G16_SINT",
"R32_TYPELESS",
"D32_FLOAT",
"R32_FLOAT",
"R32_UINT",
"R32_SINT",
"R24G8_TYPELESS",
"D24_UNORM_S8_UINT",
"R24_UNORM_X8_TYPELESS",
"X24_TYPELESS_G8_UINT",
"R8G8_TYPELESS",
"R8G8_UNORM",
"R8G8_UINT",
"R8G8_SNORM",
"R8G8_SINT",
"R16_TYPELESS",
"R16_FLOAT",
"D16_UNORM",
"R16_UNORM",
"R16_UINT",
"R16_SNORM",
"R16_SINT",
"R8_TYPELESS",
"R8_UNORM",
"R8_UINT",
"R8_SNORM",
"R8_SINT",
"A8_UNORM",
"R1_UNORM",
"R9G9B9E5_SHAREDEXP",
"R8G8_B8G8_UNORM",
"G8R8_G8B8_UNORM",
"BC1_TYPELESS",
"BC1_UNORM",
"BC1_UNORM_SRGB",
"BC2_TYPELESS",
"BC2_UNORM",
"BC2_UNORM_SRGB",
"BC3_TYPELESS",
"BC3_UNORM",
"BC3_UNORM_SRGB",
"BC4_TYPELESS",
"BC4_UNORM",
"BC4_SNORM",
"BC5_TYPELESS",
"BC5_UNORM",
"BC5_SNORM",
"B5G6R5_UNORM",
"B5G5R5A1_UNORM",
"B8G8R8A8_UNORM",
"B8G8R8X8_UNORM",
"R10G10B10_XR_BIAS_A2_UNORM",
"B8G8R8A8_TYPELESS",
"B8G8R8A8_UNORM_SRGB",
"B8G8R8X8_TYPELESS",
"B8G8R8X8_UNORM_SRGB",
"BC6H_TYPELESS",
"BC6H_UF16",
"BC6H_SF16",
"BC7_TYPELESS",
"BC7_UNORM",
"BC7_UNORM_SRGB",
"AYUV",
"Y410",
"Y416",
"NV12",
"P010",
"P016",
"420_OPAQUE",
"YUY2",
"Y210",
"Y216",
"NV11",
"AI44",
"IA44",
"P8",
"A8P8",
"B4G4R4A4_UNORM",
};
if (uint(format) >= dim(s_names))
{
WARN("Unexpected DXGI_FORMAT %d", format);
return "UNKNOWN";
}
return s_names[format];
}
int BitsPerPixel(DXGI_FORMAT format)
{
static const int s_bitsPerPixel[] =
{
0, // UNKNOWN
128, // R32G32B32A32_TYPELESS
128, // R32G32B32A32_FLOAT
128, // R32G32B32A32_UINT
128, // R32G32B32A32_SINT
96, // R32G32B32_TYPELESS
96, // R32G32B32_FLOAT
96, // R32G32B32_UINT
96, // R32G32B32_SINT
64, // R16G16B16A16_TYPELESS
64, // R16G16B16A16_FLOAT
64, // R16G16B16A16_UNORM
64, // R16G16B16A16_UINT
64, // R16G16B16A16_SNORM
64, // R16G16B16A16_SINT
64, // R32G32_TYPELESS
64, // R32G32_FLOAT
64, // R32G32_UINT
64, // R32G32_SINT
64, // R32G8X24_TYPELESS
64, // D32_FLOAT_S8X24_UINT
64, // R32_FLOAT_X8X24_TYPELESS
64, // X32_TYPELESS_G8X24_UINT
32, // R10G10B10A2_TYPELESS
32, // R10G10B10A2_UNORM
32, // R10G10B10A2_UINT
32, // R11G11B10_FLOAT
32, // R8G8B8A8_TYPELESS
32, // R8G8B8A8_UNORM
32, // R8G8B8A8_UNORM_SRGB
32, // R8G8B8A8_UINT
32, // R8G8B8A8_SNORM
32, // R8G8B8A8_SINT
32, // R16G16_TYPELESS
32, // R16G16_FLOAT
32, // R16G16_UNORM
32, // R16G16_UINT
32, // R16G16_SNORM
32, // R16G16_SINT
32, // R32_TYPELESS
32, // D32_FLOAT
32, // R32_FLOAT
32, // R32_UINT
32, // R32_SINT
32, // R24G8_TYPELESS
32, // D24_UNORM_S8_UINT
32, // R24_UNORM_X8_TYPELESS
32, // X24_TYPELESS_G8_UINT
16, // R8G8_TYPELESS
16, // R8G8_UNORM
16, // R8G8_UINT
16, // R8G8_SNORM
16, // R8G8_SINT
16, // R16_TYPELESS
16, // R16_FLOAT
16, // D16_UNORM
16, // R16_UNORM
16, // R16_UINT
16, // R16_SNORM
16, // R16_SINT
8, // R8_TYPELESS
8, // R8_UNORM
8, // R8_UINT
8, // R8_SNORM
8, // R8_SINT
8, // A8_UNORM
1, // R1_UNORM
32, // R9G9B9E5_SHAREDEXP
16, // R8G8_B8G8_UNORM
16, // G8R8_G8B8_UNORM
4, // BC1_TYPELESS
4, // BC1_UNORM
4, // BC1_UNORM_SRGB
8, // BC2_TYPELESS
8, // BC2_UNORM
8, // BC2_UNORM_SRGB
8, // BC3_TYPELESS
8, // BC3_UNORM
8, // BC3_UNORM_SRGB
4, // BC4_TYPELESS
4, // BC4_UNORM
4, // BC4_SNORM
8, // BC5_TYPELESS
8, // BC5_UNORM
8, // BC5_SNORM
16, // B5G6R5_UNORM
16, // B5G5R5A1_UNORM
32, // B8G8R8A8_UNORM
32, // B8G8R8X8_UNORM
32, // R10G10B10_XR_BIAS_A2_UNORM
32, // B8G8R8A8_TYPELESS
32, // B8G8R8A8_UNORM_SRGB
32, // B8G8R8X8_TYPELESS
32, // B8G8R8X8_UNORM_SRGB
8, // BC6H_TYPELESS
8, // BC6H_UF16
8, // BC6H_SF16
8, // BC7_TYPELESS
8, // BC7_UNORM
8, // BC7_UNORM_SRGB
// NOTE: I don't know what are the bit depths for the video formats;
// the MS docs don't seem to describe them in any detail.
0, // AYUV
0, // Y410
0, // Y416
0, // NV12
0, // P010
0, // P016
0, // 420_OPAQUE
0, // YUY2
0, // Y210
0, // Y216
0, // NV11
0, // AI44
0, // IA44
8, // P8
16, // A8P8
16, // B4G4R4A4_UNORM
};
if (uint(format) >= dim(s_bitsPerPixel))
{
WARN("Unexpected DXGI_FORMAT %d", format);
return 0;
}
return s_bitsPerPixel[format];
}
DXGI_FORMAT FindTypelessFormat(DXGI_FORMAT format)
{
static const DXGI_FORMAT s_typelessFormat[] =
{
DXGI_FORMAT_UNKNOWN, // UNKNOWN
DXGI_FORMAT_R32G32B32A32_TYPELESS, // R32G32B32A32_TYPELESS
DXGI_FORMAT_R32G32B32A32_TYPELESS, // R32G32B32A32_FLOAT
DXGI_FORMAT_R32G32B32A32_TYPELESS, // R32G32B32A32_UINT
DXGI_FORMAT_R32G32B32A32_TYPELESS, // R32G32B32A32_SINT
DXGI_FORMAT_R32G32B32_TYPELESS, // R32G32B32_TYPELESS
DXGI_FORMAT_R32G32B32_TYPELESS, // R32G32B32_FLOAT
DXGI_FORMAT_R32G32B32_TYPELESS, // R32G32B32_UINT
DXGI_FORMAT_R32G32B32_TYPELESS, // R32G32B32_SINT
DXGI_FORMAT_R16G16B16A16_TYPELESS, // R16G16B16A16_TYPELESS
DXGI_FORMAT_R16G16B16A16_TYPELESS, // R16G16B16A16_FLOAT
DXGI_FORMAT_R16G16B16A16_TYPELESS, // R16G16B16A16_UNORM
DXGI_FORMAT_R16G16B16A16_TYPELESS, // R16G16B16A16_UINT
DXGI_FORMAT_R16G16B16A16_TYPELESS, // R16G16B16A16_SNORM
DXGI_FORMAT_R16G16B16A16_TYPELESS, // R16G16B16A16_SINT
DXGI_FORMAT_R32G32_TYPELESS, // R32G32_TYPELESS
DXGI_FORMAT_R32G32_TYPELESS, // R32G32_FLOAT
DXGI_FORMAT_R32G32_TYPELESS, // R32G32_UINT
DXGI_FORMAT_R32G32_TYPELESS, // R32G32_SINT
DXGI_FORMAT_R32G8X24_TYPELESS, // R32G8X24_TYPELESS