-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b709b1
commit c57ec6a
Showing
8 changed files
with
250 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use wgpu::vertex_attr_array; | ||
use wgpu_jumpstart::wgpu; | ||
|
||
use bytemuck::{Pod, Zeroable}; | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone, Pod, Zeroable, PartialEq)] | ||
pub struct GlowInstance { | ||
pub position: [f32; 2], | ||
pub size: [f32; 2], | ||
pub color: [f32; 4], | ||
} | ||
|
||
impl Default for GlowInstance { | ||
fn default() -> Self { | ||
Self { | ||
position: [0.0, 0.0], | ||
size: [0.0, 0.0], | ||
color: [0.0, 0.0, 0.0, 1.0], | ||
} | ||
} | ||
} | ||
|
||
impl GlowInstance { | ||
pub fn attributes() -> [wgpu::VertexAttribute; 4] { | ||
vertex_attr_array!(1 => Float32x2, 2 => Float32x2, 3 => Float32x4, 4 => Float32x4) | ||
} | ||
|
||
pub fn layout(attributes: &[wgpu::VertexAttribute]) -> wgpu::VertexBufferLayout { | ||
wgpu::VertexBufferLayout { | ||
array_stride: std::mem::size_of::<GlowInstance>() as wgpu::BufferAddress, | ||
step_mode: wgpu::VertexStepMode::Instance, | ||
attributes, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
mod instance_data; | ||
pub use instance_data::GlowInstance; | ||
|
||
use wgpu_jumpstart::{wgpu, Gpu, Instances, Shape, TransformUniform, Uniform}; | ||
|
||
pub struct GlowPipeline { | ||
render_pipeline: wgpu::RenderPipeline, | ||
quad: Shape, | ||
instances: Instances<GlowInstance>, | ||
} | ||
|
||
impl<'a> GlowPipeline { | ||
pub fn new(gpu: &Gpu, transform_uniform: &Uniform<TransformUniform>) -> Self { | ||
let shader = gpu | ||
.device | ||
.create_shader_module(wgpu::ShaderModuleDescriptor { | ||
label: Some("RectanglePipeline::shader"), | ||
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(include_str!( | ||
"./shader.wgsl" | ||
))), | ||
}); | ||
|
||
let render_pipeline_layout = | ||
gpu.device | ||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { | ||
label: None, | ||
bind_group_layouts: &[&transform_uniform.bind_group_layout], | ||
push_constant_ranges: &[], | ||
}); | ||
|
||
let ri_attrs = GlowInstance::attributes(); | ||
|
||
let target = wgpu_jumpstart::default_color_target_state(gpu.texture_format); | ||
|
||
let render_pipeline = gpu | ||
.device | ||
.create_render_pipeline(&wgpu::RenderPipelineDescriptor { | ||
layout: Some(&render_pipeline_layout), | ||
fragment: Some(wgpu_jumpstart::default_fragment(&shader, &[Some(target)])), | ||
..wgpu_jumpstart::default_render_pipeline(wgpu_jumpstart::default_vertex( | ||
&shader, | ||
&[Shape::layout(), GlowInstance::layout(&ri_attrs)], | ||
)) | ||
}); | ||
|
||
let quad = Shape::new_quad(&gpu.device); | ||
let instances = Instances::new(&gpu.device, 100_000); | ||
|
||
Self { | ||
render_pipeline, | ||
|
||
quad, | ||
|
||
instances, | ||
} | ||
} | ||
|
||
pub fn render( | ||
&'a self, | ||
transform_uniform: &'a Uniform<TransformUniform>, | ||
render_pass: &mut wgpu::RenderPass<'a>, | ||
) { | ||
render_pass.set_pipeline(&self.render_pipeline); | ||
render_pass.set_bind_group(0, &transform_uniform.bind_group, &[]); | ||
|
||
render_pass.set_vertex_buffer(0, self.quad.vertex_buffer.slice(..)); | ||
render_pass.set_vertex_buffer(1, self.instances.buffer.slice(..)); | ||
|
||
render_pass.set_index_buffer(self.quad.index_buffer.slice(..), wgpu::IndexFormat::Uint16); | ||
|
||
render_pass.draw_indexed(0..self.quad.indices_len, 0, 0..self.instances.len()); | ||
} | ||
|
||
pub fn clear(&mut self) { | ||
self.instances.data.clear(); | ||
} | ||
|
||
pub fn instances(&mut self) -> &mut Vec<GlowInstance> { | ||
&mut self.instances.data | ||
} | ||
|
||
pub fn prepare(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) { | ||
self.instances.update(device, queue); | ||
} | ||
|
||
pub fn update_instance_buffer( | ||
&mut self, | ||
device: &wgpu::Device, | ||
queue: &wgpu::Queue, | ||
instances: Vec<GlowInstance>, | ||
) { | ||
self.instances.data = instances; | ||
self.instances.update(device, queue); | ||
} | ||
|
||
pub fn with_instances_mut<F: FnOnce(&mut Vec<GlowInstance>)>( | ||
&mut self, | ||
device: &wgpu::Device, | ||
queue: &wgpu::Queue, | ||
cb: F, | ||
) { | ||
cb(&mut self.instances.data); | ||
self.instances.update(device, queue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
struct ViewUniform { | ||
transform: mat4x4<f32>, | ||
size: vec2<f32>, | ||
} | ||
|
||
@group(0) @binding(0) | ||
var<uniform> view_uniform: ViewUniform; | ||
|
||
struct Vertex { | ||
@location(0) position: vec2<f32>, | ||
} | ||
|
||
struct QuadInstance { | ||
@location(1) q_position: vec2<f32>, | ||
@location(2) size: vec2<f32>, | ||
@location(3) color: vec4<f32>, | ||
} | ||
|
||
struct VertexOutput { | ||
@builtin(position) position: vec4<f32>, | ||
@location(0) uv: vec2<f32>, | ||
@location(1) quad_color: vec4<f32>, | ||
} | ||
|
||
@vertex | ||
fn vs_main(vertex: Vertex, quad: QuadInstance) -> VertexOutput { | ||
var i_transform: mat4x4<f32> = mat4x4<f32>( | ||
vec4<f32>(quad.size.x, 0.0, 0.0, 0.0), | ||
vec4<f32>(0.0, quad.size.y, 0.0, 0.0), | ||
vec4<f32>(0.0, 0.0, 1.0, 0.0), | ||
vec4<f32>(quad.q_position, 0.0, 1.0) | ||
); | ||
|
||
var out: VertexOutput; | ||
out.position = view_uniform.transform * i_transform * vec4<f32>(vertex.position, 0.0, 1.0); | ||
out.uv = vertex.position; | ||
out.quad_color = quad.color; | ||
return out; | ||
} | ||
|
||
@fragment | ||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> { | ||
let m = distance(in.uv, vec2(0.5, 0.5)) * 2.0; | ||
return mix(in.quad_color, vec4<f32>(0.0), m); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters