Skip to main content
gamedev.net
🔒 Locked

Shader instancing

Started by Christoph 4 replies 3.5k views
Original Post
Christoph
Christoph
Hi all! I want to render simple vegetation (grass) on my terrain using shader instancing. Unfortunately, the only resource I found was the DX SDK sample which uses a .fx-file and contains a lot of overhead. Thus I have a general idea what instancing is but don't know how to implement it. Can you explain what to do to use shader instancing or do you know tutorials concerning this? tHx!
paic
paic
Hi,

On the NVidia developer website, there's a good demo about geometry instancing :

whitepapaer
video
demo

Basically, it consists on setting 2 vertex streams. The first will gives the base geometry. The second will provide "per-instance" informations, such as model matrices, or other parameters.
Christoph
Christoph
Thank you for the reply.
Yet I still don't understand the role shaders play. I am now able to split vertex data and use SetSreamSourceFreq - however, they're always talking about shaders 3.0 (which my graphics card doesn't support). Also, I don't want to use HLSL... Do you have a final tip for me how to apply a "simple" version of instancing without complex shaders?
tHx
LosBrutalos
LosBrutalos
If you use the Effect class, using of shaders if much simplified than using seperate instructions, my opinion.
If i understand the concept, you are able to do instancing with vs_2_0 as well. The technic is another....you did´nt have the advantage of hardware-instancing like in vs_3_0.
In the ms-demo (wich has really pretty overhead) are presented 4 technics for instanceing. I did´nt implement the vs_2_0 technic for my own, cause i´ve got a nvidia 6800 card, so i can ´t tell you detailed how it works.

Los Brutalos(Sorry for my brutal english ;)
paic
paic
You can't use hardware instancing without shaders (hlsl or asm, it's the same at the end)

The reason is : you use 1 vertex stream to put your "per-instance" data. Those data are usually matrices (model matrices) And in a vertex, you DON'T have the possibility to store a matrix. So you must use (for example) position, normal, texCoord0 and texCoord1 to store your matrix. And then, how do the card know that the position given in your vertex stream is in fact the first row of a model matrix ? And if you want to use texCoordN only instead of position and normal ?

So you HAVE TO write a shader which know where to take the information, and what to do with it. The FFP has no way to do that.
Christoph
Christoph
Hmmm, I'm quite new to shaders.
Now my vertex structure contains pos and tex0. My instance structure consists only of a Vec4 (where x,y,z is the translation and the w-factor is the rotation on the y-axis):

struct GrassVertex
{
Vector3 position;
Vector2 texture;
}
struct GrassInstance
{
Vector4 matrix;
}

D3DVERTEXELEMENT9 grassVertexFormat[] = {
{0,0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION,0},
{0,3 * sizeof(float),D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0},
{1,0,D3DDECLTYPE_FLOAT4,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,1},
D3DDECL_END()
};

Can you show me a shader which translates and rotates the vertex position according to this Vec4 and passes through tex0?

Thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.