diff options
author | Michael Brasser <mbrasser@ford.com> | 2020-03-24 10:07:15 -0500 |
---|---|---|
committer | Michael Brasser <mbrasser@ford.com> | 2020-03-27 07:58:28 -0500 |
commit | adc9c3e6ab2cdda50cce6d2623959adf94f7845e (patch) | |
tree | dc1b798894522ac15a205d2d488414c050a9b141 | |
parent | 4d1ca5a7afc78ad4676d54ff168170bd7abe7273 (diff) |
Implement MetalRough shader optimization TODO
Calculate specularMipLevels only once, on the CPU.
Change-Id: I694e3d5e45ea369875ac3ecff36f885b3f669bcf
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
-rw-r--r-- | src/extras/shaders/es3/light.inc.frag | 1 | ||||
-rw-r--r-- | src/extras/shaders/es3/metalrough.inc.frag | 11 | ||||
-rw-r--r-- | src/extras/shaders/gl3/light.inc.frag | 1 | ||||
-rw-r--r-- | src/extras/shaders/gl3/metalrough.inc.frag | 11 | ||||
-rw-r--r-- | src/render/lights/qenvironmentlight.cpp | 5 |
5 files changed, 9 insertions, 20 deletions
diff --git a/src/extras/shaders/es3/light.inc.frag b/src/extras/shaders/es3/light.inc.frag index 18012ccc1..7b6bf3bc5 100644 --- a/src/extras/shaders/es3/light.inc.frag +++ b/src/extras/shaders/es3/light.inc.frag @@ -20,6 +20,7 @@ uniform int lightCount; struct EnvironmentLight { highp samplerCube irradiance; // For diffuse contribution highp samplerCube specular; // For specular contribution + int specularMipLevels; }; uniform EnvironmentLight envLight; uniform int envLightCount; diff --git a/src/extras/shaders/es3/metalrough.inc.frag b/src/extras/shaders/es3/metalrough.inc.frag index 188a367f5..fc014b81b 100644 --- a/src/extras/shaders/es3/metalrough.inc.frag +++ b/src/extras/shaders/es3/metalrough.inc.frag @@ -59,13 +59,6 @@ const FP float gamma = 2.2; #pragma include light.inc.frag -int mipLevelCount(const in FP samplerCube cube) -{ - int baseSize = textureSize(cube, 0).x; - int nMips = int(log2(float(baseSize > 0 ? baseSize : 1))) + 1; - return nMips; -} - FP float remapRoughness(const in FP float roughness) { // As per page 14 of @@ -91,10 +84,8 @@ FP float alphaToMipLevel(FP float alpha) const FP float k1 = 0.9921; FP float glossiness = (pow(2.0, -10.0 / sqrt(specPower)) - k0) / k1; - // TODO: Optimize by doing this on CPU and set as - // uniform int envLight.specularMipLevels say (if present in shader). // Lookup the number of mips in the specular envmap - int mipLevels = mipLevelCount(envLight.specular); + int mipLevels = envLight.specularMipLevels; // Offset of smallest miplevel we should use (corresponds to specular // power of 1). I.e. in the 32x32 sized mip. diff --git a/src/extras/shaders/gl3/light.inc.frag b/src/extras/shaders/gl3/light.inc.frag index 0b642638f..a1b07a976 100644 --- a/src/extras/shaders/gl3/light.inc.frag +++ b/src/extras/shaders/gl3/light.inc.frag @@ -20,6 +20,7 @@ uniform int lightCount; struct EnvironmentLight { samplerCube irradiance; // For diffuse contribution samplerCube specular; // For specular contribution + int specularMipLevels; }; uniform EnvironmentLight envLight; uniform int envLightCount = 0; diff --git a/src/extras/shaders/gl3/metalrough.inc.frag b/src/extras/shaders/gl3/metalrough.inc.frag index f7e3eecb7..f5fb81f51 100644 --- a/src/extras/shaders/gl3/metalrough.inc.frag +++ b/src/extras/shaders/gl3/metalrough.inc.frag @@ -55,13 +55,6 @@ uniform float gamma = 2.2; #pragma include light.inc.frag -int mipLevelCount(const in samplerCube cube) -{ - int baseSize = textureSize(cube, 0).x; - int nMips = int(log2(float(baseSize > 0 ? baseSize : 1))) + 1; - return nMips; -} - float remapRoughness(const in float roughness) { // As per page 14 of @@ -87,10 +80,8 @@ float alphaToMipLevel(float alpha) const float k1 = 0.9921; float glossiness = (pow(2.0, -10.0 / sqrt(specPower)) - k0) / k1; - // TODO: Optimize by doing this on CPU and set as - // uniform int envLight.specularMipLevels say (if present in shader). // Lookup the number of mips in the specular envmap - int mipLevels = mipLevelCount(envLight.specular); + int mipLevels = envLight.specularMipLevels; // Offset of smallest miplevel we should use (corresponds to specular // power of 1). I.e. in the 32x32 sized mip. diff --git a/src/render/lights/qenvironmentlight.cpp b/src/render/lights/qenvironmentlight.cpp index 977e117db..9510bda8f 100644 --- a/src/render/lights/qenvironmentlight.cpp +++ b/src/render/lights/qenvironmentlight.cpp @@ -42,6 +42,8 @@ #include "qabstracttexture.h" #include <QVector3D> +#include <cmath> + QT_BEGIN_NAMESPACE namespace Qt3DRender @@ -98,6 +100,9 @@ void QEnvironmentLightPrivate::_q_updateEnvMapsSize() m_specular->height(), m_specular->depth()); m_shaderData->setProperty("specularSize", QVariant::fromValue(specularSize)); + + const int levels = int(std::log2(specularSize.x() > 0.0f ? specularSize.x() : 1.0f)) + 1; + m_shaderData->setProperty("specularMipLevels", QVariant::fromValue(levels)); } Qt3DCore::QNodeCreatedChangeBasePtr QEnvironmentLight::createNodeCreationChange() const |