We will study sums of squares of regular functions on real algebraic curves and surfaces. Notion of a weakly factorial variety will be introduced and its usefulness in the study of Pythagoras number will be shown. We will then show that if X is a nonsingular rational real algebraic surface then the Pythagoras number of the ring of regular functions on X is bounded from above by 12.