Welcome to mirror list, hosted at ThFree Co, Russian Federation.

COM_BuffersIterator.h « intern « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58dbd9c6f59ab7664552d252de7e2c2a336c383f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright 2021, Blender Foundation.
 */

#pragma once

#include "BLI_rect.h"
#include "BLI_vector.hh"

namespace blender::compositor {

/**
 * Builds an iterator for simultaneously iterating an area of elements in an output buffer and any
 * number of input buffers. It's not a standard C++ iterator and it does not support neither
 * deference, equality or postfix increment operators.
 */
template<typename T> class BuffersIteratorBuilder {
 public:
  class Iterator {
    const T *out_end_;
    const T *out_row_end_;
    int out_elem_stride_;
    int out_row_stride_;
    /* Stride between an output row end and the next row start. */
    int out_rows_gap_;

    struct In {
      int elem_stride;
      int rows_gap;
      const T *in;
    };
    Vector<In, 6> ins_;

    friend class BuffersIteratorBuilder;

   public:
    /**
     * Current output element.
     */
    T *out;

   public:
    /**
     * Get current element from an input.
     */
    const T *in(int input_index) const
    {
      BLI_assert(input_index < ins_.size());
      return ins_[input_index].in;
    }

    int get_num_inputs() const
    {
      return ins_.size();
    }

    /**
     * Has the end of the area been reached.
     */
    bool is_end() const
    {
      return out >= out_end_;
    }

    /**
     * Go to the next element in the area.
     */
    void next()
    {
      out += out_elem_stride_;
      for (In &in : ins_) {
        in.in += in.elem_stride;
      }
      if (out == out_row_end_) {
        out += out_rows_gap_;
        out_row_end_ += out_row_stride_;
        for (In &in : ins_) {
          in.in += in.rows_gap;
        }
      }
    }

    Iterator &operator++()
    {
      this->next();
      return *this;
    }
  };

 private:
  Iterator iterator_;
  rcti area_;
  bool is_built_;

 public:
  /**
   * Create a buffers iterator builder to iterate given output buffer area.
   * \param output: Output buffer.
   * \param buffer_width: Number of elements in an output buffer row.
   * \param area: Rectangle area to be iterated in all buffers.
   * \param elem_stride: Output buffer element stride.
   */
  BuffersIteratorBuilder(T *output, int buffer_width, const rcti &area, int elem_stride = 1)
      : area_(area), is_built_(false)
  {
    iterator_.out_elem_stride_ = elem_stride;
    iterator_.out_row_stride_ = buffer_width * elem_stride;
    iterator_.out_rows_gap_ = iterator_.out_row_stride_ - BLI_rcti_size_x(&area) * elem_stride;
    iterator_.out = output + (intptr_t)area.ymin * iterator_.out_row_stride_ +
                    (intptr_t)area.xmin * elem_stride;
    iterator_.out_row_end_ = iterator_.out + (intptr_t)BLI_rcti_size_x(&area) * elem_stride;
    iterator_.out_end_ = iterator_.out_row_end_ +
                         (intptr_t)iterator_.out_row_stride_ * (BLI_rcti_size_y(&area) - 1);
  }

  /**
   * Create a buffers iterator builder to iterate given output buffer with no offsets.
   */
  BuffersIteratorBuilder(T *output, int buffer_width, int buffer_height, int elem_stride = 1)
      : BuffersIteratorBuilder(
            output, buffer_width, {0, buffer_width, 0, buffer_height}, elem_stride)
  {
  }

  /**
   * Add an input buffer to be iterated. Its coordinates must be correlated with the output.
   */
  void add_input(const T *input, int buffer_width, int elem_stride = 1)
  {
    BLI_assert(!is_built_);
    typename Iterator::In in;
    in.elem_stride = elem_stride;
    in.rows_gap = buffer_width * elem_stride - BLI_rcti_size_x(&area_) * elem_stride;
    in.in = input + area_.ymin * buffer_width * elem_stride + area_.xmin * elem_stride;
    iterator_.ins_.append(std::move(in));
  }

  /**
   * Build the iterator.
   */
  BuffersIteratorBuilder::Iterator build()
  {
    is_built_ = true;
    return iterator_;
  }
};

template<typename T> using BuffersIterator = typename BuffersIteratorBuilder<T>::Iterator;

}  // namespace blender::compositor