• Stars
    star
    1,729
  • Rank 26,964 (Top 0.6 %)
  • Language
    C++
  • License
    Other
  • Created over 14 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GLSL optimizer based on Mesa's GLSL compiler. Used to be used in Unity for mobile shader optimization.

GLSL optimizer

⚠️ As of mid-2016, the project is unlikely to have any significant developments. At Unity we are moving to a different shader compilation pipeline, with glsl-optimizer is not used. So from my side there won't be significant work done on it. ⚠️

A C++ library that takes GLSL shaders, does some GPU-independent optimizations on them and outputs GLSL or Metal source back. Optimizations are function inlining, dead code removal, copy propagation, constant folding, constant propagation, arithmetic optimizations and so on.

Apparently quite a few mobile platforms are pretty bad at optimizing shaders; and unfortunately they also lack offline shader compilers. So using a GLSL optimizer offline before can make the shader run much faster on a platform like that. See performance numbers in this blog post.

Even for drivers that have decent shader optimization, GLSL optimizer could be useful to just strip away dead code, make shaders smaller and do uniform/input reflection offline.

Almost all actual code is Mesa 3D's GLSL compiler; all this library does is spits out optimized GLSL or Metal back, and adds GLES type precision handling to the optimizer.

This GLSL optimizer is made for Unity's purposes and is built-in starting with Unity 3.0.

GLSL Optimizer is licensed according to the terms of the MIT license.

See change log here.

Usage

Visual Studio 2010 (Windows, x86/x64) and Xcode 5+ (Mac, i386) project files for a static library are provided in projects/vs2010/glsl_optimizer.sln and projects/xcode5/glsl_optimizer_lib respectively.

Note: only the VS and Xcode project files are maintained and should work at any time. There's also a cmake and gyp build system for Linux et al., and some stuff in contrib folder - all that may or might not work.

For Linux you can use cmake. Just type "cmake . && make" in the root directory. This will build the optimizer library and some executable binaries.

Interface for the library is src/glsl/glsl_optimizer.h. General usage is:

ctx = glslopt_initialize(targetVersion);
for (lots of shaders) {
	shader = glslopt_optimize (ctx, shaderType, shaderSource, options);
	if (glslopt_get_status (shader)) {
		newSource = glslopt_get_output (shader);
	} else {
		errorLog = glslopt_get_log (shader);
	}
	glslopt_shader_delete (shader);
}
glslopt_cleanup (ctx);

Tests

There's a testing suite for catching regressions, see tests folder. In VS, build and run glsl_optimizer_tests project; in Xcode use projects/xcode5/glsl_optimizer_tests project. The test executable requires path to the tests folder as an argument.

Each test comes as three text files; input, expected IR dump and expected optimized GLSL dump. GLES3 tests are also converted into Metal.

If you're making changes to the project and want pull requests accepted easier, I'd appreciate if there would be no test suite regressions. If you are implementing a feature, it would be cool to add tests to cover it as well!

Notes

  • GLSL versions 1.10 and 1.20 are supported. 1.10 is the default, use #version 120 to specify 1.20. Higher GLSL versions might work, but aren't tested now.
  • GLSL ES versions 1.00 and 3.00 are supported.

Dev Notes

Pulling Mesa upstream:

git fetch upstream
git merge upstream/master
sh removeDeletedByUs.sh
# inspect files, git rm unneeded ones, fix conflicts etc.
# git commit

Rebuilding flex/bison parsers:

  • When .y/.l files are changed, the parsers are not rebuilt automatically,
  • Run ./generateParsers.sh to do that. You'll need bison & flex (on Mac, do "Install Command Line Tools" from Xcode)
  • I use bison 2.3 and flex 2.5.35 (in OS X 10.8/10.9)

More Repositories

1

UnityGaussianSplatting

Toy Gaussian Splatting visualization in Unity
C#
2,025
star
2

ToyPathTracer

Toy path tracer for my own learning purposes (CPU/GPU, C++/C#, Win/Mac/Wasm, DX11/Metal, also Unity)
C++
1,058
star
3

ClangBuildAnalyzer

Clang build analysis tool using -ftime-trace
C++
979
star
4

hlsl2glslfork

HLSL to GLSL language translator based on ATI's HLSL2GLSL. Used in Unity.
C++
558
star
5

dod-playground

Sample OOP/ECS/DOD project (C++) for an internal Unity lecture in 2018
C++
366
star
6

smol-v

SMOL-V: like Vulkan/Khronos SPIR-V, but smaller.
C++
296
star
7

sizer

Win32/64 executable size reporting
C++
252
star
8

ToyMeshPathTracer

Toy Mesh Path Tracer that I used as a base for job interview tasks
C++
119
star
9

UnityGPUTexCompression

How to do DXT/BCn texture compression in Unity using compute shaders
C#
66
star
10

gamedev-emojis

128x128 sized icons of various game development things
Python
60
star
11

miniexr

Small code to write OpenEXR images
C++
58
star
12

demo-pd-cranktheworld

A real-time rendering demo for Playdate console
C
56
star
13

smol-atlas

2D rectangular bin packing with option for item removal
C++
53
star
14

UnityStbEasyFont

C#
47
star
15

HashFunctionsTest

Little test of various hash functions
C++
46
star
16

header_hero

Fork of Bitsquid's Header Hero tool
C#
41
star
17

BlackHoleDemo

C#
27
star
18

smol-compute

A tiny library for launching compute shaders on D3D11, Metal and Vulkan
C
26
star
19

markdeep-docs-style

Documentation CSS style for Markdeep
CSS
25
star
20

dingus

engine used in some nesnausk! demos
C++
23
star
21

glsl-load-speed

Quick test of GLSL shader loading speed
C++
17
star
22

obj_parse_tester

Comparing various C++ OBJ parsing libraries
C
17
star
23

bcn_decoder_tester

Testing various BCn texture format decoding libraries
C++
16
star
24

jobtask2019-trimesh-tracer

A programming task for job interviews I did in 2019 Q2
C++
16
star
25

bc7e-on-gpu

An attempt to port BC7E texture compressor to a GPU compute shader
HLSL
15
star
26

float_compr_tester

Testing various libraries/approaches for compressing floating point data
HTML
14
star
27

aras-p.hugo

This is just my website, content & Hugo bits
HTML
9
star
28

oklab_gradient_test

C++
6
star
29

sprint-fuuu

C++
6
star
30

sideshooter50

A game in 50 lines of code
4
star
31

twod_coverage_raster

Super simplistic 2D triangle coverage rasterizer
C
4
star
32

SoftRenderBlogProject

C
3
star
33

trianglomator

C#
3
star
34

dod-lecture-2018

C#
3
star
35

ggj19-rumba

A small game during Global Game Jam 2019
C#
3
star
36

filteression

C++
2
star
37

hgext

misc hg extension stuff
Python
1
star
38

website

PHP
1
star
39

image-formats-testbed-hack

C++
1
star