d3d9-reader 0.12.0

Direct3D 9 shader bytecode parser: reads vs/ps 1.x, 2.x and 3.0 token streams into typed declarations, definitions and instructions, with the CTAB constant table
Documentation
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Shader Model 1: instructions that state no length, the pixel 1.x texture registers and texture-addressing
//! instructions, co-issue, result shifts, the 1.x source modifiers, the 1.4 phase, and a vertex 1.1 program's `a0.x`
//! index with no token of its own.
mod common;

use common::*;
use d3d9_reader::{
    Declared, Item, Literal, Modifier, Opcode, Register, RegisterType, Relative, Stage, Version, read,
};

#[test]
fn vertex_1_1_reads_declarations_outputs_and_the_implicit_a0_index() {
    let bytes = program(
        0xfffe_0101,
        &[
            // dcl_position v0; dcl_normal v1
            op1(31, &[0x8000_0000, dst(INPUT, 0, 15)]),
            op1(31, &[0x8000_0003, dst(INPUT, 1, 15)]),
            // def c95, 0.5, 1, 2, 0
            op1(
                81,
                &[dst(CONST, 95, 15), 0x3f00_0000, 0x3f80_0000, 0x4000_0000, 0],
            ),
            // m4x4 oPos, v0, c0
            op1(
                20,
                &[
                    dst(RASTOUT, 0, 15),
                    src(INPUT, 0, 0xe4, 0),
                    src(CONST, 0, 0xe4, 0),
                ],
            ),
            // mov a0.x, v1.x
            op1(1, &[dst(ADDR, 0, 1), src(INPUT, 1, 0x00, 0)]),
            // mul oD0, v1, c[a0.x + 10]
            op1(
                5,
                &[
                    dst(ATTROUT, 0, 15),
                    src(INPUT, 1, 0xe4, 0),
                    src(CONST, 10, 0xe4, 0) | 0x2000,
                ],
            ),
            // mov oD1, -v1.zyxw
            op1(1, &[dst(ATTROUT, 1, 15), src(INPUT, 1, 0xc6, 1)]),
            // expp r1, v1.w; lit r2, r1; dst r3, r1, r2
            op1(78, &[dst(TEMP, 1, 15), src(INPUT, 1, 0xff, 0)]),
            op1(16, &[dst(TEMP, 2, 15), src(TEMP, 1, 0xe4, 0)]),
            op1(
                17,
                &[dst(TEMP, 3, 15), src(TEMP, 1, 0xe4, 0), src(TEMP, 2, 0xe4, 0)],
            ),
            // mov oFog, r1.x; mov oPts, c95.y; mov oT0.xy, v1
            op1(1, &[dst(RASTOUT, 1, 15), src(TEMP, 1, 0x00, 0)]),
            op1(1, &[dst(RASTOUT, 2, 15), src(CONST, 95, 0x55, 0)]),
            op1(1, &[dst(OUTPUT, 0, 3), src(INPUT, 1, 0xe4, 0)]),
        ],
    );
    let p = read(&bytes).unwrap();
    assert_eq!(
        p.version,
        Version {
            stage: Stage::Vertex,
            major: 1,
            minor: 1
        }
    );
    assert_eq!(p.version.to_string(), "vs_1_1");
    let usages: Vec<_> = p.declarations().map(|d| d.declared).collect();
    assert_eq!(
        usages,
        [
            Declared::Semantic { usage: 0, index: 0 },
            Declared::Semantic { usage: 3, index: 0 }
        ]
    );
    let def = p.definitions().next().unwrap();
    assert_eq!(
        def.literal,
        Literal::Float([0x3f00_0000, 0x3f80_0000, 0x4000_0000, 0])
    );
    let ops: Vec<_> = p.instructions().map(|i| i.opcode).collect();
    use Opcode::*;
    assert_eq!(ops, [M4x4, Mov, Mul, Mov, ExpP, Lit, Dst, Mov, Mov, Mov]);
    let mul = p.instructions().nth(2).unwrap();
    assert_eq!(
        mul.sources[1].relative,
        Some(Relative {
            word: None,
            register: Register {
                kind: RegisterType::Address,
                number: 0
            },
            swizzle: [0; 4]
        })
    );
    assert_eq!(mul.sources[1].register.number, 10);
    let neg = p.instructions().nth(3).unwrap();
    assert_eq!(neg.sources[0].modifier, Modifier::Negate);
    assert_eq!(neg.sources[0].swizzle, [2, 1, 0, 3]);
    let outs: Vec<_> = p
        .instructions()
        .filter_map(|i| i.destination)
        .map(|d| (d.register.kind, d.register.number, d.mask))
        .collect();
    assert!(outs.contains(&(RegisterType::RasterizerOut, 1, 15)));
    assert!(outs.contains(&(RegisterType::RasterizerOut, 2, 15)));
    assert!(outs.contains(&(RegisterType::Output, 0, 3)));
    // the word of every record is where its token is
    assert_eq!(p.items.first().map(Item::word), Some(1));
    assert_eq!(p.end, bytes.len() / 4 - 1);
}

