Optical waveform processing and routing with structured surface gratings