hacking on mesa 3d · developingtheopengl so,what’sourcontributiontotheopenglimplementation?...

26
Hacking on Mesa 3D Eleni Maria Stea <[email protected]>

Upload: others

Post on 07-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Hacking on Mesa 3DEleni Maria Stea <[email protected]>

Page 2: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Introduction to Mesa 3D/OpenGL

The most widely used open source OpenGL/Vulkan implementation

Eleni Maria Stea Hacking on Mesa 3D 2 / 26

Page 3: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Introduction to Mesa 3D/OpenGL

Historical

▶ It started as an open source (MIT license) software OpenGL implementation.• swrast: software rasterizer

▶ Then some hardware drivers were added as backends.• Intel, AMD Radeon, Nouveau, VMware virtual GPU

▶ More APIs, specifications were implemented.• Vulkan• OpenGL ES1, ES2 & ES3• OpenCL, OpenVG, VDPAU, XVMC, EGL interface

▶ More software drivers were written.• llvmpipe, softpipe

Eleni Maria Stea Hacking on Mesa 3D 3 / 26

Page 4: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Introduction to Mesa 3D/OpenGL

Mesa 3d is a big project :-)We are going to focus on the OpenGL development.

Eleni Maria Stea Hacking on Mesa 3D 4 / 26

Page 5: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Introduction to Mesa 3D/OpenGL

The OpenGL API as part of the Linux Graphics Stack

https://en.wikipedia.org/wiki/Mesa_(computer_graphics)

Indirect Rendering

GLX (OpenGL extension to the X window system)

Direct Rendering

DRI (Direct Rendering Infrastructure)

DRM (Direct Rendering Manager)

A combination of the two

GLX + AIGLX (Accelerated Indirect GLX)

Eleni Maria Stea Hacking on Mesa 3D 5 / 26

Page 6: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

So, what’s our contribution to the OpenGL implementation?

▶ we write extensions (16-bits etc)▶ we add new features for the extensions to work▶ testing and bug fixes▶ releases

This means that we occasionally make changes to:

▶ the mesa core▶ the glsl compiler▶ the hardware drivers▶ and anywhere else needed

Eleni Maria Stea Hacking on Mesa 3D 6 / 26

Page 7: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

OpenGL Extensions 1/4 : The concept behind

▶ OpenGL is extensible by design.

▶ Any hardware manufacturer who wishes to add a new feature that was missingfrom OpenGL can write an extension to provide it.

▶ Any user wishing to use the feature can then use the extension through theextension mechanism.

▶ Khronos group (that releases the OpenGL spec) decides which extensions willbecome part of the next OpenGL core version.

▶ The manufacturers that write their own OpenGL implementations (Nvidia,Apple, Intel/Mesa, AMD/Mesa etc) and wish to release that new version mustimplement all the extensions that have been decided to be part of the core.

Eleni Maria Stea Hacking on Mesa 3D 7 / 26

Page 8: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

OpenGL Extensions 2/4: Τhe specs

OpenGL Registry

git clone https://github.com/KhronosGroup/OpenGL-Registry.git

Extensions Specifications

▶ overview, contributors etc▶ dependencies▶ errors▶ detailed descriptions

Eleni Maria Stea Hacking on Mesa 3D 8 / 26

Page 9: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

OpenGL Extensions 3/4: Required changes

Adding a new extension requires changes in the API, the hardware drivers code (see i965), to the GLSL

compiler and elsewhere.

$ ack-grep -i -l ARB_shader_storage_buffer_object mesa

include/GL/glcorearb.hinclude/GL/glext.hsrc/mapi/glapi/registry/gl.xmlsrc/mapi/glapi/gen/Makefile.amsrc/mapi/glapi/gen/gl_API.xmlsrc/mesa/drivers/dri/i965/intel_extensions.csrc/mesa/main/extensions_table.hsrc/mesa/main/context.csrc/mesa/main/mtypes.hsrc/mesa/main/bufferobj.csrc/mesa/main/get.csrc/mesa/main/version.csrc/mesa/main/get_hash_params.pysrc/mesa/main/uniforms.csrc/mesa/state_tracker/st_extensions.csrc/compiler/glsl/glsl_lexer.llsrc/compiler/glsl/ir.hsrc/compiler/glsl/ast_function.cppsrc/compiler/glsl/glsl_parser_extras.cppsrc/compiler/glsl/glsl_parser_extras.hmesa/include/GL/glcorearb.hmesa/include/GL/glext.h