#[test]
fn pixel_1_1_reads_texture_registers_coissue_shift_and_modifiers() {
    let bytes = program(
        0xffff_0101,
        &[
            // def c0, 0.5, 0.25, 1, 1
            op1(
                81,
                &[
                    dst(CONST, 0, 15),
                    0x3f00_0000,
                    0x3e80_0000,
                    0x3f80_0000,
                    0x3f80_0000,
                ],
            ),
            // tex t0; texbem t1, t0; texcoord t2
            op1(66, &[dst(TEXTURE, 0, 15)]),
            op1(67, &[dst(TEXTURE, 1, 15), src(TEXTURE, 0, 0xe4, 0)]),
            op1(64, &[dst(TEXTURE, 2, 15)]),
            // mul r0.rgb, t0, v0 / +mov r0.a, t1.a
            op1(
                5,
                &[dst(TEMP, 0, 7), src(TEXTURE, 0, 0xe4, 0), src(INPUT, 0, 0xe4, 0)],
            ),
            [vec![0x4000_0001], vec![dst(TEMP, 0, 8), src(TEXTURE, 1, 0xff, 0)]].concat(),
            // mad_x2 r1, t1_bx2, c0, v1_bias
            op1(
                4,
                &[
                    dst(TEMP, 1, 15) | 1 << 24,
                    src(TEXTURE, 1, 0xe4, 4),
                    src(CONST, 0, 0xe4, 0),
                    src(INPUT, 1, 0xe4, 2),
                ],
            ),
            // sub_d2 r1.rgb, 1-r1, -v0 (Microsoft's assembler encodes _d2 as 15, _d4 as 14)
            op1(
                3,
                &[
                    dst(TEMP, 1, 7) | 15 << 24,
                    src(TEMP, 1, 0xe4, 6),
                    src(INPUT, 0, 0xe4, 1),
                ],
            ),
            // cnd r0, r0.a, t2, r1
            op1(
                80,
                &[
                    dst(TEMP, 0, 15),
                    src(TEMP, 0, 0xff, 0),
                    src(TEXTURE, 2, 0xe4, 0),
                    src(TEMP, 1, 0xe4, 0),
                ],
            ),
        ],
    );
    let p = read(&bytes).unwrap();
    assert_eq!(p.version.to_string(), "ps_1_1");
    let i: Vec<_> = p.instructions().collect();
    assert_eq!(i[0].opcode, Opcode::Tex);
    assert!(i[0].sources.is_empty());
    assert_eq!(i[0].destination.unwrap().register.kind, RegisterType::Texture);
    assert_eq!(i[1].opcode, Opcode::TexBem);
    assert_eq!(i[2].opcode, Opcode::TexCoord);
    assert!(!i[3].coissue && i[4].coissue);
    assert_eq!(i[4].destination.unwrap().mask, 8);
    assert_eq!(i[5].destination.unwrap().shift, 1);
    assert_eq!(
        i[5].sources.iter().map(|s| s.modifier).collect::<Vec<_>>(),
        [Modifier::Sign, Modifier::None, Modifier::Bias]
    );
    assert_eq!(i[6].destination.unwrap().shift, -1);
    assert_eq!(i[6].sources[0].modifier, Modifier::Complement);
    assert_eq!(i[7].opcode, Opcode::Cnd);
    assert_eq!(i[7].sources.len(), 3);
}

