This page has been translated automatically.
Программирование
Fundamentals
Setting Up Development Environment
UnigineScript
High-Level Systems
C++
C#
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
Rendering-Related Classes
Внимание! Эта версия документация УСТАРЕЛА, поскольку относится к более ранней версии SDK! Пожалуйста, переключитесь на самую актуальную документацию для последней версии SDK.
Внимание! Эта версия документации описывает устаревшую версию SDK, которая больше не поддерживается! Пожалуйста, обновитесь до последней версии SDK.

UUSL Keywords and Types

This documentation article contains information about keywords, data types, pre-defined variables, constants and other features of the UUSL. This information can be used as the reference document for writing shaders.

To start using UUSL, include the core/shaders/common/common.h file.

UUSL
#include <core/shaders/common/common.h>

For fragment shader functions, use the core/shaders/common/fragment.h file.

Data Types

This articles contains different data types of UUSL.

UUSL OpenGL Direct3D Description
int2 ivec2 int2 A vector with two 32-bit signed integer components.
int3 ivec3 int3 A vector with three 32-bit signed integer components.
int4 ivec4 int4 A vector with four 32-bit signed integer components.
uint2 uvec4 int2 A vector with two 32-bit unsigned integer components.
uint3 uvec4 uint3 A vector with three 32-bit unsigned integer components.
uint4 uvec4 uint4 A vector with four 32-bit unsigned integer components.
float float float A 32-bit floating point value.
float2 vec2 float2 A vector with two 32-bit floating point value components.
float3 vec3 float3 A vector with three 32-bit floating point value components.
float4 vec4 float4 A vector with four 32-bit floating point value components.
float2x2 mat2 float2x2 A matrix with 2 rows, 2 column of 32-bit floating point value components.
float3x3 mat3 float3x3 A matrix with 3 rows, 3 column of 32-bit floating point value components.
float4x4 mat4 float4x4 A matrix with 4 rows, 4 column of 32-bit floating point value components.

Use these variable aliases when working with textures to make your shader's code readable:

UUSL OpenGL Direct3D Description
TYPE_R float float A 32-bit floating point value.
TYPE_RG float2 float2 A vector with two 32-bit floating point value components.
TYPE_RGB float3 float3 A vector with three 32-bit floating point value components.
TYPE_RGBA float4 float4 A vector with four 32-bit floating point value components.

UUSL OpenGL Direct3D Description
FLOAT(X) float(X) X Converts the value to float.
FLOAT2(X) vec2(X) X Converts the value to the two-component vector.
FLOAT3(X) vec3(X) X Converts the value to the three-component vector.
FLOAT4(X) vec4(X) X Converts the value to the four-component vector.
INT(X) int(X) X Converts the value to int.
INT2(X) int2(X) X Converts the value to the two component vector.
INT3(X) int3(X) X Converts the value to the three-component vector.
INT4(X) int4(X) X Converts the value to the three-component vector.
BOOL2(X) bvec2(X) X Converts the value to the two component boolean vector.
BOOL3(X) bvec3(X) X Converts the value to the three-component boolean vector.
BOOL4(X) bvec4(X) X Converts the value to the three-component boolean vector.

Defined Values

UUSL Value
float_isrgb 2.233333f
float4_zero float4(0.0f,0.0f,0.0f,0.0f)
float4_one float4(1.0f,1.0f,1.0f,1.0f)
float4_neg_one float4(-1.0f,-1.0f,-1.0f,-1.0f)
float4_isrgb float4(float_isrgb,float_isrgb,float_isrgb,float_isrgb)
float3_zero float3(0.0f,0.0f,0.0f)
float3_one float3(1.0f,1.0f,1.0f)
float3_neg_one float3(-1.0f,-1.0f,-1.0f)
float3_up float3(0.0f,0.0f,1.0f)
float3_isrgb float3(float_isrgb,float_isrgb,float_isrgb)
float3_luma float3(0.299f,0.587f,0.114f)
float2_zero float3(2.2f,2.2f,2.2f)
float2_one float2(1.0f,1.0f)
float2_neg_one float2(-1.0f,-1.0f)
float2_isrgb float2(float_isrgb,float_isrgb)
int4_zero int4(0,0,0,0)
int4_one int4(1,1,1,1)
int4_neg_one int4(-1,-1,-1,-1)
int3_zero int3(0,0,0)
int3_one int3(1,1,1)
int3_neg_one int3(-1,-1,-1)
int2_zero int2(0,0)
int2_one int2(1,1)
int2_neg_one int2(-1,-1)
PI 3.141592654f
PI2 6.283185308f
PI05 1.570796327f
LOG2 0.693147181f
LOG10 2.302585093f
SQRT2 1.414213562f
EPSILON 1e-6f
INFINITY 1e+9f
DEG2RAD PI / 180.0f
RAD2DEG 180.0f / PI
STATICARRAY (float2,halton16,16) Array containing Halton sequence.
STATICARRAY (float2,halton8,8) Array containing Halton sequence.

