Jump to content
Sign in to follow this  
  • entries
    132
  • comments
    99
  • views
    89099

I don't think its working...

Sign in to follow this  
Driv3MeFar

126 views

So apparently, I'm pretty retarded when it comes to making a shader framework. I can write shaders well enough, but damned if I know how to use them.

For instance, I was testing out my latest framework attempt with a simple little vertex lighting shader. Just as a guess, I don't think this it how it should look:


I think one (probably of many) of the things I'm doing wrong is not making sure my mesh has the correct FVF. I'm just using the d3dx functions to load tiny, and then drawing her with a drawsubset() call. I'm pretty sure I need to use CloneMeshFVF() and all that to make sure my vertex format actually matches what the vertex shader expects.

Other than that, I still don't have a very good idea of how to handle shaders in a nice, generic fashion. What I'm trying to do now is make a custom FXVariable type and inherit from it for each shader variable type (matrices, vectors, arrays, and the like). Then, each variable provides its own SetParam function which calls the appropriate Set_____() call. It looks something like this:

FXVars.h:

#ifndef FXVARS_H
#define FXVARS_H

#include
#include
#include

class FXVariable
{
public:
virtual ~FXVariable() {}

virtual void SetEffectParam(ID3DXEffect *effect) = 0;

void SetHandle(D3DXHANDLE h)
{
m_handle = h;
}
protected:
D3DXHANDLE m_handle;
};

class FXMatrix : public FXVariable
{
public:
virtual void SetEffectParam(ID3DXEffect *effect)
{
HRESULT hr = effect->SetMatrix(m_handle, &m_value);
assert(SUCCEEDED(hr)); (void)hr;
}

void SetValue(const D3DXMATRIX &value)
{
m_value = value;
}
private:
D3DXMATRIX m_value;
};

class FXFloat3 : public FXVariable
{
public:
virtual void SetEffectParam(ID3DXEffect *effect)
{
HRESULT hr = effect->SetFloatArray(m_handle, m_value, 3);
assert(SUCCEEDED(hr)); (void)hr;
}

void SetValue(const float *value)
{
std::copy(value, value + 3, m_value);
}
private:
float m_value[3];
};


#endif



However, this isn't very elegant. I still need to know the names and types of every variable when I create the effect, just not when I draw it, and actually setting the value of an FXVariable requires reinterpret_casting nonsense. Plus, for every type of variable I want to use in a shader, I need to create a new class. Not too fun. The other problem is that my renderer has to store shaders in this monstrosity:


stdext::hash_map< ULONG, std::pair > > m_shaders;


Drawing something with a shader ends up looking like this (you can see where I've hard coded a technique in just to test):
Renderer.cpp

bool Renderer::RenderMesh(ULONG shaderID, LPD3DXMESH mesh)
{
assert(m_shaders.find(shaderID) != m_shaders.end());

std::vector &vars = m_shaders[shaderID].second;
ID3DXEffect *effect = m_shaders[shaderID].first;

//set all shader params
for (std::vector::iterator it = vars.begin(); it != vars.end(); ++it)
{
(*it)->SetEffectParam(effect);
}

//HACK
effect->SetTechnique("Vertex Lighting");

UINT numPasses;
effect->Begin(&numPasses, 0);

for (UINT iPass = 0; iPass < numPasses; iPass++)
{
effect->BeginPass(iPass);

// Render the mesh with the applied technique
mesh->DrawSubset(0);

effect->EndPass();
}
effect->End();


return true;
}



At least, that's how it all works now. And now, its horribly broken. There has to be a better way to handle shaders, I just can't think of anything at the moment other than hardcoding, and that doesn't sound fun.

So, my question to you, the reader, is: how do you handle shaders in your engine? And if you don't, how would you, or where would you go to find out?
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!