PDA

View Full Version : Cg Shader Programming




Sin
Jul 24, 2007, 04:58 AM
I'm just starting to dabble in graphics programming on the Mac (having been doing it on other platforms for quite a while, I might add...) - I've run into a bit of a problem.

As soon as I put a discard statement into the fragment shader, everything disappears. If I replace it with a more benign operation, such as altering the pixel colour, everything is fine - the pixels I'd expect to be culled change and everything else looks normal. With the discard statement in the shader, nothing is visible at all.

There are no errors being reported, and compiling the shader by hand outputs a correct looking listing (and it's identical to that generated under Windows).

This is on a shiny new MBP with the 8600 in it. AFAIK I can't update video drivers on a Mac, so I was hoping the ones that ship would be reasonably stable, at least as far as basic shaders - so hopefully I'm just missing something obvious.

Here's a very simple shader that doesn't work - all I feed it is a single triangle with pure red, green, and blue at the 3 vertices... with discard in, it won't display anything, but substituting with a change of colour shows what I'd expect (an inscribed circle).

struct input
{
float4 position : POSITION;
float4 color : COLOR0;
};


float4 main(input IN) : COLOR
{
float m = IN.color.x * IN.color.x + IN.color.y * IN.color.y +
IN.color.z * IN.color.z;

if(m > 0.5)
{
// m = 0; // This line works!
discard; // This line doesn't!
}

return float4(m,m,m,1);
}

Any ideas?



gnasher729
Jul 24, 2007, 06:07 AM
I thought that is what discard is supposed to do: It won't draw that pixel.

Sin
Jul 24, 2007, 06:27 AM
I thought that is what discard is supposed to do: It won't draw that pixel.

It's not drawing *any* pixels, not just the ones that match the condition in the code....

lazydog
Jul 24, 2007, 06:42 AM
Maybe this is of no help but, have you tried Software Update? I seem to remember reading that there was a recent update to the new MacBook Pros which included new nvidia drivers.

b e n

Sin
Jul 24, 2007, 03:46 PM
Maybe this is of no help but, have you tried Software Update? I seem to remember reading that there was a recent update to the new MacBook Pros which included new nvidia drivers.

b e n

Hmm.. I think it's a driver issue, but there aren't any new updates so I guess I'm stuck with it.

I ported the code over to GLSL instead of Cg and it's doing the right thing. The strange thing is that it seems to generate the same instructions as the Cg, but some part of the driver is obviously unhappy with the Cg code.

Oh well, I'll have to go back to my PC for now and shelve the Mac stuff for a bit.