diff options
author | Jaebaek Seo <duke.acacia@gmail.com> | 2019-02-19 23:29:08 +0900 |
---|---|---|
committer | dan sinclair <dj2@everburning.com> | 2019-02-19 09:29:08 -0500 |
commit | b3a778d1a66f34025878576b0be5e6a5c18c8b94 (patch) | |
tree | 13338d962c0fdf4c7bbcc16614bab51f3b9a10af /src | |
parent | 146a323c493ef0ec5705b66724f2a3636a4f1748 (diff) | |
download | amber-b3a778d1a66f34025878576b0be5e6a5c18c8b94.tar.gz |
Vulkan: support buffer dump (#269)
This CL adds support to dump the contents of compute buffers to a file.
Fixes #36
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/amber.cc | 29 | ||||
-rw-r--r-- | src/descriptor_set_and_binding_parser.cc | 66 | ||||
-rw-r--r-- | src/descriptor_set_and_binding_parser.h | 51 | ||||
-rw-r--r-- | src/descriptor_set_and_binding_parser_test.cc | 110 |
5 files changed, 258 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c1d57d9..db63393 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,6 +19,7 @@ set(AMBER_SOURCES command.cc command_data.cc datum_type.cc + descriptor_set_and_binding_parser.cc engine.cc executor.cc format.cc @@ -76,6 +77,7 @@ if (${AMBER_ENABLE_TESTS}) amberscript/parser_test.cc buffer_test.cc command_data_test.cc + descriptor_set_and_binding_parser_test.cc executor_test.cc format_parser_test.cc pipeline_test.cc diff --git a/src/amber.cc b/src/amber.cc index 1942b46..1506112 100644 --- a/src/amber.cc +++ b/src/amber.cc @@ -14,10 +14,13 @@ #include "amber/amber.h" +#include <cctype> +#include <cstdlib> #include <memory> #include <string> #include "src/amberscript/parser.h" +#include "src/descriptor_set_and_binding_parser.h" #include "src/engine.h" #include "src/executor.h" #include "src/make_unique.h" @@ -97,6 +100,32 @@ amber::Result Amber::ExecuteWithShaderData(const amber::Recipe* recipe, engine->Shutdown(); return r; } + + continue; + } + + DescriptorSetAndBindingParser desc_set_and_binding_parser; + r = desc_set_and_binding_parser.Parse(buffer_info.buffer_name); + if (!r.IsSuccess()) { + engine->Shutdown(); + return r; + } + + ResourceInfo info = ResourceInfo(); + r = engine->GetDescriptorInfo( + desc_set_and_binding_parser.GetDescriptorSet(), + desc_set_and_binding_parser.GetBinding(), &info); + if (!r.IsSuccess()) { + engine->Shutdown(); + return r; + } + + const uint8_t* ptr = static_cast<const uint8_t*>(info.cpu_memory); + auto& values = buffer_info.values; + for (size_t i = 0; i < info.size_in_bytes; ++i) { + values.emplace_back(); + values.back().SetIntValue(*ptr); + ++ptr; } } diff --git a/src/descriptor_set_and_binding_parser.cc b/src/descriptor_set_and_binding_parser.cc new file mode 100644 index 0000000..0d2a1ed --- /dev/null +++ b/src/descriptor_set_and_binding_parser.cc @@ -0,0 +1,66 @@ +// 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/descriptor_set_and_binding_parser.h" + +#include <iostream> +#include "src/tokenizer.h" + +namespace amber { + +DescriptorSetAndBindingParser::DescriptorSetAndBindingParser() = default; + +DescriptorSetAndBindingParser::~DescriptorSetAndBindingParser() = default; + +Result DescriptorSetAndBindingParser::Parse(const std::string& buffer_id) { + Tokenizer t(buffer_id); + auto token = t.NextToken(); + if (token->IsInteger()) { + if (token->AsInt32() < 0) { + return Result( + "Descriptor set and binding for a buffer must be non-negative " + "integer, but you gave: " + + token->ToOriginalString()); + } + + uint32_t val = token->AsUint32(); + token = t.NextToken(); + if (token->IsEOS() || token->IsEOL()) { + descriptor_set_ = 0; + binding_ = val; + return {}; + } + + descriptor_set_ = val; + } else { + descriptor_set_ = 0; + } + + std::cout << token->AsString() << std::endl; + if (!token->IsString() || token->AsString() != ",") { + return Result("Invalid buffer id: " + buffer_id); + } + + token = t.NextToken(); + if (!token->IsInteger() || token->AsInt32() < 0) { + return Result( + "Binding for a buffer must be non-negative integer, but you gave: " + + token->ToOriginalString()); + } + + binding_ = token->AsUint32(); + return {}; +} + +} // namespace amber diff --git a/src/descriptor_set_and_binding_parser.h b/src/descriptor_set_and_binding_parser.h new file mode 100644 index 0000000..44a412a --- /dev/null +++ b/src/descriptor_set_and_binding_parser.h @@ -0,0 +1,51 @@ +// 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. + +#ifndef SRC_DESCRIPTOR_SET_AND_BINDING_PARSER_H_ +#define SRC_DESCRIPTOR_SET_AND_BINDING_PARSER_H_ + +#include <string> + +#include "amber/result.h" + +namespace amber { + +/// Class for descriptor set and binding parser. +class DescriptorSetAndBindingParser { + public: + DescriptorSetAndBindingParser(); + ~DescriptorSetAndBindingParser(); + + /// Run the parser against |buffer_id|. The result is success if the parse + /// completes correctly. |buffer_id| must be non-empty string. It must + /// be a single non-negative integer or two those integers separated by + /// ':'. For example, ":0", "1", and "2:3" are valid strings for |buffer_id|, + /// but "", "-4", ":-5", ":", "a", and "b:c" are invalid strings for + /// |buffer_id|. + Result Parse(const std::string& buffer_id); + + /// Return descriptor set that is the result of Parse(). + uint32_t GetDescriptorSet() const { return descriptor_set_; } + + /// Return binding that is the result of Parse(). + uint32_t GetBinding() const { return binding_; } + + private: + uint32_t descriptor_set_ = 0; + uint32_t binding_ = 0; +}; + +} // namespace amber + +#endif // SRC_DESCRIPTOR_SET_AND_BINDING_PARSER_H_ diff --git a/src/descriptor_set_and_binding_parser_test.cc b/src/descriptor_set_and_binding_parser_test.cc new file mode 100644 index 0000000..139a7ef --- /dev/null +++ b/src/descriptor_set_and_binding_parser_test.cc @@ -0,0 +1,110 @@ +// 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 parseried. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/descriptor_set_and_binding_parser.h" + +#include "gtest/gtest.h" + +namespace amber { + +using DescriptorSetAndBindingParserTest = testing::Test; + +TEST_F(DescriptorSetAndBindingParserTest, CommaAndBinding) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse(",1234"); + ASSERT_TRUE(r.IsSuccess()) << r.Error(); + + EXPECT_EQ(0, parser.GetDescriptorSet()); + EXPECT_EQ(1234, parser.GetBinding()); +} + +TEST_F(DescriptorSetAndBindingParserTest, Binding) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse("1234"); + ASSERT_TRUE(r.IsSuccess()) << r.Error(); + + EXPECT_EQ(0, parser.GetDescriptorSet()); + EXPECT_EQ(1234, parser.GetBinding()); +} + +TEST_F(DescriptorSetAndBindingParserTest, DescSetAndBinding) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse("1234,5678"); + ASSERT_TRUE(r.IsSuccess()) << r.Error(); + + EXPECT_EQ(1234, parser.GetDescriptorSet()); + EXPECT_EQ(5678, parser.GetBinding()); +} + +TEST_F(DescriptorSetAndBindingParserTest, EmptyBufferId) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse(""); + EXPECT_EQ("Invalid buffer id: ", r.Error()); +} + +TEST_F(DescriptorSetAndBindingParserTest, InvalidCharacter) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse("abcd"); + EXPECT_EQ("Invalid buffer id: abcd", r.Error()); +} + +TEST_F(DescriptorSetAndBindingParserTest, InvalidCharacterBetweenTwoNumbers) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse("1234a5678"); + EXPECT_EQ("Invalid buffer id: 1234a5678", r.Error()); +} + +TEST_F(DescriptorSetAndBindingParserTest, InvalidCharacterAfterComma) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse("1234,a5678"); + EXPECT_EQ( + "Binding for a buffer must be non-negative integer, but you gave: a5678", + r.Error()); +} + +TEST_F(DescriptorSetAndBindingParserTest, NegativeDescSet) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse("-1234,5678"); + EXPECT_EQ( + "Descriptor set and binding for a buffer must be non-negative integer, " + "but you gave: -1234", + r.Error()); +} + +TEST_F(DescriptorSetAndBindingParserTest, NegativeBindingAfterComma) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse(",-1234"); + EXPECT_EQ( + "Binding for a buffer must be non-negative integer, but you gave: -1234", + r.Error()); +} + +TEST_F(DescriptorSetAndBindingParserTest, NegativeBinding) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse("-1234"); + EXPECT_EQ( + "Descriptor set and binding for a buffer must be non-negative integer, " + "but you gave: -1234", + r.Error()); +} + +TEST_F(DescriptorSetAndBindingParserTest, DescSetAndNegativeBinding) { + DescriptorSetAndBindingParser parser; + Result r = parser.Parse("1234,-5678"); + EXPECT_EQ( + "Binding for a buffer must be non-negative integer, but you gave: -5678", + r.Error()); +} + +} // namespace amber |