Skip to content
Closed
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
11 changes: 8 additions & 3 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1842,22 +1842,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 @@ -12242,6 +12246,7 @@ fn interp_mode(kind: Barycentric) -> Option<u8> {
})
}

include!("lower/code.rs");
include!("lower/methods.rs");
include!("lower/mesh_emu.rs");
#[cfg(test)]
Expand Down
28 changes: 28 additions & 0 deletions crates/victory-shader/src/emit/lower/code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// Where a program's constant table starts in its code: where its
/// instructions end, unless a load at a fixed offset reads below that (then
/// at `lowest`, the lowest offset any load or descriptor names). The
/// program's constants follow its instructions, and a load at a computed
/// offset reads them, not instruction bytes; a table from the first
/// computed load's offset 0 was the whole program, tens of KiB, which
/// Vulkan drivers can keep in every thread's local memory.
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)
}
}
23 changes: 22 additions & 1 deletion crates/victory-shader/src/emit/lower/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
use super::{Lowered, Numeric, Parts, buffer_format, half_shift, lower};
use super::{Lowered, Numeric, Parts, buffer_format, code_table_start, half_shift, lower};
use crate::native::{
ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource,
};
use crate::{io, ir, resource, structure};

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 constant data.
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);
}

#[test]
fn packed_pixel_inputs_find_their_second_row() {
use super::super::container::SigElement;
Expand Down
Loading