移植qt.5.12的metalrough材质到基于qt5.9.6的imx8开发板上

背景:qt5.12的qt3d模块 shader的管理发生了很大的变化,估计是为qt6作准备。

在5.9.6的版本中,metalrough材质在pc上是支持的,即opengl是支持的 但是opengl es是不支持的 glsl语言的的声明中后面根本就不带有es

1. 下载一个qt.5.12的代码 本文下载 qt-everywhere-src-5.12.10

2. 修改qt3d/tests/auto/render/shaderbuilder/tst_shaderbuilder.cpp

   void shouldHandleShaderCodeGeneration()
    {
        // GIVEN
        // Qt3DRender::Render::ShaderBuilder::setPrototypesFile(":/prototypes.json");
        // QVERIFY(!Qt3DRender::Render::ShaderBuilder::getPrototypeNames().isEmpty());

        // QFETCH(Qt3DRender::Render::ShaderBuilder::ShaderType, type);

        // const auto gl3Api = []{
        //     auto api = Qt3DRender::GraphicsApiFilterData();
        //     api.m_api = Qt3DRender::QGraphicsApiFilter::OpenGL;
        //     api.m_profile = Qt3DRender::QGraphicsApiFilter::CoreProfile;
        //     api.m_major = 3;
        //     api.m_minor = 2;
        //     return api;
        // }();

        // const auto es2Api = []{
        //     auto api = Qt3DRender::GraphicsApiFilterData();
        //     api.m_api = Qt3DRender::QGraphicsApiFilter::OpenGLES;
        //     api.m_major = 2;
        //     api.m_minor = 0;
        //     return api;
        // }();

        // const auto readCode = [](const QString &suffix) -> QString {
        //     const auto filePath = QStringLiteral(":/output.") + suffix;
        //     QFile file(filePath);
        //     if (!file.open(QFile::ReadOnly | QFile::Text))
        //         qFatal("File open failed: %s", qPrintable(filePath));
        //     return file.readAll();
        // };

        // const auto gl3Code = readCode("gl3");
        // const auto es2Code = readCode("es2");

        // Qt3DRender::Render::ShaderBuilder backend;

        // // WHEN
        // const auto graphUrl = QUrl::fromEncoded("qrc:/input.json");
        // backend.setShaderGraph(type, graphUrl);

        // // THEN
        // QCOMPARE(backend.shaderGraph(type), graphUrl);
        // QVERIFY(backend.isShaderCodeDirty(type));
        // QVERIFY(backend.shaderCode(type).isEmpty());

        // // WHEN
        // backend.setGraphicsApi(gl3Api);
        // backend.generateCode(type);

        // // THEN
        // QCOMPARE(backend.shaderGraph(type), graphUrl);
        // QVERIFY(!backend.isShaderCodeDirty(type));
        // QCOMPARE(backend.shaderCode(type), gl3Code);

        // // WHEN
        // backend.setGraphicsApi(es2Api);

        // // THEN
        // QCOMPARE(backend.shaderGraph(type), graphUrl);
        // QVERIFY(backend.isShaderCodeDirty(type));
        // QCOMPARE(backend.shaderCode(type), gl3Code);
        // qDebug()<<backend.shaderCode(type);

        // // WHEN
        // backend.generateCode(type);

        // // THEN
        // QCOMPARE(backend.shaderGraph(type), graphUrl);
        // QVERIFY(!backend.isShaderCodeDirty(type));
        // QCOMPARE(backend.shaderCode(type), es2Code);


        QFETCH(Qt3DRender::Render::ShaderBuilder::ShaderType, type);

        if(Qt3DRender::Render::ShaderBuilder::Fragment != type){
            qDebug()<<type;
            return;
        }

        const auto es3Api = []{
            auto api = Qt3DRender::GraphicsApiFilterData();
            api.m_api = Qt3DRender::QGraphicsApiFilter::OpenGLES;
            api.m_major = 3;
            api.m_minor = 0;
            return api;
        }();

        // const auto readCode = [](const QString &suffix) -> QString {
        //     const auto filePath = QStringLiteral(":/output.") + suffix;
        //     QFile file(filePath);
        //     if (!file.open(QFile::ReadOnly | QFile::Text))
        //         qFatal("File open failed: %s", qPrintable(filePath));
        //     return file.readAll();
        // };

        // const auto gl3Code = readCode("gl3");
        // const auto es2Code = readCode("es2");

        Qt3DRender::Render::ShaderBuilder backend;
        TestRenderer renderer;
        backend.setRenderer(&renderer);
        //Qt3DRender::Render::ShaderBuilder::ShaderType type = Qt3DRender::Render::ShaderBuilder::Fragment;

        // WHEN
        const auto graphUrl = QUrl::fromEncoded("qrc:/shaders/graphs/metalrough.frag.json");
        backend.setShaderGraph(type, graphUrl);

        // WHEN
        backend.setGraphicsApi(es3Api);

        // THEN
        QCOMPARE(backend.shaderGraph(type), graphUrl);

        const auto layers = QStringList() << "baseColorMap" << "metalnessMap" << "roughnessMap" << "ambientOcclusion" << "normalMap";

        auto updateChange = Qt3DCore::QPropertyUpdatedChangePtr::create(Qt3DCore::QNodeId());
        updateChange->setValue(layers);
        updateChange->setPropertyName("enabledLayers");
        backend.sceneChangeEvent(updateChange);

        QVERIFY(backend.isShaderCodeDirty(type));

        backend.generateCode(type);
        //Debug()<<backend.shaderCode(type);

        QFile file("out.txt");  
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))  
            return;  
          
        QTextStream out(&file); 
        out << backend.shaderCode(type);


    }

};

   注释 Qt3DRender::Render::ShaderBuilder::setPrototypesFile(":/prototypes.json");

    void shouldHaveGlobalDefaultPrototypes()
    {
        // GIVEN

        // THEN
        QCOMPARE(Qt3DRender::Render::ShaderBuilder::getPrototypesFile(), QStringLiteral(":/prototypes/default.json"));
        QVERIFY(!Qt3DRender::Render::ShaderBuilder::getPrototypeNames().isEmpty());

        // WHEN
        //Qt3DRender::Render::ShaderBuilder::setPrototypesFile(":/prototypes.json");

        // THEN
        QCOMPARE(Qt3DRender::Render::ShaderBuilder::getPrototypesFile(), QStringLiteral(":/prototypes.json"));
        auto prototypeNames = Qt3DRender::Render::ShaderBuilder::getPrototypeNames();
        prototypeNames.sort();
        const auto expectedPrototypeNames = QStringList() << "exposure"
                                                          << "exposureFunction"
                                                          << "fragColor"
                                                          << "lightIntensity"
                                                          << "lightModel"
                                                          << "sampleTexture"
                                                          << "texCoord"
                                                          << "texture"
                                                          << "worldPosition";
        QCOMPARE(prototypeNames, expectedPrototypeNames);
    }

