summaryrefslogtreecommitdiff
path: root/src/video_core/texture
diff options
context:
space:
mode:
authorGravatar Yuri Kunde Schlesner2017-01-05 20:11:23 -0200
committerGravatar Yuri Kunde Schlesner2017-02-04 12:31:40 -0800
commita1c9ac7845395c250a78fc8df93a9ffed29f3d5b (patch)
tree7e3768df24f82e9e732302318326113419309d33 /src/video_core/texture
parentMerge pull request #2496 from mailwl/cfg-mem (diff)
downloadyuzu-a1c9ac7845395c250a78fc8df93a9ffed29f3d5b.tar.gz
yuzu-a1c9ac7845395c250a78fc8df93a9ffed29f3d5b.tar.xz
yuzu-a1c9ac7845395c250a78fc8df93a9ffed29f3d5b.zip
VideoCore: Move LookupTexture out of debug_utils.h
Diffstat (limited to 'src/video_core/texture')
-rw-r--r--src/video_core/texture/texture_decode.cpp270
-rw-r--r--src/video_core/texture/texture_decode.h40
2 files changed, 310 insertions, 0 deletions
diff --git a/src/video_core/texture/texture_decode.cpp b/src/video_core/texture/texture_decode.cpp
new file mode 100644
index 000000000..a3b05fe81
--- /dev/null
+++ b/src/video_core/texture/texture_decode.cpp
@@ -0,0 +1,270 @@
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/assert.h"
8#include "common/color.h"
9#include "common/logging/log.h"
10#include "common/math_util.h"
11#include "common/vector_math.h"
12#include "video_core/texture/texture_decode.h"
13#include "video_core/utils.h"
14
15namespace Pica {
16namespace Texture {
17
18Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const TextureInfo& info,
19 bool disable_alpha) {
20 const unsigned int coarse_x = x & ~7;
21 const unsigned int coarse_y = y & ~7;
22
23 if (info.format != Regs::TextureFormat::ETC1 && info.format != Regs::TextureFormat::ETC1A4) {
24 // TODO(neobrain): Fix code design to unify vertical block offsets!
25 source += coarse_y * info.stride;
26 }
27
28 // TODO: Assert that width/height are multiples of block dimensions
29
30 switch (info.format) {
31 case Regs::TextureFormat::RGBA8: {
32 auto res = Color::DecodeRGBA8(source + VideoCore::GetMortonOffset(x, y, 4));
33 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
34 }
35
36 case Regs::TextureFormat::RGB8: {
37 auto res = Color::DecodeRGB8(source + VideoCore::GetMortonOffset(x, y, 3));
38 return {res.r(), res.g(), res.b(), 255};
39 }
40
41 case Regs::TextureFormat::RGB5A1: {
42 auto res = Color::DecodeRGB5A1(source + VideoCore::GetMortonOffset(x, y, 2));
43 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
44 }
45
46 case Regs::TextureFormat::RGB565: {
47 auto res = Color::DecodeRGB565(source + VideoCore::GetMortonOffset(x, y, 2));
48 return {res.r(), res.g(), res.b(), 255};
49 }
50
51 case Regs::TextureFormat::RGBA4: {
52 auto res = Color::DecodeRGBA4(source + VideoCore::GetMortonOffset(x, y, 2));
53 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
54 }
55
56 case Regs::TextureFormat::IA8: {
57 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 2);
58
59 if (disable_alpha) {
60 // Show intensity as red, alpha as green
61 return {source_ptr[1], source_ptr[0], 0, 255};
62 } else {
63 return {source_ptr[1], source_ptr[1], source_ptr[1], source_ptr[0]};
64 }
65 }
66
67 case Regs::TextureFormat::RG8: {
68 auto res = Color::DecodeRG8(source + VideoCore::GetMortonOffset(x, y, 2));
69 return {res.r(), res.g(), 0, 255};
70 }
71
72 case Regs::TextureFormat::I8: {
73 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
74 return {*source_ptr, *source_ptr, *source_ptr, 255};
75 }
76
77 case Regs::TextureFormat::A8: {
78 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
79
80 if (disable_alpha) {
81 return {*source_ptr, *source_ptr, *source_ptr, 255};
82 } else {
83 return {0, 0, 0, *source_ptr};
84 }
85 }
86
87 case Regs::TextureFormat::IA4: {
88 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
89
90 u8 i = Color::Convert4To8(((*source_ptr) & 0xF0) >> 4);
91 u8 a = Color::Convert4To8((*source_ptr) & 0xF);
92
93 if (disable_alpha) {
94 // Show intensity as red, alpha as green
95 return {i, a, 0, 255};
96 } else {
97 return {i, i, i, a};
98 }
99 }
100
101 case Regs::TextureFormat::I4: {
102 u32 morton_offset = VideoCore::GetMortonOffset(x, y, 1);
103 const u8* source_ptr = source + morton_offset / 2;
104
105 u8 i = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
106 i = Color::Convert4To8(i);
107
108 return {i, i, i, 255};
109 }
110
111 case Regs::TextureFormat::A4: {
112 u32 morton_offset = VideoCore::GetMortonOffset(x, y, 1);
113 const u8* source_ptr = source + morton_offset / 2;
114
115 u8 a = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
116 a = Color::Convert4To8(a);
117
118 if (disable_alpha) {
119 return {a, a, a, 255};
120 } else {
121 return {0, 0, 0, a};
122 }
123 }
124
125 case Regs::TextureFormat::ETC1:
126 case Regs::TextureFormat::ETC1A4: {
127 bool has_alpha = (info.format == Regs::TextureFormat::ETC1A4);
128
129 // ETC1 further subdivides each 8x8 tile into four 4x4 subtiles
130 const int subtile_width = 4;
131 const int subtile_height = 4;
132
133 int subtile_index = ((x / subtile_width) & 1) + 2 * ((y / subtile_height) & 1);
134 unsigned subtile_bytes = has_alpha ? 2 : 1; // TODO: Name...
135
136 const u64* source_ptr = (const u64*)(source + coarse_x * subtile_bytes * 4 +
137 coarse_y * subtile_bytes * 4 * (info.width / 8) +
138 subtile_index * subtile_bytes * 8);
139 u64 alpha = 0xFFFFFFFFFFFFFFFF;
140 if (has_alpha) {
141 alpha = *source_ptr;
142 source_ptr++;
143 }
144
145 union ETC1Tile {
146 // Each of these two is a collection of 16 bits (one per lookup value)
147 BitField<0, 16, u64> table_subindexes;
148 BitField<16, 16, u64> negation_flags;
149
150 unsigned GetTableSubIndex(unsigned index) const {
151 return (table_subindexes >> index) & 1;
152 }
153
154 bool GetNegationFlag(unsigned index) const {
155 return ((negation_flags >> index) & 1) == 1;
156 }
157
158 BitField<32, 1, u64> flip;
159 BitField<33, 1, u64> differential_mode;
160
161 BitField<34, 3, u64> table_index_2;
162 BitField<37, 3, u64> table_index_1;
163
164 union {
165 // delta value + base value
166 BitField<40, 3, s64> db;
167 BitField<43, 5, u64> b;
168
169 BitField<48, 3, s64> dg;
170 BitField<51, 5, u64> g;
171
172 BitField<56, 3, s64> dr;
173 BitField<59, 5, u64> r;
174 } differential;
175
176 union {
177 BitField<40, 4, u64> b2;
178 BitField<44, 4, u64> b1;
179
180 BitField<48, 4, u64> g2;
181 BitField<52, 4, u64> g1;
182
183 BitField<56, 4, u64> r2;
184 BitField<60, 4, u64> r1;
185 } separate;
186
187 const Math::Vec3<u8> GetRGB(int x, int y) const {
188 int texel = 4 * x + y;
189
190 if (flip)
191 std::swap(x, y);
192
193 // Lookup base value
194 Math::Vec3<int> ret;
195 if (differential_mode) {
196 ret.r() = static_cast<int>(differential.r);
197 ret.g() = static_cast<int>(differential.g);
198 ret.b() = static_cast<int>(differential.b);
199 if (x >= 2) {
200 ret.r() += static_cast<int>(differential.dr);
201 ret.g() += static_cast<int>(differential.dg);
202 ret.b() += static_cast<int>(differential.db);
203 }
204 ret.r() = Color::Convert5To8(ret.r());
205 ret.g() = Color::Convert5To8(ret.g());
206 ret.b() = Color::Convert5To8(ret.b());
207 } else {
208 if (x < 2) {
209 ret.r() = Color::Convert4To8(static_cast<u8>(separate.r1));
210 ret.g() = Color::Convert4To8(static_cast<u8>(separate.g1));
211 ret.b() = Color::Convert4To8(static_cast<u8>(separate.b1));
212 } else {
213 ret.r() = Color::Convert4To8(static_cast<u8>(separate.r2));
214 ret.g() = Color::Convert4To8(static_cast<u8>(separate.g2));
215 ret.b() = Color::Convert4To8(static_cast<u8>(separate.b2));
216 }
217 }
218
219 // Add modifier
220 unsigned table_index =
221 static_cast<int>((x < 2) ? table_index_1.Value() : table_index_2.Value());
222
223 static const std::array<std::array<u8, 2>, 8> etc1_modifier_table = {{
224 {{2, 8}},
225 {{5, 17}},
226 {{9, 29}},
227 {{13, 42}},
228 {{18, 60}},
229 {{24, 80}},
230 {{33, 106}},
231 {{47, 183}},
232 }};
233
234 int modifier = etc1_modifier_table.at(table_index).at(GetTableSubIndex(texel));
235 if (GetNegationFlag(texel))
236 modifier *= -1;
237
238 ret.r() = MathUtil::Clamp(ret.r() + modifier, 0, 255);
239 ret.g() = MathUtil::Clamp(ret.g() + modifier, 0, 255);
240 ret.b() = MathUtil::Clamp(ret.b() + modifier, 0, 255);
241
242 return ret.Cast<u8>();
243 }
244 } const* etc1_tile = reinterpret_cast<const ETC1Tile*>(source_ptr);
245
246 alpha >>= 4 * ((x & 3) * 4 + (y & 3));
247 return Math::MakeVec(etc1_tile->GetRGB(x & 3, y & 3),
248 disable_alpha ? (u8)255 : Color::Convert4To8(alpha & 0xF));
249 }
250
251 default:
252 LOG_ERROR(HW_GPU, "Unknown texture format: %x", (u32)info.format);
253 DEBUG_ASSERT(false);
254 return {};
255 }
256}
257
258TextureInfo TextureInfo::FromPicaRegister(const Regs::TextureConfig& config,
259 const Regs::TextureFormat& format) {
260 TextureInfo info;
261 info.physical_address = config.GetPhysicalAddress();
262 info.width = config.width;
263 info.height = config.height;
264 info.format = format;
265 info.stride = Pica::Regs::NibblesPerPixel(info.format) * info.width / 2;
266 return info;
267}
268
269} // namespace Texture
270} // namespace Pica
diff --git a/src/video_core/texture/texture_decode.h b/src/video_core/texture/texture_decode.h
new file mode 100644
index 000000000..0c1438b0f
--- /dev/null
+++ b/src/video_core/texture/texture_decode.h
@@ -0,0 +1,40 @@
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/pica.h"
10
11namespace Pica {
12namespace Texture {
13
14struct TextureInfo {
15 PAddr physical_address;
16 int width;
17 int height;
18 int stride;
19 Pica::Regs::TextureFormat format;
20
21 static TextureInfo FromPicaRegister(const Pica::Regs::TextureConfig& config,
22 const Pica::Regs::TextureFormat& format);
23};
24
25/**
26 * Lookup texel located at the given coordinates and return an RGBA vector of its color.
27 * @param source Source pointer to read data from
28 * @param s,t Texture coordinates to read from
29 * @param info TextureInfo object describing the texture setup
30 * @param disable_alpha This is used for debug widgets which use this method to display textures
31 * without providing a good way to visualize alpha by themselves. If true, this will return 255 for
32 * the alpha component, and either drop the information entirely or store it in an "unused" color
33 * channel.
34 * @todo Eventually we should get rid of the disable_alpha parameter.
35 */
36Math::Vec4<u8> LookupTexture(const u8* source, int s, int t, const TextureInfo& info,
37 bool disable_alpha = false);
38
39} // namespace Texture
40} // namespace Pica