Poor precision in depth texture

Moderator
Posts: 1,140
Joined: 2005.07
Post: #16
If you choose something other than GL_DEPTH_COMPONENT, you will get an invalid operation error. glCopyTexSubImage will also try to grab the depth buffer if it's looking at the one you've changed to determine which buffer to use.

If it's just not just snipping the precision before copying it, then I have no clue as to why it is working like that. Quite strange.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #17
TomorrowPlusX Wrote:And, when I sample the depth texture, are r,g,b and a all the same value? Am I sampling the incorrect one?

The conversion from DEPTH_COMPONENT to RGBA depends on DEPTH_TEXTURE_MODE. See section 3.8.4 in ARB_depth_texture.

Quote:I know that I'm supposed to use shadow2D samplers for GL_DEPTH_COMPONENT ( at least according to the orange book ) but there's no Rect variant.
shadow2DRect should work, and is defined by ARB_texture_rectangle, see 2.15.4.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #18
I looked around and found these threads on opengl.org:

http://www.opengl.org/cgi-bin/ubb/ultima...013943;p=1

http://www.opengl.org/cgi-bin/ubb/ultima...1&t=001116

And I learned about depth_texture_mode there -- still no luck.

So, my depth texture creation looks like this, now:
Code:
    glGenTextures( 1, &depthTexture );
    glBindTexture( GL_TEXTURE_RECTANGLE_EXT, depthTexture );
    glTexParameteri( GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_RECTANGLE_EXT, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE );
    glTexParameteri( GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE );
    glTexImage2D( GL_TEXTURE_RECTANGLE_EXT, 0, GL_DEPTH_COMPONENT24_ARB, screenWidth, screenHeight,
                  0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL );

And using the fragment shader:
Code:
uniform sampler2DRectShadow depthmap;
uniform float nearPlane;
uniform float farPlane;

float ConvertDepth1( float d )
{
    float clipZ = ( d - 0.5 ) * 2.0;
    return -(2.0 * farPlane * nearPlane ) / ( clipZ * ( farPlane - nearPlane ) - ( farPlane + nearPlane ));
}

float ConvertDepth2( float d )
{
    float a = farPlane / ( farPlane - nearPlane );
    float b = ( farPlane * nearPlane ) / ( nearPlane - farPlane );
    return b / ( d - a );
}

float ConvertDepth3(float d)
{
    return (nearPlane*farPlane)/(farPlane-d*(farPlane-nearPlane));
}

void main( void )
{
    const float threshold = 10.0;
    const vec4 waterColor = vec4( 0.3, 0.45, 0.4, 1.0 );

    float a = ConvertDepth3( shadow2DRect( depthmap, gl_FragCoord.stp ).r );
    float b = ConvertDepth3( gl_FragCoord.z );
    
    vec4 sceneColor = texture2DRect( refraction, gl_FragCoord.st );
    vec4 color = mix( sceneColor, waterColor, smoothstep( 0.0, threshold, a - b ));
    color.w = 1.0;

    gl_FragColor = color;  
}

I still get the exact same aliasing artifacts.
Quote this message in a reply
Oldtimer
Posts: 832
Joined: 2002.09
Post: #19
This, again, might be nitpicking, but are you sure that your smoothstep call in the fragment shader is correct? From where I'm standing, a-b won't be in the [0 10] range, but rather in the [0 1] range. But I guess that if that was the problem, it would make your effect far too faint to even render anyway...
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #20
Fenris Wrote:This, again, might be nitpicking, but are you sure that your smoothstep call in the fragment shader is correct? From where I'm standing, a-b won't be in the [0 10] range, but rather in the [0 1] range. But I guess that if that was the problem, it would make your effect far too faint to even render anyway...

Actually, the "ConvertDepth" functions are converting values in the depth buffer ( non-linear 0->1 ) to linear world-space, where 10 would mean "10 world units".

