Sign in to follow this  
Dom_152

[XNA] Texture stages

Recommended Posts

Dom_152    476
How do I sample from a texture set via: GraphicsDevice.Textures[n] = Blah; At the moment I have something like this:

waterEffect.Begin();
            waterEffect.CurrentTechnique.Passes[0].Begin();         

            // Prepare textures
            theGraphicsDevice.Textures[1] = reflectionMapTarget.GetTexture();


            DrawModel(theGraphicsDevice, theContent.WaterPlaneModel);

            waterEffect.CurrentTechnique.Passes[0].End();
            waterEffect.End();

DrawModel() sets Textures[0] to the models diffuse map. The HLSL looks like this:
sampler DiffuseTexSampler : TEXUNIT0 = sampler_state
{
	Texture = <DiffuseMap>;
	MipFilter = LINEAR;
	MagFilter = LINEAR;
	MinFilter = LINEAR;
};

sampler ReflectionMapSampler : TEXUNIT1 = sampler_state
{
	Texture = <ReflectionMap>;		
	MipFilter = LINEAR;
	MagFilter = LINEAR;
	MinFilter = LINEAR;
};


float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
	//float4 diffuseColour = (tex2D(Tex0Sampler, input.TexCoords) * (AmbientLightColour * AmbientLightIntensity)) * float4(1.0f, 1.0f, 1.0f, 1.0f);
	float4 projectionColour = tex2Dproj(ReflectionMapSampler, input.ProjTexCoords);
	
	return projectionColour * float4(0.1f, 0.9f, 1.0f, 0.9f);
}
But rather than sampling from the reflection map this makes it sample from the diffuse map for some reason. What am I doing wrong?

Share this post


Link to post
Share on other sites
MJP    19788
Typically with effects you do something like this:

texture2D DiffuseMap;
sampler DiffuseTexSampler = sampler_state
{
Texture = <DiffuseMap>;
MipFilter = LINEAR;
MagFilter = LINEAR;
MinFilter = LINEAR;
};


And then in your app code you get an EffectParameter for "DiffuseMap" and call SetValue to bind a specific texture to it.

If you want to explicitly bind samplers to sampler registers so that you can set textures to a certain index, you use this syntax:

sampler DiffuseTexSampler : register(s0) = sampler_state
{
MipFilter = LINEAR;
MagFilter = LINEAR;
MinFilter = LINEAR;
};


Share this post


Link to post
Share on other sites
Dom_152    476
It seems more convenient to grab textures from those registers with my current setup. Thanks for the help, works perfectly. Out of interest what does TEXUNITn refer to?

Share this post


Link to post
Share on other sites
MJP    19788
That's what's called a semantic. Semantics are basically like attributes in C#, if you know how those work. You add them to variables in functions to add extra metadata. Some have system-interpreted values...for instance you add TEXCOORD0 to a variable being passed to a shader in order to indicate that you want it to be bound to a texture coordinate register. Those are all listed here, and they all are for variables passed to or from shader stages.

For global variables you can still add semantics, but they have no inherent meaning. For instance you could add the semantic "LIGHTPOS" to certain variables to indicate that they're a light position.

The most common use of such semantics is by shader authoring tools (like RenderMonkey, FX Composer) or 3D content authoring tools (3dsmax, Maya) when they load user-supplied shaders. The application will list a set of semantics that it supports, and it use them to decorate your variables so that the app knows how to setup your shader for you. So it might have LIGHTPOS0 - LIGHTPOS7, and you would attach LIGHTPOS0 to have the app bind the position of the first light in the scene. I'm guessing that TEXUNIT came from something like this.

In HLSL you can also add attributes to variables/functions/passes/techniques, and they're like semantics but they go even further. You can use them to specify multiple string, integer, or floating point values. Those tools I mentioned before will commonly recognize SAS attributes that let you specify UI controls for tweaking values in a shader. So for instance if you had a color parameter, you could specify that the app should provide a slider with minimum values of 0 and max values of 1.

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