2022-10-18 14:52:16 +11:00
# librashader
2023-02-17 09:41:12 +11:00
![Mega Bezel SMOOTH-ADV ](shader_triangle.png )
2022-11-21 18:38:23 +11:00
2023-01-17 15:09:07 +11:00
< small > *Mega Bezel SMOOTH-ADV on DirectX 11*< / small >
2022-11-19 18:35:02 +11:00
2022-11-27 15:36:59 +11:00
librashader (*/ˈ li:brəʃeɪ dɚ/*) is a preprocessor, compiler, and runtime for RetroArch 'slang' shaders, rewritten in pure Rust.
2022-10-18 14:52:16 +11:00
2024-02-04 16:38:58 +11:00
[![Latest Version ](https://img.shields.io/crates/v/librashader.svg )](https://crates.io/crates/librashader) [![Docs ](https://docs.rs/librashader/badge.svg )](https://docs.rs/librashader) [![build result ](https://build.opensuse.org/projects/home:chyyran:librashader/packages/librashader/badge.svg )](https://software.opensuse.org//download.html?project=home%3Achyyran%3Alibrashader& package=librashader)
![License ](https://img.shields.io/crates/l/librashader )
![Nightly rust ](https://img.shields.io/badge/rust-nightly-orange.svg )
2022-11-14 17:49:51 +11:00
2024-02-04 16:43:06 +11:00
## Installation
For end-users, librashader is available from the [Open Build Service ](https://software.opensuse.org//download.html?project=home%3Achyyran%3Alibrashader&package=librashader ) for a variety of Linux distributions and platforms. Windows users can grab the latest DLL from [GitHub Releases ](https://github.com/SnowflakePowered/librashader/releases ).
2022-11-22 10:49:51 +11:00
## Supported Render APIs
2023-02-10 17:29:49 +11:00
librashader supports OpenGL 3, OpenGL 4.6, Vulkan, Direct3D 11, and Direct3D 12. Metal and WebGPU
are not currently supported (but pull-requests are welcome). librashader does not support legacy render
APIs such as older versions of OpenGL, or legacy versions of Direct3D.
2022-11-22 10:49:51 +11:00
2022-11-27 15:36:59 +11:00
| **API** | **Status** | ** `librashader` feature** |
2023-02-10 17:29:49 +11:00
|-------------|------------|--------------------------|
| OpenGL 3.3+ | ✔ | `gl` |
| OpenGL 4.6 | ✔ | `gl` |
| Vulkan | ✔ | `vk` |
| Direct3D 11 | ✔ | `d3d11` |
| Direct3D 12 | ✔ | `d3d12` |
| Metal | ❌ | |
| WebGPU | ❌ | |
✔ = Render API is supported — ❌ Render API is not supported
2023-02-17 09:33:56 +11:00
2022-11-24 17:37:16 +11:00
## Usage
2023-01-19 17:23:51 +11:00
librashader provides both a Rust API under the `librashader` crate, and a C API. Both APIs are first-class and fully supported.
The C API is geared more towards integration with existing projects. The Rust `librashader` crate exposes more
of the internals if you wish to use parts of librashader piecemeal.
2022-11-24 17:37:16 +11:00
2023-02-08 10:50:26 +11:00
The librashader C API is best used by including `librashader_ld.h` in your project, which implements a loader that dynamically
2023-01-27 11:14:38 +11:00
loads the librashader (`librashader.so` or `librashader.dll` ) implementation in the search path.
2022-11-22 10:49:51 +11:00
2023-02-17 09:33:56 +11:00
2023-02-20 07:38:11 +11:00
### C compatibility
The recommended way of integrating `librashader` is by the `librashader_ld` single header library which implements
a dynamic loader for `librashader.dll` / `librashader.so` . See the [versioning policy ](https://github.com/SnowflakePowered/librashader#versioning )
for details on how librashader handles C ABI and API stability with regards to library updates.
2023-02-17 09:33:56 +11:00
Linking statically against `librashader.h` is possible, but is not officially supported. You will need to ensure
linkage parameters are correct in order to successfully link with `librashader.lib` or `librashader.a` .
The [corrosion ](https://github.com/corrosion-rs/ ) CMake package is highly recommended.
### Thread safety
In general, it is **safe** to create a filter chain instance from a different thread, but drawing frames requires
**external synchronization** of the filter chain object.
Filter chains can be created from any thread, but requires external synchronization of the graphics device queue where applicable
(in Direct3D 11, the immediate context is considered the graphics device queue), as loading LUTs requires command submission to the GPU.
Initialization of GPU resources may be deferred asynchronously using the `filter_chain_create_deferred` functions, but the caller is responsible for
submitting the recorded commands to the graphics device queue, and **ensuring that the work is complete** before drawing shader pass frames.
OpenGL has an additional restriction where creating the filter chain instance in a different thread is safe **if and only if**
the thread local OpenGL context is initialized to the same context as the drawing thread. Support for deferral of GPU resource initialization
is not available to OpenGL.
### Quad vertices and rotations
All runtimes except OpenGL render with an identity matrix MVP and a VBO for with range `[-1, 1]` . The final pass uses a
Quad VBO with range `[0, 1]` and the following projection matrix by default.
```rust
static DEFAULT_MVP: &[f32; 16] = & [
2.0, 0.0, 0.0, 0.0,
0.0, 2.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
-1.0, -1.0, 0.0, 1.0,
];
```
As with RetroArch, a rotation on this MVP will be applied only on the final pass for these runtimes. This is the only way to
pass orientation information to shaders.
The OpenGL runtime uses a VBO for range `[0, 1]` for all passes and the following MVP for all passes.
```rust
static GL_DEFAULT_MVP: &[f32; 16] = & [
2.0, 0.0, 0.0, 0.0,
0.0, 2.0, 0.0, 0.0,
0.0, 0.0, 2.0, 0.0,
-1.0, -1.0, 0.0, 1.0,
];
```
2023-01-15 09:29:26 +11:00
### Building
2023-02-06 18:20:11 +11:00
2023-02-08 12:05:03 +11:00
librashader requires the following build time dependencies
2023-02-07 19:02:13 +11:00
2023-02-08 12:05:03 +11:00
* The [Vulkan SDK ](https://www.lunarg.com/vulkan-sdk/ )
* [CMake 3.8 or later ](https://cmake.org/ )
2023-11-29 19:02:14 +11:00
For DXIL support on Windows, the following is also needed
* [Meson ](https://mesonbuild.com/ )
2023-02-08 12:05:03 +11:00
* [Python 3.6 or later ](https://www.python.org/ )
2023-02-07 19:02:13 +11:00
2023-02-06 18:20:11 +11:00
---
2023-02-06 18:17:30 +11:00
2023-11-29 19:02:14 +11:00
For Rust projects, simply add the crate tofil your `Cargo.toml` .
2023-02-06 17:42:43 +11:00
2023-01-15 09:29:26 +11:00
```
cargo add librashader
```
2023-02-12 10:05:50 +11:00
To build the C compatible dynamic library, run the build script.
2023-01-15 09:29:26 +11:00
```
2023-02-12 10:05:50 +11:00
cargo run -p librashader-build-script -- --profile optimized
2023-01-15 09:29:26 +11:00
```
2023-02-12 10:05:50 +11:00
This will output a `librashader.dll` or `librashader.so` in the target folder. Profile can be `debug` , `release` , or
`optimized` for full LTO.
2023-01-15 09:29:26 +11:00
2023-02-17 09:33:56 +11:00
### Writing a librashader Runtime
2022-12-06 09:30:48 +11:00
2023-02-17 09:33:56 +11:00
If you wish to contribute a runtime implementation not already available, see the [librashader-runtime ](https://docs.rs/librashader-runtime/latest/librashader_runtime/ )
crate for helpers and shared logic used across all librashader runtime implementations. Using these helpers and traits will
ensure that your runtime has consistent behaviour for uniform and texture semantics bindings with the existing librashader runtimes.
2022-12-06 09:30:48 +11:00
2023-02-17 09:33:56 +11:00
These types should not be exposed to the end user in the runtime's public API, and should be kept internal to the implementation of
the runtime.
2023-01-14 10:23:31 +11:00
2023-02-17 09:33:56 +11:00
## Examples
2023-01-27 15:18:10 +11:00
The following Rust examples show how to use each librashader runtime.
2023-02-16 17:23:01 +11:00
* [Vulkan ](https://github.com/SnowflakePowered/librashader/blob/master/librashader-runtime-vk/tests/triangle.rs )
* [OpenGL ](https://github.com/SnowflakePowered/librashader/blob/master/librashader-runtime-gl/tests/triangle.rs )
* [Direct3D 11 ](https://github.com/SnowflakePowered/librashader/blob/master/librashader-runtime-d3d11/tests/triangle.rs )
* [Direct3D 12 ](https://github.com/SnowflakePowered/librashader/blob/master/librashader-runtime-d3d12/tests/triangle.rs )
2023-01-27 15:18:10 +11:00
Some basic examples on using the C API are also provided in the [librashader-capi-tests ](https://github.com/SnowflakePowered/librashader/tree/master/test/capi-tests/librashader-capi-tests )
directory.
2022-11-27 15:36:59 +11:00
## Compatibility
2023-02-06 17:25:06 +11:00
librashader implements the entire RetroArch shader pipeline and is highly compatible with existing shaders.
2022-11-27 15:36:59 +11:00
Please report an issue if you run into a shader that works in RetroArch, but not under librashader.
* Filter chains do not terminate at the backbuffer.
* Unlike RetroArch, librashader does not have full knowledge of the entire rendering state and is designed to be pluggable
at any point in your render pipeline. Instead, filter chains terminate at a caller-provided output surface and viewport.
It is the caller's responsibility to blit the surface back to the backbuffer.
2023-02-08 10:50:26 +11:00
* Shaders are compiled in parallel where possible. This should noticeably decrease preset compile times.
Parallel shader compilation is not available to OpenGL.
* HDR10 support is not part of any shader runtime and is not supported by librashader.
2023-02-10 13:03:55 +11:00
* For performance reasons, mipmaps are never generated for the input texture. In theory, this means that
presets with `mipmap_input0 = "true"` will not get a mipmapped input. In practice, no known shader presets set
2023-02-16 10:50:35 +11:00
`mipmap_input0 = "true"` .
* The preset parser is a substantially stricter implementation that the one in RetroArch. Not all shader presets may be
compatible. If you find this is the case, please file an issue so a workaround can be added.
2023-02-08 10:50:26 +11:00
### Runtime specific differences
* OpenGL
* Copying of in-flight framebuffer contents to history is done via `glBlitFramebuffer` rather than drawing a quad into an intermediate FBO.
* Sampler objects are used rather than `glTexParameter` .
* Sampler inputs and outputs are not renamed. This is useful for debugging shaders in RenderDoc.
* UBO and Push Constant Buffer sizes are padded to 16-byte boundaries.
* OpenGL 4.6+
* All caveats from the OpenGL 3.3+ section should be considered.
* Should work on OpenGL 4.5 but this is not guaranteed. The OpenGL 4.6 runtime may eventually switch to using `ARB_spirv_extensions` for loading shaders, and this will not be marked as a breaking change.
* The OpenGL 4.6 runtime uses Direct State Access to minimize changes to the OpenGL state. For GPUs released within the last 5 years, this may improve performance.
* Vulkan
* The Vulkan runtime uses [`VK_KHR_dynamic_rendering` ](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_dynamic_rendering.html ) by default.
This extension must be enabled at device creation. Explicit render passes can be used by configuring filter chain options, but may have reduced performance
compared to dynamic rendering.
2023-02-10 10:11:34 +11:00
* Allocations within the runtime are done through [gpu-allocator ](https://github.com/Traverse-Research/gpu-allocator ) rather than handled manually.
2023-02-08 10:50:26 +11:00
* Direct3D 11
* Framebuffer copies are done via `ID3D11DeviceContext::CopySubresourceRegion` rather than a CPU conversion + copy.
* Direct3D 12
* The Direct3D 12 runtime uses [render passes ](https://learn.microsoft.com/en-us/windows/win32/direct3d12/direct3d-12-render-passes ). This feature has been available since Windows 10 version 1809,
which was released in late 2018.
* For maximum compatibility with shaders, a shader compile pipeline based on [`spirv-to-dxil` ](https://github.com/SnowflakePowered/spirv-to-dxil-rs ) is used, with the SPIRV-Cross HLSL pipeline used as a fallback.
2023-02-11 17:09:25 +11:00
This brings shader compatibility beyond what the RetroArch Direct3D 12 driver provides. The HLSL pipeline fallback may be removed in the future as `spirv-to-dxil` improves.
2023-02-19 15:14:30 +11:00
* The Direct3D 12 runtime requires `dxil.dll` and `dxcompiler.dll` from the [DirectX Shader Compiler ](https://github.com/microsoft/DirectXShaderCompiler ).
2023-02-10 10:39:02 +11:00
2022-11-29 10:56:35 +11:00
Most, if not all shader presets should work fine on librashader. The runtime specific differences should not affect the output,
2023-01-12 17:28:02 +11:00
and are more a heads-up for integrating librashader into your project.
2022-12-11 17:19:25 +11:00
2023-02-17 09:33:56 +11:00
## Versioning
[![Latest Version ](https://img.shields.io/crates/v/librashader.svg )](https://crates.io/crates/librashader)
![C ABI ](https://img.shields.io/badge/ABI%20version-1-yellowgreen )
![C API ](https://img.shields.io/badge/API%20version-0-blue )
2023-02-08 10:50:26 +11:00
2023-02-17 09:33:56 +11:00
librashader typically follows [Semantic Versioning ](https://semver.org/ ) with respect to the Rust API, where a minor version
number bump indicates a 'breaking change' during `0.x.y` , and a non-'breaking change' after `1.x.y` . However, a
"breaking change" that results in a version number bump does not correspond to a break in the C API after version 0.1.0.
2023-02-08 10:50:26 +11:00
2023-02-17 09:33:56 +11:00
The C API is instead versioned separately with two monotonically increasing version numbers exported to the librashader
C headers
2023-02-13 18:32:29 +11:00
2023-02-17 09:33:56 +11:00
- [`LIBRASHADER_CURRENT_VERSION` ](https://github.com/SnowflakePowered/librashader/blob/f8de1fa2ee75270e5655284edf39ea070d6ec6f5/librashader-capi/src/version.rs#L16 ) specifies the *API* version exported by the librashader implementation.
- [`LIBRASHADER_CURRENT_ABI` ](https://github.com/SnowflakePowered/librashader/blob/f8de1fa2ee75270e5655284edf39ea070d6ec6f5/librashader-capi/src/version.rs#L29 ) specifies the *ABI* version exported by the librashader implementation.
2023-02-11 18:42:33 +11:00
2023-02-17 09:33:56 +11:00
An increase in `LIBRASHADER_CURRENT_VERSION` is guaranteed to be **backwards compatible for the same `LIBRASHADER_CURRENT_ABI`** .
It somewhat corresponds to a "minor" version in semantic versioning terminology, except that it is always monotonically increasing.
Backwards-compatible additions to the C API will result in an increase to `LIBRASHADER_CURRENT_VERSION` .
2023-02-10 17:29:49 +11:00
2023-02-17 09:33:56 +11:00
APIs introduced after a certain `LIBRASHADER_CURRENT_VERSION` **may or may not** be available to prior versions. In particular, new features enabled by
filter or frame option structs **require `LIBRASHADER_CURRENT_VERSION` be the greater than or equal** to the version in which the option was
introduced, or a default value will be passed, which may or may not enable the feature depending on backwards compatibility for
that particular feature.
2023-01-19 17:23:51 +11:00
2023-02-17 09:33:56 +11:00
Any change to `LIBRASHADER_CURRENT_ABI` indicates a **breaking change** for the C **ABI** . For safety reasons, `librashader_ld.h`
will check to ensure that `LIBRASHADER_CURRENT_ABI` matches that of the loaded librashader binary. If it does not match,
librashader will not load. A value of `0` for `LIBRASHADER_CURRENT_ABI` indicates the "null" instance where every operation
2023-02-17 17:52:53 +11:00
is a no-op, which occurs if no compatible librashader implementation could be found.
2023-01-19 17:23:51 +11:00
2024-02-03 15:46:18 +11:00
The `SONAME` of `librashader.so` when installed via package manager is set to `LIBRASHADER_CURRENT_ABI` .
2023-02-17 09:33:56 +11:00
The above does not apply to releases of librashader prior to `0.1.0` , which were allowed to break API and ABI compatibility
in both the Rust and C API without an increase to either `LIBRASHADER_CURRENT_VERSION` or `LIBRASHADER_CURRENT_ABI` .
2023-01-19 17:23:51 +11:00
2022-11-14 17:49:51 +11:00
## License
2022-11-22 12:45:57 +11:00
The core parts of librashader such as the preprocessor, the preset parser,
the reflection library, and the runtimes, are all licensed under the Mozilla Public License version 2.0.
2022-11-14 17:49:51 +11:00
2023-02-06 17:25:06 +11:00
The librashader C API, i.e. its headers and definitions, *not its implementation in `librashader-capi`* ,
2022-11-24 17:37:16 +11:00
are more permissively licensed, and may allow you to use librashader in your permissively
2022-11-14 17:49:51 +11:00
licensed or proprietary project.
2022-11-24 17:37:16 +11:00
To facilitate easier use of librashader in projects incompatible with MPL-2.0, `librashader_ld`
2022-12-10 17:59:54 +11:00
implements a loader which thunks its calls to any `librashader.so` or `librashader.dll`
2022-11-24 17:37:16 +11:00
library found in the load path. A non-MPL-2.0 compatible project may link against
2022-12-10 17:59:54 +11:00
`librashader_ld` to use the librashader runtime, *provided that `librashader.so` or `librashader.dll`
2022-11-24 17:37:16 +11:00
are distributed under the restrictions of MPLv2*.
2022-11-14 17:49:51 +11:00
2023-01-30 13:59:27 +11:00
Note that this means that if your project is unable to comply with the requirements of MPL-2.0,
you **can not distribute `librashader.so` or `librashader.dll`** alongside your project.
The end user must obtain the implementation of librashader themselves. For more information,
see the [MPL 2.0 FAQ ](https://www.mozilla.org/en-US/MPL/2.0/FAQ/ ).
2022-11-22 12:45:57 +11:00
2023-01-30 13:59:27 +11:00
At your discretion, you may instead choose to distribute `librashader` under the terms of GPLv3 rather than MPL-2.0.