Sign in to follow this  
howie_007

managing your shaders

Recommended Posts

Since so much can be done with shaders, I was wondering how you manage this with the limited amount of shader memory?

Say for example, you have your lighting setup with shaders. 5 lights for example. That alone takes up a bunch of shader memory. Now you want to add more code for other effects like blurring. You can quickly run out of memory or the shader no longer works. Since the lighting is such a big foot print, do you find that you have a bunch of shader (*.fx) files where your lighting code is the same?

Is there a better way to manage lighting and all the other effects?

Share this post


Link to post
Share on other sites
I don't quite understand. Shader code is loaded into the same memory as textures and vertices - you can load thousands of different shaders without running out of memory.

If your shaders no longer work, there's specific errors in them that need to be fixed on a case by case basis.

If you're reusing the same code in different shader files, you can put that code in a header file and #include it.

Share this post


Link to post
Share on other sites
Even though I've written a simple light shader, I guess I'm not fulling seeing the best way to work with more then one.

I see that I can load multiple *.fx files. Each *.fx file is considered a shader. I don't see how I would use two different shaders on the same mesh. It seems like after you light the pixel, now would be the time to do something else to it. I don't see how using another shader can be used other then repeating the process.

What am I missing here?

Share this post


Link to post
Share on other sites
You're right in a way.. there's no way to feed one shader colour output into another.


You want to do a search on "ubershaders".

ubershaders are ones which do several things per pixel, and you turn the parts on and off using flags. There's various techniques -- it's probably best having a read around.


Another approach is to write all your shaders as routines which (say) take a colour in and return a colour and then you can easily write some code which will write the "main()" part to just compose a list of calls in order depending on what each model asks for. You can load several code fragments into a shader[1] -- the "link" call attaches all the bits together.




[1] Or at least you can in GL, I'm presuming DX does the same.

Share this post


Link to post
Share on other sites
[quote name='howie_007' timestamp='1306797482' post='4817697']
Since so much can be done with shaders, I was wondering how you manage this with the limited amount of shader memory?

Say for example, you have your lighting setup with shaders. 5 lights for example. That alone takes up a bunch of shader memory. Now you want to add more code for other effects like blurring. You can quickly run out of memory or the shader no longer works. Since the lighting is such a big foot print, do you find that you have a bunch of shader (*.fx) files where your lighting code is the same?

Is there a better way to manage lighting and all the other effects?
[/quote]

You are right that the shader memory ( = constant buffer in D3D) has a limited size. However, for example under D3D 11 each of 16 constant buffers has 4096 float4 sized elements. So practically, you can write shaders which can do lighting with "hundreds of lights" in one pass depending on the size of your light structure. There are of course other methods of doing lighting such as light-prepass / deferred lighting which will require less shader constants and possibly less lengthy shaders but require more memory bandwidth.

Effects such as blurring are usually done in post-process. You don't need to combine all the effects in the one and same shader. Consider following : you render your scene normally as you have done so far. After that you'll take the frame buffer/render target and use it as a source for your blurring/bloom/etc shader.

As for the code reuse : you don't need to copy-paste your lighting code to every shader. HLSL, for example, supports include files and you may have your lighting code written only once.

Cheers!

Share this post


Link to post
Share on other sites
[font="arial, verdana, tahoma, sans-serif"][size="2"][quote name='Katie' timestamp='1306834467' post='4817818']
You want to do a search on "ubershaders".
[/quote]
I'll look into that.[/size][/font]
[font="arial, verdana, tahoma, sans-serif"][/font][size="2"]
[/size][font="arial, verdana, tahoma, sans-serif"][/font] [quote name='kauna' timestamp='1306836158' post='4817826']
Effects such as blurring are usually done in post-process. You don't need to combine all the effects in the one and same shader. Consider following : you render your scene normally as you have done so far. After that you'll take the frame buffer/render target and use it as a source for your blurring/bloom/etc shader.
[/quote]
Ok, that makes sense. I guess what I'm confused by is what is done where. Is a given effect done during the render process or is it done post-process?

This was a big help.

Share this post


Link to post
Share on other sites

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

Sign in to follow this