diff --git a/util/test/demos/CMakeLists.txt b/util/test/demos/CMakeLists.txt index a56c23e2f..82bb42037 100644 --- a/util/test/demos/CMakeLists.txt +++ b/util/test/demos/CMakeLists.txt @@ -45,6 +45,7 @@ set(VULKAN_SRC vk/vk_shader_debug_zoo.cpp vk/vk_shader_editing.cpp vk/vk_shader_isa.cpp + vk/vk_shader_printf.cpp vk/vk_simple_triangle.cpp vk/vk_spec_constants.cpp vk/vk_spirv_13_shaders.cpp diff --git a/util/test/demos/demos.vcxproj b/util/test/demos/demos.vcxproj index f522be259..11dfc43a8 100644 --- a/util/test/demos/demos.vcxproj +++ b/util/test/demos/demos.vcxproj @@ -305,6 +305,7 @@ + diff --git a/util/test/demos/demos.vcxproj.filters b/util/test/demos/demos.vcxproj.filters index 4083a4ab2..ab34d013b 100644 --- a/util/test/demos/demos.vcxproj.filters +++ b/util/test/demos/demos.vcxproj.filters @@ -610,6 +610,9 @@ Vulkan\demos + + Vulkan\demos + diff --git a/util/test/demos/vk/vk_shader_printf.cpp b/util/test/demos/vk/vk_shader_printf.cpp new file mode 100644 index 000000000..4b8907b2e --- /dev/null +++ b/util/test/demos/vk/vk_shader_printf.cpp @@ -0,0 +1,218 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2019-2021 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" + +RD_TEST(VK_Shader_Printf, VulkanGraphicsTest) +{ + static constexpr const char *Description = + "Tests the results of shader printf output in the replay."; + + std::string common = R"EOSHADER( + +#version 450 core + +#extension GL_EXT_debug_printf : require + +struct v2f +{ + vec4 pos; + vec4 col; + vec4 uv; +}; + +)EOSHADER"; + + const std::string vertex = R"EOSHADER( + +layout(location = 0) in vec3 Position; +layout(location = 1) in vec4 Color; +layout(location = 2) in vec2 UV; + +layout(location = 0) out v2f vertOut; + +void main() +{ + vertOut.pos = vec4(Position.xyz*vec3(1,-1,1), 1); + gl_Position = vertOut.pos; + vertOut.col = Color; + vertOut.uv = vec4(UV.xy, 0, 1); +} + +)EOSHADER"; + + const std::string pixel = R"EOSHADER( + +layout(location = 0) in v2f vertIn; + +layout(location = 0, index = 0) out vec4 Color; + +void main() +{ + if (gl_FragCoord.x >= 200 && gl_FragCoord.x <= 202 && + gl_FragCoord.y >= 150 && gl_FragCoord.y <= 152) + { + debugPrintfEXT("pixel:%d,%d,%04.2v2f,%d", int(gl_FragCoord.x), int(gl_FragCoord.y), gl_FragCoord.xy, int(gl_FragCoord.x) == 201); + debugPrintfEXT("Invalid printf string %y"); + } + + Color = vec4(0, 1, 0, 1); +} + +)EOSHADER"; + + const std::string comp = R"EOSHADER( + +layout(binding = 0, std430) buffer outbuftype { + uint counter; +} outbuf; + +layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in; + +void main() +{ + atomicAdd(outbuf.counter, 1u); + + if(gl_GlobalInvocationID.x >= 100 && gl_GlobalInvocationID.x <= 104) + debugPrintfEXT("compute:%v3u", gl_GlobalInvocationID); +} + +)EOSHADER"; + + void Prepare(int argc, char **argv) + { + devExts.push_back(VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME); + + VulkanGraphicsTest::Prepare(argc, argv); + } + + int main() + { + // initialise, create window, create context, etc + if(!Init()) + return 3; + + VkDescriptorSetLayout setlayout = createDescriptorSetLayout(vkh::DescriptorSetLayoutCreateInfo({ + {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT}, + })); + + VkPipelineLayout layout = createPipelineLayout(vkh::PipelineLayoutCreateInfo({setlayout})); + + 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(common + vertex, ShaderLang::glsl, ShaderStage::vert, "main"), + CompileShaderModule(common + pixel, ShaderLang::glsl, ShaderStage::frag, "main"), + }; + + VkPipeline pipe = createGraphicsPipeline(pipeCreateInfo); + + VkPipeline comppipe = createComputePipeline(vkh::ComputePipelineCreateInfo( + layout, CompileShaderModule(common + comp, ShaderLang::glsl, ShaderStage::comp, "main"))); + + AllocatedBuffer vb( + this, vkh::BufferCreateInfo(sizeof(DefaultTri), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | + VK_BUFFER_USAGE_TRANSFER_DST_BIT), + VmaAllocationCreateInfo({0, VMA_MEMORY_USAGE_CPU_TO_GPU})); + + vb.upload(DefaultTri); + + VkDeviceSize ssbo_size = 1024; + + AllocatedBuffer ssbo(this, vkh::BufferCreateInfo(ssbo_size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | + VK_BUFFER_USAGE_TRANSFER_DST_BIT), + VmaAllocationCreateInfo({0, VMA_MEMORY_USAGE_GPU_ONLY})); + + setName(ssbo.buffer, "SSBO"); + + VkDescriptorSet descset = allocateDescriptorSet(setlayout); + + vkh::updateDescriptorSets( + device, { + vkh::WriteDescriptorSet(descset, 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, + {vkh::DescriptorBufferInfo(ssbo.buffer)}), + }); + + 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.2f, 0.2f, 0.2f, 1.0f), 1, + vkh::ImageSubresourceRange()); + + vkh::cmdPipelineBarrier( + cmd, {}, {vkh::BufferMemoryBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, + VK_ACCESS_TRANSFER_WRITE_BIT, ssbo.buffer)}); + + vkCmdFillBuffer(cmd, ssbo.buffer, 0, ssbo_size, 0); + + vkh::cmdPipelineBarrier(cmd, {}, + {vkh::BufferMemoryBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, + VK_ACCESS_SHADER_WRITE_BIT, ssbo.buffer)}); + + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, comppipe); + vkh::cmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, layout, 0, {descset}, {}); + vkCmdDispatch(cmd, 3, 1, 1); + + 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/tests/Vulkan/VK_Shader_Printf.py b/util/test/tests/Vulkan/VK_Shader_Printf.py new file mode 100644 index 000000000..6fdfc7260 --- /dev/null +++ b/util/test/tests/Vulkan/VK_Shader_Printf.py @@ -0,0 +1,74 @@ +import renderdoc as rd +import rdtest +import struct + + +class VK_Shader_Printf(rdtest.TestCase): + demos_test_name = 'VK_Shader_Printf' + + def check_capture(self): + action: rd.ActionDescription = self.find_action('CmdDraw') + + self.controller.SetFrameEvent(action.eventId, True) + + self.check_triangle() + + ssbo = self.get_resource_by_name('SSBO').resourceId + + buf_data = self.controller.GetBufferData(ssbo, 0, 0) + + count = struct.unpack_from("L", buf_data)[0] + + if count != 3*64: + raise rdtest.TestFailureException( + "With draw selected, buffer count is wrong: {} vs {}".format(count, 3*64)) + + vkpipe = self.controller.GetVulkanPipelineState() + + self.check(len(vkpipe.shaderMessages) == 8, "Expected 8 messages for draw, got {}" + .format(len(vkpipe.shaderMessages))) + + for msg in vkpipe.shaderMessages: + if 'Invalid' in msg.message: + self.check(msg.message == "Unrecognised % formatter in \"Invalid printf string %y\"", + "Invalid message is wrong: {}".format(msg.message)) + else: + expected = "pixel:{0},{1},{0}.50, {1}.50,{2}".format(msg.location.pixel.x, msg.location.pixel.y, + int(msg.location.pixel.x == 201)) + self.check(msg.message == expected, + "Message is wrong. Got '{}' expected '{}'".format(msg.message, expected)) + + self.check(msg.location.pixel.x in [200, 201, 202]) + self.check(msg.location.pixel.y in [150, 151, 152]) + + action = self.find_action("CmdDispatch") + + self.controller.SetFrameEvent(action.eventId, False) + + vkpipe = self.controller.GetVulkanPipelineState() + + buf_data = self.controller.GetBufferData(ssbo, 0, 0) + + count = struct.unpack_from("L", buf_data)[0] + + if count != 3*64: + raise rdtest.TestFailureException( + "With dispatch selected, buffer count is wrong: {} vs {}".format(count, 3*64)) + + self.check(len(vkpipe.shaderMessages) == 5, "Expected 5 messages for dispatch, got {}" + .format(len(vkpipe.shaderMessages))) + + for msg in vkpipe.shaderMessages: + c = msg.location.compute + expected = "compute:{}, {}, {}".format(c.workgroup[0] * 64 + c.thread[0], + c.workgroup[1] * 64 + c.thread[1], + c.workgroup[2] * 64 + c.thread[2]) + self.check(msg.message == expected, + "Message is wrong. Got '{}' expected '{}'".format(msg.message, expected)) + + self.check(c.workgroup == (1, 0, 0)) + self.check(c.thread[1] == 0) + self.check(c.thread[2] == 0) + self.check(c.thread[0] in [36, 37, 38, 39, 40]) + + rdtest.log.success("All messages are as expected")