rchit.glsl shader与hitAttributeEXT变量的奇怪行为

huangapple go评论74阅读模式
英文:

Weird rchit.glsl shader behaviour with the hitAttributeEXT variable

问题

以下是翻译的内容:

我在处理一些Vulkan光线追踪渲染器时遇到了一个视觉错误。当我尝试调试时,我注意到问题出在我的rchit.glsl(射线最接近命中)代码上,与hitAttributeEXT变量有关。在将问题分解为最简单的可重现情况后,我感到困惑,每当对该参数执行计算时,我会得到难以解释的图形输出。

#version 460
#extension GL_EXT_ray_tracing : require
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_scalar_block_layout : enable

layout(location = 0) rayPayloadInEXT vec3 hitValue; // 这只是最终输出结果

hitAttributeEXT vec2 attribs; // 射线击中的三角形的重心坐标

void main()
{
    // 为了测试,我要么取消注释要么注释掉了另一个部分
    // 射线追踪着色器测试1
    // 直接设置hitValue为attribs(这按预期工作)
    hitValue = vec3(1.0, attribs);

    // 射线追踪着色器测试2
    // 添加了一个分支(这不按预期工作)
    if (attribs.x > 0.5)
    {
        hitValue = vec3(1.0, 1.0, 1.0);
    } else {
        hitValue = vec3(0.0, 0.0, 0.0);
    }

    // 射线追踪着色器测试3
    // 用于验证的固定值
    hitValue = vec3(0.0, 1.0, 0.0);
}

在下面,我进行了几次测试,使用了一个投影的球体。

射线追踪着色器测试1

所以在这里,事情似乎按预期工作,三角形的一个角有红色,另一个角有红色+蓝色=紫色,最后一个角有红色+绿色=黄色。

rchit.glsl shader与hitAttributeEXT变量的奇怪行为

射线追踪着色器测试2

所以这对我来说完全令人困惑,我无法理解如何在我的着色器设置下出现红色。

rchit.glsl shader与hitAttributeEXT变量的奇怪行为

射线追踪着色器测试3

使用恒定颜色输出

rchit.glsl shader与hitAttributeEXT变量的奇怪行为

常规光栅化

为了提供一些背景信息,这是使用光栅化渲染器的对象真实外观。

rchit.glsl shader与hitAttributeEXT变量的奇怪行为


一些额外信息

Vulkan版本:

  • "vulkan-headers/1.3.211.0"

  • "vulkan-loader/1.3.211.0"

  • "vulkan-validationlayers/1.3.211.0"

VulkanSDK-1.3.211.0中的glslc:

$ glslc --version


spirv-tools v2022.2-dev v2022.1-61-geed5c76a

glslang 11.1.0-457-gabbe4664

目标:SPIR-V 1.0

glslc命令:

glslc --target-env=vulkan1.3 rayhit_shader.rchit -o rayhit_shader.spv

英文:

I was working on some Vulkan raytracing renderers when I encountered a visual bug. When I tried to debug it I noticed it came down to my rchit.glsl (ray closest hit) code and had to do with the hitAttributeEXT variable. After breaking it down to the simplest reproducible scenario I became dumbfounded that whenever a computation is performed on said parameter I get inexplicable graphical output.

#version 460
#extension GL_EXT_ray_tracing : require
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_scalar_block_layout : enable

layout(location = 0) rayPayloadInEXT vec3 hitValue; // This is simply the resulting output

hitAttributeEXT vec2 attribs; // barycentric coordinates of the triangle hit by a ray

void main()
{
    // For testing purposes I either uncommented or commented out a separate section
	// Raytracing Shader Test 1
	// setting hit value directly as attribs (this works as expected)
	hitValue = vec3(1.0, attribs);

	// Raytracing Shader Test 2
	// Adding a branch (this does not work as expected)
	if (attribs.x > 0.5)
	{
		hitValue = vec3(1.0, 1.0, 1.0);
	} else {
		hitValue = vec3(0.0, 0.0, 0.0);
	}

	// Raytracing Shader Test 3
	// fixed value for sanity check
	hitValue = vec3(0.0, 1.0, 0.0);
}

