mirror of
https://github.com/ManimCommunity/manim.git
synced 2026-06-22 10:01:47 +00:00
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:
parent
9360129365
commit
9f72377c9d
1 changed files with 11 additions and 6 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue