Skip to content

Commit

Permalink
Dummy support for RT in metal, add AS encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Feb 24, 2023
1 parent 22983b5 commit 7dda8a6
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 28 deletions.
3 changes: 1 addition & 2 deletions blade-graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ raw-window-handle = "0.5"
[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
block = "0.1"
core-graphics-types = "0.1"
foreign-types = "0.3"
metal = "0.24"
metal = { git = "https://github.com/kvark/metal-rs", branch = "rt" }
objc = "0.2.5"
naga = { workspace = true, features = ["msl-out"] }

Expand Down
34 changes: 33 additions & 1 deletion blade-graphics/src/gles/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ impl crate::ShaderBindable for crate::BufferPiece {
}
}
}
impl crate::ShaderBindable for super::AccelerationStructure {
fn bind_to(&self, ctx: &mut super::PipelineContext, index: u32) {
for _ in ctx.targets[index as usize].iter() {
unimplemented!()
}
}
}

impl super::CommandEncoder {
pub fn start(&mut self) {
Expand All @@ -84,6 +91,10 @@ impl super::CommandEncoder {
}
}

pub fn acceleration_structure(&mut self) -> super::PassEncoder<()> {
unimplemented!()
}

pub fn compute(&mut self) -> super::PassEncoder<super::ComputePipeline> {
super::PassEncoder {
commands: &mut self.commands,
Expand Down Expand Up @@ -215,7 +226,7 @@ impl<T> Drop for super::PassEncoder<'_, T> {
.push(super::Command::InvalidateAttachment(attachment));
}
match self.kind {
super::PassKind::Transfer => {}
super::PassKind::Transfer | super::PassKind::AccelerationStructure => {}
super::PassKind::Compute => {
self.commands.push(super::Command::ResetAllSamplers);
}
Expand Down Expand Up @@ -293,6 +304,27 @@ impl crate::traits::TransferEncoder for super::PassEncoder<'_, ()> {
}
}

impl super::PassEncoder<'_, ()> {
pub fn build_bottom_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_meshes: &[crate::AccelerationStructureMesh],
_scratch_data: crate::BufferPiece,
) {
unimplemented!()
}

pub fn build_top_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_instance_count: u32,
_instance_data: crate::BufferPiece,
_scratch_data: crate::BufferPiece,
) {
unimplemented!()
}
}

