Skip to content

Commit e7fe965

Browse files
committed
rt(gl): ensure framebuffers are bound
1 parent 97ad0d6 commit e7fe965

File tree

10 files changed

+82
-41
lines changed

10 files changed

+82
-41
lines changed

librashader-runtime-gl/src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use thiserror::Error;
1010
#[derive(Error, Debug)]
1111
#[non_exhaustive]
1212
pub enum FilterChainError {
13-
#[error("fbo initialization error")]
13+
#[error("fbo initialization error {0:x}")]
1414
FramebufferInit(u32),
1515
#[error("SPIRV reflection error")]
1616
SpirvCrossReflectError(#[from] spirv_cross2::SpirvCrossError),

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<T: GLInterface> FilterChainImpl<T> {
377377
&original,
378378
&source,
379379
RenderTarget::identity(target)?,
380-
);
380+
)?;
381381

382382
let target = target.as_texture(pass.config.filter, pass.config.wrap_mode);
383383
self.common.output_textures[index] = target;
@@ -409,7 +409,7 @@ impl<T: GLInterface> FilterChainImpl<T> {
409409
&original,
410410
&source,
411411
RenderTarget::viewport_with_output(target, viewport),
412-
);
412+
)?;
413413
}
414414

415415
pass.draw(
@@ -421,7 +421,7 @@ impl<T: GLInterface> FilterChainImpl<T> {
421421
&original,
422422
&source,
423423
RenderTarget::viewport_with_output(final_viewport, viewport),
424-
);
424+
)?;
425425
self.common.output_textures[passes_len - 1] = viewport
426426
.output
427427
.as_texture(pass.config.filter, pass.config.wrap_mode);

librashader-runtime-gl/src/filter_pass.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::filter_chain::FilterCommon;
1515
use crate::gl::{BindTexture, GLFramebuffer, GLInterface, UboRing};
1616
use crate::options::FrameOptionsGL;
1717
use crate::samplers::SamplerSet;
18-
use crate::GLImage;
18+
use crate::{error, GLImage};
1919

2020
use crate::texture::InputTexture;
2121

@@ -86,17 +86,15 @@ impl<T: GLInterface> FilterPass<T> {
8686
original: &InputTexture,
8787
source: &InputTexture,
8888
output: RenderTarget<GLFramebuffer, i32>,
89-
) {
89+
) -> error::Result<()> {
9090
let framebuffer = output.output;
9191

9292
if self.config.mipmap_input && !parent.disable_mipmaps {
9393
T::BindTexture::gen_mipmaps(&parent.context, source);
9494
}
9595

9696
unsafe {
97-
parent
98-
.context
99-
.bind_framebuffer(glow::FRAMEBUFFER, Some(framebuffer.fbo));
97+
framebuffer.bind::<T::FramebufferInterface>()?;
10098
parent.context.use_program(Some(self.program));
10199
}
102100

@@ -154,6 +152,8 @@ impl<T: GLInterface> FilterPass<T> {
154152
parent.context.disable(glow::FRAMEBUFFER_SRGB);
155153
parent.context.bind_framebuffer(glow::FRAMEBUFFER, None);
156154
}
155+
156+
Ok(())
157157
}
158158
}
159159

librashader-runtime-gl/src/gl/framebuffer.rs

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ impl GLFramebuffer {
8787
wrap_mode,
8888
}
8989
}
90+
91+
pub(crate) fn bind<T: FramebufferInterface>(&self) -> Result<()> {
92+
T::bind(self)
93+
}
9094
}
9195

9296
/// A state-checked wrapper around a raw framebuffer, used exclusively for output images.

librashader-runtime-gl/src/gl/gl3/framebuffer.rs

+19
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,23 @@ impl FramebufferInterface for Gl3Framebuffer {
290290

291291
Ok(())
292292
}
293+
294+
fn bind(fb: &GLFramebuffer) -> Result<()> {
295+
unsafe {
296+
fb.ctx.bind_framebuffer(glow::FRAMEBUFFER, Some(fb.fbo));
297+
fb.ctx.framebuffer_texture_2d(
298+
glow::FRAMEBUFFER,
299+
glow::COLOR_ATTACHMENT0,
300+
glow::TEXTURE_2D,
301+
fb.image,
302+
0,
303+
);
304+
let status = fb.ctx.check_framebuffer_status(glow::FRAMEBUFFER);
305+
if status != glow::FRAMEBUFFER_COMPLETE {
306+
return Err(FilterChainError::FramebufferInit(status));
307+
}
308+
}
309+
310+
Ok(())
311+
}
293312
}

