Saturday, April 17, 2010

dotfiles

Everyone knows linux stuff is totally customizable -- but what I figured out was that a lot of the configuration options are in hidden 'dotfiles'! I wanted to give cygwin a dark theme, so here's the most basic dark theme for vim and xterm

vim using .vimrc

" Example .vimrc here http://www.vi-improved.org/vimrc.php
" Vim colorschemes generator http://inspiration.sweyla.com/code/
" {
set background=dark " we plan to use a dark background
colorscheme inspiration907827
syntax on " syntax highlighting on
" }





xterm using .Xdefaults

XTerm*Background: black
XTerm*Foreground: grey
XTerm*utf8: 1





-- check out http://dotfiles.org/ for more of this stuff

Saturday, February 20, 2010

opengl post processing

I figured out how to use OpenGL for post processing ... finally -- sort of used it as an excuse for valentines day to make something that would run on a Mac



Uses this pixel shader to make the big pixel convolution - http://www.lighthouse3d.com/opengl/ledshader/

Monday, January 18, 2010

wtl for the poor

I've always liked WTL. I learned programming by exploring Windows programming and C++ -- For me, just using the "Win32 API" was a good exercise (was it really though?). As I got better, I was constantly using C++ in weird ways, but when WTL came out, I knew that that it was actually good C++ for the Windows programming


WTL project circa 2006 - http://zxcvbn.googlecode.com/svn/trunk/wtltrip/

Recently though, Microsoft made WTL not so free, by taking rid of the MFC/ATL in the "Platform SDK" (which is now called the "Microsoft SDK").

The SDK does not have ATL base classes anymore, and it does not come with the Visual Studio Express Editions either. Free open source web browser Google Chrome uses WTL on Windows, and has some extensive workarounds for the issues


I found that I could get the ATL base classes if I register with MSDN and download the WDK here ...wait what? the device driver development kit? Microsoft acknowledges this in the WDK release notes : Windows Driver Kit (WDK) Version 7.0.0 Release Notes

[August 2009] ATL Best Practice - Use the Visual Studio release

ATL is included in the WDK as a convenience for driver developers who need to create Windows applets for their drivers. ATL should not be used for software development beyond this scope. The ATL that is available through Visual Studio provides a more complete and current set of libraries and should be used for development of software and web applications. Visual C++ Express Edition is available for free from MSDN.


So there aren't any good free sources for the MFC/ATL base, so the WTL is no longer a free software platform like it should be.

sources:
http://www.caloni.com.br/blog/archives/como-usar-wtl-com-o-atl-do-ddk


========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

Sunday, December 27, 2009

last.fm 2009

Last.fm has a cool API that let's you gewt almost any sort of data from them just using a web request in XML or JSON format. During their countdown for the top artists of 2009 they encouraged people to use the API, and I used the Last.fm API to show how many plays you scrobbled from each artist in the top 40.

http://zxcvbn.t35.com/lastfm-2009.html

Features:
  • Uses Last.fm API to get JSON data for number of songs listened to
  • Dynamic page effect using JavaScript to organize page elements
  • Uses last.fm javascript client API

The program is pretty simple, it uses the Library.getTracks API method to retrieve your listening data. I create a last.fm object. you need an 'API key' and 'API secret', which you 'apply' for on their API page http://www.last.fm/api. Then, you can call any of the methods of the API you like

Using my program as an example
/* Create last.fm object */
var lastfm = new LastFM(
{
apiKey: 'Apply for key at http://www.last.fm/api',
apiSecret: 'Apply for API code at http://www.last.fm/api',
cache: cache
});

/* Success function parses JSON data. note: stringify JSON data before parsing */
function success(json_data) {
var json_string = JSON.stringify(json_data);
var json_parse = JSON.parse(json_string);
var tracks = json_parse.tracks;
}

/* Failure function parses error codes */
function failure(code, message) {
alert(code + ': ' + message);
}

/* Call the api function with JSON format API parameters */
lastfm.library.getTracks({ user: "LastFM Username", artist: "Britney Spears" },
success_function_callback, failure_function_callback);


Update:
I made it so you can find out how many songs you've listened to by any artist you type in. It's not very easy to find out if the artist isn't in your top charts

http://zxcvbn.t35.com/lastfm-2009.html

Tuesday, December 15, 2009

cpu post processing

Out of curiosity, I tried to replicate the post processing effect using the CPU instead of using shaders on the GPU. This is complicated because we're not supposed to access the graphics data directly, but uh-hmm -- whatever

To get the rendered graphics data, I use GetRenderTextureData which uses the system memory pool. But MSDN says that : "system memory is not typically accessible by the Direct3D device". So we must wait on a lock to copy from the system memory to default memory. This is a really costly operation and basically is the failure of my experiment. Running in exclusive (fullscreen) mode, takes about ~50ms to run, and the framerate is killed to <20fps

// Texture surfaces with given CreateTexture parameters
// renderTexture is D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT
// systemMemTexture is D3DUSAGE_DYNAMIC, D3DPOOL_SYSTEMMEM
// defaultMemTexture is D3DUSAGE_DYNAMIC, D3DPOOL_DEFAULT

pd3dDevice->GetRenderTargetData(renderTextureSurface, systemMemSurface);
systemMemSurface->LockRect(&source, NULL, 0);
defaultMemSurface->LockRect(&dest, NULL, 0);
copy(source.pBits, dest.pBits);
systemMemSurface->UnlockRect();
defaultMemSurface->UnlockRect();



Apparently, GPGPU programming is enabled by hardware and the software. For example in nVidia CUDA it let's programmers control shared memory for hardware the graphics that enables programmer control (see Toms's Hardware: CUDA from the Hardware Point of View )



From the GPGPU FAQ it says about the shared memory

