Heightfield collision with 16-bit floats

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Heightfield collision with 16-bit floats

Postby JoshKlint » Sun Jul 02, 2023 12:42 pm

I was able to add "half" 16-bit floating point support for the heightfield collision in Newton 3 without much trouble:
https://github.com/mitsuba-renderer/ope ... f/half.cpp
dgCollisionHeightField.zip
(9.59 KiB) Downloaded 279 times


You also need to make this change in dgNarrowPhaseCollision:
Code: Select all
dgCollisionInstance* dgWorld::CreateHeightField(
   dgInt32 width, dgInt32 height, dgInt32 contructionMode, dgInt32 elevationDataType,
   const void* const elevationMap, const dgInt8* const atributeMap,
   dgFloat32 verticalScale, dgFloat32 horizontalScale_x, dgFloat32 horizontalScale_z)
{
   dgCollision* const collision = new  (m_allocator) dgCollisionHeightField (this, width, height, contructionMode, elevationMap,
                                                           //elevationDataType   ? dgCollisionHeightField::m_unsigned16Bit : dgCollisionHeightField::m_float32Bit,   
      dgCollisionHeightField::dgElevationType(elevationDataType),
                                                            verticalScale, atributeMap, horizontalScale_x, horizontalScale_z);
   dgCollisionInstance* const instance = CreateInstance (collision, 0, dgGetIdentityMatrix());
   collision->Release();
   return instance;
}


I like this format because it's compressed but doesn't have any hard-coded constraints for the min and max, which makes it more flexible for terrain. So it takes the same space as an unsigned 16-bit integer, but acts like a 32-bit float.

A lookup table is used when the half is converted back to a float, so it seems very fast.
JoshKlint
 
Posts: 163
Joined: Sun Dec 10, 2017 8:03 pm

Re: Heightfield collision with 16-bit floats

Postby Julio Jerez » Mon Jul 03, 2023 1:12 pm

you can just use the google bfloat16 which is identical to the IEEE float32 but truncate the mantiza to 7 bit instead of 23

https://en.wikipedia.org/wiki/Bfloat16_ ... t%20Google.

do not need lookup table to convert to in an out of 32 bit float
just do

Code: Select all
unsigned short ToFloat16 (float value)
{
   union Float32-16
  {
     unsigned short float16;
     float float32;
  } temp;
  temp.float32 = value;
  return temp.float16;
}

float ToFloat32 (unsigned short)
{
   union Float32-16
  {
     unsigned short float16;
     float float32;
  } temp;
  temp.float32 = 0;
  temp.float16 = short
  return temp.float32;
}
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Heightfield collision with 16-bit floats

Postby JoshKlint » Tue Jul 04, 2023 1:24 pm

Are those the same type as GPU half-floats (VK_FORMAT_R16_SFLOAT)? I am using the data for both physics and rendering.
JoshKlint
 
Posts: 163
Joined: Sun Dec 10, 2017 8:03 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 37 guests