integrator.h 5.6 KB
Newer Older
M
Matt Pharr 已提交
1 2 3 4
// pbrt is Copyright(c) 1998-2020 Matt Pharr, Wenzel Jakob, and Greg Humphreys.
// The pbrt source code is licensed under the Apache License, Version 2.0.
// SPDX: Apache-2.0

5 6
#ifndef PBRT_WAVEFRONT_PATHINTEGRATOR_H
#define PBRT_WAVEFRONT_PATHINTEGRATOR_H
M
Matt Pharr 已提交
7 8 9 10 11 12 13 14 15 16

#include <pbrt/pbrt.h>

#include <pbrt/base/bxdf.h>
#include <pbrt/base/camera.h>
#include <pbrt/base/film.h>
#include <pbrt/base/filter.h>
#include <pbrt/base/light.h>
#include <pbrt/base/lightsampler.h>
#include <pbrt/base/sampler.h>
17 18 19 20 21 22 23
#ifdef PBRT_BUILD_GPU_RENDERER
#include <pbrt/gpu/util.h>
#endif // PBRT_BUILD_GPU_RENDERER
#include <pbrt/options.h>
#include <pbrt/wavefront/workitems.h>
#include <pbrt/wavefront/workqueue.h>
#include <pbrt/util/parallel.h>
M
Matt Pharr 已提交
24 25 26 27 28 29
#include <pbrt/util/pstd.h>

namespace pbrt {

class ParsedScene;

30 31 32
class WavefrontAggregate {
public:
    virtual ~WavefrontAggregate() = default;
M
Matt Pharr 已提交
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    virtual Bounds3f Bounds() const = 0;

    virtual void IntersectClosest(
        int maxRays, EscapedRayQueue *escapedRayQueue,
        HitAreaLightQueue *hitAreaLightQueue, MaterialEvalQueue *basicEvalMaterialQueue,
        MaterialEvalQueue *universalEvalMaterialQueue,
        MediumSampleQueue *mediumSampleQueue, RayQueue *rayQueue, RayQueue *nextRayQueue) const = 0;

    virtual void IntersectShadow(int maxRays, ShadowRayQueue *shadowRayQueue,
                                 SOA<PixelSampleState> *pixelSampleState) const = 0;

    virtual void IntersectShadowTr(int maxRays, ShadowRayQueue *shadowRayQueue,
                                   SOA<PixelSampleState> *pixelSampleState) const = 0;

    virtual void IntersectOneRandom(int maxRays,
                                    SubsurfaceScatterQueue *subsurfaceScatterQueue) const = 0;
};

// WavefrontPathIntegrator Definition
class WavefrontPathIntegrator {
M
Matt Pharr 已提交
54
  public:
55 56
    // WavefrontPathIntegrator Public Methods
    Float Render();
M
Matt Pharr 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    void GenerateCameraRays(int y0, int sampleIndex);
    template <typename Sampler>
    void GenerateCameraRays(int y0, int sampleIndex);

    void GenerateRaySamples(int depth, int sampleIndex);
    template <typename Sampler>
    void GenerateRaySamples(int depth, int sampleIndex);

    void TraceShadowRays(int depth);
    void SampleMediumInteraction(int depth);
    void SampleSubsurface(int depth);

    void HandleEscapedRays(int depth);
    void HandleRayFoundEmission(int depth);

    void EvaluateMaterialsAndBSDFs(int depth);
74
    template <typename Mtl>
M
Matt Pharr 已提交
75
    void EvaluateMaterialAndBSDF(int depth);
76
    template <typename Mtl, typename TextureEvaluator>
M
Matt Pharr 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
    void EvaluateMaterialAndBSDF(TextureEvaluator texEval, MaterialEvalQueue *evalQueue,
                                 int depth);

    void SampleDirect(int depth);
    template <typename BxDF>
    void SampleDirect(int depth);

    void SampleIndirect(int depth);
    template <typename BxDF>
    void SampleIndirect(int depth);

