Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[Manta] r1789 - in trunk: Engine/Control Engine/SampleGenerators Model/Cameras


Chronological Thread 
  • From: boulos@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [Manta] r1789 - in trunk: Engine/Control Engine/SampleGenerators Model/Cameras
  • Date: Tue, 23 Oct 2007 14:34:57 -0600 (MDT)

Author: boulos
Date: Tue Oct 23 14:34:55 2007
New Revision: 1789

Added:
   trunk/Engine/SampleGenerators/Stratified2D.cc
   trunk/Engine/SampleGenerators/Stratified2D.h
Modified:
   trunk/Engine/Control/RTRT.cc
   trunk/Engine/SampleGenerators/CMakeLists.txt
   trunk/Model/Cameras/ThinLensCamera.cc
Log:
Engine/Control/RTRT.cc
Engine/SampleGenerators/CMakeLists.txt
Engine/SampleGenerators/Stratified2D.cc
Engine/SampleGenerators/Stratified2D.h

 Adding a new Stratified2D SampleGenerator that tries to mimic
 galileo's 2D stratification across dimensions scheme. This is a WIP
 and doesn't really work yet but is going to be useful for code review
 (it does build though and shouldn't affect anyone)

Model/Cameras/ThinLensCamera.cc

 Updating ThinLensCamera to grab samples from the SampleGenerator
 instead of directly from a RandomNumberGenerator.


Modified: trunk/Engine/Control/RTRT.cc
==============================================================================
--- trunk/Engine/Control/RTRT.cc        (original)
+++ trunk/Engine/Control/RTRT.cc        Tue Oct 23 14:34:55 2007
@@ -80,6 +80,7 @@
 // allow us to discuss the interface (so that I don't have to do all
 // the Factory work just yet)
 #include <Engine/SampleGenerators/ConstantSampleGenerator.h>
+#include <Engine/SampleGenerators/Stratified2D.h>
 #include <Engine/SampleGenerators/UniformRandomGenerator.h>
 
 #include <queue>
@@ -174,6 +175,7 @@
 
   //currentSampleGenerator = new ConstantSampleGenerator(0.f);
   currentSampleGenerator = new UniformRandomGenerator();
+  //currentSampleGenerator = new Stratified2D(16);
 }
 
 RTRT::~RTRT()

Modified: trunk/Engine/SampleGenerators/CMakeLists.txt
==============================================================================
--- trunk/Engine/SampleGenerators/CMakeLists.txt        (original)
+++ trunk/Engine/SampleGenerators/CMakeLists.txt        Tue Oct 23 14:34:55 
2007
@@ -1,6 +1,8 @@
 SET (Manta_SampleGenerators_SRCS
      SampleGenerators/ConstantSampleGenerator.h
      SampleGenerators/ConstantSampleGenerator.cc
+     SampleGenerators/Stratified2D.h
+     SampleGenerators/Stratified2D.cc
      SampleGenerators/UniformRandomGenerator.h
      SampleGenerators/UniformRandomGenerator.cc
      )

