ASTCDecoder.cs 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. namespace Ryujinx.Graphics.Texture
  7. {
  8. public class ASTCDecoderException : Exception
  9. {
  10. public ASTCDecoderException(string ExMsg) : base(ExMsg) { }
  11. }
  12. //https://github.com/GammaUNC/FasTC/blob/master/ASTCEncoder/src/Decompressor.cpp
  13. public static class ASTCDecoder
  14. {
  15. struct TexelWeightParams
  16. {
  17. public int Width;
  18. public int Height;
  19. public bool DualPlane;
  20. public int MaxWeight;
  21. public bool Error;
  22. public bool VoidExtentLDR;
  23. public bool VoidExtentHDR;
  24. public int GetPackedBitSize()
  25. {
  26. // How many indices do we have?
  27. int Indices = Height * Width;
  28. if (DualPlane)
  29. {
  30. Indices *= 2;
  31. }
  32. IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(MaxWeight);
  33. return IntEncoded.GetBitLength(Indices);
  34. }
  35. public int GetNumWeightValues()
  36. {
  37. int Ret = Width * Height;
  38. if (DualPlane)
  39. {
  40. Ret *= 2;
  41. }
  42. return Ret;
  43. }
  44. }
  45. public static byte[] DecodeToRGBA8888(
  46. byte[] InputBuffer,
  47. int BlockX,
  48. int BlockY,
  49. int BlockZ,
  50. int X,
  51. int Y,
  52. int Z)
  53. {
  54. using (MemoryStream InputStream = new MemoryStream(InputBuffer))
  55. {
  56. BinaryReader BinReader = new BinaryReader(InputStream);
  57. if (BlockX > 12 || BlockY > 12)
  58. {
  59. throw new ASTCDecoderException("Block size unsupported!");
  60. }
  61. if (BlockZ != 1 || Z != 1)
  62. {
  63. // TODO: Support 3D textures?
  64. throw new ASTCDecoderException("3D compressed textures unsupported!");
  65. }
  66. using (MemoryStream OutputStream = new MemoryStream())
  67. {
  68. int BlockIndex = 0;
  69. for (int j = 0; j < Y; j += BlockY)
  70. {
  71. for (int i = 0; i < X; i += BlockX)
  72. {
  73. int[] DecompressedData = new int[144];
  74. DecompressBlock(BinReader.ReadBytes(0x10), DecompressedData, BlockX, BlockY);
  75. int DecompressedWidth = Math.Min(BlockX, X - i);
  76. int DecompressedHeight = Math.Min(BlockY, Y - j);
  77. int BaseOffsets = (j * X + i) * 4;
  78. for (int jj = 0; jj < DecompressedHeight; jj++)
  79. {
  80. OutputStream.Seek(BaseOffsets + jj * X * 4, SeekOrigin.Begin);
  81. byte[] OutputBuffer = new byte[DecompressedData.Length * sizeof(int)];
  82. Buffer.BlockCopy(DecompressedData, 0, OutputBuffer, 0, OutputBuffer.Length);
  83. OutputStream.Write(OutputBuffer, jj * BlockX * 4, DecompressedWidth * 4);
  84. }
  85. BlockIndex++;
  86. }
  87. }
  88. return OutputStream.ToArray();
  89. }
  90. }
  91. }
  92. public static bool DecompressBlock(
  93. byte[] InputBuffer,
  94. int[] OutputBuffer,
  95. int BlockWidth,
  96. int BlockHeight)
  97. {
  98. BitArrayStream BitStream = new BitArrayStream(new BitArray(InputBuffer));
  99. TexelWeightParams TexelParams = DecodeBlockInfo(BitStream);
  100. if (TexelParams.Error)
  101. {
  102. throw new ASTCDecoderException("Invalid block mode");
  103. }
  104. if (TexelParams.VoidExtentLDR)
  105. {
  106. FillVoidExtentLDR(BitStream, OutputBuffer, BlockWidth, BlockHeight);
  107. return true;
  108. }
  109. if (TexelParams.VoidExtentHDR)
  110. {
  111. throw new ASTCDecoderException("HDR void extent blocks are unsupported!");
  112. }
  113. if (TexelParams.Width > BlockWidth)
  114. {
  115. throw new ASTCDecoderException("Texel weight grid width should be smaller than block width");
  116. }
  117. if (TexelParams.Height > BlockHeight)
  118. {
  119. throw new ASTCDecoderException("Texel weight grid height should be smaller than block height");
  120. }
  121. // Read num partitions
  122. int NumberPartitions = BitStream.ReadBits(2) + 1;
  123. Debug.Assert(NumberPartitions <= 4);
  124. if (NumberPartitions == 4 && TexelParams.DualPlane)
  125. {
  126. throw new ASTCDecoderException("Dual plane mode is incompatible with four partition blocks");
  127. }
  128. // Based on the number of partitions, read the color endpoint mode for
  129. // each partition.
  130. // Determine partitions, partition index, and color endpoint modes
  131. int PlaneIndices = -1;
  132. int PartitionIndex;
  133. uint[] ColorEndpointMode = { 0, 0, 0, 0 };
  134. BitArrayStream ColorEndpointStream = new BitArrayStream(new BitArray(16 * 8));
  135. // Read extra config data...
  136. uint BaseColorEndpointMode = 0;
  137. if (NumberPartitions == 1)
  138. {
  139. ColorEndpointMode[0] = (uint)BitStream.ReadBits(4);
  140. PartitionIndex = 0;
  141. }
  142. else
  143. {
  144. PartitionIndex = BitStream.ReadBits(10);
  145. BaseColorEndpointMode = (uint)BitStream.ReadBits(6);
  146. }
  147. uint BaseMode = (BaseColorEndpointMode & 3);
  148. // Remaining bits are color endpoint data...
  149. int NumberWeightBits = TexelParams.GetPackedBitSize();
  150. int RemainingBits = 128 - NumberWeightBits - BitStream.Position;
  151. // Consider extra bits prior to texel data...
  152. uint ExtraColorEndpointModeBits = 0;
  153. if (BaseMode != 0)
  154. {
  155. switch (NumberPartitions)
  156. {
  157. case 2: ExtraColorEndpointModeBits += 2; break;
  158. case 3: ExtraColorEndpointModeBits += 5; break;
  159. case 4: ExtraColorEndpointModeBits += 8; break;
  160. default: Debug.Assert(false); break;
  161. }
  162. }
  163. RemainingBits -= (int)ExtraColorEndpointModeBits;
  164. // Do we have a dual plane situation?
  165. int PlaneSelectorBits = 0;
  166. if (TexelParams.DualPlane)
  167. {
  168. PlaneSelectorBits = 2;
  169. }
  170. RemainingBits -= PlaneSelectorBits;
  171. // Read color data...
  172. int ColorDataBits = RemainingBits;
  173. while (RemainingBits > 0)
  174. {
  175. int NumberBits = Math.Min(RemainingBits, 8);
  176. int Bits = BitStream.ReadBits(NumberBits);
  177. ColorEndpointStream.WriteBits(Bits, NumberBits);
  178. RemainingBits -= 8;
  179. }
  180. // Read the plane selection bits
  181. PlaneIndices = BitStream.ReadBits(PlaneSelectorBits);
  182. // Read the rest of the CEM
  183. if (BaseMode != 0)
  184. {
  185. uint ExtraColorEndpointMode = (uint)BitStream.ReadBits((int)ExtraColorEndpointModeBits);
  186. uint TempColorEndpointMode = (ExtraColorEndpointMode << 6) | BaseColorEndpointMode;
  187. TempColorEndpointMode >>= 2;
  188. bool[] C = new bool[4];
  189. for (int i = 0; i < NumberPartitions; i++)
  190. {
  191. C[i] = (TempColorEndpointMode & 1) != 0;
  192. TempColorEndpointMode >>= 1;
  193. }
  194. byte[] M = new byte[4];
  195. for (int i = 0; i < NumberPartitions; i++)
  196. {
  197. M[i] = (byte)(TempColorEndpointMode & 3);
  198. TempColorEndpointMode >>= 2;
  199. Debug.Assert(M[i] <= 3);
  200. }
  201. for (int i = 0; i < NumberPartitions; i++)
  202. {
  203. ColorEndpointMode[i] = BaseMode;
  204. if (!(C[i])) ColorEndpointMode[i] -= 1;
  205. ColorEndpointMode[i] <<= 2;
  206. ColorEndpointMode[i] |= M[i];
  207. }
  208. }
  209. else if (NumberPartitions > 1)
  210. {
  211. uint TempColorEndpointMode = BaseColorEndpointMode >> 2;
  212. for (uint i = 0; i < NumberPartitions; i++)
  213. {
  214. ColorEndpointMode[i] = TempColorEndpointMode;
  215. }
  216. }
  217. // Make sure everything up till here is sane.
  218. for (int i = 0; i < NumberPartitions; i++)
  219. {
  220. Debug.Assert(ColorEndpointMode[i] < 16);
  221. }
  222. Debug.Assert(BitStream.Position + TexelParams.GetPackedBitSize() == 128);
  223. // Decode both color data and texel weight data
  224. int[] ColorValues = new int[32]; // Four values * two endpoints * four maximum partitions
  225. DecodeColorValues(ColorValues, ColorEndpointStream.ToByteArray(), ColorEndpointMode, NumberPartitions, ColorDataBits);
  226. ASTCPixel[][] EndPoints = new ASTCPixel[4][];
  227. EndPoints[0] = new ASTCPixel[2];
  228. EndPoints[1] = new ASTCPixel[2];
  229. EndPoints[2] = new ASTCPixel[2];
  230. EndPoints[3] = new ASTCPixel[2];
  231. int ColorValuesPosition = 0;
  232. for (int i = 0; i < NumberPartitions; i++)
  233. {
  234. ComputeEndpoints(EndPoints[i], ColorValues, ColorEndpointMode[i], ref ColorValuesPosition);
  235. }
  236. // Read the texel weight data.
  237. byte[] TexelWeightData = (byte[])InputBuffer.Clone();
  238. // Reverse everything
  239. for (int i = 0; i < 8; i++)
  240. {
  241. byte a = ReverseByte(TexelWeightData[i]);
  242. byte b = ReverseByte(TexelWeightData[15 - i]);
  243. TexelWeightData[i] = b;
  244. TexelWeightData[15 - i] = a;
  245. }
  246. // Make sure that higher non-texel bits are set to zero
  247. int ClearByteStart = (TexelParams.GetPackedBitSize() >> 3) + 1;
  248. TexelWeightData[ClearByteStart - 1] &= (byte)((1 << (TexelParams.GetPackedBitSize() % 8)) - 1);
  249. int cLen = 16 - ClearByteStart;
  250. for (int i = ClearByteStart; i < ClearByteStart + cLen; i++) TexelWeightData[i] = 0;
  251. List<IntegerEncoded> TexelWeightValues = new List<IntegerEncoded>();
  252. BitArrayStream WeightBitStream = new BitArrayStream(new BitArray(TexelWeightData));
  253. IntegerEncoded.DecodeIntegerSequence(TexelWeightValues, WeightBitStream, TexelParams.MaxWeight, TexelParams.GetNumWeightValues());
  254. // Blocks can be at most 12x12, so we can have as many as 144 weights
  255. int[][] Weights = new int[2][];
  256. Weights[0] = new int[144];
  257. Weights[1] = new int[144];
  258. UnquantizeTexelWeights(Weights, TexelWeightValues, TexelParams, BlockWidth, BlockHeight);
  259. // Now that we have endpoints and weights, we can interpolate and generate
  260. // the proper decoding...
  261. for (int j = 0; j < BlockHeight; j++)
  262. {
  263. for (int i = 0; i < BlockWidth; i++)
  264. {
  265. int Partition = Select2DPartition(PartitionIndex, i, j, NumberPartitions, ((BlockHeight * BlockWidth) < 32));
  266. Debug.Assert(Partition < NumberPartitions);
  267. ASTCPixel Pixel = new ASTCPixel(0, 0, 0, 0);
  268. for (int Component = 0; Component < 4; Component++)
  269. {
  270. int Component0 = EndPoints[Partition][0].GetComponent(Component);
  271. Component0 = BitArrayStream.Replicate(Component0, 8, 16);
  272. int Component1 = EndPoints[Partition][1].GetComponent(Component);
  273. Component1 = BitArrayStream.Replicate(Component1, 8, 16);
  274. int Plane = 0;
  275. if (TexelParams.DualPlane && (((PlaneIndices + 1) & 3) == Component))
  276. {
  277. Plane = 1;
  278. }
  279. int Weight = Weights[Plane][j * BlockWidth + i];
  280. int FinalComponent = (Component0 * (64 - Weight) + Component1 * Weight + 32) / 64;
  281. if (FinalComponent == 65535)
  282. {
  283. Pixel.SetComponent(Component, 255);
  284. }
  285. else
  286. {
  287. double FinalComponentFloat = FinalComponent;
  288. Pixel.SetComponent(Component, (int)(255.0 * (FinalComponentFloat / 65536.0) + 0.5));
  289. }
  290. }
  291. OutputBuffer[j * BlockWidth + i] = Pixel.Pack();
  292. }
  293. }
  294. return true;
  295. }
  296. private static int Select2DPartition(int Seed, int X, int Y, int PartitionCount, bool IsSmallBlock)
  297. {
  298. return SelectPartition(Seed, X, Y, 0, PartitionCount, IsSmallBlock);
  299. }
  300. private static int SelectPartition(int Seed, int X, int Y, int Z, int PartitionCount, bool IsSmallBlock)
  301. {
  302. if (PartitionCount == 1)
  303. {
  304. return 0;
  305. }
  306. if (IsSmallBlock)
  307. {
  308. X <<= 1;
  309. Y <<= 1;
  310. Z <<= 1;
  311. }
  312. Seed += (PartitionCount - 1) * 1024;
  313. int RightNum = Hash52((uint)Seed);
  314. byte Seed01 = (byte)(RightNum & 0xF);
  315. byte Seed02 = (byte)((RightNum >> 4) & 0xF);
  316. byte Seed03 = (byte)((RightNum >> 8) & 0xF);
  317. byte Seed04 = (byte)((RightNum >> 12) & 0xF);
  318. byte Seed05 = (byte)((RightNum >> 16) & 0xF);
  319. byte Seed06 = (byte)((RightNum >> 20) & 0xF);
  320. byte Seed07 = (byte)((RightNum >> 24) & 0xF);
  321. byte Seed08 = (byte)((RightNum >> 28) & 0xF);
  322. byte Seed09 = (byte)((RightNum >> 18) & 0xF);
  323. byte Seed10 = (byte)((RightNum >> 22) & 0xF);
  324. byte Seed11 = (byte)((RightNum >> 26) & 0xF);
  325. byte Seed12 = (byte)(((RightNum >> 30) | (RightNum << 2)) & 0xF);
  326. Seed01 *= Seed01; Seed02 *= Seed02;
  327. Seed03 *= Seed03; Seed04 *= Seed04;
  328. Seed05 *= Seed05; Seed06 *= Seed06;
  329. Seed07 *= Seed07; Seed08 *= Seed08;
  330. Seed09 *= Seed09; Seed10 *= Seed10;
  331. Seed11 *= Seed11; Seed12 *= Seed12;
  332. int SeedHash1, SeedHash2, SeedHash3;
  333. if ((Seed & 1) != 0)
  334. {
  335. SeedHash1 = (Seed & 2) != 0 ? 4 : 5;
  336. SeedHash2 = (PartitionCount == 3) ? 6 : 5;
  337. }
  338. else
  339. {
  340. SeedHash1 = (PartitionCount == 3) ? 6 : 5;
  341. SeedHash2 = (Seed & 2) != 0 ? 4 : 5;
  342. }
  343. SeedHash3 = (Seed & 0x10) != 0 ? SeedHash1 : SeedHash2;
  344. Seed01 >>= SeedHash1; Seed02 >>= SeedHash2; Seed03 >>= SeedHash1; Seed04 >>= SeedHash2;
  345. Seed05 >>= SeedHash1; Seed06 >>= SeedHash2; Seed07 >>= SeedHash1; Seed08 >>= SeedHash2;
  346. Seed09 >>= SeedHash3; Seed10 >>= SeedHash3; Seed11 >>= SeedHash3; Seed12 >>= SeedHash3;
  347. int a = Seed01 * X + Seed02 * Y + Seed11 * Z + (RightNum >> 14);
  348. int b = Seed03 * X + Seed04 * Y + Seed12 * Z + (RightNum >> 10);
  349. int c = Seed05 * X + Seed06 * Y + Seed09 * Z + (RightNum >> 6);
  350. int d = Seed07 * X + Seed08 * Y + Seed10 * Z + (RightNum >> 2);
  351. a &= 0x3F; b &= 0x3F; c &= 0x3F; d &= 0x3F;
  352. if (PartitionCount < 4) d = 0;
  353. if (PartitionCount < 3) c = 0;
  354. if (a >= b && a >= c && a >= d) return 0;
  355. else if (b >= c && b >= d) return 1;
  356. else if (c >= d) return 2;
  357. return 3;
  358. }
  359. static int Hash52(uint Val)
  360. {
  361. Val ^= Val >> 15; Val -= Val << 17; Val += Val << 7; Val += Val << 4;
  362. Val ^= Val >> 5; Val += Val << 16; Val ^= Val >> 7; Val ^= Val >> 3;
  363. Val ^= Val << 6; Val ^= Val >> 17;
  364. return (int)Val;
  365. }
  366. static void UnquantizeTexelWeights(
  367. int[][] OutputBuffer,
  368. List<IntegerEncoded> Weights,
  369. TexelWeightParams TexelParams,
  370. int BlockWidth,
  371. int BlockHeight)
  372. {
  373. int WeightIndices = 0;
  374. int[][] Unquantized = new int[2][];
  375. Unquantized[0] = new int[144];
  376. Unquantized[1] = new int[144];
  377. for (int i = 0; i < Weights.Count; i++)
  378. {
  379. Unquantized[0][WeightIndices] = UnquantizeTexelWeight(Weights[i]);
  380. if (TexelParams.DualPlane)
  381. {
  382. i++;
  383. Unquantized[1][WeightIndices] = UnquantizeTexelWeight(Weights[i]);
  384. if (i == Weights.Count)
  385. {
  386. break;
  387. }
  388. }
  389. if (++WeightIndices >= (TexelParams.Width * TexelParams.Height)) break;
  390. }
  391. // Do infill if necessary (Section C.2.18) ...
  392. int Ds = (1024 + (BlockWidth / 2)) / (BlockWidth - 1);
  393. int Dt = (1024 + (BlockHeight / 2)) / (BlockHeight - 1);
  394. int PlaneScale = TexelParams.DualPlane ? 2 : 1;
  395. for (int Plane = 0; Plane < PlaneScale; Plane++)
  396. {
  397. for (int t = 0; t < BlockHeight; t++)
  398. {
  399. for (int s = 0; s < BlockWidth; s++)
  400. {
  401. int cs = Ds * s;
  402. int ct = Dt * t;
  403. int gs = (cs * (TexelParams.Width - 1) + 32) >> 6;
  404. int gt = (ct * (TexelParams.Height - 1) + 32) >> 6;
  405. int js = gs >> 4;
  406. int fs = gs & 0xF;
  407. int jt = gt >> 4;
  408. int ft = gt & 0x0F;
  409. int w11 = (fs * ft + 8) >> 4;
  410. int w10 = ft - w11;
  411. int w01 = fs - w11;
  412. int w00 = 16 - fs - ft + w11;
  413. int v0 = js + jt * TexelParams.Width;
  414. int p00 = 0;
  415. int p01 = 0;
  416. int p10 = 0;
  417. int p11 = 0;
  418. if (v0 < (TexelParams.Width * TexelParams.Height))
  419. {
  420. p00 = Unquantized[Plane][v0];
  421. }
  422. if (v0 + 1 < (TexelParams.Width * TexelParams.Height))
  423. {
  424. p01 = Unquantized[Plane][v0 + 1];
  425. }
  426. if (v0 + TexelParams.Width < (TexelParams.Width * TexelParams.Height))
  427. {
  428. p10 = Unquantized[Plane][v0 + TexelParams.Width];
  429. }
  430. if (v0 + TexelParams.Width + 1 < (TexelParams.Width * TexelParams.Height))
  431. {
  432. p11 = Unquantized[Plane][v0 + TexelParams.Width + 1];
  433. }
  434. OutputBuffer[Plane][t * BlockWidth + s] = (p00 * w00 + p01 * w01 + p10 * w10 + p11 * w11 + 8) >> 4;
  435. }
  436. }
  437. }
  438. }
  439. static int UnquantizeTexelWeight(IntegerEncoded IntEncoded)
  440. {
  441. int BitValue = IntEncoded.BitValue;
  442. int BitLength = IntEncoded.NumberBits;
  443. int A = BitArrayStream.Replicate(BitValue & 1, 1, 7);
  444. int B = 0, C = 0, D = 0;
  445. int Result = 0;
  446. switch (IntEncoded.GetEncoding())
  447. {
  448. case IntegerEncoded.EIntegerEncoding.JustBits:
  449. Result = BitArrayStream.Replicate(BitValue, BitLength, 6);
  450. break;
  451. case IntegerEncoded.EIntegerEncoding.Trit:
  452. {
  453. D = IntEncoded.TritValue;
  454. Debug.Assert(D < 3);
  455. switch (BitLength)
  456. {
  457. case 0:
  458. {
  459. int[] Results = { 0, 32, 63 };
  460. Result = Results[D];
  461. break;
  462. }
  463. case 1:
  464. {
  465. C = 50;
  466. break;
  467. }
  468. case 2:
  469. {
  470. C = 23;
  471. int b = (BitValue >> 1) & 1;
  472. B = (b << 6) | (b << 2) | b;
  473. break;
  474. }
  475. case 3:
  476. {
  477. C = 11;
  478. int cb = (BitValue >> 1) & 3;
  479. B = (cb << 5) | cb;
  480. break;
  481. }
  482. default:
  483. throw new ASTCDecoderException("Invalid trit encoding for texel weight");
  484. }
  485. break;
  486. }
  487. case IntegerEncoded.EIntegerEncoding.Quint:
  488. {
  489. D = IntEncoded.QuintValue;
  490. Debug.Assert(D < 5);
  491. switch (BitLength)
  492. {
  493. case 0:
  494. {
  495. int[] Results = { 0, 16, 32, 47, 63 };
  496. Result = Results[D];
  497. break;
  498. }
  499. case 1:
  500. {
  501. C = 28;
  502. break;
  503. }
  504. case 2:
  505. {
  506. C = 13;
  507. int b = (BitValue >> 1) & 1;
  508. B = (b << 6) | (b << 1);
  509. break;
  510. }
  511. default:
  512. throw new ASTCDecoderException("Invalid quint encoding for texel weight");
  513. }
  514. break;
  515. }
  516. }
  517. if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits && BitLength > 0)
  518. {
  519. // Decode the value...
  520. Result = D * C + B;
  521. Result ^= A;
  522. Result = (A & 0x20) | (Result >> 2);
  523. }
  524. Debug.Assert(Result < 64);
  525. // Change from [0,63] to [0,64]
  526. if (Result > 32)
  527. {
  528. Result += 1;
  529. }
  530. return Result;
  531. }
  532. static byte ReverseByte(byte b)
  533. {
  534. // Taken from http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64Bits
  535. return (byte)((((b) * 0x80200802L) & 0x0884422110L) * 0x0101010101L >> 32);
  536. }
  537. static uint[] ReadUintColorValues(int Number, int[] ColorValues, ref int ColorValuesPosition)
  538. {
  539. uint[] Ret = new uint[Number];
  540. for (int i = 0; i < Number; i++)
  541. {
  542. Ret[i] = (uint)ColorValues[ColorValuesPosition++];
  543. }
  544. return Ret;
  545. }
  546. static int[] ReadIntColorValues(int Number, int[] ColorValues, ref int ColorValuesPosition)
  547. {
  548. int[] Ret = new int[Number];
  549. for (int i = 0; i < Number; i++)
  550. {
  551. Ret[i] = ColorValues[ColorValuesPosition++];
  552. }
  553. return Ret;
  554. }
  555. static void ComputeEndpoints(
  556. ASTCPixel[] EndPoints,
  557. int[] ColorValues,
  558. uint ColorEndpointMode,
  559. ref int ColorValuesPosition)
  560. {
  561. switch (ColorEndpointMode)
  562. {
  563. case 0:
  564. {
  565. uint[] Val = ReadUintColorValues(2, ColorValues, ref ColorValuesPosition);
  566. EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[0], (short)Val[0]);
  567. EndPoints[1] = new ASTCPixel(0xFF, (short)Val[1], (short)Val[1], (short)Val[1]);
  568. break;
  569. }
  570. case 1:
  571. {
  572. uint[] Val = ReadUintColorValues(2, ColorValues, ref ColorValuesPosition);
  573. int L0 = (int)((Val[0] >> 2) | (Val[1] & 0xC0));
  574. int L1 = (int)Math.Max(L0 + (Val[1] & 0x3F), 0xFFU);
  575. EndPoints[0] = new ASTCPixel(0xFF, (short)L0, (short)L0, (short)L0);
  576. EndPoints[1] = new ASTCPixel(0xFF, (short)L1, (short)L1, (short)L1);
  577. break;
  578. }
  579. case 4:
  580. {
  581. uint[] Val = ReadUintColorValues(4, ColorValues, ref ColorValuesPosition);
  582. EndPoints[0] = new ASTCPixel((short)Val[2], (short)Val[0], (short)Val[0], (short)Val[0]);
  583. EndPoints[1] = new ASTCPixel((short)Val[3], (short)Val[1], (short)Val[1], (short)Val[1]);
  584. break;
  585. }
  586. case 5:
  587. {
  588. int[] Val = ReadIntColorValues(4, ColorValues, ref ColorValuesPosition);
  589. BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]);
  590. BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]);
  591. EndPoints[0] = new ASTCPixel((short)Val[2], (short)Val[0], (short)Val[0], (short)Val[0]);
  592. EndPoints[1] = new ASTCPixel((short)(Val[2] + Val[3]), (short)(Val[0] + Val[1]), (short)(Val[0] + Val[1]), (short)(Val[0] + Val[1]));
  593. EndPoints[0].ClampByte();
  594. EndPoints[1].ClampByte();
  595. break;
  596. }
  597. case 6:
  598. {
  599. uint[] Val = ReadUintColorValues(4, ColorValues, ref ColorValuesPosition);
  600. EndPoints[0] = new ASTCPixel(0xFF, (short)(Val[0] * Val[3] >> 8), (short)(Val[1] * Val[3] >> 8), (short)(Val[2] * Val[3] >> 8));
  601. EndPoints[1] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[1], (short)Val[2]);
  602. break;
  603. }
  604. case 8:
  605. {
  606. uint[] Val = ReadUintColorValues(6, ColorValues, ref ColorValuesPosition);
  607. if (Val[1] + Val[3] + Val[5] >= Val[0] + Val[2] + Val[4])
  608. {
  609. EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]);
  610. EndPoints[1] = new ASTCPixel(0xFF, (short)Val[1], (short)Val[3], (short)Val[5]);
  611. }
  612. else
  613. {
  614. EndPoints[0] = ASTCPixel.BlueContract(0xFF, (short)Val[1], (short)Val[3], (short)Val[5]);
  615. EndPoints[1] = ASTCPixel.BlueContract(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]);
  616. }
  617. break;
  618. }
  619. case 9:
  620. {
  621. int[] Val = ReadIntColorValues(6, ColorValues, ref ColorValuesPosition);
  622. BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]);
  623. BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]);
  624. BitArrayStream.BitTransferSigned(ref Val[5], ref Val[4]);
  625. if (Val[1] + Val[3] + Val[5] >= 0)
  626. {
  627. EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]);
  628. EndPoints[1] = new ASTCPixel(0xFF, (short)(Val[0] + Val[1]), (short)(Val[2] + Val[3]), (short)(Val[4] + Val[5]));
  629. }
  630. else
  631. {
  632. EndPoints[0] = ASTCPixel.BlueContract(0xFF, Val[0] + Val[1], Val[2] + Val[3], Val[4] + Val[5]);
  633. EndPoints[1] = ASTCPixel.BlueContract(0xFF, Val[0], Val[2], Val[4]);
  634. }
  635. EndPoints[0].ClampByte();
  636. EndPoints[1].ClampByte();
  637. break;
  638. }
  639. case 10:
  640. {
  641. uint[] Val = ReadUintColorValues(6, ColorValues, ref ColorValuesPosition);
  642. EndPoints[0] = new ASTCPixel((short)Val[4], (short)(Val[0] * Val[3] >> 8), (short)(Val[1] * Val[3] >> 8), (short)(Val[2] * Val[3] >> 8));
  643. EndPoints[1] = new ASTCPixel((short)Val[5], (short)Val[0], (short)Val[1], (short)Val[2]);
  644. break;
  645. }
  646. case 12:
  647. {
  648. uint[] Val = ReadUintColorValues(8, ColorValues, ref ColorValuesPosition);
  649. if (Val[1] + Val[3] + Val[5] >= Val[0] + Val[2] + Val[4])
  650. {
  651. EndPoints[0] = new ASTCPixel((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]);
  652. EndPoints[1] = new ASTCPixel((short)Val[7], (short)Val[1], (short)Val[3], (short)Val[5]);
  653. }
  654. else
  655. {
  656. EndPoints[0] = ASTCPixel.BlueContract((short)Val[7], (short)Val[1], (short)Val[3], (short)Val[5]);
  657. EndPoints[1] = ASTCPixel.BlueContract((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]);
  658. }
  659. break;
  660. }
  661. case 13:
  662. {
  663. int[] Val = ReadIntColorValues(8, ColorValues, ref ColorValuesPosition);
  664. BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]);
  665. BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]);
  666. BitArrayStream.BitTransferSigned(ref Val[5], ref Val[4]);
  667. BitArrayStream.BitTransferSigned(ref Val[7], ref Val[6]);
  668. if (Val[1] + Val[3] + Val[5] >= 0)
  669. {
  670. EndPoints[0] = new ASTCPixel((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]);
  671. EndPoints[1] = new ASTCPixel((short)(Val[7] + Val[6]), (short)(Val[0] + Val[1]), (short)(Val[2] + Val[3]), (short)(Val[4] + Val[5]));
  672. }
  673. else
  674. {
  675. EndPoints[0] = ASTCPixel.BlueContract(Val[6] + Val[7], Val[0] + Val[1], Val[2] + Val[3], Val[4] + Val[5]);
  676. EndPoints[1] = ASTCPixel.BlueContract(Val[6], Val[0], Val[2], Val[4]);
  677. }
  678. EndPoints[0].ClampByte();
  679. EndPoints[1].ClampByte();
  680. break;
  681. }
  682. default:
  683. throw new ASTCDecoderException("Unsupported color endpoint mode (is it HDR?)");
  684. }
  685. }
  686. static void DecodeColorValues(
  687. int[] OutputValues,
  688. byte[] InputData,
  689. uint[] Modes,
  690. int NumberPartitions,
  691. int NumberBitsForColorData)
  692. {
  693. // First figure out how many color values we have
  694. int NumberValues = 0;
  695. for (int i = 0; i < NumberPartitions; i++)
  696. {
  697. NumberValues += (int)((Modes[i] >> 2) + 1) << 1;
  698. }
  699. // Then based on the number of values and the remaining number of bits,
  700. // figure out the max value for each of them...
  701. int Range = 256;
  702. while (--Range > 0)
  703. {
  704. IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(Range);
  705. int BitLength = IntEncoded.GetBitLength(NumberValues);
  706. if (BitLength <= NumberBitsForColorData)
  707. {
  708. // Find the smallest possible range that matches the given encoding
  709. while (--Range > 0)
  710. {
  711. IntegerEncoded NewIntEncoded = IntegerEncoded.CreateEncoding(Range);
  712. if (!NewIntEncoded.MatchesEncoding(IntEncoded))
  713. {
  714. break;
  715. }
  716. }
  717. // Return to last matching range.
  718. Range++;
  719. break;
  720. }
  721. }
  722. // We now have enough to decode our integer sequence.
  723. List<IntegerEncoded> IntegerEncodedSequence = new List<IntegerEncoded>();
  724. BitArrayStream ColorBitStream = new BitArrayStream(new BitArray(InputData));
  725. IntegerEncoded.DecodeIntegerSequence(IntegerEncodedSequence, ColorBitStream, Range, NumberValues);
  726. // Once we have the decoded values, we need to dequantize them to the 0-255 range
  727. // This procedure is outlined in ASTC spec C.2.13
  728. int OutputIndices = 0;
  729. foreach (IntegerEncoded IntEncoded in IntegerEncodedSequence)
  730. {
  731. int BitLength = IntEncoded.NumberBits;
  732. int BitValue = IntEncoded.BitValue;
  733. Debug.Assert(BitLength >= 1);
  734. int A = 0, B = 0, C = 0, D = 0;
  735. // A is just the lsb replicated 9 times.
  736. A = BitArrayStream.Replicate(BitValue & 1, 1, 9);
  737. switch (IntEncoded.GetEncoding())
  738. {
  739. case IntegerEncoded.EIntegerEncoding.JustBits:
  740. {
  741. OutputValues[OutputIndices++] = BitArrayStream.Replicate(BitValue, BitLength, 8);
  742. break;
  743. }
  744. case IntegerEncoded.EIntegerEncoding.Trit:
  745. {
  746. D = IntEncoded.TritValue;
  747. switch (BitLength)
  748. {
  749. case 1:
  750. {
  751. C = 204;
  752. break;
  753. }
  754. case 2:
  755. {
  756. C = 93;
  757. // B = b000b0bb0
  758. int b = (BitValue >> 1) & 1;
  759. B = (b << 8) | (b << 4) | (b << 2) | (b << 1);
  760. break;
  761. }
  762. case 3:
  763. {
  764. C = 44;
  765. // B = cb000cbcb
  766. int cb = (BitValue >> 1) & 3;
  767. B = (cb << 7) | (cb << 2) | cb;
  768. break;
  769. }
  770. case 4:
  771. {
  772. C = 22;
  773. // B = dcb000dcb
  774. int dcb = (BitValue >> 1) & 7;
  775. B = (dcb << 6) | dcb;
  776. break;
  777. }
  778. case 5:
  779. {
  780. C = 11;
  781. // B = edcb000ed
  782. int edcb = (BitValue >> 1) & 0xF;
  783. B = (edcb << 5) | (edcb >> 2);
  784. break;
  785. }
  786. case 6:
  787. {
  788. C = 5;
  789. // B = fedcb000f
  790. int fedcb = (BitValue >> 1) & 0x1F;
  791. B = (fedcb << 4) | (fedcb >> 4);
  792. break;
  793. }
  794. default:
  795. throw new ASTCDecoderException("Unsupported trit encoding for color values!");
  796. }
  797. break;
  798. }
  799. case IntegerEncoded.EIntegerEncoding.Quint:
  800. {
  801. D = IntEncoded.QuintValue;
  802. switch (BitLength)
  803. {
  804. case 1:
  805. {
  806. C = 113;
  807. break;
  808. }
  809. case 2:
  810. {
  811. C = 54;
  812. // B = b0000bb00
  813. int b = (BitValue >> 1) & 1;
  814. B = (b << 8) | (b << 3) | (b << 2);
  815. break;
  816. }
  817. case 3:
  818. {
  819. C = 26;
  820. // B = cb0000cbc
  821. int cb = (BitValue >> 1) & 3;
  822. B = (cb << 7) | (cb << 1) | (cb >> 1);
  823. break;
  824. }
  825. case 4:
  826. {
  827. C = 13;
  828. // B = dcb0000dc
  829. int dcb = (BitValue >> 1) & 7;
  830. B = (dcb << 6) | (dcb >> 1);
  831. break;
  832. }
  833. case 5:
  834. {
  835. C = 6;
  836. // B = edcb0000e
  837. int edcb = (BitValue >> 1) & 0xF;
  838. B = (edcb << 5) | (edcb >> 3);
  839. break;
  840. }
  841. default:
  842. throw new ASTCDecoderException("Unsupported quint encoding for color values!");
  843. }
  844. break;
  845. }
  846. }
  847. if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits)
  848. {
  849. int T = D * C + B;
  850. T ^= A;
  851. T = (A & 0x80) | (T >> 2);
  852. OutputValues[OutputIndices++] = T;
  853. }
  854. }
  855. // Make sure that each of our values is in the proper range...
  856. for (int i = 0; i < NumberValues; i++)
  857. {
  858. Debug.Assert(OutputValues[i] <= 255);
  859. }
  860. }
  861. static void FillVoidExtentLDR(BitArrayStream BitStream, int[] OutputBuffer, int BlockWidth, int BlockHeight)
  862. {
  863. // Don't actually care about the void extent, just read the bits...
  864. for (int i = 0; i < 4; ++i)
  865. {
  866. BitStream.ReadBits(13);
  867. }
  868. // Decode the RGBA components and renormalize them to the range [0, 255]
  869. ushort R = (ushort)BitStream.ReadBits(16);
  870. ushort G = (ushort)BitStream.ReadBits(16);
  871. ushort B = (ushort)BitStream.ReadBits(16);
  872. ushort A = (ushort)BitStream.ReadBits(16);
  873. int RGBA = (R >> 8) | (G & 0xFF00) | ((B) & 0xFF00) << 8 | ((A) & 0xFF00) << 16;
  874. for (int j = 0; j < BlockHeight; j++)
  875. {
  876. for (int i = 0; i < BlockWidth; i++)
  877. {
  878. OutputBuffer[j * BlockWidth + i] = RGBA;
  879. }
  880. }
  881. }
  882. static TexelWeightParams DecodeBlockInfo(BitArrayStream BitStream)
  883. {
  884. TexelWeightParams TexelParams = new TexelWeightParams();
  885. // Read the entire block mode all at once
  886. ushort ModeBits = (ushort)BitStream.ReadBits(11);
  887. // Does this match the void extent block mode?
  888. if ((ModeBits & 0x01FF) == 0x1FC)
  889. {
  890. if ((ModeBits & 0x200) != 0)
  891. {
  892. TexelParams.VoidExtentHDR = true;
  893. }
  894. else
  895. {
  896. TexelParams.VoidExtentLDR = true;
  897. }
  898. // Next two bits must be one.
  899. if ((ModeBits & 0x400) == 0 || BitStream.ReadBits(1) == 0)
  900. {
  901. TexelParams.Error = true;
  902. }
  903. return TexelParams;
  904. }
  905. // First check if the last four bits are zero
  906. if ((ModeBits & 0xF) == 0)
  907. {
  908. TexelParams.Error = true;
  909. return TexelParams;
  910. }
  911. // If the last two bits are zero, then if bits
  912. // [6-8] are all ones, this is also reserved.
  913. if ((ModeBits & 0x3) == 0 && (ModeBits & 0x1C0) == 0x1C0)
  914. {
  915. TexelParams.Error = true;
  916. return TexelParams;
  917. }
  918. // Otherwise, there is no error... Figure out the layout
  919. // of the block mode. Layout is determined by a number
  920. // between 0 and 9 corresponding to table C.2.8 of the
  921. // ASTC spec.
  922. int Layout = 0;
  923. if ((ModeBits & 0x1) != 0 || (ModeBits & 0x2) != 0)
  924. {
  925. // layout is in [0-4]
  926. if ((ModeBits & 0x8) != 0)
  927. {
  928. // layout is in [2-4]
  929. if ((ModeBits & 0x4) != 0)
  930. {
  931. // layout is in [3-4]
  932. if ((ModeBits & 0x100) != 0)
  933. {
  934. Layout = 4;
  935. }
  936. else
  937. {
  938. Layout = 3;
  939. }
  940. }
  941. else
  942. {
  943. Layout = 2;
  944. }
  945. }
  946. else
  947. {
  948. // layout is in [0-1]
  949. if ((ModeBits & 0x4) != 0)
  950. {
  951. Layout = 1;
  952. }
  953. else
  954. {
  955. Layout = 0;
  956. }
  957. }
  958. }
  959. else
  960. {
  961. // layout is in [5-9]
  962. if ((ModeBits & 0x100) != 0)
  963. {
  964. // layout is in [7-9]
  965. if ((ModeBits & 0x80) != 0)
  966. {
  967. // layout is in [7-8]
  968. Debug.Assert((ModeBits & 0x40) == 0);
  969. if ((ModeBits & 0x20) != 0)
  970. {
  971. Layout = 8;
  972. }
  973. else
  974. {
  975. Layout = 7;
  976. }
  977. }
  978. else
  979. {
  980. Layout = 9;
  981. }
  982. }
  983. else
  984. {
  985. // layout is in [5-6]
  986. if ((ModeBits & 0x80) != 0)
  987. {
  988. Layout = 6;
  989. }
  990. else
  991. {
  992. Layout = 5;
  993. }
  994. }
  995. }
  996. Debug.Assert(Layout < 10);
  997. // Determine R
  998. int R = (ModeBits >> 4) & 1;
  999. if (Layout < 5)
  1000. {
  1001. R |= (ModeBits & 0x3) << 1;
  1002. }
  1003. else
  1004. {
  1005. R |= (ModeBits & 0xC) >> 1;
  1006. }
  1007. Debug.Assert(2 <= R && R <= 7);
  1008. // Determine width & height
  1009. switch (Layout)
  1010. {
  1011. case 0:
  1012. {
  1013. int A = (ModeBits >> 5) & 0x3;
  1014. int B = (ModeBits >> 7) & 0x3;
  1015. TexelParams.Width = B + 4;
  1016. TexelParams.Height = A + 2;
  1017. break;
  1018. }
  1019. case 1:
  1020. {
  1021. int A = (ModeBits >> 5) & 0x3;
  1022. int B = (ModeBits >> 7) & 0x3;
  1023. TexelParams.Width = B + 8;
  1024. TexelParams.Height = A + 2;
  1025. break;
  1026. }
  1027. case 2:
  1028. {
  1029. int A = (ModeBits >> 5) & 0x3;
  1030. int B = (ModeBits >> 7) & 0x3;
  1031. TexelParams.Width = A + 2;
  1032. TexelParams.Height = B + 8;
  1033. break;
  1034. }
  1035. case 3:
  1036. {
  1037. int A = (ModeBits >> 5) & 0x3;
  1038. int B = (ModeBits >> 7) & 0x1;
  1039. TexelParams.Width = A + 2;
  1040. TexelParams.Height = B + 6;
  1041. break;
  1042. }
  1043. case 4:
  1044. {
  1045. int A = (ModeBits >> 5) & 0x3;
  1046. int B = (ModeBits >> 7) & 0x1;
  1047. TexelParams.Width = B + 2;
  1048. TexelParams.Height = A + 2;
  1049. break;
  1050. }
  1051. case 5:
  1052. {
  1053. int A = (ModeBits >> 5) & 0x3;
  1054. TexelParams.Width = 12;
  1055. TexelParams.Height = A + 2;
  1056. break;
  1057. }
  1058. case 6:
  1059. {
  1060. int A = (ModeBits >> 5) & 0x3;
  1061. TexelParams.Width = A + 2;
  1062. TexelParams.Height = 12;
  1063. break;
  1064. }
  1065. case 7:
  1066. {
  1067. TexelParams.Width = 6;
  1068. TexelParams.Height = 10;
  1069. break;
  1070. }
  1071. case 8:
  1072. {
  1073. TexelParams.Width = 10;
  1074. TexelParams.Height = 6;
  1075. break;
  1076. }
  1077. case 9:
  1078. {
  1079. int A = (ModeBits >> 5) & 0x3;
  1080. int B = (ModeBits >> 9) & 0x3;
  1081. TexelParams.Width = A + 6;
  1082. TexelParams.Height = B + 6;
  1083. break;
  1084. }
  1085. default:
  1086. //Don't know this layout...
  1087. TexelParams.Error = true;
  1088. break;
  1089. }
  1090. // Determine whether or not we're using dual planes
  1091. // and/or high precision layouts.
  1092. bool D = ((Layout != 9) && ((ModeBits & 0x400) != 0));
  1093. bool H = (Layout != 9) && ((ModeBits & 0x200) != 0);
  1094. if (H)
  1095. {
  1096. int[] MaxWeights = { 9, 11, 15, 19, 23, 31 };
  1097. TexelParams.MaxWeight = MaxWeights[R - 2];
  1098. }
  1099. else
  1100. {
  1101. int[] MaxWeights = { 1, 2, 3, 4, 5, 7 };
  1102. TexelParams.MaxWeight = MaxWeights[R - 2];
  1103. }
  1104. TexelParams.DualPlane = D;
  1105. return TexelParams;
  1106. }
  1107. }
  1108. }