General

Additional type qualifiers

Built-in varying variables

Built-in uniform variables

Built-in constants

Built-in types

Built-in functions

Unsupported GLSL features

Notes

 

Search Keywords: GLSL, vrayGLSL, shading language

General

The V-Ray extensions for GLSL (vrayGLSL for short) add several functions and variables that can be used in GLSL shaders with the VRayGLSLTex texture and the VRayGLSLMtl material. See the Examples section for some samples.

Additional type qualifiers

__channel

This type qualifier can be used only on global variables to mark them as render channels. Render channels would be automatically created and attached in the V-Ray VFB. There are several supported channel types: bool, int, float, vec2, vec3. Writing to the render channel is done by assigning a value to the channel variable. Should there be more than a single write event to a channel variable the last written value is considered only. Set a default channel value by using an initializer.

__channel vec2 UVSpace = vec2(0.314);

void main() {

UVSpace = vec2(gl_TexCoord[0]);

gl_FragColor = vec4(0.3, 0.8, 0.2, 1.0);

}

 

__persistent

Use this type qualifier to make render channels persistent. Persistent render channels would keep their value and write it to the V-Ray VFB even if the shader execution has reached a discard statement.

__persistent __channel vec2 UVSpace = vec2(0.314);

void main() {

UVSpace = vec2(gl_TexCoord[0]);

discard;

gl_FragColor = vec4(0.3, 0.8, 0.2, 1.0);

}

 

NOTE: These features are available only for V-Ray Standalone and V-Ray for Maya.

 

Built-in varying variables

varying vec3 vr_Position;

Intersection surface point in camera space coordinates.

 

varying vec3 vr_Normal;

Smooth surface normal unit vector at the surface intersection point in camera space.

 

varying vec3 vr_TexTangent;

Smooth surface tangent unit vector pointing along the texture u direction in camera space.

 

varying vec3 vr_TexBinormal;

Smooth surface binormal unit vector pointing along the texture v direction in camera space.

 

varying vec3 vr_Direction;

Unit vector pointing along the ray in camera space.

 

varying vec3 vr_Velocity;

Velocity of the surface point in camera space for one 3ds Max tick.

 

varying int vr_TotalRayDepth;

The total depth of the ray being traced. It is the sum of all reflection, refraction and global illumination levels traced along the ray path so far.

 

varying int vr_DiffuseRayDepth;

Diffuse depth of the ray being traced. Only the number of global illumination levels traced along the ray path so far are considered.

 

varying int vr_NumSuperSamples;

Number of samples that the parent hit point along the ray path is taking.

 

varying int vr_SuperSampleIndex;

Index of the current supersample for this intersection point.

 

varying int vr_RayFlags;

Ray flags that describe the type of the current ray. See Built-in constants for a list of supported flags.

 

varying vec3 vr_Origin;

Currently traced ray's origin in camera space coordinates.

 

varying vec3 vr_GeomNormal;

The true geometric normal unit vector for the hit primitive in camera space coordinates.

 

varying vec3 vr_TextureDu[];

The array of surface derivatives with respect to the u direction of the specified texture space. All derivatives are unit vectors in camera space.

 

varying vec3 vr_TextureDv[];

The array of surface derivatives with respect to the v direction of the specified texture space. All derivatives are unit vectors in camera space.

Built-in uniform variables

uniform int vr_NumLights;

Number of lights in the scene.

 

uniform mat4 vr_CameraToWorld;

Camera-to-world space transformation matrix.

 

uniform mat4 vr_WorldToCamera;

World-to-camera space transformation matrix.

 

uniform mat4 vr_CameraToObject;

Camera-to-object space transformation matrix.

 

uniform mat4 vr_ObjectToCamera;

Object-to-camera space transformation matrix.

 

uniform vr_FrameDataSettings vr_FrameData;

Built-in instance of the vr_FrameDataSettings structure which holds important per frame parameters. See Built-in types for all available members of the vr_FrameDataSettings type. It is best to hold a local copy of this instance instead of always referring to the original vr_FrameData.

 