Added: trunk/Engine/SampleGenerators/Stratified2D.cc
==============================================================================
--- (empty file)
+++ trunk/Engine/SampleGenerators/Stratified2D.cc       Tue Oct 23 14:34:55 
2007
@@ -0,0 +1,147 @@
+#include <Engine/SampleGenerators/Stratified2D.h>
+#include <Interface/Context.h>
+#include <Interface/Scene.h>
+#include <Interface/RenderParameters.h>
+#include <Interface/RandomNumberGenerator.h>
+#include <Interface/RayPacket.h>
+#include <SCIRun/Core/Math/Expon.h>
+#include <SCIRun/Core/Exceptions/InternalError.h>
+
+using namespace Manta;
+using SCIRun::Sqrt;
+
+Stratified2D::Stratified2D(unsigned int spp) :
+  spp(spp), thread_samples(0), max_depth(0), num_threads(0), 
current_pixel(0) {
+  sqrt_spp = static_cast<unsigned int>(Sqrt(static_cast<double>(spp)));
+  if (sqrt_spp * sqrt_spp != spp) {
+    throw SCIRun::InternalError("Stratified2D expects spp to be a perfect 
square",
+                                __FILE__, __LINE__);
+  }
+  inv_spp = 1.f/static_cast<float>(spp);
+  inv_sqrt_spp = 1.f/static_cast<float>(sqrt_spp);
+}
+
+Stratified2D::~Stratified2D() {
+}
+
+void Stratified2D::setupBegin(const SetupContext&, int numChannels) {
+}
+
+void Stratified2D::allocateSamples(unsigned int new_threads, unsigned int 
new_depth) {
+  // Already have the storage
+  if (new_threads == num_threads &&
+      new_depth == max_depth) return;
+
+  if (thread_samples) {
+    for (unsigned int i = 0; i < num_threads; i++) {
+      delete[] thread_samples[i];
+    }
+    delete[] thread_samples;
+  }
+
+  if (current_pixel) {
+    delete[] current_pixel;
+  }
+
+  num_threads = new_threads;
+  max_depth = new_depth;
+  thread_samples = new float*[num_threads];
+  current_pixel = new unsigned int[num_threads];
+  for (unsigned int i = 0; i < num_threads; i++) {
+    thread_samples[i] = new float[spp * max_depth];
+    current_pixel[i] = static_cast<unsigned int>(-1);
+  }
+}
+
+void Stratified2D::buildSamplesAllDepths(RandomNumberGenerator* rng,
+                                         unsigned int thread_id) {
+  for (unsigned int i = 0; i < max_depth; i++) {
+    buildSamples(rng, thread_id, i);
+  }
+}
+
+void Stratified2D::buildSamples(RandomNumberGenerator* rng,
+                                unsigned int thread_id,
+                                unsigned int depth) {
+  float* sample_buf = &(thread_samples[thread_id][spp * depth]);
+  if (max_depth - depth > 1)
+    build2DSamples(rng, sample_buf);
+  else
+    build1DSamples(rng, sample_buf);
+}
+
+
+void Stratified2D::build2DSamples(RandomNumberGenerator* rng,
+                                  float* sample_buf) {
+  unsigned int index = 0;
+  float ii = 0.f;
+  float jj = 0.f;
+  for (unsigned int i = 0; i < sqrt_spp; i++) {
+    jj = 0.f;
+    for (unsigned int j = 0; j < sqrt_spp; j++) {
+      float x = (ii + rng->nextFloat()) * inv_sqrt_spp;
+      float y = (jj + rng->nextFloat()) * inv_sqrt_spp;
+      jj += 1.f;
+      sample_buf[index + 0] = x;
+      sample_buf[index + 1] = y;
+    }
+    ii += 1.f;
+    index += sqrt_spp;
+  }
+}
+
+void Stratified2D::build1DSamples(RandomNumberGenerator* rng, float* 
sample_buf) {
+  for (unsigned int i = 0; i < spp; i++) {
+    sample_buf[i] = (i + rng->nextFloat()) * inv_spp;
+  }
+}
+
+void Stratified2D::setupDisplayChannel(SetupContext& context) {
+  bool stereo;
+  int width, height;
+  context.getResolution(stereo, width, height);
+  if (stereo) throw SCIRun::InternalError("Stratified2D doesn't handle 
stereo yet",
+                                          __FILE__, __LINE__);
+  xres = width;
+  yres = height;
+}
+
+void Stratified2D::setupFrame(const RenderContext& context) {
+  if (context.proc == 0) {
+    // NOTE(boulos): Sample depth is different than bounce
+    // depth. Assume we need about 2 seeds per bounce. Above the max
+    // depth, just get random
+    allocateSamples(context.numProcs, 2 * 
context.scene->getRenderParameters().maxDepth);
+  }
+}
+
+void Stratified2D::setupPacket(const RenderContext& context, RayPacket& 
rays) {
+  for (int i = rays.begin(); i < rays.end(); i++) {
+    rays.data->sample_depth[i] = 0;
+  }
+  buildSamplesAllDepths(context.rng, context.proc);
+}
+
+void Stratified2D::nextSeeds(const RenderContext& context, Packet<float>& 
results, RayPacket& rays) {
+  for (int i = rays.begin(); i < rays.end(); i++) {
+    if (static_cast<unsigned int>(rays.data->sample_depth[i]) < max_depth) {
+      float* sample_buf = &(thread_samples[context.proc][spp * 
rays.data->sample_depth[i]]);
+      results.set(i, sample_buf[rays.data->sample_id[i]]);
+    } else {
+      results.set(i, context.rng->nextFloat());
+    }
+    rays.data->sample_depth[i]++;
+  }
+}
+
+void Stratified2D::nextSeeds(const RenderContext& context, Packet<double>& 
results, RayPacket& rays) {
+  for (int i = rays.begin(); i < rays.end(); i++) {
+    if (static_cast<unsigned int>(rays.data->sample_depth[i]) < max_depth) {
+      float* sample_buf = &(thread_samples[context.proc][spp * 
rays.data->sample_depth[i]]);
+      results.set(i, 
static_cast<double>(sample_buf[rays.data->sample_id[i]]));
+    } else {
+      results.set(i, context.rng->nextDouble());
+    }
+    rays.data->sample_depth[i]++;
+  }
+}

