Skip to content

Commit

Permalink
Add color space configuration to surfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Feb 11, 2024
1 parent 26bc5e8 commit 88e69b5
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 12 deletions.
17 changes: 12 additions & 5 deletions blade-graphics/src/gles/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ struct WindowSystemInterface {
window_handle: raw_window_handle::RawWindowHandle,
renderbuf: glow::Renderbuffer,
framebuf: glow::Framebuffer,
surface_format: crate::TextureFormat,
}

struct Swapchain {
surface: egl::Surface,
extent: crate::Extent,
format: crate::TextureFormat,
}

struct ContextInner {
Expand Down Expand Up @@ -316,7 +316,6 @@ impl Context {
window_handle: window.raw_window_handle(),
renderbuf,
framebuf,
surface_format: crate::TextureFormat::Rgba8Unorm,
}),
inner: Mutex::new(ContextInner {
egl: egl_context,
Expand Down Expand Up @@ -389,6 +388,8 @@ impl Context {
egl::SINGLE_BUFFER
},
];

//TODO: detect if linear color space is supported
match inner.egl.srgb_kind {
SrgbFrameBufferKind::None => {}
SrgbFrameBufferKind::Core => {
Expand All @@ -402,6 +403,11 @@ impl Context {
}
attributes.push(egl::ATTRIB_NONE as i32);

let format = match config.color_space {
crate::ColorSpace::Linear => crate::TextureFormat::Rgba8UnormSrgb,
crate::ColorSpace::Srgb => crate::TextureFormat::Rgba8Unorm,
};

// Careful, we can still be in 1.4 version even if `upcast` succeeds
let surface = match inner.egl.instance.upcast::<egl::EGL1_5>() {
Some(egl) => {
Expand Down Expand Up @@ -434,9 +440,10 @@ impl Context {
inner.swapchain = Some(Swapchain {
surface,
extent: config.size,
format,
});

let format_desc = super::describe_texture_format(wsi.surface_format);
let format_desc = super::describe_texture_format(format);
inner.egl.make_current();
unsafe {
let gl = &inner.glow;
Expand All @@ -459,7 +466,7 @@ impl Context {
};
inner.egl.unmake_current();

wsi.surface_format
format
}

pub fn acquire_frame(&self) -> super::Frame {
Expand All @@ -470,7 +477,7 @@ impl Context {
texture: super::Texture {
inner: super::TextureInner::Renderbuffer { raw: wsi.renderbuf },
target_size: [sc.extent.width as u16, sc.extent.height as u16],
format: wsi.surface_format,
format: sc.format,
},
}
}
Expand Down
14 changes: 14 additions & 0 deletions blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,11 +959,25 @@ pub struct RenderTargetSet<'a> {
pub depth_stencil: Option<RenderTarget>,
}

#[derive(Copy, Clone, Debug, Default, Hash, Eq, PartialEq)]
pub enum ColorSpace {
#[default]
Linear,
Srgb,
}

#[derive(Debug)]
pub struct SurfaceConfig {
pub size: Extent,
pub usage: TextureUsage,
pub frame_count: u32,
/// The color space that render output colors are expected to be in.
///
/// This will affect the surface format returned by the `Context`.
///
/// For example, if the display expects sRGB space and we render
/// in `ColorSpace::Linear` space, the returned format will be sRGB.
pub color_space: ColorSpace,
}

#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down
1 change: 0 additions & 1 deletion blade-graphics/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ mod surface;
struct Surface {
view: *mut objc::runtime::Object,
render_layer: metal::MetalLayer,
format: crate::TextureFormat,
}

unsafe impl Send for Surface {}
Expand Down
19 changes: 14 additions & 5 deletions blade-graphics/src/metal/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,23 @@ impl super::Surface {
Self {
view: msg_send![view, retain],
render_layer: mem::transmute::<_, &metal::MetalLayerRef>(raw_layer).to_owned(),
format: crate::TextureFormat::Bgra8UnormSrgb,
}
}

fn reconfigure(&mut self, device: &metal::DeviceRef, config: crate::SurfaceConfig) {
fn reconfigure(
&mut self,
device: &metal::DeviceRef,
config: crate::SurfaceConfig,
) -> crate::TextureFormat {
let format = match config.color_space {
crate::ColorSpace::Linear => crate::TextureFormat::Bgra8UnormSrgb,
crate::ColorSpace::Srgb => crate::TextureFormat::Bgra8Unorm,
};

self.render_layer.set_opaque(true);
self.render_layer.set_device(device);
self.render_layer
.set_pixel_format(super::map_texture_format(self.format));
.set_pixel_format(super::map_texture_format(format));
self.render_layer
.set_framebuffer_only(config.usage == crate::TextureUsage::TARGET);
self.render_layer
Expand All @@ -83,14 +91,15 @@ impl super::Surface {
unsafe {
let () = msg_send![self.render_layer, setDisplaySyncEnabled: true];
}

format
}
}

impl super::Context {
pub fn resize(&self, config: crate::SurfaceConfig) -> crate::TextureFormat {
let mut surface = self.surface.as_ref().unwrap().lock().unwrap();
surface.reconfigure(&*self.device.lock().unwrap(), config);
surface.format
surface.reconfigure(&*self.device.lock().unwrap(), config)
}

pub fn acquire_frame(&self) -> super::Frame {
Expand Down
6 changes: 5 additions & 1 deletion blade-graphics/src/vulkan/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,11 @@ impl super::Context {
}

let queue_families = [self.queue_family_index];
let format = crate::TextureFormat::Bgra8UnormSrgb;
//TODO: consider supported color spaces by Vulkan
let format = match config.color_space {
crate::ColorSpace::Linear => crate::TextureFormat::Bgra8UnormSrgb,
crate::ColorSpace::Srgb => crate::TextureFormat::Bgra8Unorm,
};
let vk_format = super::map_texture_format(format);
let create_info = vk::SwapchainCreateInfoKHR::builder()
.surface(surface.raw)
Expand Down
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog for Blade
- support object motion
- support clockwise mesh winding

## blade-graphics-0.4 (TBD)
- color space configuration for surfaces

## blade-graphics-0.3, blade-render-0.2 (17 Nov 2023)
- tangent space generation
- spatio-temporal resampling
Expand Down
1 change: 1 addition & 0 deletions examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl Example {
},
usage: gpu::TextureUsage::TARGET,
frame_count: 3,
color_space: gpu::ColorSpace::Linear,
});

let global_layout = <Params as gpu::ShaderData>::layout();
Expand Down
1 change: 1 addition & 0 deletions examples/particle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl Example {
},
usage: gpu::TextureUsage::TARGET,
frame_count: 3,
color_space: gpu::ColorSpace::Linear,
});
let gui_painter = blade_egui::GuiPainter::new(surface_format, &context);
let particle_system = particle::System::new(
Expand Down
1 change: 1 addition & 0 deletions examples/ray-query/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl Example {
size: screen_size,
usage: gpu::TextureUsage::TARGET,
frame_count: 3,
color_space: gpu::ColorSpace::Linear,
});

let source = std::fs::read_to_string("examples/ray-query/shader.wgsl").unwrap();
Expand Down
1 change: 1 addition & 0 deletions examples/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl Example {
},
usage: gpu::TextureUsage::TARGET,
frame_count: 3,
color_space: gpu::ColorSpace::Linear,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ impl Engine {
},
usage: gpu::TextureUsage::TARGET,
frame_count: 3,
color_space: gpu::ColorSpace::Linear,
}
}

Expand Down

0 comments on commit 88e69b5

Please sign in to comment.