• 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
Migi0027

This beauty...

17 posts in this topic

Just saw this beauty from some example code:

DXT3AlphaBlock compressDXT3Alpha(vec4 colors[16])
{
	DXT3AlphaBlock dxt3Alpha;

	unsigned int quantizedAlpha0 = quantize4(int(colors[0].w * 255.0 + .5));
	unsigned int quantizedAlpha1 = quantize4(int(colors[1].w * 255.0 + .5));
	unsigned int quantizedAlpha2 = quantize4(int(colors[2].w * 255.0 + .5));
	unsigned int quantizedAlpha3 = quantize4(int(colors[3].w * 255.0 + .5));
	unsigned int quantizedAlpha4 = quantize4(int(colors[4].w * 255.0 + .5));
	unsigned int quantizedAlpha5 = quantize4(int(colors[5].w * 255.0 + .5));
	unsigned int quantizedAlpha6 = quantize4(int(colors[6].w * 255.0 + .5));
	unsigned int quantizedAlpha7 = quantize4(int(colors[7].w * 255.0 + .5));
	dxt3Alpha.alphas[0] =
		quantizedAlpha0 << unsigned int(0) |
		quantizedAlpha1 << unsigned int(4) |
		quantizedAlpha2 << unsigned int(8) |
		quantizedAlpha3 << unsigned int(12) |
		quantizedAlpha4 << unsigned int(16) |
		quantizedAlpha5 << unsigned int(20) |
		quantizedAlpha6 << unsigned int(24) |
		quantizedAlpha7 << unsigned int(28);
	
	unsigned int quantizedAlpha8 = quantize4(int(colors[8].w * 255.0 + .5));
	unsigned int quantizedAlpha9 = quantize4(int(colors[9].w * 255.0 + .5));
	unsigned int quantizedAlpha10 = quantize4(int(colors[10].w * 255.0 + .5));
	unsigned int quantizedAlpha11 = quantize4(int(colors[11].w * 255.0 + .5));
	unsigned int quantizedAlpha12 = quantize4(int(colors[12].w * 255.0 + .5));
	unsigned int quantizedAlpha13 = quantize4(int(colors[13].w * 255.0 + .5));
	unsigned int quantizedAlpha14 = quantize4(int(colors[14].w * 255.0 + .5));
	unsigned int quantizedAlpha15 = quantize4(int(colors[15].w * 255.0 + .5));
	dxt3Alpha.alphas[1] =
		quantizedAlpha8 << unsigned int(0) |
		quantizedAlpha9 << unsigned int(4) |
		quantizedAlpha10 << unsigned int(8) |
		quantizedAlpha11 << unsigned int(12) |
		quantizedAlpha12 << unsigned int(16) |
		quantizedAlpha13 << unsigned int(20) |
		quantizedAlpha14 << unsigned int(24) |
		quantizedAlpha15 << unsigned int(28);
	
	return dxt3Alpha;
}

What do you think?

-MIGI0027

2

Share this post


Link to post
Share on other sites

Not that bad. Probably about the same amount of time to write either way. More thought in a loop, more copypasta and speed typing in the unrolled version. Loop is quicker to edit later, but it doesn't look like the kind of thing that would ever need editing. I think it's more readable in the unrolled form.

1

Share this post


Link to post
Share on other sites

Not that bad. Probably about the same amount of time to write either way. More thought in a loop, more copypasta and speed typing in the unrolled version. Loop is quicker to edit later, but it doesn't look like the kind of thing that would ever need editing. I think it's more readable in the unrolled form.

 

I agree, imo loop version is probably somewhat better for me but this is okay too

0

Share this post


Link to post
Share on other sites

 

I actually find that the original version is more easily understood than doing it with loops tongue.png

DXT3AlphaBlock compressDXT3Alpha(vec4 colors[16])
{
	DXT3AlphaBlock dxt3Alpha;

	for( int j=0; j!=2; ++j )
	{
		unsigned int result = 0;
		for( int i=7; i>=0; --i )
		{
			unsigned int quantized = quantize4(int(colors[i+j*8].w * 255.0 + .5));
			result = result<<2 | quantized;
		}
		dxt3Alpha.alphas[j] = result;
	}

	return dxt3Alpha;
}

Shouldn't that result be shifted by 4, not 2? Also, why not do a forward loop and shift quantized instead of result?

