Skip to content

Commit

Permalink
Sampler remove from image (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hakolao authored Oct 1, 2024
1 parent 263421d commit 573083a
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 126 deletions.
33 changes: 9 additions & 24 deletions examples/game_of_life/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use glass::{
Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
};
use wgpu::{
AddressMode, Backends, BindGroup, BindGroupDescriptor, CommandBuffer, CommandEncoder,
ComputePassDescriptor, ComputePipeline, ComputePipelineDescriptor, Extent3d, FilterMode,
InstanceFlags, Limits, MemoryHints, PowerPreference, PresentMode, PushConstantRange,
SamplerDescriptor, ShaderStages, StorageTextureAccess, StoreOp, TextureFormat, TextureUsages,
Backends, BindGroup, BindGroupDescriptor, CommandBuffer, CommandEncoder, ComputePassDescriptor,
ComputePipeline, ComputePipelineDescriptor, Extent3d, InstanceFlags, Limits, MemoryHints,
PowerPreference, PresentMode, PushConstantRange, ShaderStages, StorageTextureAccess, StoreOp,
TextureFormat, TextureUsages,
};
use winit::{
event::{ElementState, MouseButton, WindowEvent},
Expand Down Expand Up @@ -415,15 +415,6 @@ fn create_canvas_data(
},
1,
TextureFormat::Rgba16Float,
&SamplerDescriptor {
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
address_mode_w: AddressMode::ClampToEdge,
mag_filter: FilterMode::Linear,
min_filter: FilterMode::Linear,
mipmap_filter: FilterMode::Linear,
..Default::default()
},
TextureUsages::TEXTURE_BINDING | TextureUsages::STORAGE_BINDING,
);
let data_in = Texture::empty(
Expand All @@ -436,20 +427,14 @@ fn create_canvas_data(
},
1,
TextureFormat::Rgba16Float,
&SamplerDescriptor {
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
address_mode_w: AddressMode::ClampToEdge,
mag_filter: FilterMode::Nearest,
min_filter: FilterMode::Nearest,
mipmap_filter: FilterMode::Nearest,
..Default::default()
},
TextureUsages::TEXTURE_BINDING | TextureUsages::STORAGE_BINDING,
);
// Create bind groups to match pipeline layouts (except update, create that dynamically each frame)
let canvas_bind_group =
quad_pipeline.create_bind_group(context.device(), &canvas.views[0], &canvas.sampler);
let canvas_bind_group = quad_pipeline.create_bind_group(
context.device(),
&canvas.views[0],
context.sampler_linear_clamp_to_edge(),
);
// These must match the bind group layout of our pipeline
let init_bind_group_layout = init_pipeline.get_bind_group_layout(0);
let init_bind_group = context.device().create_bind_group(&BindGroupDescriptor {
Expand Down
21 changes: 6 additions & 15 deletions examples/quad/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use glass::{
window::{GlassWindow, WindowConfig},
Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
};
use wgpu::{
AddressMode, BindGroup, CommandBuffer, FilterMode, Limits, SamplerDescriptor, StoreOp,
TextureFormat, TextureUsages,
};
use wgpu::{BindGroup, CommandBuffer, Limits, StoreOp, TextureFormat, TextureUsages};
use winit::event_loop::ActiveEventLoop;

const WIDTH: u32 = 1920;
Expand Down Expand Up @@ -131,8 +128,11 @@ struct ExampleData {
fn create_example_data(context: &GlassContext, quad_pipeline: &QuadPipeline) -> ExampleData {
let tree = create_tree_texture(context);
// Create bind group
let tree_bind_group =
quad_pipeline.create_bind_group(context.device(), &tree.views[0], &tree.sampler);
let tree_bind_group = quad_pipeline.create_bind_group(
context.device(),
&tree.views[0],
context.sampler_linear_clamp_to_edge(),
);
ExampleData {
tree,
tree_bind_group,
Expand All @@ -147,15 +147,6 @@ fn create_tree_texture(app: &GlassContext) -> Texture {
diffuse_bytes,
"tree.png",
TextureFormat::Rgba8UnormSrgb,
&SamplerDescriptor {
address_mode_u: AddressMode::Repeat,
address_mode_v: AddressMode::Repeat,
address_mode_w: AddressMode::Repeat,
mag_filter: FilterMode::Linear,
min_filter: FilterMode::Linear,
mipmap_filter: FilterMode::Linear,
..Default::default()
},
TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
)
.unwrap()
Expand Down
19 changes: 10 additions & 9 deletions examples/sand/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use glam::{IVec2, Vec2};
use glass::{pipelines::QuadPipeline, texture::Texture};
use image::RgbaImage;
use wgpu::{
BindGroup, Device, Extent3d, FilterMode, ImageCopyTexture, ImageDataLayout, Origin3d, Queue,
SamplerDescriptor, TextureAspect, TextureFormat, TextureUsages,
BindGroup, Device, Extent3d, ImageCopyTexture, ImageDataLayout, Origin3d, Queue, Sampler,
TextureAspect, TextureFormat, TextureUsages,
};

use crate::sand::{Sand, SandType};
Expand All @@ -19,7 +19,13 @@ pub struct Grid {
}

impl Grid {
pub fn new(device: &Device, quad: &QuadPipeline, width: u32, height: u32) -> Grid {
pub fn new(
device: &Device,
quad: &QuadPipeline,
sampler: &Sampler,
width: u32,
height: u32,
) -> Grid {
let data = vec![Sand::empty(); (width * height) as usize];
let rgba = RgbaImage::new(width, height);
let texture = Texture::empty(
Expand All @@ -32,14 +38,9 @@ impl Grid {
},
1,
TextureFormat::Rgba8UnormSrgb,
&SamplerDescriptor {
mag_filter: FilterMode::Nearest,
min_filter: FilterMode::Nearest,
..Default::default()
},
TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
);
let grid_bind_group = quad.create_bind_group(device, &texture.views[0], &texture.sampler);
let grid_bind_group = quad.create_bind_group(device, &texture.views[0], sampler);
Grid {
data,
rgba,
Expand Down
8 changes: 7 additions & 1 deletion examples/sand/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ impl SandSim {
}),
write_mask: wgpu::ColorWrites::ALL,
});
let grid = Grid::new(context.device(), &quad_pipeline, CANVAS_SIZE, CANVAS_SIZE);
let grid = Grid::new(
context.device(),
&quad_pipeline,
context.sampler_nearest_clamp_to_edge(),
CANVAS_SIZE,
CANVAS_SIZE,
);
SandSim {
grid,
quad_pipeline,
Expand Down
66 changes: 64 additions & 2 deletions src/device_context.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{path::PathBuf, sync::Arc};

use wgpu::{
Adapter, Backends, Device, DeviceDescriptor, Instance, InstanceDescriptor, InstanceFlags,
Limits, MemoryHints, PowerPreference, Queue, RequestAdapterOptions, Surface,
Adapter, AddressMode, Backends, Device, DeviceDescriptor, FilterMode, Instance,
InstanceDescriptor, InstanceFlags, Limits, MemoryHints, PowerPreference, Queue,
RequestAdapterOptions, Sampler, SamplerDescriptor, Surface,
};

use crate::{utils::wait_async, GlassError};
Expand Down Expand Up @@ -53,6 +54,10 @@ pub struct DeviceContext {
adapter: Adapter,
device: Arc<Device>,
queue: Arc<Queue>,
sampler_nearest_repeat: Arc<Sampler>,
sampler_linear_repeat: Arc<Sampler>,
sampler_nearest_clamp_to_edge: Arc<Sampler>,
sampler_linear_clamp_to_edge: Arc<Sampler>,
}

unsafe impl Send for DeviceContext {}
Expand All @@ -71,12 +76,53 @@ impl DeviceContext {
Ok(adq) => adq,
Err(e) => return Err(e),
};
let sampler_nearest_repeat = Arc::new(device.create_sampler(&SamplerDescriptor {
label: None,
address_mode_u: AddressMode::Repeat,
address_mode_v: AddressMode::Repeat,
mag_filter: FilterMode::Nearest,
min_filter: FilterMode::Nearest,
mipmap_filter: FilterMode::Nearest,
..Default::default()
}));
let sampler_linear_repeat = Arc::new(device.create_sampler(&SamplerDescriptor {
label: None,
address_mode_u: AddressMode::Repeat,
address_mode_v: AddressMode::Repeat,
mag_filter: FilterMode::Linear,
min_filter: FilterMode::Linear,
mipmap_filter: FilterMode::Linear,
..Default::default()
}));
let sampler_nearest_clamp_to_edge = Arc::new(device.create_sampler(&SamplerDescriptor {
label: None,
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
mag_filter: FilterMode::Nearest,
min_filter: FilterMode::Nearest,
mipmap_filter: FilterMode::Nearest,
..Default::default()
}));
let sampler_linear_clamp_to_edge = Arc::new(device.create_sampler(&SamplerDescriptor {
label: None,
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
mag_filter: FilterMode::Linear,
min_filter: FilterMode::Linear,
mipmap_filter: FilterMode::Linear,
..Default::default()
}));

Ok(Self {
config: config.clone(),
instance,
adapter,
device: Arc::new(device),
queue: Arc::new(queue),
sampler_nearest_repeat,
sampler_linear_repeat,
sampler_nearest_clamp_to_edge,
sampler_linear_clamp_to_edge,
})
}

Expand Down Expand Up @@ -129,6 +175,22 @@ impl DeviceContext {
Ok((adapter, device, queue))
}

pub fn sampler_nearest_repeat(&self) -> &Arc<Sampler> {
&self.sampler_nearest_repeat
}

pub fn sampler_linear_repeat(&self) -> &Arc<Sampler> {
&self.sampler_linear_repeat
}

pub fn sampler_nearest_clamp_to_edge(&self) -> &Arc<Sampler> {
&self.sampler_nearest_clamp_to_edge
}

pub fn sampler_linear_clamp_to_edge(&self) -> &Arc<Sampler> {
&self.sampler_linear_clamp_to_edge
}

pub fn instance(&self) -> &Instance {
&self.instance
}
Expand Down
18 changes: 17 additions & 1 deletion src/glass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use image::ImageError;
use indexmap::IndexMap;
use wgpu::{
Adapter, CreateSurfaceError, Device, Instance, PowerPreference, Queue, RequestDeviceError,
SurfaceConfiguration,
Sampler, SurfaceConfiguration,
};
use winit::{
application::ApplicationHandler,
Expand Down Expand Up @@ -374,6 +374,22 @@ impl GlassContext {
})
}

pub fn sampler_nearest_repeat(&self) -> &Arc<Sampler> {
self.device_context.sampler_nearest_repeat()
}

pub fn sampler_linear_repeat(&self) -> &Arc<Sampler> {
self.device_context.sampler_linear_repeat()
}

pub fn sampler_nearest_clamp_to_edge(&self) -> &Arc<Sampler> {
self.device_context.sampler_nearest_clamp_to_edge()
}

pub fn sampler_linear_clamp_to_edge(&self) -> &Arc<Sampler> {
self.device_context.sampler_linear_clamp_to_edge()
}

#[allow(unused)]
pub fn instance(&self) -> &Instance {
self.device_context.instance()
Expand Down
26 changes: 15 additions & 11 deletions src/pipelines/bloom/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use wgpu::{
BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingResource, BindingType, BlendComponent,
BlendFactor, BlendOperation, BlendState, Buffer, Color, ColorTargetState, ColorWrites,
CommandEncoder, Device, Extent3d, FilterMode, LoadOp, Operations, PushConstantRange,
RenderPassColorAttachment, RenderPassDescriptor, RenderPipeline, SamplerBindingType,
RenderPassColorAttachment, RenderPassDescriptor, RenderPipeline, Sampler, SamplerBindingType,
SamplerDescriptor, ShaderStages, StoreOp, TextureFormat, TextureSampleType, TextureUsages,
TextureViewDimension,
};
Expand All @@ -31,13 +31,6 @@ fn create_bloom_texture(device: &Device, width: u32, height: u32, mip_count: u32
},
mip_count,
BLOOM_TEXTURE_FORMAT,
&SamplerDescriptor {
min_filter: FilterMode::Linear,
mag_filter: FilterMode::Linear,
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
..Default::default()
},
TextureUsages::RENDER_ATTACHMENT | TextureUsages::TEXTURE_BINDING,
)
}
Expand All @@ -48,6 +41,7 @@ pub struct BloomPipeline {
upsample_pipeline: RenderPipeline,
final_pipeline: RenderPipeline,
bloom_texture: Texture,
bloom_sampler: Sampler,
downsampling_bind_groups: Vec<BindGroup>,
upsampling_bind_groups: Vec<BindGroup>,
vertices: Buffer,
Expand Down Expand Up @@ -247,12 +241,20 @@ impl BloomPipeline {
multiview: None,
cache: None,
});
let bloom_sampler = device.create_sampler(&SamplerDescriptor {
min_filter: FilterMode::Linear,
mag_filter: FilterMode::Linear,
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
..Default::default()
});

let (downsampling_bind_groups, upsampling_bind_groups) = Self::create_bind_groups(
device,
&downsample_pipeline,
&upsample_pipeline,
&bloom_texture,
&bloom_sampler,
mip_count,
);

Expand All @@ -262,6 +264,7 @@ impl BloomPipeline {
upsample_pipeline,
final_pipeline,
bloom_texture,
bloom_sampler,
downsampling_bind_groups,
upsampling_bind_groups,
vertices,
Expand All @@ -277,6 +280,7 @@ impl BloomPipeline {
downsample_pipeline: &RenderPipeline,
upsample_pipeline: &RenderPipeline,
bloom_texture: &Texture,
bloom_sampler: &Sampler,
mip_count: u32,
) -> (Vec<BindGroup>, Vec<BindGroup>) {
let bind_group_count = mip_count as usize - 1;
Expand All @@ -292,7 +296,7 @@ impl BloomPipeline {
},
BindGroupEntry {
binding: 1,
resource: BindingResource::Sampler(&bloom_texture.sampler),
resource: BindingResource::Sampler(bloom_sampler),
},
],
}));
Expand All @@ -310,7 +314,7 @@ impl BloomPipeline {
},
BindGroupEntry {
binding: 1,
resource: BindingResource::Sampler(&bloom_texture.sampler),
resource: BindingResource::Sampler(bloom_sampler),
},
],
}));
Expand Down Expand Up @@ -358,7 +362,7 @@ impl BloomPipeline {
},
BindGroupEntry {
binding: 1,
resource: BindingResource::Sampler(&self.bloom_texture.sampler),
resource: BindingResource::Sampler(&self.bloom_sampler),
},
],
});
Expand Down
Loading

0 comments on commit 573083a

Please sign in to comment.