@@ -73,13 +73,14 @@ cdef class GraphicsResource:
7373 view = StridedMemoryView.from_buffer(buf, shape=(256,), dtype=np.float32)
7474 # view.ptr is a CUDA device pointer into the GL buffer
7575
76- Or use explicit control for render loops :
76+ Or scope registration separately from mapping :
7777
7878 .. code-block:: python
7979
80- buf = resource.map(stream=s)
81- # ... launch kernels using buf.handle, buf.size ...
82- buf.close()
80+ with GraphicsResource.from_gl_buffer(vbo) as resource:
81+ with resource.map(stream=s) as buf:
82+ # ... launch kernels using buf.handle, buf.size ...
83+ pass
8384 """
8485
8586 def __init__ (self ):
@@ -110,12 +111,21 @@ cdef class GraphicsResource:
110111 with GraphicsResource.from_gl_buffer(vbo , stream = s) as buf:
111112 view = StridedMemoryView.from_buffer(buf, shape = (256 ,), dtype = np.float32)
112113
114+ If omitted , the returned resource can still be used as a context
115+ manager to scope registration and automatic cleanup::
116+
117+ with GraphicsResource.from_gl_buffer(vbo ) as resource:
118+ with resource.map(stream = s) as buf:
119+ ...
120+
113121 Returns
114122 -------
115123 GraphicsResource
116124 A new graphics resource wrapping the registered GL buffer.
117- If *stream* was given , the returned resource can be used directly
118- as a context manager.
125+ The returned resource can be used as a context manager. If
126+ *stream* was given , entering maps the resource and yields a
127+ :class:`~cuda.core.Buffer`; otherwise entering yields the
128+ :class:`GraphicsResource` itself and closes it on exit.
119129
120130 Raises
121131 ------
@@ -289,21 +299,12 @@ cdef class GraphicsResource:
289299
290300 def __enter__ (self ):
291301 if self ._context_manager_stream is None :
292- raise RuntimeError (
293- " GraphicsResource context manager requires a stream; "
294- " use resource.map(stream=...) or pass stream= to from_gl_buffer()"
295- )
302+ return self
296303 self ._entered_buffer = self .map(stream = self ._context_manager_stream)
297304 return self ._entered_buffer
298305
299306 def __exit__ (self , exc_type , exc_val , exc_tb ):
300- cdef object buf_obj = self ._entered_buffer
301- cdef Buffer buf
302- self ._entered_buffer = None
303- if buf_obj is not None :
304- buf = < Buffer> buf_obj
305- if buf._h_ptr:
306- buf.close()
307+ self .close()
307308 return False
308309
309310 cpdef close(self , stream = None ):
0 commit comments