for(unsigned j = 0; j < 8; ++j)
{
	unsigned int quantized = quantize4(int(colors[i+j*8].w * 255.0 + .5));
	result |= quantized << (4 * j);
}

Most people would consider it more readable and the multiply will be optimized away during unrolling anyways. Personally I'd also write the reverse loop as (should mention though that some compilers don't manage to unroll this, but the specific compiler I'm thinking of is unable to unroll both types of reverse loop, so...)

for(unsigned j = 8; j-- != 0;) // or even for(unsigned j = 8; j--;)
Edited by l0calh05t
1

Share this post


Link to post
Share on other sites


Shouldn't that result be shifted by 4, not 2? Also, why not do a forward loop and shift quantized instead of result?
Yep, typo. I of course didn't test that code biggrin.png

 

I wrote the shifting logic that way because I've been burnt by a micro-coded shift-by-variable instruction, where shifting by a constant takes 1 cycle, but shifting by a variable breaks down into a little for loop that shifts by 1 n times, over n*k cycles... I can't actually remember whether this is the case on modern PC CPUs or not tongue.png

0

Share this post


Link to post
Share on other sites

I really wonder which CPU did that to you because I can't think of any like that (either you could only shift by 1, or you could shift multiple bits either by a constant or by a register).

 

Unless you mean the microcode in CPUs... They used to have a barrel shifter that did any bit shift in 1 cycle (especially useful for indirect addressing modes since the index could be bit shifted), but I think Intel removed that at some point. No idea what's the current situation right now (but even then it's still the fastest method).

0

Share this post


Link to post
Share on other sites

I really wonder which CPU did that to you because I can't think of any like that.
Unless you mean the microcode in CPUs...

Yeah. The PPC's used in the PS3/360 have a shift-by-variable instruction, but it's microcoded. When the CPU hits one of them, it flushes the CPU pipeline, fetches the algorithm from ROM and runs it (while locking down shared CPU resources - e.g. dual issuing disabled), pretending that it was just a single instruction, before resuming normal operations.
 
It's nice that the new consoles are moving over to x86-land now and ditching these simple CPUs... though I do have a soft-spot for the SPE's; async memcpy ftw!

Edited by Hodgman
2

Share this post


Link to post
Share on other sites
What do you think?

 

The reason why it's unrolled is because it's speed critical (considering this is code for graphics processing).

 

What the person probably didn't know is that the compiler's optimizer will perform loop unrolls on short for-loops anyway, so Hodgman's solution will produce the same result as the code posted by the OP when looking at the disassembly.

 

In the worst of cases, you could use C++ templates to unroll loops, but I wouldn't encourage it.

0

Share this post


Link to post
Share on other sites

 

What do you think?

 

The reason why it's unrolled is because it's speed critical (considering this is code for graphics processing).

 

What the person probably didn't know is that the compiler's optimizer will perform loop unrolls on short for-loops anyway, so Hodgman's solution will produce the same result as the code posted by the OP when looking at the disassembly.

 

In the worst of cases, you could use C++ templates to unroll loops, but I wouldn't encourage it.

 

 

manual unrolling can make a bit more of a difference though if often compiling code in debug mode and still needing good performance, which can still happen sometimes (leading, sometimes, to cases where manual unrolling is justified).

 

but, then again, I am left to suspect in the above that mixing floating point and integer math could potentially be a bigger performance impact than would be whether or not the loop is unrolled.

2

Share this post


Link to post
Share on other sites

"I wrote the shifting logic that way because I've been burnt by a micro-coded shift-by-variable instruction, where shifting by a constant takes 1 cycle, but shifting by a variable breaks down into a little for loop that shifts by 1 n times, over n*k cycles... I can't actually remember whether this is the case on modern PC CPUs or not tongue.png

"

 

But why all the bulking up the cose with  unsigned int(4)   unsigned int(8)   when  4 , 8 ...     is all you need  The shift only takes ints so you could unclutter it a bit at least for that

 

 

