Buffer for TEXTURE_FORMAT_RGBA16F texture

When i make buffer for
TEXTURE_FORMAT_RGBA32F i use this code.

How can i make buffer for
TEXTURE_FORMAT_RGBA16F ?

local tparams = {
		width = width,
		height = height,
		type = resource.TEXTURE_TYPE_2D,
		format = resource.TEXTURE_FORMAT_RGBA32F,
	}
	local tbuffer = buffer.create(tparams.width * tparams.height, { { name = hash("rgba"), type = buffer.VALUE_TYPE_FLOAT32, count = 4 } })

Good question. It’s easy to forget about it throughout.
We don’t have native support for half precision floating point, but we should ofc make it possible to produce such content.

Currently, I think the best bet is to use buffer.VALUE_TYPE_UINT16, to at least get the correct size, and hopefully rely on the that the rest of the functions can handle it. (We haven’t tried this ourselves yet)

3 Likes

It worked with VALUE_TYPE_UINT16. Thanks.
I take convert float to half float code from here.

float half_to_float(const uint16_t x) { // IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
    const uint32_t e = (x&0x7C00)>>10; // exponent
    const uint32_t m = (x&0x03FF)<<13; // mantissa
    const uint32_t v = as_uint((float)m)>>23; // evil log2 bit hack to count leading zeros in denormalized format
    return as_float((x&0x8000)<<16 | (e!=0)*((e+112)<<23|m) | ((e==0)&(m!=0))*((v-37)<<23|((m<<(150-v))&0x007FE000))); // sign : normalized : denormalized
}
uint16_t float_to_half(const float x) { // IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
    const uint32_t b = as_uint(x)+0x00001000; // round-to-nearest-even: add last bit after truncated mantissa
    const uint32_t e = (b&0x7F800000)>>23; // exponent
    const uint32_t m = b&0x007FFFFF; // mantissa; in line below: 0x007FF000 = 0x00800000-0x00001000 = decimal indicator flag - initial rounding
    return (b&0x80000000)>>16 | (e>112)*((((e-112)<<10)&0x7C00)|m>>13) | ((e<113)&(e>101))*((((0x007FF000+m)>>(125-e))+1)>>1) | (e>143)*0x7FFF; // sign : normalized : denormalized : saturate
}
4 Likes