Unable to find texture param in shader

Started by
1 comment, last by pcmaster 4 years, 7 months ago

I'm unable to find my TerrainTypeSampler when I try to call it from my code! Why?


struct VertexToPixel
{
	float4 Position   	: POSITION;
	float4 Color		: COLOR0;
	float LightingFactor : TEXCOORD0;
	float2 TextureCoords: TEXCOORD1;
	//texture2D Texture : TEXTURE; //TODO: Figure out how to change the texture used for the current vertex.
};

struct PixelToFrame
{
	float4 Color : COLOR0;
};

float4x4 xView;
float4x4 xProjection;
float4x4 xWorld;
float3 xLightDirection;
float xAmbient;
bool xEnableLighting;
bool xShowNormals;

//------- Texture Samplers --------

texture2D xTexture;
SamplerState TextureSampler 
{ 
	magfilter = LINEAR; 
	minfilter = LINEAR; 
	mipfilter = LINEAR; 
	AddressU = mirror; 
	AddressV = mirror;
};

texture2D TerrainType;
SamplerState TerrainTypeSampler
{
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = mirror;
	AddressV = mirror;
};

texture2D Grass;
SamplerState GrassSampler
{
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = mirror;
	AddressV = mirror;
};

texture2D Rock;
SamplerState RockSampler
{
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = mirror;
	AddressV = mirror;
};

texture2D Sand;
SamplerState SandSampler
{
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = mirror;
	AddressV = mirror;
};

texture2D Snow;
SamplerState SnowSampler
{
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = mirror;
	AddressV = mirror;
};

texture2D Water;
SamplerState WaterSampler
{
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = mirror;
	AddressV = mirror;
};

//------- Technique: Textured --------

VertexToPixel TexturedVS(float4 inPos : POSITION, float3 inNormal : NORMAL, float2 inTexCoords : TEXCOORD0)
{
	VertexToPixel Output = (VertexToPixel)0;
	float4x4 preViewProjection = mul(xView, xProjection);
	float4x4 preWorldViewProjection = mul(xWorld, preViewProjection);

	Output.Position = mul(inPos, preWorldViewProjection);
	Output.TextureCoords = inTexCoords;

	float3 Normal = normalize(mul(normalize(inNormal), xWorld));
	Output.LightingFactor = 1;
	if (xEnableLighting)
		Output.LightingFactor = saturate(dot(Normal, -xLightDirection));

	return Output;
}

PixelToFrame TexturedPS(VertexToPixel PSIn)
{
	PixelToFrame Output = (PixelToFrame)0;
	float4 Color = TerrainType.Sample(TerrainTypeSampler, PSIn.TextureCoords);
	float4 GrassClr = float4(0, 255, 0, 255);
	float4 RockClr = float4(255, 0, 0, 255);
	float4 SandClr = float4(255, 255, 0, 255);
	float4 SnowClr = float4(255, 255, 255, 255);
	float4 WaterClr = float4(12, 0, 255, 255);

	float4 Diff = Color - GrassClr;
	if (!any(Diff))
		Output.Color = tex2D(GrassSampler, PSIn.TextureCoords);

	Diff = Color - RockClr;
	if (!any(Diff))
		Output.Color = tex2D(RockSampler, PSIn.TextureCoords);

	Diff = Color - SandClr;
	if(!any(Diff))
		Output.Color = tex2D(SandSampler, PSIn.TextureCoords);

	Diff = Color - SnowClr;
	if(!any(Diff))
		Output.Color = tex2D(SnowSampler, PSIn.TextureCoords);

	Diff = Color - WaterClr;
	if(!any(Diff))
		Output.Color = tex2D(WaterSampler, PSIn.TextureCoords);

	Output.Color = tex2D(TextureSampler, PSIn.TextureCoords);
	Output.Color.rgb *= saturate(PSIn.LightingFactor + xAmbient);

	return Output;
}

technique Textured_2_0
{
	pass Pass0
	{
		VertexShader = compile vs_4_0_level_9_1 TexturedVS();
		PixelShader = compile ps_4_0_level_9_1 TexturedPS();
	}
}

technique Textured
{
	pass Pass0
	{
		VertexShader = compile vs_4_0_level_9_1 TexturedVS();
		PixelShader = compile ps_4_0_level_9_1 TexturedPS();
	}
}

 

When I'm calling this, TerrainTypeSampler returns null! :(


        public void Draw()
        {
            Matrix WorldMatrix = Matrix.CreateTranslation(-m_TerrainWidth / 2.0f, 0, m_TerrainHeight / 2.0f);

            m_Effect.CurrentTechnique = m_Effect.Techniques["Textured"];
            m_Effect.Parameters["TerrainTypeSampler"].SetValue(m_TerrainType);
            m_Effect.Parameters["GrassSampler"].SetValue(m_Grass);
            m_Effect.Parameters["RockSampler"].SetValue(m_Rock);
            m_Effect.Parameters["SandSampler"].SetValue(m_Sand);
            m_Effect.Parameters["SnowSampler"].SetValue(m_Snow);
            m_Effect.Parameters["WaterSampler"].SetValue(m_Water);
            //m_Effect.Parameters["TextureSampler"].SetValue(m_Grass);
            m_Effect.Parameters["xView"].SetValue(m_CController.View/*m_ViewMatrix*/);
            m_Effect.Parameters["xProjection"].SetValue(m_CController.Projection/*m_ProjectionMatrix*/);
            m_Effect.Parameters["xWorld"].SetValue(WorldMatrix/*Matrix.Identity*/);

            RasterizerState RS = new RasterizerState();
            RS.CullMode = CullMode.None;
            RS.FillMode = FillMode.WireFrame;
            m_Device.RasterizerState = RS;

            m_Device.Clear(Color.CornflowerBlue);

            foreach (EffectPass Pass in m_Effect.CurrentTechnique.Passes)
            {
                Pass.Apply();

                m_Device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, m_Vertices, 0, m_Vertices.Length,
                    m_Indices, 0, m_Indices.Length / 3, VertexPositionNormalTexture.VertexDeclaration);
            }
        }

 

Please help!

Advertisement

Hi all, is it only me? I can't even copy out the text of your sources correctly in Windows 7 Firefox x64 into Notepad++. E.g. when I copy


            m_Effect.CurrentTechnique = m_Effect.Techniques["Textured"];

I actually get some weird characters, especially the "r" in "Textured" isn't ASCII 0x72 but the bytes 0x EF BB BF 72 instead, which looks like the UTF-8 BOM. Similarly for the "d" in the same word.

Furthermore, when I hit "Ctrl+F" and try to find all occurrences of "Textured" in this website, it doesn't find the one in the C++ code nor in the HLSL.

Anyway, do the other calls work correctly, if you comment out the query for TerrainTypeSampler, for example this one works fine?


            m_Effect.Parameters["GrassSampler"].SetValue(m_Grass);

My I suggest you save your all your sources are plain ASCII (not UTF-8) and try to recompile and re-run?

EDIT: Copying stopped yielding weird characters after refreshing the web, also my Firefox is fine again, WTF have I just experienced?

This topic is closed to new replies.

Advertisement