Jump to content
  • Advertisement
Sign in to follow this  
reccles

DirectX - HLSL - Texture assignment

This topic is 5132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a problem with assigning textures via HLSL. The old fashioned way of assigning textures is:

m_pDevice->SetTexture(0, m_pBlahTexture1);
m_pDevice->SetTexture(1, m_pBlahTexture2);

All of the docs on the new shader stuff suggest assigning textures like:
m_pEffect->SetTexture("g_blahVar", m_pBlahTexture1);
m_pEffect->SetTexture(blahHandle, m_pBlahTexture2);

Note that these are not explicitly set to texture units 0 and 1. In the shader you access the variables via defining variables with matching names. (i.e. texture g_blahVar; would be defined in the .fx file.)
My Question:
Is it possible to reference the texture units directly from within the shader. My goal is to reduce the amount of coupling to the shader interface. It would be nice if the shader could "just work" with existing SetTexture calls instead of using the effect interface.

Share this post


Link to post
Share on other sites
Advertisement
Not sure if this helps or not, but if you just use a SAMPLER in the HLSL script but you don't set it to use a particular TEXTURE from within that script then when you use tex2D(sampler, coords) it will just sample it from the texture that the device is currently set to. I use the method all the time when I need to use HLSL on models where there are multiple textures. I haven't made any attempt at multiple texture layers though, so I'm not sure how you would set your second pBlahTexture2 to interface in the HLSL.

Share this post


Link to post
Share on other sites
as i understand it, no you cant. Unlike normal texturing, the HLSL compiler will actually decide which register texture/sampler objects are assigned to depending upon texture access patterns in the shader. If you just use normal sampler objects in a shader then you can get a handle to a shader constant (D3DXHANDLE) and a description of the constant (D3DXCONSTANT_DESC) then get the value of the RegisterIndex member of the description var. You can then use this value in a normal IDirect3DDevice9::SetTexture call

Share this post


Link to post
Share on other sites
Quote:
Original post by rjackets
Not sure if this helps or not, but if you just use a SAMPLER in the HLSL script but you don't set it to use a particular TEXTURE from within that script then when you use tex2D(sampler, coords) it will just sample it from the texture that the device is currently set to. I use the method all the time when I need to use HLSL on models where there are multiple textures. I haven't made any attempt at multiple texture layers though, so I'm not sure how you would set your second pBlahTexture2 to interface in the HLSL.

to use multiple textures just have multiple sampler objects and get a constant description for each one then retrieve the RegisterIndex value as my previous post. Then call SetTexture with the register index value for the right texture.

Share this post


Link to post
Share on other sites
Hmmmmm Ok. Well thank you for your responses. It sounds like there is no graceful solution.

Any idea how bad the call to GetTexture is? I suppose i could always get the texture from the first texture unit and then re-assign it to the effect shader. I'm sure this incurrs some sort of overhead. Oh well, speed over pretty code i guess.

Share this post


Link to post
Share on other sites
if you look at some of the ATI RenderMonkey samples they explicitly set the registers when declaring certain things... could this perhaps help?

Share this post


Link to post
Share on other sites
The ID3DXEffect interface has a method called SetStateManager that looks like this.

HRESULT SetStateManager(
LPD3DXEFFECTSTATEMANAGER pManager
);

ID3DXEffectStateManager is an abstract class you can inherit from to manage state changes, presumably to avoid uneccesary ones. You could implement this interface, call SetStateManager with an instance of your inherited class and your class would keep track of the various calls to ID3DDevice::SetTexture and take note of which stages are set to which textures.

I might have misunderstood what you wanted to do, but it might be worth taking a look at. More information can be found in the StateManager sample or at MSDN.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!