Sign in to follow this  
Migi0027

This beauty...

Recommended Posts

Migi0027    4628

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

Share this post


Link to post
Share on other sites
Hodgman    51220

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;
}

Share this post


Link to post
Share on other sites
DekuTree64    1168

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.

Share this post


Link to post
Share on other sites
fir    460

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

Share this post


Link to post
Share on other sites
l0calh05t    1796

 

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

Share this post


Link to post
Share on other sites
Hodgman    51220


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

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).

Share this post


Link to post
Share on other sites
Hodgman    51220

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

Share this post


Link to post
Share on other sites
TheComet    3896
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.

Share this post


Link to post
Share on other sites
cr88192    1570

 

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.

Share this post


Link to post
Share on other sites
wodinoneeye    1689

"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

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