Struct CAMetalDrawable

Source
pub struct CAMetalDrawable(/* private fields */);

Implementations§

Source§

impl CAMetalDrawable

Source

pub unsafe fn texture(&self) -> *mut MTLTexture

Examples found in repository?
examples/hello_triangle.rs (line 87)
19fn main() {
20  let width = 1280;
21  let height = 720;
22
23  let app = unsafe {
24    let app = NSApplication::shared_application().as_ref().unwrap();
25    app.set_activation_policy(NSApplicationActivationPolicy::Regular);
26    setup_top_menu(app);
27    app.finish_launching();
28
29    #[cfg(debug_assertions)]
30    app.activate_ignoring_other_apps(true);
31
32    app
33  };
34
35  let window = unsafe { create_window(width, height) };
36
37  let device = unsafe { util::mtl_create_system_default_device().as_mut().unwrap() };
38
39  let layer = unsafe {
40    let layer = core_animation::CAMetalLayer::new();
41    layer.set_device(&mut *device);
42    layer.set_pixel_format(MTLPixelFormat::BGRA8Unorm);
43    layer.set_framebuffer_only(false);
44    layer
45  };
46
47  unsafe {
48    let view = window.content_view().as_ref().unwrap();
49    view.set_wants_layer(true);
50    view.set_layer(layer.as_mut_ptr() as *mut core_animation::CALayer);
51  }
52
53  let mut pipeline_state = unsafe {
54    let shader_source = include_str!("hello_triangle.metal");
55    let shader_library = compile_shader_lib(device, shader_source);
56    create_pipeline_state(device, &shader_library)
57  };
58
59  let command_queue = unsafe { device.new_command_queue() };
60
61  let mut position_buffer = unsafe {
62    let positions: [(f32, f32); 3] = [
63      (0.0, 0.75),
64      (0.75, -0.75),
65      (-0.75, -0.75),
66    ];
67
68    device.new_buffer_with_bytes_length_options(
69      positions.as_ptr() as *const _,
70      mem::size_of_val(&positions),
71      MTLResourceOptions::CPU_CACHE_MODE_WRITE_COMBINED | MTLResourceOptions::STORAGE_MODE_MANAGED
72    )
73  };
74
75  let mut termination_requested = false;
76  let mut frame_count = 0;
77
78  while !termination_requested {
79    unsafe {
80      let _context = AutoReleaseContext::new();
81
82      poll_events(&app, &mut termination_requested);
83
84      let cmd_buffer = command_queue.command_buffer().as_mut().unwrap();
85
86      let drawable = layer.next_drawable();
87      let output_texture = (&*drawable).texture().as_mut().unwrap();
88
89      let mut render_pass_descriptor = create_render_pass_descriptor(output_texture);
90
91      let encoder = cmd_buffer.render_command_encoder_with_descriptor(&mut *render_pass_descriptor).as_ref().unwrap();
92      encoder.set_render_pipeline_state(&mut *pipeline_state);
93
94      encoder.set_vertex_buffer_offset_at_index(&mut *position_buffer, 0, 0);
95
96      let displacement: (f32, f32) = {
97        let radius = 0.1;
98        let speed = 0.05;
99
100        (
101          radius * f32::cos(frame_count as f32 * speed),
102          radius * f32::sin(frame_count as f32 * speed)
103        )
104      };
105      encoder.set_vertex_bytes_offset_at_index(
106        &displacement as *const (f32, f32) as *const _,
107        mem::size_of_val(&displacement),
108        1
109      );
110
111      encoder.draw_primitives_vertex_start_vertex_count(
112        MTLPrimitiveType::Triangle,
113        0,
114        3
115      );
116
117      encoder.end_encoding();
118
119      frame_count += 1;
120
121      cmd_buffer.present_drawable(drawable as *mut MTLDrawable);
122      cmd_buffer.commit();
123    }
124  }
125
126  println!("Terminated gracefully.");
127}

Trait Implementations§

Source§

impl Message for CAMetalDrawable

Source§

unsafe fn send_message<A, R>( &self, sel: Sel, args: A, ) -> Result<R, MessageError>
where Self: Sized, A: MessageArguments, R: Any,

Sends a message to self with the given selector and arguments. Read more
Source§

fn verify_message<A, R>(&self, sel: Sel) -> Result<(), MessageError>
where Self: Sized, A: EncodeArguments, R: Encode,

Verifies that the argument and return types match the encoding of the method for the given selector. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.