Jump to content
  • Advertisement
Sign in to follow this  
maxam

Engine Designed around shaders

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want my engine to be based on shaders. I want to(should I?) eliminate fixed function all togeather. Humans have a tendancy to put off advancement as long as possible(x86,32bit cpu,dos progs <-sigh). My targeted systems will be 1.5ghz+, 256mb ram, video card with vs&ps(1.0 or 2.0 <-me no no) minimal. What do you think? Any one know any good tutorials, tech docs about shaders to get me started?

Share this post


Link to post
Share on other sites
Advertisement
Uh, that's fine. Many engines these days use a programmable-pipeline renderpath exclusively, if for no other reason than because it's simpler. In terms of documentation, the documentation for your chosen API will be the most useful for you (since the engine itself won't tend to care about the particular visual effects being implemented).

Share this post


Link to post
Share on other sites
Well, seeing how my project is mainly just a hobby thing, I found that throwing out fixed function simplified my code a lot. It might be possible to use vertex shaders for per vertex lighting (which should work on old cards too).


Share this post


Link to post
Share on other sites
Ok so fixed function is out, but my renderer is plugin based. I need some type of general outline for my shader system, so that when I write a directx or opengl renderer they have an organized design, and produce the same quality image.

Share this post


Link to post
Share on other sites
CgFX for opengl or Effect files for DirectX may be a great help to you. You can google for whichever you would like to use, but they will allow you to wrap your renderstate changes, shaders, and passes into a single interface which helps out the hobbyist writing this post :)

Share this post


Link to post
Share on other sites
My engine is designed around effect files, meaning I can still use fixed function for fallbacks [smile]

Share this post


Link to post
Share on other sites
If you want to be serious, and have fast rendering - drop DX effects, they are slow, and they are black-box. And perhabs - too general.
Writing something that can compile HLSL from a file, and produce one PS&VS is very easy - maybe just adding a support for describing render states would be helpful, and that's it.
Shader based - that alone means nothing.
What decisions would you made about materials, about lighting & shadowing?
Can a shader be plugged in the system, and somehow magically all will work...?
(there are ways to do that, but at a cost, of course...).
How these shaders will be managed, how will be bind to instances (shader variables management) and how will scale to meet the requirements of the scene - number and type of lights, fog, shadows, etc.
That's a whole lot to be thinking of.
Dropping FFP is just a very tiny step. Drop it. We dropped it, and I don't sorry even a bit...
But ps11 is still viable option. :)

Share this post


Link to post
Share on other sites
I chose to go with just using plain HLSL shaders in txt files to begin with, as I didn't want to have all the overhead of effect files confusing me at first.

The DirectX Docs are pretty good for information about HLSL shaders, but the info you need to simple get a "hello world" shader working, is spread out quite a bit.

I'll try to put here all the base code you might need.



//vertex shader file
float4x4 worldViewProj;
float4 vLight;

struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Diff : COLOR0;
float2 Tex1 : TEXCOORD0;
float2 Tex2 : TEXCOORD1;
};
VS_OUTPUT vs_main(float4 inPos : POSITION, float3 inNormal : NORMAL, float2 Tex1 : TEXCOORD0, float2 Tex2 : TEXCOORD1 )
{
VS_OUTPUT Out;
Out.Pos = mul(inPos, worldViewProj);

Out.Diff = -dot( inNormal, vLight );
Out.Diff = saturate(Out.Diff);
Out.Diff += 0.2;

Out.Tex1 = Tex1;
Out.Tex2 = Tex2;
return Out;
}

//pixel shader file
float4 ps_main( float4 inDiffuse: COLOR0, float2 inTx1: TEXCOORD0, float2 inTx2: TEXCOORD1 ) : COLOR0
{
float4 outColor = 0;
outColor.r = 1;
return outColor;
}


//in your header
IDirect3DPixelShader9* mlpSplatPixelShader;
IDirect3DVertexShader9* mlpVertexShader;
ID3DXConstantTable* mpVSConstantTable;

//in your cpp, when you initialize
D3DCAPS9 caps;
mlpD3DDevice->GetDeviceCaps(&caps);

ID3DXBuffer* pShader = NULL;

HRESULT hr;

//vertex shader
if(caps.VertexShaderVersion >= D3DVS_VERSION(1, 1))
{
// Compile the vertex shader
hr = D3DXCompileShaderFromFile(
"GraphicsEngine/shaders/mapHLSL.vsh",
NULL,
NULL,
"vs_main",
"vs_1_1",
/*D3DXSHADER_DEBUG*/D3DXSHADER_SKIPVALIDATION,
&pShader,
NULL, // error messages
&mpVSConstantTable );
if( FAILED(hr) )
{
LOG("Error! Failed compiling vertex shader from file");
SafeRelease(pShader);
SafeRelease(mpVSConstantTable);
return hr;
}
// Create the vertex shader
hr = mlpD3DDevice->CreateVertexShader( (DWORD*)pShader->GetBufferPointer(), &mlpVertexShader );
SafeRelease(pShader);
if( FAILED(hr) )
{
SafeRelease(mlpVertexShader);
SafeRelease(mpVSConstantTable);
return hr;
}
}
else
{
LOG("Error! Video card does not support Vertex Shader 1.1 ");
return E_FAIL;
}

//pixel shader
if(caps.PixelShaderVersion >= D3DPS_VERSION(1, 4))
{
hr = D3DXCompileShaderFromFile(
"GraphicsEngine/shaders/mapHLSL.psh",
NULL,
NULL,
"ps_main",
"ps_1_4",
D3DXSHADER_SKIPVALIDATION,
&pShader,
NULL,
NULL );
if(FAILED(hr))
{
LOG("Error! Failed compiling pixel shader from file");
SafeRelease(pShader);
return hr;
}

//create the pixel shader
hr = mlpD3DDevice->CreatePixelShader((DWORD*)pShader->GetBufferPointer(), &mlpSplatPixelShader);
SafeRelease(pShader);
if( FAILED(hr) )
{
SafeRelease(mlpSplatPixelShader);
return hr;
}

}
else
{
LOG("Error! Video card does not support Pixel Shader 1.4 ");
return E_FAIL;
}



//in your cpp, just before rendering
mlpD3DDevice->SetVertexShader(mlpVertexShader);
mlpD3DDevice->SetPixelShader(mlpSplatPixelShader);

matViewProj = matView * matProj;
D3DXHANDLE hMatrixConstantHandle = mpVSConstantTable->GetConstantByName( NULL, "worldViewProj" );
mpVSConstantTable->SetMatrix( mlpD3DDevice, hMatrixConstantHandle, &matViewProj );

//set the sunlight
D3DXHANDLE hLightConstantHandle = mpVSConstantTable->GetConstantByName( NULL, "vLight" );
mpVSConstantTable->SetVector( mlpD3DDevice, hLightConstantHandle, &sunLight );


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://www.gamedev.net/community/forums/topic.asp?topic_id=169710

http://www.gamedev.net/community/forums/topic.asp?topic_id=217522


// ville

Share this post


Link to post
Share on other sites
Quote:
Original post by Zemedelec
drop DX effects, they are slow

Not true anymore. They got serious speed boost in Summer 2004 SDK update.

Quote:
Original post by Anonymous Poster
Material/shader implementation

Shader system questions

Those are very good threads, and I recommend you to read them both. Although the shader system described in those threads is quite complicated, it can give you some ideas.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!