Compare commits

...

9 Commits

Author SHA1 Message Date
28ffc28dca buffer binding array? 2023-09-05 12:02:54 -07:00
7b5ce3feb1 dedicated staging belt 2023-09-05 12:02:54 -07:00
d2fdb5d94b entity_id trash 2023-09-05 12:02:54 -07:00
b68dfa791d array + instance_index is not the way 2023-09-05 12:02:54 -07:00
2df929c15f no fragment 2023-09-05 12:02:54 -07:00
1746d5445c add entity CFrames 2023-09-01 15:24:45 -07:00
5dbe207d39 comment on value 2023-09-01 14:57:38 -07:00
a853eb965e rename uniform_buf to camera_buf 2023-09-01 14:57:04 -07:00
ba2bab562f add suzanne 2023-09-01 14:22:12 -07:00
3 changed files with 2705 additions and 54 deletions

2580
models/suzanne.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -9,9 +9,11 @@ const IMAGE_SIZE: u32 = 128;
struct Vertex { struct Vertex {
pos: [f32; 3], pos: [f32; 3],
normal: [f32; 3], normal: [f32; 3],
entity_id: u32,
} }
struct Entity { struct Entity {
transform: glam::Affine3A,
vertex_count: u32, vertex_count: u32,
vertex_buf: wgpu::Buffer, vertex_buf: wgpu::Buffer,
} }
@ -34,6 +36,8 @@ struct Camera {
walkspeed: f32, walkspeed: f32,
} }
const MAGIC_ENTITIES: usize = 7;
const CONTROL_MOVEFORWARD:u32 = 0b00000001; const CONTROL_MOVEFORWARD:u32 = 0b00000001;
const CONTROL_MOVEBACK:u32 = 0b00000010; const CONTROL_MOVEBACK:u32 = 0b00000010;
const CONTROL_MOVERIGHT:u32 = 0b00000100; const CONTROL_MOVERIGHT:u32 = 0b00000100;
@ -100,7 +104,7 @@ impl Camera {
raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]); raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]);
raw[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view)[..]); raw[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view)[..]);
raw[48..51].copy_from_slice(AsRef::<[f32; 3]>::as_ref(&self.pos)); raw[48..51].copy_from_slice(AsRef::<[f32; 3]>::as_ref(&self.pos));
raw[51] = 1.0; raw[51] = 1.0;//cam_pos is vec4
raw raw
} }
} }
@ -111,10 +115,12 @@ pub struct Skybox {
entity_pipeline: wgpu::RenderPipeline, entity_pipeline: wgpu::RenderPipeline,
ground_pipeline: wgpu::RenderPipeline, ground_pipeline: wgpu::RenderPipeline,
bind_group: wgpu::BindGroup, bind_group: wgpu::BindGroup,
uniform_buf: wgpu::Buffer, camera_buf: wgpu::Buffer,
entity_buf: wgpu::Buffer,
entities: Vec<Entity>, entities: Vec<Entity>,
depth_view: wgpu::TextureView, depth_view: wgpu::TextureView,
staging_belt: wgpu::util::StagingBelt, camera_stagingbelt: wgpu::util::StagingBelt,
entity_stagingbelt: wgpu::util::StagingBelt,
} }
impl Skybox { impl Skybox {
@ -143,27 +149,22 @@ impl Skybox {
} }
} }
impl strafe_client::framework::Example for Skybox { fn get_entity_uniform_data(entities:& Vec<Entity>) -> [f32; (9+3)*MAGIC_ENTITIES] {
fn optional_features() -> wgpu::Features { let mut raw = [0f32; (9+3)*MAGIC_ENTITIES];
wgpu::Features::TEXTURE_COMPRESSION_ASTC for (i,entity) in entities.iter().enumerate() {
| wgpu::Features::TEXTURE_COMPRESSION_ETC2 raw[i*12..(i+1)*12-3].copy_from_slice(&AsRef::<[f32; 9]>::as_ref(&glam::Mat3::from(entity.transform.matrix3))[..]);
| wgpu::Features::TEXTURE_COMPRESSION_BC raw[i*12+9..(i+1)*12].copy_from_slice(AsRef::<[f32; 3]>::as_ref(&glam::Vec3::from(entity.transform.translation)));
} }
raw
}
fn init( fn add_obj(device:&wgpu::Device,entities:& mut Vec<Entity>,source:&[u8]){
config: &wgpu::SurfaceConfiguration,
_adapter: &wgpu::Adapter,
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> Self {
let mut entities = Vec::new();
{
let source = include_bytes!("../models/teslacyberv3.0.obj");
let data = obj::ObjData::load_buf(&source[..]).unwrap(); let data = obj::ObjData::load_buf(&source[..]).unwrap();
let mut vertices = Vec::new(); let mut vertices = Vec::new();
for object in data.objects { for object in data.objects {
for group in object.groups { for group in object.groups {
vertices.clear(); vertices.clear();
let entity_id = entities.len() as u32;
for poly in group.polys { for poly in group.polys {
for end_index in 2..poly.0.len() { for end_index in 2..poly.0.len() {
for &index in &[0, end_index - 1, end_index] { for &index in &[0, end_index - 1, end_index] {
@ -172,6 +173,7 @@ impl strafe_client::framework::Example for Skybox {
vertices.push(Vertex { vertices.push(Vertex {
pos: data.position[position_id], pos: data.position[position_id],
normal: data.normal[normal_id.unwrap()], normal: data.normal[normal_id.unwrap()],
entity_id,
}) })
} }
} }
@ -182,19 +184,40 @@ impl strafe_client::framework::Example for Skybox {
usage: wgpu::BufferUsages::VERTEX, usage: wgpu::BufferUsages::VERTEX,
}); });
entities.push(Entity { entities.push(Entity {
transform: glam::Affine3A::default(),
vertex_count: vertices.len() as u32, vertex_count: vertices.len() as u32,
vertex_buf, vertex_buf,
}); });
} }
} }
}
impl strafe_client::framework::Example for Skybox {
fn optional_features() -> wgpu::Features {
wgpu::Features::TEXTURE_COMPRESSION_ASTC
| wgpu::Features::TEXTURE_COMPRESSION_ETC2
| wgpu::Features::TEXTURE_COMPRESSION_BC
| wgpu::Features::BUFFER_BINDING_ARRAY
} }
fn init(
config: &wgpu::SurfaceConfiguration,
_adapter: &wgpu::Adapter,
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> Self {
let mut entities = Vec::<Entity>::new();
add_obj(device,& mut entities,include_bytes!("../models/teslacyberv3.0.obj"));
add_obj(device,& mut entities,include_bytes!("../models/suzanne.obj"));
println!("entities.len = {:?}", entities.len());
entities[6].transform=glam::Affine3A::from_translation(glam::vec3(10.,5.,10.));
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None, label: None,
entries: &[ entries: &[
wgpu::BindGroupLayoutEntry { wgpu::BindGroupLayoutEntry {
binding: 0, binding: 0,
visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT, visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer { ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform, ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false, has_dynamic_offset: false,
@ -218,6 +241,16 @@ impl strafe_client::framework::Example for Skybox {
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None, count: None,
}, },
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: Some(std::num::NonZeroU32::new(MAGIC_ENTITIES as u32).unwrap()),
},
], ],
}); });
@ -243,10 +276,17 @@ impl strafe_client::framework::Example for Skybox {
grounded: true, grounded: true,
walkspeed: 18.0, walkspeed: 18.0,
}; };
let raw_uniforms = camera.to_uniform_data(); let camera_uniforms = camera.to_uniform_data();
let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Buffer"), label: Some("Camera"),
contents: bytemuck::cast_slice(&raw_uniforms), contents: bytemuck::cast_slice(&camera_uniforms),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let entity_uniforms = get_entity_uniform_data(&entities);
let entity_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("EntityData"),
contents: bytemuck::cast_slice(&entity_uniforms),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
}); });
@ -293,7 +333,7 @@ impl strafe_client::framework::Example for Skybox {
buffers: &[wgpu::VertexBufferLayout { buffers: &[wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress, array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex, step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3], attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3, 2 => Uint32],
}], }],
}, },
fragment: Some(wgpu::FragmentState { fragment: Some(wgpu::FragmentState {
@ -431,7 +471,7 @@ impl strafe_client::framework::Example for Skybox {
entries: &[ entries: &[
wgpu::BindGroupEntry { wgpu::BindGroupEntry {
binding: 0, binding: 0,
resource: uniform_buf.as_entire_binding(), resource: camera_buf.as_entire_binding(),
}, },
wgpu::BindGroupEntry { wgpu::BindGroupEntry {
binding: 1, binding: 1,
@ -441,6 +481,10 @@ impl strafe_client::framework::Example for Skybox {
binding: 2, binding: 2,
resource: wgpu::BindingResource::Sampler(&sampler), resource: wgpu::BindingResource::Sampler(&sampler),
}, },
wgpu::BindGroupEntry {
binding: 3,
resource: entity_buf.as_entire_binding(),
},
], ],
label: None, label: None,
}); });
@ -453,10 +497,12 @@ impl strafe_client::framework::Example for Skybox {
entity_pipeline, entity_pipeline,
ground_pipeline, ground_pipeline,
bind_group, bind_group,
uniform_buf, camera_buf,
entity_buf,
entities, entities,
depth_view, depth_view,
staging_belt: wgpu::util::StagingBelt::new(0x100), camera_stagingbelt: wgpu::util::StagingBelt::new(0x100),
entity_stagingbelt: wgpu::util::StagingBelt::new(0x100),
} }
} }
@ -571,18 +617,31 @@ impl strafe_client::framework::Example for Skybox {
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
// update rotation // update rotation
let raw_uniforms = self.camera.to_uniform_data(); let camera_uniforms = self.camera.to_uniform_data();
self.staging_belt self.camera_stagingbelt
.write_buffer( .write_buffer(
&mut encoder, &mut encoder,
&self.uniform_buf, &self.camera_buf,
0, 0,
wgpu::BufferSize::new((raw_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(), wgpu::BufferSize::new((camera_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
device, device,
) )
.copy_from_slice(bytemuck::cast_slice(&raw_uniforms)); .copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
self.staging_belt.finish(); self.camera_stagingbelt.finish();
let entity_uniforms = get_entity_uniform_data(&self.entities);
self.entity_stagingbelt
.write_buffer(
&mut encoder,
&self.entity_buf,//description of where data will be written when command is executed
0,//offset in staging belt?
wgpu::BufferSize::new((entity_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
device,
)
.copy_from_slice(bytemuck::cast_slice(&entity_uniforms));
self.entity_stagingbelt.finish();
{ {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
@ -627,7 +686,8 @@ impl strafe_client::framework::Example for Skybox {
queue.submit(std::iter::once(encoder.finish())); queue.submit(std::iter::once(encoder.finish()));
self.staging_belt.recall(); self.camera_stagingbelt.recall();
self.entity_stagingbelt.recall();
} }
} }

View File

@ -67,15 +67,26 @@ struct EntityOutput {
@location(3) view: vec3<f32>, @location(3) view: vec3<f32>,
}; };
struct EntityTransform {
matrix3: mat3x3<f32>,
translation: vec3<f32>,
};
@group(0)
@binding(3)
var<uniform> r_EntityTransforms: array<EntityTransform, 7>;
@vertex @vertex
fn vs_entity( fn vs_entity(
@location(0) pos: vec3<f32>, @location(0) pos: vec3<f32>,
@location(1) normal: vec3<f32>, @location(1) normal: vec3<f32>,
@location(2) entity_id: u32,
) -> EntityOutput { ) -> EntityOutput {
var position: vec3<f32> = r_EntityTransforms[entity_id].matrix3 * pos+r_EntityTransforms[entity_id].translation;
var result: EntityOutput; var result: EntityOutput;
result.normal = normal; result.normal = r_EntityTransforms[entity_id].matrix3 * normal;
result.view = pos - r_data.cam_pos.xyz; result.view = position - r_data.cam_pos.xyz;
result.position = r_data.proj * r_data.view * vec4<f32>(pos, 1.0); result.position = r_data.proj * r_data.view * vec4<f32>(position, 1.0);
return result; return result;
} }