librashader-runtime-gl/src/gl/gl46/framebuffer.rs

+12
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,16 @@ impl FramebufferInterface for Gl46Framebuffer {
236236
}
237237
Ok(())
238238
}
239+
240+
fn bind(fb: &GLFramebuffer) -> Result<()> {
241+
unsafe {
242+
fb.ctx.bind_framebuffer(glow::FRAMEBUFFER, Some(fb.fbo));
243+
let status = fb.ctx.check_framebuffer_status(glow::FRAMEBUFFER);
244+
if status != glow::FRAMEBUFFER_COMPLETE {
245+
return Err(FilterChainError::FramebufferInit(status));
246+
}
247+
}
248+
249+
Ok(())
250+
}
239251
}

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ pub(crate) trait FramebufferInterface {
120120

121121
let size = source_size.scale_viewport(scaling, *viewport_size, *original_size);
122122

123-
if fb.size != size || (mipmap && fb.max_levels == 1) || (!mipmap && fb.max_levels != 1) {
123+
if fb.size != size
124+
|| (mipmap && fb.max_levels == 1)
125+
|| (!mipmap && fb.max_levels != 1)
126+
|| fb.image.is_none()
127+
{
124128
fb.size = size;
125129

126130
if mipmap {
@@ -145,6 +149,7 @@ pub(crate) trait FramebufferInterface {
145149
fn clear<const REBIND: bool>(fb: &GLFramebuffer);
146150
fn copy_from(fb: &mut GLFramebuffer, image: &GLImage) -> Result<()>;
147151
fn init(fb: &mut GLFramebuffer, size: Size<u32>, format: impl Into<u32>) -> Result<()>;
152+
fn bind(fb: &GLFramebuffer) -> Result<()>;
148153
}
149154

150155
pub(crate) trait BindTexture {

librashader-runtime-gl/tests/hello_triangle/gl3.rs

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::mpsc::Receiver;
22
use std::sync::Arc;
33

4-
use glfw::{Context, Glfw, Window, WindowEvent};
4+
use glfw::{fail_on_errors, Context, Glfw, GlfwReceiver, PWindow, Window, WindowEvent};
55

66
use glow::HasContext;
77
use librashader_common::{GetSize, Size, Viewport};
@@ -14,13 +14,13 @@ const TITLE: &str = "librashader OpenGL 3.3";
1414

1515
pub fn setup() -> (
1616
Glfw,
17-
Window,
18-
Receiver<(f64, WindowEvent)>,
17+
PWindow,
18+
GlfwReceiver<(f64, WindowEvent)>,
1919
glow::Program,
2020
glow::VertexArray,
2121
Arc<glow::Context>,
2222
) {
23-
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
23+
let mut glfw = glfw::init(fail_on_errors!()).unwrap();
2424
glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3));
2525
glfw.window_hint(glfw::WindowHint::OpenGlProfile(
2626
glfw::OpenGlProfileHint::Core,
@@ -171,8 +171,8 @@ void main()
171171
pub fn do_loop(
172172
gl: &Arc<glow::Context>,
173173
mut glfw: Glfw,
174-
mut window: Window,
175-
events: Receiver<(f64, WindowEvent)>,
174+
mut window: PWindow,
175+
events: GlfwReceiver<(f64, WindowEvent)>,
176176
triangle_program: glow::Program,
177177
triangle_vao: glow::VertexArray,
178178
filter: &mut FilterChainGL,
@@ -183,7 +183,7 @@ pub fn do_loop(
183183
let quad_vbuf;
184184

185185
let output_texture;
186-
let output_framebuffer_handle;
186+
// let output_framebuffer_handle;
187187
let output_quad_vbuf;
188188

189189
unsafe {
@@ -270,10 +270,11 @@ pub fn do_loop(
270270
}
271271

272272
unsafe {
273+
gl.bind_framebuffer(glow::FRAMEBUFFER, None);
273274
// do frmaebuffer
274-
output_framebuffer_handle = gl.create_framebuffer().unwrap();
275-
276-
gl.bind_framebuffer(glow::FRAMEBUFFER, Some(output_framebuffer_handle));
275+
// output_framebuffer_handle = gl.create_framebuffer().unwrap();
276+
//
277+
// gl.bind_framebuffer(glow::FRAMEBUFFER, Some(output_framebuffer_handle));
277278

278279
// glow::ObjectLabel(
279280
// glow::FRAMEBUFFER,
@@ -324,18 +325,18 @@ pub fn do_loop(
324325
);
325326

326327
// set color attachment
327-
gl.framebuffer_texture_2d(
328-
glow::FRAMEBUFFER,
329-
glow::COLOR_ATTACHMENT0,
330-
glow::TEXTURE_2D,
331-
Some(output_texture),
332-
0,
333-
);
328+
// gl.framebuffer_texture_2d(
329+
// glow::FRAMEBUFFER,
330+
// glow::COLOR_ATTACHMENT0,
331+
// glow::TEXTURE_2D,
332+
// Some(output_texture),
333+
// 0,
334+
// );
334335

335-
gl.draw_buffer(glow::COLOR_ATTACHMENT0);
336-
if gl.check_framebuffer_status(glow::FRAMEBUFFER) != glow::FRAMEBUFFER_COMPLETE {
337-
panic!("failed to create fbo")
338-
}
336+
// gl.draw_buffer(glow::COLOR_ATTACHMENT0);
337+
// if gl.check_framebuffer_status(glow::FRAMEBUFFER) != glow::FRAMEBUFFER_COMPLETE {
338+
// panic!("failed to create fbo")
339+
// }
339340

340341
let fullscreen_fbo = [
341342
-1.0f32, -1.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0,

librashader-runtime-gl/tests/hello_triangle/gl46.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::mpsc::Receiver;
22
use std::sync::Arc;
33

4-
use glfw::{Context, Glfw, Window, WindowEvent};
4+
use glfw::{fail_on_errors, Context, Glfw, GlfwReceiver, PWindow, Window, WindowEvent};
55

66
use glow::HasContext;
77
use librashader_common::{GetSize, Size, Viewport};
@@ -14,13 +14,13 @@ const TITLE: &str = "librashader OpenGL 4.6";
1414

1515
pub fn setup() -> (
1616
Glfw,
17-
Window,
18-
Receiver<(f64, WindowEvent)>,
17+
PWindow,
18+
GlfwReceiver<(f64, WindowEvent)>,
1919
glow::Program,
2020
glow::VertexArray,
2121
Arc<glow::Context>,
2222
) {
23-
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
23+
let mut glfw = glfw::init(fail_on_errors!()).unwrap();
2424
glfw.window_hint(glfw::WindowHint::ContextVersion(4, 6));
2525
glfw.window_hint(glfw::WindowHint::OpenGlProfile(
2626
glfw::OpenGlProfileHint::Core,
@@ -155,8 +155,8 @@ void main()
155155
pub fn do_loop(
156156
gl: &Arc<glow::Context>,
157157
mut glfw: Glfw,
158-
mut window: Window,
159-
events: Receiver<(f64, WindowEvent)>,
158+
mut window: PWindow,
159+
events: GlfwReceiver<(f64, WindowEvent)>,
160160
triangle_program: glow::Program,
161161
triangle_vao: glow::VertexArray,
162162
filter: &mut FilterChainGL,

librashader-runtime-gl/tests/triangle.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ fn triangle_gl() {
1111
unsafe {
1212
let mut filter = FilterChainGL::load_from_path(
1313
// "../test/basic.slangp",
14-
"../test/shaders_slang/test/feedback.slangp",
14+
"../test/shaders_slang/crt/crt-royale.slangp",
1515
Arc::clone(&context),
1616
Some(&FilterChainOptionsGL {
1717
glsl_version: 0,
1818
use_dsa: false,
1919
force_no_mipmaps: false,
20-
disable_cache: false,
20+
disable_cache: true,
2121
}),
2222
)
2323
// FilterChain::load_from_path("../test/slang-shaders/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", None)
24-
.unwrap();
24+
.expect("Failed to load filter chain");
2525
hello_triangle::gl3::do_loop(&context, glfw, window, events, shader, vao, &mut filter);
2626
}
2727
}
@@ -35,7 +35,7 @@ fn triangle_gl46() {
3535
// "../test/slang-shaders/test/history.slangp",
3636
// "../test/basic.slangp",
3737
// "../test/shaders_slang/crt/crt-royale.slangp",
38-
"../test/shaders_slang/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp",
38+
"../test/shaders_slang/crt/crt-royale.slangp",
3939
Arc::clone(&context),
4040
Some(&FilterChainOptionsGL {
4141
glsl_version: 330,

0 commit comments

Comments
 (0)