Also since you own this function couldnt you shorten the variable names  (its really fairly obvious what it does and is quite repetative and a simple comment would tell any less knowlegable person what it does

 

 

also  what is  the guts of   quantize4()   and could you imbed it even further to chop out extraneous operations ???

A table lookup for a non linear conversion function with a byte domain  (feeding it the float to int calc directly as subscript)  ???

or mutate the inline quantize4()   function  with the    * 255.0 + .5  imbedded inside it return an unsigned int from it and imbed the whole call right into the  shift equation sequence    (probably need to speed testcompare  it to see if any such condensations  make any diference besides looking cleaner  .... getting rid of the intermediary variables....)

 

heh,  you could also reuse  the 0 -7 qA variables for the second set 8 -15

 

 

 

something less bulky like

DXT3AlphaBlock compressDXT3Alpha(vec4 colors[16])
{  
unsigned int    qA0,qA1,qA2,QA3,qA4,qA5,qA6,qA7;
DXT3AlphaBlock  dxt3Alpha;

qA0 = quantize4(int(colors[0].w * 255.0 + .5));
qA1 = quantize4(int(colors[1].w * 255.0 + .5));
qA2 = quantize4(int(colors[2].w * 255.0 + .5));
qA3 = quantize4(int(colors[3].w * 255.0 + .5));
qA4 = quantize4(int(colors[4].w * 255.0 + .5));
qA5 = quantize4(int(colors[5].w * 255.0 + .5));
qA6 = quantize4(int(colors[6].w * 255.0 + .5));
qA7 = quantize4(int(colors[7].w * 255.0 + .5));

dxt3Alpha.alphas[0] =
	qA0 << 0  |
	qA1 << 4  |
	qA2 << 8  |
	qA3 << 12 |
	qA4 << 16 |
	qA5 << 20 |
	qA6 << 24 |
	qA7 << 28;
	
qA0 = quantize4(int(colors[8].w * 255.0 + .5));
qA1 = quantize4(int(colors[9].w * 255.0 + .5));
qA2 = quantize4(int(colors[10].w * 255.0 + .5));
qA3 = quantize4(int(colors[11].w * 255.0 + .5));
qA4 = quantize4(int(colors[12].w * 255.0 + .5));
qA5 = quantize4(int(colors[13].w * 255.0 + .5));
qA6 = quantize4(int(colors[14].w * 255.0 + .5));
qA7 = quantize4(int(colors[15].w * 255.0 + .5));

dxt3Alpha.alphas[1] =
	qA0 << 0  |
	qA1 << 4  |
	qA2 << 8  |
	qA3 << 12 |
	qA4 << 16 |
	qA5 << 20 |
	qA6 << 24 |
	qA7 << 28;
	
return dxt3Alpha;
}

This is a function taht looks like it will be crunching alot of bulk data for texture conversion  so  doing such (and similar)  optimization could add up for the actuual programs

 

 

 

---

DXT3AlphaBlock compressDXT3Alpha(vec4 colors[16])
{  

DXT3AlphaBlock  dxt3Alpha;

dxt3Alpha.alphas[0] =
  quantize4(int(colors[0].w * 255.0 + .5)) << 0  |
  quantize4(int(colors[1].w * 255.0 + .5)) << 4  |
  quantize4(int(colors[2].w * 255.0 + .5)) << 8  |
  quantize4(int(colors[3].w * 255.0 + .5)) << 12 |
  quantize4(int(colors[4].w * 255.0 + .5)) << 16 |
  quantize4(int(colors[5].w * 255.0 + .5)) << 20 |
  quantize4(int(colors[6].w * 255.0 + .5)) << 24 |
  quantize4(int(colors[7].w * 255.0 + .5)) << 28;

dxt3Alpha.alphas[1] =
  quantize4(int(colors[8].w * 255.0 + .5)) << 0  |
  quantize4(int(colors[9].w * 255.0 + .5)) << 4  |
  quantize4(int(colors[10].w * 255.0 + .5)) << 8  |
  quantize4(int(colors[11].w * 255.0 + .5)) << 12 |
  quantize4(int(colors[12].w * 255.0 + .5)) << 16 |
  quantize4(int(colors[13].w * 255.0 + .5)) << 20 |
  quantize4(int(colors[14].w * 255.0 + .5)) << 24 |
  quantize4(int(colors[15].w * 255.0 + .5)) << 28;
	
return dxt3Alpha;
}

wrapper of    ((unsigned int)  quantize4(...))       possibly needed  if the  shift is wonky

 

 

pointer math on the      colors[].w       Float ptr  with      ptr += 4   to eliminate the .array index multiply ???

 

 

etc....

Edited by wodinoneeye
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