Passing custom vertex data to shader

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
MasterD
Posts: 153
Joined: Sun Feb 15, 2004 4:17 pm
Location: Lübeck, Germany
Contact:

Passing custom vertex data to shader

Post by MasterD »

Hi all,

I'm currently developing a planet scale surface LOD system. I'm thinking about implementing geomorphing with the vertex shader, inspired by ShaderX² / http://www.gamedev.net/columns/hardcore ... efault.asp.

Allthough I'm not that willing to to it with ASM I'm not quite sure if this technique is useable with Irrlicht right out of the box. I must admit that I'm still a novice regarding shader programming and apart from some basic OpenGL programming I've only used Irrlicht for my 3D programming needs so far. So this is something like the excuse, if this stuff has already been answered, if so, please give me a pointer.

Back to the discussed geomorphing:
Here the vertex declaration (DX term?) is used to pass customized vertex data to the shader, meaning e.g.

Code: Select all

...
; Vertex components (as specified in the vertex DECLARATION)
;
;    v0      = (posX, posZ, texX, texY)
;    v1      = (posY, yMoveSelf, 0.0, 1.0)
... other following
You see, there is a mixed vector with positions and texture coordinates.

I'm currently searching for the place within Irrlicht's sources (DX9 part) where the addressed vertex declaration is set up, in order to modify this part of Irrlicht or use what is already there.

Currently I'm thinking that I will be able to get geomorphing working with just a second vertex position, which I could e.g. code into the vertex color.

Thanks in advance!

edit: Please don't get the last paragraph wrong: If this would be sufficient, I could just enter something like "float4 col : COLOR0" as function parameter, but I still want to know this connection between Irrlicht's data and shader vertex data.
YASS - Yet another Space Shooter
under Devolpment, see http://yass-engine.de
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

The color is 8-bit, so it wouldn't be sufficient without compacting a 32bit value into all 4 value and decompressing it in the shader.

I recommend instead you look at irr::video::S3DVertex2TCoords (Gives you two extra 32-bit precision floats for storage) or even S3DVertexTangents (Gives you two extra 32-bit precision 3d vectors for storage).

If you are for example already using the second set of texcoords for texturing (Very likely for terrain), then the second option might be best, and you could just store the texcoords inside the tangent or the binormal (Infact, funnily enough, Irrlicht already hijacks the 2nd and 3rd texture coordinate sets for tangent and binormal storage, so they are actually made for storing texcoords :P ). For these you can just use the registers for TEXCOORD1 and TEXCOORD2 in the shader.

Cheers, I hope I was helpful somehow
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
MasterD
Posts: 153
Joined: Sun Feb 15, 2004 4:17 pm
Location: Lübeck, Germany
Contact:

Post by MasterD »

I think I will switch to tangents vertices as there is still some room for data left.

When talking about irrlicht's usage of the vertex shader semantics, you mean that I can't use BINORMAL or TANGENT when addressing the parts of S3DVertexTangents?

Can you show me some directions where that connection between irrlicht and shader data is made?
YASS - Yet another Space Shooter
under Devolpment, see http://yass-engine.de
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Can you show me some directions where that connection between irrlicht and shader data is made?
I'm having trouble fully understanding what you mean here, so I'll just take the broad approach.

Irrlicht and Shaders By BlindSide
--------------------------------------

This is Timmy, he has just reached 14 years of age, Timmy is starting to see hair in places where .. woops wrong topic, let me try again,

This is Timmy, Timmy wants to exchange arbitary data between Irrlicht's application code and his own custom shader, Timmy has a few choices:

- Through the setting of custom shader constants using a shader callback.
- Through "custom" vertex data, by hijacking the texcoord or color attributes.
- By passing a texture that contains the necessary data (Only for pixel shaders.)
you mean that I can't use BINORMAL or TANGENT when addressing the parts of S3DVertexTangents?
Here is a short snippet from the built in Irrlicht NormalMapping shader:

Code: Select all

";Irrlicht Engine 0.8 D3D9 render path normal map vertex shader\n"\
   24 		"; c0-3: Transposed world matrix \n"\
   25 		"; c8-11: Transposed worldViewProj matrix (Projection * View * World) \n"\
   26 		"; c12: Light01 position \n"\
   27 		"; c13: x,y,z: Light01 color; .w: 1/LightRadius� \n"\
   28 		"; c14: Light02 position \n"\
   29 		"; c15: x,y,z: Light02 color; .w: 1/LightRadius� \n"\
   30 		"vs.1.1\n"\
   31 		"dcl_position  v0              ; position \n"\
   32 		"dcl_normal    v1              ; normal \n"\
   33 		"dcl_color     v2              ; color \n"\
   34 		"dcl_texcoord0 v3              ; texture coord \n"\
   35 		"dcl_texcoord1 v4              ; tangent \n"\
   36 		"dcl_texcoord2 v5              ; binormal \n"\
You can see in the last two lines that it uses the texcoord sets 1 and 2 for the tangent and binormal information.

Here is a case statement inside CD3D9Driver.cpp:

Code: Select all

 840 			case EVT_TANGENTS:
  841 				vertexSize = sizeof(S3DVertexTangents);
  842 				FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX3;
  843 				break;
You can see that it uses the Flexible Vertex Format (FVF) to declare a vertex with a position, normal, diffuse colour and 3 sets of texcoord information.

Cheers, I hope that somehow answered your question
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
MasterD
Posts: 153
Joined: Sun Feb 15, 2004 4:17 pm
Location: Lübeck, Germany
Contact:

Post by MasterD »

Well...

... Timmy wants to pass custom vertex data and the FVF declaration is just what he searched. But if Timmy had looked with some care at the sources he might have found it by himself...

So I presume I deserved that Timmy stuff... Thanks for the snippet
YASS - Yet another Space Shooter
under Devolpment, see http://yass-engine.de
Post Reply