PDA

View Full Version : looking up in all the texture



castellp
03-07-2006, 03:23 AM
Hello, I'm trying to develop a fragment shader, in order to get the real projected areas in pixels for every triangle. Thus, first I render the scene into a texture. Every triangle has a different colour. Then I render a point with the colour of the triangle and a fragment shader look up in all the texture, counting every texel that has the same colour of the point. So the result will be all points have changed their colour. And the colour is their area.

Well my fragment shader is:


uniform sampler2D tex;

uniform int width, height;

void getRGBA(in int number, out int r, out int g, out int b, out int a) {
int base = 256, quotient, rest, dividend = number, n = 0;

r = 0;
g = 0;
b = 0;
a = 0;

while (dividend >= base) {

quotient = dividend / base;
rest = dividend - (quotient * base);

if (n==0) r = rest;
if (n==1) g = rest;
if (n==2) b = rest;
if (n==3) a = rest;


dividend = quotient;
n++;
}

if (n==0) r = dividend;
if (n==1) g = dividend;
if (n==2) b = dividend;
if (n==3) a = dividend;
}

void main()
{
vec4 texel;
int i, n = 0, numPixels = height * width,
r, g, b, a;
float pixelw = 1.0 / float(width),
pixelh = 1.0 / float(height),
x = 0.0, y = 0.0;

for (i=0; i < numPixels; i++) {

texel = texture2D(tex, vec2(x, y));

if (all(equal(texel, gl_Color)))
n++;

if (x < (1.0 - pixelw))
{
x += pixelw;

} else {
x = 0.0;
y += pixelh;
}
}

getRGBA(n, r, g, b, a);

gl_FragColor = vec4( float(r) / 256.0, float(g) / 256.0, float(b) / 256.0, float(a) / 256.0);
}Can I go throuth all the texture?. Any idea of what's going wrong?.

Thanks in advance,
Pascual.

Relic
03-07-2006, 04:14 AM
Loops and number of executed instructions have implementation dependent limits.
Check this extension for the limits on an NVIDIA board.
http://oss.sgi.com/projects/ogl-sample/registry/NV/fragment_program2.txt
Query GetProgramivARB with
GL_MAX_PROGRAM_EXEC_INSTRUCTIONS_NV
GL_MAX_PROGRAM_CALL_DEPTH_NV
GL_MAX_PROGRAM_IF_DEPTH_NV
GL_MAX_PROGRAM_LOOP_DEPTH_NV
GL_MAX_PROGRAM_LOOP_COUNT_NV

The maximum limit for executed instructions is 65536. The program will just exit with random data if that limit is exceeded.

Depending on the size of width and height in your code you probably have exceeded both.

For example a loop construct like

for (y = 0; y < 256, y++)
for (x = 0; x < 256; x++)
do_something();is impossible inside a fragment shader today.

You need to split the work in the fragment shader into smaller chunks.

k_szczech
03-08-2006, 12:52 AM
I never user occlusion query, but I guess it would work:
Instead of rendering point render a quad covering entire screen - fragment shader would only need to check one texel - if color matches draw something (you can render to some offscreen buffer) and if color is defferent then discard pixel. Ocllusion query should return the number of pixels that were not discarded.
Further optimisation would be to predict where to render such a quad, so it wouldn't have to cover entire screen.