Writing a shader for Weston/Wayland

Before delving into Wayland‘s shaders, we need to define what a shader is and the difference between vertex shader and fragment shaders.

Shaders

A shader is a computer program that runs on the GPU and is used to produce appropriate levels of light and colour within an image. Shaders are written in a specific shading language and they can be used to achieve customised effects.

There are different types of shaders, applied in different stages of the graphic pipeline and having different purposes. Here, we are mainly concerned with two types: the Vertex Shader and the Fragment Shader.

Vertex shader

A vertex shader is run once for each vertex given to the graphics processor in the early stages of the graphical pipeline. It can manipulate properties such as position, colour, and texture coordinate. It’s in the vertex shader stage that each vertex’s 3D position is transformed to the 2D coordinate on the screen.

Fragment shader

The fragment shader, on the other hand, manipulates pixels instead of vertices. It takes part in the rasterisation step, when colour and other attributes are computed for each fragment (pixel). With limited information about the whole scene — the fragment shader can only operate in a single pixel and sample nearby pixels — fragment shaders cannot produce very complex effects apart from blur, edge detection/enhancement, post-processing and filtering.

Colour management

Because our objective is to manipulate colour spaces, it’s clear from the previous discussion about shaders that we need a fragment shader. In Wayland, adding a fragment shader is just a matter of creating an array of chars with the fragment code:

static const char texture_fragment_shader_color[] =
”    vec3 color = texture2D(cms_lut, v_texcoord).rgb;\n”
”    gl_FragColor.rgb = color;\n”
;

and concatenate it to the others fragments sources in the shader_init function, using the sources variable:

sources[i++] = texture_fragment_shader_color;

Now, the most difficult part is the creation of the colour transformation texture (cms_lut) and its integration into the fragment shader. To create the texture, we need the LCMS library and a code like this (implementation details omitted):

static unsigned char
weston_cms_transform_data(struct weston_color_profile *p,
        unsigned short data[GRID_SIZE][GRID_SIZE][GRID_SIZE][3])

{
    cmsHPROFILE input;
    cmsHTRANSFORM transform;

    input = cmsCreate_sRGBProfile();
    transform = cmsCreateTransform(input, TYPE_RGB_16, p->lcms_handle,
            TYPE_RGB_16, INTENT_PERCEPTUAL, 0);
     cmsDoTransform(transform, data, data, GRID_SIZE * GRID_SIZE * GRID_SIZE);

     cmsDeleteTransform(transform);
     cmsCloseProfile(input);

     return 0;
}

As it can be seen in this code, the texture data is being written by the LCMS library in the cmsDoTransform function. Now, we need to link this data with the fragment shader. That is accomplished in two steps. First, we need to tell OpenGL that this data exists in the fragment shader:

static const char fragment_shader_color_uniforms[] =
    “uniform sampler2D cms_lut;\n”
    ;

Because of the way the fragments shaders are constructed in Wayland, the uniform declaration must the separated from the shader code and inserted at the right point in the shader_init function. For the fragment to have a pointer to this memory area, we use the getUniformLocation function to save it into the shader program:

shader->cms_uniform = glGetUniformLocation(shader->program, “cms_lut”);

Finally, we bind the uniform as a texture type and generate the final texture:

glGenTextures(1, &(shader->cms_uniform));

glBindTexture(GL_TEXTURE_3D, shader->cms_uniform);

glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, GRID_SIZE, GRID_SIZE, GRID_SIZE, 0, GL_RGB, GL_UNSIGNED_SHORT, data);

The interesting thing about the colour conversion is that all the conversion data is stored per output. So if you have two different monitors with different colour profiles, the appropriate conversion is done to each of one, separately.

The complete set of changes can be seen in the branch ‘colour’ of my clone of the Weston repo.

, , , , ,

Leave a comment

Guadec 2013

As part of my Google Summer of Code project, I attended GUADEC 2013 in Brno from 1st to 8th of August. All the talks happened in the first four days. The subjects ranged from the current situation of Gnome, its libraries and applications, to the future planned features.

I would say the subjects discussed can be categorised into three main topics: Web related (Webkit, Evolution, Tracker, Grilo, Maps and security) Graphical Interfaces (High resolution display support, Cogl, Wayland, GTK, Glade) and Community (reporting bugs, working with the Gnome community, women and open source, etc).

One of the highlights of the conference was the talk given by Bruno Cardoso about using Clang, LLVM in Gnome. Besides giving an overview of the LLVM project, he actually ran the Clang tool in a couple of projects and was able to find four security bugs (although some of them might be false-positives).

In the 4th day I gave a brief presentation of my GSoC project. It was really brief: in 3 minutes I explained what a color profile is and the reason why we want to make the color management in Wayland/Weston using a shader.

Brno is an amazing city and I had the chance to meet some old friends there and meet a lot of new people. I also met my GSoC mentor, Richard Hughes, and we took the opportunity to discuss the future of my project.

Finally, I would like to thank Fabiano Fidencio for the hospitality and for being our tour guide through the city! And the Gnome Foundation for the sponsorship.

, , , ,

Leave a comment

An Efenniht theme for Ephoto

This week I did an efenniht theme for the Ephoto applicative.

Efenniht:

It is an alternative theme to the E17 Window Manager and Elementary Widget Library developed by the ProFUSION’s designer Marina Proni. It is basically a black theme using mostly black and orange colors. More info about it and screenshots can be found here.

Ephoto

It is a picture viewer focused on simplicity with functions for displaying pictures contained inside a directory and simple tools to view them elegantly. It is being currently developed using the default Elementary’s theme as you can see here.

Ephoto with Efenniht theme

Before doing the aforementioned theme we realized that some of the parts defined inside the Ephoto’s own theme could be merged into the default theme and reused later even for others applications. Done that, creating the new theme was just a matter of adding the appropriate parts to the Efenniht theme. These parts were basically Gengrid item’s themes: one for the up arrow, other for the folders with previews and the last one to the thumbnail itself. All the remaining used widgets already had their themes implemented inside Efenniht.

The following screenshots show the obtained result:

Thumbs

Thumbnails view

Browser

Directories view

As you can see there are still some work to be done like providing images for some icons and maybe improve the entry’s theme since it is hard to know where its boundaries are. At least Marina herself seems to already like of the new Ephoto’s look. Yay!

, ,

Leave a comment

Make Valgrind track your mempool

Warning: big post ahead!

If you are a programmer you have probably used or heard of the Valgrind tool. It is described as a framework for memory management and threading bugs detection. I usually use it to find out memory leaks or invalid memory access in my programs.

Valgrind is a great tool but think in the case of mempools. Usually you get a big memory area and manage it yourself during the life of the mempool and that area is only freed when the mempool is destroyed. If a user has requested a memory chunk but hadn’t returned it Valgrind can’t track that since it only knows about that big memory area previously allocated. Hmmm so what? Can’t we tell Valgrind what is going on?

Actually we can! Valgrind has a set of macros to allow you to track down what is happening in your mempool. Let’s learn how to use it. To do that I will use as example an EFL library I use a lot at ProFUSION called Eina.

Eina has two most used mempools: Chained and One big. The first one allocates a big memory area with malloc and manage chunks of it using a stack. The other is used when you know in advance how many objects of the same type you will have so it just calls malloc once. I will concentrate here in the Chained pool.

Before we start using the Valgrind’s macros we must understand the concepts of pool and superblock. These concepts are used in a very high level manner by Valgrind because of the many types of custom allocators existent. In Eina’s case the pool is the struct defining the mempool type and the superblock is the memory area from which chunks are given to the users, i.e, the stack pointer.

First of all we need to include valgrind’s header into our code. And we can do that allowing the users to completely ignore the macros (so avoiding some extra instructions when not running under valgrind) by declaring (or not) the NVALGRIND variable:

#ifndef NVALGRIND
# include <valgrind/memcheck.h>
#endif

Looking into the source code you can see that the mempool is created when the function eina_chained_mempool_init is called. Let’s tell valgrind the mempool was created by adding the call:

#ifndef NVALGRIND
VALGRIND_CREATE_MEMPOOL(mp, 0, 1);
#endif

The 3 macro’s arguments are the pool pointer, the number of redzone bytes and a “boolean” indicating whether the pool’s chunks are zeroed/defined. Chained pool does not use redzone bytes so we passed 0 to it. Similarly the mempool is destroyed in the function eina_chained_mempool_shutdown where we call:

#ifndef NVALGRIND
VALGRIND_DESTROY_MEMPOOL(mp);
#endif

Now we need to tell valgrind what is our superblock and when chunks are requested and returned. As we have already said the superblock is the stack pointer. It is called first in the struct and is initialized in _eina_chained_mp_pool_new internal function. As valgrind’s docs say we must mark it as NOACCESS:


#ifndef NVALGRIND
VALGRIND_MAKE_MEM_NOACCESS(ptr, pool->alloc_size - alignof);
#endif

where the second argument of the macro indicates the superblock size in bytes.

Finally the chunks are handle by the functions eina_chained_mempool_malloc and eina_chained_mempool_free. In the first one we add the call:

#ifndef NVALGRIND
VALGRIND_MEMPOOL_ALLOC(pool, mem, pool->item_alloc);
#endif

saying that a chunk of size pool->item_alloc was returned to the user with address given by mem and call:

#ifndef NVALGRIND
VALGRIND_MEMPOOL_FREE(pool, ptr);
#endif

to indicate the the chunk pointed by ptr was returned.

And that is all. Just run a program using the mempool inside valgrind and you will find out whether the pool is being correctly used. For more details you can take a look into the valgrind’s docs and in the Eina’s source code since I’ve commited that changes in revision 53405. Also in the commit message you will find a little program using it.

Just a reminder: if you want to use valgrind with eina you must ./configure it with the option –enable-valgrind.

, , , ,

Leave a comment

15 years of experience!

You may be wondering why the blog’s name is 15experience. Everything started with ProFUSION – the company I work at – and a couch. Yeah a couch! I have said I had 5 years of experience transporting couches because of a previous job of mine when a couch had arrived to the company and my boss Gustavo Barbieri changed it to 15 years and for anything I do or will ever do.

Experience is a good thing after all and it describes exactly what is this blog about: my experience as  a Computer Scientist!

A reason for the blog's name

Leave a comment