Wednesday, August 25, 2010

allocator ---(2)

Most of the situation the stl default allocator is good enough to handle memory allocation.
But I still did a test on BOOST allocator and STL default allocator.
The result is BOOST pool_allocator is faster and more robust than STL default allocator.

class a
{
public:
a(){}
private:
int t;
//long t1;
};
BOOST_AUTO_TEST_CASE( pool )

{

boost::shared_ptr<Timer> timer(new Timer());
std::vector<a,boost::pool_allocator<a>> v(100000000);
//boost::singleton_pool<boost::pool_allocator_tag, sizeof(int)>::release_memory();

std::cout<<"Time spent on pool_allocator with constructor call: " << timer->elapsed()<<std::endl;


timer->restart();
std::vector<a> v2(100000000);
std::cout<<"Time spent on default allocator with constructor call: " << timer->elapsed()<<std::endl;

}











Read More...

allocator ---- (1)

Before talking about this topic, let's talk about new operator.

Ever since I started to use c++ instead of c, malloc, calloc is somehow replaced by new oeprator,
however I had the wrong feeling new is merely call malloc to allocate memory and then call the constructor of the class.
This is true for the global new operator, however too many malloc call to switch context between user mode and system mode is too expensive.
So we need another type of new which is called placement new.
What is placement new?
it's only target on the pointer which has been allocate space but not created and initialize one instance on the specified address

new (place_address) type

new (place_address) type (initializer-list)

So to reduce the significant time waste on repeated malloc call, we can simply allocate a big space, and use placement new to initialize the instances, and that's exactly std::vector allocator doing.

Let's have a look at VS2010's vector implementation of this line

std::vector<a>

(1000);

so the constructor will call resize function, in the resize function, it will call reserve to allocate the specified space. Here reserve will use the allocator's allocate function which will finally call

::operator new(_Count * sizeof (_Ty)) which will allocate a big space altogether first,

_Uninitialized_default_fill_n will truly call _Cons_val(_Al, _First, _Valty()); to construct the instances which basically will call _Count times placement new

::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_ty>(_Val));

to initialize all the instances.

By this way there is only one time malloc happens here:

::operator new(_Count * sizeof (_Ty))

and N times placement new only initialize allocated space in user mode, no extra system mode switching, it's much faster than loop calling malloc or global operator new.


explicit vector(size_type _Count)

: _Mybase()

{ // construct from _Count * _Ty()

resize(_Count);

}

void resize(size_type _Newsize)

