题
您好和用于晦涩标题抱歉:} 我心底试图解释最好我可以。
首先,我是新来的HLSL,但我了解的管道和东西,从童话世界。什么我真的试图做的是使用一般的计算的GPU(GPGPU)。
我不`吨知道的是:我怎么能读*顶点(已使用顶点着色器转换)回到我的XNA应用程序?我读一些有关使用GPU的纹理内存,但我不能找到任何实...
预先感谢任何方式/尖端! : - )
*不知道,如果光栅化和像素着色器(如果有的话)可能bacause,我的意思是,到底它是所有关于像素吧?
解决方案
据我知道这通常是不可能的。
究竟是什么你想怎么办?有可能是另一种解决方案
EDIT ::考虑到的注释。如果你想要做的是在GPU通用矢量计算尝试在像素着色器做他们,而不是顶点着色器。
因此,例如,说你想要做跨两个向量,首先我们需要将数据写入到纹理
//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纹理数据,渲染纹理简单地使用spritebatch和效果:
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的点积到所述第二像素。然后,你可以阅读这些答案回(忽略无用的)
Vector4[] v = new Vector4[2];
dataTexture.GetData(v);
float dotOfAandB = v[0];
float dotOfBandB = v[1];
多田! 有小问题,试图这样做更大规模的整体负载,在这里评论,我会尽力帮助您与任何你遇到了:)
其他提示
如果您接通“流输出舞台,”你的顶点着色器的输出将被存储在内存缓冲区。以后这些值可以从如所期望的GPU或CPU读取。
不隶属于 StackOverflow