How much memory do Shaders use up?

Started by CodeBunny, July 18, 2012, 14:34:56

Previous topic - Next topic

CodeBunny

I'm currently programming a resource management system for my game, and I'm considering how to manage shaders. Basically, do Shaders use up that much memory (not shader programs, their shared components)?

I'm wondering if it would be a good idea to load all base shaders at game start-up, and then create programs with them as needed.

Evil-Devil

Imho you should only load and use the shaders needed by the game assets. As for the creation I´ve seen both ways in retail games. Some games compile them on runtime and others ship general shaders precompiled and the need to recompile on some machine configuration.

So how many unique lines of shaders does your system will have to handle?
In this document for Starcraft II they had about 8.000 unique lines of shader code amongst 70 files. I doubt they will all be loaded at startup.

So maybe it is preferable to create a mechanism that can handle both ways.

CodeBunny

As I'm writing a 2D game, and using shaders minimally to make things simpler, I don't have nearly that much. At all. As such, I'm thinking it'll be best to use my current strategy - initialize all shader components on startup, and then create shader programs with them as required.

sasmaster

You can use a lot .CryEngine uses thousands of shaders during the runtime for example.But that is if you have a good modern GPU . Still they best practice is reusing the same shader program object between different entities.So you don' t compile and load texture+lighting shader program for each material in the scene but do it once then just send object specific uniforms and attributes during rendering.

CodeBunny

That's basically what I'm doing. I'll share shaders between objects as much as possible, using a reference counting system to manage them. However, if I want to have object-specific uniform values, I create a standalone shader for that object.

sasmaster

For the shader programs initialization .I ,for example, don't init all the programs at once at all but on demand only.So if some entity requests a specific program ,there is a program pool which checks in the bank of already created programs if this one exists.If it does ,the entity gets a ref to that program ,and if not then this program is created.This way there are no same duplicated programs in the context.Also there is a good reading in the "Game engine Gems 2" on deferred uniforms system for shader program calls.Basically you can cut a number of calls to your shader programs (which impacts the performance) to a minimum if you send to the GPU only the data that has changed on each draw call.