Skip to content

Commit

Permalink
Initializes Vulkan Instance (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Aug 13, 2024
1 parent 7f04eee commit bee12a8
Show file tree
Hide file tree
Showing 14 changed files with 617 additions and 34 deletions.
58 changes: 39 additions & 19 deletions .github/workflows/ci-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,9 @@ jobs:
- name: Generate cache keys
run: |
echo "cargo=${{ runner.os }}-cargo" >> $env:GITHUB_OUTPUT
echo "vulkan=${{ runner.os }}-vulkan-1.3.239.0" >> $env:GITHUB_OUTPUT
echo "qt=${{ runner.os }}-qt-6.7.2" >> $env:GITHUB_OUTPUT
echo "vulkan=${{ runner.os }}-vulkan-1.3.290.0" >> $env:GITHUB_OUTPUT
id: cache-keys
- name: Install Qt
run: |
Invoke-WebRequest `
-Uri "https://download.qt.io/online/qtsdkrepository/windows_x86/desktop/qt6_660/qt.qt6.660.win64_msvc2019_64/6.6.0-0-202310040911qtbase-Windows-Windows_10_22H2-MSVC2019-Windows-Windows_10_22H2-X86_64.7z" `
-OutFile qt-base.7z
Invoke-WebRequest `
-Uri "https://download.qt.io/online/qtsdkrepository/windows_x86/desktop/qt6_660/qt.qt6.660.win64_msvc2019_64/6.6.0-0-202310040911qtsvg-Windows-Windows_10_22H2-MSVC2019-Windows-Windows_10_22H2-X86_64.7z" `
-OutFile qt-svg.7z
7z x qt-base.7z -oqt
7z x qt-svg.7z -oqt
echo "CMAKE_PREFIX_PATH=qt\6.6.0\msvc2019_64" >> $env:GITHUB_ENV
- name: Restore Cargo home
uses: actions/cache/restore@v4
with:
path: ~/.cargo
key: ${{ steps.cache-keys.outputs.cargo }}
- name: Restore Vulkan SDK
uses: actions/cache/restore@v4
with:
Expand All @@ -41,13 +26,42 @@ jobs:
id: restore-vulkan
- name: Install Vulkan SDK
run: |
Invoke-WebRequest -Uri "https://sdk.lunarg.com/sdk/download/1.3.239.0/windows/VulkanSDK-1.3.239.0-Installer.exe" -OutFile VulkanSDK.exe
Invoke-WebRequest -Uri "https://sdk.lunarg.com/sdk/download/1.3.290.0/windows/VulkanSDK-1.3.290.0-Installer.exe" -OutFile VulkanSDK.exe
.\VulkanSDK.exe --root C:\VulkanSDK --accept-licenses --default-answer --confirm-command install
echo "new-install=true" >> $env:GITHUB_OUTPUT
id: install-vulkan
if: ${{ steps.restore-vulkan.outputs.cache-hit != 'true' }}
- name: Set Vulkan SDK path
run: echo "VULKAN_SDK=C:\VulkanSDK" >> $env:GITHUB_ENV
run: echo "VULKAN_SDK=C:\VulkanSDK" >> $env:GITHUB_ENV
- name: Restore Qt
uses: actions/cache/restore@v4
with:
path: qt
key: ${{ steps.cache-keys.outputs.qt }}
id: restore-qt
- name: Download Qt
run: |
Invoke-WebRequest `
-Uri "https://download.qt.io/official_releases/qt/6.7/6.7.2/single/qt-everywhere-src-6.7.2.tar.xz" `
-OutFile qt.tar.xz
7z x -so qt.tar.xz | 7z x -si -ttar
rm qt.tar.xz
mkdir qt-build
if: ${{ steps.restore-qt.outputs.cache-hit != 'true' }}
- name: Build Qt
run: |
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
${{ github.workspace }}\qt-everywhere-src-6.7.2\configure.bat -prefix "${{ github.workspace }}\qt" -no-openssl && cmake --build . --parallel && cmake --install .
shell: cmd
working-directory: qt-build
if: ${{ steps.restore-qt.outputs.cache-hit != 'true' }}
- name: Set Qt path
run: echo "CMAKE_PREFIX_PATH=qt" >> $env:GITHUB_ENV
- name: Restore Cargo home
uses: actions/cache/restore@v4
with:
path: ~/.cargo
key: ${{ steps.cache-keys.outputs.cargo }}
- name: Update Rust
run: rustup update stable
- name: Add additional Rust targets
Expand Down Expand Up @@ -78,3 +92,9 @@ jobs:
path: ~/.cargo
key: ${{ steps.cache-keys.outputs.cargo }}-${{ github.run_id }}
if: startsWith(github.ref, 'refs/heads/')
- name: Cache Qt
uses: actions/cache/save@v4
with:
path: qt
key: ${{ steps.cache-keys.outputs.qt }}
if: startsWith(github.ref, 'refs/heads/') && steps.restore-qt.outputs.cache-hit != 'true'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ We have a Discord server for discussion about Obliteration and its development.
## System requirements

- Windows 10, Linux or macOS 11+.
- On Windows and Linux make sure you have Vulkan installed. If you encountered `Failed to initialize Vulkan (-9)` that mean you don't have a Vulkan installed.
- On Windows and Linux make sure you have Vulkan 1.3 installed. If you encountered `Failed to initialize Vulkan (-9)` that mean you don't have a Vulkan installed.
- x86-64 CPU. We want to support non-x86 but currently we don't have any developers who are willing to work on this.
- CPU with hardware virtualization supports.
- Windows and Linux users may need to enable this feature on the BIOS/UEFI settings.
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ add_executable(obliteration WIN32 MACOSX_BUNDLE
system.cpp)

if(WIN32)
target_sources(obliteration PRIVATE resources.rc)
target_sources(obliteration PRIVATE resources.rc vulkan.cpp)
elseif(APPLE)
target_sources(obliteration PRIVATE resources/obliteration.icns)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
target_sources(obliteration PRIVATE kvm.cpp)
target_sources(obliteration PRIVATE kvm.cpp vulkan.cpp)
endif()

add_dependencies(obliteration core)
Expand Down
20 changes: 19 additions & 1 deletion src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ struct RustError;
*/
struct Vmm;

/**
* Contains objects required to render the screen.
*/
struct VmmScreen {
#if !defined(__APPLE__)
size_t vk_instance
#endif
;
#if !defined(__APPLE__)
size_t vk_surface
#endif
;
#if defined(__APPLE__)
size_t view
#endif
;
};

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand Down Expand Up @@ -74,7 +92,7 @@ struct RustError *update_firmware(const char *root,

void vmm_free(struct Vmm *vmm);

struct Vmm *vmm_run(const char *kernel, size_t screen, struct RustError **err);
struct Vmm *vmm_run(const char *kernel, const struct VmmScreen *screen, struct RustError **err);

struct RustError *vmm_draw(struct Vmm *vmm);

Expand Down
3 changes: 3 additions & 0 deletions src/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ param = { path = "../param" }
pkg = { path = "../pkg" }
thiserror = "1.0"

[target.'cfg(not(target_os = "macos"))'.dependencies]
ash = "0.38.0"

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.52.0"
features = [
Expand Down
1 change: 1 addition & 0 deletions src/core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn main() {

conf.after_includes = Some(buf);
conf.pragma_once = true;
conf.tab_width = 4;
conf.language = Language::C;
conf.cpp_compat = true;
conf.style = Style::Tag;
Expand Down
13 changes: 12 additions & 1 deletion src/core/src/vmm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub unsafe extern "C" fn vmm_free(vmm: *mut Vmm) {
#[no_mangle]
pub unsafe extern "C" fn vmm_run(
kernel: *const c_char,
screen: usize,
screen: *const VmmScreen,
err: *mut *mut RustError,
) -> *mut Vmm {
// Check if path UTF-8.
Expand Down Expand Up @@ -525,6 +525,17 @@ impl Drop for Vmm {
}
}

/// Contains objects required to render the screen.
#[repr(C)]
pub struct VmmScreen {
#[cfg(not(target_os = "macos"))]
pub vk_instance: usize,
#[cfg(not(target_os = "macos"))]
pub vk_surface: usize,
#[cfg(target_os = "macos")]
pub view: usize,
}

/// Object that has a physical address in the virtual machine.
trait MemoryAddr {
/// Physical address in the virtual machine.
Expand Down
5 changes: 3 additions & 2 deletions src/core/src/vmm/screen/metal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use self::buffer::MetalBuffer;
use super::{Screen, ScreenBuffer, VmmError};
use crate::vmm::VmmScreen;
use metal::{CAMetalLayer, Device, MetalLayer};
use objc::runtime::{Object, NO, YES};
use objc::{msg_send, sel, sel_impl};
Expand All @@ -20,7 +21,7 @@ pub struct Metal {
}

impl Metal {
pub fn new(surface: usize) -> Result<Self, VmmError> {
pub fn new(screen: *const VmmScreen) -> Result<Self, VmmError> {
// Get Metal device.
let device = match Device::system_default() {
Some(v) => v,
Expand All @@ -33,7 +34,7 @@ impl Metal {
layer.set_device(&device);

// Set view layer.
let view = surface as *mut Object;
let view = unsafe { (*screen).view as *mut Object };

let _: () = unsafe { msg_send![view, setLayer:layer.as_ref()] };
let _: () = unsafe { msg_send![view, setWantsLayer:YES] };
Expand Down
186 changes: 186 additions & 0 deletions src/core/src/vmm/screen/vulkan/ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
use ash::vk::{
AllocationCallbacks, Device, DeviceCreateInfo, ExtensionProperties, ExternalBufferProperties,
ExternalFenceProperties, ExternalSemaphoreProperties, Format, FormatProperties,
FormatProperties2, ImageCreateFlags, ImageFormatProperties, ImageFormatProperties2,
ImageTiling, ImageType, ImageUsageFlags, Instance, LayerProperties, PFN_vkVoidFunction,
PhysicalDevice, PhysicalDeviceExternalBufferInfo, PhysicalDeviceExternalFenceInfo,
PhysicalDeviceExternalSemaphoreInfo, PhysicalDeviceFeatures, PhysicalDeviceFeatures2,
PhysicalDeviceGroupProperties, PhysicalDeviceImageFormatInfo2, PhysicalDeviceMemoryProperties,
PhysicalDeviceMemoryProperties2, PhysicalDeviceProperties, PhysicalDeviceProperties2,
PhysicalDeviceSparseImageFormatInfo2, PhysicalDeviceToolProperties, QueueFamilyProperties,
QueueFamilyProperties2, Result, SampleCountFlags, SparseImageFormatProperties,
SparseImageFormatProperties2,
};
use std::ffi::c_char;

extern "system" {
#[link_name = "vmm_vk_create_device"]
pub fn create_device(
physical_device: PhysicalDevice,
p_create_info: *const DeviceCreateInfo<'_>,
p_allocator: *const AllocationCallbacks<'_>,
p_device: *mut Device,
) -> Result;

#[link_name = "vmm_vk_enumerate_device_extension_properties"]
pub fn enumerate_device_extension_properties(
physical_device: PhysicalDevice,
p_layer_name: *const c_char,
p_property_count: *mut u32,
p_properties: *mut ExtensionProperties,
) -> Result;

#[link_name = "vmm_vk_enumerate_device_layer_properties"]
pub fn enumerate_device_layer_properties(
physical_device: PhysicalDevice,
p_property_count: *mut u32,
p_properties: *mut LayerProperties,
) -> Result;

#[link_name = "vmm_vk_enumerate_physical_device_groups"]
pub fn enumerate_physical_device_groups(
instance: Instance,
p_physical_device_group_count: *mut u32,
p_physical_device_group_properties: *mut PhysicalDeviceGroupProperties<'_>,
) -> Result;

#[link_name = "vmm_vk_enumerate_physical_devices"]
pub fn enumerate_physical_devices(
instance: Instance,
p_physical_device_count: *mut u32,
p_physical_devices: *mut PhysicalDevice,
) -> Result;

#[link_name = "vmm_vk_get_device_proc_addr"]
pub fn get_device_proc_addr(device: Device, p_name: *const c_char) -> PFN_vkVoidFunction;

#[link_name = "vmm_vk_get_physical_device_external_buffer_properties"]
pub fn get_physical_device_external_buffer_properties(
physical_device: PhysicalDevice,
p_external_buffer_info: *const PhysicalDeviceExternalBufferInfo<'_>,
p_external_buffer_properties: *mut ExternalBufferProperties<'_>,
);

#[link_name = "vmm_vk_get_physical_device_external_fence_properties"]
pub fn get_physical_device_external_fence_properties(
physical_device: PhysicalDevice,
p_external_fence_info: *const PhysicalDeviceExternalFenceInfo<'_>,
p_external_fence_properties: *mut ExternalFenceProperties<'_>,
);

#[link_name = "vmm_vk_get_physical_device_external_semaphore_properties"]
pub fn get_physical_device_external_semaphore_properties(
physical_device: PhysicalDevice,
p_external_semaphore_info: *const PhysicalDeviceExternalSemaphoreInfo<'_>,
p_external_semaphore_properties: *mut ExternalSemaphoreProperties<'_>,
);

#[link_name = "vmm_vk_get_physical_device_features"]
pub fn get_physical_device_features(
physical_device: PhysicalDevice,
p_features: *mut PhysicalDeviceFeatures,
);

#[link_name = "vmm_vk_get_physical_device_features2"]
pub fn get_physical_device_features2(
physical_device: PhysicalDevice,
p_features: *mut PhysicalDeviceFeatures2<'_>,
);

#[link_name = "vmm_vk_get_physical_device_format_properties"]
pub fn get_physical_device_format_properties(
physical_device: PhysicalDevice,
format: Format,
p_format_properties: *mut FormatProperties,
);

#[link_name = "vmm_vk_get_physical_device_format_properties2"]
pub fn get_physical_device_format_properties2(
physical_device: PhysicalDevice,
format: Format,
p_format_properties: *mut FormatProperties2<'_>,
);

#[link_name = "vmm_vk_get_physical_device_image_format_properties"]
pub fn get_physical_device_image_format_properties(
physical_device: PhysicalDevice,
format: Format,
ty: ImageType,
tiling: ImageTiling,
usage: ImageUsageFlags,
flags: ImageCreateFlags,
p_image_format_properties: *mut ImageFormatProperties,
) -> Result;

#[link_name = "vmm_vk_get_physical_device_image_format_properties2"]
pub fn get_physical_device_image_format_properties2(
physical_device: PhysicalDevice,
p_image_format_info: *const PhysicalDeviceImageFormatInfo2<'_>,
p_image_format_properties: *mut ImageFormatProperties2<'_>,
) -> Result;

#[link_name = "vmm_vk_get_physical_device_memory_properties"]
pub fn get_physical_device_memory_properties(
physical_device: PhysicalDevice,
p_memory_properties: *mut PhysicalDeviceMemoryProperties,
);

#[link_name = "vmm_vk_get_physical_device_memory_properties2"]
pub fn get_physical_device_memory_properties2(
physical_device: PhysicalDevice,
p_memory_properties: *mut PhysicalDeviceMemoryProperties2<'_>,
);

#[link_name = "vmm_vk_get_physical_device_properties"]
pub fn get_physical_device_properties(
physical_device: PhysicalDevice,
p_properties: *mut PhysicalDeviceProperties,
);

#[link_name = "vmm_vk_get_physical_device_properties2"]
pub fn get_physical_device_properties2(
physical_device: PhysicalDevice,
p_properties: *mut PhysicalDeviceProperties2<'_>,
);

#[link_name = "vmm_vk_get_physical_device_queue_family_properties"]
pub fn get_physical_device_queue_family_properties(
physical_device: PhysicalDevice,
p_queue_family_property_count: *mut u32,
p_queue_family_properties: *mut QueueFamilyProperties,
);

#[link_name = "vmm_vk_get_physical_device_queue_family_properties2"]
pub fn get_physical_device_queue_family_properties2(
physical_device: PhysicalDevice,
p_queue_family_property_count: *mut u32,
p_queue_family_properties: *mut QueueFamilyProperties2<'_>,
);

#[link_name = "vmm_vk_get_physical_device_sparse_image_format_properties"]
pub fn get_physical_device_sparse_image_format_properties(
physical_device: PhysicalDevice,
format: Format,
ty: ImageType,
samples: SampleCountFlags,
usage: ImageUsageFlags,
tiling: ImageTiling,
p_property_count: *mut u32,
p_properties: *mut SparseImageFormatProperties,
);

#[link_name = "vmm_vk_get_physical_device_sparse_image_format_properties2"]
pub fn get_physical_device_sparse_image_format_properties2(
physical_device: PhysicalDevice,
p_format_info: *const PhysicalDeviceSparseImageFormatInfo2<'_>,
p_property_count: *mut u32,
p_properties: *mut SparseImageFormatProperties2<'_>,
);

#[link_name = "vmm_vk_get_physical_device_tool_properties"]
pub fn get_physical_device_tool_properties(
physical_device: PhysicalDevice,
p_tool_count: *mut u32,
p_tool_properties: *mut PhysicalDeviceToolProperties<'_>,
) -> Result;
}
Loading

0 comments on commit bee12a8

Please sign in to comment.