HLSL

New tutorial: Crash Course in HLSL

I’m happy to announce a new tutorial, which happened by chance :) I accidentally stumbled upon some old drafts of mine for a project that never saw the light of day, and I decided to post one of those drafts online.

It talks about the High Level Shading Language, some of its history, syntax and structure. The whole tutorial can be found either here on this site; or on sgtconker.com. I hope you enjoy it!

Working on shadows…

Those that follow my twitter account have seen this already, but I wanted to let everyone know I’m working on a system for 2D dynamic shadows, again.

However, this one is very different from the last sample I made on the subject. In that one, you had to define the geometry of the shadow casters, and build the shadows based on that. This process also implied lots of computations done each frame on the CPU, which yielded in low peformance. So all in all, even though it looked great, I was never really satisfied with tat technique for shadows.

I spent a lot of time thinking of an alternative way to do dynamic 2D shadows, while not using the CPU too much, and having unlimited complexity for the shadow casters. And two weeks ago, the idea hit me. I’m not going to go into technical details just yet, since I plan to make a nice tutorial about this, but I can say that my idea was based on taking the concept of shadow maps from 3D and somehow use it in 2D.

Here’s two pictures of how it looks so far:

first version

second version

As you can see, the casters can have any shape and complexity, and furthermore, the complexity of the shadow casters has no effect on the framerate.

I’m still working on this, but you’ll know as soon as it’s done.

FX Composer rocks!

I had NVIDIA’s FX Composer installed on my PC for a long time now, as part of my development tools, but rarely used it after installing it.

Usually, when I want to experiment with a new shader, I boot up XNA GS, add a random model to the scene, add a new Effect file, apply the Effect on the model, and then start experimenting with the shader code.

Today, I decided to implement a idea I had for next month’s sample, but instead of going my usual route, I decided to click the FX Composer icon. After about 5 minutes of figuring out the menus and various operations you can do, I was in shader-prototyping heaven.

For a simple comparison: you probably know how when writing native DirectX/OpenGL applications, you have to write some window management code and game loop code before you can get to writing your game, while XNA GS simply lets you write the game. This is exactly what FX Composer does for me.

You can go directly to writing your shader, without bothering with scene management and objects. The changes you make to the shaders are reflected in the rendering window with a single click, and it even provides toolbar buttons to add some basic models in the scene and test your shaders on them. (and that’s a giant understatement of the features available in FX Composer)

The only thing I would like improved at this moment is some IntelliSense-like features beyond simple syntax highlighting. But given the fact that even in Visual Studio you have notepad-like features for shaders (unless you use InteliShade), I’ll take what is given to me.

So I wholeheartedly recommend FX Composer to anyone interested in shader prototyping/development. It’s a great tool.

rimlighttest-2008-07-18-23-20-23-84.jpg

July Sample: Rim Lighting

I’m going to go on vacation soon, so I’m putting the July sample online. It’s not much, since I’ve been busy writing the Fur Rendering Tutorial, but I think it’s a nice effect.

The sample is dedicated to Rim Lighting, an effect seen in some "cute" games. Read more about it and get the source code here.

RimLightTest 2008-07-18 23-20-23-84

furrender-2008-07-09-01-26-51-97.jpg

Fur Rendering in XNA

My fur rendering article just went online on Ziggyware. It is an entry for the Summer Ziggyware XNA Article Contest, so if you like it, vote for it when voting begins, in a few days.

The article explains how to draw fur, animate it, and apply it on models.

You can read it here.

FurRender 2008-07-09 01-26-51-97

rimlighttest-2008-07-02-11-42-59-12.jpg

Coming Soon…

Fake Rim Lighting, for use in cute games (inspired by Super Mario Galaxy screenshots)

RimLightTest 2008-07-02 11-42-59-12 RimLightTest 2008-07-02 11-42-26-62

RimLightTest 2008-07-02 11-42-41-85

Fur

FurRender 2008-07-02 13-32-03-11 FurRender 2008-07-02 13-31-16-24

FurRender 2008-07-02 14-29-47-10 FurRender 2008-07-02 14-30-14-23

Small pre-pre-beta shadow mapping lirary

I’m releasing a small library for shadow mapping in XNA. This is the beginning of a library that will allow using shadow maps in XNA without writing a new shader, or setting up your models. Just using some calls to a library class.

I also included a small example of using the library with 1 object. The main class of the library is ShadowMapRenderer, and it implements PCF shadow mapping, with screen-space bluring.

Remember, this is a very early version, I will probably change the API very much until a real beta version. I just thought I’d put it out to help people who want to have quick shadows in their 3D games.

You can download it here: ShadowMapRenderer

Have Fun!

depthrecovery-2008-05-19-02-30-51-86.jpg

Can a 2D image intersect a 3D model?

Yes it can :)

My may sample is also my first public sample written in XNA Game Studio 3.0 CTP, and it shows how to restore the depth buffer from a depth texture.

This way, models drawn to a render target, with post processing applied to them can intersect and obscure normal models, drawn after all the post processing is done.

