Improve rendering time by rewriting STD140BufferFormat._write_padded() (#4058)

The original implementation of `STD140BufferFormat._write_padded()` used `np.pad` which is slow.
This new implementation avoids that by creating a new array of zeroes instead. In this way, this method now takes 60x less time, and the total rendering time is almost halved.
This commit is contained in:
Francisco Manríquez Novoa 2024-12-13 20:12:01 -03:00 committed by GitHub
commit 9f72377c9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -50,7 +50,7 @@ class STD140BufferFormat:
self.name = name
self.binding = binding
self.dtype = []
self._paddings = {} # LUT for future writes
self._paddings: dict[str, int] = {} # LUT for future writes
byte_offset = 0 # Track the offset so we can calculate padding for alignment -- NOTE: use RenderDoc to debug
for data_type, var_name in struct:
_base_char, base_bytesize, shape = self._GL_DTYPES[data_type]
@ -97,11 +97,16 @@ class STD140BufferFormat:
np.ndarray
the same data with 0 or 1 columns of 0s appended
"""
try:
# This fails for 1D data (python or np.array)
return np.pad(data, ((0, 0), (0, self._paddings[var])), mode="constant")
except Exception:
return np.pad(data, ((0, self._paddings[var])), mode="constant")
data = np.asarray(data)
if self._paddings[var] == 0 or data.ndim == 0:
return data
# Make a new array with extra columns of 0s
new_shape = list(data.shape)
new_shape[-1] += self._paddings[var]
padded_data = np.zeros(new_shape)
padded_data[..., : data.shape[-1]] = data
return padded_data
def write(self, data: dict) -> None:
"""Write a dictionary of key value pairs to the STD140BufferFormat's data attribute