Search code examples
halide

Halide: Copying a block of memory to an overlapping location (same image)


I need to move a region from a texture to another location. If the two blocks don't overlap, there's not problem there. I know Halide is the right solution but I can't figure out how to wait for a read before writing to an overlapping pixel... I could iterate one way or the other depending on the direction of the move, but I couldn't find a way to express that in Halide. Is Halide able to understand these subtleties?


Solution

  • The way to iterate in the reverse direction is to invert an RDom:

    RDom range(0, width);
    f(width - range.x) = g(width - range.x); // Copy value going from higher addresses to lower.
    

    (Providing syntactic sugar for this has been on the todo list for a while. I think we've talked about scheduling directives for reversing loops as well. In that case, you'd use specialize to decide which direction handles the overlap correctly and dispatch to the appropriate schedule. At present however, the RDom subtracted from the extent method is probably the only option.)