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

Make some more traits generic over the backend #151

Merged
merged 1 commit into from
Aug 4, 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 blade-graphics/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::mem;

use super::{ResourceIndex, ShaderBinding, VertexFormat};

pub trait HasShaderBinding: super::ShaderBindable {
pub trait HasShaderBinding {
const TYPE: ShaderBinding;
}
impl<T: bytemuck::Pod> HasShaderBinding for T {
Expand Down
9 changes: 9 additions & 0 deletions blade-graphics/src/gles/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ impl<T> Drop for super::PassEncoder<'_, T> {

#[hidden_trait::expose]
impl crate::traits::TransferEncoder for super::PassEncoder<'_, ()> {
type BufferPiece = crate::BufferPiece;
type TexturePiece = crate::TexturePiece;

fn fill_buffer(&mut self, dst: crate::BufferPiece, size: u64, value: u8) {
self.commands.push(super::Command::FillBuffer {
dst: dst.into(),
Expand Down Expand Up @@ -337,6 +340,10 @@ impl crate::traits::TransferEncoder for super::PassEncoder<'_, ()> {

#[hidden_trait::expose]
impl crate::traits::AccelerationStructureEncoder for super::PassEncoder<'_, ()> {
type AccelerationStructure = crate::AccelerationStructure;
type AccelerationStructureMesh = crate::AccelerationStructureMesh;
type BufferPiece = crate::BufferPiece;

fn build_bottom_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
Expand Down Expand Up @@ -379,6 +386,8 @@ impl crate::traits::ComputePipelineEncoder for super::PipelineEncoder<'_> {

#[hidden_trait::expose]
impl crate::traits::RenderPipelineEncoder for super::PipelineEncoder<'_> {
type BufferPiece = crate::BufferPiece;

fn set_scissor_rect(&mut self, rect: &crate::ScissorRect) {
self.commands.push(super::Command::SetScissor(rect.clone()));
}
Expand Down
2 changes: 1 addition & 1 deletion blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@

pub mod derive;
#[cfg_attr(
all(not(vulkan), not(gles), any(target_os = "ios", target_os = "macos")),

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `vulkan`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `gles`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `vulkan`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `gles`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `vulkan`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `gles`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `vulkan`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `gles`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `vulkan`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `gles`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `vulkan`

Check warning on line 52 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `gles`
path = "metal/mod.rs"
)]
#[cfg_attr(
all(
not(gles),

Check warning on line 57 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `gles`

Check warning on line 57 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `gles`

Check warning on line 57 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `gles`

Check warning on line 57 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `gles`

Check warning on line 57 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `gles`

Check warning on line 57 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `gles`
any(vulkan, windows, target_os = "linux", target_os = "android")

Check warning on line 58 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `vulkan`

Check warning on line 58 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `vulkan`

Check warning on line 58 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `vulkan`

Check warning on line 58 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `vulkan`

Check warning on line 58 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `vulkan`

Check warning on line 58 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `vulkan`
),
path = "vulkan/mod.rs"
)]
#[cfg_attr(any(gles, target_arch = "wasm32"), path = "gles/mod.rs")]

Check warning on line 62 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `gles`

Check warning on line 62 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `gles`

Check warning on line 62 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `gles`

Check warning on line 62 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `gles`

Check warning on line 62 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `gles`

Check warning on line 62 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `gles`
mod hal;
mod shader;
mod traits;
Expand Down Expand Up @@ -89,13 +89,13 @@
#[derive(Debug)]
pub enum NotSupportedError {
#[cfg(all(
not(gles),

Check warning on line 92 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `gles`

Check warning on line 92 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `gles`

Check warning on line 92 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `gles`

Check warning on line 92 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `gles`

Check warning on line 92 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `gles`

Check warning on line 92 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `gles`
any(vulkan, windows, target_os = "linux", target_os = "android")

Check warning on line 93 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `vulkan`

Check warning on line 93 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `vulkan`

Check warning on line 93 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `vulkan`

Check warning on line 93 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `vulkan`

Check warning on line 93 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `vulkan`

Check warning on line 93 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `vulkan`
))]
VulkanLoadingError(ash::LoadingError),
#[cfg(all(
not(gles),

