Draw commands are now built by a GuiNode traversing its children

This commit is contained in:
Keavon Chambers 2020-05-23 22:44:35 -07:00
parent 6e0883e9b6
commit e72ab7b000
5 changed files with 16 additions and 43 deletions

View file

@ -18,10 +18,6 @@ layout(set=0, binding=0) uniform GuiNodeUniform {
};
layout(set=0, binding=1) uniform sampler2D t_texture;
// layout(set=1, binding=0) uniform WindowUniform {
// Dimensions_u32 window_dimensions;
// };
void main() {
f_color = fill_color * texture(t_texture, v_uv / textureSize(t_texture, 0) * 100);
}

View file

@ -68,12 +68,7 @@ impl Application {
// Temporary setup below, TODO: move to appropriate place in architecture
// Window uniform bind group layout
// let window_binding_types = vec![wgpu::BindingType::UniformBuffer { dynamic: false }];
// let window_bind_group_layout = Pipeline::build_bind_group_layout(&device, &window_binding_types);
// Data structure maintaining the user interface
// let extra_layouts = vec![&window_bind_group_layout];
let gui_rect_pipeline = Pipeline::new(&device, swap_chain_descriptor.format, vec![], &mut shader_cache, ("shaders/shader.vert", "shaders/shader.frag"));
pipeline_cache.set("gui_rect", gui_rect_pipeline);
@ -137,14 +132,8 @@ impl Application {
// Generates a render pass that commands are applied to, then generates a command buffer when finished
let mut command_encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("Render Encoder") });
// Build an array of draw commands
let mut nodes = vec![self.gui_root.borrow_mut()]; // TODO: Generate the DrawCommands as a list by recursively traversing the gui node tree
let mut commands = Vec::<DrawCommand>::with_capacity(nodes.len());
for i in 0..nodes.len() {
let pipeline_name = nodes[i].get_pipeline_name();
let pipeline = self.pipeline_cache.get(&pipeline_name[..]).unwrap();
commands.push(nodes[i].build_draw_command(&mut self.device, &mut self.queue, pipeline, &mut self.texture_cache));
}
// Build an array of draw commands by traversing the GUI element tree
let commands = GuiNode::build_draw_commands_recursive(&self.gui_root, &self.device, &mut self.queue, &self.pipeline_cache, &mut self.texture_cache);
// Recording of commands while in "rendering mode" that go into a command buffer
let mut render_pass = command_encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
@ -159,7 +148,7 @@ impl Application {
],
depth_stencil_attachment: None,
});
// Prepare a variable to reuse the pipeline based on its name
let mut pipeline_name = String::new();

View file

@ -7,21 +7,28 @@ use crate::gui_attributes::*;
pub struct GuiNode {
pub form_factor: GuiNodeUniform,
pub pipeline_name: String,
}
impl GuiNode {
pub fn new(width: u32, height: u32, color: Color) -> Self {
Self {
form_factor: GuiNodeUniform::new(width, height, color),
pipeline_name: String::from("gui_rect"),
}
}
// pub fn get_pipeline(&self, pipeline_cache: &ResourceCache<Pipeline>) -> &Pipeline {
// pipeline_cache.get("gui_rect").unwrap()
// }
pub fn build_draw_commands_recursive(node: &rctree::Node<GuiNode>, device: &wgpu::Device, queue: &mut wgpu::Queue, pipeline_cache: &ResourceCache<Pipeline>, texture_cache: &mut ResourceCache<Texture>) -> Vec<DrawCommand> {
let mut draw_commands: Vec<DrawCommand> = Vec::new();
pub fn get_pipeline_name(&self) -> String {
String::from("gui_rect")
for mut subnode in node.descendants() {
let mut subnode_data = subnode.borrow_mut();
let pipeline = pipeline_cache.get(&subnode_data.pipeline_name[..]).unwrap();
let command = subnode_data.build_draw_command(device, queue, pipeline, texture_cache);
draw_commands.push(command);
}
draw_commands
}
pub fn build_draw_command(&mut self, device: &wgpu::Device, queue: &mut wgpu::Queue, pipeline: &Pipeline, texture_cache: &mut ResourceCache<Texture>) -> DrawCommand {
@ -39,7 +46,7 @@ impl GuiNode {
let bind_groups = self.build_bind_groups(device, queue, pipeline, texture_cache);
// Create a draw command with the vertex data then push it to the GPU command queue
DrawCommand::new(device, self.get_pipeline_name(), bind_groups, VERTICES, INDICES)
DrawCommand::new(device, self.pipeline_name.clone(), bind_groups, VERTICES, INDICES)
}
pub fn build_bind_groups(&mut self, device: &wgpu::Device, queue: &mut wgpu::Queue, pipeline: &Pipeline, texture_cache: &mut ResourceCache<Texture>) -> Vec<wgpu::BindGroup> {

View file

@ -9,7 +9,6 @@ mod draw_command;
mod gui_node;
mod gui_attributes;
mod window_events;
mod window_uniform;
use application::Application;
use winit::event_loop::EventLoop;

View file

@ -1,18 +0,0 @@
use crate::gui_attributes::*;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct WindowUniform {
pub dimensions: Dimensions<u32>,
}
impl WindowUniform {
pub fn new(width: u32, height: u32) -> Self {
Self {
dimensions: Dimensions::new(width, height),
}
}
}
unsafe impl bytemuck::Zeroable for WindowUniform {}
unsafe impl bytemuck::Pod for WindowUniform {}