Tag Archives: c

Getting back into C

So I spend a lot of my time at my computer, it’s a fact of life as a computer science PhD student. However, while I may have a vim window open 90% of the time, more often than not there will be latex or matlab code in that vim window. Sometimes, if I get one of those rare lulls in deadlines, there may even be Ruby or Haskell code from when I’m learning or prototyping.

This, however, has made me incredibly lazy. I do no low level programming, know very limited assembler, and get spoiled rotten by higher level languages. How much work does my_hash["key"] = value do for me? Well, a lot.

To address this, I’m refreshing myself of my C knowledge and building basic abstract types. But, I’m doing it in a way which would make the most academically minded coder weep with glee in its abstraction and cleanliness – although possibly at the expense of speed. But, I’m an academic, I only need to be aware of speed, while making sure my code is clear enough for people to read and hopefully learn.

So far, I’ve blasted through stacks, queues, linked lists, and binary trees. There are still some utility functions to build with regards to the trees, but I’m engineering them to use function pointers and void * memory in order to not tie my data structures to types.

Currently the code is on GitHub and will be built up as I add more data structures. Hopefully I have time for tries, ropes, prefix trees, graphs, and example code of how to use them. Search algorithms and other classics are hopefully going in, with the aim to make a library not for performance, but for clarity and aid in teaching.

Feel free to email me suggestions and requests for algorithms.

SlimDX and Shaders – Constant Buffers

Setting up

Having played with a few GLSL shaders in C++, I thought that moving to a DirectX/HLSL solution sould be fairly simple.

Creating the most simple pixel shader was easy enough, and SlimDX is a decent wrapper for DX in C# – so after an hour or so I had the standard working triangle floating in space. I wanted to go further (obviously), and having spent the last few days delving into the demoscene and finding the most amazing site for pixelshader techniques (http://www.iquilezles.org/www/index.htm) I wanted to have a shader to work with (x,y) coordinates that were between (-1,1) rather than absolute screen coordinates.

This requires passing the current render area width and height to the shader to normalise with. This is done using Constant Buffers and on the shader side looks like:

 

cbuffer ConstBuffer : register(c0)
{
	float2 resolution;
}

// Simple vertex shader
float4 VShader(float4 position : POSITION) : SV_POSITION
{
	return position;
}

// Pixel shader
float4 PShader(float4 position : SV_POSITION) : SV_Target
{
        // Get normalised (x,y)
	float2 p = -1 +  2 * (position.xy/resolution);

        // Spikes in the corner
	float c = abs(p.x*p.y);
	return float4(c,c,c,1.0f);
}

 

To get the resolution variable into the register for the shader to use, you must create a ConstantBuffer in the program code and assign it to the shade. The Constant Buffer must be a size which is divisible by 16, so if your data is too small, just put it in a bigger buffer.

// Create data stream, we only need 8 bytes, but round up to 16
var resolution = new DataStream(16, true, true);
// Fill the stream with width/height info - I'm using a renderform
resolution.Write(new Vector2(form.ClientSize.Width, form.ClientSize.Height));
// Rewind the stream
resolution.Position = 0;
// Create and bind a buffer
context.PixelShader.SetConstantBuffer(new Buffer(device,     //Device
                                                 resolution, //Stream
                                                 16,         // Size
                                                 // Flags
                                                 ResourceUsage.Dynamic,
                                                 BindFlags.ConstantBuffer,
                                                 CpuAccessFlags.Write,
                                                 ResourceOptionFlags.None,
                                                 4),
                                      0); // Register

This lets us run the above shader, giving us:

Simple Shader results

 

Bonus

Having played on shader toy, I repurposed the Deform effect for a static image.

Shader:

cbuffer ConstBuffer : register(c0)
{
	float2 resolution;
}

// Simple vertex shader
float4 VShader(float4 position : POSITION) : SV_POSITION
{
	return position;
}

// Pixel shader
float4 PShader(float4 position : SV_POSITION) : SV_Target
{
        // Get normalised (x,y)
	float2 p = -1 +  2 * (position.xy/resolution);

        // Deform focus
	float2 m = float2(0.2f, 0.1f);

        // Deform
	float a1 = atan((p.y-m.y)/(p.x-m.x));
	float r1 = sqrt(dot(p-m,p-m));
	float a2 = atan((p.y+m.y)/(p.x+m.x));
	float r2 = sqrt(dot(p+m,p+m));

	float2 uv;
	uv.x = 0.2 + (r1-r2)*0.25;
	uv.y = sin(2.0*(a1-a2));

	float w = r1*r2*0.8;
	float c2 = abs(uv.x*uv.y);
	float4 col = float4(c2,c2,c2,1.0f);

	return float4(col.xyz/(0.1+w),1.0f);
}

Result:
Deform shader

Degree finished

Ok, so my degree results are in and I passed with first class honours. Sweet.

Now just to wait for my Ph.D funding. Hopefully today or tomorrow.

I am currently working on a C/Lua agent based AI simulation and will soon br poking around with my colleagues game engine to give it physics.

Will post interesting stuff.