HLSL rookie question

Started by
5 comments, last by Jason Z 11 years, 5 months ago
I am not very good at HLSL yet.

I have a constantbuffer that i am trying to sent an integer through.
i am 99% sure the int is going in to my HLSL pipeline but i cant get hold of the value.

this is my HLSL file:

How can i include my variable int test?

[source lang="cpp"]cbuffer ConstantBuffer
{
float4x4 final; // (4+4) * 16
float4 lightvec; // the light's vector (1) * 16
float4 lightcol; // the light's color (1) * 16
float4 ambientcol; // the ambient light's color (1) * 16
float4x4 worldMatrix; // (4+4) * 16
float4x4 viewMatrix; // (4+4) * 16
float4x4 projMatrix; // (4+4) * 16
int test;
}

Texture2D Texture;
SamplerState ss;

//DO I NEED TO DECLARATE TEST HERE?
struct VOut
{
float4 color : COLOR;
float2 texcoord : TEXCOORD; // texture coordinates
float4 position : SV_POSITION;
};


//DO I NEED TO PASS TEST HERE ALSO?
VOut VShader(float4 position : POSITION, float4 normal : NORMAL, float2 texcoord : TEXCOORD, float4 color: COLOR)
{
VOut output;

float4x4 worldViewMatrix = mul(worldMatrix, viewMatrix);
float3 positionVS = position + float3(worldViewMatrix._41, worldViewMatrix._42, worldViewMatrix._43);
output.position = mul(float4(positionVS, 1.0f), projMatrix);

output.position = mul(final, position);



// set the ambient light
output.color = ambientcol;
output.color.rgb = color.rgb;
// calculate the diffuse light and add it to the ambient light
float4 norm = normalize(normal);
float diffusebrightness = saturate(dot(norm, lightvec));
output.color += lightcol * diffusebrightness;
//be sure to take alpha last as other formulas might destroy this.
output.color.a = color.a;

output.texcoord = texcoord; // set the texture coordinates, unmodified



return output;
}

float4 PShader(float4 color : COLOR, float2 texcoord : TEXCOORD) : SV_TARGET
{
float4 resultcolor = color * Texture.Sample(ss, texcoord);

//THIS DOES NOT WORK: HERE IS MY PROBLEM.
//if (test==1)
resultcolor.a = 0.0f;

//if(resultcolor.r < 0.2f && resultcolor.g < 0.2f && resultcolor.b < 0.2f)
//resultcolor.a = 0.0f;


return resultcolor;





}
[/source]
Advertisement
You use your variable in Pixel Shader, not Vertex Shader, therefore you also need to call PSSetConstantBuffer(), not only VSSetConstantBuffer().
Constant buffers have to be aligned to 128 bit boundaries, so that may be the issue. Just for kicks, when you declare your test variable, declare it as a int4 and add the additional three integers to your structure that you are using on the CPU side. That may help you out...

Also can you be more specific about what the error is that you are getting? Is the rendered output incorrect, or do you get compilation errors, or something else?
I make the updateconstantbuffer call every frame.
The result is that the variable dont have any value, the rendering is correct and everything else works.

I have align_16 attribute to my constantbuffer struct so it automatic align my bytes.
Have you bound the constant buffer as vertex and pixel shader resource?

Cheers!
Yeah kauna, they are both bound.
It worked with a float3 and d3dxvector3 so i guess its just byte problem/alignment of some sort...

I make the updateconstantbuffer call every frame.
The result is that the variable dont have any value, the rendering is correct and everything else works.

I have align_16 attribute to my constantbuffer struct so it automatic align my bytes.


Have you checked in PIX to see if the constant buffer value is correct? If you look in PIX, and the value is incorrect, then it must be either that you aren't copying enough memory into the constant buffer, or you have an alignment issue (assuming that the buffers are being bound correctly - but you already mentioned that they are).

I would still recommend trying out the manual padding as outlined above, just to try it out. It won't take long to try, and it would clarify if the issue is alignment or not.

This topic is closed to new replies.

Advertisement