1*8975f5c5SAndroid Build Coastguard Worker# Shader substitution 2*8975f5c5SAndroid Build Coastguard Worker 3*8975f5c5SAndroid Build Coastguard WorkerANGLE provides two mechanisms for observing, modifying, and substituting 4*8975f5c5SAndroid Build Coastguard Workerthe application's shaders. This ability to interpose makes it easier to 5*8975f5c5SAndroid Build Coastguard Workerdiagnose bugs and to prototype new transforms in the shader translator. 6*8975f5c5SAndroid Build Coastguard Worker 7*8975f5c5SAndroid Build Coastguard Worker## Environment variables controlling reading/writing shaders to disk 8*8975f5c5SAndroid Build Coastguard Worker 9*8975f5c5SAndroid Build Coastguard WorkerFor both the source and translated shaders discussed below, the environment 10*8975f5c5SAndroid Build Coastguard Workervariable: 11*8975f5c5SAndroid Build Coastguard Worker 12*8975f5c5SAndroid Build Coastguard Worker``` 13*8975f5c5SAndroid Build Coastguard WorkerANGLE_SHADER_DUMP_PATH 14*8975f5c5SAndroid Build Coastguard Worker``` 15*8975f5c5SAndroid Build Coastguard Worker 16*8975f5c5SAndroid Build Coastguard Workerand the Android property: 17*8975f5c5SAndroid Build Coastguard Worker 18*8975f5c5SAndroid Build Coastguard Worker``` 19*8975f5c5SAndroid Build Coastguard Workerdebug.angle.shader_dump_path 20*8975f5c5SAndroid Build Coastguard Worker``` 21*8975f5c5SAndroid Build Coastguard Worker 22*8975f5c5SAndroid Build Coastguard Workerspecify the directory in which shader sources and translated shaders will 23*8975f5c5SAndroid Build Coastguard Workerbe written to, and, in the case of shader substitution, read from. For 24*8975f5c5SAndroid Build Coastguard Workerexample, on non-Android platforms: 25*8975f5c5SAndroid Build Coastguard Worker 26*8975f5c5SAndroid Build Coastguard Worker``` 27*8975f5c5SAndroid Build Coastguard Workermkdir -p /path/to/angle_shaders 28*8975f5c5SAndroid Build Coastguard Workerexport ANGLE_SHADER_DUMP_PATH=/path/to/angle_shaders 29*8975f5c5SAndroid Build Coastguard Worker``` 30*8975f5c5SAndroid Build Coastguard Worker 31*8975f5c5SAndroid Build Coastguard Workerwill write all data to the `angle_shaders` directory. 32*8975f5c5SAndroid Build Coastguard Worker 33*8975f5c5SAndroid Build Coastguard WorkerOn Android, it's necessary to set the `debug.angle.shader_dump_path` property 34*8975f5c5SAndroid Build Coastguard Workerand set up the SD card correctly. (Help expanding this documentation is 35*8975f5c5SAndroid Build Coastguard Workerappreciated!) 36*8975f5c5SAndroid Build Coastguard Worker 37*8975f5c5SAndroid Build Coastguard Worker## ESSL shader dumping and substitution 38*8975f5c5SAndroid Build Coastguard Worker 39*8975f5c5SAndroid Build Coastguard WorkerThe ANGLE feature `dumpShaderSource`, when enabled, writes all incoming 40*8975f5c5SAndroid Build Coastguard WorkerESSL shader sources to disk, in the shader dump directory specified 41*8975f5c5SAndroid Build Coastguard Workerabove. File names are computed by hashing the shader sources. Shaders will 42*8975f5c5SAndroid Build Coastguard Workeronly be written to disk if they were not loaded from disk via substitution, 43*8975f5c5SAndroid Build Coastguard Workerbelow. 44*8975f5c5SAndroid Build Coastguard Worker 45*8975f5c5SAndroid Build Coastguard WorkerThe ANGLE feature `enableShaderSubstitution`, when enabled, looks for a 46*8975f5c5SAndroid Build Coastguard Workerfile in the shader dump directory where the filename is the hash of the 47*8975f5c5SAndroid Build Coastguard Workerapplication's shader source, and substitutes its contents for the 48*8975f5c5SAndroid Build Coastguard Workerapplication's shader. This allows you to dump and edit these files at your 49*8975f5c5SAndroid Build Coastguard Workerleisure, and rerun the application to pick up the new versions of the 50*8975f5c5SAndroid Build Coastguard Workershaders. 51*8975f5c5SAndroid Build Coastguard Worker 52*8975f5c5SAndroid Build Coastguard WorkerIn Chromium, pass the following command line arguments to enable these 53*8975f5c5SAndroid Build Coastguard Workerfeatures: 54*8975f5c5SAndroid Build Coastguard Worker 55*8975f5c5SAndroid Build Coastguard Worker``` 56*8975f5c5SAndroid Build Coastguard Worker--enable-angle-features=dumpShaderSource 57*8975f5c5SAndroid Build Coastguard Worker--enable-angle-features=enableShaderSubstitution 58*8975f5c5SAndroid Build Coastguard Worker--enable-angle-features=dumpShaderSource,enableShaderSubstitution 59*8975f5c5SAndroid Build Coastguard Worker``` 60*8975f5c5SAndroid Build Coastguard Worker 61*8975f5c5SAndroid Build Coastguard WorkerYou must also specify `--disable-gpu-sandbox` to allow ANGLE to access 62*8975f5c5SAndroid Build Coastguard Workerthese on-disk files for reading and writing. **Do not** browse the open web 63*8975f5c5SAndroid Build Coastguard Workerwith this command line argument specified! 64*8975f5c5SAndroid Build Coastguard Worker 65*8975f5c5SAndroid Build Coastguard Worker## Translated shader dumping and substitution 66*8975f5c5SAndroid Build Coastguard Worker 67*8975f5c5SAndroid Build Coastguard WorkerThe translated shaders produced by ANGLE's shader translator can be dumped 68*8975f5c5SAndroid Build Coastguard Workerand substituted as well. This is especially useful when prototyping new 69*8975f5c5SAndroid Build Coastguard Workeroptimizations in the shader translator. 70*8975f5c5SAndroid Build Coastguard Worker 71*8975f5c5SAndroid Build Coastguard WorkerThis mechanism is relatively recent and has not been thoroughly tested. It 72*8975f5c5SAndroid Build Coastguard Workerwill likely not work in the situation where ANGLE dynamically recompiles 73*8975f5c5SAndroid Build Coastguard Workershaders internally. It should work with all text-based shader translator 74*8975f5c5SAndroid Build Coastguard Workerbackends (ESSL, GLSL, HLSL, and Metal). See comments in 75*8975f5c5SAndroid Build Coastguard Worker`src/libANGLE/Shader.cpp` describing the work needed to make this work with 76*8975f5c5SAndroid Build Coastguard Workerthe SPIR-V backend. 77*8975f5c5SAndroid Build Coastguard Worker 78*8975f5c5SAndroid Build Coastguard WorkerTranslated shaders go into the same shader dump directory specified above 79*8975f5c5SAndroid Build Coastguard Workerfor shader sources. To enable these features: 80*8975f5c5SAndroid Build Coastguard Worker 81*8975f5c5SAndroid Build Coastguard Worker``` 82*8975f5c5SAndroid Build Coastguard Worker--enable-angle-features=dumpTranslatedShaders,enableTranslatedShaderSubstitution --disable-gpu-sandbox 83*8975f5c5SAndroid Build Coastguard Worker``` 84*8975f5c5SAndroid Build Coastguard Worker 85*8975f5c5SAndroid Build Coastguard Worker## Putting it all together (example: macOS) 86*8975f5c5SAndroid Build Coastguard Worker 87*8975f5c5SAndroid Build Coastguard Worker``` 88*8975f5c5SAndroid Build Coastguard Workermkdir -p $HOME/tmp/angle_shaders 89*8975f5c5SAndroid Build Coastguard Workerexport ANGLE_SHADER_DUMP_PATH=$HOME/tmp/angle_shaders 90*8975f5c5SAndroid Build Coastguard Worker 91*8975f5c5SAndroid Build Coastguard Workerout/Release/Chromium.app/Contents/MacOS/Chromium --disable-gpu-sandbox --use-angle=metal --enable-angle-features=dumpShaderSource,enableShaderSubstitution,dumpTranslatedShaders,enableTranslatedShaderSubstitution 92*8975f5c5SAndroid Build Coastguard Worker``` 93*8975f5c5SAndroid Build Coastguard Worker 94*8975f5c5SAndroid Build Coastguard WorkerRun the application once to generate the shader dump. Edit source or 95*8975f5c5SAndroid Build Coastguard Workertranslated shaders as desired. Rerun with the same command line arguments 96*8975f5c5SAndroid Build Coastguard Workerto pick up the new versions of the shaders. 97*8975f5c5SAndroid Build Coastguard Worker 98*8975f5c5SAndroid Build Coastguard WorkerAlternatively, and especially if the application doesn't work with all of 99*8975f5c5SAndroid Build Coastguard Workerthe shaders in the substitution directory, make a new directory and copy in 100*8975f5c5SAndroid Build Coastguard Workeronly those source or translated shaders you want to substitute, and run: 101*8975f5c5SAndroid Build Coastguard Worker 102*8975f5c5SAndroid Build Coastguard Worker``` 103*8975f5c5SAndroid Build Coastguard Workerout/Release/Chromium.app/Contents/MacOS/Chromium --disable-gpu-sandbox --use-angle=metal --enable-angle-features=enableShaderSubstitution,enableTranslatedShaderSubstitution 104*8975f5c5SAndroid Build Coastguard Worker``` 105