NOTE: When using the transformation matrices, always place the vector to be transformed on the right-hand side.

 

NOTE: Always wrap the vec3 vector to be transformed in vec4 variables where the w-coordinate is 0.0 for direction vec3 vectors and 1.0 for coordinate vec3 points.

 

NOTE: vrayGLSL allows user-defined uniform variables to be initialized at global scope. This is a very handy technique to give your shaders default input values.

Built-in constants

const int VR_TRACE_REFRACT;

Tells the vr_trace() function to trace refractions. Must be set in the rayType member of the vr_TraceOptions. See Built-in types for the definition of vr_TraceOptions.

 

const int VR_TRACE_REFLECT;

Tells the vr_trace() function to trace reflections. Must be set in the rayType member of the vr_TraceOptions. See Built-in types for the definition of vr_TraceOptions.

 

const int VR_TRACE_TRANSPARENT;

Tells the vr_trace() function to trace further along the ray as if no surface had been hit. Must be set in the rayType member of the vr_TraceOptions. See Built-in types for the definition of vr_TraceOptions.

 

const int VR_TRACE_ENVIRONMENT;

Tells the vr_trace() function to sample the scene environment using the current ray direction regardless of what is in the ray's path. Must be set in the rayType member of the vr_TraceOptions. See Built-in types for the definition of vr_TraceOptions.

 

const int VR_REFLECT_FLAG;

Flag used to mark rays that have gone through at least one reflection.

 

const int VR_REFRACT_FLAG;

Flag used to mark rays that have gone through at least one refraction.

 

const int VR_SHADOW_FLAG;

Flag used to mark shadow rays.

 

const int VR_INDIRECT_FLAG;

Flag used to mark gathering global illumination rays.

 

const int VR_GLOSSY_FLAG;

Flag used to mark rays that have gone through glossy reflection or refraction.

 

const int VR_ENVIRONMENT_FLAG;

Flag used to mark rays shaded with environment contribution taken into account.

 

const int VR_SPECULAR_DISPERSAL;

Flag used to notify the vr_trace() function that we are tracing a single specular ray. Must be set in the rayGroup member of the vr_TraceOptions. See Built-in types for the definition of vr_TraceOptions.

 

const int VR_GLOSSY_DISPERSAL;

Flag used to notify the vr_trace() function that we are tracing multiple glossy rays. Must be set in the rayGroup member of the vr_TraceOptions. See Built-in types for the definition of vr_TraceOptions.

 

const int VR_DIFFUSE_DISPERSAL;

This constant is reserved for future use.

Built-in types

The vr_LightIterator structure holds important shade data for a particular light source in the scene. To obtain this information call vr_evalLight with an instance of vr_LightIterator as last argument. All members will be set with the required data for the selected light.

 

struct vr_LightIterator {

vec3 direction; // unit vector pointing towards the light source in camera coordinates.

float dot_nl; // dot product of the surface normal (possibly bumped) and the light direction.

vec3 contribution; // the direct light contribution from this light including atmospheric effects and distance falloff.

};

 

NOTE: If the light has multiple sample points (i.e. it is an area light), then direction contains an avarage vector pointing towards the cloud of sample points on the light.

 

The vr_FrameDataSettings structure packs together uniform variables that hold scene settings for the current frame. The information is easily accessible via the built-in uniform variable vr_FrameData - instance of vr_FrameDataSettings.

 

struct vr_FrameDataSettings {

int frameIndex; // Current frame index.

float frameTime; // Time of the current frame in ticks.

float frameStart; // Start of the frame in time. Usually frameStart <= frameTime <= frameEnd.

float frameEnd; // End of the frame in time. Usually frameStart <= frameTime <= frameEnd.

bool isOrthographic; // True if camera rays are parallel.

float focalLength; // The camera's focal length.

float aperture; // The camera's aperture.

float aspectRatio; // The pixel aspect ratio.

vec2 imagePlaneOffset; // The camera plane offset.

float dofRadius; // The depth of field radius.

float dofFocus; // The distance from the camera position to the focal plane.

ivec2 imageResolution; // Width and height of the entire output image in pixels ignoring the render region.

ivec4 regionMargin; // Render region margin in pixels.

};

 

