Debian Codegraph @

Source/Core/VideoBackends/Software/TextureSampler.cpp

raw symbols SCIP
1 // Copyright 2009 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/Software/TextureSampler.h"
5
6 #include <algorithm>
7 #include <span>
8
9 #include "Common/CommonTypes.h"
10 #include "Common/MsgHandler.h"
11 #include "Common/SpanUtils.h"
12 #include "Core/HW/Memmap.h"
13 #include "Core/System.h"
14
15 #include "VideoCommon/BPMemory.h"
16 #include "VideoCommon/TextureDecoder.h"
17
18 #define ALLOW_MIPMAP 1
19
20 namespace TextureSampler
21 {
22 static inline void WrapCoord(int* coordp, WrapMode wrap_mode, int image_size)
23 {
24 int coord = *coordp;
25 switch (wrap_mode)
26 {
27 case WrapMode::Clamp:
28 coord = std::clamp(coord, 0, image_size - 1);
29 break;
30 case WrapMode::Repeat:
31 // Per YAGCD's info on TX_SETMODE1_I0 (et al.), mirror "requires the texture size to be a power
32 // of two. (wrapping is implemented by a logical AND (SIZE-1))". So though this doesn't wrap
33 // nicely for non-power-of-2 sizes, that's how hardware does it.
34 coord = coord & (image_size - 1);
35 break;
36 case WrapMode::Mirror:
37 {
38 // YAGCD doesn't mention this, but this seems to be the check used to implement mirroring.
39 // With power-of-2 sizes, this correctly checks if it's an even-numbered repeat or an
40 // odd-numbered one, and thus can decide whether to reflect. It fails in unusual ways
41 // with non-power-of-2 sizes, but seems to match what happens on actual hardware.
42 if ((coord & image_size) != 0)
43 coord = ~coord;
44 coord = coord & (image_size - 1);
45 break;
46 }
47 default:
48 // Hardware testing indicates that wrap_mode set to 3 behaves the same as clamp.
49 PanicAlertFmt("Invalid wrap mode: {}", wrap_mode);
50 coord = std::clamp(coord, 0, image_size - 1);
51 break;
52 }
53 *coordp = coord;
54 }
55
56 static inline void SetTexel(const u8* inTexel, u32* outTexel, u32 fract)
57 {
58 outTexel[0] = inTexel[0] * fract;
59 outTexel[1] = inTexel[1] * fract;
60 outTexel[2] = inTexel[2] * fract;
61 outTexel[3] = inTexel[3] * fract;
62 }
63
64 static inline void AddTexel(const u8* inTexel, u32* outTexel, u32 fract)
65 {
66 outTexel[0] += inTexel[0] * fract;
67 outTexel[1] += inTexel[1] * fract;
68 outTexel[2] += inTexel[2] * fract;
69 outTexel[3] += inTexel[3] * fract;
70 }
71
72 void Sample(s32 s, s32 t, s32 lod, bool linear, u8 texmap, u8* sample)
73 {
74 int baseMip = 0;
75 bool mipLinear = false;
76
77 #if (ALLOW_MIPMAP)
78 auto texUnit = bpmem.tex.GetUnit(texmap);
79 const TexMode0& tm0 = texUnit.texMode0;
80
81 const s32 lodFract = lod & 0xf;
82
83 if (lod > 0 && tm0.mipmap_filter != MipMode::None)
84 {
85 // use mipmap
86 baseMip = lod >> 4;
87 mipLinear = (lodFract && tm0.mipmap_filter == MipMode::Linear);
88
89 // if using nearest mip filter and lodFract >= 0.5 round up to next mip
90 if (tm0.mipmap_filter == MipMode::Point && lodFract >= 8)
91 baseMip++;
92 }
93
94 if (mipLinear)
95 {
96 u8 sampledTex[4];
97 u32 texel[4];
98
99 SampleMip(s, t, baseMip, linear, texmap, sampledTex);
100 SetTexel(sampledTex, texel, (16 - lodFract));
101
102 SampleMip(s, t, baseMip + 1, linear, texmap, sampledTex);
103 AddTexel(sampledTex, texel, lodFract);
104
105 sample[0] = (u8)(texel[0] >> 4);
106 sample[1] = (u8)(texel[1] >> 4);
107 sample[2] = (u8)(texel[2] >> 4);
108 sample[3] = (u8)(texel[3] >> 4);
109 }
110 else
111 #endif
112 {
113 SampleMip(s, t, baseMip, linear, texmap, sample);
114 }
115 }
116
117 void SampleMip(s32 s, s32 t, s32 mip, bool linear, u8 texmap, u8* sample)
118 {
119 auto texUnit = bpmem.tex.GetUnit(texmap);
120
121 const TexMode0& tm0 = texUnit.texMode0;
122 const TexImage0& ti0 = texUnit.texImage0;
123 const TexTLUT& texTlut = texUnit.texTlut;
124 const TextureFormat texfmt = ti0.format;
125 const TLUTFormat tlutfmt = texTlut.tlut_format;
126
127 std::span<const u8> image_src;
128 std::span<const u8> image_src_odd;
129 if (texUnit.texImage1.cache_manually_managed)
130 {
131 image_src = TexDecoder_GetTmemSpan(texUnit.texImage1.tmem_even * TMEM_LINE_SIZE);
132 if (texfmt == TextureFormat::RGBA8)
133 image_src_odd = TexDecoder_GetTmemSpan(texUnit.texImage2.tmem_odd * TMEM_LINE_SIZE);
134 }
135 else
136 {
137 auto& system = Core::System::GetInstance();
138 auto& memory = system.GetMemory();
139
140 const u32 imageBase = texUnit.texImage3.image_base << 5;
141 image_src = memory.GetSpanForAddress(imageBase);
142 }
143
144 int image_width_minus_1 = ti0.width;
145 int image_height_minus_1 = ti0.height;
146
147 const int tlutAddress = texTlut.tmem_offset << 9;
148 const std::span<const u8> tlut = TexDecoder_GetTmemSpan(tlutAddress);
149
150 // reduce sample location and texture size to mip level
151 // move texture pointer to mip location
152 if (mip)
153 {
154 int mipWidth = image_width_minus_1 + 1;
155 int mipHeight = image_height_minus_1 + 1;
156
157 const int fmtWidth = TexDecoder_GetBlockWidthInTexels(texfmt);
158 const int fmtHeight = TexDecoder_GetBlockHeightInTexels(texfmt);
159 const int fmtDepth = TexDecoder_GetTexelSizeInNibbles(texfmt);
160
161 image_width_minus_1 >>= mip;
162 image_height_minus_1 >>= mip;
163 s >>= mip;
164 t >>= mip;
165
166 while (mip)
167 {
168 mipWidth = std::max(mipWidth, fmtWidth);
169 mipHeight = std::max(mipHeight, fmtHeight);
170 const u32 size = (mipWidth * mipHeight * fmtDepth) >> 1;
171
172 image_src = Common::SafeSubspan(image_src, size);
173 mipWidth >>= 1;
174 mipHeight >>= 1;
175 mip--;
176 }
177 }
178
179 if (linear)
180 {
181 // offset linear sampling
182 s -= 64;
183 t -= 64;
184
185 // integer part of sample location
186 int imageS = s >> 7;
187 int imageT = t >> 7;
188
189 // linear sampling
190 int imageSPlus1 = imageS + 1;
191 const int fractS = s & 0x7f;
192
193 int imageTPlus1 = imageT + 1;
194 const int fractT = t & 0x7f;
195
196 u8 sampledTex[4];
197 u32 texel[4];
198
199 WrapCoord(&imageS, tm0.wrap_s, image_width_minus_1 + 1);
200 WrapCoord(&imageT, tm0.wrap_t, image_height_minus_1 + 1);
201 WrapCoord(&imageSPlus1, tm0.wrap_s, image_width_minus_1 + 1);
202 WrapCoord(&imageTPlus1, tm0.wrap_t, image_height_minus_1 + 1);
203
204 if (!(texfmt == TextureFormat::RGBA8 && texUnit.texImage1.cache_manually_managed))
205 {
206 TexDecoder_DecodeTexel(sampledTex, image_src, imageS, imageT, image_width_minus_1, texfmt,
207 tlut, tlutfmt);
208 SetTexel(sampledTex, texel, (128 - fractS) * (128 - fractT));
209
210 TexDecoder_DecodeTexel(sampledTex, image_src, imageSPlus1, imageT, image_width_minus_1,
211 texfmt, tlut, tlutfmt);
212 AddTexel(sampledTex, texel, (fractS) * (128 - fractT));
213
214 TexDecoder_DecodeTexel(sampledTex, image_src, imageS, imageTPlus1, image_width_minus_1,
215 texfmt, tlut, tlutfmt);
216 AddTexel(sampledTex, texel, (128 - fractS) * (fractT));
217
218 TexDecoder_DecodeTexel(sampledTex, image_src, imageSPlus1, imageTPlus1, image_width_minus_1,
219 texfmt, tlut, tlutfmt);
220 AddTexel(sampledTex, texel, (fractS) * (fractT));
221 }
222 else
223 {
224 TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, image_src, image_src_odd, imageS, imageT,
225 image_width_minus_1);
226 SetTexel(sampledTex, texel, (128 - fractS) * (128 - fractT));
227
228 TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, image_src, image_src_odd, imageSPlus1, imageT,
229 image_width_minus_1);
230 AddTexel(sampledTex, texel, (fractS) * (128 - fractT));
231
232 TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, image_src, image_src_odd, imageS, imageTPlus1,
233 image_width_minus_1);
234 AddTexel(sampledTex, texel, (128 - fractS) * (fractT));
235
236 TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, image_src, image_src_odd, imageSPlus1,
237 imageTPlus1, image_width_minus_1);
238 AddTexel(sampledTex, texel, (fractS) * (fractT));
239 }
240
241 sample[0] = (u8)(texel[0] >> 14);
242 sample[1] = (u8)(texel[1] >> 14);
243 sample[2] = (u8)(texel[2] >> 14);
244 sample[3] = (u8)(texel[3] >> 14);
245 }
246 else
247 {
248 // integer part of sample location
249 int imageS = s >> 7;
250 int imageT = t >> 7;
251
252 // nearest neighbor sampling
253 WrapCoord(&imageS, tm0.wrap_s, image_width_minus_1 + 1);
254 WrapCoord(&imageT, tm0.wrap_t, image_height_minus_1 + 1);
255
256 if (!(texfmt == TextureFormat::RGBA8 && texUnit.texImage1.cache_manually_managed))
257 {
258 TexDecoder_DecodeTexel(sample, image_src, imageS, imageT, image_width_minus_1, texfmt, tlut,
259 tlutfmt);
260 }
261 else
262 {
263 TexDecoder_DecodeTexelRGBA8FromTmem(sample, image_src, image_src_odd, imageS, imageT,
264 image_width_minus_1);
265 }
266 }
267 }
268 } // namespace TextureSampler