diff --git a/Cargo.lock b/Cargo.lock index 0fe263c..374cfa9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1953,10 +1953,7 @@ dependencies = [ "librashader-presets 0.2.0-beta.2", "librashader-reflect", "librashader-runtime", - "librashader-spirv-cross", "log", - "once_cell", - "parking_lot", "pollster", "raw-window-handle 0.6.0", "rayon", diff --git a/librashader-runtime-wgpu/Cargo.toml b/librashader-runtime-wgpu/Cargo.toml index 8a0c076..5f3c082 100644 --- a/librashader-runtime-wgpu/Cargo.toml +++ b/librashader-runtime-wgpu/Cargo.toml @@ -9,21 +9,16 @@ edition = "2021" librashader-common = { path = "../librashader-common", features = ["wgpu"], version = "0.2.0-beta.2" } librashader-presets = { path = "../librashader-presets", version = "0.2.0-beta.2" } librashader-preprocess = { path = "../librashader-preprocess", version = "0.2.0-beta.2" } -librashader-reflect = { path = "../librashader-reflect", version = "0.2.0-beta.2", features = [] } +librashader-reflect = { path = "../librashader-reflect", version = "0.2.0-beta.2", features = ["wgsl"], default-features = false } librashader-runtime = { path = "../librashader-runtime" , version = "0.2.0-beta.2" } wgpu = { version = "0.19.0", features = ["spirv"] } rustc-hash = "1.1.0" image = "0.24.7" thiserror = "1.0.50" -spirv_cross = { package = "librashader-spirv-cross", version = "0.23" } -parking_lot = "0.12.1" rayon = "1.8.0" bytemuck = { version = "1.14.0", features = ["derive"] } - array-concat = "0.5.2" -config = "0.13.4" -once_cell = "1.19.0" [dev-dependencies] config = { version = "0.13.4", features = [] } diff --git a/librashader-runtime-wgpu/src/buffer.rs b/librashader-runtime-wgpu/src/buffer.rs index ae129f3..63a203c 100644 --- a/librashader-runtime-wgpu/src/buffer.rs +++ b/librashader-runtime-wgpu/src/buffer.rs @@ -1,4 +1,3 @@ -use parking_lot::RwLock; use std::ops::{Deref, DerefMut}; use std::sync::Arc; diff --git a/librashader-runtime-wgpu/src/draw_quad.rs b/librashader-runtime-wgpu/src/draw_quad.rs index ffeb83a..5134835 100644 --- a/librashader-runtime-wgpu/src/draw_quad.rs +++ b/librashader-runtime-wgpu/src/draw_quad.rs @@ -1,7 +1,7 @@ use array_concat::concat_arrays; use librashader_runtime::quad::QuadType; use wgpu::util::{BufferInitDescriptor, DeviceExt}; -use wgpu::{Buffer, BufferAddress, BufferDescriptor, Device, Maintain, Queue, RenderPass}; +use wgpu::{Buffer, Device, RenderPass}; #[rustfmt::skip] const VBO_OFFSCREEN: [f32; 16] = [ diff --git a/librashader-runtime-wgpu/src/error.rs b/librashader-runtime-wgpu/src/error.rs index 79b6e75..4049011 100644 --- a/librashader-runtime-wgpu/src/error.rs +++ b/librashader-runtime-wgpu/src/error.rs @@ -9,8 +9,6 @@ use thiserror::Error; /// Cumulative error type for WGPU filter chains. #[derive(Error, Debug)] pub enum FilterChainError { - #[error("SPIRV reflection error")] - SpirvCrossReflectError(#[from] spirv_cross::ErrorCode), #[error("shader preset parse error")] ShaderPresetError(#[from] ParsePresetError), #[error("shader preprocess error")] diff --git a/librashader-runtime-wgpu/src/filter_chain.rs b/librashader-runtime-wgpu/src/filter_chain.rs index 2e3cbe8..bc8b3fa 100644 --- a/librashader-runtime-wgpu/src/filter_chain.rs +++ b/librashader-runtime-wgpu/src/filter_chain.rs @@ -1,12 +1,12 @@ use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; -use librashader_reflect::back::targets::{SPIRV, WGSL}; +use librashader_reflect::back::targets::WGSL; use librashader_reflect::back::{CompileReflectShader, CompileShader}; use librashader_reflect::front::GlslangCompilation; use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact}; use librashader_reflect::reflect::semantics::ShaderSemantics; use librashader_reflect::reflect::ReflectShader; use librashader_runtime::binding::BindingUtil; -use librashader_runtime::image::{Image, ImageError, UVDirection, BGRA8, RGBA8}; +use librashader_runtime::image::{Image, ImageError, UVDirection}; use librashader_runtime::quad::QuadType; use librashader_runtime::uniforms::UniformStorage; use rustc_hash::FxHashMap; @@ -33,7 +33,7 @@ use crate::luts::LutTexture; use crate::mipmap::MipmapGen; use crate::options::FrameOptionsWGPU; use crate::samplers::SamplerSet; -use crate::texture::{Handle, InputImage, OwnedImage}; +use crate::texture::{InputImage, OwnedImage}; type ShaderPassMeta = ShaderPassArtifact + Send>; @@ -46,7 +46,7 @@ fn compile_passes( Ok((passes, semantics)) } -/// A Vulkan filter chain. +/// A WGPU filter chain. pub struct FilterChainWGPU { pub(crate) common: FilterCommon, passes: Box<[FilterPass]>, @@ -54,7 +54,6 @@ pub struct FilterChainWGPU { feedback_framebuffers: Box<[OwnedImage]>, history_framebuffers: VecDeque, disable_mipmaps: bool, - // residuals: Box<[FrameResiduals]>, mipmapper: MipmapGen, } @@ -199,9 +198,7 @@ impl FilterChainWGPU { ); } - unsafe { - back.copy_from(cmd, input); - } + back.copy_from(cmd, input); self.history_framebuffers.push_front(back) } @@ -212,8 +209,6 @@ impl FilterChainWGPU { passes: Vec, semantics: &ShaderSemantics, ) -> error::Result> { - // let frames_in_flight = std::cmp::max(1, frames_in_flight); - // let filters: Vec> = passes .into_par_iter() .enumerate() @@ -365,7 +360,7 @@ impl FilterChainWGPU { source.wrap_mode = pass.config.wrap_mode; source.mip_filter = pass.config.filter; - let output_image = OutputView::new(target); + let output_image = OutputView::from(target); let out = RenderTarget::identity(&output_image); pass.draw( diff --git a/librashader-runtime-wgpu/src/filter_pass.rs b/librashader-runtime-wgpu/src/filter_pass.rs index 53a128e..066dcea 100644 --- a/librashader-runtime-wgpu/src/filter_pass.rs +++ b/librashader-runtime-wgpu/src/filter_pass.rs @@ -4,7 +4,7 @@ use crate::filter_chain::FilterCommon; use crate::framebuffer::OutputView; use crate::graphics_pipeline::WgpuGraphicsPipeline; use crate::samplers::SamplerSet; -use crate::texture::{InputImage, OwnedImage}; +use crate::texture::InputImage; use librashader_common::{ImageFormat, Size, Viewport}; use librashader_preprocess::ShaderSource; use librashader_presets::ShaderPassConfig; @@ -21,11 +21,7 @@ use librashader_runtime::render_target::RenderTarget; use librashader_runtime::uniforms::{NoUniformBinder, UniformStorage, UniformStorageAccess}; use rustc_hash::FxHashMap; use std::sync::Arc; -use wgpu::util::{BufferInitDescriptor, DeviceExt}; -use wgpu::{ - BindGroupDescriptor, BindGroupEntry, BindingResource, Buffer, BufferBinding, BufferUsages, - RenderPass, ShaderStages, TextureView, -}; +use wgpu::{BindGroupDescriptor, BindGroupEntry, BindingResource, BufferBinding, ShaderStages}; pub struct FilterPass { pub device: Arc, diff --git a/librashader-runtime-wgpu/src/framebuffer.rs b/librashader-runtime-wgpu/src/framebuffer.rs index 30cef5d..64891be 100644 --- a/librashader-runtime-wgpu/src/framebuffer.rs +++ b/librashader-runtime-wgpu/src/framebuffer.rs @@ -1,18 +1,44 @@ +use crate::handle::Handle; use crate::texture::OwnedImage; use librashader_common::Size; +use wgpu::TextureViewDescriptor; pub struct OutputView<'a> { - pub size: Size, - pub view: &'a wgpu::TextureView, - pub format: wgpu::TextureFormat, + pub(crate) size: Size, + pub(crate) view: Handle<'a, wgpu::TextureView>, + pub(crate) format: wgpu::TextureFormat, } impl<'a> OutputView<'a> { - pub fn new(image: &'a OwnedImage) -> Self { + pub fn new_from_raw( + view: &'a wgpu::TextureView, + size: Size, + format: wgpu::TextureFormat, + ) -> Self { + Self { + size, + view: Handle::Borrowed(&view), + format, + } + } +} + +impl<'a> From<&'a OwnedImage> for OutputView<'a> { + fn from(image: &'a OwnedImage) -> Self { Self { size: image.size, - view: &image.view, + view: Handle::Borrowed(&image.view), format: image.image.format(), } } } + +impl From<&wgpu::Texture> for OutputView<'static> { + fn from(image: &wgpu::Texture) -> Self { + Self { + size: image.size().into(), + view: Handle::Owned(image.create_view(&TextureViewDescriptor::default())), + format: image.format(), + } + } +} diff --git a/librashader-runtime-wgpu/src/graphics_pipeline.rs b/librashader-runtime-wgpu/src/graphics_pipeline.rs index 67d0c72..8854a61 100644 --- a/librashader-runtime-wgpu/src/graphics_pipeline.rs +++ b/librashader-runtime-wgpu/src/graphics_pipeline.rs @@ -1,5 +1,5 @@ use crate::framebuffer::OutputView; -use crate::{error, util}; +use crate::util; use librashader_reflect::back::wgsl::NagaWgslContext; use librashader_reflect::back::ShaderCompilerOutput; use librashader_reflect::reflect::ShaderReflection; @@ -7,11 +7,10 @@ use librashader_runtime::render_target::RenderTarget; use std::borrow::Cow; use std::sync::Arc; use wgpu::{ - BindGroup, BindGroupDescriptor, BindGroupLayout, BindGroupLayoutDescriptor, - BindGroupLayoutEntry, BindingType, BufferBindingType, BufferSize, CommandEncoder, Device, - Operations, PipelineLayout, PushConstantRange, RenderPass, RenderPassColorAttachment, - RenderPassDescriptor, RenderPipelineDescriptor, SamplerBindingType, ShaderModule, ShaderSource, - ShaderStages, TextureFormat, TextureSampleType, TextureViewDimension, VertexAttribute, + BindGroupLayout, BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingType, + BufferBindingType, BufferSize, CommandEncoder, Operations, PipelineLayout, PushConstantRange, + RenderPass, RenderPassColorAttachment, RenderPassDescriptor, SamplerBindingType, ShaderModule, + ShaderSource, ShaderStages, TextureFormat, TextureSampleType, TextureViewDimension, VertexBufferLayout, }; diff --git a/librashader-runtime-wgpu/src/handle.rs b/librashader-runtime-wgpu/src/handle.rs new file mode 100644 index 0000000..1070749 --- /dev/null +++ b/librashader-runtime-wgpu/src/handle.rs @@ -0,0 +1,17 @@ +use std::ops::Deref; + +pub enum Handle<'a, T> { + Borrowed(&'a T), + Owned(T), +} + +impl Deref for Handle<'_, T> { + type Target = T; + + fn deref(&self) -> &Self::Target { + match self { + Handle::Borrowed(r) => &r, + Handle::Owned(r) => &r, + } + } +} diff --git a/librashader-runtime-wgpu/src/lib.rs b/librashader-runtime-wgpu/src/lib.rs index 568a61d..118cdd7 100644 --- a/librashader-runtime-wgpu/src/lib.rs +++ b/librashader-runtime-wgpu/src/lib.rs @@ -14,6 +14,7 @@ mod filter_chain; mod filter_pass; mod framebuffer; mod graphics_pipeline; +mod handle; mod luts; mod mipmap; mod options; diff --git a/librashader-runtime-wgpu/src/luts.rs b/librashader-runtime-wgpu/src/luts.rs index bfaf602..6e2adc8 100644 --- a/librashader-runtime-wgpu/src/luts.rs +++ b/librashader-runtime-wgpu/src/luts.rs @@ -1,9 +1,9 @@ use crate::mipmap::MipmapGen; use crate::samplers::SamplerSet; -use crate::texture::{Handle, InputImage}; +use crate::texture::InputImage; use librashader_common::{Size, WrapMode}; use librashader_presets::TextureConfig; -use librashader_runtime::image::{Image, BGRA8}; +use librashader_runtime::image::Image; use librashader_runtime::scaling::MipmapSize; use std::sync::Arc; use wgpu::TextureDescriptor; diff --git a/librashader-runtime-wgpu/src/samplers.rs b/librashader-runtime-wgpu/src/samplers.rs index cfba912..ed0eccb 100644 --- a/librashader-runtime-wgpu/src/samplers.rs +++ b/librashader-runtime-wgpu/src/samplers.rs @@ -1,4 +1,3 @@ -use crate::error; use librashader_common::{FilterMode, WrapMode}; use rustc_hash::FxHashMap; use std::sync::Arc; diff --git a/librashader-runtime-wgpu/src/texture.rs b/librashader-runtime-wgpu/src/texture.rs index aee743b..9b532f0 100644 --- a/librashader-runtime-wgpu/src/texture.rs +++ b/librashader-runtime-wgpu/src/texture.rs @@ -1,12 +1,9 @@ use crate::error::FilterChainError; use crate::mipmap::MipmapGen; -use crate::samplers::SamplerSet; use librashader_common::{FilterMode, ImageFormat, Size, WrapMode}; use librashader_presets::Scale2D; use librashader_runtime::scaling::{MipmapSize, ScaleFramebuffer, ViewportSize}; -use std::ops::Deref; use std::sync::Arc; -use wgpu::{ImageCopyTexture, TextureFormat, TextureView}; pub struct OwnedImage { device: Arc, @@ -17,30 +14,6 @@ pub struct OwnedImage { pub size: Size, } -pub enum Handle<'a, T> { - Borrowed(&'a T), - Owned(Arc), -} - -impl Clone for Handle<'_, T> { - fn clone(&self) -> Self { - match self { - Handle::Borrowed(r) => Handle::Borrowed(r), - Handle::Owned(r) => Handle::Owned(Arc::clone(r)), - } - } -} -impl Deref for Handle<'_, T> { - type Target = T; - - fn deref(&self) -> &Self::Target { - match self { - Handle::Borrowed(r) => &r, - Handle::Owned(r) => &r, - } - } -} - #[derive(Clone)] pub struct InputImage { /// A handle to the `VkImage`. diff --git a/librashader-runtime-wgpu/tests/hello_triangle.rs b/librashader-runtime-wgpu/tests/hello_triangle.rs index 7654048..0aa3c98 100644 --- a/librashader-runtime-wgpu/tests/hello_triangle.rs +++ b/librashader-runtime-wgpu/tests/hello_triangle.rs @@ -293,11 +293,11 @@ impl<'a> State<'a> { x: 0.0, y: 0.0, mvp: None, - output: librashader_runtime_wgpu::OutputView { - size: filter_output.size().into(), - view: &filter_view, - format: filter_output.format(), - }, + output: librashader_runtime_wgpu::OutputView::new_from_raw( + &filter_view, + filter_output.size().into(), + filter_output.format(), + ), }, &mut encoder, self.frame_count,