| 1 |
use std::fs::File; |
| 2 |
use std::io::BufWriter; |
| 3 |
use std::path::PathBuf; |
| 4 |
|
| 5 |
use metal::{ |
| 6 |
Buffer, Device, DeviceRef, LibraryRef, MTLClearColor, MTLLoadAction, MTLOrigin, MTLPixelFormat, |
| 7 |
MTLPrimitiveType, MTLRegion, MTLResourceOptions, MTLSize, MTLStoreAction, RenderPassDescriptor, |
| 8 |
RenderPassDescriptorRef, RenderPipelineDescriptor, RenderPipelineState, Texture, |
| 9 |
TextureDescriptor, TextureRef, |
| 10 |
}; |
| 11 |
use png::ColorType; |
| 12 |
|
| 13 |
const VIEW_WIDTH: u64 = 512; |
| 14 |
const VIEW_HEIGHT: u64 = 512; |
| 15 |
const TOTAL_BYTES: usize = (VIEW_WIDTH * VIEW_HEIGHT * 4) as usize; |
| 16 |
|
| 17 |
const VERTEX_SHADER: &str = "triangle_vertex"; |
| 18 |
const FRAGMENT_SHADER: &str = "triangle_fragment"; |
| 19 |
|
| 20 |
// [2 bytes position, 3 bytes color] * 3 |
| 21 |
#[rustfmt::skip] |
| 22 |
const VERTEX_ATTRIBS: [f32; 15] = [ |
| 23 |
0.0, 0.5, 1.0, 0.0, 0.0, |
| 24 |
-0.5, -0.5, 0.0, 1.0, 0.0, |
| 25 |
0.5, -0.5, 0.0, 0.0, 1.0, |
| 26 |
]; |
| 27 |
|
| 28 |
/// This example shows how to render headlessly by: |
| 29 |
/// |
| 30 |
/// 1. Rendering a triangle to an MtlDrawable |
| 31 |
/// |
| 32 |
/// 2. Waiting for the render to complete and the color texture to be synchronized with the CPU |
| 33 |
/// by using a blit command encoder |
| 34 |
/// |
| 35 |
/// 3. Reading the texture bytes from the MtlTexture |
| 36 |
/// |
| 37 |
/// 4. Saving the texture to a PNG file |
| 38 |
fn main() { |
| 39 |
let device = Device::system_default().expect("No device found"); |
| 40 |
|
| 41 |
let texture = create_texture(&device); |
| 42 |
|
| 43 |
let library_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 44 |
.join("examples/window/shaders.metallib"); |
| 45 |
|
| 46 |
let library = device.new_library_with_file(library_path).unwrap(); |
| 47 |
|
| 48 |
let pipeline_state = prepare_pipeline_state(&device, &library); |
| 49 |
|
| 50 |
let command_queue = device.new_command_queue(); |
| 51 |
|
| 52 |
let vertex_buffer = create_vertex_buffer(&device); |
| 53 |
|
| 54 |
let render_pass_descriptor = RenderPassDescriptor::new(); |
| 55 |
initialize_color_attachment(render_pass_descriptor, &texture); |
| 56 |
|
| 57 |
let command_buffer = command_queue.new_command_buffer(); |
| 58 |
let rc_encoder = command_buffer.new_render_command_encoder(render_pass_descriptor); |
| 59 |
rc_encoder.set_render_pipeline_state(&pipeline_state); |
| 60 |
rc_encoder.set_vertex_buffer(0, Some(&vertex_buffer), 0); |
| 61 |
rc_encoder.draw_primitives(MTLPrimitiveType::Triangle, 0, 3); |
| 62 |
rc_encoder.end_encoding(); |
| 63 |
|
| 64 |
render_pass_descriptor |
| 65 |
.color_attachments() |
| 66 |
.object_at(0) |
| 67 |
.unwrap() |
| 68 |
.set_load_action(MTLLoadAction::DontCare); |
| 69 |
|
| 70 |
let blit_encoder = command_buffer.new_blit_command_encoder(); |
| 71 |
blit_encoder.synchronize_resource(&texture); |
| 72 |
blit_encoder.end_encoding(); |
| 73 |
|
| 74 |
command_buffer.commit(); |
| 75 |
|
| 76 |
command_buffer.wait_until_completed(); |
| 77 |
|
| 78 |
save_image(&texture); |
| 79 |
} |
| 80 |
|
| 81 |
fn save_image(texture: &TextureRef) { |
| 82 |
let mut image = vec![0; TOTAL_BYTES]; |
| 83 |
|
| 84 |
texture.get_bytes( |
| 85 |
image.as_mut_ptr().cast(), |
| 86 |
VIEW_WIDTH * 4, |
| 87 |
MTLRegion { |
| 88 |
origin: MTLOrigin { x: 0, y: 0, z: 0 }, |
| 89 |
size: MTLSize { |
| 90 |
width: VIEW_WIDTH, |
| 91 |
height: VIEW_HEIGHT, |
| 92 |
depth: 1, |
| 93 |
}, |
| 94 |
}, |
| 95 |
0, |
| 96 |
); |
| 97 |
|
| 98 |
let out_file = |
| 99 |
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/headless-render/out.png"); |
| 100 |
let file = File::create(&out_file).unwrap(); |
| 101 |
let w = &mut BufWriter::new(file); |
| 102 |
|
| 103 |
let mut encoder = png::Encoder::new(w, VIEW_WIDTH as u32, VIEW_HEIGHT as u32); |
| 104 |
encoder.set_color(ColorType::Rgba); |
| 105 |
encoder.set_depth(png::BitDepth::Eight); |
| 106 |
let mut writer = encoder.write_header().unwrap(); |
| 107 |
|
| 108 |
writer.write_image_data(&image).unwrap(); |
| 109 |
|
| 110 |
println!("Image saved to {:?}", out_file); |
| 111 |
} |
| 112 |
|
| 113 |
fn create_texture(device: &Device) -> Texture { |
| 114 |
let texture = TextureDescriptor::new(); |
| 115 |
texture.set_width(VIEW_WIDTH); |
| 116 |
texture.set_height(VIEW_HEIGHT); |
| 117 |
texture.set_pixel_format(MTLPixelFormat::RGBA8Unorm); |
| 118 |
|
| 119 |
device.new_texture(&texture) |
| 120 |
} |
| 121 |
|
| 122 |
fn prepare_pipeline_state(device: &DeviceRef, library: &LibraryRef) -> RenderPipelineState { |
| 123 |
let vert = library.get_function(VERTEX_SHADER, None).unwrap(); |
| 124 |
let frag = library.get_function(FRAGMENT_SHADER, None).unwrap(); |
| 125 |
|
| 126 |
let pipeline_state_descriptor = RenderPipelineDescriptor::new(); |
| 127 |
|
| 128 |
pipeline_state_descriptor.set_vertex_function(Some(&vert)); |
| 129 |
pipeline_state_descriptor.set_fragment_function(Some(&frag)); |
| 130 |
|
| 131 |
pipeline_state_descriptor |
| 132 |
.color_attachments() |
| 133 |
.object_at(0) |
| 134 |
.unwrap() |
| 135 |
.set_pixel_format(MTLPixelFormat::RGBA8Unorm); |
| 136 |
|
| 137 |
device |
| 138 |
.new_render_pipeline_state(&pipeline_state_descriptor) |
| 139 |
.unwrap() |
| 140 |
} |
| 141 |
|
| 142 |
fn create_vertex_buffer(device: &DeviceRef) -> Buffer { |
| 143 |
device.new_buffer_with_data( |
| 144 |
VERTEX_ATTRIBS.as_ptr().cast(), |
| 145 |
size_of_val(&VERTEX_ATTRIBS) as u64, |
| 146 |
MTLResourceOptions::CPUCacheModeDefaultCache | MTLResourceOptions::StorageModeManaged, |
| 147 |
) |
| 148 |
} |
| 149 |
|
| 150 |
fn initialize_color_attachment(descriptor: &RenderPassDescriptorRef, texture: &TextureRef) { |
| 151 |
let color_attachment = descriptor.color_attachments().object_at(0).unwrap(); |
| 152 |
|
| 153 |
color_attachment.set_texture(Some(texture)); |
| 154 |
color_attachment.set_load_action(MTLLoadAction::Clear); |
| 155 |
color_attachment.set_clear_color(MTLClearColor::new(0.5, 0.2, 0.2, 1.0)); |
| 156 |
color_attachment.set_store_action(MTLStoreAction::Store); |
| 157 |
} |