Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPUI-related improvements #72

Merged
merged 5 commits into from
Feb 3, 2024
Merged
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ egui-gizmo = "0.12"
egui_plot = "0.23"
egui-winit = "0.23"
env_logger = "0.10"
del-msh = "0.1.22"
del-msh = "=0.1.24" # not following semver :(
glam = { workspace = true }
log = { workspace = true }
mint = { workspace = true, features = ["serde"] }
Expand Down
5 changes: 4 additions & 1 deletion blade-graphics/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl crate::traits::CommandDevice for Context {
}
}

fn destroy_command_encoder(&self, _command_encoder: CommandEncoder) {}
fn destroy_command_encoder(&self, _command_encoder: &mut CommandEncoder) {}

fn submit(&self, encoder: &mut CommandEncoder) -> SyncPoint {
{
Expand Down Expand Up @@ -451,12 +451,15 @@ struct FormatInfo {
fn describe_texture_format(format: crate::TextureFormat) -> FormatInfo {
use crate::TextureFormat as Tf;
let (internal, external, data_type) = match format {
Tf::R8Unorm => (glow::R8, glow::RED, glow::UNSIGNED_BYTE),
Tf::Rg8Unorm => (glow::RG8, glow::RG, glow::UNSIGNED_BYTE),
Tf::Rg8Snorm => (glow::RG8, glow::RG, glow::BYTE),
Tf::Rgba8Unorm => (glow::RGBA8, glow::RGBA, glow::UNSIGNED_BYTE),
Tf::Rgba8UnormSrgb => (glow::SRGB8_ALPHA8, glow::RGBA, glow::UNSIGNED_BYTE),
Tf::Bgra8Unorm => (glow::RGBA8, glow::BGRA, glow::UNSIGNED_BYTE),
Tf::Bgra8UnormSrgb => (glow::SRGB8_ALPHA8, glow::BGRA, glow::UNSIGNED_BYTE),
Tf::Rgba8Snorm => (glow::RGBA8, glow::RGBA, glow::BYTE),
Tf::R16Float => (glow::R16F, glow::RED, glow::FLOAT),
Tf::Rgba16Float => (glow::RGBA16F, glow::RGBA, glow::FLOAT),
Tf::R32Float => (glow::R32F, glow::RED, glow::FLOAT),
Tf::Rg32Float => (glow::RG32F, glow::RG, glow::FLOAT),
Expand Down
3 changes: 3 additions & 0 deletions blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,15 @@ impl From<Texture> for TexturePiece {
#[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub enum TextureFormat {
// color
R8Unorm,
Rg8Unorm,
Rg8Snorm,
Rgba8Unorm,
Rgba8UnormSrgb,
Bgra8Unorm,
Bgra8UnormSrgb,
Rgba8Snorm,
R16Float,
Rgba16Float,
R32Float,
Rg32Float,
Expand Down
5 changes: 4 additions & 1 deletion blade-graphics/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,15 @@ fn map_texture_format(format: crate::TextureFormat) -> metal::MTLPixelFormat {
use crate::TextureFormat as Tf;
use metal::MTLPixelFormat::*;
match format {
Tf::R8Unorm => R8Unorm,
Tf::Rg8Unorm => RG8Unorm,
Tf::Rg8Snorm => RG8Snorm,
Tf::Rgba8Unorm => RGBA8Unorm,
Tf::Rgba8UnormSrgb => RGBA8Unorm_sRGB,
Tf::Bgra8Unorm => BGRA8Unorm,
Tf::Bgra8UnormSrgb => BGRA8Unorm_sRGB,
Tf::Rgba8Snorm => RGBA8Snorm,
Tf::R16Float => R16Float,
Tf::Rgba16Float => RGBA16Float,
Tf::R32Float => R32Float,
Tf::Rg32Float => RG32Float,
Expand Down Expand Up @@ -419,7 +422,7 @@ impl crate::traits::CommandDevice for Context {
}
}

fn destroy_command_encoder(&self, _command_encoder: CommandEncoder) {}
fn destroy_command_encoder(&self, _command_encoder: &mut CommandEncoder) {}

fn submit(&self, encoder: &mut CommandEncoder) -> SyncPoint {
let cmd_buf = encoder.raw.take().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion blade-graphics/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub trait CommandDevice {
type SyncPoint: Clone + Debug;

fn create_command_encoder(&self, desc: super::CommandEncoderDesc) -> Self::CommandEncoder;
fn destroy_command_encoder(&self, encoder: Self::CommandEncoder);
fn destroy_command_encoder(&self, encoder: &mut Self::CommandEncoder);
fn submit(&self, encoder: &mut Self::CommandEncoder) -> Self::SyncPoint;
fn wait_for(&self, sp: &Self::SyncPoint, timeout_ms: u32) -> bool;
}
Expand Down
3 changes: 3 additions & 0 deletions blade-graphics/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ impl super::TextureFormat {
}
}
match *self {
Self::R8Unorm => uncompressed(1),
Self::Rg8Unorm => uncompressed(2),
Self::Rg8Snorm => uncompressed(2),
Self::Rgba8Unorm => uncompressed(4),
Self::Rgba8UnormSrgb => uncompressed(4),
Self::Bgra8Unorm => uncompressed(4),
Self::Bgra8UnormSrgb => uncompressed(4),
Self::Rgba8Snorm => uncompressed(4),
Self::R16Float => uncompressed(2),
Self::Rgba16Float => uncompressed(8),
Self::R32Float => uncompressed(4),
Self::Rg32Float => uncompressed(8),
Expand Down
34 changes: 33 additions & 1 deletion blade-graphics/src/vulkan/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ impl super::Context {
&entry,
&core_instance,
),
surface: if surface_handles.is_some() {
Some(khr::Surface::new(&entry, &core_instance))
} else {
None
},
core: core_instance,
};

Expand Down Expand Up @@ -536,7 +541,7 @@ impl super::Context {
last_progress,
}),
surface,
_physical_device: physical_device,
physical_device,
naga_flags,
instance,
_entry: entry,
Expand Down Expand Up @@ -587,7 +592,34 @@ impl super::Context {

impl super::Context {
pub fn resize(&self, config: crate::SurfaceConfig) -> crate::TextureFormat {
let surface_khr = self.instance.surface.as_ref().unwrap();
let mut surface = self.surface.as_ref().unwrap().lock().unwrap();

let capabilities = unsafe {
surface_khr
.get_physical_device_surface_capabilities(self.physical_device, surface.raw)
.unwrap()
};
if config.size.width < capabilities.min_image_extent.width
|| config.size.width > capabilities.max_image_extent.width
|| config.size.height < capabilities.min_image_extent.height
|| config.size.height > capabilities.max_image_extent.height
{
log::warn!(
"Requested size {}x{} is outside of surface capabilities",
config.size.width,
config.size.height
);
}
if config.frame_count < capabilities.min_image_count
|| config.frame_count > capabilities.max_image_count
{
log::warn!(
"Requested frame count {} is outside of surface capabilities",
config.frame_count
);
}

let queue_families = [self.queue_family_index];
let format = crate::TextureFormat::Bgra8UnormSrgb;
let vk_format = super::map_texture_format(format);
Expand Down
10 changes: 7 additions & 3 deletions blade-graphics/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Instance {
core: ash::Instance,
debug_utils: ext::DebugUtils,
get_physical_device_properties2: khr::GetPhysicalDeviceProperties2,
surface: Option<khr::Surface>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -101,7 +102,7 @@ pub struct Context {
queue_family_index: u32,
queue: Mutex<Queue>,
surface: Option<Mutex<Surface>>,
_physical_device: vk::PhysicalDevice,
physical_device: vk::PhysicalDevice,
naga_flags: naga::back::spv::WriterFlags,
instance: Instance,
_entry: ash::Entry,
Expand Down Expand Up @@ -380,7 +381,7 @@ impl crate::traits::CommandDevice for Context {
}
}

fn destroy_command_encoder(&self, command_encoder: CommandEncoder) {
fn destroy_command_encoder(&self, command_encoder: &mut CommandEncoder) {
for cmd_buf in command_encoder.buffers.iter() {
let raw_cmd_buffers = [cmd_buf.raw];
unsafe {
Expand All @@ -397,7 +398,7 @@ impl crate::traits::CommandDevice for Context {
.core
.destroy_command_pool(command_encoder.pool, None)
};
if let Some(crash_handler) = command_encoder.crash_handler {
if let Some(crash_handler) = command_encoder.crash_handler.take() {
self.destroy_buffer(crash_handler.marker_buf);
};
}
Expand Down Expand Up @@ -474,12 +475,15 @@ impl crate::traits::CommandDevice for Context {
fn map_texture_format(format: crate::TextureFormat) -> vk::Format {
use crate::TextureFormat as Tf;
match format {
Tf::R8Unorm => vk::Format::R8_UNORM,
Tf::Rg8Unorm => vk::Format::R8G8_UNORM,
Tf::Rg8Snorm => vk::Format::R8G8_SNORM,
Tf::Rgba8Unorm => vk::Format::R8G8B8A8_UNORM,
Tf::Rgba8UnormSrgb => vk::Format::R8G8B8A8_SRGB,
Tf::Bgra8Unorm => vk::Format::B8G8R8A8_UNORM,
Tf::Bgra8UnormSrgb => vk::Format::B8G8R8A8_SRGB,
Tf::Rgba8Snorm => vk::Format::R8G8B8A8_SNORM,
Tf::R16Float => vk::Format::R16_SFLOAT,
Tf::Rgba16Float => vk::Format::R16G16B16A16_SFLOAT,
Tf::R32Float => vk::Format::R32_SFLOAT,
Tf::Rg32Float => vk::Format::R32G32_SFLOAT,
Expand Down
13 changes: 6 additions & 7 deletions blade-render/src/util/frame_pacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct FramePacer {
frame_index: usize,
prev_resources: FrameResources,
prev_sync_point: Option<blade_graphics::SyncPoint>,
command_encoder: Option<blade_graphics::CommandEncoder>,
command_encoder: blade_graphics::CommandEncoder,
next_resources: FrameResources,
}

Expand All @@ -22,7 +22,7 @@ impl FramePacer {
frame_index: 0,
prev_resources: FrameResources::default(),
prev_sync_point: None,
command_encoder: Some(encoder),
command_encoder: encoder,
next_resources: FrameResources::default(),
}
}
Expand All @@ -46,17 +46,16 @@ impl FramePacer {

pub fn destroy(&mut self, context: &blade_graphics::Context) {
self.wait_for_previous_frame(context);
context.destroy_command_encoder(self.command_encoder.take().unwrap());
context.destroy_command_encoder(&mut self.command_encoder);
}

pub fn begin_frame(&mut self) -> (&mut blade_graphics::CommandEncoder, &mut FrameResources) {
let encoder = self.command_encoder.as_mut().unwrap();
encoder.start();
(encoder, &mut self.next_resources)
self.command_encoder.start();
(&mut self.command_encoder, &mut self.next_resources)
}

pub fn end_frame(&mut self, context: &blade_graphics::Context) -> &blade_graphics::SyncPoint {
let sync_point = context.submit(self.command_encoder.as_mut().unwrap());
let sync_point = context.submit(&mut self.command_encoder);
self.frame_index += 1;
// Wait for the previous frame immediately - this ensures that we are
// only processing one frame at a time, and yet not stalling.
Expand Down
17 changes: 8 additions & 9 deletions examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct Sprite {

struct Example {
pipeline: gpu::RenderPipeline,
command_encoder: Option<gpu::CommandEncoder>,
command_encoder: gpu::CommandEncoder,
prev_sync_point: Option<gpu::SyncPoint>,
texture: gpu::Texture,
view: gpu::TextureView,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Example {

Self {
pipeline,
command_encoder: Some(command_encoder),
command_encoder,
prev_sync_point: None,
texture,
view,
Expand Down Expand Up @@ -223,11 +223,10 @@ impl Example {
fn render(&mut self) {
let frame = self.context.acquire_frame();

let encoder = self.command_encoder.as_mut().unwrap();
encoder.start();
encoder.init_texture(frame.texture());
self.command_encoder.start();
self.command_encoder.init_texture(frame.texture());

if let mut pass = encoder.render(gpu::RenderTargetSet {
if let mut pass = self.command_encoder.render(gpu::RenderTargetSet {
colors: &[gpu::RenderTarget {
view: frame.texture_view(),
init_op: gpu::InitOp::Clear(gpu::TextureColor::TransparentBlack),
Expand Down Expand Up @@ -259,8 +258,8 @@ impl Example {
rc.draw(0, 4, 0, 1);
}
}
encoder.present(frame);
let sync_point = self.context.submit(encoder);
self.command_encoder.present(frame);
let sync_point = self.context.submit(&mut self.command_encoder);
if let Some(sp) = self.prev_sync_point.take() {
self.context.wait_for(&sp, !0);
}
Expand All @@ -273,7 +272,7 @@ impl Example {
}
self.context.destroy_texture(self.texture);
self.context
.destroy_command_encoder(self.command_encoder.take().unwrap());
.destroy_command_encoder(&mut self.command_encoder);
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/init/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(irrefutable_let_patterns)]

use std::{env, path::Path, ptr, sync::Arc};

Check warning on line 3 in examples/init/main.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused import: `ptr`

use blade_graphics as gpu;

Expand Down Expand Up @@ -223,7 +223,7 @@
let sync_point = context.submit(&mut command_encoder);

context.wait_for(&sync_point, !0);
context.destroy_command_encoder(command_encoder);
context.destroy_command_encoder(&mut command_encoder);
for buffer in temp_buffers {
context.destroy_buffer(buffer);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/mini/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn main() {
let answer = unsafe { *(result_buffer.data() as *mut u32) };
println!("Output: 0x{:x}", answer);

context.destroy_command_encoder(command_encoder);
context.destroy_command_encoder(&mut command_encoder);
context.destroy_buffer(result_buffer);
context.destroy_buffer(upload_buffer);
for view in views {
Expand Down
24 changes: 11 additions & 13 deletions examples/particle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use blade_graphics as gpu;
mod particle;

struct Example {
command_encoder: Option<gpu::CommandEncoder>,
command_encoder: gpu::CommandEncoder,
prev_sync_point: Option<gpu::SyncPoint>,
context: gpu::Context,
gui_painter: blade_egui::GuiPainter,
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Example {
let sync_point = context.submit(&mut command_encoder);

Self {
command_encoder: Some(command_encoder),
command_encoder,
prev_sync_point: Some(sync_point),
context,
gui_painter,
Expand All @@ -66,8 +66,8 @@ impl Example {
if let Some(sp) = self.prev_sync_point.take() {
self.context.wait_for(&sp, !0);
}
let encoder = self.command_encoder.take().unwrap();
self.context.destroy_command_encoder(encoder);
self.context
.destroy_command_encoder(&mut self.command_encoder);
self.gui_painter.destroy(&self.context);
self.particle_system.destroy(&self.context);
}
Expand All @@ -79,17 +79,15 @@ impl Example {
screen_desc: &blade_egui::ScreenDescriptor,
) {
let frame = self.context.acquire_frame();
let encoder = self.command_encoder.as_mut().unwrap();

encoder.start();
encoder.init_texture(frame.texture());
self.command_encoder.start();
self.command_encoder.init_texture(frame.texture());

self.gui_painter
.update_textures(encoder, gui_textures, &self.context);
.update_textures(&mut self.command_encoder, gui_textures, &self.context);

self.particle_system.update(encoder);
self.particle_system.update(&mut self.command_encoder);

if let mut pass = encoder.render(gpu::RenderTargetSet {
if let mut pass = self.command_encoder.render(gpu::RenderTargetSet {
colors: &[gpu::RenderTarget {
view: frame.texture_view(),
init_op: gpu::InitOp::Clear(gpu::TextureColor::TransparentBlack),
Expand All @@ -101,8 +99,8 @@ impl Example {
self.gui_painter
.paint(&mut pass, gui_primitives, screen_desc, &self.context);
}
encoder.present(frame);
let sync_point = self.context.submit(encoder);
self.command_encoder.present(frame);
let sync_point = self.context.submit(&mut self.command_encoder);
self.gui_painter.after_submit(&sync_point);

if let Some(sp) = self.prev_sync_point.take() {
Expand Down
Loading
Loading