Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of compiling when the GPU is enabled #209

Open
mmp opened this issue Nov 22, 2021 · 0 comments
Open

Improve performance of compiling when the GPU is enabled #209

mmp opened this issue Nov 22, 2021 · 0 comments

Comments

@mmp
Copy link
Owner

mmp commented Nov 22, 2021

Currently, wavefront/surfscatter.cpp takes about 55s to compile here, thanks to a whole bunch of template specialization (on all Material types cross the two types of texture evaluator in this case.) That can be improved to about 35s by introducing the following functions in surfscatter.cpp; these cause the corresponding code to be compiled just once, rather than once per material (since it is material-independent). This is helpful since these are the main remaining cases in the kernel where there is dynamic dispatch.

However, introducing these generally slows down rendering by 5% (and about 10% on the Zero Day scenes...)

PBRT_CPU_GPU __noinline__
pstd::optional<LightLiSample>
SampleLight(Light light, const LightSampleContext &ctx, Point2f u, SampledWavelengths lambda) {
    return light.SampleLi(ctx, u, lambda, true);
}

PBRT_CPU_GPU __noinline__
pstd::optional<SampledLight> SampleOneLight(LightSampler lightSampler,
                                          const LightSampleContext &ctx, Float u) {
    return lightSampler.Sample(ctx, u);
}

template <typename TextureEvaluator>
PBRT_CPU_GPU __noinline__
void normalBump(const Image *normalMap, FloatTexture displacement, const NormalBumpEvalContext &bctx,
                Normal3f n, Vector3f *dpdus, Normal3f *ns) {
    Vector3f dpdvs;
    if (normalMap) {
        // Call _NormalMap()_ to find shading geometry
        NormalMap(*normalMap, bctx, dpdus, &dpdvs);
        *ns = Normal3f(Normalize(Cross(*dpdus, dpdvs)));
        *ns = FaceForward(*ns, n);
    } else if (displacement) {
        // Call _BumpMap()_ to find shading geometry
        TextureEvaluator texEval;
        DCHECK(texEval.CanEvaluate({displacement}, {}));
        BumpMap(texEval, displacement, bctx, dpdus, &dpdvs);
        *ns = Normal3f(Normalize(Cross(*dpdus, dpdvs)));
        *ns = FaceForward(*ns, n);
    }
}

template <typename ConcreteMaterial>
PBRT_CPU_GPU __noinline__
void initializeVS(MaterialEvalWorkItem<ConcreteMaterial> w, Normal3f ns, Vector3f dpdx,
                  Vector3f dpdy, const BSDF &bsdf, const SampledWavelengths &lambda,
                  SOA<PixelSampleState> &pixelSampleState) {
    SurfaceInteraction isect;
    isect.pi = w.pi;
    isect.n = w.n;
    isect.shading.n = ns;
    isect.uv = w.uv;
    isect.wo = w.wo;
    isect.time = w.time;
    isect.dpdx = dpdx;
    isect.dpdy = dpdy;

    // Estimate BSDF's albedo
    // Define sample arrays _ucRho_ and _uRho_ for reflectance estimate
    constexpr int nRhoSamples = 16;
    const Float ucRho[nRhoSamples] = {
        0.75741637, 0.37870818, 0.7083487, 0.18935409, 0.9149363, 0.35417435,
        0.5990858,  0.09467703, 0.8578725, 0.45746812, 0.686759,  0.17708716,
        0.9674518,  0.2995429,  0.5083201, 0.047338516};
    const Point2f uRho[nRhoSamples] = {
        Point2f(0.855985, 0.570367), Point2f(0.381823, 0.851844),
        Point2f(0.285328, 0.764262), Point2f(0.733380, 0.114073),
        Point2f(0.542663, 0.344465), Point2f(0.127274, 0.414848),
        Point2f(0.964700, 0.947162), Point2f(0.594089, 0.643463),
        Point2f(0.095109, 0.170369), Point2f(0.825444, 0.263359),
        Point2f(0.429467, 0.454469), Point2f(0.244460, 0.816459),
        Point2f(0.756135, 0.731258), Point2f(0.516165, 0.152852),
        Point2f(0.180888, 0.214174), Point2f(0.898579, 0.503897)};

    SampledSpectrum albedo = bsdf.rho(isect.wo, ucRho, uRho);

    pixelSampleState.visibleSurface[w.pixelIndex] =
        VisibleSurface(isect, albedo, lambda);
}
huongoss pushed a commit to huongoss/pbrt-v4-docker that referenced this issue Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant