下面是我写的一个测试程序(需要引用XNA):
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Orc.SmartImage.Xna
{
public class Shader
{
private class GameHelper : Game
{
public void Init()
{
this.Initialize();
GraphicsDeviceManager m = new GraphicsDeviceManager(this);
m.ApplyChanges();
}
}
private GameHelper m_helper;
public GraphicsDevice GraphicsDevice { get; set; }
public Shader(IntPtr hwnd)
{
m_helper = new GameHelper();
m_helper.Init();
this.GraphicsDevice = m_helper.GraphicsDevice;
}
public void Test()
{
RenderTarget2D tar = new RenderTarget2D(this.GraphicsDevice, 100, 100, 1, SurfaceFormat.Color);
this.GraphicsDevice.SetRenderTarget(0, tar);
this.GraphicsDevice.Clear(Color.Yellow);
this.GraphicsDevice.SetRenderTarget(0, null);
Texture2D txt = tar.GetTexture();
uint[] data = new uint[10000];
txt.GetData(data);
return;
}
}
}
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Orc.SmartImage.Xna
{
public class Shader
{
private class GameHelper : Game
{
public void Init()
{
this.Initialize();
GraphicsDeviceManager m = new GraphicsDeviceManager(this);
m.ApplyChanges();
}
}
private GameHelper m_helper;
public GraphicsDevice GraphicsDevice { get; set; }
public Shader(IntPtr hwnd)
{
m_helper = new GameHelper();
m_helper.Init();
this.GraphicsDevice = m_helper.GraphicsDevice;
}
public void Test()
{
RenderTarget2D tar = new RenderTarget2D(this.GraphicsDevice, 100, 100, 1, SurfaceFormat.Color);
this.GraphicsDevice.SetRenderTarget(0, tar);
this.GraphicsDevice.Clear(Color.Yellow);
this.GraphicsDevice.SetRenderTarget(0, null);
Texture2D txt = tar.GetTexture();
uint[] data = new uint[10000];
txt.GetData(data);
return;
}
}
}
进一步就是写HLSL了。
离C/C++又远了一步。
附:具体测试代码
(注:那个Shader是我测试GPU计算能否通过的部分。IntPtr hwnd是因为GraphicsDevice构造函数中有这样一个参数,不过后来,我绕了过去,但测试程序这里我没删掉,还留在这里。)