PacktPublishing / Mastering-Graphics-Programming-with-Vulkan

MIT License
514 stars 65 forks source link

Compilation of IMGUI shader, and IMGUI render pass not working on Linux #47

Closed bkolligs closed 6 months ago

bkolligs commented 10 months ago

Reopening what was referenced in #45 with more detail. When I try to compile Chapter2 and run it with a GLTF file I get the following issue:

./build/source/chapter2/Chapter2 deps/src/glTF-Sample-Models/2.0/Box/glTF/Box.gltf 
Memory Service Init
HeapAllocator of size 33560976 created
WindowService init
Window created successfully
InputService init
Gpu Device init
GPU Used: Intel(R) Graphics (ADL GT2)
Create swapchain 1280 800 - saved 1280 800, min image 3
Renderer init
sh: 1: Syntax error: word unexpected

Error in creation of shader ImGui, stage VERTEX. Writing shader:
1: #version 450
2: layout( location = 0 ) in vec2 Position;
3: layout( location = 1 ) in vec2 UV;
4: layout( location = 2 ) in uvec4 Color;
5: layout( location = 0 ) out vec2 Frag_UV;
6: layout( location = 1 ) out vec4 Frag_Color;
7: layout (location = 2) flat out uint texture_id;
8: layout( std140, binding = 0 ) uniform LocalConstants { mat4 ProjMtx; };
9: void main()
10: {
11:     Frag_UV = UV;
12:     Frag_Color = Color / 255.0f;
13:     texture_id = gl_InstanceIndex;
14:     gl_Position = ProjMtx * vec4( Position.xy,0,1 );
15: }
 MessageID: VUID_Undefined 2044605652
Message: Validation Error: [ VUID_Undefined ] | MessageID = 0x79de34d4 | vkCreateShaderModule: parameter pCreateInfo->codeSize / 4 must be greater than 0.

 MessageID: VUID-VkShaderModuleCreateInfo-pCode-01379 706474367
