mirrored from https://chromium.googlesource.com/angle/angle
-
Notifications
You must be signed in to change notification settings - Fork 626
/
Copy pathBlit11.cpp
1942 lines (1666 loc) · 77.5 KB
/
Blit11.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
//
// Copyright 2013 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Blit11.cpp: Texture copy utility class.
#include "libANGLE/renderer/d3d/d3d11/Blit11.h"
#include <float.h>
#include "common/utilities.h"
#include "libANGLE/Context.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/renderer/d3d/d3d11/Context11.h"
#include "libANGLE/renderer/d3d/d3d11/RenderTarget11.h"
#include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
#include "libANGLE/renderer/d3d/d3d11/formatutils11.h"
#include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
#include "libANGLE/renderer/d3d/d3d11/texture_format_table.h"
#include "libANGLE/trace.h"
namespace rx
{
namespace
{
// Include inline shaders in the anonymous namespace to make sure no symbols are exported
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/passthrough2d11vs.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/passthroughdepth2d11ps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/passthrough3d11gs.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/passthrough3d11vs.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/resolvedepth11_ps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/resolvedepthstencil11_ps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/resolvedepthstencil11_vs.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/resolvestencil11_ps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/swizzlef2darrayps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/swizzlef2dps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/swizzlef3dps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/swizzlei2darrayps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/swizzlei2dps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/swizzlei3dps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/swizzleui2darrayps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/swizzleui2dps.h"
#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/swizzleui3dps.h"
void StretchedBlitNearest_RowByRow(const gl::Box &sourceArea,
const gl::Box &destArea,
const gl::Rectangle &clippedDestArea,
const gl::Extents &sourceSize,
unsigned int sourceRowPitch,
unsigned int destRowPitch,
size_t pixelSize,
const uint8_t *sourceData,
uint8_t *destData)
{
int srcHeightSubOne = (sourceArea.height - 1);
size_t copySize = pixelSize * clippedDestArea.width;
size_t srcOffset = sourceArea.x * pixelSize;
size_t destOffset = clippedDestArea.x * pixelSize;
for (int y = clippedDestArea.y; y < clippedDestArea.y + clippedDestArea.height; y++)
{
// TODO: Fix divide by zero when height == 1. http://anglebug.com/6099
float yPerc = static_cast<float>(y - destArea.y) / (destArea.height - 1);
// Interpolate using the original source rectangle to determine which row to sample from
// while clamping to the edges
unsigned int readRow = static_cast<unsigned int>(
gl::clamp(sourceArea.y + floor(yPerc * srcHeightSubOne + 0.5f), 0, srcHeightSubOne));
unsigned int writeRow = y;
const uint8_t *sourceRow = sourceData + readRow * sourceRowPitch + srcOffset;
uint8_t *destRow = destData + writeRow * destRowPitch + destOffset;
memcpy(destRow, sourceRow, copySize);
}
}
void StretchedBlitNearest_PixelByPixel(const gl::Box &sourceArea,
const gl::Box &destArea,
const gl::Rectangle &clippedDestArea,
const gl::Extents &sourceSize,
unsigned int sourceRowPitch,
unsigned int destRowPitch,
ptrdiff_t readOffset,
ptrdiff_t writeOffset,
size_t copySize,
size_t srcPixelStride,
size_t destPixelStride,
const uint8_t *sourceData,
uint8_t *destData)
{
auto xMax = clippedDestArea.x + clippedDestArea.width;
auto yMax = clippedDestArea.y + clippedDestArea.height;
for (int writeRow = clippedDestArea.y; writeRow < yMax; writeRow++)
{
// Interpolate using the original source rectangle to determine which row to sample from
// while clamping to the edges
float yPerc = static_cast<float>(writeRow - destArea.y) / (destArea.height - 1);
float yRounded = floor(yPerc * (sourceArea.height - 1) + 0.5f);
unsigned int readRow =
static_cast<unsigned int>(gl::clamp(sourceArea.y + yRounded, 0, sourceSize.height - 1));
for (int writeColumn = clippedDestArea.x; writeColumn < xMax; writeColumn++)
{
// Interpolate the original source rectangle to determine which column to sample
// from while clamping to the edges
float xPerc = static_cast<float>(writeColumn - destArea.x) / (destArea.width - 1);
float xRounded = floor(xPerc * (sourceArea.width - 1) + 0.5f);
unsigned int readColumn = static_cast<unsigned int>(
gl::clamp(sourceArea.x + xRounded, 0, sourceSize.width - 1));
const uint8_t *sourcePixel =
sourceData + readRow * sourceRowPitch + readColumn * srcPixelStride + readOffset;
uint8_t *destPixel =
destData + writeRow * destRowPitch + writeColumn * destPixelStride + writeOffset;
memcpy(destPixel, sourcePixel, copySize);
}
}
}
void StretchedBlitNearest(const gl::Box &sourceArea,
const gl::Box &destArea,
const gl::Rectangle &clipRect,
const gl::Extents &sourceSize,
unsigned int sourceRowPitch,
unsigned int destRowPitch,
ptrdiff_t readOffset,
ptrdiff_t writeOffset,
size_t copySize,
size_t srcPixelStride,
size_t destPixelStride,
const uint8_t *sourceData,
uint8_t *destData)
{
gl::Rectangle clippedDestArea(destArea.x, destArea.y, destArea.width, destArea.height);
if (!gl::ClipRectangle(clippedDestArea, clipRect, &clippedDestArea))
{
return;
}
// Determine if entire rows can be copied at once instead of each individual pixel. There
// must be no out of bounds lookups, whole rows copies, and no scale.
if (sourceArea.width == clippedDestArea.width && sourceArea.x >= 0 &&
sourceArea.x + sourceArea.width <= sourceSize.width && copySize == srcPixelStride &&
copySize == destPixelStride)
{
StretchedBlitNearest_RowByRow(sourceArea, destArea, clippedDestArea, sourceSize,
sourceRowPitch, destRowPitch, srcPixelStride, sourceData,
destData);
}
else
{
StretchedBlitNearest_PixelByPixel(sourceArea, destArea, clippedDestArea, sourceSize,
sourceRowPitch, destRowPitch, readOffset, writeOffset,
copySize, srcPixelStride, destPixelStride, sourceData,
destData);
}
}
using DepthStencilLoader = void(const float *, uint8_t *);
void LoadDepth16(const float *source, uint8_t *dest)
{
uint32_t convertedDepth = gl::floatToNormalized<16, uint32_t>(source[0]);
memcpy(dest, &convertedDepth, 2u);
}
void LoadDepth24(const float *source, uint8_t *dest)
{
uint32_t convertedDepth = gl::floatToNormalized<24, uint32_t>(source[0]);
memcpy(dest, &convertedDepth, 3u);
}
void LoadStencilHelper(const float *source, uint8_t *dest)
{
uint32_t convertedStencil = gl::getShiftedData<8, 0>(static_cast<uint32_t>(source[1]));
memcpy(dest, &convertedStencil, 1u);
}
void LoadStencil8(const float *source, uint8_t *dest)
{
// STENCIL_INDEX8 is implemented with D24S8, with the depth bits unused. Writes zero for safety.
float zero = 0.0f;
LoadDepth24(&zero, &dest[0]);
LoadStencilHelper(source, &dest[3]);
}
void LoadDepth24Stencil8(const float *source, uint8_t *dest)
{
LoadDepth24(source, &dest[0]);
LoadStencilHelper(source, &dest[3]);
}
void LoadDepth32F(const float *source, uint8_t *dest)
{
memcpy(dest, source, sizeof(float));
}
void LoadDepth32FStencil8(const float *source, uint8_t *dest)
{
LoadDepth32F(source, &dest[0]);
LoadStencilHelper(source, &dest[4]);
}
template <DepthStencilLoader loader>
void CopyDepthStencil(const gl::Box &sourceArea,
const gl::Box &destArea,
const gl::Rectangle &clippedDestArea,
const gl::Extents &sourceSize,
unsigned int sourceRowPitch,
unsigned int destRowPitch,
ptrdiff_t readOffset,
ptrdiff_t writeOffset,
size_t copySize,
size_t srcPixelStride,
size_t destPixelStride,
const uint8_t *sourceData,
uint8_t *destData)
{
// No stretching or subregions are supported, only full blits.
ASSERT(sourceArea == destArea);
ASSERT(sourceSize.width == sourceArea.width && sourceSize.height == sourceArea.height &&
sourceSize.depth == 1);
ASSERT(clippedDestArea.width == sourceSize.width &&
clippedDestArea.height == sourceSize.height);
ASSERT(readOffset == 0 && writeOffset == 0);
ASSERT(destArea.x == 0 && destArea.y == 0);
for (int row = 0; row < destArea.height; ++row)
{
for (int column = 0; column < destArea.width; ++column)
{
ptrdiff_t offset = row * sourceRowPitch + column * srcPixelStride;
const float *sourcePixel = reinterpret_cast<const float *>(sourceData + offset);
uint8_t *destPixel = destData + row * destRowPitch + column * destPixelStride;
loader(sourcePixel, destPixel);
}
}
}
void Depth32FStencil8ToDepth32F(const float *source, float *dest)
{
*dest = *source;
}
void Depth24Stencil8ToDepth32F(const uint32_t *source, float *dest)
{
uint32_t normDepth = source[0] & 0x00FFFFFF;
float floatDepth = gl::normalizedToFloat<24>(normDepth);
*dest = floatDepth;
}
void BlitD24S8ToD32F(const gl::Box &sourceArea,
const gl::Box &destArea,
const gl::Rectangle &clippedDestArea,
const gl::Extents &sourceSize,
unsigned int sourceRowPitch,
unsigned int destRowPitch,
ptrdiff_t readOffset,
ptrdiff_t writeOffset,
size_t copySize,
size_t srcPixelStride,
size_t destPixelStride,
const uint8_t *sourceData,
uint8_t *destData)
{
// No stretching or subregions are supported, only full blits.
ASSERT(sourceArea == destArea);
ASSERT(sourceSize.width == sourceArea.width && sourceSize.height == sourceArea.height &&
sourceSize.depth == 1);
ASSERT(clippedDestArea.width == sourceSize.width &&
clippedDestArea.height == sourceSize.height);
ASSERT(readOffset == 0 && writeOffset == 0);
ASSERT(destArea.x == 0 && destArea.y == 0);
for (int row = 0; row < destArea.height; ++row)
{
for (int column = 0; column < destArea.width; ++column)
{
ptrdiff_t offset = row * sourceRowPitch + column * srcPixelStride;
const uint32_t *sourcePixel = reinterpret_cast<const uint32_t *>(sourceData + offset);
float *destPixel =
reinterpret_cast<float *>(destData + row * destRowPitch + column * destPixelStride);
Depth24Stencil8ToDepth32F(sourcePixel, destPixel);
}
}
}
void BlitD32FS8ToD32F(const gl::Box &sourceArea,
const gl::Box &destArea,
const gl::Rectangle &clippedDestArea,
const gl::Extents &sourceSize,
unsigned int sourceRowPitch,
unsigned int destRowPitch,
ptrdiff_t readOffset,
ptrdiff_t writeOffset,
size_t copySize,
size_t srcPixelStride,
size_t destPixelStride,
const uint8_t *sourceData,
uint8_t *destData)
{
// No stretching or subregions are supported, only full blits.
ASSERT(sourceArea == destArea);
ASSERT(sourceSize.width == sourceArea.width && sourceSize.height == sourceArea.height &&
sourceSize.depth == 1);
ASSERT(clippedDestArea.width == sourceSize.width &&
clippedDestArea.height == sourceSize.height);
ASSERT(readOffset == 0 && writeOffset == 0);
ASSERT(destArea.x == 0 && destArea.y == 0);
for (int row = 0; row < destArea.height; ++row)
{
for (int column = 0; column < destArea.width; ++column)
{
ptrdiff_t offset = row * sourceRowPitch + column * srcPixelStride;
const float *sourcePixel = reinterpret_cast<const float *>(sourceData + offset);
float *destPixel =
reinterpret_cast<float *>(destData + row * destRowPitch + column * destPixelStride);
Depth32FStencil8ToDepth32F(sourcePixel, destPixel);
}
}
}
Blit11::BlitConvertFunction *GetCopyDepthStencilFunction(GLenum internalFormat)
{
switch (internalFormat)
{
case GL_DEPTH_COMPONENT16:
return &CopyDepthStencil<LoadDepth16>;
case GL_DEPTH_COMPONENT24:
return &CopyDepthStencil<LoadDepth24>;
case GL_DEPTH_COMPONENT32F:
return &CopyDepthStencil<LoadDepth32F>;
case GL_STENCIL_INDEX8:
return &CopyDepthStencil<LoadStencil8>;
case GL_DEPTH24_STENCIL8:
return &CopyDepthStencil<LoadDepth24Stencil8>;
case GL_DEPTH32F_STENCIL8:
return &CopyDepthStencil<LoadDepth32FStencil8>;
default:
UNREACHABLE();
return nullptr;
}
}
inline void GenerateVertexCoords(const gl::Box &sourceArea,
const gl::Extents &sourceSize,
const gl::Box &destArea,
const gl::Extents &destSize,
float *x1,
float *y1,
float *x2,
float *y2,
float *u1,
float *v1,
float *u2,
float *v2)
{
*x1 = (destArea.x / float(destSize.width)) * 2.0f - 1.0f;
*y1 = ((destSize.height - destArea.y - destArea.height) / float(destSize.height)) * 2.0f - 1.0f;
*x2 = ((destArea.x + destArea.width) / float(destSize.width)) * 2.0f - 1.0f;
*y2 = ((destSize.height - destArea.y) / float(destSize.height)) * 2.0f - 1.0f;
*u1 = sourceArea.x / float(sourceSize.width);
*v1 = sourceArea.y / float(sourceSize.height);
*u2 = (sourceArea.x + sourceArea.width) / float(sourceSize.width);
*v2 = (sourceArea.y + sourceArea.height) / float(sourceSize.height);
}
void Write2DVertices(const gl::Box &sourceArea,
const gl::Extents &sourceSize,
const gl::Box &destArea,
const gl::Extents &destSize,
void *outVertices,
unsigned int *outStride,
unsigned int *outVertexCount,
D3D11_PRIMITIVE_TOPOLOGY *outTopology)
{
float x1, y1, x2, y2, u1, v1, u2, v2;
GenerateVertexCoords(sourceArea, sourceSize, destArea, destSize, &x1, &y1, &x2, &y2, &u1, &v1,
&u2, &v2);
d3d11::PositionTexCoordVertex *vertices =
static_cast<d3d11::PositionTexCoordVertex *>(outVertices);
d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
*outStride = sizeof(d3d11::PositionTexCoordVertex);
*outVertexCount = 4;
*outTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
}
void Write3DVertices(const gl::Box &sourceArea,
const gl::Extents &sourceSize,
const gl::Box &destArea,
const gl::Extents &destSize,
void *outVertices,
unsigned int *outStride,
unsigned int *outVertexCount,
D3D11_PRIMITIVE_TOPOLOGY *outTopology)
{
ASSERT(sourceSize.depth > 0 && destSize.depth > 0);
float x1, y1, x2, y2, u1, v1, u2, v2;
GenerateVertexCoords(sourceArea, sourceSize, destArea, destSize, &x1, &y1, &x2, &y2, &u1, &v1,
&u2, &v2);
d3d11::PositionLayerTexCoord3DVertex *vertices =
static_cast<d3d11::PositionLayerTexCoord3DVertex *>(outVertices);
for (int i = 0; i < destSize.depth; i++)
{
float readDepth = (float)i / std::max(destSize.depth - 1, 1);
d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 0], x1, y1, i, u1, v2, readDepth);
d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 1], x1, y2, i, u1, v1, readDepth);
d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 2], x2, y1, i, u2, v2, readDepth);
d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 3], x1, y2, i, u1, v1, readDepth);
d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 4], x2, y2, i, u2, v1, readDepth);
d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 5], x2, y1, i, u2, v2, readDepth);
}
*outStride = sizeof(d3d11::PositionLayerTexCoord3DVertex);
*outVertexCount = destSize.depth * 6;
*outTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
}
unsigned int GetSwizzleIndex(GLenum swizzle)
{
unsigned int colorIndex = 0;
switch (swizzle)
{
case GL_RED:
colorIndex = 0;
break;
case GL_GREEN:
colorIndex = 1;
break;
case GL_BLUE:
colorIndex = 2;
break;
case GL_ALPHA:
colorIndex = 3;
break;
case GL_ZERO:
colorIndex = 4;
break;
case GL_ONE:
colorIndex = 5;
break;
default:
UNREACHABLE();
break;
}
return colorIndex;
}
D3D11_BLEND_DESC GetAlphaMaskBlendStateDesc()
{
D3D11_BLEND_DESC desc;
memset(&desc, 0, sizeof(desc));
desc.RenderTarget[0].BlendEnable = TRUE;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO;
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_RED |
D3D11_COLOR_WRITE_ENABLE_GREEN |
D3D11_COLOR_WRITE_ENABLE_BLUE;
return desc;
}
D3D11_INPUT_ELEMENT_DESC quad2DLayout[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
D3D11_INPUT_ELEMENT_DESC quad3DLayout[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"LAYER", 0, DXGI_FORMAT_R32_UINT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
DXGI_FORMAT GetStencilSRVFormat(const d3d11::Format &formatSet)
{
switch (formatSet.texFormat)
{
case DXGI_FORMAT_R32G8X24_TYPELESS:
return DXGI_FORMAT_X32_TYPELESS_G8X24_UINT;
case DXGI_FORMAT_R24G8_TYPELESS:
return DXGI_FORMAT_X24_TYPELESS_G8_UINT;
default:
UNREACHABLE();
return DXGI_FORMAT_UNKNOWN;
}
}
} // namespace
#include "libANGLE/renderer/d3d/d3d11/Blit11Helper_autogen.inc"
Blit11::Shader::Shader() = default;
Blit11::Shader::Shader(Shader &&other) = default;
Blit11::Shader::~Shader() = default;
Blit11::Shader &Blit11::Shader::operator=(Blit11::Shader &&other) = default;
Blit11::Blit11(Renderer11 *renderer)
: mRenderer(renderer),
mResourcesInitialized(false),
mVertexBuffer(),
mPointSampler(),
mLinearSampler(),
mScissorEnabledRasterizerState(),
mScissorDisabledRasterizerState(),
mDepthStencilState(),
mQuad2DIL(quad2DLayout,
ArraySize(quad2DLayout),
g_VS_Passthrough2D,
ArraySize(g_VS_Passthrough2D),
"Blit11 2D input layout"),
mQuad2DVS(g_VS_Passthrough2D, ArraySize(g_VS_Passthrough2D), "Blit11 2D vertex shader"),
mDepthPS(g_PS_PassthroughDepth2D,
ArraySize(g_PS_PassthroughDepth2D),
"Blit11 2D depth pixel shader"),
mQuad3DIL(quad3DLayout,
ArraySize(quad3DLayout),
g_VS_Passthrough3D,
ArraySize(g_VS_Passthrough3D),
"Blit11 3D input layout"),
mQuad3DVS(g_VS_Passthrough3D, ArraySize(g_VS_Passthrough3D), "Blit11 3D vertex shader"),
mQuad3DGS(g_GS_Passthrough3D, ArraySize(g_GS_Passthrough3D), "Blit11 3D geometry shader"),
mAlphaMaskBlendState(GetAlphaMaskBlendStateDesc(), "Blit11 Alpha Mask Blend"),
mSwizzleCB(),
mResolveDepthStencilVS(g_VS_ResolveDepthStencil,
ArraySize(g_VS_ResolveDepthStencil),
"Blit11::mResolveDepthStencilVS"),
mResolveDepthPS(g_PS_ResolveDepth, ArraySize(g_PS_ResolveDepth), "Blit11::mResolveDepthPS"),
mResolveDepthStencilPS(g_PS_ResolveDepthStencil,
ArraySize(g_PS_ResolveDepthStencil),
"Blit11::mResolveDepthStencilPS"),
mResolveStencilPS(g_PS_ResolveStencil,
ArraySize(g_PS_ResolveStencil),
"Blit11::mResolveStencilPS"),
mStencilSRV(),
mResolvedDepthStencilRTView()
{}
Blit11::~Blit11() {}
angle::Result Blit11::initResources(const gl::Context *context)
{
if (mResourcesInitialized)
{
return angle::Result::Continue;
}
ANGLE_TRACE_EVENT0("gpu.angle", "Blit11::initResources");
D3D11_BUFFER_DESC vbDesc;
vbDesc.ByteWidth =
static_cast<unsigned int>(std::max(sizeof(d3d11::PositionLayerTexCoord3DVertex),
sizeof(d3d11::PositionTexCoordVertex)) *
6 * mRenderer->getNativeCaps().max3DTextureSize);
vbDesc.Usage = D3D11_USAGE_DYNAMIC;
vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
vbDesc.MiscFlags = 0;
vbDesc.StructureByteStride = 0;
Context11 *context11 = GetImplAs<Context11>(context);
ANGLE_TRY(mRenderer->allocateResource(context11, vbDesc, &mVertexBuffer));
mVertexBuffer.setInternalName("Blit11VertexBuffer");
D3D11_SAMPLER_DESC pointSamplerDesc;
pointSamplerDesc.Filter = D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR;
pointSamplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
pointSamplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
pointSamplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
pointSamplerDesc.MipLODBias = 0.0f;
pointSamplerDesc.MaxAnisotropy = 0;
pointSamplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
pointSamplerDesc.BorderColor[0] = 0.0f;
pointSamplerDesc.BorderColor[1] = 0.0f;
pointSamplerDesc.BorderColor[2] = 0.0f;
pointSamplerDesc.BorderColor[3] = 0.0f;
pointSamplerDesc.MinLOD = 0.0f;
pointSamplerDesc.MaxLOD = FLT_MAX;
ANGLE_TRY(mRenderer->allocateResource(context11, pointSamplerDesc, &mPointSampler));
mPointSampler.setInternalName("Blit11PointSampler");
D3D11_SAMPLER_DESC linearSamplerDesc;
linearSamplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
linearSamplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
linearSamplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
linearSamplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
linearSamplerDesc.MipLODBias = 0.0f;
linearSamplerDesc.MaxAnisotropy = 0;
linearSamplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
linearSamplerDesc.BorderColor[0] = 0.0f;
linearSamplerDesc.BorderColor[1] = 0.0f;
linearSamplerDesc.BorderColor[2] = 0.0f;
linearSamplerDesc.BorderColor[3] = 0.0f;
linearSamplerDesc.MinLOD = 0.0f;
linearSamplerDesc.MaxLOD = FLT_MAX;
ANGLE_TRY(mRenderer->allocateResource(context11, linearSamplerDesc, &mLinearSampler));
mLinearSampler.setInternalName("Blit11LinearSampler");
// Use a rasterizer state that will not cull so that inverted quads will not be culled
D3D11_RASTERIZER_DESC rasterDesc;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.CullMode = D3D11_CULL_NONE;
rasterDesc.FrontCounterClockwise = FALSE;
rasterDesc.DepthBias = 0;
rasterDesc.SlopeScaledDepthBias = 0.0f;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = TRUE;
rasterDesc.MultisampleEnable = FALSE;
rasterDesc.AntialiasedLineEnable = FALSE;
rasterDesc.ScissorEnable = TRUE;
ANGLE_TRY(mRenderer->allocateResource(context11, rasterDesc, &mScissorEnabledRasterizerState));
mScissorEnabledRasterizerState.setInternalName("Blit11ScissoringRasterizerState");
rasterDesc.ScissorEnable = FALSE;
ANGLE_TRY(mRenderer->allocateResource(context11, rasterDesc, &mScissorDisabledRasterizerState));
mScissorDisabledRasterizerState.setInternalName("Blit11NoScissoringRasterizerState");
D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
depthStencilDesc.DepthEnable = TRUE;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
depthStencilDesc.StencilEnable = FALSE;
depthStencilDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
depthStencilDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
ANGLE_TRY(mRenderer->allocateResource(context11, depthStencilDesc, &mDepthStencilState));
mDepthStencilState.setInternalName("Blit11DepthStencilState");
D3D11_BUFFER_DESC swizzleBufferDesc;
swizzleBufferDesc.ByteWidth = sizeof(unsigned int) * 4;
swizzleBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
swizzleBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
swizzleBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
swizzleBufferDesc.MiscFlags = 0;
swizzleBufferDesc.StructureByteStride = 0;
ANGLE_TRY(mRenderer->allocateResource(context11, swizzleBufferDesc, &mSwizzleCB));
mSwizzleCB.setInternalName("Blit11SwizzleConstantBuffer");
mResourcesInitialized = true;
return angle::Result::Continue;
}
// static
Blit11::SwizzleShaderType Blit11::GetSwizzleShaderType(GLenum type,
D3D11_SRV_DIMENSION dimensionality)
{
switch (dimensionality)
{
case D3D11_SRV_DIMENSION_TEXTURE2D:
switch (type)
{
case GL_FLOAT:
return SWIZZLESHADER_2D_FLOAT;
case GL_UNSIGNED_INT:
return SWIZZLESHADER_2D_UINT;
case GL_INT:
return SWIZZLESHADER_2D_INT;
default:
UNREACHABLE();
return SWIZZLESHADER_INVALID;
}
case D3D11_SRV_DIMENSION_TEXTURECUBE:
switch (type)
{
case GL_FLOAT:
return SWIZZLESHADER_CUBE_FLOAT;
case GL_UNSIGNED_INT:
return SWIZZLESHADER_CUBE_UINT;
case GL_INT:
return SWIZZLESHADER_CUBE_INT;
default:
UNREACHABLE();
return SWIZZLESHADER_INVALID;
}
case D3D11_SRV_DIMENSION_TEXTURE3D:
switch (type)
{
case GL_FLOAT:
return SWIZZLESHADER_3D_FLOAT;
case GL_UNSIGNED_INT:
return SWIZZLESHADER_3D_UINT;
case GL_INT:
return SWIZZLESHADER_3D_INT;
default:
UNREACHABLE();
return SWIZZLESHADER_INVALID;
}
case D3D11_SRV_DIMENSION_TEXTURE2DARRAY:
switch (type)
{
case GL_FLOAT:
return SWIZZLESHADER_ARRAY_FLOAT;
case GL_UNSIGNED_INT:
return SWIZZLESHADER_ARRAY_UINT;
case GL_INT:
return SWIZZLESHADER_ARRAY_INT;
default:
UNREACHABLE();
return SWIZZLESHADER_INVALID;
}
default:
UNREACHABLE();
return SWIZZLESHADER_INVALID;
}
}
angle::Result Blit11::getShaderSupport(const gl::Context *context,
const Shader &shader,
Blit11::ShaderSupport *supportOut)
{
Context11 *context11 = GetImplAs<Context11>(context);
switch (shader.dimension)
{
case SHADER_2D:
{
ANGLE_TRY(mQuad2DIL.resolve(context11, mRenderer));
ANGLE_TRY(mQuad2DVS.resolve(context11, mRenderer));
supportOut->inputLayout = &mQuad2DIL.getObj();
supportOut->vertexShader = &mQuad2DVS.getObj();
supportOut->geometryShader = nullptr;
supportOut->vertexWriteFunction = Write2DVertices;
break;
}
case SHADER_3D:
case SHADER_2DARRAY:
{
ANGLE_TRY(mQuad3DIL.resolve(context11, mRenderer));
ANGLE_TRY(mQuad3DVS.resolve(context11, mRenderer));
ANGLE_TRY(mQuad3DGS.resolve(context11, mRenderer));
supportOut->inputLayout = &mQuad3DIL.getObj();
supportOut->vertexShader = &mQuad3DVS.getObj();
supportOut->geometryShader = &mQuad3DGS.getObj();
supportOut->vertexWriteFunction = Write3DVertices;
break;
}
default:
UNREACHABLE();
}
return angle::Result::Continue;
}
angle::Result Blit11::swizzleTexture(const gl::Context *context,
const d3d11::SharedSRV &source,
const d3d11::RenderTargetView &dest,
const gl::Extents &size,
const gl::SwizzleState &swizzleTarget)
{
ANGLE_TRY(initResources(context));
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
D3D11_SHADER_RESOURCE_VIEW_DESC sourceSRVDesc;
source.get()->GetDesc(&sourceSRVDesc);
GLenum componentType = d3d11::GetComponentType(sourceSRVDesc.Format);
if (componentType == GL_NONE)
{
// We're swizzling the depth component of a depth-stencil texture.
switch (sourceSRVDesc.Format)
{
case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
componentType = GL_UNSIGNED_NORMALIZED;
break;
case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
componentType = GL_FLOAT;
break;
default:
UNREACHABLE();
break;
}
}
GLenum shaderType = GL_NONE;
switch (componentType)
{
case GL_UNSIGNED_NORMALIZED:
case GL_SIGNED_NORMALIZED:
case GL_FLOAT:
shaderType = GL_FLOAT;
break;
case GL_INT:
shaderType = GL_INT;
break;
case GL_UNSIGNED_INT:
shaderType = GL_UNSIGNED_INT;
break;
default:
UNREACHABLE();
break;
}
const Shader *shader = nullptr;
ANGLE_TRY(getSwizzleShader(context, shaderType, sourceSRVDesc.ViewDimension, &shader));
// Set vertices
D3D11_MAPPED_SUBRESOURCE mappedResource;
ANGLE_TRY(mRenderer->mapResource(context, mVertexBuffer.get(), 0, D3D11_MAP_WRITE_DISCARD, 0,
&mappedResource));
ShaderSupport support;
ANGLE_TRY(getShaderSupport(context, *shader, &support));
UINT stride = 0;
UINT drawCount = 0;
D3D11_PRIMITIVE_TOPOLOGY topology;
gl::Box area(0, 0, 0, size.width, size.height, size.depth);
support.vertexWriteFunction(area, size, area, size, mappedResource.pData, &stride, &drawCount,
&topology);
deviceContext->Unmap(mVertexBuffer.get(), 0);
// Set constant buffer
ANGLE_TRY(mRenderer->mapResource(context, mSwizzleCB.get(), 0, D3D11_MAP_WRITE_DISCARD, 0,
&mappedResource));
unsigned int *swizzleIndices = static_cast<unsigned int *>(mappedResource.pData);
swizzleIndices[0] = GetSwizzleIndex(swizzleTarget.swizzleRed);
swizzleIndices[1] = GetSwizzleIndex(swizzleTarget.swizzleGreen);
swizzleIndices[2] = GetSwizzleIndex(swizzleTarget.swizzleBlue);
swizzleIndices[3] = GetSwizzleIndex(swizzleTarget.swizzleAlpha);
deviceContext->Unmap(mSwizzleCB.get(), 0);
StateManager11 *stateManager = mRenderer->getStateManager();
// Apply vertex buffer
stateManager->setSingleVertexBuffer(&mVertexBuffer, stride, 0);
// Apply constant buffer
stateManager->setPixelConstantBuffer(0, &mSwizzleCB);
// Apply state
stateManager->setSimpleBlendState(nullptr);
stateManager->setDepthStencilState(nullptr, 0xFFFFFFFF);
stateManager->setRasterizerState(&mScissorDisabledRasterizerState);
// Apply shaders
stateManager->setInputLayout(support.inputLayout);
stateManager->setPrimitiveTopology(topology);
stateManager->setDrawShaders(support.vertexShader, support.geometryShader,
&shader->pixelShader);
// Apply render target
stateManager->setRenderTarget(dest.get(), nullptr);
// Set the viewport
stateManager->setSimpleViewport(size);
// Apply textures and sampler
stateManager->setSimplePixelTextureAndSampler(source, mPointSampler);
// Draw the quad
deviceContext->Draw(drawCount, 0);
return angle::Result::Continue;
}
angle::Result Blit11::copyTexture(const gl::Context *context,
const d3d11::SharedSRV &source,
const gl::Box &sourceArea,
const gl::Extents &sourceSize,
GLenum sourceFormat,
const d3d11::RenderTargetView &dest,
const gl::Box &destArea,
const gl::Extents &destSize,
const gl::Rectangle *scissor,
GLenum destFormat,
GLenum destTypeForDownsampling,
GLenum filter,
bool maskOffAlpha,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha)
{
ANGLE_TRY(initResources(context));
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
// Determine if the source format is a signed integer format, the destFormat will already
// be GL_XXXX_INTEGER but it does not tell us if it is signed or unsigned.
D3D11_SHADER_RESOURCE_VIEW_DESC sourceSRVDesc;
source.get()->GetDesc(&sourceSRVDesc);
GLenum componentType = d3d11::GetComponentType(sourceSRVDesc.Format);
ASSERT(componentType != GL_NONE);
bool isSrcSigned = (componentType == GL_INT);
D3D11_RENDER_TARGET_VIEW_DESC destRTVDesc;
dest.get()->GetDesc(&destRTVDesc);
GLenum destComponentType = d3d11::GetComponentType(destRTVDesc.Format);
ASSERT(componentType != GL_NONE);
bool isDestSigned = (destComponentType == GL_INT);
ShaderDimension dimension = SHADER_INVALID;
switch (sourceSRVDesc.ViewDimension)
{
case D3D11_SRV_DIMENSION_TEXTURE2D:
dimension = SHADER_2D;
break;
case D3D11_SRV_DIMENSION_TEXTURE3D:
dimension = SHADER_3D;
break;
case D3D11_SRV_DIMENSION_TEXTURE2DARRAY:
dimension = SHADER_2DARRAY;
break;
default:
UNREACHABLE();
}
const Shader *shader = nullptr;
ANGLE_TRY(getBlitShader(context, destFormat, sourceFormat, isSrcSigned, isDestSigned,
unpackPremultiplyAlpha, unpackUnmultiplyAlpha, destTypeForDownsampling,
dimension, &shader));
ShaderSupport support;
ANGLE_TRY(getShaderSupport(context, *shader, &support));
// Set vertices
D3D11_MAPPED_SUBRESOURCE mappedResource;
ANGLE_TRY(mRenderer->mapResource(context, mVertexBuffer.get(), 0, D3D11_MAP_WRITE_DISCARD, 0,
&mappedResource));
UINT stride = 0;
UINT drawCount = 0;
D3D11_PRIMITIVE_TOPOLOGY topology;
support.vertexWriteFunction(sourceArea, sourceSize, destArea, destSize, mappedResource.pData,
&stride, &drawCount, &topology);
deviceContext->Unmap(mVertexBuffer.get(), 0);
StateManager11 *stateManager = mRenderer->getStateManager();
// Apply vertex buffer
stateManager->setSingleVertexBuffer(&mVertexBuffer, stride, 0);
// Apply state
if (maskOffAlpha)
{
ANGLE_TRY(mAlphaMaskBlendState.resolve(GetImplAs<Context11>(context), mRenderer));
stateManager->setSimpleBlendState(&mAlphaMaskBlendState.getObj());
}