Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (775)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  16-bit float conversion Java code?  (Read 3550 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2015-09-30 16:31:34 »

Hey. I'm interested in improving the packing of my vertex data to reduce its size further. In some cases I have stuff like HDR colors that don't necessarily need 32 bits of precision. 16-bit half-floats would work just as well and save quite a bit of precision. The problem is converting 32-bit float values to 16-bit values on the Java side. My Google-fu tells me that this is more complicated than I expected if I'm going to handle special values like infinities, NaN and denormals. Has anyone already implemented this perhaps?

Myomyomyo.
Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #1 - Posted 2015-09-30 17:11:11 »

yes i made a port of a nvidia gameworks sample (https://github.com/NVIDIAGameWorks/OpenGLSamples/blob/master/samples/es3aep-kepler/HDR/HDRImages.cpp).

http://pastebin.java-gaming.org/dafa48a6f331b

works perfect for me.
HFloat.convertFloatToHFloat(value)
, short output into a buffer and a
GL30.GL_HALF_FLOAT
glVertexAttribPointer for instance works as you'd expect it.

i tried the simpler half conversion like

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
@Deprecated @SuppressWarnings ( "FloatingPointEquality" )
public static short toHalfFloat(final float v)
{
  if(Float.isNaN(v)) throw new UnsupportedOperationException("NaN to half conversion not supported!");
  if(v == Float.POSITIVE_INFINITY) return(short)0x7c00;
  if(v == Float.NEGATIVE_INFINITY) return(short)0xfc00;
  if(v == 0.0f) return(short)0x0000;
  if(v == -0.0f) return(short)0x8000;
  if(v > 65504.0f) return 0x7bff;  // max value supported by half float
  if(v < -65504.0f) return(short)( 0x7bff | 0x8000 );
  if(v > 0.0f && v < 5.96046E-8f) return 0x0001;
  if(v < 0.0f && v > -5.96046E-8f) return(short)0x8001;

  final int f = Float.floatToIntBits(v);

  return(short)((( f>>16 ) & 0x8000 ) | (((( f & 0x7f800000 ) - 0x38000000 )>>13 ) & 0x7c00 ) | (( f>>13 ) & 0x03ff ));
}


and

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
@Deprecated @SuppressWarnings ( "UnnecessaryExplicitNumericCast" )
public static float toFloat(final short half)
{
  switch((int)half)
  {
    case 0x0000 :
      return 0.0f;
    case 0x8000 :
      return -0.0f;
    case 0x7c00 :
      return Float.POSITIVE_INFINITY;
    case 0xfc00 :
      return Float.NEGATIVE_INFINITY;
    // @TODO: support for NaN ?
    default :
      return Float.intBitsToFloat((( half & 0x8000 )<<16 ) | ((( half & 0x7c00 ) + 0x1C000 )<<13 ) | (( half & 0x03FF )<<13 ));
  }
}


but that's not how opengl likes it.
Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #2 - Posted 2015-10-01 18:52:16 »

just ran into https://github.com/g-truc/glm/blob/master/glm/detail/type_half.inl which looks pretty similar.
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (13 views)
2018-11-16 13:46:03

hadezbladez (18 views)
2018-11-16 13:41:33

hadezbladez (6 views)
2018-11-16 13:35:35

hadezbladez (6 views)
2018-11-16 13:32:03

EgonOlsen (1880 views)
2018-06-10 19:43:48

EgonOlsen (1904 views)
2018-06-10 19:43:44

EgonOlsen (1261 views)
2018-06-10 19:43:20

DesertCoockie (1706 views)
2018-05-13 18:23:11

nelsongames (1392 views)
2018-04-24 18:15:36

nelsongames (2007 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!