A very different, but similarly insightful, view of boyhood from the one depicted by Richard Linklater in his masterful twelve-years-in-the-making film is offered by Barry Jenkins in “Moonlight,” which follows the life of an African-American boy from adolescence to manhood. The fact that along the way he recognizes he is gay adds a further dimension to his story.

The film is told in three chapters. In the first, “Little,” young Chiron (Alex Hibbert), a fatherless Miami kid, is being raised by his single mother Paula (Naomie Harris), who’s emotionally (and sometimes physically) abusive, especially during her bouts of drug and alcohol use. Bullied, with only a solitary pal—a kid named Kevin (Jaden Piner)—he’s befriended by drug-dealer Juan (Mahershala Ali), who becomes a sort of surrogate father to him and, along with his girlfriend Teresa (Janelle Monae), give refuge to the boy when he can’t stand being at home. Chiron’s relationship with Juan suffers a setback, however, when he realizes that the man is the source of his mother’s drugs.

In the second chapter, “Chiron,” the boy has become a teen (Ashton Sanders), tormented at school by classmate Terrel (Patrick Decile). Though he sometimes still flees to Teresa, among his peers only Kevin (Jharrel Jerome) offers any friendship—and one night after sharing a joint on the beach, they enjoy a moment of intimacy as well. Terrel intervenes, however, forcing Kevin to pummel Chiron in some sort of juvenile ritual. Chiron refuses to squeal on his tormentors but decides to take vengeance on Terrel himself, in a fashion that leads to his arrest.

“Black,” the final part of the trilogy, finds Chiron (Trevante Rhodes) not just grown up but physically very different from his formerly thin, gangly self. Now well-muscled, he’s a drug-dealer in Atlanta, where he visits Paula in a treatment facility. His inner vulnerability, however, is revealed when he gets an unexpected call from Kevin (Andre Holland), who’s working as a cook at a Miami diner. He drives to Florida, where he and Kevin reunite—at first awkwardly at the diner, and later more passionately at Kevin’s apartment.

The initial two chapters of “Moonlight” are absolutely extraordinary—written and directed by Jenkins with uncommon precision, acted with stark naturalism and shot, by James Laxton, with a gritty realism that can morph into luxurious sensuality when Chiron makes contact with Kevin. The film presents a starkly revealing portrait of a boy coming of age in an environment that’s largely unforgiving, but nonetheless has touches of compassion, kindness and love.

It’s the third chapter that comes off less successfully. After the bracing quality of the first two, it’s presented as solace, a promise that Chiron might finally find the emotional connection he’s been searching for all his life. That’s certainly an entirely appropriate way to end his story. It’s definitely at odds with the bleakness that’s preceded it, however, and more importantly feels somewhat off in stylistic terms. The episode is stagey and talky rather than dramatic, and in terms of visuals it serves as a dreamily hypnotic counterpoint to the earlier realism. One must assume that the effect is intentional, but it doesn’t quite work. Still, one has to admire the commitment that Rhodes and Holland bring to what is essentially a two-person dialogue.

However one feels about the third chapter, in any event, as a whole “Moonlight” tells a compelling story, and does so in a way that can’t help but affect you whatever your race, sexual identity or economic status. It’s a searing coming-of-age tale, one that goes well beyond any genre boundaries to resonate in a universal way.