10
10
11
11
from .utils import data_to_ctypes
12
12
13
- if TYPE_CHECKING : # handle import cycle caused by type hinting
13
+ if TYPE_CHECKING :
14
14
from arcade .gl import Context
15
15
16
16
@@ -30,12 +30,16 @@ class Buffer:
30
30
31
31
.. warning:: Buffer objects should be created using :py:meth:`arcade.gl.Context.buffer`
32
32
33
- :param ctx: The context this buffer belongs to
34
- :param data: The data this buffer should contain.
35
- It can be a ``bytes`` instance or any
36
- object supporting the buffer protocol.
37
- :param reserve: Create a buffer of a specific byte size
38
- :param usage: A hit of this buffer is ``static`` or ``dynamic`` (can mostly be ignored)
33
+ Args:
34
+ ctx:
35
+ The context this buffer belongs to
36
+ data:
37
+ The data this buffer should contain. It can be a ``bytes`` instance or any
38
+ object supporting the buffer protocol.
39
+ reserve:
40
+ Create a buffer of a specific byte size
41
+ usage:
42
+ A hit of this buffer is ``static`` or ``dynamic`` (can mostly be ignored)
39
43
"""
40
44
41
45
__slots__ = "_ctx" , "_glo" , "_size" , "_usage" , "__weakref__"
@@ -47,7 +51,7 @@ class Buffer:
47
51
48
52
def __init__ (
49
53
self ,
50
- ctx : " Context" ,
54
+ ctx : Context ,
51
55
data : BufferProtocol | None = None ,
52
56
reserve : int = 0 ,
53
57
usage : str = "static" ,
@@ -93,38 +97,40 @@ def __del__(self):
93
97
94
98
@property
95
99
def size (self ) -> int :
96
- """
97
- The byte size of the buffer.
98
- """
100
+ """The byte size of the buffer."""
99
101
return self ._size
100
102
101
103
@property
102
104
def ctx (self ) -> "Context" :
103
- """
104
- The context this resource belongs to.
105
- """
105
+ """The context this resource belongs to."""
106
106
return self ._ctx
107
107
108
108
@property
109
109
def glo (self ) -> gl .GLuint :
110
- """
111
- The OpenGL resource id
112
- """
110
+ """The OpenGL resource id."""
113
111
return self ._glo
114
112
115
- def delete (self ):
113
+ def delete (self ) -> None :
116
114
"""
117
115
Destroy the underlying OpenGL resource.
118
- Don't use this unless you know exactly what you are doing.
116
+
117
+ .. warning:: Don't use this unless you know exactly what you are doing.
119
118
"""
120
119
Buffer .delete_glo (self ._ctx , self ._glo )
121
120
self ._glo .value = 0
122
121
123
122
@staticmethod
124
- def delete_glo (ctx : " Context" , glo : gl .GLuint ):
123
+ def delete_glo (ctx : Context , glo : gl .GLuint ):
125
124
"""
126
125
Release/delete open gl buffer.
126
+
127
127
This is automatically called when the object is garbage collected.
128
+
129
+ Args:
130
+ ctx:
131
+ The context the buffer belongs to
132
+ glo:
133
+ The OpenGL buffer id
128
134
"""
129
135
# If we have no context, then we are shutting down, so skip this
130
136
if gl .current_context is None :
@@ -139,8 +145,11 @@ def delete_glo(ctx: "Context", glo: gl.GLuint):
139
145
def read (self , size : int = - 1 , offset : int = 0 ) -> bytes :
140
146
"""Read data from the buffer.
141
147
142
- :param size: The bytes to read. -1 means the entire buffer (default)
143
- :param offset: Byte read offset
148
+ Args:
149
+ size:
150
+ The bytes to read. -1 means the entire buffer (default)
151
+ offset:
152
+ Byte read offset
144
153
"""
145
154
if size == - 1 :
146
155
size = self ._size - offset
@@ -183,9 +192,12 @@ def write(self, data: BufferProtocol, offset: int = 0):
183
192
truncated to fit. If the supplied data is smaller than the
184
193
buffer, the remaining bytes will be left unchanged.
185
194
186
- :param data: The byte data to write. This can be bytes or any object
187
- supporting the buffer protocol.
188
- :param offset: The byte offset
195
+ Args:
196
+ data:
197
+ The byte data to write. This can be bytes or any object
198
+ supporting the buffer protocol.
199
+ offset:
200
+ The byte offset
189
201
"""
190
202
gl .glBindBuffer (gl .GL_ARRAY_BUFFER , self ._glo )
191
203
size , data = data_to_ctypes (data )
@@ -195,13 +207,18 @@ def write(self, data: BufferProtocol, offset: int = 0):
195
207
raise ValueError ("Attempting to write negative number bytes to buffer" )
196
208
gl .glBufferSubData (gl .GL_ARRAY_BUFFER , gl .GLintptr (offset ), size , data )
197
209
198
- def copy_from_buffer (self , source : "Buffer" , size = - 1 , offset = 0 , source_offset = 0 ):
199
- """Copy data into this buffer from another buffer
200
-
201
- :param source: The buffer to copy from
202
- :param size: The amount of bytes to copy
203
- :param offset: The byte offset to write the data in this buffer
204
- :param source_offset: The byte offset to read from the source buffer
210
+ def copy_from_buffer (self , source : Buffer , size = - 1 , offset = 0 , source_offset = 0 ):
211
+ """Copy data into this buffer from another buffer.
212
+
213
+ Args:
214
+ source:
215
+ The buffer to copy from
216
+ size:
217
+ The amount of bytes to copy
218
+ offset:
219
+ The byte offset to write the data in this buffer
220
+ source_offset:
221
+ The byte offset to read from the source buffer
205
222
"""
206
223
# Read the entire source buffer into this buffer
207
224
if size == - 1 :
@@ -232,13 +249,17 @@ def orphan(self, size: int = -1, double: bool = False):
232
249
If the current buffer is busy in rendering operations
233
250
it will be deallocated by OpenGL when completed.
234
251
235
- :param size: New size of buffer. -1 will retain the current size.
236
- :param double: Is passed in with `True` the buffer size will be doubled
252
+ Args:
253
+ size: (optional)
254
+ New size of buffer. -1 will retain the current size.
255
+ Takes precedence over ``double`` parameter if specified.
256
+ double (optional):
257
+ Is passed in with `True` the buffer size will be doubled
258
+ from its current size.
237
259
"""
238
- if size > - 1 :
260
+ if size > 0 :
239
261
self ._size = size
240
-
241
- if double :
262
+ elif double is True :
242
263
self ._size *= 2
243
264
244
265
gl .glBindBuffer (gl .GL_ARRAY_BUFFER , self ._glo )
@@ -248,9 +269,13 @@ def bind_to_uniform_block(self, binding: int = 0, offset: int = 0, size: int = -
248
269
"""Bind this buffer to a uniform block location.
249
270
In most cases it will be sufficient to only provide a binding location.
250
271
251
- :param binding: The binding location
252
- :param offset: byte offset
253
- :param size: size of the buffer to bind.
272
+ Args:
273
+ binding:
274
+ The binding location
275
+ offset:
276
+ Byte offset
277
+ size:
278
+ Size of the buffer to bind.
254
279
"""
255
280
if size < 0 :
256
281
size = self .size
@@ -261,9 +286,13 @@ def bind_to_storage_buffer(self, *, binding=0, offset=0, size=-1):
261
286
"""
262
287
Bind this buffer as a shader storage buffer.
263
288
264
- :param binding: The binding location
265
- :param offset: Byte offset in the buffer
266
- :param size: The size in bytes. The entire buffer will be mapped by default.
289
+ Args:
290
+ binding:
291
+ The binding location
292
+ offset:
293
+ Byte offset in the buffer
294
+ size:
295
+ The size in bytes. The entire buffer will be mapped by default.
267
296
"""
268
297
if size < 0 :
269
298
size = self .size
0 commit comments