From 42eaaf742d9ca358f7133fb886572392f707e99e Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 14 Jun 2016 18:29:06 -0700 Subject: [PATCH] Protoype done --- .../render-utils/src/DeferredLightingEffect.h | 2 +- .../render-utils/src/RenderDeferredTask.cpp | 3 + .../render-utils/src/SubsurfaceScattering.cpp | 11 ++- .../render-utils/src/SubsurfaceScattering.h | 8 +-- .../subsurfaceScattering_drawScattering.slf | 71 +++++++++---------- libraries/render/src/render/BlurTask.h | 2 +- libraries/render/src/render/Task.cpp | 8 +-- libraries/render/src/render/Task.h | 19 +++-- 8 files changed, 68 insertions(+), 56 deletions(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 6428cc5742..63d8f4d175 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -44,7 +44,7 @@ public: const glm::quat& orientation = glm::quat(), float exponent = 0.0f, float cutoff = PI); void prepare(RenderArgs* args); - void render(const render::RenderContextPointer& renderContext, ); + void render(const render::RenderContextPointer& renderContext); void setupKeyLightBatch(gpu::Batch& batch, int lightBufferUnit, int skyboxCubemapUnit); diff --git a/libraries/render-utils/src/RenderDeferredTask.cpp b/libraries/render-utils/src/RenderDeferredTask.cpp index dcdf45e0c9..fb505f83e9 100755 --- a/libraries/render-utils/src/RenderDeferredTask.cpp +++ b/libraries/render-utils/src/RenderDeferredTask.cpp @@ -113,6 +113,9 @@ RenderDeferredTask::RenderDeferredTask(CullFunctor cullFunctor) { // Opaque all rendered, generate surface geometry buffers const auto curvatureFramebufferAndDepth = addJob("SurfaceGeometry", deferredFrameTransform); + + const auto theCurvatureVarying = curvatureFramebufferAndDepth[0]; + #define SIMPLE_BLUR 1 #if SIMPLE_BLUR const auto curvatureFramebuffer = addJob("DiffuseCurvature", curvatureFramebufferAndDepth.get().first); diff --git a/libraries/render-utils/src/SubsurfaceScattering.cpp b/libraries/render-utils/src/SubsurfaceScattering.cpp index 5c728b7307..cab12e7377 100644 --- a/libraries/render-utils/src/SubsurfaceScattering.cpp +++ b/libraries/render-utils/src/SubsurfaceScattering.cpp @@ -34,7 +34,7 @@ enum ScatteringShaderMapSlots { ScatteringTask_AlbedoMapSlot, ScatteringTask_LinearMapSlot, - SCatteringTask_IBLMapSlot, + ScatteringTask_IBLMapSlot, }; @@ -84,6 +84,8 @@ gpu::PipelinePointer SubsurfaceScattering::getScatteringPipeline() { slotBindings.insert(gpu::Shader::Binding(std::string("albedoMap"), ScatteringTask_AlbedoMapSlot)); slotBindings.insert(gpu::Shader::Binding(std::string("linearDepthMap"), ScatteringTask_LinearMapSlot)); + slotBindings.insert(gpu::Shader::Binding(std::string("skyboxMap"), ScatteringTask_IBLMapSlot)); + gpu::Shader::makeProgram(*program, slotBindings); gpu::StatePointer state = gpu::StatePointer(new gpu::State()); @@ -194,8 +196,12 @@ void SubsurfaceScattering::run(const render::SceneContextPointer& sceneContext, batch.setUniformBuffer(ScatteringTask_FrameTransformSlot, frameTransform->getFrameTransformBuffer()); batch.setUniformBuffer(ScatteringTask_ParamSlot, _parametersBuffer); - if (theLight->light) + if (theLight->light) { batch.setUniformBuffer(ScatteringTask_LightSlot, theLight->light->getSchemaBuffer()); + if (theLight->light->getAmbientMap()) { + batch.setResourceTexture(ScatteringTask_IBLMapSlot, theLight->light->getAmbientMap()); + } + } batch.setResourceTexture(ScatteringTask_ScatteringTableSlot, _scatteringTable); batch.setResourceTexture(ScatteringTask_CurvatureMapSlot, curvatureFramebuffer->getRenderBuffer(0)); batch.setResourceTexture(ScatteringTask_DiffusedCurvatureMapSlot, diffusedFramebuffer->getRenderBuffer(0)); @@ -203,7 +209,6 @@ void SubsurfaceScattering::run(const render::SceneContextPointer& sceneContext, batch.setResourceTexture(ScatteringTask_AlbedoMapSlot, framebufferCache->getDeferredColorTexture()); batch.setResourceTexture(ScatteringTask_LinearMapSlot, framebufferCache->getDepthPyramidTexture()); - batch.draw(gpu::TRIANGLE_STRIP, 4); if (_showLUT) { diff --git a/libraries/render-utils/src/SubsurfaceScattering.h b/libraries/render-utils/src/SubsurfaceScattering.h index 818c18bcb9..61bccab48f 100644 --- a/libraries/render-utils/src/SubsurfaceScattering.h +++ b/libraries/render-utils/src/SubsurfaceScattering.h @@ -35,12 +35,12 @@ public: float bentRed{ 1.5f }; float bentGreen{ 0.8f }; float bentBlue{ 0.3f }; - float bentScale{ 1.0f }; + float bentScale{ 1.5f }; - float curvatureOffset{ 0.012f }; - float curvatureScale{ 0.25f }; + float curvatureOffset{ 0.08f }; + float curvatureScale{ 0.8f }; - bool showLUT{ true }; + bool showLUT{ false }; signals: void dirty(); diff --git a/libraries/render-utils/src/subsurfaceScattering_drawScattering.slf b/libraries/render-utils/src/subsurfaceScattering_drawScattering.slf index eac4da0709..154f5f0e8c 100644 --- a/libraries/render-utils/src/subsurfaceScattering_drawScattering.slf +++ b/libraries/render-utils/src/subsurfaceScattering_drawScattering.slf @@ -14,7 +14,9 @@ <$declareDeferredFrameTransform()$> -<@include model/Light.slh@> +<@include DeferredGlobalLight.slh@> + +<$declareEvalGlobalSpecularIrradiance(0, 1, 0)$> uniform sampler2D linearDepthMap; float getZEye(ivec2 pixel) { @@ -26,35 +28,6 @@ float getZEyeLinear(vec2 texcoord) { <@include DeferredBufferRead.slh@> - -vec3 fresnelSchlick(vec3 fresnelColor, vec3 lightDir, vec3 halfDir) { - return fresnelColor + (1.0 - fresnelColor) * pow(1.0 - clamp(dot(lightDir, halfDir), 0.0, 1.0), 5); -} - -float specularDistribution(float roughness, vec3 normal, vec3 halfDir) { - float ndoth = clamp(dot(halfDir, normal), 0.0, 1.0); - float gloss2 = pow(0.001 + roughness, 4); - float denom = (ndoth * ndoth*(gloss2 - 1) + 1); - float power = gloss2 / (3.14159 * denom * denom); - return power; -} - -// Frag Shading returns the diffuse amount as W and the specular rgb as xyz -vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, float metallic, vec3 fresnel, float roughness) { - // Diffuse Lighting - float diffuse = clamp(dot(fragNormal, fragLightDir), 0.0, 1.0); - - // Specular Lighting - vec3 halfDir = normalize(fragEyeDir + fragLightDir); - vec3 fresnelColor = fresnelSchlick(fresnel, fragLightDir,halfDir); - float power = specularDistribution(roughness, fragNormal, halfDir); - vec3 specular = power * fresnelColor * diffuse; - - return vec4(specular, (1.0 - metallic) * diffuse * (1 - fresnelColor.x)); -} - - - vec2 sideToFrameTexcoord(vec2 side, vec2 texcoordPos) { return vec2((texcoordPos.x + side.x) * side.y, texcoordPos.y); } @@ -132,10 +105,10 @@ void main(void) { // --> Calculate the light vector. Light light = getLight(); - vec3 lightVector = -getLightDirection(light); //normalize(uniformLightVector); //normalize(lightPos - sourcePos.xyz); + vec3 fragLightDir = -getLightDirection(light); //normalize(uniformLightVector); //normalize(lightPos - sourcePos.xyz); - // _fragColor = vec4(fetchBRDF(dot(bentNormalR, lightVector), abs(diffusedCurvature.w * 2 - 1)), 1.0); - // _fragColor = vec4(vec3(abs(dot(bentNormalR, lightVector))), 1.0); + // _fragColor = vec4(fetchBRDF(dot(bentNormalR, fragLightDir), abs(diffusedCurvature.w * 2 - 1)), 1.0); + // _fragColor = vec4(vec3(abs(dot(bentNormalR, fragLightDir))), 1.0); // _fragColor = vec4(vec3(varTexCoord0, 0.0), 1.0); // _fragColor = vec4(vec3(bentNormalR * 0.5 + 0.5), 1.0); @@ -147,7 +120,7 @@ void main(void) { vec3 gN = normalize(mix(bentNormalHigh, bentNormalLow, bendFactorSpectrum.y)); vec3 bN = normalize(mix(bentNormalHigh, bentNormalLow, bendFactorSpectrum.z)); - vec3 NdotLSpectrum = vec3(dot(rN, lightVector), dot(gN, lightVector), dot(bN, lightVector)); + vec3 NdotLSpectrum = vec3(dot(rN, fragLightDir), dot(gN, fragLightDir), dot(bN, fragLightDir)); // --> Look up the pre-integrated curvature-dependent BDRF textures vec3 bdrf = fetchBRDFSpectrum(NdotLSpectrum, curvature); @@ -174,11 +147,37 @@ void main(void) { fresnel = fragment.diffuse; fragment.metallic = 1.0; } + vec3 albedo = fragment.diffuse; + vec3 fragNormal = fragment.normal; - //vec4 shading = evalPBRShading(rS, lightVector, fragEyeDir, fragment.metallic, fresnel, fragment.roughness); - _fragColor = vec4(fragment.diffuse * vec3(bdrf.xyz), 1.0); + vec4 shading; + + { // Key Sun Lighting + // Diffuse Lighting + float diffuse = clamp(dot(fragNormal, fragLightDir), 0.0, 1.0); + + // Specular Lighting + vec3 halfDir = normalize(fragEyeDir + fragLightDir); + vec3 fresnelColor = fresnelSchlick(fresnel, fragLightDir,halfDir); + float power = specularDistribution(fragment.roughness, fragNormal, halfDir); + vec3 specular = power * fresnelColor * diffuse; + + shading = vec4(specular, (1.0 - fragment.metallic) * diffuse * (1 - fresnelColor.x)); + } + + vec3 color = vec3(albedo * vec3(bdrf.xyz) + shading.rgb) * getLightColor(light) * getLightIntensity(light); + // Diffuse from ambient + color += (1 - fragment.metallic) * albedo * evalSphericalLight(getLightAmbientSphere(light), bentNormalHigh).xyz * 1.0 * getLightAmbientIntensity(light); + + // Specular highlight from ambient + vec3 specularLighting = evalGlobalSpecularIrradiance(light, fragEyeDir, fragNormal, fragment.roughness, fresnel, 1.0); + color += specularLighting; + + //_fragColor = vec4(evalSkyboxLight(rS, 0.0).rgb, 1.0); + + _fragColor = vec4(color, 1.0); } diff --git a/libraries/render/src/render/BlurTask.h b/libraries/render/src/render/BlurTask.h index 1f3b1000d7..333cd22f8e 100644 --- a/libraries/render/src/render/BlurTask.h +++ b/libraries/render/src/render/BlurTask.h @@ -77,7 +77,7 @@ class BlurGaussianConfig : public Job::Config { Q_PROPERTY(float filterScale MEMBER filterScale NOTIFY dirty) // expose enabled flag public: - float filterScale{ 1.0f }; + float filterScale{ 1.2f }; signals : void dirty(); diff --git a/libraries/render/src/render/Task.cpp b/libraries/render/src/render/Task.cpp index 8727923c70..a180e7ff8e 100644 --- a/libraries/render/src/render/Task.cpp +++ b/libraries/render/src/render/Task.cpp @@ -27,14 +27,14 @@ void TaskConfig::refresh() { namespace render{ - template <> void varyingGet(const VaryingPairBase& data, uint8_t index, Varying& var) { + template <> void varyingGet(const VaryingPairBase* data, uint8_t index, Varying& var) { if (index == 0) { - var = data.first; + var = data->first; } else { - var = data.second; + var = data->second; } } - template <> uint8_t varyingLength(const VaryingPairBase& data) { return 2; } + template <> uint8_t varyingLength(const VaryingPairBase* data) { return 2; } } \ No newline at end of file diff --git a/libraries/render/src/render/Task.h b/libraries/render/src/render/Task.h index 5a67e68f20..947f1a3da0 100644 --- a/libraries/render/src/render/Task.h +++ b/libraries/render/src/render/Task.h @@ -31,8 +31,8 @@ namespace render { class Varying; - template void varyingGet(const T& data, uint8_t index, Varying& var) {} -template uint8_t varyingLength(const T& data) { return 0; } + template void varyingGet(const T* data, uint8_t index, Varying& var) {} +template uint8_t varyingLength(const T* data) { return 0; } // A varying piece of data, to be used as Job/Task I/O // TODO: Task IO @@ -65,6 +65,8 @@ protected: template class Model : public Concept { public: using Data = T; + // using VarContainer = std::enable_if; + //using VarContainer = std::conditional, T::VarContainer, T>; Model(const Data& data) : _data(data) {} virtual ~Model() = default; @@ -72,10 +74,10 @@ protected: virtual Varying operator[] (uint8_t index) const { Varying var; - varyingGet(_data, index, var); + // varyingGet(&_data, index, var); return var; } - virtual uint8_t length() const { return varyingLength(_data); } + virtual uint8_t length() const { return varyingLength(&_data); } Data _data; }; @@ -89,13 +91,14 @@ protected: using VaryingPairBase = std::pair; -template <> void varyingGet(const VaryingPairBase& data, uint8_t index, Varying& var); -template <> uint8_t varyingLength(const VaryingPairBase& data); +template <> void varyingGet(const VaryingPairBase* data, uint8_t index, Varying& var); +template <> uint8_t varyingLength(const VaryingPairBase* data); -template < class T0, class T1 > +template < typename T0, typename T1 > class VaryingPair : public VaryingPairBase { public: using Parent = VaryingPairBase; + using VarContainer = VaryingPairBase; VaryingPair() : Parent(Varying(T0()), Varying(T1())) {} VaryingPair(const VaryingPair& pair) : Parent(pair.first, pair.second) {} @@ -108,6 +111,7 @@ public: T1& editSecond() { return second.edit(); } }; + template class VaryingTrio : public std::tuple{ public: @@ -496,4 +500,5 @@ protected: } + #endif // hifi_render_Task_h