Guest User

Untitled

a guest
Oct 19th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.32 KB | None | 0 0
  1. // used for constructing instances
  2. // of errors from ::anyhow
  3. use anyhow::{anyhow, Result};
  4. use winit::window::Window;
  5. use vulkanalia::{
  6.   Version,  // used for macos
  7.   loader::{LibloadingLoader, LIBRARY},
  8.   window as vk_window,
  9.   prelude::v1_0::*,
  10.   vk::ExtDebugUtilsExtensionInstanceCommands, // command wrapper
  11.                                              // for debug functionality
  12. };
  13. use log::*; // logging abstract
  14. use std::{
  15.   collections::HashSet, // needed for storing and querying
  16.                         // supported layers and others
  17.   ffi::CStr,
  18.   os::raw::c_void,
  19. };
  20.  
  21. // minimum version for macos devices
  22. // since it doesnt fully support vulkan
  23. // and instead partially translated to metal
  24. const PORTABILITY_MACOS_VERSION: Version = Version::new(1, 3, 216);
  25. // also, cfg is configurational condition check
  26. const VALIDATION_ENABLED: bool = cfg!(debug_assertions);
  27. const VALIDATION_LAYER: vk::ExtensionName =
  28.   vk::ExtensionName::from_bytes(b"VK_LAYERS_KHRONOS_validation");
  29.  
  30. // struct used for setting up the
  31. // rendering, setting, and destroying logics
  32. #[derive(Clone, Debug)]
  33. pub struct V_A {
  34.   entry: Entry,
  35.   instance: Instance,
  36. }
  37.  
  38. impl V_A {
  39.   // entry corresponds for vulkan entry
  40.   // which is created later
  41.   unsafe fn create_instance(window: &Window, entry: &Entry) -> Result<Instance> {
  42.     // most of this arguments r not that important
  43.     let app_info = vk::ApplicationInfo::builder()
  44.       .application_name(b"jesus is dead\0")
  45.       .application_version(vk::make_version(1, 0, 0))
  46.       .engine_name(b"brainfuck\0")
  47.       .engine_version(vk::make_version(1, 0, 0))
  48.       .api_version(vk::make_version(1, 0, 0));
  49.  
  50.     // collects available layers
  51.     // into hashset
  52.     let available_layers = entry
  53.       .enumerate_instance_layer_properties()?
  54.       .iter()
  55.       .map(|l| l.layer_name)
  56.       .collect::<HashSet<_>>();
  57.  
  58.     if VALIDATION_ENABLED && !available_layers.contains(&VALIDATION_LAYER) {
  59.       return Err(anyhow!("validation layers requested but not supported"));
  60.     }
  61.  
  62.     // layers data storing vector.
  63.     // checks if the v l is enabled and
  64.     // creates a list of available names
  65.     let layers = if VALIDATION_ENABLED {
  66.       vec![VALIDATION_LAYER.as_ptr()]
  67.     }
  68.     else {
  69.       Vec::new()
  70.     };
  71.  
  72.     // arguments to vulkan driver about
  73.     // global extensions and validation
  74.     // layers used
  75.     let mut extensions = vk_window::get_required_instance_extensions(window)
  76.       .iter()
  77.       .map(|e| e.as_ptr())
  78.       .collect::<Vec<_>>();
  79.  
  80.     // adds the name field from vk::..
  81.     // to the list of desired extension names
  82.     if VALIDATION_ENABLED {
  83.       extensions.push(vk::EXT_DEBUG_UTILS_EXTENSION.name.as_ptr());
  84.     }
  85.  
  86.     // enables KHR_PORTABILITY_ENUMERATION_EXTENSION
  87.     // for a platform that lacks vulkan compability
  88.     let flags = if
  89.       cfg!(target_os = "macos") && entry.version()? >= PORTABILITY_MACOS_VERSION {
  90.         info!("enables macos extensions needed for portability");
  91.         extensions.push(vk::KHR_GET_PHYSICAL_DEVICE_PROPERTIES2_EXTENSION.name.as_ptr());
  92.         extensions.push(vk::KHR_PORTABILITY_ENUMERATION_EXTENSION.name.as_ptr());
  93.         vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR
  94.     }
  95.     else {
  96.       vk::InstanceCreateFlags::empty()
  97.     };
  98.     // shit which creates instances from
  99.     // that previously made app_info, and os
  100.     // specific flags
  101.     let info = vk::InstanceCreateInfo::builder()
  102.       .application_info(&app_info)
  103.       .enabled_layer_names(&layers)  // requested layers
  104.       .enabled_extension_names(&extensions)
  105.       .flags(flags);
  106.     // returns created entry with, info, and
  107.     // custom allocator callbacks. here none
  108.     Ok(entry.create_instance(&info, None)?)
  109.   }
  110.  
  111.   // populates fields in struct
  112.   pub unsafe fn create(window: &Window) -> Result<Self> {
  113.     let loader = LibloadingLoader::new(LIBRARY)?;
  114.     let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?;
  115.     let instance = Self::create_instance(window, &entry)?;
  116.  
  117.     Ok(Self { entry, instance })
  118.   }
  119.  
  120.   pub unsafe fn render(&mut self, window: &Window) -> Result<()> {
  121.     Ok(())
  122.   }
  123.  
  124.   pub unsafe fn destroy(&mut self) {
  125.     self.instance.destroy_instance(None);
  126.   }
  127. }
  128.  
  129. // container with resources needed for vulkan
  130. #[derive(Clone, Debug, Default)]
  131. pub struct V_A_Data {}
Add Comment
Please, Sign In to add comment