From 4742f40128235ac35815fcdb1ba4becfc92fac6f Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 19 Jul 2016 12:23:57 -0700 Subject: [PATCH] Separating the normal packing into it s own file and make sure to sclae the filter radius correctly dpeending on the resolution of diffusion --- libraries/gpu/src/gpu/PackedNormal.slh | 61 +++++++++++++++++++ .../render-utils/src/DebugDeferredBuffer.cpp | 2 +- .../src/DeferredLightingEffect.cpp | 3 +- .../render-utils/src/SurfaceGeometryPass.cpp | 15 ++++- libraries/render-utils/src/simple.slf | 2 +- .../surfaceGeometry_downsampleDepthNormal.slf | 57 ++--------------- libraries/render/src/render/BlurTask.slh | 6 +- 7 files changed, 86 insertions(+), 60 deletions(-) create mode 100644 libraries/gpu/src/gpu/PackedNormal.slh diff --git a/libraries/gpu/src/gpu/PackedNormal.slh b/libraries/gpu/src/gpu/PackedNormal.slh new file mode 100644 index 0000000000..ecb383fc30 --- /dev/null +++ b/libraries/gpu/src/gpu/PackedNormal.slh @@ -0,0 +1,61 @@ + +<@if not PACKED_NORMAL_SLH@> +<@def PACKED_NORMAL_SLH@> + +vec2 signNotZero(vec2 v) { + return vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0); +} + +vec2 float32x3_to_oct(in vec3 v) { + vec2 p = v.xy * (1.0 / (abs(v.x) + abs(v.y) + abs(v.z))); + return ((v.z <= 0.0) ? ((1.0 - abs(p.yx)) * signNotZero(p)) : p); +} + + +vec3 oct_to_float32x3(in vec2 e) { + vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y)); + if (v.z < 0) { + v.xy = (1.0 - abs(v.yx)) * signNotZero(v.xy); + } + return normalize(v); +} + +vec3 snorm12x2_to_unorm8x3(vec2 f) { + vec2 u = vec2(round(clamp(f, -1.0, 1.0) * 2047.0 + 2047.0)); + float t = floor(u.y / 256.0); + + return floor(vec3( + u.x / 16.0, + fract(u.x / 16.0) * 256.0 + t, + u.y - t * 256.0 + )) / 255.0; +} + +vec2 unorm8x3_to_snorm12x2(vec3 u) { + u *= 255.0; + u.y *= (1.0 / 16.0); + vec2 s = vec2( u.x * 16.0 + floor(u.y), + fract(u.y) * (16.0 * 256.0) + u.z); + return clamp(s * (1.0 / 2047.0) - 1.0, vec2(-1.0), vec2(1.0)); +} + + +// Recommended function to pack/unpack vec3 normals to vec3 rgb with best efficiency +vec3 packNormal(in vec3 n) { + return snorm12x2_to_unorm8x3(float32x3_to_oct(n)); +} + +vec3 unpackNormal(in vec3 p) { + return oct_to_float32x3(unorm8x3_to_snorm12x2(p)); +} + +<@endif@> diff --git a/libraries/render-utils/src/DebugDeferredBuffer.cpp b/libraries/render-utils/src/DebugDeferredBuffer.cpp index 672bb00cc8..9d0e38027e 100644 --- a/libraries/render-utils/src/DebugDeferredBuffer.cpp +++ b/libraries/render-utils/src/DebugDeferredBuffer.cpp @@ -414,7 +414,7 @@ void DebugDeferredBuffer::run(const SceneContextPointer& sceneContext, const Ren batch.setResourceTexture(HalfNormal, linearDepthTarget->getHalfNormalTexture()); batch.setResourceTexture(Curvature, surfaceGeometryFramebuffer->getCurvatureTexture()); - batch.setResourceTexture(DiffusedCurvature, diffusedCurvatureFramebuffer->getRenderBuffer(0)); + batch.setResourceTexture(DiffusedCurvature, surfaceGeometryFramebuffer->getLowCurvatureTexture()); if (DependencyManager::get()->isAmbientOcclusionEnabled()) { batch.setResourceTexture(AmbientOcclusion, framebufferCache->getOcclusionTexture()); } else { diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 9d8c1ef200..6f202f6200 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -448,7 +448,8 @@ void RenderDeferredSetup::run(const render::SceneContextPointer& sceneContext, c batch.setResourceTexture(DEFERRED_BUFFER_CURVATURE_UNIT, surfaceGeometryFramebuffer->getCurvatureTexture()); } if (lowCurvatureNormalFramebuffer) { - batch.setResourceTexture(DEFERRED_BUFFER_DIFFUSED_CURVATURE_UNIT, lowCurvatureNormalFramebuffer->getRenderBuffer(0)); + // batch.setResourceTexture(DEFERRED_BUFFER_DIFFUSED_CURVATURE_UNIT, lowCurvatureNormalFramebuffer->getRenderBuffer(0)); + batch.setResourceTexture(DEFERRED_BUFFER_DIFFUSED_CURVATURE_UNIT, surfaceGeometryFramebuffer->getLowCurvatureTexture()); } if (subsurfaceScatteringResource) { batch.setUniformBuffer(SCATTERING_PARAMETERS_BUFFER_SLOT, subsurfaceScatteringResource->getParametersBuffer()); diff --git a/libraries/render-utils/src/SurfaceGeometryPass.cpp b/libraries/render-utils/src/SurfaceGeometryPass.cpp index 26063ba30c..fd778d30be 100644 --- a/libraries/render-utils/src/SurfaceGeometryPass.cpp +++ b/libraries/render-utils/src/SurfaceGeometryPass.cpp @@ -367,9 +367,10 @@ SurfaceGeometryPass::SurfaceGeometryPass() : } void SurfaceGeometryPass::configure(const Config& config) { + const float CM_TO_M = 0.01f; - if ((config.depthThreshold * 100.0f) != getCurvatureDepthThreshold()) { - _parametersBuffer.edit().curvatureInfo.x = config.depthThreshold * 100.0f; + if ((config.depthThreshold * CM_TO_M) != getCurvatureDepthThreshold()) { + _parametersBuffer.edit().curvatureInfo.x = config.depthThreshold * CM_TO_M; } if (config.basisScale != getCurvatureBasisScale()) { @@ -389,7 +390,8 @@ void SurfaceGeometryPass::configure(const Config& config) { _parametersBuffer.edit().resolutionInfo.w = config.resolutionLevel; } - _diffusePass.getParameters()->setFilterRadiusScale(config.diffuseFilterScale); + auto filterRadius = (getResolutionLevel() > 0 ? config.diffuseFilterScale / 2.0f : config.diffuseFilterScale); + _diffusePass.getParameters()->setFilterRadiusScale(filterRadius); _diffusePass.getParameters()->setDepthThreshold(config.diffuseDepthThreshold); } @@ -445,6 +447,7 @@ void SurfaceGeometryPass::run(const render::SceneContextPointer& sceneContext, c auto diffuseVPipeline = _diffusePass.getBlurVPipeline(); auto diffuseHPipeline = _diffusePass.getBlurHPipeline(); + _diffusePass.getParameters()->setWidthHeight(curvatureViewport.z, curvatureViewport.w, args->_context->isStereo()); glm::ivec2 textureSize(curvatureTexture->getDimensions()); _diffusePass.getParameters()->setTexcoordTransform(gpu::Framebuffer::evalSubregionTexcoordTransformCoefficients(textureSize, curvatureViewport)); _diffusePass.getParameters()->setDepthPerspective(args->getViewFrustum().getProjection()[1][1]); @@ -475,6 +478,12 @@ void SurfaceGeometryPass::run(const render::SceneContextPointer& sceneContext, c batch.setResourceTexture(SurfaceGeometryPass_NormalMapSlot, normalTexture); batch.draw(gpu::TRIANGLE_STRIP, 4); + + batch.setResourceTexture(SurfaceGeometryPass_DepthMapSlot, nullptr); + batch.setResourceTexture(SurfaceGeometryPass_NormalMapSlot, nullptr); + batch.setUniformBuffer(SurfaceGeometryPass_ParamsSlot, nullptr); + batch.setUniformBuffer(SurfaceGeometryPass_FrameTransformSlot, nullptr); + // Diffusion pass const int BlurTask_ParamsSlot = 0; const int BlurTask_SourceSlot = 0; diff --git a/libraries/render-utils/src/simple.slf b/libraries/render-utils/src/simple.slf index 4a16c1c46a..dd5faf40db 100644 --- a/libraries/render-utils/src/simple.slf +++ b/libraries/render-utils/src/simple.slf @@ -41,7 +41,7 @@ void main(void) { #ifdef PROCEDURAL_V1 specular = getProceduralColor().rgb; // Procedural Shaders are expected to be Gamma corrected so let's bring back the RGB in linear space for the rest of the pipeline - specular = pow(specular, vec3(2.2)); + //specular = pow(specular, vec3(2.2)); emissiveAmount = 1.0; #else emissiveAmount = getProceduralColors(diffuse, specular, shininess); diff --git a/libraries/render-utils/src/surfaceGeometry_downsampleDepthNormal.slf b/libraries/render-utils/src/surfaceGeometry_downsampleDepthNormal.slf index af371d53a8..533073b224 100644 --- a/libraries/render-utils/src/surfaceGeometry_downsampleDepthNormal.slf +++ b/libraries/render-utils/src/surfaceGeometry_downsampleDepthNormal.slf @@ -10,35 +10,11 @@ // - +<@include gpu/PackedNormal.slh@> uniform sampler2D linearDepthMap; uniform sampler2D normalMap; - -vec2 signNotZero(vec2 v) { - return vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0); -} - -vec3 oct_to_float32x3(in vec2 e) { - vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y)); - if (v.z < 0) { - v.xy = (1.0 - abs(v.yx)) * signNotZero(v.xy); - } - return normalize(v); -} - -vec2 unorm8x3_to_snorm12x2(vec3 u) { - u *= 255.0; - u.y *= (1.0 / 16.0); - vec2 s = vec2( u.x * 16.0 + floor(u.y), - fract(u.y) * (16.0 * 256.0) + u.z); - return clamp(s * (1.0 / 2047.0) - 1.0, vec2(-1.0), vec2(1.0)); -} -vec3 unpackNormal(in vec3 p) { - return oct_to_float32x3(unorm8x3_to_snorm12x2(p)); -} - in vec2 varTexCoord0; out vec4 outLinearDepth; @@ -46,43 +22,22 @@ out vec4 outNormal; void main(void) { // Gather 2 by 2 quads from texture - vec4 Zeyes = textureGather(linearDepthMap, varTexCoord0, 0); + + // Try different filters for Z +// vec4 Zeyes = textureGather(linearDepthMap, varTexCoord0, 0); + // float Zeye = min(min(Zeyes.x, Zeyes.y), min(Zeyes.z, Zeyes.w)); + float Zeye = texture(linearDepthMap, varTexCoord0).x; vec4 rawNormalsX = textureGather(normalMap, varTexCoord0, 0); vec4 rawNormalsY = textureGather(normalMap, varTexCoord0, 1); vec4 rawNormalsZ = textureGather(normalMap, varTexCoord0, 2); - float Zeye = min(min(Zeyes.x, Zeyes.y), min(Zeyes.z, Zeyes.w)); vec3 normal = vec3(0.0); normal += unpackNormal(vec3(rawNormalsX[0], rawNormalsY[0], rawNormalsZ[0])); normal += unpackNormal(vec3(rawNormalsX[1], rawNormalsY[1], rawNormalsZ[1])); normal += unpackNormal(vec3(rawNormalsX[2], rawNormalsY[2], rawNormalsZ[2])); normal += unpackNormal(vec3(rawNormalsX[3], rawNormalsY[3], rawNormalsZ[3])); - /* - ivec2 texpos = ivec2(gl_FragCoord.xy) * 2; - - vec4 Zeyes; - Zeyes[0] = texelFetch(linearDepthMap, texpos, 0).x; - Zeyes[1] = texelFetch(linearDepthMap, texpos + ivec2(0, 1), 0).x; - Zeyes[2] = texelFetch(linearDepthMap, texpos + ivec2(1, 0), 0).x; - Zeyes[3] = texelFetch(linearDepthMap, texpos + ivec2(1, 1), 0).x; - - vec3 rawNormals[4]; - rawNormals[0] = texelFetch(normalMap, texpos, 0).xyz; - rawNormals[1] = texelFetch(normalMap, texpos + ivec2(0, 1), 0).xyz; - rawNormals[2] = texelFetch(normalMap, texpos + ivec2(1, 0), 0).xyz; - rawNormals[3] = texelFetch(normalMap, texpos + ivec2(1, 1), 0).xyz; - - float Zeye = min(min(Zeyes.x, Zeyes.y), min(Zeyes.z, Zeyes.w)); - - vec3 normal = vec3(0.0); - - normal += unpackNormal(rawNormals[0]); - normal += unpackNormal(rawNormals[1]); - normal += unpackNormal(rawNormals[2]); - normal += unpackNormal(rawNormals[3]); -*/ normal = normalize(normal); diff --git a/libraries/render/src/render/BlurTask.slh b/libraries/render/src/render/BlurTask.slh index d40aca4a76..de2614eb51 100644 --- a/libraries/render/src/render/BlurTask.slh +++ b/libraries/render/src/render/BlurTask.slh @@ -126,7 +126,7 @@ vec4 pixelShaderGaussianDepthAware(vec2 texcoord, vec2 direction, vec2 pixelStep // Accumulate the center sample vec4 srcBlurred = gaussianDistributionCurve[0] * sampleCenter; -/* for(int i = 1; i < NUM_TAPS; i++) { + for(int i = 1; i < NUM_TAPS; i++) { // Fetch color and depth for current sample. vec2 sampleCoord = texcoord + (gaussianDistributionOffset[i] * finalStep); float srcDepth = texture(depthMap, sampleCoord).x; @@ -139,8 +139,8 @@ vec4 pixelShaderGaussianDepthAware(vec2 texcoord, vec2 direction, vec2 pixelStep // Accumulate. srcBlurred += gaussianDistributionCurve[i] * srcSample; } -*/ + /* for(int i = 1; i < NUM_HALF_TAPS; i++) { // Fetch color and depth for current sample. vec2 texcoordOffset = (gaussianDistributionOffsetHalf[i] * finalStep); @@ -159,7 +159,7 @@ vec4 pixelShaderGaussianDepthAware(vec2 texcoord, vec2 direction, vec2 pixelStep // Accumulate. srcBlurred += gaussianDistributionCurveHalf[i] * (srcSampleP + srcSampleN); - } + }*/ return srcBlurred; }