Added: trunk/Engine/SampleGenerators/Stratified2D.h
==============================================================================
--- (empty file)
+++ trunk/Engine/SampleGenerators/Stratified2D.h        Tue Oct 23 14:34:55 
2007
@@ -0,0 +1,51 @@
+#ifndef MANTA_ENGINE_SAMPLEGENERATORS_STRATIFIED_2D_H_
+#define MANTA_ENGINE_SAMPLEGENERATORS_STRATIFIED_2D_H_
+
+#include <Interface/SampleGenerator.h>
+
+namespace Manta {
+  class RandomNumberGenerator;
+
+  class Stratified2D : public SampleGenerator {
+  public:
+    Stratified2D(unsigned int spp);
+    ~Stratified2D();
+
+    void allocateSamples(unsigned int num_threads, unsigned int max_depth);
+
+    void buildSamplesAllDepths(RandomNumberGenerator* rng,
+                               unsigned int thread_id);
+
+    void buildSamples(RandomNumberGenerator* rng,
+                      unsigned int thread_id,
+                      unsigned int depth);
+
+    void build2DSamples(RandomNumberGenerator* rng,
+                        float* sample_buf);
+
+    void build1DSamples(RandomNumberGenerator* rng,
+                        float* sample_buf);
+
+    virtual void setupBegin(const SetupContext&, int numChannels);
+    virtual void setupDisplayChannel(SetupContext& context);
+    virtual void setupFrame(const RenderContext& context);
+
+    virtual void setupPacket(const RenderContext& context, RayPacket& rays);
+
+    virtual void nextSeeds(const RenderContext& context, Packet<float>& 
results, RayPacket& rays);
+    virtual void nextSeeds(const RenderContext& context, Packet<double>& 
results, RayPacket& rays);
+
+    unsigned int spp;
+    float inv_spp;
+    unsigned int sqrt_spp;
+    float inv_sqrt_spp;
+    float** thread_samples;
+    unsigned int max_depth;
+    unsigned int num_threads;
+    unsigned int* current_pixel; // num_threads wide
+    unsigned int xres;
+    unsigned int yres;
+  };
+} // end namespace Manta
+
+#endif // MANTA_ENGINE_SAMPLEGENERATORS_UNIFORM_RANDOM_GENERATOR_H_

Modified: trunk/Model/Cameras/ThinLensCamera.cc
==============================================================================
--- trunk/Model/Cameras/ThinLensCamera.cc       (original)
+++ trunk/Model/Cameras/ThinLensCamera.cc       Tue Oct 23 14:34:55 2007
@@ -6,7 +6,7 @@
 #include <Interface/Context.h>
 #include <Interface/MantaInterface.h>
 #include <Interface/RayPacket.h>
-#include <Interface/RandomNumberGenerator.h>
+#include <Interface/SampleGenerator.h>
 #include <Interface/Scene.h>
 #include <Core/Geometry/BBox.h>
 #include <Core/Geometry/AffineTransform.h>
