Original Post
I'm using CG in a graphics framework I'm writing to render a large number of meshes using the same shaders. Through profiling with PerfHUD I've come to realise my bottleneck is through texture state changes. I do culling to construct a list of first by material then by mesh, so my rendering pipeline is:
- Loop through each material
- Set vertex program.
- Loop through each mesh using this material
- Load index/vertex program for this.
- Loop through each instance of this mesh
- Load world/view/proj matrix into vertex program's parameter.
- Sey first pass fragment program.
- Render
- If more than one pass:
- Disable z-writing and enable additive blending
- Loop through each pass.
- Set fragment program.
- Render
- Enable z-writing and enable additive blending
I'm loading the fragment program using cgD3D9BindProgram, and I notice that during each call it's doing texture-state changes. Can I do an optimisating in CG for sharing parameters (sampler2d's) across multiply shaders? (I haven't found any info of this in the SDK docs, unless I'm looking for the wrong thing) For example, if I have a material with two passes, and both fragment program's accept these following parameters: uniform sampler2D diffuse, uniform sampler2D normal Which reference the exact same textures, then preferably I would like to change the texture state only once when I begin using a material, not for each instance of each mesh using that material like I am now.