Showing posts with label directx. Show all posts
Showing posts with label directx. Show all posts

Wednesday, June 1, 2011

DirectX: No lights, no colors

Whenever you find yourself writing a serious 3D application, with things like physics, skeletal animations, custom collision and interaction structures, predefined camera paths and / or spatial subdivision structures, you'll soon encounter the need for debugging geometry.

In the middle of all that eye candy shader goodness, you want your debugging geometry to be simple and clean. In our 3D framework, we allow the application to add simple debugging shapes (lines, boxes, spheres, etc) to the render queue, with a single argument specifying the desired color. Internally, the engine switches from shaders to fixed function pipeline for rendering those debugging primitives. Oh, and to make sure the debugging geometry is rendered in a monotonous color, lighting is disabled. Simple, right?

Wrong. All debugging geometry turned out bright white. No matter what color we specified. No warnings, no errors, no nothing, just plain white.

Turns out that in DirectX Fixed Function Pipeline, to use material colors, you have to enable lighting. Which wouldn't be so bad if someone had done a decent job documenting it.



Diffuse color is generated when lighting is enabled, that is, D3DRS_LIGHTING is set to TRUE

It's there alright, right on the page about Fixed Function Vertex Processing (DirectX9), but even if you stumble upon that page (as opposed to the dedicated pages about material and lighting, where you are very likely to end up from a google/bing/msdn search), on first glance it looks like that's just documenting IDirect3DDevice9::ProcessVertices, a call you are very likely not to be using. Like, ever.

The solution is to simply enable lighting, disable all fixed pipeline lights and set the ambient light to bright white. That, or simply use a dedicated shader.

Saturday, July 3, 2010

Empty buffers in PIX... or so it seems.

One of the most powerful tools in the DirectX SDK toolbox is PIX. In the early stages of 3D development it will be an indispensable help in pinpointing rendering issues... and the moment you got those nailed, PIX turns into an awesome performance analysis tool to help you achieve those truly interactive frame rates.

In this post I'd like to focus on a single particular pitfall that I repeatedly run into (because well, I'm thick). All of this is actually properly documented inside the PIX help... but no proper caffeine-laden milestone-dreading developer ever takes the time to read the manuals, right?

Let's say we are working on the scene depicted below. It features some simple geometric shapes and a basic stencil shadow implementation.


Now pretend we spot a tiny error in the shadowing in the above image. Suspecting a small issue in the stencil buffer, we fire up PIX and take a look at the Stencil Channel in the Render Tab.



Black void? That's rather surprising, as we do have shadows showing up in the scene, so something must be working right in the stencil algorithm. So why don't we see anything in this buffer?

The trick here is the range displayed in the PIX Render Tab. By default, it tries to depict the full (0.000 to 1.000) range of the buffer values. Even with multiple overlapping shadows, stencil values will typically be really low (in this case, only the first couple of values out of a full 24 bits range).

The solution is the double slider control in the upper right of the Render Tab: the top and bottom sliders can be used to modify the minimum and maximum value of the visualization. Simply grab the bottom slider and drag it nearly all the way to the left to have the values show up.



And behold, there are our stencil shadows! Same thing goes for the depth buffer, with the caveat that in a typical setup the values in our depth buffer will be really high. You now have to tweak the top slider until you get the proper minimum level for things to show up.


Luckily PIX is clever enough to remember the slider settings for each buffer individually, so once you have them set to the proper levels you can freely switch buffers without having to constantly fiddle with the settings.

So now, let the debugging commence!