@@ -7,12 +7,14 @@ use librashader::runtime::gl::{
7
7
FilterChain , FilterChainOptions , FrameOptions , GLFramebuffer , GLImage ,
8
8
} ;
9
9
use std:: ffi:: CStr ;
10
- use std:: ffi:: { c_char, c_void, CString } ;
10
+ use std:: ffi:: { c_char, c_void} ;
11
11
use std:: mem:: MaybeUninit ;
12
+ use std:: num:: NonZeroU32 ;
12
13
use std:: ptr:: NonNull ;
13
14
use std:: slice;
14
-
15
+ use std :: sync :: Arc ;
15
16
use crate :: LIBRASHADER_API_VERSION ;
17
+ use librashader:: runtime:: gl:: error:: FilterChainError ;
16
18
use librashader:: runtime:: FilterChainParameters ;
17
19
use librashader:: runtime:: { Size , Viewport } ;
18
20
@@ -49,8 +51,12 @@ pub struct libra_output_framebuffer_gl_t {
49
51
50
52
impl From < libra_source_image_gl_t > for GLImage {
51
53
fn from ( value : libra_source_image_gl_t ) -> Self {
54
+ let handle = NonZeroU32 :: try_from ( value. handle )
55
+ . ok ( )
56
+ . map ( glow:: NativeTexture ) ;
57
+
52
58
GLImage {
53
- handle : value . handle ,
59
+ handle,
54
60
format : value. format ,
55
61
size : Size :: new ( value. width , value. height ) ,
56
62
}
@@ -108,27 +114,6 @@ config_struct! {
108
114
}
109
115
}
110
116
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
-
132
117
extern_fn ! {
133
118
/// Create the filter chain given the shader preset.
134
119
///
@@ -141,6 +126,7 @@ extern_fn! {
141
126
/// - `out` must be aligned, but may be null, invalid, or uninitialized.
142
127
fn libra_gl_filter_chain_create(
143
128
preset: * mut libra_shader_preset_t,
129
+ loader: libra_gl_loader_t,
144
130
options: * const MaybeUninit <filter_chain_gl_opt_t>,
145
131
out: * mut MaybeUninit <libra_gl_filter_chain_t>
146
132
) {
@@ -160,7 +146,11 @@ extern_fn! {
160
146
let options = options. map( FromUninit :: from_uninit) ;
161
147
162
148
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( ) ) ?;
164
154
165
155
out. write( MaybeUninit :: new( NonNull :: new( Box :: into_raw( Box :: new(
166
156
chain,
@@ -206,8 +196,19 @@ extern_fn! {
206
196
} ;
207
197
208
198
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
+
211
212
let viewport = Viewport {
212
213
x: viewport. x,
213
214
y: viewport. y,
0 commit comments