多くの記事では、固定バインディング ポイントを指定してアトミック カウンターを使用する方法について説明しています。
//Shader:
layout(binding = 0, offset = 0) uniform atomic_uint myAtomicCounter;
//App code
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, myBufferHandle);
ここでは、ハードコーディングされたバインディング ポイントbinding = 0
がシェーダー コードとアプリケーション コードの両方で指定されています。これらの記事はこのようにしていると思います。
アトミック カウンターには場所が割り当てられていないため、
Uniform*
コマンドを使用して変更することはできません。プログラム オブジェクトのアトミック カウンターに属するバインディング、オフセット、およびストライドは無効になり、再リンクが成功するたびに新しいカウンターが割り当てられます。[shader_atomic_counters]
The above is fine until you want some more modular shader code. For example I have two shader include files, each needing an atomic counter that I've written as pluggable code that doesn't know about the other. Clearly I don't want to specify hard coded binding points and I'd like the application to handle it automatically. I don't really care which binding points they use, just that they aren't the same.
Vertex shader attributes appear similar. I can force a binding location at runtime (glBindAttribLocation
) before shader linking or alternatively let OpenGL choose them for me and then query the result (glGetAttribLocation
). I can also search through all attributes (glGetActiveAttrib
).
How can I implement automatic unique binding points for atomic counters, so they're not hard coded and I can mix shader code?
リンク後に変更しないという制限がありますが、これが可能な方法がいくつかあります。
- シェーダーでバインディング ポイントを指定せず、リンク時に OpenGL に選択させます。OpenGLがこれを行うかどうかはわかりません。もしそうなら、どのように照会して、使用された結合点を見つけますか?
- リンクする前にシェーダー オブジェクトをクエリして、アトミック カウンターを見つけます。次に、属性の場合と同様に、一意のバインド場所を指定
glBindAttribLocation
します。結合点を割り当てるメカニズムはありますか? - アトミック カウンターを探してすべてのシェーダー コードを解析し、バインド ポイントを、おそらく
#define
マクロを使用して、シェーダー コード自体の一意のインデックスに置き換えます。最後の手段。私は本当にこれをする必要はありません。