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;
|
2022-11-28 21:00:54 -05:00
|
|
|
use bytemuck::offset_of;
|
|
|
|
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;
|
2023-01-31 00:53:55 -05:00
|
|
|
use librashader_runtime::quad::QuadType;
|
2022-11-28 21:00:54 -05:00
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug, Copy, Clone, Default)]
|
|
|
|
struct D3D11Vertex {
|
|
|
|
position: [f32; 2],
|
|
|
|
texcoord: [f32; 2],
|
2022-11-30 01:38:05 -05:00
|
|
|
color: [f32; 4],
|
2022-11-28 21:00:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const CLEAR: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
|
|
|
|
|
2023-01-31 00:53:55 -05:00
|
|
|
static OFFSCREEN_VBO_DATA: &[D3D11Vertex; 4] = &[
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
static FINAL_VBO_DATA: &[D3D11Vertex; 4] = &[
|
2022-11-28 21:00:54 -05: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,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
pub(crate) struct DrawQuad {
|
2023-01-31 00:53:55 -05:00
|
|
|
final_vbo: ID3D11Buffer,
|
2022-11-28 21:00:54 -05:00
|
|
|
context: ID3D11DeviceContext,
|
|
|
|
offset: u32,
|
|
|
|
stride: u32,
|
2023-01-31 00:53:55 -05:00
|
|
|
offscreen_vbo: ID3D11Buffer,
|
2022-11-28 21:00:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DrawQuad {
|
2022-11-30 19:05:12 -05:00
|
|
|
pub fn new(device: &ID3D11Device, context: &ID3D11DeviceContext) -> error::Result<DrawQuad> {
|
2022-11-28 21:00:54 -05:00
|
|
|
unsafe {
|
2023-01-31 00:53:55 -05:00
|
|
|
let mut final_vbo = None;
|
2023-01-16 18:45:02 -05:00
|
|
|
device.CreateBuffer(
|
2022-11-30 01:38:05 -05:00
|
|
|
&D3D11_BUFFER_DESC {
|
|
|
|
ByteWidth: std::mem::size_of::<[D3D11Vertex; 4]>() as u32,
|
|
|
|
Usage: D3D11_USAGE_IMMUTABLE,
|
|
|
|
BindFlags: D3D11_BIND_VERTEX_BUFFER,
|
|
|
|
CPUAccessFlags: Default::default(),
|
|
|
|
MiscFlags: Default::default(),
|
|
|
|
StructureByteStride: 0,
|
|
|
|
},
|
|
|
|
Some(&D3D11_SUBRESOURCE_DATA {
|
2023-01-31 00:53:55 -05:00
|
|
|
pSysMem: FINAL_VBO_DATA.as_ptr().cast(),
|
2022-11-30 01:38:05 -05:00
|
|
|
SysMemPitch: 0,
|
|
|
|
SysMemSlicePitch: 0,
|
|
|
|
}),
|
2023-01-31 00:53:55 -05:00
|
|
|
Some(&mut final_vbo),
|
2022-11-30 01:38:05 -05:00
|
|
|
)?;
|
2023-01-31 00:53:55 -05:00
|
|
|
assume_d3d11_init!(final_vbo, "CreateBuffer");
|
|
|
|
|
|
|
|
let mut offscreen_vbo = None;
|
|
|
|
device.CreateBuffer(
|
|
|
|
&D3D11_BUFFER_DESC {
|
|
|
|
ByteWidth: std::mem::size_of::<[D3D11Vertex; 4]>() as u32,
|
|
|
|
Usage: D3D11_USAGE_IMMUTABLE,
|
|
|
|
BindFlags: D3D11_BIND_VERTEX_BUFFER,
|
|
|
|
CPUAccessFlags: Default::default(),
|
|
|
|
MiscFlags: Default::default(),
|
|
|
|
StructureByteStride: 0,
|
|
|
|
},
|
|
|
|
Some(&D3D11_SUBRESOURCE_DATA {
|
|
|
|
pSysMem: OFFSCREEN_VBO_DATA.as_ptr().cast(),
|
|
|
|
SysMemPitch: 0,
|
|
|
|
SysMemSlicePitch: 0,
|
|
|
|
}),
|
|
|
|
Some(&mut offscreen_vbo),
|
|
|
|
)?;
|
|
|
|
assume_d3d11_init!(offscreen_vbo, "CreateBuffer");
|
2022-11-28 21:00:54 -05:00
|
|
|
|
|
|
|
Ok(DrawQuad {
|
2023-01-31 00:53:55 -05:00
|
|
|
final_vbo,
|
|
|
|
offscreen_vbo,
|
2022-11-28 21:00:54 -05:00
|
|
|
context: context.clone(),
|
|
|
|
offset: 0,
|
2022-11-30 01:38:05 -05:00
|
|
|
stride: std::mem::size_of::<D3D11Vertex>() as u32,
|
2022-11-28 21:00:54 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 00:53:55 -05:00
|
|
|
pub fn bind_vertices(&self, vbo_type: QuadType) {
|
2022-11-28 21:00:54 -05:00
|
|
|
unsafe {
|
2022-11-30 01:38:05 -05:00
|
|
|
self.context
|
|
|
|
.IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
2023-01-31 00:53:55 -05:00
|
|
|
let buffer = match vbo_type {
|
|
|
|
QuadType::Offscreen => &self.offscreen_vbo,
|
|
|
|
QuadType::Final => &self.final_vbo,
|
|
|
|
};
|
|
|
|
|
2022-11-30 01:38:05 -05:00
|
|
|
self.context.IASetVertexBuffers(
|
|
|
|
0,
|
|
|
|
1,
|
2023-01-31 00:53:55 -05:00
|
|
|
Some(&Some(buffer.clone())),
|
2022-11-30 01:38:05 -05:00
|
|
|
Some(&self.stride),
|
|
|
|
Some(&self.offset),
|
|
|
|
);
|
2022-11-28 21:00:54 -05:00
|
|
|
}
|
|
|
|
}
|
2022-11-30 01:35:20 -05:00
|
|
|
|
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,
|
|
|
|
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 01:38:05 -05:00
|
|
|
},
|
2022-11-28 21:00:54 -05:00
|
|
|
]
|
|
|
|
}
|
2022-11-30 01:38:05 -05:00
|
|
|
}
|