当我将它们用于数学运算时,为什么我的特化常数不能正常工作?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当我将它们用于数学运算时,为什么我的特化常数不能正常工作?相关的知识,希望对你有一定的参考价值。

我正在编写一个vulkan应用程序,并在使用特化常量时遇到一些麻烦。在Renderdoc中看不到vec4数组,我用它来调试我的应用程序,只要我使用特化常量作为该数组的大小而不是其他常量值。 Renderdoc告诉我,特殊常数的当前值是1;

我正在使用Nvidea GTX 880M并使用LunarG glaslangvalidator编译我的代码。当我直接插入特化常量作为数组大小时没有发生错误,但是当我应用数学运算,即加0时,常量变为0。

这里的GLSL中的代码是由renderdoc从编译代码生成的:

#version 450

layout(constant_id = 2) const int TEXTURE_COORDS_COUNT = 1;
const int _61 = (0 + TEXTURE_COORDS_COUNT);
const int _63 = (TEXTURE_COORDS_COUNT / 4);
const int _64 = (_61 - _63);
const int specialSize = (0 + TEXTURE_COORDS_COUNT);

layout(set = 0, binding = 0, std140) uniform cameraUniform
{
    mat4 perspectiveView;
} cu;

layout(location = 0) in mat4 instanceTransformation;
layout(location = 4) in vec3 inPosition;
layout(location = 0) out vec4 fragNormals;
layout(location = 5) in vec3 inNormals;
layout(location = 1) out vec4 fragSpecial[specialSize];
layout(location = 6) in vec4 inSpecial[specialSize];

void main()
{
    gl_Position = (cu.perspectiveView * instanceTransformation) * vec4(inPosition, 1.0);
    fragNormals = (cu.perspectiveView * instanceTransformation) * vec4(inNormals, 0.0);
    for (int i = 0; i < _64; i++)
    {
        fragSpecial[i] = inSpecial[i];
    }
}

这里是Renderdoc生成的spir-v代码:

; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 7
; Bound: 82
; Schema: 0
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Vertex %main "main" %_ %instanceTransformation %inPosition %fragNormals %inNormals %fragSpecial %inSpecial
               OpSource GLSL 450
               OpSourceExtension "GL_ARB_separate_shader_objects"
               OpName %main "main"
               OpName %gl_PerVertex "gl_PerVertex"
               OpMemberName %gl_PerVertex 0 "gl_Position"
               OpMemberName %gl_PerVertex 1 "gl_PointSize"
               OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
               OpMemberName %gl_PerVertex 3 "gl_CullDistance"
               OpName %_ ""
               OpName %cameraUniform "cameraUniform"
               OpMemberName %cameraUniform 0 "perspectiveView"
               OpName %cu "cu"
               OpName %instanceTransformation "instanceTransformation"
               OpName %inPosition "inPosition"
               OpName %fragNormals "fragNormals"
               OpName %inNormals "inNormals"
               OpName %i "i"
               OpName %TEXTURE_COORDS_COUNT "TEXTURE_COORDS_COUNT"
               OpName %specialSize "specialSize"
               OpName %fragSpecial "fragSpecial"
               OpName %inSpecial "inSpecial"
               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
               OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
               OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
               OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
               OpDecorate %gl_PerVertex Block
               OpMemberDecorate %cameraUniform 0 ColMajor
               OpMemberDecorate %cameraUniform 0 Offset 0
               OpMemberDecorate %cameraUniform 0 MatrixStride 16
               OpDecorate %cameraUniform Block
               OpDecorate %cu DescriptorSet 0
               OpDecorate %cu Binding 0
               OpDecorate %instanceTransformation Location 0
               OpDecorate %inPosition Location 4
               OpDecorate %fragNormals Location 0
               OpDecorate %inNormals Location 5
               OpDecorate %TEXTURE_COORDS_COUNT SpecId 2
               OpDecorate %fragSpecial Location 1
               OpDecorate %inSpecial Location 6
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
      %float = OpTypeFloat 32
    %v4float = OpTypeVector %float 4
       %uint = OpTypeInt 32 0
     %uint_1 = OpConstant %uint 1
%_arr_float_uint_1 = OpTypeArray %float %uint_1
%gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
        %int = OpTypeInt 32 1
      %int_0 = OpConstant %int 0
%mat4v4float = OpTypeMatrix %v4float 4
%cameraUniform = OpTypeStruct %mat4v4float
%_ptr_Uniform_cameraUniform = OpTypePointer Uniform %cameraUniform
         %cu = OpVariable %_ptr_Uniform_cameraUniform Uniform
