Skip to content

Commit

Permalink
rt(d3d9): load LUTS as BGRA8
Browse files Browse the repository at this point in the history
Seems like ARGB8 is incorrect.
  • Loading branch information
chyyran committed Sep 25, 2024
1 parent d26d113 commit 7a5036a
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 70 deletions.
4 changes: 2 additions & 2 deletions librashader-runtime-d3d9/src/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -190,7 +190,7 @@ impl FilterChainD3D9 {
let images = textures
.iter()
.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() {
let texture = LutTexture::new(device, &image, &texture)?;
Expand Down
4 changes: 2 additions & 2 deletions librashader-runtime-d3d9/src/luts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,7 +21,7 @@ impl AsRef<D3D9InputTexture> for LutTexture {
impl LutTexture {
pub fn new(
device: &IDirect3DDevice9,
source: &Image<ARGB8>,
source: &Image<BGRA8>,
config: &TextureConfig,
) -> error::Result<LutTexture> {
let mut texture = None;
Expand Down
154 changes: 88 additions & 66 deletions librashader-runtime-d3d9/tests/hello_triangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -230,6 +230,7 @@ pub mod d3d9_hello_triangle {
// pub deferred_context: ID3D11DeviceContext,
pub vbo: IDirect3DVertexBuffer9,
pub vao: IDirect3DVertexDeclaration9,
pub texture: IDirect3DTexture9,
}

impl Sample {
Expand Down Expand Up @@ -284,6 +285,38 @@ pub mod d3d9_hello_triangle {
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 {
device,
filter,
Expand All @@ -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,
});
Expand Down Expand Up @@ -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::<Vertex>() 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::<Vertex>() 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,
Expand Down

0 comments on commit 7a5036a

Please sign in to comment.