#[test]
fn pixel_1_3_reads_the_texture_matrix_instructions() {
    let bytes = program(
        0xffff_0103,
        &[
            op1(66, &[dst(TEXTURE, 0, 15)]),
            // texm3x3pad t1, t0_bx2; texm3x3pad t2, t0_bx2; texm3x3spec t3, t0_bx2, c0
            op1(73, &[dst(TEXTURE, 1, 15), src(TEXTURE, 0, 0xe4, 4)]),
            op1(73, &[dst(TEXTURE, 2, 15), src(TEXTURE, 0, 0xe4, 4)]),
            op1(
                76,
                &[
                    dst(TEXTURE, 3, 15),
                    src(TEXTURE, 0, 0xe4, 4),
                    src(CONST, 0, 0xe4, 0),
                ],
            ),
            // texm3x2depth (1.3 only) after a texm3x2pad
            op1(71, &[dst(TEXTURE, 1, 15), src(TEXTURE, 0, 0xe4, 0)]),
            op1(84, &[dst(TEXTURE, 2, 15), src(TEXTURE, 0, 0xe4, 0)]),
            // mul_x4 r0, t3, v0; texkill t0
            op1(
                5,
                &[
                    dst(TEMP, 0, 15) | 2 << 24,
                    src(TEXTURE, 3, 0xe4, 0),
                    src(INPUT, 0, 0xe4, 0),
                ],
            ),
            op1(65, &[dst(TEXTURE, 0, 15)]),
        ],
    );
    let p = read(&bytes).unwrap();
    let ops: Vec<_> = p.instructions().map(|i| (i.opcode, i.sources.len())).collect();
    use Opcode::*;
    assert_eq!(
        ops,
        [
            (Tex, 0),
            (TexM3x3Pad, 1),
            (TexM3x3Pad, 1),
            (TexM3x3Spec, 2),
            (TexM3x2Pad, 1),
            (TexM3x2Depth, 1),
            (Mul, 2),
            (TexKill, 0)
        ]
    );
    assert_eq!(p.instructions().nth(6).unwrap().destination.unwrap().shift, 2);
    // texm3x2depth is 1.3's, and a 1.2 program with it is read, as Microsoft's disassembler lists it
    let mut older = bytes.clone();
    older[..4].copy_from_slice(&0xffff_0102u32.to_le_bytes());
    assert!(
        read(&older)
            .unwrap()
            .instructions()
            .any(|i| i.opcode == Opcode::TexM3x2Depth)
    );
}

