diff options
| author | Bryan Galdrikian <[email protected]> | 2017-02-24 09:32:20 -0800 |
|---|---|---|
| committer | Bryan Galdrikian <[email protected]> | 2017-02-24 09:32:20 -0800 |
| commit | e1bf674c16e3c8472b29574159c789cd3f0c64e0 (patch) | |
| tree | 9f0cfce09c71a2c27ff19589fcad6cd83504477c /samples/SampleBase/utils | |
| parent | first commit (diff) | |
| download | blast-e1bf674c16e3c8472b29574159c789cd3f0c64e0.tar.xz blast-e1bf674c16e3c8472b29574159c789cd3f0c64e0.zip | |
Updating to [email protected] and [email protected] with a new directory structure.
NvBlast folder is gone, files have been moved to top level directory. README is changed to reflect this.
Diffstat (limited to 'samples/SampleBase/utils')
| -rw-r--r-- | samples/SampleBase/utils/PxInputDataFromPxFileBuf.h | 51 | ||||
| -rw-r--r-- | samples/SampleBase/utils/SampleProfiler.cpp | 223 | ||||
| -rw-r--r-- | samples/SampleBase/utils/SampleProfiler.h | 79 | ||||
| -rw-r--r-- | samples/SampleBase/utils/SampleTime.h | 58 | ||||
| -rw-r--r-- | samples/SampleBase/utils/UIHelpers.h | 56 | ||||
| -rw-r--r-- | samples/SampleBase/utils/Utils.cpp | 13 | ||||
| -rw-r--r-- | samples/SampleBase/utils/Utils.h | 91 |
7 files changed, 571 insertions, 0 deletions
diff --git a/samples/SampleBase/utils/PxInputDataFromPxFileBuf.h b/samples/SampleBase/utils/PxInputDataFromPxFileBuf.h new file mode 100644 index 0000000..dfa8260 --- /dev/null +++ b/samples/SampleBase/utils/PxInputDataFromPxFileBuf.h @@ -0,0 +1,51 @@ +/* +* Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef PXINPUTDATAFROMPXFILEBUF_H +#define PXINPUTDATAFROMPXFILEBUF_H + +#include <PsFileBuffer.h> + + +// Copied from APEX +class PxInputDataFromPxFileBuf : public physx::PxInputData +{ +public: + PxInputDataFromPxFileBuf(physx::PxFileBuf& fileBuf) : mFileBuf(fileBuf) {} + + // physx::PxInputData interface + virtual uint32_t getLength() const + { + return mFileBuf.getFileLength(); + } + + virtual void seek(uint32_t offset) + { + mFileBuf.seekRead(offset); + } + + virtual uint32_t tell() const + { + return mFileBuf.tellRead(); + } + + // physx::PxInputStream interface + virtual uint32_t read(void* dest, uint32_t count) + { + return mFileBuf.read(dest, count); + } + + PX_NOCOPY(PxInputDataFromPxFileBuf) +private: + physx::PxFileBuf& mFileBuf; +}; + + +#endif //PXINPUTDATAFROMPXFILEBUF_H
\ No newline at end of file diff --git a/samples/SampleBase/utils/SampleProfiler.cpp b/samples/SampleBase/utils/SampleProfiler.cpp new file mode 100644 index 0000000..4df23fd --- /dev/null +++ b/samples/SampleBase/utils/SampleProfiler.cpp @@ -0,0 +1,223 @@ +/* +* Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + + +#include "SampleProfiler.h" +#include <map> +#include <iostream> +#include <fstream> +#include <stack> + +using namespace std::chrono; + +struct ProfileData +{ + steady_clock::time_point start; + + microseconds time; + microseconds prevTime; + microseconds maxTime; + uint32_t calls; + uint32_t prevCalls; + + ProfileData() : time(0), prevTime(0), maxTime(0), calls(0), prevCalls(0) + {} +}; + +struct Node +{ + ProfileData data; + std::map<const char*, Node> childs; + Node* parent; +}; + +static std::map<const char*, Node> s_roots; +static Node* s_currentNode; +static bool s_beginEndMismatch; +static microseconds s_overhead; +static microseconds s_prevOverhead; + +void SampleProfilerInit() +{ + s_roots.clear(); + s_currentNode = nullptr; + s_beginEndMismatch = false; + s_overhead = microseconds(); +} + +void SampleProfilerBegin(const char* name) +{ + auto start = steady_clock::now(); + { + Node* parent = s_currentNode; + if (s_currentNode == nullptr) + { + s_currentNode = &s_roots[name]; + } + else + { + s_currentNode = &s_currentNode->childs[name]; + } + s_currentNode->parent = parent; + s_currentNode->data.calls++; + s_currentNode->data.start = steady_clock::now(); + } + s_overhead += duration_cast<microseconds>(steady_clock::now() - start); +} + +void SampleProfilerEnd() +{ + auto start = steady_clock::now(); + { + if (s_currentNode) + { + auto& data = s_currentNode->data; + data.time += duration_cast<microseconds>(steady_clock::now() - data.start); + data.maxTime = data.time > data.maxTime ? data.time : data.maxTime; + s_currentNode = s_currentNode->parent; + } + else + { + s_beginEndMismatch = true; + } + } + s_overhead += duration_cast<microseconds>(steady_clock::now() - start); +} + +struct SampleProfilerTreeIteratorImpl final : public SampleProfilerTreeIterator +{ + struct StackNode + { + Node* node; + const char* name; + }; + + SampleProfilerTreeIteratorImpl(std::map<const char*, Node>& roots) + { + for (auto& root : roots) + { + m_stack.emplace(StackNode { &root.second, root.first }); + } + + next(); + } + + virtual const Data* data() const override + { + return m_valid ? &m_data : nullptr; + } + + Node* node() + { + return m_node; + } + + virtual bool isDone() const + { + return !m_valid; + } + + virtual void next() + { + if (!m_stack.empty()) + { + auto& e = m_stack.top(); + m_stack.pop(); + m_node = e.node; + m_data.depth = 0; + m_data.hash = (uint64_t)m_node; + for (const Node* p = m_node; p != nullptr; p = p->parent) + { + m_data.depth++; + } + m_data.name = e.name; + m_data.calls = m_node->data.prevCalls; + m_data.time = m_node->data.prevTime; + m_data.maxTime = m_node->data.maxTime; + m_data.hasChilds = !m_node->childs.empty(); + + for (auto it = m_node->childs.rbegin(); it != m_node->childs.rend(); ++it) + { + m_stack.emplace(StackNode { &(*it).second, (*it).first }); + } + m_valid = true; + } + else + { + m_valid = false; + } + } + + virtual void release() + { + delete this; + } + + bool m_valid; + Data m_data; + Node* m_node; + std::stack<StackNode > m_stack; +}; + +void SampleProfilerReset() +{ + for (SampleProfilerTreeIteratorImpl it(s_roots); !it.isDone(); it.next()) + { + auto& data = it.node()->data; + data.prevTime = data.time; + data.prevCalls = data.calls; + data.time = microseconds(); + data.calls = 0; + } + s_currentNode = nullptr; + s_beginEndMismatch = false; + s_prevOverhead = s_overhead; + s_overhead = microseconds(); +} + +bool SampleProfilerIsValid() +{ + return !s_beginEndMismatch; +} + +microseconds SampleProfilerGetOverhead() +{ + return s_prevOverhead; +} + +SampleProfilerTreeIterator* SampleProfilerCreateTreeIterator() +{ + return SampleProfilerIsValid() ? new SampleProfilerTreeIteratorImpl(s_roots) : nullptr; +} + +void SampleProfilerDumpToFile(const char* path) +{ + std::ofstream myfile(path, std::ios_base::out); + if (myfile.is_open()) + { + if (s_beginEndMismatch) + { + myfile << "Error: Begin/End Mismatch.\n"; + } + else + { + myfile << "[Root]\n"; + for(SampleProfilerTreeIteratorImpl it(s_roots); !it.isDone(); it.next()) + { + auto data = it.data(); + for (uint32_t i = 0; i < data->depth; ++i) + myfile << "\t"; + myfile << data->name << " --> calls: " << data->calls << ", total: " << data->time.count() * 0.001 << "ms\n"; + } + } + + myfile.close(); + } +} diff --git a/samples/SampleBase/utils/SampleProfiler.h b/samples/SampleBase/utils/SampleProfiler.h new file mode 100644 index 0000000..1ea3663 --- /dev/null +++ b/samples/SampleBase/utils/SampleProfiler.h @@ -0,0 +1,79 @@ +/* +* Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef SAMPLEPROFILER_H +#define SAMPLEPROFILER_H + +#include <chrono> + +#if NV_PROFILE + +void SampleProfilerInit(); +void SampleProfilerBegin(const char* name); +void SampleProfilerEnd(); +void SampleProfilerReset(); + +struct SampleProfilerScoped +{ + SampleProfilerScoped(const char* name) + { + SampleProfilerBegin(name); + } + + ~SampleProfilerScoped() + { + SampleProfilerEnd(); + } +}; + +#define PROFILER_INIT() SampleProfilerInit() +#define PROFILER_BEGIN(x) SampleProfilerBegin(x) +#define PROFILER_END() SampleProfilerEnd() +#define PROFILER_SCOPED(x) SampleProfilerScoped __scopedProfiler__(x) +#define PROFILER_SCOPED_FUNCTION() SampleProfilerScoped __scopedProfiler__(__FUNCTION__) +#define PROFILER_RESET() SampleProfilerReset() + +#else + +#define PROFILER_INIT() +#define PROFILER_BEGIN(x) +#define PROFILER_END() +#define PROFILER_SCOPED(x) +#define PROFILER_SCOPED_FUNCTION() +#define PROFILER_RESET() + +#endif + +void SampleProfilerDumpToFile(const char* path); +bool SampleProfilerIsValid(); +std::chrono::microseconds SampleProfilerGetOverhead(); + +struct SampleProfilerTreeIterator +{ + struct Data + { + uint64_t hash; + const char* name; + bool hasChilds; + uint32_t depth; + std::chrono::microseconds time; + std::chrono::microseconds maxTime; + uint32_t calls; + }; + + virtual const Data* data() const = 0; + virtual bool isDone() const = 0; + virtual void next() = 0; + virtual void release() = 0; +}; + +SampleProfilerTreeIterator* SampleProfilerCreateTreeIterator(); + +#endif //SAMPLEPROFILER_H
\ No newline at end of file diff --git a/samples/SampleBase/utils/SampleTime.h b/samples/SampleBase/utils/SampleTime.h new file mode 100644 index 0000000..c62ced2 --- /dev/null +++ b/samples/SampleBase/utils/SampleTime.h @@ -0,0 +1,58 @@ +/* +* Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef SAMPLE_TIME_H +#define SAMPLE_TIME_H + +#include <stdint.h> + +class Time +{ +public: + Time() : m_lastTickCount(getTimeTicks()) {} + + double Time::getElapsedSeconds() + { + const int64_t lastTickCount = m_lastTickCount; + m_lastTickCount = getTimeTicks(); + return (m_lastTickCount - lastTickCount) * s_secondsPerTick; + } + + double Time::peekElapsedSeconds() const + { + return (getTimeTicks() - m_lastTickCount) * s_secondsPerTick; + } + + double Time::getLastTime() const + { + return m_lastTickCount * s_secondsPerTick; + } + +private: + static double getTickDuration() + { + LARGE_INTEGER a; + QueryPerformanceFrequency(&a); + return 1.0 / (double)a.QuadPart; + } + + int64_t getTimeTicks() const + { + LARGE_INTEGER a; + QueryPerformanceCounter(&a); + return a.QuadPart; + } + + int64_t m_lastTickCount; + static const double s_secondsPerTick; +}; + + +#endif
\ No newline at end of file diff --git a/samples/SampleBase/utils/UIHelpers.h b/samples/SampleBase/utils/UIHelpers.h new file mode 100644 index 0000000..b23eb84 --- /dev/null +++ b/samples/SampleBase/utils/UIHelpers.h @@ -0,0 +1,56 @@ +/* +* Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef UI_HELPERS_H +#define UI_HELPERS_H + +#include "imgui.h" +#include "PxVec3.h" + + +static void ImGui_DragFloat3Dir(const char* label, float v[3]) +{ + if (ImGui::Button("Normalize")) + { + ((physx::PxVec3*)v)->normalize(); + } + ImGui::SameLine(); + ImGui::DragFloat3(label, v); +}; + + +#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR))) + +template<int valuesCount = 90> +class PlotLinesInstance +{ +public: + PlotLinesInstance() + { + memset(m_values, 0, sizeof(float) * valuesCount); + } + + void plot(const char* label, float newValue, const char* overlay_text, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 80)) + { + for (; ImGui::GetTime() > m_time + 1.0f / 60.0f; m_time += 1.0f / 60.0f) + { + m_values[m_offset] = newValue; + m_offset = (m_offset + 1) % valuesCount; + } + ImGui::PlotLines(label, m_values, valuesCount, m_offset, overlay_text, scale_min, scale_max, graph_size); + } + +private: + float m_values[valuesCount]; + int m_offset; + float m_time = ImGui::GetTime(); +}; + +#endif //UI_HELPERS_H
\ No newline at end of file diff --git a/samples/SampleBase/utils/Utils.cpp b/samples/SampleBase/utils/Utils.cpp new file mode 100644 index 0000000..a271137 --- /dev/null +++ b/samples/SampleBase/utils/Utils.cpp @@ -0,0 +1,13 @@ +#include "Utils.h" + +#include <string> +#include <cstdarg> + +HRESULT messagebox_printf(const char* caption, UINT mb_type, const char* format, ...) +{ + va_list args; + va_start(args, format); + char formatted_text[512]; + _vsnprintf(formatted_text, 512, format, args); + return MessageBoxA(nullptr, formatted_text, caption, mb_type); +} diff --git a/samples/SampleBase/utils/Utils.h b/samples/SampleBase/utils/Utils.h new file mode 100644 index 0000000..5d4addc --- /dev/null +++ b/samples/SampleBase/utils/Utils.h @@ -0,0 +1,91 @@ +#ifndef UTILS_H +#define UTILS_H + +#include <DeviceManager.h> +#include <assert.h> + +#include "PxPreprocessor.h" + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// MACROS +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#ifndef V_RETURN +#define V_RETURN(x) \ + { \ + hr = (x); \ + if(FAILED(hr)) \ + { \ + return hr; \ + } \ + } +#endif + +#ifndef V +#define V(x) \ + { \ + HRESULT hr = (x); \ + _ASSERT(SUCCEEDED(hr)); \ + } +#endif + +#ifndef SAFE_RELEASE +#define SAFE_RELEASE(p) \ + { \ + if(p) \ + { \ + (p)->Release(); \ + (p) = NULL; \ + } \ + } +#endif + +#ifndef SAFE_DELETE +#define SAFE_DELETE(p) \ + { \ + if(p) \ + { \ + delete (p); \ + (p) = NULL; \ + } \ + } +#endif + +#define ASSERT_PRINT(cond, format, ...) \ + if(!(cond)) \ + { \ + messagebox_printf("Assertion Failed!", MB_OK | MB_ICONERROR, #cond "\n" format, __VA_ARGS__); \ + assert(cond); \ + } + +HRESULT messagebox_printf(const char* caption, UINT mb_type, const char* format, ...); + + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +static const char* strext(const char* str) +{ + const char* ext = NULL; // by default no extension found! + while (str) + { + str = strchr(str, '.'); + if (str) + { + str++; + ext = str; + } + } + return ext; +} + +static inline float lerp(float a, float b, float t) { return a + (b - a) * t; } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#endif
\ No newline at end of file |