For more details, jump to the sample’s page: Restoring the Depth Buffer, where you can find a short explanation on how the sample works.

To dive right into the code, download it here. Please note that the sample is written in XNA Game Studio 3.0 CTP, but the technique also works just fine in 2.0.

DepthRecovery 2008-05-19 02-30-51-86

Converting Displacement Maps into Normal maps

This is a repost from the old blog, since this is one of those interesting stuff, which weren’t big enough to warrant a sample.

Working on my VTF tutorial, I came across the need to apply lighting to a terrain. For lighting, we need normals, of course, but I only had heightmaps available. When the terrain is static, this poses no problems at all. All we have to de is generate a normal map from the heightmaps with whatever tools we want. For example, NVIDIA has a Photoshop plugin for this here.

However, when we have dynamic terrain, with real time deformation, this is no longer an option. This is especially the case when we make the deformations on the displacement map. After lots of reading and searching, I’ve come across the Sobel Filter, which can be used to compute normal maps.

As a result, I made a pixel shader that takes as input the displacement map and outputs the normal map. At runtime, after modifying the displacement map, I apply this shader and put the result into a render target. This result is then used when drawing the terrain, when computing lighting in the pixel shader.

So here’s the HLSL code:

float textureSize = 256.0f;
float texelSize =  1.0f / textureSize ; //size of one texel;
float normalStrength = 8;
 
float4 ComputeNormalsPS(in float2 uv:TEXCOORD0) : COLOR
    {
    float tl = abs(tex2D (displacementSampler, uv + texelSize * float2(-1, -1)).x);   // top left
    float  l = abs(tex2D (displacementSampler, uv + texelSize * float2(-1,  0)).x);   // left
    float bl = abs(tex2D (displacementSampler, uv + texelSize * float2(-1,  1)).x);   // bottom left
    float  t = abs(tex2D (displacementSampler, uv + texelSize * float2( 0, -1)).x);   // top
    float  b = abs(tex2D (displacementSampler, uv + texelSize * float2( 0,  1)).x);   // bottom
    float tr = abs(tex2D (displacementSampler, uv + texelSize * float2( 1, -1)).x);   // top right
    float  r = abs(tex2D (displacementSampler, uv + texelSize * float2( 1,  0)).x);   // right
    float br = abs(tex2D (displacementSampler, uv + texelSize * float2( 1,  1)).x);   // bottom right
 
    // Compute dx using Sobel:
    //           -1 0 1 
    //           -2 0 2
    //           -1 0 1
    float dX = tr + 2*r + br -tl - 2*l - bl;
 
    // Compute dy using Sobel:
    //           -1 -2 -1 
    //            0  0  0
    //            1  2  1
    float dY = bl + 2*b + br -tl - 2*t - tr;
 
    // Build the normalized normal
    float4 N = float4(normalize(float3(dX, 1.0f / normalStrength, dY)), 1.0f);
 
    //convert (-1.0 , 1.0) to (0.0 , 1.0), if needed
    return N * 0.5f + 0.5f;
}

You should tweak normalStrength until the result you get is satisfactory.

And here is the result of applying that code to two heightmaps, and some terrain lit using a normal map generated by this shader.

 

displacement normals

HLSL Learning Resources

In response to some people asking me how to start learning shader programming, I made a list of some resources that could prove helpful when learning HLSL:

The Shader Series, found on creators.xna.com are a very good set of articles and samples, that explain the basic knowledge needed to understand and write shaders in HLSL.

Riemer Grootjans also has a series of tutorials on HLSL, which can be found on his site.

A good book on shaders is The Microsoft DirectX 9 Programmable Graphics Pipeline by Kris Gray. I found this book extremely useful, with very good explanations, and nicely structured. The code samples, however, are in C/C++, but for most people that shouldn’t be a problem.

After mastering the basics, you can explore other samples and articles, which can be found on www.ziggyware.com and creators.xna.com.

Another good place to find information on shaders are the web pages of the two big GPU manufacturers, NVIDIA and ATI. Each site has a section devoted to developers, which are great places for everyone, beginners are advanced users alike. Both sites provide code samples, documentation, tools and SDK’s. Out of these, my favorites are NVIDIA Shader Library, NVIDIA SDK and ATI SDK. Most important, these site contains lots of articles and presentations from various conferences, which explain a wide range of effects and techniques, and how they are achieved on the GPU. Admittedly, most of them do not provide HLSL code, but the idea and algorithm is almost always more important than the code itself.

For advanced effects and usages of the GPU, I recommend the GPU Gems series and the ShaderX series.

Finally, as a recommendations for those who want to learn HLSL, try to examine as many samples as possible, and play with the HLSL code to see what happens when changing values, until you understand why things happen the way they do. Don’t start by doing HDR Lighting, or Relief Mapping. Start with the small tutorials, and understand what happens and why before going on to the more spectacular samples. Last, but not least, don’t be afraid to look at C/C++ samples. The DirectX SDK contains some very good samples of HLSL. I know, they’re written in C/C++, but the shader code stays the same when going from C++ to XNA.