2023-12-22 16:51:21 -08:00
|
|
|
use anyhow::Result;
|
|
|
|
use bytemuck;
|
2023-12-23 23:24:04 -08:00
|
|
|
use cgmath::{EuclideanSpace, Matrix2, Point2};
|
2023-12-23 12:52:36 -08:00
|
|
|
use std::{iter, rc::Rc};
|
|
|
|
use wgpu;
|
2023-12-23 14:07:12 -08:00
|
|
|
use winit::{self, dpi::PhysicalSize, window::Window};
|
2023-12-22 16:51:21 -08:00
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
use crate::Game;
|
2023-12-22 21:39:47 -08:00
|
|
|
|
|
|
|
use super::{
|
2023-12-23 23:24:04 -08:00
|
|
|
globaldata::{GlobalData, GlobalDataContent},
|
2023-12-23 11:03:06 -08:00
|
|
|
pipeline::PipelineBuilder,
|
2023-12-22 21:39:47 -08:00
|
|
|
texturearray::TextureArray,
|
2023-12-23 12:52:36 -08:00
|
|
|
vertexbuffer::{
|
|
|
|
data::{SPRITE_INDICES, SPRITE_VERTICES},
|
2023-12-23 23:24:04 -08:00
|
|
|
types::{SpriteInstance, StarfieldInstance, TexturedVertex},
|
2023-12-23 12:52:36 -08:00
|
|
|
VertexBuffer,
|
|
|
|
},
|
2023-12-22 21:39:47 -08:00
|
|
|
};
|
2023-12-22 16:51:21 -08:00
|
|
|
|
|
|
|
pub struct GPUState {
|
|
|
|
device: wgpu::Device,
|
|
|
|
config: wgpu::SurfaceConfiguration,
|
|
|
|
surface: wgpu::Surface,
|
|
|
|
queue: wgpu::Queue,
|
|
|
|
|
|
|
|
pub window: Window,
|
2023-12-23 14:07:12 -08:00
|
|
|
pub window_size: winit::dpi::PhysicalSize<u32>,
|
|
|
|
window_aspect: f32,
|
2023-12-22 16:51:21 -08:00
|
|
|
|
2023-12-23 11:03:06 -08:00
|
|
|
sprite_pipeline: wgpu::RenderPipeline,
|
|
|
|
starfield_pipeline: wgpu::RenderPipeline,
|
2023-12-22 16:51:21 -08:00
|
|
|
|
|
|
|
texture_array: TextureArray,
|
2023-12-23 23:24:04 -08:00
|
|
|
global_data: GlobalData,
|
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 {
|
|
|
|
sprite: Rc<VertexBuffer>,
|
|
|
|
starfield: Rc<VertexBuffer>,
|
2023-12-22 16:51:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GPUState {
|
|
|
|
// We can draw at most this many sprites on the screen.
|
|
|
|
// TODO: compile-time option
|
2023-12-23 14:07:12 -08:00
|
|
|
pub const SPRITE_INSTANCE_LIMIT: u64 = 100;
|
2023-12-23 23:24:04 -08:00
|
|
|
pub const STARFIELD_INSTANCE_LIMIT: u64 = 501 * 9;
|
2023-12-22 16:51:21 -08:00
|
|
|
|
|
|
|
pub async fn new(window: Window) -> 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 {
|
|
|
|
sprite: Rc::new(VertexBuffer::new::<TexturedVertex, SpriteInstance>(
|
|
|
|
"sprite",
|
|
|
|
&device,
|
|
|
|
Some(SPRITE_VERTICES),
|
|
|
|
Some(SPRITE_INDICES),
|
2023-12-23 14:07:12 -08:00
|
|
|
Self::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),
|
2023-12-23 14:07:12 -08:00
|
|
|
Self::STARFIELD_INSTANCE_LIMIT,
|
2023-12-23 12:52:36 -08:00
|
|
|
)),
|
|
|
|
};
|
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
// Load uniforms
|
|
|
|
let global_data = GlobalData::new(&device);
|
2023-12-22 16:51:21 -08:00
|
|
|
let texture_array = TextureArray::new(&device, &queue)?;
|
|
|
|
|
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,
|
|
|
|
&global_data.bind_group_layout,
|
|
|
|
];
|
|
|
|
|
2023-12-23 12:52:36 -08:00
|
|
|
// Create render pipelines
|
2023-12-23 11:03:06 -08:00
|
|
|
let sprite_pipeline = PipelineBuilder::new("sprite", &device)
|
|
|
|
.set_shader(include_str!(concat!(
|
|
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
|
|
"/src/render/shaders/",
|
|
|
|
"sprite.wgsl"
|
|
|
|
)))
|
|
|
|
.set_format(config.format)
|
|
|
|
.set_triangle(true)
|
2023-12-23 12:52:36 -08:00
|
|
|
.set_vertex_buffer(&vertex_buffers.sprite)
|
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)
|
|
|
|
.set_shader(include_str!(concat!(
|
|
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
|
|
"/src/render/shaders/",
|
|
|
|
"starfield.wgsl"
|
|
|
|
)))
|
|
|
|
.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();
|
|
|
|
|
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
|
|
|
|
2023-12-23 11:03:06 -08:00
|
|
|
sprite_pipeline,
|
|
|
|
starfield_pipeline,
|
2023-12-23 12:52:36 -08:00
|
|
|
|
2023-12-22 16:51:21 -08:00
|
|
|
texture_array,
|
2023-12-23 23:24:04 -08:00
|
|
|
global_data,
|
2023-12-23 12:52:36 -08:00
|
|
|
vertex_buffers,
|
2023-12-22 16:51:21 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn window(&self) -> &Window {
|
|
|
|
&self.window
|
|
|
|
}
|
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
pub fn resize(&mut self, new_size: PhysicalSize<u32>) {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update(&mut self) {}
|
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
/// Make a SpriteInstance for each of the game's visible sprites.
|
|
|
|
/// Will panic if SPRITE_INSTANCE_LIMIT is exceeded.
|
|
|
|
///
|
|
|
|
/// This is only called inside self.render()
|
|
|
|
fn make_sprite_instances(&self, game: &Game) -> Vec<SpriteInstance> {
|
|
|
|
let mut instances: Vec<SpriteInstance> = Vec::new();
|
2023-12-22 16:51:21 -08:00
|
|
|
|
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.
|
2023-12-23 14:07:12 -08:00
|
|
|
let clip_ne = Point2::from((-1.0, 1.0)) * game.camera.zoom;
|
|
|
|
let clip_sw = Point2::from((1.0, -1.0)) * game.camera.zoom;
|
2023-12-23 11:03:06 -08:00
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
for s in game.sprites() {
|
2023-12-23 23:56:10 -08:00
|
|
|
// Parallax is computed here, so we can check if this sprite is visible.
|
|
|
|
let pos = (s.pos - game.camera.pos.to_vec()) / s.parallax;
|
2023-12-22 16:51:21 -08:00
|
|
|
let texture = self.texture_array.get_texture(&s.name[..]);
|
|
|
|
|
2023-12-22 22:10:38 -08:00
|
|
|
// Game dimensions of this sprite post-scale.
|
|
|
|
//
|
|
|
|
// We only need height / 2 to check if we're on the screen,
|
|
|
|
// but we omit the division.
|
|
|
|
// This gives us a small margin, and lets us re-use the value
|
|
|
|
// without an extra multiply.
|
2023-12-23 07:21:14 -08:00
|
|
|
let height = s.height * s.scale;
|
2023-12-22 21:39:47 -08:00
|
|
|
let width = height * texture.aspect;
|
|
|
|
|
2023-12-22 21:44:03 -08:00
|
|
|
// Don't draw (or compute matrices for)
|
|
|
|
// sprites that are off the screen
|
2023-12-22 21:39:47 -08:00
|
|
|
if pos.x < clip_ne.x - width
|
|
|
|
|| pos.y > clip_ne.y + height
|
|
|
|
|| pos.x > clip_sw.x + width
|
|
|
|
|| pos.y < clip_sw.y - height
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-12-22 21:44:03 -08:00
|
|
|
// Compute the values we need to draw
|
|
|
|
// scale: combines texture scale and zoom scale.
|
|
|
|
// screen_pos: position of this sprite in screen coordinates
|
|
|
|
//
|
|
|
|
// We can't use screen_pos to exclude off-screen sprites because
|
|
|
|
// it can't account for height and width.
|
2023-12-22 21:39:47 -08:00
|
|
|
|
|
|
|
instances.push(SpriteInstance {
|
2023-12-23 23:24:04 -08:00
|
|
|
position: pos.into(),
|
|
|
|
aspect: texture.aspect,
|
|
|
|
rotation: Matrix2::from_angle(s.angle).into(),
|
|
|
|
height,
|
2023-12-22 16:51:21 -08:00
|
|
|
texture_index: texture.index,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enforce sprite limit
|
2023-12-23 14:07:12 -08:00
|
|
|
if instances.len() as u64 > Self::SPRITE_INSTANCE_LIMIT {
|
2023-12-22 16:51:21 -08:00
|
|
|
// TODO: no panic, handle this better.
|
2023-12-23 14:07:12 -08:00
|
|
|
unreachable!("Sprite limit exceeded!")
|
2023-12-22 16:51:21 -08:00
|
|
|
}
|
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
return instances;
|
|
|
|
}
|
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.
|
|
|
|
///
|
|
|
|
/// This is only called inside self.render()
|
2023-12-23 23:24:04 -08:00
|
|
|
fn make_starfield_instances(&self, game: &Game) -> Vec<StarfieldInstance> {
|
2023-12-23 23:56:10 -08:00
|
|
|
let instances: Vec<StarfieldInstance> = game
|
|
|
|
.system
|
|
|
|
.starfield
|
|
|
|
.iter()
|
|
|
|
.map(|s| StarfieldInstance {
|
2023-12-23 23:24:04 -08:00
|
|
|
position: s.pos.into(),
|
|
|
|
parallax: s.parallax,
|
2023-12-23 23:56:10 -08:00
|
|
|
height: s.height,
|
|
|
|
})
|
|
|
|
.collect();
|
2023-12-23 11:03:06 -08:00
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
// Enforce starfield limit
|
|
|
|
if instances.len() as u64 > Self::STARFIELD_INSTANCE_LIMIT {
|
|
|
|
// TODO: no panic, handle this better.
|
|
|
|
unreachable!("Starfield limit exceeded!")
|
|
|
|
}
|
2023-12-23 23:24:04 -08:00
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
return instances;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn render(&mut self, game: &Game) -> Result<(), wgpu::SurfaceError> {
|
|
|
|
let output = self.surface.get_current_texture()?;
|
|
|
|
let view = output
|
|
|
|
.texture
|
|
|
|
.create_view(&wgpu::TextureViewDescriptor::default());
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
// Update global values
|
|
|
|
self.queue.write_buffer(
|
|
|
|
&self.global_data.buffer,
|
|
|
|
0,
|
|
|
|
bytemuck::cast_slice(&[GlobalDataContent {
|
|
|
|
camera_position: game.camera.pos.into(),
|
|
|
|
camera_zoom: game.camera.zoom,
|
|
|
|
window_aspect: self.window_aspect,
|
|
|
|
starfield_texture: 1,
|
2023-12-23 23:56:10 -08:00
|
|
|
starfield_tile_size: 80000.0,
|
2023-12-23 23:24:04 -08:00
|
|
|
padding: Default::default(),
|
|
|
|
}]),
|
|
|
|
);
|
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
// Create sprite instances
|
|
|
|
let sprite_instances = self.make_sprite_instances(game);
|
|
|
|
self.queue.write_buffer(
|
|
|
|
&self.vertex_buffers.sprite.instances,
|
|
|
|
0,
|
|
|
|
bytemuck::cast_slice(&sprite_instances),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create starfield instances
|
|
|
|
let starfield_instances = self.make_starfield_instances(game);
|
2023-12-23 11:03:06 -08:00
|
|
|
self.queue.write_buffer(
|
2023-12-23 12:52:36 -08:00
|
|
|
&self.vertex_buffers.starfield.instances,
|
2023-12-22 21:39:47 -08:00
|
|
|
0,
|
2023-12-23 14:07:12 -08:00
|
|
|
bytemuck::cast_slice(&starfield_instances),
|
2023-12-23 11:03:06 -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, &[]);
|
2023-12-23 23:24:04 -08:00
|
|
|
render_pass.set_bind_group(1, &self.global_data.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);
|
2023-12-23 23:24:04 -08:00
|
|
|
|
2023-12-23 14:07:12 -08:00
|
|
|
render_pass.draw(0..1, 0..starfield_instances.len() as _);
|
2023-12-23 23:24:04 -08:00
|
|
|
render_pass.draw_indexed(
|
|
|
|
0..SPRITE_INDICES.len() as u32,
|
|
|
|
0,
|
|
|
|
0..starfield_instances.len() as _,
|
|
|
|
);
|
2023-12-23 11:03:06 -08:00
|
|
|
|
|
|
|
// Sprite pipeline
|
2023-12-23 12:52:36 -08:00
|
|
|
self.vertex_buffers.sprite.set_in_pass(&mut render_pass);
|
2023-12-23 11:03:06 -08:00
|
|
|
render_pass.set_pipeline(&self.sprite_pipeline);
|
2023-12-23 14:07:12 -08:00
|
|
|
render_pass.draw_indexed(
|
|
|
|
0..SPRITE_INDICES.len() as u32,
|
|
|
|
0,
|
|
|
|
0..sprite_instances.len() as _,
|
|
|
|
);
|
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(())
|
|
|
|
}
|
|
|
|
}
|