Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,30 @@ fn unsupported(offset: usize, what: Why) -> Stop {
Stop::Fail(ShaderError::Unsupported { offset, what })
}

/// Where a program's constant table starts: after its instructions, unless
/// a fixed-offset load reads below it.
fn code_table_start(
p: &crate::ir::Program,
addresses: &[&crate::resource::Address],
lowest: u32,
) -> u32 {
let code_end = p
.blocks
.iter()
.flat_map(|b| &b.insts)
.map(|i| i.machine.offset + 4 * i.machine.words.len())
.max()
.unwrap_or(0) as u32;
let fixed_below = addresses
.iter()
.any(|a| a.dynamic.is_none() && a.offset < code_end);
if fixed_below {
lowest
} else {
lowest.max(code_end & !3)
}
}

impl<'a> Lower<'a> {
fn new(x: &'a Parts<'a>, bad: &'a HashMap<Value, Why>) -> Self {
let p = x.p;
Expand Down Expand Up @@ -1701,22 +1725,26 @@ impl<'a> Lower<'a> {
let (g, base, len) = match self.code_table {
Some(t) => t,
None => {
let first = self
let addresses: Vec<&crate::resource::Address> = self
.x
.res
.scalar
.iter()
.map(|s| s.1)
.map(|s| &s.1)
.chain(self.x.res.descriptors.iter().flat_map(|d| {
d.source.leaves().into_iter().filter_map(|l| match l {
Source::Built { address, .. } => Some(*address),
Source::Built { address, .. } => Some(address),
_ => None,
})
}))
.filter(|a| a.param == CODE)
.collect();
let lowest = addresses
.iter()
.map(|a| a.offset & !3)
.min()
.ok_or("a code table")?;
let first = code_table_start(self.x.p, &addresses, lowest);
let code = &self.x.native.code;
let words: Vec<u32> = code
.get(first as usize..)
Expand Down Expand Up @@ -12291,3 +12319,6 @@ mod tests {
}
}


#[cfg(test)]
include!("lower/code_table_tests.rs");
26 changes: 26 additions & 0 deletions crates/victory-shader/src/emit/lower/code_table_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Tests for where a program's constant table starts.

use crate::{ir, resource};

const S_ENDPGM: u32 = 0xBF81_0000;

/// A program's constant table starts after its instructions, unless a
/// load at a fixed offset reads below that.
#[test]
fn the_code_table_starts_after_the_instructions() {
// s_nop 0, s_endpgm, then 1,000 dwords of constants.
let mut words = vec![0xBF80_0000, S_ENDPGM];
words.extend(0..1000u32);
let code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
let p = ir::build(&code, false, 256).unwrap();
let at = |offset: u32, dynamic: bool| resource::Address {
param: resource::CODE,
offset,
dynamic: dynamic.then_some(ir::Value(0)),
};
let (computed, fixed_after, fixed_inside) = (at(0, true), at(12, false), at(4, false));
assert_eq!(code_table_start(&p, &[&computed], 0), 8);
assert_eq!(code_table_start(&p, &[&computed, &fixed_after], 0), 8);
assert_eq!(code_table_start(&p, &[&computed, &fixed_inside], 0), 0);
assert_eq!(code_table_start(&p, &[&fixed_after], 12), 12);
}
Loading