Browsed by
Month: August 2010

Some Sugar with your Syntax?

Some Sugar with your Syntax?

In the tutorial recently posted, I created the render state objects using some old-school syntax.

[csharp]
StencilAlways = new DepthStencilState();
StencilAlways.StencilEnable = true;
StencilAlways.StencilFunction = CompareFunction.Always;
StencilAlways.StencilPass = StencilOperation.Replace;
StencilAlways.ReferenceStencil = 1;
StencilAlways.DepthBufferEnable = false;
[/csharp]

You can do this in a much better way nowadays.

[csharp]
StencilAlways = new DepthStencilState()
{
StencilEnable = true,
StencilFunction = CompareFunction.Always,
StencilPass = StencilOperation.Replace,
ReferenceStencil = 1,
DepthBufferEnable = false
};

I’ve seen this syntax before, but 20 years of habits die hard and I rarely remember to use it. Hopefully it will stick now, and make my crappy code that much less crappy.

[/csharp]

Texture Modification using Render Targets, with some Stencil Buffer Action

Texture Modification using Render Targets, with some Stencil Buffer Action

Sometimes you need to modify a texture while your game is running, and there are a number of ways to do this. One of the first things newer game programmers often try to do is use Texture2D.GetData to copy the texture data from the GPU to an array on the CPU, modify the bytes, and then send it back to the GPU with Texture2D.SetData.

This is a bad idea on many, levels. Beyond issues with pipeline stalls, GetData and SetData can be slow, especially when working with a large texture. Any time you’re tempted grab data from the GPU for use on the CPU you should very carefully consider all of your options. There are often other solutions that let you keep the data entirely on the GPU and accomplish the same thing.

This tutorial will use an example that could be solved with GetData and SetData, and show you another alternative using render targets and the stencil buffer that will let you perform the same function entirely on the GPU.

Past Lives

Past Lives

I’ve taken a bit of a programming hiatus so far this summer, with the exception of the WordPress plugin I mentioned last time which I just spent a couple hours a week on over the past several months. Kind of taking a deep breath before plunging into making a game for the upcoming Windows Phone 7. During my break I’ve spent some time thinking about some of the projects I’ve worked on over the years – some that I’ve hated, and some that I’ve loved. One that always stands out for me is a little something called MariusNet, and I thought I’d share the story.