As far as I can tell, the math is right. My depth texture, however, remains nastily aliased.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #21
On the off chance that somebody might be willing to help me out, here:

http://zakariya.net/shamyl/etc/Water.zip

This ought to build and run on intel ( but untested ). I can't seem to fix the problem, and maybe somebody here can see what I'm doing wrong.

When you run it, you'll see something like this:
[Image: depth_precision.png]

On the left is the converted depth value for gl_FragCoord.z, on the right is the value from the depth texture. Very clearly aliased.

Any help would be greatly appreciated!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #22
I hate to add to the problems, but it shows up as pure white on the right hand side on my MacBook Pro. I can try it on my PowerMac when I get home, but since it's also an ATI card, I doubt I'll see much improvement. I tried to change the formats a bit, such as using just GL_DEPTH_COMPONENT without specifying a size and trying different types such as GL_UNSIGNED_INT and GL_UNSIGNED_SHORT to no avail.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #23
akb825 Wrote:I hate to add to the problems, but it shows up as pure white on the right hand side on my MacBook Pro. I can try it on my PowerMac when I get home, but since it's also an ATI card, I doubt I'll see much improvement. I tried to change the formats a bit, such as using just GL_DEPTH_COMPONENT without specifying a size and trying different types such as GL_UNSIGNED_INT and GL_UNSIGNED_SHORT to no avail.

Hey, since you're on an ATI, could you try running it ( from the command line ) as such:

Code:
% ./Water water

THis will cause it to execute the water shaders, instead of the default depth_test shaders.

Could you let me know if it renders ( anything )?
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #24
It seems to work fine with just the water. Runs at about 42 FPS on my MacBook Pro.

It's kind of interesting seeing the difference between your demo and mine. I guess your method is best at simulating bodies of water like a turbulent ocean, while mine is better for something like a calm lake. When I start making bodies of water in games etc., I'll probably do something similar to yours for things such as an ocean, having less of the refraction showing through and having the diffuse lighting.
Quote this message in a reply
Oldtimer
Posts: 832
Joined: 2002.09
Post: #25
Same here, just white on the right hand side, but the water shader looks fine. Not sure if the transparency shows up, though, so I'd wager that you're getting all ones or zeroes. I tried taking a screenshot, and something magical happened:

[Image: shamyl.png]
The water just didn't print on the screenshot! Grin

Anyway, notice how there isn't a transparent teapot under the water surface...
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #26
Screenshots can get... very finicky. Sometimes you'll get nothing, sometimes you'll get a piece of the final picture. The slower the framerate is, the harder it is to grab a correct one. It can often take several tries.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #27
LOL @ the screenshot. If you want to capture it correctly, in my experience, use command-shift-3 -- as opposed to command-shift-4. FOr some reason that captures opengl better, perhaps it waits for a v-sync or something.

That said, thanks for the nice words, I still need to fix lighting in the reflection, though. It's inverted! And I can't figure out why...
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #28
BTW, I've been wondering, how do you do your movement? I took the lazy way out and just translated the water texture, and it looks fine as it is now, but if I ever want the more turbulent look and add diffuse lighting, then it definitely would be more obvious.

I couldn't figure out why your lighting is inverted, though. I just used gluLookAt for my camera, then inverted the x coordinate to achieve my lighting.
Quote this message in a reply
Moderator
Posts: 770
Joined: 2003.04
Post: #29
MacBook results:

Double click on Water:
Code:
error compiling file: "../../shaders/depth_test.vs"
Couldn't open file: "../../shaders/depth_test.vs"

Abort trap
logout
[Process completed]

./water from the Terminal:
Code:
Date/Time:      2006-06-30 05:36:49.946 -0300
OS Version:     10.4.6 (Build 8I2025)
Report Version: 4

Command: water
Path:    ./water
Parent:  bash [298]

Version: ??? (???)

PID:    299
Thread: 0

Exception:  EXC_BAD_ACCESS (0x0001)
Codes:      KERN_PROTECTION_FAILURE (0x0002) at 0x00000000

