[Feature] Use librashader for .slangp (RetroArch) shader support#4678
[Feature] Use librashader for .slangp (RetroArch) shader support#4678mr-ke wants to merge 12 commits intoTASEmulators:masterfrom
Conversation
|
WOW What's the workflow to use of an arbitrary libretro shader with this? For example https://github.com/libretro/slang-shaders/blob/master/crt/crt-royale.slangp |
1f848a4 to
81910d9
Compare
Hi, good question, I guess you could follow below steps:
|
fixes 32dc37e
| private static uint GetTexID(ITexture2D texture) | ||
| { | ||
| if (texture == null || TexIDField == null) return 0; | ||
| return (uint)TexIDField.GetValue(texture); |
There was a problem hiding this comment.
I think this is equivalent to return (texture as OpenGLTexture2D)?.TexID ?? default;. Did you use reflection because OpenGLTexture2D is internal?
There was a problem hiding this comment.
I think this is equivalent to
return (texture as OpenGLTexture2D)?.TexID ?? default;. Did you use reflection becauseOpenGLTexture2Disinternal?
If we do not use reflection to access, then InternalsVisibleTo Include="BizHawk.Client.Common" is needed for project BizHawk.Bizware.Graphics, maybe isolate this changeset without change expose charastic of BizHawk.Bizware.Graphics would be a option as better, what do you say?
Same reason apply to drawFbo
There was a problem hiding this comment.
There was a problem hiding this comment.
Don't put this librashader handling in BizHawk.Client.Common in the first place, put it in BizHawk.Bizware.Graphics. This should outright be part of the IGL so it can handle the IGL impl specific details (BizHawk.Client.Common should only be calling IGL APIs, it's not supposed to handle raw OpenGL commands).
This impl using raw OpenGL commands also means the feature is locked to the OpenGL display method, rather than also being available to the (default!) Direct3D11 display method (which librashader can support just fine).
There was a problem hiding this comment.
Don't put this librashader handling in
BizHawk.Client.Commonin the first place, put it inBizHawk.Bizware.Graphics. This should outright be part of the IGL so it can handle the IGL impl specific details (BizHawk.Client.Commonshould only be calling IGL APIs, it's not supposed to handle raw OpenGL commands).This impl using raw OpenGL commands also means the feature is locked to the OpenGL display method, rather than also being available to the (default!) Direct3D11 display method (which librashader can support just fine).
Well, you are spot on the view of what should be in BizHawk.Client.Common and what should be in BizHawk.Bizware.Graphics, in the meanwhile, I've made a progress on https://github.com/mr-ke/BizHawk/commits/feature/librashader_d3d11, which I also make librashader works on Bizhawk default d3d11 mode, do you consider it a idea we consist d3d11 together or we need to firstly put these architecture adjustment at first place for OpenGL part then consist d3d11 afterwards.
Looking forward to hear all your ideas in mind.
There was a problem hiding this comment.
If you got D3D working, you can include that in this PR too.
There was a problem hiding this comment.
Here it goes with the joint work d3d & openGL (Also abstract common part into LibrashaderFilterBase.cs).
I've done a instrument on filter_chain_create (shader compile at boot) for both d3d & openGL as below:
Preset: vhs_and_crt_godot.slangp (complex enough)
OpenGL 140ms (Cold start)
OpenGL 55ms (2nd start)
D3D11 1600ms (Cold start)
D3D11 55ms (2nd start) 2026/4/21 updated!!!
That's make sense OpenGL's GLSL is much faster than D3D's HLSL compiler.
Also OpenGL's shader cache works along with D3D's shader cache not work. (You can inspect at %LOCALAPPDATA%\librashader)
I've take a while to check option I set for D3D11, no obvious basis prove we use wrong option for D3D11 but librashader.db.1 can not generated to accelerate D3D11's shader compile.
Updated 2026/4/21 I've identified why shader cache is not generated as expected in D3D11 mode, it is due to a concurrent issue in librashader.dll, after carefull inspect the rust code, the cache issue can be work around by attach D3D11_CREATE_DEVICE_SINGLETHREADED flag to D3D device created.
Please help me check if 0e698fd really matter on other cases.
I understood that EncoreGLTextureProvider and MelonDSGLTextureProvider use GL Interop to share the GL texture to D3D, but it still have fallback methods, do I read it correctly?
There was a problem hiding this comment.
GL interop is used as a fast method to transfer the GL texture to the display method. The fallback method is slow. This slowness doesn't matter too much at 1x internal resolution, it ends up mattering a lot if you decide to bump up the internal resolution (which I imagine ends up massively overlapping with the userbase that would use these shaders).
Even then, if there's some concurrency occurring within librashader it does not make sense to make the D3D11 device single threaded, if you're saying there's threading in librashader then the single threaded flag should not be set.
Summary
This PR integrates librashader as a modern shader filter backend for BizHawk, replacing the legacy Cg/GLSL shader system with support for modern slangp and glslp shader presets.
Changes
New Files
BizHawk.Client.Common/DisplayManager/Filters/Librashader.cs- Native bindings for librashader.dllBizHawk.Client.Common/DisplayManager/Filters/LibrashaderFilter.cs- Filter implementation for the display pipelineModified Files
BizHawk.Client.Common/DisplayManager/DisplayManagerBase.csLibrashaderFilterfield andRefreshLibrashader()methodBuildDefaultChain()(TargetDisplayFilter=4)BizHawk.Client.EmuHawk/config/DisplayConfig.cs.cgpfor legacy UserFilter,.slangp/.glslpfor librashaderBizHawk.Client.EmuHawk/config/DisplayConfig.Designer.csBizHawk.Client.EmuHawk/MainForm.Events.csRefreshLibrashader()call after DisplayConfig changesFeatures
Util.DebugWriteLine()(DEBUG builds only)Requirements
librashader.dllmust be placed indll\directoryTesting
Tested with various slangp shader presets (VHS, CRT effects) on NES and other cores.
Check if completed: