Skip to content

Commit

Permalink
Render to a Window (#1008)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* end-to-end demo working

* Try to cache most of the expensive wgpu setup calls

* The caching dramatically improves the framerate, visible when resizing the window, but it hits a segfault when you click the close button, so attempt to fix that by manually cleaning up the AlanWindow struct

* Make the title show the rendering time for fun

* Add a debug log so I can see what's available

* Move logging to the window sizing

* Update the logging to include a potential solution to the buffer sizing problem based on my reading of how [`copyBufferToTexture`](https://gpuweb.github.io/gpuweb/#dom-gpucommandencoder-copybuffertotexture) works

* The logs are giving me weird results. Solution? Add more logs

* Start reworking things to work by copying a buffer into the texture

* Make sure the buffer is being created on the right device

* Getting a crash but I'm not entirely sure why, add some forceful logging to make sure the events are firing in the order I expect

* I don't know how I didn't notice the missing COPY_DST message

* Remove some debug logging, try to improve the perf of the redraw path, and try to fix the crash-on-exit I'm seeing

* Un-comment the window init call in the redraw path, it seems necessary on MacOS

* On MacOS, the resize event is *not* fired first, so I need to make sure both paths can work without the other

* Attempt to unlock all features when initializing wgpu on the window texture

* Attempt to do *something* with the buffer

* Attempt to fix crash on exit, and actually do some work

* That renders something, but not what I expected. Let's see if I can fix that

* Add a debug log. I think the buffer_width is not being set properly

* The issue was that the buffer_width is the byte width, but what I need is the equivalent pixel width within the shader

* Remove the debug log in the middle of the render loop

* There's some weird rendering artifacts on the Pi5, adding back some debug logging to try and figure it out

* It doesn't appear to be a race condition on the buffer size, but on whether or not the compute shader is completed before the buffer copy starts. So I'm going to try two separate command lists to avoid this, at the cost of likely some added latency.

* Another attempt at getting the rendering to work on the RPi5

* Another attempt to fix rendering on the RPi5

* Another try

* I didn't want two polls, but let's try that

* Let's see if the issue is just due to resize glitches that disappear after the first frame (it currently doesn't redraw except if there's a resize or a focus change)

* I saw the rendering glitch with the cleared buffer. Let's see what happens if I don't clear it

* This *should* work...

* When closing the application with actual continuous rendering, it segfaults. This will hopefully stop that

* Just double checking that this is what 'fixes' rendering on the RPi5

* Let's just try copying twice?

* None of the queue mangling is helping, so let's drop it. Perhaps the Pi5 is not picking FIFO by default?

* Let's try having a wait here, again. I can't figure out why I'm getting weird rendering issues on the Pi5 right now

* Perhaps increasing the queue size will help here

* Maybe there's some weirdness between when a resize event is triggered and when a redraw is triggered (two resizes in a row followed by a redraw for the *first* resize, perhaps

* Even if that didn't work here, I prefer the reduction in code and elimination of a potential race condition

* Let's swap the order of the buffer copy and buffer generation to attempt to bypass the apparent race condition

* Revert "Let's swap the order of the buffer copy and buffer generation to attempt to bypass the apparent race condition"

This reverts commit 1fcebdf.

* Disable the debug line

* Do we need this poll?

* Let's try adding a floating point run time to the window and use it in the shader to do some (trivial) animation

* Restore the render time indicator as the frame time, not the total run time, and tweak the animation

* One more tweak to the animation logic

* The animation actually doing something now crashes MacOS pretty reliably. Perhaps I need to explicitly destroy the old buffers instead of relying on this to be done for me?

* I don't have vim set up on this MacOS machine, so I didn't catch these mistakes in time

* It seemed to work better at first, but then it ate up a terabyte of virtual memory and nearly crashed the computer. Maybe I need to explicitly destroy the context buffer, too

* Start actually making it useful

* Fix which buffer is destroyed

* Add an initial probe to get the adapter first (so it should be possible to construct the buffers ahead of time to provide to a callback for a full GPGPU object)

* That didn't work at all, so have a function pointer stored in the AlanWindow and hope this works

* Also pass through the total width and height

* Another attempt at this crazy closure passing logic

* Add the updated root scope bindings

* The prior implementation crashed on resize. Add some trickery to try to avoid this. Also remove the width and height parameters because they can't be hardcoded. More trickery to simply replace the workgroup sizes for the last GPGPU shader on-the-fly

* I forgot that sometimes a resize can come *first*

* Try remove excess work from the render loop by having a simple boolean check to gate running the init function and removing a buffer copy step by swapping the context buffer around

* Turn the init function into a method, in preparation for more methods

* Make the context generated per-frame by the user via a closure function

* Trying to pass a reference to the struct to the closure that is being held by the struct results in circular types, so put the values we need to give to the closure in a sub-type to try to resolve this

* This is now finally at the bare-bones 'solid' for this PR

* Make clippy happy

* Forgot one

* Switch to named context and framebuffer buffers from a Frame object

* Fixes for the root scope binding with the new Frame type

* Replace one last magic piece of the API with something a bit less so

* Missed this mutation on init
  • Loading branch information
dfellis authored Dec 16, 2024
1 parent b8ce762 commit 7e62ac6
Show file tree
Hide file tree
Showing 4 changed files with 1,568 additions and 51 deletions.
Loading

0 comments on commit 7e62ac6

Please sign in to comment.