Skip to content

Commit

Permalink
Render both screens
Browse files Browse the repository at this point in the history
  • Loading branch information
DDRBoxman committed Oct 15, 2023
1 parent 6a3c5e6 commit 982d41f
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 30 deletions.
95 changes: 78 additions & 17 deletions cappy3ds_render/src/dsscreen.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use crate::primitive::Vertex;
use bytes::BytesMut;
use glam::Mat4;
use wgpu::{util::DeviceExt, Extent3d, TextureFormat};

const VERTICES: &[Vertex] = &[
Vertex {
position: [-1.0, -1.0, 0.0],
position: [0.0, 0.0, 0.0],
tex_coords: [0.0, 0.0],
},
Vertex {
position: [1.0, -1.0, 0.0],
position: [1.0, 0.0, 0.0],
tex_coords: [0.0, 1.0],
},
Vertex {
position: [-1.0, 1.0, 0.0],
position: [0.0, 1.0, 0.0],
tex_coords: [1.0, 0.0],
},
Vertex {
Expand All @@ -33,18 +34,25 @@ pub struct DSScreen {
texture_update: bool,
texture_size: Extent3d,
buffer: BytesMut,
pos_x: u32,
pos_y: u32,
transform_buffer: wgpu::Buffer,
}

impl DSScreen {
pub fn new(device: &wgpu::Device, texture_format: TextureFormat) -> Self {
pub fn new(
device: &wgpu::Device,
texture_format: TextureFormat,
width: u32,
height: u32,
placeholderImageBytes: &[u8],
) -> Self {
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex Buffer"),
contents: bytemuck::cast_slice(VERTICES),
usage: wgpu::BufferUsages::VERTEX,
});

let num_vertices = VERTICES.len() as u32;

let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Index Buffer"),
contents: bytemuck::cast_slice(INDICES),
Expand All @@ -54,8 +62,8 @@ impl DSScreen {
let num_indices = INDICES.len() as u32;

let texture_size = wgpu::Extent3d {
width: 240,
height: 400,
width,
height,
depth_or_array_layers: 1,
};
let diffuse_texture = device.create_texture(&wgpu::TextureDescriptor {
Expand All @@ -81,10 +89,6 @@ impl DSScreen {
view_formats: &[],
});

let diffuse_bytes = include_bytes!("../resources/test/upper_5.png");
let diffuse_image = image::load_from_memory(diffuse_bytes).unwrap();
let diffuse_rgba = diffuse_image.to_rgba8();

// use image::GenericImageView;
// let dimensions = diffuse_image.dimensions();

Expand All @@ -107,6 +111,16 @@ impl DSScreen {
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(64),
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
Expand All @@ -116,7 +130,7 @@ impl DSScreen {
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
// This should match the filterable field of the
// corresponding Texture entry above.
Expand All @@ -127,15 +141,28 @@ impl DSScreen {
label: Some("texture_bind_group_layout"),
});

let transform = glam::Mat4::default();

let mx_ref: &[f32; 16] = transform.as_ref();
let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(mx_ref),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});

let diffuse_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &texture_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&diffuse_texture_view),
resource: uniform_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&diffuse_texture_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&diffuse_sampler),
},
],
Expand Down Expand Up @@ -193,7 +220,7 @@ impl DSScreen {

// todo: hardcoded lol
let mut buffer = BytesMut::with_capacity(384000);
buffer.extend_from_slice(&diffuse_rgba);
buffer.extend_from_slice(&placeholderImageBytes);

DSScreen {
vertex_buffer,
Expand All @@ -205,6 +232,9 @@ impl DSScreen {
texture_update: true,
texture_size,
buffer,
pos_x: 0,
pos_y: 0,
transform_buffer: uniform_buf,
}
}

Expand All @@ -229,14 +259,26 @@ impl DSScreen {
// The layout of the texture
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(4 * 240),
rows_per_image: Some(400),
bytes_per_row: Some(4 * self.texture_size.width),
rows_per_image: Some(self.texture_size.height),
},
self.texture_size,
);
}
}

pub fn set_position(&mut self, queue: &wgpu::Queue, x: u32, y: u32) {
self.pos_x = x;
self.pos_y = y;
let transform = self.get_matrix();
let mx_ref: &[f32; 16] = transform.as_ref();
queue.write_buffer(&self.transform_buffer, 0, bytemuck::cast_slice(mx_ref));
}

fn get_matrix(&self) -> glam::Mat4 {
generate_matrix(self.texture_size.width, self.texture_size.height, self.pos_x, self.pos_y)
}

