bjornbytes / lovr

Lua Virtual Reality Framework
https://lovr.org
MIT License
1.96k stars 136 forks source link

Instancing throws error and corrupts shader cache on macOS #790

Closed hmans closed 2 months ago

hmans commented 3 months ago

My system:

MacBook Pro 2023 with M2 Pro (couldn't try this on Windows since I'm traveling right now, but I will do so once I'm back home next weekend.)

Version of lovr:

0.17.1 (macOS)

What is happening?

When I run the instancing example, lovr shows the following error:

image

After that, every attempt to start lovr (no matter the project code, or even standalone) fails, showing the following error on the console:

[mvk-error] VK_ERROR_INITIALIZATION_FAILED: Shader library compile failed (Error code 3):
program_source:165:5: error: use of undeclared identifier '_0'
    _0 = gl_ViewIndex - spvViewMask[0];
    ^
.
Error:

GPU error: Pipeline cache creation failed (VK_ERROR_INITIALIZATION_FAILED)

Stack:

boot.lua:92: in function <boot.lua:4>
[C]: in function 'xpcall'

Deleting lovr's shader cache restores lovr back into a functioning state:

rm -rf ~/Library/Application\ Support/LOVR/

To reproduce:

git clone https://github.com/bjornbytes/lovr-docs
cd lovr-docs/examples/Optimization/Instancing/
lovr .
hmans commented 3 months ago

I tried to narrow this down to see what exactly is triggering this by implementing instanced drawing step by step. It seems to happen as soon as my vertex shader starts making use of InstanceIndex, which also isn't listed as a built-in on the Shaders page in the docs.

hmans commented 3 months ago

I've compiled current dev (41da42b153c11b5adba03cae9ae908851c9f5347) from source, and instancing seems to work fine there.

bjornbytes commented 3 months ago

Interesting, this maybe sounds like a bug in MoltenVK/SPIRV-Cross. Which version of MoltenVK do you have?

On dev, the InstanceIndex macro changed from gl_InstanceIndex - gl_BaseInstance to just plain gl_InstanceIndex. Maybe that ended up inadvertently working around a shader compiler issue?

(InstanceIndex isn't on the Shaders guide because that page currently doesn't list any of the builtin GLSL variables, but it should probably mention that!).

bjornbytes commented 3 months ago

Actually, another thing I remembered is that in March we upgraded GitHub actions to use MoltenVK 1.3.275 (up from 1.3.231). It's more likely that this upgrade is what fixed it on dev. See #761.

hmans commented 2 months ago

On dev, the InstanceIndex macro changed from gl_InstanceIndex - gl_BaseInstance to just plain gl_InstanceIndex. Maybe that ended up inadvertently working around a shader compiler issue?

Just for the record, I tried using the gl_* variables directly, and got the same crash.

Actually, another thing I remembered is that in March we upgraded GitHub actions to use MoltenVK 1.3.275 (up from 1.3.231). It's more likely that this upgrade is what fixed it on dev. See #761.

Yeah, that sounds much more likely. I so far haven't managed to get 0.17.1 compiled, but I'll have another go at it today and report back.

hmans commented 2 months ago

As discussed on Matrix, compiling stable with the latest version of the Vulkan SDK yielded the same error. With t.graphics.debug = true, this is the error output:

[mvk-error] VK_ERROR_INITIALIZATION_FAILED: Shader library compile failed (Error code 3):
program_source:160:5: error: use of undeclared identifier '_0'
    _0 = gl_ViewIndex - spvViewMask[0];
    ^
.
VK_ERROR_INITIALIZATION_FAILED: Shader library compile failed (Error code 3):
program_source:160:5: error: use of undeclared identifier '_0'
    _0 = gl_ViewIndex - spvViewMask[0];
    ^
.
[mvk-error] VK_ERROR_INVALID_SHADER_NV: Vertex shader function could not be compiled into pipeline. See previous logged error.
VK_ERROR_INVALID_SHADER_NV: Vertex shader function could not be compiled into pipeline. See previous logged error.
Error:

GPU error: Could not create pipeline

Stack:

boot.lua:126: in function <boot.lua:108>
[C]: in function 'xpcall'
Validation Error: [ VUID-VkVertexInputAttributeDescription-vertexAttributeAccessBeyondStride-04457 ] | MessageID = 0x8690bfd0 | vkCreateGraphicsPipelines(): pCreateInfos[0] (portability error): attribute.offset (0) + sizeof(vertex_description.format) (16) is larger than the vertex stride (0). The Vulkan spec states: If the VK_KHR_portability_subset extension is enabled, and VkPhysicalDevicePortabilitySubsetFeaturesKHR::vertexAttributeAccessBeyondStride is VK_FALSE, the sum of offset plus the size of the vertex attribute data described by format must not be greater than stride in the VkVertexInputBindingDescription referenced in binding (https://vulkan.lunarg.com/doc/view/1.3.283.0/mac/1.3-extensions/vkspec.html#VUID-VkVertexInputAttributeDescription-vertexAttributeAccessBeyondStride-04457)
Validation Error: [ VUID-VkVertexInputAttributeDescription-vertexAttributeAccessBeyondStride-04457 ] | MessageID = 0x8690bfd0 | vkCreateGraphicsPipelines(): pCreateInfos[0] (portability error): attribute.offset (0) + sizeof(vertex_description.format) (16) is larger than the vertex stride (0). The Vulkan spec states: If the VK_KHR_portability_subset extension is enabled, and VkPhysicalDevicePortabilitySubsetFeaturesKHR::vertexAttributeAccessBeyondStride is VK_FALSE, the sum of offset plus the size of the vertex attribute data described by format must not be greater than stride in the VkVertexInputBindingDescription referenced in binding (https://vulkan.lunarg.com/doc/view/1.3.283.0/mac/1.3-extensions/vkspec.html#VUID-VkVertexInputAttributeDescription-vertexAttributeAccessBeyondStride-04457)
Validation Error: [ VUID-vkDestroyDevice-device-05137 ] Object 0: handle = 0x8bdc430000000646, name = Scratch Buffer, type = VK_OBJECT_TYPE_BUFFER; | MessageID = 0x4872eaa0 | vkCreateDevice():  OBJ ERROR : For VkDevice 0x11a864c18[], VkBuffer 0x8bdc430000000646[Scratch Buffer] has not been destroyed. The Vulkan spec states: All child objects created on device must have been destroyed prior to destroying device (https://vulkan.lunarg.com/doc/view/1.3.283.0/mac/1.3-extensions/vkspec.html#VUID-vkDestroyDevice-device-05137)
Validation Error: [ VUID-vkDestroyDevice-device-05137 ] Object 0: handle = 0x91990000000053f, name = Font Atlas, type = VK_OBJECT_TYPE_IMAGE; | MessageID = 0x4872eaa0 | vkCreateDevice():  OBJ ERROR : For VkDevice 0x11a864c18[], VkImage 0x91990000000053f[Font Atlas] has not been destroyed. The Vulkan spec states: All child objects created on device must have been destroyed prior to destroying device (https://vulkan.lunarg.com/doc/view/1.3.283.0/mac/1.3-extensions/vkspec.html#VUID-vkDestroyDevice-device-05137)
Validation Error: [ VUID-vkDestroyDevice-device-05137 ] Object 0: handle = 0x4ea28f0000000540, type = VK_OBJECT_TYPE_IMAGE_VIEW; | MessageID = 0x4872eaa0 | vkCreateDevice():  OBJ ERROR : For VkDevice 0x11a864c18[], VkImageView 0x4ea28f0000000540[] has not been destroyed. The Vulkan spec states: All child objects created on device must have been destroyed prior to destroying device (https://vulkan.lunarg.com/doc/view/1.3.283.0/mac/1.3-extensions/vkspec.html#VUID-vkDestroyDevice-device-05137)
Validation Error: [ VUID-vkDestroyDevice-device-05137 ] Object 0: handle = 0xf0ad2e0000000643, type = VK_OBJECT_TYPE_SHADER_MODULE; | MessageID = 0x4872eaa0 | vkCreateDevice():  OBJ ERROR : For VkDevice 0x11a864c18[], VkShaderModule 0xf0ad2e0000000643[] has not been destroyed. The Vulkan spec states: All child objects created on device must have been destroyed prior to destroying device (https://vulkan.lunarg.com/doc/view/1.3.283.0/mac/1.3-extensions/vkspec.html#VUID-vkDestroyDevice-device-05137)
Validation Error: [ VUID-vkDestroyDevice-device-05137 ] Object 0: handle = 0xeed6010000000644, type = VK_OBJECT_TYPE_SHADER_MODULE; | MessageID = 0x4872eaa0 | vkCreateDevice():  OBJ ERROR : For VkDevice 0x11a864c18[], VkShaderModule 0xeed6010000000644[] has not been destroyed. The Vulkan spec states: All child objects created on device must have been destroyed prior to destroying device (https://vulkan.lunarg.com/doc/view/1.3.283.0/mac/1.3-extensions/vkspec.html#VUID-vkDestroyDevice-device-05137)
Validation Error: [ VUID-vkDestroyDevice-device-05137 ] Object 0: handle = 0xca7b200000000645, type = VK_OBJECT_TYPE_PIPELINE_LAYOUT; | MessageID = 0x4872eaa0 | vkCreateDevice():  OBJ ERROR : For VkDevice 0x11a864c18[], VkPipelineLayout 0xca7b200000000645[] has not been destroyed. The Vulkan spec states: All child objects created on device must have been destroyed prior to destroying device (https://vulkan.lunarg.com/doc/view/1.3.283.0/mac/1.3-extensions/vkspec.html#VUID-vkDestroyDevice-device-05137)
bjornbytes commented 2 months ago

Going to close this since it was accidentally fixed on dev. This is definitely a bug in spirv-cross related to multiview.

wolf81 commented 1 month ago

@bjornbytes Is this fixed in the latest stable macOS builds? As I've actually installed the most recent stable build on macOS, but I still see the error.

bjornbytes commented 1 month ago

It's fixed on dev, not stable. The fix can't easily be applied to the stable branch because it was a big rework to how shader variables work