{ // determine new length, padding with _Ty() elements as needed

if (_Newsize <>

erase(begin() + _Newsize, end());

else if (size() <>

{ // pad as needed

_Reserve(_Newsize - size());

_Uninitialized_default_fill_n(this->_Mylast, _Newsize - size(),

(_Ty *)0, this->_Alval);

this->_Mylast += _Newsize - size();

}

}

void _Uninit_def_fill_n(_FwdIt _First, _Diff _Count,

const _Tval *, _Alloc& _Al,

_Valty *, _Nonscalar_ptr_iterator_tag)

{ // copy _Count * _Valty() to raw _First, using _Al, arbitrary type

#if _ITERATOR_DEBUG_LEVEL == 2

// if (_Count <>

// _DEBUG_ERROR("negative count in uninitialized fill");

#endif /* _ITERATOR_DEBUG_LEVEL == 2 */


_FwdIt _Next = _First;


_TRY_BEGIN

for (; 0 <>


_Cons_val(_Al, _First, _Valty());


_CATCH_ALL

for (; _Next != _First; ++_Next)

_Dest_val(_Al, _Next);

_RERAISE;

_CATCH_END

}

void _Cons_val(_Alloc& _Alval, _Ty1 *_Pdest, _Ty2&& _Src)
{ // construct using allocator
_Alval.construct(_Pdest, _STD forward<_ty2>(_Src));
}

void construct(pointer _Ptr, _Ty&& _Val)
{ // construct object at _Ptr with value _Val
::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_ty>(_Val));
}










Read More...

Monday, May 4, 2009

Easy way to check memory leak

Instead of overload new operator by ourself, we can use visual studio debugger and  c runtime library to detect memory leak.

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#if defined(_DEBUG) && !defined(DEBUG_NEW)
#define _CRTDBG_MAP_ALLOC
#include < stdlib.h > 
#include < crtdbg.h >
#define DEBUG_NEW new(_NORMAL_BLOCK,THIS_FILE,__LINE__)
#endif

crtdbg.h gives debug version  of malloc and free function and these functions can track memory leak in debug mode. 
Next add _CrtDumpMemoryLeaks() in the place where you want to check memory leak.
If memory leak happens you will get info like this:
d:\l\projects\myprogram\dxengine\dxengine\dxengine\stdafx.h(121) : {216} normal block at 0x009C7B10, 20 bytes long.
 Data: <        <       > 00 05 00 00 20 03 00 00 3C 00 00 00 16 00 00 00
Next according to the memory leak block number 216,add this line at the beginning of the program to tell application to break on the memory leak point.
_CrtSetBreakAlloc(216);

Read More...

Friday, April 10, 2009

opengl and demoscene

Demosceners are a small group of people who dedicate to making wonderful demos. Here is detailed information about them:

Recently, i began to learn how to write this kind of small demos. And i pick up OPENGL+GLSL to do the job.
The small demo i made is follow the idea of this website:
As the author said "it's lots of fun to invent as many formulas as you can"
i also felt the beauty of mathmatic from this demo.
Every thing is easy unless how to pass the float LUT table into the shader to calculate.
All the UV coordinate are floats, how can i save them into a texture.
After search on internet, i found an easy but may not be best way:
This article is about how to use FBO,VBO,PBO etc in opengl to do normal purpose calculation.
And i noticed i can use the 128bit opengl extend texture format. So the internal format is R32G32B32A32(X32Y32Z32W32,or S32T32Q32R32) 
I saved x,y coordinate into each pixel, but i only used R32G32,the others are waste.
uniform sampler2D tex;
uniform samplerRECT tex2;
uniform float     time;
void main()
{
vec4 uv =  texRECT( tex2, gl_TexCoord[0].xy );
vec4 co =  texture2D( tex, uv.xy+vec2(time));
gl_FragColor = co;
} ;
In this way i can implement nearly the same GLSL shader as the author mentioned
except i use samplerRECT,texRECT for the 128bit texture.
And you don't have to set texture coordinate into (0,0)-(1,1), keep the original size instead.

However i understand this is not the best way, the code is as easy as the author mentioned,
but the video memory is somehow waste a bit(I didn't use the Z,W at all).
A friend who is experienced with computer graphics write this two line's shader piece like this:
float x =  texture2D(tex1,float2(gl_TexCoord[0].x,0.0)); 
float y =  texture2D(tex1,float2(gl_TexCoord[0].x,1.0)); 
I realized i can create LUMINANCE format texture,use one pixel to save one float, the first half to
store X coordinates, the second half to store Y coordinates.
Some improvement may happen if i use PBO, i suppose.
However, either LUMINANCE or RECTANGLE_ARB is not supported by all the graphic cards.
NV3xNV4x, G7x, G8xATI
texture 2D, ATI/ARB_texture_float, LUMINANCEnonono
texture 2D, ATI/ARB_texture_float, RGB, RGBAnoyesyes
texture 2D, NV_float_buffer, LUMINANCEnonono
texture 2D, NV_float_buffer, RGB, RGBAnonono
texture RECT, ATI/ARB_texture_float, LUMINANCEnonono
texture RECT, ATI/ARB_texture_float, RGB, RGBAnoyesyes
texture RECT, NV_float_buffer, LUMINANCEyesyesno
texture RECT, NV_float_buffer, RGB, RGBAyesyesno

i attached two sources of small demo, one is use GLSL to implement simple multiple textures, one is LUT texture animation. The 1K/4Kframework is from
specially for compressed exe size and running efficient, so bear the coding style if you don't like it.
LUT example uses CRT malloc etc. functions so it doesn't compile in relese mode and is bigger than 4K. Anyway this is my first try on demoscene, i will do it better next time.
source




 

Read More...

Tuesday, March 17, 2009

my CV

Download Link:



RESUME
Personal Information:
Famlily Name: Xue Given Name: Youchao
Date of Birth: 14/10/1982 Birth Place: China Sex: Male
E-mail: xueyouchao@gmail.com
Mobile: 07980412147
Address: 11 Greg street, Inverness

Education & Working Experience

Sept. 2001 ~ July 2005:Nanjing University of Aeronautics and Astronautics ,Bachelor degree in Information and Computational Science specialized subject.
Courses studied:
Mathematical Analysis, Numerical approximation, Methods of Mathematical Physics, Numerical Methods for Differential Equations, Data Structure, Artificial Intelligence, etc. math, computer science related courses.


Aug. 2005 ~ Nov.2006: worked in Huawei Technologies Co. Ltd., ShenZhen, P.R.China as a software engineer
Joined several telecommunication projects, worked through the full life cycle of software development, including project planning, requirements analysis, design, coding, unit testing, system integration testing, debugging and release.

Nov 2006~ April 2007: English study for IELTS test

July 2007 July~ May 2009 : University of Abertay Dundee
Computer Games Technology Course for MSc/PG Dip

July 2009 ~ Now: Weatherford International, 3D Graphics programmer

Developed projects:
3d AI path finding application demo using vertex shader- accelerated skeleton animation tech.
Directx + Physx + Normal map, vertex animation demo.
Group project : Ant game
PS2 coursework: terrain rendering demo on PS2
World of Champloo: A complete 3D game with split screen and networking two versions, presented in daretobedigital game competition 2008.
Gesture recognition using the Wii remote with neural network approach
1 week's fast-build game using XNA and Physx Engine: Body Cleaner
Ogre3d Editor
Screen shots from some of my previous projects:




Please visit my blog for more details.

Developing Skills
Frequently used programming language: C/C++, c++/cli, c#
Develop Tools: visual studio 2008
Using SDK ,library, Frameworks: .net framework, Ogre engine, Nvidia Ageia Physx engine,Raknet networking lib,directx sdk,XNA etc.
Familiar with HLSL,CG shading language and FXcomposer tool.

English Skills :
Have a good command of both spoken and written English

Hobbies
basketball, traveling, programming, PC/ps2/psp/xbox360 games.




Read More...

Monday, March 16, 2009

A few videos show my Editor functionalities.

1.Load/Save Editor layout:


2.Multi Project working environment:


3.Load/Save scene using .NET XML serializer/deserializer, native running test using TiniXML:


Read More...

Monday, March 9, 2009

.net serializer/deserializer


Load Scene/Save Scene Complete;

Add native application running test functionality;
Layout Save/Load
Here is the structure of scene load/save part.

Why not use TiniXML to do all these jobs?
.net XML Serializer is quite easy to use and i have ready made property classes for property grid, i only have to serialize these property classes into an xml file instead of serialize the whole binary object. It also supports Generic Template List serialization.The lack of supports for Dictionary is a pity though( i have to transport elements of dictionary to a list for this.).

However in deserialization procedure
, TiniXML is much easier to use than .net xml deserializer.

First, i need to prepare an empty constructor for every class i want to deserialize. For this i have to change every referenced member of class to pointer member so i can easily initialize them to NULL in c++/cli for the empty constructor.
Next, in the deserilization procedure it will call every property's set function automatically, so
i have to create certain object after some important property are read at the beginning. Thus i need to fix orders for properties. Add this [XmlElement(Order = ..)] for this step.[XmlIgnore] to ignore the property you don't want to serialize or native member.
The only issue i haven't solved is the situation where you have two classes with inheritance relation. The parent classes' properties are prior to the children classes' properties in the serilized xml file. And there are always situation where the parent class includes an pure virtual object which can't be instantiated first and need to pass children's pointer instead.So deserialize children classes' properties first to instanciate a children class first is the only way to solve this inheritance relation's deserialization.But there is no way to change this parent-children properties order via xml serilizer.( i searched internet and didn't find any one solve this.) I have to change inheritance relation to include relation.

Finally i found i used much more time to solve all these issues but save quite a lot of code lines(I believe) than using TiniXML. Hope serialization of .net can become better in the future.


Read More...

Wednesday, March 4, 2009

Recent project

Didn't update my blog for two weeks. the first week i was working on my editor, adding load/save scene.  It was nearly finished and i will post progress when i finish it completely. And from the second week until now, i was busy making a game with two friends for the Microsoft Image Cup. Ben and me didn't plan to apply for this until we finally find our artist Gao ying who is one of the artist from the Dare2008 winner team.We got 6 days to finish our first demo and submit.


Plus we are obliged to use XNA for this competition. My friend has his own XNA renderer for Axiom engine. From this beginning we found StillDesign physx wrapper for us to connect XNA with physX engine. We worked hard for the whole week, however there was a networking problem at the end that we failed to upload our game to the website. Now i hope they give us another chance to submit our work. Anyway thanks to Benjamin ,Gao ying and me too, that we get our one week product --nano game. Here are screen shoots of shooting virus inside bloodtube.
 

Level Design:

Free exporter OgreMax: can export our scene to xml file, and we made the loader to load the blood tube level and cells as we designed in 3D tool.


Physics View to help us checking physics status of our level elements.

Read More...

Sunday, February 15, 2009

.net and tools

After dare i was working on my master project. During this period, i have studied .NET developement from my friend and flatmate Benjamin lassort(You know you are a great man). He also introduced me an excellent dotnet component weifengluo dockpanel, and the way to render 3D content into weifengluo.With the help of this we can surely develop tools looks like visual studio. Many thanks to my friend to lead me to the way of dotnet and tool development.

I did a small tool for my master project,  gesture recognition using the wii remote with a neural network approach. Basiclly, it's about using a wii remote to draw some shape on a black panel,the
neural network algorithm will then calculate and find the matched result, the character output the matched action at the end.
After dissertation submission, i went back to China. Now i am rewriting this tool, changing it to a 3d editor. Here is the screen shot of this two week's work. I managed to add move,
rotate,scale axis,model view.Also it supports multiple scene managers and each scene manager can has multiple render windows for adjusting camera to different positions. There are quite a few problems when dealing with manipulating objects in different scene manager's different render window. But i test a lot on every step before go forward. On some important steps i drew figures on the paper to make a clear plan. Thanks to the patient it works fine now. I will add as many features as possible later. 







Read More...

Saturday, February 14, 2009

World of Champloo


This is the game we presented in EICC (Edinburgh International Conference Centre) for the daretobedigital competition 2008.With only 2 programmers, 3 artists in the team, we managed to complete this game with split screen and networking two versions in less than 2 months.
Game Trailer:

Networking game play:

You can watch series of videos on youtube:
There are old versions downloadable on the showcase of daretobedigital website(year 2008) :

My work include:  framework, graphics, gui, networking.

SDKs been used:
Ogre engine: absolutely the best choice for graphics rendering with the best c++ software design structure I've ever see.
Physx Engine+NxOgrePhysx is a very good physics engine with visual debugger to use.
NxOgre make it much easier to use with Ogre engine.
OpenAL+OgreAL: same as physx+nxogre, but some sound bugs happen for some sound card. don't know why at the end.
CEGUI:wonderful gui library with two main tools: layout editor, image editor(too many bugs for image editor). with this i implemented particle effects on gui panel when special ability is ready.
Raknet:  Easy to use, with this no need to focus on the low level networking technology, but implement a high level message sending system. A nice lobby, with UDP broadcasting functionality to find all the local machines automaticlly, also was implemented. 

Lessons i learned:
Attach/detach billboard animation ball to physics ball and make them moving with invisible bone to implement picking, throwing, special skills of character, this is not so hard and get a very good effect, for example the water race's final skill has a billboard ball scale and go inside her head. 
In single play version,when player pick up a ball, i found the physics ball bouncing towards the invisible physics plane. Because we were in a hurry, i didn't try too much time then changed another way to solve this problem. That is when players pick up a ball, disable the collision of the physics ball and delete its attached animation, create a new billboard animation moving with the invisible bone in picking,holding,shooting action.After shooting action delete this animation and
attach a new billboard to the original physics ball to shoot it out. However when moving to the network version I remember i kept adding messages at the end to fix all the bugs that led by this method.For synchronizing a special ability 5-6 messages have sent to fix all the bugs in different situations.
Recently, i found actually there is some way to move the physics ball with bone and not collide with the floor. If i can spend more time before, lots of problems will not happen in networking version and the application can become much efficient.
Coding in a hurry, trying to use tricks is making troubles for myself in the future.
When a problem happens, a quick trick or idea to fix may not be the best solution. There always has an easy and suitable way to go but hard to find.I 'd rather spend 10 days to find the entry of the simple way than immediately choose a hard way to go.We need best algorithms, however we also need to make things as simple and straight as possible. Never use tricks to avoid problems, find a good method to solve it.














Read More...