• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
cozzie

Sorting out the bits

6 posts in this topic

Hi,

 

I'm trying to create 'bit keys' for my renderables, to be able to sort them in an order I want.

I got so far that I can create the keys and retrieve information from them, they're even sorted.

 

Unfortunately thery're sorted on 'mesh'.

Can someome try to help me out on how to sort them based on:

1 - matgroup, then

2 - material

3 - then mesh

4 - then distance

 

Any input is appreciated, here's the test app source.

#include <Windows.h>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>

typedef struct Q_RENDERABLE
{
	int	MatGroup;
	int	Material;
	int	Mesh;
	int	Instance;
	int	Id;		
	bool Visible;
	float DistToCamSqr;

	// testing
	int DistToCamConv;
} Q_RENDERABLE;


std::vector<Q_RENDERABLE> tRenderables;
std::vector<int> tIndexUnsorted;
std::vector<int> tIndexSorted;

std::vector<UINT64> tBitKeys;

bool Setup(const std::string pFilename);
bool CreateIndexUnsorted();
bool CreateBitKeys();
bool SortKeys();
bool OutputResults(const std::string pFilename);

bool loaded = false;
bool created = false;
bool bitKeysCreated = false;


int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{
	if(!Setup("testbucket.txt")) return 0;
	
	if(!CreateIndexUnsorted()) return 0;
	if(!CreateBitKeys()) return 0;
	if(!SortKeys()) return 0;

	if(!OutputResults("indices.txt")) return 0;

	return 1;
}


/*** LOAD THE TEST RENDERABLES'S DATA - ASSUMING ALL VISIBLE FOR TESTING ***/

bool Setup(const std::string pFilename)
{
	std::ifstream readFile(pFilename);
	if(!readFile) return false;

	Q_RENDERABLE tempRend;
	tRenderables.resize(0);

	char temp[255];
	for(int j=0;j<6;++j) readFile >> temp;

	bool done = false;
	while(!done)
	{
		readFile >> tempRend.Id;
		if(tempRend.Id == 9999) break;

		readFile >> tempRend.MatGroup;
		readFile >> tempRend.Material;
		readFile >> tempRend.Mesh;
		readFile >> tempRend.Instance;
		readFile >> tempRend.DistToCamSqr;
		tempRend.Visible = true;

		tRenderables.push_back(tempRend);
	}
	readFile.close();

	loaded = true;
	return true;
}

/*** ADD INDICIES OF ALL VISIBLE RENDERABLES TO 'UNSORTED' INDEX OF RENDERABLES = BUCKET ***/

bool CreateIndexUnsorted()
{
	if(!loaded) return false;
	
	tIndexUnsorted.resize(0);
	
	for(size_t rend=0;rend<tRenderables.size();++rend)
	{
		if(tRenderables[rend].Visible) tIndexUnsorted.push_back(rend);
	}
	created = true;
	return true;
}

bool CreateBitKeys()
{
	if(!loaded) return false;

	// USE A 64 BIT SORTING KEY: 44 BITS NEEDED FOR NOW
	
	// 4 bits: material group	(range 0 - 16)
	// 8 bits: materal ID		(range 0 - 255)
	// 8 bits: mesh ID			(range 0 - 255)
	// 16 bits: mesh inst ID	(range 0 - 2048)
	// 8 bits: dist to cam		(range 0 - 255)		=> needs conversion

	// convert dists to cam, testing (define max and then convert)
	float maxDist = tRenderables[0].DistToCamSqr;
	for(size_t rend=1;rend<tRenderables.size();++rend)
	{
		if(tRenderables[rend].DistToCamSqr > maxDist) maxDist = tRenderables[rend].DistToCamSqr;
	}
	// maxDist == 250
	for(size_t rend=0;rend<tRenderables.size();++rend) tRenderables[rend].DistToCamConv = (int((250 * tRenderables[rend].DistToCamSqr) / maxDist));

	// SET THE BITS AND CREATE THE SORTING KEYS
/*	00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
						  MGRP	 MAT	  MESH	   INST		INST	 DIST
	
	MGRP << 40;		4 bits
	MAT << 32;		8 bits
	MESH << 24;		8 bits
	INST << 8;		16 bits
	DIST << 0;		8 bits

		makeKey = ((0xFF & tr.MatGroup) << 40)	| ((0xFF & tr.Mesh) << 32)		| ((0xFF & tr.Instance) << 16)		| ((0xFF & tr.DistToCamConv) << 8)	| (0xFF & tr.Material);

*/

	for(size_t rend=0;rend<tIndexUnsorted.size();++rend)
	{
		auto &tr = tRenderables[tIndexUnsorted[rend]];
		UINT64 makeKey = 0;

		makeKey = ((0xFF & tr.MatGroup) << 40)	| ((0xFF & tr.Material) << 32)	| ((0xFF & tr.Mesh) << 24)			| ((0xFF & tr.Instance) << 8)		| (0xFF & tr.DistToCamConv);

		tBitKeys.push_back(makeKey);
	}
	bitKeysCreated = true;
	return true;
}

