Skip to content

Commit

Permalink
Make some more traits generic over the backend (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki authored Aug 4, 2024
1 parent ace5606 commit 9bf7ca3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 27 deletions.
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 @@ -585,7 +585,7 @@ pub enum ShaderBinding {
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

0 comments on commit 9bf7ca3

Please sign in to comment.