Communicating with shader from within a VBO

Started by poo2thegeek, July 24, 2014, 07:57:59

Previous topic - Next topic

poo2thegeek

Is there a way to communicate with a shader from within a VBO.
I am using inter weaved VBO's separated into chunks to render a hex map. Each hex may be one o several different types of terrain.
Most of these (dirt, grass, hill) will use the same shader. However some of the of the tiles will need separate shaders, such as the water tiles.

If I need to I can simply create several VBO's, one for each different type of shader, and just bind the correct shader for each VBO.
But I was wondering if I could instead create a uniform integer inside my shader (i.e "uniform int HEX_ID"), and use an else if statement inside my shader
if(HEX_ID==WATER){
//DoWater
}else if(HEX_ID==DIRT or GRASS ...){
//Do normal
}


Basically I need a way of communicating with the shader from inside the VBO. I could always create an unneeded bit of data inside the VBO (Such as setting a colour pointer that I wont use inside the shader, and then just using the colour to decide what too do inside the shader. i.e "if(gl_Color==vec4(1,0,0,1))".
However I don't think this would be very efficient.

Thanks for any help :D

quew8

Use different shaders. If statements, especially like that, are extremely inefficient in shaders (explanation below). It doesn't have to be several VBOs, you can have the same VBO and just render each terrain type individually, changing the shader in between. But definitely write a different shader for each terrain type.

The reason if statements are so inefficient in shaders is that fragments are batched together to be processed by the shader. Hence you have multiple instances of the same shader running simultaneously. Obviously one fragment could go down one branch of and if/else statement whilst another could do another and so with the simultaneous instances, all branches of the if/else must be run for every fragment regardless of whether or not they are needed. Clearly not a good idea. I'm sure newer hardware has a better solution than what I stated but the principle is the same and you will see a fairly large drop in performance by using complex if statements.