From 8368545e6c5068ce113d36e43196d25c1f81bab0 Mon Sep 17 00:00:00 2001 From: Piotrek Date: Fri, 23 Apr 2021 14:32:45 +0200 Subject: [PATCH] fix texture format --- src/renderer/buffers/brick_buffer.rs | 22 +++++++++++++--------- src/renderer/mod.rs | 2 +- src/shaders/bin/shader.frag.spv | Bin 5724 -> 5800 bytes src/shaders/raycasting.cginc | 7 ++++++- src/shaders/shader.frag | 3 ++- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/renderer/buffers/brick_buffer.rs b/src/renderer/buffers/brick_buffer.rs index 8e32d95..ab7594e 100644 --- a/src/renderer/buffers/brick_buffer.rs +++ b/src/renderer/buffers/brick_buffer.rs @@ -2,7 +2,7 @@ use wgpu::util::DeviceExt; use std::convert::TryInto; const BRICK_SIZE : usize = 32; // 32x32x32 brick size -const BRICK_NUM : usize = 10000; // 64 bricks in texture +const BRICK_NUM : usize = 1; // 64 bricks in texture const BRICK_LEN : usize = BRICK_SIZE*BRICK_SIZE*BRICK_SIZE; const DATA_LEN : usize = BRICK_LEN * BRICK_NUM; @@ -46,7 +46,7 @@ impl BrickBuffer { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D3, - format: wgpu::TextureFormat::R32Uint, + format: wgpu::TextureFormat::R8Uint, usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, // STORAGE? label: Some("Brick texture"), } @@ -93,6 +93,7 @@ impl BrickBuffer { // Data let mut bricks = BrickData::new(); + println!("Brick buffer size: {} MB", DATA_LEN as f32 / 1024.0 / 1024.0); for x in 0..32 { for y in 0..16 { for z in 0..32 { @@ -102,23 +103,26 @@ impl BrickBuffer { } // Done - println!("Brick buffer size: {} MB", DATA_LEN as f32 / 1024.0 / 1024.0); Self { bricks, texture, size, bind_layout, bind_group } } - pub fn write(&mut self, queue: &wgpu::Queue) + /* + Write brick data to gpu, should be called to apply changes + offset: Brick index to copy + */ + pub fn write(&mut self, queue: &wgpu::Queue, offset: usize) { - return (); - // Get first brick - let brick_bytes : [u8;BRICK_LEN] = self.bricks.data[0..BRICK_LEN].try_into().unwrap(); + let brick_offset = offset*BRICK_LEN; + let brick_bytes : [u8;BRICK_LEN] = self.bricks.data[brick_offset..brick_offset+BRICK_LEN].try_into().unwrap(); let brick_size = wgpu::Extent3d{ width: BRICK_SIZE as u32, height: BRICK_SIZE as u32, depth: BRICK_SIZE as u32 }; + let brick_origin = wgpu::Origin3d{ x:0, y:0, z: offset as u32 }; // Write queue.write_texture( - wgpu::TextureCopyView { texture: &self.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO }, + wgpu::TextureCopyView { texture: &self.texture, mip_level: 0, origin: brick_origin }, &brick_bytes, - wgpu::TextureDataLayout { offset: 0, bytes_per_row: self.size.width, rows_per_image: self.size.height }, + wgpu::TextureDataLayout { offset: 0, bytes_per_row: brick_size.width, rows_per_image: brick_size.height }, brick_size ); } diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 33e18b8..3333adb 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -111,7 +111,7 @@ impl Renderer { node_buffer.write(&queue); let mut brick_buffer = BrickBuffer::new(&device); - brick_buffer.write(&queue); + brick_buffer.write(&queue, 0); // Pipeline let pipeline = Self::create_pipeline(&device, &swapchain_desc, &[&uniform_buffer.bind_layout, &node_buffer.bind_layout, &brick_buffer.bind_layout]); diff --git a/src/shaders/bin/shader.frag.spv b/src/shaders/bin/shader.frag.spv index 6716f2d05dd8619fdace11b050d2369f88774f32..32d518ee0a560a1c4596a21543c93f7ebb66d2fb 100644 GIT binary patch delta 124 zcmcbkvqG1bnMs+Qfq{{Mi-DKn%|>2BCJ{DW- ztPE@nuYqz&MVZOjFv-cC%%3IQ8CbwFNWuS1^Cr Nyo1Gpc@xJ9J^*qg4>te+ diff --git a/src/shaders/raycasting.cginc b/src/shaders/raycasting.cginc index c592d6f..311ff9d 100644 --- a/src/shaders/raycasting.cginc +++ b/src/shaders/raycasting.cginc @@ -19,6 +19,11 @@ vec3 solve(vec3 point, vec3 view, vec3 normal) return (ambient + diffuse + specular) * object_color; } +vec3 castBricks(vec3 origin, vec3 dir) +{ + +} + vec3 castNodes(vec3 origin, vec3 dir) { // Round input pos to nearest voxel @@ -37,7 +42,7 @@ vec3 castNodes(vec3 origin, vec3 dir) for(int i=0;i<50;i++) { // Get node - uint node = texelFetch(diffuseTex, ivec3(pos), 0).r; + uint node = texelFetch(nodesTexture, ivec3(pos), 0).r; if(node != 0) { // Hit point diff --git a/src/shaders/shader.frag b/src/shaders/shader.frag index e7adf88..df84b62 100644 --- a/src/shaders/shader.frag +++ b/src/shaders/shader.frag @@ -14,7 +14,8 @@ layout(set=0, binding=0) uniform Uniforms //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; +layout(set=1,binding=0) uniform usampler3D nodesTexture; +layout(set=2,binding=0) uniform usampler3D bricksTexture; // Defines #define PI 3.141592