我正在寻找编写一个 perona malik 各向异性过滤器,看起来出于性能原因我需要使用 gpu。长话短说,我想知道如何在 xna 中使用 HLSL 来执行 GPGPU 任务。
我正在寻找一些代码片段来将一些数据移动到 GPU,逐帧处理它,然后返回它,以便我可以用它做其他事情。
根据我的阅读,我需要使用“乒乓球”
编辑
这是我到目前为止所拥有的
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
namespace HotplateTest
{
public class XNAClass : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
RenderTarget2D Target;
RenderTarget2D Output;
Effect physicsEffect;
Vector4[] positions;
public XNAClass()
{
graphics = new GraphicsDeviceManager(this);
}
protected override void Initialize()
{
Target = new RenderTarget2D(graphics.GraphicsDevice, 10, 10, false, SurfaceFormat.Vector4, DepthFormat.None);
Output = new RenderTarget2D(graphics.GraphicsDevice, 10, 10, false, SurfaceFormat.Vector4, DepthFormat.None);
positions = new Vector4[100];
for (int i = 0; i < positions.Length; i++)
{
positions[i] = new Vector4(i);
}
Target.SetData<Vector4>(positions);
base.Initialize();
}
protected override void LoadContent()
{
base.LoadContent();
physicsEffect = Content.Load<Effect>("shader");
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(Target);
GraphicsDevice.Clear(Color.Black);
physicsEffect.Techniques[0].Passes[0].Apply();
physicsEffect.Parameters["oldPositionTexture"].SetValue(Output);
physicsEffect.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.SetRenderTarget(null);
Target.GetData<Vector4>(positions);
base.Draw(gameTime);
}
}
}
HLSL
texture oldPositionTexture;
sampler oldPositionSampler = sampler_state
{
Texture = < oldPositionTexture >;
MipFilter = POINT;
MinFilter = POINT;
MagFilter = POINT;
ADDRESSU = CLAMP;
ADDRESSV = CLAMP;
};
struct VertexShaderInput
{
float4 Position : POSITION;
float2 Tex : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION;
float2 Tex : TEXCOORD0;
};
// input texture dimensions
static const float w = 10;
static const float h = 10;
static const float2 pixel = float2(1.0 / w, 1.0 / h);
static const float2 halfPixel = float2(pixel.x / 2, pixel.y / 2);
VertexShaderOutput VS(VertexShaderInput input)
{
VertexShaderOutput output = (VertexShaderOutput)0;
output.Tex = input.Tex;
return output;
}
float4 PS1(VertexShaderOutput input) : COLOR0
{
float2 myV = input.Tex;
float myPosAndMass = tex2D(oldPositionSampler, myV);
return float4(myPosAndMass, myPosAndMass, myPosAndMass, myPosAndMass);
}
technique Technique1
{
pass Pass0
{
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS1();
}
}
我收到一条错误消息
Microsoft.Xna.Framework.Graphics.dll 中出现“System.ArgumentException”类型的未处理异常
附加信息:您在此方法中用于 T 的类型是此资源的无效大小。
有趣的是,当我在 C# 代码中使用 Single 而不是 Vector4 时,不会出现错误消息。当然,这意味着在位置变量中计算的结果现在是无用的,因为当它们真的是 vector4 时,它们被解释为单个。