Robin Williams checked himself into rehab in a desperate bid to win a three-year battle with booze that’s threatening to destroy his marriage, The ENQUIRER has learned exclusively.

The Oscar-winning actor’s drinking was so out of control that he suffered repeated blackouts, sources say, and after he was photographed hugging a waitress in a bar on May 22, his wife Marsha slapped him with an ultimatum: Get help or I’m leaving.

To save himself and his 17-year marriage, Williams entered a 30-day program at the Hazelden Springbrook alcohol rehab facility in Newberg, Ore., on July 11.

“My drink of choice was vodka,” he told a pal.

“I would buy two pints at a time, one for each jacket pocket. But I had to be careful. I didn’t want to be seen buying liquor every day.”

Williams told a souce that he hatched a plan to buy booze only at liquor stores owned by Asians, figuring he wouldn’t be recognized. He was wrong.

The first time he went into an Asian-owned liquor store, the proprietor said: “Robin Williams, how are you? You buy liquor?”

All the details are in the new issue of the Enquirer.