|
| 1 | +/* |
| 2 | +* Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. |
| 3 | +* |
| 4 | +* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | +* copy of this software and associated documentation files (the "Software"), |
| 6 | +* to deal in the Software without restriction, including without limitation |
| 7 | +* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | +* and/or sell copies of the Software, and to permit persons to whom the |
| 9 | +* Software is furnished to do so, subject to the following conditions: |
| 10 | +* |
| 11 | +* The above copyright notice and this permission notice shall be included in |
| 12 | +* all copies or substantial portions of the Software. |
| 13 | +* |
| 14 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | +* DEALINGS IN THE SOFTWARE. |
| 21 | +*/ |
| 22 | +#pragma once |
| 23 | + |
| 24 | +#include "../../lssusd/usd_include_begin.h" |
| 25 | +#include <pxr/base/gf/vec3f.h> |
| 26 | +#include "../../lssusd/usd_include_end.h" |
| 27 | + |
| 28 | +#include <atomic> |
| 29 | + |
| 30 | +namespace dxvk { |
| 31 | + |
| 32 | +struct AtomicOriginCalc; // Forward declare |
| 33 | +struct OriginCalc { |
| 34 | + inline void compareAndSwap(const pxr::GfVec3f& vec3) { |
| 35 | + replaceMin(&vec3[0]); |
| 36 | + replaceMax(&vec3[0]); |
| 37 | + } |
| 38 | + inline void compareAndSwap(const OriginCalc& other) { |
| 39 | + replaceMin(other.min); |
| 40 | + replaceMax(other.max); |
| 41 | + } |
| 42 | + inline pxr::GfVec3f calc() const { |
| 43 | + return (pxr::GfVec3f(&min[0]) + pxr::GfVec3f(&max[0])) / 2; |
| 44 | + } |
| 45 | +private: |
| 46 | + static constexpr float fMax = std::numeric_limits<float>::max(); |
| 47 | + float min[3] = { fMax, fMax, fMax}; |
| 48 | + float max[3] = {-fMax,-fMax,-fMax}; |
| 49 | + inline void replaceMin(const float* const vec3) { |
| 50 | + min[0] = std::min(min[0],vec3[0]); |
| 51 | + min[1] = std::min(min[1],vec3[1]); |
| 52 | + min[2] = std::min(min[2],vec3[2]); |
| 53 | + } |
| 54 | + inline void replaceMax(const float* const vec3) { |
| 55 | + max[0] = std::max(max[0],vec3[0]); |
| 56 | + max[1] = std::max(max[1],vec3[1]); |
| 57 | + max[2] = std::max(max[2],vec3[2]); |
| 58 | + } |
| 59 | + friend AtomicOriginCalc; |
| 60 | +}; |
| 61 | + |
| 62 | +struct AtomicOriginCalc { |
| 63 | + inline void compareAndSwap(const pxr::GfVec3f& vec3) { |
| 64 | + replaceMin(&vec3[0]); |
| 65 | + replaceMax(&vec3[0]); |
| 66 | + } |
| 67 | + inline void compareAndSwap(const OriginCalc& other) { |
| 68 | + replaceMin(other.min); |
| 69 | + replaceMax(other.max); |
| 70 | + } |
| 71 | + inline pxr::GfVec3f calc() const { |
| 72 | + return (pxr::GfVec3f{min[0].load(),min[1].load(),min[2].load()} + |
| 73 | + pxr::GfVec3f{max[0].load(),max[1].load(),max[2].load()}) |
| 74 | + / 2; |
| 75 | + } |
| 76 | + inline void reset() { |
| 77 | + for(size_t i = 0; i < 3; ++i) { |
| 78 | + min[i].store(fMax); |
| 79 | + max[i].store(-fMax); |
| 80 | + } |
| 81 | + } |
| 82 | +private: |
| 83 | + static constexpr float fMax = std::numeric_limits<float>::max(); |
| 84 | + std::atomic<float> min[3] = { fMax, fMax, fMax}; |
| 85 | + std::atomic<float> max[3] = {-fMax,-fMax,-fMax}; |
| 86 | + inline void replaceMin(const float* const vec3) { |
| 87 | + swapIfLess(min[0],vec3[0]); |
| 88 | + swapIfLess(min[1],vec3[1]); |
| 89 | + swapIfLess(min[2],vec3[2]); |
| 90 | + } |
| 91 | + inline void replaceMax(const float* const vec3) { |
| 92 | + swapIfGreater(max[0],vec3[0]); |
| 93 | + swapIfGreater(max[1],vec3[1]); |
| 94 | + swapIfGreater(max[2],vec3[2]); |
| 95 | + } |
| 96 | + static inline void swapIfLess(std::atomic<float>& atomic, const float other) { |
| 97 | + float val = fMax; |
| 98 | + do { |
| 99 | + val = atomic.load(); |
| 100 | + } while(val > other && !atomic.compare_exchange_weak(val, other)); |
| 101 | + } |
| 102 | + static void swapIfGreater(std::atomic<float>& atomic, const float other) { |
| 103 | + float val = -fMax; |
| 104 | + do { |
| 105 | + val = atomic.load(); |
| 106 | + } while(val < other && !atomic.compare_exchange_weak(val, other)); |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +} |
0 commit comments