-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTexture.py
326 lines (265 loc) · 10.2 KB
/
Texture.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""Provides functions for manipulating xbox textures."""
# pylint: disable=consider-using-f-string
# pylint: disable=too-many-arguments
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements
# FIXME: Move to xboxpy.NV2A.PGRAPH.Texture
from collections import namedtuple
from PIL import Image
from Xbox import Xbox
import XboxHelper
from xboxpy import nv2a
# Value that may be added to contiguous memory addresses to access as ADDR_AGPMEM, which
# is guaranteed to be linear (and thus may be slower than tiled ADDR_FBMEM but can be
# manipulated directly).
AGP_MEMORY_BASE = 0xF0000000
TextureParameters = namedtuple(
"TextureParameters",
[
"width",
"height",
"color_pitch",
"color_offset",
"format_color",
"depth_pitch",
"depth_offset",
"format_depth",
"surface_type",
"swizzle_unk",
"swizzle_unk2",
"swizzled",
],
)
# Right hand side is always in RGB or RGBA channel order.
TextureDescription = namedtuple(
"TextureDescription", ["bpp", "channel_bpps", "channel_offsets"]
)
Y8 = TextureDescription(8, (8, 8, 8), (0, 0, 0))
AY8 = TextureDescription(8, (8, 8, 8, 8), (0, 0, 0, 0))
A8 = TextureDescription(8, (0, 0, 0, 8), (0, 0, 0, 0))
A8Y8 = TextureDescription(16, (8, 8, 8, 8), (0, 0, 0, 8))
R5G6B5 = TextureDescription(16, (5, 6, 5), (11, 5, 0))
A4R4G4B4 = TextureDescription(16, (4, 4, 4, 4), (8, 4, 0, 12))
A1R5G5B5 = TextureDescription(16, (5, 5, 5, 1), (10, 5, 0, 15))
X1R5G5B5 = TextureDescription(16, (5, 5, 5), (10, 5, 0))
A8R8G8B8 = TextureDescription(32, (8, 8, 8, 8), (16, 8, 0, 24))
X8R8G8B8 = TextureDescription(32, (8, 8, 8), (16, 8, 0))
def _decode_texture(
data, size, pitch, swizzled, bits_per_pixel, channel_sizes, channel_offsets
):
"""Convert the given texture data into a PIL.Image."""
# Check argument sanity
assert len(size) == 2 # FIXME: Support 1D and 3D?
assert len(channel_offsets) == len(channel_sizes)
# Helper function to extract integer at bit offset with bit size
def get_bits(bits, offset, length):
mask = (1 << length) - 1
return (bits >> offset) & mask
width = size[0]
height = size[1]
if len(channel_sizes) == 3:
mode = "RGB"
elif len(channel_sizes) == 4:
mode = "RGBA"
else:
raise Exception("Unsupported channel_sizes %d" % len(channel_sizes))
img = Image.new(mode, (width, height))
# TODO: Is unswizzling actually necessary if textures are read via AGP?
# Need to set up a swizzled test case and verify behavior.
# FIXME: Unswizzle data on the fly instead
if swizzled:
data = nv2a.Unswizzle(data, bits_per_pixel, (width, height), pitch)
pixels = img.load() # create the pixel map
assert bits_per_pixel % 8 == 0
for y in range(height):
for x in range(width):
pixel_offset = y * pitch + x * bits_per_pixel // 8
pixel_bytes = data[pixel_offset : pixel_offset + bits_per_pixel // 8]
pixel_bits = int.from_bytes(pixel_bytes, byteorder="little")
pixel_channels = ()
for channel_offset, channel_size in zip(channel_offsets, channel_sizes):
channel_value = get_bits(pixel_bits, channel_offset, channel_size)
# Normalize channel
if channel_size > 0:
channel_value /= (1 << channel_size) - 1
channel_value = int(channel_value * 0xFF)
else:
channel_value = 0x00
pixel_channels += (channel_value,)
pixels[x, y] = pixel_channels
return img
def surface_color_format_to_texture_format(fmt, swizzled):
"""Convert nv2a draw format to the equivalent Texture format."""
if fmt == 0x3: # ARGB1555
return 0x3 if swizzled else 0x1C
if fmt == 0x5: # RGB565
return 0x5 if swizzled else 0x11
if fmt in [0x7, 0x08]: # XRGB8888
return 0x7 if swizzled else 0x1E
if fmt == 0xC: # ARGB8888
return 0x6 if swizzled else 0x12
raise Exception(
"Unknown color fmt %d (0x%X) %s"
% (fmt, fmt, "swizzled" if swizzled else "unswizzled")
)
def surface_zeta_format_to_texture_format(fmt, swizzled, is_float):
"""Convert nv2a zeta format to the equivalent Texture format."""
if fmt == 0x1: # Z16
if is_float:
return 0x2D if swizzled else 0x31
return 0x2C if swizzled else 0x30
if fmt == 0x2: # Z24S8
if is_float:
return 0x2B if swizzled else 0x2F
return 0x2A if swizzled else 0x2E
raise Exception(
"Unknown zeta fmt %d (0x%X) %s %s"
% (
fmt,
fmt,
"float" if is_float else "fixed",
"swizzled" if swizzled else "unswizzled",
)
)
def read_texture_parameters(xbox: Xbox) -> TextureParameters:
"""Reads the current texture state"""
color_pitch = xbox.read_u32(0xFD400858)
depth_pitch = xbox.read_u32(0xFD40085C)
color_offset = xbox.read_u32(0xFD400828)
depth_offset = xbox.read_u32(0xFD40082C)
color_base = xbox.read_u32(0xFD400840)
depth_base = xbox.read_u32(0xFD400844)
# FIXME: Is this correct? pbkit uses _base, but D3D seems to use _offset?
color_offset += color_base
depth_offset += depth_base
surface_clip_x = xbox.read_u32(0xFD4019B4)
surface_clip_y = xbox.read_u32(0xFD4019B8)
draw_format = xbox.read_u32(0xFD400804)
surface_type = xbox.read_u32(0xFD400710)
swizzle_unk = xbox.read_u32(0xFD400818)
swizzle_unk2 = xbox.read_u32(0xFD40086C)
clip_x = (surface_clip_x >> 0) & 0xFFFF
clip_y = (surface_clip_y >> 0) & 0xFFFF
clip_w = (surface_clip_x >> 16) & 0xFFFF
clip_h = (surface_clip_y >> 16) & 0xFFFF
surface_anti_aliasing = (surface_type >> 4) & 3
clip_x, clip_y = XboxHelper.apply_anti_aliasing_factor(
surface_anti_aliasing, clip_x, clip_y
)
clip_w, clip_h = XboxHelper.apply_anti_aliasing_factor(
surface_anti_aliasing, clip_w, clip_h
)
width = clip_x + clip_w
height = clip_y + clip_h
# FIXME: 128 x 128 [pitch = 256 (0x100)], at 0x01AA8000 [PGRAPH: 0x01AA8000?], format 0x5, type: 0x21000002, swizzle: 0x7070000 [used 0]
# FIXME: This does not seem to be a good field for this
# FIXME: Patched to give 50% of coolness
swizzled = (surface_type & 3) == 2
# FIXME: if surface_type is 0, we probably can't even draw..
format_color = (draw_format >> 12) & 0xF
# FIXME: Support 3D surfaces.
_format_depth_buffer = (draw_format >> 18) & 0x3
if not format_color:
fmt_color = None
else:
fmt_color = surface_color_format_to_texture_format(format_color, swizzled)
# TODO: Extract swizzle and float state.
# fmt_depth = surface_zeta_format_to_texture_format(format_depth_buffer)
return TextureParameters(
width=width,
height=height,
color_pitch=color_pitch,
color_offset=color_offset,
format_color=fmt_color,
depth_pitch=depth_pitch,
depth_offset=depth_offset,
format_depth=None,
surface_type=surface_type,
swizzle_unk=swizzle_unk,
swizzle_unk2=swizzle_unk2,
swizzled=swizzled,
)
def dump_texture(xbox, offset, pitch, fmt_color, width, height):
"""Convert the texture at the given offset into a PIL.Image."""
img = None
if fmt_color == 0x0:
tex_info = (True, Y8)
elif fmt_color == 0x1:
tex_info = (True, AY8)
elif fmt_color == 0x2:
tex_info = (True, A1R5G5B5)
elif fmt_color == 0x3:
tex_info = (True, X1R5G5B5)
elif fmt_color == 0x4:
tex_info = (True, A4R4G4B4)
elif fmt_color == 0x5:
tex_info = (True, R5G6B5)
elif fmt_color == 0x6:
tex_info = (True, A8R8G8B8)
elif fmt_color == 0x7:
tex_info = (True, X8R8G8B8)
elif fmt_color == 0xB:
img = Image.new(
"RGB", (width, height), (255, 0, 255, 255)
) # FIXME! Palette mode!
elif fmt_color == 0xC: # DXT1
data = xbox.read(AGP_MEMORY_BASE | offset, width * height // 2)
img = Image.frombytes("RGBA", (width, height), data, "bcn", 1) # DXT1
elif fmt_color == 0xE: # DXT3
data = xbox.read(AGP_MEMORY_BASE | offset, width * height * 1)
img = Image.frombytes("RGBA", (width, height), data, "bcn", 2) # DXT3
elif fmt_color == 0xF: # DXT5
data = xbox.read(AGP_MEMORY_BASE | offset, width * height * 1)
img = Image.frombytes("RGBA", (width, height), data, "bcn", 3) # DXT5
elif fmt_color == 0x10:
tex_info = (False, A1R5G5B5)
elif fmt_color == 0x11:
tex_info = (False, R5G6B5)
elif fmt_color == 0x12:
tex_info = (False, A8R8G8B8)
elif fmt_color == 0x19:
tex_info = (True, A8)
elif fmt_color == 0x1A:
tex_info = (True, A8Y8)
elif fmt_color == 0x1C:
tex_info = (False, X1R5G5B5)
elif fmt_color == 0x1D:
tex_info = (False, A4R4G4B4)
elif fmt_color == 0x1E:
tex_info = (False, X8R8G8B8)
elif fmt_color == 0x2E:
img = Image.new(
"RGB", (width, height), (255, 0, 255, 255)
) # FIXME! Depth format
elif fmt_color == 0x30:
img = Image.new(
"RGB", (width, height), (255, 0, 255, 255)
) # FIXME! Depth format
elif fmt_color == 0x31:
img = Image.new(
"RGB", (width, height), (255, 0, 255, 255)
) # FIXME! Depth format
else:
raise Exception("Unknown texture format: 0x%X" % fmt_color)
# Some formats might have been parsed already
if img is None:
swizzled = tex_info[0]
format_info = tex_info[1]
# Parse format info
bits_per_pixel, channel_sizes, channel_offsets = format_info
# FIXME: Avoid this nasty ~~convience feature~~ hack
if pitch == 0:
pitch = width * bits_per_pixel // 8
# FIXME: Might want to skip the empty area if pitch and width diverge?
data = xbox.read(AGP_MEMORY_BASE | offset, pitch * height)
img = _decode_texture(
data,
(width, height),
pitch,
swizzled,
bits_per_pixel,
channel_sizes,
channel_offsets,
)
return img