
Cross-Compiling Shading Languages Masterthesis Lukas Hermanns KOM-M-0598 Fachbereich Elektrotechnik und Informationstechnik Fachbereich Informatik (Zweitmitglied) Fachgebiet Multimedia Kommunikation Prof. Dr.-Ing. Ralf Steinmetz Cross-Compiling Shading Languages Masterthesis KOM-M-0598 Submitted by Lukas Hermanns Day of submission: October 19, 2017 Consultant: Prof. Dr.-Ing. Ralf Steinmetz Supervisor: Robert Konrad Technische Universität Darmstadt Fachbereich Elektrotechnik und Informationstechnik Fachbereich Informatik (Zweitmitglied) Fachgebiet Multimedia Kommunikation (KOM) Prof. Dr.-Ing. Ralf Steinmetz Ehrenwörtliche Erklärung Hiermit versichere ich, die vorliegende Masterthesis ohne Hilfe Dritter und nur mit den angegebenen Quellen und Hilfsmitteln angefertigt zu haben. Alle Stellen, die aus den Quellen entnommen wurden, sind als solche kenntlich gemacht worden. Diese Arbeit hat in dieser oder ähnlicher Form noch keiner Prüfungsbehörde vorgelegen. Die schriftliche Fassung stimmt mit der elektronischen Fassung überein. Darmstadt, den 19. Oktober 2017 Lukas Hermanns i Abstract Shading languages are the major class of programming languages for a modern mainstream Graphics Processing Unit (GPU). The programs of those languages are called “Shaders” as they were originally used to describe shading characteristics for computer graphics applications. To make use of GPU accel- erated shaders a sophisticated rendering Application Programming Interface (API) is required and the available rendering APIs at the present time are OpenGL, Direct3D, Vulkan, and Metal. While Direct3D and Metal are only supported on a limited set of platforms, OpenGL and Vulkan are for the most part platform independent. On the one hand, Direct3D is the leading rendering API for many real-time graph- ics applications, especially in the video game industry. But on the other hand, OpenGL and Vulkan are the prevalent rendering APIs on mobile devices, especially for Android with the largest market share. Each rendering API has its own shading language which are very similar to each other but varying enough to make it difficult for developers to write a single shader to be used across multiple APIs. However, since the enormous appearance of mobile devices many graphics systems are reliant on being platform independent. Therefore, several rendering technologies must be provided as back ends. The naive approach is to write all shaders multiple times, i.e. once for each shading language which is error- prone, highly redundant, and difficult to maintain. This thesis investigates different approaches to automatically transform shaders from one high-level language into another, so called “cross-compilation” (sometimes also referred to as “trans-compilation”). High-level to high-level translation is reviewed as well as algorithms with an Intermediate Representation (IR) such as Standard Portable Intermediate Representation (SPIR-V). We are focusing the two most prevalent shading languages, which are firstly OpenGL Shading Language (GLSL) and secondly DirectX High Level Shading Language (HLSL), while Metal Shading Language (MSL) is only briefly examined. The benefits and failings of state-of-the-art approaches are clearly separated and a novel algorithm for generic shader cross-compilation is presented. 1 Acknowledgment First of all, I would like to thank my family for their support during my entire study. Through their support, they made many of the circumstances beside my study much easier. I would also like to thank Robert Konrad, who gave me the opportunity to base my thesis upon a long-term project in the research field of cross-platform development for real-time graphics applications. Finally, I would also like to thank Marko Pintera for his contribution to the XShaderCompiler project. 3 Contents 1 Introduction 7 1.1 Overview . .7 1.2 Motivation . .7 1.3 Outline . .8 2 Background 11 2.1 Shaders . 11 2.1.1 Language Feature Comparison . 15 2.2 Formal Languages . 16 2.2.1 Regular Expressions . 16 2.2.2 Pattern Systems . 17 2.2.3 Grammars . 17 2.3 Compilers . 19 2.3.1 Lexical Analyzer . 19 2.3.2 Abstract Syntax Tree . 20 2.3.3 Syntactic Analyzer . 21 2.3.4 Semantic Analyzer . 21 2.3.5 Control Flow Graph . 22 3 Related Work 23 3.1 Proceedings . 23 3.1.1 Macro Expansion . 23 3.1.2 Meta-Language & Graph-Based Shaders . 24 3.1.3 Byte-Code Decompiling . 25 3.1.4 Source-to-Source . 25 3.1.5 Intermediate Language . 27 3.2 Comparison . 29 4 Concept 31 4.1 Approach . 31 4.2 Investigation . 31 4.2.1 Matrix Ordering . 31 4.2.2 Matrix Subscript Swizzling . 32 4.2.3 Memory Packing Alignment . 33 4.2.4 Type Conversion . 35 4.2.5 Input and Output . 36 4.2.6 Intrinsics . 39 4.2.7 Attributes . 41 4.2.8 Buffer Objects . 43 4.2.9 Structures . 46 4.2.10 Member Functions . 48 4.2.11 Boolean Vector Expressions . 49 4.2.12 Name Mangling . 50 4.3 Restrictions . 51 5 5 Implementation 53 5.1 Front-End . 53 5.1.1 Uniform AST Design . 53 5.1.2 Generic Parsing . 57 5.1.3 Context Analysis . 59 5.2 Middle-End . 64 5.2.1 Transformation . 64 5.3 Back-End . 66 5.3.1 Code Generation . 66 6 Results and Discussion 69 6.1 Benchmarks . 69 6.1.1 Simple Texturing . 69 6.1.2 Compute Shader for Filtering . 71 6.1.3 Practical Shaders . 74 6.2 Performance Comparison . 75 7 Conclusion and Future Work 77 7.1 Conclusion . 77 7.2 Future Work . 77 7.3 Appendix . 77 Bibliography 78 6 Contents 1 Introduction 1.1 Overview In this thesis a novel method for cross-compiling shading languages is presented and compared to other state of the art approaches. The main focus is on the two mainstream shading languages HLSL and GLSL for real-time graphics applications. HLSL is the shading language of the Direct3D rendering API, while GLSL and its subtly modified dialect OpenGL ES Shading Language (ESSL) is the shading language for the OpenGL, OpenGL ES, and WebGL rendering APIs. There is also the modern shading language called “Metal” (or rather MSL) that is supported by the correspondent rendering API, but due to its limitation for products by Apple Inc. and the alternative of ESSL it is only briefly examined. Although Direct3D is only available on a limited set of platforms as well, more precisely on platforms provided by Microsoft Corporation, its large distribution in the video game industry makes HLSL a major candidate for cross-compilation. Although HLSL and GLSL have the same purpose and are quite similar in many ways, there are also lots of differences in both syntax and semantics. Even a very primitive shader program like shown in Listing 1.1 must be translated to something that looks quite different as shown in Listing 1.2, or vice versa. These two shaders both only pass the input vertex coordinate to the fragment shader. Listing 1.1: HLSL Listing 1.2: GLSL 1 struct v2f { 1 #version 130 2 float4 position : SV_Position; 2 in vec4 coord; 3 }; 3 void main() { 4 v2f VS(float4 coord : COORD) { 4 gl_Position = coord; 5 return (v2f)coord; 5 } 6 } This thesis covers different approaches to allow those shaders being written only once and to be used on multiple rendering APIs. These approaches reach from heavy macro meta-programming over simple parsers to full shader compilers. 1.2 Motivation Platform independence is a very important quality for a broad range of applications. This applies to end user applications that are meant to run on a variety of desktop and mobile platforms such as Windows, macOS, GNU/Linux, Android, and iOS as well as to middleware software that composes the foundation of such end user programs. In the situation of real-time graphics applications this requirement has changed in the recent years. Before the enormous appearance of mobile phones with 3D hardware acceleration (simply known as “smartphones”), the leading shading language was HLSL and its (now obsolete) dialect C for Graphics (Cg), which was developed by NVIDIA together with Microsoft [23, 34]. This was the case for at least the video game industry where HLSL (or Cg) was supported on the major gaming platforms: Microsoft® Windows, Microsoft® Xbox® 360, and Sony™ PlayStation® 3. In the field of research, GLSL was the prevalent shading language, due its platform independence on desktop computers. Though, since the market share of smartphones, especially those with Google Android and Apple iOS that do not sup- port HLSL at all, has grown rapidly, the importance to support multiple shading languages in graphics applications has increased. Because the naive approach of translating shader programs by hand is time consuming, error-prone, and hardly maintainable when shaders are altered or extended, an automatic translation of these pro- 7 grams is highly necessary. Since this problem is not new to the development process of platform in- dependent rendering systems, many different approaches and solutions exist throughout the web.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages87 Page
-
File Size-