提交 faac34d1 编写于 作者: M Matt Pharr

Switch to std::hash for default hasher for InternCache

上级 4607f0e4
......@@ -76,20 +76,18 @@ std::string LightBase::BaseToString() const {
mediumInterface, renderFromLight);
}
InternCache<DenselySampledSpectrum, DenselySampledSpectrum::Hash>
*LightBase::spectrumCache;
InternCache<DenselySampledSpectrum> *LightBase::spectrumCache;
const DenselySampledSpectrum *LightBase::LookupSpectrum(Spectrum s) {
// Initialize _spectrumCache_ on first call
static std::mutex mutex;
mutex.lock();
if (!spectrumCache)
spectrumCache =
new InternCache<DenselySampledSpectrum, DenselySampledSpectrum::Hash>(
spectrumCache = new InternCache<DenselySampledSpectrum>(
#ifdef PBRT_BUILD_GPU_RENDERER
Options->useGPU ? Allocator(&CUDATrackedMemoryResource::singleton) :
Options->useGPU ? Allocator(&CUDATrackedMemoryResource::singleton) :
#endif
Allocator{});
Allocator{});
mutex.unlock();
// Return unique _DenselySampledSpectrum_ from intern cache for _s_
......
......@@ -25,6 +25,7 @@
#include <pbrt/util/transform.h>
#include <pbrt/util/vecmath.h>
#include <functional>
#include <memory>
#include <string>
......@@ -181,8 +182,7 @@ class LightBase {
LightType type;
Transform renderFromLight;
MediumInterface mediumInterface;
static InternCache<DenselySampledSpectrum, DenselySampledSpectrum::Hash>
*spectrumCache;
static InternCache<DenselySampledSpectrum> *spectrumCache;
};
// PointLight Definition
......@@ -822,4 +822,16 @@ inline LightType Light::Type() const {
} // namespace pbrt
namespace std {
template <>
struct hash<pbrt::Light> {
PBRT_CPU_GPU
size_t operator()(pbrt::Light light) const noexcept {
return pbrt::Hash(light.ptr());
}
};
} // namespace std
#endif // PBRT_LIGHTS_H
......@@ -22,12 +22,6 @@
namespace pbrt {
// LightHash Definition
struct LightHash {
PBRT_CPU_GPU
size_t operator()(Light light) const { return Hash(light.ptr()); }
};
// UniformLightSampler Definition
class UniformLightSampler {
public:
......@@ -100,7 +94,7 @@ class PowerLightSampler {
private:
// PowerLightSampler Private Members
pstd::vector<Light> lights;
HashMap<Light, size_t, LightHash> lightToIndex;
HashMap<Light, size_t> lightToIndex;
AliasTable aliasTable;
};
......@@ -406,7 +400,7 @@ class BVHLightSampler {
pstd::vector<Light> infiniteLights;
Bounds3f allLightBounds;
pstd::vector<LightBVHNode> nodes;
HashMap<Light, uint32_t, LightHash> lightToBitTrail;
HashMap<Light, uint32_t> lightToBitTrail;
};
// ExhaustiveLightSampler Definition
......@@ -441,7 +435,7 @@ class ExhaustiveLightSampler {
private:
pstd::vector<Light> lights, boundedLights, infiniteLights;
pstd::vector<LightBounds> lightBounds;
HashMap<Light, size_t, LightHash> lightToBoundedIndex;
HashMap<Light, size_t> lightToBoundedIndex;
};
inline pstd::optional<SampledLight> LightSampler::Sample(const LightSampleContext &ctx,
......
......@@ -68,7 +68,7 @@ TEST(BVHLightSampling, OneSpot) {
TEST(BVHLightSampling, Point) {
RNG rng;
std::vector<Light> lights;
std::unordered_map<Light, int, LightHash> lightToIndex;
std::unordered_map<Light, int> lightToIndex;
ConstantSpectrum one(1.f);
for (int i = 0; i < 33; ++i) {
// Random point in [-5, 5]
......@@ -116,7 +116,7 @@ TEST(BVHLightSampling, PointVaryPower) {
std::vector<Float> lightPower;
std::vector<std::unique_ptr<ConstantSpectrum>> lightSpectra;
Float sumPower = 0;
std::unordered_map<Light, int, LightHash> lightToIndex;
std::unordered_map<Light, int> lightToIndex;
for (int i = 0; i < 82; ++i) {
// Random point in [-5, 5]
Vector3f p{Lerp(rng.Uniform<Float>(), -5, 5), Lerp(rng.Uniform<Float>(), -5, 5),
......
......@@ -221,11 +221,6 @@ struct InstanceSceneEntity {
const Transform *renderFromInstance = nullptr;
};
// TransformHash Definition
struct TransformHash {
size_t operator()(const Transform &t) const { return t.Hash(); }
};
// MaxTransforms Definition
constexpr int MaxTransforms = 2;
......@@ -494,7 +489,7 @@ class BasicSceneBuilder : public ParserTarget {
static constexpr int AllTransformsBits = (1 << MaxTransforms) - 1;
std::map<std::string, TransformSet> namedCoordinateSystems;
class Transform renderFromWorld;
InternCache<class Transform, TransformHash> transformCache;
InternCache<class Transform> transformCache;
std::vector<GraphicsState> pushedGraphicsStates;
std::vector<std::pair<char, FileLoc>> pushStack; // 'a': attribute, 'o': object
struct ActiveInstanceDefinition {
......
......@@ -621,7 +621,7 @@ class InlinedVector {
};
// HashMap Definition
template <typename Key, typename Value, typename Hash,
template <typename Key, typename Value, typename Hash = std::hash<Key>,
typename Allocator =
pstd::pmr::polymorphic_allocator<pstd::optional<std::pair<Key, Value>>>>
class HashMap {
......
......@@ -22,6 +22,7 @@
#include <algorithm>
#include <cmath>
#include <functional>
#include <memory>
#include <numeric>
#include <string>
......@@ -448,14 +449,8 @@ class DenselySampledSpectrum {
return true;
}
struct Hash {
size_t operator()(const DenselySampledSpectrum &s) const {
return HashBuffer(s.values.data(), s.values.size());
}
};
private:
friend struct Hash;
friend struct std::hash<pbrt::DenselySampledSpectrum>;
// DenselySampledSpectrum Private Members
int lambda_min, lambda_max;
pstd::vector<Float> values;
......@@ -793,4 +788,16 @@ inline Float Spectrum::MaxValue() const {
} // namespace pbrt
namespace std {
template <>
struct hash<pbrt::DenselySampledSpectrum> {
PBRT_CPU_GPU
size_t operator()(const pbrt::DenselySampledSpectrum &s) const {
return pbrt::HashBuffer(s.values.data(), s.values.size());
}
};
} // namespace std
#endif // PBRT_UTIL_SPECTRUM_H
......@@ -16,6 +16,7 @@
#include <stdio.h>
#include <cmath>
#include <functional>
#include <limits>
#include <memory>
......@@ -35,8 +36,6 @@ class Transform {
template <typename T>
PBRT_CPU_GPU inline Normal3<T> ApplyInverse(Normal3<T>) const;
uint64_t Hash() const { return HashBuffer<sizeof(m)>(&m); }
std::string ToString() const;
Transform() = default;
......@@ -538,4 +537,17 @@ class AnimatedTransform {
} // namespace pbrt
namespace std {
template <>
struct hash<pbrt::Transform> {
PBRT_CPU_GPU
size_t operator()(const pbrt::Transform &t) const {
pbrt::SquareMatrix<4> m = t.GetMatrix();
return pbrt::HashBuffer<sizeof(m)>(&m);
}
};
} // namespace std
#endif // PBRT_UTIL_TRANSFORM_H
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册