Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r393 - trunk/Engine/PixelSamplers


Chronological Thread 
  • From: bigler@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r393 - trunk/Engine/PixelSamplers
  • Date: Fri, 17 Jun 2005 14:32:43 -0600 (MDT)

Author: bigler
Date: Fri Jun 17 14:32:42 2005
New Revision: 393

Modified:
   trunk/Engine/PixelSamplers/JitterSampler.cc
   trunk/Engine/PixelSamplers/JitterSampler.h
Log:

JitterSampler.cc
JitterSampler.h

        Reimplemented computeAverages to be much less complex and
        easier to understand.  It doesn't really have an effect on
        performance in comparison with the old one.  Oh, well.  At
        least it's more elegant.  Reordered some code to group
        variables better.  Changed next_slot to sample_count.  I think
        that is more intuitive.


Modified: trunk/Engine/PixelSamplers/JitterSampler.cc
==============================================================================
--- trunk/Engine/PixelSamplers/JitterSampler.cc (original)
+++ trunk/Engine/PixelSamplers/JitterSampler.cc Fri Jun 17 14:32:42 2005
@@ -82,63 +82,47 @@
   context.renderer->setupFrame(context);
 }
 
-
-// Ohh, man.  Is this code wacked or what?  Anyway, the point of this
-// code it to gather up all the samples that belong to a single
-// fragment, then update the color in the fragment.  Fagments' samples
-// can exists across multiple RayPackets, so we have to be careful
-// here.  Also with the way the loop works you have to make sure you
-// pick up the last sample if it is part of a newrun (see
-// compute_last_ave below).  Anyway, I know there is probably a more
-// elegant solution out there, but this works for all sorts of
-// combinations of fragment sizes and number of samples.
-void JitterSampler::computeAverages(Fragment& fragment, RayPacket& rays,
-                                    Color* sample_color,
-                                    int* fragment_owning_sample) {
-  // At this point we have rays.getSize() number of samples
-  // that can belong to at most fragment.getSize() number of
-  // fragments.  We need to sum the colors from the samples
-  // for a single fragment then divide it by the total
-  // number of samples and add it to the fragment's total.
-  int slot_index = 0;
-  int num_samples_per_fragment = 1;
-  int current_fragment = fragment_owning_sample[slot_index];
-  Color fragment_color = sample_color[slot_index];
-  // If the last sample is a new run, you need to add it after the for loop.
-  bool compute_last_ave = false; 
-  for(int slot_index = 1; slot_index < rays.getSize(); slot_index++){
-    bool compute_average = false;
-    if (current_fragment == fragment_owning_sample[slot_index]) {
-      fragment_color += sample_color[slot_index];
-      num_samples_per_fragment++;
-      // Now the case where the last sample is has been
-      // reached, we must force it to be averaged.
-      if (slot_index == rays.getSize()-1)
-        compute_average = true;
-    } else {
-      // Now we have a break in the fragment, so time to
-      // compute the average.
-      compute_average = true;
-      // If however we have a break at the last pixel, we need to
-      // signal to pick up that extra sample.
-      if (slot_index == rays.getSize()-1)
-        compute_last_ave = true;
-    }
-    if (compute_average) {
-      // Divide the color by the number of samples and
-      // increment it with the fragment.
-      fragment.get(current_fragment).color +=
-        fragment_color / num_samples;
-      // Now update for the next fragment run
-      fragment_color = sample_color[slot_index];
-      current_fragment = fragment_owning_sample[slot_index];
-      num_samples_per_fragment = 1;
+// The fragment, sample_color, and sample_color_size paramters are
+// obvious enough, but the other two need some explaination.
+//
+// samples_collected_return represent the number of samples that have
+// already been used to color a fragment element.  If a fragment
+// element's samples span more than one RayPacket, then we account for
+// that with this variable.
+//
+// current_fragment_return keeps track of which fragment elements have
+// had their color computed.
+void JitterSampler::computeAverages(Fragment& fragment, Color* sample_color,
+                                    int sample_color_size,
+                                    int& samples_collected_return,
+                                    int& current_fragment_return) {
+  // Copy the values, so the we can hopefully use the cache when
+  // assigning the values.
+  int samples_collected = samples_collected_return;
+  int current_fragment = current_fragment_return;
+  Color fragment_color = Color::black();
+  for(int sample_index = 0; sample_index < sample_color_size; 
sample_index++) {
+    fragment_color += sample_color[sample_index];
+    samples_collected++;
+    // We've collected enough samples, so compute the average and
+    // assign it to the fragment.
+    if(samples_collected == num_samples) {
+      fragment.get(current_fragment).color += fragment_color / num_samples;
+      // Reset our accumulation variables
+      fragment_color = Color::black();
+      samples_collected = 0;
+      current_fragment++;
     }
-  } // end slot_index loop
-
-  // Pick up the last sample if we need to
-  if (compute_last_ave)
+  }
+  // Pick up left over fragments.  It is OK if this is a partial sum.
+  // The remaining sum will be picked up in the next call to
+  // computeAverages.
+  if (samples_collected > 0) {
     fragment.get(current_fragment).color += fragment_color / num_samples;
+  }
+  // Assign the values back to the parameters.
+  samples_collected_return = samples_collected;
+  current_fragment_return = current_fragment;
 }
 
 void JitterSampler::renderFragment(const RenderContext& context,
@@ -151,10 +135,6 @@
   if(fragment.getFlags() & Fragment::ConstantEye)
     flags |= RayPacket::ConstantEye;
 
-  int next_slot = 0;
-  Color sample_color[RayPacket::MaxSize];
-  int fragment_owning_sample[RayPacket::MaxSize];
-
   int consecutivex_flag = fragment.getFlags() & Fragment::ConsecutiveX;
 
   CheapRNG rng;
@@ -174,6 +154,13 @@
   Real iny = (Real)1/ny;
   Real px, py;
   
+  int sample_count = 0;
+  Color sample_color[RayPacket::MaxSize];
+  // No samples accumulated yet.
+  int samples_collected = 0;
+  // Index to the first fragment's element.
+  int current_fragment = 0;
+
   // We can compute at most RayPacket::MaxSize number of rays at time.
   for(int frag_index = 0; frag_index < fragment.getSize(); frag_index++) {
     // Initialize the color
@@ -204,34 +191,33 @@
           }
           px = (fe0.x+(x_sample))*ci.xscale+ci.xoffset;
           py = (fe0.y+(y_sample))*ci.yscale+ci.yoffset;
-          rays.setPixel(next_slot, 0, px, py, &sample_color[next_slot]);
-          fragment_owning_sample[next_slot] = frag_index;
-          next_slot++;
+          rays.setPixel(sample_count, 0, px, py, 
&sample_color[sample_count]);
+          sample_count++;
           
-          if (next_slot == RayPacket::MaxSize) {
+          if (sample_count == RayPacket::MaxSize) {
             // Filled up the ray packet, so send them off!
-            rays.resize(next_slot);
+            rays.resize(sample_count);
             context.renderer->traceEyeRays(context, rays);
 
-            computeAverages(fragment, rays,
-                            sample_color, fragment_owning_sample);
+            computeAverages(fragment, sample_color, rays.getSize(),
+                            samples_collected, current_fragment);
             
             // Now reset the index, so that we can start filling up
             // the RayPacket again.
-            next_slot = 0;
+            sample_count = 0;
             // Make sure we start with a fresh slate
             rays.resetHit();
-            //            rays.resetFlag(flags);
           }
         } // end sample filling loops
   } // end fragment loop
 
   // Pick up any stragling samples
-  if (next_slot > 0) {
-    rays.resize(next_slot);
+  if (sample_count > 0) {
+    rays.resize(sample_count);
     context.renderer->traceEyeRays(context, rays);
     
-    computeAverages(fragment, rays, sample_color, fragment_owning_sample);
+    computeAverages(fragment, sample_color, rays.getSize(),
+                    samples_collected, current_fragment);
   }
 }  
 

Modified: trunk/Engine/PixelSamplers/JitterSampler.h
==============================================================================
--- trunk/Engine/PixelSamplers/JitterSampler.h  (original)
+++ trunk/Engine/PixelSamplers/JitterSampler.h  Fri Jun 17 14:32:42 2005
@@ -31,8 +31,9 @@
     JitterSampler(const JitterSampler&);
     JitterSampler& operator=(const JitterSampler&);
 
-    void computeAverages(Fragment& fragment, RayPacket& rays,
-                         Color* sample_color, int* fragment_owning_sample);
+    void computeAverages(Fragment& fragment, Color* sample_color,
+                         int sample_color_size,
+                         int& samples_collected, int& current_fragment);
     
     int num_samples;
     // nx*ny == num_samples where nx~=ny (or as close as you can get it).




  • [MANTA] r393 - trunk/Engine/PixelSamplers, bigler, 06/17/2005

Archive powered by MHonArc 2.6.16.

Top of page