From c632ec4bacef631b0357dadcdd5d6bacb652c585 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Tue, 30 Oct 2018 09:18:54 -0700 Subject: Implemented HLSL vertex shader inputs mapping in Vulkan backend using SPV_GOOGLE_hlsl_functionality1 (causes validation layer errors for now) --- .../GraphicsEngineVulkan/include/ShaderVkImpl.h | 2 ++ Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp | 40 +++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) (limited to 'Graphics/GraphicsEngineVulkan') diff --git a/Graphics/GraphicsEngineVulkan/include/ShaderVkImpl.h b/Graphics/GraphicsEngineVulkan/include/ShaderVkImpl.h index 144581bf..adcc6a31 100644 --- a/Graphics/GraphicsEngineVulkan/include/ShaderVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/ShaderVkImpl.h @@ -87,6 +87,8 @@ public: #endif private: + void MapHLSLVertexShaderInputs(); + // ShaderResources class instance must be referenced through the shared pointer, because // it is referenced by ShaderResourceLayoutVk class instances std::shared_ptr m_pShaderResources; diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp index 23d2123d..56e32ed1 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp @@ -22,6 +22,7 @@ */ #include +#include #include "pch.h" #include "ShaderVkImpl.h" @@ -78,14 +79,51 @@ ShaderVkImpl::ShaderVkImpl(IReferenceCounters* pRefCounters, RenderDeviceVkImpl* // Load shader resources auto& Allocator = GetRawAllocator(); auto* pRawMem = ALLOCATE(Allocator, "Allocator for ShaderResources", sizeof(SPIRVShaderResources)); - auto* pResources = new (pRawMem) SPIRVShaderResources(Allocator, pRenderDeviceVk, m_SPIRV, m_Desc, CreationAttribs.UseCombinedTextureSamplers ? CreationAttribs.CombinedSamplerSuffix : nullptr); + bool IsHLSLVertexShader = CreationAttribs.SourceLanguage == SHADER_SOURCE_LANGUAGE_HLSL && m_Desc.ShaderType == SHADER_TYPE_VERTEX; + auto* pResources = new (pRawMem) SPIRVShaderResources(Allocator, pRenderDeviceVk, m_SPIRV, m_Desc, CreationAttribs.UseCombinedTextureSamplers ? CreationAttribs.CombinedSamplerSuffix : nullptr, IsHLSLVertexShader); m_pShaderResources.reset(pResources, STDDeleterRawMem(Allocator)); + + if (IsHLSLVertexShader) + { + MapHLSLVertexShaderInputs(); + } m_StaticResLayout.InitializeStaticResourceLayout(m_pShaderResources, GetRawAllocator(), m_StaticResCache); // m_StaticResLayout only contains static resources, so reference all of them m_StaticVarsMgr.Initialize(m_StaticResLayout, GetRawAllocator(), nullptr, 0, m_StaticResCache); } +void ShaderVkImpl::MapHLSLVertexShaderInputs() +{ + for (Uint32 i = 0; i < m_pShaderResources->GetNumShaderStageInputs(); ++i) + { + const auto& Input = m_pShaderResources->GetShaderStageInputAttribs(i); + const char* s = Input.Semantic; + static const char* Prefix = "attrib"; + const char* p = Prefix; + while(*s != 0 && *p != 0 && *p == std::tolower(static_cast(*s)) ) + { + ++p; + ++s; + } + + if (*p != 0) + { + LOG_ERROR_MESSAGE("Unable to map semantic '", Input.Semantic, "' to input location: semantics must have 'ATTRIBx' format."); + continue; + } + + char* EndPtr = nullptr; + auto Location = strtol(s, &EndPtr, 10); + if (*EndPtr != 0) + { + LOG_ERROR_MESSAGE("Unable to map semantic '", Input.Semantic, "' to input location: semantics must have 'ATTRIBx' format."); + continue; + } + m_SPIRV[Input.LocationDecorationOffset] = Location; + } +} + ShaderVkImpl::~ShaderVkImpl() { m_StaticVarsMgr.Destroy(GetRawAllocator()); -- cgit v1.2.3