mirror of
https://github.com/gfx-rs/wgpu.git
synced 2026-04-22 03:02:01 -04:00
[rs] Change the texture sample type for the cube example from Float to Uint, and move the mandelbrot->color mapping functionality from the texel generation code to the fragment shader.
This commit is contained in:
@@ -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)]
|
||||
@@ -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;
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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 {
|
||||
@@ -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());
|
||||
@@ -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 {
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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)]]
|
||||
|
||||
Reference in New Issue
Block a user