glsl texture invalid

Started by
2 comments, last by V-man 13 years, 4 months ago
Hey,
I have an advection calculation that uses a rgba8 texture representing optical flow vectors that stores the vec2 values in rgb (rg is normalized values, b is the exponent scalar), the advection calculation is accumulated in two RGBA16F textures that ping-pong to allow read/writes to accumulate the optical flow vectors.

The fragment shader simply adds the optical-flow texture to the advection texture (for reading) to the other (writing) advection texture. It then resets the advection vector to vec2( 0.0, 0.0 ) if the accumulated vector exceeds the bounds of the given texel.

The problem is that whatever texture is bound to glActiveTextureARB(GL_TEXTURE1_ARB) simply returns black (vec4( 0.0, 0.0, 0.0, 0.0))

If there are ANY suggestions about what could be going wrong PLEASE help me.



//************** C++ Code ************************************

glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, AdvectionGLTexture);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE);

glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, OpticalFlowGLTexture);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE);



PainterlyRenderingMipMapRTT[CurrentMovementRTT]->PushRenderTargets();

for( CDUInt32 i = 0; i < NumMipMaps-1; i++ )
{

AdvectionShader->SetActive( CDTRUE );

CDInt32 Tex1 = glGetUniformLocationARB(GLShaderHandle, "OpticalFlowTexture");
CDInt32 Tex0 = glGetUniformLocationARB(GLShaderHandle, "AdvectionTexture");

glUniform1iARB( Tex0, 0 );
glUniform1iARB( Tex1, 1 );

PainterlyRenderingMipMapRTT[CurrentMovementRTT]->GenerateMipmaps( 0, i, i, StrokeDataFunctor );

AdvectionShader->SetActive( CDFALSE );
glFinish();
}



//************** glsl fragment shader *************************
uniform sampler2D AdvectionTexture; // Advection
uniform sampler2D OpticalFlowTexture; // Optical flow


vec2 UnpackOpticalFlow( in vec2 OFV, in float Mag )
{
const vec2 One = vec2( 1.0, 1.0 );
float S = pow( 2.0, Mag * 32.0 )/1024.0;
return (OFV.xy * 2.0 - One) * S;
}

// SCALARVAL is 2.0^MipLevel
// INVSCALEVAL is 1.0/SCALARVAL

void main( void )
{
vec2 Scalar = vec2( SCALARVAL, SCALARVAL );
vec2 InvScale = vec2( INVSCALEVAL, INVSCALEVAL );

vec2 SuperTextureCoordinate = gl_TexCoord[0].st * Scalar;

vec2 ResetThreshold = InvScale;

const vec2 UVLow = vec2( 0.0002, 0.0002 );
const vec2 UVHi = vec2( 0.9999, 0.9999 );

vec4 PreviousPixelAdvections;
// PreviousPixelAdvections = texture2DLod( AdvectionTexture, clamp( SuperTextureCoordinate, UVLow, UVHi ), float( LODLEVEL ) );
PreviousPixelAdvections = texture2DLod( AdvectionTexture, SuperTextureCoordinate, float( LODLEVEL ) );

vec4 CurrentFlows;
// CurrentFlows = texture2DLod( OpticalFlowTexture, SuperTextureCoordinate, float( LODLEVEL ) );
CurrentFlows = texture2DLod( OpticalFlowTexture, SuperTextureCoordinate, float( LODLEVEL ) );

vec2 OpticalFlowVec = UnpackOpticalFlow( CurrentFlows.xy, CurrentFlows.z );

vec2 CurrentResult = PreviousPixelAdvections.xy;
vec2 CurrentSecondResult = PreviousPixelAdvections.zw;

if( (abs( CurrentResult.x ) >= abs( ResetThreshold.x )) || (abs( CurrentResult.y ) >= abs( ResetThreshold.y )) )
{
CurrentResult = CurrentSecondResult;
CurrentSecondResult = vec2( 0.0, 0.0 );
}
else
{
CurrentResult = CurrentResult + OpticalFlowVec;
}

CurrentSecondResult = OpticalFlowVec.xy + 0.000000001 * CurrentResult;

gl_FragData[0] = vec4( CurrentResult.x, CurrentResult.y, CurrentSecondResult.x, CurrentSecondResult.y );
}
Advertisement
My GLSL 1.20 spec says that texture2DLod are only allowed in the vertex shader. It is possible that nVidia ignores it.
Are the shader logs ok?
glGetError()?
Did you try to disable mipmaps?

Calls to glTexEnvf are useless since you are using shaders.
Calls to glEnable(GL_TEXTURE_2D) are useless.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
> My GLSL 1.20 spec says that texture2DLod are only allowed in the vertex shader. It is possible that nVidia ignores it.

The GLSL 4.0 Quick reference card says that all of the texture functions are available in the vertex, geometry and fragment shaders. I also have another 7 stages of the renderer that makes extensive use of texture2DLod to sample from and generate mipmaps.

> Are the shader logs ok?

No errors when compiling shaders on both NVidia and AMD cards.

> Did you try to disable mipmaps?

Disabling mipmaps changes nothing. Also the entire point of this program is to make use of mipmaps.

> Calls to glTexEnvf are useless since you are using shaders.
> Calls to glEnable(GL_TEXTURE_2D) are useless.


Agreed, just posted these to let everyone know that I have done my due diligence before posting for help.

I also have code that proves that the input textures are correct for previous stages of the renderer. (I.E. they render perfectly when not part of this stage) No version of texture* in the glsl will return anything other than 0 for the texture bound to the second texture stage.

GLSL 4.00?
I assumed you were using an early version since you had calls to ARB functions and the presence of gl_TexCoord[0].

Anyway, I would write a simple fs such as
#version 400uniform sampler2D AdvectionTexture; // Advectionuniform sampler2D OpticalFlowTexture; // Optical flowvoid main(){vec4 PreviousPixelAdvections;// PreviousPixelAdvections = texture2D( AdvectionTexture, clamp( SuperTextureCoordinate, UVLow, UVHi ));PreviousPixelAdvections = texture2D( AdvectionTexture, SuperTextureCoordinate);vec4 CurrentFlows;// CurrentFlows = texture2D( OpticalFlowTexture, SuperTextureCoordinate);CurrentFlows = texture2D( OpticalFlowTexture, SuperTextureCoordinate);gl_FragData[0] =  CurrentFlows * 0.0001 + PreviousPixelAdvections;}


This is to make sure the return value is not vec4( 0.0, 0.0, 0.0, 0.0)
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement