Avoiding fixed functionality in GLSL

Started by
5 comments, last by karwosts 13 years ago
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
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.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
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.
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
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.

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
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
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.

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.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

This topic is closed to new replies.

Advertisement