mono/MonoGame — pull request #1636 by Shqrdx (2013-04-08 11:46:18+00:00)
Title: ComputeShader for MonoGame !

This is a huge commit for monogame.

First mark - its worked - I tested - and not conflict with other platforms.
Second mark - Im not added cs files to project simply because I dont know how to do it. You can fix these bugs later.
Third mark - I dont know how to implement Content routine. You can add this later anyways.
Forth mark. Its only for DirectX for now, and only support Windows8. opengl 4.3 i left to future.(oh Opentk is not support it ?)
Fifth mark. I dont added license(only on 1 file). If you want you can add they later.

Added ComputeShader - the object which used compiled compute shader and bring output to ComputeSurface. From compute surface you can simple extract data from texture.

Heres example of WinRT Game with my compute shader.

notes - xnb is renamed cso file which can easily generated in C++ visual studio environment - this example using compiled compute shader code below.

[code]
RWTexture2D Output;

[numthreads(32, 32, 1)]
void main( uint3 threadID : SV_DispatchThreadID )
{
Output[threadID.xy] = float4(threadID.xy / 1024.0f, 0, 1);
}
[/code]

[code]
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.Storage.Streams;

namespace ComputeShaderTest
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
ComputeShader computeShader;
ComputeSurface2D computeSurface2D;
Texture2D output;
IBuffer buffer;
bool done = false;

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

protected override async void Initialize()
{
// TODO: Add your initialization logic here

computeSurface2D = new ComputeSurface2D(GraphicsDevice,512,512,true,SurfaceFormat.Color);
output = new Texture2D(GraphicsDevice,512,512,true,SurfaceFormat.Color);

var file = await Package.Current.InstalledLocation.GetFileAsync(@"Content\cs.xnb");
buffer = await FileIO.ReadBufferAsync(file);

base.Initialize();
}

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

//computeShader = Content.Load("cs");

// TODO: use this.Content to load your game content here
}

protected override void UnloadContent()
{
computeShader.Dispose();
computeSurface2D.Dispose();
}

protected override void Update(GameTime gameTime)
{
if (!done && buffer!=null)
{
byte[] arr = new byte[buffer.Length];
Windows.Storage.Streams.DataReader.FromBuffer(buffer).ReadBytes(arr);
computeShader = ComputeShader.CreateFromCompiledBytecode(GraphicsDevice, arr);

computeShader.SetOutputSurface(computeSurface2D);
computeShader.Compute(32,32,1);

Color[] data = new Color[512*512];
computeSurface2D.Texture.GetData(data);
output.SetData(data);

done = true;
}
base.Update(gameTime);
}

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

spriteBatch.Begin();

if (done)
{
spriteBatch.Draw(output, new Vector2(0, 0), Color.White);
}
spriteBatch.End();

base.Draw(gameTime);
}
}
}
[/code]

And here is result [img]http://imageshack.us/photo/my-images/688/testttb.png/[/img]

Please accept this as I do a hard work and really want to help monogame !