LoginSignup
3
1

More than 3 years have passed since last update.

GLSLでSSBOのメモリアライメントに引っかかったこと

Posted at

GLSLで定義した構造体を含めたSSBO(ShaderStorageBufferObject)についてです。
例えば、以下のようなコードで想定外の動作に遭いました。

struct TPoint
{
    int Status;
    vec3 Position;
    vec3 Velocity;
};

layout (std430, binding = 0) buffer Point
{
    TPoint Points[];
};

問題は2つです。
1. vec3はvec4としてアライメントされる
2. 最大のデータサイズに応じてメモリパディングが行われる(構造体は構造体としてサイズ定義)
これを踏まえると、GLSLではTPoint構造体の変数1つについて図のようなメモリ配置と認識します。

アライメントさえ分かっていれば、SSBOのメモリ確保をこれに合わせれば解決です。
あるいは、vec3の変数をXYZの3要素に分解してもいいです。パディングが無駄に感じる場合は(筆者はそうですが)この方がいいでしょう。
次が修正したコードです。

struct TPoint
{
    int Status;
    float PositionX;
    float PositionY;
    float PositionZ;
    float VelocityX;
    float VelocityY;
    float VelocityZ;
};

layout (std430, binding = 0) buffer Point
{
    TPoint Points[];
};

参考:
https://stackoverflow.com/questions/29531237/memory-allocation-with-std430-qualifier

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1