私は、時間の経過とともに粒子が増加したいと思っています。私はadviceを得て、バッファー値を高く設定して、パーティクルの量を再生することができます。私が考えているのは、バッファに最大カウントサイズを設定し、次にshader
に、粒子属性を取る配列のstruct
を持っています。メタルシェーディング言語 - バッファーバインディング
私は私のswift
でこれを持っている:
var vectMaxCount = 10
var metalvects = [float3(0.0,0.0,0.0),float3(1.0,0.0,0.0),float3(2.0,0.0,0.0)]
var vectBuffer: MTLBuffer!
その後、私はbuffer
を登録します。
vectBuffer = device!.makeBuffer(length: MemoryLayout<float3>.size * vectMaxCount, options: [])
し、それに応じてbuffer
を更新:
...
command_encoder.setBuffer(vectBuffer, offset: 0, at: 2)
var bufferPointer = vectBuffer.contents()
memcpy(bufferPointer, &metalvects, MemoryLayout<float3>.size * vectMaxCount)
let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width/threadGroupCount.width, drawable.texture.height/threadGroupCount.height, 1)
command_encoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount)
command_encoder.endEncoding()
command_buffer.present(drawable)
command_buffer.commit()
やからそれを取得しようmetal
ファイル:
struct Vects
{
float3 position[100];
};
kernel void compute(texture2d<float, access::write> output [[texture(0)]],
constant Vects &vects [[buffer(2)]],
uint2 gid [[thread_position_in_grid]]) {
...
}
と私はエラーました:私のエラーを与えるそれはラインcommand_encoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount)
を示している
validateComputeFunctionArguments:727: failed assertion `(length - offset)(160) must be >= 1600 at buffer binding at index 2 for vects[0].'
を。私はbuffer binding
について少し読んで、私は問題を与えるthreadGroupCounts
またはThreadGroup
を送信する方法だと思います。
float3 position[100];
をfloat3 position[7];
に変更すると、それでも機能します。それ以上のものであれば、同様のエラーが発生します。
どうすれば解決できますか?
と推定される数式はthreadGroups
とthreadGroupCount
ですか?それを行うための経験則でさえ?ケンThomasesの答えに基づいて
Update01
は、私はに私のコードを変更します。
SWIFT:
vectBuffer = device!.makeBuffer(length: MemoryLayout<float3>.stride * metalvects.count, options: [])
...
memcpy(bufferPointer, &metalvects, MemoryLayout<float3>.stride * metalvects.count)
...
金属:
struct Vects
{
float3 position[3];
};
...
それは今の仕事を行います。しかし、私はどのようにして、後でthis postのようなアプリケーションで使用するバッファメモリを割り当てることができますか?
update01を確認してください。私はあなたが答えた別の投稿からの参照を取っています。 – sooon
バッファを割り当てるときには 'vextMaxCount'を使い続けることができます。これは、 'metalvects.count'を使うべき' memcpy() 'の呼び出しです。 –
うん、それは動作します。私は 'Memory Layout'などについてもっと読む必要があります。良いアドバイスはありますか? – sooon