Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glsl es dFdx/dFdy analog

I'm writing crossplatform application. It should run on Android devices.
I want to use dFdx/dFdy for antialiasing. But, unfortunately, glsl es 2.0 does not support derivatives.

Can I replace dFdx/dFdy with something? I.E. 1/sprite_width, 1/sprite_height in screen pixels.

As I said, I need this to work on android devices. And I saw that my device support GL_OES_standard_derivatives, which allow it to use this functions. Does all android opengl es 2.0 devices support it?

like image 812
tower120 Avatar asked Oct 02 '22 07:10

tower120


1 Answers

As I said many Opengl ES 2.0 devices support GL_OES_standard_derivatives extension.
But for thoose who don't, I made this workaround:

float myFunc(vec2 p){
return p.x*p.x - p.y; // that's our function. We want derivative from it.
}


// this calculated in vertex shader
// width/height = triangle/quad width/height in px;
vec2 pixel_step = vec2(1/width, 1/height);       

float current = myFunc(texCoord);
float dfdx = myFunc(texCoord + pixel_step.x) - current;
float dfdy = myFunc(texCoord + pixel_step.y) - current;

float fwidth = abs(dx) + abs(dy);       // from khronos doc #http://www.khronos.org/registry/gles/extensions/OES/OES_standard_derivatives.txt

P.S. I get very close results to the glsl built ins, A little bit more blurry (in my shader). To fix this I added multiply pixel_step on 1/1.75. If someone knows why, let me know.

like image 175
tower120 Avatar answered Oct 05 '22 10:10

tower120