Material / Shader and vertex buffers

Started by
9 comments, last by emosoft05 19 years, 3 months ago
Hello, I'm working on my 3D engine, and have something that works with OpenGL and DirectX in separate dll, I recently add material scripts, with different technique, pass, and texture layer, (something that looks like Ogre materials) and now I'm trying to add Cg shaders. Now I create my mesh with a default vertex format, but I have to change it to work with multiple pass and multiple texture layers. I need to know which vertex format to use when I create the vertex buffer for a mesh, Is it a good idea to define the vertex format in the material script ? or it's possible (better ?) to find which vertex elements are needed while reading the material script ? Another problem, at the beginning I thought I will need, for a mesh, only one vertex buffer for each different material used in this mesh, but now, I think some material will need a different vertex shader at each pass, so each pass could have a different vertex declaration, and need a different vertex buffer. How to create needed vertex buffers and vertex streams for a mesh with a given material script, without storing many times the same vertex elements for each passes ? Thanks.
Advertisement

Please, no one has an answer, or advice about my question ?
or you don't understand my problem ?

Should I try to reformulate my question ?


Thanks.



Hmm... I think I see what you're asking.

I can't give you much input on the OpenGL side, I haven't worked with it in a while, but with Direct3D you could create all the different data for your mesh in separate streams quite easily, which would mean you don't store redundant data as you'd not really be using a different vertex buffer, just perhaps assigning the streams differently to fit the declaration.

As for the vertex format/elements/declaration you either need to define this in the script somehow (perhaps with a components field, which just lists what that material requires) or you do it intelligently at runtime by reading what the material contains. IIRC from that big Material/Shader Implementation thread, there was a field in the shader which specified what data that shader needed from the geometry in order to render correctly.

Have I understood the question correctly?

-Mezz

Thank you for the answer,
it seems more simple to define the vertex declaration in the script,
but I'm not sure to understand for vertex streams.

My problem is not specific to OpenGL or Direct3D,
I don't understand how to find which vertex elements and streams to use,
if I have multiple pass.

Example :

If I have an Ambient pass, that need vertex position, and texture coordinates,
then a Diffuse pass for each lights... (ex : for bump-mapping)

I need the vertex position for these two pass,
but I don't want to store the position twice.

How to know that I must have the position, or other elements,
in a separate stream ?


OK, it seems like you're going to need to define the vertex properties inside the script.

If you think about what a vertex declaration is - it's really just a list of things that make up a vertex e.g. position, normal, texture coordinates.

Each of those elements could be in a separate stream, just as long as you make them available to the shader which will need them when rendering. I'd probably create all the vertex data you need in different streams, then you just need to figure out from the declaration in the script which streams need to be set for that shader to work.

I hope that answers a bit more.

-Mezz

I understand.
I think, for now, I will define vertex declaration in the script,
and do as you say, to have something that works,
later I could try to find a way to "guess" the vertex declaration
while reading the script.

Thanks again.


Have you looked into the CgFX or .FX file format? Even though you may not use them explicitly, they could give you good ideas. Check out the "Semantics" and "Annotations" sections of the DX documentation on Effect files.

Essentially you can read meta-data about the vertex shader, including any necessary constants and the vertex elements that are required. I think your scripts are very similar to these two file formats, you'll have to compare.

If you can spare the bandwidth and you don't want to implement the steams as Mezz suggested, you could simply just determine the largest vertex format that you will need for the most complex shader, and use that one for all of your draw calls. I find that the simplest solution is sometimes the best! [smile]

I don't want to use .fx or CgFX, only need Vertex Shader and Pixel Shader,
because later I may add the possibility to use Assembly, or GLSL programs.
but I will have a better look at them.

I can use the same vertex format for all pass but I will have unused elements
and have to change the vertex declaration in the Cg program too.

the object you are rendering should determine most of the vertex format. static world geometry probably doesn't need bone weights for example. but some parts of the vertex format should be determined by the shader which is usually defined by the material somehow. the shader would specify if you needed normal, tangent, and bitangent vectors or any other user defined variables. you could always define the vertex format in the material script, but that might restrict which materials can be applied to what objects.

so position is always a given, you can pretty much always assume that. texcoords should be derived from the shader/material and possibly the type of texture. for example, the base texture always needs it own texcoords, lightmap probably needs it own (if u use them), normal map can probably use the base texcoords, etc. the material would define if you need tangent space or not. the object itself would determine if you need bone weights or some specular color.

one thing that will make life easier is to build the vertex format in the same way for every shader. essentially everything is going to be in the same order, you just might not use the same number of components each time. as long as you stick to the same conventions when writing your shaders, you can use the same function for determining all vertex formats. plus you will also want a function that makes sure your vertex format is the right amount of bytes.
I understand how to create the function that is determining the vertex format,
but this doesn't solve the problem if I have multiple pass,
or I will have to use a different stream for each elements,
and select needed streams when rendering a pass.

Thanks.

This topic is closed to new replies.

Advertisement