From 7a5036a20761be59b5686495051dd0d3e0d19426 Mon Sep 17 00:00:00 2001 From: chyyran Date: Wed, 25 Sep 2024 01:02:00 -0400 Subject: [PATCH] rt(d3d9): load LUTS as BGRA8 Seems like ARGB8 is incorrect. --- librashader-runtime-d3d9/src/filter_chain.rs | 4 +- librashader-runtime-d3d9/src/luts.rs | 4 +- .../tests/hello_triangle/mod.rs | 154 ++++++++++-------- 3 files changed, 92 insertions(+), 70 deletions(-) diff --git a/librashader-runtime-d3d9/src/filter_chain.rs b/librashader-runtime-d3d9/src/filter_chain.rs index ffdb010b..9a0916ea 100644 --- a/librashader-runtime-d3d9/src/filter_chain.rs +++ b/librashader-runtime-d3d9/src/filter_chain.rs @@ -23,7 +23,7 @@ use librashader_reflect::reflect::semantics::ShaderSemantics; use librashader_reflect::reflect::ReflectShader; use librashader_runtime::binding::{BindingUtil, TextureInput}; use librashader_runtime::framebuffer::FramebufferInit; -use librashader_runtime::image::{Image, ImageError, UVDirection, ARGB8}; +use librashader_runtime::image::{Image, ImageError, UVDirection, ARGB8, BGRA8}; use librashader_runtime::quad::QuadType; use librashader_runtime::render_target::RenderTarget; use librashader_runtime::scaling::ScaleFramebuffer; @@ -190,7 +190,7 @@ impl FilterChainD3D9 { let images = textures .iter() .map(|texture| Image::load(&texture.path, UVDirection::TopLeft)) - .collect::>, ImageError>>()?; + .collect::>, ImageError>>()?; for (index, (texture, image)) in textures.iter().zip(images).enumerate() { let texture = LutTexture::new(device, &image, &texture)?; diff --git a/librashader-runtime-d3d9/src/luts.rs b/librashader-runtime-d3d9/src/luts.rs index d9e9f11e..fa4493ce 100644 --- a/librashader-runtime-d3d9/src/luts.rs +++ b/librashader-runtime-d3d9/src/luts.rs @@ -3,7 +3,7 @@ use crate::error::assume_d3d_init; use crate::texture::D3D9InputTexture; use librashader_presets::TextureConfig; -use librashader_runtime::image::{Image, ARGB8}; +use librashader_runtime::image::{Image, ARGB8, BGRA8}; use windows::Win32::Graphics::Direct3D9::{ IDirect3DDevice9, D3DFMT_A8R8G8B8, D3DLOCKED_RECT, D3DPOOL_MANAGED, @@ -21,7 +21,7 @@ impl AsRef for LutTexture { impl LutTexture { pub fn new( device: &IDirect3DDevice9, - source: &Image, + source: &Image, config: &TextureConfig, ) -> error::Result { let mut texture = None; diff --git a/librashader-runtime-d3d9/tests/hello_triangle/mod.rs b/librashader-runtime-d3d9/tests/hello_triangle/mod.rs index 112d23a3..e7dae07c 100644 --- a/librashader-runtime-d3d9/tests/hello_triangle/mod.rs +++ b/librashader-runtime-d3d9/tests/hello_triangle/mod.rs @@ -195,10 +195,10 @@ pub mod d3d9_hello_triangle { use std::path::{Path, PathBuf}; use librashader_common::{GetSize, Viewport}; - use librashader_runtime::quad::IDENTITY_MVP; use librashader_runtime_d3d9::options::FilterChainOptionsD3D9; use librashader_runtime_d3d9::FilterChainD3D9; use std::time::Instant; + use librashader_runtime::image::{Image, UVDirection, ARGB8, BGRA8, RGBA8}; pub struct Sample { pub direct3d: IDirect3D9, @@ -230,6 +230,7 @@ pub mod d3d9_hello_triangle { // pub deferred_context: ID3D11DeviceContext, pub vbo: IDirect3DVertexBuffer9, pub vao: IDirect3DVertexDeclaration9, + pub texture: IDirect3DTexture9, } impl Sample { @@ -284,6 +285,38 @@ pub mod d3d9_hello_triangle { tex.unwrap() }; + const IMAGE_PATH: &str = "../triangle.png"; + + let image = Image::::load(IMAGE_PATH, UVDirection::TopLeft) + .expect("triangle.png not found"); + + let texture = unsafe { + let mut texture = None; + device.CreateTexture( + image.size.width, + image.size.height, + 1, + 0, + D3DFMT_A8R8G8B8, + D3DPOOL_MANAGED, + &mut texture, + std::ptr::null_mut(), + )?; + + texture.unwrap() + }; + + unsafe { + let mut lock = D3DLOCKED_RECT::default(); + texture.LockRect(0, &mut lock, std::ptr::null_mut(), 0)?; + std::ptr::copy_nonoverlapping( + image.bytes.as_ptr(), + lock.pBits.cast(), + image.bytes.len(), + ); + texture.UnlockRect(0)?; + } + self.resources = Some(Resources { device, filter, @@ -292,18 +325,7 @@ pub mod d3d9_hello_triangle { frame_end: Instant::now(), frame_start: Instant::now(), elapsed: 0f32, - // renderbuffer, - // renderbufffer_rtv: render_rtv, - // deferred_context: context, - // viewport: D3D11_VIEWPORT { - // TopLeftX: 0.0, - // TopLeftY: 0.0, - // Width: WIDTH as f32, - // Height: HEIGHT as f32, - // MinDepth: D3D11_MIN_DEPTH, - // MaxDepth: D3D11_MAX_DEPTH, - // }, - // shader_output: None, + texture, frame_count: 0usize, renderbuffer, }); @@ -348,68 +370,68 @@ pub mod d3d9_hello_triangle { // resources.triangle_uniform_values.model_matrix = Mat4::rotate(Quaternion::axis_angle(Vec3::new(0.0, 0.0, 1.0), resources.elapsed)); unsafe { - resources - .device - .SetTransform(D3DTS_PROJECTION, IDENTITY_MVP.as_ptr().cast())?; - resources - .device - .SetTransform(D3DTS_VIEW, IDENTITY_MVP.as_ptr().cast())?; - resources - .device - .SetTransform(D3DTRANSFORMSTATETYPE(256), IDENTITY_MVP.as_ptr().cast())?; - - let rendertarget = resources.renderbuffer.GetSurfaceLevel(0).unwrap(); - + // resources + // .device + // .SetTransform(D3DTS_PROJECTION, IDENTITY_MVP.as_ptr().cast())?; + // resources + // .device + // .SetTransform(D3DTS_VIEW, IDENTITY_MVP.as_ptr().cast())?; + // resources + // .device + // .SetTransform(D3DTRANSFORMSTATETYPE(256), IDENTITY_MVP.as_ptr().cast())?; + // + // let rendertarget = resources.renderbuffer.GetSurfaceLevel(0).unwrap(); + // let backbuffer = resources .device .GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO)?; + // + // resources.device.SetRenderTarget(0, &rendertarget)?; - resources.device.SetRenderTarget(0, &rendertarget)?; - - resources.device.Clear( - 0, - std::ptr::null_mut(), - D3DCLEAR_TARGET as u32, - 0xFF4d6699, - 0.0, - 0, - )?; - - resources.device.BeginScene()?; - - resources.device.SetStreamSource( - 0, - &resources.vbo, - 0, - std::mem::size_of::() as u32, - )?; - resources.device.SetVertexDeclaration(&resources.vao)?; - - resources - .device - .SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE.0 as u32)?; - resources - .device - .SetRenderState(D3DRS_CLIPPING, FALSE.0 as u32)?; - resources - .device - .SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS.0 as u32)?; - - resources - .device - .SetRenderState(D3DRS_ZENABLE, FALSE.0 as u32)?; - resources - .device - .SetRenderState(D3DRS_LIGHTING, FALSE.0 as u32)?; - - resources.device.DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1)?; + // resources.device.Clear( + // 0, + // std::ptr::null_mut(), + // D3DCLEAR_TARGET as u32, + // 0xFF4d6699, + // 0.0, + // 0, + // )?; - resources.device.EndScene()?; + // resources.device.BeginScene()?; + // + // resources.device.SetStreamSource( + // 0, + // &resources.vbo, + // 0, + // std::mem::size_of::() as u32, + // )?; + // resources.device.SetVertexDeclaration(&resources.vao)?; + // + // resources + // .device + // .SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE.0 as u32)?; + // resources + // .device + // .SetRenderState(D3DRS_CLIPPING, FALSE.0 as u32)?; + // resources + // .device + // .SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS.0 as u32)?; + // + // resources + // .device + // .SetRenderState(D3DRS_ZENABLE, FALSE.0 as u32)?; + // resources + // .device + // .SetRenderState(D3DRS_LIGHTING, FALSE.0 as u32)?; + // + // resources.device.DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1)?; + // + // resources.device.EndScene()?; resources .filter .frame( - resources.renderbuffer.clone(), + resources.texture.clone(), &Viewport { x: 0.0, y: 0.0,