@@ -159,24 +159,24 @@
   Packet<Real> lens_coord_x;
   Packet<Real> lens_coord_y;
 
-  context.rng->nextPacket(lens_coord_x, rays);
-  context.rng->nextPacket(lens_coord_y, rays);
-  
+  context.sample_generator->nextSeeds(context, lens_coord_x, rays);
+  context.sample_generator->nextSeeds(context, lens_coord_y, rays);
+
 #ifdef MANTA_SSE
   int b = (rays.rayBegin + 3) & (~3);
   int e = rays.rayEnd & (~3);
-  
+
   if(b>=e) {
     for(int i = rays.begin(); i < rays.end(); ++i) {
       Real imageX = rays.getImageCoordinates(i, 0);
       Real imageY = rays.getImageCoordinates(i, 1);
-      
+
       Real theta = 2.0 * M_PI * lens_coord_x.get(i);
       Real r = radius * SCIRun::Sqrt( lens_coord_y.get(i) );
-    
+
       Vector origin = r*Cos(theta)*u + r*Sin(theta)*v;
       Vector on_film = imageX*v+imageY*u+direction*focal_length;
-    
+
       rays.setRay(i, eye+origin, on_film-origin);
     }
   } else {
@@ -184,30 +184,30 @@
     for(;i<b;i++) {
       Real imageX = rays.getImageCoordinates(i, 0);
       Real imageY = rays.getImageCoordinates(i, 1);
-      
+
       Real theta = 2.0 * M_PI * lens_coord_x.get(i);
       Real r = radius * SCIRun::Sqrt( lens_coord_y.get(i) );
-      
+
       Vector origin = r*Cos(theta)*u + r*Sin(theta)*v;
       Vector on_film = imageX*v+imageY*u+direction*focal_length;
-    
+
       rays.setRay(i, eye+origin, on_film-origin);
     }
     for(i=e;i<rays.rayEnd;i++) {
       Real imageX = rays.getImageCoordinates(i, 0);
       Real imageY = rays.getImageCoordinates(i, 1);
-      
+
       Real theta = 2.0 * M_PI * lens_coord_x.get(i);
       Real r = radius * SCIRun::Sqrt( lens_coord_y.get(i) );
-    
+
       Vector origin = r*Cos(theta)*u + r*Sin(theta)*v;
       Vector on_film = imageX*v+imageY*u+direction*focal_length;
-    
+
       rays.setRay(i, eye+origin, on_film-origin);
     }
 
     RayPacketData* data = rays.data;
-    
+
     const sse_t eyex = set4(eye.data[0]);
     const sse_t eyey = set4(eye.data[1]);
     const sse_t eyez = set4(eye.data[2]);
@@ -215,7 +215,7 @@
     const sse_t v0 = set4(v[0]);
     const sse_t v1 = set4(v[1]);
     const sse_t v2 = set4(v[2]);
-    
+
     const sse_t u0 = set4(u[0]);
     const sse_t u1 = set4(u[1]);
     const sse_t u2 = set4(u[2]);
@@ -236,7 +236,7 @@
       const sse_t theta4 = mul4(twoPI, lensCoordsX);
       const sse_t r4 = mul4(radius4, sqrt4( lensCoordsY ));
 
-#if 0     
+#if 0
       const sse_t rCosTheta = mul4(r4, cos4(theta4));
       const sse_t rSinTheta = mul4(r4, sin4(theta4));
 #else
@@ -279,10 +279,10 @@
 
     Real theta = 2.0 * M_PI * lens_coord_x.get(i);
     Real r = radius * SCIRun::Sqrt( lens_coord_y.get(i) );
-    
+
     Vector origin = r*Cos(theta)*u + r*Sin(theta)*v;
     Vector on_film = imageX*v+imageY*u+direction*focal_length;
-    
+
     rays.setRay(i, eye+origin, on_film-origin);
   }
 #endif




  • [Manta] r1789 - in trunk: Engine/Control Engine/SampleGenerators Model/Cameras, boulos, 10/23/2007

Archive powered by MHonArc 2.6.16.

Top of page