Skip to content

Commit 61fdaee

Browse files
committed
capi(gl): update to take opengl context in constructor
1 parent 2552d43 commit 61fdaee

File tree

7 files changed

+39
-70
lines changed

7 files changed

+39
-70
lines changed

include/librashader.h

+2-20
Original file line numberDiff line numberDiff line change
@@ -644,16 +644,11 @@ typedef int32_t (*PFN_libra_error_write)(libra_error_t error, char **out);
644644
/// Function pointer definition for libra_error_free_string
645645
typedef int32_t (*PFN_libra_error_free_string)(char **out);
646646

647-
#if defined(LIBRA_RUNTIME_OPENGL)
648-
/// Function pointer definition for
649-
///libra_gl_init_context
650-
typedef libra_error_t (*PFN_libra_gl_init_context)(libra_gl_loader_t loader);
651-
#endif
652-
653647
#if defined(LIBRA_RUNTIME_OPENGL)
654648
/// Function pointer definition for
655649
///libra_gl_filter_chain_create
656650
typedef libra_error_t (*PFN_libra_gl_filter_chain_create)(libra_shader_preset_t *preset,
651+
libra_gl_loader_t loader,
657652
const struct filter_chain_gl_opt_t *options,
658653
libra_gl_filter_chain_t *out);
659654
#endif
@@ -1197,20 +1192,6 @@ libra_error_t libra_preset_get_runtime_params(libra_shader_preset_t *preset,
11971192
/// in undefined behaviour.
11981193
libra_error_t libra_preset_free_runtime_params(struct libra_preset_param_list_t preset);
11991194

1200-
#if defined(LIBRA_RUNTIME_OPENGL)
1201-
/// Initialize the OpenGL Context for librashader.
1202-
///
1203-
/// This only has to be done once throughout the lifetime of the application,
1204-
/// unless for whatever reason you switch OpenGL loaders mid-flight.
1205-
///
1206-
/// ## Safety
1207-
/// Attempting to create a filter chain will fail if the GL context is not initialized.
1208-
///
1209-
/// Reinitializing the OpenGL context with a different loader immediately invalidates previous filter
1210-
/// chain objects, and drawing with them causes immediate undefined behaviour.
1211-
libra_error_t libra_gl_init_context(libra_gl_loader_t loader);
1212-
#endif
1213-
12141195
#if defined(LIBRA_RUNTIME_OPENGL)
12151196
/// Create the filter chain given the shader preset.
12161197
///
@@ -1222,6 +1203,7 @@ libra_error_t libra_gl_init_context(libra_gl_loader_t loader);
12221203
/// - `options` must be either null, or valid and aligned.
12231204
/// - `out` must be aligned, but may be null, invalid, or uninitialized.
12241205
libra_error_t libra_gl_filter_chain_create(libra_shader_preset_t *preset,
1206+
libra_gl_loader_t loader,
12251207
const struct filter_chain_gl_opt_t *options,
12261208
libra_gl_filter_chain_t *out);
12271209
#endif

include/librashader_ld.h

+2-18
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,9 @@ libra_error_t __librashader__noop_preset_free_runtime_params(
194194
return NULL;
195195
}
196196
#if defined(LIBRA_RUNTIME_OPENGL)
197-
libra_error_t __librashader__noop_gl_init_context(libra_gl_loader_t loader) {
198-
return NULL;
199-
}
200-
201197
libra_error_t __librashader__noop_gl_filter_chain_create(
202-
libra_shader_preset_t *preset, const struct filter_chain_gl_opt_t *options,
198+
libra_shader_preset_t *preset, libra_gl_loader_t loader,
199+
const struct filter_chain_gl_opt_t *options,
203200
libra_gl_filter_chain_t *out) {
204201
*out = NULL;
205202
return NULL;
@@ -799,17 +796,6 @@ typedef struct libra_instance_t {
799796
PFN_libra_error_free_string error_free_string;
800797

801798
#if defined(LIBRA_RUNTIME_OPENGL)
802-
/// Initialize the OpenGL Context for librashader.
803-
///
804-
/// ## Safety
805-
/// Attempting to create a filter chain will fail if the context is not
806-
/// initialized.
807-
///
808-
/// Reinitializing the OpenGL context with a different loader immediately
809-
/// invalidates previous filter chain objects, and drawing with them causes
810-
/// immediate undefined behaviour.
811-
PFN_libra_gl_init_context gl_init_context;
812-
813799
/// Create the filter chain given the shader preset.
814800
///
815801
/// The shader preset is immediately invalidated and must be recreated after
@@ -1445,7 +1431,6 @@ libra_instance_t __librashader_make_null_instance(void) {
14451431
instance.error_free_string = __librashader__noop_error_free_string;
14461432

14471433
#if defined(LIBRA_RUNTIME_OPENGL)
1448-
instance.gl_init_context = __librashader__noop_gl_init_context;
14491434
instance.gl_filter_chain_create =
14501435
__librashader__noop_gl_filter_chain_create;
14511436
instance.gl_filter_chain_frame = __librashader__noop_gl_filter_chain_frame;
@@ -1620,7 +1605,6 @@ libra_instance_t librashader_load_instance(void) {
16201605
_LIBRASHADER_ASSIGN(librashader, instance, error_free_string);
16211606

16221607
#if defined(LIBRA_RUNTIME_OPENGL)
1623-
_LIBRASHADER_ASSIGN(librashader, instance, gl_init_context);
16241608
_LIBRASHADER_ASSIGN(librashader, instance, gl_filter_chain_create);
16251609
_LIBRASHADER_ASSIGN(librashader, instance, gl_filter_chain_frame);
16261610
_LIBRASHADER_ASSIGN(librashader, instance, gl_filter_chain_free);

librashader-capi/src/runtime/gl/filter_chain.rs

+28-27
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ use librashader::runtime::gl::{
77
FilterChain, FilterChainOptions, FrameOptions, GLFramebuffer, GLImage,
88
};
99
use std::ffi::CStr;
10-
use std::ffi::{c_char, c_void, CString};
10+
use std::ffi::{c_char, c_void};
1111
use std::mem::MaybeUninit;
12+
use std::num::NonZeroU32;
1213
use std::ptr::NonNull;
1314
use std::slice;
14-
15+
use std::sync::Arc;
1516
use crate::LIBRASHADER_API_VERSION;
17+
use librashader::runtime::gl::error::FilterChainError;
1618
use librashader::runtime::FilterChainParameters;
1719
use librashader::runtime::{Size, Viewport};
1820

@@ -49,8 +51,12 @@ pub struct libra_output_framebuffer_gl_t {
4951

5052
impl From<libra_source_image_gl_t> for GLImage {
5153
fn from(value: libra_source_image_gl_t) -> Self {
54+
let handle = NonZeroU32::try_from(value.handle)
55+
.ok()
56+
.map(glow::NativeTexture);
57+
5258
GLImage {
53-
handle: value.handle,
59+
handle,
5460
format: value.format,
5561
size: Size::new(value.width, value.height),
5662
}
@@ -108,27 +114,6 @@ config_struct! {
108114
}
109115
}
110116

111-
extern_fn! {
112-
/// Initialize the OpenGL Context for librashader.
113-
///
114-
/// This only has to be done once throughout the lifetime of the application,
115-
/// unless for whatever reason you switch OpenGL loaders mid-flight.
116-
///
117-
/// ## Safety
118-
/// Attempting to create a filter chain will fail if the GL context is not initialized.
119-
///
120-
/// Reinitializing the OpenGL context with a different loader immediately invalidates previous filter
121-
/// chain objects, and drawing with them causes immediate undefined behaviour.
122-
raw fn libra_gl_init_context(loader: libra_gl_loader_t) {
123-
gl::load_with(|s| unsafe {
124-
let proc_name = CString::new(s).unwrap_unchecked();
125-
loader(proc_name.as_ptr())
126-
});
127-
128-
LibrashaderError::ok()
129-
}
130-
}
131-
132117
extern_fn! {
133118
/// Create the filter chain given the shader preset.
134119
///
@@ -141,6 +126,7 @@ extern_fn! {
141126
/// - `out` must be aligned, but may be null, invalid, or uninitialized.
142127
fn libra_gl_filter_chain_create(
143128
preset: *mut libra_shader_preset_t,
129+
loader: libra_gl_loader_t,
144130
options: *const MaybeUninit<filter_chain_gl_opt_t>,
145131
out: *mut MaybeUninit<libra_gl_filter_chain_t>
146132
) {
@@ -160,7 +146,11 @@ extern_fn! {
160146
let options = options.map(FromUninit::from_uninit);
161147

162148
unsafe {
163-
let chain = FilterChain::load_from_preset(*preset, options.as_ref())?;
149+
let context = glow::Context::from_loader_function_cstr(
150+
|proc_name| loader(proc_name.as_ptr()));
151+
152+
let chain = FilterChain::load_from_preset(Arc::new(context),
153+
*preset, options.as_ref())?;
164154

165155
out.write(MaybeUninit::new(NonNull::new(Box::into_raw(Box::new(
166156
chain,
@@ -206,8 +196,19 @@ extern_fn! {
206196
};
207197

208198
let opt = opt.map(FromUninit::from_uninit);
209-
let framebuffer = GLFramebuffer::new_from_raw(out.texture, out.fbo, out.format,
210-
Size::new(out.width, out.height), 1);
199+
200+
let texture = NonZeroU32::try_from(out.texture)
201+
.ok()
202+
.map(glow::NativeTexture);
203+
204+
let fbo = NonZeroU32::try_from(out.fbo)
205+
.ok()
206+
.map(glow::NativeFramebuffer)
207+
.ok_or(FilterChainError::GlInvalidFramebuffer)?;
208+
209+
let framebuffer = GLFramebuffer::new_from_raw(Arc::clone(chain.get_context()),
210+
texture, fbo, out.format, Size::new(out.width, out.height), 1);
211+
211212
let viewport = Viewport {
212213
x: viewport.x,
213214
y: viewport.y,

librashader-capi/src/version.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ pub const LIBRASHADER_CURRENT_VERSION: LIBRASHADER_API_VERSION = 1;
3131
/// ## ABI Versions
3232
/// - ABI version 0: null instance (unloaded)
3333
/// - ABI version 1: 0.1.0
34-
pub const LIBRASHADER_CURRENT_ABI: LIBRASHADER_ABI_VERSION = 1;
34+
/// - ABI version 2: 0.4.0
35+
pub const LIBRASHADER_CURRENT_ABI: LIBRASHADER_ABI_VERSION = 2;
3536

3637
/// Function pointer definition for libra_abi_version
3738
pub type PFN_libra_instance_abi_version = extern "C" fn() -> LIBRASHADER_ABI_VERSION;

librashader-runtime-gl/src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub enum FilterChainError {
3434
GlSamplerError,
3535
#[error("opengl could not create samplers")]
3636
GlProgramError,
37+
#[error("an invalid framebuffer was provided to frame")]
38+
GlInvalidFramebuffer,
3739
#[error("opengl error: {0}")]
3840
GlError(String),
3941
}

librashader-runtime-gl/src/filter_chain/chain.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<T: GLInterface> FilterChainImpl<T> {
132132
/// Load a filter chain from a pre-parsed `ShaderPreset`.
133133
pub(crate) unsafe fn load_from_preset(
134134
preset: ShaderPreset,
135-
context: glow::Context,
135+
context: Arc<glow::Context>,
136136
options: Option<&FilterChainOptionsGL>,
137137
) -> error::Result<Self> {
138138
let disable_cache = options.map_or(false, |o| o.disable_cache);
@@ -156,7 +156,6 @@ impl<T: GLInterface> FilterChainImpl<T> {
156156
// load luts
157157
let luts = T::LoadLut::load_luts(&context, &preset.textures)?;
158158

159-
let context = Arc::new(context);
160159
let framebuffer_gen = || T::FramebufferInterface::new(&context, 1);
161160
let input_gen = || InputTexture {
162161
image: Default::default(),

librashader-runtime-gl/src/filter_chain/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct FilterChainGL {
2424
impl FilterChainGL {
2525
/// Load a filter chain from a pre-parsed `ShaderPreset`.
2626
pub unsafe fn load_from_preset(
27-
ctx: glow::Context,
27+
ctx: Arc<glow::Context>,
2828
preset: ShaderPreset,
2929
options: Option<&FilterChainOptionsGL>,
3030
) -> Result<Self> {
@@ -47,7 +47,7 @@ impl FilterChainGL {
4747

4848
/// Load the shader preset at the given path into a filter chain.
4949
pub unsafe fn load_from_path(
50-
ctx: glow::Context,
50+
ctx: Arc<glow::Context>,
5151
path: impl AsRef<Path>,
5252
options: Option<&FilterChainOptionsGL>,
5353
) -> Result<Self> {

0 commit comments

Comments
 (0)