rt(mtl): fix faulty history but only copying one mipmap slice of texture

rt(mtl): push history before doing writing

This does make the metal backend one frame behind compared to the other stuff but eh
This commit is contained in:
chyyran 2024-09-09 03:00:26 -04:00 committed by Ronny Chan
parent fe48fd03a5
commit cebc7a939a
3 changed files with 18 additions and 27 deletions

View file

@ -27,7 +27,7 @@ use librashader_runtime::quad::QuadType;
use librashader_runtime::render_target::RenderTarget; use librashader_runtime::render_target::RenderTarget;
use librashader_runtime::scaling::ScaleFramebuffer; use librashader_runtime::scaling::ScaleFramebuffer;
use librashader_runtime::uniforms::UniformStorage; use librashader_runtime::uniforms::UniformStorage;
use objc2::rc::Id; use objc2::rc::{Id, Retained};
use objc2::runtime::ProtocolObject; use objc2::runtime::ProtocolObject;
use objc2_foundation::NSString; use objc2_foundation::NSString;
use objc2_metal::{ use objc2_metal::{
@ -204,8 +204,8 @@ impl FilterChainMetal {
fn push_history( fn push_history(
&mut self, &mut self,
input: &ProtocolObject<dyn MTLTexture>,
cmd: &ProtocolObject<dyn MTLCommandBuffer>, cmd: &ProtocolObject<dyn MTLCommandBuffer>,
input: &ProtocolObject<dyn MTLTexture>,
) -> error::Result<()> { ) -> error::Result<()> {
let mipmapper = cmd let mipmapper = cmd
.blitCommandEncoder() .blitCommandEncoder()
@ -227,10 +227,11 @@ impl FilterChainMetal {
} }
back.copy_from(&mipmapper, input)?; back.copy_from(&mipmapper, input)?;
self.history_framebuffers.push_front(back);
}
mipmapper.endEncoding(); mipmapper.endEncoding();
self.history_framebuffers.push_front(back);
} else {
mipmapper.endEncoding();
}
Ok(()) Ok(())
} }
@ -326,20 +327,7 @@ impl FilterChainMetal {
options: Option<&FrameOptionsMetal>, options: Option<&FrameOptionsMetal>,
) -> error::Result<()> { ) -> error::Result<()> {
let max = std::cmp::min(self.passes.len(), self.common.config.passes_enabled()); let max = std::cmp::min(self.passes.len(), self.common.config.passes_enabled());
let passes = &mut self.passes[0..max];
if let Some(options) = &options { if let Some(options) = &options {
let desc = unsafe {
let desc = MTLRenderPassDescriptor::new();
desc.colorAttachments()
.objectAtIndexedSubscript(0)
.setLoadAction(MTLLoadAction::Clear);
desc.colorAttachments()
.objectAtIndexedSubscript(0)
.setStoreAction(MTLStoreAction::DontCare);
desc
};
let clear_desc = unsafe { MTLRenderPassDescriptor::new() }; let clear_desc = unsafe { MTLRenderPassDescriptor::new() };
if options.clear_history { if options.clear_history {
for (index, history) in self.history_framebuffers.iter().enumerate() { for (index, history) in self.history_framebuffers.iter().enumerate() {
@ -352,13 +340,17 @@ impl FilterChainMetal {
ca.setStoreAction(MTLStoreAction::Store); ca.setStoreAction(MTLStoreAction::Store);
} }
} }
}
let clearpass = cmd let clearpass = cmd
.renderCommandEncoderWithDescriptor(&desc) .renderCommandEncoderWithDescriptor(&clear_desc)
.ok_or(FilterChainError::FailedToCreateCommandBuffer)?; .ok_or(FilterChainError::FailedToCreateCommandBuffer)?;
clearpass.endEncoding(); clearpass.endEncoding();
} }
}
self.push_history(&cmd, &input)?;
let passes = &mut self.passes[0..max];
if passes.is_empty() { if passes.is_empty() {
return Ok(()); return Ok(());
} }
@ -482,7 +474,6 @@ impl FilterChainMetal {
)?; )?;
} }
self.push_history(&input, &cmd)?;
self.common.internal_frame_count = self.common.internal_frame_count.wrapping_add(1); self.common.internal_frame_count = self.common.internal_frame_count.wrapping_add(1);
Ok(()) Ok(())
} }

View file

@ -28,5 +28,5 @@ fn select_optimal_pixel_format(format: MTLPixelFormat) -> MTLPixelFormat {
if format == MTLPixelFormat::RGBA8Unorm_sRGB { if format == MTLPixelFormat::RGBA8Unorm_sRGB {
return MTLPixelFormat::BGRA8Unorm_sRGB; return MTLPixelFormat::BGRA8Unorm_sRGB;
} }
return format; format
} }

View file

@ -70,11 +70,11 @@ impl OwnedTexture {
1 1
}); });
descriptor.setStorageMode(MTLStorageMode::Private);
descriptor.setUsage( descriptor.setUsage(
MTLTextureUsage::ShaderRead MTLTextureUsage::ShaderRead
| MTLTextureUsage::ShaderWrite | MTLTextureUsage::ShaderWrite
| MTLTextureUsage::RenderTarget, | MTLTextureUsage::RenderTarget
| MTLTextureUsage::PixelFormatView,
); );
descriptor descriptor
@ -106,7 +106,7 @@ impl OwnedTexture {
|| (!mipmap && self.max_miplevels != 1) || (!mipmap && self.max_miplevels != 1)
|| format != select_optimal_pixel_format(format) || format != select_optimal_pixel_format(format)
{ {
let mut new = OwnedTexture::new(device, size, self.max_miplevels, format)?; let mut new = OwnedTexture::new(device, size, self.max_miplevels, select_optimal_pixel_format(format))?;
std::mem::swap(self, &mut new); std::mem::swap(self, &mut new);
} }
Ok(size) Ok(size)