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
//! D3DX's CTAB comment: offsets are relative to the header following the FourCC.
use crate::{Result, error};
/// A CTAB type. Numeric class/type values are D3DXPARAMETER_CLASS/TYPE, kept alongside their dimensions.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Type {
/// D3DXPARAMETER_CLASS as stored (u16 at offset 0 of the type record): scalar, vector, row or column matrix, object,
/// struct.
pub class: u16,
/// D3DXPARAMETER_TYPE as stored (u16 at offset 2): bool, int, float, sampler and so on.
pub kind: u16,
/// Rows (u16 at offset 4).
pub rows: u16,
/// Columns (u16 at offset 6).
pub columns: u16,
/// Array length (u16 at offset 8); 1 for a type that is not an array.
pub elements: u16,
/// Members of a struct type in the order stored (the u16 count at offset 10 says how many); empty for any other
/// class.
pub members: Vec<TypeMember>,
}
/// One member of a struct type.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TypeMember {
/// The member's name.
pub name: String,
/// The member's type.
pub ty: Type,
}
/// A named allocation, including the original type even when optimization shortened its register range.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Constant {
/// The constant's name, from the NUL-terminated UTF-8 string its record points at.
pub name: String,
/// D3DXREGISTER_SET: BOOL=0, INT4=1, FLOAT4=2, SAMPLER=3.
pub register_set: u16,
/// First register of the allocation (u16 at offset 6 of the constant record), in the bank `register_set` names.
pub register: u16,
/// Registers allocated (u16 at offset 8). Reading refuses 0 and a range past the end of the bank (16 registers for
/// bool, int4 and, in a `ps_3_0`, samplers; 4 samplers in a `vs_3_0`; 224 float4 registers in a `ps_3_0`, 256 in a
/// `vs_3_0`).
pub count: u16,
/// Compiler-reserved word of D3DXSHADER_CONSTANTINFO. Compilers write nonzero values;
/// it is retained verbatim and does not change the explicit register allocation.
pub reserved: u16,
/// The declared type; it can be larger than `count` registers when the compiler shortened the allocation.
pub ty: Type,
/// Raw register defaults (BOOL rows are 4 bytes, other numeric rows 16); no defaults on samplers.
pub default: Option<Vec<u8>>,
}
/// The constant table (CTAB comment) of a program: who compiled it, for which target, and the named register allocations.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConstantTable {
/// The creator string the header's offset at 4 points at (the compiler's name and version).
pub creator: String,
/// The target profile string (`ps_3_0`) the header's offset at 24 points at.
pub target: String,
/// The shader version token (u32 at offset 8 of the header): `0xffff0300` for `ps_3_0`, `0xfffe0300` for `vs_3_0`.
/// Reading refuses a table that disagrees with the program's stage.
pub version: u32,
/// Compile flags (u32 at offset 20 of the header), as stored.
pub flags: u32,
/// The allocations in the order stored: at most 1024, none overlapping another in the same register set, no name
/// repeated.
pub constants: Vec<Constant>,
}
struct Reader<'a> {
data: &'a [u8],
word: usize,
}
impl Reader<'_> {
/// The word of the program that byte `at` of the table lies in, for an error.
fn word_of(&self, at: usize) -> usize {
self.word.saturating_add(at / 4)
}
/// `at + by`, an error naming `at` when it does not fit.
fn plus(&self, at: usize, by: usize) -> Result<usize> {
at.checked_add(by).ok_or_else(|| {
error(
self.word_of(at),
format!("CTAB offset {at}+{by} is past the largest offset"),
)
})
}
/// `count * size` bytes, an error naming `at` when it does not fit.
fn bytes(&self, at: usize, count: usize, size: usize) -> Result<usize> {
count.checked_mul(size).ok_or_else(|| {
error(
self.word_of(at),
format!("CTAB {count} entries of {size} bytes do not fit"),
)
})
}
fn range(&self, at: usize, len: usize) -> Result<&[u8]> {
at.checked_add(len)
.and_then(|end| self.data.get(at..end))
.ok_or_else(|| {
error(
self.word_of(at),
format!(
"CTAB range {at}+{len} outside the comment's {} bytes",
self.data.len()
),
)
})
}
fn u16(&self, at: usize) -> Result<u16> {
match self.range(at, 2)? {
&[a, b] => Ok(u16::from_le_bytes([a, b])),
_ => Err(error(self.word_of(at), "CTAB u16 outside comment")),
}
}
fn u32(&self, at: usize) -> Result<u32> {
match self.range(at, 4)? {
&[a, b, c, d] => Ok(u32::from_le_bytes([a, b, c, d])),
_ => Err(error(self.word_of(at), "CTAB u32 outside comment")),
}
}
fn offset(&self, at: usize) -> Result<usize> {
let offset = self.u32(at)?;
usize::try_from(offset).map_err(|_| {
error(
self.word_of(at),
format!("CTAB offset {offset} does not fit an address"),
)
})
}
fn string(&self, at: usize) -> Result<String> {
let bytes = self.range(at, self.data.len().saturating_sub(at))?;
let end = bytes
.iter()
.position(|&b| b == 0)
.ok_or_else(|| error(self.word_of(at), "CTAB string has no NUL"))?;
let text = bytes.get(..end).unwrap_or_default();
std::str::from_utf8(text)
.map(str::to_owned)
.map_err(|_| error(self.word_of(at), "CTAB string is not UTF-8"))
}
fn ty(&self, at: usize, depth: usize, budget: &mut usize) -> Result<Type> {
if depth > 32 || *budget == 0 {
return Err(error(
self.word_of(at),
"CTAB type nesting/cycle or 4096-node limit",
));
}
*budget = budget.saturating_sub(1);
self.range(at, 16)?;
let count = usize::from(self.u16(self.plus(at, 10)?)?);
let members_at = self.offset(self.plus(at, 12)?)?;
self.range(members_at, self.bytes(members_at, count, 8)?)?;
let mut members = Vec::new();
for n in 0..count {
let member = self.plus(members_at, self.bytes(members_at, n, 8)?)?;
members.push(TypeMember {
name: self.string(self.offset(member)?)?,
ty: self.ty(
self.offset(self.plus(member, 4)?)?,
depth.saturating_add(1),
budget,
)?,
});
}
Ok(Type {
class: self.u16(at)?,
kind: self.u16(self.plus(at, 2)?)?,
rows: self.u16(self.plus(at, 4)?)?,
columns: self.u16(self.plus(at, 6)?)?,
elements: self.u16(self.plus(at, 8)?)?,
members,
})
}
}
pub(crate) fn read(data: &[u8], word: usize, expected_version: u32) -> Result<ConstantTable> {
let r = Reader { data, word };
if r.u32(0)? != 28 {
return Err(error(word, "CTAB header size is not 28"));
}
let version = r.u32(8)?;
if version != expected_version {
return Err(error(
r.word_of(8),
format!(
"CTAB version disagrees with the program stage: {version:#010x}, where the stage's is {expected_version:#010x}"
),
));
}
let count = r.offset(12)?;
if count > 1024 {
return Err(error(
r.word_of(12),
format!("CTAB has {count} constants, where it has at most 1024"),
));
}
let base = r.offset(16)?;
r.range(base, r.bytes(base, count, 20)?)?;
let mut constants: Vec<Constant> = Vec::new();
let mut budget = 4096;
for n in 0..count {
let at = r.plus(base, r.bytes(base, n, 20)?)?;
let name = r.string(r.offset(at)?)?;
let register_set = r.u16(r.plus(at, 4)?)?;
let register = r.u16(r.plus(at, 6)?)?;
let count = r.u16(r.plus(at, 8)?)?;
let limit = match register_set {
0 | 1 => 16,
3 => {
if version == 0xfffe_0300 {
4
} else {
16
}
}
2 => {
if version == 0xfffe_0300 {
256
} else {
224
}
}
_ => {
return Err(error(
r.word_of(at),
format!(
"CTAB register set {register_set}, where it is 0 (bool), 1 (int4), 2 (float4) or 3 (sampler)"
),
));
}
};
if count == 0 || u32::from(register).saturating_add(u32::from(count)) > limit {
return Err(error(
r.word_of(at),
format!(
"CTAB register range c{register}+{count} is empty or out-of-bank (the bank has {limit} registers)"
),
));
}
if constants.iter().any(|c| {
c.name == name
|| (c.register_set == register_set
&& u32::from(c.register) < u32::from(register).saturating_add(u32::from(count))
&& register < c.register.saturating_add(c.count))
}) {
return Err(error(
r.word_of(at),
format!("CTAB constant {name} overlaps an allocation or repeats a name"),
));
}
let default_at = r.offset(r.plus(at, 16)?)?;
let default = if default_at == 0 {
None
} else {
if register_set == 3 {
return Err(error(r.word_of(at), "CTAB sampler default"));
}
let row = if register_set == 0 { 4 } else { 16 };
Some(
r.range(default_at, r.bytes(default_at, usize::from(count), row)?)?
.to_vec(),
)
};
constants.push(Constant {
name,
register_set,
register,
count,
reserved: r.u16(r.plus(at, 10)?)?,
ty: r.ty(r.offset(r.plus(at, 12)?)?, 0, &mut budget)?,
default,
});
}
Ok(ConstantTable {
creator: r.string(r.offset(4)?)?,
version,
flags: r.u32(20)?,
target: r.string(r.offset(24)?)?,
constants,
})
}