"Texture read operations are routed through a cache structure to allow efficient exploitation of reference locality. Read operations also factor into shader thread scheduling as read operations that miss the cache will block the thread and cause other ready threads to run while waiting for the read to memory to complete. Writes are routed through different logic that does some amount of buffering to maximize the efficieny of writes."

Saturday, December 12, 2009

gpu post-processing

I wanted to explain an overview of the post-processing effects using GPU shader programs since I've been working on this for awhile now. Basically, the rendering for post processing is broken into two steps

- we render-to-texture using normal rendering routines
- then use a pixel shader to texture a square with the render-target texture

The code I used the DirectX SDK sample ($SDK\Samples\C++\Direct3D\PostProcess)
Here is a simple model of the rendering

// render-to-texture
graphics->SetRenderTarget( offscreenTexture );
graphics->RenderScene( );

// square is the size of the screen, from back buffer description
VERTEX quad [4] = /* use the BackBufferDescription( ); */

// set shader parameters
shader->SetTechnique( "RenderPostProcess" );
shader->SetTexture( “offscreen_texture", offscreenTexture );

// square is textured by the pixel shader effect
graphics->SetRenderTarget( screen );
graphics->DrawPrimitive( quad );


I based this code off of the DirectX SDK sample ($SDK\Samples\C++\Direct3D\PostProcess, definitely look at that this if you are trying this). The square is calculated to fit the screen (using DXUTGetD3D9BackBufferSurfaceDesc). Then we texture the "fullscreen square" using our RTT texture, and we can use pixel shader effects during this rendering.

Here, the simplest "toon shader" ever

// render target from effect->settexture
texture offscreen_texture;
sampler2D s = sampler_state {
Texture = <>;
};

// "toon shader"
float4 PostProcessPixel(float2 uv : TEXCOORD0) : COLOR
{
float4 c = tex2D(s, uv_coord);
c.rgba -= (Color.rgba % 0.3f );
return c;
}

technique RenderPostProcess {
pass p0 {
VertexShader = null;
PixelShader = compile ps_2_0 PostProcessPixel ();
ZEnable = false;
}
}


Thanks to gamedev for helping me with this miracle



Updated:

Source code - http://zxcvbn.googlecode.com/svn/trunk/skelly/
Wiki page - http://code.google.com/p/zxcvbn/wiki/skelly
Demo - http://zxcvbn.googlecode.com/files/Skelly.exe

Monday, November 16, 2009

graphics pipeline

It's important to know how shaders work with the graphics pipeline, because the "shader model" of graphics programming is totally different from the "fixed function model"! MSDN says here http://msdn.microsoft.com/en-us/library/ms800169.aspx

"The following user-mode display driver functions are not called by the Direct3D runtime when the vertex shader is enabled
  • * MultiplyTransform
  • * SetTransform
  • * SetMaterial
  • * SetLight
  • * CreateLight
  • * DestroyLight


This means that when you use shaders, you have to compute lighting and transformations! DirectX will also ignore flags like D3DRS_LIGHTING and texture state flags when you use shaders. When you program using the shader model, your shader program has responsibilities!

Contrast the API for the fixed function model, which uses the D3D device functions, with the shader model, which is similar but passeses parameters to your shader effects through the shader interface

IDirect3DDevice9::SetTexture vs ID3DXEffect::SetTexture
IDirect3DDevice9::SetTransform vs ID3DXEffect::SetMatrix
IDirect3DDevice9::SetLight vs ID3DXEffect::SetValue

I'll go over the exact responsibilities of shaders in your program. The MSDN page on the graphics pipeline says has some really important things about this - http://msdn.microsoft.com/en-us/library/ee415715(VS.85).aspx and I will use this gamasutra article for base code - http://www.gamasutra.com/features/20030418/engel_02.shtml



1) "The vertex-shader (VS) stage processes vertices ... performing per-vertex operations such as transformations, morphing, skinning, and per-vertex lighting."

Therefore, the vertex shader is typically responsible for worldview transformations and also calculating light values. The vertex shader sets up a VS_OUTPUT struct that contains transformed vertex information that is rasterized and passed to the pixel shader. The vertex shader outputs a VS_OUTPUT structure

struct VS_OUTPUT
{
float4 Pos : POSITION;
float3 Light : TEXCOORD0;
float3 Norm : TEXCOORD1;
};

VS_OUTPUT VS(float4 Pos : POSITION, float3 Normal : NORMAL)
{
VS_OUTPUT Out = (VS_OUTPUT)0;
Out.Pos = mul(Pos, matWorldViewProj); // transform Position wrt worldview
Out.Light = vecLightDir; // output light vector
Out.Norm = normalize(mul(Normal, matWorld)); // transform normal wrt world
return Out;
}

2) "A pixel shader is a program that combines texture data, per-vertex values, and other data to produce per-pixel outputs...Pixel shader inputs are interpolated from the vertex attributes of the primitive being rasterized"

We basically receive a VS_OUTPUT from our vertex shader as our pixel shader input, but the vertex data is interpolated. We use the NORMAL value to calculate lighting in our pixel shader, because the NORMAL in our pixel shader is interpolated from the rasterizer for our little pixel! The pixel shader outputs is a RGBA

float4 PS(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1) : COLOR
{
float4 diffuse = { 1.0f, 0.0f, 0.0f, 1.0f};
float4 ambient = {0.1, 0.0, 0.0, 1.0};
return ambient + diffuse * saturate(dot(Light, Norm));
}

Note: The POSITION register is not valid in the pixel shader, we are using pixels and not 3D space, but we still use the NORMAL data for lighting. The rasterization stage that converts our vertex shader data into pixel shader data is interesting. See the wikipedia page is good http://en.wikipedia.org/wiki/rasterization