Skip to content

Commit

Permalink
Merge pull request #25 from corporateshark/spirv-reflect
Browse files Browse the repository at this point in the history
Create `VkPipelineLayout` object per each individual LVK pipeline state
  • Loading branch information
corporateshark authored Feb 18, 2024
2 parents a45ff2c + d72549a commit 22c740d
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 144 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,6 @@ jobs:
run: |
cmake -G "Unix Makefiles" -S "${{ github.workspace }}" -B build -DLVK_WITH_SAMPLES_ANDROID=ON
- name: Assemble HelloTriangle APK
run: |
cd build/android/HelloTriangle
chmod +x ./gradlew
./gradlew assembleDebug
- name: Assemble Tiny APK
run: |
cd build/android/Tiny
chmod +x ./gradlew
./gradlew assembleDebug
- name: Assemble Tiny_Mesh APK
run: |
cd build/android/Tiny_Mesh
Expand Down
8 changes: 8 additions & 0 deletions lvk/vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ lvk_set_folder(SPIRV "third-party/glslang")
lvk_set_folder(glslang-default-resource-limits "third-party/glslang")
# cmake-format: on

# SPIRV-Reflect
set(SPIRV_REFLECT_EXECUTABLE OFF CACHE BOOL "")
set(SPIRV_REFLECT_STATIC_LIB ON CACHE BOOL "")
add_subdirectory(${LVK_ROOT_DIR}/third-party/deps/src/SPIRV-Reflect "SPIRV-Reflect")
lvk_set_folder(spirv-reflect-static "third-party")

if(NOT LVK_USE_CUSTOM_MOLTENVK)
find_package(Vulkan REQUIRED)
endif()
Expand All @@ -45,6 +51,8 @@ endif()

target_link_libraries(LVKVulkan PRIVATE LVKLibrary)
target_link_libraries(LVKVulkan PRIVATE glslang SPIRV glslang-default-resource-limits)
target_link_libraries(LVKVulkan PRIVATE spirv-reflect-static)

if(LVK_USE_CUSTOM_MOLTENVK)
target_include_directories(LVKVulkan PUBLIC "${LVK_CUSTOM_MOLTENVK_PATH}/include")
target_link_libraries(LVKVulkan PUBLIC "${LVK_CUSTOM_MOLTENVK_PATH}/dylib/macOS/libMoltenVK.dylib")
Expand Down
291 changes: 184 additions & 107 deletions lvk/vulkan/VulkanClasses.cpp

Large diffs are not rendered by default.

31 changes: 20 additions & 11 deletions lvk/vulkan/VulkanClasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,11 @@ struct RenderPipelineState final {
VkVertexInputBindingDescription vkBindings_[VertexInput::LVK_VERTEX_BUFFER_MAX] = {};
VkVertexInputAttributeDescription vkAttributes_[VertexInput::LVK_VERTEX_ATTRIBUTES_MAX] = {};

// non-owning, cached the last pipeline layout from the context (if the context has a new layout, invalidate all VkPipeline objects)
VkPipelineLayout pipelineLayout_ = VK_NULL_HANDLE;
// non-owning, the last seen VkDescriptorSetLayout from VulkanContext::vkDSL_ (if the context has a new layout, invalidate all VkPipeline objects)
VkDescriptorSetLayout lastVkDescriptorSetLayout_ = VK_NULL_HANDLE;

VkShaderStageFlags shaderStageFlags_ = 0;
VkPipelineLayout pipelineLayout_ = VK_NULL_HANDLE;
VkPipeline pipeline_ = VK_NULL_HANDLE;
};

