multiple shaders per model

Started by
2 comments, last by tanzanite7 12 years ago
hi
(i'm using opengl 4.0 but this question is rather theroetical so thats not important)

currently i have a structure, like this (simplified)
model{ // object on screen with a world matrix
modelpart{ // a part of the model with a material, e.g. arm, leg, or pants, or window (if model is a house..)
mesh { vertBuffer, iBUffer... etc.. }
}
}


when i render, i activate the models shader, a normal shader for example ( sometimes in renderlists a shader is even activated along multiple models)

my question is, what if i would like to have different shaders on each modelpart, how do you guys go on about that?
if for example the naked arm has a different shader than some fancy blink metal armor on the chest, or a part has normalmapping, and a part is parallax?

because at the moment the Model->Render() method sets a ModelViewMatrix for the active shader, and now each ModelPart would need a reference to the parent Model's ModelViewMatrix, and activate it's own shader and set the modelviewmatrix


is this situation even realistic or does a shader per model suffice in most cases? Or is it that extreme that instead of rendering Models, you have renderlists of modelparts with the same shader?

would it help to chang my model structure somehow?


thanks in advance
Advertisement
Different meshes can share the same vertex/index buffers -- the model can own these buffers, and then the meshes can contain offsets into the buffers.
Likewise, different meshes can share the same constant buffers (aka uniform buffers in GL) -- you can write the common parameters (e.g. modelViewMatrix) into one cbuffer, which is then used by every shader on that model.

Or is it that extreme that instead of rendering Models, you have renderlists of modelparts with the same shader?
Yes, kind of. When you "render" a model, I wouldn't actually have it render anything, I would have the model insert it's meshes into an array. After all models to be drawn have been inserted into the array, I would then sort it and then actually render the sorted meshes.
ah very interesting. i had also thought about seperating meshes from models once but i took the convenient (and slow) route and set the buffer pointers on each mesh draw call. but with your way i can bind a buffer for a model of a kind once and then render it on different positions with different parameters.

this brings me to another question:

when you have your meshes sorted in a list, if we say for example 3 meshes share the same buffers and are drawn one after the other, but for some reason one of those wants to use a different shader. what would you prioritise?

- would you rather group meshes with the same buffer first, and then with those meshes sort those with the same shaders and change shaders more often
- or group shaders, and then within shaders group meshes which share buffers?



thanks is advance, your answer was very very helpful

what would you prioritise?

Shader. However, nearly all my meshes come from one buffer (over 128MB buf in my current project) - so, there essentially are no buffer changes anyway. And there are only a few vertex formats (two, currently) sourcing from that buffer.

This topic is closed to new replies.

Advertisement