Jump to content
  • Advertisement
Sign in to follow this  
c_olin

OpenGL Avoiding fixed functionality in GLSL

This topic is 2779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been moving away from all of OpenGL's fixed functionality (matrix transformations, vertex attributes in GLSL). Now for all of the inputs to my shaders (uniforms and vertex attributes) I am using my own variables and none of the built-ins. My question is about the outputs from the vertex and fragments shaders; more specifically gl_Position, gl_FragData, and gl_FragColor. Are these necessary to use? It feels awkward that these are the only built-in variables that I am using and I can't find any information on ouputing frag or vertex position data through non-built in variables.

Thanks

Share this post


Link to post
Share on other sites
Advertisement
You can get rid of gl_FragData and gl_FragColor by utilizing the function glBindFragDataLocation, I'll leave it to you to lookup how to use it.

gl_Position is still required to use, because it ties into the rasterization pipeline of opengl, which is still fixed function. OpenGL needs to know the vertex position for every vertex, so there is a special variable to define that.

Share this post


Link to post
Share on other sites
Or you can just compile and link your shaders and then afterwards ask for the location using.

glGetFragDataLocation()


Disadvantage is that you need to keep track of this info when you create VAOs etc, you can not just pick a default.

Advantage is that it is more flexible..you are not tied to a default attribute/location definitions.

Share this post


Link to post
Share on other sites

Since this topic is here I may ask as well: when was glBindFragDataLocation introduced? I could find the reference for it but not when it was introduced.


Looks like it was created in GL_EXT_gpu_shader4

http://www.opengl.org/registry/specs/EXT/gpu_shader4.txt

Share this post


Link to post
Share on other sites
Note that you don't have to use glGetFragDataLocation at all, but can just use a layout qualifier in the shader to set the output to whatever color attachment you need, e.g.:



layout(location = 0) out vec4 g_buffer_1; // renders to GL_COLOR_ATTACHMENT0
layout(location = 1) out vec4 g_buffer_2; // renders to GL_COLOR_ATTACHMENT1
// ...


Personally, I find binding the right thing to the right place works just fine. I suppose for more complex applications than mine, it might be useful to get the location programatically, but you still have to know the name of the variable in the shader as opposed to the number, so it's the same amount of coupling between C++ (or whatever) and shader code.

Share this post


Link to post
Share on other sites

Note that you don't have to use glGetFragDataLocation at all, but can just use a layout qualifier in the shader to set the output to whatever color attachment you need, e.g.:



layout(location = 0) out vec4 g_buffer_1; // renders to GL_COLOR_ATTACHMENT0
layout(location = 1) out vec4 g_buffer_2; // renders to GL_COLOR_ATTACHMENT1
// ...



This works too, but this is a much newer extension than bindAttribLocation, and I'm not sure if all cards support this. I think you might need glsl version 330 or something to use this.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!