Skip to content

Commit

Permalink
Use VkFence and VkSemaphore directly without any wrapper classes
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Jul 12, 2023
1 parent c26dbfa commit 4bd80c9
Show file tree
Hide file tree
Showing 24 changed files with 252 additions and 383 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ option(IGL_WITH_TRACY "Enable Tracy profiler" ON)
option(IGL_DEPLOY_DEPS "Deploy dependencies via CMake" ON)
# cmake-format: on

include(cmake/CommonMacros.txt)

if(DEFINED ENV{VULKAN_SDK})
message(STATUS "VULKAN_SDK=$ENV{VULKAN_SDK}")
if(NOT EXISTS $ENV{VULKAN_SDK})
Expand Down Expand Up @@ -73,6 +75,9 @@ if(IGL_WITH_TRACY)
igl_set_folder(TracyClient "third-party")
endif()

# temporary
find_package(Vulkan REQUIRED)

include_directories(.)

add_subdirectory(src/igl)
Expand Down
26 changes: 26 additions & 0 deletions cmake/CommonMacros.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# based on https://github.com/PacktPublishing/3D-Graphics-Rendering-Cookbook/blob/master/CMake/CommonMacros.txt
#

cmake_minimum_required(VERSION 3.16)

macro(lvk_setup_groups src_files)
foreach(FILE ${src_files})
get_filename_component(PARENT_DIR "${FILE}" PATH)

# skip src or include and changes /'s to \\'s
set(GROUP "${PARENT_DIR}")
string(REPLACE "/" "\\" GROUP "${GROUP}")

source_group("${GROUP}" FILES "${FILE}")
endforeach()
endmacro()

macro(lvk_set_folder target folder_name)
set_property(TARGET ${target} PROPERTY FOLDER ${folder_name})
endmacro()