Main Function

Void Main Function

To start and end the void Main function of the fragment shader (when you don't need to provide the output color value but depth), use the following instructions:

UUSL
MAIN_VOID_BEGIN(STRUCT_IN)
	<your code here>
MAIN_VOID_END
Warning
You should add a new line (press Enter) after closing the instruction.

This code is equivalent to:

OpenGL
void main() {
	<your code here>
}
Direct3D
void main(STRUCT_IN IN) {
	<your code here>
}

Main Function with Return Value

To start and end the Main function with return value, use the following instructions:

UUSL
MAIN_BEGIN(STRUCT_OUT,STRUCT_IN)
	<your code here>
MAIN_END
Warning
You should add a new line (press Enter) after closing the instruction.

This code is equivalent to:

OpenGL
void main() {
	<your code here>
}
Direct3D
STRUCT_OUT main(STRUCT_IN IN) { 
	STRUCT_OUT OUT;
	<your code here>
return OUT; }

Geometry Shader Main Function

To start and end the Main function of the geometry shader, use the following instructions:

UUSL
MAIN_GEOM_BEGIN(STRUCT_OUT,STRUCT_IN)
	<your code here>
END_GEOM
Warning
You should add a new line (press Enter) after closing the instruction.

This code is equivalent to:

OpenGL
layout(TYPE_GEOM_IN) in;
layout(TYPE_GEOM_OUT,max_vertices = MAX_VERTICES_GEOM) out;
void main() {
	<your code here>
}
Direct3D
[maxvertexcount(MAX_VERTICES_GEOM)]
void main(TYPE_GEOM_IN STRUCT_IN IN[COUNT_GEOM_IN],inout TYPE_GEOM_OUT<STRUCT_OUT> stream) {
	STRUCT_OUT OUT;
	<your code here>
}

Global Variables

To define a global variable, use the following syntax:

UUSL
GLOBAL <your var>

This is equal to the following GLSL and HLSL commands:

OpenGL
uniform <your var>
Direct3D
uniform <your var>

Static Variables and Arrays

Static Variables

To define a static variable, use the following syntax:

UUSL
STATICVAR <your var>

This is equal to the following GLSL and HLSL commands:

OpenGL
const <your var>
Direct3D
static const <your var>

Static Arrays

To work with static arrays by using UUSL, use the following instructions:

UUSL
STATICARRAY(TYPE,NAME,SIZE)
	<your array members>
ENDARRAY
Warning
You should add a new line (press Enter) after closing the instruction.
  • TYPE - the type of the array (float, etc.).
  • NAME - the name of the array.
  • SIZE - the size of the array.

This code block is equivalent to:

OpenGL
TYPE NAME [SIZE] = TYPE[](<your array members>);
Direct3D
static const TYPE NAME [SIZE] = {<your array members>};

Blending Presets

UUSL contains blending presets. When you specify the type of blending in material, the USSL wrapper automatically defines a new definition, that you can use in your shader:

Blending type UUSL
BLEND_SRC_FUNC_SRC_ALPHA && BLEND_DEST_FUNC_ONE_MINUS_SRC_ALPHA BLEND_ALPHABLEND
BLEND_SRC_FUNC_ONE && BLEND_DEST_FUNC_ONE BLEND_ADDITIVE
BLEND_SRC_FUNC_DEST_COLOR && BLEND_DEST_FUNC_ZERO BLEND_MULTIPLICATIVE
BLEND_SRC_FUNC_NONE && BLEND_DEST_FUNC_NONE BLEND_NONE

HLSL features

HLSL Flow Control Attributes

UUSL OpenGL Direct3D Description
branch - [branch] Performs branching using control flow instructions.
call - [call] Prevents inlining of a function.
flatten - [flatten] Performs branching using conditional move instructions.
ifAll - [ifAll] Executes the conditional part of an if statement when the condition is true for all threads on which the current shader is running.
ifAny - [ifAny] Executes the conditional part of an if statement when the condition is true for any thread on which the current shader is running.
isolate - [isolate] Optimizes the specified HLSL code independently of the surrounding code.
loop - [loop] Gives preference to flow control constructs.
maxexports - [maxexports] Specifies the maximum number of export instructions that will execute along any path from the entry point to an exit.
maxInstructionCount - [maxInstructionCount] Sets the maximum number of instructions available to a shader.
maxtempreg - [maxtempreg] Restricts temporary register usage to the number of registers specified. Generates a compiler error if unsuccessful.
noExpressionOptimizations - [noExpressionOptimizations] Avoids optimization of expressions.
predicate - [predicate] Performs branching by using predication.
predicateBlock - [predicateBlock] Performs branching by using predicated exec blocks.
reduceTempRegUsage - [reduceTempRegUsage] Restricts temporary register usage to the number of registers specified. Generates a compiler warning if unsuccessful.
removeUnusedInputs - [removeUnusedInputs] Removes unused interpolator inputs from pixel shaders.
sampreg - [sampreg] Sets the ranges of pixel sampler and vertex sampler registers used by the compiler.
unroll - [unroll] Avoids flow control constructs.
unused - [unused] Suppresses warnings about unused shader parameters.
xps - [xps] Specifies that all vertex fetch operations are done after the last vfetch instruction. This instruction is used to reduce the latency back to the command processor to free the vertex buffer.
earlydepthstencil - [earlydepthstencil] Forces depth-stencil testing before a shader executes. Doesn't work with custom depth.

Interpolation Modifiers

UUSL OpenGL Direct3D Description
MODIFER_LINEAR - linear Interpolate between shader inputs; linear is the default value if no interpolation modifier is specified.
MODIFER_CENTROID - centroid Interpolate between samples that are somewhere within the covered area of the pixel (this may require extrapolating end points from a pixel center). Centroid sampling may improve antialiasing if a pixel is partially covered (even if the pixel center is not covered). The centroid modifier must be combined with either the linear or noperspective modifier.
MODIFER_NOINTERPOLATION - nointerpolation Do not interpolate.
MODIFER_NOPERSPECTIVE - noperspective Do not perform perspective-correction during interpolation. The noperspective modifier can be combined with the centroid modifier.
MODIFER_SAMPLE - sample Interpolate at sample location rather than at the pixel center. This causes the pixel shader to execute per-sample rather than per-pixel.

See Also

See also the article on Interpolation Modifiers.

Constants and Structs

Structs

In USSL structs is the way to organize the bunch of input and output data.

To start using structs in your shader code, use the following instructions:

UUSL
STRUCT(NAME)
	<your data>
END
Warning
You should add a new line (press Enter) after closing the instruction.
  • NAME - the name of the struct.

Here is an example of vertex shader input struct:

UUSL
// Input vertex data
STRUCT(VERTEX_IN)
	INIT_ATTRIBUTE(float4,0,POSITION)	// Vertex position
	INIT_ATTRIBUTE(float4,1,TEXCOORD0)	// Vertex texcoord (uv)
	INIT_ATTRIBUTE(float4,2,TEXCOORD1)	// Vertex basis tangent
	INIT_ATTRIBUTE(float4,3,TEXCOORD2)	// Vertex color
END
Warning
You should add a new line (press Enter) after closing the instruction.

After that, you should pass the VERTEX_IN struct to the main function of the vertex shader.

Constants

For using constant buffer in your shader code, use the following instructions:

UUSL
CBUFFER(NAME)
	<your data>
END
Warning
You should add a new line (press Enter) after closing the instruction.
  • NAME - the name of the cbuffer.

Here is an example of using constant buffer:

UUSL
CBUFFER(parameters)
	UNIFORM float grayscale_power;
END
Warning
You should add a new line (press Enter) after closing the instruction.

If you defined this parameter in the material, you'll be able to specify this value.

See Also

  • How we use CBUFFER instruction in the tutorial on shaders for post-process pass.

For Loop

There is a for loop implemented in UUSL:

UUSL
forloop(NAME,BEGIN,END)
UUSL
for(int NAME = (BEGIN); NAME < (END); NAME++)

This command exports the shader program into a file: for Direct3D the file will have an .hlsl extension, for OpenGL the file will have an .glsl extension.

The command expands all of the predefined UUSL syntax and creates shader files in native languages for both graphic APIs.

Shader Export

To export shader, use the following command:

UUSL
EXPORT_SHADER(FILE)
  • FILE - The name of the file.

This command exports the shader program into a file: for Direct3D the file will have an .hlsl extension, for OpenGL the file will have an .glsl extension.

The command expands all of the predefined UUSL syntax and creates shader files in native languages for both graphic APIs.

Last update: 03.07.2017
Build: ()