pub fn render(&self, encoder: &mut wgpu::CommandEncoder, render_target: &wgpu::TextureView) {
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
Expand All @@ -261,3 +303,22 @@ impl DSScreen {
}
}
}

fn generate_matrix(width: u32, height: u32, x: u32, y: u32) -> glam::Mat4 {
const SCENE_WIDTH: u32 = 1270;
const SCENE_HEIGHT: u32 = 720;

let mx_total = glam::Mat4::orthographic_rh(0.0, SCENE_WIDTH as f32, 0.0, SCENE_HEIGHT as f32, 0.0, 100.0);

// todo: fix the verticies so our quad isn't rotated or whatever, it is going to kill us later
let y = SCENE_HEIGHT - width - y;//y + height;//SCENE_HEIGHT - height - y;

let translate = glam::Mat4::from_translation(glam::Vec3 {
x: x as f32,
y: y as f32,
z: 0.0,
});
let scale = glam::Mat4::from_scale(glam::Vec3::new(height as f32, width as f32, 1.0));

mx_total * translate * scale
}
2 changes: 1 addition & 1 deletion cappy3ds_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn trash_code(v: &mut State) {
let mut cappy3ds = cappy3ds::Cappy3ds::new(
|audio: &[i16], upper_buffer: BytesMut, lower_buffer: BytesMut| {
if upper_buffer.len() >= 288000 {
v.write_texture(&upper_buffer);
v.write_texture(&upper_buffer, &lower_buffer);

v.render();
}
Expand Down
45 changes: 37 additions & 8 deletions cappy3ds_render/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ pub struct State {
// it gets dropped after it as the surface contains
// unsafe references to the window's resources.
//window: Window,
ds_screen: DSScreen,
ds_screen_upper: DSScreen,
ds_screen_lower: DSScreen,
}

impl State {
Expand Down Expand Up @@ -84,14 +85,38 @@ impl State {
};
surface.configure(&device, &config);

let ds_screen = DSScreen::new(&device, surface_format);
ds_screen.update_textures(&queue);
let diffuse_bytes = include_bytes!("../resources/test/upper_5.png");
let diffuse_image = image::load_from_memory(diffuse_bytes).unwrap();
let diffuse_rgba = diffuse_image.to_rgba8();
let mut ds_screen_upper = DSScreen::new(
&device,
surface_format,
240,
400,
diffuse_rgba.as_raw().as_slice(),
);
ds_screen_upper.update_textures(&queue);
ds_screen_upper.set_position(&queue, 0, 0);

let diffuse_bytes = include_bytes!("../resources/test/lower_wow.png");
let diffuse_image = image::load_from_memory(diffuse_bytes).unwrap();
let diffuse_rgba = diffuse_image.to_rgba8();
let mut ds_screen_lower = DSScreen::new(
&device,
surface_format,
240,
320,
diffuse_rgba.as_raw().as_slice(),
);
ds_screen_lower.update_textures(&queue);
ds_screen_lower.set_position(&queue, 40, 240);

Self {
surface,
device,
queue,
ds_screen,
ds_screen_upper,
ds_screen_lower,
}
}

Expand Down Expand Up @@ -128,7 +153,8 @@ impl State {
});
}

self.ds_screen.render(&mut encoder, &view);
self.ds_screen_upper.render(&mut encoder, &view);
self.ds_screen_lower.render(&mut encoder, &view);

// submit will accept anything that implements IntoIter
self.queue.submit(std::iter::once(encoder.finish()));
Expand All @@ -137,9 +163,12 @@ impl State {
Ok(())
}

pub fn write_texture(&mut self, buffer: &BytesMut) {
self.ds_screen.write_texture(buffer);
self.ds_screen.update_textures(&self.queue);
pub fn write_texture(&mut self, upper_buffer: &BytesMut, lower_buffer: &BytesMut) {
self.ds_screen_upper.write_texture(upper_buffer);
self.ds_screen_upper.update_textures(&self.queue);

self.ds_screen_lower.write_texture(lower_buffer);
self.ds_screen_lower.update_textures(&self.queue);
}
}

Expand Down
12 changes: 8 additions & 4 deletions cappy3ds_render/src/shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ struct VertexInput {
}

struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@builtin(position) position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
}

@group(0)
@binding(0)
var<uniform> transform: mat4x4<f32>;

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

@group(0) @binding(0)
@group(0) @binding(1)
var t_diffuse: texture_2d<f32>;
@group(0)@binding(1)
@group(0)@binding(2)
var s_diffuse: sampler;

@fragment
Expand Down

0 comments on commit 982d41f

Please sign in to comment.