Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actuall0calh05t

Posted 01 October 2013 - 09:10 AM

 

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

#2l0calh05t

Posted 24 September 2013 - 06:05 AM

 

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

#1l0calh05t

Posted 24 September 2013 - 06:04 AM

 

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

for(unsigned j = 8; j-- != 0;)

PARTNERS