Skip to content

simple_renderer: Fix UniformBuffer bounds and Vulkan push constant ranges - #135

Merged
natetrost merged 2 commits into
android:mainfrom
natetrost:main
Nov 23, 2026
Merged

natetrost merged 2 commits into
android:mainfrom
natetrost:main

Conversation

@natetrost

Copy link
Copy Markdown
Contributor

Fix uniform buffer element bounds, sizes and layout, and how Vulkan push
constant ranges are declared and pushed:

  • UniformBuffer::GetElement, UniformBufferGLES::GetElementOffset and
    UniformBufferVk::GetElementOffset checked index > count instead of
    index >= count, so index == count read past the end of the element
    array and the offsets.
  • UniformBufferVk::SetBufferElementData required the data to end before
    float 32 (end_offset < kMaxUniformBufferFloatSize), so a buffer using
    the full 128 bytes couldn't write its last element (assertion failure in
    debug, dropped in release). GLES already allowed it.
  • Writes weren't checked against the element they target, only against
    the end of the buffer, so writing more than an element's size silently
    overwrote the next element. A write must now be a nonzero number of
    whole floats that fits in its element (a byte count that isn't a
    multiple of 4, such as a float count passed by mistake, tore a float).
  • The element layout (float offsets of each element) was duplicated in
    UniformBufferGLES and UniformBufferVk, written for every element into
    arrays of kMaxUniforms entries (as were GLES uniform locations): more
    than 4 elements overflowed them. The layout now lives in UniformBuffer,
    and only the leading elements that fit are used (at most kMaxUniforms,
    of a known type, within 128 bytes), with an error for the rest.
    GetElementOffset is documented as returning floats (as it always did),
    not bytes. Buffer data starts zeroed rather than uninitialized.
  • The buffer size and stride (GetBufferSizeInBytes, GetBufferStride) were
    data_byte_count as passed, even when it didn't match the elements or
    elements were dropped. They are now the size of the usable elements,
    and a mismatched data_byte_count is reported.
  • UniformBuffer kept the caller's element array (and element names), so
    an array that didn't outlive Renderer::CreateUniformBuffer was read
    after it was gone. The usable elements and their names are now copied
    (a null name becomes an empty name, rather than being passed to
    glGetUniformLocation and %s).
  • RenderStateVk::UpdateUniformData always pushed the vertex stage from
    offset 0, even for a buffer without a vertex stage range (pushing
    0xFFFFFFFF bytes: VUID-vkCmdPushConstants-size-00369, size-00371 and
    offset-01795, then a SIGSEGV in the driver), and for a vertex range
    starting past 0 pushed the wrong bytes. Each used stage is now pushed
    from its own range.
  • Overlapping vertex and fragment ranges (documented as supported) were
    pushed with one stage each, but bytes in both ranges must be pushed with
    both stages (VUID-vkCmdPushConstants-offset-01796). The ranges are now
    split at their edges and each piece pushed with the stages of the
    ranges that cover it.
  • Stage ranges are validated once, when the buffer is created: 4 byte
    aligned, nonzero, and within the element data (which is within the 128
    bytes of push constants Vulkan guarantees). A stage is unused only when
    both its offset and size are kUnusedStageRange. An invalid range is
    reported and its stage isn't used. The pipeline layout's push constant
    ranges come from the same validated ranges.
  • A render state without a uniform buffer (a shader program with no
    uniforms) dereferenced the null buffer in RenderStateVk and
    RenderStateGLES. It now has no push constant ranges or uniforms.

natetrost and others added 2 commits November 23, 2026 17:28
…nges

Fix uniform buffer element bounds, sizes and layout, and how Vulkan push
constant ranges are declared and pushed:

- UniformBuffer::GetElement, UniformBufferGLES::GetElementOffset and
  UniformBufferVk::GetElementOffset checked index > count instead of
  index >= count, so index == count read past the end of the element
  array and the offsets.
- UniformBufferVk::SetBufferElementData required the data to end before
  float 32 (end_offset < kMaxUniformBufferFloatSize), so a buffer using
  the full 128 bytes couldn't write its last element (assertion failure in
  debug, dropped in release). GLES already allowed it.
