Entries for tag "rendering", ordered from most recent. Entry count: 187.
# MRI Rendering
Mon
14
Feb 2011
Having injured arm is not a good thing, but for this reason I had MRI (Magnetic Resonance Imaging) and got results on a CD, so I've decided to try to render them in 3D as soon as I could move my arm a bit. Here is the final result:
Comments | #directx #medicine #rendering Share
# Parallelizing Algorithms with Intel TBB and C++ Lambdas
Fri
27
Aug 2010
My demo for RiverWash is practically finished. I still could polish it or even make some big changes, because I know it's not great, but that's another story. What I want to write about today is how easily an algorithm can be changed to run in parallel on multicore processors when you use Intel Threading Building Blocks and C++ lambdas.
First, here is an algorithm. In one of my graphic effects I fill a 256x512 texture on CPU every frame. For each pixel I calculate a color based on some input data, which are constant during this operation. So the code looks like this:
void SaveToTexture(const D3DLOCKED_RECT &lockedRect)
{
uint x, y;
char *rowPtr = (char*)lockedRect.pBits;
for (y = 0; y < TEXTURE_SIZEY; ++y)
{
XMCOLOR *pixelPtr = (XMCOLOR*)rowPtr;
for (x = 0; x < TEXTURE_SIZEX; ++x)
{
*pixelPtr = CalcColorForPixel(x, y);
++pixelPtr;
}
rowPtr += lockedRect.Pitch;
}
}
How to parallelize such loop? First, some theoretical background. Intel TBB is a free C++ library for high-level parallel programming. It has nice interface that makes extensive use of C++ language features but is very clean and simple. It provides many useful classes, from different kinds of mutexes and atomic operations, through thread-safe, scalable containers and memory allocators, till sophisticated task scheduler. But for my problem it was sufficient to use simple parallel_for function that utilizes the task scheduler internally. To start using TBB, I've just had to download and unpack this library, add appropriate paths as Include and Library directories in my Visual C++ and add this code:
#include <tbb/tbb.h>
#ifdef _DEBUG
#pragma comment(lib, "tbb_debug.lib")
#else
#pragma comment(lib, "tbb.lib")
#endif
Second topic I want to cover here are lambdas - new, great language feature from C++0x standard, available since Visual C++ 2010. Lambdas are simply unnamed functions defined inline inside some code. What's so great about them is they can capture the context of the caller. Selected variables can be passed by value or by reference, as well as this pointer or even "everything". It makes them ideal replacement for ugly functors that had to be used in C++ before.
Summing it all together, parallelized version of my algorithm is not much more complicated than the serial version:
void SaveToTexture(const D3DLOCKED_RECT &lockedRect)
{
tbb::parallel_for(
tbb::blocked_range<uint>(0, TEXTURE_SIZEY),
[this, &lockedRect](const tbb::blocked_range<uint> &range)
{
uint x, y;
char *rowPtr = (char*)lockedRect.pBits + lockedRect.Pitch * range.begin();
for (y = range.begin(); y != range.end(); ++y)
{
XMCOLOR *pixelPtr = (XMCOLOR*)rowPtr;
for (x = 0; x < TEXTURE_SIZEX; ++x)
{
*pixelPtr = CalcColorForPixel(x, y);
++pixelPtr;
}
rowPtr += lockedRect.Pitch;
}
} );
}
This simple change made all my 4 CPU cores busy for 90+% and gave almost 4x speedup in terms of frame time, which is good result. So as you can see, coding parallel applications is not necessarily difficult :)
Comments | #libraries #rendering #c++ Share
# Demo for RiverWash 2010
Fri
20
Aug 2010
I code a demo for RiverWash 2010 demoscene party. I had a lot of doubt on this, but the more is done, the more I feel convinced I'm going to show it on the party, despite I know it's not of the highest quality, both because no super-advanced technology is involved and because I made all the graphics myself :) It will be my first demo made for such competition. All in all I'm more from Warsztat than demoscene community. Still I like the scenish atmosphere and after each big party, like Assembly or Breakpoint, I download and watch the productions. I've also been at the RiverWash last year and I had a lot of fun there.
I probably shouldn't show my demo before the party, so I'll do something inspired by what my former employers did on the website of their new game development studio - 11 bit studios - I'll show only small parts of some screenshots :)

