Conversion Rules
Conversion rules allow converting a valid C# code, into a valid GLSL code, so that the code could be edited as C# in Visual Studio, and still compile as GLSL by the viewer.
Conversion rules are defined using line annotations.
Built-in Rules
A predefined set of conversion rules, that are used as a starting point and are covering most of the common cases. Additional rules could be added for more specific cases, and would have higher priority over the built-in rules.
namespaceandusingdeclaration lines are automatically removed.namespaceandclassindentations are automatically reduced.#regionand#endregionlines are automatically removed.C# array initialization is automatically converted to GLSL array initialization (see arrays workarounds).
C# keywords are removed
publicprivateinternalprotectedvirtualoverridestaticnew(see modifiers workarounds).C# explicit castings are removed
(float),(double)(see floating points workarounds).Underscores are removed from Environment’s casting methods and type aliases for
bool,int,uint, andfloat.Environment
Floatis replaced withfloat.Floating point
f/Fsuffix is removed.readonlyis automatically converted toconst(see const workarounds).refis automatically converted toinoutin functions declaration.refandoutparameter modifiers in functions calls are removed (see modifiers workarounds).#if Aand#if !Aare replaced with#ifdef Aand#ifndef A(see preprocessor directives workarounds).
Line Rules
Conversion rules that convert an entire line.
Replace Line
Replace a C# compatible line, with a GLSL compatible line.
- Rule:
//@replace-line: <value>
- value:
A line end terminated value.
- Example:
//@replace-line: float value = float(2); // GLSL casting float value = (float)2; // C# casting
Insert Line
Insert a line that is only needed in GLSL.
- Rule:
//@insert-line: <value>
- value:
A line end terminated value.
- Example:
//@insert-line: float getValue(); // declaration
Remove Line
Remove a redundant line, that is only needed in C# but not in GLSL.
- Rule:
//@remove-line- Example:
//@remove-line using System; // not needed in GLSL
Text Rules
Conversion rules that convert part of a line. These rules can be used with -all suffix (for example replace-all) to make them run on all of the subsequent lines.
Replace / Remove Text
Replace or remove all instances of a string.
- Rule:
//@replace, source: <instance>, target: <replacement>//@remove, source: <instance>, target: <replacement>
- source:
the text to search (string, identifier, or a number).
- target:
the text to replace with (string, identifier, or a number).
Replace / Remove Word
Replace or remove all instances of a word (surrounded by word boundaries).
- Rule:
//@replace-word, source: <word>, target: <replacement>//@remove-word, source: <word>, target: <replacement>
- source:
the word to search (string, identifier, or a number).
- target:
the text to replace with (string, identifier, or a number).
Replace Regex
Match regex, and replace using a pattern.
- Rule:
//@replace-regex, source: <regex>, target: <replacement>
- source:
Regex search pattern (string).
- target:
Replace pattern, using capture groups (string).
Capture groups can be referenced with
$and a group index, or a group name:
$1, $2, $3...
$groupName, or$(groupName)(defined with(?<groupName>...))- Example:
//@replace-regex, source: "(?<name>\\w+): (?<value>[0-9.]+)", target: "$name=$value"
Language Specific Rules
Conversion rules that amend C# with GLSL specific syntax.
Uniform
Convert a field declaration into a uniform declaration, removing a
const, or areadonlymodifiers if used, and preserving the uniform line annotation properties.
- Rule:
//@uniform
//@uniform, key1: value1, key2: value2, ...- Example:
Source C#
//@uniform, min: 0.0, step: 0.1 vec2 value = vec2(1.0, 2.0);Converted GLSL
//@uniform, min: 0.0, step: 0.1 uniform vec2 value = vec2(1.0, 2.0);
Const
Convert a variable declaration into a const value declaration.
- Rule:
//@const- Example:
Source C#
//@const vec2 value = vec2(1.0, 2.0);Converted GLSL
const vec2 value = vec2(1.0, 2.0);
Define
Convert a variable declaration or a lambda method (“expression bodied function”) into a
#definedirective.
- Rule:
//@define- Value Example:
Source C#
//@define float PI = 3.14159265f;Converted GLSL
#define PI 3.14159265- Macro Example:
Source C#
//@define float sqr(float x) => ((x) * (x));Converted GLSL
#define sqr(x) ((x) * (x))Note
Since the body of the method is used as it is, just like with a regular macro, parenthesis have to added around variables, and also parameters name should not conflict with accessed methods or members names, for example, the macro:
#define pow2(v, x) vec2(pow(v.x, x), pow(v.y, x))would produce an invalid code atv.x, asxis ambiguous.