In the below I made a couple tests with a projected ico-sphere.

Raytracing Shader Test 1

So here things appear to work as expected, triangles have red on one corner, red+blue=purple on another and red+green=yellow on the final corner.

rchit.glsl shader与hitAttributeEXT变量的奇怪行为

Raytracing Shader Test 2

So this is completely perplexing to me, I can't see how the color red is even possible given my shader setup.

rchit.glsl shader与hitAttributeEXT变量的奇怪行为

Raytracing Shader Test 3

Using a constant color output

rchit.glsl shader与hitAttributeEXT变量的奇怪行为

Regular Rasterization

And just for some context this is how the object really looks with a rasterization renderer.

rchit.glsl shader与hitAttributeEXT变量的奇怪行为


some additional info

Vulkan vers:

  • "vulkan-headers/1.3.211.0",

  • "vulkan-loader/1.3.211.0",

  • "vulkan-validationlayers/1.3.211.0",

glslc from VulkanSDK-1.3.211.0:

$ glslc --version

shaderc v2022.1 v2022.1

spirv-tools v2022.2-dev v2022.1-61-geed5c76a

glslang 11.1.0-457-gabbe4664

Target: SPIR-V 1.0

glslc command:

glslc --target-env=vulkan1.3 rayhit_shader.rchit -o rayhit_shader.spv

答案1

得分: 1

以下是您要翻译的内容:

我在 Reddit 上发布了这个帖子,有人建议这与我的着色器绑定表设置有关,那就是我发现问题的地方,以防其他人遇到类似的情况...

与着色器索引的着色器绑定表和光线追踪管线布局不匹配有关。

在我的光线追踪管线布局中,我有以下内容:

VkRayTracingShaderGroupCreateInfoKHR raygen{VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR};
raygen.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR;
raygen.generalShader = 0;

VkRayTracingShaderGroupCreateInfoKHR rayhit{VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR};
rayhit.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR;
rayhit.closestHitShader = 1;

VkRayTracingShaderGroupCreateInfoKHR raymiss{VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR};
raymiss.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR;
raymiss.generalShader = 2;

std::vector<VkRayTracingShaderGroupCreateInfoKHR> rt_shader_groups = { raygen, rayhit, raymiss };

与此同时,我在我的着色器绑定表设置中有以下内容:

raygen_sbt_region.deviceAddress = sbt_address;
raymiss_sbt_region.deviceAddress = sbt_address + raygen_sbt_region.size;
rayhit_sbt_region.deviceAddress = sbt_address + raygen_sbt_region.size + raymiss_sbt_region.size;

正确同步这两者修复了问题。我不知道为什么 raymiss 和 hit 着色器有时之前能正常工作。

英文:

I posted this on reddit and someone suggested it had something to do with my shader binding table setup and that's where I discovered the issue, in case someone else stumbles into something similar...

It had to do with a shader binding table and raytracing pipeline layout mismatch of the shader index.

I had something like the below in my raytracing pipeline layout

VkRayTracingShaderGroupCreateInfoKHR raygen{VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR};
raygen.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR;
raygen.generalShader = 0;

VkRayTracingShaderGroupCreateInfoKHR rayhit{VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR};
rayhit.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR;
rayhit.closestHitShader = 1;

VkRayTracingShaderGroupCreateInfoKHR raymiss{VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR};
raymiss.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR;
raymiss.generalShader = 2

std::vector&lt;VkRayTracingShaderGroupCreateInfoKHR&gt; rt_shader_groups = { raygen, rayhit, raymiss };

Meanwhile I had this in my shader binding table setup

raygen_sbt_region.deviceAddress = sbt_address;
raymiss_sbt_region.deviceAddress = sbt_address + raygen_sbt_region.size;
rayhit_sbt_region.deviceAddress = sbt_address + raygen_sbt_region.size + raymiss_sbt_region.size;

Syncing the two properly fixed the issue. I have no idea how the raymiss and hit shaders worked sometimes previously.

huangapple
  • 本文由 发表于 2023年4月13日 21:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定