Using binary shaders
By default Kanzi uses online shaders, which Kanzi exports to an application as source code and which the target platform compiles into executable programs during application loading. This compilation takes time and increases the loading time of your application. To decrease the loading times of your Kanzi application you can:
Compiling and storing shader binaries on the target device
If you want to compile and store shader binaries on the target device keep in mind:
- When you compile shaders in Kanzi Studio, configuring your application to compile and store compiled shader binaries on the device has no effect.
- Disable the storing of compiled shader binaries if your Kanzi application is not allowed to write to the device file system.
To compile and store shader binaries on the target device:
- In the directory where you store your Kanzi application create application.cfg file and add to the file:
# Enables caching of compiled shader binaries and compiles the shader
# binaries when you run your Kanzi application for the first time.
UseShaderCaching = 1
# Stores the compiled shader binaries to directory ShaderCache,
# which is in the same directory as your Kanzi application.
ShaderCacheDirectory = "ShaderCache"
or
- In the C application of your Kanzi application add to the
kzApplicationConfigure() function:
// Enables caching of compiled shader binaries and compiles the shader
// binaries when you run your Kanzi application for the first time.
configuration->useShaderCaching = KZ_TRUE;
// Stores the compiled shader binaries to directory ShaderCache,
// which is in the same directory as your Kanzi application.
configuration->shaderCacheDirectory = "ShaderCache";
See Reference for application configuration.
Compiling shaders in Kanzi Studio using a common binary shader format
To compile shaders in Kanzi Studio using a common binary shader format:
- Use the binary shader compiler provided by the GPU vendor of your target device. You can get the compiler from the vendor's SDK kit.
- In the Library right-click Materials and Textures > Shader Formats and select Create Binary Shader Format.
- Name the binary shader format after your target platform:
- Tegra if you are using NVidia's Tegra binary shader (GL_NVIDIA_PLATFORM_BINARY_NV)
- IMG if you are using Imagination Technologies' SGX 5xx binary shader (GL_SGX_BINARY_IMG)
- AMD if you are using AMD's AMDZ400 binary shader (GL_Z400_BINARY_AMD)
To use any other shader, see Compiling shaders in Kanzi Studio using a non-standard binary shader format.
- In the Properties set the properties for the binary shader compiler your are using.
For example, when using NVidia's Tegra platforms set these properties:- Enable the Enabled property
- Vertex Shader Compile to
..\BinaryShaderCompilers\Tegra\glslv.bat <vertexshadercode> <vertexshaderbinary> - Fragment Shader Compile to
..\BinaryShaderCompilers\Tegra\glslf.bat <fragmentshadercode> <fragmentshaderbinary>
This embeds the binary versions of all shaders in your project to the .kzb.
You can set the command line for the binary shader compilation either for each shader separately or as a combined command for both shaders. For combined command use Combined Compile property.
- To automatically compile a shader each time you edit and save it, enable the Enabled property.
-
(Optional) If you are deploying your Kanzi application to more than one hardware platform:
- Create a profile for each platform. See Profiles.
- In the Library select the binary shader format used by the hardware platform.
- In the Properties enable the Enabled property.
Compiling shaders in Kanzi Studio using a non-standard binary shader format
If you have a compiler available, you can compile any shader format.
To compile shaders in Kanzi Studio using a non-standard shader format:
- In the Library right-click Materials and Textures > Shader Formats and select Create Binary Shader Format.
- Name the binary shader format after your target platform.
For example, MyShaderFormat.
- In the Properties set the properties for the binary shader compiler your are using.
For example, see Compiling shaders in Kanzi Studio using a common binary shader format.
- In the Kanzi Engine API call
kzcRendererSetBinaryShaderFormatIdentifier(coreRednerer, "MyShaderFormat",
shaderBinaryGLIdentitfier)
shaderBinaryGLIdentifier is platform specific and you can get it from the platform GL extension header. For example, the constant on Tegra platform is 0x890B.
Now Kanzi compiles and deploys all shaders in the .kzb to identifier MyShaderFormat.
- To automatically compile a shader each time you edit and save the shader, enable the Enabled property.
- (Optional) If you are deploying your Kanzi application to a hardware platform that requires the use of
OES_get_program_binary OpenGL extension:- Set the Combined Compile property, but leave both Vertex Shader Compile and Fragment Shader Compile properties empty.
- Call in your application's startup routine
kzcRendererSetProgramBinaryExtensionEnabled(coreRenderer, KZ_TRUE);
-
(Optional) If you are deploying your Kanzi application to more than one hardware platform:
- Create a profile for each platform. See Profiles.
- In the Library select the binary shader format used by the hardware platform.
- In the Properties enable the Enabled property.
See also
Reducing shader switches
Optimizing pixel shaders
Loading resources in parallel
Shaders best practices
Measuring the performance of your Kanzi application
Best practices
Open topic with navigation