librashader/librashader-runtime-d3d11/src/draw_quad.rs

137 lines
4.1 KiB
Rust
Raw Normal View History

2022-11-30 19:05:12 -05:00
use crate::error;
2023-01-16 18:45:02 -05:00
use crate::error::assume_d3d11_init;
2023-02-06 21:56:30 -05:00
use array_concat::concat_arrays;
2022-11-28 21:00:54 -05:00
use bytemuck::offset_of;
2024-02-22 00:41:29 -05:00
use librashader_runtime::quad::{QuadType, VertexInput};
2022-11-28 21:00:54 -05:00
use windows::core::PCSTR;
2022-11-30 01:38:05 -05:00
use windows::Win32::Graphics::Direct3D::D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
use windows::Win32::Graphics::Direct3D11::{
ID3D11Buffer, ID3D11Device, ID3D11DeviceContext, D3D11_BIND_VERTEX_BUFFER, D3D11_BUFFER_DESC,
D3D11_INPUT_ELEMENT_DESC, D3D11_INPUT_PER_VERTEX_DATA, D3D11_SUBRESOURCE_DATA,
D3D11_USAGE_IMMUTABLE,
};
2022-11-28 21:00:54 -05:00
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT;
2024-02-22 00:41:29 -05:00
const OFFSCREEN_VBO_DATA: [VertexInput; 4] = [
VertexInput {
position: [-1.0, -1.0, 0.0, 1.0],
texcoord: [0.0, 1.0],
},
2024-02-22 00:41:29 -05:00
VertexInput {
position: [-1.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 0.0],
},
2024-02-22 00:41:29 -05:00
VertexInput {
position: [1.0, -1.0, 0.0, 1.0],
texcoord: [1.0, 1.0],
},
2024-02-22 00:41:29 -05:00
VertexInput {
position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 0.0],
},
];
2024-02-22 00:41:29 -05:00
const FINAL_VBO_DATA: [VertexInput; 4] = [
VertexInput {
position: [0.0, 0.0, 0.0, 1.0],
2022-11-28 21:00:54 -05:00
texcoord: [0.0, 1.0],
},
2024-02-22 00:41:29 -05:00
VertexInput {
position: [0.0, 1.0, 0.0, 1.0],
2022-11-28 21:00:54 -05:00
texcoord: [0.0, 0.0],
},
2024-02-22 00:41:29 -05:00
VertexInput {
position: [1.0, 0.0, 0.0, 1.0],
2022-11-28 21:00:54 -05:00
texcoord: [1.0, 1.0],
},
2024-02-22 00:41:29 -05:00
VertexInput {
position: [1.0, 1.0, 0.0, 1.0],
2022-11-28 21:00:54 -05:00
texcoord: [1.0, 0.0],
},
];
2024-02-22 00:41:29 -05:00
static VBO_DATA: &[VertexInput; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
2022-11-28 21:00:54 -05:00
pub(crate) struct DrawQuad {
stride: u32,
vbo: ID3D11Buffer,
2022-11-28 21:00:54 -05:00
}
impl DrawQuad {
pub fn new(device: &ID3D11Device) -> error::Result<DrawQuad> {
2022-11-28 21:00:54 -05:00
unsafe {
let mut vbo = None;
device.CreateBuffer(
&D3D11_BUFFER_DESC {
2024-02-22 00:41:29 -05:00
ByteWidth: 2 * std::mem::size_of::<[VertexInput; 4]>() as u32,
Usage: D3D11_USAGE_IMMUTABLE,
2024-02-26 19:04:40 -05:00
BindFlags: D3D11_BIND_VERTEX_BUFFER.0 as u32,
CPUAccessFlags: Default::default(),
MiscFlags: Default::default(),
StructureByteStride: 0,
},
Some(&D3D11_SUBRESOURCE_DATA {
pSysMem: VBO_DATA.as_ptr().cast(),
SysMemPitch: 0,
SysMemSlicePitch: 0,
}),
Some(&mut vbo),
)?;
assume_d3d11_init!(vbo, "CreateBuffer");
2022-11-28 21:00:54 -05:00
Ok(DrawQuad {
vbo,
2024-02-22 00:41:29 -05:00
stride: std::mem::size_of::<VertexInput>() as u32,
2022-11-28 21:00:54 -05:00
})
}
}
pub fn bind_vbo_for_frame(&self, context: &ID3D11DeviceContext) {
2022-11-28 21:00:54 -05:00
unsafe {
context.IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
context.IASetVertexBuffers(
2022-11-30 01:38:05 -05:00
0,
1,
Some(&Some(self.vbo.clone())),
2022-11-30 01:38:05 -05:00
Some(&self.stride),
Some(&0),
2022-11-30 01:38:05 -05:00
);
2022-11-28 21:00:54 -05:00
}
}
2022-11-30 01:35:20 -05:00
pub fn draw_quad(&self, context: &ID3D11DeviceContext, vbo_type: QuadType) {
let offset = match vbo_type {
QuadType::Offscreen => 0,
QuadType::Final => 4,
};
unsafe {
context.Draw(4, offset);
}
}
2022-11-28 21:00:54 -05:00
pub fn get_spirv_cross_vbo_desc() -> [D3D11_INPUT_ELEMENT_DESC; 2] {
[
D3D11_INPUT_ELEMENT_DESC {
SemanticName: PCSTR(b"TEXCOORD\0".as_ptr()),
SemanticIndex: 0,
Format: DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0,
2024-02-22 00:41:29 -05:00
AlignedByteOffset: offset_of!(VertexInput, position) as u32,
2022-11-28 21:00:54 -05:00
InputSlotClass: D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
D3D11_INPUT_ELEMENT_DESC {
SemanticName: PCSTR(b"TEXCOORD\0".as_ptr()),
SemanticIndex: 1,
Format: DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0,
2024-02-22 00:41:29 -05:00
AlignedByteOffset: offset_of!(VertexInput, texcoord) as u32,
2022-11-28 21:00:54 -05:00
InputSlotClass: D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
2022-11-30 01:38:05 -05:00
},
2022-11-28 21:00:54 -05:00
]
}
2022-11-30 01:38:05 -05:00
}