1 // Copyright 2019 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/vulkan/vertex_buffer.h"
16
17 #include <utility>
18
19 #include "amber/value.h"
20 #include "gtest/gtest.h"
21 #include "src/format.h"
22 #include "src/make_unique.h"
23 #include "src/type_parser.h"
24 #include "src/vulkan/transfer_buffer.h"
25
26 namespace amber {
27 namespace vulkan {
28 namespace {
29
30 class BufferForTest : public TransferBuffer {
31 public:
BufferForTest(Device * device,uint32_t size_in_bytes)32 BufferForTest(Device* device, uint32_t size_in_bytes)
33 : TransferBuffer(device, size_in_bytes) {
34 memory_.resize(4096);
35 SetMemoryPtr(memory_.data());
36 }
37 ~BufferForTest() override = default;
38
CopyToDevice(CommandBuffer *)39 void CopyToDevice(CommandBuffer*) override {}
40
41 private:
42 std::vector<uint8_t> memory_;
43 };
44
45 class VertexBufferTest : public testing::Test {
46 public:
VertexBufferTest()47 VertexBufferTest() {
48 vertex_buffer_ = MakeUnique<VertexBuffer>(nullptr);
49
50 std::unique_ptr<TransferBuffer> buffer =
51 MakeUnique<BufferForTest>(nullptr, 0U);
52 buffer_memory_ = buffer->HostAccessibleMemoryPtr();
53 vertex_buffer_->SetBufferForTest(std::move(buffer));
54 }
55
56 ~VertexBufferTest() = default;
57
SetIntData(uint8_t location,Format * format,std::vector<Value> values)58 Result SetIntData(uint8_t location,
59 Format* format,
60 std::vector<Value> values) {
61 auto buffer = MakeUnique<Buffer>();
62 buffer->SetFormat(format);
63 buffer->SetData(std::move(values));
64
65 vertex_buffer_->SetData(location, buffer.get());
66 return vertex_buffer_->SendVertexData(nullptr);
67 }
68
SetDoubleData(uint8_t location,Format * format,std::vector<Value> values)69 Result SetDoubleData(uint8_t location,
70 Format* format,
71 std::vector<Value> values) {
72 auto buffer = MakeUnique<Buffer>();
73 buffer->SetFormat(format);
74 buffer->SetData(std::move(values));
75
76 vertex_buffer_->SetData(location, buffer.get());
77 return vertex_buffer_->SendVertexData(nullptr);
78 }
79
GetVkBufferPtr() const80 const void* GetVkBufferPtr() const { return buffer_memory_; }
81
82 private:
83 std::unique_ptr<VertexBuffer> vertex_buffer_;
84 const void* buffer_memory_ = nullptr;
85 };
86
87 } // namespace
88
SetBufferForTest(std::unique_ptr<TransferBuffer> buffer)89 void VertexBuffer::SetBufferForTest(std::unique_ptr<TransferBuffer> buffer) {
90 transfer_buffer_ = std::move(buffer);
91 }
92
TEST_F(VertexBufferTest,R8G8B8A8_UINT)93 TEST_F(VertexBufferTest, R8G8B8A8_UINT) {
94 std::vector<Value> values(4);
95 values[0].SetIntValue(55);
96 values[1].SetIntValue(3);
97 values[2].SetIntValue(27);
98 values[3].SetIntValue(255);
99
100 TypeParser parser;
101 auto type = parser.Parse("R8G8B8A8_UINT");
102 Format fmt(type.get());
103 Result r = SetIntData(0, &fmt, values);
104
105 const uint8_t* ptr = static_cast<const uint8_t*>(GetVkBufferPtr());
106 EXPECT_EQ(55, ptr[0]);
107 EXPECT_EQ(3, ptr[1]);
108 EXPECT_EQ(27, ptr[2]);
109 EXPECT_EQ(255, ptr[3]);
110 }
111
TEST_F(VertexBufferTest,R16G16B16A16_UINT)112 TEST_F(VertexBufferTest, R16G16B16A16_UINT) {
113 std::vector<Value> values(4);
114 values[0].SetIntValue(55);
115 values[1].SetIntValue(3);
116 values[2].SetIntValue(27);
117 values[3].SetIntValue(255);
118
119 TypeParser parser;
120 auto type = parser.Parse("R16G16B16A16_UINT");
121 Format fmt(type.get());
122 Result r = SetIntData(0, &fmt, values);
123
124 const uint16_t* ptr = static_cast<const uint16_t*>(GetVkBufferPtr());
125 EXPECT_EQ(55, ptr[0]);
126 EXPECT_EQ(3, ptr[1]);
127 EXPECT_EQ(27, ptr[2]);
128 EXPECT_EQ(255, ptr[3]);
129 }
130
TEST_F(VertexBufferTest,R32G32B32A32_UINT)131 TEST_F(VertexBufferTest, R32G32B32A32_UINT) {
132 std::vector<Value> values(4);
133 values[0].SetIntValue(55);
134 values[1].SetIntValue(3);
135 values[2].SetIntValue(27);
136 values[3].SetIntValue(255);
137
138 TypeParser parser;
139 auto type = parser.Parse("R32G32B32A32_UINT");
140 Format fmt(type.get());
141 Result r = SetIntData(0, &fmt, values);
142
143 const uint32_t* ptr = static_cast<const uint32_t*>(GetVkBufferPtr());
144 EXPECT_EQ(55, ptr[0]);
145 EXPECT_EQ(3, ptr[1]);
146 EXPECT_EQ(27, ptr[2]);
147 EXPECT_EQ(255, ptr[3]);
148 }
149
TEST_F(VertexBufferTest,R64G64B64A64_UINT)150 TEST_F(VertexBufferTest, R64G64B64A64_UINT) {
151 std::vector<Value> values(4);
152 values[0].SetIntValue(55);
153 values[1].SetIntValue(3);
154 values[2].SetIntValue(27);
155 values[3].SetIntValue(255);
156
157 TypeParser parser;
158 auto type = parser.Parse("R64G64B64A64_UINT");
159 Format fmt(type.get());
160 Result r = SetIntData(0, &fmt, values);
161
162 const uint64_t* ptr = static_cast<const uint64_t*>(GetVkBufferPtr());
163 EXPECT_EQ(55, ptr[0]);
164 EXPECT_EQ(3, ptr[1]);
165 EXPECT_EQ(27, ptr[2]);
166 EXPECT_EQ(255, ptr[3]);
167 }
168
TEST_F(VertexBufferTest,R8G8B8A8_SNORM)169 TEST_F(VertexBufferTest, R8G8B8A8_SNORM) {
170 std::vector<Value> values(4);
171 values[0].SetIntValue(static_cast<uint64_t>(-55));
172 values[1].SetIntValue(3);
173 values[2].SetIntValue(static_cast<uint64_t>(-128));
174 values[3].SetIntValue(127);
175
176 TypeParser parser;
177 auto type = parser.Parse("R8G8B8A8_SNORM");
178 Format fmt(type.get());
179 Result r = SetIntData(0, &fmt, values);
180 const int8_t* ptr = static_cast<const int8_t*>(GetVkBufferPtr());
181
182 EXPECT_EQ(-55, ptr[0]);
183 EXPECT_EQ(3, ptr[1]);
184 EXPECT_EQ(-128, ptr[2]);
185 EXPECT_EQ(127, ptr[3]);
186 }
187
TEST_F(VertexBufferTest,R16G16B16A16_SNORM)188 TEST_F(VertexBufferTest, R16G16B16A16_SNORM) {
189 std::vector<Value> values(4);
190 values[0].SetIntValue(static_cast<uint64_t>(-55));
191 values[1].SetIntValue(3);
192 values[2].SetIntValue(static_cast<uint64_t>(-27));
193 values[3].SetIntValue(255);
194
195 TypeParser parser;
196 auto type = parser.Parse("R16G16B16A16_SNORM");
197 Format fmt(type.get());
198 Result r = SetIntData(0, &fmt, values);
199
200 const int16_t* ptr = static_cast<const int16_t*>(GetVkBufferPtr());
201 EXPECT_EQ(-55, ptr[0]);
202 EXPECT_EQ(3, ptr[1]);
203 EXPECT_EQ(-27, ptr[2]);
204 EXPECT_EQ(255, ptr[3]);
205 }
206
TEST_F(VertexBufferTest,R32G32B32A32_SINT)207 TEST_F(VertexBufferTest, R32G32B32A32_SINT) {
208 std::vector<Value> values(4);
209 values[0].SetIntValue(static_cast<uint64_t>(-55));
210 values[1].SetIntValue(3);
211 values[2].SetIntValue(static_cast<uint64_t>(-27));
212 values[3].SetIntValue(255);
213
214 TypeParser parser;
215 auto type = parser.Parse("R32G32B32A32_SINT");
216 Format fmt(type.get());
217 Result r = SetIntData(0, &fmt, values);
218
219 const int32_t* ptr = static_cast<const int32_t*>(GetVkBufferPtr());
220 EXPECT_EQ(-55, ptr[0]);
221 EXPECT_EQ(3, ptr[1]);
222 EXPECT_EQ(-27, ptr[2]);
223 EXPECT_EQ(255, ptr[3]);
224 }
225
TEST_F(VertexBufferTest,R64G64B64A64_SINT)226 TEST_F(VertexBufferTest, R64G64B64A64_SINT) {
227 std::vector<Value> values(4);
228 values[0].SetIntValue(static_cast<uint64_t>(-55));
229 values[1].SetIntValue(3);
230 values[2].SetIntValue(static_cast<uint64_t>(-27));
231 values[3].SetIntValue(255);
232
233 TypeParser parser;
234 auto type = parser.Parse("R64G64B64A64_SINT");
235 Format fmt(type.get());
236 Result r = SetIntData(0, &fmt, values);
237
238 const int64_t* ptr = static_cast<const int64_t*>(GetVkBufferPtr());
239 EXPECT_EQ(-55, ptr[0]);
240 EXPECT_EQ(3, ptr[1]);
241 EXPECT_EQ(-27, ptr[2]);
242 EXPECT_EQ(255, ptr[3]);
243 }
244
TEST_F(VertexBufferTest,R32G32B32_SFLOAT)245 TEST_F(VertexBufferTest, R32G32B32_SFLOAT) {
246 std::vector<Value> values(3);
247 values[0].SetDoubleValue(-6.0);
248 values[1].SetDoubleValue(14.0);
249 values[2].SetDoubleValue(0.1171875);
250
251 TypeParser parser;
252 auto type = parser.Parse("R32G32B32_SFLOAT");
253 Format fmt(type.get());
254 Result r = SetDoubleData(0, &fmt, values);
255
256 const float* ptr = static_cast<const float*>(GetVkBufferPtr());
257 EXPECT_FLOAT_EQ(-6.0f, ptr[0]);
258 EXPECT_FLOAT_EQ(14.0f, ptr[1]);
259 EXPECT_FLOAT_EQ(0.1171875f, ptr[2]);
260 }
261
TEST_F(VertexBufferTest,R64G64B64_SFLOAT)262 TEST_F(VertexBufferTest, R64G64B64_SFLOAT) {
263 std::vector<Value> values(3);
264 values[0].SetDoubleValue(-6.0);
265 values[1].SetDoubleValue(14.0);
266 values[2].SetDoubleValue(0.1171875);
267
268 TypeParser parser;
269 auto type = parser.Parse("R64G64B64_SFLOAT");
270 Format fmt(type.get());
271 Result r = SetDoubleData(0, &fmt, values);
272
273 const double* ptr = static_cast<const double*>(GetVkBufferPtr());
274 EXPECT_DOUBLE_EQ(-6.0, ptr[0]);
275 EXPECT_DOUBLE_EQ(14.0, ptr[1]);
276 EXPECT_DOUBLE_EQ(0.1171875, ptr[2]);
277 }
278
279 } // namespace vulkan
280 } // namespace amber
281