mono/MonoGame — pull request #2100 by Shqrdx (2013-10-20 15:36:25+00:00)
Title: ComputeShader again :)

I know, I know - a while ago I sended simular commit - and it was declained. But now I refresh it a bit and tryed again.

1. These files ComputeShader.cs and ComputeSurface2D.cs added only to WinDX and WinRT solutions. They will not touch opengl projects.
2. I added some preparations for OpenGL to code. I assume, in future it will be helpful, SDL or opengl4net will be implemented, instead Opentk.
3. Here is examples.
-----------------------------------------WinDX demo project-----------------------------------------

using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ComputeShaderTestWinDX
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

ComputeShader cs;
ComputeSurface2D csSurf;
Texture2D output;

public Game1()
: base()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

public static byte[] GetFileContent(string filename)
{
if (!File.Exists(filename)) return null;

string str = null;
try
{
return File.ReadAllBytes(filename);
}
catch (Exception e)
{
throw e;
}

return null;
}

protected override void Initialize()
{
output = new Texture2D(GraphicsDevice, 1024, 1024, true, SurfaceFormat.Color);

var arr = GetFileContent(@"Content\cs.cso");

cs = new ComputeShader(GraphicsDevice, arr);
csSurf = new ComputeSurface2D(GraphicsDevice, 1024, 1024);

cs.SetOutputSurface(csSurf);
cs.Compute(32, 32, 1);

Color[] color = new Color[1024 * 1024];
csSurf.GetData(color);
output.SetData(color);

base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}

protected override void UnloadContent()
{
cs.Dispose();
csSurf.Dispose();
output.Dispose();
}

protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();

spriteBatch.Draw(output, Vector2.Zero, Color.White);

spriteBatch.End();

base.Draw(gameTime);
}
}
}

--------------------------------WinRT demo project--------------------------------------------------

using System;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.Storage.Streams;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ComputeShaderTestWinRT
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

ComputeShader cs;
ComputeSurface2D csSurf;
Texture2D output;
Task bytecodebuffer;

public static async Task GetFileContent(string filename)
{
var file = await Package.Current.InstalledLocation.GetFileAsync(filename);
return await FileIO.ReadBufferAsync(file);
}

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
output = new Texture2D(GraphicsDevice, 1024, 1024, true, SurfaceFormat.Color);
bytecodebuffer = GetFileContent(@"Content\cs.cso");
base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}

protected override void UnloadContent()
{
cs.Dispose();
csSurf.Dispose();
output.Dispose();
}

protected override void Update(GameTime gameTime)
{
if (cs == null && bytecodebuffer.IsCompleted)
{
var buffer = bytecodebuffer.Result;
byte[] arr = new byte[buffer.Length];
Windows.Storage.Streams.DataReader.FromBuffer(buffer).ReadBytes(arr);

cs = new ComputeShader(GraphicsDevice, arr);
csSurf = new ComputeSurface2D(GraphicsDevice, 1024, 1024);

cs.SetOutputSurface(csSurf);
cs.Compute(32, 32, 1);

Color[] color = new Color[1024 * 1024];
csSurf.GetData(color);
output.SetData(color);
}

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();

spriteBatch.Draw(output, Vector2.Zero, Color.White);

spriteBatch.End();

base.Draw(gameTime);
}
}
}

4 The test compiled shader file is here - http://wikisend.com/download/332120/cs.cso
Push this file to Content folder for work.

5. Its just a place for future experimenting - dont look angry on it.