forked from Nominom/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtcBlockEncoder.cs
More file actions
136 lines (110 loc) · 3.35 KB
/
Copy pathAtcBlockEncoder.cs
File metadata and controls
136 lines (110 loc) · 3.35 KB
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
using BCnEncoder.Shared;
using BCnEncoder.Shared.ImageFiles;
namespace BCnEncoder.Encoder
{
internal unsafe class AtcBlockEncoder : BaseBcBlockEncoder<AtcBlock, RawBlock4X4Rgba32>
{
private readonly Bc1BlockEncoder bc1BlockEncoder;
public AtcBlockEncoder()
{
bc1BlockEncoder = new Bc1BlockEncoder();
}
public override AtcBlock EncodeBlock(RawBlock4X4Rgba32 block, CompressionQuality quality)
{
var atcBlock = new AtcBlock();
// EncodeBlock with BC1 first
var bc1Block = bc1BlockEncoder.EncodeBlock(block, quality);
// Atc specific modifications to BC1
// According to http://www.guildsoftware.com/papers/2012.Converting.DXTC.to.Atc.pdf
// Change color0 from rgb565 to rgb555 with method 0
atcBlock.color0 = new ColorRgb555(bc1Block.color0.R, bc1Block.color0.G, bc1Block.color0.B);
atcBlock.color1 = bc1Block.color1;
// Remap color indices from BC1 to ATC
var remap = stackalloc byte[] { 0, 3, 1, 2 };
for (var i = 0; i < 16; i++)
{
atcBlock[i] = remap[bc1Block[i]];
}
return atcBlock;
}
public override GlInternalFormat GetInternalFormat()
{
return GlInternalFormat.GlCompressedRgbAtc;
}
public override GlFormat GetBaseInternalFormat()
{
return GlFormat.GlRgb;
}
public override DxgiFormat GetDxgiFormat()
{
return DxgiFormat.DxgiFormatAtcExt;
}
}
internal class AtcExplicitAlphaBlockEncoder : BaseBcBlockEncoder<AtcExplicitAlphaBlock, RawBlock4X4Rgba32>
{
private readonly AtcBlockEncoder atcBlockEncoder;
public AtcExplicitAlphaBlockEncoder()
{
atcBlockEncoder = new AtcBlockEncoder();
}
public override AtcExplicitAlphaBlock EncodeBlock(RawBlock4X4Rgba32 block, CompressionQuality quality)
{
var atcBlock = atcBlockEncoder.EncodeBlock(block, quality);
// EncodeBlock alpha
var bc2AlphaBlock = new Bc2AlphaBlock();
for (var i = 0; i < 16; i++)
{
bc2AlphaBlock.SetAlpha(i, block[i].a);
}
return new AtcExplicitAlphaBlock
{
alphas = bc2AlphaBlock,
colors = atcBlock
};
}
public override GlInternalFormat GetInternalFormat()
{
return GlInternalFormat.GlCompressedRgbaAtcExplicitAlpha;
}
public override GlFormat GetBaseInternalFormat()
{
return GlFormat.GlRgba;
}
public override DxgiFormat GetDxgiFormat()
{
return DxgiFormat.DxgiFormatAtcExplicitAlphaExt;
}
}
internal class AtcInterpolatedAlphaBlockEncoder : BaseBcBlockEncoder<AtcInterpolatedAlphaBlock, RawBlock4X4Rgba32>
{
private readonly Bc4ComponentBlockEncoder bc4BlockEncoder;
private readonly AtcBlockEncoder atcBlockEncoder;
public AtcInterpolatedAlphaBlockEncoder()
{
bc4BlockEncoder = new Bc4ComponentBlockEncoder(ColorComponent.A);
atcBlockEncoder = new AtcBlockEncoder();
}
public override AtcInterpolatedAlphaBlock EncodeBlock(RawBlock4X4Rgba32 block, CompressionQuality quality)
{
var bc4Block = bc4BlockEncoder.EncodeBlock(block, quality);
var atcBlock = atcBlockEncoder.EncodeBlock(block, quality);
return new AtcInterpolatedAlphaBlock
{
alphas = bc4Block,
colors = atcBlock
};
}
public override GlInternalFormat GetInternalFormat()
{
return GlInternalFormat.GlCompressedRgbaAtcInterpolatedAlpha;
}
public override GlFormat GetBaseInternalFormat()
{
return GlFormat.GlRgba;
}
public override DxgiFormat GetDxgiFormat()
{
return DxgiFormat.DxgiFormatAtcInterpolatedAlphaExt;
}
}
}