Thread 0 Crashed:
0   <<00000000>>     0x00000000 0 + 0
1   water                              0x00003d20 renderScene() + 538 (crt.c:305)
2   water                              0x00004b79 display() + 261 (crt.c:305)
3   com.apple.glut                     0x9716f99e -[GLUTView drawRect:] + 101
4   com.apple.AppKit                   0x9341892f -[NSView _drawRect:clip:] + 3228
5   com.apple.AppKit                   0x93417989 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 614
6   com.apple.AppKit                   0x93429921 _recursiveDisplayInRect2 + 149
7   com.apple.CoreFoundation           0x90834f26 CFArrayApplyFunction + 307
8   com.apple.AppKit                   0x93417b91 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 1134
9   com.apple.AppKit                   0x934169f1 -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForV​iew:topView:] + 217
10  com.apple.AppKit                   0x934160f8 -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForV​iew:topView:] + 290
11  com.apple.AppKit                   0x934158e4 -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 523
12  com.apple.AppKit                   0x93415214 -[NSView displayIfNeeded] + 439
13  com.apple.AppKit                   0x93414fb6 -[NSWindow displayIfNeeded] + 168
14  com.apple.AppKit                   0x933ba968 -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1225
15  com.apple.AppKit                   0x933ba44e -[NSWindow orderWindow:relativeTo:] + 104
16  com.apple.glut                     0x9716da6d -[GLUTWindow orderWindow:relativeTo:] + 221
17  com.apple.AppKit                   0x93411534 -[NSWindow makeKeyAndOrderFront:] + 182
18  com.apple.glut                     0x97172719 -[GLUTView handleWorkEvent:] + 996
19  com.apple.glut                     0x9718c11f processWindowWorkList + 76
20  com.apple.glut                     0x9718c187 __glutProcessWorkEvents + 37
21  com.apple.glut                     0x9717b508 -[GLUTApplication run] + 158
22  com.apple.glut                     0x9718bb86 glutMainLoop + 319
23  water                              0x00005289 main + 225 (crt.c:305)
24  water                              0x0000282e _start + 228 (crt.c:272)
25  water                              0x00002749 start + 41

Thread 1:
0   libSystem.B.dylib                  0x90049207 semaphore_timedwait_signal_trap + 7
1   ...ple.CoreServices.CarbonCore     0x90cd2c08 TSWaitOnSemaphoreCommon + 163
2   ...ickTimeComponents.component     0x98bea252 ReadSchedulerThreadEntryPoint + 4895
3   libSystem.B.dylib                  0x90024a27 _pthread_body + 84

Thread 2:
0   libSystem.B.dylib                  0x90049207 semaphore_timedwait_signal_trap + 7
1   ...ple.CoreServices.CarbonCore     0x90cd2c08 TSWaitOnSemaphoreCommon + 163
2   ...ple.CoreServices.CarbonCore     0x90cdcad7 AIOFileThread(void*) + 1067
3   libSystem.B.dylib                  0x90024a27 _pthread_body + 84

Thread 0 crashed with i386 Thread State:
eax: 0x005524b0    ebx: 0x000084c1 ecx:0x00000009 edx: 0x0042c020
edi: 0x00000008    esi: 0x00033204 ebp:0xbfffee08 esp: 0xbfffedcc
ss: 0x0000002f    efl: 0x00010282 eip:0x00000000  cs: 0x00000027
ds: 0x0000002f     es: 0x0000002f  fs:0x00000000  gs: 0x00000037

Binary Images Description:
    0x1000 -    0x31fff water     /Users/ignacio/Desktop/Water/build/Release/water
  0x27d000 -   0x2d2fff com.apple.driver.AppleIntelGMA950GLDriver 1.4.28 (4.2.8)    /System/Library/Extensions/AppleIntelGMA950GLDriver.bundle/Contents/MacOS/AppleIntelGMA950GLDriver
  0x2d9000 -   0x2f5fff GLDriver     /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLDriver.bundle/GLDriver
  0x405000 -   0x426fff GLRendererFloat     /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLRendererFloat.bundle/GLRendererFloat
  0x605000 -   0x746fff GLEngine     /System/Library/Frameworks/OpenGL.framework/Resources/GLEngine.bundle/GLEngine