The vr_Color structure represents a rgba color. It is meant to replace the vec4 type in uniform variable declarations where vec4 is used as color.

 

struct vr_Color {

float r; // Red color component.

float g; // Green color component.

float b; // Blue color component.

float a; // Alpha color component.

};

 

NOTE: This structure is only to be used for uniform variable declarations and uniform initializers at global scope. It is used instead of a vec4 when declaring a shader input parameter that is a rgba color.

uniform vr_Color diffuse_color = vr_Color(0.9, 0.2, 0.8, 1.0);

That way the UI will display a convenient color swatch instead of the controls displayed for a vec4 input parameter.

 

The vr_DiffuseBRDFSettings structure holds parameters of a diffuse BRDF. It configures the diffuse BRDF to be attached via the vr_brdf_diffuse built-in function. See Built-in functions for more information.

 

struct vr_DiffuseBRDFSettings {

vec3 color; // The diffuse color for the BRDF. Default value is vec3(1.0, 1.0, 1.0).

vec3 normal; // The normal for which to calculate the BRDF. Default value is vec3(1.0, 1.0, 1.0).

float roughness; // Diffuse deviation in the [0.0, 1.0] range. Default value is 0.0.

vec3 transparency; // The transparency of the BRDF. Default value is vec3(0.0, 0.0, 0.0).

};

 

The vr_GlossyBRDFSettings structure holds parameters of a class of glossy BRDFs, namely the Phong, Blinn and Ward BRDFs. It configures the BRDF to be attached via the vr_brdf_phong, vr_brdf_blinn or vr_brdf_ward built-in functions. See Built-in functions for more information.

 

struct vr_GlossyBRDFSettings {

vec3 color; // The specular(reflection) color for the BRDF. Default value is vec3(1.0, 1.0, 1.0).

vec3 normal; // The normal for which to calculate the BRDF. Default value is vec3(1.0, 1.0, 1.0).

float highlightGlossiness; // Highlight glossiness of the BRDF in the [0.0, 1.0] range. Default value is 0.7.

float reflectionGlossiness; // Reflection glossiness of the BRDF in the [0.0, 1.0] range. Default value is 0.7.

int subdivs; // Amount of samples for the glossy reflection. Default value is 8.

float anisotropy; // Anisotropy of the BRDF in the (-1.0, 1.0) range. Default value is 0.0.

bool traceReflections; // Set if the BRDF should trace reflections. Default value is true.

vec3 transparency; // The transparency of the BRDF. Default value is vec3(0.0, 0.0, 0.0).

float softenEdge; // Amount of softening the transition from dark to bright areas in specular reflections in the [-1.0, 1.0] range. Default value is 0.001.

float anisoRotation; // Rotation of the BRDF anisotropy in degrees. Default value is 0.0.

};

 

The vr_MirrorBRDFSettings structure holds parameters of a mirror BRDF. It configures the mirror BRDF to be attached via the vr_brdf_mirror built-in function. See Built-in functions for more information.

 

struct vr_MirrorBRDFSettings {

vec3 color; // Filter color of the BRDF. Default value is vec3(1.0, 1.0, 1.0).

vec3 normal; // The normal for which to calculate the BRDF. Default value is vec3(1.0, 1.0, 1.0).

vec3 transparency; // The transparency of the BRDF. Default value is vec3(0.0, 0.0, 0.0).

};

 

The vr_GlassBRDFSettings structure holds parameters of a glass BRDF. It configures the glass BRDF to be attached via the vr_brdf_glass built-in function. See Built-in functions for more information.

 

