+#ifdef ADAPTIVE_SAMPLING
+ #extension GL_ARB_shader_image_load_store : require
+ #extension GL_NV_shader_atomic_float : require
+#endif
+
#ifdef USE_TEXTURES
#extension GL_ARB_bindless_texture : require
#endif
//! Inverse model-view-projection matrix.
uniform mat4 uUnviewMat;
+//! Model-view-projection matrix.
+uniform mat4 uViewMat;
+
//! Texture buffer of data records of bottom-level BVH nodes.
uniform isamplerBuffer uSceneNodeInfoTexture;
//! Texture buffer of minimum points of bottom-level BVH nodes.
//! Environment map texture.
uniform sampler2D uEnvironmentMapTexture;
-//! Input pre-raytracing image rendered by OpenGL.
-uniform sampler2D uOpenGlColorTexture;
-//! Input pre-raytracing depth image rendered by OpenGL.
-uniform sampler2D uOpenGlDepthTexture;
-
//! Total number of light sources.
uniform int uLightCount;
//! Intensity of global ambient light.
#ifdef USE_TEXTURES
//! Unique 64-bit handles of OpenGL textures.
- uniform sampler2D uTextureSamplers[MAX_TEX_NUMBER];
+ uniform uvec2 uTextureSamplers[MAX_TEX_NUMBER];
+#endif
+
+#ifdef ADAPTIVE_SAMPLING
+ //! OpenGL image used for accumulating rendering result.
+ volatile restrict layout(size1x32) uniform image2D uRenderImage;
+
+ //! OpenGL image storing offsets of sampled pixels blocks.
+ coherent restrict layout(size2x32) uniform iimage2D uOffsetImage;
#endif
//! Top color of gradient background.
in vec4 m2,
in vec4 m3)
{
- return vec3 (m0[0] * v.x + m1[0] * v.y + m2[0] * v.z + m3[0],
- m0[1] * v.x + m1[1] * v.y + m2[1] * v.z + m3[1],
- m0[2] * v.x + m1[2] * v.y + m2[2] * v.z + m3[2]);
+ return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z + m3.x,
+ m0.y * v.x + m1.y * v.y + m2.y * v.z + m3.y,
+ m0.z * v.x + m1.z * v.y + m2.z * v.z + m3.z);
}
// =======================================================================
vec3 MatrixColMultiplyDir (in vec3 v,
in vec4 m0,
in vec4 m1,
- in vec4 m2,
- in vec4 m3)
+ in vec4 m2)
{
- return vec3 (m0[0] * v.x + m1[0] * v.y + m2[0] * v.z,
- m0[1] * v.x + m1[1] * v.y + m2[1] * v.z,
- m0[2] * v.x + m1[2] * v.y + m2[2] * v.z);
+ return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z,
+ m0.y * v.x + m1.y * v.y + m2.y * v.z,
+ m0.z * v.x + m1.z * v.y + m2.z * v.z);
}
//=======================================================================
//=======================================================================
vec4 BackgroundColor()
{
+#ifdef ADAPTIVE_SAMPLING
+
+ ivec2 aFragCoord = ivec2 (gl_FragCoord.xy);
+
+ ivec2 aTileXY = imageLoad (uOffsetImage, ivec2 (aFragCoord.x / BLOCK_SIZE,
+ aFragCoord.y / BLOCK_SIZE)).xy;
+
+ aTileXY.y += aFragCoord.y % min (uWinSizeY - aTileXY.y, BLOCK_SIZE);
+
+ return mix (uBackColorBot, uBackColorTop, float (aTileXY.y) / uWinSizeY);
+
+#else
+
return mix (uBackColorBot, uBackColorTop, vPixel.y);
+
+#endif
}
/////////////////////////////////////////////////////////////////////////////////////////
return SRay (mix (aP0, aP1, thePixel.y), aDirection);
}
-// =======================================================================
-// function : ComputeOpenGlDepth
-// purpose :
-// =======================================================================
-float ComputeOpenGlDepth (in SRay theRay)
-{
- // a depth in range [0,1]
- float anOpenGlDepth = texelFetch (uOpenGlDepthTexture, ivec2 (gl_FragCoord.xy), 0).r;
- // pixel point in NDC-space [-1,1]
- vec4 aPoint = vec4 (2.0f * vPixel.x - 1.0f,
- 2.0f * vPixel.y - 1.0f,
- 2.0f * anOpenGlDepth - 1.0f,
- 1.0f);
- vec4 aFinal = uUnviewMat * aPoint;
- aFinal.xyz *= 1.f / aFinal.w;
-
- return (anOpenGlDepth < 1.f) ? length (aFinal.xyz - theRay.Origin) : MAXFLOAT;
-}
-
-// =======================================================================
-// function : ComputeOpenGlColor
-// purpose :
-// =======================================================================
-vec4 ComputeOpenGlColor()
-{
- vec4 anOpenGlColor = texelFetch (uOpenGlColorTexture, ivec2 (gl_FragCoord.xy), 0);
- // During blending with factors GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA (for text and markers)
- // the alpha channel (written in the color buffer) was squared.
- anOpenGlColor.a = 1.f - sqrt (anOpenGlColor.a);
-
- return anOpenGlColor;
-}
-
// =======================================================================
// function : IntersectSphere
// purpose : Computes ray-sphere intersection
// function : IntersectTriangle
// purpose : Computes ray-triangle intersection (branchless version)
// =======================================================================
-float IntersectTriangle (in SRay theRay,
- in vec3 thePnt0,
- in vec3 thePnt1,
- in vec3 thePnt2,
- out vec2 theUV,
- out vec3 theNorm)
+void IntersectTriangle (in SRay theRay,
+ in vec3 thePnt0,
+ in vec3 thePnt1,
+ in vec3 thePnt2,
+ out vec3 theUVT,
+ out vec3 theNorm)
{
+ vec3 aToTrg = thePnt0 - theRay.Origin;
+
vec3 aEdge0 = thePnt1 - thePnt0;
vec3 aEdge1 = thePnt0 - thePnt2;
theNorm = cross (aEdge1, aEdge0);
- vec3 aEdge2 = (1.0f / dot (theNorm, theRay.Direct)) * (thePnt0 - theRay.Origin);
+ vec3 theVect = cross (theRay.Direct, aToTrg);
- float aTime = dot (theNorm, aEdge2);
+ theUVT = vec3 (dot (theNorm, aToTrg),
+ dot (theVect, aEdge1),
+ dot (theVect, aEdge0)) * (1.f / dot (theNorm, theRay.Direct));
- vec3 theVec = cross (theRay.Direct, aEdge2);
+ theUVT.x = any (lessThan (theUVT, ZERO)) || (theUVT.y + theUVT.z) > 1.f ? MAXFLOAT : theUVT.x;
+}
- theUV.x = dot (theVec, aEdge1);
- theUV.y = dot (theVec, aEdge0);
+#define EMPTY_ROOT ivec4(0)
- return bool (int(aTime >= 0.0f) &
- int(theUV.x >= 0.0f) &
- int(theUV.y >= 0.0f) &
- int(theUV.x + theUV.y <= 1.0f)) ? aTime : MAXFLOAT;
-}
+//! Utility structure containing information about
+//! currently traversing sub-tree of scene's BVH.
+struct SSubTree
+{
+ //! Transformed ray.
+ SRay TrsfRay;
+
+ //! Inversed ray direction.
+ vec3 Inverse;
+
+ //! Parameters of sub-root node.
+ ivec4 SubData;
+};
+
+#define MATERIAL_AMBN(index) (18 * index + 0)
+#define MATERIAL_DIFF(index) (18 * index + 1)
+#define MATERIAL_SPEC(index) (18 * index + 2)
+#define MATERIAL_EMIS(index) (18 * index + 3)
+#define MATERIAL_REFL(index) (18 * index + 4)
+#define MATERIAL_REFR(index) (18 * index + 5)
+#define MATERIAL_TRAN(index) (18 * index + 6)
+#define MATERIAL_TRS1(index) (18 * index + 7)
+#define MATERIAL_TRS2(index) (18 * index + 8)
+#define MATERIAL_TRS3(index) (18 * index + 9)
+
+#define TRS_OFFSET(treelet) treelet.SubData.x
+#define BVH_OFFSET(treelet) treelet.SubData.y
+#define VRT_OFFSET(treelet) treelet.SubData.z
+#define TRG_OFFSET(treelet) treelet.SubData.w
//! Identifies the absence of intersection.
#define INALID_HIT ivec4 (-1)
int Stack[STACK_SIZE];
// =======================================================================
-// function : ObjectNearestHit
-// purpose : Finds intersection with nearest object triangle
+// function : pop
+// purpose :
// =======================================================================
-ivec4 ObjectNearestHit (in int theBVHOffset, in int theVrtOffset, in int theTrgOffset,
- in SRay theRay, in vec3 theInverse, inout SIntersect theHit, in int theSentinel)
+int pop (inout int theHead)
{
- int aHead = theSentinel; // stack pointer
- int aNode = theBVHOffset; // node to visit
+ int aData = Stack[theHead];
+
+ int aMask = aData >> 26;
+ int aNode = aMask & 0x3;
+
+ aMask >>= 2;
+ if ((aMask & 0x3) == aNode)
+ {
+ --theHead;
+ }
+ else
+ {
+ aMask |= (aMask << 2) & 0x30;
+
+ Stack[theHead] = (aData & 0x03FFFFFF) | (aMask << 26);
+ }
+
+ return (aData & 0x03FFFFFF) + aNode;
+}
+
+// =======================================================================
+// function : SceneNearestHit
+// purpose : Finds intersection with nearest scene triangle
+// =======================================================================
+ivec4 SceneNearestHit (in SRay theRay, in vec3 theInverse, inout SIntersect theHit, out int theTrsfId)
+{
ivec4 aTriIndex = INALID_HIT;
- for (bool toContinue = true; toContinue;)
+ int aNode = 0; // node to traverse
+ int aHead = -1; // pointer of stack
+ int aStop = -1; // BVH level switch
+
+ SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
+
+ for (bool toContinue = true; toContinue; /* none */)
{
- ivec3 aData = texelFetch (uSceneNodeInfoTexture, aNode).xyz;
+ ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
if (aData.x == 0) // if inner node
{
- float aTimeOut;
- float aTimeLft;
- float aTimeRgh;
+ aData.y += BVH_OFFSET (aSubTree);
- aData.y += theBVHOffset;
- aData.z += theBVHOffset;
+ vec4 aHitTimes = vec4 (MAXFLOAT,
+ MAXFLOAT,
+ MAXFLOAT,
+ MAXFLOAT);
- vec3 aNodeMinLft = texelFetch (uSceneMinPointTexture, aData.y).xyz;
- vec3 aNodeMinRgh = texelFetch (uSceneMinPointTexture, aData.z).xyz;
- vec3 aNodeMaxLft = texelFetch (uSceneMaxPointTexture, aData.y).xyz;
- vec3 aNodeMaxRgh = texelFetch (uSceneMaxPointTexture, aData.z).xyz;
+ vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;
- vec3 aTime0 = (aNodeMinLft - theRay.Origin) * theInverse;
- vec3 aTime1 = (aNodeMaxLft - theRay.Origin) * theInverse;
+ vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
- vec3 aTimeMax = max (aTime0, aTime1);
- vec3 aTimeMin = min (aTime0, aTime1);
+ vec3 aTimeMax = max (aNodeMin0, aNodeMax0);
+ vec3 aTimeMin = min (aNodeMin0, aNodeMax0);
- aTime0 = (aNodeMinRgh - theRay.Origin) * theInverse;
- aTime1 = (aNodeMaxRgh - theRay.Origin) * theInverse;
+ float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
+ float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
- aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
- aTimeLft = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
+ aHitTimes.x = mix (MAXFLOAT, aTimeEnter,
+ aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f);
- int aHitLft = int(aTimeLft <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeLft <= theHit.Time);
+ aTimeMax = max (aNodeMin1, aNodeMax1);
+ aTimeMin = min (aNodeMin1, aNodeMax1);
- aTimeMax = max (aTime0, aTime1);
- aTimeMin = min (aTime0, aTime1);
+ aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
+ aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
- aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
- aTimeRgh = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
+ aHitTimes.y = mix (MAXFLOAT, aTimeEnter,
+ aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f);
- int aHitRgh = int(aTimeRgh <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeRgh <= theHit.Time);
+ aTimeMax = max (aNodeMin2, aNodeMax2);
+ aTimeMin = min (aNodeMin2, aNodeMax2);
- if (bool(aHitLft & aHitRgh))
+ aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
+ aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
+
+ aHitTimes.z = mix (MAXFLOAT, aTimeEnter,
+ aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 1);
+
+ aTimeMax = max (aNodeMin3, aNodeMax3);
+ aTimeMin = min (aNodeMin3, aNodeMax3);
+
+ aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
+ aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
+
+ aHitTimes.w = mix (MAXFLOAT, aTimeEnter,
+ aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 2);
+
+ ivec4 aChildren = ivec4 (0, 1, 2, 3);
+
+ aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;
+ aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;
+ aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;
+ aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;
+ aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;
+ aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;
+ aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;
+ aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;
+ aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;
+ aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;
+
+ if (aHitTimes.x != MAXFLOAT)
{
- aNode = (aTimeLft < aTimeRgh) ? aData.y : aData.z;
+ int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2
+ | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);
- Stack[++aHead] = (aTimeLft < aTimeRgh) ? aData.z : aData.y;
+ if (aHitTimes.y != MAXFLOAT)
+ Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;
+
+ aNode = aData.y + aChildren.x;
}
else
{
- if (bool(aHitLft | aHitRgh))
+ toContinue = (aHead >= 0);
+
+ if (aHead == aStop) // go to top-level BVH
{
- aNode = bool(aHitLft) ? aData.y : aData.z;
+ aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
}
- else
- {
- toContinue = (aHead != theSentinel);
- if (toContinue)
- aNode = Stack[aHead--];
- }
+ if (aHead >= 0)
+ aNode = pop (aHead);
}
}
- else // if leaf node
+ else if (aData.x < 0) // leaf node (contains triangles)
{
vec3 aNormal;
- vec2 aParams;
+ vec3 aTimeUV;
for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)
{
- ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + theTrgOffset);
+ ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));
- vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += theVrtOffset).xyz;
- vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += theVrtOffset).xyz;
- vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += theVrtOffset).xyz;
+ vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += VRT_OFFSET (aSubTree)).xyz;
+ vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += VRT_OFFSET (aSubTree)).xyz;
+ vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += VRT_OFFSET (aSubTree)).xyz;
- float aTime = IntersectTriangle (theRay,
- aPoint0,
- aPoint1,
- aPoint2,
- aParams,
- aNormal);
+ IntersectTriangle (aSubTree.TrsfRay, aPoint0, aPoint1, aPoint2, aTimeUV, aNormal);
- if (aTime < theHit.Time)
+ if (aTimeUV.x < theHit.Time)
{
aTriIndex = aTriangle;
- theHit = SIntersect (aTime, aParams, aNormal);
+ theTrsfId = TRS_OFFSET (aSubTree);
+
+ theHit = SIntersect (aTimeUV.x, aTimeUV.yz, aNormal);
}
}
- toContinue = (aHead != theSentinel);
-
- if (toContinue)
- aNode = Stack[aHead--];
- }
- }
-
- return aTriIndex;
-}
-
-#define MATERIAL_AMBN(index) (18 * index + 0)
-#define MATERIAL_DIFF(index) (18 * index + 1)
-#define MATERIAL_SPEC(index) (18 * index + 2)
-#define MATERIAL_EMIS(index) (18 * index + 3)
-#define MATERIAL_REFL(index) (18 * index + 4)
-#define MATERIAL_REFR(index) (18 * index + 5)
-#define MATERIAL_TRAN(index) (18 * index + 6)
-#define MATERIAL_TRS1(index) (18 * index + 7)
-#define MATERIAL_TRS2(index) (18 * index + 8)
-#define MATERIAL_TRS3(index) (18 * index + 9)
-
-// =======================================================================
-// function : ObjectAnyHit
-// purpose : Finds intersection with any object triangle
-// =======================================================================
-float ObjectAnyHit (in int theBVHOffset, in int theVrtOffset, in int theTrgOffset,
- in SRay theRay, in vec3 theInverse, in float theDistance, in int theSentinel)
-{
- int aHead = theSentinel; // stack pointer
- int aNode = theBVHOffset; // node to visit
-
- float aFactor = 1.f;
-
- for (bool toContinue = true; toContinue;)
- {
- ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
-
- if (aData.x == 0) // if inner node
- {
- float aTimeOut;
- float aTimeLft;
- float aTimeRgh;
-
- aData.y += theBVHOffset;
- aData.z += theBVHOffset;
+ toContinue = (aHead >= 0);
- vec3 aNodeMinLft = texelFetch (uSceneMinPointTexture, aData.y).xyz;
- vec3 aNodeMaxLft = texelFetch (uSceneMaxPointTexture, aData.y).xyz;
- vec3 aNodeMinRgh = texelFetch (uSceneMinPointTexture, aData.z).xyz;
- vec3 aNodeMaxRgh = texelFetch (uSceneMaxPointTexture, aData.z).xyz;
-
- vec3 aTime0 = (aNodeMinLft - theRay.Origin) * theInverse;
- vec3 aTime1 = (aNodeMaxLft - theRay.Origin) * theInverse;
-
- vec3 aTimeMax = max (aTime0, aTime1);
- vec3 aTimeMin = min (aTime0, aTime1);
-
- aTime0 = (aNodeMinRgh - theRay.Origin) * theInverse;
- aTime1 = (aNodeMaxRgh - theRay.Origin) * theInverse;
-
- aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
- aTimeLft = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
-
- int aHitLft = int(aTimeLft <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeLft <= theDistance);
-
- aTimeMax = max (aTime0, aTime1);
- aTimeMin = min (aTime0, aTime1);
-
- aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
- aTimeRgh = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
-
- int aHitRgh = int(aTimeRgh <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeRgh <= theDistance);
-
- if (bool(aHitLft & aHitRgh))
+ if (aHead == aStop) // go to top-level BVH
{
- aNode = (aTimeLft < aTimeRgh) ? aData.y : aData.z;
-
- Stack[++aHead] = (aTimeLft < aTimeRgh) ? aData.z : aData.y;
+ aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
}
- else
- {
- if (bool(aHitLft | aHitRgh))
- {
- aNode = bool(aHitLft) ? aData.y : aData.z;
- }
- else
- {
- toContinue = (aHead != theSentinel);
- if (toContinue)
- aNode = Stack[aHead--];
- }
- }
+ if (aHead >= 0)
+ aNode = pop (aHead);
}
- else // if leaf node
+ else if (aData.x > 0) // switch node
{
- vec3 aNormal;
- vec2 aParams;
+ aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root
- for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)
- {
- ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + theTrgOffset);
+ vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);
+ vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);
+ vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);
+ vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);
- vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x + theVrtOffset).xyz;
- vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y + theVrtOffset).xyz;
- vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z + theVrtOffset).xyz;
+ aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,
+ aInvTransf0,
+ aInvTransf1,
+ aInvTransf2);
- float aTime = IntersectTriangle (theRay,
- aPoint0,
- aPoint1,
- aPoint2,
- aParams,
- aNormal);
+ aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) /
+ max (abs (aSubTree.TrsfRay.Direct), SMALL);
-#ifdef TRANSPARENT_SHADOWS
- if (aTime < theDistance)
- {
- aFactor *= 1.f - texelFetch (uRaytraceMaterialTexture, MATERIAL_TRAN (aTriangle.w)).x;
- }
-#else
- if (aTime < theDistance)
- {
- aFactor = 0.f;
- }
-#endif
- }
+ aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,
+ aInvTransf0,
+ aInvTransf1,
+ aInvTransf2,
+ aInvTransf3);
- toContinue = (aHead != theSentinel) && (aFactor > 0.1f);
+ aNode = BVH_OFFSET (aSubTree); // go to sub-root node
- if (toContinue)
- aNode = Stack[aHead--];
+ aStop = aHead; // store current stack pointer
}
}
- return aFactor;
+ return aTriIndex;
}
// =======================================================================
-// function : SceneNearestHit
-// purpose : Finds intersection with nearest scene triangle
+// function : SceneAnyHit
+// purpose : Finds intersection with any scene triangle
// =======================================================================
-ivec4 SceneNearestHit (in SRay theRay, in vec3 theInverse, inout SIntersect theHit, out int theTrsfId)
+float SceneAnyHit (in SRay theRay, in vec3 theInverse, in float theDistance)
{
- int aHead = -1; // stack pointer
- int aNode = 0; // node to visit
+ float aFactor = 1.f;
- ivec4 aHitObject = INALID_HIT;
+ int aNode = 0; // node to traverse
+ int aHead = -1; // pointer of stack
+ int aStop = -1; // BVH level switch
- for (bool toContinue = true; toContinue;)
+ SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
+
+ for (bool toContinue = true; toContinue; /* none */)
{
ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
- if (aData.x != 0) // if leaf node
+ if (aData.x == 0) // if inner node
{
- vec3 aNodeMin = texelFetch (uSceneMinPointTexture, aNode).xyz;
- vec3 aNodeMax = texelFetch (uSceneMaxPointTexture, aNode).xyz;
-
- vec3 aTime0 = (aNodeMin - theRay.Origin) * theInverse;
- vec3 aTime1 = (aNodeMax - theRay.Origin) * theInverse;
+ aData.y += BVH_OFFSET (aSubTree);
- vec3 aTimes = min (aTime0, aTime1);
+ vec4 aHitTimes = vec4 (MAXFLOAT,
+ MAXFLOAT,
+ MAXFLOAT,
+ MAXFLOAT);
- if (max (aTimes.x, max (aTimes.y, aTimes.z)) < theHit.Time)
- {
- // fetch object transformation
- int aTrsfId = (aData.x - 1) * 4;
-
- vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, aTrsfId + 0);
- vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, aTrsfId + 1);
- vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, aTrsfId + 2);
- vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, aTrsfId + 3);
+ vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;
- SRay aTrsfRay = SRay (
- MatrixColMultiplyPnt (theRay.Origin, aInvTransf0, aInvTransf1, aInvTransf2, aInvTransf3),
- MatrixColMultiplyDir (theRay.Direct, aInvTransf0, aInvTransf1, aInvTransf2, aInvTransf3));
+ vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
+ vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
- vec3 aTrsfInverse = 1.0f / max (abs (aTrsfRay.Direct), SMALL);
+ vec3 aTimeMax = max (aNodeMin0, aNodeMax0);
+ vec3 aTimeMin = min (aNodeMin0, aNodeMax0);
- aTrsfInverse = mix (-aTrsfInverse, aTrsfInverse, step (ZERO, aTrsfRay.Direct));
+ float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
+ float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
- ivec4 aTriIndex = ObjectNearestHit (
- aData.y, aData.z, aData.w, aTrsfRay, aTrsfInverse, theHit, aHead);
-
- if (aTriIndex.x != -1)
- {
- aHitObject = ivec4 (aTriIndex.x, // vertex 0
- aTriIndex.y, // vertex 1
- aTriIndex.z, // vertex 2
- aTriIndex.w); // material
-
- theTrsfId = aTrsfId;
- }
- }
+ aHitTimes.x = mix (MAXFLOAT, aTimeEnter,
+ aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f);
- toContinue = aHead >= 0;
+ aTimeMax = max (aNodeMin1, aNodeMax1);
+ aTimeMin = min (aNodeMin1, aNodeMax1);
- if (toContinue)
- aNode = Stack[aHead--];
- }
- else // if inner node
- {
- float aTimeOut;
- float aTimeLft;
- float aTimeRgh;
+ aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
+ aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
- vec3 aNodeMinLft = texelFetch (uSceneMinPointTexture, aData.y).xyz;
- vec3 aNodeMaxLft = texelFetch (uSceneMaxPointTexture, aData.y).xyz;
- vec3 aNodeMinRgh = texelFetch (uSceneMinPointTexture, aData.z).xyz;
- vec3 aNodeMaxRgh = texelFetch (uSceneMaxPointTexture, aData.z).xyz;
+ aHitTimes.y = mix (MAXFLOAT, aTimeEnter,
+ aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f);
- vec3 aTime0 = (aNodeMinLft - theRay.Origin) * theInverse;
- vec3 aTime1 = (aNodeMaxLft - theRay.Origin) * theInverse;
+ aTimeMax = max (aNodeMin2, aNodeMax2);
+ aTimeMin = min (aNodeMin2, aNodeMax2);
- vec3 aTimeMax = max (aTime0, aTime1);
- vec3 aTimeMin = min (aTime0, aTime1);
+ aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
+ aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
- aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
- aTimeLft = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
+ aHitTimes.z = mix (MAXFLOAT, aTimeEnter,
+ aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 1);
- int aHitLft = int(aTimeLft <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeLft <= theHit.Time);
+ aTimeMax = max (aNodeMin3, aNodeMax3);
+ aTimeMin = min (aNodeMin3, aNodeMax3);
- aTime0 = (aNodeMinRgh - theRay.Origin) * theInverse;
- aTime1 = (aNodeMaxRgh - theRay.Origin) * theInverse;
+ aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
+ aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
- aTimeMax = max (aTime0, aTime1);
- aTimeMin = min (aTime0, aTime1);
+ aHitTimes.w = mix (MAXFLOAT, aTimeEnter,
+ aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 2);
- aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
- aTimeRgh = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
+ ivec4 aChildren = ivec4 (0, 1, 2, 3);
- int aHitRgh = int(aTimeRgh <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeRgh <= theHit.Time);
+ aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;
+ aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;
+ aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;
+ aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;
+ aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;
+ aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;
+ aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;
+ aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;
+ aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;
+ aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;
- if (bool(aHitLft & aHitRgh))
+ if (aHitTimes.x != MAXFLOAT)
{
- aNode = (aTimeLft < aTimeRgh) ? aData.y : aData.z;
+ int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2
+ | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);
- Stack[++aHead] = (aTimeLft < aTimeRgh) ? aData.z : aData.y;
+ if (aHitTimes.y != MAXFLOAT)
+ Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;
+
+ aNode = aData.y + aChildren.x;
}
else
{
- if (bool(aHitLft | aHitRgh))
+ toContinue = (aHead >= 0);
+
+ if (aHead == aStop) // go to top-level BVH
{
- aNode = bool(aHitLft) ? aData.y : aData.z;
+ aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
}
- else
- {
- toContinue = aHead >= 0;
- if (toContinue)
- aNode = Stack[aHead--];
- }
+ if (aHead >= 0)
+ aNode = pop (aHead);
}
}
- }
-
- return aHitObject;
-}
-
-// =======================================================================
-// function : SceneAnyHit
-// purpose : Finds intersection with any scene triangle
-// =======================================================================
-float SceneAnyHit (in SRay theRay, in vec3 theInverse, in float theDistance)
-{
- int aHead = -1; // stack pointer
- int aNode = 0; // node to visit
-
- float aFactor = 1.f;
-
- for (bool toContinue = true; toContinue;)
- {
- ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
-
- if (aData.x != 0) // if leaf node
+ else if (aData.x < 0) // leaf node
{
- // fetch object transformation
- int aTrsfId = (aData.x - 1) * 4;
-
- vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, aTrsfId + 0);
- vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, aTrsfId + 1);
- vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, aTrsfId + 2);
- vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, aTrsfId + 3);
+ vec3 aNormal;
+ vec3 aTimeUV;
- SRay aTrsfRay = SRay (
- MatrixColMultiplyPnt (theRay.Origin, aInvTransf0, aInvTransf1, aInvTransf2, aInvTransf3),
- MatrixColMultiplyDir (theRay.Direct, aInvTransf0, aInvTransf1, aInvTransf2, aInvTransf3));
+ for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)
+ {
+ ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));
- vec3 aTrsfInverse = 1.0f / max (abs (aTrsfRay.Direct), SMALL);
+ vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += VRT_OFFSET (aSubTree)).xyz;
+ vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += VRT_OFFSET (aSubTree)).xyz;
+ vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += VRT_OFFSET (aSubTree)).xyz;
- aTrsfInverse = mix (-aTrsfInverse, aTrsfInverse, step (ZERO, aTrsfRay.Direct));
+ IntersectTriangle (aSubTree.TrsfRay, aPoint0, aPoint1, aPoint2, aTimeUV, aNormal);
#ifdef TRANSPARENT_SHADOWS
- aFactor *= ObjectAnyHit (
- aData.y, aData.z, aData.w, aTrsfRay, aTrsfInverse, theDistance, aHead);
-
- toContinue = aHead >= 0 && aFactor >= 0.1f;
+ if (aTimeUV.x < theDistance)
+ {
+ aFactor *= 1.f - texelFetch (uRaytraceMaterialTexture, MATERIAL_TRAN (aTriangle.w)).x;
+ }
#else
- aFactor = ObjectAnyHit (
- aData.y, aData.z, aData.w, aTrsfRay, aTrsfInverse, theDistance, aHead);
-
- toContinue = aHead >= 0 && aFactor != 0.0f;
+ if (aTimeUV.x < theDistance)
+ {
+ aFactor = 0.f;
+ }
#endif
+ }
- if (toContinue)
- aNode = Stack[aHead--];
- }
- else // if inner node
- {
- float aTimeOut;
- float aTimeLft;
- float aTimeRgh;
-
- vec3 aNodeMinLft = texelFetch (uSceneMinPointTexture, aData.y).xyz;
- vec3 aNodeMaxLft = texelFetch (uSceneMaxPointTexture, aData.y).xyz;
- vec3 aNodeMinRgh = texelFetch (uSceneMinPointTexture, aData.z).xyz;
- vec3 aNodeMaxRgh = texelFetch (uSceneMaxPointTexture, aData.z).xyz;
-
- vec3 aTime0 = (aNodeMinLft - theRay.Origin) * theInverse;
- vec3 aTime1 = (aNodeMaxLft - theRay.Origin) * theInverse;
-
- vec3 aTimeMax = max (aTime0, aTime1);
- vec3 aTimeMin = min (aTime0, aTime1);
-
- aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
- aTimeLft = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
+ toContinue = (aHead >= 0) && (aFactor > 0.1f);
- int aHitLft = int(aTimeLft <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeLft <= theDistance);
+ if (aHead == aStop) // go to top-level BVH
+ {
+ aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
+ }
- aTime0 = (aNodeMinRgh - theRay.Origin) * theInverse;
- aTime1 = (aNodeMaxRgh - theRay.Origin) * theInverse;
+ if (aHead >= 0)
+ aNode = pop (aHead);
+ }
+ else if (aData.x > 0) // switch node
+ {
+ aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root
- aTimeMax = max (aTime0, aTime1);
- aTimeMin = min (aTime0, aTime1);
+ vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);
+ vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);
+ vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);
+ vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);
- aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
- aTimeRgh = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
+ aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,
+ aInvTransf0,
+ aInvTransf1,
+ aInvTransf2);
- int aHitRgh = int(aTimeRgh <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeRgh <= theDistance);
+ aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,
+ aInvTransf0,
+ aInvTransf1,
+ aInvTransf2,
+ aInvTransf3);
- if (bool(aHitLft & aHitRgh))
- {
- aNode = (aTimeLft < aTimeRgh) ? aData.y : aData.z;
+ aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) / max (abs (aSubTree.TrsfRay.Direct), SMALL);
- Stack[++aHead] = (aTimeLft < aTimeRgh) ? aData.z : aData.y;
- }
- else
- {
- if (bool(aHitLft | aHitRgh))
- {
- aNode = bool(aHitLft) ? aData.y : aData.z;
- }
- else
- {
- toContinue = aHead >= 0;
+ aNode = BVH_OFFSET (aSubTree); // go to sub-root node
- if (toContinue)
- aNode = Stack[aHead--];
- }
- }
+ aStop = aHead; // store current stack pointer
}
}
int aTrsfId;
- float anOpenGlDepth = ComputeOpenGlDepth (theRay);
+ float aRaytraceDepth = MAXFLOAT;
for (int aDepth = 0; aDepth < NB_BOUNCES; ++aDepth)
{
}
else
{
- vec4 aGlColor = ComputeOpenGlColor();
- aColor = vec4 (BackgroundColor().rgb * aGlColor.w + ComputeOpenGlColor().rgb, aGlColor.w);
+ aColor = BackgroundColor();
}
aResult += aWeight.xyz * aColor.xyz; aWeight.w *= aColor.w;
dot (aInvTransf1, aHit.Normal),
dot (aInvTransf2, aHit.Normal)));
- // For polygons that are parallel to the screen plane, the depth slope
- // is equal to 1, resulting in small polygon offset. For polygons that
- // that are at a large angle to the screen, the depth slope tends to 1,
- // resulting in a larger polygon offset
- float aPolygonOffset = uSceneEpsilon * EPS_SCALE /
- max (abs (dot (theRay.Direct, aHit.Normal)), MIN_SLOPE);
+ theRay.Origin += theRay.Direct * aHit.Time; // intersection point
- if (anOpenGlDepth < aHit.Time + aPolygonOffset)
+ // Evaluate depth on first hit
+ if (aDepth == 0)
{
- vec4 aGlColor = ComputeOpenGlColor();
+ // For polygons that are parallel to the screen plane, the depth slope
+ // is equal to 1, resulting in small polygon offset. For polygons that
+ // that are at a large angle to the screen, the depth slope tends to 1,
+ // resulting in a larger polygon offset
+ float aPolygonOffset = uSceneEpsilon * EPS_SCALE /
+ max (abs (dot (theRay.Direct, aHit.Normal)), MIN_SLOPE);
- aResult += aWeight.xyz * aGlColor.xyz;
- aWeight *= aGlColor.w;
- }
+ // Hit point in NDC-space [-1,1] (the polygon offset is applied in the world space)
+ vec4 aNDCPoint = uViewMat * vec4 (theRay.Origin + theRay.Direct * aPolygonOffset, 1.f);
- theRay.Origin += theRay.Direct * aHit.Time; // intersection point
+ aRaytraceDepth = (aNDCPoint.z / aNDCPoint.w) * 0.5f + 0.5f;
+ }
vec3 aNormal = SmoothNormal (aHit.UV, aTriIndex);
dot (aTrsfRow2, aTexCoord));
vec3 aTexColor = textureLod (
- uTextureSamplers[int(aDiffuse.w)], aTexCoord.st, 0.f).rgb;
+ sampler2D (uTextureSamplers[int(aDiffuse.w)]), aTexCoord.st, 0.f).rgb;
aDiffuse.rgb *= aTexColor;
aAmbient.rgb *= aTexColor;
{
theRay.Direct = Refract (theRay.Direct, aNormal, aOpacity.z, aOpacity.w);
}
- else
- {
- anOpenGlDepth -= aHit.Time + uSceneEpsilon;
- }
}
else
{
theInverse = 1.0f / max (abs (theRay.Direct), SMALL);
theInverse = mix (-theInverse, theInverse, step (ZERO, theRay.Direct));
-
- anOpenGlDepth = MAXFLOAT; // disable combining image with OpenGL output
}
theRay.Origin += theRay.Direct * uSceneEpsilon;
}
+ gl_FragDepth = aRaytraceDepth;
+
return vec4 (aResult.x,
aResult.y,
aResult.z,