C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

How To Use Skia Shader SKSL Shading Language Code in C++ Builder

The latest RAD Studio / C++ Builder 12 comes with a lot of new features, one of which is Skia graphics library support in C++. Skia is an open-source graphic library that provides to support a variety of hardware and software platforms. One of Skia’s amazing features is its support for the shader graphics SkSL Shading Language. That means you can use GPU Shaders with Skia in C++ Builder 12. In this post, we explain what a shader is, what is the SkSL Shading language, how you can create Skia shader code, and how you can run animations based on SkSL code in C++ Builder.

What is Skia in modern C++?

Skia is an open-source graphic library that provides support variety of hardware and software platforms. Skia is sponsored and managed by Google but is available for use by anyone under the BSD Free Software License. Mainly designed for Google Chrome and ChromeOS, Android, Flutter, Mozilla Firefox and Firefox OS, and many other products. The core components are done by the Skia development team but they consider contributions from any source.

What is a Shader?

In modern graphics, a Shader is a GPU based program that calculates pixel colors by using Shader tasks in GPU, such as levels of light, darkness, and color during the rendering of a 3D scene. This process is known as a shading process, and it is a GPU based computation. Modern graphic cards have evolved to perform a variety of specialized functions in computer graphics, and shaders can be used for special effects and video post-processing, as well as general-purpose computing on graphics processing units. They can be used in some GPU based multi-threaded operations. Here is a good example how your graphics are improved by shaders.

Image source <a href=httpsenwikipediaorgwikiPhong shading samplejpg target= blank rel=noopener nofollow title= data rel=lightbox image 0 data rl title= data rl caption=>enPhong shading samplejpg<a>

For example, the latest NVidia GPU RTX 4090 has 16384 shading units that have a total of 83 TFLOPS performance in its shader cores. In other words, shaders are power to graphical calculations, and in the last decade, they are very popular by 2D and 3D mathematicians, and computer programmers.

If you want to know what Shader examples are visit https://shaders.skia.org The Shadertoy website also has thousands of shader examples that you can modify for Skia SkSL code. You can find ShaderToy here: https://www.shadertoy.com

What is the SKSL Shading Language in C++?

SkSL codes are very similar to C/C++ code, and it is Skia’s shading languageSkRuntimeEffect is a Skia C++ object that can be used to create SkShader, SkColorFilter, and SkBlender objects with behavior controlled by SkSL code. Here are some SkSL code examples at Skia’s official page https://shaders.skia.org

The syntax of SkSL is very similar to GLSL. When using SkSL effects in your Skia application, there are important differences (from GLSL) to remember. Most of these differences are because of one basic fact: With GPU shading languages, you are programming a stage of the GPU pipeline. With SkSL, you are programming a stage of the Skia pipeline. Please see here for more details about https://skia.org/docs/user/sksl/

Here is a very simple SkSL code that changes the color of a pixel over time. Here we have vec2 fragCoord input which is the coordinates of x and y, and we have vec4 output which returns the color of pixel in ARGB form. This is how a shader works,

As you see it is very similar to C++ codes, so you can use and create more mathematical functions there.

How to use Skia shader sksl shading language code in C++ Builder?

1. Add a SkAnimatedPaintBox to your form

If you want to use Skia Shaders, first you need to drag TSkAnimatedPaintBox on your form, this will refresh drawing in a given duration. You can set it’s animation properties as below,

2. Declare some Skia globals in your form header

In your form header you can add these our Skia variables,

3. Create your simple SkSL code to be used as a text

Here is a very simple SkSL code that changes color of pixels by time (iTime), in its resolution (iResolution). Here we have vec2 input which is coordinates of x and y, and we have vec4 output which is color of pixel in ARGB form. This is how a shader works,

Or you can use SkSL codes from https://shaders.skia.org/. Or you can modify https://www.shadertoy.com/
I made some simple example in there too. https://www.shadertoy.com/user/yyoru

4. Load SkSL Shader Language codes and make shader in C++ Builder

Now we can add a button to start doing effects from a text which is a SkSL code, this text can be obtained from a String, from a file, or a component such as from a Memo1->Text. We can use this SKSL code to create the Skia Runtime Effect by using the TSkRuntimeEffect::MakeForShader() method, and we can create Runtime Shader Builder by using the SkRuntimeShaderBuilder() method, and finally, we can make a shader by using MakeShader() method. Before these, we should stop animation and then we should start animation. If we sum up all, a Button1Click() can be as below,

5. Configure animation drawing event in C++ Builder

Now, select this SkAnimatePaintBox1 object, and double click to OnAnimationDraw() event on its Events tab section.

Here if LShaderBuilder is created we can update Shader by using some SkSL globals, such as iResolution, iTime, iMouse if your SkSL code uses these globals. Then we create a new SkPaint() and we draw this to the current ACanvas of this AnimationPaintBox by using ACanvas->DrawPaint(LPaint);. Here is how we can do this,

How can I use more advanced Shaders in C++ Builder?

Shader codes are little brain burning codes while they are close to C++ codes, 3D operations in math are mind blowing. If you still say its okay, I can watch an example, here is a wonderful code example that runs 3D funny creature on the ground. You can find many shader examples on Youtube.

or you can try this,

If you want to see more examples, please install the latest RAD Studio / C++ Builder12.1 full or trial version and check this sample in Samples folder:
C:\Users\Public\Documents\Embarcadero\Studio\23.0\Samples\CPP\Multi-Device Samples\Skia4Delphi project. It has many examples that runs different Skia graphical examples.

How To Use Skia Shader SKSL Shading Language Code in C++ Builder C++ logo

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17C++20C++23Learn C++Syntax

How To Use std::make_from_tuple in C++ 17

C++C++17C++20Learn C++

How To Use std::apply With Tuple In C++ 17 And Beyond?

C++C++17C++20Learn C++NumericsSyntax

How To Compute The Greatest Common Divisor And Least Common Multiple in C++?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++