Skip to content

Commit

Permalink
Fix uniform type enum UB regarding extra fragment-bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcourteaux committed Jan 25, 2025
1 parent 68a6788 commit 0a9e3ee
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/bgfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1515,14 +1515,14 @@ namespace bgfx
1,
};

void UniformBuffer::writeUniform(UniformType::Enum _type, uint16_t _loc, const void* _value, uint16_t _num)
void UniformBuffer::writeUniform(uint16_t _type, uint16_t _loc, const void* _value, uint16_t _num)
{
const uint32_t opcode = encodeOpcode(_type, _loc, _num, true);
write(opcode);
write(_value, g_uniformTypeSize[_type]*_num);
}

void UniformBuffer::writeUniformHandle(UniformType::Enum _type, uint16_t _loc, UniformHandle _handle, uint16_t _num)
void UniformBuffer::writeUniformHandle(uint16_t _type, uint16_t _loc, UniformHandle _handle, uint16_t _num)
{
const uint32_t opcode = encodeOpcode(_type, _loc, _num, false);
write(opcode);
Expand Down Expand Up @@ -2520,7 +2520,7 @@ namespace bgfx
break;
}

UniformType::Enum type;
uint16_t type;
uint16_t loc;
uint16_t num;
uint16_t copy;
Expand Down
10 changes: 5 additions & 5 deletions src/bgfx_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ namespace bgfx
}
}

static uint32_t encodeOpcode(UniformType::Enum _type, uint16_t _loc, uint16_t _num, uint16_t _copy)
static uint32_t encodeOpcode(uint16_t _type, uint16_t _loc, uint16_t _num, uint16_t _copy)
{
const uint32_t type = _type << kConstantOpcodeTypeShift;
const uint32_t loc = _loc << kConstantOpcodeLocShift;
Expand All @@ -1517,14 +1517,14 @@ namespace bgfx
return type|loc|num|copy;
}

static void decodeOpcode(uint32_t _opcode, UniformType::Enum& _type, uint16_t& _loc, uint16_t& _num, uint16_t& _copy)
static void decodeOpcode(uint32_t _opcode, uint16_t& _type, uint16_t& _loc, uint16_t& _num, uint16_t& _copy)
{
const uint32_t type = (_opcode&kConstantOpcodeTypeMask) >> kConstantOpcodeTypeShift;
const uint32_t loc = (_opcode&kConstantOpcodeLocMask ) >> kConstantOpcodeLocShift;
const uint32_t num = (_opcode&kConstantOpcodeNumMask ) >> kConstantOpcodeNumShift;
const uint32_t copy = (_opcode&kConstantOpcodeCopyMask); // >> kConstantOpcodeCopyShift;

_type = (UniformType::Enum)(type);
_type = (uint16_t)type;
_copy = (uint16_t)copy;
_num = (uint16_t)num;
_loc = (uint16_t)loc;
Expand Down Expand Up @@ -1582,8 +1582,8 @@ namespace bgfx
m_pos = 0;
}

void writeUniform(UniformType::Enum _type, uint16_t _loc, const void* _value, uint16_t _num = 1);
void writeUniformHandle(UniformType::Enum _type, uint16_t _loc, UniformHandle _handle, uint16_t _num = 1);
void writeUniform(uint16_t _type, uint16_t _loc, const void* _value, uint16_t _num = 1);
void writeUniformHandle(uint16_t _type, uint16_t _loc, UniformHandle _handle, uint16_t _num = 1);
void writeMarker(const bx::StringView& _name);

private:
Expand Down
4 changes: 2 additions & 2 deletions src/renderer_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3377,7 +3377,7 @@ namespace bgfx { namespace d3d11
break;
}

UniformType::Enum type;
uint16_t type;
uint16_t loc;
uint16_t num;
uint16_t copy;
Expand All @@ -3395,7 +3395,7 @@ namespace bgfx { namespace d3d11
data = (const char*)m_uniforms[handle.idx];
}

switch ( (uint32_t)type)
switch (type)
{
case UniformType::Mat3:
case UniformType::Mat3|kUniformFragmentBit:
Expand Down
6 changes: 3 additions & 3 deletions src/renderer_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3384,7 +3384,7 @@ namespace bgfx { namespace d3d12
break;
}

UniformType::Enum type;
uint16_t type;
uint16_t loc;
uint16_t num;
uint16_t copy;
Expand All @@ -3402,7 +3402,7 @@ namespace bgfx { namespace d3d12
data = (const char*)m_uniforms[handle.idx];
}

switch ( (uint32_t)type)
switch (type)
{
case UniformType::Mat3:
case UniformType::Mat3|kUniformFragmentBit:
Expand Down Expand Up @@ -4834,7 +4834,7 @@ namespace bgfx { namespace d3d12
}

kind = "user";
m_constantBuffer->writeUniformHandle( (UniformType::Enum)(type|fragmentBit), regIndex, info->m_handle, regCount);
m_constantBuffer->writeUniformHandle((type|fragmentBit), regIndex, info->m_handle, regCount);
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/renderer_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4420,7 +4420,7 @@ namespace bgfx { namespace gl
break;
}

UniformType::Enum type;
uint16_t type;
uint16_t ignore;
uint16_t num;
uint16_t copy;
Expand Down
6 changes: 3 additions & 3 deletions src/renderer_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ void commit(UniformBuffer& _uniformBuffer)
break;
}

UniformType::Enum type;
uint16_t type;
uint16_t loc;
uint16_t num;
uint16_t copy;
Expand All @@ -1765,7 +1765,7 @@ void commit(UniformBuffer& _uniformBuffer)
data = (const char*)m_uniforms[handle.idx];
}

switch ( (uint32_t)type)
switch (type)
{
case UniformType::Mat3:
case UniformType::Mat3|kUniformFragmentBit:
Expand Down Expand Up @@ -2225,7 +2225,7 @@ void processArguments(
}

UniformType::Enum type = convertMtlType(dataType);
constantBuffer->writeUniformHandle( (UniformType::Enum)(type|fragmentBit), uint32_t(uniform.offset), info->m_handle, uint16_t(num) );
constantBuffer->writeUniformHandle((type|fragmentBit), uint32_t(uniform.offset), info->m_handle, uint16_t(num) );
BX_TRACE("store %s %d offset:%d", name, info->m_handle, uint32_t(uniform.offset) );
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/renderer_vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4150,7 +4150,7 @@ VK_IMPORT_DEVICE
break;
}

UniformType::Enum type;
uint16_t type;
uint16_t loc;
uint16_t num;
uint16_t copy;
Expand All @@ -4168,7 +4168,7 @@ VK_IMPORT_DEVICE
data = (const char*)m_uniforms[handle.idx];
}

switch ( (uint32_t)type)
switch (type)
{
case UniformType::Mat3:
case UniformType::Mat3|kUniformFragmentBit:
Expand Down Expand Up @@ -5090,7 +5090,7 @@ VK_DESTROY
}

kind = "user";
m_constantBuffer->writeUniformHandle( (UniformType::Enum)(type|fragmentBit), regIndex, info->m_handle, regCount);
m_constantBuffer->writeUniformHandle((type|fragmentBit), regIndex, info->m_handle, regCount);
}
}
}
Expand Down

0 comments on commit 0a9e3ee

Please sign in to comment.