Skip to content

Commit 9add330

Browse files
committed
buffer: adjust wgpu to use buffers
1 parent 715e9de commit 9add330

File tree

6 files changed

+231
-36
lines changed

6 files changed

+231
-36
lines changed

generated/graphics/wgpu/cargo-gpu/mygraphics/src/wgpu_renderer/render_pipeline.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::wgpu_renderer::renderer::{GlobalBindGroup, GlobalBindGroupLayout};
12
use mygraphics_shaders::ShaderConstants;
23
use wgpu::{
34
ColorTargetState, ColorWrites, Device, FragmentState, FrontFace, MultisampleState,
@@ -6,17 +7,22 @@ use wgpu::{
67
include_spirv,
78
};
89

10+
#[derive(Debug, Clone)]
911
pub struct MyRenderPipeline {
1012
pipeline: RenderPipeline,
1113
}
1214

1315
impl MyRenderPipeline {
14-
pub fn new(device: &Device, out_format: TextureFormat) -> anyhow::Result<Self> {
16+
pub fn new(
17+
device: &Device,
18+
global_bind_group_layout: &GlobalBindGroupLayout,
19+
out_format: TextureFormat,
20+
) -> anyhow::Result<Self> {
1521
let module = device.create_shader_module(include_spirv!(env!("SHADER_SPV_PATH")));
1622

1723
let layout = device.create_pipeline_layout(&PipelineLayoutDescriptor {
1824
label: Some("MyRenderPipeline layout"),
19-
bind_group_layouts: &[],
25+
bind_group_layouts: &[&global_bind_group_layout.0],
2026
push_constant_ranges: &[PushConstantRange {
2127
stages: ShaderStages::VERTEX_FRAGMENT,
2228
range: 0..size_of::<ShaderConstants>() as u32,
@@ -60,13 +66,9 @@ impl MyRenderPipeline {
6066
})
6167
}
6268

63-
pub fn draw(&self, rpass: &mut RenderPass<'_>, shader_constants: &ShaderConstants) {
69+
pub fn draw(&self, rpass: &mut RenderPass<'_>, global_bind_group: &GlobalBindGroup) {
6470
rpass.set_pipeline(&self.pipeline);
65-
rpass.set_push_constants(
66-
ShaderStages::VERTEX_FRAGMENT,
67-
0,
68-
bytemuck::bytes_of(shader_constants),
69-
);
71+
rpass.set_bind_group(0, &global_bind_group.0, &[]);
7072
rpass.draw(0..3, 0..1);
7173
}
7274
}

generated/graphics/wgpu/cargo-gpu/mygraphics/src/wgpu_renderer/renderer.rs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
use crate::wgpu_renderer::render_pipeline::MyRenderPipeline;
22
use mygraphics_shaders::ShaderConstants;
3+
use wgpu::util::{BufferInitDescriptor, DeviceExt};
34
use wgpu::wgt::CommandEncoderDescriptor;
45
use wgpu::{
5-
Color, Device, LoadOp, Operations, Queue, RenderPassColorAttachment, RenderPassDescriptor,
6-
StoreOp, TextureFormat, TextureView,
6+
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutDescriptor,
7+
BindGroupLayoutEntry, BindingResource, BindingType, Buffer, BufferBinding, BufferBindingType,
8+
BufferUsages, Color, Device, LoadOp, Operations, Queue, RenderPassColorAttachment,
9+
RenderPassDescriptor, ShaderStages, StoreOp, TextureFormat, TextureView,
710
};
811

912
pub struct MyRenderer {
1013
pub device: Device,
1114
pub queue: Queue,
15+
global_bind_group_layout: GlobalBindGroupLayout,
1216
pipeline: MyRenderPipeline,
1317
}
1418

1519
impl MyRenderer {
1620
pub fn new(device: Device, queue: Queue, out_format: TextureFormat) -> anyhow::Result<Self> {
21+
let global_bind_group_layout = GlobalBindGroupLayout::new(&device);
22+
let pipeline = MyRenderPipeline::new(&device, &global_bind_group_layout, out_format)?;
1723
Ok(Self {
18-
pipeline: MyRenderPipeline::new(&device, out_format)?,
24+
global_bind_group_layout,
25+
pipeline,
1926
device,
2027
queue,
2128
})
2229
}
2330

2431
pub fn render(&self, shader_constants: &ShaderConstants, output: TextureView) {
32+
let global_bind_group = self
33+
.global_bind_group_layout
34+
.create(&self.device, shader_constants);
35+
2536
let mut cmd = self
2637
.device
2738
.create_command_encoder(&CommandEncoderDescriptor {
@@ -43,9 +54,61 @@ impl MyRenderer {
4354
timestamp_writes: None,
4455
occlusion_query_set: None,
4556
});
46-
self.pipeline.draw(&mut rpass, shader_constants);
57+
self.pipeline.draw(&mut rpass, &global_bind_group);
4758
drop(rpass);
4859

4960
self.queue.submit(std::iter::once(cmd.finish()));
5061
}
5162
}
63+
64+
#[derive(Debug, Clone)]
65+
pub struct GlobalBindGroupLayout(pub BindGroupLayout);
66+
67+
impl GlobalBindGroupLayout {
68+
pub fn new(device: &Device) -> Self {
69+
Self(device.create_bind_group_layout(&BindGroupLayoutDescriptor {
70+
label: Some("GlobalBindGroupLayout"),
71+
entries: &[BindGroupLayoutEntry {
72+
binding: 0,
73+
visibility: ShaderStages::VERTEX_FRAGMENT,
74+
ty: BindingType::Buffer {
75+
ty: BufferBindingType::Storage { read_only: true },
76+
has_dynamic_offset: false,
77+
min_binding_size: None,
78+
},
79+
count: None,
80+
}],
81+
}))
82+
}
83+
84+
pub fn create(&self, device: &Device, shader_constants: &ShaderConstants) -> GlobalBindGroup {
85+
let shader_constants = device.create_buffer_init(&BufferInitDescriptor {
86+
label: Some("ShaderConstants"),
87+
contents: bytemuck::bytes_of(shader_constants),
88+
usage: BufferUsages::STORAGE,
89+
});
90+
self.create_from_buffer(device, &shader_constants)
91+
}
92+
93+
pub fn create_from_buffer(
94+
&self,
95+
device: &Device,
96+
shader_constants: &Buffer,
97+
) -> GlobalBindGroup {
98+
GlobalBindGroup(device.create_bind_group(&BindGroupDescriptor {
99+
label: Some("GlobalBindGroup"),
100+
layout: &self.0,
101+
entries: &[BindGroupEntry {
102+
binding: 0,
103+
resource: BindingResource::Buffer(BufferBinding {
104+
buffer: shader_constants,
105+
offset: 0,
106+
size: None,
107+
}),
108+
}],
109+
}))
110+
}
111+
}
112+
113+
#[derive(Debug, Clone)]
114+
pub struct GlobalBindGroup(pub BindGroup);

generated/graphics/wgpu/spirv-builder/mygraphics/src/wgpu_renderer/render_pipeline.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::wgpu_renderer::renderer::{GlobalBindGroup, GlobalBindGroupLayout};
12
use mygraphics_shaders::ShaderConstants;
23
use wgpu::{
34
ColorTargetState, ColorWrites, Device, FragmentState, FrontFace, MultisampleState,
@@ -6,17 +7,22 @@ use wgpu::{
67
include_spirv,
78
};
89

10+
#[derive(Debug, Clone)]
911
pub struct MyRenderPipeline {
1012
pipeline: RenderPipeline,
1113
}
1214

1315
impl MyRenderPipeline {
14-
pub fn new(device: &Device, out_format: TextureFormat) -> anyhow::Result<Self> {
16+
pub fn new(
17+
device: &Device,
18+
global_bind_group_layout: &GlobalBindGroupLayout,
19+
out_format: TextureFormat,
20+
) -> anyhow::Result<Self> {
1521
let module = device.create_shader_module(include_spirv!(env!("SHADER_SPV_PATH")));
1622

1723
let layout = device.create_pipeline_layout(&PipelineLayoutDescriptor {
1824
label: Some("MyRenderPipeline layout"),
19-
bind_group_layouts: &[],
25+
bind_group_layouts: &[&global_bind_group_layout.0],
2026
push_constant_ranges: &[PushConstantRange {
2127
stages: ShaderStages::VERTEX_FRAGMENT,
2228
range: 0..size_of::<ShaderConstants>() as u32,
@@ -60,13 +66,9 @@ impl MyRenderPipeline {
6066
})
6167
}
6268

63-
pub fn draw(&self, rpass: &mut RenderPass<'_>, shader_constants: &ShaderConstants) {
69+
pub fn draw(&self, rpass: &mut RenderPass<'_>, global_bind_group: &GlobalBindGroup) {
6470
rpass.set_pipeline(&self.pipeline);
65-
rpass.set_push_constants(
66-
ShaderStages::VERTEX_FRAGMENT,
67-
0,
68-
bytemuck::bytes_of(shader_constants),
69-
);
71+
rpass.set_bind_group(0, &global_bind_group.0, &[]);
7072
rpass.draw(0..3, 0..1);
7173
}
7274
}

generated/graphics/wgpu/spirv-builder/mygraphics/src/wgpu_renderer/renderer.rs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
use crate::wgpu_renderer::render_pipeline::MyRenderPipeline;
22
use mygraphics_shaders::ShaderConstants;
3+
use wgpu::util::{BufferInitDescriptor, DeviceExt};
34
use wgpu::wgt::CommandEncoderDescriptor;
45
use wgpu::{
5-
Color, Device, LoadOp, Operations, Queue, RenderPassColorAttachment, RenderPassDescriptor,
6-
StoreOp, TextureFormat, TextureView,
6+
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutDescriptor,
7+
BindGroupLayoutEntry, BindingResource, BindingType, Buffer, BufferBinding, BufferBindingType,
8+
BufferUsages, Color, Device, LoadOp, Operations, Queue, RenderPassColorAttachment,
9+
RenderPassDescriptor, ShaderStages, StoreOp, TextureFormat, TextureView,
710
};
811

912
pub struct MyRenderer {
1013
pub device: Device,
1114
pub queue: Queue,
15+
global_bind_group_layout: GlobalBindGroupLayout,
1216
pipeline: MyRenderPipeline,
1317
}
1418

1519
impl MyRenderer {
1620
pub fn new(device: Device, queue: Queue, out_format: TextureFormat) -> anyhow::Result<Self> {
21+
let global_bind_group_layout = GlobalBindGroupLayout::new(&device);
22+
let pipeline = MyRenderPipeline::new(&device, &global_bind_group_layout, out_format)?;
1723
Ok(Self {
18-
pipeline: MyRenderPipeline::new(&device, out_format)?,
24+
global_bind_group_layout,
25+
pipeline,
1926
device,
2027
queue,
2128
})
2229
}
2330

2431
pub fn render(&self, shader_constants: &ShaderConstants, output: TextureView) {
32+
let global_bind_group = self
33+
.global_bind_group_layout
34+
.create(&self.device, shader_constants);
35+
2536
let mut cmd = self
2637
.device
2738
.create_command_encoder(&CommandEncoderDescriptor {
@@ -43,9 +54,61 @@ impl MyRenderer {
4354
timestamp_writes: None,
4455
occlusion_query_set: None,
4556
});
46-
self.pipeline.draw(&mut rpass, shader_constants);
57+
self.pipeline.draw(&mut rpass, &global_bind_group);
4758
drop(rpass);
4859

4960
self.queue.submit(std::iter::once(cmd.finish()));
5061
}
5162
}
63+
64+
#[derive(Debug, Clone)]
65+
pub struct GlobalBindGroupLayout(pub BindGroupLayout);
66+
67+
impl GlobalBindGroupLayout {
68+
pub fn new(device: &Device) -> Self {
69+
Self(device.create_bind_group_layout(&BindGroupLayoutDescriptor {
70+
label: Some("GlobalBindGroupLayout"),
71+
entries: &[BindGroupLayoutEntry {
72+
binding: 0,
73+
visibility: ShaderStages::VERTEX_FRAGMENT,
74+
ty: BindingType::Buffer {
75+
ty: BufferBindingType::Storage { read_only: true },
76+
has_dynamic_offset: false,
77+
min_binding_size: None,
78+
},
79+
count: None,
80+
}],
81+
}))
82+
}
83+
84+
pub fn create(&self, device: &Device, shader_constants: &ShaderConstants) -> GlobalBindGroup {
85+
let shader_constants = device.create_buffer_init(&BufferInitDescriptor {
86+
label: Some("ShaderConstants"),
87+
contents: bytemuck::bytes_of(shader_constants),
88+
usage: BufferUsages::STORAGE,
89+
});
90+
self.create_from_buffer(device, &shader_constants)
91+
}
92+
93+
pub fn create_from_buffer(
94+
&self,
95+
device: &Device,
96+
shader_constants: &Buffer,
97+
) -> GlobalBindGroup {
98+
GlobalBindGroup(device.create_bind_group(&BindGroupDescriptor {
99+
label: Some("GlobalBindGroup"),
100+
layout: &self.0,
101+
entries: &[BindGroupEntry {
102+
binding: 0,
103+
resource: BindingResource::Buffer(BufferBinding {
104+
buffer: shader_constants,
105+
offset: 0,
106+
size: None,
107+
}),
108+
}],
109+
}))
110+
}
111+
}
112+
113+
#[derive(Debug, Clone)]
114+
pub struct GlobalBindGroup(pub BindGroup);

graphics/mygraphics/src/wgpu_renderer/render_pipeline.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::wgpu_renderer::renderer::{GlobalBindGroup, GlobalBindGroupLayout};
12
use mygraphics_shaders::ShaderConstants;
23
use wgpu::{
34
ColorTargetState, ColorWrites, Device, FragmentState, FrontFace, MultisampleState,
@@ -6,17 +7,22 @@ use wgpu::{
67
include_spirv,
78
};
89

10+
#[derive(Debug, Clone)]
911
pub struct MyRenderPipeline {
1012
pipeline: RenderPipeline,
1113
}
1214

1315
impl MyRenderPipeline {
14-
pub fn new(device: &Device, out_format: TextureFormat) -> anyhow::Result<Self> {
16+
pub fn new(
17+
device: &Device,
18+
global_bind_group_layout: &GlobalBindGroupLayout,
19+
out_format: TextureFormat,
20+
) -> anyhow::Result<Self> {
1521
let module = device.create_shader_module(include_spirv!(env!("SHADER_SPV_PATH")));
1622

1723
let layout = device.create_pipeline_layout(&PipelineLayoutDescriptor {
1824
label: Some("MyRenderPipeline layout"),
19-
bind_group_layouts: &[],
25+
bind_group_layouts: &[&global_bind_group_layout.0],
2026
push_constant_ranges: &[PushConstantRange {
2127
stages: ShaderStages::VERTEX_FRAGMENT,
2228
range: 0..size_of::<ShaderConstants>() as u32,
@@ -60,13 +66,9 @@ impl MyRenderPipeline {
6066
})
6167
}
6268

63-
pub fn draw(&self, rpass: &mut RenderPass<'_>, shader_constants: &ShaderConstants) {
69+
pub fn draw(&self, rpass: &mut RenderPass<'_>, global_bind_group: &GlobalBindGroup) {
6470
rpass.set_pipeline(&self.pipeline);
65-
rpass.set_push_constants(
66-
ShaderStages::VERTEX_FRAGMENT,
67-
0,
68-
bytemuck::bytes_of(shader_constants),
69-
);
71+
rpass.set_bind_group(0, &global_bind_group.0, &[]);
7072
rpass.draw(0..3, 0..1);
7173
}
7274
}

0 commit comments

Comments
 (0)