Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<'a> Decoder<'a> {
b"IDAT" => Chunk::ImageData(self.read_slice(length)?),
b"IEND" => break,
b"gAMA" => Chunk::Gamma(self.read_u32()?),
b"sRGB" => todo!("Parse srgb chunks"),
// b"sRGB" => todo!("Parse srgb chunks"),
_foreign => {
// todo! how would ancillary chunks be parsed?
self.cursor += length;
Expand Down
62 changes: 62 additions & 0 deletions src/renderer/image_buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::renderer::TextureVertex;
use wgpu::util::{BufferInitDescriptor, DeviceExt};
use wgpu::{Buffer, BufferUsages, Device};

#[derive(Debug)]
pub(crate) struct ImageBuffer {
vertex_buffer: Buffer,
index_buffer: Buffer,
}

impl ImageBuffer {
const VERTICES: [TextureVertex; 4] = [
TextureVertex {
position: [-1.0, -1.0, 0.0],
tex_coords: [0.0, 1.0],
},
TextureVertex {
position: [-1.0, 1.0, 0.0],
tex_coords: [0.0, 0.0],
},
TextureVertex {
position: [0.6, -1.0, 0.0],
tex_coords: [1.0, 1.0],
},
TextureVertex {
position: [0.6, 1.0, 0.0],
tex_coords: [1.0, 0.0],
},
];

const INDICES: [u16; 6] = [
0, 1, 2, // first triangle
2, 1, 3, // second triangle
];

pub(crate) fn new(device: &Device) -> Self {
Self {
vertex_buffer: device.create_buffer_init(&BufferInitDescriptor {
label: Some("Image TextureVertex Buffer"),
contents: bytemuck::cast_slice(&Self::VERTICES),
usage: BufferUsages::VERTEX,
}),
index_buffer: device.create_buffer_init(&BufferInitDescriptor {
label: Some("Index Buffer"),
contents: bytemuck::cast_slice(&Self::INDICES),
usage: BufferUsages::INDEX,
}),
}
}

pub(crate) fn vertex_buffer(&self) -> &Buffer {
&self.vertex_buffer
}

pub(crate) fn index_buffer(&self) -> &Buffer {
&self.index_buffer
}

pub(crate) fn num_indices(&self) -> u32 {
Self::INDICES.len() as u32
}
}
3 changes: 3 additions & 0 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pub(crate) use texture::*;
pub(crate) use vertex::*;

mod feature_uniform;
mod image_buffer;
mod quad_uniform;
mod rectangle_buffer;
mod state;
mod texture;
mod vertex;
11 changes: 11 additions & 0 deletions src/renderer/quad_shader.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 450

layout(location=0) in vec3 a_position;
layout(location=1) in vec4 a_color;

layout(location=0) out vec4 v_color;

void main() {
v_color = a_color;
gl_Position = vec4(a_position, 1.0);
}
Binary file added src/renderer/quad_shader.vert.spv
Binary file not shown.
36 changes: 36 additions & 0 deletions src/renderer/quad_shader.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
struct QuadUniform {
over_edge: u32,
};


@group(0) @binding(0)
var<uniform> quad_uniform: QuadUniform;

struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) color: vec3<f32>,
};

struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) color: vec3<f32>,
};

@vertex
fn vs_main(
model: VertexInput,
) -> VertexOutput {
var out: VertexOutput;
out.color = model.color;
out.clip_position = vec4<f32>(model.position, 1.0);
return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
if quad_uniform.over_edge == 1u {
return vec4<f32>(0.5);
}

return vec4<f32>(in.color, 0.5);
}
21 changes: 21 additions & 0 deletions src/renderer/quad_uniform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct QuadUniform {
over_quad: u32,
}

impl QuadUniform {
pub(crate) const fn new() -> Self {
Self { over_quad: 0 }
}
}

impl QuadUniform {
pub(crate) const fn over_quad(&self) -> bool {
self.over_quad == 1
}

pub(crate) fn set_over_quad(&mut self, state: bool) {
self.over_quad = state as u32;
}
}
59 changes: 59 additions & 0 deletions src/renderer/rectangle_buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::renderer::Vertex;
use wgpu::util::{BufferInitDescriptor, DeviceExt};
use wgpu::{Buffer, BufferUsages, Device};

pub(crate) struct RectangleBuffer {
vertex_buffer: Buffer,
index_buffer: Buffer,
}

impl RectangleBuffer {
const WHITE: [f32; 3] = [0.0, 0.0, 0.0];

const VERTICES: [Vertex; 4] = [
Vertex {
position: [0.6, -1.0, 0.0],
color: Self::WHITE,
},
Vertex {
position: [1.0, -1.0, 0.0],
color: Self::WHITE,
},
Vertex {
position: [1.0, 1.0, 0.0],
color: Self::WHITE,
},
Vertex {
position: [0.6, 1.0, 0.0],
color: Self::WHITE,
},
];

const INDICES: [u16; 6] = [0, 1, 2, 2, 3, 0];

pub(crate) fn new(device: &Device) -> Self {
Self {
vertex_buffer: device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Rectangle Vertex Buffer"),
contents: bytemuck::cast_slice(&Self::VERTICES),
usage: BufferUsages::VERTEX,
}),
index_buffer: device.create_buffer_init(&BufferInitDescriptor {
label: Some("Rectangle Index Buffer"),
contents: bytemuck::cast_slice(&Self::INDICES),
usage: BufferUsages::INDEX,
}),
}
}

pub(crate) fn vertex_buffer(&self) -> &Buffer {
&self.vertex_buffer
}

pub(crate) fn index_buffer(&self) -> &Buffer {
&self.index_buffer
}
pub(crate) fn num_indices(&self) -> u32 {
Self::INDICES.len() as u32
}
}
Loading