Jul 11, 2012

Why Da Hell Am I Inventing the Wheel Again?

My goal of this week: finishing the Screen Space Decal presentation that I'll give out at Siggraph 2012. If it was purely just making a ppt file I think I would have been more inspired to finish it. The problem is that I'm not with Relic any more and I have no access to their codebase or running editor.  So, I'm re-implementing all the features again just to have a demo program where I can take enough screen captures.

It's pretty boring to be honest. I'm not really learning anything by doing this. Just repeating what I already know inside out. Anyways all the feature implementation is done, and some test assets are all placed.  So I'll try to finish this week.

I have a couple of interesting projects I wanna work on after this, so I might be able to use that as my motivation to finish the presentation... we will seee...

Jul 10, 2012

How To Generate C# Object from XML

Found this from my old work note.  Before I forget about it, let me post it here. Maybe someone find it useful.

Auto-generating a C# object from an XML file. This was something I had to do while trying to import a CAT rigged animation from 3DS Max to my Unity project. A FBX file doesn't correctly export IK-ed CAT rig animations unless every single frame is baked.  So I tried to do it through 3DS Max's animation export feature through an XML file.

I took a look at the XML file thinking I should be able to manually write a C# class that can be deserialized from the XML file.  But I was wrong, the XML file was too complicated.  Then I thought there must be an way to auto-generate a C# object from an XML file.

A quick Google search gave me these two useful stackoverflow pages:

After reading above pages, what I needed to do was basically running the following commands in order in a command prompt window

xsd test.xml /classes -> test.xsd
xsd test.xsd /classes -> test.cs

The first line generates an XML schema file from the given xml, and the 2nd line generates a C# class from the schema. Once this test.cs file is generated all I had to do was using C# XML Serializer to load all the data into a C# object.  It looked something like this

XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(test));
FileStream stream = File.Open(@"test.xml", FileMode.Open);
var a= (test)x.Deserialize(stream);

Simple eh?  Yeah I thought so too :) But unfortunately I couldn't get all the key frame info I wanted from the XML file, either.  So I ended up not using this.  Still a good knowledge to have! :D

Jul 8, 2012


I recently had a chance to use OpenCL to speed up an app I was developing. It was pretty fun, but certainly debugging OpenCL was not so easy. I wish it had more error messages at least.

This is the problem I had:

When I was calling clEnqueueNDRangeKernel() function, it suddenly gave me CL_MEM_OBJECT_ALLOCATION_FAILURE error and later I also got OUT_OF_RESOURCE error too. And the following is all the things I tried and how I eventually solved.... This post is more for my own reference if I run into the same issue later. :P

1. Check how much memory is really being allocated for OpenCL
The sum of all the cl_mem buffers I was using was less than3MB.  OpenCL's minimal global memory size requirement is 128MB. So no problem here. Move on.

2. Implement notification function
So apparently we can attach a notification function that will get some descriptive messages whenever there's an error.  I managed to get a bit more error messages after implementing this: clEnqueueWriteBuffer() function calls were giving me CL_MEM_OBJECT_ALLOCATION_FAILURE. But this one didn't really help to solve the problem.

3. Try to run OpenCL on CPU
It ran fine on my Intel CPU, it only happend on my NVidia GPU.  So I installed the up-to-date GPU driver. Still same problem :(

4. Check memory stomp
So I looked into my OpenCL code more closely.  And it turned out to be I was reading/writing outside of allocated local memory.. DOH! After fixing this, the problem disappeared.. Yay... I solved the problem.. But somehow I still feel stupid. :)