struct vr_GlassBRDFSettings {

vec3 color; // Filter color of the BRDF. Default value is vec3(1.0, 1.0, 1.0).

vec3 normal; // The normal for which to calculate the BRDF. Default value is vec3(1.0, 1.0, 1.0).

float glossiness; // Glossiness of the BRDF in the [0.0, 1.0] range. Default value is 0.7.

float ior; // Surface index of refraction for the BRDF. Default value is 1.5.

int subdivs; // Amount of samples for the glossy reflections/refraction. Default value is 8.

vec3 transparency; // The transparency of the BRDF. Default value is vec3(0.0, 0.0, 0.0).

};

 

The vr_LightBRDFSettings structure holds parameters of a V-Ray light BRDF. It configures the light BRDF to be attached via the vr_brdf_light built-in function. See Built-in functions for more information.

 

struct vr_LightBRDFSettings {

vec3 color; // Light color of the BRDF. Default value is vec3(1.0, 1.0, 1.0).

vec3 transparency; // The transparency of the BRDF. Default value is vec3(0.0, 0.0, 0.0).

bool doubleSided; // Makes the BRDF double sided. Default value is false.

bool emitOnBackSide; // Makes the BRDF emit light from its back side as well. If this is off, the material is rendered as black on the back sides. Default value is true.

};

 

The vr_TraceOptions structure holds all settings required for a ray trace. To carry out a successful trace a well configured instance must be passed as a parameter to the vr_trace function. See the vr_trace built-in function for more information.

 

struct vr_TraceOptions {

int rayType; // One of the built-in constants VR_TRACE_REFLECT, VR_TRACE_REFRACT, VR_TRACE_TRANSPARENT or VR_TRACE_ENVIRONMENT. See Built-in constants. Default values is VR_TRACE_REFLECT.

int rayGroup; // One of the VR_SPECULAR_DISPERSAL or VR_GLOSSY_DISPERSAL built-in constants. See Built-in constants. Default value is VR_SPECULAR_DISPERSAL.

float ior; // Surface index of refraction at the current hit point. Ignored unless tracing a refracted ray. Default value is 1.44.

vec3 ray; // User specified ray to be traced. Must be a unit vector in camera space. Default value is vec3(0.0, 0.0, 0.0).

vec3 normal; // The surface normal (possibly bumped) according to which the reflections/refraction will be calculated. Must be a unit vector in camera space. Default value is vec3(0.0, 0.0, 0.0).

};

 

The vr_OcclusionOptions structure holds settings for the vr_occlusion built-in function. It could be used to fine tune the occlusion sampling process. See Built-in functions for more information.

 

struct vr_OcclusionOptions {

float radius; // This parameters determines the amount of area (in units) where the ambient occlusion is calculated. Default value is 10.0.

float distribution; // This parameter will force the sampling rays to gather closer to the surface normal. For evenly distributed samples use 0.0. For maximum surface normal concentrated sampling use 1.0. Allowed value range is [0.0, 1.0]. Default value is 0.0.

float falloff; // This parameter controls the speed of the transition between occluded and unoccluded areas. Default value is 0.0.

int subdivs; // This parameter holds the number of samples that V-Ray takes to calculate the occlusion effect. Lower values render faster but produce a more noisy result. Default value is 8.

};

 

NOTE: When creating an instance of type vr_DiffuseBRDFSettings, vr_GlossyBRDFSettings, vr_MirrorBRDFSettings, vr_GlassBRDFSettings, vr_LightBRDFSettings, vr_TraceOptions or vr_OcclusionOptions all members are initialized to the given values above.

 

Built-in functions

void vr_evalLight(int lightIndex, vec3 position, vec3 normal, out vr_LightIterator light);

Evaluates direct light contribution from an indexed light source. Use this function to evaluate direct light illumination of your shader.

 

lightIndex - index of the light source in the scene; must be in the range [0, vr_NumLights).

position - surface hit point coordinates in camera space.

normal - surface normal in camera space; expected to be a unit vector.

lightIterator - holds the result of the light sampling; see vr_LightIterator.

 

vec4 vr_trace(int traceOptions, vec3 normal [, float materialIOR = 1.44]);

Traces the next ray depending on the traceOptions set following the light path. Use this function to trace different rays originating from the current surface hit point and get their color result. These overloads of the vr_trace function are depricated. Use the new vr_trace(vr_TraceOptions) overload instead.

 

