Skip to content

Commit

Permalink
graphics: optional color render pass color attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
not-fl3 committed Nov 1, 2023
1 parent aa38a5b commit f9899b5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,9 +1156,13 @@ pub trait RenderingBackend {
height: i32,
bytes: &[u8],
);
fn new_render_pass(&mut self, color_img: TextureId, depth_img: Option<TextureId>)
-> RenderPass;
fn render_pass_texture(&self, render_pass: RenderPass) -> TextureId;
fn new_render_pass(
&mut self,
color_img: Option<TextureId>,
depth_img: Option<TextureId>,
) -> RenderPass;
/// for depth-only render pass returns None
fn render_pass_texture(&self, render_pass: RenderPass) -> Option<TextureId>;
fn delete_render_pass(&mut self, render_pass: RenderPass);
fn new_pipeline(
&mut self,
Expand Down
37 changes: 24 additions & 13 deletions src/graphics/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ fn get_uniform_location(program: GLuint, name: &str) -> Option<i32> {

pub(crate) struct RenderPassInternal {
gl_fb: GLuint,
texture: TextureId,
texture: Option<TextureId>,
depth_texture: Option<TextureId>,
}

Expand Down Expand Up @@ -912,21 +912,26 @@ impl RenderingBackend for GlContext {

fn new_render_pass(
&mut self,
color_img: TextureId,
color_img: Option<TextureId>,
depth_img: Option<TextureId>,
) -> RenderPass {
if color_img.is_none() && depth_img.is_none() {
panic!("Render pass should have at least one non-none target");
}
let mut gl_fb = 0;

unsafe {
glGenFramebuffers(1, &mut gl_fb as *mut _);
glBindFramebuffer(GL_FRAMEBUFFER, gl_fb);
glFramebufferTexture2D(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
self.textures.get(color_img).raw,
0,
);
if let Some(color_img) = color_img {
glFramebufferTexture2D(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
self.textures.get(color_img).raw,
0,
);
}
if let Some(depth_img) = depth_img {
glFramebufferTexture2D(
GL_FRAMEBUFFER,
Expand All @@ -948,15 +953,18 @@ impl RenderingBackend for GlContext {

RenderPass(self.passes.len() - 1)
}
fn render_pass_texture(&self, render_pass: RenderPass) -> TextureId {
// for depth-only render pass will return None
fn render_pass_texture(&self, render_pass: RenderPass) -> Option<TextureId> {
self.passes[render_pass.0].texture
}
fn delete_render_pass(&mut self, render_pass: RenderPass) {
let render_pass = &mut self.passes[render_pass.0];

unsafe { glDeleteFramebuffers(1, &mut render_pass.gl_fb as *mut _) }

self.textures.get(render_pass.texture).delete();
if let Some(color_texture) = render_pass.texture {
self.textures.get(color_texture).delete();
}
if let Some(depth_texture) = render_pass.depth_texture {
self.textures.get(depth_texture).delete();
}
Expand Down Expand Up @@ -1416,10 +1424,13 @@ impl RenderingBackend for GlContext {
}
Some(pass) => {
let pass = &self.passes[pass.0];
// new_render_pass will panic with both color and depth components none
// so unwrap is safe here
let texture = pass.texture.or(pass.depth_texture).unwrap();
(
pass.gl_fb,
self.textures.get(pass.texture).params.width as i32,
self.textures.get(pass.texture).params.height as i32,
self.textures.get(texture).params.width as i32,
self.textures.get(texture).params.height as i32,
)
}
};
Expand Down
8 changes: 5 additions & 3 deletions src/graphics/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,11 @@ impl RenderingBackend for MetalContext {

fn new_render_pass(
&mut self,
color_img: TextureId,
color_img: Option<TextureId>,
depth_img: Option<TextureId>,
) -> RenderPass {
let color_img = color_img.expect("color_img: None not yet implemented");

unsafe {
let render_pass_desc =
msg_send_![class!(MTLRenderPassDescriptor), renderPassDescriptor];
Expand Down Expand Up @@ -576,8 +578,8 @@ impl RenderingBackend for MetalContext {
}
}

fn render_pass_texture(&self, render_pass: RenderPass) -> TextureId {
self.passes[render_pass.0].texture
fn render_pass_texture(&self, render_pass: RenderPass) -> Option<TextureId> {
Some(self.passes[render_pass.0].texture)
}

fn new_buffer(&mut self, _: BufferType, _usage: BufferUsage, data: BufferSource) -> BufferId {
Expand Down

0 comments on commit f9899b5

Please sign in to comment.