Skip to content

Commit

Permalink
compileShader() returns SPIR-V code instead of creating VkShaderModule
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Feb 18, 2024
1 parent 678a712 commit 020e3b1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 45 deletions.
48 changes: 19 additions & 29 deletions lvk/vulkan/VulkanClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "VulkanUtils.h"

#include <glslang/Include/glslang_c_interface.h>
#include <SPIRV-Reflect/spirv_reflect.h>
#include <ldrutils/lutils/ScopeExit.h>

#ifndef VK_USE_PLATFORM_WIN32_KHR
Expand Down Expand Up @@ -3990,12 +3991,8 @@ lvk::Format lvk::VulkanContext::getFormat(TextureHandle handle) const {

lvk::Holder<lvk::ShaderModuleHandle> lvk::VulkanContext::createShaderModule(const ShaderModuleDesc& desc, Result* outResult) {
Result result;
VkShaderModule sm = desc.dataSize ?
// binary
createShaderModule(desc.data, desc.dataSize, desc.debugName, &result)
:
// text
createShaderModule(desc.stage, desc.data, desc.debugName, &result);
VkShaderModule sm = desc.dataSize ? createShaderModuleFromSPIRV(desc.data, desc.dataSize, desc.debugName, &result) // binary
: createShaderModuleFromGLSL(desc.stage, desc.data, desc.debugName, &result); // text

if (!result.isOk()) {
Result::setResult(outResult, result);
Expand All @@ -4006,13 +4003,16 @@ lvk::Holder<lvk::ShaderModuleHandle> lvk::VulkanContext::createShaderModule(cons
return {this, shaderModulesPool_.create(std::move(sm))};
}

VkShaderModule lvk::VulkanContext::createShaderModule(const void* data, size_t length, const char* debugName, Result* outResult) const {
VkShaderModule lvk::VulkanContext::createShaderModuleFromSPIRV(const void* spirv,
size_t numBytes,
const char* debugName,
Result* outResult) const {
VkShaderModule vkShaderModule = VK_NULL_HANDLE;

const VkShaderModuleCreateInfo ci = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = length,
.pCode = (const uint32_t*)data,
.codeSize = numBytes,
.pCode = (const uint32_t*)spirv,
};
const VkResult result = vkCreateShaderModule(vkDevice_, &ci, nullptr, &vkShaderModule);

Expand All @@ -4029,10 +4029,10 @@ VkShaderModule lvk::VulkanContext::createShaderModule(const void* data, size_t l
return vkShaderModule;
}

VkShaderModule lvk::VulkanContext::createShaderModule(ShaderStage stage,
const char* source,
const char* debugName,
Result* outResult) const {
VkShaderModule lvk::VulkanContext::createShaderModuleFromGLSL(ShaderStage stage,
const char* source,
const char* debugName,
Result* outResult) const {
const VkShaderStageFlagBits vkStage = shaderStageToVkShaderStage(stage);
LVK_ASSERT(vkStage != VK_SHADER_STAGE_FLAG_BITS_MAX_ENUM);
LVK_ASSERT(source);
Expand Down Expand Up @@ -4103,20 +4103,10 @@ VkShaderModule lvk::VulkanContext::createShaderModule(ShaderStage stage,

const glslang_resource_t glslangResource = lvk::getGlslangResource(getVkPhysicalDeviceProperties().limits);

VkShaderModule vkShaderModule = VK_NULL_HANDLE;
const Result result = lvk::compileShader(vkDevice_, vkStage, source, &vkShaderModule, &glslangResource);

Result::setResult(outResult, result);

if (!result.isOk()) {
return VK_NULL_HANDLE;
}
std::vector<uint8_t> spirv;
const Result result = lvk::compileShader(vkStage, source, &spirv, &glslangResource);

VK_ASSERT(lvk::setDebugObjectName(vkDevice_, VK_OBJECT_TYPE_SHADER_MODULE, (uint64_t)vkShaderModule, debugName));

LVK_ASSERT(vkShaderModule != VK_NULL_HANDLE);

return vkShaderModule;
return createShaderModuleFromSPIRV(spirv.data(), spirv.size(), debugName, outResult);
}

lvk::Format lvk::VulkanContext::getSwapchainFormat() const {
Expand Down Expand Up @@ -4217,7 +4207,7 @@ void lvk::VulkanContext::createInstance() {
VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT,
VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_RESERVE_BINDING_SLOT_EXT,
};
#endif
#endif // ANDROID

#if defined(__APPLE__)
// Shader validation doesn't work in MoltenVK for SPIR-V 1.6 under Vulkan 1.3:
Expand All @@ -4226,7 +4216,7 @@ void lvk::VulkanContext::createInstance() {
VK_VALIDATION_FEATURE_DISABLE_SHADERS_EXT,
VK_VALIDATION_FEATURE_DISABLE_SHADER_VALIDATION_CACHE_EXT,
};
#endif
#endif // __APPLE__

const VkValidationFeaturesEXT features = {
.sType = VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT,
Expand All @@ -4252,7 +4242,7 @@ void lvk::VulkanContext::createInstance() {
.settingCount = (uint32_t)LVK_ARRAY_NUM_ELEMENTS(settings),
.pSettings = settings
};
#endif
#endif // __APPLE__

const VkApplicationInfo appInfo = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Expand Down
4 changes: 2 additions & 2 deletions lvk/vulkan/VulkanClasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,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;
VkShaderModule createShaderModuleFromSPIRV(const void* spirv, size_t numBytes, const char* debugName, Result* outResult) const;
VkShaderModule createShaderModuleFromGLSL(ShaderStage stage, const char* source, const char* debugName, Result* outResult) const;

private:
friend class lvk::VulkanSwapchain;
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

0 comments on commit 020e3b1

Please sign in to comment.