And I believe there were steps to flatten and I believe effectively sand the surface flat after some layers.
Would note that since FinFETs, deposition occurs from the side as well as top.
The Wikipedia article on CMOS has a very nice illustration of the basic steps of this process: https://en.wikipedia.org/wiki/CMOS#/media/File:CMOS_fabricat... Current processes are much more complex than even that.
For step 7 for example, you create a masking layer that way, after which you etch away only the exposed parts where source and drain contacts will end up.
For step 8, you do something similar. The openings you’ve etched in step 8 will allow only to dope the contacts, but you need to mask the nmos and pmos separately, otherwise you get the same doping in both transistors.
A lot of the ‘magic’ to make this work is in the materials and chemistry: you need selectivity: etching that only interacts with the exposed parts. In highly scaled finger nodes, the material combinations are very complex as you sometimes want 2, or 3 different material combinations to interact in the way you want, reliably, at nanometer scale.
For each layer of metal, they go through several steps of deposition of insulator, masking, etching, deposition of metal (often in several passes now), grinding or etching that, and then covering it all up with more insulator and grinding that layer down to produce a smooth surface for the next layer.
Once the chips are complete, 3D stacking is a packaging process. It involves grinding the backsides of the chips down until they are very thin and attaching the dies together using vias that run through the thin remaining silicon layer.
EDIT: Flash memory today has multiple stacked doped silicon layers, but it is a special process that is largely unsuitable for logic.
So how are the multiple layers done, if light is shined on the surface, how do you reach the other layers? And how is this doping done, you also need to choose what element goes where but this couldn't be done with light and a mask I'd think (so even if single layer I wonder how this works)? And how do you reach the other layers with those chemical elements for doping?