Skip to content

Commit 97d2d9b

Browse files
committed
Test.NativeLaunch: add "miss" primitives and conditionally enable thread device
- Can use the threaded device in release builds as it does not SO there - Add the "miss" stuff as the comments describe
1 parent 8435650 commit 97d2d9b

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

Test.NativeLaunch/src/main.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,26 @@ type D3D11CreateDeviceAndSwapChainFN = extern "system" fn (
249249

250250
/// Enabling this will prevent MM from hooking, see load_d3d11 comment below
251251
const USE_DEBUG_DEVICE:bool = false;
252-
/// When running with MM, if this is false, it may stack overflow when creating the d3d resources, for unknown reasons
252+
/// When running with MM, if this is false, it may stack overflow when creating the d3d resources, in the debug build,
253+
/// for unknown reasons. the release build does not appear to S.O.
254+
/// The SO appears to be related to the d3d load thread MM uses when the device says it is multithread.
255+
#[cfg(debug_assertions)]
253256
const SINGLE_THREADED_DEVICE: bool = true;
257+
#[cfg(not(debug_assertions))]
258+
const SINGLE_THREADED_DEVICE: bool = false;
254259
/// Whether to render or not.
255260
const RENDER:bool = true;
256-
/// If true sometimes the program will not render the full geometry causing a "miss" in modelmod,
261+
/// If > 0, the program will not sometimes render the full geometry causing a "miss" in modelmod,
257262
/// in other words no matching mod which is what it does most of the time in practice.
258263
/// (If this is true and RENDER is true and a valid MOD exists for the prim/vert count,
259264
/// this will cause the mod to flicker on screen
260-
/// since nothing is drawn otherwise)
261-
const RENDER_SIMULATE_MISS:bool = false;
265+
/// since nothing is drawn otherwise). The value is the chance of a miss.
266+
const RENDER_SIMULATE_MISS_FREQ:f32 = 0.0; // 0.0005;
267+
/// If set, when a miss occurs (see RENDER_SIMULATE_MISS_FREQ) this number of primitives/vertexes
268+
/// will be used to draw instead of a random amount. This allows a different mod to be selected for the miss.
269+
/// These values must be <= the primary prim/vert counts used for the program. The
270+
/// program will exit with an error if they are greater.
271+
const RENDER_MISS_PRIMVERT_COUNT:Option<(u32,u32)> = None; // Some((7630,5435));
262272

263273
// load the d3d11 library and obtain a pointer to the D3D11CreateDevice function
264274
unsafe fn load_d3d11() -> Option<D3D11CreateDeviceAndSwapChainFN> {
@@ -810,7 +820,24 @@ unsafe fn runapp() -> anyhow::Result<()> {
810820
let index_buffer = create_index_buffer(
811821
device, num_indices as u32, |_num_indices| index_data)?; //.try_into().expect("can't convert to u32?")?;
812822

813-
use std::ptr::null_mut;
823+
let (miss_prims, vertbuf_miss, indexbuf_miss) = if let Some((miss_prim, miss_vert)) = RENDER_MISS_PRIMVERT_COUNT {
824+
if miss_prim >= prim_count as u32 || miss_vert >= vert_count as u32 {
825+
panic!("miss primitives/vertex counts must be lower than program argument prim,verts");
826+
}
827+
let vert_data = get_empty_vertices(vert_size, miss_vert as usize)
828+
.expect("failed to create empty vert buf for miss case");
829+
let index_data = get_indices((miss_prim * 3) as u32);
830+
831+
let (vertex_buffer,vsize) = create_vertex_buffer(device, || (vert_data, vert_size))?;
832+
if vsize != vert_size {
833+
panic!("oops miss vertex buffer size does not match");
834+
}
835+
let index_buffer = create_index_buffer(device, (miss_prim * 3) as u32, |_| index_data)?;
836+
837+
(miss_prim, vertex_buffer, index_buffer)
838+
} else {
839+
(0, null_mut(), null_mut())
840+
};
814841

815842
let rend_data = if RENDER {
816843
Some(render::create_data(device)?)
@@ -1002,7 +1029,8 @@ unsafe fn runapp() -> anyhow::Result<()> {
10021029
let pStrides = [vert_size as u32];
10031030
let pOffsets = [0];
10041031
let ppVertexBuffers = [vertex_buffer];
1005-
let ppVertexBuffers = ppVertexBuffers;
1032+
let ppVertexBuffers = ppVertexBuffers;
1033+
let missPPVertexBuffers = [vertbuf_miss];
10061034

10071035
let the_beginning = SystemTime::now();
10081036
let mut msg;
@@ -1258,13 +1286,21 @@ unsafe fn runapp() -> anyhow::Result<()> {
12581286

12591287
let primCount = if let Mesh::Shape = opts.mesh {
12601288
max_prims
1261-
} else if RENDER_SIMULATE_MISS {
1289+
} else if RENDER_SIMULATE_MISS_FREQ > 0.0 {
12621290
// a small pct of the time, draw the target primCount, the rest draw a random number of
12631291
// prims up to primCount (simulates the high miss rate in mod rendering)
1264-
if rand::random::<f32>() < 0.05 {
1292+
if rand::random::<f32>() > RENDER_SIMULATE_MISS_FREQ {
12651293
max_prims
12661294
} else {
1267-
rand::random::<usize>() % max_prims
1295+
if miss_prims > 0 {
1296+
(*context).IASetIndexBuffer(indexbuf_miss, DXGI_FORMAT_R16_UINT, 0);
1297+
1298+
(*context).IASetVertexBuffers(0, 1,
1299+
missPPVertexBuffers.as_ptr(), pStrides.as_ptr(), pOffsets.as_ptr());
1300+
miss_prims as usize
1301+
} else {
1302+
rand::random::<usize>() % max_prims
1303+
}
12681304
}
12691305
} else {
12701306
max_prims

0 commit comments

Comments
 (0)