InferPipeline.cpp 3.26 KB
Newer Older
Nianchen Deng's avatar
Nianchen Deng committed
1
2
3
#include "InferPipeline.h"
#include "Nmsl2.h"

Nianchen Deng's avatar
Nianchen Deng committed
4
InferPipeline::InferPipeline(sptr<Msl> net, unsigned int nRays, unsigned int nSamplesPerRay, glm::vec2 depthRange,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
5
6
7
                             int encodeDim, int coordChns)
    : _nRays(nRays),
      _nSamplesPerRay(nSamplesPerRay),
Nianchen Deng's avatar
Nianchen Deng committed
8
      _coordChns(coordChns),
Nianchen Deng's avatar
sync    
Nianchen Deng committed
9
10
11
12
      _net(net),
      _sampler(new Sampler(depthRange, nSamplesPerRay, coordChns == 3)),
      _encoder(new Encoder(encodeDim, coordChns)),
      _renderer(new Renderer()) {
Nianchen Deng's avatar
Nianchen Deng committed
13
    unsigned int nSamples = _nRays * _nSamplesPerRay;
Nianchen Deng's avatar
sync    
Nianchen Deng committed
14
15
16
17
    _coords = sptr<CudaArray<float>>(new CudaArray<float>(nSamples * coordChns));
    _depths = sptr<CudaArray<float>>(new CudaArray<float>(nSamples));
    _encoded = sptr<CudaArray<float>>(new CudaArray<float>(nSamples * _encoder->outDim()));
    _layeredColors = sptr<CudaArray<glm::vec4>>(new CudaArray<glm::vec4>(nSamples));
Nianchen Deng's avatar
Nianchen Deng committed
18
19
20
    _net->bindResources(_encoded.get(), _depths.get(), _layeredColors.get());
}

Nianchen Deng's avatar
sync    
Nianchen Deng committed
21
22
void InferPipeline::run(sptr<CudaArray<glm::vec4>> o_colors, sptr<CudaArray<glm::vec3>> rays,
                        glm::vec3 origin, bool showPerf) {
Nianchen Deng's avatar
Nianchen Deng committed
23
24
25
26
27

    CudaEvent eStart, eSampled, eEncoded, eInferred, eRendered;

    cudaEventRecord(eStart);

Nianchen Deng's avatar
sync    
Nianchen Deng committed
28
    _sampler->sampleOnRays(_coords, _depths, rays, origin);
Nianchen Deng's avatar
Nianchen Deng committed
29
    CHECK_EX(cudaDeviceSynchronize());
Nianchen Deng's avatar
Nianchen Deng committed
30
31

    cudaEventRecord(eSampled);
Nianchen Deng's avatar
sync    
Nianchen Deng committed
32
33

    _encoder->encode(_encoded, _coords);
Nianchen Deng's avatar
Nianchen Deng committed
34
    CHECK_EX(cudaDeviceSynchronize());
Nianchen Deng's avatar
Nianchen Deng committed
35
36
37
38

    cudaEventRecord(eEncoded);

    _net->infer();
Nianchen Deng's avatar
Nianchen Deng committed
39
    CHECK_EX(cudaDeviceSynchronize());
Nianchen Deng's avatar
Nianchen Deng committed
40
41
42
43
44
45
46

    cudaEventRecord(eInferred);

    _renderer->render(o_colors, _layeredColors);

    cudaEventRecord(eRendered);

Nianchen Deng's avatar
sync    
Nianchen Deng committed
47
    if (showPerf) {
Nianchen Deng's avatar
Nianchen Deng committed
48
49
50
51
52
53
54
55
56
57
58
        CHECK_EX(cudaDeviceSynchronize());

        float timeTotal, timeSample, timeEncode, timeInfer, timeRender;
        cudaEventElapsedTime(&timeTotal, eStart, eRendered);
        cudaEventElapsedTime(&timeSample, eStart, eSampled);
        cudaEventElapsedTime(&timeEncode, eSampled, eEncoded);
        cudaEventElapsedTime(&timeInfer, eEncoded, eInferred);
        cudaEventElapsedTime(&timeRender, eInferred, eRendered);

        std::ostringstream sout;
        sout << "Infer pipeline: " << timeTotal << "ms (Sample: " << timeSample
Nianchen Deng's avatar
sync    
Nianchen Deng committed
59
60
             << "ms, Encode: " << timeEncode << "ms, Infer: " << timeInfer
             << "ms, Render: " << timeRender << "ms)";
Nianchen Deng's avatar
Nianchen Deng committed
61
62
        Logger::instance.info(sout.str());
    }
Nianchen Deng's avatar
Nianchen Deng committed
63
    
Nianchen Deng's avatar
sync    
Nianchen Deng committed
64
    /*
Nianchen Deng's avatar
sync    
Nianchen Deng committed
65
66
67
    {
        std::ostringstream sout;
        sout << "Rays:" << std::endl;
Nianchen Deng's avatar
Nianchen Deng committed
68
        dumpArray<glm::vec3, float>(sout, *rays, 10);
Nianchen Deng's avatar
sync    
Nianchen Deng committed
69
70
71
72
73
        Logger::instance.info(sout.str());
    }
    {
        std::ostringstream sout;
        sout << "Spherical coords:" << std::endl;
Nianchen Deng's avatar
Nianchen Deng committed
74
        dumpArray(sout, *_coords, 10, _coordChns * _nSamplesPerRay);
Nianchen Deng's avatar
sync    
Nianchen Deng committed
75
76
77
78
79
        Logger::instance.info(sout.str());
    }
    {
        std::ostringstream sout;
        sout << "Depths:" << std::endl;
Nianchen Deng's avatar
Nianchen Deng committed
80
        dumpArray(sout, *_depths, 10, _nSamplesPerRay);
Nianchen Deng's avatar
sync    
Nianchen Deng committed
81
82
83
84
85
        Logger::instance.info(sout.str());
    }
    {
        std::ostringstream sout;
        sout << "Encoded:" << std::endl;
Nianchen Deng's avatar
Nianchen Deng committed
86
87
88
89
90
91
92
        dumpArray(sout, *_encoded, 10, _encoder->outDim() * _nSamplesPerRay);
        Logger::instance.info(sout.str());
    }
    {
        std::ostringstream sout;
        sout << "Color:" << std::endl;
        dumpArray<glm::vec4, float>(sout, *o_colors, 10);
Nianchen Deng's avatar
sync    
Nianchen Deng committed
93
94
95
        Logger::instance.info(sout.str());
    }
    */
Nianchen Deng's avatar
Nianchen Deng committed
96
}