/*** SORT THE KEYS AND CREATE A NEW SORTED INDEX ***/

bool SortKeys()
{
	if(!bitKeysCreated) return false;

	std::vector<int> tSortIndexTemp;
	std::vector<UINT64> tSortKeysTemp;
	tSortIndexTemp.resize(tIndexUnsorted.size());

	for(size_t tIndex=0;tIndex<tIndexUnsorted.size();++tIndex) tSortIndexTemp[tIndex] = tIndex;

	std::sort(tSortIndexTemp.begin(), tSortIndexTemp.end(), [&](int a, int b)
	{ return tBitKeys[a] < tBitKeys[b]; } );

	tIndexSorted.resize(tIndexUnsorted.size());
	for(size_t sorted=0;sorted<tIndexUnsorted.size();++sorted)
		tIndexSorted[sorted] = tSortIndexTemp[tIndexUnsorted[sorted]];
	
	return true;
}

/*** SAVE ALL TEST RESULTS TO ASCII FILE ***/

bool OutputResults(const std::string pFilename)
{
	if(!loaded || !created) return false;

	std::ofstream oResult(pFilename);

	// UNSORTED VISIBLE INDEX

	oResult << "Unsorted (visible) index: " << std::endl;
	for(size_t rend=0;rend<tIndexUnsorted.size();++rend)
	{
		oResult << tIndexUnsorted[rend] << std::endl;
	}
	
	// TESTING BITKEYS STORING & RETRIEVING
	
	oResult << "Checking visible renderable 5" << std::endl;
	oResult << tRenderables[tIndexUnsorted[5]].MatGroup << ", ";
	oResult << tRenderables[tIndexUnsorted[5]].Material << ", ";
	oResult << tRenderables[tIndexUnsorted[5]].Mesh << ", ";
	oResult << tRenderables[tIndexUnsorted[5]].Instance << ", ";
	oResult << tRenderables[tIndexUnsorted[5]].DistToCamConv << std::endl;

	Q_RENDERABLE retrTest;

	retrTest.MatGroup = (tBitKeys[tIndexUnsorted[5]] >> 40  & 0xFF);
	retrTest.Material = (tBitKeys[tIndexUnsorted[5]] >> 32  & 0xFF);
	retrTest.Mesh = (tBitKeys[tIndexUnsorted[5]] >> 24  & 0xFF);
	retrTest.Instance = (tBitKeys[tIndexUnsorted[5]] >> 8  & 0xFF);
	retrTest.DistToCamConv = (tBitKeys[tIndexUnsorted[5]] & 0xFF);
	
	oResult << "Retrieved renderable 5: " << std::endl;
	oResult << retrTest.MatGroup << ", ";
	oResult << retrTest.Material << ", ";
	oResult << retrTest.Mesh << ", ";
	oResult << retrTest.Instance << ", ";
	oResult << retrTest.DistToCamConv << std::endl;

	oResult << std::endl;
	oResult << "Sorted renderables using bit keys:" << std::endl;
	oResult << "ID\tmatgrp\tmatl\tmesh\tinst\tdistsqr" << std::endl;

	for(size_t rend=0;rend<tIndexSorted.size();++rend)
	{
		oResult << tRenderables[tIndexSorted[rend]].Id << "\t";
		oResult << tRenderables[tIndexSorted[rend]].MatGroup << "\t";
		oResult << tRenderables[tIndexSorted[rend]].Material << "\t";
		oResult << tRenderables[tIndexSorted[rend]].Mesh << "\t";
		oResult << tRenderables[tIndexSorted[rend]].Instance << "\t";
		oResult << tRenderables[tIndexSorted[rend]].DistToCamSqr << std::endl;
	}
	oResult.close();

	return true;
}