%_ptr_Uniform_mat4v4float = OpTypePointer Uniform %mat4v4float
%_ptr_Input_mat4v4float = OpTypePointer Input %mat4v4float
%instanceTransformation = OpVariable %_ptr_Input_mat4v4float Input
    %v3float = OpTypeVector %float 3
%_ptr_Input_v3float = OpTypePointer Input %v3float
 %inPosition = OpVariable %_ptr_Input_v3float Input
    %float_1 = OpConstant %float 1
%_ptr_Output_v4float = OpTypePointer Output %v4float
%fragNormals = OpVariable %_ptr_Output_v4float Output
  %inNormals = OpVariable %_ptr_Input_v3float Input
    %float_0 = OpConstant %float 0
%_ptr_Function_int = OpTypePointer Function %int
%TEXTURE_COORDS_COUNT = OpSpecConstant %int 1
         %61 = OpSpecConstantOp %int IAdd %int_0 %TEXTURE_COORDS_COUNT
      %int_4 = OpConstant %int 4
         %63 = OpSpecConstantOp %int SDiv %TEXTURE_COORDS_COUNT %int_4
         %64 = OpSpecConstantOp %int ISub %61 %63
       %bool = OpTypeBool
%specialSize = OpSpecConstantOp %int IAdd %int_0 %TEXTURE_COORDS_COUNT
%_arr_v4float_specialSize = OpTypeArray %v4float %specialSize
%_ptr_Output__arr_v4float_specialSize = OpTypePointer Output %_arr_v4float_specialSize
%fragSpecial = OpVariable %_ptr_Output__arr_v4float_specialSize Output
%_ptr_Input__arr_v4float_specialSize = OpTypePointer Input %_arr_v4float_specialSize
  %inSpecial = OpVariable %_ptr_Input__arr_v4float_specialSize Input
%_ptr_Input_v4float = OpTypePointer Input %v4float
      %int_1 = OpConstant %int 1
       %main = OpFunction %void None %3
          %5 = OpLabel
          %i = OpVariable %_ptr_Function_int Function
         %21 = OpAccessChain %_ptr_Uniform_mat4v4float %cu %int_0
         %22 = OpLoad %mat4v4float %21
         %25 = OpLoad %mat4v4float %instanceTransformation
         %26 = OpMatrixTimesMatrix %mat4v4float %22 %25
         %30 = OpLoad %v3float %inPosition
         %32 = OpCompositeExtract %float %30 0
         %33 = OpCompositeExtract %float %30 1
         %34 = OpCompositeExtract %float %30 2
         %35 = OpCompositeConstruct %v4float %32 %33 %34 %float_1
         %36 = OpMatrixTimesVector %v4float %26 %35
         %38 = OpAccessChain %_ptr_Output_v4float %_ %int_0
               OpStore %38 %36
         %40 = OpAccessChain %_ptr_Uniform_mat4v4float %cu %int_0
         %41 = OpLoad %mat4v4float %40
         %42 = OpLoad %mat4v4float %instanceTransformation
         %43 = OpMatrixTimesMatrix %mat4v4float %41 %42
         %45 = OpLoad %v3float %inNormals
         %47 = OpCompositeExtract %float %45 0
         %48 = OpCompositeExtract %float %45 1
         %49 = OpCompositeExtract %float %45 2
         %50 = OpCompositeConstruct %v4float %47 %48 %49 %float_0
         %51 = OpMatrixTimesVector %v4float %43 %50
               OpStore %fragNormals %51
               OpStore %i %int_0
               OpBranch %54
         %54 = OpLabel
               OpLoopMerge %56 %57 None
               OpBranch %58
         %58 = OpLabel
         %59 = OpLoad %int %i
         %66 = OpSLessThan %bool %59 %64
               OpBranchConditional %66 %55 %56
         %55 = OpLabel
         %71 = OpLoad %int %i
         %74 = OpLoad %int %i
         %76 = OpAccessChain %_ptr_Input_v4float %inSpecial %74
         %77 = OpLoad %v4float %76
         %78 = OpAccessChain %_ptr_Output_v4float %fragSpecial %71
               OpStore %78 %77
               OpBranch %57
         %57 = OpLabel
         %79 = OpLoad %int %i
         %81 = OpIAdd %int %79 %int_1
               OpStore %i %81
               OpBranch %54
         %56 = OpLabel
               OpReturn
               OpFunctionEnd

这里的工作代码:

#version 450

layout(constant_id = 2) const int specialSize = 1;
const int _61 = (0 + specialSize);
const int _63 = (specialSize / 4);
const int _64 = (_61 - _63);

