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

indexing (not working currently) #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 13 additions & 7 deletions examples/window/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,26 @@ fn prepare_pipeline_state<'a>(device: &DeviceRef, library: &LibraryRef) -> Rende
pipeline_state_descriptor.set_vertex_function(Some(&vert));
pipeline_state_descriptor.set_fragment_function(Some(&frag));
pipeline_state_descriptor
.color_attachments()
.object_at(0)
.unwrap()
.color_attachments()[0]
// .object_at(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's clean up all these comments

// .unwrap()
.set_pixel_format(MTLPixelFormat::BGRA8Unorm);

device
.new_render_pipeline_state(&pipeline_state_descriptor)
.unwrap()
}

fn ppd(desc: &mut RenderPassDescriptorRef) {
// let color_attachment = &desc.color_attachments()[0];
desc.color_attachments()[0] = desc.color_attachments()[0].to_owned();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can live with the fact this doesn't work. It doesn't have to be provided - users can still do set_object_at, right?

}

fn prepare_render_pass_descriptor(descriptor: &RenderPassDescriptorRef, texture: &TextureRef) {
//descriptor.color_attachments().set_object_at(0, MTLRenderPassColorAttachmentDescriptor::alloc());
//let color_attachment: MTLRenderPassColorAttachmentDescriptor = unsafe { msg_send![descriptor.color_attachments().0, _descriptorAtIndex:0] };//descriptor.color_attachments().object_at(0);
let color_attachment = descriptor.color_attachments().object_at(0).unwrap();
let color_attachment = &descriptor.color_attachments()[0];


color_attachment.set_texture(Some(texture));
color_attachment.set_load_action(MTLLoadAction::Clear);
Expand Down Expand Up @@ -133,9 +139,9 @@ fn main() {
encoder.end_encoding();

render_pass_descriptor
.color_attachments()
.object_at(0)
.unwrap()
.color_attachments()[0]
// .object_at(0)
// .unwrap()
.set_load_action(MTLLoadAction::DontCare);

let encoder = command_buffer.new_render_command_encoder(&render_pass_descriptor);
Expand Down
35 changes: 27 additions & 8 deletions src/pipeline/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,28 @@ foreign_obj_type! {
pub struct AttributeDescriptorArrayRef;
}

impl AttributeDescriptorArrayRef {
pub fn object_at(&self, index: usize) -> Option<&AttributeDescriptorRef> {
// impl AttributeDescriptorArrayRef {
// pub fn object_at(&self, index: NSUInteger) -> Option<&AttributeDescriptorRef> {
// unsafe { msg_send![self, objectAtIndexedSubscript: index] }
// }

// pub fn set_object_at(&self, index: NSUInteger, buffer_desc: Option<&AttributeDescriptorRef>) {
// unsafe { msg_send![self, setObject:buffer_desc atIndexedSubscript:index] }
// }
// }


impl std::ops::Index<NSUInteger> for AttributeDescriptorArrayRef {
type Output = AttributeDescriptor;

fn index(&self, index: NSUInteger) -> &Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

pub fn set_object_at(&self, index: usize, buffer_desc: Option<&AttributeDescriptorRef>) {
unsafe { msg_send![self, setObject:buffer_desc atIndexedSubscript:index] }
impl std::ops::IndexMut<NSUInteger> for AttributeDescriptorArrayRef {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that this isn't actually needed. Today, metal-rs doesn't enforce thread guarantees (unfortunately!) so all the methods are &self. Therefore, there isn't a case where we'd need &mut self.

In the future, it will definitely be desired to make us properly Send/Sync/Clone/&mut, but this is out of scope of this PR.

fn index_mut(&mut self, index: NSUInteger) -> &mut Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

Expand Down Expand Up @@ -313,13 +328,17 @@ foreign_obj_type! {
pub struct BufferLayoutDescriptorArrayRef;
}

impl BufferLayoutDescriptorArrayRef {
pub fn object_at(&self, index: usize) -> Option<&BufferLayoutDescriptorRef> {
impl std::ops::Index<NSUInteger> for BufferLayoutDescriptorArrayRef {
type Output = BufferLayoutDescriptor;

fn index(&self, index: NSUInteger) -> &Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

pub fn set_object_at(&self, index: usize, buffer_desc: Option<&BufferLayoutDescriptorRef>) {
unsafe { msg_send![self, setObject:buffer_desc atIndexedSubscript:index] }
impl std::ops::IndexMut<NSUInteger> for BufferLayoutDescriptorArrayRef {
fn index_mut(&mut self, index: NSUInteger) -> &mut Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

Expand Down
22 changes: 18 additions & 4 deletions src/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,27 @@ foreign_obj_type! {
pub struct PipelineBufferDescriptorArrayRef;
}

impl PipelineBufferDescriptorArrayRef {
pub fn object_at(&self, index: NSUInteger) -> Option<&PipelineBufferDescriptorRef> {
// impl PipelineBufferDescriptorArrayRef {
// pub fn object_at(&self, index: NSUInteger) -> Option<&PipelineBufferDescriptorRef> {
// unsafe { msg_send![self, objectAtIndexedSubscript: index] }
// }

// pub fn set_object_at(&self, index: NSUInteger, buffer_desc: Option<&PipelineBufferDescriptorRef>) {
// unsafe { msg_send![self, setObject:buffer_desc atIndexedSubscript:index] }
// }
// }

impl std::ops::Index<NSUInteger> for PipelineBufferDescriptorArrayRef {
type Output = PipelineBufferDescriptor;

fn index(&self, index: NSUInteger) -> &Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

pub fn set_object_at(&self, index: NSUInteger, buffer_desc: Option<&PipelineBufferDescriptorRef>) {
unsafe { msg_send![self, setObject:buffer_desc atIndexedSubscript:index] }
impl std::ops::IndexMut<NSUInteger> for PipelineBufferDescriptorArrayRef {
fn index_mut(&mut self, index: NSUInteger) -> &mut Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

Expand Down
39 changes: 27 additions & 12 deletions src/pipeline/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,19 +403,34 @@ foreign_obj_type! {
pub struct RenderPipelineColorAttachmentDescriptorArrayRef;
}

impl RenderPipelineColorAttachmentDescriptorArrayRef {
pub fn object_at(&self, index: NSUInteger) -> Option<&RenderPipelineColorAttachmentDescriptorRef> {
// impl RenderPipelineColorAttachmentDescriptorArrayRef {
// pub fn object_at(&self, index: NSUInteger) -> Option<&RenderPipelineColorAttachmentDescriptorRef> {
// unsafe { msg_send![self, objectAtIndexedSubscript: index] }
// }

// pub fn set_object_at(
// &self,
// index: NSUInteger,
// attachment: Option<&RenderPipelineColorAttachmentDescriptorRef>,
// ) {
// unsafe {
// msg_send![self, setObject:attachment
// atIndexedSubscript:index]
// }
// }
// }


impl std::ops::Index<NSUInteger> for RenderPipelineColorAttachmentDescriptorArrayRef {
type Output = RenderPipelineColorAttachmentDescriptor;

fn index(&self, index: NSUInteger) -> &Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

pub fn set_object_at(
&self,
index: NSUInteger,
attachment: Option<&RenderPipelineColorAttachmentDescriptorRef>,
) {
unsafe {
msg_send![self, setObject:attachment
atIndexedSubscript:index]
}
impl std::ops::IndexMut<NSUInteger> for RenderPipelineColorAttachmentDescriptorArrayRef {
fn index_mut(&mut self, index: NSUInteger) -> &mut Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}
}
41 changes: 30 additions & 11 deletions src/renderpass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,39 @@ foreign_obj_type! {
pub struct RenderPassColorAttachmentDescriptorArrayRef;
}

impl RenderPassColorAttachmentDescriptorArrayRef {
pub fn object_at(&self, index: usize) -> Option<&RenderPassColorAttachmentDescriptorRef> {

// impl RenderPassColorAttachmentDescriptorArrayRef {
// // #[deprecated(
// // since = "0.17.3",
// // note = "Use Index trait methods instead"
// // )]
// pub fn object_at(&self, index: NSUInteger) -> Option<&RenderPassColorAttachmentDescriptorRef> {
// unsafe { msg_send![self, objectAtIndexedSubscript: index] }
// }

// pub fn set_object_at(
// &self,
// index: usize,
// attachment: Option<&RenderPassColorAttachmentDescriptorRef>,
// ) {
// unsafe {
// msg_send![self, setObject:attachment
// atIndexedSubscript:index]
// }
// }
// }

impl std::ops::Index<NSUInteger> for RenderPassColorAttachmentDescriptorArrayRef {
type Output = RenderPassColorAttachmentDescriptorRef;

fn index(&self, index: NSUInteger) -> &Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

pub fn set_object_at(
&self,
index: usize,
attachment: Option<&RenderPassColorAttachmentDescriptorRef>,
) {
unsafe {
msg_send![self, setObject:attachment
atIndexedSubscript:index]
}
impl std::ops::IndexMut<NSUInteger> for RenderPassColorAttachmentDescriptorArrayRef {
fn index_mut(&mut self, index: NSUInteger) -> &mut Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/vertexdescriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,17 @@ foreign_obj_type! {
pub struct VertexBufferLayoutDescriptorArrayRef;
}

impl VertexBufferLayoutDescriptorArrayRef {
pub fn object_at(&self, index: NSUInteger) -> Option<&VertexBufferLayoutDescriptorRef> {
impl std::ops::Index<NSUInteger> for VertexBufferLayoutDescriptorArrayRef {
type Output = VertexBufferLayoutDescriptor;

fn index(&self, index: NSUInteger) -> &Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

pub fn set_object_at(&self, index: NSUInteger, layout: Option<&VertexBufferLayoutDescriptorRef>) {
unsafe {
msg_send![self, setObject:layout
atIndexedSubscript:index]
}
impl std::ops::IndexMut<NSUInteger> for VertexBufferLayoutDescriptorArrayRef {
fn index_mut(&mut self, index: NSUInteger) -> &mut Self::Output {
unsafe { msg_send![self, objectAtIndexedSubscript: index] }
}
}

Expand Down