2022-11-29 23:56:10 -05:00
|
|
|
use crate::error::{FilterChainError, Result};
|
2022-11-30 19:05:24 -05:00
|
|
|
use crate::framebuffer::GLImage;
|
2022-11-30 17:21:32 -05:00
|
|
|
use crate::gl::framebuffer::Framebuffer;
|
|
|
|
use crate::gl::FramebufferInterface;
|
2022-11-29 23:56:10 -05:00
|
|
|
use crate::texture::Texture;
|
2022-11-30 19:05:24 -05:00
|
|
|
use crate::viewport::Viewport;
|
2022-11-30 01:38:05 -05:00
|
|
|
use gl::types::{GLenum, GLint, GLsizei, GLuint};
|
2022-11-30 19:05:24 -05:00
|
|
|
use librashader_common::{ImageFormat, Size};
|
2022-11-30 01:38:05 -05:00
|
|
|
use librashader_presets::Scale2D;
|
2022-12-21 21:13:35 -05:00
|
|
|
use librashader_runtime::scaling::{MipmapSize, ViewportSize};
|
2022-11-29 23:56:10 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2022-11-30 17:21:32 -05:00
|
|
|
pub struct Gl46Framebuffer;
|
2022-11-30 00:39:42 -05:00
|
|
|
|
2022-11-30 17:21:32 -05:00
|
|
|
impl FramebufferInterface for Gl46Framebuffer {
|
|
|
|
fn new(max_levels: u32) -> Framebuffer {
|
2022-11-29 23:56:10 -05:00
|
|
|
let mut framebuffer = 0;
|
|
|
|
unsafe {
|
2022-11-30 00:39:42 -05:00
|
|
|
gl::CreateFramebuffers(1, &mut framebuffer);
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:21:32 -05:00
|
|
|
Framebuffer {
|
2022-11-29 23:56:10 -05:00
|
|
|
image: 0,
|
|
|
|
size: Size {
|
|
|
|
width: 1,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
format: 0,
|
|
|
|
max_levels,
|
2022-11-30 17:21:32 -05:00
|
|
|
mip_levels: 0,
|
2022-11-29 23:56:10 -05:00
|
|
|
handle: framebuffer,
|
|
|
|
is_raw: false,
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 02:32:21 -05:00
|
|
|
|
2022-11-29 23:56:10 -05:00
|
|
|
fn scale(
|
2022-11-30 17:21:32 -05:00
|
|
|
fb: &mut Framebuffer,
|
2022-11-29 23:56:10 -05:00
|
|
|
scaling: Scale2D,
|
|
|
|
format: ImageFormat,
|
2022-11-30 17:21:32 -05:00
|
|
|
viewport: &Viewport,
|
2022-11-29 23:56:10 -05:00
|
|
|
_original: &Texture,
|
|
|
|
source: &Texture,
|
2023-01-11 19:09:15 -05:00
|
|
|
mipmap: bool,
|
2022-11-29 23:56:10 -05:00
|
|
|
) -> Result<Size<u32>> {
|
2022-11-30 17:21:32 -05:00
|
|
|
if fb.is_raw {
|
|
|
|
return Ok(fb.size);
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
|
|
|
|
2022-12-21 21:39:31 -05:00
|
|
|
let size = source
|
|
|
|
.image
|
|
|
|
.size
|
|
|
|
.scale_viewport(scaling, viewport.output.size);
|
2022-11-29 23:56:10 -05:00
|
|
|
|
2023-01-11 19:09:15 -05:00
|
|
|
if fb.size != size || (mipmap && fb.max_levels == 1) || (!mipmap && fb.max_levels != 1) {
|
2022-11-30 17:21:32 -05:00
|
|
|
fb.size = size;
|
2022-11-29 23:56:10 -05:00
|
|
|
|
2023-01-11 19:09:15 -05:00
|
|
|
if mipmap {
|
|
|
|
fb.max_levels = u32::MAX;
|
|
|
|
} else {
|
|
|
|
fb.max_levels = 1
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:21:32 -05:00
|
|
|
Self::init(
|
|
|
|
fb,
|
2022-11-29 23:56:10 -05:00
|
|
|
size,
|
|
|
|
if format == ImageFormat::Unknown {
|
|
|
|
ImageFormat::R8G8B8A8Unorm
|
|
|
|
} else {
|
|
|
|
format
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Ok(size)
|
|
|
|
}
|
2022-11-30 17:21:32 -05:00
|
|
|
fn clear<const REBIND: bool>(fb: &Framebuffer) {
|
2022-11-29 23:56:10 -05:00
|
|
|
unsafe {
|
2022-11-30 01:38:05 -05:00
|
|
|
gl::ClearNamedFramebufferfv(
|
2022-11-30 17:21:32 -05:00
|
|
|
fb.handle,
|
2022-11-30 01:38:05 -05:00
|
|
|
gl::COLOR,
|
|
|
|
0,
|
|
|
|
[0.0f32, 0.0, 0.0, 0.0].as_ptr().cast(),
|
|
|
|
);
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
|
|
|
}
|
2022-11-30 17:21:32 -05:00
|
|
|
fn copy_from(fb: &mut Framebuffer, image: &GLImage) -> Result<()> {
|
2022-11-30 17:59:55 -05:00
|
|
|
// todo: confirm this behaviour for unbound image.
|
|
|
|
if image.handle == 0 {
|
2022-11-30 19:05:24 -05:00
|
|
|
return Ok(());
|
2022-11-30 17:59:55 -05:00
|
|
|
}
|
|
|
|
|
2022-11-29 23:56:10 -05:00
|
|
|
// todo: may want to use a shader and draw a quad to be faster.
|
2022-11-30 17:21:32 -05:00
|
|
|
if image.size != fb.size || image.format != fb.format {
|
2022-11-30 19:05:24 -05:00
|
|
|
Self::init(fb, image.size, image.format)?;
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
2022-11-30 17:21:32 -05:00
|
|
|
// gl::NamedFramebufferDrawBuffer(fb.handle, gl::COLOR_ATTACHMENT1);
|
2022-11-30 00:39:42 -05:00
|
|
|
gl::NamedFramebufferReadBuffer(image.handle, gl::COLOR_ATTACHMENT0);
|
2023-01-12 19:25:52 -05:00
|
|
|
gl::NamedFramebufferDrawBuffer(fb.handle, gl::COLOR_ATTACHMENT0);
|
2022-11-29 23:56:10 -05:00
|
|
|
|
2022-11-30 01:38:05 -05:00
|
|
|
gl::BlitNamedFramebuffer(
|
|
|
|
image.handle,
|
2022-11-30 17:21:32 -05:00
|
|
|
fb.handle,
|
2022-11-30 01:38:05 -05:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
image.size.width as GLint,
|
|
|
|
image.size.height as GLint,
|
|
|
|
0,
|
|
|
|
0,
|
2022-11-30 17:21:32 -05:00
|
|
|
fb.size.width as GLint,
|
|
|
|
fb.size.height as GLint,
|
2022-11-30 01:38:05 -05:00
|
|
|
gl::COLOR_BUFFER_BIT,
|
|
|
|
gl::NEAREST,
|
|
|
|
);
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-11-30 17:21:32 -05:00
|
|
|
fn init(fb: &mut Framebuffer, mut size: Size<u32>, format: impl Into<GLenum>) -> Result<()> {
|
|
|
|
if fb.is_raw {
|
2022-11-29 23:56:10 -05:00
|
|
|
return Ok(());
|
|
|
|
}
|
2022-11-30 17:21:32 -05:00
|
|
|
fb.format = format.into();
|
|
|
|
fb.size = size;
|
2022-11-29 23:56:10 -05:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// reset the framebuffer image
|
2022-11-30 17:21:32 -05:00
|
|
|
if fb.image != 0 {
|
|
|
|
gl::NamedFramebufferTexture(fb.handle, gl::COLOR_ATTACHMENT0, 0, 0);
|
|
|
|
gl::DeleteTextures(1, &fb.image);
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:21:32 -05:00
|
|
|
gl::CreateTextures(gl::TEXTURE_2D, 1, &mut fb.image);
|
2022-11-29 23:56:10 -05:00
|
|
|
|
|
|
|
if size.width == 0 {
|
|
|
|
size.width = 1;
|
|
|
|
}
|
|
|
|
if size.height == 0 {
|
|
|
|
size.height = 1;
|
|
|
|
}
|
|
|
|
|
2022-12-21 21:13:35 -05:00
|
|
|
fb.mip_levels = size.calculate_miplevels();
|
2022-11-30 17:21:32 -05:00
|
|
|
if fb.mip_levels > fb.max_levels {
|
|
|
|
fb.mip_levels = fb.max_levels;
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
2022-11-30 17:21:32 -05:00
|
|
|
if fb.mip_levels == 0 {
|
|
|
|
fb.mip_levels = 1;
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
|
|
|
|
2022-11-30 00:39:42 -05:00
|
|
|
gl::TextureStorage2D(
|
2022-11-30 17:21:32 -05:00
|
|
|
fb.image,
|
|
|
|
fb.mip_levels as GLsizei,
|
|
|
|
fb.format,
|
2022-11-29 23:56:10 -05:00
|
|
|
size.width as GLsizei,
|
|
|
|
size.height as GLsizei,
|
|
|
|
);
|
|
|
|
|
2022-11-30 17:21:32 -05:00
|
|
|
gl::NamedFramebufferTexture(fb.handle, gl::COLOR_ATTACHMENT0, fb.image, 0);
|
2022-11-29 23:56:10 -05:00
|
|
|
|
|
|
|
let status = gl::CheckFramebufferStatus(gl::FRAMEBUFFER);
|
|
|
|
if status != gl::FRAMEBUFFER_COMPLETE {
|
|
|
|
match status {
|
|
|
|
gl::FRAMEBUFFER_UNSUPPORTED => {
|
|
|
|
eprintln!("unsupported fbo");
|
|
|
|
|
2022-11-30 17:21:32 -05:00
|
|
|
gl::NamedFramebufferTexture(fb.handle, gl::COLOR_ATTACHMENT0, 0, 0);
|
|
|
|
gl::DeleteTextures(1, &fb.image);
|
|
|
|
gl::CreateTextures(gl::TEXTURE_2D, 1, &mut fb.image);
|
2022-11-29 23:56:10 -05:00
|
|
|
|
2022-12-21 21:13:35 -05:00
|
|
|
fb.mip_levels = size.calculate_miplevels();
|
2022-11-30 17:21:32 -05:00
|
|
|
if fb.mip_levels > fb.max_levels {
|
|
|
|
fb.mip_levels = fb.max_levels;
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
2022-11-30 17:21:32 -05:00
|
|
|
if fb.mip_levels == 0 {
|
|
|
|
fb.mip_levels = 1;
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
|
|
|
|
2022-11-30 00:39:42 -05:00
|
|
|
gl::TextureStorage2D(
|
2022-11-30 17:21:32 -05:00
|
|
|
fb.image,
|
|
|
|
fb.mip_levels as GLsizei,
|
2022-11-29 23:56:10 -05:00
|
|
|
ImageFormat::R8G8B8A8Unorm.into(),
|
|
|
|
size.width as GLsizei,
|
|
|
|
size.height as GLsizei,
|
|
|
|
);
|
2022-11-30 17:21:32 -05:00
|
|
|
gl::NamedFramebufferTexture(fb.handle, gl::COLOR_ATTACHMENT0, fb.image, 0);
|
|
|
|
// fb.init =
|
2022-11-29 23:56:10 -05:00
|
|
|
// gl::CheckFramebufferStatus(gl::FRAMEBUFFER) == gl::FRAMEBUFFER_COMPLETE;
|
|
|
|
}
|
2022-11-30 01:38:05 -05:00
|
|
|
_ => return Err(FilterChainError::FramebufferInit(status)),
|
2022-11-29 23:56:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|