Skip to content

Commit

Permalink
comments in juptyer notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
mhochsteger committed Oct 25, 2024
1 parent d98f300 commit 7e9a805
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions webgpu.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Simple example that uses prepared draw function\n",
"import webgpu.jupyter\n",
"from ngsolve import *\n",
"\n",
Expand All @@ -22,39 +23,43 @@
"metadata": {},
"outputs": [],
"source": [
"# more verbose example with custom render function implementation\n",
"\n",
"import webgpu.jupyter\n",
"from ngsolve import *\n",
"\n",
"def render(data):\n",
" # this function is running in pyodide\n",
" \"\"\"This is the setup function running in pyodide (with access to the html dom and JS)\"\"\"\n",
" print(\"hello from pyodide\")\n",
" import pyodide.ffi\n",
" import ngsolve as ngs\n",
" from webgpu.jupyter import gpu\n",
" from webgpu.mesh import create_mesh_buffers, create_function_value_buffers, MeshRenderObject\n",
"\n",
" # unpack the data argument\n",
" mesh = data['mesh']\n",
" cf = data['cf']\n",
" order = data['order']\n",
" \n",
" region = mesh.Region(ngs.VOL)\n",
"\n",
" # Copy mesh/function data to the GPU\n",
" region = mesh.Region(ngs.VOL)\n",
" n_trigs, buffers = create_mesh_buffers(\n",
" gpu.device, region\n",
" )\n",
" \n",
" buffers = buffers | create_function_value_buffers(\n",
" gpu.device, cf, region, order\n",
" )\n",
" \n",
" mesh_object = MeshRenderObject(gpu, buffers, n_trigs)\n",
"\n",
" frame_counter = 0\n",
" import time\n",
" t0 = time.time()\n",
" def render_function(t):\n",
" \"\"\"This function is called for each frame\"\"\"\n",
" nonlocal frame_counter\n",
"\n",
" # change colormap for animation\n",
" # change colormap for some animation\n",
" gpu.uniforms.colormap.min = 0.3 + 0.3*ngs.sin(4*(time.time()-t0))\n",
" gpu.uniforms.colormap.max = 1.0 - gpu.uniforms.colormap.min\n",
" gpu.uniforms.update_buffer()\n",
Expand All @@ -64,12 +69,12 @@
" mesh_object.render(encoder)\n",
" gpu.device.queue.submit([encoder.finish()])\n",
"\n",
" # request next frame (for animations), stop after 200 frames to avoid endless loop\n",
" # request next frame (for animation, not necessary otherwise)\n",
" # stop after 200 frames to avoid endless loop\n",
" frame_counter += 1\n",
" if frame_counter < 200:\n",
" js.requestAnimationFrame(render_function)\n",
"\n",
"\n",
" # we need to create a proxy object to pass a python function as JS callback\n",
" render_function = pyodide.ffi.create_proxy(render_function)\n",
"\n",
Expand Down

0 comments on commit 7e9a805

Please sign in to comment.