(it's actually just one cpp and will compile if you try:))

0

Share this post


Link to post
Share on other sites

Wow, thanks! This is so cool, it fully works.

Below is the sorted result.

 

Can you give an example how I would do it by using <<=, I thought that was a bad idea, because for example an actual value of one of the integers, might be smaller then the number of reserved bits. Doing it like I do above/ now, makes sure the bits are always on the same starting place.

 

The dist to cam conv(erted) is actually also an int (converted to a range 0 - 255), so it will fit into 8 bits.

 

As I understand correctly it sorts from right to left, because most right bits are the material group, then material etc.

The only thing I don't understand yet is why it now sorts first on distance and then on inst (because the bits are the other way around).

 

Update: it doesn't, I just had a bad example. I'll play around switching inst and dist and see what happens :)

Sorted renderables using bit keys:
ID	matgrp	matl	mesh	inst	distsqr
2	0	0	7	44	1523.69
1	0	0	7	44	1928.28
0	0	0	7	44	2679.06
1	0	0	8	45	1526.07
2	0	0	8	45	1961.64
0	0	0	8	45	2678.64
5	0	1	1	30	641.774
0	0	1	1	30	863.876
1	0	2	1	28	297.844
7	0	2	1	28	298.979
3	0	2	1	28	302.922
3	0	2	1	30	681.976
1	0	2	1	30	715.996
7	0	2	1	30	710.025
2	0	3	1	28	324.312
2	0	3	1	30	784.915
0	0	9	13	14	26.7233
0	0	9	13	15	50.9233
0	0	9	13	16	26.5313
0	0	9	13	17	50.7313
1	0	10	13	14	31.211
1	0	10	13	15	56.941
1	0	10	13	16	31.019
1	0	10	13	17	56.749
2	0	11	13	14	26.546
2	0	11	13	15	50.746
2	0	11	13	16	27.884
2	0	11	13	17	52.084
3	0	12	13	14	23.4845
3	0	12	13	15	46.1545
3	0	12	13	16	23.2925
3	0	12	13	17	45.9625
4	0	13	13	14	28.1495
4	0	13	13	15	52.3495
4	0	13	13	16	26.4275
4	0	13	13	17	50.6275
0	0	19	17	10	632.05
0	0	24	21	3	244.611
1	0	24	21	3	241.486
2	0	25	21	3	257.666
0	1	14	14	1	0
0	1	14	14	12	24.6981
1	1	15	14	1	0
1	1	15	14	12	25.8981
2	1	16	14	1	0
2	1	16	14	12	25.8144
19	1	21	19	5	210.195
3	1	21	19	5	220.368
4	1	21	19	5	219.088
5	1	21	19	5	240.494
9	1	21	19	5	242.705
12	1	21	19	5	237.035
1	1	21	19	5	251.263
17	1	21	19	5	248.869
18	1	21	19	5	266.779
16	1	21	19	5	274.828
2	1	21	19	5	274.758
23	1	21	19	5	286.909
0	1	21	19	5	279.369
20	1	21	19	5	281.241
21	1	21	19	5	289.241
25	1	21	19	5	299.007
22	1	21	19	5	296.312
24	1	21	19	5	301.771
11	1	21	19	5	313.179
15	1	21	19	5	316.496
6	1	21	19	5	327.245
7	1	21	19	5	325.97
8	1	21	19	5	347.619
10	1	21	19	5	349.88
14	1	21	19	5	357.944
13	1	21	19	5	371.248
9	1	21	19	6	333.87
5	1	21	19	6	339.316
2	1	21	19	6	352.877
16	1	21	19	6	348.235
19	1	21	19	6	361.184
12	1	21	19	6	371.141
18	1	21	19	6	391.29
3	1	21	19	6	406.877
8	1	21	19	6	405.05
0	1	21	19	6	399.231
10	1	21	19	6	399.653
20	1	21	19	6	401.6
23	1	21	19	6	401.844
4	1	21	19	6	413.153
21	1	21	19	6	408.107
22	1	21	19	6	413.031
24	1	21	19	6	415.278
25	1	21	19	6	414.707
15	1	21	19	6	424.459
11	1	21	19	6	423.048
13	1	21	19	6	439.107
17	1	21	19	6	434.9
1	1	21	19	6	431.338
14	1	21	19	6	478.828
6	1	21	19	6	472.361
7	1	21	19	6	478.642
8	1	35	25	54	319.827
4	2	4	1	28	307.071
6	2	4	1	28	307.903
4	2	4	1	30	692.514
6	2	4	1	30	691.557
3	2	7	4	18	648.601
0	2	7	4	18	731.78
5	2	7	4	18	852.965
4	2	7	4	18	977.865
2	2	7	4	24	807.783
4	2	7	4	24	1048.26
5	2	7	4	24	1195.96
1	2	27	22	2	25.9537
0	3	6	3	39	458.656
0	3	6	3	40	505.257
0	3	6	3	41	541.335
0	3	8	9	46	660.565
0	3	8	10	47	698.492
0	3	8	11	48	160.596
0	3	8	12	49	131.051
0	3	17	15	0	0
1	3	17	15	0	0
2	3	17	15	0	0
0	3	17	15	13	50.8262
1	3	17	15	13	48.7949
2	3	17	15	13	51.9707
0	3	22	20	4	804.828
1	3	23	20	4	879.321
0	3	26	22	2	25.941
0	3	30	25	54	323.753
1	3	30	25	54	323.794
2	3	30	25	54	321.517
3	3	31	25	54	328
4	3	32	25	54	322.811
5	3	32	25	54	323.494
6	3	33	25	54	321.439
10	3	33	25	54	321.502
7	3	34	25	54	320.425
9	3	36	25	54	320.352