layout(set = 0, binding = 0, std140) uniform cameraUniform
{
    mat4 perspectiveView;
} cu;

layout(location = 0) in mat4 instanceTransformation;
layout(location = 4) in vec3 inPosition;
layout(location = 0) out vec4 fragNormals;
layout(location = 5) in vec3 inNormals;
layout(location = 1) out vec4 fragSpecial[specialSize];
layout(location = 6) in vec4 inSpecial[specialSize];

void main()
{
    gl_Position = (cu.perspectiveView * instanceTransformation) * vec4(inPosition, 1.0);
    fragNormals = (cu.perspectiveView * instanceTransformation) * vec4(inNormals, 0.0);
    for (int i = 0; i < _64; i++)
    {
        fragSpecial[i] = inSpecial[i];
    }
}

它的spir-v代码:

; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 7
; Bound: 81
; Schema: 0
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Vertex %main "main" %_ %instanceTransformation %inPosition %fragNormals %inNormals %fragSpecial %inSpecial
               OpSource GLSL 450
               OpSourceExtension "GL_ARB_separate_shader_objects"
               OpName %main "main"
               OpName %gl_PerVertex "gl_PerVertex"
               OpMemberName %gl_PerVertex 0 "gl_Position"
               OpMemberName %gl_PerVertex 1 "gl_PointSize"
               OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
               OpMemberName %gl_PerVertex 3 "gl_CullDistance"
               OpName %_ ""
               OpName %cameraUniform "cameraUniform"
               OpMemberName %cameraUniform 0 "perspectiveView"
               OpName %cu "cu"
               OpName %instanceTransformation "instanceTransformation"
               OpName %inPosition "inPosition"
               OpName %fragNormals "fragNormals"
               OpName %inNormals "inNormals"
               OpName %i "i"
               OpName %TEXTURE_COORDS_COUNT "TEXTURE_COORDS_COUNT"
               OpName %TEXTURE_COORDS_COUNT "specialSize"
               OpName %fragSpecial "fragSpecial"
               OpName %inSpecial "inSpecial"
               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
               OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
               OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
               OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
               OpDecorate %gl_PerVertex Block
               OpMemberDecorate %cameraUniform 0 ColMajor
               OpMemberDecorate %cameraUniform 0 Offset 0
               OpMemberDecorate %cameraUniform 0 MatrixStride 16
               OpDecorate %cameraUniform Block
               OpDecorate %cu DescriptorSet 0
               OpDecorate %cu Binding 0
               OpDecorate %instanceTransformation Location 0
               OpDecorate %inPosition Location 4
               OpDecorate %fragNormals Location 0
               OpDecorate %inNormals Location 5
               OpDecorate %TEXTURE_COORDS_COUNT SpecId 2
               OpDecorate %fragSpecial Location 1
               OpDecorate %inSpecial Location 6
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
      %float = OpTypeFloat 32
    %v4float = OpTypeVector %float 4
       %uint = OpTypeInt 32 0
     %uint_1 = OpConstant %uint 1
%_arr_float_uint_1 = OpTypeArray %float %uint_1
%gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
        %int = OpTypeInt 32 1
      %int_0 = OpConstant %int 0
%mat4v4float = OpTypeMatrix %v4float 4
%cameraUniform = OpTypeStruct %mat4v4float
%_ptr_Uniform_cameraUniform = OpTypePointer Uniform %cameraUniform
         %cu = OpVariable %_ptr_Uniform_cameraUniform Uniform
%_ptr_Uniform_mat4v4float = OpTypePointer Uniform %mat4v4float
%_ptr_Input_mat4v4float = OpTypePointer Input %mat4v4float
%instanceTransformation = OpVariable %_ptr_Input_mat4v4float Input
    %v3float = OpTypeVector %float 3
%_ptr_Input_v3float = OpTypePointer Input %v3float
 %inPosition = OpVariable %_ptr_Input_v3float Input
    %float_1 = OpConstant %float 1
%_ptr_Output_v4float = OpTypePointer Output %v4float
%fragNormals = OpVariable %_ptr_Output_v4float Output
  %inNormals = OpVariable %_ptr_Input_v3float Input
    %float_0 = OpConstant %float 0
%_ptr_Function_int = OpTypePointer Function %int
%TEXTURE_COORDS_COUNT = OpSpecConstant %int 1
         %61 = OpSpecConstantOp %int IAdd %int_0 %TEXTURE_COORDS_COUNT
      %int_4 = OpConstant %int 4
         %63 = OpSpecConstantOp %int SDiv %TEXTURE_COORDS_COUNT %int_4
         %64 = OpSpecConstantOp %int ISub %61 %63
       %bool = OpTypeBool
