diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2018-04-15 04:57:19 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2018-04-15 04:57:19 +0000 |
| commit | bf9177a31943fd4637778317c8d4f06ec9df39c3 (patch) | |
| tree | 1d1b9531babca5ab2545f039f0a6a5a08735cf10 /Graphics | |
| parent | Added GLSL to SPIRV shader compilation (diff) | |
| download | DiligentCore-bf9177a31943fd4637778317c8d4f06ec9df39c3.tar.gz DiligentCore-bf9177a31943fd4637778317c8d4f06ec9df39c3.zip | |
Added vulkan shader module creation
Diffstat (limited to 'Graphics')
11 files changed, 66 insertions, 11 deletions
diff --git a/Graphics/GLSLTools/include/GLSLSourceBuilder.h b/Graphics/GLSLTools/include/GLSLSourceBuilder.h index c0ab5a7d..572ea180 100644 --- a/Graphics/GLSLTools/include/GLSLSourceBuilder.h +++ b/Graphics/GLSLTools/include/GLSLSourceBuilder.h @@ -29,6 +29,12 @@ namespace Diligent { -String BuildGLSLSourceString(const ShaderCreationAttribs &CreationAttribs); +enum TargetGLSLCompiler +{ + glslang, + driver +}; + +String BuildGLSLSourceString(const ShaderCreationAttribs &CreationAttribs, TargetGLSLCompiler TargetCompiler); }
\ No newline at end of file diff --git a/Graphics/GLSLTools/src/GLSL2SPIRV.cpp b/Graphics/GLSLTools/src/GLSL2SPIRV.cpp index e36a3196..69cadd2c 100644 --- a/Graphics/GLSLTools/src/GLSL2SPIRV.cpp +++ b/Graphics/GLSLTools/src/GLSL2SPIRV.cpp @@ -201,7 +201,9 @@ std::vector<unsigned int> GLSLtoSPIRV(const SHADER_TYPE ShaderType, const char * const char *ShaderStrings[] = { ShaderSource }; Shader.setStrings(ShaderStrings, 1); - if (!Shader.parse(&Resources, 100, false, messages)) + Shader.setAutoMapBindings(true); + Shader.setAutoMapLocations(true); + if (!Shader.parse(&Resources, 100, false, messages)) { LOG_ERROR_MESSAGE("Failed to parse shader source: \n", Shader.getInfoLog(), '\n', Shader.getInfoDebugLog()); return std::move(spirv); diff --git a/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp b/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp index 064f18aa..f9140a53 100644 --- a/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp +++ b/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp @@ -32,7 +32,7 @@ namespace Diligent { -String BuildGLSLSourceString(const ShaderCreationAttribs &CreationAttribs) +String BuildGLSLSourceString(const ShaderCreationAttribs &CreationAttribs, TargetGLSLCompiler TargetCompiler) { String GLSLSource; @@ -169,6 +169,13 @@ String BuildGLSLSourceString(const ShaderCreationAttribs &CreationAttribs) "layout(std140) uniform;\n" ); + if(ShaderType == SHADER_TYPE_VERTEX && TargetCompiler == TargetGLSLCompiler::glslang) + { + // https://github.com/KhronosGroup/GLSL/blob/master/extensions/khr/GL_KHR_vulkan_glsl.txt + GLSLSource.append("#define gl_VertexID gl_VertexIndex\n" + "#define gl_InstanceID gl_InstanceIndex\n"); + } + const Char* ShaderTypeDefine = nullptr; switch (ShaderType) { diff --git a/Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp b/Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp index e3a0478a..4f4d4d42 100644 --- a/Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp @@ -38,7 +38,7 @@ ShaderGLImpl::ShaderGLImpl(IReferenceCounters *pRefCounters, RenderDeviceGLImpl m_GlProgObj(false), m_GLShaderObj( false, GLObjectWrappers::GLShaderObjCreateReleaseHelper( GetGLShaderType( m_Desc.ShaderType ) ) ) { - auto GLSLSource = BuildGLSLSourceString(CreationAttribs); + auto GLSLSource = BuildGLSLSourceString(CreationAttribs, TargetGLSLCompiler::driver); // Note: there is a simpler way to create the program: //m_uiShaderSeparateProg = glCreateShaderProgramv(GL_VERTEX_SHADER, _countof(ShaderStrings), ShaderStrings); diff --git a/Graphics/GraphicsEngineVulkan/include/ShaderVkImpl.h b/Graphics/GraphicsEngineVulkan/include/ShaderVkImpl.h index 4bd9d43d..a861700f 100644 --- a/Graphics/GraphicsEngineVulkan/include/ShaderVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/ShaderVkImpl.h @@ -30,6 +30,7 @@ #include "ShaderVk.h" #include "ShaderBase.h" #include "ShaderResourceLayoutVk.h" +#include "VulkanUtilities/VulkanObjectWrappers.h" #ifdef _DEBUG # define VERIFY_SHADER_BINDINGS @@ -56,8 +57,12 @@ public: virtual IShaderVariable* GetShaderVariable(const Char* Name)override; + virtual VkShaderModule GetVkShaderModule()override final + { + return m_VkShaderModule; + } + /* - ID3DBlob* GetShaderByteCode(){return m_pShaderByteCode;} const std::shared_ptr<const ShaderResourcesVk>& GetShaderResources()const{return m_pShaderResources;} const ShaderResourceLayoutVk& GetConstResLayout()const{return m_StaticResLayout;} @@ -75,6 +80,7 @@ private: ShaderResourceLayoutVk m_StaticResLayout; ShaderResourceCacheVk m_ConstResCache; */ + VulkanUtilities::ShaderModuleWrapper m_VkShaderModule; }; } diff --git a/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanLogicalDevice.h b/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanLogicalDevice.h index 901f42af..8dba0dd7 100644 --- a/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanLogicalDevice.h +++ b/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanLogicalDevice.h @@ -72,6 +72,7 @@ namespace VulkanUtilities DeviceMemoryWrapper AllocateDeviceMemory(const VkMemoryAllocateInfo &AllocInfo, const char *DebugName = "")const; PipelineWrapper CreateComputePipeline (const VkComputePipelineCreateInfo &PipelineCI, VkPipelineCache cache, const char *DebugName = "")const; PipelineWrapper CreateGraphicsPipeline(const VkGraphicsPipelineCreateInfo &PipelineCI, VkPipelineCache cache, const char *DebugName = "")const; + ShaderModuleWrapper CreateShaderModule (const VkShaderModuleCreateInfo &ShaderModuleCI, const char *DebugName = "")const; VkCommandBuffer AllocateVkCommandBuffer(const VkCommandBufferAllocateInfo &AllocInfo, const char *DebugName = "")const; @@ -84,6 +85,7 @@ namespace VulkanUtilities void ReleaseVulkanObject(RenderPassWrapper&& RenderPass)const; void ReleaseVulkanObject(DeviceMemoryWrapper&& Memory)const; void ReleaseVulkanObject(PipelineWrapper&& Pipeline)const; + void ReleaseVulkanObject(ShaderModuleWrapper&& ShaderModule)const; VkMemoryRequirements GetBufferMemoryRequirements(VkBuffer vkBuffer)const; VkMemoryRequirements GetImageMemoryRequirements (VkImage vkImage )const; diff --git a/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanObjectWrappers.h b/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanObjectWrappers.h index 456b4853..7e749a70 100644 --- a/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanObjectWrappers.h +++ b/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanObjectWrappers.h @@ -99,4 +99,5 @@ namespace VulkanUtilities using FenceWrapper = VulkanObjectWrapper<VkFence>; using RenderPassWrapper = VulkanObjectWrapper<VkRenderPass>; using PipelineWrapper = VulkanObjectWrapper<VkPipeline>; + using ShaderModuleWrapper = VulkanObjectWrapper<VkShaderModule>; } diff --git a/Graphics/GraphicsEngineVulkan/interface/ShaderVk.h b/Graphics/GraphicsEngineVulkan/interface/ShaderVk.h index e9fc51de..2f7acbda 100644 --- a/Graphics/GraphicsEngineVulkan/interface/ShaderVk.h +++ b/Graphics/GraphicsEngineVulkan/interface/ShaderVk.h @@ -40,11 +40,8 @@ class IShaderVk : public IShader { public: - /// Returns a pointer to the ID3D12DeviceChild interface of the internal Direct3D12 object. - - /// The method does *NOT* call AddRef() on the returned interface, - /// so Release() must not be called. - //virtual ID3D12DeviceChild* GetD3D12Shader() = 0; + /// Returns Vulkan shader module handle + virtual VkShaderModule GetVkShaderModule() = 0; }; } diff --git a/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp index 13d79d06..e18186ff 100644 --- a/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp @@ -412,6 +412,7 @@ template void RenderDeviceVkImpl::SafeReleaseVkObject<VkImageView> (VulkanUti template void RenderDeviceVkImpl::SafeReleaseVkObject<VkDeviceMemory> (VulkanUtilities::DeviceMemoryWrapper &&Object); template void RenderDeviceVkImpl::SafeReleaseVkObject<VkRenderPass> (VulkanUtilities::RenderPassWrapper &&Object); template void RenderDeviceVkImpl::SafeReleaseVkObject<VkPipeline> (VulkanUtilities::PipelineWrapper &&Object); +template void RenderDeviceVkImpl::SafeReleaseVkObject<VkShaderModule> (VulkanUtilities::ShaderModuleWrapper &&Object); void RenderDeviceVkImpl::DiscardStaleVkObjects(Uint64 CmdListNumber, Uint64 FenceValue) diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp index d012b1bc..507d3aea 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp @@ -43,12 +43,22 @@ ShaderVkImpl::ShaderVkImpl(IReferenceCounters *pRefCounters, RenderDeviceVkImpl m_DummyShaderVar(*this), m_ConstResCache(ShaderResourceCacheVk::DbgCacheContentType::StaticShaderResources)*/ { - auto GLSLSource = BuildGLSLSourceString(CreationAttribs); + auto GLSLSource = BuildGLSLSourceString(CreationAttribs, TargetGLSLCompiler::glslang); auto SPIRV = GLSLtoSPIRV(m_Desc.ShaderType, GLSLSource.c_str()); if(SPIRV.empty()) { LOG_ERROR_AND_THROW("Failed to compile shader"); } + + const auto &LogicalDevice = pRenderDeviceVk->GetLogicalDevice(); + VkShaderModuleCreateInfo ShaderModuleCI = {}; + ShaderModuleCI.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; + ShaderModuleCI.pNext = NULL; + ShaderModuleCI.flags = 0; + ShaderModuleCI.codeSize = SPIRV.size() * sizeof(unsigned int); + ShaderModuleCI.pCode = SPIRV.data(); + m_VkShaderModule = LogicalDevice.CreateShaderModule(ShaderModuleCI, m_Desc.Name); + /* // Load shader resources auto &Allocator = GetRawAllocator(); @@ -65,6 +75,8 @@ ShaderVkImpl::ShaderVkImpl(IReferenceCounters *pRefCounters, RenderDeviceVkImpl ShaderVkImpl::~ShaderVkImpl() { + auto pDeviceVkImpl = ValidatedCast<RenderDeviceVkImpl>(GetDevice()); + pDeviceVkImpl->SafeReleaseVkObject(std::move(m_VkShaderModule)); } void ShaderVkImpl::BindResources(IResourceMapping* pResourceMapping, Uint32 Flags) diff --git a/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanLogicalDevice.cpp b/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanLogicalDevice.cpp index 70c0e671..c178d3d2 100644 --- a/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanLogicalDevice.cpp +++ b/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanLogicalDevice.cpp @@ -235,6 +235,21 @@ namespace VulkanUtilities return PipelineWrapper{ GetSharedPtr(), std::move(vkPipeline) }; } + ShaderModuleWrapper VulkanLogicalDevice::CreateShaderModule(const VkShaderModuleCreateInfo &ShaderModuleCI, const char *DebugName)const + { + if (DebugName == nullptr) + DebugName = ""; + + VkShaderModule vkShaderModule = VK_NULL_HANDLE; + auto err = vkCreateShaderModule(m_VkDevice, &ShaderModuleCI, m_VkAllocator, &vkShaderModule); + CHECK_VK_ERROR_AND_THROW(err, "Failed to create Render Pass '", DebugName, '\''); + + if (DebugName != nullptr && *DebugName != 0) + VulkanUtilities::SetShaderModuleName(m_VkDevice, vkShaderModule, DebugName); + + return ShaderModuleWrapper{ GetSharedPtr(), std::move(vkShaderModule) }; + } + VkCommandBuffer VulkanLogicalDevice::AllocateVkCommandBuffer(const VkCommandBufferAllocateInfo &AllocInfo, const char *DebugName)const { @@ -305,6 +320,12 @@ namespace VulkanUtilities Pipeline.m_VkObject = VK_NULL_HANDLE; } + void VulkanLogicalDevice::ReleaseVulkanObject(ShaderModuleWrapper&& ShaderModule)const + { + vkDestroyShaderModule(m_VkDevice, ShaderModule.m_VkObject, m_VkAllocator); + ShaderModule.m_VkObject = VK_NULL_HANDLE; + } + VkMemoryRequirements VulkanLogicalDevice::GetBufferMemoryRequirements(VkBuffer vkBuffer)const { VkMemoryRequirements MemReqs = {}; |
