gl: pad uniform buffer ranges and buffers to 16 bytes, so a range short of its std140 block's trailing padding draws on WebGL (SuperTuxKart 1.4)

This commit is contained in:
Pooya Parsa
2026-11-22 22:29:33 +00:00
parent 74e8c88957
commit b916d273dd
2 changed files with 27 additions and 0 deletions

View File

@@ -48,6 +48,10 @@
#define glEndTransformFeedback kw_glEndTransformFeedback
#define glPauseTransformFeedback kw_glPauseTransformFeedback
/* WebGL's uniform buffer sizes: kw_gl.js pads buffers and uniform buffer
* ranges to 16 bytes. */
#define glBindBufferRange kw_glBindBufferRange
/* GL_RENDERER: the WebGL implementation's own name (kw_gl.js), which vrend
* forwards to the guest ("virgl (...)") in place of "WebKit WebGL". */
#define glGetString kw_glGetString
@@ -70,6 +74,8 @@ void kw_glDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count
const void *indices);
void kw_glEndTransformFeedback(void);
void kw_glPauseTransformFeedback(void);
void kw_glBindBufferRange(GLenum target, GLuint index, GLuint buffer, GLintptr offset,
GLsizeiptr size);
const GLubyte *kw_glGetString(GLenum name);
bool epoxy_has_gl_extension(const char *extension);

View File

@@ -19,6 +19,18 @@
// stale, and an indexed draw reloads a stale twin by
// reading the buffer back. vrend's calls reach these
// through the kw_ names (epoxy/gl.h).
// uniform sizes WebGL refuses a draw whose uniform buffer range is
// smaller than the block it backs (UNIFORM_BLOCK_DATA_SIZE,
// GL_INVALID_OPERATION "a uniform buffer that is too
// small"), which desktop drivers let pass, and vrend binds
// the guest's range as it is. An std140 block's size is a
// multiple of 16, so a range short of only its trailing
// padding is a common bug (SuperTuxKart 1.4's Matrices
// block: 592 bytes, its buffer 584): a uniform buffer range
// is rounded up to 16 bytes, and every buffer vrend
// allocates (no data) to 16 bytes so that the range fits
// (offsets are aligned to UNIFORM_BUFFER_OFFSET_ALIGNMENT,
// a multiple of 16).
// buffer maps glMapBufferRange, glUnmapBuffer, glFlushMappedBufferRange:
// WebGL has no buffer mapping. A map is a copy in module
// memory, filled by getBufferSubData when the range must be
@@ -218,6 +230,7 @@ addToLibrary({
// an element buffer too.
kw_glBufferData__deps: ['$kwgl', 'glBufferData'],
kw_glBufferData: (target, size, data, usage) => {
if (!data) size = (size + 15) & ~15; // room for a padded uniform range (uniform sizes)
const { buf, twin } = kwgl.reach(target);
if (twin && target === kwgl.ELEMENT)
kwgl.withCopy(kwgl.COPY_WRITE, buf, () => _glBufferData(kwgl.COPY_WRITE, size, data, usage));
@@ -266,6 +279,14 @@ addToLibrary({
_glDeleteBuffers(n, buffers);
},
// --- uniform sizes ---
kw_glBindBufferRange__deps: ['glBindBufferRange'],
kw_glBindBufferRange: (target, index, buffer, offset, size) => {
if (target === 0x8A11 /* UNIFORM_BUFFER */) size = (size + 15) & ~15;
_glBindBufferRange(target, index, buffer, offset, size);
},
// transform feedback wrote to its buffers
$kwglFeedbackWritten__deps: ['$kwgl'],
$kwglFeedbackWritten: () => {