Skip to content

Cannot use ShaderCI.FilePath to load byte code #710

@hzqst

Description

@hzqst

I'm trying to fill ShaderCI.FilePath with bytecode path (e.g. shader/cache/DrawWorldSurface.spirv / shader/cache/DrawWorldSurface.dxbc) from a manifest file to take advantage of RENDER_STATE_CACHE_FILE_HASH_MODE_BY_NAME,

however ShaderVkImpl::Initialize , CompileD3DBytecode and GetD3D12ShaderModel always treats ShaderCI.FilePath as text source input:

void ShaderVkImpl::Initialize(const ShaderCreateInfo& ShaderCI,
                              const CreateInfo&       VkShaderCI)
{
    if (ShaderCI.Source != nullptr || ShaderCI.FilePath != nullptr)
    {
        DEV_CHECK_ERR(ShaderCI.ByteCode == nullptr, "'ByteCode' must be null when shader is created from source code or a file");

        SHADER_COMPILER ShaderCompiler = ShaderCI.ShaderCompiler;
        if (ShaderCompiler == SHADER_COMPILER_DXC)
        {
            IDXCompiler* pDXCompiler = VkShaderCI.pDXCompiler;
            if (pDXCompiler == nullptr || !pDXCompiler->IsLoaded())
            {
                LOG_WARNING_MESSAGE("DX Compiler is not loaded. Using default shader compiler");
                ShaderCompiler = SHADER_COMPILER_DEFAULT;
            }
        }

        switch (ShaderCompiler)
        {
            case SHADER_COMPILER_DXC:
                m_SPIRV = CompileShaderDXC(ShaderCI, VkShaderCI);
                break;

            case SHADER_COMPILER_DEFAULT:
            case SHADER_COMPILER_GLSLANG:
                m_SPIRV = CompileShaderGLSLang(ShaderCI, VkShaderCI);
                break;

            default:
                LOG_ERROR_AND_THROW("Unsupported shader compiler");
        }

        if (m_SPIRV.empty())
        {
            LOG_ERROR_AND_THROW("Failed to compile shader '", m_Desc.Name, '\'');
        }
    }
    else if (ShaderCI.ByteCode != nullptr)
    {
        DEV_CHECK_ERR(ShaderCI.ByteCodeSize != 0, "ByteCodeSize must not be 0");
        DEV_CHECK_ERR(ShaderCI.ByteCodeSize % 4 == 0, "Byte code size (", ShaderCI.ByteCodeSize, ") is not multiple of 4");
        m_SPIRV.resize(ShaderCI.ByteCodeSize / 4);
        memcpy(m_SPIRV.data(), ShaderCI.ByteCode, ShaderCI.ByteCodeSize);
    }
    else
    {
        LOG_ERROR_AND_THROW("Shader source must be provided through one of the 'Source', 'FilePath' or 'ByteCode' members");
    }
  • Possible fix A:

Add a new flag SHADER_COMPILE_FLAG_BYTECODE to always treats the input ShaderCI.FilePath as bytecode file and skip the shader compiling. (also leave ShaderCI.ByteCode for backward compatibility)

  • Possible fix B:

check if ShaderCI.FilePath ends with ".dxbc", ".dxil", ".spv", ".spirv" and skip the shader compiling for them

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions