首页 > 解决方案 > HLSL:TextureCube.SampleGrad() 需要哪个 DDX DDY

问题描述

我想知道 SampleGrad() 函数对 TextureCube 对象期望哪个 DDX DDY 值。我知道这是 2D 纹理的 UV 坐标的变化。所以我想,在这种情况下,这将是方向的变化。然而,情况似乎并非如此。

如果我尝试使用 Sample 函数与 SampleGrad,我会得到不同的结果:


样本:

// calculate reflected ray
float3 reflRay = reflect(-viewDir, normal);
// reflection map lookup
return reflectionMap.Sample(linearSampler, reflRay);

正确的


样品毕业:

// calculate reflected ray
float3 reflRay = reflect(-viewDir, normal);
// reflection map lookup
float3 dxr = ddx(reflRay);
float3 dyr = ddy(reflRay);
return reflectionMap.SampleGrad(linearSampler, reflRay, dxr, dyr);

错误的

标签: directxhlsl

解决方案


我仍然不知道 DDX 和 DDY 的哪些值是必需的,但如果找到了一个可接受的解决方法来计算我的渐变的详细程度。不幸的是,该解决方案的质量不如具有各向异性过滤的真实样本函数。

如果有人需要它:计算描述在:https ://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#LODCalculation

我的 HLSL 实现:

// calculate reflected ray
float3 reflRay = reflect(-viewDir, normal);
// reflection map lookup
float3 dxr = ddx(reflRay);
float3 dyr = ddy(reflRay);
// cubemap size for lod computation
float reflWidth, reflHeight;
reflectionMap.GetDimensions(reflWidth, reflHeight);
// calculate lod based on raydiffs
float lod = calcLod(getCubeDiff(reflRay, dxr).xy * reflWidth, getCubeDiff(reflRay, dyr).xy * reflHeight);
return reflectionMap.SampleLevel(linearSampler, reflRay, lod).rgb;

辅助功能:

float pow2(float x) {
    return x * x;
}

// calculates texture coordinates [-1, 1] for the view direction (xy values must be divided by axisMajorValue for proper [-1, 1] range).else
// z coordinate is the faceId
float3 getCubeCoord(float3 viewDir, out float axisMajorValue)
{
    // according to dx spec: https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#PointSampling
    // Choose the largest magnitude component of the input vector. Call this magnitude of this value AxisMajor. In the case of a tie, the following precedence should occur: Z, Y, X. 
    int axisMajor = 0;
    int axisFlip = 0;
    axisMajorValue = 0.0;
    [unroll] for (int i = 0; i < 3; ++i)
    {
        if (abs(viewDir[i]) >= axisMajorValue)
        {
            axisMajor = i;
            axisFlip = viewDir[i] < 0.0f ? 1 : 0;
            axisMajorValue = abs(viewDir[i]);
        }
    }

    int faceId = axisMajor * 2 + axisFlip;

    // Select and mirror the minor axes as defined by the TextureCube coordinate space. Call this new 2d coordinate Position.

    int axisMinor1 = axisMajor == 0 ? 2 : 0; // first coord is x or z
    int axisMinor2 = 3 - axisMajor - axisMinor1;
    // Project the coordinate onto the cube by dividing the components Position by AxisMajor. 
    //float u = viewDir[axisMinor1] / axisMajorValue;
    //float v = -viewDir[axisMinor2] / axisMajorValue;
    // don't project for getCubeDiff function!
    float u = viewDir[axisMinor1];
    float v = -viewDir[axisMinor2];

    switch (faceId)
    {
        case 0:
        case 5:
            u *= -1.0f;
            break;
        case 2:
            v *= -1.0f;
            break;
    }

    return float3(u, v, float(faceId));
}

float3 getCubeDiff(float3 ray, float3 diff)
{
    // from: https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#LODCalculation
    // Using TC, determine which component is of the largest magnitude, as when calculating the texel location. If any of the components are equivalent, precedence is as follows: Z, Y, X. The absolute value of this will be referred to as AxisMajor. 
    // select and mirror the minor axes of TC as defined by the TextureCube coordinate space to generate TC'.uv 
    float axisMajor;
    float3 tuv = getCubeCoord(ray, axisMajor);
    // select and mirror the minor axes of the partial derivative vectors as defined by the TextureCube coordinate space, generating 2 new partial derivative vectors dX'.uv & dY'.uv. 
    float derivateMajor;
    float3 duv = getCubeCoord(diff, derivateMajor);
    // Calculate 2 new dX and dY vectors for future calculations as follows:
    // dX.uv = (AxisMajor*dX'.uv - TC'.uv*DerivativeMajorX)/(AxisMajor*AxisMajor)
    float3 res;
    res.z = 0.0;
    res.xy = (axisMajor * duv.xy - tuv.xy * derivateMajor) / (axisMajor * axisMajor);

    return res * 0.5;
}

// dx, dy in pixel coordinates
float calcLod(float2 dX, float2 dY)
{
    // from: https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#LODCalculation
    float A = pow2(dX.y) + pow2(dY.y);
    float B = -2.0 * (dX.x * dX.y + dY.x * dY.y);
    float C = pow2(dX.x) + pow2(dY.x);
    float F = pow2(dX.x * dY.y - dY.x * dX.y);

    float p = A - C;
    float q = A + C;
    float t = sqrt(pow2(p) + pow2(B));

    float lengthX = sqrt(abs(F * (t+p) / ( t * (q+t))) + abs(F * (t-p) / ( t * (q+t))));
    float lengthY = sqrt(abs(F * (t-p) / ( t * (q-t))) + abs(F * (t+p) / ( t * (q-t))));

    return log2(max(lengthX,lengthY));
}

推荐阅读