Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion android_test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
#include "amber/amber.h"

void android_main(struct android_app* /*state*/) {
amber::Amber amber;
amber::Amber amber(nullptr);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should create and pass in an empty options object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It expects a delegate pointer. Do you mean I should create a dummy delegate object here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right was thinking it was an AmberOptions object. No need to create a delegate here.

}
5 changes: 5 additions & 0 deletions docs/amber_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ BUFFER {name} DATA_TYPE {type} {STD140 | STD430} WIDTH {w} HEIGHT {h} \
BUFFER {name} DATA_TYPE {type} {STD140 | STD430} SIZE _size_in_items_ \
FILE BINARY {file_name}

# Defines a buffer which is filled with text data parsed from a file specified
# by `FILE`.
BUFFER {name} DATA_TYPE {type} {STD140 | STD430} SIZE _size_in_items_ \
FILE TEXT {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
10 changes: 7 additions & 3 deletions include/amber/amber.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,12 @@ struct Options {
/// If true, disables SPIR-V validation. If false, SPIR-V shaders will be
/// validated using the Validator component (spirv-val) from SPIRV-Tools.
bool disable_spirv_validation;
/// Delegate implementation
Delegate* delegate;
};

/// Main interface to the Amber environment.
class Amber {
public:
Amber();
explicit Amber(Delegate* delegate);
~Amber();

/// Parse the given |data| into the |recipe|.
Expand All @@ -161,6 +159,12 @@ class Amber {
amber::Result ExecuteWithShaderData(const amber::Recipe* recipe,
Options* opts,
const ShaderMap& shader_data);

/// Returns the delegate object.
Delegate* GetDelegate() const { return delegate_; }

private:
Delegate* delegate_;
};

} // namespace amber
Expand Down
20 changes: 7 additions & 13 deletions samples/amber.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class SampleDelegate : public amber::Delegate {
#else
return amber::Result("PNG support is not enabled in compile options.");
#endif // AMBER_ENABLE_LODEPNG
} else if (file_type == amber::BufferDataFileType::kBinary) {
} else {
auto data = ReadFile(path_ + file_name);
if (data.empty())
return amber::Result("Failed to load buffer data " + file_name);
Expand All @@ -349,11 +349,6 @@ class SampleDelegate : public amber::Delegate {

buffer->width = 1;
buffer->height = 1;

} else {
assert(file_type == amber::BufferDataFileType::kText);

// TODO(asuonpaa): Read text file and pass it to parser.
}

return {};
Expand Down Expand Up @@ -418,6 +413,7 @@ std::string disassemble(const std::string& env,
int main(int argc, const char** argv) {
std::vector<std::string> args(argv, argv + argc);
Options options;
SampleDelegate delegate;

if (!ParseArgs(args, &options)) {
std::cerr << "Failed to parse arguments." << std::endl;
Expand Down Expand Up @@ -457,7 +453,10 @@ int main(int argc, const char** argv) {
continue;
}

amber::Amber am;
// Parse file path and set it for delegate to use when loading buffer data.
delegate.SetScriptPath(file.substr(0, file.find_last_of("/\\") + 1));

amber::Amber am(&delegate);
std::unique_ptr<amber::Recipe> recipe = amber::MakeUnique<amber::Recipe>();

result = am.Parse(data, recipe.get());
Expand All @@ -478,7 +477,6 @@ int main(int argc, const char** argv) {
if (options.parse_only)
return 0;

SampleDelegate delegate;
if (options.log_graphics_calls)
delegate.SetLogGraphicsCalls(true);
if (options.log_graphics_calls_time)
Expand All @@ -492,7 +490,6 @@ int main(int argc, const char** argv) {
amber_options.execution_type = options.pipeline_create_only
? amber::ExecutionType::kPipelineCreateOnly
: amber::ExecutionType::kExecute;
amber_options.delegate = &delegate;
amber_options.disable_spirv_validation = options.disable_spirv_validation;

std::set<std::string> required_features;
Expand Down Expand Up @@ -568,10 +565,7 @@ int main(int argc, const char** argv) {
const auto* recipe = recipe_data_elem.recipe.get();
const auto& file = recipe_data_elem.file;

// Parse file path and set it for delegate to use when loading buffer data.
delegate.SetScriptPath(file.substr(0, file.find_last_of("/\\") + 1));

amber::Amber am;
amber::Amber am(&delegate);
result = am.Execute(recipe, &amber_options);
if (!result.IsSuccess()) {
std::cerr << file << ": " << result.Error() << std::endl;
Expand Down
26 changes: 14 additions & 12 deletions src/amber.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ Options::Options()
: engine(amber::EngineType::kEngineTypeVulkan),
config(nullptr),
execution_type(ExecutionType::kExecute),
disable_spirv_validation(false),
delegate(nullptr) {}
disable_spirv_validation(false) {}

Options::~Options() = default;

Expand All @@ -83,7 +82,7 @@ BufferInfo& BufferInfo::operator=(const BufferInfo&) = default;

Delegate::~Delegate() = default;

Amber::Amber() = default;
Amber::Amber(Delegate* delegate) : delegate_(delegate) {}

Amber::~Amber() = default;

Expand All @@ -93,9 +92,9 @@ amber::Result Amber::Parse(const std::string& input, amber::Recipe* recipe) {

std::unique_ptr<Parser> parser;
if (input.substr(0, 7) == "#!amber")
parser = MakeUnique<amberscript::Parser>();
parser = MakeUnique<amberscript::Parser>(GetDelegate());
else
parser = MakeUnique<vkscript::Parser>();
parser = MakeUnique<vkscript::Parser>(GetDelegate());

Result r = parser->Parse(input);
if (!r.IsSuccess())
Expand All @@ -113,6 +112,7 @@ namespace {
// pointer is borrowed, and should not be freed.
Result CreateEngineAndCheckRequirements(const Recipe* recipe,
Options* opts,
Delegate* delegate,
std::unique_ptr<Engine>* engine_ptr,
Script** script_ptr) {
if (!recipe)
Expand All @@ -131,10 +131,10 @@ Result CreateEngineAndCheckRequirements(const Recipe* recipe,

// Engine initialization checks requirements. Current backends don't do
// much else. Refactor this if they end up doing to much here.
Result r = engine->Initialize(opts->config, opts->delegate,
script->GetRequiredFeatures(),
script->GetRequiredInstanceExtensions(),
script->GetRequiredDeviceExtensions());
Result r =
engine->Initialize(opts->config, delegate, script->GetRequiredFeatures(),
script->GetRequiredInstanceExtensions(),
script->GetRequiredDeviceExtensions());
if (!r.IsSuccess())
return r;

Expand All @@ -150,7 +150,8 @@ amber::Result Amber::AreAllRequirementsSupported(const amber::Recipe* recipe,
std::unique_ptr<Engine> engine;
Script* script = nullptr;

return CreateEngineAndCheckRequirements(recipe, opts, &engine, &script);
return CreateEngineAndCheckRequirements(recipe, opts, GetDelegate(), &engine,
&script);
}

amber::Result Amber::Execute(const amber::Recipe* recipe, Options* opts) {
Expand All @@ -163,14 +164,15 @@ amber::Result Amber::ExecuteWithShaderData(const amber::Recipe* recipe,
const ShaderMap& shader_data) {
std::unique_ptr<Engine> engine;
Script* script = nullptr;
Result r = CreateEngineAndCheckRequirements(recipe, opts, &engine, &script);
Result r = CreateEngineAndCheckRequirements(recipe, opts, GetDelegate(),
&engine, &script);
if (!r.IsSuccess())
return r;
script->SetSpvTargetEnv(opts->spv_env);

Executor executor;
Result executor_result =
executor.Execute(engine.get(), script, shader_data, opts);
executor.Execute(engine.get(), script, shader_data, opts, GetDelegate());
// Hold the executor result until the extractions are complete. This will let
// us dump any buffers requested even on failure.

Expand Down
Loading