#[test]
fn pixel_1_4_reads_texld_texcrd_bem_phase_and_texdepth() {
    let bytes = program(
        0xffff_0104,
        &[
            // texld r0, t0; texcrd r1.rgb, t1_dw.xyw
            op1(66, &[dst(TEMP, 0, 15), src(TEXTURE, 0, 0xe4, 0)]),
            op1(64, &[dst(TEMP, 1, 7), src(TEXTURE, 1, 0xf4, 10)]),
            // bem r2.rg, c0, r0
            op1(
                89,
                &[dst(TEMP, 2, 3), src(CONST, 0, 0xe4, 0), src(TEMP, 0, 0xe4, 0)],
            ),
            vec![0xfffd],
            // texld r3, r2; texld r4, r1_dz.xyz; texdepth r5
            op1(66, &[dst(TEMP, 3, 15), src(TEMP, 2, 0xe4, 0)]),
            op1(66, &[dst(TEMP, 4, 15), src(TEMP, 1, 0xa4, 9)]),
            op1(87, &[dst(TEMP, 5, 15)]),
            // mul_x4_sat r0, r3, v0 / cmp r0.rgb, r0, c0, r4 / +mov r0.a, c0.b
            op1(
                5,
                &[
                    dst(TEMP, 0, 15) | 2 << 24 | 1 << 20,
                    src(TEMP, 3, 0xe4, 0),
                    src(INPUT, 0, 0xe4, 0),
                ],
            ),
            op1(
                88,
                &[
                    dst(TEMP, 0, 7),
                    src(TEMP, 0, 0xe4, 0),
                    src(CONST, 0, 0xe4, 0),
                    src(TEMP, 4, 0xe4, 0),
                ],
            ),
            [vec![0x4000_0001], vec![dst(TEMP, 0, 8), src(CONST, 0, 0xaa, 0)]].concat(),
            // dp3_d2 r1, r0_bx2, c0
            op1(
                8,
                &[
                    dst(TEMP, 1, 15) | 15 << 24,
                    src(TEMP, 0, 0xe4, 4),
                    src(CONST, 0, 0xe4, 0),
                ],
            ),
        ],
    );
    let p = read(&bytes).unwrap();
    let ops: Vec<_> = p.instructions().map(|i| (i.opcode, i.sources.len())).collect();
    use Opcode::*;
    assert_eq!(
        ops,
        [
            (Tex, 1),
            (TexCoord, 1),
            (Bem, 2),
            (Phase, 0),
            (Tex, 1),
            (Tex, 1),
            (TexDepth, 0),
            (Mul, 2),
            (Cmp, 3),
            (Mov, 1),
            (Dp3, 2)
        ]
    );
    let i: Vec<_> = p.instructions().collect();
    assert_eq!(i[1].sources[0].modifier, Modifier::DivideW);
    assert_eq!(i[5].sources[0].modifier, Modifier::DivideZ);
    let d = i[7].destination.unwrap();
    assert_eq!((d.shift, d.saturate), (2, true));
    assert!(i[9].coissue);
    // phase is 1.4's only, and is the bare token
    let mut phase = bytes.clone();
    phase[..4].copy_from_slice(&0xffff_0103u32.to_le_bytes());
    assert!(read(&phase).is_err());
    let at = 4 * p.instructions().nth(3).unwrap().word;
    let mut flagged = bytes.clone();
    flagged[at + 3] = 0x40;
    assert!(refusal(&flagged).1.contains("phase token"));
}

#[test]
fn shader_model_1_lengths_are_the_opcode_table_s_or_the_stated_one() {
    // a 1.x instruction may state its length; it must then be its operands'
    let stated = program(
        0xfffe_0101,
        &[op(
            2,
            &[dst(TEMP, 0, 15), src(INPUT, 0, 0xe4, 0), src(INPUT, 1, 0xe4, 0)],
        )],
    );
    assert_eq!(read(&stated).unwrap().instructions().count(), 1);
    let wrong = program(
        0xfffe_0101,
        &[[
            vec![2 | 2 << 24],
            vec![dst(TEMP, 0, 15), src(INPUT, 0, 0xe4, 0), src(INPUT, 1, 0xe4, 0)],
        ]
        .concat()],
    );
    let (word, why) = refusal(&wrong);
    assert_eq!(word, 1);
    assert!(
        why.contains("states 2 operand tokens, where its operands take 3"),
        "{why}"
    );
    // a token missing at the end of the stream: the operand reading runs into END's word and past it
    let short = program(
        0xfffe_0101,
        &[op1(2, &[dst(TEMP, 0, 15), src(INPUT, 0, 0xe4, 0)])],
    );
    assert!(read(&short).is_err());
}

