This beauty...

Started by
16 comments, last by wodinoneeye 10 years, 6 months ago

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--;)
Advertisement


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

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

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

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!

That's the most broken implementation of microcode I've ever seen o_____O

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
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.

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

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.

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

--------------------------------------------[size="1"]Ratings are Opinion, not Fact

This topic is closed to new replies.

Advertisement