Skip to content

Commit

Permalink
boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonsdaleiter committed May 11, 2020
1 parent 09ccd42 commit d930309
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
80 changes: 67 additions & 13 deletions examples/quad/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
use iron_oxide::{
Array, MTLCPUCacheMode, MTLCompileOptions, MTLCopyAllDevices, MTLPixelFormat,
MTLRenderPipelineColorAttachmentDescriptor, MTLRenderPipelineDescriptor, MTLResourceOptions,
MTLStorageMode, MTLVertexDescriptor,
};
use iron_oxide::*;
use std::os::raw::c_void;
use winit::event_loop::{EventLoop, ControlFlow};
use winit::window::{WindowBuilder, Window};
use winit::dpi::PhysicalSize;
use std::time::{Duration, Instant};
use winit::event::{Event, StartCause, WindowEvent};

fn main() {
unsafe {
struct MetalBoilerplate {
//
}

impl MetalBoilerplate {
unsafe fn new(window: &Window) -> MetalBoilerplate {
let devices = MTLCopyAllDevices();
let device = devices.into_iter().find_map(|d| Some(d)).unwrap();

let layer = CAMetalLayer::new();
layer.set_device(&device);
layer.set_vsync(true);
window.set_layer(&layer);

let queue = device.new_command_queue();

let library = device
Expand All @@ -34,19 +44,63 @@ fn main() {
})
.unwrap();

let quad_bytes = [ // TODO add the other triangle
const QUAD_BYTES: [f32; 21] = [ // TODO add the other triangle
-1.0f32, -1.0, 0.0, // v1
0.0, 0.0, 0.0, 1.0, // black
1.0, -1.0, 0.0, // v2
1.0, 0.0, 0.0, 1.0, // red
1.0, 1.0, 0.0, // v3
0.0, 1.0, 0.0, 1.0, // green
];
let quad = device.new_buffer_with_bytes(
quad_bytes.as_ptr() as *const c_void,
quad_bytes.len() as u64 * 4,
const SIZE: NSUInteger = QUAD_BYTES.len() as NSUInteger * 4;
let quad_buffer = device.new_buffer_with_bytes(
QUAD_BYTES.as_ptr() as *const c_void,
SIZE,
MTLResourceOptions::new()
.set_cpu_cache_mode(MTLCPUCacheMode::Default)
.set_storage_mode(MTLStorageMode::Private),
.set_cpu_cache_mode(MTLCPUCacheMode::WriteCombined)
.set_storage_mode(MTLStorageMode::Managed),
);
quad_buffer.did_modify_range(0..SIZE);

MetalBoilerplate {}
}
}

fn main() {
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_inner_size(PhysicalSize::new(1280, 720))
.with_title("Quad")
.build(&event_loop)
.unwrap();

let boilerplate = unsafe { MetalBoilerplate::new(&window) };

let duration = Duration::from_millis(17);
let mut now = Instant::now();
event_loop.run(move |event, _, control_flow|{
*control_flow = ControlFlow::WaitUntil(now + duration);

match event {
Event::NewEvents(cause) => {
match cause {
StartCause::ResumeTimeReached { start: _, requested_resume: _ } => {
now = Instant::now();
},
_ => {},
}
},
Event::RedrawRequested(_) => {},
Event::WindowEvent { window_id: _, event } => {
match event {
WindowEvent::Resized(new_size) => {},
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
},
_ => {},
}
},
_ => {},
}
})
}
2 changes: 1 addition & 1 deletion examples/quad/quad.metal
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ vertex ToQuadFragment quad_v(device ToQuadVertex* vertexArray [[ buffer(0) ]],
unsigned int vid [[ vertex_id ]])
{
ToQuadFragment out;
out.position = float4(vertexArray[vid].position, 0.0, 1.0);
out.position = float4(vertexArray[vid].position, 1.0);
out.colour = vertexArray[vid].colour;
return out;
}
Expand Down

0 comments on commit d930309

Please sign in to comment.