mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 23:04:49 +00:00
forgot to submit cmd_queue
This commit is contained in:
parent
8ca09dae73
commit
ea3f15f950
2 changed files with 78 additions and 58 deletions
|
@ -39,10 +39,6 @@ impl QuadBufferBuilder {
|
||||||
max_y: f32,
|
max_y: f32,
|
||||||
color: [f32; 3],
|
color: [f32; 3],
|
||||||
) -> Self {
|
) -> Self {
|
||||||
// println!("min_x {:?}", min_x);
|
|
||||||
// println!("min_y {:?}", min_y);
|
|
||||||
// println!("max_x {:?}", max_x);
|
|
||||||
// println!("max_y {:?}", max_y);
|
|
||||||
self.vertex_data.extend(&[
|
self.vertex_data.extend(&[
|
||||||
Vertex {
|
Vertex {
|
||||||
position: (min_x, min_y).into(),
|
position: (min_x, min_y).into(),
|
||||||
|
@ -99,6 +95,12 @@ pub fn create_rect_buffers(
|
||||||
height: 300.0,
|
height: 300.0,
|
||||||
color: [1.0, 0.0, 0.0],
|
color: [1.0, 0.0, 0.0],
|
||||||
};
|
};
|
||||||
|
let test_rect_2 = Rect {
|
||||||
|
top_left_coords: (400.0, 300.0).into(),
|
||||||
|
width: 400.0,
|
||||||
|
height: 300.0,
|
||||||
|
color: [0.0, 0.0, 1.0],
|
||||||
|
};
|
||||||
|
|
||||||
let vertex_buffer = gpu_device.create_buffer(&wgpu::BufferDescriptor {
|
let vertex_buffer = gpu_device.create_buffer(&wgpu::BufferDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
|
@ -119,6 +121,7 @@ pub fn create_rect_buffers(
|
||||||
let num_rects = {
|
let num_rects = {
|
||||||
let (stg_vertex, stg_index, num_indices) = QuadBufferBuilder::new()
|
let (stg_vertex, stg_index, num_indices) = QuadBufferBuilder::new()
|
||||||
.push_rect(&test_rect_1)
|
.push_rect(&test_rect_1)
|
||||||
|
.push_rect(&test_rect_2)
|
||||||
.build(&gpu_device);
|
.build(&gpu_device);
|
||||||
|
|
||||||
stg_vertex.copy_to_buffer(encoder, &vertex_buffer);
|
stg_vertex.copy_to_buffer(encoder, &vertex_buffer);
|
||||||
|
|
|
@ -20,15 +20,15 @@
|
||||||
use crate::buffer::create_rect_buffers;
|
use crate::buffer::create_rect_buffers;
|
||||||
use crate::text::{build_glyph_brush, Text};
|
use crate::text::{build_glyph_brush, Text};
|
||||||
use crate::vertex::Vertex;
|
use crate::vertex::Vertex;
|
||||||
|
use cgmath::Ortho;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use wgpu::util::DeviceExt;
|
||||||
|
use wgpu::{BindGroup, BindGroupLayoutDescriptor, BindGroupLayoutEntry, Buffer, ShaderStage};
|
||||||
use winit::event;
|
use winit::event;
|
||||||
use winit::event::{Event, ModifiersState};
|
use winit::event::{Event, ModifiersState};
|
||||||
use winit::event_loop::ControlFlow;
|
use winit::event_loop::ControlFlow;
|
||||||
use wgpu::{BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindGroup, ShaderStage};
|
|
||||||
use wgpu::util::DeviceExt;
|
|
||||||
use cgmath::{Ortho};
|
|
||||||
|
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
pub mod bucket;
|
pub mod bucket;
|
||||||
|
@ -55,30 +55,26 @@ pub fn launch(_filepaths: &[&Path]) -> io::Result<()> {
|
||||||
struct Uniforms {
|
struct Uniforms {
|
||||||
// We can't use cgmath with bytemuck directly so we'll have
|
// We can't use cgmath with bytemuck directly so we'll have
|
||||||
// to convert the Matrix4 into a 4x4 f32 array
|
// to convert the Matrix4 into a 4x4 f32 array
|
||||||
ortho: [[f32; 4]; 4]
|
ortho: [[f32; 4]; 4],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Uniforms {
|
impl Uniforms {
|
||||||
fn new(w: u32, h: u32) -> Self {
|
fn new(w: u32, h: u32) -> Self {
|
||||||
let ortho: cgmath::Matrix4<f32> =
|
let ortho: cgmath::Matrix4<f32> = Ortho::<f32> {
|
||||||
Ortho::<f32> {
|
left: 0.0,
|
||||||
left: 0.0,
|
right: w as f32,
|
||||||
right: w as f32,
|
bottom: h as f32,
|
||||||
bottom: h as f32,
|
top: 0.0,
|
||||||
top: 0.0,
|
near: -1.0,
|
||||||
near: -1.0,
|
far: 1.0,
|
||||||
far: 1.0,
|
}
|
||||||
}
|
.into();
|
||||||
.into();
|
|
||||||
println!("{:?}", ortho);
|
|
||||||
Self {
|
Self {
|
||||||
ortho: ortho.into(),
|
ortho: ortho.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
|
@ -124,7 +120,6 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
// Prepare swap chain
|
// Prepare swap chain
|
||||||
let render_format = wgpu::TextureFormat::Bgra8UnormSrgb;
|
let render_format = wgpu::TextureFormat::Bgra8UnormSrgb;
|
||||||
let mut size = window.inner_size();
|
let mut size = window.inner_size();
|
||||||
println!("size: {:?}", size);
|
|
||||||
|
|
||||||
let swap_chain_descr = wgpu::SwapChainDescriptor {
|
let swap_chain_descr = wgpu::SwapChainDescriptor {
|
||||||
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
||||||
|
@ -137,7 +132,8 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
let mut swap_chain = gpu_device.create_swap_chain(&surface, &swap_chain_descr);
|
let mut swap_chain = gpu_device.create_swap_chain(&surface, &swap_chain_descr);
|
||||||
|
|
||||||
let (rect_pipeline, ortho_bind_group) = make_rect_pipeline(&gpu_device, &swap_chain_descr);
|
let (rect_pipeline, ortho_bind_group, ortho_buffer) =
|
||||||
|
make_rect_pipeline(&gpu_device, &swap_chain_descr);
|
||||||
|
|
||||||
let mut glyph_brush = build_glyph_brush(&gpu_device, render_format)?;
|
let mut glyph_brush = build_glyph_brush(&gpu_device, render_format)?;
|
||||||
|
|
||||||
|
@ -158,10 +154,12 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
|
//Close
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event: event::WindowEvent::CloseRequested,
|
event: event::WindowEvent::CloseRequested,
|
||||||
..
|
..
|
||||||
} => *control_flow = winit::event_loop::ControlFlow::Exit,
|
} => *control_flow = winit::event_loop::ControlFlow::Exit,
|
||||||
|
//Resize
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event: event::WindowEvent::Resized(new_size),
|
event: event::WindowEvent::Resized(new_size),
|
||||||
..
|
..
|
||||||
|
@ -179,13 +177,42 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
present_mode: wgpu::PresentMode::Immediate,
|
present_mode: wgpu::PresentMode::Immediate,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
//update orthographic buffer according to new window size
|
||||||
|
let new_uniforms = Uniforms::new(size.width, size.height);
|
||||||
|
|
||||||
|
let new_ortho_buffer =
|
||||||
|
gpu_device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some("Ortho Uniform Buffer"),
|
||||||
|
contents: bytemuck::cast_slice(&[new_uniforms]),
|
||||||
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_SRC,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get a command encoder for the current frame
|
||||||
|
let mut encoder =
|
||||||
|
gpu_device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||||
|
label: Some("Resize"),
|
||||||
|
});
|
||||||
|
|
||||||
|
encoder.copy_buffer_to_buffer(
|
||||||
|
&new_ortho_buffer,
|
||||||
|
0,
|
||||||
|
&ortho_buffer,
|
||||||
|
0,
|
||||||
|
(std::mem::size_of::<Uniforms>() * vec![new_uniforms].as_slice().len())
|
||||||
|
as wgpu::BufferAddress,
|
||||||
|
);
|
||||||
|
|
||||||
|
cmd_queue.submit(Some(encoder.finish()));
|
||||||
}
|
}
|
||||||
|
//Received Character
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event: event::WindowEvent::ReceivedCharacter(ch),
|
event: event::WindowEvent::ReceivedCharacter(ch),
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
update_text_state(&mut text_state, &ch);
|
update_text_state(&mut text_state, &ch);
|
||||||
}
|
}
|
||||||
|
//Keyboard Input
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event: event::WindowEvent::KeyboardInput { input, .. },
|
event: event::WindowEvent::KeyboardInput { input, .. },
|
||||||
..
|
..
|
||||||
|
@ -198,6 +225,7 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Modifiers Changed
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event: event::WindowEvent::ModifiersChanged(modifiers),
|
event: event::WindowEvent::ModifiersChanged(modifiers),
|
||||||
..
|
..
|
||||||
|
@ -270,50 +298,39 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
fn make_rect_pipeline(
|
fn make_rect_pipeline(
|
||||||
gpu_device: &wgpu::Device,
|
gpu_device: &wgpu::Device,
|
||||||
swap_chain_descr: &wgpu::SwapChainDescriptor,
|
swap_chain_descr: &wgpu::SwapChainDescriptor,
|
||||||
) -> (wgpu::RenderPipeline, BindGroup) {
|
) -> (wgpu::RenderPipeline, BindGroup, Buffer) {
|
||||||
let uniforms = Uniforms::new(swap_chain_descr.width, swap_chain_descr.height);
|
let uniforms = Uniforms::new(swap_chain_descr.width, swap_chain_descr.height);
|
||||||
|
|
||||||
let ortho_buffer = gpu_device.create_buffer_init(
|
let ortho_buffer = gpu_device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
&wgpu::util::BufferInitDescriptor {
|
label: Some("Ortho Uniform Buffer"),
|
||||||
label: Some("Ortho Uniform Buffer"),
|
contents: bytemuck::cast_slice(&[uniforms]),
|
||||||
contents: bytemuck::cast_slice(&[uniforms]),
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||||
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
let ortho_bind_group_layout =
|
let ortho_bind_group_layout = gpu_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
|
||||||
gpu_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
|
entries: &[BindGroupLayoutEntry {
|
||||||
entries: &[
|
binding: 0,
|
||||||
BindGroupLayoutEntry {
|
visibility: ShaderStage::VERTEX,
|
||||||
binding: 0,
|
ty: wgpu::BindingType::UniformBuffer {
|
||||||
visibility: ShaderStage::VERTEX,
|
dynamic: false,
|
||||||
ty: wgpu::BindingType::UniformBuffer {
|
min_binding_size: None,
|
||||||
dynamic: false,
|
},
|
||||||
min_binding_size: None,
|
count: None,
|
||||||
},
|
}],
|
||||||
count: None,
|
label: None,
|
||||||
}
|
});
|
||||||
],
|
|
||||||
label: None
|
|
||||||
});
|
|
||||||
|
|
||||||
let ortho_bind_group = gpu_device.create_bind_group(&wgpu::BindGroupDescriptor {
|
let ortho_bind_group = gpu_device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
layout: &ortho_bind_group_layout,
|
layout: &ortho_bind_group_layout,
|
||||||
entries: &[
|
entries: &[wgpu::BindGroupEntry {
|
||||||
wgpu::BindGroupEntry {
|
binding: 0,
|
||||||
binding: 0,
|
resource: wgpu::BindingResource::Buffer(ortho_buffer.slice(..)),
|
||||||
resource: wgpu::BindingResource::Buffer(ortho_buffer.slice(..))
|
}],
|
||||||
}
|
|
||||||
],
|
|
||||||
label: Some("ortho_bind_group"),
|
label: Some("ortho_bind_group"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let pipeline_layout = gpu_device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
let pipeline_layout = gpu_device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
bind_group_layouts: &[
|
bind_group_layouts: &[&ortho_bind_group_layout],
|
||||||
&ortho_bind_group_layout
|
|
||||||
],
|
|
||||||
push_constant_ranges: &[],
|
push_constant_ranges: &[],
|
||||||
label: Some("Rectangle Pipeline Layout"),
|
label: Some("Rectangle Pipeline Layout"),
|
||||||
});
|
});
|
||||||
|
@ -326,7 +343,7 @@ fn make_rect_pipeline(
|
||||||
wgpu::include_spirv!("shaders/rect.frag.spv"),
|
wgpu::include_spirv!("shaders/rect.frag.spv"),
|
||||||
);
|
);
|
||||||
|
|
||||||
(pipeline, ortho_bind_group)
|
(pipeline, ortho_bind_group, ortho_buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_render_pipeline(
|
fn create_render_pipeline(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue