• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
  • entries
    83
  • comments
    0
  • views
    65126

Is SpriteBatch Turing complete?

Sign in to follow this  
Followers 0
shawnhar

1238 views

Time for some end-of-year silliness...
Inspired by this recent Twitter exchange:

Scionwest - @shawnhargreaves @nickgravelyn How about post processing for WP7? I've seen people do Bloom effects and thought P.P. was not part of WP7?
nickgravelyn - @Scionwest Shaders aren't available, but I've seen some clever post-processing done without custom shaders. You theoretically could do CPU based "shaders", but you can also use blend states in fun ways to achieve effects

(which is actually an interesting topic that I may someday return to)
I found myself thinking "well, of course you could implement bloom, because the no-programmable-shaders Reach graphics subset on Windows Phone is Turing complete, so can do anything you can imagine given sufficient ingenuity. It might not be easy, efficient, or sensible, but anything is possible..."
And then I found myself thinking, is that really true?
We can add and subtract via blend states, and perform comparisons with AlphaTestEffect, which seems like it should be enough to enable arbitrary algorithms (at least if we ignore mundane practicalities such as time and space constraints :-) But can I prove that XNA on Windows Phone is enough to enable Turing complete GPU computation?
Conway's Game of Life is already proven to be a form of universal Turing machine, so I set out to see if I could implement that on the GPU without custom shaders.
Starting with the default Game template, I declared two rendertargets to hold the state of my simulation: const int width = 256;
const int height = 128;

RenderTarget2D currentState;
RenderTarget2D nextState;
Plus two helper objects: AlphaTestEffect alphaTestEffect;

Texture2D singleWhitePixel;
These are initialized in LoadContent: currentState = new RenderTarget2D(GraphicsDevice, width, height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8); nextState = new RenderTarget2D(GraphicsDevice, width, height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8); alphaTestEffect = new AlphaTestEffect(GraphicsDevice); singleWhitePixel = new Texture2D(GraphicsDevice, 1, 1);
singleWhitePixel.SetData(new Color[] { Color.White });
In order for it to do anything interesting, the Game of Life must be initialized to more than just zeros. This code (at the end of LoadContent) creates a single glider that will move diagonally across the screen: Color[] initialData = new Color[width * height];

initialData[3 + 1 * width] = Color.White;
initialData[3 + 2 * width] = Color.White;
initialData[3 + 3 * width] = Color.White;
initialData[2 + 3 * width] = Color.White;
initialData[1 + 2 * width] = Color.White;

currentState.SetData(initialData);
For a more interesting initial state, add this (before the currentState.SetData call) to randomize the cells: Random random = new Random();

for (int i = 0; i < 10000; i++)
{
initialData[random.Next(width * height)] = Color.White;
}
My Draw code is simple. It uses SamplerState.PointClamp to scale up the current simulation state to fill the entire screen, without any filtering: SimulateLife();

GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null);
spriteBatch.Draw(currentState, GraphicsDevice.Viewport.Bounds, Color.White);
spriteBatch.End();
Ok, that's the boilerplate taken care of. How are we actually going to implement this SimulateLife method? Wikipedia defines the rules as:

Any live cell with fewer than two live neighbours dies, as if caused by under-population
Any live cell with two or three live neighbours lives on to the next generation
Any live cell with more than three live neighbours dies, as if by overcrowding
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction

But I found it useful to rearrange this to a different (equivalent) form:

Rule 1: If there are two live neighbours, preserve the current state of the cell
Rule 2: If there are three live neighbours, set the cell to alive
Rule 3: Otherwise set the cell to dead

Ok, let's do it! SimulateLife starts by drawing onto the nextState rendertarget, and setting up the AlphaTestEffect to only accept pixels with alpha greater than 128, i.e.. cells that are currently alive: GraphicsDevice.SetRenderTarget(nextState);

GraphicsDevice.Clear(Color.Transparent);

Viewport viewport = GraphicsDevice.Viewport;

Matrix projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);

alphaTestEffect.Projection = halfPixelOffset * projection;

alphaTestEffect.AlphaFunction = CompareFunction.Greater;
alphaTestEffect.ReferenceAlpha = 128;
We are going to count how many alive neighbours each cell has, accumulating this value in the stencil buffer (which was previously cleared to zero). We do this by drawing the current simulation state texture 8 times, shifted one pixel to the top left, top, top right, left, etc. We use a blend state that prevents drawing anything to the color buffer, because we are only interested in generating stencil values. This code combines AlphaTestEffect (which rejects dead neighbour pixels) and the stencil buffer (which counts how many neighbour pixels pass the alpha test): spriteBatch.Begin(SpriteSortMode.Deferred, blendNone, null, stencilAdd, null, alphaTestEffect);

spriteBatch.Draw(currentState, new Vector2(-1, -1), Color.White);
spriteBatch.Draw(currentState, new Vector2( 0, -1), Color.White);
spriteBatch.Draw(currentState, new Vector2( 1, -1), Color.White);
spriteBatch.Draw(currentState, new Vector2(-1, 0), Color.White);
spriteBatch.Draw(currentState, new Vector2( 1, 0), Color.White);
spriteBatch.Draw(currentState, new Vector2(-1, 1), Color.White);
spriteBatch.Draw(currentState, new Vector2( 0, 1), Color.White);
spriteBatch.Draw(currentState, new Vector2( 1, 1), Color.White);

spriteBatch.End();
This uses two custom state objects: static readonly BlendState blendNone = new BlendState
{
ColorSourceBlend = Blend.Zero,
ColorDestinationBlend = Blend.One,

AlphaSourceBlend = Blend.Zero,
AlphaDestinationBlend = Blend.One,

ColorWriteChannels = ColorWriteChannels.None,
};

static readonly DepthStencilState stencilAdd = new DepthStencilState
{
DepthBufferEnable = false,
StencilEnable = true,
StencilFunction = CompareFunction.Always,
StencilPass = StencilOperation.Increment,
};
We now have our rendertarget cleared to zero (which means all dead, so we have already implemented rule #3), while stencil contains a count of live neighbour cells. It is but the work of a moment to draw a copy of the current simulation state, using stencil to only accept cells that have 2 live neighbours, thus implementing rule #1: spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, stencilDrawIf2, null);
spriteBatch.Draw(currentState, Vector2.Zero, Color.White);
spriteBatch.End();
The last draw call implements rule #2 by rendering solid white (which means alive) over only those cells which have 3 live neighbours: spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, stencilDrawIf3, null);
spriteBatch.Draw(singleWhitePixel, viewport.Bounds, Color.White);
spriteBatch.End();
This requires two new state objects: static readonly DepthStencilState stencilDrawIf2 = new DepthStencilState
{
DepthBufferEnable = false,
StencilEnable = true,
StencilFunction = CompareFunction.Equal,
ReferenceStencil = 2,
};

static readonly DepthStencilState stencilDrawIf3 = new DepthStencilState
{
DepthBufferEnable = false,
StencilEnable = true,
StencilFunction = CompareFunction.Equal,
ReferenceStencil = 3,
};
Finally, the end of SimulateLife unsets our output rendertarget, then swap the two rendertargets, replacing the current state with the new one we just computed: GraphicsDevice.SetRenderTarget(null);

Swap(ref currentState, ref nextState);
Swap is a handy little helper, implemented like so: static void Swap(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
So, does it work?

2234.image_5F00_thumb_5F00_32B4C608.png

Tada! SpriteBatch is Turing complete :-)
Exercise for the reader: is it possible to do the same thing without using stencil?aggbug.aspx?PostID=10251913

Source

3
Sign in to follow this  
Followers 0


0 Comments


There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now