HLSL:未对齐/打包浮动的索引 [英] HLSL: Index to unaligned/packed floats
问题描述
我有一个顶点着色器(2.0)做一些实例化-每个顶点都指定一个数组索引.
I have a vertex shader (2.0) doing some instancing - each vertex specifies an index into an array.
如果我有一个像这样的数组:
If I have an array like this:
float instanceData[100];
编译器为其分配100个常量寄存器.每个常量寄存器都是一个float4
,因此它分配的空间是所需的4倍.
The compiler allocates it 100 constant registers. Each constant register is a float4
, so it's allocating 4 times as much space as is needed.
我需要一种方法,使其仅分配25个常量寄存器,并在每个常量寄存器中存储四个值.
I need a way to make it allocate just 25 constant registers and store four values in each of them.
理想情况下,我想要一种在CPU和GPU上仍看起来像float[]
的方法(现在我正在调用EffectParamter.SetValue(Single[])
,我正在使用XNA).但是也可以手动打包和解包float4[]
.
Ideally I'd like a method where it still looks like a float[]
on both the CPU and GPU (Right now I am calling EffectParamter.SetValue(Single[])
, I'm using XNA). But manually packing and unpacking a float4[]
is an option, too.
此外:这样做对性能有何影响?真的值得吗? (对我来说,这将每四到五次节省大约一批次).
Also: what are the performance implications for doing this? Is it actually worth it? (For me, this will save about one batch in every four or five).
推荐答案
这有帮助吗?:
float4 packedInstanceData[25];
...
float data = packedInstanceData[index / 4][index % 4];
这篇关于HLSL:未对齐/打包浮动的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!