- Writes weren't checked against the element they target, only against
  the end of the buffer, so writing more than an element's size silently
  overwrote the next element. A write must now be a nonzero number of
  whole floats that fits in its element (a byte count that isn't a
  multiple of 4, such as a float count passed by mistake, tore a float).
- The element layout (float offsets of each element) was duplicated in
  UniformBufferGLES and UniformBufferVk, written for every element into
  arrays of kMaxUniforms entries (as were GLES uniform locations): more
  than 4 elements overflowed them. The layout now lives in UniformBuffer,
  and only the leading elements that fit are used (at most kMaxUniforms,
  of a known type, within 128 bytes), with an error for the rest.
  GetElementOffset is documented as returning floats (as it always did),
  not bytes. Buffer data starts zeroed rather than uninitialized.
- The buffer size and stride (GetBufferSizeInBytes, GetBufferStride) were
  data_byte_count as passed, even when it didn't match the elements or
  elements were dropped. They are now the size of the usable elements,
  and a mismatched data_byte_count is reported.
- UniformBuffer kept the caller's element array (and element names), so
  an array that didn't outlive Renderer::CreateUniformBuffer was read
  after it was gone. The usable elements and their names are now copied
  (a null name becomes an empty name, rather than being passed to
  glGetUniformLocation and %s).
- RenderStateVk::UpdateUniformData always pushed the vertex stage from
  offset 0, even for a buffer without a vertex stage range (pushing
  0xFFFFFFFF bytes: VUID-vkCmdPushConstants-size-00369, size-00371 and
  offset-01795, then a SIGSEGV in the driver), and for a vertex range
  starting past 0 pushed the wrong bytes. Each used stage is now pushed
  from its own range.
- Overlapping vertex and fragment ranges (documented as supported) were
  pushed with one stage each, but bytes in both ranges must be pushed with
  both stages (VUID-vkCmdPushConstants-offset-01796). The ranges are now
  split at their edges and each piece pushed with the stages of the
  ranges that cover it.
- Stage ranges are validated once, when the buffer is created: 4 byte
  aligned, nonzero, and within the element data (which is within the 128
  bytes of push constants Vulkan guarantees). A stage is unused only when
  both its offset and size are kUnusedStageRange. An invalid range is
  reported and its stage isn't used. The pipeline layout's push constant
  ranges come from the same validated ranges.
- A render state without a uniform buffer (a shader program with no
  uniforms) dereferenced the null buffer in RenderStateVk and
  RenderStateGLES. It now has no push constant ranges or uniforms.

Tested on a Pixel 10 Pro with agdktunnel and Vulkan Validation Layers
1.4.363.1, using a temporary hook:
- A quad drawn with a uniform buffer that only has a fragment stage range,
  at byte offset 16 (with a small test shader pair). Before: the push
  constant errors above and a SIGSEGV. After: the quad renders, no
  errors.
- agdktunnel's tunnel uniform buffer with overlapping ranges (vertex
  0..112, fragment 80..112). Before: offset-01796 on every draw. After:
  no errors, renders as before.
- agdktunnel's basic uniform buffer as two matrices (128 bytes), with the
  second written in full. Before: assertion failure. After: renders.
- agdktunnel's basic uniform buffers created from an element array and
  names that are overwritten after creation. Before: assertion failure
  (Vulkan) and a SIGSEGV in glGetUniformLocation (GLES). After: renders.
- A quad drawn with a render state without a uniform buffer. Before: a
  SIGSEGV (Vulkan and GLES). After: the quad renders, no errors.
- No validation errors other than the semaphore reuse already on main
  (finding android#23, PR 12). GLES gameplay renders as before. Debug and
  release both build.
- Host test (ASan/UBSan) of the element layout and bounds: index ==
  count, a write ending at byte 128, writes larger than their element,
  of 0 bytes or not whole floats, more than 4 elements, elements past
  128 bytes, an unknown element type, no elements, a mismatched
  data_byte_count, element copies and null names, and half unused stage
  ranges. And of the pushes for every pair of vertex and fragment stage
  ranges within the buffer: each passes the validation layers' push
  constant checks, and pushes every byte of each range for its stage.
@natetrost
natetrost merged commit b4ea448 into android:main Nov 23, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant