Trusted Software Excellence across Desktop and Embedded
Take a glance at the areas of expertise where KDAB excels ranging from swift troubleshooting, ongoing consulting and training to multi-year, large-scale software development projects.
Find out why customers from innovative industries rely on our extensive expertise, including Medical, Biotech, Science, Renewable Energy, Transportation, Mobility, Aviation, Automation, Electronics, Agriculture and Defense.
High-quality Embedded Engineering across the Stack
To successfully develop an embedded device that meets your expectations regarding quality, budget and time to market, all parts of the project need to fit perfectly together.
Learn more about KDAB's expertise in embedded software development.
Where the capabilities of modern mobile devices or web browsers fall short, KDAB engineers help you expertly architect and build high-functioning desktop and workstation applications.
Extensible, Safety-compliant Software for the Medical Sector
Create intelligent, patient-focused medical software and devices and stay ahead with technology that adapts to your needs.
KDAB offers you expertise in developing a broad spectrum of clinical and home-healthcare devices, including but not limited to, internal imaging systems, robotic surgery devices, ventilators and non-invasive monitoring systems.
Building digital dashboards and cockpits with fluid animations and gesture-controlled touchscreens is a big challenge.
In over two decades of developing intricate UI solutions for cars, trucks, tractors, scooters, ships, airplanes and more, the KDAB team has gained market leading expertise in this realm.
Build on Advanced Expertise when creating Modern UIs
KDAB assists you in the creation of user-friendly interfaces designed specifically for industrial process control, manufacturing, and fabrication.
Our specialties encompass the custom design and development of HMIs, enabling product accessibility from embedded systems, remote desktops, and mobile devices on the move.
Legacy software is a growing but often ignored problem across all industries. KDAB helps you elevate your aging code base to meet the dynamic needs of the future.
Whether you want to migrate from an old to a modern GUI toolkit, update to a more recent version, or modernize your code base, you can rely on over 25 years of modernization experience.
KDAB offers a wide range of services to address your software needs including consulting, development, workshops and training tailored to your requirements.
Our expertise spans cross-platform desktop, embedded and 3D application development, using the proven technologies for the job.
When working with KDAB, the first-ever Qt consultancy, you benefit from a deep understanding of Qt internals, that allows us to provide effective solutions, irrespective of the depth or scale of your Qt project.
Qt Services include developing applications, building runtimes, mixing native and web technologies, solving performance issues, and porting problems.
KDAB helps create commercial, scientific or industrial desktop applications from scratch, or update its code or framework to benefit from modern features.
Discover clean, efficient solutions that precisely meet your requirements.
Boost your team's programming skills with in-depth, constantly updated, hands-on training courses delivered by active software engineers who love to teach and share their knowledge.
Our courses cover Modern C++, Qt/QML, Rust, 3D programming, Debugging, Profiling and more.
The collective expertise of KDAB's engineering team is at your disposal to help you choose the software stack for your project or master domain-specific challenges.
Our particular focus is on software technologies you use for cross-platform applications or for embedded devices.
Since 1999, KDAB has been the largest independent Qt consultancy worldwide and today is a Qt Platinum partner. Our experts can help you with any aspect of software development with Qt and QML.
KDAB specializes in Modern C++ development, with a focus on desktop applications, GUI, embedded software, and operating systems.
Our experts are industry-recognized contributors and trainers, leveraging C++'s power and relevance across these domains to deliver high-quality software solutions.
KDAB can guide you incorporating Rust into your project, from as overlapping element to your existing C++ codebase to a complete replacement of your legacy code.
Unique Expertise for Desktop and Embedded Platforms
Whether you are using Linux, Windows, MacOS, Android, iOS or real-time OS, KDAB helps you create performance optimized applications on your preferred platform.
If you are planning to create projects with Slint, a lightweight alternative to standard GUI frameworks especially on low-end hardware, you can rely on the expertise of KDAB being one of the earliest adopters and official service partner of Slint.
KDAB has deep expertise in embedded systems, which coupled with Flutter proficiency, allows us to provide comprehensive support throughout the software development lifecycle.
Our engineers are constantly contributing to the Flutter ecosystem, for example by developing flutter-pi, one of the most used embedders.
KDAB invests significant time in exploring new software technologies to maintain its position as software authority. Benefit from this research and incorporate it eventually into your own project.
Start here to browse information on the KDAB website(s) and take advantage of useful developer resources like blogs, publications and videos about Qt, C++, Rust, 3D technologies like OpenGL and Vulkan, the KDAB developer tools and more.
The KDAB Youtube channel has become a go-to source for developers looking for high-quality tutorial and information material around software development with Qt/QML, C++, Rust and other technologies.
Click to navigate the all KDAB videos directly on this website.
In over 25 years KDAB has served hundreds of customers from various industries, many of them having become long-term customers who value our unique expertise and dedication.
Learn more about KDAB as a company, understand why we are considered a trusted partner by many and explore project examples in which we have proven to be the right supplier.
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications.
Read more about the history, the values, the team and the founder of the company.
When working with KDAB you can expect quality software and the desired business outcomes thanks to decades of experience gathered in hundreds of projects of different sizes in various industries.
Have a look at selected examples where KDAB has helped customers to succeed with their projects.
KDAB is committed to developing high-quality and high-performance software, and helping other developers deliver to the same high standards.
We create software with pride to improve your engineering and your business, making your products more resilient and maintainable with better performance.
KDAB has been the first certified Qt consulting and software development company in the world, and continues to deliver quality processes that meet or exceed the highest expectations.
In KDAB we value practical software development experience and skills higher than academic degrees. We strive to ensure equal treatment of all our employees regardless of age, ethnicity, gender, sexual orientation, nationality.
Interested? Read more about working at KDAB and how to apply for a job in software engineering or business administration.
A long time ago, I wrote a post about creating custom shapes in Qt Quick, via the scene-graph APIs. That post covered defining suitable geometry to draw a part of a circle, known also as a 'sector', efficiently, since such sectors occur commonly in instrument and vehicle interfaces. I started writing the second part, about implementing the material and shaders to make the complete functioning item.
Then, somehow, life and customers intervened, and I forgot all about publishing the second part, and now it's an embarrassingly long time later. Not as bad as the wait for A Dance With Dragons or Duke Nukem forever, but still, embarrassing. This is all my way of saying, here's a post about writing shaders and QtQuick scene graph materials, which is interesting but also more than three years late, and that WordPress apparently doesn't remind you that you have drafts sitting around.
In the previous post, I talked about creating the geometry for the custom sector item. We saw that I need to define the geometry as a collection of triangles (since GPUs love triangles), each triangle having three points, or vertices. Additionally, we saw that we can create our triangles somewhat larger than we need, to allow us to make a perfectly curved edge for our sector, as well as to permit some other effects such as anti-aliasing (reducing the appearance of steps or ‘jaggies’ on the edge) and even adding borders. And UX designers love borders almost as much as they love gradients and rounded corners.
Making our triangles larger also means we need fewer. The extreme case of this is just to make two triangles (a square) covering the whole circular area of the sector. But that's wasteful, especially as our fragment shader grows more complex, which it's about to do. Also, on a tiled renderer (which means almost every mobile GPU in practice), if we can reduce how many tiles the geometry intersects, we get some additional performance wins. So, we want to have enough triangles that our geometry approximates our real shape, but not so many that we're making the scene-graph or vertex processing elements do unnecessary work.
All about the pixels (baby)
The challenge facing us is: we need to define what color the pixels of our geometry end up with, and that's decided for each pixel by a shader program (written in GLSL for OpenGL, SPIR-V for Vulkan, and similar formats for other QtQuick backends). In the QtQuick scene graph system, shaders are interfaced to C++ via a material (QSGMaterial) subclass.
The main task of our material (and shader), then, is to answer two questions about a pixel:
Is it outside our shape, part of the border, or somewhere inside?
What colour should it be? (based on its position in the sector and the various colors defined in properties from QML)
The shader is a program within our program, executed by the GPU, which answers the above two questions for each and every pixel in our triangles, every time the scene is rendered (which is usually sixty times per second in Qt Quick). Fortunately, GPUs are fast and perform this work in parallel.
With the geometry, we had to define an array of X and Y values. But for the material side, we now need to get into the details of shading languages. We're going to focus on OpenGL, since it's the most common. But, fortunately, all shading languages are very similar. The OpenGL shading language is called GLSL. If you’ve ever experimented with the ShaderEffect item in QtQuick, you’ll have a good idea what is required, but there’s some book-keeping needed. Most importantly, we have to define our shader source code as some GLSL code. In my item, I do this as text strings inside the C++ source (you could also put them in a resource and load them via QFile). But, apart from the code, we also need to define the control values we will pass into our shader from C++. These values are called uniform values in OpenGL, and we need to manage them in a particular way for performance reasons.
The basic principle is to put all our control values into a structure, and ensure the scene-graph can compare two examples of our structure for equality. This means, if we have several sectors on the screen using the same colors and border, the scene-graph can identify that they use identical uniform values. Why is this important? Well, the most beneficial optimization strategy that the renderer performs is to group geometry that can be drawn at the same time into batches, to reduce the number of drawing calls and state changes. To allow multiple geometries to be batched, they must meet certain criteria. Having the material state be consistent is one of them. That’s why we follow some standard patterns, inherit from QSGSimpleMaterialShader, and use the macro QSG_DECLARE_SIMPLE_COMPARABLE_SHADER so that our shader can be compared with other instances and hopefully batched together when rendering.
You could skip using the comparable stuff. But if you want to have ten or fifty sectors on screen at once, making them batch-able becomes significant. (Maybe you want to re-create the engine display panel for the 'Classic' series of 737s.)
The job of our material class, then, is to collect all the parameter data, be able to compare it in a robust way (often with some tolerances, for floating point values: qFuzzyCompare helps), and to map the parameters in C++ to uniforms on our shader. It also has to load the shader source from a file, resource to text string. Fortunately, the QSGMaterial subclasses have helpers to make all of this relatively easy, and handle some other complexities, as well (such as variations between desktop and embedded GLSL shaders).
Writing the fragment shader
To answer our 'is it inside our shape?' and 'what colour should it be?' questions for each pixel, we need to convert back into polar coordinates inside our pixel. We need to know our angular position to calculate the gradient color smoothly. And we need the radius to work out, if we're inside or outside the shape, or on the border. That’s actually why we passed some additional data along with each vertex, to give us an easy numerical range to work with in our shader. We use a coordinate system where the center of the sector is always (0,0). To convert back to polar form, we use the atan2() function in GLSL, followed by a length() function to compute the radius. (For those of you familiar with OpenGL, we're using the texture coordinates U and V to encode this simplified position alongside the real X and Y values. We could compute the values from X and Y in the shader with some additional uniforms. But with the limited number of triangles used, the overhead of two additional attributes per vertex is minimal.)
Then, we can check our radius against the inner, outer, and border values (passed in via uniforms), to see what we need to do. If we’re outside the outer radius, we can discard the pixel (analogous to an early-return in a C++ function). Discarding pixels can save some effort for the renderer, if done early in the fragment shader.
If we’re inside the border, we can use the border color. Otherwise, we need to compute an interior color. The built-in GLSL function mix() helps us to smoothly interpolate (blend) between two colors. To implement anti-aliasing, we also check if we’re near the transition between the interior and the border, or between the border and the outside. In those cases, we modify the pixel’s transparency, or mix the color with the border, to give a smooth transition.
Four sample fragments in our shader
Outside the shape : discarded
On the transition between the the border and the main fill - borderOpacity will be for example 0.5
Within the fill: color in interpolated between the four inner colors to give final pixel RGB. In the code below, tNorm will be approximately 0.5 (half way between inner and outer radius)
Fully within the border: borderOpacity is 1.0
Let's express that in some GLSL:
uniform lowp float qt_Opacity;uniform lowp vec4 startInnerColor;uniform lowp vec4 startOuterColor;uniform lowp vec4 endInnerColor;uniform lowp vec4 endOuterColor;uniform lowp float startAngleRad;uniform lowp float spanAngleRad;uniform lowp float innerRadius;uniform lowp float outerRadius;uniform lowp float borderWidth;uniform lowp vec4 borderColor;const lowp float aaWidth =0.1;varying lowp vec2 vpos;voidmain(){float theta =atan(vpos.y, vpos.x);float t =length(vpos);// are we between the inner and outer radius? With some blending at the edge based on AA widthfloat baseOpacity =smoothstep(innerRadius - aaWidth, innerRadius + aaWidth, t)*(1.0-smoothstep(outerRadius - aaWidth, outerRadius + aaWidth, t));// discard if we're outside the areaif(baseOpacity <0.05){ discard;}// border color handling : are within a borderWidth of either edge? Again compute an opacity// based on the distance and the AA width.float borderOpacity =(1.0-smoothstep(innerRadius + borderWidth - aaWidth, innerRadius + borderWidth + aaWidth, t))+smoothstep(outerRadius - borderWidth - aaWidth, outerRadius - borderWidth + aaWidth, t);// radial gradient: compute our t parameter which 0.0 at the inner radius and 1.0 at the outerfloat tNorm =(t - innerRadius)/(outerRadius - innerRadius);// gradient along the sector: compute S parameter which is 0.0 at startAngleRad, and 1.0// at the ending angle. Use mod() to handle wrappingfloat s =mod(theta - startAngleRad,6.28318)/ spanAngleRad;// mix our four gradient colors together vec4 sc =mix(startInnerColor, startOuterColor, tNorm); vec4 ec =mix(endInnerColor, endOuterColor, tNorm); vec4 fillColor =mix(sc, ec, s);// finally mix in our border based on its opacity vec4 srcColor =mix(fillColor, borderColor, borderOpacity); gl_FragColor = srcColor * baseOpacity * qt_Opacity;}
Our GLSL fragment shader has main(), just like a regular C++ program, but it runs millions of times per second (in parallel, of course). We start by doing our atan call, and then we proceed to work out our color value based on that.
You may notice the code always applies a border and the possible gradient options. Why not use if-s to avoid this extra work? Well, shader programs really hate branching, whereas multiplying values by zero is optimized explicitly. Thus, it's usually (but not always) advisable to write straight-line code without branching, if possible. This especially applies on lower powered embedded GPUs. Of course, if we had many sectors where a gradient was not required, we could create a different shader program without that feature and select one program or the other in our C++ code. This would keep the GLSL code friendly to our GPU, while allowing us to squeeze as much performance as possible out of the hardware. And our UX designers would never need to care.
Final result
Putting this all together, we define our QSGMaterial subclass, our state struct, and code, to look up the uniform values which pass information from C++ into GLSL. There are quite a few small points to get correct. But, in the end, we have a beautifully rendered sector on the screen, and an API we can give to our UX designers, confident that even if they go crazy with adding sectors, animating them, making them pulse, adding big and small ones, the performance should be excellent.
The good news is that, having done this once, doing it for other shapes is more of the same. On the geometry side, figure out some triangles which cover the pixels (fragments) of your shape. Inside your shader, use the vertex information, any uniforms you need, and possibly some other vertex information to compute if you're within your shape, and what color to give it. It's possible to do much more complex techniques, of course, creating procedural patterns following the many examples on ShaderToy, or defining vertices, dynamically, based on a real-time input (for example, an audio waveform).
About KDAB
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications. In addition to being leading experts in Qt, C++ and 3D technologies for over two decades, KDAB provides deep expertise across the stack, including Linux, Rust and modern UI frameworks. With 100+ employees from 20 countries and offices in Sweden, Germany, USA, France and UK, we serve clients around the world.
Ah, at last!
Another great post in series. Thank you 👍🏻
James Turner
Senior Software Engineer & Teamlead
Senior software engineer and team lead at KDAB, James has been developing with Qt since 2002. He contributes to the current maintenance of Mac platform support as well as the development of OpenGL and 3D support in Qt. James has a background in UI, graphics, and simulation development as well as a long history of development on OS-X and prior versions of Mac OS. He is a lead developer on FlightGear, the open-source flight simulator, and holds a BSc in Computer Science.
1 Comment
25 - May - 2022
S.M.Mousavi
Ah, at last! Another great post in series. Thank you 👍🏻