Search code examples
c++halide

Output values to buffers of different sizes


I need to output 2 values to buffers of different sizes. One is the same size as the input, and the other one is half the width and half the height. I thought of using tuples, but I'm getting outside the bounds since it's iterating over the input size. Here's a quick example:

uint8_t _in[4] = { 1, 2, 3, 4 };
uint8_t _out1[4];
uint8_t _out2[1];

Buffer<uint8_t> in(_in, 2, 2);
Buffer<uint8_t> out1(_out1, 2, 2);
Buffer<uint8_t> out2(_out2, 1, 1);

Var x, y;
Func f;

f(x, y) = {
    in(x,y),
    in(x / 2, y / 2)
};

f.realize({ out1, out2 });

Are tuples not the right solution for this?


Solution

  • Construct a Halide::Pipeline using the two output Funcs and call realize on the Pipeline object.

    Something like so:

    uint8_t _in[4] = { 1, 2, 3, 4 };
    uint8_t _out1[4];
    uint8_t _out2[1];
    
    Buffer<uint8_t> in(_in, 2, 2);
    Buffer<uint8_t> out1(_out1, 2, 2);
    Buffer<uint8_t> out2(_out2, 1, 1);
    
    Var x, y;
    Func f1, f2;
    
    f1(x, y) = in(x,y);
    f2(x, y) = in(x / 2, y / 2);
    
    Pipeline pipeline({f1, f2});
    
    pipeline.realize({ out1, out2 });