【问题标题】:Non-constant index into uniform array CG Shader均匀数组 CG 着色器中的非常量索引
【发布时间】:2012-11-09 20:18:08
【问题描述】:

根据this nVidia CG tutorial(和我自己的经验),在 CG 着色器中使用非常量索引访问统一数组要么效率低下,要么不受支持(通常情况下,似乎不受支持)。

我的问题是;我该如何规避这个问题?

我目前正在编写一个 GPU 蒙皮着色器,我在其中传递了一组骨骼(4x4 矩阵),我需要使用存储在顶点属性中的索引来访问这些骨骼(具体来说,它的组件被转换为整数的 float4 向量) .显然,由于上述限制,这不起作用……也许我错过了更好的方法?

【问题讨论】:

    标签: graphics shader skinning vertex-shader cg


    【解决方案1】:

    这确实是常见的做法,例如(这是 HLSL,但本质上是一样的——注意全局统一的 'boneArray')

    float4x3 CalcBoneTransform(float4 blendWeights, float4 boneIndices)
    {
      // Calculate normalized fourth bone weight2
      float4 weights = float4(blendWeights.x, blendWeights.y, blendWeights.z , 1.0f - blendWeights.x - blendWeights.y - blendWeights.z);
      // Calculate bone transform
      float4x3 boneTransform;
      int4 indices = boneIndices;
      boneTransform =  weights.x * boneArray[indices.x];
      boneTransform += weights.y * boneArray[indices.y];
      boneTransform += weights.z * boneArray[indices.z];
      boneTransform += weights.w * boneArray[indices.w];
      return boneTransform;
    }
    

    【讨论】:

    • 感谢您的回复!事实证明,事实上,可以为我的平台(Playstation Mobile)执行此操作,但是有一个未记录的怪癖,这意味着单个统一数组最多可能只有 256 个字节(因此是四个 4x4 矩阵的数组) .一旦我发现了这一点,只需将我的骨骼数组拆分成单独的 256 字节块。