Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL ES 2.0 Derivative Texture

I have an OpenGL ES 2.0 based iPhone app which I am running to a bit of OpenGL ES trouble.

I am trying to complile a fragment shader which computes/displays the derivative of an input texture. My fragment shader code is:

 varying highp vec2 textureCoordinate;

 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;

 void main(void)
 {
   mediump vec4 derivData = vec4(dFdx(texture2D(inputImageTexture, textureCoordinate).xyz, 0.0);

   gl_FragColor = derivData;
 }

However, this fails the compile. If I take out the dFdX, it compiles just fine.

Does anyone have any experience with this? I'd eventually like to calculate the derivative with respect to Y as well, then merge them seeing how the input texture is an image.

I've been struggling on this for a few days now so any advice you have would be greatly appreciated!

like image 610
Brett Avatar asked Mar 12 '12 17:03

Brett


1 Answers

All iOS hardware that supports ES 2.0 will support the GL_OES_standard_derivatives extension: https://developer.apple.com/library/ios/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/OpenGLESPlatforms/OpenGLESPlatforms.html

However, you don't get it "for free". In your fragment shader, you must add the following at the top (from http://www.khronos.org/registry/gles/extensions/OES/OES_standard_derivatives.txt):

#extension GL_OES_standard_derivatives : enable

All the info in the comments to the first answer is mostly accurate, but without this piece you'll continue to get the following error:

ERROR: 0:15: Call to undeclared function 'dFdx'
ERROR: 0:16: Call to undeclared function 'dFdy'

This threw me for a loop, but once I added the enable line, it seems to work on both the device and the simulator (haven't actually evaulate that it works on both, but it does compile).

like image 199
mobob Avatar answered Nov 15 '22 11:11

mobob