Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

wenjieshen
Copy link
Collaborator

@wenjieshen wenjieshen commented Oct 17, 2025

We've implemented a comprehensive binary shader caching system to significantly improve OpenGL rendering performance on macOS by eliminating redundant shader compilation overhead on subsequent application runs.

This feature introduces the GlShaderCache class that provides persistent storage and retrieval of compiled OpenGL shader binaries. The system automatically caches compiled shader programs to the user's cache directory using a hash-based naming scheme and attempts to load cached binaries before falling back to source compilation.

Key implementation details:

  • Utilizes OpenGL binary shader format support via glGetProgramBinary/glProgramBinary extensions
  • Stores cached binaries in the System cache folder on macOS and Windows; other platforms are testing
  • Integrates with the existing GlProgram compilation pipeline

The caching mechanism works transparently:

  1. When a shader program is first compiled, the binary is automatically cached
  2. On subsequent runs, the system attempts to load the cached binary
  3. If successful, compilation time is virtually eliminated
  4. If cache miss or load failure occurs, falls back to normal source compilation

Performance benefits:

  • Eliminates shader compilation stutters during runtime
  • No API changes required - caching works automatically

issue: #2884

@github-actions github-actions bot added infrastructure Dev infrastructure gl OpenGL/WebGL render backend labels Oct 17, 2025
@wenjieshen wenjieshen force-pushed the exp/binary-shader-support-mac branch from a821049 to 2dcd2cf Compare October 17, 2025 10:23
@github-actions github-actions bot removed the infrastructure Dev infrastructure label Oct 17, 2025
@wenjieshen wenjieshen force-pushed the exp/binary-shader-support-mac branch from 2dcd2cf to 9941559 Compare October 17, 2025 10:26
@wenjieshen wenjieshen changed the title gl_engine/shader: Introduced binary shader caching for Destop gl_engine/shader: Introduced binary shader caching for Desktop [skip ci] Oct 17, 2025
@wenjieshen wenjieshen self-assigned this Oct 17, 2025
@hermet hermet requested a review from Copilot October 17, 2025 13:22
@hermet hermet added the optimization Enhance performance / Reduce memory usage or binary size label Oct 17, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a binary shader caching system for the OpenGL renderer to improve performance by eliminating redundant shader compilation on subsequent application runs. The system automatically stores compiled OpenGL shader binaries to disk and attempts to load them before falling back to source compilation.

  • Introduces GlShaderCache class with persistent binary shader storage and retrieval functionality
  • Integrates caching into existing GlProgram compilation pipeline with transparent fallback behavior
  • Adds OpenGL 4.1+ binary shader API support via glGetProgramBinary/glProgramBinary extensions

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tvgGlShaderCache.h Defines GlShaderCache class interface for binary shader caching operations
tvgGlShaderCache.cpp Implements shader cache functionality with file I/O and hash-based naming
tvgGlProgram.cpp Integrates shader caching into program compilation with cache-first loading
tvgGl.h Adds OpenGL 4.1 binary shader API constants and function pointer declarations
tvgGl.cpp Implements dynamic loading of binary shader API functions with version checking
meson.build Adds new shader cache source files to build configuration

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@wenjieshen wenjieshen force-pushed the exp/binary-shader-support-mac branch 5 times, most recently from 94fa894 to 95cb9bd Compare October 19, 2025 13:45
@wenjieshen
Copy link
Collaborator Author

wenjieshen commented Oct 20, 2025

@hermet @SergeyLebedkin
Thanks for reviewing. I’d like to confirm two points:

Binary shader support is an OpenGL 4.1+ feature, while our requirement is OpenGL 3. I added a helper function glIsProgramBinarySupportAvailable() in tvgGl.h to check runtime availability. This is the first runtime capability check in the project, so I’m not sure if this approach aligns with our design principles.

Although my MacBook Air supports OpenGL 4.1, it still returns GL_NUM_PROGRAM_BINARY_FORMATS = 0, meaning binary shader support isn’t available. I’m waiting for GPU access on a cloud platform to test the performance improvement in another environment.

While waiting, I’ll continue investigating the stroke rendering issue on the GL engine.

@wenjieshen wenjieshen marked this pull request as ready for review October 20, 2025 02:30
@hermet
Copy link
Member

hermet commented Oct 20, 2025