mesa/src/mapi/glapi/registry/gl.xmlmesa/src/mapi/glapi/gen/meson.buildmesa/src/mapi/glapi/gen/Makefile.ammesa/src/mapi/glapi/gen/Makefilemesa/src/mapi/glapi/gen/Makefile.inmesa/src/mapi/glapi/gen/gl_API.xmlmesa/src/mesa/drivers/dri/i965/intel_extensions.cmesa/src/mesa/main/extensions_table.hmesa/src/mesa/main/context.cmesa/src/mesa/main/mtypes.hmesa/src/mesa/main/bufferobj.cmesa/src/mesa/main/get.cmesa/src/mesa/main/version.cmesa/src/mesa/main/get_hash_params.pymesa/src/mesa/main/uniforms.cmesa/src/mesa/state_tracker/st_extensions.cmesa/src/compiler/glsl/glsl_lexer.llmesa/src/compiler/glsl/ir.hmesa/src/compiler/glsl/ast_function.cppmesa/src/compiler/glsl/glsl_parser_extras.cppmesa/src/compiler/glsl/glsl_parser_extras.h

Eleni Maria Stea Hacking on Mesa 3D 9 / 26

Page 10: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

GLSL/GLSL Compiler

In the previous slide we mentioned a ... GLSL compiler

What’s the purpose of using a compiler into a graphics API?

Eleni Maria Stea Hacking on Mesa 3D 10 / 26

Page 11: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

Rendering με το OpenGL : how we use the API

Remember that every OpenGL objectis made by primitives!

Eleni Maria Stea Hacking on Mesa 3D 11 / 26

Page 12: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

The OpenGL Pipeline : Fully Programmable using the GLSL

http://chamilo2.grenet.fr/inp/courses/ENSIMAG4MMG3D/document/instructions/practical_01.html

Eleni Maria Stea Hacking on Mesa 3D 12 / 26

Page 13: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

GLSL->GLSL IR->NIR->ASM

From OpenGL Shading Language to Assembly

▶ GLSL▶ GLSL IR▶ NIR : SSA (Single Static Assignment) + non SSA▶ Native Code

Eleni Maria Stea Hacking on Mesa 3D 13 / 26

Page 14: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

GLSL->GLSL IR->NIR->ASM 1/4

Let us examine a very simple OpenGL program:

GLSL

attribute vec4 vertex;void main(){

gl_Position = vertex;}

GLSL IR

((declare (location=0 shader_out ) vec4 gl_Position)(declare (location=17 shader_in ) vec4 vertex)( function main(signature void(parameters)((assign (xyzw) (var_ref gl_Position) (var_ref vertex) )

))))

Eleni Maria Stea Hacking on Mesa 3D 14 / 26

Page 15: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

GLSL->GLSL IR->NIR->ASM 2/4

NIR (ssa form)

decl_var shader_in INTERP_MODE_NONE vec4 vertex (VERT_ATTRIB_GENERIC0, 17, 0)decl_var shader_out INTERP_MODE_NONE vec4 gl_Position (VARYING_SLOT_POS, 0, 0)decl_function main returning voidimpl main {

block block_0:/* preds: */vec1 32 ssa_0 = load_const (0x00000000 /* 0.000000 */)vec4 32 ssa_1 = intrinsic load_input (ssa_0) () (0, 0) /* base=0 */ /* component=0 */intrinsic store_output (ssa_1, ssa_0) () (0, 15, 0) /* base=0 */ /* wrmask=xyzw */

/* component=0 */ /* gl_Position *//* succs: block_0 */block block_0:

}

Eleni Maria Stea Hacking on Mesa 3D 15 / 26

Page 16: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

GLSL->GLSL IR->NIR->ASM 3/4

NIR (final form)

decl_var shader_in INTERP_MODE_NONE vec4 vertex (VERT_ATTRIB_GENERIC0, 17, 0)decl_var shader_out INTERP_MODE_NONE vec4 gl_Position (VARYING_SLOT_POS, 0, 0)decl_function main returning voidimpl main {

block block_0:/* preds: */vec1 32 ssa_0 = load_const (0x00000000 /* 0.000000 */)vec4 32 ssa_1 = intrinsic load_input (ssa_0) () (0, 0) /* base=0 */ /* component=0 */intrinsic store_output (ssa_1, ssa_0) () (0, 15, 0) /* base=0 */ /* wrmask=xyzw */

/* component=0 */ /* gl_Position *//* succs: block_0 */block block_0:

}

Eleni Maria Stea Hacking on Mesa 3D 16 / 26

Page 17: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Developing the OpenGL

GLSL->GLSL IR->NIR->ASM 4/4

Native Code

START B0 (26 cycles)mov(8) g123<1>UD g1<8,8,1>UD { align1 WE_all 1Q compacted };mov(8) g124<1>F g2<8,8,1>F { align1 1Q compacted };mov(8) g125<1>F g3<8,8,1>F { align1 1Q compacted };mov(8) g126<1>F g4<8,8,1>F { align1 1Q compacted };mov(8) g127<1>F g5<8,8,1>F { align1 1Q compacted };send(8) null<1>F g123<8,8,1>F

urb 1 SIMD8 write mlen 5 rlen 0 { align1 1Q EOT };nop ;

END B0

Eleni Maria Stea Hacking on Mesa 3D 17 / 26

