| Crates.io | astrelis-render |
| lib.rs | astrelis-render |
| version | 0.1.0 |
| created_at | 2025-12-04 14:29:09.944372+00 |
| updated_at | 2026-01-21 12:00:49.147586+00 |
| description | Astrelis Core Rendering Module |
| homepage | |
| repository | https://github.com/hxyulin/astrelis |
| max_upload_size | |
| id | 1966536 |
| size | 482,469 |
Modular rendering framework for the Astrelis game engine.
astrelis-render provides a modular, extensible architecture for managing GPU resources and rendering. It wraps WGPU with higher-level abstractions while maintaining low-level control when needed.
context - Graphics context management (device, queue, adapter)window - Window rendering contexts and surface managementframe - Frame lifecycle and render pass buildersrenderer - Low-level extensible renderer for resource managementuse astrelis_render::{GraphicsContext, RenderableWindow, WindowContextDescriptor};
use astrelis_winit::{app::run_app, window::WindowDescriptor};
run_app(|ctx| {
// Create graphics context
let graphics_ctx = GraphicsContext::new_sync();
// Create window
let window = ctx.create_window(WindowDescriptor::default())?;
let window = RenderableWindow::new(window, graphics_ctx);
Box::new(MyApp { window })
});
use astrelis_render::Renderer;
let graphics_ctx = GraphicsContext::new_sync();
let renderer = Renderer::new(graphics_ctx);
// Create shader
let shader = renderer.create_shader(Some("My Shader"), shader_source);
// Create vertex buffer
let vertices: &[f32] = &[/* ... */];
let vertex_buffer = renderer.create_vertex_buffer(Some("Vertices"), vertices);
// Create texture
let texture = renderer.create_texture_2d(
Some("My Texture"),
width, height,
wgpu::TextureFormat::Rgba8UnormSrgb,
wgpu::TextureUsages::TEXTURE_BINDING,
texture_data,
);
// Create sampler
let sampler = renderer.create_linear_sampler(Some("Sampler"));
// Create bind group
let bind_group_layout = renderer.create_bind_group_layout(
Some("Layout"),
&[/* entries */],
);
let bind_group = renderer.create_bind_group(
Some("Bind Group"),
&bind_group_layout,
&[/* entries */],
);
impl App for MyApp {
fn update(&mut self, _ctx: &mut AppCtx) {
// Global logic
}
fn render(&mut self, _ctx: &mut AppCtx, window_id: WindowId, events: &mut EventBatch) {
if window_id != self.window_id {
return;
}
// Handle events
events.dispatch(|event| {
match event {
Event::WindowResized(size) => {
self.window.resized(*size);
HandleStatus::consumed()
}
_ => HandleStatus::ignored()
}
});
// Begin frame
let mut frame = self.window.begin_drawing();
{
// Create render pass
let mut render_pass = RenderPassBuilder::new()
.label("Main Pass")
.color_attachment(
None, // Use window surface
None,
wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
store: wgpu::StoreOp::Store,
},
)
.build(&mut frame);
let pass = render_pass.descriptor();
// ... render commands
}
frame.finish();
}
}
Manages the WGPU instance, adapter, device, and queue.
// Create with defaults
let ctx = GraphicsContext::new_sync();
// Create with custom descriptor
let ctx = GraphicsContext::new_with_descriptor(
GraphicsContextDescriptor {
backends: wgpu::Backends::VULKAN | wgpu::Backends::METAL,
power_preference: wgpu::PowerPreference::HighPerformance,
features: wgpu::Features::PUSH_CONSTANTS,
..Default::default()
}
).await;
// Query device info
let info = ctx.info();
let limits = ctx.limits();
let features = ctx.features();
Manages a window surface and its configuration.
let window_ctx = WindowContext::new(
window,
graphics_ctx,
WindowContextDescriptor {
format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
present_mode: Some(wgpu::PresentMode::Mailbox),
alpha_mode: Some(wgpu::CompositeAlphaMode::Opaque),
},
);
// Handle resize
window_ctx.resized(new_size);
// Reconfigure surface
window_ctx.reconfigure_surface(new_config);
// Access surface
let surface = window_ctx.surface();
let config = window_ctx.surface_config();
Low-level API for creating GPU resources.
// Vertex buffer
let vertex_buffer = renderer.create_vertex_buffer(label, vertices);
// Index buffer
let index_buffer = renderer.create_index_buffer(label, indices);
// Uniform buffer
let uniform_buffer = renderer.create_uniform_buffer(label, &uniforms);
// Update uniform
renderer.update_uniform_buffer(&uniform_buffer, &new_uniforms);
// 2D texture with data
let texture = renderer.create_texture_2d(
label,
width, height,
format,
usage,
data,
);
// Custom texture descriptor
let texture = renderer.create_texture(&descriptor);
// Linear sampler
let sampler = renderer.create_linear_sampler(label);
// Nearest sampler
let sampler = renderer.create_nearest_sampler(label);
// Custom sampler
let sampler = renderer.create_sampler(&descriptor);
// Create layout
let layout = renderer.create_bind_group_layout(label, &entries);
// Create bind group
let bind_group = renderer.create_bind_group(label, &layout, &entries);
// Create shader
let shader = renderer.create_shader(label, source);
// Create pipeline layout
let pipeline_layout = renderer.create_pipeline_layout(
label,
&[&bind_group_layout],
&push_constant_ranges,
);
// Create render pipeline
let pipeline = renderer.create_render_pipeline(&descriptor);
// Create compute pipeline
let compute_pipeline = renderer.create_compute_pipeline(&descriptor);
// Create encoder
let mut encoder = renderer.create_command_encoder(label);
// ... record commands
// Submit
renderer.submit(std::iter::once(encoder.finish()));
Builder for creating render passes with automatic encoder management.
let mut render_pass = RenderPassBuilder::new()
.label("My Pass")
.color_attachment(
Some(&texture_view),
None, // No resolve target
wgpu::Operations {
load: wgpu::LoadOp::Clear(color),
store: wgpu::StoreOp::Store,
},
)
.depth_stencil_attachment(
&depth_view,
Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: wgpu::StoreOp::Store,
}),
None, // No stencil
)
.build(&mut frame);
// Use the render pass
let pass = render_pass.descriptor();
pass.set_pipeline(&pipeline);
// ... render commands
// Encoder automatically returned to frame when dropped
All WGPU types are re-exported for convenience:
use astrelis_render::wgpu;
// Instead of:
// use wgpu::TextureFormat;
// You can use:
use astrelis_render::wgpu::TextureFormat;
The Renderer is designed as a foundation for specialized renderers:
pub struct TextRenderer {
renderer: Renderer,
pipeline: wgpu::RenderPipeline,
font_atlas: wgpu::Texture,
// ...
}
impl TextRenderer {
pub fn new(context: &'static GraphicsContext) -> Self {
let renderer = Renderer::new(context);
// Use renderer API to create resources
let shader = renderer.create_shader(/* ... */);
let font_atlas = renderer.create_texture_2d(/* ... */);
Self { renderer, pipeline, font_atlas }
}
pub fn draw_text(&mut self, text: &str, position: Vec2) {
// High-level text rendering API
}
}
See the examples/ directory for complete examples:
renderer_api.rs - Demonstrates the low-level Renderer APItextured_window.rs - Basic textured quad renderingmulti_window.rs - Multiple windows with different contentRun an example:
cargo run --package astrelis-render --example renderer_api
Part of the Astrelis game engine.