2023-12-22 16:51:21 -08:00
|
|
|
use anyhow::Result;
|
|
|
|
use bytemuck;
|
2024-01-08 17:57:49 -08:00
|
|
|
use cgmath::Point2;
|
2024-01-01 10:44:55 -08:00
|
|
|
use galactica_constants;
|
2024-01-08 15:17:20 -08:00
|
|
|
|
2024-01-05 18:04:30 -08:00
|
|
|
use rand::seq::SliceRandom;
|
2024-01-03 06:37:02 -08:00
|
|
|
use std::{iter, rc::Rc};
|
2024-01-08 19:11:46 -08:00
|
|
|
use wgpu::{self, BufferAddress};
|
2024-01-08 17:57:49 -08:00
|
|
|
use winit::{self, window::Window};
|
2023-12-22 16:51:21 -08:00
|
|
|
|
2023-12-31 18:48:35 -08:00
|
|
|
use crate::{
|
2024-01-01 10:44:55 -08:00
|
|
|
content,
|
2024-01-08 15:17:20 -08:00
|
|
|
globaluniform::{GlobalDataContent, GlobalUniform},
|
2023-12-23 11:03:06 -08:00
|
|
|
pipeline::PipelineBuilder,
|
2024-01-01 09:45:27 -08:00
|
|
|
starfield::Starfield,
|
2023-12-22 21:39:47 -08:00
|
|
|
texturearray::TextureArray,
|
2023-12-23 12:52:36 -08:00
|
|
|
vertexbuffer::{
|
2023-12-25 11:17:08 -08:00
|
|
|
consts::{SPRITE_INDICES, SPRITE_VERTICES},
|
2024-01-08 15:17:20 -08:00
|
|
|
types::{
|
|
|
|
ObjectInstance, ParticleInstance, RadialBarInstance, StarfieldInstance, TexturedVertex,
|
|
|
|
UiInstance,
|
|
|
|
},
|
2024-01-02 19:11:18 -08:00
|
|
|
BufferObject, VertexBuffer,
|
2023-12-23 12:52:36 -08:00
|
|
|
},
|
2024-01-08 17:57:49 -08:00
|
|
|
RenderState,
|
2023-12-22 21:39:47 -08:00
|
|
|
};
|
2023-12-22 16:51:21 -08:00
|
|
|
|
2024-01-08 15:17:20 -08:00
|
|
|
// Additional implementaitons for GPUState
|
|
|
|
mod hud;
|
|
|
|
mod world;
|
|
|
|
|
2023-12-31 18:59:08 -08:00
|
|
|
/// A high-level GPU wrapper. Consumes game state,
|
|
|
|
/// produces pretty pictures.
|
2023-12-22 16:51:21 -08:00
|
|
|
pub struct GPUState {
|
2023-12-31 18:59:08 -08:00
|
|
|
/// The window to we draw on
|
|
|
|
pub window: Window,
|
|
|
|
|
|
|
|
/// The size of the window we draw on
|
|
|
|
pub window_size: winit::dpi::PhysicalSize<u32>,
|
|
|
|
|
2023-12-22 16:51:21 -08:00
|
|
|
device: wgpu::Device,
|
|
|
|
config: wgpu::SurfaceConfiguration,
|
|
|
|
surface: wgpu::Surface,
|
|
|
|
queue: wgpu::Queue,
|
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
window_aspect: f32,
|
2023-12-22 16:51:21 -08:00
|
|
|
|
2024-01-02 09:23:38 -08:00
|
|
|
object_pipeline: wgpu::RenderPipeline,
|
2023-12-23 11:03:06 -08:00
|
|
|
starfield_pipeline: wgpu::RenderPipeline,
|
2024-01-02 19:11:18 -08:00
|
|
|
particle_pipeline: wgpu::RenderPipeline,
|
2024-01-02 09:23:38 -08:00
|
|
|
ui_pipeline: wgpu::RenderPipeline,
|
2024-01-08 15:17:20 -08:00
|
|
|
radialbar_pipeline: wgpu::RenderPipeline,
|
2023-12-22 16:51:21 -08:00
|
|
|
|
2024-01-01 09:45:27 -08:00
|
|
|
starfield: Starfield,
|
2023-12-22 16:51:21 -08:00
|
|
|
texture_array: TextureArray,
|
2024-01-04 17:18:31 -08:00
|
|
|
global_uniform: GlobalUniform,
|
2023-12-23 12:52:36 -08:00
|
|
|
vertex_buffers: VertexBuffers,
|
|
|
|
}
|
2023-12-23 11:03:06 -08:00
|
|
|
|
2023-12-23 12:52:36 -08:00
|
|
|
struct VertexBuffers {
|
2024-01-08 19:11:46 -08:00
|
|
|
// Keeps track of length of each buffer
|
|
|
|
pub object_counter: BufferAddress,
|
|
|
|
pub ui_counter: BufferAddress,
|
|
|
|
pub particle_counter: BufferAddress,
|
|
|
|
pub radialbar_counter: BufferAddress,
|
|
|
|
|
2024-01-02 09:23:38 -08:00
|
|
|
object: Rc<VertexBuffer>,
|
2023-12-23 12:52:36 -08:00
|
|
|
starfield: Rc<VertexBuffer>,
|
2024-01-02 09:23:38 -08:00
|
|
|
ui: Rc<VertexBuffer>,
|
2024-01-02 19:11:18 -08:00
|
|
|
|
|
|
|
particle: Rc<VertexBuffer>,
|
2024-01-08 15:17:20 -08:00
|
|
|
radialbar: Rc<VertexBuffer>,
|
2023-12-22 16:51:21 -08:00
|
|
|
}
|
|
|
|
|
2024-01-04 21:30:12 -08:00
|
|
|
/// Basic wgsl preprocesser
|
2024-01-04 17:18:31 -08:00
|
|
|
fn preprocess_shader(
|
|
|
|
shader: &str,
|
|
|
|
global_uniform: &GlobalUniform,
|
|
|
|
global_uniform_group: u32,
|
|
|
|
) -> String {
|
2024-01-04 21:30:12 -08:00
|
|
|
// Insert dynamically-generated global definitions
|
|
|
|
let shader = shader.replace(
|
2024-01-04 17:18:31 -08:00
|
|
|
"// INCLUDE: global uniform header",
|
|
|
|
&global_uniform.shader_header(global_uniform_group),
|
2024-01-04 21:30:12 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Insert common functions
|
|
|
|
let shader = shader.replace(
|
|
|
|
"// INCLUDE: animate.wgsl",
|
|
|
|
&include_str!(concat!(
|
|
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
|
|
"/shaders/include/",
|
|
|
|
"animate.wgsl"
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-01-08 17:57:49 -08:00
|
|
|
let shader = shader.replace(
|
|
|
|
"// INCLUDE: anchor.wgsl",
|
|
|
|
&include_str!(concat!(
|
|
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
|
|
"/shaders/include/",
|
|
|
|
"anchor.wgsl"
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-01-04 21:30:12 -08:00
|
|
|
return shader;
|
2024-01-04 17:18:31 -08:00
|
|
|
}
|
|
|
|
|
2023-12-22 16:51:21 -08:00
|
|
|
impl GPUState {
|
2023-12-31 18:59:08 -08:00
|
|
|
/// Make a new GPUState that draws on `window`
|
2023-12-31 18:48:35 -08:00
|
|
|
pub async fn new(window: Window, ct: &content::Content) -> Result<Self> {
|
2023-12-23 14:07:12 -08:00
|
|
|
let window_size = window.inner_size();
|
|
|
|
let window_aspect = window_size.width as f32 / window_size.height as f32;
|
2023-12-22 16:51:21 -08:00
|
|
|
|
|
|
|
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
|
|
|
backends: wgpu::Backends::all(),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
let surface = unsafe { instance.create_surface(&window) }.unwrap();
|
|
|
|
|
|
|
|
// Basic setup
|
|
|
|
let device;
|
|
|
|
let queue;
|
|
|
|
let config;
|
|
|
|
|
|
|
|
{
|
|
|
|
let adapter = instance
|
|
|
|
.request_adapter(&wgpu::RequestAdapterOptions {
|
|
|
|
power_preference: wgpu::PowerPreference::default(),
|
|
|
|
compatible_surface: Some(&surface),
|
|
|
|
force_fallback_adapter: false,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
(device, queue) = adapter
|
|
|
|
.request_device(
|
|
|
|
&wgpu::DeviceDescriptor {
|
|
|
|
features: wgpu::Features::TEXTURE_BINDING_ARRAY | wgpu::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING,
|
|
|
|
// We may need limits if we compile for wasm
|
|
|
|
limits: wgpu::Limits::default(),
|
|
|
|
label: Some("gpu device"),
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Assume sRGB
|
|
|
|
let surface_caps = surface.get_capabilities(&adapter);
|
|
|
|
let surface_format = surface_caps
|
|
|
|
.formats
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.filter(|f| f.is_srgb())
|
|
|
|
.filter(|f| f.has_stencil_aspect())
|
|
|
|
.next()
|
|
|
|
.unwrap_or(surface_caps.formats[0]);
|
|
|
|
|
|
|
|
config = wgpu::SurfaceConfiguration {
|
|
|
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
|
|
|
format: surface_format,
|
2023-12-23 14:07:12 -08:00
|
|
|
width: window_size.width,
|
|
|
|
height: window_size.height,
|
2023-12-22 16:51:21 -08:00
|
|
|
present_mode: surface_caps.present_modes[0],
|
|
|
|
alpha_mode: surface_caps.alpha_modes[0],
|
|
|
|
view_formats: vec![],
|
|
|
|
};
|
|
|
|
|
|
|
|
surface.configure(&device, &config);
|
|
|
|
}
|
|
|
|
|
2023-12-23 12:52:36 -08:00
|
|
|
let vertex_buffers = VertexBuffers {
|
2024-01-08 19:11:46 -08:00
|
|
|
object_counter: 0,
|
|
|
|
ui_counter: 0,
|
|
|
|
particle_counter: 0,
|
|
|
|
radialbar_counter: 0,
|
|
|
|
|
2024-01-02 10:12:10 -08:00
|
|
|
object: Rc::new(VertexBuffer::new::<TexturedVertex, ObjectInstance>(
|
2024-01-02 19:11:18 -08:00
|
|
|
"object",
|
2023-12-23 12:52:36 -08:00
|
|
|
&device,
|
|
|
|
Some(SPRITE_VERTICES),
|
|
|
|
Some(SPRITE_INDICES),
|
2024-01-02 09:23:38 -08:00
|
|
|
galactica_constants::OBJECT_SPRITE_INSTANCE_LIMIT,
|
2023-12-23 12:52:36 -08:00
|
|
|
)),
|
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
starfield: Rc::new(VertexBuffer::new::<TexturedVertex, StarfieldInstance>(
|
2023-12-23 12:52:36 -08:00
|
|
|
"starfield",
|
|
|
|
&device,
|
2023-12-23 23:24:04 -08:00
|
|
|
Some(SPRITE_VERTICES),
|
|
|
|
Some(SPRITE_INDICES),
|
2024-01-02 09:23:38 -08:00
|
|
|
galactica_constants::STARFIELD_SPRITE_INSTANCE_LIMIT,
|
|
|
|
)),
|
|
|
|
|
2024-01-02 10:12:10 -08:00
|
|
|
ui: Rc::new(VertexBuffer::new::<TexturedVertex, UiInstance>(
|
2024-01-02 09:23:38 -08:00
|
|
|
"ui",
|
|
|
|
&device,
|
|
|
|
Some(SPRITE_VERTICES),
|
|
|
|
Some(SPRITE_INDICES),
|
|
|
|
galactica_constants::UI_SPRITE_INSTANCE_LIMIT,
|
2023-12-23 12:52:36 -08:00
|
|
|
)),
|
2024-01-02 19:11:18 -08:00
|
|
|
|
|
|
|
particle: Rc::new(VertexBuffer::new::<TexturedVertex, ParticleInstance>(
|
|
|
|
"particle",
|
|
|
|
&device,
|
|
|
|
Some(SPRITE_VERTICES),
|
|
|
|
Some(SPRITE_INDICES),
|
|
|
|
galactica_constants::PARTICLE_SPRITE_INSTANCE_LIMIT,
|
|
|
|
)),
|
2024-01-08 15:17:20 -08:00
|
|
|
|
|
|
|
radialbar: Rc::new(VertexBuffer::new::<TexturedVertex, RadialBarInstance>(
|
|
|
|
"radial bar",
|
|
|
|
&device,
|
|
|
|
Some(SPRITE_VERTICES),
|
|
|
|
Some(SPRITE_INDICES),
|
|
|
|
10,
|
|
|
|
)),
|
2023-12-23 12:52:36 -08:00
|
|
|
};
|
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
// Load uniforms
|
2024-01-04 17:18:31 -08:00
|
|
|
let global_uniform = GlobalUniform::new(&device);
|
2023-12-30 10:58:17 -08:00
|
|
|
let texture_array = TextureArray::new(&device, &queue, ct)?;
|
2023-12-22 16:51:21 -08:00
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
// Make sure these match the indices in each shader
|
|
|
|
let bind_group_layouts = &[
|
|
|
|
&texture_array.bind_group_layout,
|
2024-01-04 17:18:31 -08:00
|
|
|
&global_uniform.bind_group_layout,
|
2023-12-23 23:24:04 -08:00
|
|
|
];
|
|
|
|
|
2023-12-23 12:52:36 -08:00
|
|
|
// Create render pipelines
|
2024-01-02 09:23:38 -08:00
|
|
|
let object_pipeline = PipelineBuilder::new("object", &device)
|
2024-01-04 17:18:31 -08:00
|
|
|
.set_shader(&preprocess_shader(
|
|
|
|
&include_str!(concat!(
|
|
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
|
|
"/shaders/",
|
|
|
|
"object.wgsl"
|
|
|
|
)),
|
|
|
|
&global_uniform,
|
|
|
|
1,
|
|
|
|
))
|
2023-12-23 11:03:06 -08:00
|
|
|
.set_format(config.format)
|
|
|
|
.set_triangle(true)
|
2024-01-02 09:23:38 -08:00
|
|
|
.set_vertex_buffer(&vertex_buffers.object)
|
2023-12-23 23:24:04 -08:00
|
|
|
.set_bind_group_layouts(bind_group_layouts)
|
2023-12-23 11:03:06 -08:00
|
|
|
.build();
|
|
|
|
|
|
|
|
let starfield_pipeline = PipelineBuilder::new("starfield", &device)
|
2024-01-04 17:18:31 -08:00
|
|
|
.set_shader(&preprocess_shader(
|
|
|
|
&include_str!(concat!(
|
|
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
|
|
"/shaders/",
|
|
|
|
"starfield.wgsl"
|
|
|
|
)),
|
|
|
|
&global_uniform,
|
|
|
|
1,
|
|
|
|
))
|
2023-12-23 11:03:06 -08:00
|
|
|
.set_format(config.format)
|
2023-12-23 23:24:04 -08:00
|
|
|
.set_triangle(true)
|
2023-12-23 12:52:36 -08:00
|
|
|
.set_vertex_buffer(&vertex_buffers.starfield)
|
2023-12-23 23:24:04 -08:00
|
|
|
.set_bind_group_layouts(bind_group_layouts)
|
2023-12-23 11:03:06 -08:00
|
|
|
.build();
|
|
|
|
|
2024-01-02 09:23:38 -08:00
|
|
|
let ui_pipeline = PipelineBuilder::new("ui", &device)
|
2024-01-04 17:18:31 -08:00
|
|
|
.set_shader(&preprocess_shader(
|
|
|
|
&include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/shaders/", "ui.wgsl")),
|
|
|
|
&global_uniform,
|
|
|
|
1,
|
|
|
|
))
|
2024-01-02 09:23:38 -08:00
|
|
|
.set_format(config.format)
|
|
|
|
.set_triangle(true)
|
|
|
|
.set_vertex_buffer(&vertex_buffers.ui)
|
|
|
|
.set_bind_group_layouts(bind_group_layouts)
|
|
|
|
.build();
|
|
|
|
|
2024-01-02 19:11:18 -08:00
|
|
|
let particle_pipeline = PipelineBuilder::new("particle", &device)
|
2024-01-04 17:18:31 -08:00
|
|
|
.set_shader(&preprocess_shader(
|
|
|
|
&include_str!(concat!(
|
|
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
|
|
"/shaders/",
|
|
|
|
"particle.wgsl"
|
|
|
|
)),
|
|
|
|
&global_uniform,
|
|
|
|
1,
|
|
|
|
))
|
2024-01-02 19:11:18 -08:00
|
|
|
.set_format(config.format)
|
|
|
|
.set_triangle(true)
|
|
|
|
.set_vertex_buffer(&vertex_buffers.particle)
|
|
|
|
.set_bind_group_layouts(bind_group_layouts)
|
|
|
|
.build();
|
|
|
|
|
2024-01-08 15:17:20 -08:00
|
|
|
let radialbar_pipeline = PipelineBuilder::new("radialbar", &device)
|
|
|
|
.set_shader(&preprocess_shader(
|
|
|
|
&include_str!(concat!(
|
|
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
|
|
"/shaders/",
|
|
|
|
"radialbar.wgsl"
|
|
|
|
)),
|
|
|
|
&global_uniform,
|
|
|
|
1,
|
|
|
|
))
|
|
|
|
.set_format(config.format)
|
|
|
|
.set_triangle(true)
|
|
|
|
.set_vertex_buffer(&vertex_buffers.radialbar)
|
|
|
|
.set_bind_group_layouts(bind_group_layouts)
|
|
|
|
.build();
|
|
|
|
|
2024-01-01 09:45:27 -08:00
|
|
|
let mut starfield = Starfield::new();
|
|
|
|
starfield.regenerate();
|
|
|
|
|
2023-12-22 16:51:21 -08:00
|
|
|
return Ok(Self {
|
|
|
|
device,
|
|
|
|
config,
|
2023-12-23 12:52:36 -08:00
|
|
|
surface,
|
|
|
|
queue,
|
|
|
|
|
2023-12-22 16:51:21 -08:00
|
|
|
window,
|
2023-12-23 14:07:12 -08:00
|
|
|
window_size,
|
|
|
|
window_aspect,
|
2023-12-23 12:52:36 -08:00
|
|
|
|
2024-01-02 09:23:38 -08:00
|
|
|
object_pipeline,
|
2023-12-23 11:03:06 -08:00
|
|
|
starfield_pipeline,
|
2024-01-02 09:23:38 -08:00
|
|
|
ui_pipeline,
|
2024-01-02 19:11:18 -08:00
|
|
|
particle_pipeline,
|
2024-01-08 15:17:20 -08:00
|
|
|
radialbar_pipeline,
|
2023-12-23 12:52:36 -08:00
|
|
|
|
2024-01-01 09:45:27 -08:00
|
|
|
starfield,
|
2023-12-22 16:51:21 -08:00
|
|
|
texture_array,
|
2024-01-04 17:18:31 -08:00
|
|
|
global_uniform,
|
2023-12-23 12:52:36 -08:00
|
|
|
vertex_buffers,
|
2023-12-22 16:51:21 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-31 18:59:08 -08:00
|
|
|
/// Get the window this GPUState is attached to
|
2023-12-22 16:51:21 -08:00
|
|
|
pub fn window(&self) -> &Window {
|
|
|
|
&self.window
|
|
|
|
}
|
|
|
|
|
2023-12-31 18:59:08 -08:00
|
|
|
/// Update window size.
|
|
|
|
/// This should be called whenever our window is resized.
|
2024-01-01 09:45:27 -08:00
|
|
|
pub fn resize(&mut self) {
|
2023-12-31 18:59:08 -08:00
|
|
|
let new_size = self.window.inner_size();
|
2023-12-22 16:51:21 -08:00
|
|
|
if new_size.width > 0 && new_size.height > 0 {
|
2023-12-23 14:07:12 -08:00
|
|
|
self.window_size = new_size;
|
|
|
|
self.window_aspect = new_size.width as f32 / new_size.height as f32;
|
2023-12-22 16:51:21 -08:00
|
|
|
self.config.width = new_size.width;
|
|
|
|
self.config.height = new_size.height;
|
|
|
|
self.surface.configure(&self.device, &self.config);
|
|
|
|
}
|
2024-01-01 09:45:27 -08:00
|
|
|
self.update_starfield_buffer()
|
2023-12-22 16:51:21 -08:00
|
|
|
}
|
|
|
|
|
2024-01-08 19:11:46 -08:00
|
|
|
/// Entrypoint for all vertex buffer builders
|
|
|
|
pub(super) fn update_all_buffers(&mut self, state: &RenderState) {
|
2023-12-22 21:39:47 -08:00
|
|
|
// Game coordinates (relative to camera) of ne and sw corners of screen.
|
|
|
|
// Used to skip off-screen sprites.
|
2024-01-04 18:15:30 -08:00
|
|
|
let clip_ne = Point2::from((-self.window_aspect, 1.0)) * state.camera_zoom;
|
|
|
|
let clip_sw = Point2::from((self.window_aspect, -1.0)) * state.camera_zoom;
|
|
|
|
|
2024-01-08 19:11:46 -08:00
|
|
|
// TODO: sorting. We don't need to sort ships, but we do need to sort system objects by z-level
|
|
|
|
// (which we don't yet draw)
|
|
|
|
// that should probably be done in iter_system().
|
|
|
|
|
|
|
|
// Order matters, it determines what is drawn on top.
|
|
|
|
// The order inside ships and projectiles doesn't matter,
|
|
|
|
// but ships should always be under projectiles.
|
2024-01-09 21:14:57 -08:00
|
|
|
self.world_push_system(state, (clip_ne, clip_sw));
|
|
|
|
self.world_push_ship(state, (clip_ne, clip_sw));
|
|
|
|
self.world_push_projectile(state, (clip_ne, clip_sw));
|
2024-01-02 09:23:38 -08:00
|
|
|
|
2024-01-08 19:11:46 -08:00
|
|
|
self.hud_add_radar(state);
|
|
|
|
self.hud_add_status(state);
|
2023-12-23 14:07:12 -08:00
|
|
|
}
|
2023-12-23 11:03:06 -08:00
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
/// Make a StarfieldInstance for each star that needs to be drawn.
|
|
|
|
/// Will panic if STARFIELD_INSTANCE_LIMIT is exceeded.
|
|
|
|
///
|
2023-12-24 09:34:39 -08:00
|
|
|
/// Starfield data rarely changes, so this is called only when it's needed.
|
2024-01-01 09:45:27 -08:00
|
|
|
pub fn update_starfield_buffer(&mut self) {
|
2023-12-24 07:33:09 -08:00
|
|
|
self.queue.write_buffer(
|
|
|
|
&self.vertex_buffers.starfield.instances,
|
|
|
|
0,
|
2024-01-01 09:45:27 -08:00
|
|
|
bytemuck::cast_slice(&self.starfield.make_instances(self.window_aspect)),
|
2023-12-24 07:33:09 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-04 17:18:31 -08:00
|
|
|
/// Initialize the rendering engine
|
|
|
|
pub fn init(&mut self) {
|
|
|
|
// Update global values
|
|
|
|
self.queue.write_buffer(
|
|
|
|
&self.global_uniform.atlas_buffer,
|
|
|
|
0,
|
2024-01-05 19:56:26 -08:00
|
|
|
bytemuck::cast_slice(&[self.texture_array.image_locations]),
|
2024-01-04 18:15:30 -08:00
|
|
|
);
|
|
|
|
self.queue.write_buffer(
|
|
|
|
&self.global_uniform.sprite_buffer,
|
|
|
|
0,
|
2024-01-05 19:56:26 -08:00
|
|
|
bytemuck::cast_slice(&[self.texture_array.sprite_data]),
|
2024-01-04 17:18:31 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
self.update_starfield_buffer();
|
|
|
|
}
|
|
|
|
|
2023-12-31 18:59:08 -08:00
|
|
|
/// Main render function. Draws sprites on a window.
|
2024-01-04 18:15:30 -08:00
|
|
|
pub fn render(&mut self, state: RenderState) -> Result<(), wgpu::SurfaceError> {
|
2023-12-23 14:07:12 -08:00
|
|
|
let output = self.surface.get_current_texture()?;
|
2024-01-08 15:17:20 -08:00
|
|
|
let view = output.texture.create_view(&Default::default());
|
2023-12-23 14:07:12 -08:00
|
|
|
|
|
|
|
let mut encoder = self
|
|
|
|
.device
|
|
|
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
|
|
|
label: Some("render encoder"),
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
|
|
label: Some("render pass"),
|
|
|
|
|
|
|
|
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
|
|
|
view: &view,
|
|
|
|
resolve_target: None,
|
|
|
|
ops: wgpu::Operations {
|
|
|
|
load: wgpu::LoadOp::Clear(wgpu::Color {
|
|
|
|
r: 0.0,
|
|
|
|
g: 0.0,
|
|
|
|
b: 0.0,
|
|
|
|
a: 1.0,
|
|
|
|
}),
|
|
|
|
store: wgpu::StoreOp::Store,
|
|
|
|
},
|
|
|
|
})],
|
|
|
|
depth_stencil_attachment: None,
|
|
|
|
occlusion_query_set: None,
|
|
|
|
timestamp_writes: None,
|
|
|
|
});
|
|
|
|
|
2024-01-08 19:11:46 -08:00
|
|
|
self.vertex_buffers.object_counter = 0;
|
|
|
|
self.vertex_buffers.ui_counter = 0;
|
|
|
|
self.vertex_buffers.radialbar_counter = 0;
|
|
|
|
// Don't reset particle counter, it's special
|
|
|
|
|
2024-01-04 18:15:30 -08:00
|
|
|
let s = state.content.get_starfield_handle();
|
2024-01-05 19:56:26 -08:00
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
// Update global values
|
|
|
|
self.queue.write_buffer(
|
2024-01-04 17:18:31 -08:00
|
|
|
&self.global_uniform.data_buffer,
|
2023-12-23 23:24:04 -08:00
|
|
|
0,
|
2024-01-06 14:02:50 -08:00
|
|
|
bytemuck::cast_slice(&[GlobalDataContent {
|
2024-01-04 18:15:30 -08:00
|
|
|
camera_position: state.camera_pos.into(),
|
|
|
|
camera_zoom: [state.camera_zoom, 0.0],
|
2024-01-01 10:44:55 -08:00
|
|
|
camera_zoom_limits: [galactica_constants::ZOOM_MIN, galactica_constants::ZOOM_MAX],
|
2023-12-24 11:08:44 -08:00
|
|
|
window_size: [
|
|
|
|
self.window_size.width as f32,
|
|
|
|
self.window_size.height as f32,
|
|
|
|
],
|
2024-01-08 15:17:20 -08:00
|
|
|
window_scale: [self.window.scale_factor() as f32, 0.0],
|
2023-12-24 11:08:44 -08:00
|
|
|
window_aspect: [self.window_aspect, 0.0],
|
2024-01-05 12:09:59 -08:00
|
|
|
starfield_sprite: [s.get_index(), 0],
|
2024-01-01 10:44:55 -08:00
|
|
|
starfield_tile_size: [galactica_constants::STARFIELD_SIZE as f32, 0.0],
|
2023-12-31 18:39:37 -08:00
|
|
|
starfield_size_limits: [
|
2024-01-01 10:44:55 -08:00
|
|
|
galactica_constants::STARFIELD_SIZE_MIN,
|
|
|
|
galactica_constants::STARFIELD_SIZE_MAX,
|
2023-12-31 18:39:37 -08:00
|
|
|
],
|
2024-01-04 18:15:30 -08:00
|
|
|
current_time: [state.current_time, 0.0],
|
2023-12-23 23:24:04 -08:00
|
|
|
}]),
|
|
|
|
);
|
|
|
|
|
2024-01-03 06:37:02 -08:00
|
|
|
// Write all new particles to GPU buffer
|
2024-01-08 15:17:20 -08:00
|
|
|
state.particles.shuffle(&mut rand::thread_rng());
|
|
|
|
for i in state.particles.iter() {
|
2024-01-02 19:11:18 -08:00
|
|
|
self.queue.write_buffer(
|
|
|
|
&self.vertex_buffers.particle.instances,
|
2024-01-08 19:11:46 -08:00
|
|
|
ParticleInstance::SIZE * self.vertex_buffers.particle_counter,
|
2024-01-02 19:11:18 -08:00
|
|
|
bytemuck::cast_slice(&[ParticleInstance {
|
2024-01-03 06:37:02 -08:00
|
|
|
position: [i.pos.x, i.pos.y],
|
|
|
|
velocity: i.velocity.into(),
|
2024-01-06 14:02:50 -08:00
|
|
|
angle: i.angle.0,
|
|
|
|
angvel: i.angvel.0,
|
2024-01-02 19:11:18 -08:00
|
|
|
size: i.size,
|
2024-01-05 12:09:59 -08:00
|
|
|
sprite_index: i.sprite.get_index(),
|
2024-01-04 18:15:30 -08:00
|
|
|
created: state.current_time,
|
|
|
|
expires: state.current_time + i.lifetime,
|
2024-01-07 12:15:34 -08:00
|
|
|
fade: i.fade,
|
2024-01-02 19:11:18 -08:00
|
|
|
}]),
|
|
|
|
);
|
2024-01-08 19:11:46 -08:00
|
|
|
self.vertex_buffers.particle_counter += 1;
|
|
|
|
if self.vertex_buffers.particle_counter
|
2024-01-02 19:11:18 -08:00
|
|
|
== galactica_constants::PARTICLE_SPRITE_INSTANCE_LIMIT
|
|
|
|
{
|
2024-01-08 19:11:46 -08:00
|
|
|
self.vertex_buffers.particle_counter = 0;
|
2024-01-02 19:11:18 -08:00
|
|
|
}
|
|
|
|
}
|
2024-01-08 15:17:20 -08:00
|
|
|
state.particles.clear();
|
2024-01-02 19:11:18 -08:00
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
// Create sprite instances
|
2024-01-08 19:11:46 -08:00
|
|
|
self.update_all_buffers(&state);
|
2023-12-23 14:07:12 -08:00
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
// These should match the indices in each shader,
|
|
|
|
// and should each have a corresponding bind group layout.
|
2023-12-23 14:07:12 -08:00
|
|
|
render_pass.set_bind_group(0, &self.texture_array.bind_group, &[]);
|
2024-01-04 17:18:31 -08:00
|
|
|
render_pass.set_bind_group(1, &self.global_uniform.bind_group, &[]);
|
2023-12-23 14:07:12 -08:00
|
|
|
|
2023-12-23 11:03:06 -08:00
|
|
|
// Starfield pipeline
|
2023-12-23 12:52:36 -08:00
|
|
|
self.vertex_buffers.starfield.set_in_pass(&mut render_pass);
|
2023-12-23 11:03:06 -08:00
|
|
|
render_pass.set_pipeline(&self.starfield_pipeline);
|
2024-01-01 09:45:27 -08:00
|
|
|
render_pass.draw_indexed(
|
|
|
|
0..SPRITE_INDICES.len() as u32,
|
|
|
|
0,
|
|
|
|
0..self.starfield.instance_count,
|
|
|
|
);
|
2023-12-23 11:03:06 -08:00
|
|
|
|
|
|
|
// Sprite pipeline
|
2024-01-02 09:23:38 -08:00
|
|
|
self.vertex_buffers.object.set_in_pass(&mut render_pass);
|
|
|
|
render_pass.set_pipeline(&self.object_pipeline);
|
2024-01-08 19:11:46 -08:00
|
|
|
render_pass.draw_indexed(
|
|
|
|
0..SPRITE_INDICES.len() as u32,
|
|
|
|
0,
|
|
|
|
0..self.vertex_buffers.object_counter as _,
|
|
|
|
);
|
2024-01-02 09:23:38 -08:00
|
|
|
|
2024-01-02 19:11:18 -08:00
|
|
|
// Particle pipeline
|
|
|
|
self.vertex_buffers.particle.set_in_pass(&mut render_pass);
|
|
|
|
render_pass.set_pipeline(&self.particle_pipeline);
|
|
|
|
render_pass.draw_indexed(
|
|
|
|
0..SPRITE_INDICES.len() as u32,
|
|
|
|
0,
|
|
|
|
0..galactica_constants::PARTICLE_SPRITE_INSTANCE_LIMIT as _,
|
|
|
|
);
|
|
|
|
|
2024-01-02 09:23:38 -08:00
|
|
|
// Ui pipeline
|
|
|
|
self.vertex_buffers.ui.set_in_pass(&mut render_pass);
|
|
|
|
render_pass.set_pipeline(&self.ui_pipeline);
|
2024-01-08 19:11:46 -08:00
|
|
|
render_pass.draw_indexed(
|
|
|
|
0..SPRITE_INDICES.len() as u32,
|
|
|
|
0,
|
|
|
|
0..self.vertex_buffers.ui_counter as _,
|
|
|
|
);
|
2023-12-22 16:51:21 -08:00
|
|
|
|
2024-01-08 18:12:13 -08:00
|
|
|
// Radial progress bars
|
2024-01-08 15:17:20 -08:00
|
|
|
// TODO: do we need to do this every time?
|
|
|
|
self.vertex_buffers.radialbar.set_in_pass(&mut render_pass);
|
|
|
|
render_pass.set_pipeline(&self.radialbar_pipeline);
|
2024-01-08 19:11:46 -08:00
|
|
|
render_pass.draw_indexed(
|
|
|
|
0..SPRITE_INDICES.len() as u32,
|
|
|
|
0,
|
|
|
|
0..self.vertex_buffers.radialbar_counter as _,
|
|
|
|
);
|
2024-01-08 15:17:20 -08:00
|
|
|
|
2023-12-22 16:51:21 -08:00
|
|
|
// begin_render_pass borrows encoder mutably, so we can't call finish()
|
|
|
|
// without dropping this variable.
|
|
|
|
drop(render_pass);
|
|
|
|
|
|
|
|
self.queue.submit(iter::once(encoder.finish()));
|
|
|
|
output.present();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|