1 // Copyright 2018 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/buffer.h"
16
17 #include <utility>
18
19 #include "gtest/gtest.h"
20 #include "src/type_parser.h"
21
22 namespace amber {
23
24 using BufferTest = testing::Test;
25
TEST_F(BufferTest,EmptyByDefault)26 TEST_F(BufferTest, EmptyByDefault) {
27 Buffer b(BufferType::kColor);
28 EXPECT_EQ(static_cast<size_t>(0U), b.ElementCount());
29 EXPECT_EQ(static_cast<size_t>(0U), b.ValueCount());
30 EXPECT_EQ(static_cast<size_t>(0U), b.GetSizeInBytes());
31 }
32
TEST_F(BufferTest,Size)33 TEST_F(BufferTest, Size) {
34 TypeParser parser;
35 auto type = parser.Parse("R16_SINT");
36 Format fmt(type.get());
37
38 Buffer b(BufferType::kColor);
39 b.SetFormat(&fmt);
40 b.SetElementCount(10);
41 EXPECT_EQ(10, b.ElementCount());
42 EXPECT_EQ(10, b.ValueCount());
43 EXPECT_EQ(10 * sizeof(int16_t), b.GetSizeInBytes());
44 }
45
TEST_F(BufferTest,SizeFromData)46 TEST_F(BufferTest, SizeFromData) {
47 std::vector<Value> values;
48 values.resize(5);
49
50 TypeParser parser;
51 auto type = parser.Parse("R32_SFLOAT");
52 Format fmt(type.get());
53
54 Buffer b(BufferType::kColor);
55 b.SetFormat(&fmt);
56 b.SetData(std::move(values));
57
58 EXPECT_EQ(5, b.ElementCount());
59 EXPECT_EQ(5, b.ValueCount());
60 EXPECT_EQ(5 * sizeof(float), b.GetSizeInBytes());
61 }
62
TEST_F(BufferTest,SizeFromDataDoesNotOverrideSize)63 TEST_F(BufferTest, SizeFromDataDoesNotOverrideSize) {
64 std::vector<Value> values;
65 values.resize(5);
66
67 TypeParser parser;
68 auto type = parser.Parse("R32_SFLOAT");
69 Format fmt(type.get());
70
71 Buffer b(BufferType::kColor);
72 b.SetFormat(&fmt);
73 b.SetElementCount(20);
74 b.SetData(std::move(values));
75
76 EXPECT_EQ(20, b.ElementCount());
77 EXPECT_EQ(20, b.ValueCount());
78 EXPECT_EQ(20 * sizeof(float), b.GetSizeInBytes());
79 }
80
TEST_F(BufferTest,SizeMatrixStd430)81 TEST_F(BufferTest, SizeMatrixStd430) {
82 TypeParser parser;
83 auto type = parser.Parse("R16G16_SINT");
84 type->SetColumnCount(3);
85 Format fmt(type.get());
86
87 Buffer b(BufferType::kColor);
88 b.SetFormat(&fmt);
89 b.SetElementCount(10);
90
91 EXPECT_EQ(10, b.ElementCount());
92 EXPECT_EQ(60, b.ValueCount());
93 EXPECT_EQ(60 * sizeof(int16_t), b.GetSizeInBytes());
94 }
95
TEST_F(BufferTest,SizeMatrixStd140)96 TEST_F(BufferTest, SizeMatrixStd140) {
97 TypeParser parser;
98 auto type = parser.Parse("R16G16_SINT");
99 type->SetColumnCount(3);
100 Format fmt(type.get());
101 fmt.SetLayout(Format::Layout::kStd140);
102
103 Buffer b(BufferType::kColor);
104 b.SetFormat(&fmt);
105 b.SetElementCount(10);
106
107 EXPECT_EQ(10, b.ElementCount());
108 EXPECT_EQ(10 * 2 * 3, b.ValueCount());
109 EXPECT_EQ(120 * sizeof(int16_t), b.GetSizeInBytes());
110 }
111
TEST_F(BufferTest,SizeMatrixPaddedStd430)112 TEST_F(BufferTest, SizeMatrixPaddedStd430) {
113 TypeParser parser;
114 auto type = parser.Parse("R32G32B32_SINT");
115 type->SetColumnCount(3);
116 Format fmt(type.get());
117
118 Buffer b(BufferType::kColor);
119 b.SetFormat(&fmt);
120 b.SetValueCount(9);
121
122 EXPECT_EQ(1U, b.ElementCount());
123 EXPECT_EQ(9U, b.ValueCount());
124 EXPECT_EQ(12U * sizeof(int32_t), b.GetSizeInBytes());
125 }
126
127 } // namespace amber
128