Skip to content

Commit

Permalink
graphics: Make draw call size adjustable
Browse files Browse the repository at this point in the history
  • Loading branch information
not-fl3 committed Sep 12, 2024
1 parent 8571468 commit 5d63542
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
44 changes: 37 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,12 @@ impl MiniquadInputEvent {
impl Context {
const DEFAULT_BG_COLOR: Color = BLACK;

fn new() -> Context {
fn new(
update_on: conf::UpdateTrigger,
default_filter_mode: crate::FilterMode,
draw_call_vertex_capacity: usize,
draw_call_index_capacity: usize,
) -> Context {
let mut ctx: Box<dyn miniquad::RenderingBackend> =
miniquad::window::new_rendering_backend();
let (screen_width, screen_height) = miniquad::window::screen_size();
Expand Down Expand Up @@ -331,7 +336,11 @@ impl Context {
input_events: Vec::new(),

camera_matrix: None,
gl: QuadGl::new(&mut *ctx),
gl: QuadGl::new(
&mut *ctx,
draw_call_vertex_capacity,
draw_call_index_capacity,
),

ui_context: UiContext::new(&mut *ctx, screen_width, screen_height),
fonts_storage: text::FontsStorage::new(&mut *ctx),
Expand All @@ -354,9 +363,9 @@ impl Context {

quad_context: ctx,

default_filter_mode: crate::quad_gl::FilterMode::Linear,
default_filter_mode,
textures: crate::texture::TexturesContext::new(),
update_on: Default::default(),
update_on,
}
}

Expand Down Expand Up @@ -806,6 +815,18 @@ pub mod conf {
/// update_on will tell macroquad when to proceed with the event loop.
pub update_on: Option<UpdateTrigger>,
pub default_filter_mode: crate::FilterMode,
/// Macroquad performs automatic and static batching for each
/// draw_* call. For each draw call, it pre-allocate a huge cpu/gpu
/// buffer to add vertices to. When it exceeds the buffer, it allocates the
/// new one, marking the new draw call.
///
/// Some examples when altering those values migh be convinient:
/// - for huge 3d models that do not fit into a single draw call, increasing
/// the buffer size might be easier than splitting the model.
/// - when each draw_* call got its own material,
/// buffer size might be reduced to save some memory
pub draw_call_vertex_capacity: usize,
pub draw_call_index_capacity: usize,
}

impl Default for Conf {
Expand All @@ -814,6 +835,8 @@ pub mod conf {
miniquad_conf: miniquad::conf::Conf::default(),
update_on: Some(UpdateTrigger::default()),
default_filter_mode: crate::FilterMode::Linear,
draw_call_vertex_capacity: 10000,
draw_call_index_capacity: 5000,
}
}
}
Expand All @@ -825,6 +848,8 @@ impl From<miniquad::conf::Conf> for conf::Conf {
miniquad_conf: conf,
update_on: None,
default_filter_mode: crate::FilterMode::Linear,
draw_call_vertex_capacity: 10000,
draw_call_index_capacity: 5000,
}
}
}
Expand Down Expand Up @@ -852,15 +877,20 @@ impl Window {
miniquad_conf,
update_on,
default_filter_mode,
draw_call_vertex_capacity,
draw_call_index_capacity,
} = config.into();
miniquad::start(miniquad_conf, move || {
thread_assert::set_thread_id();
unsafe {
MAIN_FUTURE = Some(Box::pin(future));
}
let mut context = Context::new();
context.update_on = update_on.unwrap_or_default();
context.default_filter_mode = default_filter_mode;
let context = Context::new(
update_on.unwrap_or_default(),
default_filter_mode,
draw_call_vertex_capacity,
draw_call_index_capacity,
);
unsafe { CONTEXT = Some(context) };

Box::new(Stage {})
Expand Down
10 changes: 7 additions & 3 deletions src/quad_gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,11 @@ pub struct QuadGl {
}

impl QuadGl {
pub fn new(ctx: &mut dyn miniquad::RenderingBackend) -> QuadGl {
pub fn new(
ctx: &mut dyn miniquad::RenderingBackend,
max_vertices: usize,
max_indices: usize,
) -> QuadGl {
let white_texture = ctx.new_texture_from_rgba8(1, 1, &[255, 255, 255, 255]);

QuadGl {
Expand All @@ -605,8 +609,8 @@ impl QuadGl {
start_time: miniquad::date::now(),

white_texture: white_texture,
max_vertices: 10000,
max_indices: 5000,
max_vertices,
max_indices,
}
}

Expand Down

0 comments on commit 5d63542

Please sign in to comment.