TGSI for Mesa
TGSI
Tungsten Graphics Shader Infrastructure
In a Gallium driver, these are first transformed into TGSI by the state tracker and are then transformed into something that will run on the card by the driver.^link
TGSI, Tungsten Graphics Shader Infrastructure, is an intermediate language for describing shaders. Since Gallium is inherently shaderful, shaders are an important part of the API. TGSI is the only intermediate representation used by all drivers.
来自:wikipedia mesa
TGSI是Gallium框架中的所有驱动程序使用着色器的唯一中间表示形式,这里特指
的是着色器
的中间形式,着色器对驱动而言的所有格式将是TGSI。
TGSI中间语言
介于在着色器(GLSL)代码与GPU指令之间的一种中间语言,类似与C语言与CPU指令之间存在的汇编语言一样。
在Mesa上,GLSL首先被编译器翻译成tgsi中间语言,然后显卡特定的驱动将这些tgsi语言的代码编译成GPU指令。
1 | glxgears: shader |
glxgears在渲染中生成的部分TGSI代码
- TGSI specification
- TGSI Instruction Set
FRAG
:fragment片元着色器VERT
:vertex顶点着色器DCL
: declaration 申明resourcesIMM
: immediate 立即数PROPERTY
: property 性质
着色器的编译链接
GLSL中则通过两种对象——
着色器对象
和着色器程序对象
——来分别处理编译过程和连接过程
1 | call glShaderSource(shader=58, count=3, ) |
glShaderSource
: 替换着色器对象中的源代码glCompileShader
: 编译一个着色器对象glGetShaderiv
: 从着色器对象返回一个参数glCreateProgram
: 创建一个空program对象并返回一个可以被引用的非零值(program ID)glUseProgram
: 安装program对象作为当前渲染状态的一部分
GLSL使用
着色器代码:
1 | static const char vertex_shader[] = |
1 | /* Compile the vertex shader */ |
glCompileShader
主要编译着色器的源代码(即vertex_shader中的GLSL代码)
- 编译后的代码是TGSI中间代码?
- 如果是在哪个阶段进行的转换?
- 在virgl驱动中的着色器代码是否进行了转换?
glCompileShader
glCompileShader
compiles the source code strings that have been stored in the shader object specified by shader.
在mesa中的函数调用流程:
1 | _mesa_CompileShader (src/mesa/main/shaderapi.c) |
版本:19.3.0-devel 237c7636ca4c429d4dbfce95b6e3281a8309eac7
1 | /** |
1 | /** |
file: src/mesa/state_tracker/st_cb_program.c
1 | /** |
file: src/mesa/state_tracker/st_glsl_to_ir.cpp
1 | /** |
file: src/mesa/state_tracker/st_glsl_to_tgsi.cpp
1 | st_link_shader |
virgl中着色器的转换
amdgpu使用开源驱动
Then, 3D commands. These are close to what we can find in a API like Vulkan. We can setup a viewport, scissor state, create a VBO, and draw it. Shaders are also supported, but we first need to translate them to TGSI; an assembly-like representation. Once on the host, they will be re-translated to GLSL and sent to OpenGL.
https://studiopixl.com/2017-08-27/3d-acceleration-using-virtio.html
- 在tgsi的传输中为什么不直接使用tgsi token进行传输,而要转换为text的形式传输???
- 地址空间的不同是否相关?
- tgsi text转换为tgsi token的过程中与当前使用到的纹理数据等其他资源进行关联?
1 | struct tgsi_instruction |
1 | struct tgsi_instruction_texture |
1 | /* |
tgsi_instruction_texture:表明存在指令纹理,其与纹理资源数据之间的关系?
示例
1 | glxgears: shader |
TGSI转换成GLSL
amdgpu中着色器的转换
在radeonsi用户空间驱动中tgsi的使用
1 | Setup actions for TGSI memory opcode, including texture opcodes. |
TGSI与texture之间在渲染时,之间的联系??
radeonsi for shader
目前LLVM是amdgpu的后端编译器,在mesa19.3中使用ACO
(AMD COmpiler)编译着色器代码
1 | TGSI->LLVM |
It was just two days ago that Valve’s performance-focused “ACO” shader compiler was submitted for review to be included in Mesa for the “RADV” Radeon Vulkan driver. Just minutes ago that new shader compiler back-end was merged for Mesa 19.3.
ACO, short for the AMD COmpiler, is the effort led by Valve at creating a more performant and optimized shader compiler for the Radeon Linux graphics driver. Besides trying to generate the fastest shaders, ACO also aims to provide speedy shader compilation too, as an alternative to the AMDGPU LLVM shader compiler back-end. Initially ACO is for the RADV Vulkan driver but it may be brought to the RadeonSI OpenGL driver in the future. At the moment ACO is in good shape for Volcanic Islands through Vega while the Navi shader support is in primitive form.
- Valve’s ACO Shader Compiler For The Mesa Radeon Vulkan Driver Just Landed
ISA Code
Instruction Set Architecture(指令集架构) —— ISA
The AMDGPU backend provides
ISA code
generation for AMD GPUs, starting with the R600 family up until the current GCN families. It lives in the lib/Target/AMDGPU directory.
- User Guide for AMDGPU Backend
- “Vega” Instruction Set Architecture
- GCN ISA Manuals
着色器形式的转换
1 | +-------+ +---------+ +---------+ +------+ +------+ +-------+ |
示例
GLSL着色器
- vertex shader
1 | //vertex顶点着色器 |
- fragment shader
1 | //fragment片元着色器 |
TGSI text
通过mesa编译后生成的TGSI token进行dump出的text
1 | VIRGL_DEBUG=tgsi ./a.out |
- vertex shader
1 | TGSI: |
- fragment shader
1 | TGSI: |
TGSI转换GLSL
1 | VREND_DEBUG=shader virgl_test_server |
- vertex shader
1 | a.out: shader |
- fragment shader
1 | a.out: shader |
其他
- 打印着色器程序相关的所有参数和字段
1
ST_DEBUG=mesa ./a.out
1
2
3
4
5
6
7
8/**
* Print all of a program's parameters/fields to stderr.
*/
void
_mesa_print_program_parameters(struct gl_context *ctx, const struct gl_program *prog)
{
_mesa_fprint_program_parameters(stderr, ctx, prog);
}file: mesa/program/prog_print.c
参考
- TGSI
- gallium3d-xds2007
- A beginners guide to TGSI
- The State of Open Source 3D
- learnopengl–Shaders|【CN】
- Linux环境下的图形系统和AMD R600显卡编程(11)——R600指令集
- GLSL compiler
- GSoC 2017 - 3D acceleration using VirtIOGPU
- The Linux Graphics Stack
- Testing Out Mesa’s GLSL-To-TGSI Translator
- Introduction to GPU Programming with GLSL
- A beginners guide to TGSI
- Implementing Bicubic Scaling in TGSI