...

Model: MacBook1,1, BootROM MB11.005F.B00, 2 processors, Intel Core Duo, 1.83 GHz, 1 GB
Graphics: Intel GMA 950, GMA 950, Built-In, spdisplays_integrated_vram
Memory Module: DIMM0/BANK 0, 512 MB, DDR2 SDRAM, 667 MHz
Memory Module: DIMM1/BANK 1, 512 MB, DDR2 SDRAM, 667 MHz
AirPort: spairport_wireless_card_type_airport_extreme (0x168C, 0x86), 0.1.20
Bluetooth: Version 1.7.4f16, 2 service, 0 devices, 1 incoming serial ports
Network Service: Built-in Ethernet, Ethernet, en0
Serial ATA Device: FUJITSU ----------, 55.89 GB
Parallel ATA Device: MATSHITACD-RW  CW-8221
USB Device: Built-in iSight, Micron, Up to 480 Mb/sec, 500 mA
USB Device: Apple Internal Keyboard / Trackpad, Apple Computer, Up to 12 Mb/sec, 500 mA
USB Device: IR Receiver, Apple Computer, Inc., Up to 12 Mb/sec, 500 mA
USB Device: Bluetooth HCI, Up to 12 Mb/sec, 500 mA

./Water water from the Terminal:
got the same bus error & crash log as above, with this output on the Terminal:
Code:
Got shader file prefix: water
Bus error

Rebuilt in Xcode and got the same results. Sad

FWIW, I should mention that akb825's water demo from that other thread does work fine on my MacBook, both the FBO & non-FBO versions (with the FBO version being 5-6% faster)
Quote this message in a reply
Member
Posts: 30
Joined: 2006.04
Post: #30
I have had some sucess getting the depth of field code working, well just the reading of the depth texture and creating a blur value based on distance from the camera.

The shader code is as follows (I appologise for my lazyness and hard coding so many values as opposed to passing them in)

Code:
uniform sampler2DRect depthMap;

void main()
{
    const float zFar = 1000.0;
    const float zNear = 0.1;

    float depth = texture2DRect(depthMap, gl_TexCoord[0].st).x;
    
    float a = zFar / (zFar - zNear);
    float b = zFar * zNear / (zNear - zFar);

    float dist = b / (depth - a);
    
    const float focalDist = 5.0;
    const float dofNear = 2.0;
    const float dofFar = 15.0;
    const float blurAmount = 1.0;
    
    float f;
    if(dist < focalDist)
    {
        f = (dist - focalDist) / (focalDist-dofNear);
    }
    else
    {
        f = (dist - focalDist) / (dofFar - focalDist);
    }
    float blur = clamp(abs(f), 0.0, blurAmount);
    gl_FragColor = vec4(blur, blur, blur, 1.0);
}

Here is a screenshot of the result for a simple scene.

[Image: shot1.png]

The black area would be the focal point, and white at and beyond the near and far depth values. If I had a larger scene to play with I could show it over a larger range. However as I am not rendering the depth value directly, I am possibly hiding the evidence of any banding. This is running on a Mobility Radeon 9600 which slows down quite a lot with this shader probably due to the branching. I have not yet tried it on my Geforce 6600.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Normal Mapping Precision on iOS OptimisticMonkey 6 11,042 Apr 13, 2011 11:35 PM
Last Post: OptimisticMonkey
  Poor quality Maya PLE Render. Marjock 0 2,762 Jan 2, 2006 05:02 PM
Last Post: Marjock
  Higher color precision CobraMantis 2 3,121 Aug 20, 2005 03:49 PM
Last Post: arekkusu