Expand Down Expand Up @@ -335,12 +337,19 @@ class VulkanPipelineBuilder final {

struct ComputePipelineState final {
ComputePipelineDesc desc_;
// non-owning, cached the last pipeline layout from the context
VkPipelineLayout pipelineLayout_ = VK_NULL_HANDLE;

// non-owning, the last seen VkDescriptorSetLayout from VulkanContext::vkDSL_ (invalidate all VkPipeline objects on new layout)
VkDescriptorSetLayout lastVkDescriptorSetLayout_ = VK_NULL_HANDLE;

VkPipelineLayout pipelineLayout_ = VK_NULL_HANDLE;
VkPipeline pipeline_ = VK_NULL_HANDLE;
};

struct ShaderModuleState final {
VkShaderModule sm = VK_NULL_HANDLE;
uint32_t pushConstantsSize = 0;
};

class CommandBuffer final : public ICommandBuffer {
public:
CommandBuffer() = default;
Expand Down Expand Up @@ -417,7 +426,8 @@ class CommandBuffer final : public ICommandBuffer {

bool isRendering_ = false;

lvk::RenderPipelineHandle currentPipeline_ = {};
lvk::RenderPipelineHandle currentPipelineGraphics_ = {};
lvk::ComputePipelineHandle currentPipelineCompute_ = {};
};

class VulkanStagingDevice final {
Expand Down Expand Up @@ -516,7 +526,7 @@ class VulkanContext final : public IContext {
ColorSpace getSwapChainColorSpace() const override;
uint32_t getNumSwapchainImages() const override;
void recreateSwapchain(int newWidth, int newHeight) override;

uint32_t getFramebufferMSAABitMask() const override;

double getTimestampPeriodToMs() const override;
Expand Down Expand Up @@ -580,7 +590,7 @@ class VulkanContext final : public IContext {
void* getVmaAllocator() const;

void checkAndUpdateDescriptorSets();
void bindDefaultDescriptorSets(VkCommandBuffer cmdBuf, VkPipelineBindPoint bindPoint) const;
void bindDefaultDescriptorSets(VkCommandBuffer cmdBuf, VkPipelineBindPoint bindPoint, VkPipelineLayout layout) const;

// for shaders debugging
void invokeShaderModuleErrorCallback(int line, int col, const char* debugName, VkShaderModule sm);
Expand All @@ -592,8 +602,8 @@ class VulkanContext final : public IContext {
void processDeferredTasks() const;
void waitDeferredTasks();
lvk::Result growDescriptorPool(uint32_t maxTextures, uint32_t maxSamplers);
VkShaderModule createShaderModule(const void* data, size_t length, const char* debugName, Result* outResult) const;
VkShaderModule createShaderModule(ShaderStage stage, const char* source, const char* debugName, Result* outResult) const;
ShaderModuleState createShaderModuleFromSPIRV(const void* spirv, size_t numBytes, const char* debugName, Result* outResult) const;
ShaderModuleState createShaderModuleFromGLSL(ShaderStage stage, const char* source, const char* debugName, Result* outResult) const;

private:
friend class lvk::VulkanSwapchain;
Expand Down Expand Up @@ -635,7 +645,6 @@ class VulkanContext final : public IContext {
std::unique_ptr<lvk::VulkanStagingDevice> stagingDevice_;
uint32_t currentMaxTextures_ = 16;
uint32_t currentMaxSamplers_ = 16;
VkPipelineLayout vkPipelineLayout_ = VK_NULL_HANDLE;
VkDescriptorSetLayout vkDSL_ = VK_NULL_HANDLE;
VkDescriptorPool vkDPool_ = VK_NULL_HANDLE;
VkDescriptorSet vkDSet_ = VK_NULL_HANDLE;
Expand All @@ -651,7 +660,7 @@ class VulkanContext final : public IContext {

lvk::ContextConfig config_;

lvk::Pool<lvk::ShaderModule, VkShaderModule> shaderModulesPool_;
lvk::Pool<lvk::ShaderModule, lvk::ShaderModuleState> shaderModulesPool_;
lvk::Pool<lvk::RenderPipeline, lvk::RenderPipelineState> renderPipelinesPool_;
lvk::Pool<lvk::ComputePipeline, lvk::ComputePipelineState> computePipelinesPool_;
lvk::Pool<lvk::Sampler, VkSampler> samplersPool_;
Expand Down
19 changes: 8 additions & 11 deletions lvk/vulkan/VulkanUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,15 +573,14 @@ static glslang_stage_t getGLSLangShaderStage(VkShaderStageFlagBits stage) {
return GLSLANG_STAGE_COUNT;
}

lvk::Result lvk::compileShader(VkDevice device,
VkShaderStageFlagBits stage,
lvk::Result lvk::compileShader(VkShaderStageFlagBits stage,
const char* code,
VkShaderModule* outShaderModule,
std::vector<uint8_t>* outSPIRV,
const glslang_resource_t* glslLangResource) {
LVK_PROFILER_FUNCTION();

if (!outShaderModule) {
return Result(Result::Code::ArgumentOutOfRange, "outShaderModule is NULL");
if (!outSPIRV) {
return Result(Result::Code::ArgumentOutOfRange, "outSPIRV is NULL");
}

const glslang_input_t input = {
Expand Down Expand Up @@ -655,12 +654,10 @@ lvk::Result lvk::compileShader(VkDevice device,
LLOGW("%s\n", glslang_program_SPIRV_get_messages(program));
}

const VkShaderModuleCreateInfo ci = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = glslang_program_SPIRV_get_size(program) * sizeof(uint32_t),
.pCode = glslang_program_SPIRV_get_ptr(program),
};
VK_ASSERT_RETURN(vkCreateShaderModule(device, &ci, nullptr, outShaderModule));
const uint8_t* spirv = reinterpret_cast<const uint8_t*>(glslang_program_SPIRV_get_ptr(program));
const size_t numBytes = glslang_program_SPIRV_get_size(program) * sizeof(uint32_t);

*outSPIRV = std::vector(spirv, spirv + numBytes);

return Result();
}
Expand Down
7 changes: 4 additions & 3 deletions lvk/vulkan/VulkanUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <cassert>
#include <cstdio>
#include <vector>

#include <volk.h>
#include <vk_mem_alloc.h>
#include <lvk/LVK.h>
Expand Down Expand Up @@ -70,10 +72,9 @@ VkResult allocateMemory(VkPhysicalDevice physDev,
VkDeviceMemory* outMemory);

glslang_resource_t getGlslangResource(const VkPhysicalDeviceLimits& limits);
Result compileShader(VkDevice device,
VkShaderStageFlagBits stage,
Result compileShader(VkShaderStageFlagBits stage,
const char* code,
VkShaderModule* outShaderModule,
std::vector<uint8_t>* outSPIRV,
const glslang_resource_t* glslLangResource = nullptr);

VkSamplerCreateInfo samplerStateDescToVkSamplerCreateInfo(const lvk::SamplerStateDesc& desc, const VkPhysicalDeviceLimits& limits);
Expand Down
8 changes: 8 additions & 0 deletions third-party/bootstrap-deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
"file": "glslang.py"
}
},
{
"name": "SPIRV-Reflect",
"source": {
"type": "git",
"url": "https://github.com/KhronosGroup/SPIRV-Reflect.git",
"revision": "vulkan-sdk-1.3.275.0"
}
},
{
"name": "tinyobjloader",
"source": {
Expand Down

0 comments on commit 22c740d

Please sign in to comment.