I don't have the numbers for SF specifically, but I doubt they differ that much from LA. The numbers in LA are that 27% of homeless people have a substance abuse issue and 25% have a severe mental illness[1]. Those are not mutually exclusive, so there is some overlap in those groups. It means that roughly 50% of homeless people are mentally well when they become homeless and potentially much more than 50% as these numbers don't represent any causal information. Many people might become homeless for other reasons and the difficulty of homelessness triggers a mental health crisis or pushes them towards drugs as a coping mechanism.
This idea that drugs cause most homeless is largely just confirmation bias. You likely cross paths with many homeless people that you don't realize are homeless. It is the drug addicts and the mentally ill that stand out as obviously homeless. The person who is out on the street because they couldn't make rent, probably isn't noticeably homeless if they spend their nights in a shelter or sleep in a car before showering at a local gym.
[1] - https://siepr.stanford.edu/publications/policy-brief/homeles...