diff options
Diffstat (limited to 'samples')
-rw-r--r-- | samples/Android.mk | 3 | ||||
-rw-r--r-- | samples/CMakeLists.txt | 1 | ||||
-rw-r--r-- | samples/amber.cc | 28 | ||||
-rw-r--r-- | samples/timestamp.cc | 69 | ||||
-rw-r--r-- | samples/timestamp.h | 27 |
5 files changed, 126 insertions, 2 deletions
diff --git a/samples/Android.mk b/samples/Android.mk index 06f5fe9..bb8945f 100644 --- a/samples/Android.mk +++ b/samples/Android.mk @@ -23,7 +23,8 @@ LOCAL_SRC_FILES:= \ config_helper_vulkan.cc \ log.cc \ ppm.cc \ - png.cc + png.cc \ + timestamp.cc LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. $(LOCAL_PATH)/../include LOCAL_LDLIBS:=-landroid -lvulkan -llog LOCAL_CXXFLAGS:=-std=c++11 -fno-exceptions -fno-rtti -Werror -Wno-unknown-pragmas -DAMBER_ENGINE_VULKAN=1 diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 213d4c3..1a3183d 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -20,6 +20,7 @@ set(AMBER_SOURCES log.cc ppm.cc png.cc + timestamp.cc ${CMAKE_BINARY_DIR}/src/build-versions.h.fake ) diff --git a/samples/amber.cc b/samples/amber.cc index 79d0cee..19c323f 100644 --- a/samples/amber.cc +++ b/samples/amber.cc @@ -26,6 +26,7 @@ #include "samples/config_helper.h" #include "samples/png.h" #include "samples/ppm.h" +#include "samples/timestamp.h" #include "src/build-versions.h" #include "src/make_unique.h" @@ -46,6 +47,7 @@ struct Options { bool show_help = false; bool show_version_info = false; bool log_graphics_calls = false; + bool log_graphics_calls_time = false; amber::EngineType engine = amber::kEngineTypeVulkan; std::string spv_env; }; @@ -66,6 +68,7 @@ const char kUsage[] = R"(Usage: amber [options] SCRIPT [SCRIPTS...] -v <engine version> -- Engine version (eg, 1.1 for Vulkan). Default 1.0. -V, --version -- Output version information for Amber and libraries. --log-graphics-calls -- Log graphics API calls (only for Vulkan so far). + --log-graphics-calls-time -- Log timing of graphics API calls timing (Vulkan only). -h -- This help text. )"; @@ -161,6 +164,8 @@ bool ParseArgs(const std::vector<std::string>& args, Options* opts) { opts->quiet = true; } else if (arg == "--log-graphics-calls") { opts->log_graphics_calls = true; + } else if (arg == "--log-graphics-calls-time") { + opts->log_graphics_calls_time = true; } else if (arg.size() > 0 && arg[0] == '-') { std::cerr << "Unrecognized option " << arg << std::endl; return false; @@ -209,7 +214,8 @@ std::string ReadFile(const std::string& input_file) { class SampleDelegate : public amber::Delegate { public: - SampleDelegate() : log_graphics_calls_(false) {} + SampleDelegate() + : log_graphics_calls_(false), log_graphics_calls_time_(false) {} ~SampleDelegate() override = default; void Log(const std::string& message) override { @@ -222,8 +228,25 @@ class SampleDelegate : public amber::Delegate { log_graphics_calls_ = log_graphics_calls; } + bool LogGraphicsCallsTime() const override { + return log_graphics_calls_time_; + } + + void SetLogGraphicsCallsTime(bool log_graphics_calls_time) { + log_graphics_calls_time_ = log_graphics_calls_time; + if (log_graphics_calls_time) { + // Make sure regular logging is also enabled + log_graphics_calls_ = true; + } + } + + uint64_t GetTimestampNs() const override { + return timestamp::SampleGetTimestampNs(); + } + private: bool log_graphics_calls_; + bool log_graphics_calls_time_; }; } // namespace @@ -291,6 +314,9 @@ int main(int argc, const char** argv) { if (options.log_graphics_calls) { delegate.SetLogGraphicsCalls(true); } + if (options.log_graphics_calls_time) { + delegate.SetLogGraphicsCallsTime(true); + } amber::Options amber_options; amber_options.engine = options.engine; diff --git a/samples/timestamp.cc b/samples/timestamp.cc new file mode 100644 index 0000000..50c6539 --- /dev/null +++ b/samples/timestamp.cc @@ -0,0 +1,69 @@ +// 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 "samples/timestamp.h" + +#include <cassert> + +#if defined(_WIN32) || defined(_WIN64) +#define SAMPLE_PLATFORM_WINDOWS 1 +#define SAMPLE_PLATFORM_POSIX 0 +#elif defined(__linux__) || defined(__APPLE__) +#define SAMPLE_PLATFORM_POSIX 1 +#define SAMPLE_PLATFORM_WINDOWS 0 +#endif + +#if SAMPLE_PLATFORM_WINDOWS +#include <windows.h> +#elif SAMPLE_PLATFORM_POSIX +#include <time.h> +#else +#error "Unknown platform" +#endif + +namespace timestamp { + +uint64_t SampleGetTimestampNs() { + uint64_t timestamp = 0; + +#if SAMPLE_PLATFORM_WINDOWS + + LARGE_INTEGER tick_per_seconds; + if (!QueryPerformanceFrequency(&tick_per_seconds)) { + return 0; + } + LARGE_INTEGER ticks; + if (!QueryPerformanceCounter(&ticks)) { + return 0; + } + double tick_duration_ns = static_cast<double>(1.0e9) / + static_cast<double>(tick_per_seconds.QuadPart); + timestamp = uint64_t(static_cast<double>(ticks.QuadPart) * tick_duration_ns); + +#elif SAMPLE_PLATFORM_POSIX + + struct timespec time; + if (clock_gettime(CLOCK_MONOTONIC, &time)) { + return 0; + } + timestamp = static_cast<uint64_t>((time.tv_sec * 1000000000) + time.tv_nsec); + +#else +#error "Implement timestamp::SampleGetTimestampNs" +#endif + + return timestamp; +} + +} // namespace timestamp diff --git a/samples/timestamp.h b/samples/timestamp.h new file mode 100644 index 0000000..81b0f55 --- /dev/null +++ b/samples/timestamp.h @@ -0,0 +1,27 @@ +// 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 SAMPLES_TIMESTAMP_H_ +#define SAMPLES_TIMESTAMP_H_ + +#include "amber/amber.h" + +namespace timestamp { + +/// Return a timestamp in nanoseconds. +uint64_t SampleGetTimestampNs(); + +} // namespace timestamp + +#endif // SAMPLES_TIMESTAMP_H_ |