aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.h
blob: d03a4a4e94584776a6a3e17807c29c6a6d49f16e (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
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2018 The Amber Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SRC_BUFFER_H_
#define SRC_BUFFER_H_

#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "amber/result.h"
#include "amber/value.h"
#include "src/buffer_data.h"
#include "src/datum_type.h"
#include "src/format.h"

namespace amber {

class DataBuffer;
class FormatBuffer;

/// A buffer stores data. The buffer maybe provided from the input script, or
/// maybe created as needed. A buffer must have a unique name.
class Buffer {
 public:
  /// Create a buffer of |type_|.
  explicit Buffer(BufferType type);

  virtual ~Buffer();

  /// Returns |true| if this is a buffer described by a |DatumType|.
  virtual bool IsDataBuffer() const { return false; }
  /// Returns |true| if this is a buffer described by a |Format|.
  virtual bool IsFormatBuffer() const { return false; }

  /// Converts the buffer to a |DataBuffer|. Note, |IsDataBuffer| must be true
  /// for this method to be used.
  DataBuffer* AsDataBuffer();
  /// Converts the buffer to a |FormatBuffer|. Note, |IsFormatBuffer| must be
  /// true for this method to be used.
  FormatBuffer* AsFormatBuffer();

  /// Returns the BufferType of this buffer.
  BufferType GetBufferType() const { return buffer_type_; }
  void SetBufferType(BufferType type) { buffer_type_ = type; }

  /// Set the location binding value for the buffer.
  void SetLocation(uint8_t loc) { location_ = loc; }
  /// Get the location binding value for the buffer.
  uint8_t GetLocation() const { return location_; }

  /// Sets the buffer |name|.
  void SetName(const std::string& name) { name_ = name; }
  /// Returns the name of the buffer.
  std::string GetName() const { return name_; }

  /// Sets the number of items in the buffer.
  void SetSize(uint32_t size) { size_ = size; }

  /// Returns the number of items in the buffer.
  uint32_t GetSize() const { return size_; }

  uint32_t GetWidth() const { return width_; }
  void SetWidth(uint32_t width) { width_ = width; }
  uint32_t GetHeight() const { return height_; }
  void SetHeight(uint32_t height) { height_ = height; }

  /// Returns the number of bytes needed for the data in the buffer.
  virtual uint32_t GetSizeInBytes() const { return size_; }

  /// Sets the data into the buffer. The size will also be updated to be the
  /// size of the data provided.
  virtual Result SetData(std::vector<Value>&& data) = 0;

  std::vector<uint8_t>* ValuePtr() { return &values_; }
  const std::vector<uint8_t>* ValuePtr() const { return &values_; }

  template <typename T>
  const T* GetValues() const {
    return reinterpret_cast<const T*>(values_.data());
  }

  /// Copy the buffer values to an other one
  Result CopyTo(Buffer* buffer) const;

 protected:
  /// Create an un-typed buffer.
  Buffer();

  std::vector<uint8_t> values_;

 private:
  BufferType buffer_type_ = BufferType::kUnknown;
  std::string name_;
  uint32_t size_ = 0;
  uint32_t width_ = 0;
  uint32_t height_ = 0;
  uint8_t location_ = 0;
};

/// A buffer class where the data is described by a |DatumType| object.
class DataBuffer : public Buffer {
 public:
  DataBuffer();
  explicit DataBuffer(BufferType type);
  ~DataBuffer() override;

  // Buffer
  bool IsDataBuffer() const override { return true; }
  uint32_t GetSizeInBytes() const override {
    return GetSize() * datum_type_.SizeInBytes();
  }
  Result SetData(std::vector<Value>&& data) override;

  /// Sets the DatumType of the buffer to |type|.
  void SetDatumType(const DatumType& type) { datum_type_ = type; }
  /// Returns the DatumType describing the buffer data.
  const DatumType& GetDatumType() const { return datum_type_; }

 private:
  Result CopyData(const std::vector<Value>& data);

  DatumType datum_type_;
};

/// A buffer class where the data is described by a |format| object.
class FormatBuffer : public Buffer {
 public:
  FormatBuffer();
  explicit FormatBuffer(BufferType type);
  ~FormatBuffer() override;

  // Buffer
  bool IsFormatBuffer() const override { return true; }
  uint32_t GetSizeInBytes() const override {
    return GetSize() * format_->GetByteSize();
  }
  Result SetData(std::vector<Value>&& data) override;

  /// Sets the Format of the buffer to |format|.
  void SetFormat(std::unique_ptr<Format> format) {
    format_ = std::move(format);
  }
  /// Returns the Format describing the buffer data.
  const Format& GetFormat() const { return *(format_.get()); }

  uint32_t GetTexelStride() { return format_->GetByteSize(); }

  // When copying the image to the host buffer, we specify a row length of 0
  // which results in tight packing of rows.  So the row stride is the product
  // of the texel stride and the number of texels in a row.
  uint32_t GetRowStride() { return GetTexelStride() * GetWidth(); }

 private:
  Result CopyData(const std::vector<Value>& data);

  std::unique_ptr<Format> format_;
};

}  // namespace amber

#endif  // SRC_BUFFER_H_