Remove DatumType. (#667)
This CL converts the DatumTypeParser over to creating a Format objects
and updates the command parser to use the formats directly.
diff --git a/Android.mk b/Android.mk
index f52c659..f9e7c5a 100644
--- a/Android.mk
+++ b/Android.mk
@@ -44,7 +44,6 @@
src/value.cc \
src/verifier.cc \
src/vkscript/command_parser.cc \
- src/vkscript/datum_type.cc \
src/vkscript/datum_type_parser.cc \
src/vkscript/parser.cc \
src/vkscript/section_parser.cc \
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b1c23e6..76c6f12 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -36,7 +36,6 @@
value.cc
verifier.cc
vkscript/command_parser.cc
- vkscript/datum_type.cc
vkscript/datum_type_parser.cc
vkscript/parser.cc
vkscript/section_parser.cc
@@ -144,7 +143,6 @@
tokenizer_test.cc
verifier_test.cc
vkscript/command_parser_test.cc
- vkscript/datum_type_test.cc
vkscript/datum_type_parser_test.cc
vkscript/parser_test.cc
vkscript/section_parser_test.cc
diff --git a/src/buffer.h b/src/buffer.h
index 1532bb0..b95a3d4 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -24,7 +24,6 @@
#include "amber/result.h"
#include "amber/value.h"
#include "src/format.h"
-#include "src/vkscript/datum_type.h"
namespace amber {
diff --git a/src/vkscript/command_parser.cc b/src/vkscript/command_parser.cc
index 6d560d6..d054c03 100644
--- a/src/vkscript/command_parser.cc
+++ b/src/vkscript/command_parser.cc
@@ -588,11 +588,10 @@
token->ToOriginalString());
DatumTypeParser tp;
- Result r = tp.Parse(token->AsString());
- if (!r.IsSuccess())
- return r;
+ auto fmt = tp.Parse(token->AsString());
+ if (!fmt)
+ return Result("Invalid type provided: " + token->AsString());
- auto fmt = tp.GetType().AsFormat();
auto* buf = cmd->GetBuffer();
if (buf->FormatIsDefault() || !buf->GetFormat())
buf->SetFormat(std::move(fmt));
@@ -617,7 +616,7 @@
cmd->SetOffset(token->AsUint32());
std::vector<Value> values;
- r = ParseValues("ssbo", buf->GetFormat(), &values);
+ Result r = ParseValues("ssbo", buf->GetFormat(), &values);
if (!r.IsSuccess())
return r;
@@ -738,11 +737,9 @@
}
DatumTypeParser tp;
- Result r = tp.Parse(token->AsString());
- if (!r.IsSuccess())
- return r;
-
- auto fmt = tp.GetType().AsFormat();
+ auto fmt = tp.Parse(token->AsString());
+ if (!fmt)
+ return Result("Invalid type provided: " + token->AsString());
// uniform is always std140.
if (is_ubo)
@@ -771,7 +768,7 @@
cmd->SetOffset(token->AsUint32());
std::vector<Value> values;
- r = ParseValues("uniform", buf->GetFormat(), &values);
+ Result r = ParseValues("uniform", buf->GetFormat(), &values);
if (!r.IsSuccess())
return r;
@@ -2016,9 +2013,9 @@
token->ToOriginalString());
DatumTypeParser tp;
- Result r = tp.Parse(token->AsString());
- if (!r.IsSuccess())
- return r;
+ auto fmt = tp.Parse(token->AsString());
+ if (!fmt)
+ return Result("Invalid type provided: " + token->AsString());
token = tokenizer_->NextToken();
if (!token->IsInteger())
@@ -2059,7 +2056,6 @@
std::to_string(binding));
}
- auto fmt = tp.GetType().AsFormat();
if (buffer->FormatIsDefault() || !buffer->GetFormat())
buffer->SetFormat(MakeUnique<Format>(*fmt));
else if (buffer->GetFormat() && !buffer->GetFormat()->Equal(fmt.get()))
@@ -2084,7 +2080,7 @@
token->ToOriginalString());
ProbeSSBOCommand::Comparator comp = ProbeSSBOCommand::Comparator::kEqual;
- r = ParseComparator(token->AsString(), &comp);
+ Result r = ParseComparator(token->AsString(), &comp);
if (!r.IsSuccess())
return r;
diff --git a/src/vkscript/command_parser.h b/src/vkscript/command_parser.h
index 680bdaa..f57989f 100644
--- a/src/vkscript/command_parser.h
+++ b/src/vkscript/command_parser.h
@@ -25,7 +25,6 @@
#include "src/pipeline.h"
#include "src/pipeline_data.h"
#include "src/script.h"
-#include "src/vkscript/datum_type.h"
namespace amber {
diff --git a/src/vkscript/datum_type.cc b/src/vkscript/datum_type.cc
deleted file mode 100644
index 2f9ff97..0000000
--- a/src/vkscript/datum_type.cc
+++ /dev/null
@@ -1,76 +0,0 @@
-// 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/vkscript/datum_type.h"
-
-#include <string>
-
-#include "src/format_parser.h"
-
-namespace amber {
-namespace vkscript {
-namespace {
-
-uint32_t ElementSizeInBytes(DataType type) {
- if (type == DataType::kInt8 || type == DataType::kUint8)
- return 1;
- if (type == DataType::kInt16 || type == DataType::kUint16)
- return 2;
- if (type == DataType::kInt32 || type == DataType::kUint32)
- return 4;
- if (type == DataType::kInt64 || type == DataType::kUint64)
- return 8;
- if (type == DataType::kFloat)
- return sizeof(float);
-
- return sizeof(double);
-}
-
-} // namespace
-
-DatumType::DatumType() = default;
-
-DatumType::DatumType(const DatumType&) = default;
-
-DatumType::~DatumType() = default;
-
-std::unique_ptr<Format> DatumType::AsFormat() const {
- uint32_t bits_per_element = ElementSizeInBytes(type_) * 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;
- auto fmt = fp.Parse(name);
- // There is no format string equivalent to a matrix ...
- if (column_count_ > 1) {
- fmt->SetFormatType(FormatType::kUnknown);
- fmt->SetColumnCount(column_count_);
- }
-
- return fmt;
-}
-
-} // namespace vkscript
-} // namespace amber
diff --git a/src/vkscript/datum_type.h b/src/vkscript/datum_type.h
deleted file mode 100644
index b229e20..0000000
--- a/src/vkscript/datum_type.h
+++ /dev/null
@@ -1,81 +0,0 @@
-// 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_VKSCRIPT_DATUM_TYPE_H_
-#define SRC_VKSCRIPT_DATUM_TYPE_H_
-
-#include <cstddef>
-#include <cstdint>
-#include <memory>
-
-#include "src/format.h"
-
-namespace amber {
-namespace vkscript {
-
-enum class DataType {
- kInt8 = 0,
- kInt16,
- kInt32,
- kInt64,
- kUint8,
- kUint16,
- kUint32,
- kUint64,
- kFloat,
- kDouble,
-};
-
-/// Stores information on a given type of data. This class should only be used
-/// as a simple way to create format objects. DatumType should not appear as a
-/// member of any classes.
-class DatumType {
- public:
- DatumType();
- DatumType(const DatumType&);
- ~DatumType();
-
- bool IsInt8() const { return type_ == DataType::kInt8; }
- bool IsInt16() const { return type_ == DataType::kInt16; }
- bool IsInt32() const { return type_ == DataType::kInt32; }
- bool IsInt64() const { return type_ == DataType::kInt64; }
- bool IsUint8() const { return type_ == DataType::kUint8; }
- bool IsUint16() const { return type_ == DataType::kUint16; }
- bool IsUint32() const { return type_ == DataType::kUint32; }
- bool IsUint64() const { return type_ == DataType::kUint64; }
- bool IsFloat() const { return type_ == DataType::kFloat; }
- bool IsDouble() const { return type_ == DataType::kDouble; }
-
- void SetType(DataType type) { type_ = type; }
- DataType GetType() const { return type_; }
-
- void SetColumnCount(uint32_t count) { column_count_ = count; }
- uint32_t ColumnCount() const { return column_count_; }
-
- void SetRowCount(uint32_t count) { row_count_ = count; }
- uint32_t RowCount() const { return row_count_; }
-
- std::unique_ptr<Format> AsFormat() const;
-
- private:
- DataType type_ = DataType::kUint8;
- uint32_t column_count_ = 1;
- uint32_t row_count_ = 1;
- bool is_std140_ = true;
-};
-
-} // namespace vkscript
-} // namespace amber
-
-#endif // SRC_VKSCRIPT_DATUM_TYPE_H_
diff --git a/src/vkscript/datum_type_parser.cc b/src/vkscript/datum_type_parser.cc
index ca0c6e5..223a97d 100644
--- a/src/vkscript/datum_type_parser.cc
+++ b/src/vkscript/datum_type_parser.cc
@@ -14,226 +14,133 @@
#include "src/vkscript/datum_type_parser.h"
+#include "src/format_parser.h"
+#include "src/make_unique.h"
+
namespace amber {
namespace vkscript {
+namespace {
+
+FormatComponentType FORMAT_TYPES[] = {
+ FormatComponentType::kR, FormatComponentType::kG, FormatComponentType::kB,
+ FormatComponentType::kA};
+
+} // namespace
DatumTypeParser::DatumTypeParser() = default;
DatumTypeParser::~DatumTypeParser() = default;
-Result DatumTypeParser::Parse(const std::string& data) {
- // TODO(dsinclair): Might want to make this nicer in the future, but this
- // works and is easy for now.
+std::unique_ptr<Format> DatumTypeParser::Parse(const std::string& data) {
+ auto fmt = MakeUnique<Format>();
+
+ bool matrix = false;
if (data == "int") {
- type_.SetType(DataType::kInt32);
+ fmt->AddComponent(FormatComponentType::kR, FormatMode::kSInt, 32);
} else if (data == "uint") {
- type_.SetType(DataType::kUint32);
+ fmt->AddComponent(FormatComponentType::kR, FormatMode::kUInt, 32);
} else if (data == "int8_t") {
- type_.SetType(DataType::kInt8);
+ fmt->AddComponent(FormatComponentType::kR, FormatMode::kSInt, 8);
} else if (data == "uint8_t") {
- type_.SetType(DataType::kUint8);
+ fmt->AddComponent(FormatComponentType::kR, FormatMode::kUInt, 8);
} else if (data == "int16_t") {
- type_.SetType(DataType::kInt16);
+ fmt->AddComponent(FormatComponentType::kR, FormatMode::kSInt, 16);
} else if (data == "uint16_t") {
- type_.SetType(DataType::kUint16);
+ fmt->AddComponent(FormatComponentType::kR, FormatMode::kUInt, 16);
} else if (data == "int64_t") {
- type_.SetType(DataType::kInt64);
+ fmt->AddComponent(FormatComponentType::kR, FormatMode::kSInt, 64);
} else if (data == "uint64_t") {
- type_.SetType(DataType::kUint64);
+ fmt->AddComponent(FormatComponentType::kR, FormatMode::kUInt, 64);
} else if (data == "float") {
- type_.SetType(DataType::kFloat);
+ fmt->AddComponent(FormatComponentType::kR, FormatMode::kSFloat, 32);
} else if (data == "double") {
- type_.SetType(DataType::kDouble);
- } else if (data == "vec2") {
- type_.SetType(DataType::kFloat);
- type_.SetRowCount(2);
- } else if (data == "vec3") {
- type_.SetType(DataType::kFloat);
- type_.SetRowCount(3);
- } else if (data == "vec4") {
- type_.SetType(DataType::kFloat);
- type_.SetRowCount(4);
- } else if (data == "dvec2") {
- type_.SetType(DataType::kDouble);
- type_.SetRowCount(2);
- } else if (data == "dvec3") {
- type_.SetType(DataType::kDouble);
- type_.SetRowCount(3);
- } else if (data == "dvec4") {
- type_.SetType(DataType::kDouble);
- type_.SetRowCount(4);
- } else if (data == "ivec2") {
- type_.SetType(DataType::kInt32);
- type_.SetRowCount(2);
- } else if (data == "ivec3") {
- type_.SetType(DataType::kInt32);
- type_.SetRowCount(3);
- } else if (data == "ivec4") {
- type_.SetType(DataType::kInt32);
- type_.SetRowCount(4);
- } else if (data == "uvec2") {
- type_.SetType(DataType::kUint32);
- type_.SetRowCount(2);
- } else if (data == "uvec3") {
- type_.SetType(DataType::kUint32);
- type_.SetRowCount(3);
- } else if (data == "uvec4") {
- type_.SetType(DataType::kUint32);
- type_.SetRowCount(4);
- } else if (data == "i8vec2") {
- type_.SetType(DataType::kInt8);
- type_.SetRowCount(2);
- } else if (data == "i8vec3") {
- type_.SetType(DataType::kInt8);
- type_.SetRowCount(3);
- } else if (data == "i8vec4") {
- type_.SetType(DataType::kInt8);
- type_.SetRowCount(4);
- } else if (data == "u8vec2") {
- type_.SetType(DataType::kUint8);
- type_.SetRowCount(2);
- } else if (data == "u8vec3") {
- type_.SetType(DataType::kUint8);
- type_.SetRowCount(3);
- } else if (data == "u8vec4") {
- type_.SetType(DataType::kUint8);
- type_.SetRowCount(4);
- } else if (data == "i16vec2") {
- type_.SetType(DataType::kInt16);
- type_.SetRowCount(2);
- } else if (data == "i16vec3") {
- type_.SetType(DataType::kInt16);
- type_.SetRowCount(3);
- } else if (data == "i16vec4") {
- type_.SetType(DataType::kInt16);
- type_.SetRowCount(4);
- } else if (data == "u16vec2") {
- type_.SetType(DataType::kUint16);
- type_.SetRowCount(2);
- } else if (data == "u16vec3") {
- type_.SetType(DataType::kUint16);
- type_.SetRowCount(3);
- } else if (data == "u16vec4") {
- type_.SetType(DataType::kUint16);
- type_.SetRowCount(4);
- } else if (data == "i64vec2") {
- type_.SetType(DataType::kInt64);
- type_.SetRowCount(2);
- } else if (data == "i64vec3") {
- type_.SetType(DataType::kInt64);
- type_.SetRowCount(3);
- } else if (data == "i64vec4") {
- type_.SetType(DataType::kInt64);
- type_.SetRowCount(4);
- } else if (data == "u64vec2") {
- type_.SetType(DataType::kUint64);
- type_.SetRowCount(2);
- } else if (data == "u64vec3") {
- type_.SetType(DataType::kUint64);
- type_.SetRowCount(3);
- } else if (data == "u64vec4") {
- type_.SetType(DataType::kUint64);
- type_.SetRowCount(4);
- } else if (data == "mat2") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(2);
- type_.SetRowCount(2);
- } else if (data == "mat2x2") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(2);
- type_.SetRowCount(2);
- } else if (data == "mat2x3") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(2);
- type_.SetRowCount(3);
- } else if (data == "mat2x4") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(2);
- type_.SetRowCount(4);
- } else if (data == "mat3") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(3);
- type_.SetRowCount(3);
- } else if (data == "mat3x2") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(3);
- type_.SetRowCount(2);
- } else if (data == "mat3x3") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(3);
- type_.SetRowCount(3);
- } else if (data == "mat3x4") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(3);
- type_.SetRowCount(4);
- } else if (data == "mat4") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(4);
- type_.SetRowCount(4);
- } else if (data == "mat4x2") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(4);
- type_.SetRowCount(2);
- } else if (data == "mat4x3") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(4);
- type_.SetRowCount(3);
- } else if (data == "mat4x4") {
- type_.SetType(DataType::kFloat);
- type_.SetColumnCount(4);
- type_.SetRowCount(4);
- } else if (data == "dmat2") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(2);
- type_.SetRowCount(2);
- } else if (data == "dmat2x2") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(2);
- type_.SetRowCount(2);
- } else if (data == "dmat2x3") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(2);
- type_.SetRowCount(3);
- } else if (data == "dmat2x4") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(2);
- type_.SetRowCount(4);
- } else if (data == "dmat3") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(3);
- type_.SetRowCount(3);
- } else if (data == "dmat3x2") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(3);
- type_.SetRowCount(2);
- } else if (data == "dmat3x3") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(3);
- type_.SetRowCount(3);
- } else if (data == "dmat3x4") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(3);
- type_.SetRowCount(4);
- } else if (data == "dmat4") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(4);
- type_.SetRowCount(4);
- } else if (data == "dmat4x2") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(4);
- type_.SetRowCount(2);
- } else if (data == "dmat4x3") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(4);
- type_.SetRowCount(3);
- } else if (data == "dmat4x4") {
- type_.SetType(DataType::kDouble);
- type_.SetColumnCount(4);
- type_.SetRowCount(4);
+ fmt->AddComponent(FormatComponentType::kR, FormatMode::kSFloat, 64);
} else {
- return Result("Invalid type provided: " + data);
+ int row_count = 4;
+ FormatMode mode = FormatMode::kSFloat;
+ uint8_t num_bits = 32;
+
+ size_t vec_pos = data.find("vec");
+ if (vec_pos != std::string::npos) {
+ if (data[0] == 'i') {
+ mode = FormatMode::kSInt;
+ } else if (data[0] == 'u') {
+ mode = FormatMode::kUInt;
+ } else if (data[0] == 'd') {
+ num_bits = 64;
+ }
+
+ if (data[1] == '8')
+ num_bits = 8;
+ else if (data[1] == '1' && data[2] == '6')
+ num_bits = 16;
+ else if (data[1] == '6' && data[2] == '4')
+ num_bits = 64;
+
+ if ((vec_pos + 3) < data.length())
+ row_count = data[vec_pos + 3] - '0';
+
+ } else {
+ size_t mat_pos = data.find("mat");
+ if (mat_pos == std::string::npos)
+ return nullptr;
+
+ matrix = true;
+
+ if (data[0] == 'd')
+ num_bits = 64;
+
+ int column_count = 1;
+ if (mat_pos + 3 < data.length())
+ column_count = data[mat_pos + 3] - '0';
+
+ if (mat_pos + 5 < data.length())
+ row_count = data[mat_pos + 5] - '0';
+ else
+ row_count = column_count;
+
+ fmt->SetColumnCount(static_cast<uint32_t>(column_count));
+ }
+
+ for (int i = 0; i < row_count; ++i)
+ fmt->AddComponent(FORMAT_TYPES[i], mode, num_bits);
}
- return {};
+
+ // Convert the name back into a FormatType so we can use it in the buffer
+ // later Otherwise, we end up with a type of Unknown.
+ //
+ // There is no equivalent type for a matrix.
+ if (!matrix) {
+ std::string name = "";
+ std::string parts = "ARGB";
+ const auto& comps = fmt->GetComponents();
+ for (const auto& comp : comps) {
+ name += parts[static_cast<uint8_t>(comp.type)] +
+ std::to_string(comp.num_bits);
+ }
+ name += "_";
+ switch (comps[0].mode) {
+ case FormatMode::kUNorm:
+ case FormatMode::kUFloat:
+ case FormatMode::kUScaled:
+ case FormatMode::kSNorm:
+ case FormatMode::kSScaled:
+ case FormatMode::kSRGB:
+ return nullptr;
+ case FormatMode::kUInt:
+ name += "UINT";
+ break;
+ case FormatMode::kSInt:
+ name += "SINT";
+ break;
+ case FormatMode::kSFloat:
+ name += "SFLOAT";
+ break;
+ }
+
+ fmt->SetFormatType(FormatParser::NameToType(name));
+ }
+ return fmt;
}
} // namespace vkscript
diff --git a/src/vkscript/datum_type_parser.h b/src/vkscript/datum_type_parser.h
index a4653d8..9d37228 100644
--- a/src/vkscript/datum_type_parser.h
+++ b/src/vkscript/datum_type_parser.h
@@ -15,25 +15,22 @@
#ifndef SRC_VKSCRIPT_DATUM_TYPE_PARSER_H_
#define SRC_VKSCRIPT_DATUM_TYPE_PARSER_H_
+#include <memory>
#include <string>
#include "amber/result.h"
-#include "src/vkscript/datum_type.h"
+#include "src/format.h"
namespace amber {
namespace vkscript {
-/// Parses a data description on the VkScript format into a DatumType object.
+/// Parses a data description on the VkScript format.
class DatumTypeParser {
public:
DatumTypeParser();
~DatumTypeParser();
- Result Parse(const std::string& data);
- const DatumType& GetType() const { return type_; }
-
- private:
- DatumType type_;
+ std::unique_ptr<Format> Parse(const std::string& data);
};
} // namespace vkscript
diff --git a/src/vkscript/datum_type_parser_test.cc b/src/vkscript/datum_type_parser_test.cc
index 8a5c052..9ba7501 100644
--- a/src/vkscript/datum_type_parser_test.cc
+++ b/src/vkscript/datum_type_parser_test.cc
@@ -18,26 +18,37 @@
namespace amber {
namespace vkscript {
+namespace {
+
+bool AllCompsAreType(Format* fmt, FormatMode mode, uint8_t num_bits) {
+ for (auto& comp : fmt->GetComponents()) {
+ if (comp.mode != mode || comp.num_bits != num_bits)
+ return false;
+ }
+
+ return true;
+}
+
+} // namespace
using DatumTypeParserTest = testing::Test;
TEST_F(DatumTypeParserTest, EmptyType) {
DatumTypeParser tp;
- Result r = tp.Parse("");
- ASSERT_FALSE(r.IsSuccess());
- EXPECT_EQ("Invalid type provided: ", r.Error());
+ auto fmt = tp.Parse("");
+ ASSERT_TRUE(fmt == nullptr);
}
TEST_F(DatumTypeParserTest, InvalidType) {
DatumTypeParser tp;
- Result r = tp.Parse("INVALID");
- ASSERT_FALSE(r.IsSuccess());
- EXPECT_EQ("Invalid type provided: INVALID", r.Error());
+ auto fmt = tp.Parse("INVALID");
+ ASSERT_TRUE(fmt == nullptr);
}
struct DatumTypeData {
const char* name;
- DataType type;
+ FormatMode type;
+ uint8_t num_bits;
uint32_t column_count;
uint32_t row_count;
};
@@ -48,87 +59,180 @@
const auto& test_data = GetParam();
DatumTypeParser tp;
- Result r = tp.Parse(test_data.name);
- const auto& t = tp.GetType();
- ASSERT_TRUE(r.IsSuccess()) << r.Error();
- EXPECT_EQ(test_data.type, t.GetType());
- EXPECT_EQ(test_data.column_count, t.ColumnCount());
- EXPECT_EQ(test_data.row_count, t.RowCount());
+ auto fmt = tp.Parse(test_data.name);
+
+ ASSERT_TRUE(fmt != nullptr);
+ EXPECT_TRUE(AllCompsAreType(fmt.get(), test_data.type, test_data.num_bits));
+ EXPECT_EQ(test_data.column_count, fmt->ColumnCount());
+ EXPECT_EQ(test_data.row_count, fmt->RowCount());
}
INSTANTIATE_TEST_SUITE_P(
DatumTypeParserTest1,
DatumTypeDataTest,
- testing::Values(DatumTypeData{"int", DataType::kInt32, 1, 1},
- DatumTypeData{"uint", DataType::kUint32, 1, 1},
- DatumTypeData{"int8_t", DataType::kInt8, 1, 1},
- DatumTypeData{"uint8_t", DataType::kUint8, 1, 1},
- DatumTypeData{"int16_t", DataType::kInt16, 1, 1},
- DatumTypeData{"uint16_t", DataType::kUint16, 1, 1},
- DatumTypeData{"int64_t", DataType::kInt64, 1, 1},
- DatumTypeData{"uint64_t", DataType::kUint64, 1, 1},
- DatumTypeData{"float", DataType::kFloat, 1, 1},
- DatumTypeData{"double", DataType::kDouble, 1, 1},
- DatumTypeData{"vec2", DataType::kFloat, 1, 2},
- DatumTypeData{"vec3", DataType::kFloat, 1, 3},
- DatumTypeData{"vec4", DataType::kFloat, 1, 4},
- DatumTypeData{"dvec2", DataType::kDouble, 1, 2},
- DatumTypeData{"dvec3", DataType::kDouble, 1, 3},
- DatumTypeData{"dvec4", DataType::kDouble, 1, 4},
- DatumTypeData{"ivec2", DataType::kInt32, 1, 2},
- DatumTypeData{"ivec3", DataType::kInt32, 1, 3},
- DatumTypeData{"ivec4", DataType::kInt32, 1, 4},
- DatumTypeData{"uvec2", DataType::kUint32, 1, 2},
- DatumTypeData{"uvec3", DataType::kUint32, 1, 3},
- DatumTypeData{"uvec4", DataType::kUint32, 1, 4},
- DatumTypeData{"i8vec2", DataType::kInt8, 1, 2},
- DatumTypeData{"i8vec3", DataType::kInt8, 1, 3},
- DatumTypeData{"i8vec4", DataType::kInt8, 1, 4},
- DatumTypeData{"u8vec2", DataType::kUint8, 1, 2},
- DatumTypeData{"u8vec3", DataType::kUint8, 1, 3},
- DatumTypeData{"u8vec4", DataType::kUint8, 1, 4},
- DatumTypeData{"i16vec2", DataType::kInt16, 1,
+ testing::Values(DatumTypeData{"int", FormatMode::kSInt, 32, 1, 1},
+ DatumTypeData{"uint", FormatMode::kUInt, 32, 1, 1},
+ DatumTypeData{"int8_t", FormatMode::kSInt, 8, 1, 1},
+ DatumTypeData{"uint8_t", FormatMode::kUInt, 8, 1, 1},
+ DatumTypeData{"int16_t", FormatMode::kSInt, 16, 1, 1},
+ DatumTypeData{"uint16_t", FormatMode::kUInt, 16, 1, 1},
+ DatumTypeData{"int64_t", FormatMode::kSInt, 64, 1, 1},
+ DatumTypeData{"uint64_t", FormatMode::kUInt, 64, 1, 1},
+ DatumTypeData{"float", FormatMode::kSFloat, 32, 1, 1},
+ DatumTypeData{"double", FormatMode::kSFloat, 64, 1, 1},
+ DatumTypeData{"vec2", FormatMode::kSFloat, 32, 1, 2},
+ DatumTypeData{"vec3", FormatMode::kSFloat, 32, 1, 3},
+ DatumTypeData{"vec4", FormatMode::kSFloat, 32, 1, 4},
+ DatumTypeData{"dvec2", FormatMode::kSFloat, 64, 1, 2},
+ DatumTypeData{"dvec3", FormatMode::kSFloat, 64, 1, 3},
+ DatumTypeData{"dvec4", FormatMode::kSFloat, 64, 1, 4},
+ DatumTypeData{"ivec2", FormatMode::kSInt, 32, 1, 2},
+ DatumTypeData{"ivec3", FormatMode::kSInt, 32, 1, 3},
+ DatumTypeData{"ivec4", FormatMode::kSInt, 32, 1, 4},
+ DatumTypeData{"uvec2", FormatMode::kUInt, 32, 1, 2},
+ DatumTypeData{"uvec3", FormatMode::kUInt, 32, 1, 3},
+ DatumTypeData{"uvec4", FormatMode::kUInt, 32, 1, 4},
+ DatumTypeData{"i8vec2", FormatMode::kSInt, 8, 1, 2},
+ DatumTypeData{"i8vec3", FormatMode::kSInt, 8, 1, 3},
+ DatumTypeData{"i8vec4", FormatMode::kSInt, 8, 1, 4},
+ DatumTypeData{"u8vec2", FormatMode::kUInt, 8, 1, 2},
+ DatumTypeData{"u8vec3", FormatMode::kUInt, 8, 1, 3},
+ DatumTypeData{"u8vec4", FormatMode::kUInt, 8, 1, 4},
+ DatumTypeData{"i16vec2", FormatMode::kSInt, 16, 1,
2})); // NOLINT(whitespace/parens)
INSTANTIATE_TEST_SUITE_P(
DatumTypeParserTest2,
DatumTypeDataTest,
- testing::Values(DatumTypeData{"i16vec3", DataType::kInt16, 1, 3},
- DatumTypeData{"i16vec4", DataType::kInt16, 1, 4},
- DatumTypeData{"u16vec2", DataType::kUint16, 1, 2},
- DatumTypeData{"u16vec3", DataType::kUint16, 1, 3},
- DatumTypeData{"u16vec4", DataType::kUint16, 1, 4},
- DatumTypeData{"i64vec2", DataType::kInt64, 1, 2},
- DatumTypeData{"i64vec3", DataType::kInt64, 1, 3},
- DatumTypeData{"i64vec4", DataType::kInt64, 1, 4},
- DatumTypeData{"u64vec2", DataType::kUint64, 1, 2},
- DatumTypeData{"u64vec3", DataType::kUint64, 1, 3},
- DatumTypeData{"u64vec4", DataType::kUint64, 1, 4},
- DatumTypeData{"mat2", DataType::kFloat, 2, 2},
- DatumTypeData{"mat2x2", DataType::kFloat, 2, 2},
- DatumTypeData{"mat2x3", DataType::kFloat, 2, 3},
- DatumTypeData{"mat2x4", DataType::kFloat, 2, 4},
- DatumTypeData{"mat3", DataType::kFloat, 3, 3},
- DatumTypeData{"mat3x2", DataType::kFloat, 3, 2},
- DatumTypeData{"mat3x3", DataType::kFloat, 3, 3},
- DatumTypeData{"mat3x4", DataType::kFloat, 3, 4},
- DatumTypeData{"mat4", DataType::kFloat, 4, 4},
- DatumTypeData{"mat4x2", DataType::kFloat, 4, 2},
- DatumTypeData{"mat4x3", DataType::kFloat, 4, 3},
- DatumTypeData{"mat4x4", DataType::kFloat, 4, 4},
- DatumTypeData{"dmat2", DataType::kDouble, 2, 2},
- DatumTypeData{"dmat2x2", DataType::kDouble, 2, 2},
- DatumTypeData{"dmat2x3", DataType::kDouble, 2, 3},
- DatumTypeData{"dmat2x4", DataType::kDouble, 2, 4},
- DatumTypeData{"dmat3", DataType::kDouble, 3, 3},
- DatumTypeData{"dmat3x2", DataType::kDouble, 3, 2},
- DatumTypeData{"dmat3x3", DataType::kDouble, 3, 3},
- DatumTypeData{"dmat3x4", DataType::kDouble, 3, 4},
- DatumTypeData{"dmat4", DataType::kDouble, 4, 4},
- DatumTypeData{"dmat4x2", DataType::kDouble, 4, 2},
- DatumTypeData{"dmat4x3", DataType::kDouble, 4, 3},
- DatumTypeData{"dmat4x4", DataType::kDouble, 4,
+ testing::Values(DatumTypeData{"i16vec3", FormatMode::kSInt, 16, 1, 3},
+ DatumTypeData{"i16vec4", FormatMode::kSInt, 16, 1, 4},
+ DatumTypeData{"u16vec2", FormatMode::kUInt, 16, 1, 2},
+ DatumTypeData{"u16vec3", FormatMode::kUInt, 16, 1, 3},
+ DatumTypeData{"u16vec4", FormatMode::kUInt, 16, 1, 4},
+ DatumTypeData{"i64vec2", FormatMode::kSInt, 64, 1, 2},
+ DatumTypeData{"i64vec3", FormatMode::kSInt, 64, 1, 3},
+ DatumTypeData{"i64vec4", FormatMode::kSInt, 64, 1, 4},
+ DatumTypeData{"u64vec2", FormatMode::kUInt, 64, 1, 2},
+ DatumTypeData{"u64vec3", FormatMode::kUInt, 64, 1, 3},
+ DatumTypeData{"u64vec4", FormatMode::kUInt, 64, 1, 4},
+ DatumTypeData{"mat2", FormatMode::kSFloat, 32, 2, 2},
+ DatumTypeData{"mat2x2", FormatMode::kSFloat, 32, 2, 2},
+ DatumTypeData{"mat2x3", FormatMode::kSFloat, 32, 2, 3},
+ DatumTypeData{"mat2x4", FormatMode::kSFloat, 32, 2, 4},
+ DatumTypeData{"mat3", FormatMode::kSFloat, 32, 3, 3},
+ DatumTypeData{"mat3x2", FormatMode::kSFloat, 32, 3, 2},
+ DatumTypeData{"mat3x3", FormatMode::kSFloat, 32, 3, 3},
+ DatumTypeData{"mat3x4", FormatMode::kSFloat, 32, 3, 4},
+ DatumTypeData{"mat4", FormatMode::kSFloat, 32, 4, 4},
+ DatumTypeData{"mat4x2", FormatMode::kSFloat, 32, 4, 2},
+ DatumTypeData{"mat4x3", FormatMode::kSFloat, 32, 4, 3},
+ DatumTypeData{"mat4x4", FormatMode::kSFloat, 32, 4, 4},
+ DatumTypeData{"dmat2", FormatMode::kSFloat, 64, 2, 2},
+ DatumTypeData{"dmat2x2", FormatMode::kSFloat, 64, 2, 2},
+ DatumTypeData{"dmat2x3", FormatMode::kSFloat, 64, 2, 3},
+ DatumTypeData{"dmat2x4", FormatMode::kSFloat, 64, 2, 4},
+ DatumTypeData{"dmat3", FormatMode::kSFloat, 64, 3, 3},
+ DatumTypeData{"dmat3x2", FormatMode::kSFloat, 64, 3, 2},
+ DatumTypeData{"dmat3x3", FormatMode::kSFloat, 64, 3, 3},
+ DatumTypeData{"dmat3x4", FormatMode::kSFloat, 64, 3, 4},
+ DatumTypeData{"dmat4", FormatMode::kSFloat, 64, 4, 4},
+ DatumTypeData{"dmat4x2", FormatMode::kSFloat, 64, 4, 2},
+ DatumTypeData{"dmat4x3", FormatMode::kSFloat, 64, 4, 3},
+ DatumTypeData{"dmat4x4", FormatMode::kSFloat, 64, 4,
4})); // NOLINT(whitespace/parens)
+struct DatumFormatData {
+ std::string name;
+ FormatType format_type;
+};
+
+using DatumTypeTestFormat = testing::TestWithParam<DatumFormatData>;
+TEST_P(DatumTypeTestFormat, ToFormat) {
+ auto test_data = GetParam();
+
+ DatumTypeParser tp;
+ auto fmt = tp.Parse(test_data.name);
+
+ ASSERT_TRUE(fmt != nullptr);
+ ASSERT_EQ(test_data.format_type, fmt->GetFormatType());
+}
+
+INSTANTIATE_TEST_SUITE_P(
+ DatumTypeFormat1,
+ DatumTypeTestFormat,
+ testing::Values(
+ DatumFormatData{"int", FormatType::kR32_SINT},
+ DatumFormatData{"uint", FormatType::kR32_UINT},
+ DatumFormatData{"int8_t", FormatType::kR8_SINT},
+ DatumFormatData{"uint8_t", FormatType::kR8_UINT},
+ DatumFormatData{"int16_t", FormatType::kR16_SINT},
+ DatumFormatData{"uint16_t", FormatType::kR16_UINT},
+ DatumFormatData{"int64_t", FormatType::kR64_SINT},
+ DatumFormatData{"uint64_t", FormatType::kR64_UINT},
+ DatumFormatData{"float", FormatType::kR32_SFLOAT},
+ DatumFormatData{"double", FormatType::kR64_SFLOAT},
+ DatumFormatData{"vec2", FormatType::kR32G32_SFLOAT},
+ DatumFormatData{"vec3", FormatType::kR32G32B32_SFLOAT},
+ DatumFormatData{"vec4", FormatType::kR32G32B32A32_SFLOAT},
+ DatumFormatData{"dvec2", FormatType::kR64G64_SFLOAT},
+ DatumFormatData{"dvec3", FormatType::kR64G64B64_SFLOAT},
+ DatumFormatData{"dvec4", FormatType::kR64G64B64A64_SFLOAT},
+ DatumFormatData{"ivec2", FormatType::kR32G32_SINT},
+ DatumFormatData{"ivec3", FormatType::kR32G32B32_SINT},
+ DatumFormatData{"ivec4", FormatType::kR32G32B32A32_SINT},
+ DatumFormatData{"uvec2", FormatType::kR32G32_UINT},
+ DatumFormatData{"uvec3", FormatType::kR32G32B32_UINT},
+ DatumFormatData{"uvec4", FormatType::kR32G32B32A32_UINT},
+ DatumFormatData{"i8vec2", FormatType::kR8G8_SINT},
+ DatumFormatData{"i8vec3", FormatType::kR8G8B8_SINT},
+ DatumFormatData{"i8vec4", FormatType::kR8G8B8A8_SINT},
+ DatumFormatData{"u8vec2", FormatType::kR8G8_UINT},
+ DatumFormatData{"u8vec3", FormatType::kR8G8B8_UINT},
+ DatumFormatData{"u8vec4", FormatType::kR8G8B8A8_UINT},
+ DatumFormatData{
+ "i16vec2",
+ FormatType::kR16G16_SINT})); // NOLINT(whitespace/parens)
+
+INSTANTIATE_TEST_SUITE_P(
+ DatumTypeFormat2,
+ DatumTypeTestFormat,
+ testing::Values(DatumFormatData{"i16vec3", FormatType::kR16G16B16_SINT},
+ DatumFormatData{"i16vec4", FormatType::kR16G16B16A16_SINT},
+ DatumFormatData{"u16vec2", FormatType::kR16G16_UINT},
+ DatumFormatData{"u16vec3", FormatType::kR16G16B16_UINT},
+ DatumFormatData{"u16vec4", FormatType::kR16G16B16A16_UINT},
+ DatumFormatData{"i64vec2", FormatType::kR64G64_SINT},
+ DatumFormatData{"i64vec3", FormatType::kR64G64B64_SINT},
+ DatumFormatData{"i64vec4", FormatType::kR64G64B64A64_SINT},
+ DatumFormatData{"u64vec2", FormatType::kR64G64_UINT},
+ DatumFormatData{"u64vec3", FormatType::kR64G64B64_UINT},
+ DatumFormatData{"u64vec4", FormatType::kR64G64B64A64_UINT},
+ DatumFormatData{"mat2", FormatType::kUnknown},
+ DatumFormatData{"mat2x2", FormatType::kUnknown},
+ DatumFormatData{"mat2x3", FormatType::kUnknown},
+ DatumFormatData{"mat2x4", FormatType::kUnknown},
+ DatumFormatData{"mat3", FormatType::kUnknown},
+ DatumFormatData{"mat3x2", FormatType::kUnknown},
+ DatumFormatData{"mat3x3", FormatType::kUnknown},
+ DatumFormatData{"mat3x4", FormatType::kUnknown},
+ DatumFormatData{"mat4", FormatType::kUnknown},
+ DatumFormatData{"mat4x2", FormatType::kUnknown},
+ DatumFormatData{"mat4x3", FormatType::kUnknown},
+ DatumFormatData{"mat4x4", FormatType::kUnknown},
+ DatumFormatData{"dmat2", FormatType::kUnknown},
+ DatumFormatData{"dmat2x2", FormatType::kUnknown},
+ DatumFormatData{"dmat2x3", FormatType::kUnknown},
+ DatumFormatData{"dmat2x4", FormatType::kUnknown},
+ DatumFormatData{"dmat3", FormatType::kUnknown},
+ DatumFormatData{"dmat3x2", FormatType::kUnknown},
+ DatumFormatData{"dmat3x3", FormatType::kUnknown},
+ DatumFormatData{"dmat3x4", FormatType::kUnknown},
+ DatumFormatData{"dmat4", FormatType::kUnknown},
+ DatumFormatData{"dmat4x2", FormatType::kUnknown},
+ DatumFormatData{"dmat4x3", FormatType::kUnknown},
+ DatumFormatData{
+ "dmat4x4",
+ FormatType::kUnknown})); // NOLINT(whitespace/parens)
+
} // namespace vkscript
} // namespace amber
diff --git a/src/vkscript/datum_type_test.cc b/src/vkscript/datum_type_test.cc
deleted file mode 100644
index 3e89444..0000000
--- a/src/vkscript/datum_type_test.cc
+++ /dev/null
@@ -1,90 +0,0 @@
-// 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/vkscript/datum_type.h"
-
-#include "gtest/gtest.h"
-
-namespace amber {
-namespace vkscript {
-
-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();
- EXPECT_EQ(test_data.format_type, fmt->GetFormatType());
-}
-INSTANTIATE_TEST_SUITE_P(
- DatumTypeTestFormatSamples,
- 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 vkscript
-} // namespace amber