# Stride Shading Language (SDSL) Overview Welcome to the documentation page on understanding the bigger concepts of Stride Shading Language (SDSL). This guide aims to provide an overview of how SDSL functions and the key principles behind it. If you're a seasoned shader developer looking to extend Stride's standard PBR shaders or create custom shader functionality, this information will be invaluable. ## Introduction to SDSL Stride Shading Language, or SDSL, is a powerful system that allows for flexible and modular shader development within the Stride game engine. While it may seem complex at first, once you grasp its core principles, it becomes a versatile tool for crafting unique shaders. ## Modular Inheritance SDSL operates on the principle of modular inheritance. This means that you can create custom shaders by inheriting from existing shaders. When you inherit from a shader, you effectively pull in its code into your current shader. This inheritance mechanism allows you to build on existing shaders and make precise modifications. ## Shader Inheritance Hierarchy In Stride, shaders are organized in an inheritance hierarchy. When you derive from a shader, you can override its methods, similar to object-oriented programming (OOP). The final shader will use your overridden methods. You can also call `base.Method()` to access the base shader's implementation. Understanding the shader hierarchy is crucial for effective shader development. ## Streams and Dataflow SDSL uses the concept of streams to represent the state of a shader. Data flows from the Vertex Shader (VS) to other stages and finally to the Pixel Shader (PS). At each stage, you can modify the streams, enabling you to manipulate various aspects of the shader's behavior. This dataflow structure is integral to creating complex shaders. ## Key Shaders in SDSL To work with SDSL effectively, it's essential to know the key shaders and shader components that make up the Stride rendering pipeline. Here are some critical ones: - `ShadingBase`: Provides the foundation for shading. - `ShaderBaseStream`: Represents the stream of data in the shader. - `PositionStream4`: Handles the position stream. - `NormalBase` and `NormalStream`: Deal with normals. - `TransformationBase` and `Transformation`: Manage transformations. - `Texturing`: Handles texture-related operations. By understanding and inheriting from these shaders, you can modify various aspects of the rendering process. In addition to these key shaders, Stride offers a valuable tool known as the **Stride Shader Explorer**. This tool visualizes the shader hierarchy within the Stride game engine and can be found [here](https://github.com/tebjan/Stride.ShaderExplorer). The Stride Shader Explorer has several features, including: - Listing base shaders. - Listing derived shaders. - Navigation history for easy traversal of the shader hierarchy. - Showing where a method or variable is defined or used within the shaders. - Searching for a specific shader. - Allowing you to add custom shader folders. - Providing the ability to open selected shaders in the file explorer. The Stride Shader Explorer is a powerful resource for understanding the shader structure in Stride, making it easier to work with and modify shaders to suit your specific rendering needs. ## Creating Custom Shaders To create custom shaders, you'll inherit from the shader that defines what you want to modify. For instance, if you wish to change UV generation, you can inherit from `Texturing` and modify the `streams.TexCoord`. Additionally, you can use the `ComputeColor` shader class to create inputs for your shader and customize shader behavior. ## Effects and Shaders Effects in Stride use C#-like syntax to further combine shaders. They provide conditional composition of shaders to generate effect permutations. As some platforms can't compile shaders at runtime (e.g., iOS, Android), effect permutation files (.sdeffectlog) are used to enumerate all permutations ahead of time. In addition to shaders, Stride uses a programmable shading pipeline. You can write custom shaders, create Effects from them, and use them for drawing. The EffectSystem class provides an easy way to load an effect. You can then bind the effect as pipeline state. An effect often defines a set of parameters, which you need to bind resources before drawing. ## Targeting Different Platforms Stride shaders are automatically converted to the target graphics platform—either plain HLSL for Direct3D, GLSL for OpenGL, or SPIR-V for Vulkan platforms. For mobile platforms, shaders are optimized by a GLSL optimizer to improve performance. ## Shading Language Stride provides a superset of the HLSL Shading language, bringing advanced and higher-level language constructions. This includes: - Extensibility to allow shaders to be extended easily using object-oriented programming concepts such as classes, inheritance, and composition. - Modularity to provide a set of modular shaders, each focusing on a single rendering technique, making them more easily manageable. - Reusability to maximize code reuse between shaders. SDSL is automatically transformed into an existing shading language (HLSL, GLSL, GLSL ES). ## Shader Classes, Mixins, and Inheritance SDSL is an extension of HLSL, making it closer to C# syntax and concepts. The language is object-oriented: - Shader classes are the foundation of the code. - Shader classes contain methods and members. - Shader classes can be inherited, and methods can be overridden. - Member types can be shader classes. Inheritance in SDSL is performed through mixins, which means that the order of inheritance defines the actual implementation of a method (the last override). You can call the previous implementation of a method using `base.<method name>(<arguments>)`. ## Keywords SDSL introduces additional keywords to the standard HLSL, such as `stage`, `stream`, `streams`, `override`, and more. These keywords provide specific functionality and help structure your shaders effectively. ## Composition In addition to the inheritance system, SDSL introduces the concept of composition. A composition is a member whose type is another shader class. It's defined the same way as variables. You can compose with an instance of the desired shader class or an instance of a shader class that inherits from the desired one. # Automatic Shader Stage Input/Output When you write an HLSL shader, you often need to define your vertex attributes and manage their flow across various shader stages. In SDSL, this process is simplified through automatic shader stage input/output. - **Streams**: SDSL uses the concept of streams to manage variables across different shader stages. Streams are variables defined with the `stream` keyword. These variables are accessible at every stage of the shader, and they serve as a global structure for storing data needed across several stages of the shader. - **Attributes, Varyings, and Outputs**: In traditional shader programming, you must explicitly create semantics for variables. In SDSL, the compiler generates semantics automatically. However, keep in mind that variables with the same semantic will be merged in the final shader, which can be useful for local usage. ## Abstract Methods SDSL supports abstract methods, which should be prefixed with the `abstract` keyword. This feature allows you to inherit from a shader class with abstract methods without implementing them immediately. The compiler will generate a warning, but you can implement these methods in your final shader to prevent compilation errors. ## Composition and Advanced Concepts In addition to inheritance, SDSL introduces the concept of composition. Composition allows you to create members whose type is another shader class, providing a flexible way to structure your shaders. It's a powerful tool to enhance code organization and reusability. ## Understanding the Shader System The SDSL shader system is like a graph in the end, with inheritance and composition forming the nodes and connections. This system was developed by experts in the field and provides a high degree of flexibility for crafting shaders that suit your specific needs. By delving into the intricacies of SDSL and understanding the inheritance hierarchy, streams, and composition, you can create advanced shaders tailored to your game's requirements. ## Conclusion Stride Shading Language (SDSL) is a powerful tool for creating custom shaders within the Stride game engine. It offers a flexible and modular approach to shader development, allowing you to build on existing shaders and create unique rendering effects. While SDSL may appear complex initially, a deep understanding of its principles will empower you to craft stunning visuals for your games. For further details and specific shader code examples, consider referring to the Stride documentation and the shader source code in the `/log` folder. We hope this overview has provided you with valuable insights into the world of SDSL, enabling you to take your game's visuals to the next level. Happy shader programming! For more detailed information, code examples, and practical usage, please refer to the official Stride documentation.