Question on writing a basic cg/HLSL shader

Hi everyone.

I’m trying to learn how to write shaders from Ben’s HLSL dvd 1 and the cg tutorial. I wrote a VERY simple shader to start and I have a question.

I wrote an “AplhaIn” float attribute to control the final Pixel program color output’s alpha. So that I could control the object transparency.

The shader works just fine, but when I move the alpha slider, nothing happens.
Any help is super welcomed :slight_smile:

here’s the shader:

// hello world shader.

/************************************
************* Tweakables ************
************************************/

float3 ColorIn : Diffuse
<
string UIName = “Color”;
string UIWidget = “Color”;
> = {0f, .5f, 1f};

float AlphaIn
<
string UIWidget = “slider”;
float UIMin = 0;
float UIMax = 1;
string UIName = “Alpha”;
> = .5;

/************************************
************* Matrices **************
************************************/

float4x4 WorldViewProjection : WorldViewProjection < string UIWidget = “None”; >;

/************************************
************** Structs **************
************************************/

struct maya
{
float3 position : POSITION;
};

struct v_out
{
float4 position : POSITION;
};

struct f_out
{
float4 color : COLOR;
};

/************************************
*********** Vertex Program **********
************************************/

v_out v_p(maya IN)
{
v_out OUT;

float4 pos = float4(IN.position, 1);
OUT.position = mul(WorldViewProjection, pos);

return OUT;

}

/************************************
********* Fragment Program **********
************************************/

f_out f_p() : COLOR
{
f_out result;

result.color = float4(ColorIn, AlphaIn);

return result;

}

/************************************
************ Technique **************
************************************/

technique ONE
{
pass one
{
CullFace = Back;
DepthTestEnable=true;
DepthFunc = LEqual;
VertexProgram = compile arbvp1 v_p();
FragmentProgram = compile arbfp1 f_p();
}
}

If you’re hoping that your alpha value will be used to make the object transparent, then you’ll need to define that in your render states. Alpha blending of the object with the other objects in the scene is something that happens in the video hardware after the pixel shader has been run. So in the pixel shader, the color and alpha values are computed, but how the alpha value is used is actually determined by the render states that you define inside the technique and pass at the bottom of the shader.

I think that the problem is that you haven’t defined those render states, so your shader is defaulting to alpha blending being off. To turn alpha blending on, try adding the following lines to your shader:

    AlphaBlendEnable = true; 
    SrcBlend = SrcAlpha; 
    DestBlend = InvSrcAlpha; 

The exact wording of these will be different depending on if you’re using CgFX or HLSL. The lines above are for HLSL. If you’re using Cg, they’d be like this:

    BlendEnable = true;
    BlendFunc = int2(SrcAlpha, OneMinusSrcAlpha);

Hi Mr. Cloward.

Thanks for helping me with my ubber noob question. The second code block worked perfectly(I’m using Cg).
I ended up finding the AlphaBlendEnable = true; on DVD2 ch1 shader, but then I noticed it wasn’t working on my Cg shader.

I thought HLSL and Cg were almost the same, but after looking the two code blocks above I realized I was wrong.
Is there any documentation out there that shows the HLSL to Cg equivalent?

Thanks for great DVD! Didn’t know that shaders could actually be fun to write :slight_smile:
Thanks for your help!

HLSL and Cg are almost the same - but the parts that are the same are in the pixel shader and vertex shader code. Outside the shaders in the FX framework, they differ quite a bit in syntax, but they’re still the same in functionality.

I haven’t done an extensive search, but I don’t know of a place to find differences between the two. When I was writing the CgFX exporter for ShaderFX, I just used the documentation on Nvidia’s web site to find the specs for Cg.

I just download all the documentation on Nvidia’s website. That helps a lot, but sometimes I still get lost.
I wish there was a good Cg IDE for Mac. That would help a lot too.
I’m going to install windows just so that I can use FX composer.
Thanks again for helping me.