3.编译 注意增加developer-build选项 编译tests模块

mkdir build
cd build
../configure -xcb -qt-libjpeg -qt-libpng -developer-build -qpa xcb -qt-xcb --prefix=/home/zhangey/qtrootfs/
make -j 8

4.运行

export LD_LIBRARY_PATH=/home/zhangey/qtrootfs/usr/local/Qt-5.12.10/lib/:/home/zhangey/qtrootfs/usr/local/Qt-5.12.10/plugins/platforms
export QT_QPA_PLATFORM_PLUGIN_PATH=/home/zhangey/qtrootfs/usr/local/Qt-5.12.10/plugins/
./tst_shaderbuilder

结果如下:

metalrough.frag:

#version 300 es

in vec2 texCoord;
in vec3 worldPosition;
in vec3 worldNormal;
in vec4 worldTangent;

out vec4 fragColor;

// Qt 3D built in uniforms
uniform vec3 eyePosition; // World space eye position
uniform float time; // Time in seconds

// PBR Material maps
uniform sampler2D baseColorMap;
uniform sampler2D metalnessMap;
uniform sampler2D roughnessMap;
uniform sampler2D normalMap;
uniform sampler2D ambientOcclusionMap;

// User control parameters
uniform float metalFactor = 1.0;

// Exposure correction
uniform float exposure = 0.0;
// Gamma correction
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
    // http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf
    // we remap the roughness to give a more perceptually linear response
    // of "bluriness" as a function of the roughness specified by the user.
    // r = roughness^2
    const float maxSpecPower = 999999.0;
    const float minRoughness = sqrt(2.0 / (maxSpecPower + 2));
    return max(roughness * roughness, minRoughness);
}

