diff --git a/gi/src/graphics/collections/agg_fast_path.rs b/gi/src/graphics/collections/agg_fast_path.rs index 31503e2..400e642 100644 --- a/gi/src/graphics/collections/agg_fast_path.rs +++ b/gi/src/graphics/collections/agg_fast_path.rs @@ -158,10 +158,11 @@ impl AttaWithBuffer for AggFastPath { let len = ebo.len() as i32; - attach.bind_data(&vbo, Some(&ebo), len, glow::STATIC_DRAW); + attach.bind_data(gl,&vbo, Some(&ebo), len, glow::STATIC_DRAW); Ok(()) } + #[cfg(target_os = "macos")] fn init( &self, gl: &glow::Context, @@ -197,6 +198,48 @@ impl AttaWithBuffer for AggFastPath { (vao, vbo, Some(ebo), None) } } + + #[cfg(not(target_os = "macos"))] + fn init( + &self, + gl: &glow::Context, + ) -> ( + NativeVertexArray, + NativeBuffer, + Option, + Option, + ) { + unsafe { + let vao = gl.create_vertex_array().unwrap(); + let vbo = gl.create_buffer().unwrap(); + + gl.vertex_array_vertex_buffer(vao, 0, Some(vbo), 0, 40); + + + gl.enable_vertex_array_attrib(vao, 0); + gl.vertex_array_attrib_format_i32(vao, 0, 4, glow::FLOAT, 0); + + gl.enable_vertex_array_attrib(vao, 1); + gl.vertex_array_attrib_format_i32(vao, 1, 3, glow::FLOAT, 12); + + gl.enable_vertex_array_attrib(vao, 2); + gl.vertex_array_attrib_format_i32(vao, 2, 3, glow::FLOAT, 24); + + gl.enable_vertex_array_attrib(vao, 3); + gl.vertex_array_attrib_format_i32(vao, 3, 3, glow::FLOAT, 36); + + gl.vertex_array_attrib_binding_f32(vao, 0, 0); + gl.vertex_array_attrib_binding_f32(vao, 1, 0); + gl.vertex_array_attrib_binding_f32(vao, 2, 0); + gl.vertex_array_attrib_binding_f32(vao, 3, 0); + + let ebo = gl.create_buffer().unwrap(); + gl.vertex_array_element_buffer(vao, Some(ebo)); + + + (vao, vbo, Some(ebo), None) + } + } } #[repr(C)] #[derive(Debug, Clone, Copy, Zeroable, Pod)] diff --git a/gi/src/graphics/colormap/linear.rs b/gi/src/graphics/colormap/linear.rs index 415042d..3cd87cd 100644 --- a/gi/src/graphics/colormap/linear.rs +++ b/gi/src/graphics/colormap/linear.rs @@ -54,6 +54,7 @@ impl ColorMap for LinearColormap { program: &crate::components::Program, ) -> crate::errors::Result<()> { use bytemuck::cast_slice; + #[cfg(target_os = "macos")] unsafe { if self.color_changed { let texture = gl.create_texture().unwrap(); @@ -92,6 +93,27 @@ impl ColorMap for LinearColormap { } } + #[cfg(not(target_os = "macos"))] + unsafe { + let texture = gl.create_named_texture(glow::TEXTURE_1D).unwrap(); + gl.texture_parameter_i32( + texture, + glow::TEXTURE_MIN_FILTER, + glow::NEAREST as i32, + ); + gl.texture_parameter_i32( + texture, + glow::TEXTURE_MAG_FILTER, + glow::NEAREST as i32, + ); + + // gl.tex_storage_1d(texture, 0, glow::RGBA, self.colors.len() as i32) + + todo!("implement this"); + + + } + Ok(()) } diff --git a/gi/src/graphics/font/mod.rs b/gi/src/graphics/font/mod.rs index 12cb3e9..199178d 100644 --- a/gi/src/graphics/font/mod.rs +++ b/gi/src/graphics/font/mod.rs @@ -310,10 +310,11 @@ impl AttaWithBuffer for Text { &mut *self.font_manager.borrow_mut(), &mut *self.cache.borrow_mut(), )?; - attach.bind_data(&v.vertex(), None, v.len() as i32, glow::STATIC_DRAW); + attach.bind_data(gl,&v.vertex(), None, v.len() as i32, glow::STATIC_DRAW); Ok(()) } + #[cfg(target_os = "macos")] fn init( &self, gl: &glow::Context, @@ -337,6 +338,36 @@ impl AttaWithBuffer for Text { gl.bind_vertex_array(None); gl.bind_buffer(glow::ARRAY_BUFFER, None); + (vao, vbo, None, None) + } + } + + // OpenGl 4.6 + #[cfg(not(target_os = "macos"))] + fn init( + &self, + gl: &glow::Context, + ) -> ( + glow::NativeVertexArray, + glow::NativeBuffer, + Option, + Option, + ) { + + unsafe { + let vao = gl.create_vertex_array().unwrap(); + let vbo = gl.create_buffer().unwrap(); + gl.vertex_array_vertex_buffer(vao, 0, Some(vbo), 0, 48); + + gl.enable_vertex_array_attrib(vao, 0); + gl.vertex_array_attrib_format_i32(vao, 0, 4, glow::FLOAT, 0); + + gl.enable_vertex_array_attrib(vao, 1); + gl.vertex_array_attrib_format_i32(vao, 0, 4, glow::FLOAT, 16); + + gl.vertex_array_attrib_binding_f32(vao, 0, 0); + gl.vertex_array_attrib_binding_f32(vao, 1, 0); + (vao, vbo, None, None) } } diff --git a/gi/src/graphics/geoquadmesh.rs b/gi/src/graphics/geoquadmesh.rs index 06daa8f..42b90f3 100644 --- a/gi/src/graphics/geoquadmesh.rs +++ b/gi/src/graphics/geoquadmesh.rs @@ -304,7 +304,7 @@ impl AttaWithBuffer for GeoQuadMesh { 0.0, ]); } - attach.bind_data(&vertices, None, 4, glow::STATIC_DRAW); + attach.bind_data(gl,&vertices, None, 4, glow::STATIC_DRAW); // Texture unsafe { @@ -376,6 +376,7 @@ impl AttaWithBuffer for GeoQuadMesh { } } + #[cfg(target_os = "macos")] fn init( &self, gl: &glow::Context, @@ -398,6 +399,65 @@ impl AttaWithBuffer for GeoQuadMesh { gl.bind_vertex_array(None); gl.bind_buffer(glow::ARRAY_BUFFER, None); + // Create Texture + let texture = gl.create_texture().unwrap(); + gl.bind_texture(glow::TEXTURE_3D, Some(texture)); + + gl.tex_parameter_i32( + glow::TEXTURE_3D, + glow::TEXTURE_MIN_FILTER, + glow::NEAREST as i32, + ); + gl.tex_parameter_i32( + glow::TEXTURE_3D, + glow::TEXTURE_MAG_FILTER, + glow::NEAREST as i32, + ); + gl.tex_parameter_i32( + glow::TEXTURE_3D, + glow::TEXTURE_WRAP_S, + glow::CLAMP_TO_EDGE as i32, + ); + gl.tex_parameter_i32( + glow::TEXTURE_3D, + glow::TEXTURE_WRAP_T, + glow::CLAMP_TO_EDGE as i32, + ); + gl.tex_parameter_i32( + glow::TEXTURE_3D, + glow::TEXTURE_WRAP_R, + glow::CLAMP_TO_EDGE as i32, + ); + + (vao, vbo, None, Some(texture)) + } + } + + #[cfg(not(target_os = "macos"))] + fn init( + &self, + gl: &glow::Context, + ) -> ( + glow::NativeVertexArray, + glow::NativeBuffer, + Option, + Option, + ) { + unsafe { + let vao = gl.create_vertex_array().unwrap(); + let vbo = gl.create_buffer().unwrap(); + + gl.vertex_array_vertex_buffer(vao, 0, Some(vbo), 0, 24); + + gl.enable_vertex_array_attrib(vao, 0); + gl.vertex_array_attrib_format_i32(vao, 0, 3, glow::FLOAT, 0); + gl.enable_vertex_array_attrib(vao, 1); + gl.vertex_array_attrib_format_i32(vao, 1, 3, glow::FLOAT, 12); + + gl.vertex_array_attrib_binding_f32(vao, 0, 0); + gl.vertex_array_attrib_binding_f32(vao, 1, 0); + + // Create Texture let texture = gl.create_texture().unwrap(); gl.bind_texture(glow::TEXTURE_3D, Some(texture)); diff --git a/gi/src/graphics/ppi.rs b/gi/src/graphics/ppi.rs index 957ed9c..6bfd3d1 100644 --- a/gi/src/graphics/ppi.rs +++ b/gi/src/graphics/ppi.rs @@ -204,18 +204,19 @@ impl AttaWithBuffer for PPI { let last_range = *range.last().unwrap() as f32; + let data = &data.data.cast_to::(); + for azi_idx in 0..azimuth_len { for r_idx in 0..r_len { let azi = *azimuth.get(azi_idx).unwrap() as f32; let r = *range.get(r_idx).unwrap() as f32 / last_range; - let data = &data.data.cast_to::(); let dt = data.get([layer, azi_idx, r_idx]).unwrap(); vertices.extend([r, azi, ele, *dt]); } } let len = vertices.len() as i32 / 4; - attach.bind_data(&vertices, None, len, glow::STATIC_DRAW); + attach.bind_data(gl,&vertices, None, len, glow::STATIC_DRAW); Ok(()) } _ => { @@ -223,6 +224,9 @@ impl AttaWithBuffer for PPI { } } } + + // OpenGl 4.1 + #[cfg(target_os = "macos")] fn init( &self, gl: &glow::Context, @@ -245,6 +249,33 @@ impl AttaWithBuffer for PPI { (vao, vbo, None, None) } } + + + // OpenGl 4.6 + #[cfg(not(target_os = "macos"))] + fn init( + &self, + gl: &glow::Context, + ) -> ( + NativeVertexArray, + NativeBuffer, + Option, + Option, + ) { + + unsafe { + let vao = gl.create_vertex_array().unwrap(); + let vbo = gl.create_buffer().unwrap(); + gl.vertex_array_vertex_buffer(vao, 0, Some(vbo), 0, 16); + + gl.enable_vertex_array_attrib(vao, 0); + gl.vertex_array_attrib_format_i32(vao, 0, 4, glow::FLOAT, 0); + + gl.vertex_array_attrib_binding_f32(vao, 0, 0); + + (vao, vbo, None, None) + } + } } #[derive(Default, Clone, Debug)] diff --git a/gi/src/pg/layout_type.rs b/gi/src/pg/layout_type.rs index 34e5790..9822ac8 100644 --- a/gi/src/pg/layout_type.rs +++ b/gi/src/pg/layout_type.rs @@ -62,7 +62,7 @@ pub struct ViewPort { impl ViewPort { pub fn bind(&self, gl: &glow::Context) { - self.main_fbo.bind(glow::FRAMEBUFFER); + self.main_fbo.bind(gl,glow::FRAMEBUFFER); unsafe { gl.viewport( 0, @@ -110,12 +110,12 @@ impl ViewPort { let main_rbo: RcGlRcResource = gl.create_resource_rc(); let main_depth_rbo: RcGlRcResource = gl.create_resource_rc(); - main_fbo.bind(glow::FRAMEBUFFER); + main_fbo.bind(gl,glow::FRAMEBUFFER); unsafe { // Color Attachment gl.active_texture(glow::TEXTURE0); - main_rbo.bind(glow::TEXTURE_2D); + main_rbo.bind(gl,glow::TEXTURE_2D); gl.tex_image_2d( glow::TEXTURE_2D, 0, @@ -149,7 +149,7 @@ impl ViewPort { if depth { // Depth Attachment - main_depth_rbo.bind(glow::RENDERBUFFER); + main_depth_rbo.bind(gl,glow::RENDERBUFFER); gl.renderbuffer_storage( glow::RENDERBUFFER, glow::DEPTH_COMPONENT24, @@ -195,11 +195,11 @@ impl ViewPort { let main_rbo: RcGlRcResource = gl.create_resource_rc(); let main_depth_rbo: RcGlRcResource = gl.create_resource_rc(); - main_fbo.bind(glow::FRAMEBUFFER); + main_fbo.bind(gl,glow::FRAMEBUFFER); unsafe { // Color Attachment - main_rbo.bind(glow::RENDERBUFFER); + main_rbo.bind(gl,glow::RENDERBUFFER); gl.renderbuffer_storage(glow::RENDERBUFFER, glow::RGBA8, RBO_WIDTH, RBO_HEIGHT); gl.framebuffer_renderbuffer( glow::FRAMEBUFFER, @@ -211,7 +211,7 @@ impl ViewPort { if depth { // Depth Attachment - main_depth_rbo.bind(glow::RENDERBUFFER); + main_depth_rbo.bind(gl,glow::RENDERBUFFER); gl.renderbuffer_storage( glow::RENDERBUFFER, glow::DEPTH_COMPONENT24, diff --git a/gi/src/pg/modules/geoquadmesh.rs b/gi/src/pg/modules/geoquadmesh.rs index 973aeb7..9baffa4 100644 --- a/gi/src/pg/modules/geoquadmesh.rs +++ b/gi/src/pg/modules/geoquadmesh.rs @@ -194,8 +194,10 @@ impl<'b, 'a: 'b> Module for GeoQuadMeshModule<'b, 'a> { .set_config(&self.gl, &config.to_quad_config())?; // Quad Draw - quad_attach.bind_self(self.geo_quad_mesh_program.program_ref()); + quad_attach.bind_self(&self.gl,self.geo_quad_mesh_program.program_ref()); // Bind the texture + + #[cfg(target_os = "macos")] self.bind_attach_tex(quad_attach)?; // Draw the quad diff --git a/gi/src/pg/modules/mod.rs b/gi/src/pg/modules/mod.rs index f79ffb1..aadd22b 100644 --- a/gi/src/pg/modules/mod.rs +++ b/gi/src/pg/modules/mod.rs @@ -9,6 +9,7 @@ use crate::{ utils::resources::{ManagedResource, RcGlBuffer, RcGlVertexArray}, GL, }; +use cgmath::num_traits::cast; use femtovg::{renderer::OpenGl, Canvas}; use glow::{HasContext, NativeBuffer, NativeTexture, NativeVertexArray}; use radarg_core::{config::Setting, Data}; @@ -64,12 +65,20 @@ impl Attach { } } - fn bind_self(&self, program: &Program) { - self.vao.bind(glow::VERTEX_ARRAY); - self.vbo.bind(glow::ARRAY_BUFFER); - if let Some(ebo) = self.ebo.as_ref() { - ebo.bind(glow::ELEMENT_ARRAY_BUFFER); + fn bind_self(&self,gl: &glow::Context, program: &Program) { + #[cfg(target_os = "macos")] + { + self.vao.bind(gl,glow::VERTEX_ARRAY); + self.vbo.bind(gl,glow::ARRAY_BUFFER); + if let Some(ebo) = self.ebo.as_ref() { + ebo.bind(gl,glow::ELEMENT_ARRAY_BUFFER); + } } + #[cfg(not(target_os = "macos"))] + unsafe { + gl.bind_vertex_array(Some(self.vao.native())); + } + } fn unbind_self(&self) { @@ -80,24 +89,38 @@ impl Attach { } } - pub fn bind_data(&mut self, vbo: &Vec, ebo: Option<&Vec>, len: i32, usage: u32) { + pub fn bind_data(&mut self,gl:&glow::Context, vbo: &Vec, ebo: Option<&Vec>, len: i32, usage: u32) { use bytemuck::cast_slice; - self.vbo.bind(glow::ARRAY_BUFFER); - unsafe { - self.gl - .buffer_data_u8_slice(glow::ARRAY_BUFFER, cast_slice(&vbo), usage); - if let Some(ebo) = ebo { - self.gl.bind_buffer( - glow::ELEMENT_ARRAY_BUFFER, - Some(self.ebo.as_ref().unwrap().native()), - ); - self.gl - .buffer_data_u8_slice(glow::ELEMENT_ARRAY_BUFFER, cast_slice(&ebo), usage); + #[cfg(target_os = "macos")] + { + self.vbo.bind(gl,glow::ARRAY_BUFFER); + unsafe { + gl + .buffer_data_u8_slice(glow::ARRAY_BUFFER, cast_slice(&vbo), usage); + if let Some(ebo) = ebo { + gl.bind_buffer( + glow::ELEMENT_ARRAY_BUFFER, + Some(self.ebo.as_ref().unwrap().native()), + ); + gl + .buffer_data_u8_slice(glow::ELEMENT_ARRAY_BUFFER, cast_slice(&ebo), usage); - self.gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None); + gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None); + } + } + self.vbo.unbind(glow::ARRAY_BUFFER); + } + + #[cfg(not(target_os = "macos"))] + { + unsafe { + gl.named_buffer_data_u8_slice(self.vbo.native(), cast_slice(&vbo), usage); + if let Some(ebo) = ebo { + gl.named_buffer_data_u8_slice(self.ebo.as_ref().unwrap().native(), cast_slice(ebo), usage); + } } } - self.vbo.unbind(glow::ARRAY_BUFFER); + self.len = len; } diff --git a/gi/src/pg/modules/ppi.rs b/gi/src/pg/modules/ppi.rs index 8b44aac..587f767 100644 --- a/gi/src/pg/modules/ppi.rs +++ b/gi/src/pg/modules/ppi.rs @@ -314,8 +314,11 @@ impl<'b, 'a: 'b> Module for PPIModule<'b, 'a> { } // PPI Draw - ppi_attach.bind_self(&self.ppi_program.program_ref()); + ppi_attach.bind_self(&self.gl,&self.ppi_program.program_ref()); + self.ppi_program.draw(&self.gl, ppi_attach.len())?; + + #[cfg(target_os = "macos")] ppi_attach.unbind_self(); // Unmount PPI Program @@ -343,7 +346,7 @@ impl<'b, 'a: 'b> Module for PPIModule<'b, 'a> { // PPI Tick Draw let attach = &mut cursor.line_attach; - attach.bind_self(self.line_program.program_ref()); + attach.bind_self(&self.gl,self.line_program.program_ref()); self.line_program.draw(&self.gl, attach.len())?; attach.unbind_self(); @@ -358,7 +361,7 @@ impl<'b, 'a: 'b> Module for PPIModule<'b, 'a> { self.text_program .set_config(&self.gl, &config.to_font_config()); - tick_attach.bind_self(self.text_program.program_ref()); + tick_attach.bind_self(&self.gl,self.text_program.program_ref()); self.text_program.draw(&self.gl, tick_attach.len())?; tick_attach.unbind_self(); diff --git a/gi/src/utils/resources.rs b/gi/src/utils/resources.rs index 5663b6e..384bfbe 100644 --- a/gi/src/utils/resources.rs +++ b/gi/src/utils/resources.rs @@ -17,7 +17,7 @@ pub type RcGlRcBuffer = RcGlRcResource; // pub type RcGlResource<'a, T> = Rc>; pub trait ManagedResource { - fn bind(&self, target: u32); + fn bind(&self, gl:&glow::Context, target: u32); fn unbind(&self, target: u32); } #[derive(Debug, Clone)] @@ -305,8 +305,8 @@ impl Deref for GL { } impl ManagedResource for RcGlResource<'_, T> { - fn bind(&self, target: u32) { - self.0.resource.bind(self.0.gl, target); + fn bind(&self,gl:&glow::Context, target: u32) { + self.0.resource.bind(gl, target); } fn unbind(&self, target: u32) { @@ -315,8 +315,8 @@ impl ManagedResource for RcGlResource<'_, T> { } impl ManagedResource for RcGlRcResource { - fn bind(&self, target: u32) { - self.0.resource.bind(&self.0.gl, target); + fn bind(&self,gl:&glow::Context, target: u32) { + self.0.resource.bind(gl, target); } fn unbind(&self, target: u32) { diff --git a/radar-g/src/main.rs b/radar-g/src/main.rs index 3be5ee4..024f4f0 100644 --- a/radar-g/src/main.rs +++ b/radar-g/src/main.rs @@ -15,14 +15,14 @@ use components::app::AppModel; use gi::{App as GI, Helper, GL}; use once_cell::{sync::Lazy as SafeLazy, unsync::Lazy as UnsafeLazy}; use relm4::RelmApp; -use surfman::declare_surfman; +// use surfman::declare_surfman; use tracing::info; use tracing_subscriber; mod predefined; mod widgets; -declare_surfman!(); +// declare_surfman!(); const APP_ID: &str = "org.tsuki.radar_g"; static RUNTIME: SafeLazy = diff --git a/radar-g/src/widgets/render/imp.rs b/radar-g/src/widgets/render/imp.rs index 416373f..eebbfee 100644 --- a/radar-g/src/widgets/render/imp.rs +++ b/radar-g/src/widgets/render/imp.rs @@ -127,9 +127,12 @@ impl ObjectImpl for Render { } impl WidgetImpl for Render { + fn realize(&self) { self.parent_realize(); } + + fn unrealize(&self) { self.obj().make_current(); self.gi.borrow_mut().as_mut().unwrap().destroy(); @@ -139,6 +142,14 @@ impl WidgetImpl for Render { } impl GLAreaImpl for Render { + + fn create_context(&self) -> Option { + let context = self.parent_create_context(); + context.as_ref().map(|ctx| { + }); + context + } + fn resize(&self, width: i32, height: i32) { { let mut status = self.status.borrow_mut(); @@ -173,21 +184,9 @@ impl GLAreaImpl for Render { if let Some(gi) = gi.as_mut() { let gl = &gi.context.gl; - unsafe { - let err = gl.get_error(); - if err != glow::NO_ERROR { - panic!("GL Error: {:?}", err); - } - } let mut operation = self.opeartion.borrow_mut(); let viewport = self.viewport.borrow(); operation.deal_io(&viewport.as_ref().unwrap(), &self.io.borrow()); - unsafe { - let err = gl.get_error(); - if err != glow::NO_ERROR { - panic!("GL Error: {:?}", err); - } - } gi.render( &mut *self.modules.borrow_mut(), @@ -213,6 +212,8 @@ impl Render { info!("Creating canvas"); widget.make_current(); widget.attach_buffers(); + + info!("Debug enabled: {}", self.obj().context().unwrap().is_debug_enabled()); let (mut gi, viewport) = unsafe { static LOAD_FN: fn(&str) -> *const std::ffi::c_void = |s| epoxy::get_proc_addr(s) as *const _; @@ -316,16 +317,16 @@ fn gl_debug_output( fn enable_opengl_debugging(ctx: &mut glow::Context) { unsafe { if ctx.supported_extensions().contains("GL_ARB_debug_output") { - unsafe { - ctx.debug_message_callback(|source, _type, id, severity, message| { - println!( - "GL ARB Debug Message: Source: {:?}, ID: {}, Severity: {:?}, Message: {}", - source, id, severity, message - ); - }); - ctx.enable(glow::DEBUG_OUTPUT); - ctx.enable(glow::DEBUG_OUTPUT_SYNCHRONOUS); - } + + ctx.enable(glow::DEBUG_OUTPUT); + ctx.enable(glow::DEBUG_OUTPUT_SYNCHRONOUS); + ctx.debug_message_callback(|source, _type, id, severity, message| { + println!( + "GL ARB Debug Message: Source: {:?}, ID: {}, Severity: {:?}, Message: {}", + source, id, severity, message + ); + }); + } else { info!("GL_ARB_debug_output not supported"); }