nbarkhina / N64Wasm

A web based N64 Emulator
https://www.neilb.net/n64wasm/
MIT License
617 stars 121 forks source link

Is it possible to use WebGPU? #58

Open BeezBumba opened 1 month ago

BeezBumba commented 1 month ago

I found an article that talks about the conversion from WebGL to WebGPU. WebGPU is said to have compute shaders and geometry shaders. Geometry shaders are what the emulator needs because, with them, every triangle has a shader that finds the right pixel colors. This explains why there is no color in the images. I posted this in a new issue since the other one was pretty long. Screenshot 2024-06-13 175229 Screenshot 2024-06-13 174826

BeezBumba commented 1 month ago

Sorry I forgot the actual link. https://developer.chrome.com/blog/from-webgl-to-webgpu

BeezBumba commented 1 month ago

The WebGPU using a different shading language than WebGL, but the concepts are fairly the same.

BeezBumba commented 1 month ago

I saw shader_frag and shader_vert files in the code directory. WebGL can't support geometry shaders. so could there have been a shader_geo.hlsl file? That might be easier if it's possible.

nbarkhina commented 1 month ago

those shaders are just used for the emulator overlay menu - "New Game, Save State, etc..." it's not used by the emulator itself

BeezBumba commented 1 month ago

So what is your option on the whole conversion? Do you think it's possible at all?

nbarkhina commented 1 month ago

My knowledge of shaders unfortunately is quite basic - so I would really not know

BeezBumba commented 1 month ago

Its not to much about shaders. It's just the conversion from WebGL to WebGPU.

BeezBumba commented 3 weeks ago

Well can I ask what spots you changed so the emulator uses webGL?

BeezBumba commented 3 weeks ago

and the spots where you are using the webgl context

nbarkhina commented 3 weeks ago

The emulator uses WebGL because that is what emscripten translates to when compiling from OpenGL. See this article -

https://emscripten.org/docs/porting/multimedia_and_graphics/OpenGL-support.html

Here in the code is where it sets up the OpenGL Context -

https://github.com/nbarkhina/N64Wasm/blob/4aacfded69cdba1c19a07bfe7f3eb13c22252f7f/code/mymain.cpp#L632

So I don't actually use any WebGL commands, it's just straight up OpenGL which the compiler at a lower level turns into WebGL. I'm not sure how you would get it to use something else, the emulator would have to be rewritten to use something other than OpenGL - which in Desktop could use different backends (Vulkan, Direct3D, etc...) but on the web you don't have those options. So don't really think this is possible in my opinion.

BeezBumba commented 1 week ago

Do you know how to disable the emulator emulating lag?

BeezBumba commented 1 week ago

Emulators do that just for the pure accuracy.

nbarkhina commented 1 week ago

I don't believe there is any lag, the main tool for maintaining correct framerate is by syncing to the refresh rate of the monitor. this is done using the emscripten_set_main_loop() function:

https://github.com/nbarkhina/N64Wasm/blob/4aacfded69cdba1c19a07bfe7f3eb13c22252f7f/code/mymain.cpp#L734

you can read about it here

https://emscripten.org/docs/api_reference/emscripten.h.html

one thing you can experiment with is setting the variable doubleSpeed to true here -

https://github.com/nbarkhina/N64Wasm/blob/4aacfded69cdba1c19a07bfe7f3eb13c22252f7f/code/mymain.cpp#L42

that will attempt to run 2 frames for every 1 frame of time, however on slow machines it won't be able to keep up and won't yield any benefit.

BeezBumba commented 1 week ago

I asked this because some games on Ipad are a lot better now with vertex buffer objects. but that update still did something with safari.