Manta Interactive Ray Tracer Development Mailing List

Text archives Help


Re: [Manta] r2165 - in trunk/Engine: PixelSamplers SampleGenerators


Chronological Thread 
  • From: Solomon Boulos <boulos@cs.utah.edu>
  • Cc: manta@sci.utah.edu
  • Subject: Re: [Manta] r2165 - in trunk/Engine: PixelSamplers SampleGenerators
  • Date: Thu, 3 Apr 2008 19:58:55 -0700

On my box, this change to Stratified2D results in a 25% improvement for the Cornell Box dashboard test, enjoy ;).

On Apr 3, 2008, at 8:03 PM, Solomon Boulos wrote:
Author: boulos
Date: Thu Apr  3 21:03:21 2008
New Revision: 2165

Modified:
  trunk/Engine/PixelSamplers/ClusterSampler.cc
  trunk/Engine/SampleGenerators/Stratified2D.cc
  trunk/Engine/SampleGenerators/Stratified2D.h
Log:
Engine/PixelSamplers/ClusterSampler.cc

Forgot to initialize min_shading_samples... (a previous version of
the code had this hard coded, but I figure at some point it should be
an option).

Clean up memory in the destructor.

Engine/SampleGenerators/Stratified2D.cc
Engine/SampleGenerators/Stratified2D.h

Generate new depths as necessary. reached_depth keeps track of what
depth level has been reached so far in each region. This massively
reduces the cost of generating samples when not all the depth will be
used (seems incredibly likely).


Modified: trunk/Engine/PixelSamplers/ClusterSampler.cc
= = = = = = = = ======================================================================
--- trunk/Engine/PixelSamplers/ClusterSampler.cc        (original)
+++ trunk/Engine/PixelSamplers/ClusterSampler.cc Thu Apr 3 21:03:21 2008
@@ -1,5 +1,6 @@

#include <Core/Exceptions/IllegalArgument.h>
+#include <Core/Exceptions/InternalError.h>
#include <Core/Math/MiscMath.h>
#include <Core/Util/Args.h>
#include <Engine/PixelSamplers/ClusterSampler.h>
@@ -43,7 +44,7 @@

  // Compute nx and ny
  findFactorsNearRoot(num_samples, nx, ny);
-
+  min_shading_samples = 4;
  maxSqrt = Ceil(Sqrt(static_cast<Real>(num_samples)));
  antialiasGenerator = new Stratified2D(num_samples);
  sampleGenerators = new SampleGenerator*[maxSqrt];
@@ -55,7 +56,11 @@

ClusterSampler::~ClusterSampler()
{
-
+  delete antialiasGenerator;
+  for (int i = 0; i < maxSqrt; i++) {
+    delete sampleGenerators[i];
+  }
+  delete[] sampleGenerators;
}

void ClusterSampler::setupBegin(const SetupContext& context, int numChannels)
@@ -198,6 +203,7 @@
    int sqrtRays = Ceil(Sqrt(static_cast<Real>(neededRays)));
    int realNumRays = sqrtRays * sqrtRays;

+    //if (sqrtRays < 1) throw InternalError("Error SqrtRays is < 1");
    mutable_context.sample_generator = sampleGenerators[sqrtRays - 1];
    shading_rays.resize(realNumRays);
    sample_count = 0;

Modified: trunk/Engine/SampleGenerators/Stratified2D.cc
= = = = = = = = ======================================================================
--- trunk/Engine/SampleGenerators/Stratified2D.cc       (original)
+++ trunk/Engine/SampleGenerators/Stratified2D.cc Thu Apr 3 21:03:21 2008
@@ -13,7 +13,7 @@

Stratified2D::Stratified2D(unsigned int spp) :
  spp(spp), thread_samples(0), max_depth(0), num_threads(0),
-  current_region_id(0), current_raw_region_id(0),
+  current_region_id(0), current_raw_region_id(0), reached_depth(0),
  xres(0), yres(0) {
sqrt_spp = static_cast<unsigned int>(Sqrt(static_cast<double>(spp)));
  if (sqrt_spp * sqrt_spp != spp) {
@@ -59,14 +59,25 @@
      delete[] thread_samples[i];
    }
    delete[] thread_samples;
+    thread_samples = 0;
+  }
+
+  if (reached_depth) {
+    for (unsigned int i = 0; i < num_threads; i++) {
+      delete[] reached_depth[i];
+    }
+    delete[] reached_depth;
+    reached_depth = 0;
  }

  // TODO(boulos): Handle the false sharing issues here...
  if (current_region_id) {
    delete[] current_region_id;
+    current_region_id = 0;
  }
  if (current_raw_region_id) {
    delete[] current_raw_region_id;
+    current_raw_region_id = 0;
  }

  num_threads = new_threads;
@@ -74,10 +85,13 @@
  thread_samples = new float**[num_threads];
  current_region_id = new unsigned int[num_threads];
  current_raw_region_id = new unsigned int[num_threads];
