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 6159497
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
28 changes: 18 additions & 10 deletions examples/basic_shapes.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
use macroquad::prelude::*;

#[macroquad::main("BasicShapes")]
fn window_conf() -> macroquad::conf::Conf {
macroquad::conf::Conf {
draw_call_vertex_capacity: 20,
draw_call_index_capacity: 10,
..Default::default()
}
}

#[macroquad::main(window_conf)]
async fn main() {
let mut v: Vec<Texture2D> = vec![];
for i in 0..256 {

Check warning on line 14 in examples/basic_shapes.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

unused variable: `i`

Check warning on line 14 in examples/basic_shapes.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

unused variable: `i`
v.push(load_texture("examples/ferris.png").await.unwrap());
}
loop {
clear_background(LIGHTGRAY);

draw_line(40.0, 40.0, 100.0, 200.0, 15.0, BLUE);
draw_rectangle(screen_width() / 2.0 - 60.0, 100.0, 120.0, 60.0, GREEN);
draw_circle(screen_width() - 30.0, screen_height() - 30.0, 15.0, YELLOW);

draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);

next_frame().await
for i in 0..40 * 80 {
let j = i % 256;
draw_texture(&v[j], 0.0, 0.0, WHITE);
}
next_frame().await;
}
}
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 6159497

Please sign in to comment.