integrator.h 5.5 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_INTEGRATOR_H
#define PBRT_WAVEFRONT_INTEGRATOR_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
#ifdef PBRT_BUILD_GPU_RENDERER
#include <pbrt/gpu/util.h>
19
#endif  // PBRT_BUILD_GPU_RENDERER
20 21
#include <pbrt/options.h>
#include <pbrt/util/parallel.h>
M
Matt Pharr 已提交
22
#include <pbrt/util/pstd.h>
23 24
#include <pbrt/wavefront/workitems.h>
#include <pbrt/wavefront/workqueue.h>
M
Matt Pharr 已提交
25 26 27 28 29

namespace pbrt {

class ParsedScene;

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

36 37
    virtual Bounds3f Bounds() const = 0;

38 39 40 41 42 43 44
    virtual void IntersectClosest(int maxRays, const RayQueue *rayQ,
                                  EscapedRayQueue *escapedRayQ,
                                  HitAreaLightQueue *hitAreaLightQ,
                                  MaterialEvalQueue *basicMtlQ,
                                  MaterialEvalQueue *universalMtlQ,
                                  MediumSampleQueue *mediumSampleQ,
                                  RayQueue *nextRayQ) const = 0;
45 46 47 48 49 50

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

51 52
    virtual void IntersectOneRandom(
        int maxRays, SubsurfaceScatterQueue *subsurfaceScatterQueue) const = 0;
53 54 55 56
};

// WavefrontPathIntegrator Definition
class WavefrontPathIntegrator {
M
Matt Pharr 已提交
57
  public:
58 59
    // WavefrontPathIntegrator Public Methods
    Float Render();
M
Matt Pharr 已提交
60 61 62 63 64

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

65
    void GenerateRaySamples(int wavefrontDepth, int sampleIndex);
M
Matt Pharr 已提交
66
    template <typename Sampler>
67
    void GenerateRaySamples(int wavefrontDepth, int sampleIndex);
M
Matt Pharr 已提交
68

69 70
    void TraceShadowRays(int wavefrontDepth);
    void SampleMediumInteraction(int wavefrontDepth);
71
    template <typename PhaseFunction>
72 73
    void SampleMediumScattering(int wavefrontDepth);
    void SampleSubsurface(int wavefrontDepth);
M
Matt Pharr 已提交
74

75
    void HandleEscapedRays();
76
    void HandleEmissiveIntersection();
M
Matt Pharr 已提交
77

78
    void EvaluateMaterialsAndBSDFs(int wavefrontDepth);
79
    template <typename ConcreteMaterial>
80
    void EvaluateMaterialAndBSDF(int wavefrontDepth);
81 82
    template <typename ConcreteMaterial, typename TextureEvaluator>
    void EvaluateMaterialAndBSDF(MaterialEvalQueue *evalQueue, int wavefrontDepth);
M
Matt Pharr 已提交
83 84 85

    void UpdateFilm();

86 87
    WavefrontPathIntegrator(pstd::pmr::memory_resource *memoryResource,
                            ParsedScene &scene);
88

89 90
    template <typename F>
    void ParallelFor(const char *description, int nItems, F &&func) {
91
        if (Options->useGPU)
92 93 94 95 96
#ifdef PBRT_BUILD_GPU_RENDERER
            GPUParallelFor(description, nItems, func);
#else
            LOG_FATAL("Options->useGPU was set without PBRT_BUILD_GPU_RENDERER enabled");
#endif
97
        else
98 99
            pbrt::ParallelFor(0, nItems, func);
    }
100

101 102
    template <typename F>
    void Do(const char *description, F &&func) {
103
        if (Options->useGPU)
104
#ifdef PBRT_BUILD_GPU_RENDERER
M
Matt Pharr 已提交
105
            GPUParallelFor(description, 1, [=] PBRT_GPU(int) mutable { func(); });
106 107 108
#else
            LOG_FATAL("Options->useGPU was set without PBRT_BUILD_GPU_RENDERER enabled");
#endif
109
        else
110 111 112
            func();
    }

113 114 115 116 117 118
    RayQueue *CurrentRayQueue(int wavefrontDepth) {
        return rayQueues[wavefrontDepth & 1];
    }
    RayQueue *NextRayQueue(int wavefrontDepth) {
        return rayQueues[(wavefrontDepth + 1) & 1];
    }
119

120
    // WavefrontPathIntegrator Member Variables
M
Matt Pharr 已提交
121 122 123
    bool initializeVisibleSurface;
    bool haveSubsurface;
    bool haveMedia;
124 125
    pstd::array<bool, Material::NumTags()> haveBasicEvalMaterial;
    pstd::array<bool, Material::NumTags()> haveUniversalEvalMaterial;
M
Matt Pharr 已提交
126

127 128
    struct Stats {
        Stats(int maxDepth, Allocator alloc);
M
Matt Pharr 已提交
129

130
        std::string Print() const;
M
Matt Pharr 已提交
131

132 133 134 135 136
        // 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 已提交
137

138 139
    pstd::pmr::memory_resource *memoryResource;

140 141 142 143
    Filter filter;
    Film film;
    Sampler sampler;
    Camera camera;
144
    pstd::vector<Light> *infiniteLights;
145
    LightSampler lightSampler;
M
Matt Pharr 已提交
146

147
    int maxDepth, samplesPerPixel;
148 149
    bool regularize;

M
Matt Pharr 已提交
150
    int scanlinesPerPass, maxQueueSize;
151 152

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

M
Matt Pharr 已提交
154 155
    RayQueue *rayQueues[2];

156 157
    WavefrontAggregate *aggregate = nullptr;

M
Matt Pharr 已提交
158 159 160
    MediumSampleQueue *mediumSampleQueue = nullptr;
    MediumScatterQueue *mediumScatterQueue = nullptr;

161
    EscapedRayQueue *escapedRayQueue = nullptr;
M
Matt Pharr 已提交
162

163 164 165 166 167 168 169 170 171
    HitAreaLightQueue *hitAreaLightQueue = nullptr;

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

    ShadowRayQueue *shadowRayQueue = nullptr;

    GetBSSRDFAndProbeRayQueue *bssrdfEvalQueue = nullptr;
    SubsurfaceScatterQueue *subsurfaceScatterQueue = nullptr;
M
Matt Pharr 已提交
172 173 174 175
};

}  // namespace pbrt

176
#endif  // PBRT_WAVEFRONT_INTEGRATOR_H