#[test]
fn a_result_shift_the_token_format_does_not_define_is_refused() {
    // shift 5 is none of x2, x4, x8, d2, d4 or d8 (Microsoft's disassembler writes no shift for 4 to 12)
    let bytes = program(
        0xffff_0101,
        &[op1(1, &[dst(TEMP, 0, 15) | 5 << 24, src(INPUT, 0, 0xe4, 0)])],
    );
    let (_, why) = refusal(&bytes);
    assert!(why.contains("result shift 5"), "{why}");
}

#[test]
fn instructions_and_registers_of_later_versions_are_read_in_shader_model_1() {
    // rep i0 in a vertex 1.1 program, texld r0, s0 in a pixel 1.4 one, and the pixel instruction cnd in a vertex
    // program: Microsoft's disassembler lists each, whatever the version
    let rep = program(0xfffe_0101, &[op1(38, &[src(CONSTINT, 0, 0xe4, 0)])]);
    let i = read(&rep).unwrap().instructions().next().unwrap().clone();
    assert_eq!(
        (i.opcode, i.sources[0].register),
        (
            Opcode::Rep,
            Register {
                kind: RegisterType::ConstInt,
                number: 0
            }
        )
    );
    let sampler = program(
        0xffff_0104,
        &[op1(66, &[dst(TEMP, 0, 15), src(SAMPLER, 0, 0xe4, 0)])],
    );
    let i = read(&sampler).unwrap().instructions().next().unwrap().clone();
    assert_eq!(i.sources[0].register.kind, RegisterType::Sampler);
    let cnd = program(
        0xfffe_0101,
        &[op1(
            80,
            &[
                dst(TEMP, 0, 15),
                src(INPUT, 0, 0xe4, 0),
                src(INPUT, 0, 0xe4, 0),
                src(INPUT, 0, 0xe4, 0),
            ],
        )],
    );
    let i = read(&cnd).unwrap().instructions().next().unwrap().clone();
    assert_eq!((i.opcode, i.sources.len()), (Opcode::Cnd, 3));
    // and the operand flags of other versions: a relative source in a pixel 1.x program (its implicit index is listed
    // `t0.x`), a relative destination, predication, co-issue in a vertex program, and a pixel 1.x shift in one
    let relative = program(
        0xffff_0101,
        &[op1(1, &[dst(TEMP, 0, 15), src(CONST, 0, 0xe4, 0) | 0x2000])],
    );
    let i = read(&relative).unwrap().instructions().next().unwrap().clone();
    assert!(i.sources[0].relative.is_some_and(|r| r.word.is_none()));
    let destination = program(
        0xfffe_0101,
        &[op1(1, &[dst(TEMP, 0, 15) | 0x2000, src(INPUT, 0, 0xe4, 0)])],
    );
    let i = read(&destination).unwrap().instructions().next().unwrap().clone();
    assert!(i.destination.unwrap().relative.is_some());
    let predicated = program(
        0xfffe_0101,
        &[op1(
            1 | 1 << 28,
            &[
                dst(TEMP, 0, 15),
                src(PREDICATE, 0, 0xe4, 0),
                src(INPUT, 0, 0xe4, 0),
            ],
        )],
    );
    let i = read(&predicated).unwrap().instructions().next().unwrap().clone();
    assert_eq!(i.predicate.unwrap().register.kind, RegisterType::Predicate);
    let coissued = program(
        0xfffe_0101,
        &[op1(1 | 1 << 30, &[dst(TEMP, 0, 15), src(INPUT, 0, 0xe4, 0)])],
    );
    assert!(read(&coissued).unwrap().instructions().next().unwrap().coissue);
    let shifted = program(
        0xfffe_0101,
        &[op1(1, &[dst(TEMP, 0, 15) | 1 << 24, src(INPUT, 0, 0xe4, 0)])],
    );
    let i = read(&shifted).unwrap().instructions().next().unwrap().clone();
    assert_eq!(i.destination.unwrap().shift, 1);
}