6262import time
6363
6464import numpy as np
65-
6665from cuda .core import (
6766 Device ,
6867 GraphicsRegisterFlags ,
@@ -118,14 +117,14 @@ def create_window():
118117 from pyglet .gl import gl as _gl
119118 except ImportError :
120119 print (
121- "This example requires pyglet >= 2.0.\n "
122- "Install it with: pip install pyglet" ,
120+ "This example requires pyglet >= 2.0.\n Install it with: pip install pyglet" ,
123121 file = sys .stderr ,
124122 )
125123 sys .exit (1 )
126124
127125 window = pyglet .window .Window (
128- WIDTH , HEIGHT ,
126+ WIDTH ,
127+ HEIGHT ,
129128 caption = "GraphicsResource Example - CUDA Plasma" ,
130129 vsync = False ,
131130 )
@@ -151,15 +150,36 @@ def create_display_resources(gl, width, height):
151150 shader_prog = ShaderProgram (vert , frag )
152151
153152 # Fullscreen quad (two triangles covering the entire window)
154- quad_verts = np .array ([
155- # x, y, s, t (position + texture coordinate)
156- - 1 , - 1 , 0 , 0 ,
157- 1 , - 1 , 1 , 0 ,
158- 1 , 1 , 1 , 1 ,
159- - 1 , - 1 , 0 , 0 ,
160- 1 , 1 , 1 , 1 ,
161- - 1 , 1 , 0 , 1 ,
162- ], dtype = np .float32 )
153+ quad_verts = np .array (
154+ [
155+ # x, y, s, t (position + texture coordinate)
156+ - 1 ,
157+ - 1 ,
158+ 0 ,
159+ 0 ,
160+ 1 ,
161+ - 1 ,
162+ 1 ,
163+ 0 ,
164+ 1 ,
165+ 1 ,
166+ 1 ,
167+ 1 ,
168+ - 1 ,
169+ - 1 ,
170+ 0 ,
171+ 0 ,
172+ 1 ,
173+ 1 ,
174+ 1 ,
175+ 1 ,
176+ - 1 ,
177+ 1 ,
178+ 0 ,
179+ 1 ,
180+ ],
181+ dtype = np .float32 ,
182+ )
163183
164184 vao = ctypes .c_uint (0 )
165185 gl .glGenVertexArrays (1 , ctypes .byref (vao ))
@@ -169,7 +189,8 @@ def create_display_resources(gl, width, height):
169189 gl .glGenBuffers (1 , ctypes .byref (vbo ))
170190 gl .glBindBuffer (gl .GL_ARRAY_BUFFER , vbo .value )
171191 gl .glBufferData (
172- gl .GL_ARRAY_BUFFER , quad_verts .nbytes ,
192+ gl .GL_ARRAY_BUFFER ,
193+ quad_verts .nbytes ,
173194 quad_verts .ctypes .data_as (ctypes .c_void_p ),
174195 gl .GL_STATIC_DRAW ,
175196 )
@@ -192,9 +213,15 @@ def create_display_resources(gl, width, height):
192213 gl .glTexParameteri (gl .GL_TEXTURE_2D , gl .GL_TEXTURE_MIN_FILTER , gl .GL_LINEAR )
193214 gl .glTexParameteri (gl .GL_TEXTURE_2D , gl .GL_TEXTURE_MAG_FILTER , gl .GL_LINEAR )
194215 gl .glTexImage2D (
195- gl .GL_TEXTURE_2D , 0 , gl .GL_RGBA8 ,
196- width , height , 0 ,
197- gl .GL_RGBA , gl .GL_UNSIGNED_BYTE , None ,
216+ gl .GL_TEXTURE_2D ,
217+ 0 ,
218+ gl .GL_RGBA8 ,
219+ width ,
220+ height ,
221+ 0 ,
222+ gl .GL_RGBA ,
223+ gl .GL_UNSIGNED_BYTE ,
224+ None ,
198225 )
199226
200227 return shader_prog , vao .value , tex .value
@@ -223,9 +250,14 @@ def copy_pbo_to_texture(gl, pbo_id, tex_id, width, height):
223250 gl .glBindBuffer (gl .GL_PIXEL_UNPACK_BUFFER , pbo_id )
224251 gl .glBindTexture (gl .GL_TEXTURE_2D , tex_id )
225252 gl .glTexSubImage2D (
226- gl .GL_TEXTURE_2D , 0 , 0 , 0 ,
227- width , height ,
228- gl .GL_RGBA , gl .GL_UNSIGNED_BYTE ,
253+ gl .GL_TEXTURE_2D ,
254+ 0 ,
255+ 0 ,
256+ 0 ,
257+ width ,
258+ height ,
259+ gl .GL_RGBA ,
260+ gl .GL_UNSIGNED_BYTE ,
229261 None , # None = read from the currently bound PBO, not from CPU
230262 )
231263 gl .glBindBuffer (gl .GL_PIXEL_UNPACK_BUFFER , 0 )
@@ -243,6 +275,7 @@ def draw_fullscreen_quad(gl, shader_prog, vao_id, tex_id):
243275
244276# ================================== main() ==================================
245277
278+
246279def main ():
247280 # --- Step 1: Set up CUDA (compile kernel, create stream) ---
248281 dev , stream , kernel , config = setup_cuda (PLASMA_KERNEL_SOURCE )
@@ -263,9 +296,7 @@ def main():
263296 # THIS IS THE KEY LINE. GraphicsResource.from_gl_buffer() tells the
264297 # CUDA driver "I want to access this OpenGL buffer from CUDA kernels."
265298 # WRITE_DISCARD means CUDA will overwrite the entire buffer each frame.
266- resource = GraphicsResource .from_gl_buffer (
267- pbo_id , flags = GraphicsRegisterFlags .WRITE_DISCARD
268- )
299+ resource = GraphicsResource .from_gl_buffer (pbo_id , flags = GraphicsRegisterFlags .WRITE_DISCARD )
269300
270301 # --- Step 6: Render loop ---
271302 start_time = time .monotonic ()
@@ -285,11 +316,13 @@ def on_draw():
285316 with resource .map (stream = stream ) as buf :
286317 # (b) Launch the plasma kernel -- it writes RGBA pixels into buf.
287318 launch (
288- stream , config , kernel ,
289- buf .handle , # pointer to PBO memory (on GPU)
319+ stream ,
320+ config ,
321+ kernel ,
322+ buf .handle , # pointer to PBO memory (on GPU)
290323 np .int32 (WIDTH ),
291324 np .int32 (HEIGHT ),
292- np .float32 (t ), # animation time
325+ np .float32 (t ), # animation time
293326 )
294327 # (c) Unmap happens automatically when the `with` block exits.
295328 # The PBO now belongs to OpenGL again. No stream.sync() is
@@ -309,7 +342,10 @@ def on_draw():
309342 if now - fps_time >= 1.0 :
310343 fps = frame_count / (now - fps_time )
311344 frame_us = 1_000_000.0 / fps if fps > 0 else 0
312- window .set_caption (f"GraphicsResource Example - CUDA Plasma ({ WIDTH } x{ HEIGHT } , { fps :.0f} FPS, { frame_us :.0f} \u00b5 s frame)" )
345+ window .set_caption (
346+ f"GraphicsResource Example - CUDA Plasma"
347+ f" ({ WIDTH } x{ HEIGHT } , { fps :.0f} FPS, { frame_us :.0f} \u00b5 s frame)"
348+ )
313349 frame_count = 0
314350 fps_time = now
315351
0 commit comments