use super::super::Handle;
use super::backend::GpuDevice;
use super::types::*;
use crate::fitz::geometry::Matrix;
pub struct VulkanDevice {
capabilities: GpuCapabilities,
_instance: u64,
_physical_device: u64,
_device: u64,
_graphics_queue: u64,
_command_pool: u64,
}
impl VulkanDevice {
pub fn new() -> GpuResult<Self> {
let capabilities = GpuCapabilities {
max_texture_size: 16384,
max_texture_units: 32,
compute_shaders: true,
geometry_shaders: true,
tessellation: true,
max_msaa_samples: 8,
float_textures: true,
instancing: true,
vram_mb: 0,
device_name: "Vulkan Device".into(),
vendor_name: "Unknown".into(),
driver_version: "Vulkan 1.3".into(),
};
Ok(Self {
capabilities,
_instance: 0,
_physical_device: 0,
_device: 0,
_graphics_queue: 0,
_command_pool: 0,
})
}
}
impl GpuDevice for VulkanDevice {
fn backend(&self) -> GpuBackendType {
GpuBackendType::Vulkan
}
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::Vulkan);
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::Vulkan);
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::Vulkan);
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_GLSL: &str = r#"
#version 450
layout(location = 0) in vec2 a_position;
layout(location = 1) in vec2 a_texcoord;
layout(location = 0) out vec2 v_texcoord;
layout(push_constant) uniform PushConstants {
mat4 projection;
mat4 transform;
} pc;
void main() {
gl_Position = pc.projection * pc.transform * vec4(a_position, 0.0, 1.0);
v_texcoord = a_texcoord;
}
"#;
pub const QUAD_FRAGMENT_SHADER_GLSL: &str = r#"
#version 450
layout(location = 0) in vec2 v_texcoord;
layout(location = 0) out vec4 fragColor;
layout(set = 0, binding = 0) uniform sampler2D u_texture;
layout(push_constant) uniform PushConstants {
mat4 projection;
mat4 transform;
vec4 color;
} pc;
void main() {
fragColor = texture(u_texture, v_texcoord) * pc.color;
}
"#;
pub const PATH_VERTEX_SHADER_GLSL: &str = r#"
#version 450
layout(location = 0) in vec2 a_position;
layout(push_constant) uniform PushConstants {
mat4 projection;
mat4 transform;
vec4 color;
} pc;
void main() {
gl_Position = pc.projection * pc.transform * vec4(a_position, 0.0, 1.0);
}
"#;
pub const PATH_FRAGMENT_SHADER_GLSL: &str = r#"
#version 450
layout(location = 0) out vec4 fragColor;
layout(push_constant) uniform PushConstants {
mat4 projection;
mat4 transform;
vec4 color;
} pc;
void main() {
fragColor = pc.color;
}
"#;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_device() {
let device = VulkanDevice::new().unwrap();
assert_eq!(device.backend(), GpuBackendType::Vulkan);
}
#[test]
fn test_create_texture() {
let device = VulkanDevice::new().unwrap();
let texture = device.create_texture(512, 512, GpuFormat::Rgba8).unwrap();
assert_eq!(texture.width, 512);
assert_eq!(texture.height, 512);
}
}