View Full Version : counterpart to "float4 uv[8] :TEXCOORD0" in GLSL

02-11-2012, 12:21 AM
I am implementing the Summed-Area Table algorithm. There is a statement

float4 uv[8]:TEXCOORD0;

in the shader code originally written in HLSL in Slide 9 in the following slides
Summed-Area Table AMD (http://developer.amd.com/media/gpu_assets/GDC2005_SATEnvironmentReflections.pdf)
When complied in GLSL, it failed and said "OpenGL does not allow Cg-style semantics".

Is there any way in GLSL to achieve the same effect?

02-11-2012, 05:24 AM
Since that seems to be part of their VSoutput, I would hazard a guess that it is a vertex shader and that uv thing is a texcoordinate input for your vertex shader.

In GLSL version 1.10
you use gl_MultiTexCoord0

This is from AMD's RenderMonkey Earth example

#version 110

// Earth vertex shader computes lighting coefficients.
// Note: For optimal light animation set the RenderMonkey cycle time
// for predefined variables to 20 seconds. (Found in the Edit->Preferences).

// predefined variables used to animate light direction
uniform float cos_time_0_2PI, sin_time_0_2PI;

// artist variable to control season
uniform float season;

// varying variables passed to fragment shader
varying float Diffuse;
varying vec3 Specular;
varying vec2 TexCoord;

void main(void)
// calculate vertex position in eye coordinates
vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;

// compute the transformed normal
vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);

// compute the light vector pointing toward the sun, in model coordinates
// x,y compose the longitude and z the (seasonal) lattitude of the nadir point.
vec3 lightVec = normalize(gl_NormalMatrix * vec3(cos_time_0_2PI, season,sin_time_0_2PI));

// compute the reflection vector
vec3 reflectVec = reflect(-lightVec, tnorm);

// compute a unit vector in direction of viewing position
vec3 viewVec = normalize(vec3 (-ecPosition));

// Calculate specular light intensity, scale down and
// apply a slightly yellowish tint.
float specIntensity = pow(max(dot(reflectVec, viewVec), 0.0), 8.0);
specIntensity = 0.3 * specIntensity;
Specular = specIntensity * vec3 (1.0, 0.941, 0.898);

// Calculate a diffuse light intensity
Diffuse = dot(lightVec, tnorm);

// Pass texture coordinates fragment shader
TexCoord = vec2 (gl_MultiTexCoord0);
TexCoord.x = TexCoord.x;

// output final vertex information
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

02-11-2012, 07:55 PM
Sorry, but it is a vertex shader output (an input for the fragment shader). See the original code here:

void satHV(
float4 position: POSITION,
float2 inUV: TEXCOORD0,

out float4 oPosition: POSITION,
out float4 uv[8] : TEXCOORD0,

uniform float fPassIndex,
uniform float4x4 modelViewProjMatrix)
oPosition = mul(modelViewProjMatrix, position);
float passOffSet = pow(16.0, fPassIndex)/256;

uv[0].xy = inUV;
uv[0].wz = uv[0].xy - float2(passOffSet, 0);

for(int i = 1; i < 8; i++)
uv[i].xy = uv[0].xy - float2(2.0 * i * passOffSet, 0);
uv[i].wz = uv[0].xy - float2((2.0 * i + 1.0) * passOffSet, 0);


I rewrote it with GLSL, like the following:

#version 330

in vec3 pos;
in vec2 inUV;

smooth out vec4 uv[8]: TEXCOORD0;

uniform int fPassIndex;

void main(void){
gl_Position = vec4(pos, 1.0);

float passOffSet = pow(16.0, fPassIndex)/256;

uv[0].xy = inUV;
uv[0].wz = uv[0].xy - vec2(passOffSet, 0);

for(int i = 1; i < 8; i++)
uv[i].xy = uv[0].xy - vec2(2.0 * i * passOffSet, 0);
uv[i].wz = uv[0].xy - vec2((2.0 * i + 1.0) * passOffSet, 0);

but GLSL cannot recognize that statement.

Alfonse Reinheart
02-11-2012, 09:24 PM
That's because the statement has no meaning for OpenGL.

With the exception of separate shaders, the connection between shader outputs and inputs is done by name. If your vertex shader has a `out vec4 uv[8]`, then your fragment shader needs a corresponding `in vec4 uv[8]` to match.

Separate shader objects allows the intersection to be made via an index, but that only works when doing dynamic linking between separate programs.