Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // used for constructing instances
- // of errors from ::anyhow
- use anyhow::{anyhow, Result};
- use winit::window::Window;
- use vulkanalia::{
- Version, // used for macos
- loader::{LibloadingLoader, LIBRARY},
- window as vk_window,
- prelude::v1_0::*,
- vk::ExtDebugUtilsExtensionInstanceCommands, // command wrapper
- // for debug functionality
- };
- use log::*; // logging abstract
- use std::{
- collections::HashSet, // needed for storing and querying
- // supported layers and others
- ffi::CStr,
- os::raw::c_void,
- };
- // minimum version for macos devices
- // since it doesnt fully support vulkan
- // and instead partially translated to metal
- const PORTABILITY_MACOS_VERSION: Version = Version::new(1, 3, 216);
- // also, cfg is configurational condition check
- const VALIDATION_ENABLED: bool = cfg!(debug_assertions);
- const VALIDATION_LAYER: vk::ExtensionName =
- vk::ExtensionName::from_bytes(b"VK_LAYERS_KHRONOS_validation");
- // struct used for setting up the
- // rendering, setting, and destroying logics
- #[derive(Clone, Debug)]
- pub struct V_A {
- entry: Entry,
- instance: Instance,
- }
- impl V_A {
- // entry corresponds for vulkan entry
- // which is created later
- unsafe fn create_instance(window: &Window, entry: &Entry) -> Result<Instance> {
- // most of this arguments r not that important
- let app_info = vk::ApplicationInfo::builder()
- .application_name(b"jesus is dead\0")
- .application_version(vk::make_version(1, 0, 0))
- .engine_name(b"brainfuck\0")
- .engine_version(vk::make_version(1, 0, 0))
- .api_version(vk::make_version(1, 0, 0));
- // collects available layers
- // into hashset
- let available_layers = entry
- .enumerate_instance_layer_properties()?
- .iter()
- .map(|l| l.layer_name)
- .collect::<HashSet<_>>();
- if VALIDATION_ENABLED && !available_layers.contains(&VALIDATION_LAYER) {
- return Err(anyhow!("validation layers requested but not supported"));
- }
- // layers data storing vector.
- // checks if the v l is enabled and
- // creates a list of available names
- let layers = if VALIDATION_ENABLED {
- vec![VALIDATION_LAYER.as_ptr()]
- }
- else {
- Vec::new()
- };
- // arguments to vulkan driver about
- // global extensions and validation
- // layers used
- let mut extensions = vk_window::get_required_instance_extensions(window)
- .iter()
- .map(|e| e.as_ptr())
- .collect::<Vec<_>>();
- // adds the name field from vk::..
- // to the list of desired extension names
- if VALIDATION_ENABLED {
- extensions.push(vk::EXT_DEBUG_UTILS_EXTENSION.name.as_ptr());
- }
- // enables KHR_PORTABILITY_ENUMERATION_EXTENSION
- // for a platform that lacks vulkan compability
- let flags = if
- cfg!(target_os = "macos") && entry.version()? >= PORTABILITY_MACOS_VERSION {
- info!("enables macos extensions needed for portability");
- extensions.push(vk::KHR_GET_PHYSICAL_DEVICE_PROPERTIES2_EXTENSION.name.as_ptr());
- extensions.push(vk::KHR_PORTABILITY_ENUMERATION_EXTENSION.name.as_ptr());
- vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR
- }
- else {
- vk::InstanceCreateFlags::empty()
- };
- // shit which creates instances from
- // that previously made app_info, and os
- // specific flags
- let info = vk::InstanceCreateInfo::builder()
- .application_info(&app_info)
- .enabled_layer_names(&layers) // requested layers
- .enabled_extension_names(&extensions)
- .flags(flags);
- // returns created entry with, info, and
- // custom allocator callbacks. here none
- Ok(entry.create_instance(&info, None)?)
- }
- // populates fields in struct
- pub unsafe fn create(window: &Window) -> Result<Self> {
- let loader = LibloadingLoader::new(LIBRARY)?;
- let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?;
- let instance = Self::create_instance(window, &entry)?;
- Ok(Self { entry, instance })
- }
- pub unsafe fn render(&mut self, window: &Window) -> Result<()> {
- Ok(())
- }
- pub unsafe fn destroy(&mut self) {
- self.instance.destroy_instance(None);
- }
- }
- // container with resources needed for vulkan
- #[derive(Clone, Debug, Default)]
- pub struct V_A_Data {}
Add Comment
Please, Sign In to add comment