Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm designing Material/Shader System (target API DX10+ and may be OpenGL3+, now only DX10). I know, there was a lot of topics about this, but i can't find what i need.

I don't want to do some kind of compilation/parsing scripts in real-time.

So there some artist-created material, written at some analog of CG. After it compiled to hlsl code and after to final shader. Also there are some hard-coded ConstantBuffers, like

cbuffer EveryFrameChanging
{ 
    float4x4 matView;
    float time;
    float delta;
}

And shader use shared constant buffers to get parameters. For each mesh in the scene, getting needs and what it can give (normals, binormals etc.) and finding corresponding permutation of shader or calculating missing parts.

Also, during build calculating render states and the permutations or hash for this shader which later will be used for sorting or even giving the ID from 0 to ShaderCount w/o gaps to it for sorting.

FinalShader have only 1 technique and one pass. After it for each Mesh setting some shader and it's good to render.

some pseudo code

SetConstantBuffer(ConstantBuffer::PerFrame);
foreach (shader in FinalShaders)
    SetConstantBuffer(ConstantBuffer::PerShader, shader);
    SetRenderState(shader);
    foreach (mesh in shader.GetAllMeshes)
        SetConstantBuffer(ConstantBuffer::PerMesh, mesh); 
        SetBuffers(mesh);
        Draw();       


class FinalShader
{
public:
    UUID m_ID;
    RenderState m_RenderState;
    CBufferBindings m_BufferBindings;
}

But i have no idea how to create this CG language and do i really need it? May be WYSIWYG editor?

Edit:

I found interesting articles: Shader Designer and here DICE Frostbyte. I think it's good cause it's more easy to build then parsing custom language and more flexible cuz all graph elements are inherited from IBaseNode which have inputs, outputs, so cbuffer EveryFrameChanging is also IBaseNode with 0 inputs and 3 outputs. Maybe possible apply graph optimization theory to shaders. Any usefull links or advises?

share|improve this question
1  
"Do you need it" depends on what you're making. An answer I recently gave which might be of relevance though not a direct answer to your question: gamedev.stackexchange.com/questions/57957/… –  Sean Middleditch Jun 25 '13 at 2:30
add comment

1 Answer

This is a common thing where there is tons of different implementations.
I Will give you my approach, how i would implement this. ( according to your psudo code )

SetConstantBuffer( GlobalData ); <- this would be bound to the first register, so i always know that this one will be there.

For( unsigned int i = 0; i < m_Materials; ++i ) <- where this is a variable containing the amount of material we got in our "database"
SetConstantBuffer( MaterialSpecificData ) <- R1
SetRenderState( MaterialSpecificStates )
for( unsigned int u = 0; u < meshQueue[i].size(); ++i )
Draw->

This point is all the same. But the main thing i would do is to have a loadtime collector, where you collect each mesh, find itś assigned material and from there store them correctly in the meshQueue, where meshqueue would be a vector vector, so you could store an array for each corresponding material.

For the shaders, I would have one main Include named something like, System.H or Materials.H which would hold the Registery0 where all the "per frame" stuff would be held. Also some neat helpers you might need.

From that point on, you could have a define or a integer saying what part of a shader to run. You could compile different part of the program with ifdef/ifndef. So if you would have a "Main" shader entry point, you could esily branch that one out to different branches by just having defines beeing defined before running. since the .h files wont get compiled you can change them on the fly with simple c++ code.

I hope this answers your question as you hoped.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.