glProgramStringARB causing GL_INVALID_OPERATION

I am trying to write a fragment program that will take a texture and clamp the texels between two values. That is, if the min value is say 0.2 and the max value is 0.6, any texel less than 0.2 will become 0, any texel greater than 0.6 will become 1.0, and all values in between will be mapped from 0 to 1.0.

My call to glProgramStringARB is causing a GL_INVALID_OPERATION. I don’t understand why. Please help.

This is my first attempt at writing a shader so I’m not entirely sure what I’m doing. Here’s my code:

String str = 
   "!!ARBfp1.0
"+            
   "TEMP R0;
"+
   "MOV R0.x, fragment.texcoord[1];
"+
   "ADD R0.w, fragment.texcoord[2].x, -R0.x;
"+
   "TEX R0.xyz, fragment.texcoord[0], texture[0], 2D;
"+
   "RCP R0.w, R0.w;
"+
   "ADD R0.xyz, R0, -fragment.texcoord[1].x;
"+
   "MUL_SAT result.color.xyz, R0, R0.w;
"+
   "END
";

int count = str.Length;

Gl.glEnable(Gl.GL_FRAGMENT_PROGRAM_ARB);
Gl.glGenProgramsARB(1, out mFragProg);            
Gl.glBindProgramARB(Gl.GL_FRAGMENT_PROGRAM_ARB, mFragProg);
Gl.glProgramStringARB(Gl.GL_FRAGMENT_PROGRAM_ARB,  Gl.GL_PROGRAM_FORMAT_ASCII_ARB, count, str);
GetGLError("glProgramStringARB");
Gl.glDisable(Gl.GL_FRAGMENT_PROGRAM_ARB);

Then to use it I do the following:

Gl.glEnable(Gl.GL_FRAGMENT_PROGRAM_ARB);
Gl.glBindProgramARB(Gl.GL_FRAGMENT_PROGRAM_ARB, mFragProg);
float max = (mMiddle + (mRange / 2.0f))/65535.0f;
float min = (mMiddle - (mRange / 2.0f))/65535.0f;
Gl.glMultiTexCoord1f(Gl.GL_TEXTURE1_ARB, min);
Gl.glMultiTexCoord1f(Gl.GL_TEXTURE2_ARB, max);
GetGLError("Enable Program for Drawing");

/* 
 * Drawing Code
 */

Gl.glDisable(Gl.GL_FRAGMENT_PROGRAM_ARB);

I have not looked deeply into your code but what is MUL_SAT?

I’ve been coding ARBfp1.0 for a while now and I’ve never come across that command.

The doc for fragment program is here:
http://oss.sgi.com/projects/ogl-sample/registry/ARB/fragment_program.txt

Also there’s a nice way to find out where you program fails. You can actually see the line first line where the fragment program has stopped compiling by running this piece of code.

//Gets the position in the program string where program fails
GLint errorPos;
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);

//Gets the first bad string that causes failure
const GLubyte* errorString;
errorString = glGetString( GL_PROGRAM_ERROR_STRING_ARB );

Let me know if that’s helpful.

cheers

There’s nothing (syntactically) wrong with that program text (although your usage of texcoords doesn’t seem very efficient.)

Are you sure the error is thrown by glProgramStringARB? Check the error before that, too, it might have been thrown much earlier in your application.

@matchStickMan, _SAT is a modifier available to all fragment operations. Read the spec you linked.

OK, my apologies.

_SAT is a valid command. It’s just that i never used it. :stuck_out_tongue:

But do try getting the error string.