aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordan sinclair <dj2@everburning.com>2019-04-05 11:07:56 -0400
committerDavid Neto <dneto@google.com>2019-04-05 11:07:56 -0400
commita8acd2ec631cfafb3ea68c82524defca81a20547 (patch)
treec75b01aa6feab9e9811ccd91549b77b72fd632d9
parentda78976bbc877e37fd7bd662621857fe15b297e5 (diff)
downloadamber-a8acd2ec631cfafb3ea68c82524defca81a20547.tar.gz
Generate a Format from a DataBuffer. (#448)
Generate a Format from a DataBuffer. When sending a VertexBuffer to Vulkan the data format is passed as a VkFormat string. When the buffer is provided in AmberScript it is not a FORMAT buffer. This CL adds the necessary code to generate a Format from a DatumType. This will allow creating the necessary format to send to Vulkan.
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/datum_type.cc24
-rw-r--r--src/datum_type.h5
-rw-r--r--src/datum_type_test.cc89
4 files changed, 119 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 9906d07..5129739 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -94,6 +94,7 @@ if (${AMBER_ENABLE_TESTS})
amberscript/parser_test.cc
buffer_test.cc
command_data_test.cc
+ datum_type_test.cc
descriptor_set_and_binding_parser_test.cc
executor_test.cc
format_parser_test.cc
diff --git a/src/datum_type.cc b/src/datum_type.cc
index 8a05894..37634bc 100644
--- a/src/datum_type.cc
+++ b/src/datum_type.cc
@@ -14,6 +14,10 @@
#include "src/datum_type.h"
+#include <string>
+
+#include "src/format_parser.h"
+
namespace amber {
DatumType::DatumType() = default;
@@ -52,4 +56,24 @@ uint32_t DatumType::SizeInBytes() const {
return bytes;
}
+std::unique_ptr<Format> DatumType::AsFormat() const {
+ uint32_t bits_per_element = ElementSizeInBytes() * 8;
+ static const char* prefixes = "RGBA";
+ std::string name = "";
+ for (size_t i = 0; i < row_count_; ++i)
+ name += prefixes[i] + std::to_string(bits_per_element);
+
+ name += "_";
+
+ if (IsFloat() || IsDouble())
+ name += "SFLOAT";
+ else if (IsInt8() || IsInt16() || IsInt32() || IsInt64())
+ name += "SINT";
+ else
+ name += "UINT";
+
+ FormatParser fp;
+ return fp.Parse(name);
+}
+
} // namespace amber
diff --git a/src/datum_type.h b/src/datum_type.h
index 437bf80..1d60f5f 100644
--- a/src/datum_type.h
+++ b/src/datum_type.h
@@ -17,6 +17,9 @@
#include <cstddef>
#include <cstdint>
+#include <memory>
+
+#include "src/format.h"
namespace amber {
@@ -63,6 +66,8 @@ class DatumType {
uint32_t ElementSizeInBytes() const;
uint32_t SizeInBytes() const;
+ std::unique_ptr<Format> AsFormat() const;
+
private:
DataType type_ = DataType::kUint8;
uint32_t column_count_ = 1;
diff --git a/src/datum_type_test.cc b/src/datum_type_test.cc
new file mode 100644
index 0000000..ef20bed
--- /dev/null
+++ b/src/datum_type_test.cc
@@ -0,0 +1,89 @@
+// Copyright 2019 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/datum_type.h"
+#include "gtest/gtest.h"
+
+namespace amber {
+
+using DatumTypeTest = testing::Test;
+
+struct Data {
+ DataType type;
+ uint32_t row_count;
+ FormatType format_type;
+};
+using DatumTypeTestFormat = testing::TestWithParam<Data>;
+TEST_P(DatumTypeTestFormat, ToFormat) {
+ auto test_data = GetParam();
+
+ DatumType dt;
+ dt.SetType(test_data.type);
+ dt.SetRowCount(test_data.row_count);
+
+ auto fmt = dt.AsFormat();
+ ASSERT_TRUE(fmt != nullptr);
+ EXPECT_EQ(test_data.format_type, fmt->GetFormatType());
+}
+INSTANTIATE_TEST_CASE_P(
+ DatumTypeTestFormat,
+ DatumTypeTestFormat,
+ testing::Values(
+ Data{DataType::kInt8, 1, FormatType::kR8_SINT},
+ Data{DataType::kInt8, 2, FormatType::kR8G8_SINT},
+ Data{DataType::kInt8, 3, FormatType::kR8G8B8_SINT},
+ Data{DataType::kInt8, 4, FormatType::kR8G8B8A8_SINT},
+ Data{DataType::kInt16, 1, FormatType::kR16_SINT},
+ Data{DataType::kInt16, 2, FormatType::kR16G16_SINT},
+ Data{DataType::kInt16, 3, FormatType::kR16G16B16_SINT},
+ Data{DataType::kInt16, 4, FormatType::kR16G16B16A16_SINT},
+ Data{DataType::kInt32, 1, FormatType::kR32_SINT},
+ Data{DataType::kInt32, 2, FormatType::kR32G32_SINT},
+ Data{DataType::kInt32, 3, FormatType::kR32G32B32_SINT},
+ Data{DataType::kInt32, 4, FormatType::kR32G32B32A32_SINT},
+ Data{DataType::kInt64, 1, FormatType::kR64_SINT},
+ Data{DataType::kInt64, 2, FormatType::kR64G64_SINT},
+ Data{DataType::kInt64, 3, FormatType::kR64G64B64_SINT},
+ Data{DataType::kInt64, 4, FormatType::kR64G64B64A64_SINT},
+
+ Data{DataType::kUint8, 1, FormatType::kR8_UINT},
+ Data{DataType::kUint8, 2, FormatType::kR8G8_UINT},
+ Data{DataType::kUint8, 3, FormatType::kR8G8B8_UINT},
+ Data{DataType::kUint8, 4, FormatType::kR8G8B8A8_UINT},
+ Data{DataType::kUint16, 1, FormatType::kR16_UINT},
+ Data{DataType::kUint16, 2, FormatType::kR16G16_UINT},
+ Data{DataType::kUint16, 3, FormatType::kR16G16B16_UINT},
+ Data{DataType::kUint16, 4, FormatType::kR16G16B16A16_UINT},
+ Data{DataType::kUint32, 1, FormatType::kR32_UINT},
+ Data{DataType::kUint32, 2, FormatType::kR32G32_UINT},
+ Data{DataType::kUint32, 3, FormatType::kR32G32B32_UINT},
+ Data{DataType::kUint32, 4, FormatType::kR32G32B32A32_UINT},
+ Data{DataType::kUint64, 1, FormatType::kR64_UINT},
+ Data{DataType::kUint64, 2, FormatType::kR64G64_UINT},
+ Data{DataType::kUint64, 3, FormatType::kR64G64B64_UINT},
+ Data{DataType::kUint64, 4, FormatType::kR64G64B64A64_UINT},
+
+ Data{DataType::kFloat, 1, FormatType::kR32_SFLOAT},
+ Data{DataType::kFloat, 2, FormatType::kR32G32_SFLOAT},
+ Data{DataType::kFloat, 3, FormatType::kR32G32B32_SFLOAT},
+ Data{DataType::kFloat, 4, FormatType::kR32G32B32A32_SFLOAT},
+
+ Data{DataType::kDouble, 1, FormatType::kR64_SFLOAT},
+ Data{DataType::kDouble, 2, FormatType::kR64G64_SFLOAT},
+ Data{DataType::kDouble, 3, FormatType::kR64G64B64_SFLOAT},
+ Data{
+ DataType::kDouble, 4,
+ FormatType::kR64G64B64A64_SFLOAT}), ); // NOLINT(whitespace/parens)
+
+} // namespace amber