working 3d textures
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
use winit::{
|
||||
event::{Event, WindowEvent, KeyboardInput, MouseButton, ElementState, VirtualKeyCode},
|
||||
event::{Event, WindowEvent},
|
||||
event_loop::{ControlFlow, EventLoop},
|
||||
window::WindowBuilder,
|
||||
dpi::LogicalSize
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
|
||||
pub const DATA: &[u8] = &[
|
||||
pub const DATA: &[u32] = &[
|
||||
// z0
|
||||
1,0,
|
||||
0,1,
|
||||
0,0,
|
||||
// z1
|
||||
0,1,
|
||||
1,0,
|
||||
];
|
||||
0,0,
|
||||
0,0,
|
||||
];
|
||||
|
||||
|
||||
pub struct NodeBuffer {
|
||||
texture: wgpu::Texture,
|
||||
@@ -27,7 +28,7 @@ impl NodeBuffer {
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D3,
|
||||
format: wgpu::TextureFormat::R8Uint,
|
||||
format: wgpu::TextureFormat::R32Uint,
|
||||
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, // STORAGE?
|
||||
label: Some("Node texture"),
|
||||
}
|
||||
@@ -57,12 +58,8 @@ impl NodeBuffer {
|
||||
// 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,
|
||||
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(
|
||||
@@ -81,10 +78,23 @@ impl NodeBuffer {
|
||||
}
|
||||
|
||||
pub fn write(&mut self, queue: &wgpu::Queue) {
|
||||
// Convert 32 bit values to 8 bit
|
||||
const LEN: usize = DATA.len();
|
||||
let mut bytes : [u8; LEN*4] = [0; LEN*4];
|
||||
for i in 0..LEN {
|
||||
let idx = i * 4;
|
||||
let byte = DATA[i].to_be_bytes();
|
||||
bytes[idx+0] = byte[0];
|
||||
bytes[idx+1] = byte[1];
|
||||
bytes[idx+2] = byte[2];
|
||||
bytes[idx+3] = byte[3];
|
||||
}
|
||||
|
||||
// Write
|
||||
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 },
|
||||
&bytes,
|
||||
wgpu::TextureDataLayout { offset: 0, bytes_per_row: 4*self.size.width, rows_per_image: self.size.height },
|
||||
self.size
|
||||
);
|
||||
}
|
||||
|
||||
+2
-3
@@ -1,5 +1,4 @@
|
||||
use winit::{window::Window, dpi::PhysicalSize};
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
mod buffers; use buffers::{NodeBuffer, UniformBuffer, RasterBuffer, Vertex};
|
||||
pub mod camera; use camera::Camera;
|
||||
@@ -28,7 +27,7 @@ impl Renderer {
|
||||
format: adapter.get_swap_chain_preferred_format(&surface),
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
present_mode: wgpu::PresentMode::Immediate, // Fifo = Vsync
|
||||
present_mode: wgpu::PresentMode::Mailbox, // Fifo = Vsync
|
||||
};
|
||||
let swapchain = device.create_swap_chain(&surface, &swapchain_desc);
|
||||
(swapchain_desc, swapchain)
|
||||
@@ -84,7 +83,7 @@ impl Renderer {
|
||||
pub async fn new(window: &Window) -> Self {
|
||||
|
||||
// Create surface and pick device
|
||||
let instance = wgpu::Instance::new(wgpu::BackendBit::DX11);
|
||||
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
|
||||
let surface = unsafe { instance.create_surface(window) };
|
||||
|
||||
// Pick adapter (physical gpu)
|
||||
|
||||
Binary file not shown.
+13
-6
@@ -12,8 +12,9 @@ layout(set=0, binding=0) uniform Uniforms
|
||||
vec3 cam_pos;
|
||||
};
|
||||
|
||||
layout(set = 1, binding = 0) uniform texture3D node_tex;
|
||||
layout(set = 1, binding = 1) uniform sampler node_sampl;
|
||||
//layout(set = 1, binding = 0) uniform texture3D node_tex;
|
||||
//layout(set = 1, binding = 1) uniform usampler3D node_sampl;
|
||||
layout(set=1,binding=0) uniform usampler3D diffuseTex;
|
||||
|
||||
// Defines
|
||||
#define PI 3.141592
|
||||
@@ -27,9 +28,15 @@ layout(set = 1, binding = 1) uniform sampler node_sampl;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Test
|
||||
uint node = texelFetch(diffuseTex, ivec3(0, 0, 0), 0).r;
|
||||
if(node == 4294967295) out_color = vec4(0.0, 0.5, 1.0, 1.0);
|
||||
else out_color = vec4(1.0, 0.1, 0.0, 1.0);
|
||||
|
||||
|
||||
// Calculate ray direction
|
||||
vec3 view_dir = (proj_matrix_inv * vec4(vert_uv, 0, 1)).xyz;
|
||||
vec3 ray_dir = normalize(view_matrix * vec4(view_dir,0)).xyz;
|
||||
// Calculate color
|
||||
out_color = vec4(raymarch(cam_pos, ray_dir), 1);
|
||||
// vec3 view_dir = (proj_matrix_inv * vec4(vert_uv, 0, 1)).xyz;
|
||||
// vec3 ray_dir = normalize(view_matrix * vec4(view_dir,0)).xyz;
|
||||
// // Calculate color
|
||||
// out_color = vec4(raymarch(cam_pos, ray_dir), 1);
|
||||
}
|
||||
Reference in New Issue
Block a user