wip: creating 3d texture node buffer
This commit is contained in:
@@ -1,8 +1,91 @@
|
|||||||
|
|
||||||
|
pub const DATA: &[u8] = &[
|
||||||
|
// z0
|
||||||
|
1,0,
|
||||||
|
0,1,
|
||||||
|
// z1
|
||||||
|
0,1,
|
||||||
|
1,0,
|
||||||
|
];
|
||||||
|
|
||||||
pub struct NodeBuffer {
|
pub struct NodeBuffer {
|
||||||
|
texture: wgpu::Texture,
|
||||||
|
size: wgpu::Extent3d,
|
||||||
|
pub bind_layout: wgpu::BindGroupLayout,
|
||||||
|
pub bind_group: wgpu::BindGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NodeBuffer {
|
impl NodeBuffer {
|
||||||
|
|
||||||
|
pub fn new(device: &wgpu::Device, _size: u32) -> Self {
|
||||||
|
|
||||||
|
// Create texture
|
||||||
|
let size = wgpu::Extent3d { width: _size, height: _size, depth: _size };
|
||||||
|
let texture = device.create_texture(
|
||||||
|
&wgpu::TextureDescriptor {
|
||||||
|
size: size,
|
||||||
|
mip_level_count: 1,
|
||||||
|
sample_count: 1,
|
||||||
|
dimension: wgpu::TextureDimension::D3,
|
||||||
|
format: wgpu::TextureFormat::R8Uint,
|
||||||
|
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, // STORAGE?
|
||||||
|
label: Some("Node texture"),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Bind layout
|
||||||
|
let bind_layout = device.create_bind_group_layout(
|
||||||
|
&wgpu::BindGroupLayoutDescriptor {
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 0,
|
||||||
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||||
|
ty: wgpu::BindingType::Texture { multisampled: false, view_dimension: wgpu::TextureViewDimension::D3, sample_type: wgpu::TextureSampleType::Uint },
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 1,
|
||||||
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||||
|
ty: wgpu::BindingType::Sampler { comparison: false, filtering: false },
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
label: Some("Node texture layout"),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Bind group
|
||||||
|
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
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::Nearest,
|
||||||
|
mipmap_filter: wgpu::FilterMode::Nearest,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
let bind_group = device.create_bind_group(
|
||||||
|
&wgpu::BindGroupDescriptor {
|
||||||
|
layout: &bind_layout,
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&view) },
|
||||||
|
wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&sampler) }
|
||||||
|
],
|
||||||
|
label: Some("Node texture group"),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Done
|
||||||
|
Self { texture, size, bind_layout, bind_group }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(&mut self, queue: &wgpu::Queue) {
|
||||||
|
queue.write_texture(
|
||||||
|
wgpu::TextureCopyView { texture: &self.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO },
|
||||||
|
DATA,
|
||||||
|
wgpu::TextureDataLayout { offset: 0, bytes_per_row: self.size.width, rows_per_image: self.size.height },
|
||||||
|
self.size
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+6
-2
@@ -15,6 +15,7 @@ pub struct Renderer {
|
|||||||
// Buffers
|
// Buffers
|
||||||
raster_buffer: RasterBuffer,
|
raster_buffer: RasterBuffer,
|
||||||
uniform_buffer: UniformBuffer,
|
uniform_buffer: UniformBuffer,
|
||||||
|
node_buffer: NodeBuffer,
|
||||||
// Camera
|
// Camera
|
||||||
pub camera: Camera,
|
pub camera: Camera,
|
||||||
}
|
}
|
||||||
@@ -105,13 +106,15 @@ impl Renderer {
|
|||||||
// Buffers
|
// Buffers
|
||||||
let uniform_buffer = UniformBuffer::new(&device);
|
let uniform_buffer = UniformBuffer::new(&device);
|
||||||
let raster_buffer = RasterBuffer::new(&device);
|
let raster_buffer = RasterBuffer::new(&device);
|
||||||
|
let mut node_buffer = NodeBuffer::new(&device, 2);
|
||||||
|
node_buffer.write(&queue);
|
||||||
|
|
||||||
// Pipeline
|
// Pipeline
|
||||||
let pipeline = Self::create_pipeline(&device, &swapchain_desc, &[&uniform_buffer.bind_layout]);
|
let pipeline = Self::create_pipeline(&device, &swapchain_desc, &[&uniform_buffer.bind_layout, &node_buffer.bind_layout]);
|
||||||
|
|
||||||
// Save values in app
|
// Save values in app
|
||||||
println!("Initialized");
|
println!("Initialized");
|
||||||
Self { surface, device, queue, swapchain_desc, swapchain, pipeline, raster_buffer, uniform_buffer, camera }
|
Self { surface, device, queue, swapchain_desc, swapchain, pipeline, raster_buffer, uniform_buffer, node_buffer, camera }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resize(&mut self, new_size: Option<PhysicalSize<u32>>) {
|
pub fn resize(&mut self, new_size: Option<PhysicalSize<u32>>) {
|
||||||
@@ -158,6 +161,7 @@ impl Renderer {
|
|||||||
// Data
|
// Data
|
||||||
render_pass.set_pipeline(&self.pipeline);
|
render_pass.set_pipeline(&self.pipeline);
|
||||||
render_pass.set_bind_group(0, &self.uniform_buffer.bind_group, &[]);
|
render_pass.set_bind_group(0, &self.uniform_buffer.bind_group, &[]);
|
||||||
|
render_pass.set_bind_group(1, &self.node_buffer.bind_group, &[]);
|
||||||
// Vertices and indices
|
// Vertices and indices
|
||||||
render_pass.set_vertex_buffer(0, self.raster_buffer.vertex_buffer.slice(..));
|
render_pass.set_vertex_buffer(0, self.raster_buffer.vertex_buffer.slice(..));
|
||||||
render_pass.set_index_buffer(self.raster_buffer.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
render_pass.set_index_buffer(self.raster_buffer.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
||||||
|
|||||||
Binary file not shown.
@@ -12,6 +12,9 @@ layout(set=0, binding=0) uniform Uniforms
|
|||||||
vec3 cam_pos;
|
vec3 cam_pos;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
layout(set = 1, binding = 0) uniform texture3D node_tex;
|
||||||
|
layout(set = 1, binding = 1) uniform sampler node_sampl;
|
||||||
|
|
||||||
// Defines
|
// Defines
|
||||||
#define PI 3.141592
|
#define PI 3.141592
|
||||||
#define EPSILON 0.01
|
#define EPSILON 0.01
|
||||||
|
|||||||
Reference in New Issue
Block a user