bevyengine / bevyengine/bevy

Add shader libraries with shared linking

Open
#21,932 2 comments 0 reactions 0 assignees View on GitHub
A-Rendering C-Feature D-Shaders S-Blocked
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## What problem does this solve or what need does it fill?

Shader stages that are very redundant and or small, like vertex shaders, tend not to scale well. There is no way to share a vertex shader across multiple shaders meaning they end up getting compiled many times redundantly.

This cost adds up as a game scales to hundreds or even thousands of shader material variants. Shared linking compiles faster and reduces compiled binary size in comparison to shader includes using a preprocessor.

## What solution would you like?

A flag or boolean property to mark a shader as a "shared library" so it will be linked instead of copied and pasted.

## What alternative(s) have you considered?

Unified Shaders (aka Ubershaders, who's name I don't like) but they are not as lean and efficient as the final compiled shader variants.

## Additional context

For Vulkan the shared linking can be implemented two ways, the first with `spirv-link` which is part of the SPIR-V Tools to combine the two SPIR-V modules together. The linker, just like in older GL, will in practice _**aggressively**_ optimize away unused parts of the library.

Their repo specifically mentions this as a use case:
>* Combine into a **library** (exports are retained) or an executable (no symbols are exported).

https://github.com/KhronosGroup/SPIRV-Tools?tab=readme-ov-file#linker

There also seems to be `VK_EXT_graphics_pipeline_library` which gives VK the same behavior as old GL with multiple attachments. From what I can tell it seems to be relatively well supported, even Valve's Source 2 uses it apparently.

For older OpenGL 2 and stuff, it will leverage the fact that OpenGL API allows 2 or more attachments per shader stage.

> It is **permissible** to attach _**multiple shader objects of the same type**_ because each may contain a portion of the complete shader. It is also permissible to attach a shader object to more than one program object.
https://registry.khronos.org/OpenGL-Refpages/gl4/html/glAttachShader.xhtml

This is typically the approach taken by other frameworks to create true shader libraries with shared linkage.

Contributor guide

Open the contributing guide

Research direction

No files, tests, or entry points are named. Start by reviewing the cited SPIR-V Tools linker documentation and VK_EXT_graphics_pipeline_library, then compare the OpenGL multi-attachment approach; done means agreeing on and implementing a supported shared-library design across the relevant graphics backends.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
computer-graphics, game-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.