Page 18: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Debugging Tools

GLSL->GLSL IR->NIR->ASM Debugging

We can test the intermediate representationsusing some Mesa environment variables for the drivers

$ export INTEL_DEBUG=vs$ export INTEL_DEBUG=fs$ export INTEL_DEBUG=vs,fs, ...

Like in the following example ;-)

Eleni Maria Stea Hacking on Mesa 3D 18 / 26

Page 19: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Debugging Tools

OpenGL Extensions 4/4: Testing & Debugging

And since we mentioned Debugging...

There’s a process through which we can test our OpenGL extensions implementations.

▶ For every new core feature that is added to OpenGL, developers (usually from Khronosgroup) write a test that tests the feature.

▶ This test performs the same operation using software rendering and shaders andcompares the output of each.

▶ VK-GL-CTS: https://github.com/KhronosGroup/VK-GL-CTS

▶ Every manufacturer can run the tests on its own implementation of an OpenGL version tomake sure that the features of that version work fine.

▶ When a test fails and the bug is not in the test, we know that probably our driver has aproblem.

▶ We also check for regressions from version to version or for cases where a feature mightbreak another one (a test that used to run without errors doesn’t do that anymore).

Eleni Maria Stea Hacking on Mesa 3D 19 / 26

Page 20: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Debugging Tools

Other Debugging Tools

Some useful tools for when a problem is detected.

▶ GDB

▶ MESA environment variables: https://www.mesa3d.org/envvars.html• We can dump and replace information (asm, IR, GLSL code)

• We can switch between GL versions

• We can test different code paths We can test driver specific operations

• only get a look at the website :)

▶ Some safe environments to run our experiments: chroot, jhbuild shell

Eleni Maria Stea Hacking on Mesa 3D 20 / 26

Page 21: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Debugging Tools

Debuging mesa, debugging open source graphics apps, debugging anything!

We can debug any graphics app!!

Mesa provides debugging tools for:▶ mesa itself▶ other open source graphics applications▶ even for proprietary software!

Eleni Maria Stea Hacking on Mesa 3D 21 / 26

Page 22: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Debugging Tools

Replacing the shaders’ code

Limbo Showcase:

No matter if we have a program’s source code, we can test but alsomodify its shaders while it’s running.

▶ $ export MESA_SHADER_DUMP_PATH=‘pwd‘/dump_sdr/

▶ $ export MESA_SHADER_READ_PATH=‘pwd‘/read_sdr/

Eleni Maria Stea Hacking on Mesa 3D 22 / 26

Page 23: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Contributions

How could someone get involved?

Coding Time!

▶ Mesa Repositories:https://cgit.freedesktop.org/mesa/

▶ Issue Tracking:https://www.mesa3d.org/bugs.html

▶ Debugging Tools:https://www.mesa3d.org/envvars.html

▶ Patches! (the process to submit them):https://www.mesa3d.org/submittingpatches.html

▶ Khronos CTS Repositories και Issue Tracker:https://github.com/KhronosGroup/VK-GL-CTS

Eleni Maria Stea Hacking on Mesa 3D 23 / 26

Page 24: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

Contributions

Some useful links

1 The mesa 3d graphics library (Documentation/Tools):https://www.mesa3d.org/

2 Khronos OpenGL registry:https://www.khronos.org/registry/OpenGL/index_gl.php

3 Intel microarchitectures:https://en.wikichip.org/wiki/intel/microarchitectures

4 Intel PRMs:https://01.org/linuxgraphics/documentation/hardware-specification-prms

5 Iago Toral’s introduction on mesa:https://blogs.igalia.com/itoral/category/graphics/

6 Jasper St. Pierre’s blog post on Linux graphics stack:http://blog.mecheye.net/2012/06/the-linux-graphics-stack/

7 Stéphane Marchesin’s book on Linux graphics drivers:https://people.freedesktop.org/~marcheu/linuxgraphicsdrivers.pdf

8 Samuel Iglesias’s posts on Piglit:https://blogs.igalia.com/siglesias/

9 Alejandro Piñeiro’s posts on Mesa/Intel driver development we do at Igalia:https://blogs.igalia.com/apinheiro/

10 Ben Widawsky’s post on GEN graphics and the URB:https://bwidawsk.net/blog/index.php/2015/09/gen-graphics-and-the-urb

Eleni Maria Stea Hacking on Mesa 3D 24 / 26

Page 25: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

The End...

[email protected]://eleni.mutantstargoat.com/hikiko

Eleni Maria Stea Hacking on Mesa 3D 25 / 26

Page 26: Hacking on Mesa 3D · DevelopingtheOpenGL So,what’sourcontributiontotheOpenGLimplementation? wewriteextensions(16-bitsetc) weaddnewfeaturesfortheextensionstowork

The End...

Thank you very much!

Eleni Maria Stea Hacking on Mesa 3D 26 / 26