I've been attempting to translate my shaders from glsl to hlsl, but having a problem getting the directional light shadow working right in hlsl

I’ve been attempting to translate my shaders from glsl to hlsl, but having a problem getting the directional light shadow working right in hlsl.

I’ve tried using spirv-cross.exe to translate the spv file of the working glsl file to hlsl and it still didn’t work.
Translated the spv of the working glsl back into glsl and it works.
Confirmed the output values are the same in HLSL and GLSL.

I’m pretty much out of ideas where the problem could be:
cpp:
Directional light update logic which goes to DirectionalLightBuffer in the shader.

void DirectionalLight::Update()
{
	ProjectionMatrix = glm::ortho(LeftRight.x, LeftRight.y, TopBottom.x, TopBottom.y, NearFar.x, NearFar.y);
	ProjectionMatrix[1][1] *= -1;

	ViewMatrix = glm::lookAt(LightBuffer.UniformDataInfo.direction, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));

	LightBuffer.UniformDataInfo.LightSpaceMatrix = ProjectionMatrix * ViewMatrix;

	Light::Update();
}

Directional light code in hlsl

float3 DirectionalLightCalc(float3 V, float3 N, float3 F0, float3 Pos, MaterialProperties material)
{
    float3 Lo = float3(0.0.rrr);
    for (int x = 0; x < sceneDataProperties.DirectionalLightCount; x++)
    {
        float3 L = normalize(-DirectionalLightBuffer[x].direction);
        float3 H = normalize(V + L);
        float watts = DirectionalLightBuffer[x].intensity;
        float3 radiance = DirectionalLightBuffer[x].diffuse * watts;

        float NDF = DistributionGGX(N, H, material.Roughness);
        float G = GeometrySmith(N, V, L, material.Roughness);
        float3 F = fresnelSchlickRoughness(max(dot(H, V), 0.0), F0, material.Roughness);

        float3 numerator = NDF * G * F;
        float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
        float3 specular = numerator / denominator;

        float3 kS = F;
        float3 kD = float3(1.0.rrr) - kS;
        kD *= 1.0 - material.Metallic;

        float NdotL = max(dot(N, L), 0.0);
        
        float4 LightSpace = mul(float4(Pos, 1.0), mul(MeshPropertiesBuffer[sceneDataProperties.MeshIndex].MeshTransform, mul(transpose(DirectionalLightBuffer[0].LightSpaceMatrix), LightBiasMatrix)));
        float shadow = filterPCF(LightSpace / LightSpace.w, x);

        Lo += (kD * material.Albedo.rgb / PI + specular) * radiance * NdotL;
        Lo *= shadow;
    }
    return Lo;
}

Working Glsl version of the light shader:

vec3 CalcDirectionalLight(vec3 F0, vec3 V, vec3 N, MaterialProperties material)
{
    vec3 Lo = vec3(0.0);
    for (int x = 0; x < sceneData.DirectionalLightCount; x++)
    {
        vec3 L = normalize(-DLight[x].directionalLight.direction);
        vec3 H = normalize(V + L);
        float watts = DLight[x].directionalLight.intensity;
        vec3 radiance = DLight[x].directionalLight.diffuse * watts;

        float NDF = DistributionGGX(N, H, material.Roughness);
        float G = GeometrySmith(N, V, L, material.Roughness);
        vec3 F = fresnelSchlickRoughness(max(dot(H, V), 0.0), F0, material.Roughness);

        vec3 numerator = NDF * G * F;
        float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
        vec3 specular = numerator / denominator;

        vec3 kS = F;
        vec3 kD = vec3(1.0) - kS;
        kD *= 1.0 - material.Metallic;

        float NdotL = max(dot(N, L), 0.0);

        vec4 LightSpace = (LightBiasMatrix * DLight[x].directionalLight.LightSpaceMatrix * meshBuffer[sceneData.MeshIndex].meshProperties.MeshTransform) * vec4(FragPos, 1.0);
        float shadow = filterPCF(LightSpace / LightSpace.w, x);


        Lo += (kD * material.Albedo / PI + specular) * radiance * NdotL;
    }
    return Lo;
}

Don’t mind the other lighting differences between the 2 versions the glsl version is a older version.
HLSL

GLSL

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.