Jump to content
  • Advertisement
Sign in to follow this  
DarkRonin

Basic HLSL pixel shader question

This topic is 1068 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

Hi guys,

I have a basic pixel shader (as follows)
 
float4 PShader(float4 pos : POSITION) : SV_TARGET
{
	if (pos.z == 0.0f)
		return float4(0.5f, 0.5f, 0.5f, 1.0f);
	if (pos.z > 0.0f || pos.z < 0.0f)
		return float4(1.0f, 1.0f, 1.0f, 1.0f);

	return float4(1.0f, 0.0f, 0.0f, 1.0f);
}
I have flat 2D objects rendering in 3D space.

The intention here is to render any objects that have a z vertex position of 0.0f as mid grey and anything <> 0.0f as white. I understand the return value of red should never happen with the current code.

At this stage even though object 1 is at z=0.0f and object 2 is at z=10.0f (within the vertex buffer that is), both objects are being rendered mid grey.


This is all leading up to having multiple objects in one vertex buffer at different depths and have the shader determine the correct color depending on that depth.

Is what I am trying to achieve possible?

Thanks in advance. smile.png Edited by DarkRonin

Share this post


Link to post
Share on other sites
Advertisement
Hi Hodgman,

This is the shader in its entirely. I am still learning the basics of HLSL, so it is likely that there are some things that could be handled better smile.png

cbuffer PerObject : register(b0)
{
	matrix worldMatrix;
}

cbuffer PerFrame : register(b1)
{
	matrix viewMatrix;
}

cbuffer PerApplication : register(b2)
{
	matrix projectionMatrix;
}

cbuffer cbColor : register(b3)
{
	float4 color;
}

struct VS_Input
{
	float4 pos  : POSITION;
};

struct VS_Output
{
	float4 pos  : SV_POSITION;
};

VS_Output VShader(VS_Input IN)
{
	VS_Output OUT = IN;

	OUT.pos = mul(IN.pos, worldMatrix);
	OUT.pos = mul(OUT.pos, viewMatrix);
	OUT.pos = mul(OUT.pos, projectionMatrix);

	return OUT;
}

float4 PShader(float4 pos : POSITION) : SV_TARGET
{
	//if (pos.z > -0.1f && pos.z < 0.1f)
	if (pos.z == 0.0f)
		return float4(0.5f, 0.5f, 0.5f, 1.0f);
	if (pos.z > 0.0f || pos.z < 0.0f)
		return float4(1.0f, 1.0f, 1.0f, 1.0f);

	return float4(1.0f, 0.0f, 0.0f, 1.0f);
}

Share this post


Link to post
Share on other sites
Thanks for the advice. smile.png

Although the Vertex Shader compilation is failing at the moment.

Seems to be at this line

	float4 vert_pos : POSITION; // let's make a 2nd variable to pass the vertex position data in

Share this post


Link to post
Share on other sites
Ok, I think I solved the vertex side of the shader. VS_Input is a different sized struct to VS_Output so it couldn't be directly copied like that.

So I chaged it to this

VS_Output VShader(VS_Input IN)
{
	VS_Output OUT;
	OUT.pos	= IN.pos;

	OUT.pos = mul(IN.pos, worldMatrix);
	OUT.pos = mul(OUT.pos, viewMatrix);
	OUT.pos = mul(OUT.pos, projectionMatrix);

	OUT.vert_pos = IN.pos;//send through the vertex position unchanged

	return OUT;
}
Now I am just trying to work out how to pass OUT.vert_pos to the Pixel Shader.

Share this post


Link to post
Share on other sites

You can use TEXCOORDx for passing stuff through shader stages ^^

Just add the definition to your input layout for the pshader :)

Share this post


Link to post
Share on other sites

Just set the pixel shader to take a VS_Output struct as an input parameter.  In other words:

float4 PShader(VS_Output psInput) : SV_TARGET
{
	//... whatever you want to do here
}

You don't need to change the POSITION semantic for the vert_pos attribute.  You can access it by using

psInput.vert_pos

This combined with Hodgman's post about SV_POSITION should get you everything you need.

 

Edit:  changed vert_pos name to match previous examples in thread.

Edited by WFP

Share this post


Link to post
Share on other sites
@WFP - Man, I was sure I tried this last night and it wouldn't compile.

Tried again, first thing this morning and it is working perfectly. Goes to show what a tried mind will do.

Thanks again Hodgman and WFP.


I'll add the entire working shader for future reference (and to help anyone else who has a similar query)

cbuffer PerObject : register(b0)
{
	matrix worldMatrix;
}

cbuffer PerFrame : register(b1)
{
	matrix viewMatrix;
}

cbuffer PerApplication : register(b2)
{
	matrix projectionMatrix;
}

cbuffer cbColor : register(b3)
{
	float4 color;
}

struct VS_Input
{
	float4 pos  : POSITION;
};

struct VS_Output
{
	float4 pos  : SV_POSITION; // SV_POSITION is the magic "system variable" used by the rasterizer
	float4 vert_pos : POSITION; // let's make a 2nd variable to pass the vertex position data in
};

VS_Output VShader(VS_Input IN)
{
	VS_Output OUT;
	OUT.pos	= IN.pos;

	OUT.pos = mul(IN.pos, worldMatrix);
	OUT.pos = mul(OUT.pos, viewMatrix);
	OUT.pos = mul(OUT.pos, projectionMatrix);

	OUT.vert_pos = IN.pos;//send through the vertex position unchanged

	return OUT;
}

float4 PShader(VS_Output psInput) : SV_TARGET
{
	if (psInput.vert_pos.z == 0.0f)
		return float4(0.0f, 0.0f, 0.0f, 1.0f);
	if (psInput.vert_pos.z > 0.0f || psInput.vert_pos.z < 0.0f)
		return float4(1.0f, 1.0f, 1.0f, 1.0f);

	// Below line should never be reached
	return float4(1.0f, 0.0f, 0.0f, 1.0f);
}

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!