2022-12-01 11:05:12 +11:00
|
|
|
use crate::error;
|
2023-01-17 10:45:02 +11:00
|
|
|
use crate::error::assume_d3d11_init;
|
2023-02-07 13:56:30 +11:00
|
|
|
use array_concat::concat_arrays;
|
2022-11-29 13:00:54 +11:00
|
|
|
use bytemuck::offset_of;
|
2023-02-06 08:17:23 +11:00
|
|
|
use librashader_runtime::quad::QuadType;
|
2022-11-29 13:00:54 +11:00
|
|
|
use windows::core::PCSTR;
|
2022-11-30 17:38:05 +11: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-29 13:00:54 +11:00
|
|
|
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug, Copy, Clone, Default)]
|
|
|
|
struct D3D11Vertex {
|
|
|
|
position: [f32; 2],
|
|
|
|
texcoord: [f32; 2],
|
2022-11-30 17:38:05 +11:00
|
|
|
color: [f32; 4],
|
2022-11-29 13:00:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
const CLEAR: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
|
|
|
|
|
2023-02-06 16:23:37 +11:00
|
|
|
const OFFSCREEN_VBO_DATA: [D3D11Vertex; 4] = [
|
2023-01-31 16:53:55 +11:00
|
|
|
D3D11Vertex {
|
|
|
|
position: [-1.0, -1.0],
|
|
|
|
texcoord: [0.0, 1.0],
|
|
|
|
color: CLEAR,
|
|
|
|
},
|
|
|
|
D3D11Vertex {
|
|
|
|
position: [-1.0, 1.0],
|
|
|
|
texcoord: [0.0, 0.0],
|
|
|
|
color: CLEAR,
|
|
|
|
},
|
|
|
|
D3D11Vertex {
|
|
|
|
position: [1.0, -1.0],
|
|
|
|
texcoord: [1.0, 1.0],
|
|
|
|
color: CLEAR,
|
|
|
|
},
|
|
|
|
D3D11Vertex {
|
|
|
|
position: [1.0, 1.0],
|
|
|
|
texcoord: [1.0, 0.0],
|
|
|
|
color: CLEAR,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2023-02-06 16:23:37 +11:00
|
|
|
const FINAL_VBO_DATA: [D3D11Vertex; 4] = [
|
2022-11-29 13:00:54 +11:00
|
|
|
D3D11Vertex {
|
|
|
|
position: [0.0, 0.0],
|
|
|
|
texcoord: [0.0, 1.0],
|
|
|
|
color: CLEAR,
|
|
|
|
},
|
|
|
|
D3D11Vertex {
|
|
|
|
position: [0.0, 1.0],
|
|
|
|
texcoord: [0.0, 0.0],
|
|
|
|
color: CLEAR,
|
|
|
|
},
|
|
|
|
D3D11Vertex {
|
|
|
|
position: [1.0, 0.0],
|
|
|
|
texcoord: [1.0, 1.0],
|
|
|
|
color: CLEAR,
|
|
|
|
},
|
|
|
|
D3D11Vertex {
|
|
|
|
position: [1.0, 1.0],
|
|
|
|
texcoord: [1.0, 0.0],
|
|
|
|
color: CLEAR,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2023-02-06 16:23:37 +11:00
|
|
|
static VBO_DATA: &[D3D11Vertex; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
|
|
|
|
|
2022-11-29 13:00:54 +11:00
|
|
|
pub(crate) struct DrawQuad {
|
|
|
|
stride: u32,
|
2023-02-06 16:23:37 +11:00
|
|
|
vbo: ID3D11Buffer,
|
2022-11-29 13:00:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DrawQuad {
|
2023-02-11 10:08:11 +11:00
|
|
|
pub fn new(device: &ID3D11Device) -> error::Result<DrawQuad> {
|
2022-11-29 13:00:54 +11:00
|
|
|
unsafe {
|
2023-02-06 16:23:37 +11:00
|
|
|
let mut vbo = None;
|
2023-01-31 16:53:55 +11:00
|
|
|
device.CreateBuffer(
|
|
|
|
&D3D11_BUFFER_DESC {
|
2023-02-06 16:23:37 +11:00
|
|
|
ByteWidth: 2 * std::mem::size_of::<[D3D11Vertex; 4]>() as u32,
|
2023-01-31 16:53:55 +11:00
|
|
|
Usage: D3D11_USAGE_IMMUTABLE,
|
|
|
|
BindFlags: D3D11_BIND_VERTEX_BUFFER,
|
|
|
|
CPUAccessFlags: Default::default(),
|
|
|
|
MiscFlags: Default::default(),
|
|
|
|
StructureByteStride: 0,
|
|
|
|
},
|
|
|
|
Some(&D3D11_SUBRESOURCE_DATA {
|
2023-02-06 16:23:37 +11:00
|
|
|
pSysMem: VBO_DATA.as_ptr().cast(),
|
2023-01-31 16:53:55 +11:00
|
|
|
SysMemPitch: 0,
|
|
|
|
SysMemSlicePitch: 0,
|
|
|
|
}),
|
2023-02-06 16:23:37 +11:00
|
|
|
Some(&mut vbo),
|
2023-01-31 16:53:55 +11:00
|
|
|
)?;
|
2023-02-06 16:23:37 +11:00
|
|
|
assume_d3d11_init!(vbo, "CreateBuffer");
|
2022-11-29 13:00:54 +11:00
|
|
|
|
|
|
|
Ok(DrawQuad {
|
2023-02-06 16:23:37 +11:00
|
|
|
vbo,
|
2022-11-30 17:38:05 +11:00
|
|
|
stride: std::mem::size_of::<D3D11Vertex>() as u32,
|
2022-11-29 13:00:54 +11:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:08:11 +11:00
|
|
|
pub fn bind_vbo_for_frame(&self, context: &ID3D11DeviceContext) {
|
2022-11-29 13:00:54 +11:00
|
|
|
unsafe {
|
2023-02-11 10:08:11 +11:00
|
|
|
context.IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
2023-01-31 16:53:55 +11:00
|
|
|
|
2023-02-11 10:08:11 +11:00
|
|
|
context.IASetVertexBuffers(
|
2022-11-30 17:38:05 +11:00
|
|
|
0,
|
|
|
|
1,
|
2023-02-06 16:23:37 +11:00
|
|
|
Some(&Some(self.vbo.clone())),
|
2022-11-30 17:38:05 +11:00
|
|
|
Some(&self.stride),
|
2023-02-06 16:23:37 +11:00
|
|
|
Some(&0),
|
2022-11-30 17:38:05 +11:00
|
|
|
);
|
2022-11-29 13:00:54 +11:00
|
|
|
}
|
|
|
|
}
|
2022-11-30 17:35:20 +11:00
|
|
|
|
2023-02-06 16:23:37 +11: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-29 13:00:54 +11: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,
|
|
|
|
AlignedByteOffset: offset_of!(D3D11Vertex, position) as u32,
|
|
|
|
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,
|
|
|
|
AlignedByteOffset: offset_of!(D3D11Vertex, texcoord) as u32,
|
|
|
|
InputSlotClass: D3D11_INPUT_PER_VERTEX_DATA,
|
|
|
|
InstanceDataStepRate: 0,
|
2022-11-30 17:38:05 +11:00
|
|
|
},
|
2022-11-29 13:00:54 +11:00
|
|
|
]
|
|
|
|
}
|
2022-11-30 17:38:05 +11:00
|
|
|
}
|