From 0b049d489772432c6c49e5fc238b227cc1d12626 Mon Sep 17 00:00:00 2001 From: pheonix Date: Sat, 24 Feb 2024 15:47:04 -0800 Subject: [PATCH] Use bx::bit_cast where appropriate for type punning, applying packed struct for arrays when necessary (#3212) --- examples/09-hdr/hdr.cpp | 7 ++++--- examples/33-pom/pom.cpp | 28 +++++++--------------------- src/bgfx_p.h | 7 ------- src/shader_dxbc.cpp | 3 +-- 4 files changed, 12 insertions(+), 33 deletions(-) diff --git a/examples/09-hdr/hdr.cpp b/examples/09-hdr/hdr.cpp index 7d4c82c48d..52a29a0f66 100644 --- a/examples/09-hdr/hdr.cpp +++ b/examples/09-hdr/hdr.cpp @@ -362,9 +362,10 @@ class ExampleHDR : public entry::AppI if (bgfx::isValid(m_rb) ) { - union { uint32_t color; uint8_t bgra[4]; } cast = { m_lumBgra8 }; - float exponent = cast.bgra[3]/255.0f * 255.0f - 128.0f; - float lumAvg = cast.bgra[2]/255.0f * bx::exp2(exponent); + struct Packed { uint8_t bgra[4]; } arr = bx::bit_cast(m_lumBgra8); + float exponent = arr.bgra[3] / 255.0f * 255.0f - 128.0f; + float lumAvg = arr.bgra[2] / 255.0f * bx::exp2(exponent); + ImGui::SliderFloat("Lum Avg", &lumAvg, 0.0f, 1.0f); } diff --git a/examples/33-pom/pom.cpp b/examples/33-pom/pom.cpp index f55aa6df73..f4a810ace3 100644 --- a/examples/33-pom/pom.cpp +++ b/examples/33-pom/pom.cpp @@ -37,29 +37,15 @@ struct PosTangentBitangentTexcoordVertex bgfx::VertexLayout PosTangentBitangentTexcoordVertex::ms_layout; -uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w) -{ - union - { - uint32_t ui32; - uint8_t arr[4]; - } un; - - un.arr[0] = _x; - un.arr[1] = _y; - un.arr[2] = _z; - un.arr[3] = _w; - - return un.ui32; -} - uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f) { - const uint8_t xx = uint8_t(_x*127.0f + 128.0f); - const uint8_t yy = uint8_t(_y*127.0f + 128.0f); - const uint8_t zz = uint8_t(_z*127.0f + 128.0f); - const uint8_t ww = uint8_t(_w*127.0f + 128.0f); - return packUint32(xx, yy, zz, ww); + struct Packed { uint8_t value[4]; } arr = { 0 }; + arr.value[0] = uint8_t(_x * 127.0f + 128.0f); + arr.value[1] = uint8_t(_y * 127.0f + 128.0f); + arr.value[2] = uint8_t(_z * 127.0f + 128.0f); + arr.value[3] = uint8_t(_w * 127.0f + 128.0f); + + return bx::bit_cast(arr); } static PosTangentBitangentTexcoordVertex s_cubeVertices[24] = diff --git a/src/bgfx_p.h b/src/bgfx_p.h index b903413c6e..9ca1912ecf 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -605,13 +605,6 @@ namespace bgfx release( (const Memory*)_mem); } - inline uint32_t castfu(float _value) - { - union { float fl; uint32_t ui; } un; - un.fl = _value; - return un.ui; - } - inline uint64_t packStencil(uint32_t _fstencil, uint32_t _bstencil) { return (uint64_t(_bstencil)<<32)|uint64_t(_fstencil); diff --git a/src/shader_dxbc.cpp b/src/shader_dxbc.cpp index 7962f98aa3..21af08f7d4 100644 --- a/src/shader_dxbc.cpp +++ b/src/shader_dxbc.cpp @@ -1723,11 +1723,10 @@ namespace bgfx case DxbcOperandType::Imm64: for (uint32_t jj = 0; jj < operand.num; ++jj) { - union { uint32_t i; float f; } cast = { operand.un.imm32[jj] }; size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size) , "%s%f" , 0 == jj ? "(" : ", " - , cast.f + , bx::bit_cast(operand.un.imm32[jj]) ); }