+  reached_depth = new int*[num_threads];
  for (unsigned int i = 0; i < num_threads; i++) {
    thread_samples[i] = new float*[max_regions];
+    reached_depth[i] = new int[max_regions];
    for (unsigned int r = 0; r < max_regions; r++) {
      thread_samples[i][r] = new float[spp * max_depth];
+      reached_depth[i][r] = -1;
    }
    current_region_id[i] = 0;
    current_raw_region_id[i] = static_cast<unsigned int>(-1);
@@ -85,26 +99,32 @@
}

void Stratified2D::buildSamplesAllDepths(RandomNumberGenerator* rng,
-                                         unsigned int thread_id) {
+                                         unsigned int thread_id,
+                                         unsigned int region_id) {
  for (unsigned int i = 0; i < max_depth; i++) {
-    buildSamples(rng, thread_id, i);
+    buildSamples(rng, thread_id, region_id, i);
  }
}

void Stratified2D::buildSamples(RandomNumberGenerator* rng,
                                unsigned int thread_id,
+                                unsigned int region_id,
                                unsigned int depth) {
- float* all_samples = thread_samples[thread_id] [current_region_id[thread_id]];
+  int cur_depth = reached_depth[thread_id][region_id];
+  if (cur_depth >= static_cast<int>(depth)) return;
+  float* all_samples = thread_samples[thread_id][region_id];
  if (max_depth - depth > 1) {
    // NOTE(boulos): We only want to generate the even cases
    if (depth % 2 == 0) {
      float* x_samples = &(all_samples[spp * depth]);
      float* y_samples = &(all_samples[spp * (depth+1)]);
      build2DSamples(rng, x_samples, y_samples);
+      reached_depth[thread_id][region_id] = depth + 1;
    }
  } else {
    float* sample_buf = &(all_samples[spp * depth]);
    build1DSamples(rng, sample_buf);
+    reached_depth[thread_id][region_id] = depth;
  }
}

@@ -194,7 +214,12 @@
    current_region_id[context.proc]++;
    current_region_id[context.proc] %= max_regions;
    current_raw_region_id[context.proc] = raw_region_id;
-    buildSamplesAllDepths(context.rng, context.proc);
+ reached_depth[context.proc][current_region_id[context.proc]] = -1;
+#if 0
+    // NOTE(boulos): Old behavior which costs a lot if you never end
+    // up using the samples...
+ buildSamplesAllDepths(context.rng, context.proc, current_region_id[context.proc]);
+#endif
  }

  return current_region_id[context.proc];
@@ -234,6 +259,7 @@
void Stratified2D::nextSeeds(const RenderContext& context, Packet<float>& results, RayPacket& rays) {
  for (int i = rays.begin(); i < rays.end(); i++) {
    if (rays.data->sample_depth[i] < max_depth) {
+ buildSamples(context.rng, context.proc, rays.data- >region_id[i], rays.data->sample_depth[i]);
results.set(i, thread_samples[context.proc][rays.data- >region_id[i]][spp * rays.data->sample_depth[i] + rays.data- >sample_id[i]]);
    } else {
      results.set(i, context.rng->nextFloat());
@@ -245,6 +271,7 @@
void Stratified2D::nextSeeds(const RenderContext& context, Packet<double>& results, RayPacket& rays) {
  for (int i = rays.begin(); i < rays.end(); i++) {
    if (rays.data->sample_depth[i] < max_depth) {
+ buildSamples(context.rng, context.proc, rays.data- >region_id[i], rays.data->sample_depth[i]);
results.set(i, static_cast<double>(thread_samples[context.proc] [rays.data->region_id[i]][spp * rays.data->sample_depth[i] + rays.data->sample_id[i]]));
    } else {
      results.set(i, context.rng->nextDouble());

Modified: trunk/Engine/SampleGenerators/Stratified2D.h
= = = = = = = = ======================================================================
--- trunk/Engine/SampleGenerators/Stratified2D.h        (original)
+++ trunk/Engine/SampleGenerators/Stratified2D.h Thu Apr 3 21:03:21 2008
@@ -14,10 +14,12 @@
void allocateSamples(unsigned int num_threads, unsigned int max_depth);

    void buildSamplesAllDepths(RandomNumberGenerator* rng,
-                               unsigned int thread_id);
+                               unsigned int thread_id,
+                               unsigned int region_id);

    void buildSamples(RandomNumberGenerator* rng,
                      unsigned int thread_id,
+                      unsigned int region_id,
                      unsigned int depth);

    void build2DSamples(RandomNumberGenerator* rng,
@@ -50,6 +52,8 @@
    unsigned int num_threads;
    unsigned int* current_region_id; // num_threads wide
    unsigned int* current_raw_region_id; // num_threads wide
+    int** reached_depth; // num_threads by num_regions
+
    unsigned int xres;
    unsigned int yres;
  };






Archive powered by MHonArc 2.6.16.

Top of page