c++directxhlslshaderdirect3d10

D3D10 HLSL: How do I bind a texture to a global Texture2D via reflection?


Ok so assuming I have, somewhere in my shader, a statement as follows (Note I am enabling legacy support to share shaders between DX9 and DX10):

Texture2D   DiffuseMap;

I can compile up the shader with no problems but I'm at a slight loss as to how I bind a ShaderResourceView to "DiffuseMap". When I "Reflect" the shader I rather assumed it would turn up amongst the variable in a constant buffer. It doesn't. In fact I can't seem to identify it anywhere. So how do I know what texture "stage" (to use the DX9 term) that I should bind the ShaderResourceView too?

Edit: I've discovered I can identify the sampler name by using "GetResourceBindingDesc". I'm not sure that helps me at all though :(

Edit2: Equally I hadn't noticed that its the same under DX9 as well ... ie I can only get the sampler.

Edit3: My Texture2D and Sampler look like this:

Texture2D DiffuseMap  : DiffuseTexture;
sampler   DiffuseSampler  = sampler_state
{
    Texture   = (DiffuseMap);
    MinFilter = Linear;
    MaxFilter = Linear;
};

Now in the effect frame work I could get the Texture2D by the semantic "DiffuseTexture". I could then set a ResourceView(D3D10)/Texture(D3D9) to the Texture2D. Alas there doesn't seem to be any way to handle "semantics" using bog standard shaders (It'd be great to know how D3D does it but studying the D3D11 effect framework has got me nowhere thus far. It seems to be reading it out of the binary, ie compiled, data and I can only see "DiffuseSampler" in there myself).


Solution

  • Hmm let me rephrase that. On the C++ side you have a bunch of loaded textures and associated SRVs. Now you want to set a shader (that comes from DX9) and without looking at how the shader is written, bind SRVs (diffuse to diffuse slot, specular, normal maps—you name it). Right?

    Then I think as you said that you best bet is using GetResourceBindingDesc:

    HRESULT GetResourceBindingDesc(
      [in]  UINT ResourceIndex,
      [in]  D3D10_SHADER_INPUT_BIND_DESC *pDesc
    );
    

    I think you have to iterate each ResourceIndex (starting at 0, then 1, 2 etc), if HRESULT is S_OK then you have pDesc.BindPoint that will represent the position of associated SRV in ppShaderResourceViews array of a *SetShaderResources call. And pDesc.Name (like "DiffuseMap") gives you the association information.