UUSL Textures
The Unified UNIGINE shader language (UUSL) unifies the way of using textures in shaders. You need to define the texture slot in the material, initialize the texture in the shader's code and then use it.
Here is an example of using texture in the vertex shader's code:
// Include the UUSL header file
#include <core/shaders/common/common.h>
// Initialize a texture from the first texture slot.
// It's better to use "define" command
// to avoid whole code correction if you
// change the texture slot in the material
#define TEX_HEIGHT 1
INIT_TEXTURE(TEX_HEIGHT)
/* Input and output structures */
MAIN_BEGIN(VERTEX_OUT,VERTEX_IN)
/* some code */
// Set the texture
float4 height = TEXTURE_BIAS_ZERO(TEX_HEIGHT,texcoord.xy);
/* some code */
MAIN_END
Texture Initialization Functions
The implementation of the SAMPLER(NUM) function in Direct3D is the following:
SamplerState s_sampler_ ## NUM : register(s ## NUM);
SamplerComparisonState s_sampler_compare_ ## NUM : register(s ## NUM);
INIT_TEXTURE (value NUM)
Initialize a sampler with the 2D texture.Equivalents
uniform sampler2D s_texture_ ## NUM;
Texture2D s_texture_ ## NUM : register(t ## NUM); SAMPLER(NUM)
Arguments
- value NUM - A number of the texture slot.
INIT_TEXTURE_CUBE (value NUM)
Initialize a sampler with the cubemap texture.Equivalents
uniform samplerCube s_texture_ ## NUM;
TextureCube s_texture_ ## NUM : register(t ## NUM); SAMPLER(NUM)
Arguments
- value NUM - A number of the texture slot.
INIT_TEXTURE_3D (value NUM)
Initialize a sampler with the texture3D.Equivalents
uniform sampler3D s_texture_ ## NUM;
Texture3D s_texture_ ## NUM : register(t ## NUM); SAMPLER(NUM)
Arguments
- value NUM - A number of the texture slot.
INIT_TEXTURE_ARRAY (value NUM)
Initialize a sampler with the array of 2D textures.Equivalents
uniform sampler2DArray s_texture_ ## NUM;
Texture2DArray s_texture_ ## NUM : register(t ## NUM); SAMPLER(NUM)
Arguments
- value NUM - A number of the texture slot.
INIT_TEXTURE_SHADOW (value NUM)
Initialize a sampler with the 2D shadow texture.Equivalents
uniform sampler2DShadow s_texture_ ## NUM;
Texture2DArray s_texture_ ## NUM : register(t ## NUM); SAMPLER(NUM)
Arguments
- value NUM - A number of the texture slot.
INIT_TEXTURE_ARRAY_SHADOW (value NUM)
Initialize a sampler with the array of 2D shadow textures.Equivalents
uniform sampler2DArrayShadow s_texture_ ## NUM;
Texture2DArray s_texture_ ## NUM : register(t ## NUM); SAMPLER(NUM)
Arguments
- value NUM - A number of the texture slot.
INIT_TEXTURE_CUBE_SHADOW (value NUM)
Initialize a sampler with the shadow cubemap textures.Equivalents
uniform samplerCubeShadow s_texture_ ## NUM;
TextureCube s_texture_ ## NUM : register(t ## NUM); SAMPLER(NUM)
Arguments
- value NUM - A number of the texture slot.
INIT_RW_TEXTURE (value NUM)
Initialize a 2D RWTexture. Works only with enabled USE_ARB_SHADER_IMAGE_LOAD_STORE and USE_RW_TEXTURES for OpenGL and USE_RW_TEXTURES only for Direct3DEquivalents
coherent uniform layout(rgba8) image2D s_rw_texture_ ## NUM;
RWTexture2D<uint> s_rw_texture_ ## NUM : register(u ## NUM);
Arguments
- value NUM - A number of the texture slot.
Texture Functions
TEXTURE (value NUM, value COORD)
Samples a texture.Equivalents
texture(s_texture_ ## NUM,COORD)
s_texture_ ## NUM.Sample(s_sampler_ ## NUM ,COORD)
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
TEXTURE_BIAS (value NUM, value COORD, value BIAS)
Samples a texture using a mipmap-level offset (performs a texture lookup with explicit level-of-detail).Equivalents
textureLod(s_texture_ ## NUM,COORD,BIAS)
s_texture_ ## NUM.SampleLevel(s_sampler_ ## NUM,COORD,BIAS)
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
- value BIAS - Mipmap level.
TEXTURE_OFFSET_BIAS (value NUM, value COORD, value OFFSET, value BIAS)
Samples a texture using a mipmap-level offset.Equivalents
textureOffset(s_texture_ ## NUM,COORD,OFFSET,BIAS)
s_texture_ ## NUM.SampleLevel(s_sampler_ ## NUM,COORD,BIAS,OFFSET)
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
- value OFFSET - Offset.
- value BIAS - Mipmap level.
TEXTURE_OFFSET (value NUM, value COORD, value OFFSET)
Samples a texture using a offset on mipmap level 0 only (performs a texture lookup with offset).Equivalents
textureOffset(s_texture_ ## NUM,COORD,OFFSET,0)
s_texture_ ## NUM.SampleLevel(s_sampler_ ## NUM,COORD,0,OFFSET)
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
- value OFFSET - Offset.
TEXTURE_GRAD (value NUM, value COORD, value DDX, value DDY)
Samples a texture using a gradient to influence the way the sample location is calculated.Equivalents
textureGrad(s_texture_ ## NUM,COORD,DDX,DDY)
s_texture_ ## NUM.SampleGrad(s_sampler_ ## NUM,COORD,DDX,DDY)
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
- value DDX - The rate of change of the surface geometry in the x direction.
- value DDY - The rate of change of the surface geometry in the y direction.
TEXTURE_LOAD_LOD (value NUM, value COORD, value LOD)
Reads texel data without any filtering or sampling.Equivalents
texelFetch(s_texture_ ## NUM,COORD,LOD)
s_texture_ ## NUM.Load(uint3(COORD,LOD))
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
- value LOD - Level-of-detail within the texture from which the texel will be fetched.
TEXTURE_LOAD (value NUM, value COORD)
Reads texel data without any filtering or sampling with zero offset.Equivalents
texelFetch(s_texture_ ## NUM,COORD,0)
s_texture_ ## NUM.Load(uint3(COORD,0))
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
TEXTURE_SHADOW (value NUM, value COORD)
Samples a shadow texture.Equivalents
texture(s_texture_ ## NUM,COORD)
s_texture_ ## NUM.SampleCmpLevelZero(s_sampler_compare_ ## NUM,COORD.xy,COORD.z)
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
TEXTURE_SHADOW_PROJ (value NUM, value COORD)
Samples a shadow texture with projection.Equivalents
textureProj(s_texture_ ## NUM,COORD)
s_texture_ ## NUM.SampleCmpLevelZero(s_sampler_compare_ ## NUM,COORD.xy,COORD.z)
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
TEXTURE_CUBE_SHADOW (value NUM, value DIRECTION, value DEPTH)
Samples a cubemap shadow texture.Equivalents
texture(s_texture_ ## NUM,float4(DIRECTION,DEPTH)))
s_texture_ ## NUM.SampleCmpLevelZero(s_sampler_compare_ ## NUM,DIRECTION,DEPTH)
Arguments
- value NUM - A number of the texture slot.
- value DIRECTION - Direction value.
- value DEPTH - Depth value.
TEXTURE_2D_CUBIC (value NUM, value COORD, value TEXSIZE)
Samples a texture with bicubic interpolation.Equivalents
texture2DCubic(s_texture_ ## NUM,COORD,TEXSIZE)
texture2DCubic(s_texture_ ## NUM,s_sampler_ ## NUM,COORD,TEXSIZE)
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
- value TEXSIZE - Texture size.
TEXTURE_BIAS_ZERO (value NUM, value COORDS)
Samples a texture using a mipmap-level offset on mipmap level 0 only.Equivalents
textureLod(s_texture_ ## NUM,COORDS,0.0f)
s_texture_ ## NUM.SampleLevel(s_sampler_ ## NUM,COORDS,0.0f)
Arguments
- value NUM - A number of the texture slot.
- value COORDS - UV coordinates.
TEXTURE_RW_LOAD (value NUM, value COORD)
Loads RW texture. Works only with enabled USE_ARB_SHADER_IMAGE_LOAD_STORE and USE_RW_TEXTURES for OpenGL and USE_RW_TEXTURES only for Direct3DEquivalents
imageLoad(s_rw_texture_ ## NUM,COORD)
unpack_uint32_to_rgba8(s_rw_texture_ ## NUM[uint2(COORD)])
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
TEXTURE_RW_STORE (value NUM, value COORD, value VALUE)
Stores RW texture. Works only with enabled USE_ARB_SHADER_IMAGE_LOAD_STORE and USE_RW_TEXTURES for OpenGL and USE_RW_TEXTURES only for Direct3DEquivalents
imageStore(s_rw_texture_ ## NUM,COORD,VALUE)
s_rw_texture_ ## NUM[uint2(COORD)] = pack_rgba8_to_uint32(VALUE)
Arguments
- value NUM - A number of the texture slot.
- value COORD - UV coordinates.
- value VALUE - A value to store.
Passing Textures to Functions
UUSL allows you to pass textures to functions as arguments.
Here is a usage example:
float4 func_name(float4 color,float2 uv,TEXTURE_IN_2(texture_0,texture_1)) {
return TEXTURE(texture_0,uv) * TEXTURE_BIAS(texture_1,uv,5.0f) * color;
}
float4 new_color = func_name(color,uv,TEXTURE_OUT_2(TEX_COLOR_0,TEX_COLOR_1));
TEXTURE_OUT (value NUM)
Allows to pass a texture to function.Equivalents
s_texture_ ## NUM
s_texture_ ## NUM,s_sampler_ ## NUM
Arguments
- value NUM - A number of the texture slot.
TEXTURE_OUT_2 (value NUM0, value NUM1)
Allows to pass two textures to a function.Equivalents
TEXTURE_OUT(NUM_0),TEXTURE_OUT(NUM_1)
TEXTURE_OUT(NUM_0),TEXTURE_OUT(NUM_1)
Arguments
- value NUM0 - A number of the texture slot (for the first texture).
- value NUM1 - A number of the texture slot (for the second texture).
TEXTURE_OUT_3 (value NUM0, value NUM1, value NUM2)
Allows to pass three textures to a function.Equivalents
TEXTURE_OUT_2(NUM_0,NUM_1),TEXTURE_OUT(NUM_2)
TEXTURE_OUT_2(NUM_0,NUM_1),TEXTURE_OUT(NUM_2)
Arguments
- value NUM0 - A number of the texture slot (for the first texture).
- value NUM1 - A number of the texture slot (for the second texture).
- value NUM2 - A number of the texture slot (for the third texture).
TEXTURE_OUT_4 (value NUM0, value NUM1, value NUM2, value NUM3)
Allows to pass four textures to a function.Equivalents
TEXTURE_OUT_3(NUM_0,NUM_1,NUM_2),TEXTURE_OUT(NUM_3)
TEXTURE_OUT_3(NUM_0,NUM_1,NUM_2),TEXTURE_OUT(NUM_3)
Arguments
- value NUM0 - A number of the texture slot (for the first texture).
- value NUM1 - A number of the texture slot (for the second texture).
- value NUM2 - A number of the texture slot (for the third texture).
- value NUM3 - A number of the texture slot (for the fourth texture).
TEXTURE_IN (value NAME)
Specifies the 2D texture for passing to function.Equivalents
sampler2D s_texture_ ## NAME
Texture2D s_texture_ ## NAME,SamplerState s_sampler_ ## NAME
Arguments
- value NAME - A name of the texture.
TEXTURE_IN_2 (value NAME0, value NAME1)
Specifies two 2D textures for passing to function.Equivalents
TEXTURE_IN(NAME_0),TEXTURE_IN(NAME_1)
TEXTURE_IN(NAME_0),TEXTURE_IN(NAME_1)
Arguments
- value NAME0 - A name of the first texture.
- value NAME1 - A name of the second texture.
TEXTURE_IN_3 (value NAME0, value NAME1, value NAME2)
Specifies three 2D textures for passing to function.Equivalents
TEXTURE_IN_2(NAME_0,NAME_1),TEXTURE_IN(NAME_2)
TEXTURE_IN_2(NAME_0,NAME_1),TEXTURE_IN(NAME_2)
Arguments
- value NAME0 - A name of the first texture.
- value NAME1 - A name of the second texture.
- value NAME2 - A name of the third texture.
TEXTURE_IN_4 (value NAME0, value NAME1, value NAME2, value NAME3)
Specifies four 2D textures for passing to function.Equivalents
TEXTURE_IN_3(NAME_0,NAME_1,NAME_2),TEXTURE_IN(NAME_3)
TEXTURE_IN_3(NAME_0,NAME_1,NAME_2),TEXTURE_IN(NAME_3)
Arguments
- value NAME0 - A name of the first texture.
- value NAME1 - A name of the second texture.
- value NAME2 - A name of the third texture.
- value NAME3 - A name of the fourth texture.
TEXTURE_IN_CUBE (value NAME)
Specifies the cube texture for passing to function.Equivalents
samplerCube s_texture_ ## NAME
TextureCube s_texture_ ## NAME,SamplerState s_sampler_ ## NAME
Arguments
- value NAME - A name of the texture.
TEXTURE_IN_3D (value NAME)
Specifies the 3D texture for passing to function.Equivalents
sampler3D s_texture_ ## NAME
Texture3D s_texture_ ## NAME,SamplerState s_sampler_ ## NAME
Arguments
- value NAME - A name of the texture.
TEXTURE_IN_ARRAY (value NAME)
Specifies the texture array for passing to function.Equivalents
sampler2DArray s_texture_ ## NAME
Texture2DArray s_texture_ ## NAME,SamplerState s_sampler_ ## NAME
Arguments
- value NAME - A name of the texture.
TEXTURE_IN_SHADOW (value NAME)
Specifies the texture shadow for passing to function.Equivalents
sampler2DShadow s_texture_ ## NAME
Texture2D s_texture_ ## NAME,SamplerState s_sampler_ ## NAME
Arguments
- value NAME - A name of the texture.
TEXTURE_IN_ARRAY_SHADOW (value NAME)
Specifies the texture shadow array for passing to function.Equivalents
sampler2DArrayShadow s_texture_ ## NAME
Texture2DArray s_texture_ ## NAME,SamplerState s_sampler_ ## NAME
Arguments
- value NAME - A name of the texture.
TEXTURE_IN_CUBE_SHADOW (value NAME)
Specifies the texture shadow cube for passing to function.Equivalents
samplerCubeShadow s_texture_ ## NAME
TextureCube s_texture_ ## NAME,SamplerState s_sampler_ ## NAME
Arguments
- value NAME - A name of the texture.