  We present a generalized formalism to describe the optical energy flow and
spatially resolved absorption in arbitrarily anisotropic layered structures.
The algorithm is capable of treating any number of layers of arbitrarily
anisotropic, birefringent, and absorbing media and is implemented in an open
access computer program. We derive explicit expressions for the transmitted and
absorbed power at any point in the multilayer structure, using the electric
field distribution from a 4 $\times$ 4 transfer matrix formalism. As a test
ground, we study three nanophotonic device structures featuring unique
layer-resolved absorption characteristics, making use of (i) in-plane
hyperbolic phonon polaritons, (ii) layer-selective, cavity-enhanced exciton
absorption in transition metal dichalcogenide monolayers, and (iii)
intersubband-cavity polaritons in quantum wells. Covering such a broad spectral
range from the far-infrared to the visible, the case studies demonstrate the
generality and wide applicability of our approach.