Edited by cozzie
0

Share this post


Link to post
Share on other sites
// 4 bits: material group
UINT64 makeKey = tr.MatGroup;
// 8 bits: materal ID
makeKey <<= 8; //push it over by 8 bits to make room
makeKey |= tr.Material & 0xFF;
// 8 bits: mesh ID
makeKey <<= 8; //push it over by 8 bits to make room
makeKey |= tr.Mesh & 0xFF;
// 16 bits: mesh inst ID
makeKey <<= 16; //push it over by 16 bits to make room
makeKey |= tr.Instance & 0xFFFF;
// 8 bits: dist to cam
makeKey <<= 8; //push it over by 8 bits to make room
makeKey |= (char)tr.DistToCamConv; //float is usually 32bit, so casting to char will give you an 8-bit value...
2

Share this post


Link to post
Share on other sites

Thanks, I switched dist and inst, because I first want to sort on dist (distance from camera).

For now:

		makeKey |= (UINT64)tr.MatGroup		<< 60;
		makeKey |= (UINT64)tr.Material		<< 52;
		makeKey |= (UINT64)tr.Mesh			<< 44;
		makeKey |= (UINT64)tr.DistToCamSqr	<< 28;
		makeKey |= (UINT64)tr.Instance;

Results:

ID	matgrp	matl	mesh	inst	distsqr
2	0	0	7	1523.69	44
1	0	0	7	1928.28	44
0	0	0	7	2679.06	44
1	0	0	8	1526.07	45
2	0	0	8	1961.64	45
0	0	0	8	2678.64	45
5	0	1	1	641.774	30
0	0	1	1	863.876	30
1	0	2	1	297.844	28
7	0	2	1	298.979	28
3	0	2	1	302.922	28
3	0	2	1	681.976	30
7	0	2	1	710.025	30
1	0	2	1	715.996	30
2	0	3	1	324.312	28
2	0	3	1	784.915	30
0	0	9	13	26.7233	14
0	0	9	13	26.5313	16
0	0	9	13	50.9233	15
0	0	9	13	50.7313	17
1	0	10	13	31.211	14
1	0	10	13	31.019	16
1	0	10	13	56.941	15
1	0	10	13	56.749	17
2	0	11	13	26.546	14
2	0	11	13	27.884	16
2	0	11	13	50.746	15
2	0	11	13	52.084	17
3	0	12	13	23.4845	14
3	0	12	13	23.2925	16
3	0	12	13	45.9625	17
3	0	12	13	46.1545	15
4	0	13	13	26.4275	16
4	0	13	13	28.1495	14
4	0	13	13	50.6275	17
4	0	13	13	52.3495	15
0	0	19	17	632.05	10
1	0	24	21	241.486	3
0	0	24	21	244.611	3
2	0	25	21	257.666	3
0	1	14	14	0	1
0	1	14	14	24.6981	12
1	1	15	14	0	1
1	1	15	14	25.8981	12
2	1	16	14	0	1
2	1	16	14	25.8144	12
19	1	21	19	210.195	5
4	1	21	19	219.088	5
3	1	21	19	220.368	5
12	1	21	19	237.035	5
5	1	21	19	240.494	5
9	1	21	19	242.705	5
17	1	21	19	248.869	5
1	1	21	19	251.263	5
18	1	21	19	266.779	5
16	1	21	19	274.828	5
2	1	21	19	274.758	5
0	1	21	19	279.369	5
20	1	21	19	281.241	5
23	1	21	19	286.909	5
21	1	21	19	289.241	5
22	1	21	19	296.312	5
25	1	21	19	299.007	5
24	1	21	19	301.771	5
11	1	21	19	313.179	5
15	1	21	19	316.496	5
7	1	21	19	325.97	5
6	1	21	19	327.245	5
9	1	21	19	333.87	6
5	1	21	19	339.316	6
8	1	21	19	347.619	5
16	1	21	19	348.235	6
10	1	21	19	349.88	5
2	1	21	19	352.877	6
14	1	21	19	357.944	5
19	1	21	19	361.184	6
13	1	21	19	371.248	5
12	1	21	19	371.141	6
18	1	21	19	391.29	6
0	1	21	19	399.231	6
10	1	21	19	399.653	6
20	1	21	19	401.6	6
23	1	21	19	401.844	6
8	1	21	19	405.05	6
3	1	21	19	406.877	6
21	1	21	19	408.107	6
4	1	21	19	413.153	6
22	1	21	19	413.031	6
25	1	21	19	414.707	6
24	1	21	19	415.278	6
11	1	21	19	423.048	6
15	1	21	19	424.459	6
1	1	21	19	431.338	6
17	1	21	19	434.9	6
13	1	21	19	439.107	6
6	1	21	19	472.361	6
14	1	21	19	478.828	6
7	1	21	19	478.642	6
8	1	35	25	319.827	54
4	2	4	1	307.071	28
6	2	4	1	307.903	28
6	2	4	1	691.557	30
4	2	4	1	692.514	30
3	2	7	4	648.601	18
0	2	7	4	731.78	18
2	2	7	4	807.783	24
5	2	7	4	852.965	18
4	2	7	4	977.865	18
4	2	7	4	1048.26	24
5	2	7	4	1195.96	24
1	2	27	22	25.9537	2
0	3	6	3	458.656	39
0	3	6	3	505.257	40
0	3	6	3	541.335	41
0	3	8	9	660.565	46
0	3	8	10	698.492	47
0	3	8	11	160.596	48
0	3	8	12	131.051	49
0	3	17	15	0	0
1	3	17	15	0	0
2	3	17	15	0	0
1	3	17	15	48.7949	13
0	3	17	15	50.8262	13
2	3	17	15	51.9707	13
0	3	22	20	804.828	4
1	3	23	20	879.321	4
0	3	26	22	25.941	2
2	3	30	25	321.517	54
0	3	30	25	323.753	54
1	3	30	25	323.794	54
3	3	31	25	328	54
4	3	32	25	322.811	54
5	3	32	25	323.494	54
6	3	33	25	321.439	54
10	3	33	25	321.502	54
7	3	34	25	320.425	54
9	3	36	25	320.352	54

1

Share this post


Link to post
Share on other sites

As an aside, have you thought about using a union?

 

something along the lines of:

 

union {

  struct MaterialGroup {

   char MatGroup

   char Material

  char Mesh

 char DistToCamSqr

char Instance

etc, whatever gets you up to using a 64 bit value

}

UINT64 packedValue;

 

}

 

EDIT:  Oh, i was lazy, i see some stuff is different sizes than char, in which case you want something like int MaterialGroup:2 or what not as the size requires.

Edited by ferrous
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0