mat3 calcWorldSpaceToTangentSpaceMatrix(const in vec3 wNormal, const in vec4 wTangent)
{
    // Make the tangent truly orthogonal to the normal by using Gram-Schmidt.
    // This allows to build the tangentMatrix below by simply transposing the
    // tangent -> eyespace matrix (which would now be orthogonal)
    vec3 wFixedTangent = normalize(wTangent.xyz - dot(wTangent.xyz, wNormal) * wNormal);

    // Calculate binormal vector. No "real" need to renormalize it,
    // as built by crossing two normal vectors.
    // To orient the binormal correctly, use the fourth coordinate of the tangent,
    // which is +1 for a right hand system, and -1 for a left hand system.
    vec3 wBinormal = cross(wNormal, wFixedTangent.xyz) * wTangent.w;

    // Construct matrix to transform from world space to tangent space
    // This is the transpose of the tangentToWorld transformation matrix
    mat3 tangentToWorldMatrix = mat3(wFixedTangent, wBinormal, wNormal);
    mat3 worldToTangentMatrix = transpose(tangentToWorldMatrix);
    return worldToTangentMatrix;
}

float alphaToMipLevel(float alpha)
{
    float specPower = 2.0 / (alpha * alpha) - 2.0;

    // We use the mip level calculation from Lys' default power drop, which in
    // turn is a slight modification of that used in Marmoset Toolbag. See
    // https://docs.knaldtech.com/doku.php?id=specular_lys for details.
    // For now we assume a max specular power of 999999 which gives
    // maxGlossiness = 1.
    const float k0 = 0.00098;
    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);

    // Offset of smallest miplevel we should use (corresponds to specular
    // power of 1). I.e. in the 32x32 sized mip.
    const float mipOffset = 5.0;

    // The final factor is really 1 - g / g_max but as mentioned above g_max
    // is 1 by definition here so we can avoid the division. If we make the
    // max specular power for the spec map configurable, this will need to
    // be handled properly.
    float mipLevel = (mipLevels - 1.0 - mipOffset) * (1.0 - glossiness);
    return mipLevel;
}

float normalDistribution(const in vec3 n, const in vec3 h, const in float alpha)
{
    // Blinn-Phong approximation - see
    // http://graphicrants.blogspot.co.uk/2013/08/specular-brdf-reference.html
    float specPower = 2.0 / (alpha * alpha) - 2.0;
    return (specPower + 2.0) / (2.0 * 3.14159) * pow(max(dot(n, h), 0.0), specPower);
}

vec3 fresnelFactor(const in vec3 color, const in float cosineFactor)
{
    // Calculate the Fresnel effect value
    vec3 f = color;
    vec3 F = f + (1.0 - f) * pow(1.0 - cosineFactor, 5.0);
    return clamp(F, f, vec3(1.0));
}

float geometricModel(const in float lDotN,
                     const in float vDotN,
                     const in vec3 h)
{
    // Implicit geometric model (equal to denominator in specular model).
    // This currently assumes that there is no attenuation by geometric shadowing or
    // masking according to the microfacet theory.
    return lDotN * vDotN;
}

vec3 specularModel(const in vec3 F0,
                   const in float sDotH,
                   const in float sDotN,
                   const in float vDotN,
                   const in vec3 n,
                   const in vec3 h)
{
    // Clamp sDotN and vDotN to small positive value to prevent the
    // denominator in the reflection equation going to infinity. Balance this
    // by using the clamped values in the geometric factor function to
    // avoid ugly seams in the specular lighting.
    float sDotNPrime = max(sDotN, 0.001);
    float vDotNPrime = max(vDotN, 0.001);

    vec3 F = fresnelFactor(F0, sDotH);
    float G = geometricModel(sDotNPrime, vDotNPrime, h);

    vec3 cSpec = F * G / (4.0 * sDotNPrime * vDotNPrime);
    return clamp(cSpec, vec3(0.0), vec3(1.0));
}

