Sign in to follow this  

d3d11 + cgfx

Recommended Posts


In my cgfx shader I have a sampler parameter:

uniform sampler2D texAlbedo;

In my shader load code, I loop over all effect parameters starting at [b]cgGetFirstEffectParameter[/b]

For any parameters where cgGetParameterType(param) == CG_SAMPLER2D I cache the CGparameter handle

Now, when I am ready to render, I'd like to call PSSetShaderResources() to bind the ID3D11ShaderResourceView* that I have for a texture.

How do I get the index of the shader resource?

Calling cgGetParameterResourceIndex on the parameter returns 0xFFFFFFFF, which is obviously the wrong index... cgGetError() returns CG_NO_ERROR after the call to cgGetParameterResourceIndex,

A thought I had was that maybe I'm looking at the wrong CGparameter? This is the parameter I've gotten at the 'effect' level - but obviously a texture can be bound to other shaders besides pixel shaders, and I suspect it can have a different resource index per unit. Is there a way to take the effect parameter and enumerate the other shader units? Or do I need to dive into the effect and get the fragment shader programs and look for the parameter that way?

A little hand would be appreciated, thanks!

Share this post

Link to post
Share on other sites
So, I changed my shader to have two samplers:

uniform sampler2D samp0;
uniform sampler2D samp1;

I took the CGeffect and got the fragment program and looped over the global parameters:

CGprogram fp = cgGetPassProgram(p, CG_FRAGMENT_DOMAIN);
CGparameter param = cgGetFirstParameter(fp, CG_GLOBAL);

and found the CG_SAMPLER2D parameters

However, calling cgGetParameterResourceIndex on both parameters returns 0, so that's not it...

Share this post

Link to post
Share on other sites
So, I tried letting the cg runtime set the texture parameters.

Why do these functions take ID3D11Resource* instead of ID3D11ShaderResourceView* ???

CGD3D11DLL_API void CGD3D11ENTRY cgD3D11SetTextureParameter(CGparameter Parameter, ID3D11Resource *pTexture);
CGD3D11DLL_API void CGD3D11ENTRY cgD3D11SetTextureSamplerStateParameter(CGparameter Parameter, ID3D11Resource *pTexture, ID3D11SamplerState *pSamplerState);

What a mistake. Although it DOES seem to correctly bind the texture...

It wouldn't be that bad, except every time I call one of these functions, [b]cg internally creates a ShaderResourceView and holds onto it[/b]. So when I exit my app after running it for a few seconds, there are tens of thousands of created SRVs. What the heck?

This happens regardless of whether cgD3D11SetManageTextureParameters is set to true or false

Share this post

Link to post
Share on other sites
I thought that maybe the shader resource indices were allocated in the order they're declared in the shader. That matches the order they are in the cg parameter list.

Turns out this isn't the case, after some experimenting (rearranging code, trial+error) the resource indices are allocated in the order the textures are sampled within the fragment shader code. That's unfortunate and means I'm still not any closer to finding the mapping ;)

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