Improving loading times for 2D animations in Directx

Started by
21 comments, last by MartinSmith160 10 years, 10 months ago

ok this is making sense now. Thanks for being patient.


Advertisement

Hi Belfegor,

Just a quick question, each graphic file will have binary data for the D3DXIMAGE_INFO and the actual graphic data from the source graphic file. For a specific item, i will extract the binary data from the packed file and put that into a buffer. I was just wandering does the data have to be in a specific order. So first I add the D3DXIMAGE_INFO bytes in and then the graphic data in and then use that buffer in the D3DXCreateTextureFromFileInMemory function.

Thanks again for the help.

Martin


It doesn't matter what kind of and in what order do you put things, what matters is that you read those in order that you put them and you need to know their types and size.

Sometimes you must first write the count of objects for array type of things like vectors and strings, before you write those, so you can know how much to read later on:

write


std::vector<int> vInt;
...
std::size_t len = vInt.size();
os.write( (const char*)&len, sizeof(std::size_t) );
os.write( (const char*)&vInt[0], vInt.size() * sizeof(int) );

read


std::size_t len;
is.read( (char*)&len, sizeof(std::size_t) ); // first thing in file written is length so we read that first, sizeof(std::size_t) bytes
std::vector<int> vInt(len); // create buffer with enough space to read into it
is.read( (char*)&vInt[0], len * sizeof(int) ); // second thing in file is buffer to read, len * sizeof(int) bytes

Thanks a lot, you have been a great help.

Hi Belfegor,

I have done what you said and its working. I have wrote the code to do the following:

  • Pack data into one big binary file
  • Extract the data in a usable form
  • load a LPDIRECT3DTEXTURE9 using that data

My question is now, i think my extraction must be poor because loading now takes longer.

This Is my header code:


struct TextureInfo
{
    std::string name; // file name/path (dds, tga...)
    D3DXIMAGE_INFO ii;
};

struct Texture_Item
{
	std::string name;
	std::vector<char> info_buffer;
	std::vector<char> buffer;
};

std::vector<std::string> texture_filenames;
std::vector<TextureInfo> vTextureNames;
std::vector<Texture_Item> vTextureItems;

void CreateBinaryFile();
void ExtractBinaryData();

ST_Texture test_bulk_textures[3000];
ST_Sprite *test_bulk_sprites[3000];

Here is my Pack data function:


void GameApp::CreateBinaryFile()
{
	texture_filenames.clear();
	vTextureNames.clear();

	for(int i = 0; i < 3000; i++)
		texture_filenames.push_back("Graphics/cloud-day-3.tga");

	TextureInfo temp_texture_info;

	for(int i = 0; i < texture_filenames.size(); i++)
	{
		temp_texture_info.name = texture_filenames;
		D3DXGetImageInfoFromFile(temp_texture_info.name.c_str(), &temp_texture_info.ii);
		vTextureNames.push_back(temp_texture_info);
	}

	std::ofstream os("packed_textures.bin", ios::binary);
	UINT texFilesCount = vTextureNames.size();
	os.write( (const char*)&texFilesCount, sizeof(UINT) );

	for(size_t i = 0; i < vTextureNames.size(); ++i) 
	{
		UINT nameLen = vTextureNames.name.size();
		os.write( (const char*)&nameLen, sizeof(size_t) );  // to know how much to read it later
		os.write( vTextureNames.name.c_str(), vTextureNames.name.size() );
		os.write( (const char*)&vTextureNames.ii, sizeof(D3DXIMAGE_INFO) );
	
		// opening texture file to copy it to our binary
		ifstream is(vTextureNames.name, ios::binary);
		is.seekg (0, is.end);
		UINT length = is.tellg();
		is.seekg (0, is.beg);
		std::vector<char> vFile(length);
		is.read((char*)&vFile[0], vFile.size());
		is.close();
	
		os.write( (const char*)&length, sizeof(UINT) ); // to know how much to read it later
		os.write( (const char*)&vFile[0], vFile.size() );
	}
	
	os.close();
}

This is my Unpack function:


void GameApp::ExtractBinaryData()
{
	vTextureItems.clear();

	Texture_Item temp_item;

	UINT total_texture_count = 0;
	size_t name_length = 0;
	UINT texture_byte_length = 0;

	ifstream is("packed_textures.bin", ios::binary);
	is.seekg (0, is.end);
	UINT length = is.tellg();
	is.seekg (0, is.beg);
	
	//Read total number of texture
	is.read( (char*) &total_texture_count, sizeof(UINT) );

	for(int i = 0; i < total_texture_count; i++)
	{
		temp_item.name = "";
		temp_item.info_buffer.clear();
		temp_item.buffer.clear();

		is.read( (char*) &name_length, sizeof(size_t) );
		
		temp_item.name.resize(name_length);
		is.read( (char*) &temp_item.name[0], temp_item.name.size() * sizeof(char));

		temp_item.info_buffer.resize(sizeof(D3DXIMAGE_INFO));
		is.read( (char*) &temp_item.info_buffer[0], sizeof(D3DXIMAGE_INFO) );

		is.read( (char*) &texture_byte_length, sizeof(UINT) );

		temp_item.buffer.resize(texture_byte_length);
		is.read( (char*) &temp_item.buffer[0], texture_byte_length);

		vTextureItems.push_back(temp_item);

	}

	is.close();
}

