0

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

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

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

4

1 回答 1

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;
}
于 2012-12-04T22:32:30.297 回答