Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should modern OpenGL shaders be written so as to be compatible with each other?

In the fancy new versions of OpenGL (3.0 and 4.0 up), built-in vertex attributes like gl_Vertex are being deprecated. The "new way" to actually render anything is to specify your own vertex attributes for position, color, etc., and then bind these custom attributes to buffers.

My question is this: how can one do this without closely coupling the rendering code and the shader? If I write a shader that uses "position" as the vertex position, the host code using the shader has to know that and pass vertex data as "position". If I want to use a different shader that was written to take vertex data in "vertex_pos", I have to either rewrite that shader first, or modify my host code to send vertex data as "vertex_pos" instead.

Is there a set of best-practice names for standard vertex and fragment attributes that all shaders should be using? Or are there Balkanized engine-specific standards, such that a shader written for one engine cannot work on another without modification? Or are there no standards at all, such that, in general, every object needs its own custom rendering code to match its custom shader?

like image 323
interfect Avatar asked Jan 12 '11 05:01

interfect


People also ask

How do shaders work in OpenGL?

A Shader is a user-defined program designed to run on some stage of a graphics processor. Shaders provide the code for certain programmable stages of the rendering pipeline. They can also be used in a slightly more limited form for general, on-GPU computation.

What is considered modern OpenGL?

In its modern form, OpenGL is a cross-platform library for interfacing with programmable GPUs for the purpose of rendering real-time 3d graphics. Its use is common in games, CAD, and data visualization applications.

Can you use multiple shaders OpenGL?

You can have as many shader objects (shaders loaded into memory and compiled) as you want; only one can be bound (active) at a time. In terms of implementation, in every frame I use glUseProgram(1) and glUseProgram(2) to change shaders?

What is the best shading language?

High-Level Shading Language (HLSL) is arguably the best language for writing shaders, as it's very well supported, separates samplers and textures (unlike GLSL), and has common language features like: namespaces. template generics.


2 Answers

Just keep calling them the old names. If you've got a core profile (i.e. no backwards compatibility) the reserved names of older GLSL specs are freed up declared as unavilable; redeclaring them for binding vertex attributes. Seems to change their availability attribute. In the compatibility profile those variable names are preallocated and binded.

So it boils down to this: Keeping the old naming in the shaders is a convenience and seems to work with the current GLSL compilers. If you want to go safe use the preprocessor to rewrite the gl_ prefix reserved names into an self choosen prefix and bind that one.

like image 150
datenwolf Avatar answered Sep 28 '22 16:09

datenwolf


First, to answer your question. I am unaware of any such standard naming convention.

But... it is more complicated than attribute names. Hidden under the question is the notion of attribute semantics. Each of the input attributes have some form of semantics that each shader expects.

And what I learned from the fixed gl_ names is that they under-specify their semantics.

  • Which space is gl_Position in ? (answer: it completely depends on what the host passed in. The engine does not have to pass in something that is local-space, e.g. if it transformed it already as it required world space for different reasons).
  • is gl_TexCoord1 a texture coordinate, or really, a tangent ? what about its range ? Is it encoded ?

It's not clear that a specific nomenclature could be found that really addresses all those issues, but it would be required to make various engines compatible.

More problematic, it's not even obvious that a specific engine (or specific asset) would have the capability to provide specific attributes required by a shader coming from a different engine. What then ?

Those are all reasons why we end up with balkanized shader environments.

like image 41
Bahbar Avatar answered Sep 28 '22 17:09

Bahbar