traceOptions - one of the VR_TRACE_REFLECT, VR_TRACE_REFRACT, VR_TRACE_TRANSPARENT, VR_TRACE_ENVIRONMENT constants.

normal - surface normal (possibly bumped) unit vector in camera space.

materialIOR - surface index of refraction for the current hit point. Optional parameter.

return value - color as a result of tracing the specified ray type from the current surface hit point.

 

NOTE: When the maximum ray depth limit is hit, the vr_trace function returns black.

 

NOTE: If the reflected by the bumped normal ray is beneath the hit surface it is replaced by a another one calculated using the geometric (not smoothed) normal. This may bring up artifacts (e.g. in your glossy reflection effects), so weight samples strictly, or limit the normal bumping factor.

 

NOTE: materialIOR is taken into account only if VM_TRACE_REFRACT is specified. If vr_trace is called with the VR_TRACE_REFRACT and materialIOR is not specified it will use material IOR of 1.44.

 

vec4 vr_trace(vr_TraceOptions traceOptions);

Traces the next ray depending on the traceOptions parameter following the light path. Use this function to trace different rays originating from the current surface hit point and get their color result. This function has two modes. If the traceOptions.ray member is set to a non null vector it will be traced. Else V-Ray calculates the ray to be traced depending on traceOptions.normal. In both cases traceOptions.rayType and traceOptions.rayGroup must be set.

 

traceOptions - properly set instance of vr_TraceOptions according to the above description. See Built-in types for more information.

return value - color as a result of tracing the specified ray type from the current surface hit point.

 

NOTE: When the maximum ray depth limit is hit, the vr_trace function returns black.

 

NOTE: If the reflected by the bumped normal ray is beneath the hit surface it is replaced by a another one calculated using the geometric (not smoothed) normal. This may bring up artifacts (e.g. in your glossy reflection effects), so weight samples strictly, or limit the normal bumping factor.

 

NOTE: traceOptions.ior is taken into account only if traceOptions.rayType is specified as VR_TRACE_REFRACT. If we are tracing a refraction and no ior value is set V-Ray will use the default.

 

vec3 vr_irradiance(vec3 normal, float importance);

Use this function to acquire global illumination at the intersection point.

 

normal - surface normal (possibly bumped) unit vector in camera space.

importance - used to control the precision of the global illumination calculation. Should be in the range [0,1].

return value - rgb color as a result of the GI evaluation. No normalization factors are required.

 

vec3 vr_randomSample(int sampleIndex, int totalSamples)

Retrieves a vector with random component values in the [0,1) range suitable for DMC sampling.

 

sampleIndex - index of current sample. Must be in the range [0,totalSamples).

totalSamples - the total number of samples we would take from this hit point.

return value - a vec3 vector with random component values in the [0,1) range suitable for DMC sampling.

 

void vr_brdf_diffuse(vr_DiffuseBRDFSettings brdfSettings);

Use this function to assign a diffuse BRDF to the shader that will be automatically used for light evaluation (both direct and indirect). The new BRDF will be layered above other previously attached BRDFs but all result will be summed equally. No light loops or vr_irradiance calls are required. A write to gl_FragColor is necessary only to determine the alpha component of the result. The rgb components of gl_FragColor will be ignored.

 

brdfSettings - holds the diffuse BRDF settings. See Built-in types for more information.

 

void vr_brdf_phong(vr_GlossyBRDFSettings brdfSettings);

Use this function to assign a Phong BRDF to the shader that will be automatically used for light evaluation (both direct and indirect). The new BRDF will be layered above other previously attached BRDFs but all result will be summed equally. No light loops or vr_irradiance calls are required. A write to gl_FragColor is necessary only to determine the alpha component of the result. The rgb components of gl_FragColor will be ignored.

 

brdfSettings - holds the Phong BRDF settings. All anisotropy members are ignored. See Built-in types for more information.

 

void vr_brdf_blinn(vr_GlossyBRDFSettings brdfSettings);

