Repository navigation
simple_renderer: Fix UniformBuffer bounds and Vulkan push constant ranges - #135
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix uniform buffer element bounds, sizes and layout, and how Vulkan push
constant ranges are declared and pushed:
UniformBufferVk::GetElementOffset checked index > count instead of
index >= count, so index == count read past the end of the element
array and the offsets.
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.
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).
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.
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.
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).
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.
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.
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.
uniforms) dereferenced the null buffer in RenderStateVk and
RenderStateGLES. It now has no push constant ranges or uniforms.