summaryrefslogtreecommitdiff
path: root/src/video_core/texture
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/texture')
-rw-r--r--src/video_core/texture/etc1.cpp122
-rw-r--r--src/video_core/texture/etc1.h16
-rw-r--r--src/video_core/texture/texture_decode.cpp227
-rw-r--r--src/video_core/texture/texture_decode.h60
4 files changed, 0 insertions, 425 deletions
diff --git a/src/video_core/texture/etc1.cpp b/src/video_core/texture/etc1.cpp
deleted file mode 100644
index 43f7f56db..000000000
--- a/src/video_core/texture/etc1.cpp
+++ /dev/null
@@ -1,122 +0,0 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <array>
6#include "common/bit_field.h"
7#include "common/color.h"
8#include "common/common_types.h"
9#include "common/math_util.h"
10#include "common/vector_math.h"
11#include "video_core/texture/etc1.h"
12
13namespace Pica {
14namespace Texture {
15
16namespace {
17
18constexpr std::array<std::array<u8, 2>, 8> etc1_modifier_table = {{
19 {2, 8}, {5, 17}, {9, 29}, {13, 42}, {18, 60}, {24, 80}, {33, 106}, {47, 183},
20}};
21
22union ETC1Tile {
23 u64 raw;
24
25 // Each of these two is a collection of 16 bits (one per lookup value)
26 BitField<0, 16, u64> table_subindexes;
27 BitField<16, 16, u64> negation_flags;
28
29 unsigned GetTableSubIndex(unsigned index) const {
30 return (table_subindexes >> index) & 1;
31 }
32
33 bool GetNegationFlag(unsigned index) const {
34 return ((negation_flags >> index) & 1) == 1;
35 }
36
37 BitField<32, 1, u64> flip;
38 BitField<33, 1, u64> differential_mode;
39
40 BitField<34, 3, u64> table_index_2;
41 BitField<37, 3, u64> table_index_1;
42
43 union {
44 // delta value + base value
45 BitField<40, 3, s64> db;
46 BitField<43, 5, u64> b;
47
48 BitField<48, 3, s64> dg;
49 BitField<51, 5, u64> g;
50
51 BitField<56, 3, s64> dr;
52 BitField<59, 5, u64> r;
53 } differential;
54
55 union {
56 BitField<40, 4, u64> b2;
57 BitField<44, 4, u64> b1;
58
59 BitField<48, 4, u64> g2;
60 BitField<52, 4, u64> g1;
61
62 BitField<56, 4, u64> r2;
63 BitField<60, 4, u64> r1;
64 } separate;
65
66 const Math::Vec3<u8> GetRGB(unsigned int x, unsigned int y) const {
67 int texel = 4 * x + y;
68
69 if (flip)
70 std::swap(x, y);
71
72 // Lookup base value
73 Math::Vec3<int> ret;
74 if (differential_mode) {
75 ret.r() = static_cast<int>(differential.r);
76 ret.g() = static_cast<int>(differential.g);
77 ret.b() = static_cast<int>(differential.b);
78 if (x >= 2) {
79 ret.r() += static_cast<int>(differential.dr);
80 ret.g() += static_cast<int>(differential.dg);
81 ret.b() += static_cast<int>(differential.db);
82 }
83 ret.r() = Color::Convert5To8(ret.r());
84 ret.g() = Color::Convert5To8(ret.g());
85 ret.b() = Color::Convert5To8(ret.b());
86 } else {
87 if (x < 2) {
88 ret.r() = Color::Convert4To8(static_cast<u8>(separate.r1));
89 ret.g() = Color::Convert4To8(static_cast<u8>(separate.g1));
90 ret.b() = Color::Convert4To8(static_cast<u8>(separate.b1));
91 } else {
92 ret.r() = Color::Convert4To8(static_cast<u8>(separate.r2));
93 ret.g() = Color::Convert4To8(static_cast<u8>(separate.g2));
94 ret.b() = Color::Convert4To8(static_cast<u8>(separate.b2));
95 }
96 }
97
98 // Add modifier
99 unsigned table_index =
100 static_cast<int>((x < 2) ? table_index_1.Value() : table_index_2.Value());
101
102 int modifier = etc1_modifier_table[table_index][GetTableSubIndex(texel)];
103 if (GetNegationFlag(texel))
104 modifier *= -1;
105
106 ret.r() = MathUtil::Clamp(ret.r() + modifier, 0, 255);
107 ret.g() = MathUtil::Clamp(ret.g() + modifier, 0, 255);
108 ret.b() = MathUtil::Clamp(ret.b() + modifier, 0, 255);
109
110 return ret.Cast<u8>();
111 }
112};
113
114} // anonymous namespace
115
116Math::Vec3<u8> SampleETC1Subtile(u64 value, unsigned int x, unsigned int y) {
117 ETC1Tile tile{value};
118 return tile.GetRGB(x, y);
119}
120
121} // namespace Texture
122} // namespace Pica
diff --git a/src/video_core/texture/etc1.h b/src/video_core/texture/etc1.h
deleted file mode 100644
index e188b19df..000000000
--- a/src/video_core/texture/etc1.h
+++ /dev/null
@@ -1,16 +0,0 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/common_types.h"
8#include "common/vector_math.h"
9
10namespace Pica {
11namespace Texture {
12
13Math::Vec3<u8> SampleETC1Subtile(u64 value, unsigned int x, unsigned int y);
14
15} // namespace Texture
16} // namespace Pica
diff --git a/src/video_core/texture/texture_decode.cpp b/src/video_core/texture/texture_decode.cpp
deleted file mode 100644
index 0818d652c..000000000
--- a/src/video_core/texture/texture_decode.cpp
+++ /dev/null
@@ -1,227 +0,0 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/assert.h"
6#include "common/color.h"
7#include "common/logging/log.h"
8#include "common/math_util.h"
9#include "common/swap.h"
10#include "common/vector_math.h"
11#include "video_core/regs_texturing.h"
12#include "video_core/texture/etc1.h"
13#include "video_core/texture/texture_decode.h"
14#include "video_core/utils.h"
15
16using TextureFormat = Pica::TexturingRegs::TextureFormat;
17
18namespace Pica {
19namespace Texture {
20
21constexpr size_t TILE_SIZE = 8 * 8;
22constexpr size_t ETC1_SUBTILES = 2 * 2;
23
24size_t CalculateTileSize(TextureFormat format) {
25 switch (format) {
26 case TextureFormat::RGBA8:
27 return 4 * TILE_SIZE;
28
29 case TextureFormat::RGB8:
30 return 3 * TILE_SIZE;
31
32 case TextureFormat::RGB5A1:
33 case TextureFormat::RGB565:
34 case TextureFormat::RGBA4:
35 case TextureFormat::IA8:
36 case TextureFormat::RG8:
37 return 2 * TILE_SIZE;
38
39 case TextureFormat::I8:
40 case TextureFormat::A8:
41 case TextureFormat::IA4:
42 return 1 * TILE_SIZE;
43
44 case TextureFormat::I4:
45 case TextureFormat::A4:
46 return TILE_SIZE / 2;
47
48 case TextureFormat::ETC1:
49 return ETC1_SUBTILES * 8;
50
51 case TextureFormat::ETC1A4:
52 return ETC1_SUBTILES * 16;
53
54 default: // placeholder for yet unknown formats
55 UNIMPLEMENTED();
56 return 0;
57 }
58}
59
60Math::Vec4<u8> LookupTexture(const u8* source, unsigned int x, unsigned int y,
61 const TextureInfo& info, bool disable_alpha) {
62 // Coordinate in tiles
63 const unsigned int coarse_x = x / 8;
64 const unsigned int coarse_y = y / 8;
65
66 // Coordinate inside the tile
67 const unsigned int fine_x = x % 8;
68 const unsigned int fine_y = y % 8;
69
70 const u8* line = source + coarse_y * info.stride;
71 const u8* tile = line + coarse_x * CalculateTileSize(info.format);
72 return LookupTexelInTile(tile, fine_x, fine_y, info, disable_alpha);
73}
74
75Math::Vec4<u8> LookupTexelInTile(const u8* source, unsigned int x, unsigned int y,
76 const TextureInfo& info, bool disable_alpha) {
77 DEBUG_ASSERT(x < 8);
78 DEBUG_ASSERT(y < 8);
79
80 using VideoCore::MortonInterleave;
81
82 switch (info.format) {
83 case TextureFormat::RGBA8: {
84 auto res = Color::DecodeRGBA8(source + MortonInterleave(x, y) * 4);
85 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
86 }
87
88 case TextureFormat::RGB8: {
89 auto res = Color::DecodeRGB8(source + MortonInterleave(x, y) * 3);
90 return {res.r(), res.g(), res.b(), 255};
91 }
92
93 case TextureFormat::RGB5A1: {
94 auto res = Color::DecodeRGB5A1(source + MortonInterleave(x, y) * 2);
95 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
96 }
97
98 case TextureFormat::RGB565: {
99 auto res = Color::DecodeRGB565(source + MortonInterleave(x, y) * 2);
100 return {res.r(), res.g(), res.b(), 255};
101 }
102
103 case TextureFormat::RGBA4: {
104 auto res = Color::DecodeRGBA4(source + MortonInterleave(x, y) * 2);
105 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
106 }
107
108 case TextureFormat::IA8: {
109 const u8* source_ptr = source + MortonInterleave(x, y) * 2;
110
111 if (disable_alpha) {
112 // Show intensity as red, alpha as green
113 return {source_ptr[1], source_ptr[0], 0, 255};
114 } else {
115 return {source_ptr[1], source_ptr[1], source_ptr[1], source_ptr[0]};
116 }
117 }
118
119 case TextureFormat::RG8: {
120 auto res = Color::DecodeRG8(source + MortonInterleave(x, y) * 2);
121 return {res.r(), res.g(), 0, 255};
122 }
123
124 case TextureFormat::I8: {
125 const u8* source_ptr = source + MortonInterleave(x, y);
126 return {*source_ptr, *source_ptr, *source_ptr, 255};
127 }
128
129 case TextureFormat::A8: {
130 const u8* source_ptr = source + MortonInterleave(x, y);
131
132 if (disable_alpha) {
133 return {*source_ptr, *source_ptr, *source_ptr, 255};
134 } else {
135 return {0, 0, 0, *source_ptr};
136 }
137 }
138
139 case TextureFormat::IA4: {
140 const u8* source_ptr = source + MortonInterleave(x, y);
141
142 u8 i = Color::Convert4To8(((*source_ptr) & 0xF0) >> 4);
143 u8 a = Color::Convert4To8((*source_ptr) & 0xF);
144
145 if (disable_alpha) {
146 // Show intensity as red, alpha as green
147 return {i, a, 0, 255};
148 } else {
149 return {i, i, i, a};
150 }
151 }
152
153 case TextureFormat::I4: {
154 u32 morton_offset = MortonInterleave(x, y);
155 const u8* source_ptr = source + morton_offset / 2;
156
157 u8 i = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
158 i = Color::Convert4To8(i);
159
160 return {i, i, i, 255};
161 }
162
163 case TextureFormat::A4: {
164 u32 morton_offset = MortonInterleave(x, y);
165 const u8* source_ptr = source + morton_offset / 2;
166
167 u8 a = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
168 a = Color::Convert4To8(a);
169
170 if (disable_alpha) {
171 return {a, a, a, 255};
172 } else {
173 return {0, 0, 0, a};
174 }
175 }
176
177 case TextureFormat::ETC1:
178 case TextureFormat::ETC1A4: {
179 bool has_alpha = (info.format == TextureFormat::ETC1A4);
180 size_t subtile_size = has_alpha ? 16 : 8;
181
182 // ETC1 further subdivides each 8x8 tile into four 4x4 subtiles
183 constexpr unsigned int subtile_width = 4;
184 constexpr unsigned int subtile_height = 4;
185
186 unsigned int subtile_index = (x / subtile_width) + 2 * (y / subtile_height);
187 x %= subtile_width;
188 y %= subtile_height;
189
190 const u8* subtile_ptr = source + subtile_index * subtile_size;
191
192 u8 alpha = 255;
193 if (has_alpha) {
194 u64_le packed_alpha;
195 memcpy(&packed_alpha, subtile_ptr, sizeof(u64));
196 subtile_ptr += sizeof(u64);
197
198 alpha = Color::Convert4To8((packed_alpha >> (4 * (x * subtile_width + y))) & 0xF);
199 }
200
201 u64_le subtile_data;
202 memcpy(&subtile_data, subtile_ptr, sizeof(u64));
203
204 return Math::MakeVec(SampleETC1Subtile(subtile_data, x, y),
205 disable_alpha ? (u8)255 : alpha);
206 }
207
208 default:
209 LOG_ERROR(HW_GPU, "Unknown texture format: %x", (u32)info.format);
210 DEBUG_ASSERT(false);
211 return {};
212 }
213}
214
215TextureInfo TextureInfo::FromPicaRegister(const TexturingRegs::TextureConfig& config,
216 const TexturingRegs::TextureFormat& format) {
217 TextureInfo info;
218 info.physical_address = config.GetPhysicalAddress();
219 info.width = config.width;
220 info.height = config.height;
221 info.format = format;
222 info.SetDefaultStride();
223 return info;
224}
225
226} // namespace Texture
227} // namespace Pica
diff --git a/src/video_core/texture/texture_decode.h b/src/video_core/texture/texture_decode.h
deleted file mode 100644
index 8507cfeb8..000000000
--- a/src/video_core/texture/texture_decode.h
+++ /dev/null
@@ -1,60 +0,0 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/common_types.h"
8#include "common/vector_math.h"
9#include "video_core/regs_texturing.h"
10
11namespace Pica {
12namespace Texture {
13
14/// Returns the byte size of a 8*8 tile of the specified texture format.
15size_t CalculateTileSize(TexturingRegs::TextureFormat format);
16
17struct TextureInfo {
18 PAddr physical_address;
19 unsigned int width;
20 unsigned int height;
21 ptrdiff_t stride;
22 TexturingRegs::TextureFormat format;
23
24 static TextureInfo FromPicaRegister(const TexturingRegs::TextureConfig& config,
25 const TexturingRegs::TextureFormat& format);
26
27 /// Calculates stride from format and width, assuming that the entire texture is contiguous.
28 void SetDefaultStride() {
29 stride = CalculateTileSize(format) * (width / 8);
30 }
31};
32
33/**
34 * Lookup texel located at the given coordinates and return an RGBA vector of its color.
35 * @param source Source pointer to read data from
36 * @param x,y Texture coordinates to read from
37 * @param info TextureInfo object describing the texture setup
38 * @param disable_alpha This is used for debug widgets which use this method to display textures
39 * without providing a good way to visualize alpha by themselves. If true, this will return 255 for
40 * the alpha component, and either drop the information entirely or store it in an "unused" color
41 * channel.
42 * @todo Eventually we should get rid of the disable_alpha parameter.
43 */
44Math::Vec4<u8> LookupTexture(const u8* source, unsigned int x, unsigned int y,
45 const TextureInfo& info, bool disable_alpha = false);
46
47/**
48 * Looks up a texel from a single 8x8 texture tile.
49 *
50 * @param source Pointer to the beginning of the tile.
51 * @param x, y In-tile coordinates to read from. Must be < 8.
52 * @param info TextureInfo describing the texture format.
53 * @param disable_alpha Used for debugging. Sets the result alpha to 255 and either discards the
54 * real alpha or inserts it in an otherwise unused channel.
55 */
56Math::Vec4<u8> LookupTexelInTile(const u8* source, unsigned int x, unsigned int y,
57 const TextureInfo& info, bool disable_alpha);
58
59} // namespace Texture
60} // namespace Pica