Skip to content

Commit

Permalink
[rs] Merge gfx-rs#914
Browse files Browse the repository at this point in the history
914:  Change the texture sample type for the cube example from Float to Uint r=kvark a=igowen

this is a rework of gfx-rs#764. i think wgpu supports everything we need for this now!



Co-authored-by: Ian Gowen <[email protected]>
  • Loading branch information
bors[bot] and igowen authored May 18, 2021
2 parents 2a7e07d + 29b1df5 commit 7a43516
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 35 deletions.
37 changes: 6 additions & 31 deletions wgpu/examples/cube/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod framework;

use bytemuck::{Pod, Zeroable};
use std::{borrow::Cow, iter, mem};
use std::{borrow::Cow, mem};
use wgpu::util::DeviceExt;

#[repr(C)]
Expand Down Expand Up @@ -67,7 +67,7 @@ fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {

fn create_texels(size: usize) -> Vec<u8> {
(0..size * size)
.flat_map(|id| {
.map(|id| {
// get high five for recognizing this ;)
let cx = 3.0 * (id % size) as f32 / (size - 1) as f32 - 2.0;
let cy = 2.0 * (id / size) as f32 / (size - 1) as f32 - 1.0;
Expand All @@ -78,10 +78,7 @@ fn create_texels(size: usize) -> Vec<u8> {
y = 2.0 * old_x * y + cy;
count += 1;
}
iter::once(0xFF - (count * 5) as u8)
.chain(iter::once(0xFF - (count * 15) as u8))
.chain(iter::once(0xFF - (count * 50) as u8))
.chain(iter::once(0xFF))
count
})
.collect()
}
Expand Down Expand Up @@ -155,20 +152,11 @@ impl framework::Example for Example {
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
sample_type: wgpu::TextureSampleType::Uint,
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler {
comparison: false,
filtering: true,
},
count: None,
},
],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
Expand All @@ -191,7 +179,7 @@ impl framework::Example for Example {
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
format: wgpu::TextureFormat::R8Uint,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
});
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
Expand All @@ -204,22 +192,13 @@ impl framework::Example for Example {
&texels,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(std::num::NonZeroU32::new(4 * size).unwrap()),
bytes_per_row: Some(std::num::NonZeroU32::new(size).unwrap()),
rows_per_image: None,
},
texture_extent,
);

// Create other resources
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Nearest,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
});
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
let mx_ref: &[f32; 16] = mx_total.as_ref();
let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
Expand All @@ -240,10 +219,6 @@ impl framework::Example for Example {
binding: 1,
resource: wgpu::BindingResource::TextureView(&texture_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&sampler),
},
],
label: None,
});
Expand Down
8 changes: 4 additions & 4 deletions wgpu/examples/cube/shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ fn vs_main(
}

[[group(0), binding(1)]]
var r_color: texture_2d<f32>;
[[group(0), binding(2)]]
var r_sampler: sampler;
var r_color: texture_2d<u32>;

[[stage(fragment)]]
fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
return textureSample(r_color, r_sampler, in.tex_coord);
let tex = textureLoad(r_color, vec2<i32>(in.tex_coord * 256.0), 0);
let v = f32(tex.x) / 255.0;
return vec4<f32>(1.0 - (v * 5.0), 1.0 - (v * 15.0), 1.0 - (v * 50.0), 1.0);
}

[[stage(fragment)]]
Expand Down

0 comments on commit 7a43516

Please sign in to comment.