Check warning on line 97 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `gles`

Check warning on line 97 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `gles`

Check warning on line 97 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `gles`

Check warning on line 97 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `gles`

Check warning on line 97 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `gles`

Check warning on line 97 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `gles`
any(vulkan, windows, target_os = "linux", target_os = "android")

Check warning on line 98 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `vulkan`

Check warning on line 98 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `vulkan`

Check warning on line 98 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `vulkan`

Check warning on line 98 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `vulkan`

Check warning on line 98 in blade-graphics/src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `vulkan`

Check warning on line 98 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `vulkan`
))]
VulkanError(ash::vk::Result),

Expand Down Expand Up @@ -567,7 +567,7 @@
Plain { size: u32 },
}

pub trait ShaderBindable: Clone + Copy {
pub trait ShaderBindable: Clone + Copy + derive::HasShaderBinding {
fn bind_to(&self, context: &mut PipelineContext, index: u32);
}

Expand Down
9 changes: 9 additions & 0 deletions blade-graphics/src/metal/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ impl super::CommandEncoder {

#[hidden_trait::expose]
impl crate::traits::TransferEncoder for super::TransferCommandEncoder<'_> {
type BufferPiece = crate::BufferPiece;
type TexturePiece = crate::TexturePiece;

fn fill_buffer(&mut self, dst: crate::BufferPiece, size: u64, value: u8) {
let range = metal::NSRange {
location: dst.offset,
Expand Down Expand Up @@ -315,6 +318,10 @@ impl Drop for super::TransferCommandEncoder<'_> {
impl crate::traits::AccelerationStructureEncoder
for super::AccelerationStructureCommandEncoder<'_>
{
type AccelerationStructure = crate::AccelerationStructure;
type AccelerationStructureMesh = crate::AccelerationStructureMesh;
type BufferPiece = crate::BufferPiece;

fn build_bottom_level(
&mut self,
acceleration_structure: super::AccelerationStructure,
Expand Down Expand Up @@ -515,6 +522,8 @@ impl crate::traits::PipelineEncoder for super::RenderPipelineContext<'_> {

#[hidden_trait::expose]
impl crate::traits::RenderPipelineEncoder for super::RenderPipelineContext<'_> {
type BufferPiece = crate::BufferPiece;

fn set_scissor_rect(&mut self, rect: &crate::ScissorRect) {
let scissor = metal::MTLScissorRect {
x: rect.x as _,
Expand Down
54 changes: 29 additions & 25 deletions blade-graphics/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,54 @@ pub trait CommandDevice {
}

pub trait TransferEncoder {
fn fill_buffer(&mut self, dst: super::BufferPiece, size: u64, value: u8);
fn copy_buffer_to_buffer(
&mut self,
src: super::BufferPiece,
dst: super::BufferPiece,
size: u64,
);
type BufferPiece: Send + Sync + Clone + Copy + Debug;
type TexturePiece: Send + Sync + Clone + Copy + Debug;

fn fill_buffer(&mut self, dst: Self::BufferPiece, size: u64, value: u8);
fn copy_buffer_to_buffer(&mut self, src: Self::BufferPiece, dst: Self::BufferPiece, size: u64);
fn copy_texture_to_texture(
&mut self,
src: super::TexturePiece,
dst: super::TexturePiece,
src: Self::TexturePiece,
dst: Self::TexturePiece,
size: super::Extent,
);

fn copy_buffer_to_texture(
&mut self,
src: super::BufferPiece,
src: Self::BufferPiece,
bytes_per_row: u32,
dst: super::TexturePiece,
dst: Self::TexturePiece,
size: super::Extent,
);

fn copy_texture_to_buffer(
&mut self,
src: super::TexturePiece,
dst: super::BufferPiece,
src: Self::TexturePiece,
dst: Self::BufferPiece,
bytes_per_row: u32,
size: super::Extent,
);
}

pub trait AccelerationStructureEncoder {
type AccelerationStructure: Send + Sync + Clone + Debug;
type AccelerationStructureMesh: Send + Sync + Clone + Debug;
type BufferPiece: Send + Sync + Clone + Copy + Debug;

fn build_bottom_level(
&mut self,
acceleration_structure: crate::AccelerationStructure,
meshes: &[super::AccelerationStructureMesh],
scratch_data: super::BufferPiece,
acceleration_structure: Self::AccelerationStructure,
meshes: &[Self::AccelerationStructureMesh],
scratch_data: Self::BufferPiece,
);

fn build_top_level(
&mut self,
acceleration_structure: crate::AccelerationStructure,
bottom_level: &[crate::AccelerationStructure],
acceleration_structure: Self::AccelerationStructure,
bottom_level: &[Self::AccelerationStructure],
instance_count: u32,
instance_data: super::BufferPiece,
scratch_data: super::BufferPiece,
instance_data: Self::BufferPiece,
scratch_data: Self::BufferPiece,
);
}

Expand All @@ -95,9 +97,11 @@ pub trait ComputePipelineEncoder: PipelineEncoder {
}

pub trait RenderPipelineEncoder: PipelineEncoder {
type BufferPiece: Send + Sync + Clone + Copy + Debug;

//TODO: reconsider exposing this here
fn set_scissor_rect(&mut self, rect: &super::ScissorRect);
fn bind_vertex(&mut self, index: u32, vertex_buf: super::BufferPiece);
fn bind_vertex(&mut self, index: u32, vertex_buf: Self::BufferPiece);
fn draw(
&mut self,
first_vertex: u32,
Expand All @@ -107,18 +111,18 @@ pub trait RenderPipelineEncoder: PipelineEncoder {
);
fn draw_indexed(
&mut self,
index_buf: super::BufferPiece,
index_buf: Self::BufferPiece,
index_type: super::IndexType,
index_count: u32,
base_vertex: i32,
start_instance: u32,
instance_count: u32,
);
fn draw_indirect(&mut self, indirect_buf: super::BufferPiece);
fn draw_indirect(&mut self, indirect_buf: Self::BufferPiece);
fn draw_indexed_indirect(
&mut self,
index_buf: crate::BufferPiece,
index_buf: Self::BufferPiece,
index_type: crate::IndexType,
indirect_buf: super::BufferPiece,
indirect_buf: Self::BufferPiece,
);
}
9 changes: 9 additions & 0 deletions blade-graphics/src/vulkan/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ impl super::CommandEncoder {

#[hidden_trait::expose]
impl crate::traits::TransferEncoder for super::TransferCommandEncoder<'_> {
type BufferPiece = crate::BufferPiece;
type TexturePiece = crate::TexturePiece;

fn fill_buffer(&mut self, dst: crate::BufferPiece, size: u64, value: u8) {
let value_u32 = (value as u32) * 0x1010101;
unsafe {
Expand Down Expand Up @@ -545,6 +548,10 @@ impl crate::traits::TransferEncoder for super::TransferCommandEncoder<'_> {
impl crate::traits::AccelerationStructureEncoder
for super::AccelerationStructureCommandEncoder<'_>
{
type AccelerationStructure = crate::AccelerationStructure;
type AccelerationStructureMesh = crate::AccelerationStructureMesh;
type BufferPiece = crate::BufferPiece;

fn build_bottom_level(
&mut self,
acceleration_structure: super::AccelerationStructure,
Expand Down Expand Up @@ -737,6 +744,8 @@ impl crate::traits::ComputePipelineEncoder for super::PipelineEncoder<'_, '_> {

#[hidden_trait::expose]
impl crate::traits::RenderPipelineEncoder for super::PipelineEncoder<'_, '_> {
type BufferPiece = crate::BufferPiece;

fn set_scissor_rect(&mut self, rect: &crate::ScissorRect) {
let vk_scissor = vk::Rect2D {
offset: vk::Offset2D {
Expand Down
Loading