    void UpdateFilm();

90
    WavefrontPathIntegrator(Allocator alloc, ParsedScene &scene);
91

92 93 94
    RayQueue *CurrentRayQueue(int depth) { return rayQueues[depth & 1]; }
    RayQueue *NextRayQueue(int depth) { return rayQueues[(depth + 1) & 1]; }

95 96 97 98 99 100 101
    void IntersectClosest(RayQueue *rayQueue, EscapedRayQueue *escapedRayQueue,
                          HitAreaLightQueue *hitAreaLightQueue,
                          MaterialEvalQueue *basicEvalMaterialQueue,
                          MaterialEvalQueue *universalEvalMaterialQueue,
                          MediumSampleQueue *mediumSampleQueue,
                          RayQueue *nextRayQueue) const;

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    template <typename F>
    void ParallelFor(const char *description, int nItems, F &&func) {
        if (Options->useGPU) {
#ifdef PBRT_BUILD_GPU_RENDERER
            GPUParallelFor(description, nItems, func);
#else
            LOG_FATAL("Options->useGPU was set without PBRT_BUILD_GPU_RENDERER enabled");
#endif
        } else
            pbrt::ParallelFor(0, nItems, func);
    }
    template <typename F>
    void Do(const char *description, F &&func) {
        if (Options->useGPU) {
#ifdef PBRT_BUILD_GPU_RENDERER
            GPUParallelFor(description, 1, [=] PBRT_GPU(int) mutable { func(); });
#else
            LOG_FATAL("Options->useGPU was set without PBRT_BUILD_GPU_RENDERER enabled");
#endif
        } else
            func();
    }

    // WavefrontPathIntegrator Member Variables
M
Matt Pharr 已提交
126 127 128
    bool initializeVisibleSurface;
    bool haveSubsurface;
    bool haveMedia;
129 130
    pstd::array<bool, Material::NumTags()> haveBasicEvalMaterial;
    pstd::array<bool, Material::NumTags()> haveUniversalEvalMaterial;
M
Matt Pharr 已提交
131

132
    WavefrontAggregate *aggregate = nullptr;
M
Matt Pharr 已提交
133

134 135
    struct Stats {
        Stats(int maxDepth, Allocator alloc);
M
Matt Pharr 已提交
136

137
        std::string Print() const;
M
Matt Pharr 已提交
138

139 140 141 142 143
        // Note: not atomics: tid 0 always updates them for everyone...
        uint64_t cameraRays = 0;
        pstd::vector<uint64_t> indirectRays, shadowRays;
    };
    Stats *stats;
M
Matt Pharr 已提交
144

145 146 147 148 149 150
    Filter filter;
    Film film;
    Sampler sampler;
    Camera camera;
    pstd::vector<Light> *envLights;
    LightSampler lightSampler;
M
Matt Pharr 已提交
151

152 153 154
    int maxDepth;
    bool regularize;

M
Matt Pharr 已提交
155
    int scanlinesPerPass, maxQueueSize;
156 157

    SOA<PixelSampleState> pixelSampleState;
M
Matt Pharr 已提交
158

M
Matt Pharr 已提交
159 160
    RayQueue *rayQueues[2];

M
Matt Pharr 已提交
161 162 163
    MediumSampleQueue *mediumSampleQueue = nullptr;
    MediumScatterQueue *mediumScatterQueue = nullptr;

164
    EscapedRayQueue *escapedRayQueue = nullptr;
M
Matt Pharr 已提交
165

166 167 168 169 170 171 172 173 174
    HitAreaLightQueue *hitAreaLightQueue = nullptr;

    MaterialEvalQueue *basicEvalMaterialQueue = nullptr;
    MaterialEvalQueue *universalEvalMaterialQueue = nullptr;

    ShadowRayQueue *shadowRayQueue = nullptr;

    GetBSSRDFAndProbeRayQueue *bssrdfEvalQueue = nullptr;
    SubsurfaceScatterQueue *subsurfaceScatterQueue = nullptr;
M
Matt Pharr 已提交
175 176 177 178
};

}  // namespace pbrt

179
#endif  // PBRT_WAVEFRONT_PATHINTEGRATOR_H