Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix msaa_render_target on WebGL #487

Merged
merged 5 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/msaa_render_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ impl Stage {
sample_count: 4,
..Default::default()
});

// Without this check, new_render_pass_mrt might panic on GL2/WebGl1.
// It is recommended to handle it and create a normal,
// non-msaa render pass instead.
assert!(
ctx.info().features.resolve_attachments,
"MSAA render targets are not supported on current rendering backend!"
);
let offscreen_pass =
ctx.new_render_pass_mrt(&[color_img], Some(&[color_resolve_img]), Some(depth_img));

Expand Down Expand Up @@ -235,7 +241,7 @@ impl EventHandler for Stage {
self.ctx.apply_pipeline(&self.display_pipeline);
self.ctx.apply_bindings(&self.display_bind);
self.ctx.apply_uniforms(UniformsSource::table(&vs_params));
self.ctx.draw(0, 36, 1);
self.ctx.draw(0, 6, 1);
self.ctx.end_render_pass();

self.ctx.commit_frame();
Expand All @@ -245,6 +251,7 @@ impl EventHandler for Stage {
fn main() {
let mut conf = conf::Conf::default();
let metal = std::env::args().nth(1).as_deref() == Some("metal");
conf.platform.webgl_version = conf::WebGLVersion::WebGL2;
conf.platform.apple_gfx_api = if metal {
conf::AppleGfxApi::Metal
} else {
Expand Down
1 change: 0 additions & 1 deletion examples/quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ fn main() {
} else {
conf::AppleGfxApi::OpenGl
};
conf.platform.webgl_version = conf::WebGLVersion::WebGL2;

miniquad::start(conf, move || Box::new(Stage::new()));
}
Expand Down
27 changes: 27 additions & 0 deletions js/gl.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,9 @@ var importObject = {
glGenFramebuffers: function (n, ids) {
_glGenObject(n, ids, 'createFramebuffer', GL.framebuffers, 'glGenFramebuffers');
},
glGenRenderbuffers: function (n, ids) {
_glGenObject(n, ids, 'createRenderbuffer', GL.renderbuffers, 'glGenRenderbuffers');
},
glBindVertexArray: function (vao) {
gl.bindVertexArray(GL.vaos[vao]);
},
Expand All @@ -826,7 +829,11 @@ var importObject = {

gl.bindFramebuffer(target, GL.framebuffers[framebuffer]);
},
glBindRenderbuffer: function (target, renderbuffer) {
GL.validateGLObjectID(GL.renderbuffers, renderbuffer, 'glBindRenderbuffer', 'renderbuffer');

gl.bindRenderbuffer(target, GL.renderbuffers[renderbuffer]);
},
glGenBuffers: function (n, buffers) {
_glGenObject(n, buffers, 'createBuffer', GL.buffers, 'glGenBuffers');
},
Expand Down Expand Up @@ -1129,6 +1136,26 @@ var importObject = {
glGenerateMipmap: function (index) {
gl.generateMipmap(index);
},
glRenderbufferStorageMultisample: function(target, samples, internalformat, width, height) {
gl.renderbufferStorageMultisample(target, samples, internalformat, width, height);
},
glFramebufferRenderbuffer: function(target, attachment, renderbuffertarget, renderbuffer) {
GL.validateGLObjectID(GL.renderbuffers, renderbuffer, 'glFramebufferRenderbuffer', 'renderbuffer');
gl.framebufferRenderbuffer(target, attachment, renderbuffertarget, GL.renderbuffers[renderbuffer]);
},
glCheckFramebufferStatus: function(target) {
return gl.checkFramebufferStatus(target);
},
glReadBuffer: function(source) {
gl.readBuffer(source)
},
glBlitFramebuffer: function(srcX0, srcY0, srcX1, srcY1,
dstX0, dstY0, dstX1, dstY1,
mask, filter) {
gl.blitFramebuffer(srcX0, srcY0, srcX1, srcY1,
dstX0, dstY0, dstX1, dstY1,
mask, filter);
},

setup_canvas_size: function (high_dpi) {
window.high_dpi = high_dpi;
Expand Down
19 changes: 16 additions & 3 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ pub struct TextureParams {
/// On OpenGL, for a `sample_count > 1` render texture, render buffer object will
/// be created instead of a regulat texture.
///
/// The only way to use
/// The only way to use
pub sample_count: i32,
}

Expand Down Expand Up @@ -569,11 +569,21 @@ pub const MAX_SHADERSTAGE_IMAGES: usize = 12;
#[derive(Clone, Debug)]
pub struct Features {
pub instancing: bool,
/// Does current rendering backend support automatic resolve of
/// multisampled render passes on end_render_pass.
/// Would be false on WebGl1 and GL2.
///
/// With resolve_attachments: false, not-none resolve_img in new_render_pass will
/// result in a runtime panic.
pub resolve_attachments: bool,
}

impl Default for Features {
fn default() -> Features {
Features { instancing: true }
Features {
instancing: true,
resolve_attachments: true,
}
}
}

Expand Down Expand Up @@ -1203,7 +1213,10 @@ pub trait RenderingBackend {
/// Same as "new_render_pass", but allows multiple color attachments.
/// if `resolve_img` is set, MSAA-resolve operation will happen in `end_render_pass`
/// this operation require `color_img` to have sample_count > 1,resolve_img have
/// sample_count == 1, and color_img.len() should be equal to resolve_img.len()
/// sample_count == 1, and color_img.len() should be equal to resolve_img.len()
///
/// Note that resolve attachments may be not supported by current backend!
/// They are only available when `ctx.info().features.resolve_attachments` is true.
fn new_render_pass_mrt(
&mut self,
color_img: &[TextureId],
Expand Down
36 changes: 30 additions & 6 deletions src/graphics/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ struct Texture {
params: TextureParams,
}

impl TextureFormat {
fn sized_internal_format(&self) -> GLenum {
match self {
TextureFormat::RGB8 => GL_RGB8,
TextureFormat::RGBA8 => GL_RGBA8,
TextureFormat::RGBA16F => GL_RGBA16F,
TextureFormat::Depth => GL_DEPTH_COMPONENT16,
TextureFormat::Depth32 => GL_DEPTH_COMPONENT32,
#[cfg(target_arch = "wasm32")]
TextureFormat::Alpha => GL_ALPHA,
#[cfg(not(target_arch = "wasm32"))]
TextureFormat::Alpha => GL_R8,
}
}
}

/// Converts from TextureFormat to (internal_format, format, pixel_type)
impl From<TextureFormat> for (GLenum, GLenum, GLenum) {
fn from(format: TextureFormat) -> Self {
Expand Down Expand Up @@ -160,6 +176,7 @@ impl Texture {
unsafe {
glGenRenderbuffers(1, &mut renderbuffer as *mut _);
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer as _);
let internal_format = params.format.sized_internal_format();
glRenderbufferStorageMultisample(
GL_RENDERBUFFER,
params.sample_count,
Expand Down Expand Up @@ -484,11 +501,7 @@ impl GlContext {

glGenVertexArrays(1, &mut vao as *mut _);
glBindVertexArray(vao);
let features = Features {
instancing: !crate::native::gl::is_gl2(),
..Default::default()
};
let info = gl_info(features);
let info = gl_info();
GlContext {
default_framebuffer,
shaders: ResourceManager::default(),
Expand Down Expand Up @@ -765,14 +778,25 @@ impl GlContext {
}
}

fn gl_info(features: Features) -> ContextInfo {
fn gl_info() -> ContextInfo {
let version_string = unsafe { glGetString(super::gl::GL_VERSION) };
let gl_version_string = unsafe { std::ffi::CStr::from_ptr(version_string as _) }
.to_str()
.unwrap()
.to_string();
//let gles2 = !gles3 && gl_version_string.contains("OpenGL ES");

let gl2 = gl_version_string.is_empty()
|| gl_version_string.starts_with("2")
|| gl_version_string.starts_with("OpenGL ES 2");
let webgl1 = gl_version_string == "WebGL 1.0";

let features = Features {
instancing: !gl2,
resolve_attachments: !webgl1 && !gl2,
..Default::default()
};

let mut glsl_support = GlslSupport::default();

// this is not quite documented,
Expand Down
5 changes: 4 additions & 1 deletion src/graphics/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,10 @@ impl RenderingBackend for MetalContext {
backend: Backend::Metal,
gl_version_string: Default::default(),
glsl_support: Default::default(),
features: Features { instancing: true },
features: Features {
instancing: true,
resolve_attachments: false,
},
}
}
fn buffer_size(&mut self, buffer: BufferId) -> usize {
Expand Down
2 changes: 2 additions & 0 deletions src/native/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ pub const GL_LEQUAL: u32 = 0x0203;
pub const GL_STENCIL_TEST: u32 = 0x0B90;
pub const GL_DITHER: u32 = 0x0BD0;
pub const GL_DEPTH_COMPONENT16: u32 = 0x81A5;
pub const GL_DEPTH_COMPONENT24: u32 = 0x81A6;
pub const GL_DEPTH_COMPONENT32: u32 = 0x81A7;
pub const GL_EQUAL: u32 = 0x0202;
pub const GL_FRAMEBUFFER: u32 = 0x8D40;
pub const GL_RGB5: u32 = 0x8050;
Expand Down
2 changes: 2 additions & 0 deletions src/native/wasm/webgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ pub const GL_LEQUAL: u32 = 0x0203;
pub const GL_STENCIL_TEST: u32 = 0x0B90;
pub const GL_DITHER: u32 = 0x0BD0;
pub const GL_DEPTH_COMPONENT16: u32 = 0x81A5;
pub const GL_DEPTH_COMPONENT24: u32 = 0x81A6;
pub const GL_DEPTH_COMPONENT32: u32 = 0x81A7;
pub const GL_EQUAL: u32 = 0x0202;
pub const GL_FRAMEBUFFER: u32 = 0x8D40;
pub const GL_RGB5: u32 = 0x8050;
Expand Down
Loading