aboutsummaryrefslogtreecommitdiff
path: root/src/buffer_test.cc
blob: 1868b9595183bc26a686df83d0504ac304d327f0 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// 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.

#include "src/buffer.h"

#include <limits>
#include <utility>

#include "gtest/gtest.h"
#include "src/float16_helper.h"
#include "src/type_parser.h"

namespace amber {

using BufferTest = testing::Test;

TEST_F(BufferTest, EmptyByDefault) {
  Buffer b;
  EXPECT_EQ(static_cast<size_t>(0U), b.ElementCount());
  EXPECT_EQ(static_cast<size_t>(0U), b.ValueCount());
  EXPECT_EQ(static_cast<size_t>(0U), b.GetSizeInBytes());
}

TEST_F(BufferTest, Size) {
  TypeParser parser;
  auto type = parser.Parse("R16_SINT");
  Format fmt(type.get());

  Buffer b;
  b.SetFormat(&fmt);
  b.SetElementCount(10);
  EXPECT_EQ(10u, b.ElementCount());
  EXPECT_EQ(10u, b.ValueCount());
  EXPECT_EQ(10u * sizeof(int16_t), b.GetSizeInBytes());
}

TEST_F(BufferTest, SizeFromData) {
  std::vector<Value> values;
  values.resize(5);

  TypeParser parser;
  auto type = parser.Parse("R32_SFLOAT");
  Format fmt(type.get());

  Buffer b;
  b.SetFormat(&fmt);
  b.SetData(std::move(values));

  EXPECT_EQ(5u, b.ElementCount());
  EXPECT_EQ(5u, b.ValueCount());
  EXPECT_EQ(5u * sizeof(float), b.GetSizeInBytes());
}

TEST_F(BufferTest, SizeFromDataDoesNotOverrideSize) {
  std::vector<Value> values;
  values.resize(5);

  TypeParser parser;
  auto type = parser.Parse("R32_SFLOAT");
  Format fmt(type.get());

  Buffer b;
  b.SetFormat(&fmt);
  b.SetElementCount(20);
  b.SetData(std::move(values));

  EXPECT_EQ(20u, b.ElementCount());
  EXPECT_EQ(20u, b.ValueCount());
  EXPECT_EQ(20u * sizeof(float), b.GetSizeInBytes());
}

TEST_F(BufferTest, SizeMatrixStd430) {
  TypeParser parser;
  auto type = parser.Parse("R16G16_SINT");
  type->SetColumnCount(3);
  Format fmt(type.get());

  Buffer b;
  b.SetFormat(&fmt);
  b.SetElementCount(10);

  EXPECT_EQ(10u, b.ElementCount());
  EXPECT_EQ(60u, b.ValueCount());
  EXPECT_EQ(60u * sizeof(int16_t), b.GetSizeInBytes());
}

TEST_F(BufferTest, SizeMatrixStd140) {
  TypeParser parser;
  auto type = parser.Parse("R16G16_SINT");
  type->SetColumnCount(3);
  Format fmt(type.get());
  fmt.SetLayout(Format::Layout::kStd140);

  Buffer b;
  b.SetFormat(&fmt);
  b.SetElementCount(10);

  EXPECT_EQ(10u, b.ElementCount());
  EXPECT_EQ(10u * 2u * 3u, b.ValueCount());
  EXPECT_EQ(120u * sizeof(int16_t), b.GetSizeInBytes());
}

TEST_F(BufferTest, SizeMatrixPaddedStd430) {
  TypeParser parser;
  auto type = parser.Parse("R32G32B32_SINT");
  type->SetColumnCount(3);
  Format fmt(type.get());

  Buffer b;
  b.SetFormat(&fmt);
  b.SetValueCount(9);

  EXPECT_EQ(1U, b.ElementCount());
  EXPECT_EQ(9U, b.ValueCount());
  EXPECT_EQ(12U * sizeof(int32_t), b.GetSizeInBytes());
}

// Creates 10 RGBA pixel values, with the blue channels ranging from 0 to 255,
// and checks that the bin for each blue channel value contains 1, as expected.
TEST_F(BufferTest, GetHistogramForChannelGradient) {
  TypeParser parser;
  auto type = parser.Parse("R8G8B8A8_UINT");
  Format fmt(type.get());

  // Creates 10 RBGA pixel values with the blue channels ranging from 0 to 255.
  // Every value gets multiplied by 25 to create a gradient
  std::vector<Value> values(40);
  for (uint32_t i = 0; i < values.size(); i += 4)
    values[i + 2].SetIntValue(i / 4 * 25);

  Buffer b;
  b.SetFormat(&fmt);
  b.SetData(values);

  std::vector<uint64_t> bins = b.GetHistogramForChannel(2, 256);
  for (uint32_t i = 0; i < values.size(); i += 4)
    EXPECT_EQ(1u, bins[i / 4 * 25]);
}

// Creates 10 RGBA pixel values, with all channels being 0, and checks that all
// channels have a count of 10 (all pixels) in the 0 bin.
TEST_F(BufferTest, GetHistogramForChannelAllBlack) {
  TypeParser parser;
  auto type = parser.Parse("R8G8B8A8_UINT");
  Format fmt(type.get());

  std::vector<Value> values(40);
  for (uint32_t i = 0; i < values.size(); i++)
    values[i].SetIntValue(0);

  Buffer b;
  b.SetFormat(&fmt);
  b.SetData(values);

  for (uint8_t i = 0; i < 4; i++) {
    std::vector<uint64_t> bins = b.GetHistogramForChannel(i, 256);
    for (uint32_t y = 0; y < values.size(); y++)
      EXPECT_EQ(10u, bins[0]);
  }
}

// Creates 10 RGBA pixel values, with all channels being the maximum value of 8
// bit uint, and checks that all channels have a count of 10 (all pixels) in the
// 255 (max uint8_t) bin.
TEST_F(BufferTest, GetHistogramForChannelAllWhite) {
  TypeParser parser;
  auto type = parser.Parse("R8G8B8A8_UINT");
  Format fmt(type.get());

  std::vector<Value> values(40);
  for (uint32_t i = 0; i < values.size(); i++)
    values[i].SetIntValue(std::numeric_limits<uint8_t>::max());

  Buffer b;
  b.SetFormat(&fmt);
  b.SetData(values);

  for (uint8_t i = 0; i < 4; i++) {
    std::vector<uint64_t> bins = b.GetHistogramForChannel(i, 256);
    for (uint32_t y = 0; y < values.size(); y++)
      EXPECT_EQ(10u, bins[255]);
  }
}

// Creates two sets of equal pixel values, except for one pixel that has +50 in
// its red channel. Compares the histograms to see if they are equal with a low
// threshold, which we expect to fail.
TEST_F(BufferTest, CompareHistogramEMDToleranceFalse) {
  TypeParser parser;
  auto type = parser.Parse("R8G8B8A8_UINT");
  Format fmt(type.get());

  // Every value gets multiplied by 25 to create a gradient
  std::vector<Value> values1(40);
  for (uint32_t i = 0; i < values1.size(); i += 4)
    values1[i].SetIntValue(i / 4 * 25);

  std::vector<Value> values2 = values1;
  values2[4].SetIntValue(values2[4].AsUint8() + 50);

  Buffer b1;
  b1.SetFormat(&fmt);
  b1.SetData(values1);

  Buffer b2;
  b2.SetFormat(&fmt);
  b2.SetData(values2);

  EXPECT_FALSE(b1.CompareHistogramEMD(&b2, 0.001f).IsSuccess());
}

// Creates two sets of equal pixel values, except for one pixel that has +50 in
// its red channel. Compares the histograms to see if they are equal with a high
// threshold, which we expect to succeed.
TEST_F(BufferTest, CompareHistogramEMDToleranceTrue) {
  TypeParser parser;
  auto type = parser.Parse("R8G8B8A8_UINT");
  Format fmt(type.get());

  // Every value gets multiplied by 25 to create a gradient
  std::vector<Value> values1(40);
  for (uint32_t i = 0; i < values1.size(); i += 4)
    values1[i].SetIntValue(i / 4 * 25);

  std::vector<Value> values2 = values1;
  values2[4].SetIntValue(values2[4].AsUint8() + 50);

  Buffer b1;
  b1.SetFormat(&fmt);
  b1.SetData(values1);

  Buffer b2;
  b2.SetFormat(&fmt);
  b2.SetData(values2);

  EXPECT_TRUE(b1.CompareHistogramEMD(&b2, 0.02f).IsSuccess());
}

// Creates two identical sets of RGBA pixel values and checks that the
// histograms are equal.
TEST_F(BufferTest, CompareHistogramEMDToleranceAllBlack) {
  TypeParser parser;
  auto type = parser.Parse("R8G8B8A8_UINT");
  Format fmt(type.get());

  std::vector<Value> values1(40);
  for (uint32_t i = 0; i < values1.size(); i++)
    values1[i].SetIntValue(0);

  std::vector<Value> values2 = values1;

  Buffer b1;
  b1.SetFormat(&fmt);
  b1.SetData(values1);

  Buffer b2;
  b2.SetFormat(&fmt);
  b2.SetData(values2);

  EXPECT_TRUE(b1.CompareHistogramEMD(&b2, 0.0f).IsSuccess());
}

// Creates two identical sets of RGBA pixel values and checks that the
// histograms are equal.
TEST_F(BufferTest, CompareHistogramEMDToleranceAllWhite) {
  TypeParser parser;
  auto type = parser.Parse("R8G8B8A8_UINT");
  Format fmt(type.get());

  std::vector<Value> values1(40);
  for (uint32_t i = 0; i < values1.size(); i++)
    values1[i].SetIntValue(std::numeric_limits<uint8_t>().max());

  std::vector<Value> values2 = values1;

  Buffer b1;
  b1.SetFormat(&fmt);
  b1.SetData(values1);

  Buffer b2;
  b2.SetFormat(&fmt);
  b2.SetData(values2);

  EXPECT_TRUE(b1.CompareHistogramEMD(&b2, 0.0f).IsSuccess());
}

TEST_F(BufferTest, SetFloat16) {
  std::vector<Value> values;
  values.resize(2);
  values[0].SetDoubleValue(2.8);
  values[1].SetDoubleValue(1234.567);

  TypeParser parser;
  auto type = parser.Parse("R16_SFLOAT");

  Format fmt(type.get());
  Buffer b;
  b.SetFormat(&fmt);
  b.SetData(std::move(values));

  EXPECT_EQ(2u, b.ElementCount());
  EXPECT_EQ(2u, b.ValueCount());
  EXPECT_EQ(4u, b.GetSizeInBytes());

  auto v = b.GetValues<uint16_t>();
  EXPECT_EQ(float16::FloatToHexFloat16(2.8f), v[0]);
  EXPECT_EQ(float16::FloatToHexFloat16(1234.567f), v[1]);
}

}  // namespace amber