Skip to content

Commit

Permalink
- messy hacks, using metal library from file to work around excruciat…
Browse files Browse the repository at this point in the history
…ing shader issues with metal capture
  • Loading branch information
polymonster committed Oct 8, 2024
1 parent 86164cf commit 513fdc4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 90 deletions.
53 changes: 0 additions & 53 deletions plugins/imgui.ini

This file was deleted.

62 changes: 44 additions & 18 deletions src/gfx/mtl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use std::result;
use cocoa::{appkit::NSView, base::id as cocoa_id};
use core_graphics_types::geometry::CGSize;

use std::path::Path;

const MEGA_BYTE : usize = 1024 * 1024 * 1024;

const fn to_mtl_vertex_format(format: super::Format) -> MTLVertexFormat {
Expand Down Expand Up @@ -104,7 +106,7 @@ impl super::SwapChain<Device> for SwapChain {
}

fn get_num_buffers(&self) -> u32 {
0
3
}

fn get_frame_fence_value(&self) -> u64 {
Expand Down Expand Up @@ -172,6 +174,7 @@ pub struct CmdBuf {
render_encoder: Option<metal::RenderCommandEncoder>,
compute_encoder: Option<metal::ComputeCommandEncoder>,
bound_index_buffer: Option<metal::Buffer>,
bound_index_stride: usize
}

impl super::CmdBuf<Device> for CmdBuf {
Expand Down Expand Up @@ -281,6 +284,7 @@ impl super::CmdBuf<Device> for CmdBuf {

fn set_index_buffer(&mut self, buffer: &Buffer) {
self.bound_index_buffer = Some(buffer.metal_buffer.clone());
self.bound_index_stride = buffer.element_stride;
}

fn set_render_pipeline(&self, pipeline: &RenderPipeline) {
Expand Down Expand Up @@ -369,7 +373,9 @@ impl super::CmdBuf<Device> for CmdBuf {
self.render_encoder
.as_ref()
.expect("hotline_rs::gfx::metal expected a call to begin render pass before using render commands")
.set_fragment_bytes(slot as u64 + 2, num_values as u64 * 4, data.as_ptr() as _);
// .set_fragment_bytes(slot as u64 + 2, num_values as u64 * 4, data.as_ptr() as _);
// .set_vertex_bytes(slot as u64 + 2, num_values as u64 * 4, data.as_ptr() as _);
.set_vertex_bytes(0, num_values as u64 * 4, data.as_ptr() as _);
}

fn push_compute_constants<T: Sized>(&self, slot: u32, num_values: u32, dest_offset: u32, data: &[T]) {
Expand Down Expand Up @@ -413,7 +419,7 @@ impl super::CmdBuf<Device> for CmdBuf {
index_count as u64,
metal::MTLIndexType::UInt16,
&self.bound_index_buffer.as_ref().unwrap(),
start_index as u64,
start_index as u64 * self.bound_index_stride as u64,
instance_count as u64,
base_vertex as i64,
start_instance as u64
Expand Down Expand Up @@ -473,11 +479,16 @@ impl super::CmdBuf<Device> for CmdBuf {
}

pub struct Buffer {
metal_buffer: metal::Buffer
metal_buffer: metal::Buffer,
element_stride: usize
}

impl super::Buffer<Device> for Buffer {
fn update<T: Sized>(&mut self, offset: usize, data: &[T]) -> result::Result<(), super::Error> {
unsafe {
let data_ptr = self.metal_buffer.contents();
std::ptr::copy_nonoverlapping(data.as_ptr() as *mut u8, data_ptr as *mut u8, data.len());
}
Ok(())
}

Expand Down Expand Up @@ -518,7 +529,6 @@ impl super::Buffer<Device> for Buffer {
}

pub struct Shader {
function: metal::Function,
lib: metal::Library,
data: Vec<u8>
}
Expand Down Expand Up @@ -942,8 +952,7 @@ impl super::Device for Device {

// feature info
let tier = device.argument_buffers_support();
println!("Argument buffer support: {:?}", tier);
assert_eq!(metal::MTLArgumentBuffersTier::Tier2, tier);
assert_eq!(metal::MTLArgumentBuffersTier::Tier2, tier); //TODO: message

Device {
command_queue: command_queue,
Expand Down Expand Up @@ -1021,7 +1030,8 @@ impl super::Device for Device {
cmd: None,
render_encoder: None,
compute_encoder: None,
bound_index_buffer: None
bound_index_buffer: None,
bound_index_stride: 0
}
})
}
Expand All @@ -1033,15 +1043,18 @@ impl super::Device for Device {
objc::rc::autoreleasepool(|| {
let pipeline_state_descriptor = metal::RenderPipelineDescriptor::new();

let vs_data = info.vs.unwrap().data.to_vec();
let lib = self.metal_device.new_library_with_data(vs_data.as_slice())?;
let vvs = lib.get_function("vs_main", None).unwrap();
//let vs_data = info.vs.unwrap().data.to_vec();
//let lib = self.metal_device.new_library_with_data(vs_data.as_slice())?;

let vvs = info.vs.unwrap().lib.get_function("vs_main", None).unwrap();
println!("lib: {:?}", info.vs.unwrap().lib);

pipeline_state_descriptor.set_vertex_function(Some(&vvs));

let ps_data = info.fs.unwrap().data.to_vec();
let lib = self.metal_device.new_library_with_data(ps_data.as_slice())?;
let pps = lib.get_function("ps_main", None).unwrap();
//let ps_data = info.fs.unwrap().data.to_vec();
//let lib = self.metal_device.new_library_with_data(ps_data.as_slice())?;

let pps = info.fs.unwrap().lib.get_function("ps_main", None).unwrap();

pipeline_state_descriptor.set_fragment_function(Some(&pps));

Expand Down Expand Up @@ -1166,10 +1179,21 @@ impl super::Device for Device {
let data = slice_as_u8_slice(src);

let lib = if let Some(compile_info) = info.compile_info.as_ref() {

let u8slice = slice_as_u8_slice(src);
println!("{:?}", u8slice);

let src = std::str::from_utf8(u8slice)?;
println!("{:?}", src);

self.metal_device.new_library_with_file(std::path::Path::new(src))?

/*
let src = std::str::from_utf8(slice_as_u8_slice(src))?;
let opt = metal::CompileOptions::new();
opt.set_fast_math_enabled(true);
self.metal_device.new_library_with_source(src, &opt)?
*/
}
else {
self.metal_device.new_library_with_data(data)?
Expand All @@ -1178,7 +1202,6 @@ impl super::Device for Device {
let names = lib.function_names();
if names.len() == 1 {
Ok(Shader{
function: lib.get_function(names[0].as_str(), None)?.to_owned(),
lib: lib.to_owned(),
data: data.to_vec()
})
Expand Down Expand Up @@ -1214,7 +1237,8 @@ impl super::Device for Device {
};

Ok(Buffer{
metal_buffer: buf
metal_buffer: buf,
element_stride: info.stride
})
})
}
Expand All @@ -1239,7 +1263,8 @@ impl super::Device for Device {
};

Ok(Buffer{
metal_buffer: buf
metal_buffer: buf,
element_stride: info.stride
})
})
}
Expand All @@ -1256,7 +1281,8 @@ impl super::Device for Device {
let buf = self.metal_device.new_buffer(byte_len, opt);

Ok(Buffer{
metal_buffer: buf
metal_buffer: buf,
element_stride: size
})
})
}
Expand Down
29 changes: 22 additions & 7 deletions src/imgui.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use imgui_sys::*;

use crate::gfx::Heap;
use crate::gfx::ShaderCompileFlags;
use crate::gfx::ShaderCompileInfo;
use crate::os;
use crate::os::App;
use crate::os::MonitorInfo;
Expand Down Expand Up @@ -244,17 +246,30 @@ fn create_render_pipeline<D: Device, A: App>(info: &ImGuiInfo<D, A>) -> Result<D
let vsc_data = fs::read(vsc_filepath)?;
let psc_data = fs::read(psc_filepath)?;

let vsc_filepath = crate::get_data_path("shaders/imgui/vs_main.vsc");
let psc_filepath = crate::get_data_path("shaders/imgui/ps_main.psc");

let vs_info = gfx::ShaderInfo {
shader_type: gfx::ShaderType::Vertex,
compile_info: None
compile_info: Some(ShaderCompileInfo{
entry_point: String::from("vs_main"),
target: String::from("vs_6_0"),
flags: ShaderCompileFlags::NONE
})
};
let vs = device.create_shader(&vs_info, &vsc_data)?;
// let vs = device.create_shader(&vs_info, &vsc_data)?;
let vs = device.create_shader(&vs_info, vsc_filepath.as_bytes())?;

let ps_info = gfx::ShaderInfo {
shader_type: gfx::ShaderType::Vertex,
compile_info: None
shader_type: gfx::ShaderType::Fragment,
compile_info: Some(ShaderCompileInfo{
entry_point: String::from("ps_main"),
target: String::from("ps_6_0"),
flags: ShaderCompileFlags::NONE
})
};
let fs = device.create_shader(&ps_info, &psc_data)?;
// let fs = device.create_shader(&ps_info, &psc_data)?;
let fs = device.create_shader(&ps_info, psc_filepath.as_bytes())?;

device.create_render_pipeline(&gfx::RenderPipelineInfo {
vs: Some(&vs),
Expand Down Expand Up @@ -583,8 +598,8 @@ impl<D, A> ImGui<D, A> where D: Device, A: App, D::RenderPipeline: gfx::Pipeline
igCreateContext(std::ptr::null_mut());
let mut io = &mut *igGetIO();

io.ConfigFlags |= ImGuiConfigFlags_DockingEnable as i32;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable as i32;
// io.ConfigFlags |= ImGuiConfigFlags_DockingEnable as i32;
// io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable as i32;

// construct path for ini to be along side the exe
let exe_path = std::env::current_exe().ok().unwrap();
Expand Down
25 changes: 13 additions & 12 deletions src/os/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl super::App for App {

/// Retuns the mouse in screen coordinates
fn get_mouse_pos(&self) -> super::Point<i32> {
panic!();
// TODO:
super::Point {
x: 0,
y: 0
Expand All @@ -122,25 +122,26 @@ impl super::App for App {

/// Retuns the mouse vertical wheel position
fn get_mouse_wheel(&self) -> f32 {
panic!();
//panic!();
// TODO:
0.0
}

/// Retuns the mouse horizontal wheel positions
fn get_mouse_hwheel(&self) -> f32 {
panic!();
// panic!();
0.0
}

/// Retuns the mouse button states, up or down
fn get_mouse_buttons(&self) -> [bool; super::MouseButton::Count as usize] {
panic!();
// panic!();
[false; 5]
}

/// Returns the distance the mouse has moved since the last frame
fn get_mouse_pos_delta(&self) -> super::Size<i32> {
panic!();
// panic!();
super::Size {
x: 0,
y: 0
Expand All @@ -149,32 +150,32 @@ impl super::App for App {

/// Returns a vector of utf-16 characters that have been input since the last frame
fn get_utf16_input(&self) -> Vec<u16> {
panic!();
// panic!();
vec![]
}

/// Returns an array of bools containing 0-256 keys down (true) or up (false)
fn get_keys_down(&self) -> [bool; 256] {
panic!();
// panic!();
[false; 256]
}

/// Returns an array of bools containing 0-256 of keys pressed, will trigger only once and then require debouce
fn get_keys_pressed(&self) -> [bool; 256] {
panic!();
// panic!();
[false; 256]
}

/// Returns true if the sys key is down and false if the key is up
fn is_sys_key_down(&self, key: super::SysKey) -> bool {
panic!();
// panic!();
false
}

/// Returns true if the sys key is pressed this frame and
/// requires debounce until it is pressed again
fn is_sys_key_pressed(&self, key: super::SysKey) -> bool {
panic!();
// panic!();
false
}

Expand Down Expand Up @@ -372,7 +373,7 @@ impl super::Window<App> for Window {

/// Returns true if the mouse if hovering this window
fn is_mouse_hovered(&self) -> bool {
panic!();
// TODO:
false
}

Expand Down Expand Up @@ -437,7 +438,7 @@ impl super::Window<App> for Window {

/// Return mouse position in relative coordinates from the top left corner of the window
fn get_mouse_client_pos(&self, mouse_pos: super::Point<i32>) -> super::Point<i32> {
panic!();
// panic!();
super::Point {
x: 0,
y: 0
Expand Down

0 comments on commit 513fdc4

Please sign in to comment.