Skip to main content
gamedev.net
🔒 Locked

Optimising texture-state changes with CG

Started by MessiahAndrw Apr 10, 2009 at 7:50 AM 1 replies 850+ views
Original Post
MessiahAndrw
MessiahAndrw
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.
MessiahAndrw
MessiahAndrw
I've disabled shadowing, which is doing what I want. But is there a general rule (e.g. the order or parameters) that defines the index of each resource each shader uses?

E.g. how do I know "sampler2d diffuse" in on shader uses the same sampler was "sample2d diffuse" in another shader?
MJP
MJP
Unfortunately I know very little about CG, and have never actually used it myself. In HLSL, shader constants are generally bound to constant registers in the order they're declared. So your first float4 would get mapped to c0, the second to c1, and so on. The same goes for sampling registers. I don't believe this is guaranteed, however you can explicitly bind a constant or sampler to a register like this:
float4 someConstant : register (c0);sampler2D someSampler : register (s0);


Now I don't know if CG supports this same syntax, but it's possible since from what I know it's rather similar to HLSL. I would check through the documentation and try it out, since it would let you manually bind your textures without going through the CG interface.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.