vec3 pbrModel(const in int lightIndex,
              const in vec3 wPosition,
              const in vec3 wNormal,
              const in vec3 wView,
              const in vec3 baseColor,
              const in float metalness,
              const in float alpha,
              const in float ambientOcclusion)
{
    // Calculate some useful quantities
    vec3 n = wNormal;
    vec3 s = vec3(0.0);
    vec3 v = wView;
    vec3 h = vec3(0.0);

    float vDotN = dot(v, n);
    float sDotN = 0.0;
    float sDotH = 0.0;
    float att = 1.0;

    if (lights[lightIndex].type != TYPE_DIRECTIONAL) {
        // Point and Spot lights
        vec3 sUnnormalized = vec3(lights[lightIndex].position) - wPosition;
        s = normalize(sUnnormalized);

        // Calculate the attenuation factor
        sDotN = dot(s, n);
        if (sDotN > 0.0) {
            if (lights[lightIndex].constantAttenuation != 0.0
             || lights[lightIndex].linearAttenuation != 0.0
             || lights[lightIndex].quadraticAttenuation != 0.0) {
                float dist = length(sUnnormalized);
                att = 1.0 / (lights[lightIndex].constantAttenuation +
                             lights[lightIndex].linearAttenuation * dist +
                             lights[lightIndex].quadraticAttenuation * dist * dist);
            }

            // The light direction is in world space already
            if (lights[lightIndex].type == TYPE_SPOT) {
                // Check if fragment is inside or outside of the spot light cone
                if (degrees(acos(dot(-s, lights[lightIndex].direction))) > lights[lightIndex].cutOffAngle)
                    sDotN = 0.0;
            }
        }
    } else {
        // Directional lights
        // The light direction is in world space already
        s = normalize(-lights[lightIndex].direction);
        sDotN = dot(s, n);
    }

    h = normalize(s + v);
    sDotH = dot(s, h);

    // Calculate diffuse component
    vec3 diffuseColor = (1.0 - metalness) * baseColor;
    vec3 diffuse = diffuseColor * max(sDotN, 0.0) / 3.14159;

    // Calculate specular component
    vec3 dielectricColor = vec3(0.04);
    vec3 F0 = mix(dielectricColor, baseColor, metalness);
    vec3 specularFactor = vec3(0.0);
    if (sDotN > 0.0) {
        specularFactor = specularModel(F0, sDotH, sDotN, vDotN, n, h);
        specularFactor *= normalDistribution(n, h, alpha);
    }
    vec3 specularColor = lights[lightIndex].color;
    vec3 specular = specularColor * specularFactor;

    // Blend between diffuse and specular to conserver energy
    vec3 color = lights[lightIndex].intensity * (specular + diffuse * (vec3(1.0) - specular));

    // Reduce by ambient occlusion amount
    color *= ambientOcclusion;

    return color;
}

