write GLSL POPs
https://derivative.ca/UserGuide/Write_GLSL_POPs
GLSL POP
最もシンプルなGLSL POP
point/vertex/prim のいずれかひとつを操作する
唯一 "Passes" で複数回かけることができる
全てのアトリビュートは読み込み可能
指定した他のクラスのアトリビュートも関数を通じてアクセス可能
pointクラスの"Attrib"なら TDIn_Attrib()
vertexクラスの"Attrib"なら TDInVert_Attrib()
primクラスの"Attrib"なら TDInPrim_Attrib()
Index Bufferも使える
TDInputPointIndex(uint inputIndex, uint VertIndex) を参照
スレッド周り
TDNumElements(): number of requested threads
TDIndex() : 1D index
Manualモードでは TDNumElements(), TDIndex() はともにundefinedでコンパイルエラーに
code:glsl
void main() {
const uint id = TDIndex();
if(id >= TDNumElements())
return;
Pid = TDIn_P(); //same as TDIn_P(0, TDIndex());
}
P[id] = TDIn_P(); を通すために
Pがinputに存在している
Output AttributesでPを選択している
TDIn_P()はTDIn_P(0, TDIndex())の略
TDindex()はidに対応しているため
TDNumElements() はスレッド数だが、実際のポイント数などより大きな数
32や64といったワークグループの倍数に繰り上げられるから
if文箇所で、確保されたSSBOの範囲外への書き込みを弾いてエラーを防ぐ
GLSL Advanced POP
GLSL POPと以下の点での違いや追加機能がある
point, vertex, primitiveの全クラスに対するアトリビュートの読み書きおよび作成
複数のPOP出力 (下流にてGLSL Select POPで選択)
ポイント数やプリミティブ数の変更
インデックスバッファへの書き込み
Per Primitive Batchモード: プリミティブ毎の処理モード
複数種のプリミティブ
効率化のため、プリミティブの種別毎にインデックスバッファ内でグループ化されて処理される
プリミティブのタイプ毎にシェーダを作るのが有利
if文で分岐せずとも同じ種別のプリミティブを処理することが前提となるため
GLSL Copy POP
コピー毎にGLSLを実行する
全てのコピーは同じトポロジーに制限される (Copy POPと同様)
テンプレート(点群)を入力できる (Copy POPと同様)
その他の入力はないので POP Buffer ページとTDBuffer()関数を使う必要がある
GLSL TOPとやMATと同様
point, vertex, primそれぞれにコンピュートシェーダが実行される (Copy POPと内部的に同様)
デフォルトではpointのみカスタムシェーダが走る
vertexやprimは入力されたアトリビュートがそのままコピーされる
ただしvertexやprimにカスタムシェーダを実行することも可能
code:glsl
void main() {
const uint id = TDIndex();
if(id >= TDNumPoints())
return;
//TDInputNumPoints(): number of points on input geo
//TDInputPointIndex(), TDInputIndex(): point id matching current point id on input
//TDCopyIndex(): copy number
//TDTemplate_Attrib() functions to sample template input attributes
//Pid = TDIn_P() + TDTemplate_P();
//same as Pid = TDIn_P(TDInputPointIndex()) + TDTemplate_P(TDCopyIndex());
Pid = TDIn_P() + TDCopyIndex() * vec3(0.5, 0, 0);
//updates existing point groups if any
TDUpdatePointGroups();
}
TDIndex(): 出力点のindex
出力する各点に対して1スレッド実行される
TDInputPointIndex(): 今の出力点に対応する入力点のindex
TDIn_P(): TDInputPointIndex()のP (引数のないとき)
対応する入力元の点を読むようにしたいため例外的に
TDCopyIndex(): 今のコピーindex
TDTemplate_P(): CopyIndexに対応するテンプレート入力点のP (引数のないとき)
TDUpdatePointGroups(): 入力点にポイントグループがあったとき、各コピーにそれを引き継ぐ
All
code:glsl
TDInputIndex(); //index matching the input (source) point/prim/vert
TDCopyIndex(); //current copy index
TDTemplateNumPoints(); //number of point in template input
point
code:glsl
uint TDNumPoints(); //number of output points
uint TDInputNumPoints(); //number of input points
vertex
code:glsl
uint TDNumVertsBatch(); //number of output verts in current primitive batch
uint TDInputNumVertsBatch(); //number of input verts in current primitive batch
uint TDInputNumVerts(); //total number of input verts
uint TDVertIndex(); //current global vert index
prim
code:glsl
uint TDNumPrimsBatch(); //number of output prims in current primitive batch
uint TDInputNumPrimsBatch(); //number of input prims in current primitive batch
uint TDInputNumPrims(); //total number of input prims
uint TDPrimIndex(); //current global prim index
Attribute Access
Input Attributes
読むとき
ほぼこれ
code:glsl
TDIn_AttribName()
Indexとかを指定したいとき
index範囲外だと最後の要素が返ってくる (bounds checkingが有効)
code:glsl
//to access input inputIndex AttribName
attribType TDIn_AttribName(uint inputIndex, uint elementId, uint arrayIndex)
//if no argument is specified it returns the value at elementId for input 0, at arrayIndex 0 if attribute is an array attribute
TDIn_AttribName() = TDIn_AttribName(0, TDIndex(), 0);
// arrayIndex is optional and defaults to 0
TDIn_AttribName(uint inputIndex, uint elementId) = TDIn_AttribName(inputIndex, elementId, 0);
//this performs bounds checking, return last elements if outside of bounds, last arrayIndex if arrayIndex is out of bounds
//to access frames cached with cache POPs, arrayIndex is optional
attribType TDInCache_AttribName(uint inputIndex, uint cacheIndex, uint elemId, uint arrayIndex);
GLSL POPで他のクラスから読むとき
GLSL Advanced POPで読むときもほぼ一緒
code: glsl
attribType TDInPoint_AttribName(uint inputIndex, uint pointId, uint arrayIndex);
attribType TDInPrim_AttribName(uint inputIndex, uint primId, uint arrayIndex);
attribType TDInVert_AttribName(uint inputIndex, uint vertId, uint arrayIndex);
書くとき
GLSL POP
code:glsl
AttribName[] = value;
GLSL Advanced POP
code:glsl
attribType oTDPoint_AttribName[];
attribType oTDPrim_AttribName[];
attribType oTDVert_AttribName[];
GLSL Advanced POP (Extra Outputsを使ったとき)
インデックスではなく出力名を関数にいれる
code:glsl
attribType oTDPoint_OutputName_AttribName[];
attribType oTDPrim_OutputName_AttribName[];
attribType oTDVert_OutputName_AttribName[];
Built-in Functions
大体GLSL Copy POP以外
TDIndex()
TDNumElements()
TDInputNumPoints(uint inputIndex)
TDInputNumPrims(uint inputIndex)
TDInputNumVerts(uint inputIndex)