use super::super::Handle;
use super::backend::GpuDevice;
use super::types::*;
use crate::fitz::geometry::Matrix;
#[cfg(target_os = "windows")]
pub struct DirectX11Device {
capabilities: GpuCapabilities,
_device: u64,
_context: u64,
}
#[cfg(target_os = "windows")]
impl DirectX11Device {
pub fn new() -> GpuResult<Self> {
let capabilities = GpuCapabilities {
max_texture_size: 16384,
max_texture_units: 128,
compute_shaders: true,
geometry_shaders: true,
tessellation: true,
max_msaa_samples: 8,
float_textures: true,
instancing: true,
vram_mb: 0,
device_name: "DirectX 11 Device".into(),
vendor_name: "Unknown".into(),
driver_version: "DirectX 11.1".into(),
};
Ok(Self {
capabilities,
_device: 0,
_context: 0,
})
}
}
#[cfg(target_os = "windows")]
impl GpuDevice for DirectX11Device {
fn backend(&self) -> GpuBackendType {
GpuBackendType::DirectX11
}
fn capabilities(&self) -> &GpuCapabilities {
&self.capabilities
}
fn create_texture(&self, width: u32, height: u32, format: GpuFormat) -> GpuResult<GpuTexture> {
let mut texture = GpuTexture::new(width, height, format, GpuBackendType::DirectX11);
texture.native_handle = 1;
Ok(texture)
}
fn destroy_texture(&self, _texture: &GpuTexture) -> GpuResult<()> {
Ok(())
}
fn upload_texture(
&self,
texture: &mut GpuTexture,
_data: &[u8],
_stride: u32,
) -> GpuResult<()> {
let _ = texture;
Ok(())
}
fn download_texture(
&self,
texture: &GpuTexture,
_data: &mut [u8],
_stride: u32,
) -> GpuResult<()> {
let _ = texture;
Ok(())
}
fn clear_texture(&self, texture: &mut GpuTexture, color: [f32; 4]) -> GpuResult<()> {
let _ = (texture, color);
Ok(())
}
fn create_shader(&self, _vertex_src: &str, _fragment_src: &str) -> GpuResult<GpuShader> {
let shader = GpuShader::new("shader", GpuBackendType::DirectX11);
Ok(shader)
}
fn destroy_shader(&self, _shader: &GpuShader) -> GpuResult<()> {
Ok(())
}
fn create_buffer(&self, size: usize, usage: GpuBufferUsage) -> GpuResult<GpuBuffer> {
let buffer = GpuBuffer::new(size, usage, GpuBackendType::DirectX11);
Ok(buffer)
}
fn destroy_buffer(&self, _buffer: &GpuBuffer) -> GpuResult<()> {
Ok(())
}
fn upload_buffer(&self, buffer: &mut GpuBuffer, _data: &[u8], _offset: usize) -> GpuResult<()> {
let _ = buffer;
Ok(())
}
fn render_page(
&self,
page: Handle,
texture: &mut GpuTexture,
transform: &Matrix,
) -> GpuResult<()> {
let _ = (page, texture, transform);
Ok(())
}
fn composite(
&self,
src: &GpuTexture,
dst: &mut GpuTexture,
x: i32,
y: i32,
blend_mode: GpuBlendMode,
) -> GpuResult<()> {
let _ = (src, dst, x, y, blend_mode);
Ok(())
}
fn draw_quad(
&self,
texture: &GpuTexture,
dst: &mut GpuTexture,
src_rect: [f32; 4],
dst_rect: [f32; 4],
color: [f32; 4],
) -> GpuResult<()> {
let _ = (texture, dst, src_rect, dst_rect, color);
Ok(())
}
fn flush(&self) -> GpuResult<()> {
Ok(())
}
fn finish(&self) -> GpuResult<()> {
Ok(())
}
}
#[cfg(target_os = "windows")]
pub struct DirectX12Device {
capabilities: GpuCapabilities,
_device: u64,
_command_queue: u64,
_command_allocator: u64,
_command_list: u64,
_fence: u64,
_fence_value: u64,
}
#[cfg(target_os = "windows")]
impl DirectX12Device {
pub fn new() -> GpuResult<Self> {
let capabilities = GpuCapabilities {
max_texture_size: 16384,
max_texture_units: 128,
compute_shaders: true,
geometry_shaders: true,
tessellation: true,
max_msaa_samples: 8,
float_textures: true,
instancing: true,
vram_mb: 0,
device_name: "DirectX 12 Device".into(),
vendor_name: "Unknown".into(),
driver_version: "DirectX 12".into(),
};
Ok(Self {
capabilities,
_device: 0,
_command_queue: 0,
_command_allocator: 0,
_command_list: 0,
_fence: 0,
_fence_value: 0,
})
}
}
#[cfg(target_os = "windows")]
impl GpuDevice for DirectX12Device {
fn backend(&self) -> GpuBackendType {
GpuBackendType::DirectX12
}
fn capabilities(&self) -> &GpuCapabilities {
&self.capabilities
}
fn create_texture(&self, width: u32, height: u32, format: GpuFormat) -> GpuResult<GpuTexture> {
let mut texture = GpuTexture::new(width, height, format, GpuBackendType::DirectX12);
texture.native_handle = 1;
Ok(texture)
}
fn destroy_texture(&self, _texture: &GpuTexture) -> GpuResult<()> {
Ok(())
}
fn upload_texture(
&self,
texture: &mut GpuTexture,
_data: &[u8],
_stride: u32,
) -> GpuResult<()> {
let _ = texture;
Ok(())
}
fn download_texture(
&self,
texture: &GpuTexture,
_data: &mut [u8],
_stride: u32,
) -> GpuResult<()> {
let _ = texture;
Ok(())
}
fn clear_texture(&self, texture: &mut GpuTexture, color: [f32; 4]) -> GpuResult<()> {
let _ = (texture, color);
Ok(())
}
fn create_shader(&self, _vertex_src: &str, _fragment_src: &str) -> GpuResult<GpuShader> {
let shader = GpuShader::new("shader", GpuBackendType::DirectX12);
Ok(shader)
}
fn destroy_shader(&self, _shader: &GpuShader) -> GpuResult<()> {
Ok(())
}
fn create_buffer(&self, size: usize, usage: GpuBufferUsage) -> GpuResult<GpuBuffer> {
let buffer = GpuBuffer::new(size, usage, GpuBackendType::DirectX12);
Ok(buffer)
}
fn destroy_buffer(&self, _buffer: &GpuBuffer) -> GpuResult<()> {
Ok(())
}
fn upload_buffer(&self, buffer: &mut GpuBuffer, _data: &[u8], _offset: usize) -> GpuResult<()> {
let _ = buffer;
Ok(())
}
fn render_page(
&self,
page: Handle,
texture: &mut GpuTexture,
transform: &Matrix,
) -> GpuResult<()> {
let _ = (page, texture, transform);
Ok(())
}
fn composite(
&self,
src: &GpuTexture,
dst: &mut GpuTexture,
x: i32,
y: i32,
blend_mode: GpuBlendMode,
) -> GpuResult<()> {
let _ = (src, dst, x, y, blend_mode);
Ok(())
}
fn draw_quad(
&self,
texture: &GpuTexture,
dst: &mut GpuTexture,
src_rect: [f32; 4],
dst_rect: [f32; 4],
color: [f32; 4],
) -> GpuResult<()> {
let _ = (texture, dst, src_rect, dst_rect, color);
Ok(())
}
fn flush(&self) -> GpuResult<()> {
Ok(())
}
fn finish(&self) -> GpuResult<()> {
Ok(())
}
}
pub const QUAD_VERTEX_SHADER_HLSL: &str = r#"
cbuffer Constants : register(b0) {
float4x4 projection;
float4x4 transform;
float4 color;
};
struct VSInput {
float2 position : POSITION;
float2 texcoord : TEXCOORD0;
};
struct VSOutput {
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
VSOutput main(VSInput input) {
VSOutput output;
output.position = mul(projection, mul(transform, float4(input.position, 0.0, 1.0)));
output.texcoord = input.texcoord;
return output;
}
"#;
pub const QUAD_PIXEL_SHADER_HLSL: &str = r#"
cbuffer Constants : register(b0) {
float4x4 projection;
float4x4 transform;
float4 color;
};
Texture2D tex : register(t0);
SamplerState samp : register(s0);
struct PSInput {
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
float4 main(PSInput input) : SV_TARGET {
return tex.Sample(samp, input.texcoord) * color;
}
"#;
pub const PATH_VERTEX_SHADER_HLSL: &str = r#"
cbuffer Constants : register(b0) {
float4x4 projection;
float4x4 transform;
float4 color;
};
struct VSInput {
float2 position : POSITION;
};
struct VSOutput {
float4 position : SV_POSITION;
};
VSOutput main(VSInput input) {
VSOutput output;
output.position = mul(projection, mul(transform, float4(input.position, 0.0, 1.0)));
return output;
}
"#;
pub const PATH_PIXEL_SHADER_HLSL: &str = r#"
cbuffer Constants : register(b0) {
float4x4 projection;
float4x4 transform;
float4 color;
};
float4 main() : SV_TARGET {
return color;
}
"#;
#[cfg(all(test, target_os = "windows"))]
mod tests {
use super::*;
#[test]
fn test_create_dx11_device() {
let device = DirectX11Device::new().unwrap();
assert_eq!(device.backend(), GpuBackendType::DirectX11);
}
#[test]
fn test_create_dx12_device() {
let device = DirectX12Device::new().unwrap();
assert_eq!(device.backend(), GpuBackendType::DirectX12);
}
}