vec3 pbrIblModel(const in vec3 wNormal,
                 const in vec3 wView,
                 const in vec3 baseColor,
                 const in float metalness,
                 const in float alpha,
                 const in float ambientOcclusion)
{
    // Calculate reflection direction of view vector about surface normal
    // vector in world space. This is used in the fragment shader to sample
    // from the environment textures for a light source. This is equivalent
    // to the l vector for punctual light sources. Armed with this, calculate
    // the usual factors needed
    vec3 n = wNormal;
    vec3 l = reflect(-wView, n);
    vec3 v = wView;
    vec3 h = normalize(l + v);
    float vDotN = dot(v, n);
    float lDotN = dot(l, n);
    float lDotH = dot(l, h);

    // Calculate diffuse component
    vec3 diffuseColor = (1.0 - metalness) * baseColor;
    vec3 diffuse = diffuseColor * texture(envLight.irradiance, l).rgb;

    // Calculate specular component
    vec3 dielectricColor = vec3(0.04);
    vec3 F0 = mix(dielectricColor, baseColor, metalness);
    vec3 specularFactor = specularModel(F0, lDotH, lDotN, vDotN, n, h);

    float lod = alphaToMipLevel(alpha);
//#define DEBUG_SPECULAR_LODS
#ifdef DEBUG_SPECULAR_LODS
    if (lod > 7.0)
        return vec3(1.0, 0.0, 0.0);
    else if (lod > 6.0)
        return vec3(1.0, 0.333, 0.0);
    else if (lod > 5.0)
        return vec3(1.0, 1.0, 0.0);
    else if (lod > 4.0)
        return vec3(0.666, 1.0, 0.0);
    else if (lod > 3.0)
        return vec3(0.0, 1.0, 0.666);
    else if (lod > 2.0)
        return vec3(0.0, 0.666, 1.0);
    else if (lod > 1.0)
        return vec3(0.0, 0.0, 1.0);
    else if (lod > 0.0)
        return vec3(1.0, 0.0, 1.0);
#endif
    vec3 specularSkyColor = textureLod(envLight.specular, l, lod).rgb;
    vec3 specular = specularSkyColor * specularFactor;

    // Blend between diffuse and specular to conserve energy
    vec3 iblColor = specular + diffuse * (vec3(1.0) - specularFactor);

    // Reduce by ambient occlusion amount
    iblColor *= ambientOcclusion;

    return iblColor;
}

vec3 toneMap(const in vec3 c)
{
    return c / (c + vec3(1.0));
}

vec3 gammaCorrect(const in vec3 color)
{
    return pow(color, vec3(1.0 / gamma));
}

void main()
{
    vec3 cLinear = vec3(0.0);

    // Calculate the perturbed texture coordinates from parallax occlusion mapping
    mat3 worldToTangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent);
    vec3 wView = normalize(eyePosition - worldPosition);
    vec3 tView = worldToTangentMatrix * wView;

    // Sample the inputs needed for the metal-roughness PBR BRDF
    vec3 baseColor = texture(baseColorMap, texCoord).rgb;
    float metalness = texture(metalnessMap, texCoord).r * metalFactor;
    float roughness = texture(roughnessMap, texCoord).r;
    float ambientOcclusion = texture(ambientOcclusionMap, texCoord).r;
    vec3 tNormal = 2.0 * texture(normalMap, texCoord).rgb - vec3(1.0);
    vec3 wNormal = normalize(transpose(worldToTangentMatrix) * tNormal);

    // Remap roughness for a perceptually more linear correspondence
    float alpha = remapRoughness(roughness);

    for (int i = 0; i < envLightCount; ++i) {
        cLinear += pbrIblModel(wNormal,
                               wView,
                               baseColor,
                               metalness,
                               alpha,
                               ambientOcclusion);
    }

    for (int i = 0; i < lightCount; ++i) {
        cLinear += pbrModel(i,
                            worldPosition,
                            wNormal,
                            wView,
                            baseColor.rgb,
                            metalness,
                            alpha,
                            ambientOcclusion);
    }

    // Apply exposure correction
    cLinear *= pow(2.0, exposure);

    // Apply simple (Reinhard) tonemap transform to get into LDR range [0, 1]
    vec3 cToneMapped = toneMap(cLinear);

    // Apply gamma correction prior to display
    vec3 cGamma = gammaCorrect(cToneMapped);
    fragColor = vec4(cGamma, 1.0);
}

metalrough.vert

#version 150

in vec3 vertexPosition;
in vec3 vertexNormal;
in vec4 vertexTangent;
in vec2 vertexTexCoord;

out vec3 worldPosition;
out vec3 worldNormal;
out vec4 worldTangent;
out vec2 texCoord;

uniform mat4 modelMatrix;
uniform mat3 modelNormalMatrix;
uniform mat4 mvp;

void main()
{
    // Pass the texture coordinates through
    texCoord = vertexTexCoord;

    // Transform position, normal, and tangent to world space
    worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0));
    worldNormal = normalize(modelNormalMatrix * vertexNormal);
    worldTangent.xyz = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));
    worldTangent.w = vertexTangent.w;

    gl_Position = mvp * vec4(vertexPosition, 1.0);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值