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