Skip to content

Commit

Permalink
Sped up ETC1 parsing by optimizing some array accesses.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Oct 23, 2023
1 parent 7c4b9a3 commit 4aed28f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion FinModelUtility/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ csharp_new_line_before_catch = false
csharp_new_line_before_else = false
csharp_new_line_before_finally = false
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_open_brace = none
csharp_new_line_between_query_expression_clauses = true

Expand Down
24 changes: 12 additions & 12 deletions FinModelUtility/Fin/Fin/src/image/io/tile/Etc1TileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace fin.image.io.tile {
/// Stolen from:
/// https://github.com/xdanieldzd/Scarlet/blob/master/Scarlet/Drawing/Compression/ETC1.cs
/// </summary>
public class Etc1TileReader : ITileReader<Rgba32> {
public readonly struct Etc1TileReader : ITileReader<Rgba32> {
private readonly bool hasAlpha_;

/* Specs: https://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt */
Expand All @@ -22,15 +22,15 @@ public class Etc1TileReader : ITileReader<Rgba32> {
* https://github.com/Gericom/EveryFileExplorer/blob/master/3DS/GPU/Textures.cs
* https://github.com/gdkchan/Ohana3DS-Rebirth/blob/master/Ohana3DS%20Rebirth/Ohana/TextureCodec.cs */

private static readonly int[,] ETC1_MODIFIER_TABLES_ = {
{ 2, 8, -2, -8 },
{ 5, 17, -5, -17 },
{ 9, 29, -9, -29 },
{ 13, 42, -13, -42 },
{ 18, 60, -18, -60 },
{ 24, 80, -24, -80 },
{ 33, 106, -33, -106 },
{ 47, 183, -47, -183 }
private static readonly int[] ETC1_MODIFIER_TABLES_ = {
2, 8, -2, -8,
5, 17, -5, -17,
9, 29, -9, -29,
13, 42, -13, -42,
18, 60, -18, -60,
24, 80, -24, -80,
33, 106, -33, -106,
47, 183, -47, -183
};

public Etc1TileReader(bool hasAlpha) {
Expand Down Expand Up @@ -147,13 +147,13 @@ private static void DecodeETC1Block_(Span<Rgb24> colors, ulong block) {

if ((flipBit == 0x01 && py < 2) || (flipBit == 0x00 && px < 2)) {
int modifier =
Etc1TileReader.ETC1_MODIFIER_TABLES_[tableIndex1, index];
Etc1TileReader.ETC1_MODIFIER_TABLES_[4 * tableIndex1 + index];
colors[indexInPart] = new Rgb24(ClampByte_(r1 + modifier),
ClampByte_(g1 + modifier),
ClampByte_(b1 + modifier));
} else {
int modifier =
Etc1TileReader.ETC1_MODIFIER_TABLES_[tableIndex2, index];
Etc1TileReader.ETC1_MODIFIER_TABLES_[4 * tableIndex2 + index];
colors[indexInPart] = new Rgb24(ClampByte_(r2 + modifier),
ClampByte_(g2 + modifier),
ClampByte_(b2 + modifier));
Expand Down

0 comments on commit 4aed28f

Please sign in to comment.