PDA

View Full Version : forward declaring functions



knackered
01-18-2006, 06:21 AM
Why isn't it possible to forward-declare a function within the same source file as the function body itself.
Example:

vec3 getNormal();

void main(void)
{
vec3 normal = getNormal();
}

vec3 getNormal()
{
return gl_Normal;
}

I get "getNormal is already defined..." errors.
It works fine if the getNormal function body is in another shader attachment...

RigidBody
01-19-2006, 03:11 AM
forward declaration is an instrument which makes one-pass compilation possible. maybe your compiler works with 2 passes and therefore does not need- maybe does not even understand- a forward declaration, but interprets a forward declared function to be external.

does it work without forward declaration and the getNormal function body at the end of the source?

John Kessenich
01-19-2006, 12:39 PM
OpenGL 2.0 says that forward declaring a function, like you did in your example, works. You can give the prototype, then call it, then provide the body. This feature is intentionally provided, and is part of the core OpenGL 2.0 specification.

JohnK

RigidBody
01-19-2006, 01:50 PM
ladies and gentlemen, tonight it is knackered vs. the OpenGl2.0 specification

seriously, a spec says how something should work. knackered says it doesn't. so what's wrong? wrong hardware, driver, compiler...? or maybe the wrong OS? i'm getting out of this discussion right now, since i've never created even the smallest possible shader program :D

knackered
01-20-2006, 01:18 AM
Right, so it's in the spec. Ok, I'll report it to nvidia....again.
The other day, in a shader I was writing, "vec3 v(1,1,1);" made the GLSL compiler crash! Changing it to "vec3 v(1.0, 1.0, 1.0);" stopped it crashing. A compiler error or warning would be fair enough, but crashing the application is bad. This is just another in a long line of weirdness with the nVidia GLSL implementation.

RigidBody, have a word with yourself will you.

Relic
01-20-2006, 03:34 AM
The given example doesn't write gl_Position or uses normal. So what would a reproducing shader source be?
Seems to work for me just fine.

RigidBody
01-20-2006, 08:57 AM
hmm... didn't want to bother you, knackered. i will stand in the corner for the weekend ;)
(by the way, i'm not quite sure about "have a word with yourself", didn't find it it in my dictionairy. i even asked a native speaker, but she couldn't translate it. i assume it means that you want me to punish myself for being naughty?)

knackered
01-21-2006, 09:37 AM
It's a south-london slang term, it means "act like a third-party in the situation, and advise yourself that you're behaving inappropriately". Not necessarily that you're naughty, just that you're not making much sense. It's often said to people as a more friendly alternative to "shut up, ars*hole".
So I meant to be friendly.

RigidBody
01-22-2006, 09:30 AM
ah, i see. so i'll try to be more serious from now on ;)

Mars_999
01-30-2006, 11:55 AM
Originally posted by knackered:
Right, so it's in the spec. Ok, I'll report it to nvidia....again.
The other day, in a shader I was writing, "vec3 v(1,1,1);" made the GLSL compiler crash! Changing it to "vec3 v(1.0, 1.0, 1.0);" stopped it crashing. A compiler error or warning would be fair enough, but crashing the application is bad. This is just another in a long line of weirdness with the nVidia GLSL implementation.

RigidBody, have a word with yourself will you.AFAIK GLSL will not typecast from a int to a float... I know that Nivida cards bark at that kind of coding.

knackered
01-31-2006, 01:36 AM
I don't mind barking, I think it's great - but crashing the application is not acceptable.
The thing is, lots of these weird things happen in more complicated shaders, not in simple test cases.

Zulfiqar Malik
02-01-2006, 12:08 AM
vec3 v(1, 1, 1) seems to work fine on my GLSL compiler (6800GT & 78.01). I have tried such syntax with older driver versions and older hardware (FX5700 ultra). ATI on the other hand did not compile the shader properly and in accordance with opengl specs.
As for crashing, are you sure that the compile crashes your application or the usage of the shader later on in the application?

knackered
02-01-2006, 01:02 AM
I compile on first usage. It was the compile that crashed.
I can't believe people haven't experienced similar problems in arbitary shaders.