diff --git a/util/test/demos/demos.vcxproj b/util/test/demos/demos.vcxproj index 759aefcf8..8e33f6025 100644 --- a/util/test/demos/demos.vcxproj +++ b/util/test/demos/demos.vcxproj @@ -214,6 +214,7 @@ + diff --git a/util/test/demos/demos.vcxproj.filters b/util/test/demos/demos.vcxproj.filters index b8bd35a9e..8afd0183a 100644 --- a/util/test/demos/demos.vcxproj.filters +++ b/util/test/demos/demos.vcxproj.filters @@ -324,6 +324,9 @@ Vulkan\demos + + Vulkan\demos + diff --git a/util/test/demos/test_common.cpp b/util/test/demos/test_common.cpp index ff3b86aa1..f12c3d207 100644 --- a/util/test/demos/test_common.cpp +++ b/util/test/demos/test_common.cpp @@ -264,6 +264,9 @@ std::vector CompileShaderToSpv(const std::string &source_text, SPIRVTa if(target == SPIRVTarget::opengl) shaderc_compile_options_set_target_env(opts, shaderc_target_env_opengl, 0); + else if(target == SPIRVTarget::vulkan11) + shaderc_compile_options_set_target_env(opts, shaderc_target_env_vulkan, + shaderc_env_version_vulkan_1_1); shaderc_compilation_result_t res = shaderc_compile_into_spv( shaderc, source_text.c_str(), source_text.size(), shader_kind, "inshader", entry_point, opts); @@ -319,6 +322,8 @@ std::vector CompileShaderToSpv(const std::string &source_text, SPIRVTa if(target == SPIRVTarget::opengl) command_line += " --target-env=opengl"; + else if(target == SPIRVTarget::vulkan11) + command_line += " --target-env=vulkan1.1"; char infile[MAX_PATH] = {}; char outfile[MAX_PATH] = {}; diff --git a/util/test/demos/test_common.h b/util/test/demos/test_common.h index ca03653e9..992b67700 100644 --- a/util/test/demos/test_common.h +++ b/util/test/demos/test_common.h @@ -44,7 +44,8 @@ typedef uint8_t byte; enum class SPIRVTarget { opengl, - vulkan + vulkan, + vulkan11, }; enum class ShaderLang { diff --git a/util/test/demos/vk/vk_spirv_13_shaders.cpp b/util/test/demos/vk/vk_spirv_13_shaders.cpp new file mode 100644 index 000000000..7d2e22111 --- /dev/null +++ b/util/test/demos/vk/vk_spirv_13_shaders.cpp @@ -0,0 +1,142 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2018-2019 Baldur Karlsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ + +#include "vk_test.h" + +TEST(VK_SPIRV_13_Shaders, VulkanGraphicsTest) +{ + static constexpr const char *Description = + "Draws a triangle using SPIR-V 1.3 shaders to ensure they are handled correctly."; + + const std::string vertex = R"EOSHADER( + +void vertmain(in float3 pos : INPOS, in float4 col : INCOL, in float2 uv : UV, + out float4 opos : SV_Position, out float4 outcol : COL) +{ + opos = float4(pos.xyz*float3(1,-1,1), 1); + outcol = col; +} + +)EOSHADER"; + + const std::string pixel = R"EOSHADER( + +void fragmain(in float4 incol : COL, out float4 ocol : SV_Target0) +{ + ocol = incol; +} + +)EOSHADER"; + + void Prepare(int argc, char **argv) + { + VulkanGraphicsTest::Prepare(argc, argv); + + if(!Avail.empty()) + return; + + if(devVersion < VK_API_VERSION_1_1) + Avail = "Vulkan device version isn't 1.1"; + } + + int main() + { + // initialise, create window, create context, etc + if(!Init()) + return 3; + + VkPipelineLayout layout = createPipelineLayout(vkh::PipelineLayoutCreateInfo()); + + vkh::GraphicsPipelineCreateInfo pipeCreateInfo; + + pipeCreateInfo.layout = layout; + pipeCreateInfo.renderPass = mainWindow->rp; + + pipeCreateInfo.vertexInputState.vertexBindingDescriptions = {vkh::vertexBind(0, DefaultA2V)}; + pipeCreateInfo.vertexInputState.vertexAttributeDescriptions = { + vkh::vertexAttr(0, 0, DefaultA2V, pos), vkh::vertexAttr(1, 0, DefaultA2V, col), + vkh::vertexAttr(2, 0, DefaultA2V, uv), + }; + + pipeCreateInfo.stages = { + CompileShaderModule(vertex, ShaderLang::hlsl, ShaderStage::vert, "vertmain", + SPIRVTarget::vulkan11), + CompileShaderModule(pixel, ShaderLang::hlsl, ShaderStage::frag, "fragmain", + SPIRVTarget::vulkan11), + }; + + VkPipeline pipe = createGraphicsPipeline(pipeCreateInfo); + + const DefaultA2V tri[3] = { + {Vec3f(-0.5f, -0.5f, 0.0f), Vec4f(0.0f, 1.0f, 0.0f, 1.0f), Vec2f(0.0f, 0.0f)}, + {Vec3f(0.0f, 0.5f, 0.0f), Vec4f(0.0f, 1.0f, 0.0f, 1.0f), Vec2f(0.0f, 1.0f)}, + {Vec3f(0.5f, -0.5f, 0.0f), Vec4f(0.0f, 1.0f, 0.0f, 1.0f), Vec2f(1.0f, 0.0f)}, + }; + + AllocatedBuffer vb(allocator, + vkh::BufferCreateInfo(sizeof(tri), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | + VK_BUFFER_USAGE_TRANSFER_DST_BIT), + VmaAllocationCreateInfo({0, VMA_MEMORY_USAGE_CPU_TO_GPU})); + + vb.upload(tri); + + while(Running()) + { + VkCommandBuffer cmd = GetCommandBuffer(); + + vkBeginCommandBuffer(cmd, vkh::CommandBufferBeginInfo()); + + VkImage swapimg = + StartUsingBackbuffer(cmd, VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_LAYOUT_GENERAL); + + vkCmdClearColorImage(cmd, swapimg, VK_IMAGE_LAYOUT_GENERAL, + vkh::ClearColorValue(0.4f, 0.5f, 0.6f, 1.0f), 1, + vkh::ImageSubresourceRange()); + + vkCmdBeginRenderPass( + cmd, vkh::RenderPassBeginInfo(mainWindow->rp, mainWindow->GetFB(), mainWindow->scissor), + VK_SUBPASS_CONTENTS_INLINE); + + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe); + vkCmdSetViewport(cmd, 0, 1, &mainWindow->viewport); + vkCmdSetScissor(cmd, 0, 1, &mainWindow->scissor); + vkh::cmdBindVertexBuffers(cmd, 0, {vb.buffer}, {0}); + vkCmdDraw(cmd, 3, 1, 0, 0); + + vkCmdEndRenderPass(cmd); + + FinishUsingBackbuffer(cmd, VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_LAYOUT_GENERAL); + + vkEndCommandBuffer(cmd); + + Submit(0, 1, {cmd}); + + Present(); + } + + return 0; + } +}; + +REGISTER_TEST(); diff --git a/util/test/demos/vk/vk_test.cpp b/util/test/demos/vk/vk_test.cpp index a45c68cc3..24c69dc3b 100644 --- a/util/test/demos/vk/vk_test.cpp +++ b/util/test/demos/vk/vk_test.cpp @@ -613,12 +613,12 @@ void VulkanGraphicsTest::Present(VulkanWindow *window, VkQueue q) } VkPipelineShaderStageCreateInfo VulkanGraphicsTest::CompileShaderModule( - const std::string &source_text, ShaderLang lang, ShaderStage stage, const char *entry_point) + const std::string &source_text, ShaderLang lang, ShaderStage stage, const char *entry_point, + SPIRVTarget target) { VkShaderModule ret = VK_NULL_HANDLE; - std::vector spirv = - ::CompileShaderToSpv(source_text, SPIRVTarget::vulkan, lang, stage, entry_point); + std::vector spirv = ::CompileShaderToSpv(source_text, target, lang, stage, entry_point); if(spirv.empty()) return {}; diff --git a/util/test/demos/vk/vk_test.h b/util/test/demos/vk/vk_test.h index eaac594f9..ef2883834 100644 --- a/util/test/demos/vk/vk_test.h +++ b/util/test/demos/vk/vk_test.h @@ -223,7 +223,8 @@ struct VulkanGraphicsTest : public GraphicsTest VkPipelineShaderStageCreateInfo CompileShaderModule(const std::string &source_text, ShaderLang lang, ShaderStage stage, - const char *entry_point = "main"); + const char *entry_point = "main", + SPIRVTarget target = SPIRVTarget::vulkan); VkCommandBuffer GetCommandBuffer(VkCommandBufferLevel level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, VulkanWindow *window = NULL); diff --git a/util/test/tests/Vulkan/VK_SPIRV_13_Shaders.py b/util/test/tests/Vulkan/VK_SPIRV_13_Shaders.py new file mode 100644 index 000000000..bd53c10db --- /dev/null +++ b/util/test/tests/Vulkan/VK_SPIRV_13_Shaders.py @@ -0,0 +1,95 @@ +import renderdoc as rd +import rdtest + + +class VK_SPIRV_13_Shaders(rdtest.TestCase): + demos_test_name = 'VK_SPIRV_13_Shaders' + + def check_capture(self): + draw = self.find_draw("Draw") + + self.check(draw is not None) + + self.controller.SetFrameEvent(draw.eventId, False) + + # Make an output so we can pick pixels + out: rd.ReplayOutput = self.controller.CreateOutput(rd.CreateHeadlessWindowingData(100, 100), rd.ReplayOutputType.Texture) + + pipe: rd.PipeState = self.controller.GetPipelineState() + + refl: rd.ShaderReflection = pipe.GetShaderReflection(rd.ShaderStage.Vertex) + + disasm: str = self.controller.DisassembleShader(pipe.GetGraphicsPipelineObject(), refl, "") + + if (refl.inputSignature[0].varName != 'pos' or refl.inputSignature[0].compCount != 3): + raise rdtest.TestFailureException("Vertex shader input 'pos' not reflected correctly") + if (refl.inputSignature[1].varName != 'col' or refl.inputSignature[1].compCount != 4): + raise rdtest.TestFailureException("Vertex shader input 'col' not reflected correctly") + if (refl.inputSignature[2].varName != 'uv' or refl.inputSignature[2].compCount != 2): + raise rdtest.TestFailureException("Vertex shader input 'uv' not reflected correctly") + + if (refl.outputSignature[0].varName != 'opos' or refl.outputSignature[0].compCount != 4 or refl.outputSignature[0].systemValue != rd.ShaderBuiltin.Position): + raise rdtest.TestFailureException("Vertex shader output 'opos' not reflected correctly") + if (refl.outputSignature[1].varName != 'outcol' or refl.outputSignature[1].compCount != 4): + raise rdtest.TestFailureException("Vertex shader output 'outcol' not reflected correctly") + + if 'vertmain' not in disasm: + raise rdtest.TestFailureException("Vertex shader disassembly failed, entry point not found") + + refl: rd.ShaderReflection = pipe.GetShaderReflection(rd.ShaderStage.Fragment) + + disasm: str = self.controller.DisassembleShader(pipe.GetGraphicsPipelineObject(), refl, "") + + if (refl.inputSignature[0].varName != 'incol' or refl.inputSignature[0].compCount != 4): + raise rdtest.TestFailureException("Fragment shader input 'incol' not reflected correctly") + + if (refl.outputSignature[0].varName != 'ocol' or refl.outputSignature[0].compCount != 4 or refl.outputSignature[0].systemValue != rd.ShaderBuiltin.ColorOutput): + raise rdtest.TestFailureException("Fragment shader output 'ocol' not reflected correctly") + + if 'fragmain' not in disasm: + raise rdtest.TestFailureException("Fragment shader disassembly failed, entry point not found") + + rdtest.log.success("shader reflection and disassembly as expected") + + postvs_data = self.get_postvs(rd.MeshDataStage.VSOut, 0, draw.numIndices) + + postvs_ref = { + 0: { + 'vtx': 0, + 'idx': 0, + 'opos': [-0.5, 0.5, 0.0, 1.0], + 'outcol': [0.0, 1.0, 0.0, 1.0], + }, + 1: { + 'vtx': 1, + 'idx': 1, + 'opos': [0.0, -0.5, 0.0, 1.0], + 'outcol': [0.0, 1.0, 0.0, 1.0], + }, + 2: { + 'vtx': 2, + 'idx': 2, + 'opos': [0.5, 0.5, 0.0, 1.0], + 'outcol': [0.0, 1.0, 0.0, 1.0], + }, + } + + self.check_mesh_data(postvs_ref, postvs_data) + + rdtest.log.success("vertex output is as expected") + + tex = rd.TextureDisplay() + tex.resourceId = pipe.GetOutputTargets()[0].resourceId + out.SetTextureDisplay(tex) + + texdetails = self.get_texture(tex.resourceId) + + picked: rd.PixelValue = out.PickPixel(tex.resourceId, False, + int(texdetails.width / 2), int(texdetails.height / 2), 0, 0, 0) + + if not rdtest.value_compare(picked.floatValue, [0.0, 1.0, 0.0, 1.0]): + raise rdtest.TestFailureException("Picked value {} doesn't match expectation".format(picked.floatValue)) + + rdtest.log.success("picked value is as expected") + + out.Shutdown()