Binary shader support is an OpenGL 4.1+ feature, while our requirement is OpenGL 3. I added a helper function glIsProgramBinarySupportAvailable() in tvgGl.h to check runtime availability. This is the first runtime capability check in the project, so I’m not sure if this approach aligns with our design principles.

@wenjieshen Looks good, if the binary shaders doesn't working, we can fallback to a normal scenario.

@hermet
Copy link
Member

hermet commented Oct 20, 2025

@wenjieshen Could you please double-check this as well? a few systems might use this GL_ARB_get_program_binary for gl3 case. But please hold this, Maybe we can bump up the opengl dependency to v4 as the minimum requirement... If so, we can drop the v3 use-case.

@thorvg thorvg deleted a comment from huming2207 Oct 20, 2025
@thorvg thorvg deleted a comment from huming2207 Oct 20, 2025
@wenjieshen
Copy link
Collaborator Author

@wenjieshen Could you please hold this? I think we can discuss the opengl dependency further. Maybe we can bump up the opengl dependency to v4 as the minimum requirement... If so, we can drop the v3 use-case.

@hermet Understood, I’ll pause the PR.
Agree that we should discuss the OpenGL dependency direction first. If we decide to move to 4.0+, we can remove the v3 fallback logic entirely.

@wenjieshen wenjieshen marked this pull request as draft October 20, 2025 03:29
@hermet hermet removed this from ThorVG Oct 20, 2025
@hermet
Copy link
Member

hermet commented Oct 20, 2025

Understood, I’ll pause the PR.

@wenjieshen I mean we can hold support v3 for binary shaders. I didn't mean hold this PR. Please go proceed. :) Thanks.

@wenjieshen wenjieshen marked this pull request as ready for review October 20, 2025 05:12
@hermet hermet changed the title gl_engine/shader: Introduced binary shader caching for Desktop [skip ci] gl_engine/shader: Introduced binary shader caching Oct 20, 2025
Copy link
Member

@hermet hermet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wenjieshen Please check comments and please respect thorvg coding convention.
Thanks.

@wenjieshen wenjieshen force-pushed the exp/binary-shader-support-mac branch 6 times, most recently from 9057eb4 to e7ff8e7 Compare October 21, 2025 03:43
Copy link
Member

@hermet hermet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most your comments are unnecesary. Don't need to add comments for saying code itself.
We'd rather leave comments when it needs a unusual, special context and purposes.

@wenjieshen wenjieshen force-pushed the exp/binary-shader-support-mac branch 3 times, most recently from 7ce8965 to 498162c Compare October 21, 2025 05:24
@wenjieshen wenjieshen force-pushed the exp/binary-shader-support-mac branch 3 times, most recently from 2a2f4db to 3b8869a Compare October 21, 2025 06:56
Copy link
Member

@SergeyLebedkin SergeyLebedkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments

#include "tvgGlShaderCache.h"

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should remove this line for windows or at least check for predefinisions
image

@hermet
Copy link
Member

hermet commented Oct 23, 2025

image

@wenjieshen Please check your commit message. I marked some inappropriate msg (recommend to remove), Thanks.

  • Add relative issue info in commit msg such as:
    issue: https://github.com/thorvg/thorvg/issues/2884

@wenjieshen wenjieshen force-pushed the exp/binary-shader-support-mac branch 2 times, most recently from 5f1bd2d to b775c14 Compare October 23, 2025 02:12
@wenjieshen
Copy link
Collaborator Author

@hermet Thank you. I have updated the commit message.

Implement binary shader caching system to improve OpenGL performance by:
- Add GlShaderCache class with read/write functionality for compiled shaders
- Support OpenGL binary shader format
- Add proper OpenGL ARB extension loading for glGetProgramBinary/glProgramBinary
- Integrate caching into GlProgram compilation pipeline

This reduces shader compilation overhead on subsequent application runs,
significantly improving startup performance for OpenGL-based rendering.

Related issue: thorvg#2884
@wenjieshen wenjieshen force-pushed the exp/binary-shader-support-mac branch from b775c14 to 3a7a8a6 Compare October 23, 2025 02:22
@wenjieshen wenjieshen marked this pull request as draft October 23, 2025 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gl OpenGL/WebGL render backend optimization Enhance performance / Reduce memory usage or binary size

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants