A beginners guide to TGSI04 Jul 2016
TGSI stands for Tungsten Graphics Shader Infrastructure. The development of TGSI was started by tungsten graphics and hence the name. So what the heck is TGSI? Let’s find out together.
When we write a code and compile it, the compiler converts it into a intermediate representation which is suitable for optimisation before it is translated to machine code. TGSI is a intermediate representation to write shaders and it is compiled to create machine code according to the underlying hardware. When you code in TGSI you get the feeling of coding in assembly language. When I was working on VDPAU I had to write shaders in TGSI and the only docs that I could get my hands on was this. Though it was not complete and only had information about opcodes I was able to find my way by looking at the code available in mesa. So enough with the chit chat let’s look at some at some code.
This is the simplest fragment shader you can come up with, it is like the hello world program of shaders. Let’s go over it and try to understand what it is doing.
All the TGSI shaders have a ureg_program which is kind of program that has to be excuted for each pixel so each of our commands are indexed in ureg_program and every instruction requires ureg_program as argument.
All TGSI instructions, known as opcodes, operate on arbitrary-precision floating-point four-component vectors. We use X, Y, Z and W to represent the components of the vector.
ureg_src and ureg_dst represent 4 component vectors, ureg_dst is the destination vector and ureg_src is the source vector as is obivous with their names. We can get a ureg_src from a ureg_dst using
So let’s start with the code, we first declare all the ureg_* which we will need during the program. The next step is to initialize the ureg program. After that we call ureg_DECL_fs_input which gives us the coordinate of the pixel on which we are executing. The next thing is the sampler, the sampler has the information about the texture of the pixel. Now we declare a temporary vector and the output vector in which we have to put the color of the pixel we are operating on.
ureg_TEX() function then puts the texture of the given coordinate retrieved from the sampler_view to the dst vector. The color information
is stored as RGBA (X:R, Y:G, Z:B, W:A) in ureg_dst. We can now perform operations on the texel like setting a particular component to zero and even
complex algorithms like lanczos scaling. In above example we do not edit the color of pixel and simply put it the o_fragment vector.
All the opcodes in TGSI have one dst vector and from 0 to 3 source vectors. Let’s look at some of the opcodes. You can find a comprehensive list of opcodes here. All these instructions are excuted component wise.
ureg_MOV(shader, dst, src);: dst = src
ureg_ADD(shader, dst, src0, src1): dst = src0 + src1
ureg_SUB(shader, dst, src0, src1): dst = src0 - src1
ureg_MUL(shader, dst, src0, src1): dst = src0 * src1
ureg_DIV(shader, dst, src0, src1): dst = src0 / src1
ureg_MAD(shader, dst, src0, src1, src2): dst = (src0 * src1) + src2
ureg_FRC(shader, dst, src): dst = fraction(src)
ureg_FLR(shader, dst, src): dst = floor(src)
There are some functions other than the opcodes which are very useful like
The ‘*’ can be 1, 2, 3 and 4. This function is used to create a ureg_src with given values. e.g.
It allows us to perform operations on a particular components of a vector. e.g.
ureg_scalarThis allows us to create a vector which contains a particular component of some other vector in all of its components. e.g.
This output of this instruction is
I hope that now you are capable of writing a shader in TGSI. We will look how we can write more complex shaders using TGSI in the next post until then
Have a Good Day!!