Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I recently created a game (LD21) that uses a geometry shader to convert points into textured triangles/culling. Since I was under the impression that the support for #330 was widespread I only wrote #330 shaders, but it seems that a lot of not so old hardware only support #130 (according to GLView)

Now, since I'm only familiar with the #330 core functionality I am having trouble rewriting my shaders to #130. The fragment shader was quite trivial to rewrite, but I've only managed to get my vertex and geometry shader down to #150.

So, is it possible to rewrite the shaders, or would it require a lot of changes in my rendering engine?

Geometry shader

#version 150
layout(points) in;
layout(triangle_strip, max_vertices = 4) out;
uniform mat4 oMatrix;
in VertexData
{
    vec4 position;
    vec4 texcoord;
    vec4 size;
} vert;

out vec2 gTexCoord;

void main()
{
    if(vert.position.x>-4f && vert.position.x<4f && vert.position.y>-2f && vert.position.y<2f)
    {
        gTexCoord=vec2(vert.texcoord.z,vert.texcoord.y);
        gl_Position = vert.position + vec4(vert.size.x,vert.size.y,0,0);
        EmitVertex();

        gTexCoord=vec2(vert.texcoord.x,vert.texcoord.y);
        gl_Position = vert.position + vec4(0.0,vert.size.y,0,0);
        EmitVertex();

        gTexCoord=vec2(vert.texcoord.z,vert.texcoord.w);
        gl_Position = vert.position + vec4(vert.size.x,0.0,0,0);
        EmitVertex();

        gTexCoord=vec2(vert.texcoord.x,vert.texcoord.w);
        gl_Position = vert.position;
        EmitVertex();

        EndPrimitive();
    }
}

Vertex shader

#version 150
#extension GL_ARB_explicit_attrib_location : enable

layout (location = 0) in vec2 position;
layout (location = 1) in vec4 textureCoord;
layout (location = 2) in vec2 size;
uniform mat4 oMatrix;
uniform vec2 offset;
out VertexData
{
    vec4 position;
    vec4 texcoord;
    vec4 size;
} outData;

void main()
{
    outData.position = oMatrix * vec4(position.x+offset.x,position.y+offset.y,0,1);
    outData.texcoord = textureCoord;
    outData.size = oMatrix * vec4(size.x,size.y,0,0);
}
share|improve this question
    
Don't forget to inform yourself about the accept and up-vote features. –  Christian Rau Aug 26 '11 at 14:44
1  
"Don't forget to inform yourself about the accept and up-vote features." Read : "Accept my Answer." –  Jonathan Connell Aug 26 '11 at 14:49
    
So? It's rude not to accept answers. –  kmm Aug 26 '11 at 15:14
1  
@Jonathan Wasn't meant that way. It's not rude to not accept if the answer was not of help. Actually the OP has maybe accepted my answer too fast (sorry for that). But I often (even in my still short SO-life) I see one-time visitors ask a question and then completely forget about it (maybe they solved it 3 minutes later themselves or they mistook the site as a forum). But the reputation system is the driving force of this feedback oriented Q&A site. It wasn't meant to immediately accept my answer without thought, but as written, to be aware of the accept feature (as many newbies aren't). –  Christian Rau Aug 26 '11 at 15:57
1  
@Christian Rau This does indeed sometimes happen though it's probably best to give people the benefit of the doubt. If you do want to post a comment, "Welcome, please read the site rules" would be more appropriate IMO :) Oh and +1 for your answer ;) –  Jonathan Connell Aug 27 '11 at 8:29

1 Answer 1

up vote 5 down vote accepted

First of all, you won't be able to use structs for varying data (changed by 1.50), but you still can use in and out instead of attribute and varying. Additionally you have to write into the builtin variable gl_Position in the vertex shader (changed by 1.40). So in your vertex shader substitute the declaration of outData with something similar to

out vec4 texcoord;
out vec4 vSize;       //size already used

and use gl_Position instead of outData.position.

I'm not sure if the ARB_explicit_attrib_location extension is already core, so maybe you have to add

#extension ARB_explicit_attrib_location : require

to use the layout syntax or just set the attribute locations in the application by calling glBindAttribLocation before linking the shader, if this extension is not supprted.

In the geometry shader change the decralation of vert to something like

in vec4 texcoord[];      //names have to match vertex shader, of course
in vec4 vSize[];

Although you only get a single input (as you're using points), they still have to be arrays, I think (although always index with [0], when used). Change the use of vert.position to gl_PositionIn[0].

Next, I think the layout syntax for the geometry shader has been introduced in 1.50, so you might have to set the geometry shader layout in the application by means of glProgramParameteri.

I have to admit, that I have not much practical experience with geometry shaders or the modern syntax, as I still sit on 2.1 hardware, but all these changes can be deduced by looking into the respective specifications. Just look at the shader info logs if compilation (or linking) fails to gain some hints about what could be wrong.

The missing of the layout syntax in both the vertex and the geometry shader are the only things that might require some small additions to your application code, but I'm not completely sure if they are really unsupprted, so try them last.

Also, feel free to update your question if I missed anything and the shaders still don't work, maybe this time with some error messages from the GLSL compiler.

share|improve this answer
    
Thanks! Got it down to #140, but #130 is within my reach :) –  nilstastic Aug 26 '11 at 15:36
    
@nilstatic So what have you changed and what still doesn't work. Maybe you can edit your question to include the new 1.40 shaders. If you already got 1.40, maybe you just have to change the use of your custom position varyingto gl_Position`. –  Christian Rau Aug 26 '11 at 16:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.