-
Notifications
You must be signed in to change notification settings - Fork 65
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
|
@@ -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 { | ||
|
@@ -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); | ||
buffer->values.push_back(v); | ||
} | ||
|
||
buffer->width = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these set? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
@@ -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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a separate fix? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.