Understanding thermal quenching of photoluminescence from first principles