Message: Validation Error: [ VUID-VkShaderModuleCreateInfo-pCode-01379 ] | MessageID = 0x2a1bf17f | SPIR-V module not valid: Invalid SPIR-V magic number. The Vulkan spec states: If pCode is a pointer to GLSL code, it must be valid GLSL code written to the GL_KHR_vulkan_glsl GLSL extension specification (https://vulkan.lunarg.com/doc/view/1.3.261.0/linux/1.3-extensions/vkspec.html#VUID-VkShaderModuleCreateInfo-pCode-01379)

Aborted (core dumped)

This shader is not included in the source/chapter2/shaders subdirectory, which makes me think it's an issue with the IMGUI specific shader here: https://github.com/PacktPublishing/Mastering-Graphics-Programming-with-Vulkan/blob/2ad4e94a0e003d37dd3dbef46cc033a483f133d6/source/chapter2/graphics/raptor_imgui.cpp#L30-L44

beaumanvienna commented 10 months ago

@bkolligs Hi Ben, I'm running into the same issue on Zorin.

"vkCreateShaderModule: parameter pCreateInfo->codeSize / 4 must be greater than 0." It didn't compile the shader. When I save the GLSL code you posted above in a file vertex.vert and compile it, it works. --> Most likely it doesn't find the compiler. I haven't set the bash variables yet. Did you?

export VULKAN_SDK=~/vulkan/1.2.198.1/x86_64 export PATH=$VULKAN_SDK/bin:$PATH export LD_LIBRARY_PATH=$VULKAN_SDK/lib:$LD_LIBRARY_PATH export VK_LAYER_PATH=$VULKAN_SDK/etc/vulkan/explicit_layer.d

Check out this line: https://github.com/PacktPublishing/Mastering-Graphics-Programming-with-Vulkan/blob/2ad4e94a0e003d37dd3dbef46cc033a483f133d6/source/chapter1/graphics/gpu_device.cpp#L1048

I have on my system: ~/dev/book >>> which glslangValidator
/usr/bin/glslangValidator ^^^ "/usr" should be VULKAN_SDK

see also "gpu_device.cpp", line 615 https://github.com/PacktPublishing/Mastering-Graphics-Programming-with-Vulkan/blob/2ad4e94a0e003d37dd3dbef46cc033a483f133d6/source/chapter1/graphics/gpu_device.cpp#L615 https://github.com/PacktPublishing/Mastering-Graphics-Programming-with-Vulkan/blob/2ad4e94a0e003d37dd3dbef46cc033a483f133d6/source/chapter1/graphics/gpu_device.cpp#L616

So with "export VULKAN_SDK=/usr", and "./build/source/chapter1/Chapter1 ./deps/src/glTF-Sample-Models/2.0/BarramundiFish/glTF/BarramundiFish.gltf", and a scale of 100, I get

image

On a side note, I also had to change "SDL2::SDL2" to "${SDL2_LIBRARIES}" in the CMakeLists.txt files in all 15 chapters.

bkolligs commented 10 months ago

I am on Ubuntu 22. What version of Vulkan are you using @beaumanvienna ? I am using version 1.3.261. I am getting Imgui render pass issues now, despite the window opening.

 MessageID: VUID-vkCmdDrawIndexed-renderPass-02684 -1934215230
Message: Validation Error: [ VUID-vkCmdDrawIndexed-renderPass-02684 ] Object 0: handle = 0xcb1c7c000000001b, name = Swapchain, type = VK_OBJECT_TYPE_RENDER_PASS; Object 1: handle = 0x2e2cd000000002b, name = Pipeline_ImGui, type = VK_OBJECT_TYPE_RENDER_PASS; | MessageID = 0x8cb637c2 | vkCmdDrawIndexed: RenderPasses incompatible between active render pass w/ VkRenderPass 0xcb1c7c000000001b[Swapchain] and pipeline state object w/ VkRenderPass 0x2e2cd000000002b[Pipeline_ImGui] Attachment 0 is not compatible with 0: They have different formats.. The Vulkan spec states: The current render pass must be compatible with the renderPass member of the VkGraphicsPipelineCreateInfo structure specified when creating the VkPipeline bound to VK_PIPELINE_BIND_POINT_GRAPHICS (https://vulkan.lunarg.com/doc/view/1.3.261.0/linux/1.3-extensions/vkspec.html#VUID-vkCmdDrawIndexed-renderPass-02684)

 MessageID: VUID-vkCmdDrawIndexed-renderPass-02684 -1934215230
Message: Validation Error: [ VUID-vkCmdDrawIndexed-renderPass-02684 ] Object 0: handle = 0xcb1c7c000000001b, name = Swapchain, type = VK_OBJECT_TYPE_RENDER_PASS; Object 1: handle = 0x2e2cd000000002b, name = Pipeline_ImGui, type = VK_OBJECT_TYPE_RENDER_PASS; | MessageID = 0x8cb637c2 | vkCmdDrawIndexed: RenderPasses incompatible between active render pass w/ VkRenderPass 0xcb1c7c000000001b[Swapchain] and pipeline state object w/ VkRenderPass 0x2e2cd000000002b[Pipeline_ImGui] Attachment 0 is not compatible with 0: They have different formats.. The Vulkan spec states: The current render pass must be compatible with the renderPass member of the VkGraphicsPipelineCreateInfo structure specified when creating the VkPipeline bound to VK_PIPELINE_BIND_POINT_GRAPHICS (https://vulkan.lunarg.com/doc/view/1.3.261.0/linux/1.3-extensions/vkspec.html#VUID-vkCmdDrawIndexed-renderPass-02684)

 MessageID: VUID-vkCmdDrawIndexed-renderPass-02684 -1934215230
Message: Validation Error: [ VUID-vkCmdDrawIndexed-renderPass-02684 ] Object 0: handle = 0xcb1c7c000000001b, name = Swapchain, type = VK_OBJECT_TYPE_RENDER_PASS; Object 1: handle = 0x2e2cd000000002b, name = Pipeline_ImGui, type = VK_OBJECT_TYPE_RENDER_PASS; | MessageID = 0x8cb637c2 | vkCmdDrawIndexed: RenderPasses incompatible between active render pass w/ VkRenderPass 0xcb1c7c000000001b[Swapchain] and pipeline state object w/ VkRenderPass 0x2e2cd000000002b[Pipeline_ImGui] Attachment 0 is not compatible with 0: They have different formats.. The Vulkan spec states: The current render pass must be compatible with the renderPass member of the VkGraphicsPipelineCreateInfo structure specified when creating the VkPipeline bound to VK_PIPELINE_BIND_POINT_GRAPHICS (https://vulkan.lunarg.com/doc/view/1.3.261.0/linux/1.3-extensions/vkspec.html#VUID-vkCmdDrawIndexed-renderPass-02684)

 MessageID: VUID-vkCmdDrawIndexed-renderPass-02684 -1934215230
Message: Validation Error: [ VUID-vkCmdDrawIndexed-renderPass-02684 ] Object 0: handle = 0xcb1c7c000000001b, name = Swapchain, type = VK_OBJECT_TYPE_RENDER_PASS; Object 1: handle = 0x2e2cd000000002b, name = Pipeline_ImGui, type = VK_OBJECT_TYPE_RENDER_PASS; | MessageID = 0x8cb637c2 | vkCmdDrawIndexed: RenderPasses incompatible between active render pass w/ VkRenderPass 0xcb1c7c000000001b[Swapchain] and pipeline state object w/ VkRenderPass 0x2e2cd000000002b[Pipeline_ImGui] Attachment 0 is not compatible with 0: They have different formats.. The Vulkan spec states: The current render pass must be compatible with the renderPass member of the VkGraphicsPipelineCreateInfo structure specified when creating the VkPipeline bound to VK_PIPELINE_BIND_POINT_GRAPHICS (https://vulkan.lunarg.com/doc/view/1.3.261.0/linux/1.3-extensions/vkspec.html#VUID-vkCmdDrawIndexed-renderPass-02684)
bkolligs commented 10 months ago

Tried using Vulkan 1.2.198 instead and I get the same render pass error

bkolligs commented 10 months ago

Despite the render pass error, it still renders the geometry so I'll close this.

theWatchmen commented 10 months ago

@beaumanvienna Thanks for your comment and the help! I'll make a note to add a better failure message if the compiler path is invalid.

@bkolligs We get the swapchain format from the supported formats exposed by the driver, but maybe there is a mismatch with the format we use for the ImGui pass. I'll double check.

Thanks for reporting the issue!

theWatchmen commented 10 months ago

I think I found the problem with at least chapter 2 and 3: we compile the shaders through CMake (rather than at runtime as we do for other chapters). I added a check to make sure the compiler path is valid. For the other chapters, failing to find the compiler executable should already result in an error message.

Still looking into the swapchain format issue :)

bkolligs commented 10 months ago

I will reopen so you can track the swap chain issue

beaumanvienna commented 10 months ago

@beaumanvienna Thanks for your comment and the help! I'll make a note to add a better failure message if the compiler path is invalid.

Thanks for reporting the issue!

You're welcome! Maybe change it to shaderc altogether? I noticed one more thing, I believe you have the -j4 for MAKEFLAGS still with a space in between the j and the 4. Did you try to fix it in the errata? (see also my edit below, the space is Ok, which renders the errata unnecessary)

https://github.com/PacktPublishing/Mastering-Graphics-Programming-with-Vulkan#linux and here https://github.com/PacktPublishing/Mastering-Graphics-Programming-with-Vulkan#errata

EDIT: Wait, it does work with the space in between j and 4. I think you can remove the errata entirely then.

time cmake --build build --target Chapter1 -- -j16 --> 43.25s user 1.96s system 509% cpu 8.880 total (the linking stages are single thread, hence the CPU usage does not go up 16x) time cmake --build build --target Chapter1 -- -j 16 --> 42.77s user 1.94s system 499% cpu 8.955 total time cmake --build build --target Chapter1 -- -j 2 --> 34.04s user 1.56s system 186% cpu 19.128 total

I looked this up in the cmake documentation, the space is allowed

theWatchmen commented 10 months ago

@bkolligs I had a better look at the code for the swapchain and render pass format, and it looks correct. If we fail to find a format we want though I think we don't populate one of the structures correctly. Could you try and apply this patch?

diff --git a/source/chapter2/graphics/gpu_device.cpp b/source/chapter2/graphics/gpu_device.cpp
index 21e79d1..64e0e80 100644
--- a/source/chapter2/graphics/gpu_device.cpp
+++ b/source/chapter2/graphics/gpu_device.cpp
@@ -506,7 +506,6 @@ void GpuDevice::init( const DeviceCreation& creation ) {
         for ( u32 j = 0; j < supported_count; j++ ) {
             if ( supported_formats[ j ].format == surface_image_formats[ i ] && supported_formats[ j ].colorSpace == surface_color_space ) {
                 vulkan_surface_format = supported_formats[ j ];
-                swapchain_output.color( surface_image_formats[ j ] );
                 format_found = true;
                 break;
             }
@@ -523,6 +522,8 @@ void GpuDevice::init( const DeviceCreation& creation ) {
     }
     rfree( supported_formats, allocator );

+    swapchain_output.color( vulkan_surface_format.format );
+
     set_present_mode( present_mode );

     //////// Create swapchain

If you haven't done this before, copy this text into a file called (say) a.patch in the root folder of this repo, then run git apply a.patch. If you still get the validation error, could you check which formats are being used here and here?

@beaumanvienna The errata is to fix the typo for chapter1 -> Chapter1, not the cmake flags :)

charlie-ht commented 10 months ago

@theWatchmen I can confirm your patch fixes,

 MessageID: VUID-vkCmdDrawIndexed-renderPass-02684 -1934215230
Message: Validation Error: [ VUID-vkCmdDrawIndexed-renderPass-02684 ] Object 0: handle = 0x1c000000001c, name = Swapchain, type = VK_OBJECT_TYPE_RENDER_PASS; Object 1: handle = 0x2c000000002c, name = Pipeline_ImGui, type = VK_OBJECT_TYPE_RENDER_PASS; | MessageID = 0x8cb637c2 | vkCmdDrawIndexed: RenderPasses incompatible between active render pass w/ VkRenderPass 0x1c000000001c[Swapchain] and pipeline state object w/ VkRenderPass 0x2c000000002c[Pipeline_ImGui] Attachment 0 is not compatible with 0: They have different formats.. The Vulkan spec states: The current render pass must be compatible with the renderPass member of the VkGraphicsPipelineCreateInfo structure specified when creating the VkPipeline bound to VK_PIPELINE_BIND_POINT_GRAPHICS (https://vulkan.lunarg.com/doc/view/1.3.261.1/linux/1.3-extensions/vkspec.html#VUID-vkCmdDrawIndexed-renderPass-02684)

when running the Sponza model.

I needed this for chapter 1 too (not got to chapter 2 yet). I tested with an AMD Radeon RX 6600, using SDK 1.3.261.1

bkolligs commented 10 months ago

@theWatchmen after applying the patch I am able to run chapter 1! Thanks.

theWatchmen commented 9 months ago

I have now applied the fix to all chapters :) I'll leave this open for a few days just in case.

beaumanvienna commented 9 months ago

I got your book in the meantime :-) Thank you, Sir! On a side note: https://www.youtube.com/@beaumanvienna6844 I just finished skeletal animation with gltf and multiple animations, a new video is in the making ...

theWatchmen commented 6 months ago

Closing this as it should be solved.