%_arr_v4float_TEXTURE_COORDS_COUNT = OpTypeArray %v4float %TEXTURE_COORDS_COUNT
%_ptr_Output__arr_v4float_TEXTURE_COORDS_COUNT = OpTypePointer Output %_arr_v4float_TEXTURE_COORDS_COUNT
%fragSpecial = OpVariable %_ptr_Output__arr_v4float_TEXTURE_COORDS_COUNT Output
%_ptr_Input__arr_v4float_TEXTURE_COORDS_COUNT = OpTypePointer Input %_arr_v4float_TEXTURE_COORDS_COUNT
  %inSpecial = OpVariable %_ptr_Input__arr_v4float_TEXTURE_COORDS_COUNT Input
%_ptr_Input_v4float = OpTypePointer Input %v4float
      %int_1 = OpConstant %int 1
       %main = OpFunction %void None %3
          %5 = OpLabel
          %i = OpVariable %_ptr_Function_int Function
         %21 = OpAccessChain %_ptr_Uniform_mat4v4float %cu %int_0
         %22 = OpLoad %mat4v4float %21
         %25 = OpLoad %mat4v4float %instanceTransformation
         %26 = OpMatrixTimesMatrix %mat4v4float %22 %25
         %30 = OpLoad %v3float %inPosition
         %32 = OpCompositeExtract %float %30 0
         %33 = OpCompositeExtract %float %30 1
         %34 = OpCompositeExtract %float %30 2
         %35 = OpCompositeConstruct %v4float %32 %33 %34 %float_1
         %36 = OpMatrixTimesVector %v4float %26 %35
         %38 = OpAccessChain %_ptr_Output_v4float %_ %int_0
               OpStore %38 %36
         %40 = OpAccessChain %_ptr_Uniform_mat4v4float %cu %int_0
         %41 = OpLoad %mat4v4float %40
         %42 = OpLoad %mat4v4float %instanceTransformation
         %43 = OpMatrixTimesMatrix %mat4v4float %41 %42
         %45 = OpLoad %v3float %inNormals
         %47 = OpCompositeExtract %float %45 0
         %48 = OpCompositeExtract %float %45 1
         %49 = OpCompositeExtract %float %45 2
         %50 = OpCompositeConstruct %v4float %47 %48 %49 %float_0
         %51 = OpMatrixTimesVector %v4float %43 %50
               OpStore %fragNormals %51
               OpStore %i %int_0
               OpBranch %54
         %54 = OpLabel
               OpLoopMerge %56 %57 None
               OpBranch %58
         %58 = OpLabel
         %59 = OpLoad %int %i
         %66 = OpSLessThan %bool %59 %64
               OpBranchConditional %66 %55 %56
         %55 = OpLabel
         %70 = OpLoad %int %i
         %73 = OpLoad %int %i
         %75 = OpAccessChain %_ptr_Input_v4float %inSpecial %73
         %76 = OpLoad %v4float %75
         %77 = OpAccessChain %_ptr_Output_v4float %fragSpecial %70
               OpStore %77 %76
               OpBranch %57
         %57 = OpLabel
         %78 = OpLoad %int %i
         %80 = OpIAdd %int %78 %int_1
               OpStore %i %80
               OpBranch %54
         %56 = OpLabel
               OpReturn
               OpFunctionEnd

specialSize应为1(就像在工作示例中一样),但是为0.感谢您的帮助!

编辑:

如果没有人能够找出问题,也许有人可以告诉我这是否是一个错误,我必须报告它以便修复它。

编辑:

看起来像Renderdoc包含一个在正确的时间没有计算某些值的错误。

答案

这个问题可能会解决。有一件事可能是基于Renderdoc中的错误计算,其他组件(此处未提及)可能基于一些已知的验证层问题。

以上是关于当我将它们用于数学运算时,为什么我的特化常数不能正常工作?的主要内容,如果未能解决你的问题,请参考以下文章

具有编译时间常数的模板特化

当我将它们添加到UIKitView时,为什么我的对象会被放置并且尺寸如此完美?

当我将它们的元素设置为彼此堆叠(块)时,为啥我的 flex-box 元素会“浮动”彼此相邻?

当我将屏幕大小调整为移动设备时,我的网站移动视图在桌面上正确显示隐藏元素,但在我的手机上它们没有隐藏

认识数学各个分支

Java打包问题