问题
Is it possible to modify pixel coordinates in a fragment (pixel) shader using Cg? I'm sure such functionality became available in 2nd/3rd-generation shaders but I don't know what profiles exactly, or how to do it.
回答1:
No, it is not possible.
The only coordinate you can modify in a fragment shader is the Z, going into the Z-buffer. And even that has performance implications, as it defeats some optimizations (like Hierarchical Z).
the X and Y positions are set before the fragment shader is ever executed (in the Rasterizer). Typical rasterizers actually generate at the very least 2x2 chunks of pixels, and the hardware does not really handle pixels individually, all the way to the blending stage.
What some people tend to do to mimic that type of feature is to generate more pixels than necessary, and discard the extraneous pixels.
The feature you have heard about could be tesselation though. It is not done as part of the fragment shader, but is a separate part of the pipeline, that allow to generate additional geometry.
Ultimately, what technique you can use depends on what you're trying to achieve. But a blanket modification of X and Y has never been supported by the various APIs out there, be it in Cg, OpenGL or DirectX, because the hardware can't do it.
回答2:
You cannot modify the actual pixels of the bitmap in realtime, but you CAN displace the U/V coordinate mapping, which changes the point where each pixel is mapped.
Imagine the UV mapping as a red and green gradient. Red represents X position mapping while green is Y position mapping. If you were to multiply those coordinates by, say, perlin noise, the mapping would be offsetted, and therefore, so would the pixels.
来源:https://stackoverflow.com/questions/8123188/how-to-modify-displace-pixel-position-in-a-cg-fragment-shader