and this is my new load function to use that data:


bool ST_Texture::Load(LPDIRECT3DDEVICE9 d3dDevice,std::string filename, std::vector<char> d3d_info_buffer, std::vector<char> graphic_buffer, D3DCOLOR transcolor)
{
	//standard Windows return value
	HRESULT result;

	//get width and height from bitmap file
	/*result = D3DXGetImageInfoFromFileInMemory((LPCVOID)&d3d_info_buffer[0], d3d_info_buffer.size(), &info);
	if (result != D3D_OK) 	
	{
		st_engine->LogError("Failed to load graphics file: INFO BUFFER",false,"ENGINE_ERRORS.txt");
		texture = NULL;
		return 0;
	}*/

	//get width and height from bitmap file
	result = D3DXGetImageInfoFromFile(filename.c_str(), &info);
	if (result != D3D_OK) 	
	{
		st_engine->LogError("Failed to load graphics file: " + filename,false,"ENGINE_ERRORS.txt");
		texture = NULL;
		return 0;
	}

	result = D3DXCreateTextureFromFileInMemoryEx(
		d3dDevice,
		(LPCVOID)&graphic_buffer[0],
		graphic_buffer.size(),
		info.Width,
		info.Height,
		1,                     //mip-map levels (1 for no chain)
		D3DPOOL_DEFAULT,       //the type of surface (standard)
		D3DFMT_UNKNOWN,        //surface format (default)
		D3DPOOL_DEFAULT,       //memory class for the texture
		D3DX_DEFAULT,          //image filter
		D3DX_DEFAULT,          //mip filter
		transcolor,            //color key for transparency
		&info,                 //bitmap file info (from loaded file)
		NULL,                  //color palette
		&texture );            //destination texture

	if (result != D3D_OK) 	
	{
		st_engine->LogError("Failed to load graphics file: FROM BUFFER",false,"ENGINE_ERRORS.txt");
		texture = NULL;
		return 0;
	}

	return 1;

}

As you can see from the load function, i couldnt get the loading of the D3DXIMAGE_INFO from memory working. im not sure why. so I tried it with the standard way and it worked. so atleast i know the graphic binary data load is working.

My question is, am i doing something obviously wrong because my load time is huge now. The ExtractBinaryData() takes ages and so does the actual load.

it worked great for one file, but then i tried your example of using 3000 items. now its takes ages.

Sorry i know that was a lot.

3000 textures OMG blink.png

I see you load whole file in memory, that is not good idea as you will run out of memory fast.

Read one in buffer fill texture then discard that buffer.

...i couldnt get the loading of the D3DXIMAGE_INFO from memory working...

You are doing it all wrong, "graphic_buffer" holds "file in memory" not "d3d_info_buffer" so:


result = D3DXGetImageInfoFromFileInMemory((LPCVOID)&graphic_buffer[0], graphic_buffer.size(), &info);

but you dont even need that since you read that info from file already. Code is a big mess now for quick solution.

It should be read and then discarded:


//temp_item.info_buffer.resize(sizeof(D3DXIMAGE_INFO)); // NO!
//is.read( (char*) &temp_item.info_buffer[0], sizeof(D3DXIMAGE_INFO) );// NO!
...
D3DXIMAGE_INFO info;
is.read( (char*) &info, sizeof(D3DXIMAGE_INFO) );
 
result = D3DXCreateTextureFromFileInMemoryEx(
        ...,...,,....,
        &info, 
        NULL, //color palette
        &texture ); 
 

Let me setup project and i will write whole code sample for you.

EDIT:

OK here is the test in attachment. I think that OS is doing some tricks behind back with file caching (or whatever) since i create 3000 textures from 1 file (as you do),

and reading from 1 binary is just 100ms faster then old "from file" method, although it took just ~1 second to load them all (Release mode), and i don't have SSD hdd.

I think it should be tested with different texture files so it doesn't have a chance to do funny bisnis and results to be valid for comparison.

Ok cheers for making that code. I ran it and I used one of my graphics. After some testing i think .tga is better than .png. They seem to load faster. Anyway, I made some changes to my code to reflect you feedback and now files are loading faster. I ran the 3000 test in your program and then I ran it it in mine and they were pretty much the same. Around 4 seconds to load 3000 textures which seems great.

but...

I added the time measuring between start and end of loading to my application and then tested the time it would take to load 3000 textures via the old method. Using the D3DXCreateTextureFromFileEx function and they wern't very far apart at all.

I will however start using this loading technique with my game and see if i start to see faster results.

Thanks again for the help, your a true legend.

Ill let you know how I get on.

I wrote a test program that created 500 textures. all the textures were mixed, so different file types and directories. I used the standard load from file method and then used the load from single binary file and measured the durations.

its still faster to use the load from file method. The binary file takes longer. Not really sure what im going to do now. might have to look into getting flash video into the game instead of using all the big animations. I started off tying to get flash in but i could get the transparency of a video to show through which meant it was hard to mix the videos in with the other game graphics.

I will keep trying tho.

Cheers mate.

Do I understand it right that you have each animation frame in a separate image file? Maybe you could try to put more frames to one image, into some kind of matrix. And then just change which part of the image is the sprite using, instead of changing which image is the sprite using. That could be overall faster.

This topic is closed to new replies.

Advertisement