#[hidden_trait::expose]
impl crate::traits::PipelineEncoder for super::PipelineEncoder<'_> {
fn bind<D: crate::ShaderData>(&mut self, group: u32, data: &D) {
Expand Down
5 changes: 5 additions & 0 deletions blade-graphics/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ pub struct Sampler {
raw: glow::Sampler,
}

#[derive(Clone, Copy, Debug, Hash, PartialEq)]
pub struct AccelerationStructure {}

type SlotList = Vec<u32>;

struct BindGroupInfo {
Expand Down Expand Up @@ -312,6 +315,7 @@ pub struct CommandEncoder {

enum PassKind {
Transfer,
AccelerationStructure,
Compute,
Render,
}
Expand Down Expand Up @@ -445,6 +449,7 @@ fn describe_texture_format(format: crate::TextureFormat) -> FormatInfo {
Tf::Rgba8Unorm => (glow::RGBA8, glow::RGBA, glow::UNSIGNED_BYTE),
Tf::Rgba8UnormSrgb => (glow::SRGB8_ALPHA8, glow::RGBA, glow::UNSIGNED_BYTE),
Tf::Bgra8UnormSrgb => (glow::SRGB8_ALPHA8, glow::BGRA, glow::UNSIGNED_BYTE),
Tf::Rgba16Float => (glow::RGBA16F, glow::RGBA, glow::FLOAT),
Tf::Depth32Float => (glow::DEPTH_COMPONENT32F, glow::DEPTH_COMPONENT, glow::FLOAT),
};
FormatInfo {
Expand Down
3 changes: 3 additions & 0 deletions blade-graphics/src/gles/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ impl super::Context {
targets.push(params[0] as u32);
}
}
crate::ShaderBinding::AccelerationStructure => {
unimplemented!()
}
crate::ShaderBinding::Plain { size } => {
if let Some(index) = gl.get_uniform_block_index(program, glsl_name) {
let expected_size = gl.get_active_uniform_block_parameter_i32(
Expand Down
37 changes: 37 additions & 0 deletions blade-graphics/src/gles/resource.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
use glow::HasContext as _;
use std::{ptr, slice};

impl super::Context {
pub fn get_bottom_level_acceleration_structure_sizes(
&self,
_meshes: &[crate::AccelerationStructureMesh],
) -> crate::AccelerationStructureSizes {
unimplemented!()
}

pub fn get_top_level_acceleration_structure_sizes(
&self,
_instance_count: u32,
) -> crate::AccelerationStructureSizes {
unimplemented!()
}

pub fn create_acceleration_structure_instance_buffer(
&self,
_instances: &[crate::AccelerationStructureInstance],
) -> super::Buffer {
unimplemented!()
}

pub fn create_acceleration_structure(
&self,
_desc: crate::AccelerationStructureDesc,
) -> super::AccelerationStructure {
unimplemented!()
}

pub fn destroy_acceleration_structure(
&self,
_acceleration_structure: super::AccelerationStructure,
) {
unimplemented!()
}
}

#[hidden_trait::expose]
impl crate::traits::ResourceDevice for super::Context {
type Buffer = super::Buffer;
Expand Down
57 changes: 57 additions & 0 deletions blade-graphics/src/metal/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ impl crate::ShaderBindable for crate::BufferPiece {
}
}
}
impl crate::ShaderBindable for crate::AccelerationStructure {
fn bind_to(&self, ctx: &mut super::PipelineContext, index: u32) {
let slot = ctx.targets[index as usize] as _;
let value = Some(self.as_ref());
if let Some(encoder) = ctx.vs_encoder {
encoder.set_vertex_acceleration_structure(slot, value);
}
if let Some(encoder) = ctx.fs_encoder {
encoder.set_fragment_acceleration_structure(slot, value);
}
if let Some(encoder) = ctx.cs_encoder {
encoder.set_acceleration_structure(slot, value);
}
}
}

impl super::CommandEncoder {
pub fn start(&mut self) {
Expand Down Expand Up @@ -95,6 +110,20 @@ impl super::CommandEncoder {
}
}

pub fn acceleration_structure(&mut self) -> super::AccelerationStructureCommandEncoder {
let raw = objc::rc::autoreleasepool(|| {
self.raw
.as_mut()
.unwrap()
.new_acceleration_structure_command_encoder()
.to_owned()
});
super::AccelerationStructureCommandEncoder {
raw,
phantom: PhantomData,
}
}

pub fn compute(&mut self) -> super::ComputeCommandEncoder {
let raw = objc::rc::autoreleasepool(|| {
self.raw
Expand Down Expand Up @@ -272,6 +301,34 @@ impl Drop for super::TransferCommandEncoder<'_> {
}
}

impl<'a> super::AccelerationStructureCommandEncoder<'a> {
//TODO: move into the trait
pub fn build_bottom_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_meshes: &[crate::AccelerationStructureMesh],
_scratch_data: crate::BufferPiece,
) {
unimplemented!()
}

pub fn build_top_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_instance_count: u32,
_instance_data: crate::BufferPiece,
_scratch_data: crate::BufferPiece,
) {
unimplemented!()
}
}

impl Drop for super::AccelerationStructureCommandEncoder<'_> {
fn drop(&mut self) {
self.raw.end_encoding();
}
}

impl super::ComputeCommandEncoder<'_> {
pub fn with<'p>(
&'p mut self,
Expand Down
28 changes: 27 additions & 1 deletion blade-graphics/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
thread, time,
};

use foreign_types::ForeignTypeRef as _;
use metal::foreign_types::{ForeignType as _, ForeignTypeRef as _};

mod command;
mod pipeline;
Expand Down Expand Up @@ -124,6 +124,25 @@ impl Sampler {
}
}

#[derive(Clone, Copy, Debug, Hash, PartialEq)]
pub struct AccelerationStructure {
raw: *mut metal::MTLAccelerationStructure,
}

impl Default for AccelerationStructure {
fn default() -> Self {
Self {
raw: ptr::null_mut(),
}
}
}

impl AccelerationStructure {
fn as_ref(&self) -> &metal::AccelerationStructureRef {
unsafe { metal::AccelerationStructureRef::from_ptr(self.raw) }
}
}

#[derive(Clone, Debug)]
pub struct SyncPoint {
cmd_buf: metal::CommandBuffer,
Expand Down Expand Up @@ -191,6 +210,12 @@ pub struct TransferCommandEncoder<'a> {
phantom: PhantomData<&'a CommandEncoder>,
}

#[derive(Debug)]
pub struct AccelerationStructureCommandEncoder<'a> {
raw: metal::AccelerationStructureCommandEncoder,
phantom: PhantomData<&'a CommandEncoder>,
}

#[derive(Debug)]
pub struct ComputeCommandEncoder<'a> {
raw: metal::ComputeCommandEncoder,
Expand Down Expand Up @@ -232,6 +257,7 @@ fn map_texture_format(format: crate::TextureFormat) -> metal::MTLPixelFormat {
Tf::Rgba8Unorm => RGBA8Unorm,
Tf::Rgba8UnormSrgb => RGBA8Unorm_sRGB,
Tf::Bgra8UnormSrgb => BGRA8Unorm_sRGB,
Tf::Rgba16Float => RGBA16Float,
Tf::Depth32Float => Depth32Float,
}
}
Expand Down
5 changes: 5 additions & 0 deletions blade-graphics/src/metal/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl super::PipelineLayout {
let mut num_textures = 0u32;
let mut num_samplers = 0u32;
let mut num_buffers = 0u32;
let mut num_acceleration_structures = 0u32;
for layout in bind_group_layouts.iter() {
let mut targets = Vec::with_capacity(layout.bindings.len());
for &(_, ref binding) in layout.bindings.iter() {
Expand All @@ -143,6 +144,10 @@ impl super::PipelineLayout {
num_buffers += 1;
num_buffers - 1
}
crate::ShaderBinding::AccelerationStructure => {
num_acceleration_structures += 1;
num_acceleration_structures - 1
}
crate::ShaderBinding::Plain { .. } => {
num_buffers += 1;
num_buffers - 1
Expand Down
37 changes: 37 additions & 0 deletions blade-graphics/src/metal/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,43 @@ fn map_border_color(color: crate::TextureColor) -> metal::MTLSamplerBorderColor
}
}

impl super::Context {
pub fn get_bottom_level_acceleration_structure_sizes(
&self,
_meshes: &[crate::AccelerationStructureMesh],
) -> crate::AccelerationStructureSizes {
unimplemented!()
}

pub fn get_top_level_acceleration_structure_sizes(
&self,
_instance_count: u32,
) -> crate::AccelerationStructureSizes {
unimplemented!()
}

pub fn create_acceleration_structure_instance_buffer(
&self,
_instances: &[crate::AccelerationStructureInstance],
) -> super::Buffer {
unimplemented!()
}

pub fn create_acceleration_structure(
&self,
_desc: crate::AccelerationStructureDesc,
) -> super::AccelerationStructure {
unimplemented!()
}

pub fn destroy_acceleration_structure(
&self,
_acceleration_structure: super::AccelerationStructure,
) {
unimplemented!()
}
}

#[hidden_trait::expose]
impl crate::traits::ResourceDevice for super::Context {
type Buffer = super::Buffer;
Expand Down
Loading

0 comments on commit 7dda8a6

Please sign in to comment.