rt(d3d9): load LUTS as BGRA8

Seems like ARGB8 is incorrect.
This commit is contained in:
chyyran 2024-09-25 01:02:00 -04:00 committed by Ronny Chan
parent dbfa822f7c
commit b0df631651
3 changed files with 92 additions and 70 deletions

View file

@ -23,7 +23,7 @@ use librashader_reflect::reflect::semantics::ShaderSemantics;
use librashader_reflect::reflect::ReflectShader; use librashader_reflect::reflect::ReflectShader;
use librashader_runtime::binding::{BindingUtil, TextureInput}; use librashader_runtime::binding::{BindingUtil, TextureInput};
use librashader_runtime::framebuffer::FramebufferInit; 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::quad::QuadType;
use librashader_runtime::render_target::RenderTarget; use librashader_runtime::render_target::RenderTarget;
use librashader_runtime::scaling::ScaleFramebuffer; use librashader_runtime::scaling::ScaleFramebuffer;
@ -190,7 +190,7 @@ impl FilterChainD3D9 {
let images = textures let images = textures
.iter() .iter()
.map(|texture| Image::load(&texture.path, UVDirection::TopLeft)) .map(|texture| Image::load(&texture.path, UVDirection::TopLeft))
.collect::<Result<Vec<Image<ARGB8>>, ImageError>>()?; .collect::<Result<Vec<Image<BGRA8>>, ImageError>>()?;
for (index, (texture, image)) in textures.iter().zip(images).enumerate() { for (index, (texture, image)) in textures.iter().zip(images).enumerate() {
let texture = LutTexture::new(device, &image, &texture)?; let texture = LutTexture::new(device, &image, &texture)?;

View file

@ -3,7 +3,7 @@ use crate::error::assume_d3d_init;
use crate::texture::D3D9InputTexture; use crate::texture::D3D9InputTexture;
use librashader_presets::TextureConfig; use librashader_presets::TextureConfig;
use librashader_runtime::image::{Image, ARGB8}; use librashader_runtime::image::{Image, ARGB8, BGRA8};
use windows::Win32::Graphics::Direct3D9::{ use windows::Win32::Graphics::Direct3D9::{
IDirect3DDevice9, D3DFMT_A8R8G8B8, D3DLOCKED_RECT, D3DPOOL_MANAGED, IDirect3DDevice9, D3DFMT_A8R8G8B8, D3DLOCKED_RECT, D3DPOOL_MANAGED,
@ -21,7 +21,7 @@ impl AsRef<D3D9InputTexture> for LutTexture {
impl LutTexture { impl LutTexture {
pub fn new( pub fn new(
device: &IDirect3DDevice9, device: &IDirect3DDevice9,
source: &Image<ARGB8>, source: &Image<BGRA8>,
config: &TextureConfig, config: &TextureConfig,
) -> error::Result<LutTexture> { ) -> error::Result<LutTexture> {
let mut texture = None; let mut texture = None;

View file

@ -195,10 +195,10 @@ pub mod d3d9_hello_triangle {
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use librashader_common::{GetSize, Viewport}; use librashader_common::{GetSize, Viewport};
use librashader_runtime::quad::IDENTITY_MVP;
use librashader_runtime_d3d9::options::FilterChainOptionsD3D9; use librashader_runtime_d3d9::options::FilterChainOptionsD3D9;
use librashader_runtime_d3d9::FilterChainD3D9; use librashader_runtime_d3d9::FilterChainD3D9;
use std::time::Instant; use std::time::Instant;
use librashader_runtime::image::{Image, UVDirection, ARGB8, BGRA8, RGBA8};
pub struct Sample { pub struct Sample {
pub direct3d: IDirect3D9, pub direct3d: IDirect3D9,
@ -230,6 +230,7 @@ pub mod d3d9_hello_triangle {
// pub deferred_context: ID3D11DeviceContext, // pub deferred_context: ID3D11DeviceContext,
pub vbo: IDirect3DVertexBuffer9, pub vbo: IDirect3DVertexBuffer9,
pub vao: IDirect3DVertexDeclaration9, pub vao: IDirect3DVertexDeclaration9,
pub texture: IDirect3DTexture9,
} }
impl Sample { impl Sample {
@ -284,6 +285,38 @@ pub mod d3d9_hello_triangle {
tex.unwrap() tex.unwrap()
}; };
const IMAGE_PATH: &str = "../triangle.png";
let image = Image::<BGRA8>::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 { self.resources = Some(Resources {
device, device,
filter, filter,
@ -292,18 +325,7 @@ pub mod d3d9_hello_triangle {
frame_end: Instant::now(), frame_end: Instant::now(),
frame_start: Instant::now(), frame_start: Instant::now(),
elapsed: 0f32, elapsed: 0f32,
// renderbuffer, texture,
// 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,
frame_count: 0usize, frame_count: 0usize,
renderbuffer, 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)); // resources.triangle_uniform_values.model_matrix = Mat4::rotate(Quaternion::axis_angle(Vec3::new(0.0, 0.0, 1.0), resources.elapsed));
unsafe { unsafe {
resources // resources
.device // .device
.SetTransform(D3DTS_PROJECTION, IDENTITY_MVP.as_ptr().cast())?; // .SetTransform(D3DTS_PROJECTION, IDENTITY_MVP.as_ptr().cast())?;
resources // resources
.device // .device
.SetTransform(D3DTS_VIEW, IDENTITY_MVP.as_ptr().cast())?; // .SetTransform(D3DTS_VIEW, IDENTITY_MVP.as_ptr().cast())?;
resources // resources
.device // .device
.SetTransform(D3DTRANSFORMSTATETYPE(256), IDENTITY_MVP.as_ptr().cast())?; // .SetTransform(D3DTRANSFORMSTATETYPE(256), IDENTITY_MVP.as_ptr().cast())?;
//
let rendertarget = resources.renderbuffer.GetSurfaceLevel(0).unwrap(); // let rendertarget = resources.renderbuffer.GetSurfaceLevel(0).unwrap();
//
let backbuffer = resources let backbuffer = resources
.device .device
.GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO)?; .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.Clear( // resources.device.BeginScene()?;
0, //
std::ptr::null_mut(), // resources.device.SetStreamSource(
D3DCLEAR_TARGET as u32, // 0,
0xFF4d6699, // &resources.vbo,
0.0, // 0,
0, // std::mem::size_of::<Vertex>() as u32,
)?; // )?;
// resources.device.SetVertexDeclaration(&resources.vao)?;
resources.device.BeginScene()?; //
// resources
resources.device.SetStreamSource( // .device
0, // .SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE.0 as u32)?;
&resources.vbo, // resources
0, // .device
std::mem::size_of::<Vertex>() as u32, // .SetRenderState(D3DRS_CLIPPING, FALSE.0 as u32)?;
)?; // resources
resources.device.SetVertexDeclaration(&resources.vao)?; // .device
// .SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS.0 as u32)?;
resources //
.device // resources
.SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE.0 as u32)?; // .device
resources // .SetRenderState(D3DRS_ZENABLE, FALSE.0 as u32)?;
.device // resources
.SetRenderState(D3DRS_CLIPPING, FALSE.0 as u32)?; // .device
resources // .SetRenderState(D3DRS_LIGHTING, FALSE.0 as u32)?;
.device //
.SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS.0 as u32)?; // resources.device.DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1)?;
//
resources // resources.device.EndScene()?;
.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 resources
.filter .filter
.frame( .frame(
resources.renderbuffer.clone(), resources.texture.clone(),
&Viewport { &Viewport {
x: 0.0, x: 0.0,
y: 0.0, y: 0.0,