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.

  • namespace and using declaration lines are automatically removed.

  • namespace and class indentations are automatically reduced.

  • #region and #endregion lines are automatically removed.

  • C# array initialization is automatically converted to GLSL array initialization (see arrays workarounds).

  • C# keywords are removed public private internal protected virtual override static new (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, and float.

  • Environment Float is replaced with float.

  • Floating point f/F suffix is removed.

  • readonly is automatically converted to const (see const workarounds).

  • ref is automatically converted to inout in functions declaration.

  • ref and out parameter modifiers in functions calls are removed (see modifiers workarounds).

  • #if A and #if !A are replaced with #ifdef A and #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 a readonly modifiers 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 #define directive.

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 at v.x, as x is ambiguous.