In an homogeneous instrumentation (e.g. in a string ensemble) the top note is perceived as the melody. There are a lot of exceptions to that, let’s say melody in Cello/Bass and accompaniment in the upper strings. But then these two elements have to be separated, of course primarily by their range. There are several means to achieve that. It can can be done by letting the melody be more unpredictable and letting the accompaniment be very repetitive (or very inactive).