Forward declaring functions

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…

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?

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

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 :smiley:

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.

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.

hmm… didn’t want to bother you, knackered. i will stand in the corner for the weekend :wink:
(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?)

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.

ah, i see. so i’ll try to be more serious from now on :wink:

Originally posted by knackered:
[b]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.[/b]
AFAIK GLSL will not typecast from a int to a float… I know that Nivida cards bark at that kind of coding.

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.

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?

I compile on first usage. It was the compile that crashed.
I can’t believe people haven’t experienced similar problems in arbitary shaders.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.