Jump to content

  • Log In with Google      Sign In   
  • Create Account


HLSL rookie question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 KurtO   Members   -  Reputation: 210

Like
0Likes
Like

Posted 14 November 2012 - 04:30 AM

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]

Sponsor:

#2 Zaoshi Kaba   Crossbones+   -  Reputation: 3601

Like
0Likes
Like

Posted 14 November 2012 - 06:47 AM

You use your variable in Pixel Shader, not Vertex Shader, therefore you also need to call PSSetConstantBuffer(), not only VSSetConstantBuffer().

#3 Jason Z   Crossbones+   -  Reputation: 4672

Like
0Likes
Like

Posted 14 November 2012 - 05:00 PM

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?

#4 KurtO   Members   -  Reputation: 210

Like
0Likes
Like

Posted 15 November 2012 - 02:22 AM

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.

#5 kauna   Crossbones+   -  Reputation: 2155

Like
0Likes
Like

Posted 15 November 2012 - 07:12 AM

Have you bound the constant buffer as vertex and pixel shader resource?

Cheers!

#6 KurtO   Members   -  Reputation: 210

Like
0Likes
Like

Posted 15 November 2012 - 12:06 PM

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


#7 Jason Z   Crossbones+   -  Reputation: 4672

Like
0Likes
Like

Posted 15 November 2012 - 07:44 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS