Jump to content
  • Advertisement
MatsK

3D What's wrong with this shader?

Recommended Posts

Shader source

This compiles just fine, but when I try to load it in MonoGame, SharpDX gives me a cryptic "Invalid Parameter" error. :(

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)
5 hours ago, MatsK said:

Shader source

This compiles just fine, but when I try to load it in MonoGame, SharpDX gives me a cryptic "Invalid Parameter" error. :(

My guess is something to do with passing a texture sampler as a param in the shader

Edited by Steve_Segreto
Mistake

Share this post


Link to post
Share on other sites
Posted (edited)
9 hours ago, Steve_Segreto said:

passing a texture sampler

That is what I'm looking at as well. This doc.microsoft talks about shader model changes. sampler_state{Texture= is a d3d9 keyword only. Surely not sm4.0

 

is a similar discussion I believe.

Edited by MarkK.
oh vvv

Share this post


Link to post
Share on other sites
Posted (edited)
58 minutes ago, MarkK. said:

sampler_state{Texture= is a d3d9 keyword only. Surely not sm4.0

Wait, what?

Ok so how would that be in SM4.0?

Here's the new code:

texture DisplacementMap;
/*sampler DisplacementSampler = sampler_state
{
	Texture = <DisplacementMap>;
	MipFilter = Point;
	MinFilter = Point;
	MagFilter = Point;
	AddressU = Clamp;
	AddressV = Clamp;
};*/

SamplerState DisplacementSampler
{
	Texture = <DisplacementMap>;
	MipFilter = Point;
	MinFilter = Point;
	MagFilter = Point;
	AddressU = Clamp;
	AddressV = Clamp;
};

struct VS_INPUT 
{
	float4 position	: POSITION;
	float4 uv : TEXCOORD0;
};
struct VS_OUTPUT
{
	float4 uv : TEXCOORD0;
	float4 worldPos : TEXCOORD1;
	float4 textureWeights : TEXCOORD2;
	float4 position  : POSITION;
};

float4x4 world;
float4x4 view;
float4x4 proj;

float maxHeight = 128;

float textureSize = 512.0f;
float texelSize = 1.0f / 512.0f; //size of one texel;

float4 tex2Dlod_bilinear(/*sampler texSam,*/ float4 uv)
{
	float4 height00 = tex2Dlod(DisplacementSampler, uv);
	float4 height10 = tex2Dlod(DisplacementSampler, uv + float4(texelSize, 0, 0, 0));
	float4 height01 = tex2Dlod(DisplacementSampler, uv + float4(0, texelSize, 0, 0));
	float4 height11 = tex2Dlod(DisplacementSampler, uv + float4(texelSize, texelSize, 0, 0));

	float2 f = frac(uv.xy * textureSize);

	float4 tA = lerp(height00, height10, f.x);
	float4 tB = lerp(height01, height11, f.x);

	return lerp(tA, tB, f.y);
}

VS_OUTPUT Transform(VS_INPUT In)
{
	VS_OUTPUT Out = (VS_OUTPUT)0;
	float4x4 viewProj = mul(view, proj);
	float4x4 worldViewProj = mul(world, viewProj);

	float height = tex2Dlod_bilinear(/*DisplacementSampler,*/ float4(In.uv.xy, 0, 0)).r;

	In.position.y = height * maxHeight;
	Out.worldPos = mul(In.position, world);
	Out.position = mul(In.position, worldViewProj);
	Out.uv = In.uv;
	float4 TexWeights = 0;

	TexWeights.x = saturate(1.0f - abs(height - 0) / 0.2f);
	TexWeights.y = saturate(1.0f - abs(height - 0.3) / 0.25f);
	TexWeights.z = saturate(1.0f - abs(height - 0.6) / 0.25f);
	TexWeights.w = saturate(1.0f - abs(height - 0.9) / 0.25f);
	float totalWeight = TexWeights.x + TexWeights.y + TexWeights.z + TexWeights.w;
	TexWeights /= totalWeight;
	Out.textureWeights = TexWeights;

	return Out;
}

technique GridDraw
{
	pass P0
	{
		VertexShader = compile vs_4_0 Transform();
	}
}

 

Edited by MatsK

Share this post


Link to post
Share on other sites

I'm not saying the previously mentioned is your problem, but clearly you are missing the suggestion that the keyword -Texture in the sampler_state block will probably not be valid in shader model 4 as you define in your technique pass.

reference: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-sample

If the api you are using is d3d9, try vs_4_0_level_9_1 perhaps. This is what my declaration looks like (monogame)

texture ModelTexture;
sampler2D TextureSampler = sampler_state 
{ 
	Texture   = (ModelTexture);
	MagFilter = Linear;
	MinFilter = Linear;
	AddressU  = Clamp;
	AddressV  = Clamp;	
};

 

Share this post


Link to post
Share on other sites
12 minutes ago, MarkK. said:

I'm not saying the previously mentioned is your problem, but clearly you are missing the suggestion that the keyword Texture in the sampler_state block will probably not be valid in shader model 4 as you define in your technique pass. 

Thanks! How do I pass the texture to the sampler?

 

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

  • 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!