http://www.ogre3d.org/phpBB2/viewtopic. ... 127#155127
.. in which I port crashy's HLSL instancing code to GLSL.
It includes code if you want to look in to any of these issues, but it's not the most minimal demonstration of the problem.
I guess I'll just list a few of the problems I ran in to. I'm not submitting a patch because I still think it's likely that I just went about this the wrong way.
The main thing the instancing program does is to set up a lot of custom parameters on the batchInstance renderable that get written into the elements of an array in the vertex shader.
The constants aren't referenced in the material file because there are a lot of them, so they are added in code. in the original HLSL version from crashy, this is done simply with:
Code: Select all
for (int i =0;i<objectCount;i++)
{
params->setAutoConstant(i, GpuProgramParameters::ACT_CUSTOM,i);
}
i needed to use named constants, but I had no idea what names to use to refer to an array. in the shader it is defined as
Code: Select all
uniform vec4 Instance_Posititons[200]
Code: Select all
Instance_Positions[0]
Instance_Positions[1]
...
Instance_Positions[199]
Code: Select all
for (int i=0;i<objectCount;++i)
{
String name = "Instance_Position["+StringConverter::toString(i)+"]";
params->setNamedAutoConstant(name,GpuProgramParameters::ACT_CUSTOM,i);
}
Code: Select all
setNamed*Constant
Code: Select all
getParamIndex(name)
Code: Select all
for (int i=0;i<objectCount;++i)
{
String name = "Instance_Position["+StringConverter::toString(i)+"]";
params->setNamedConstant(name,Vector4::ZERO);
params->setNamedAutoConstant(name,GpuProgramParameters::ACT_CUSTOM,i);
}
Instance_Position[0] is an array of 200 elements
Instance_Position[1] is an array of 199 elements
etc
so each step it goes through and assigns all 200 RealConstants to Instance_Position[0]. then the next constants 1-199 to Instance_Position[1], 2-199 to Instance_Position[2] etc.. it seems that you need to write to the whole array at once, because it seems the final call writing the last value into Instance_Position[199] is the only value remaining in the array when the shader gets run. this is why in my earlier screenshot in the linked thread, shows each batch of ninjas only containing the last ninja.
i tried commenting out the array handling code in updateUniforms but it writing each element of the array individually still had the same effect, so i needed a way to get it to write the whole array. basically i came up with this solution, naming the parameters thusly:
Code: Select all
String name = i==0?"Instance_Position[0]":"Instance_Position-"+StringConverter::toString(i);
anyhow, i hope this makes some sense to someone who has enough knowledge of this part of the code to fix it.
in a sense this all would have been easier had GLSL supported indexed parameters, but this probably also would have played havoc with its very-late-binding of parameter names.
pix.