Nonlinear optics of graphene and other 2D materials in layered structures