Use this function to assign a Blinn BRDF to the shader that will be automatically used for light evaluation (both direct and indirect). The new BRDF will be layered above other previously attached BRDFs but all result will be summed equally. No light loops or vr_irradiance calls are required. A write to gl_FragColor is necessary only to determine the alpha component of the result. The rgb components of gl_FragColor will be ignored.

 

brdfSettings - holds the Blinn BRDF settings. See Built-in types for more information.

 

void vr_brdf_ward(vr_GlossyBRDFSettings brdfSettings);

Use this function to assign a Ward BRDF to the shader that will be automatically used for light evaluation (both direct and indirect). The new BRDF will be layered above other previously attached BRDFs but all result will be summed equally. No light loops or vr_irradiance calls are required. A write to gl_FragColor is necessary only to determine the alpha component of the result. The rgb components of gl_FragColor will be ignored.

 

brdfSettings - holds the Ward BRDF settings. See Built-in types for more information.

 

void vr_brdf_glass(vr_GlassBRDFSettings brdfSettings);

Use this function to assign a Glass BRDF to the shader that will be automatically used for light evaluation (both direct and indirect). The new BRDF will be layered above other previously attached BRDFs but all result will be summed equally. No light loops or vr_irradiance calls are required. A write to gl_FragColor is necessary only to determine the alpha component of the result. The rgb components of gl_FragColor will be ignored.

 

brdfSettings - holds the Glass BRDF settings. See Built-in types for more information.

 

void vr_brdf_mirror(vr_MirrorBRDFSettings brdfSettings);

Use this function to assign a Mirror BRDF to the shader that will be automatically used for light evaluation (both direct and indirect). The new BRDF will be layered above other previously attached BRDFs but all result will be summed equally. No light loops or vr_irradiance calls are required. A write to gl_FragColor is necessary only to determine the alpha component of the result. The rgb components of gl_FragColor will be ignored.

 

brdfSettings - holds the Mirror BRDF settings. See Built-in types for more information.

 

void vr_brdf_light(vr_LightBRDFSettings brdfSettings);

Use this function to assign a Light BRDF to the shader that will be automatically used for light evaluation (both direct and indirect). The new BRDF will be layered above other previously attached BRDFs but all result will be summed equally. No light loops or vr_irradiance calls are required. A write to gl_FragColor is necessary only to determine the alpha component of the result. The rgb components of gl_FragColor will be ignored.

 

brdfSettings - holds the Light BRDF settings. See Built-in types for more information.

 

vec3 vr_textureSize(sampler1D sampler);

Use this function to get the size of the attached texture in pixels.

 

sampler - sampler to which the texture is attached.

return value - vec3 holding the width of the attached texure to the sampler in its x component or 0.0 if there is no texture attached. All other components will be 0.0.

 

vec3 vr_textureSize(sampler2D sampler);

Use this function to get the size of the attached texture in pixels.

 

sampler - sampler to which the texture is attached.

return value - vec3 holding the width and height of the attached texure to the sampler in its x and y components or 0.0 if there is no texture attached. The z component will be 0.0.

 

float vr_occlusion(vr_OcclusionOptions options);

Use this function to evaluate the amount of ambient occlusion at the current surface hit point.

 

options - configuration parameters for the sampling process. See Built-in types for more information.

return value - a float value in the [0.0, 1.0] range. A return value of 0.0 means no surface ambient occlusion, while 1.0 is interpreted as fully occluded surface point.

 

vec4 textureCube(samplerCube sampler, vec3 sampleDir);

Use this function to do cubemap texture sampling.

 

sampler - sampler to which the cubemap texture is attached. The texture must follow the horizontal cross configuration with +Y pointing up.

sampleDir - sampling direction. Expected to be a normalized unit vector in camera space.

return value - color corresponding to the sampling direction.

 

Unsupported GLSL features

The following GLSL features are not supported by the V-Ray GLSL implementation.

 

Built-in varying variables:

 

Built-in uniform variables:

 

Special output variables:

 

Built-in functions:

 

Notes