Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added implementation for loading buffer data from a binary file. #838

Merged
merged 3 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/amber_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ BUFFER {name} DATA_TYPE {type} {STD140 | STD430} SIZE _size_in_items_ \
BUFFER {name} DATA_TYPE {type} {STD140 | STD430} WIDTH {w} HEIGHT {h} \
{initializer}

# Defines a buffer which is filled with binary data from a file specified
# by `FILE`.
BUFFER {name} DATA_TYPE {type} {STD140 | STD430} SIZE _size_in_items_ \
FILE {file_name}

# Creates a buffer which will store the given `FORMAT` of data. These
# buffers are used as image and depth buffers in the `PIPELINE` commands.
# The buffer will be sized based on the `RENDER_SIZE` of the `PIPELINE`.
Expand Down
23 changes: 18 additions & 5 deletions samples/amber.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ bool ParseArgs(const std::vector<std::string>& args, Options* opts) {
return true;
}

std::string ReadFile(const std::string& input_file) {
std::vector<char> ReadFile(const std::string& input_file) {
FILE* file = nullptr;
#if defined(_MSC_VER)
fopen_s(&file, input_file.c_str(), "rb");
Expand Down Expand Up @@ -287,7 +287,7 @@ std::string ReadFile(const std::string& input_file) {
return {};
}

return std::string(data.begin(), data.end());
return data;
}

class SampleDelegate : public amber::Delegate {
Expand Down Expand Up @@ -337,9 +337,21 @@ class SampleDelegate : public amber::Delegate {
return r;
#endif // AMBER_ENABLE_LODEPNG

// TODO(asuonpaa): Try to load a binary format.
// Try to load a binary format.
auto data = ReadFile(path_ + file_name);
if (data.empty())
return amber::Result("Failed to load buffer data " + file_name);

return amber::Result("Failed to load buffer data " + file_name);
for (auto d : data) {
amber::Value v;
v.SetIntValue(d);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not need to match the DATA_TYPE of the buffer? If the buffer is all float data, why is this written as all Int's?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data is always loaded as byte format. There's no point converting the data here and then converting it back to bytes when loading the data into a buffer.

buffer->values.push_back(v);
}

buffer->width = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these set?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the width and height a buffer gets when initializing it inline. I'm matching it to avoid errors in buffer comparison.

buffer->height = 1;

return {};
}

private:
Expand Down Expand Up @@ -432,7 +444,8 @@ int main(int argc, const char** argv) {
};
std::vector<RecipeData> recipe_data;
for (const auto& file : options.input_filenames) {
auto data = ReadFile(file);
auto char_data = ReadFile(file);
auto data = std::string(char_data.begin(), char_data.end());
if (data.empty()) {
std::cerr << file << " is empty." << std::endl;
failures.push_back(file);
Expand Down
10 changes: 10 additions & 0 deletions src/amberscript/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,16 @@ Result Parser::ParseBufferInitializerSize(Buffer* buffer) {
return ParseBufferInitializerFill(buffer, size_in_items);
if (token->AsString() == "SERIES_FROM")
return ParseBufferInitializerSeries(buffer, size_in_items);
if (token->AsString() == "FILE") {
token = tokenizer_->NextToken();

if (!token->IsIdentifier())
return Result("invalid value for FILE");

buffer->SetDataFile(token->AsString());

return {};
}

return Result("invalid BUFFER initializer provided");
}
Expand Down
25 changes: 25 additions & 0 deletions src/amberscript/parser_buffer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1092,5 +1092,30 @@ TEST_F(AmberScriptParserTest, BufferDataFile) {
EXPECT_EQ("foo.png", buffers[0]->GetDataFile());
}

TEST_F(AmberScriptParserTest, BufferSsboMissingDataFile) {
std::string in = "BUFFER my_buffer DATA_TYPE float SIZE 10 FILE";

Parser parser;
Result r = parser.Parse(in);
ASSERT_FALSE(r.IsSuccess());

EXPECT_EQ("1: invalid value for FILE", r.Error());
}

TEST_F(AmberScriptParserTest, BufferSsboDataFile) {
std::string in = "BUFFER my_buffer DATA_TYPE int32 SIZE 10 FILE data.bin";

Parser parser;
Result r = parser.Parse(in);
ASSERT_TRUE(r.IsSuccess()) << r.Error();

auto script = parser.GetScript();
const auto& buffers = script->GetBuffers();
ASSERT_EQ(1U, buffers.size());

ASSERT_TRUE(buffers[0] != nullptr);
EXPECT_EQ("data.bin", buffers[0]->GetDataFile());
}

} // namespace amberscript
} // namespace amber
11 changes: 10 additions & 1 deletion src/executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,16 @@ Result Executor::Execute(Engine* engine,
if (!r.IsSuccess())
return r;

buf->SetData(info.values);
std::vector<uint8_t>* data = buf->ValuePtr();

data->clear();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a separate fix?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a more general solution that doesn't assume the destination buffer is in uint8 format. It works both with the png images and buffers of any format. Basically we keep the destination buffer format but directly copy the bytes and then adjust the element size of the destination buffer based on the format.

data->reserve(info.values.size());
for (auto v : info.values) {
data->push_back(v.AsUint8());
}

buf->SetElementCount(static_cast<uint32_t>(data->size()) /
buf->GetFormat()->SizeInBytes());
buf->SetWidth(info.width);
buf->SetHeight(info.height);
}
Expand Down
55 changes: 55 additions & 0 deletions tests/cases/buffer_load_binary.amber
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!amber
#
# Copyright 2020 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
#
# https://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.

SHADER compute compute_shader GLSL
#version 430

layout(set = 0, binding = 0) buffer block0 {
float in_data[12];
};

layout(set = 0, binding = 1) buffer block1 {
float out_data[12];
};

void main() {
for (int i = 0; i < 12; i++)
out_data[i] = in_data[i] * 2.0;
}
END

BUFFER buf0 DATA_TYPE vec4<float> SIZE 3 FILE vec4data.bin
asuonpaa marked this conversation as resolved.
Show resolved Hide resolved
BUFFER buf1 DATA_TYPE vec4<float> SIZE 3 FILL 0.0

BUFFER ref0 DATA_TYPE vec4<float> DATA
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0
END

BUFFER ref1 DATA_TYPE vec4<float> DATA
2.0 4.0 6.0 8.0 10.0 12.0 14.0 16.0 18.0 20.0 22.0 24.0
END

PIPELINE compute pipeline
ATTACH compute_shader

BIND BUFFER buf0 AS storage DESCRIPTOR_SET 0 BINDING 0
BIND BUFFER buf1 AS storage DESCRIPTOR_SET 0 BINDING 1
END

RUN pipeline 1 1 1

EXPECT buf0 EQ_BUFFER ref0
EXPECT buf1 EQ_BUFFER ref1
Binary file added tests/cases/vec4data.bin
Binary file not shown.