Skip to content

Commit

Permalink
feat: issue commands instantaneously instead of using a command decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
SkillerRaptor committed Sep 24, 2024
1 parent 6b2538e commit 5408c59
Show file tree
Hide file tree
Showing 35 changed files with 1,090 additions and 1,190 deletions.
2 changes: 1 addition & 1 deletion crates/hyper_engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ impl Engine {
self.renderer.render(&mut self.surface, &self.scene);
}

self.graphics_device.wait_idle();
self.graphics_device.wait_for_idle();
}
}
91 changes: 51 additions & 40 deletions crates/hyper_render/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use std::sync::Arc;
use hyper_math::Vec4;
use hyper_rhi::{
buffer::{Buffer, BufferDescriptor, BufferUsage},
commands::render_pass::RenderPassDescriptor,
command_list::CommandList,
graphics_device::GraphicsDevice,
graphics_pipeline::{GraphicsPipeline, GraphicsPipelineDescriptor},
pipeline_layout::{PipelineLayout, PipelineLayoutDescriptor},
render_pass::{DrawIndexedArguments, RenderPassDescriptor},
shader_module::{ShaderModuleDescriptor, ShaderStage},
surface::Surface,
};
Expand All @@ -28,42 +29,43 @@ pub struct Renderer {
positions: Arc<dyn Buffer>,
material: Arc<dyn Buffer>,

opaque_pipeline: Arc<dyn GraphicsPipeline>,
object_pipeline_layout: Arc<dyn PipelineLayout>,
pipeline: Arc<dyn GraphicsPipeline>,
pipeline_layout: Arc<dyn PipelineLayout>,

command_list: Arc<dyn CommandList>,

graphics_device: Arc<dyn GraphicsDevice>,
}

impl Renderer {
pub fn new(graphics_device: &Arc<dyn GraphicsDevice>) -> Self {
let object_pipeline_layout =
graphics_device.create_pipeline_layout(&PipelineLayoutDescriptor {
label: Some("Object Pipeline Layout"),
push_constants_size: size_of::<ObjectPushConstants>(),
});
let command_list = graphics_device.create_command_list();

let opaque_pipeline = {
let vertex_shader = graphics_device.create_shader_module(&ShaderModuleDescriptor {
label: Some(""),
path: "./assets/shaders/opaque_shader.hlsl",
entry_point: "vs_main",
stage: ShaderStage::Vertex,
});
let pipeline_layout = graphics_device.create_pipeline_layout(&PipelineLayoutDescriptor {
label: Some("Object Pipeline Layout"),
push_constants_size: size_of::<ObjectPushConstants>(),
});

let fragment_shader = graphics_device.create_shader_module(&ShaderModuleDescriptor {
label: Some(""),
path: "./assets/shaders/opaque_shader.hlsl",
entry_point: "fs_main",
stage: ShaderStage::Fragment,
});
let vertex_shader = graphics_device.create_shader_module(&ShaderModuleDescriptor {
label: Some("Opaque Vertex Shader"),
path: "./assets/shaders/opaque_shader.hlsl",
entry_point: "vs_main",
stage: ShaderStage::Vertex,
});

graphics_device.create_graphics_pipeline(&GraphicsPipelineDescriptor {
label: Some("Opaque Graphics Pipeline"),
layout: &object_pipeline_layout,
vertex_shader: &vertex_shader,
fragment_shader: &fragment_shader,
})
};
let fragment_shader = graphics_device.create_shader_module(&ShaderModuleDescriptor {
label: Some("Opaque Fragment Shader"),
path: "./assets/shaders/opaque_shader.hlsl",
entry_point: "fs_main",
stage: ShaderStage::Fragment,
});

let pipeline = graphics_device.create_graphics_pipeline(&GraphicsPipelineDescriptor {
label: Some("Opaque Graphics Pipeline"),
layout: &pipeline_layout,
vertex_shader: &vertex_shader,
fragment_shader: &fragment_shader,
});

let material = graphics_device.create_buffer(&BufferDescriptor {
label: Some("Material Buffer"),
Expand Down Expand Up @@ -115,8 +117,10 @@ impl Renderer {
positions,
material,

opaque_pipeline,
object_pipeline_layout,
pipeline,
pipeline_layout,

command_list,

graphics_device: Arc::clone(graphics_device),
}
Expand All @@ -128,28 +132,35 @@ impl Renderer {

let swapchain_texture = surface.current_texture();

let mut command_encoder = self.graphics_device.create_command_encoder();
self.command_list.begin();

{
let mut render_pass = command_encoder.begin_render_pass(&RenderPassDescriptor {
let render_pass = self.command_list.begin_render_pass(&RenderPassDescriptor {
label: Some("Main Render Pass"),
texture: &swapchain_texture,
color_attachment: &swapchain_texture,
depth_stencil_attachment: None,
});

render_pass.bind_pipeline(&self.opaque_pipeline);

render_pass.bind_index_buffer(&self.indices);
render_pass.push_constants(bytemuck::bytes_of(&ObjectPushConstants {
render_pass.set_pipeline(&self.pipeline);
render_pass.set_index_buffer(&self.indices);
render_pass.set_push_constants(bytemuck::bytes_of(&ObjectPushConstants {
mesh: self.mesh.handle(),
material: self.material.handle(),
..Default::default()
}));
render_pass.draw_indexed(6, 1, 0, 0, 0);

render_pass.draw_indexed(&DrawIndexedArguments {
index_count: 6,
instance_count: 1,
first_index: 0,
vertex_offset: 0,
first_instance: 0,
});
}

self.graphics_device.end_frame();
self.command_list.end();

self.graphics_device.submit(command_encoder.finish());
self.graphics_device.execute(&self.command_list);
self.graphics_device.present(surface);

self.frame_index += 1;
Expand Down
21 changes: 21 additions & 0 deletions crates/hyper_rhi/src/command_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright (c) 2024, SkillerRaptor
//
// SPDX-License-Identifier: MIT
//

use std::sync::Arc;

use downcast_rs::Downcast;

use crate::render_pass::{RenderPass, RenderPassDescriptor};

pub trait CommandList: Downcast {
fn begin(&self);

fn end(&self);

fn begin_render_pass(&self, descriptor: &RenderPassDescriptor) -> Arc<dyn RenderPass>;
}

downcast_rs::impl_downcast!(CommandList);
47 changes: 0 additions & 47 deletions crates/hyper_rhi/src/commands/command.rs

This file was deleted.

37 changes: 0 additions & 37 deletions crates/hyper_rhi/src/commands/command_decoder.rs

This file was deleted.

31 changes: 0 additions & 31 deletions crates/hyper_rhi/src/commands/command_encoder.rs

This file was deleted.

Loading

0 comments on commit 5408c59

Please sign in to comment.