macro(lvk_setup_target target)
set_property(TARGET ${target} PROPERTY CXX_STANDARD 20)
set_property(TARGET ${target} PROPERTY CXX_STANDARD_REQUIRED ON)
endmacro()
7 changes: 5 additions & 2 deletions lvk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ file(GLOB HEADER_FILES LIST_DIRECTORIES false RELATIVE ${CMAKE_CURRENT_SOURCE_DI

add_library(LVKLibrary ${SRC_FILES} ${HEADER_FILES})

lvk_setup_groups("${SRC_FILES}")
lvk_setup_groups("${HEADER_FILES}")

target_include_directories(LVKLibrary PUBLIC "${IGL_ROOT_DIR}/lvk")

igl_set_cxxstd(LVKLibrary 20)
igl_set_folder(LVKLibrary "LVK")
lvk_setup_target(LVKLibrary)
lvk_set_folder(LVKLibrary "LVK")

add_subdirectory(vulkan)

Expand Down
14 changes: 9 additions & 5 deletions lvk/LVK.h
Original file line number Diff line number Diff line change
Expand Up @@ -747,11 +747,15 @@ class IDevice {

virtual std::shared_ptr<ITexture> getCurrentSwapchainTexture() = 0;

virtual ShaderStages createShaderStages(const char* vs,
const char* debugNameVS,
const char* fs,
const char* debugNameFS,
Result* outResult = nullptr);
ShaderStages createShaderStages(const char* vs,
const char* debugNameVS,
const char* fs,
const char* debugNameFS,
Result* outResult = nullptr) {
auto VS = createShaderModule(igl::ShaderModuleDesc(vs, Stage_Vertex, debugNameVS), outResult);
auto FS = createShaderModule(igl::ShaderModuleDesc(fs, Stage_Fragment, debugNameFS), outResult);
return igl::ShaderStages(VS, FS);
}

protected:
IDevice() = default;
Expand Down
26 changes: 24 additions & 2 deletions lvk/vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,34 @@ file(GLOB HEADER_FILES LIST_DIRECTORIES false RELATIVE ${CMAKE_CURRENT_SOURCE_DI

add_library(LVKVulkan ${SRC_FILES} ${HEADER_FILES})

igl_set_cxxstd(LVKVulkan 20)
igl_set_folder(LVKVulkan "LVK")
lvk_setup_target(LVKVulkan)
lvk_set_folder(LVKVulkan "LVK")

lvk_setup_groups("${SRC_FILES}")
lvk_setup_groups("${HEADER_FILES}")

# glslang
# cmake-format: off
set(ENABLE_GLSLANG_BINARIES OFF CACHE BOOL "")
set(ENABLE_HLSL OFF CACHE BOOL "")
set(ENABLE_CTEST OFF CACHE BOOL "")
set(ENABLE_OPT OFF CACHE BOOL "")
set(ENABLE_SPVREMAPPER OFF CACHE BOOL "")
set(SKIP_GLSLANG_INSTALL ON CACHE BOOL "")
add_subdirectory(${IGL_ROOT_DIR}/third-party/deps/src/glslang "glslang")
lvk_set_folder(GenericCodeGen "third-party/glslang")
lvk_set_folder(glslang "third-party/glslang")
lvk_set_folder(MachineIndependent "third-party/glslang")
lvk_set_folder(OGLCompiler "third-party/glslang")
lvk_set_folder(OSDependent "third-party/glslang")
lvk_set_folder(SPIRV "third-party/glslang")
lvk_set_folder(glslang-default-resource-limits "third-party/glslang")
# cmake-format: on

find_package(Vulkan REQUIRED)

target_link_libraries(LVKVulkan PRIVATE IGLLibrary)
target_link_libraries(LVKVulkan PUBLIC glslang SPIRV glslang-default-resource-limits)
target_link_libraries(LVKVulkan PUBLIC Vulkan::Vulkan)

target_include_directories(LVKVulkan PUBLIC "${IGL_ROOT_DIR}/third-party/deps/src/volk")
Expand Down
136 changes: 136 additions & 0 deletions lvk/vulkan/VulkanUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@
#include <igl/vulkan/Common.h>
#include <igl/vulkan/VulkanContext.h>

VkSemaphore lvk::createSemaphore(VkDevice device, const char* debugName) {
const VkSemaphoreCreateInfo ci = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.flags = 0,
};
VkSemaphore semaphore = VK_NULL_HANDLE;
VK_ASSERT(vkCreateSemaphore(device, &ci, nullptr, &semaphore));
VK_ASSERT(
ivkSetDebugObjectName(device, VK_OBJECT_TYPE_SEMAPHORE, (uint64_t)semaphore, debugName));
return semaphore;
}

VkFence lvk::createFence(VkDevice device, const char* debugName) {
const VkFenceCreateInfo ci = {
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.flags = 0,
};
VkFence fence = VK_NULL_HANDLE;
VK_ASSERT(vkCreateFence(device, &ci, nullptr, &fence));
VK_ASSERT(ivkSetDebugObjectName(device, VK_OBJECT_TYPE_FENCE, (uint64_t)fence, debugName));
return fence;
}

uint32_t lvk::findQueueFamilyIndex(VkPhysicalDevice physDev, VkQueueFlags flags) {
using igl::vulkan::DeviceQueues;

Expand Down Expand Up @@ -97,3 +120,116 @@ VmaAllocator lvk::createVmaAllocator(VkPhysicalDevice physDev,
VK_ASSERT(vmaCreateAllocator(&ci, &vma));
return vma;
}

glslang_resource_t lvk::getGlslangResource(const VkPhysicalDeviceLimits& limits) {
const glslang_resource_t resource = {
.max_lights = 32,
.max_clip_planes = 6,
.max_texture_units = 32,
.max_texture_coords = 32,
.max_vertex_attribs = (int)limits.maxVertexInputAttributes,
.max_vertex_uniform_components = 4096,
.max_varying_floats = 64,
.max_vertex_texture_image_units = 32,
.max_combined_texture_image_units = 80,
.max_texture_image_units = 32,
.max_fragment_uniform_components = 4096,
.max_draw_buffers = 32,
.max_vertex_uniform_vectors = 128,
.max_varying_vectors = 8,
.max_fragment_uniform_vectors = 16,
.max_vertex_output_vectors = 16,
.max_fragment_input_vectors = 15,
.min_program_texel_offset = -8,
.max_program_texel_offset = 7,
.max_clip_distances = (int)limits.maxClipDistances,
.max_compute_work_group_count_x = (int)limits.maxComputeWorkGroupCount[0],
.max_compute_work_group_count_y = (int)limits.maxComputeWorkGroupCount[1],
.max_compute_work_group_count_z = (int)limits.maxComputeWorkGroupCount[2],
.max_compute_work_group_size_x = (int)limits.maxComputeWorkGroupSize[0],
.max_compute_work_group_size_y = (int)limits.maxComputeWorkGroupSize[1],
.max_compute_work_group_size_z = (int)limits.maxComputeWorkGroupSize[2],
.max_compute_uniform_components = 1024,
.max_compute_texture_image_units = 16,
.max_compute_image_uniforms = 8,
.max_compute_atomic_counters = 8,
.max_compute_atomic_counter_buffers = 1,
.max_varying_components = 60,
.max_vertex_output_components = (int)limits.maxVertexOutputComponents,
.max_geometry_input_components = (int)limits.maxGeometryInputComponents,
.max_geometry_output_components = (int)limits.maxGeometryOutputComponents,
.max_fragment_input_components = (int)limits.maxFragmentInputComponents,
.max_image_units = 8,
.max_combined_image_units_and_fragment_outputs = 8,
.max_combined_shader_output_resources = 8,
.max_image_samples = 0,
.max_vertex_image_uniforms = 0,
.max_tess_control_image_uniforms = 0,
.max_tess_evaluation_image_uniforms = 0,
.max_geometry_image_uniforms = 0,
.max_fragment_image_uniforms = 8,
.max_combined_image_uniforms = 8,
.max_geometry_texture_image_units = 16,
.max_geometry_output_vertices = (int)limits.maxGeometryOutputVertices,
.max_geometry_total_output_components = (int)limits.maxGeometryTotalOutputComponents,
.max_geometry_uniform_components = 1024,
.max_geometry_varying_components = 64,
.max_tess_control_input_components =
(int)limits.maxTessellationControlPerVertexInputComponents,
.max_tess_control_output_components =
(int)limits.maxTessellationControlPerVertexOutputComponents,
.max_tess_control_texture_image_units = 16,
.max_tess_control_uniform_components = 1024,
.max_tess_control_total_output_components = 4096,
.max_tess_evaluation_input_components = (int)limits.maxTessellationEvaluationInputComponents,
.max_tess_evaluation_output_components =
(int)limits.maxTessellationEvaluationOutputComponents,
.max_tess_evaluation_texture_image_units = 16,
.max_tess_evaluation_uniform_components = 1024,
.max_tess_patch_components = 120,
.max_patch_vertices = 32,
.max_tess_gen_level = 64,
.max_viewports = (int)limits.maxViewports,
.max_vertex_atomic_counters = 0,
.max_tess_control_atomic_counters = 0,
.max_tess_evaluation_atomic_counters = 0,
.max_geometry_atomic_counters = 0,
.max_fragment_atomic_counters = 8,
.max_combined_atomic_counters = 8,
.max_atomic_counter_bindings = 1,
.max_vertex_atomic_counter_buffers = 0,
.max_tess_control_atomic_counter_buffers = 0,
.max_tess_evaluation_atomic_counter_buffers = 0,
.max_geometry_atomic_counter_buffers = 0,
.max_fragment_atomic_counter_buffers = 1,
.max_combined_atomic_counter_buffers = 1,
.max_atomic_counter_buffer_size = 16384,
.max_transform_feedback_buffers = 4,
.max_transform_feedback_interleaved_components = 64,
.max_cull_distances = (int)limits.maxCullDistances,
.max_combined_clip_and_cull_distances = (int)limits.maxCombinedClipAndCullDistances,
.max_samples = 4,
.max_mesh_output_vertices_nv = 256,
.max_mesh_output_primitives_nv = 512,
.max_mesh_work_group_size_x_nv = 32,
.max_mesh_work_group_size_y_nv = 1,
.max_mesh_work_group_size_z_nv = 1,
.max_task_work_group_size_x_nv = 32,
.max_task_work_group_size_y_nv = 1,
.max_task_work_group_size_z_nv = 1,
.max_mesh_view_count_nv = 4,
.maxDualSourceDrawBuffersEXT = 1,
.limits = {
.non_inductive_for_loops = true,
.while_loops = true,
.do_while_loops = true,
.general_uniform_indexing = true,
.general_attribute_matrix_vector_indexing = true,
.general_varying_indexing = true,
.general_sampler_indexing = true,
.general_variable_indexing = true,
.general_constant_matrix_vector_indexing = true,
}};

return resource;
}
5 changes: 5 additions & 0 deletions lvk/vulkan/VulkanUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@

#include <volk.h>
#include <vk_mem_alloc.h>
#include <glslang/Include/glslang_c_interface.h>

namespace lvk {

VkSemaphore createSemaphore(VkDevice device, const char* debugName);
VkFence createFence(VkDevice device, const char* debugName);
VmaAllocator createVmaAllocator(VkPhysicalDevice physDev,
VkDevice device,
VkInstance instance,
uint32_t apiVersion);
uint32_t findQueueFamilyIndex(VkPhysicalDevice physDev, VkQueueFlags flags);

glslang_resource_t getGlslangResource(const VkPhysicalDeviceLimits& limits);

} // namespace lvk
10 changes: 0 additions & 10 deletions src/igl/IGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,4 @@ uint32_t calcNumMipLevels(uint32_t width, uint32_t height) {
return levels;
}

ShaderStages IDevice::createShaderStages(const char* vs,
const char* debugNameVS,
const char* fs,
const char* debugNameFS,
Result* outResult) {
auto VS = createShaderModule(igl::ShaderModuleDesc(vs, Stage_Vertex, debugNameVS), outResult);
auto FS = createShaderModule(igl::ShaderModuleDesc(fs, Stage_Fragment, debugNameFS), outResult);
return igl::ShaderStages(VS, FS);
}

} // namespace igl
23 changes: 3 additions & 20 deletions src/igl/vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,16 @@ add_library(IGLVulkan ${SRC_FILES} ${HEADER_FILES})
igl_set_cxxstd(IGLVulkan 20)
igl_set_folder(IGLVulkan "IGL")

# glslang
# cmake-format: off
set(ENABLE_GLSLANG_BINARIES OFF CACHE BOOL "")
set(ENABLE_HLSL OFF CACHE BOOL "")
set(ENABLE_CTEST OFF CACHE BOOL "")
set(ENABLE_OPT OFF CACHE BOOL "")
set(ENABLE_SPVREMAPPER OFF CACHE BOOL "")
set(SKIP_GLSLANG_INSTALL ON CACHE BOOL "")
add_subdirectory(${IGL_ROOT_DIR}/third-party/deps/src/glslang "glslang")
igl_set_folder(GenericCodeGen "third-party/glslang")
igl_set_folder(glslang "third-party/glslang")
igl_set_folder(MachineIndependent "third-party/glslang")
igl_set_folder(OGLCompiler "third-party/glslang")
igl_set_folder(OSDependent "third-party/glslang")
igl_set_folder(SPIRV "third-party/glslang")
igl_set_folder(glslang-default-resource-limits "third-party/glslang")
# cmake-format: on

find_package(Vulkan REQUIRED)

target_link_libraries(IGLVulkan PRIVATE IGLLibrary)
target_link_libraries(IGLVulkan PUBLIC glslang SPIRV glslang-default-resource-limits)
target_link_libraries(IGLVulkan PUBLIC Vulkan::Vulkan)

target_include_directories(IGLVulkan PUBLIC "${IGL_ROOT_DIR}/third-party/deps/src/volk")
target_include_directories(IGLVulkan PUBLIC "${IGL_ROOT_DIR}/third-party/deps/src/vma/include")

target_link_libraries(IGLVulkan PRIVATE LVKLibrary)
target_link_libraries(IGLVulkan PRIVATE LVKVulkan)

if(WIN32)
add_definitions("-DVK_USE_PLATFORM_WIN32_KHR=1")
add_definitions("-DNOMINMAX")
Expand Down
6 changes: 3 additions & 3 deletions src/igl/vulkan/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void Device::submit(const igl::ICommandBuffer& commandBuffer,
// Submit to the graphics queue.
const bool shouldPresent = isGraphicsQueue && ctx.hasSwapchain() && present;
if (shouldPresent) {
ctx.immediate_->waitSemaphore(ctx.swapchain_->acquireSemaphore_->vkSemaphore_);
ctx.immediate_->waitSemaphore(ctx.swapchain_->acquireSemaphore_);
}

vkCmdBuffer->lastSubmitHandle_ = ctx.immediate_->submit(vkCmdBuffer->wrapper_);
Expand Down Expand Up @@ -293,8 +293,8 @@ std::shared_ptr<VulkanShaderModule> Device::createShaderModule(ShaderStage stage
source = sourcePatched.c_str();
}

glslang_resource_t glslangResource;
ivkGlslangResource(&glslangResource, &ctx_->getVkPhysicalDeviceProperties());
const glslang_resource_t glslangResource =
lvk::getGlslangResource(ctx_->getVkPhysicalDeviceProperties().limits);

VkShaderModule vkShaderModule = VK_NULL_HANDLE;
const Result result =
Expand Down
1 change: 0 additions & 1 deletion src/igl/vulkan/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include <lvk/LVK.h>
#include <igl/vulkan/Common.h>
#include <igl/vulkan/VulkanSemaphore.h>
#include <memory>
#include <vector>

Expand Down
6 changes: 2 additions & 4 deletions src/igl/vulkan/VulkanContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <igl/vulkan/VulkanPipelineBuilder.h>
#include <igl/vulkan/VulkanPipelineLayout.h>
#include <igl/vulkan/VulkanSampler.h>
#include <igl/vulkan/VulkanSemaphore.h>
#include <igl/vulkan/VulkanSwapchain.h>
#include <igl/vulkan/VulkanTexture.h>
#include <lvk/vulkan/VulkanUtils.h>
Expand Down Expand Up @@ -530,9 +529,8 @@ igl::Result VulkanContext::initContext(const HWDeviceDesc& desc) {

// Create Vulkan Memory Allocator
if (IGL_VULKAN_USE_VMA) {
pimpl_->vma_ =
lvk::createVmaAllocator(vkPhysicalDevice_, vkDevice_, vkInstance_, apiVersion);
IGL_ASSERT(pimpl_->vma_ != VK_NULL_HANDLE);
pimpl_->vma_ = lvk::createVmaAllocator(vkPhysicalDevice_, vkDevice_, vkInstance_, apiVersion);
IGL_ASSERT(pimpl_->vma_ != VK_NULL_HANDLE);
}

// The staging device will use VMA to allocate a buffer, so this needs
Expand Down
Loading

0 comments on commit 4bd80c9

Please sign in to comment.