From dd7bd8469e0504ea103f6cdea30c2d9cc9bd4859 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 24 Aug 2025 12:33:54 +0200 Subject: [PATCH] Fix shader fragment rate vulkan validation error --- client/scenes/stream_reprojection.cpp | 63 ++++++++++++++------------- client/shaders/reprojection.glsl | 54 ++--------------------- client/shaders/reprojection.glsl.inc | 56 ++++++++++++++++++++++++ client/shaders/reprojection_vsr.glsl | 5 +++ 4 files changed, 97 insertions(+), 81 deletions(-) create mode 100644 client/shaders/reprojection.glsl.inc create mode 100644 client/shaders/reprojection_vsr.glsl diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index eefb06cb..662ad97a 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -214,8 +214,20 @@ stream_reprojection::stream_reprojection( renderpass = vk::raii::RenderPass(device, renderpass_info.get()); + const auto & vk_device_extensions = application::get_vk_device_extensions(); + bool fragment_shading_rate = false; + if (utils::contains(vk_device_extensions, VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME) and + utils::contains(vk_device_extensions, VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) + { + const auto & [prop, rate_prop] = physical_device.getProperties2(); + const auto & [feat, fragment_feat] = physical_device.getFeatures2(); + fragment_shading_rate = rate_prop.fragmentShadingRateNonTrivialCombinerOps and + fragment_feat.primitiveFragmentShadingRate and + fragment_feat.attachmentFragmentShadingRate; + } + // Vertex shader - vk::raii::ShaderModule vertex_shader = load_shader(device, "reprojection.vert"); + vk::raii::ShaderModule vertex_shader = load_shader(device, fragment_shading_rate ? "reprojection_vsr.vert" : "reprojection.vert"); // Fragment shader @@ -317,40 +329,31 @@ stream_reprojection::stream_reprojection( .fragmentSize = vk::Extent2D{1, 1}, .combinerOps = combiner, }; - const auto & vk_device_extensions = application::get_vk_device_extensions(); - if (utils::contains(vk_device_extensions, VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME) and - utils::contains(vk_device_extensions, VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) + if (fragment_shading_rate) { - const auto & [prop, rate_prop] = physical_device.getProperties2(); - const auto & [feat, fragment_feat] = physical_device.getFeatures2(); - if (rate_prop.fragmentShadingRateNonTrivialCombinerOps and - fragment_feat.primitiveFragmentShadingRate and - fragment_feat.attachmentFragmentShadingRate) - { - pipeline_info.pNext = &shading; + pipeline_info.pNext = &shading; - spdlog::info("Available fragment shading rates:"); - for (const auto rate: physical_device.getFragmentShadingRatesKHR()) + spdlog::info("Available fragment shading rates:"); + for (const auto rate: physical_device.getFragmentShadingRatesKHR()) + { + if (rate.sampleCounts & vk::SampleCountFlagBits::e1) { - if (rate.sampleCounts & vk::SampleCountFlagBits::e1) + spdlog::info("\tfragment size: {}x{}", rate.fragmentSize.width, rate.fragmentSize.height); + int flags = 0; + if (rate.fragmentSize.width == 4) + flags |= 8; + else if (rate.fragmentSize.width == 2) + flags |= 4; + if (rate.fragmentSize.height == 4) + flags |= 2; + else if (rate.fragmentSize.height == 2) + flags |= 1; + for (int y = std::bit_width(rate.fragmentSize.height) - 1; y < 3; ++y) { - spdlog::info("\tfragment size: {}x{}", rate.fragmentSize.width, rate.fragmentSize.height); - int flags = 0; - if (rate.fragmentSize.width == 4) - flags |= 8; - else if (rate.fragmentSize.width == 2) - flags |= 4; - if (rate.fragmentSize.height == 4) - flags |= 2; - else if (rate.fragmentSize.height == 2) - flags |= 1; - for (int y = std::bit_width(rate.fragmentSize.height) - 1; y < 3; ++y) + for (int x = std::bit_width(rate.fragmentSize.width) - 1; x < 3; ++x) { - for (int x = std::bit_width(rate.fragmentSize.width) - 1; x < 3; ++x) - { - if (fragment_sizes[x][y] == 0) - fragment_sizes[x][y] = flags; - } + if (fragment_sizes[x][y] == 0) + fragment_sizes[x][y] = flags; } } } diff --git a/client/shaders/reprojection.glsl b/client/shaders/reprojection.glsl index 286041a8..8cb7decb 100644 --- a/client/shaders/reprojection.glsl +++ b/client/shaders/reprojection.glsl @@ -1,52 +1,4 @@ -/* - * WiVRn VR streaming - * Copyright (C) 2022 Guillaume Meunier - * Copyright (C) 2022 Patrick Nicolas - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - #version 450 - -#extension GL_EXT_fragment_shading_rate: require -#ifdef VERT_SHADER - -layout (location = 0) in vec2 vPosition; -layout (location = 1) in vec2 vUV; -layout (location = 2) in int vShadingRate; - -layout(location = 0) out vec2 outUV; - -void main() -{ - gl_Position = vec4(vPosition, 0.0, 1.0); - outUV = vUV; - gl_PrimitiveShadingRateEXT = vShadingRate; -} -#endif - -#ifdef FRAG_SHADER - -layout(set = 0, binding = 0) uniform sampler2D texSampler; - -layout(location = 0) in vec2 inUV; - -layout(location = 0) out vec4 outColor; - -void main() -{ - outColor = texture(texSampler, inUV).rgba; -} - -#endif +#extension GL_ARB_shading_language_include : require +#define VSR 0 +#include "reprojection.glsl.inc" diff --git a/client/shaders/reprojection.glsl.inc b/client/shaders/reprojection.glsl.inc new file mode 100644 index 00000000..b82b7b4f --- /dev/null +++ b/client/shaders/reprojection.glsl.inc @@ -0,0 +1,56 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022 Guillaume Meunier + * Copyright (C) 2022 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifdef VERT_SHADER + +#if VSR + #extension GL_EXT_fragment_shading_rate: enable +#endif + + +layout (location = 0) in vec2 vPosition; +layout (location = 1) in vec2 vUV; +layout (location = 2) in int vShadingRate; + +layout(location = 0) out vec2 outUV; + +void main() +{ + gl_Position = vec4(vPosition, 0.0, 1.0); + outUV = vUV; +#if VSR + gl_PrimitiveShadingRateEXT = vShadingRate; +#endif +} +#endif + +#ifdef FRAG_SHADER + +layout(set = 0, binding = 0) uniform sampler2D texSampler; + +layout(location = 0) in vec2 inUV; + +layout(location = 0) out vec4 outColor; + +void main() +{ + outColor = texture(texSampler, inUV).rgba; +} + +#endif diff --git a/client/shaders/reprojection_vsr.glsl b/client/shaders/reprojection_vsr.glsl new file mode 100644 index 00000000..6c1d9d11 --- /dev/null +++ b/client/shaders/reprojection_vsr.glsl @@ -0,0 +1,5 @@ +#version 450 +#extension GL_ARB_shading_language_include : require +#define VSR 1 +#include "reprojection.glsl.inc" +