私の知る限り、これは一般的に不可能です。
正確に何をしようとしていますか?おそらく別の解決策があります
編集::コメントを考慮して。GPU での一般的なベクトル計算だけを行いたい場合は、頂点シェーダーではなくピクセル シェーダーで実行してみてください。
たとえば、2 つのベクトルを交差させたいとします。まず、データをテクスチャに書き込む必要があります。
//Data must be in the 0-1 range before writing into the texture, so you'll need to scale everything appropriately
Vector4 a = new Vector4(1, 0, 1, 1);
Vector4 b = new Vector4(0, 1, 0, 0);
Texture2D dataTexture = new Texture2D(device, 2, 1);
dataTexture.SetData<Vector4>(new Vector4[] { a, b });
これで、データを含む 2*1 のテクスチャが得られました。スプライトバッチとエフェクトを使用して、テクスチャを単純にレンダリングします。
Effect gpgpu;
gpgpu.Begin();
gpgpu.CurrentTechnique = gpgpu.Techniques["DotProduct"];
gpgpu.CurrentTechnique.Begin();
spriteBatch.Begin();
gpgpu.CurrentTechnique.Passes[0].Begin();
spriteBatch.Draw(dataTexture, new Rectangle(0,0,2,1), Color.White);
spriteBatch.end();
gpgpu.CurrentTechnique.Passes[0].End();
gpgpu.CurrentTechnique.End();
今必要なのは、上で示した gpgpu 効果だけです。これは標準的な後処理シェーダーで、次のようになります。
sampler2D DataSampler = sampler_state
{
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
AddressU = Clamp;
AddressV = Clamp;
};
float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
{
float4 A = tex2D(s, texCoord);
float4 B = tex2D(s, texCoord + float2(0.5, 0); //0.5 is the size of 1 pixel, 1 / textureWidth
float d = dot(a, b)
return float4(d, 0, 0, 0);
}
technique DotProduct
{
pass Pass1
{
PixelShader = compile ps_3_0 PixelShaderFunction();
}
}
これにより、A と B の内積が最初のピクセルに書き込まれ、B と B の内積が 2 番目のピクセルに書き込まれます。次に、これらの回答を読み返すことができます (役に立たないものは無視します)。
Vector4[] v = new Vector4[2];
dataTexture.GetData(v);
float dotOfAandB = v[0];
float dotOfBandB = v[1];
多田!これを大規模に実行しようとすると、小さな問題がたくさんあります。ここにコメントしてください。遭遇した問題があればお手伝いします:)