migrated gifs

pull/29/head
Ben Hansen 4 years ago
parent 032253fea9
commit 4a772f7ee0

3
Cargo.lock generated

@ -840,11 +840,12 @@ dependencies = [
"cgmath 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"framework 0.1.0",
"futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"gif 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)",
"glsl-to-spirv 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.22.4 (registry+https://github.com/rust-lang/crates.io-index)",
"tobj 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

@ -44,9 +44,9 @@ impl Vertex for ModelVertex {
}
}
pub struct Material {
pub struct Material<'a> {
pub name: String,
pub diffuse_texture: texture::Texture,
pub diffuse_texture: texture::Texture<'a>,
pub bind_group: wgpu::BindGroup,
}
@ -58,12 +58,12 @@ pub struct Mesh {
pub material: usize,
}
pub struct Model {
pub struct Model<'a> {
pub meshes: Vec<Mesh>,
pub materials: Vec<Material>,
pub materials: Vec<Material<'a>>,
}
impl Model {
impl<'a> Model<'a> {
pub fn load<P: AsRef<Path>>(
device: &wgpu::Device,
layout: &wgpu::BindGroupLayout,

@ -5,14 +5,14 @@ use std::mem;
use crate::buffer;
pub struct Texture {
pub struct Texture<'a> {
pub texture: wgpu::Texture,
pub view: wgpu::TextureView,
pub sampler: wgpu::Sampler,
// pub desc: wgpu::TextureDescriptor,
pub desc: wgpu::TextureDescriptor<'a>,
}
impl Texture {
impl<'a> Texture<'a> {
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
pub fn load<P: AsRef<Path>>(device: &wgpu::Device, path: P) -> Result<(Self, wgpu::CommandBuffer), failure::Error> {
@ -28,7 +28,7 @@ impl Texture {
height: sc_desc.height,
depth: 1,
},
array
array_layer_count: 1,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
@ -38,7 +38,7 @@ impl Texture {
Self::from_descriptor(device, desc)
}
pub fn from_descriptor(device: &wgpu::Device, desc: wgpu::TextureDescriptor) -> Self {
pub fn from_descriptor(device: &wgpu::Device, desc: wgpu::TextureDescriptor<'a>) -> Self {
let texture = device.create_texture(&desc);
let view = texture.create_default_view();
@ -54,7 +54,7 @@ impl Texture {
compare: wgpu::CompareFunction::LessEqual,
});
Self { texture, view, sampler, /*desc*/ }
Self { texture, view, sampler, desc }
}
pub fn from_bytes(device: &wgpu::Device, bytes: &[u8]) -> Result<(Self, wgpu::CommandBuffer), failure::Error> {
@ -88,7 +88,9 @@ impl Texture {
wgpu::BufferUsage::COPY_SRC,
);
let mut encoder = device.create_command_encoder(&Default::default());
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: None,
});
encoder.copy_buffer_to_texture(
wgpu::BufferCopyView {

@ -13,7 +13,8 @@ glsl-to-spirv = "0.1.7"
cgmath = "0.17.0"
failure = "0.1"
tobj = "0.1"
wgpu = "0.4.0"
wgpu = "0.5.0"
futures = "0.3.4"
gif = "0.10.3"
framework = { path = "../framework" }

@ -3,9 +3,21 @@ extern crate framework;
use std::mem;
use std::sync::{Arc, Mutex};
fn main() {
let adapter = wgpu::Adapter::request(&Default::default()).unwrap();
let (device, mut queue) = adapter.request_device(&Default::default());
async fn run() {
let adapter = wgpu::Adapter::request(
&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::Default,
compatible_surface: None,
},
wgpu::BackendBit::PRIMARY, // Vulkan + Metal + DX12 + Browser WebGPU
).await.unwrap();
let (device, queue) = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: Default::default(),
}).await;
let colors = [
[0.0, 0.0, 0.0],
@ -33,6 +45,7 @@ fn main() {
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsage::COPY_SRC
| wgpu::TextureUsage::OUTPUT_ATTACHMENT,
label: None,
};
let render_target = framework::Texture::from_descriptor(&device, rt_desc);
@ -42,6 +55,7 @@ fn main() {
let buffer_desc = wgpu::BufferDescriptor {
size: buffer_size,
usage: wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::MAP_READ,
label: None,
};
let output_buffer = device.create_buffer(&buffer_desc);
@ -52,7 +66,9 @@ fn main() {
let frames = Arc::new(Mutex::new(Vec::new()));
for c in &colors {
let mut encoder = device.create_command_encoder(&Default::default());
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: None,
});
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[
@ -88,8 +104,8 @@ fn main() {
wgpu::BufferCopyView {
buffer: &output_buffer,
offset: 0,
row_pitch: pixel_size * texture_size,
image_height: texture_size,
bytes_per_row: pixel_size * texture_size,
rows_per_image: texture_size,
},
render_target.desc.size
);
@ -97,19 +113,22 @@ fn main() {
queue.submit(&[encoder.finish()]);
let frames_clone = frames.clone();
output_buffer.map_read_async(0, buffer_size, move |result: wgpu::BufferMapAsyncResult<&[u8]>| {
match result {
Ok(mapping) => {
let data = Vec::from(mapping.data);
let mut f = frames_clone.lock().unwrap();
(*f).push(data);
}
_ => { eprintln!("Something went wrong") }
}
});
let mapping = output_buffer.map_read(0, buffer_size);
// wait for the GPU to finish
device.poll(true);
device.poll(wgpu::Maintain::Wait);
let result = mapping.await;
match result {
Ok(mapping) => {
let data = Vec::from(mapping.as_slice());
let mut f = frames_clone.lock().unwrap();
(*f).push(data);
}
_ => { eprintln!("Something went wrong") }
}
}
let mut frames = Arc::try_unwrap(frames)
@ -191,8 +210,10 @@ fn create_render_pipeline(device: &wgpu::Device, target: &framework::Texture) ->
},
],
depth_stencil_state: None,
index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[],
vertex_state: wgpu::VertexStateDescriptor {
index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[],
},
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
@ -201,3 +222,7 @@ fn create_render_pipeline(device: &wgpu::Device, target: &framework::Texture) ->
render_pipeline
}
fn main() {
use futures::executor::block_on;
block_on(run());
}

@ -1,35 +0,0 @@
#version 450
layout(location=0) in vec2 v_tex_coords;
layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_position;
layout(location=0) out vec4 f_color;
layout(set = 0, binding = 0) uniform texture2D t_diffuse;
layout(set = 0, binding = 1) uniform sampler s_diffuse;
layout(set=1, binding=2)
uniform Lights {
vec3 u_light;
};
const vec3 ambient_color = vec3(0.0, 0.0, 0.0);
const vec3 specular_color = vec3(1.0, 1.0, 1.0);
const float shininess = 32;
void main() {
vec4 diffuse_color = texture(sampler2D(t_diffuse, s_diffuse), v_tex_coords);
float diffuse_term = max(dot(normalize(v_normal), normalize(u_light)), 0);
vec3 camera_dir = normalize(-v_position);
// This is an aproximation of the actual reflection vector, aka what
// angle you have to look at the object to be blinded by the light
vec3 half_direction = normalize(normalize(u_light) + camera_dir);
float specular_term = pow(max(dot(normalize(v_normal), half_direction), 0.0), shininess);
f_color = vec4(ambient_color, 1.0) + vec4(specular_term * specular_color, 1.0) + diffuse_term * diffuse_color;
}

@ -1,34 +0,0 @@
#version 450
layout(location=0) in vec3 a_position;
layout(location=1) in vec2 a_tex_coords;
layout(location=2) in vec3 a_normal;
layout(location=0) out vec2 v_tex_coords;
layout(location=1) out vec3 v_normal;
layout(location=2) out vec3 v_position;
layout(set=1, binding=0)
uniform Uniforms {
mat4 u_view_proj;
};
layout(set=1, binding=1)
buffer Instances {
mat4 s_models[];
};
void main() {
v_tex_coords = a_tex_coords;
mat4 model = s_models[gl_InstanceIndex];
// Rotate the normals with respect to the model, ignoring scaling
mat3 normal_matrix = mat3(transpose(inverse(mat3(model))));
v_normal = normal_matrix * a_normal;
gl_Position = u_view_proj * model * vec4(a_position, 1.0);
// Get the position relative to the view for the lighting calc
v_position = gl_Position.xyz / gl_Position.w;
}

@ -1,9 +1,9 @@
#version 450
const vec2 positions[3] = vec2[3](
vec2(0.0, -0.5),
vec2(-0.5, 0.5),
vec2(0.5, 0.5)
vec2(0.0, 0.5),
vec2(-0.5, -0.5),
vec2(0.5, -0.5)
);
void main() {

Loading…
Cancel
Save