Comments | #rendering #events #demoscene #productions Share
# SIGGRAPH 2010
Mon
02
Aug 2010
On July 25-29th the 37th SIGGRAPH 2010 conference took place, subtitled "The People Behind the Pixels" this year. As always, many interesting papers were presented, varying from purely scientific, not-so-practical and working with SIGGRAPH-Interative-Frame-Rate (TM) :) through advances in movies and photography technology till what's most interesting for me - the real-time, hardware-accelerated graphics rendering related to game development. Now, after the event, many papers and slides are available for free to download. If you are interested, here are some links:
There is also a very active #siggraph hashtag on Twitter.
Comments | #rendering #events Share
# Music Analysis - Spectrogram
Wed
14
Jul 2010
I've started learning about sound analysis. I have some deficiencies in education when it comes to digital signal processing (greetings for the professor who taught this subject at our university ;) but Wikipedia comes to the rescue. As a starting point, here is a spectrogram I've made from one of my recent favourite songs: Sarge Devant feat. Emma Hewitt - Take Me With You.
Now I'm going to exaplain in details how I've done this by showing some C++ code. First I had to figure out how to decode an MP3, OGG or other compressed sound format. FMOD is my favourite sound library and I knew it can play many file formats. It took me some time though to find functions for fast decoding uncompressed PCM data from a song without actually playing it for all 3 minutes. I've found on the FMOD forum that Sound::seekData and Sound::readData can do the job. Finally I've finished with this code (all code shown here is stripped from error checking which I actually do everywhere):
Comments | #math #libraries #music #rendering #dsp #algorithms Share
# Color Names in .NET - CheatSheet
Mon
05
Jul 2010
Some color values used in computer science have their names, like "Red" (#FF0000) or "Navy" (#000080). You probably know them if you've written anything in HTML. But there are more of them than just several most popular ones, made of values 0x00, 0x80 and 0xFF. I've prepared (or rather, to be honest, copied from MSDN Library) a table of color names available in .NET standard library, as static variables in System.Drawing.Color, System.Drawing.Pens and System.Drawing.Brushes classes. Here is my "Color Names in .NET" CheatSheet:
Color_Names_in_DotNet.pdf
Color_Names_in_DotNet.odt
Comments | #rendering #graphics #.net Share
# Effects in DirectX 11
Sat
15
May 2010
I no longer believe Microsoft did a good job complicating new DirectX so much. Effects framework - the API that supported loading and using effect files that grouped HLSL code and render states into passes and techniques - is no longer intrinsic part of D3DX. Instead they provided source code for this library so you have to compile it by yourself!
To do this: Enter your DX SDK subdirectory "Samples\C++\Effects11", open a "Effects11_*.sln" solution file appropriate for your Visual C++ version and compile the project in both Debug and Release configuration.
Then to use effects API in your project you have to include this header: "YOUR_DX_SDK_PATH\Samples\C++\Effects11\Inc\D3dx11effect.h" and link with this lib: "YOUR_DX_SDK_PATH\Samples\C++\Effects11\Debug\D3DX11EffectsD.lib (Debug) or "YOUR_DX_SDK_PATH\Samples\C++\Effects11\Release\D3DX11Effects.lib" (Release), as well as with "d3dcompiler.lib" (in both configurations).
Here is example of how to load an effect from file. You need to first compile a source code from file or memory into a blob binary effect and then create real effect object from this blob.
// Compile effect from HLSL file into binary Blob in memory ID3D10Blob *effectBlob = 0, *errorsBlob = 0; HRESULT hr = D3DX11CompileFromFile( "Effect1.fx", 0, 0, 0, "fx_5_0", 0, 0, 0, &effectBlob, &errorsBlob, 0); assert(SUCCEEDED(hr) && effectBlob); if (errorsBlob) errorsBlob->Release();
// Create D3DX11 effect from compiled binary memory block ID3DX11Effect *g_Effect; hr = D3DX11CreateEffectFromMemory( effectBlob->GetBufferPointer(), effectBlob->GetBufferSize(), 0, g_Dev, &g_Effect); assert(SUCCEEDED(hr)); effectBlob->Release();
The effect itself is not enough. You need to retrieve object that represents "pass" to use it. So you get a technique from the effect (by index or by name) and then the pass from the technique.
ID3DX11EffectTechnique *g_EffectTechnique; // No need to be Release()-d. g_EffectTechnique = g_Effect->GetTechniqueByIndex(0); assert(g_EffectTechnique && g_EffectTechnique->IsValid()); ID3DX11EffectPass *g_EffectPass; // No need to be Release()-d. g_EffectPass = g_EffectTechnique->GetPassByIndex(0); assert(g_EffectPass && g_EffectPass->IsValid());
Now when you have this object, you can apply settings from this pass to the device context during rendering:
g_EffectPass->Apply(0, g_Ctx); g_Ctx->Draw(3, 0);
But still one problem reamins. In DirectX 11 you need to pass a pointer to the bytecode with compiled shader when creating input layout - a step that you probably cannot omit. Fortunately there is a way to access this pointer stored inside loaded effect. You just need to pass through two descriptors, just like this:
D3DX11_PASS_SHADER_DESC effectVsDesc;
g_EffectPass->GetVertexShaderDesc(&effectVsDesc);
D3DX11_EFFECT_SHADER_DESC effectVsDesc2;
effectVsDesc.pShaderVariable->GetShaderDesc(effectVsDesc.ShaderIndex, &effectVsDesc2);
const void *vsCodePtr = effectVsDesc2.pBytecode;
unsigned vsCodeLen = effectVsDesc2.BytecodeLength;
ID3D11InputLayout *g_InputLayout;
D3D11_INPUT_ELEMENT_DESC inputDesc[] = { /* ... */ };
hr = g_Dev->CreateInputLayout(
inputDesc, _countof(inputDesc), vsCodePtr, vsCodeLen, &g_InputLayout);
Luckily it looks like the effect framework doesn't add much functionality over pure HLSL shader supported by D3D11 itself, so you don't have to use it. Defining these techniques and passess is not so important after all...
Comments | #directx #rendering Share
# HLSL syntax highlighting for jEdit
Sat
01
May 2010
Some time ago I've created a syntax highlighting mode of HLSL language (the shader language of DirectX) for jEdit - my favourite text editor. It is now included in the official jEdit distribution. But it wasn't updated for a long time, since Shader Model 2. Now I've created a new version that supports all the features of new DirectX shaders and effects up to these from DirectX 11 (Shader Model 5), including ones from the upcoming June 2010 version.
So if you don't use jEdit, just keep in mind that there is probably no other text editor (which I would know about) with a coloring scheme for shader language. (I know AMD RenderMonkey and NVIDIA Fx Composer do that, but these are big shader IDE-s, not just text editors.) And if you do, here is how to install it. Download the file:
Place it in your jEdit's "modes" subdirectory, e.g.: "C:\Program Files (x86)\jEdit 4.3.1\modes" and replace the existing one. That's all. You can double-click on the right part of the status bar in jEdit to open the Buffer Options window and select Edit mode = "hlsl".
But it's better to associate this coloring scheme with some file extensions. To do that, open file: "C:\Program Files (x86)\jEdit 4.3.1\modes\catalog", comment out the "javafx" mode as it owns the "fx" file extension by defalt:
<!--<MODE NAME="javafx" FILE="javafx.xml"
FILE_NAME_GLOB="*.fx" />-->
Then find and alter the entry about "hlsl" mode to associate it with whatever file extension you use for your shaders, like the example:
<MODE NAME="hlsl" FILE="hlsl.xml"
FILE_NAME_GLOB="*.{fx,hlsl}" />
If you edit this file inside jEdit, you don't even have to restart it - new rules are applied automatically.
You may ask why not just use the C++ coloring scheme for shader code? Of course you can do it, the syntax is similar because all the tokens, like strings, numbers and identifiers look the same way. But my coloring schemes give separate colors for language elements such as: semantics (like c:COLOR0), component indexing (like v.xyzz), atomic types (like float), object types (like Texture2D or RWStructuredBuffer) and intrinsic functions (like sin, cos, InterlockedCompareExchange).