diff options
Diffstat (limited to 'src/vkscript')
-rw-r--r-- | src/vkscript/command_parser.h | 2 | ||||
-rw-r--r-- | src/vkscript/datum_type.cc | 76 | ||||
-rw-r--r-- | src/vkscript/datum_type.h | 81 | ||||
-rw-r--r-- | src/vkscript/datum_type_parser.h | 2 | ||||
-rw-r--r-- | src/vkscript/datum_type_test.cc | 90 |
5 files changed, 249 insertions, 2 deletions
diff --git a/src/vkscript/command_parser.h b/src/vkscript/command_parser.h index 1fde92e..680bdaa 100644 --- a/src/vkscript/command_parser.h +++ b/src/vkscript/command_parser.h @@ -22,10 +22,10 @@ #include "amber/result.h" #include "src/command.h" -#include "src/datum_type.h" #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 new file mode 100644 index 0000000..2f9ff97 --- /dev/null +++ b/src/vkscript/datum_type.cc @@ -0,0 +1,76 @@ +// 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 new file mode 100644 index 0000000..b229e20 --- /dev/null +++ b/src/vkscript/datum_type.h @@ -0,0 +1,81 @@ +// 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.h b/src/vkscript/datum_type_parser.h index b85bfd9..a4653d8 100644 --- a/src/vkscript/datum_type_parser.h +++ b/src/vkscript/datum_type_parser.h @@ -18,7 +18,7 @@ #include <string> #include "amber/result.h" -#include "src/datum_type.h" +#include "src/vkscript/datum_type.h" namespace amber { namespace vkscript { diff --git a/src/vkscript/datum_type_test.cc b/src/vkscript/datum_type_test.cc new file mode 100644 index 0000000..3e89444 --- /dev/null +++ b/src/vkscript/datum_type_test.cc @@ -0,0 +1,90 @@ +// 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 |