Question

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 cause a GL_INVALID_OPERATION. I can't seem to figure out why this is happening. 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\n"+            
   "TEMP R0;\n"+
   "MOV R0.x, fragment.texcoord[1];\n"+
   "ADD R0.w, fragment.texcoord[2].x, -R0.x;\n"+
   "TEX R0.xyz, fragment.texcoord[0], texture[0], 2D;\n"+
   "RCP R0.w, R0.w;\n"+
   "ADD R0.xyz, R0, -fragment.texcoord[1].x;\n"+
   "MUL_SAT result.color.xyz, R0, R0.w;\n"+
   "END\n";

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);
Was it helpful?

Solution

I haven't really programed any shaders, but maybe the shader compiler doesn't recognize the new line? have you tried putting "\n\r"?

EDIT:

Another question that you may ask yourself is what language are you using? Are strings in UNICODE, ie 16-bits/char? I just noticed that the format you are passing into the glProgramStringARB() is set to ASCII. If the string is really UNICODE, then it will cause problems.

For example, JAVA and C# Strings are in UNICODE. not ASCII.

OTHER TIPS

First: I don't know much about ARB_fragment_program so I'm partly guessing here.

Your best option would be to get the error string (glGetString(GL_PROGRAM_ERROR_STRING_ARB)) and see what that tells you.

After looking at the shader, you seem to be using the wrong number of components in lines 3 and 7 (and probably some more). For example I don't think you can assign a 4-component vector (fragment.texcoord[1]) to a scalar field (R0.x).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top