Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r1080 - in trunk: Engine/Display Image Model/Materials


Chronological Thread 
  • From: bigler@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r1080 - in trunk: Engine/Display Image Model/Materials
  • Date: Tue, 23 May 2006 11:58:12 -0600 (MDT)

Author: bigler
Date: Tue May 23 11:58:11 2006
New Revision: 1080

Modified:
   trunk/Engine/Display/OpenGLDisplay.cc
   trunk/Engine/Display/OpenGLDisplay.h
   trunk/Engine/Display/PureOpenGLDisplay.cc
   trunk/Engine/Display/PureOpenGLDisplay.h
   trunk/Image/SimpleImage.h
   trunk/Image/SimpleImageBase.h
   trunk/Model/Materials/Phong.cc
Log:

Engine/Display/OpenGLDisplay.cc
Engine/Display/OpenGLDisplay.h

  Added verbose and pbo image options.

Engine/Display/PureOpenGLDisplay.cc
Engine/Display/PureOpenGLDisplay.h

  Added ability to use pixel buffer objects (PBOs).  These are
  suposidly the fastest way to get pixel data to the video card, but
  it has so far been the slowest.  I'm even trying to double buffer
  the sending and using of the buffers.

Image/SimpleImage.h
Image/SimpleImageBase.h

  Added pixelSize function to get the size of the pixels in bytes.

Model/Materials/Phong.cc

  Use SCIRun::Pow instead of pow to reduce ambiguity.


Modified: trunk/Engine/Display/OpenGLDisplay.cc
==============================================================================
--- trunk/Engine/Display/OpenGLDisplay.cc       (original)
+++ trunk/Engine/Display/OpenGLDisplay.cc       Tue May 23 11:58:11 2006
@@ -51,12 +51,14 @@
     } else if(args[i] == "-mode"){
       if(!getStringArg(i, args, mode))
         throw IllegalArgument("OpenGLDisplay -mode", i, args);
-      if(mode != "image" && mode != "texture")
+      if(mode != "image" && mode != "texture" && mode != "pbo")
         throw IllegalValue<string>("Illegal dispay mode", mode);
     } else if(args[i] == "-displayFrameRate"){
       displayFrameRate = true;
     } else if(args[i] == "-nodisplayFrameRate"){
       displayFrameRate = false;
+    } else if(args[i] == "-v"){
+      verbose = true;
     } else {
       throw IllegalArgument("OpenGLDisplay", i, args);
     }
@@ -80,6 +82,7 @@
   displayProc = 0;
   displayFrameRate = true;
   mode = "texture";
+  verbose = false;
 }
 
 void OpenGLDisplay::setup()
@@ -90,6 +93,7 @@
   // If this is zero then the value hasn't been used yet.
   last_frame_time = 0;
   ogl = new PureOpenGLDisplay(mode);
+  if (verbose) ogl->verbose = verbose;
 }
 
 OpenGLDisplay::~OpenGLDisplay()

Modified: trunk/Engine/Display/OpenGLDisplay.h
==============================================================================
--- trunk/Engine/Display/OpenGLDisplay.h        (original)
+++ trunk/Engine/Display/OpenGLDisplay.h        Tue May 23 11:58:11 2006
@@ -46,6 +46,7 @@
     int displayProc;
     string mode;
     PureOpenGLDisplay* ogl;
+    bool verbose;
 
     bool displayFrameRate;
 

Modified: trunk/Engine/Display/PureOpenGLDisplay.cc
==============================================================================
--- trunk/Engine/Display/PureOpenGLDisplay.cc   (original)
+++ trunk/Engine/Display/PureOpenGLDisplay.cc   Tue May 23 11:58:11 2006
@@ -26,6 +26,8 @@
   DEALINGS IN THE SOFTWARE.
 */
 
+#define GL_GLEXT_PROTOTYPES
+
 #include <Engine/Display/PureOpenGLDisplay.h>
 #include <Image/NullImage.h>
 #include <Image/Pixel.h>
@@ -37,6 +39,7 @@
 
 #include <GL/gl.h>
 #include <GL/glu.h>
+#include <GL/glext.h>
 
 #include <sgi_stl_warnings_off.h>
 #include <iostream>
@@ -90,7 +93,9 @@
 #ifndef GL_APPLE_texture_range
 #  define GL_APPLE_texture_range 0
 #  define GL_TEXTURE_STORAGE_HINT_APPLE 0
-#  define GL_STORAGE_SHARED_APPLE 0
+#  ifndef GL_STORAGE_SHARED_APPLE
+#    define GL_STORAGE_SHARED_APPLE 0
+#  endif
 #  define glTextureRangeAPPLE(x, y, z)
 #endif
 
@@ -99,6 +104,37 @@
 using namespace SCIRun;
 using namespace std;
 
+PureOpenGLDisplay::PBO::PBO()
+  :
+  need_ids(true),
+  texSize(0),
+  format(0),
+  type(0)
+{
+}
+
+PureOpenGLDisplay::PBO::~PBO()
+{
+}
+
+bool
+PureOpenGLDisplay::PBO::initialize(const SimpleImageBase* si)
+{
+  si->getResolution(stereo, xres, yres);
+  rowLength = si->getRowLength();
+
+  // We need the whole buffer size including the padding
+  GLsizeiptrARB new_texSize =
+    si->getRowLength()*yres*si->pixelSize()*(stereo? 2: 1);
+  if (new_texSize > texSize) {
+    texSize = new_texSize;
+    return true;
+  } else {
+    texSize = new_texSize;
+    return false;
+  }
+}
+
 PureOpenGLDisplay::PureOpenGLDisplay(const string& new_mode)
 {
   setDefaults();
@@ -114,12 +150,14 @@
 PureOpenGLDisplay::setDefaults()
 {
   mode = "texture";
+  verbose = false;
 }
 
 void
 PureOpenGLDisplay::setup()
 {
   need_texids = true;
+  current_pbo = 0;
   int x = 0x12345678;
   char* p = (char*)&x;
   if(p[0] == 0x12)
@@ -132,7 +170,7 @@
 PureOpenGLDisplay::setMode(const string& new_mode)
 {
   if (new_mode  != "") {
-    if(mode != "image" && mode != "texture")
+    if(mode != "image" && mode != "texture" && mode != "pbo")
       throw IllegalValue<string>("Illegal dispay mode", mode);
     mode = new_mode;
   }
@@ -148,18 +186,27 @@
       have_extension("GL_ARB_texture_rectangle")))
     {
       have_texturerectangle = true;
-      cout << "Have GL texture rectangle\n";
+      if (verbose) cout << "Have GL texture rectangle\n";
     }
   have_clientstorage = false;
   if(GL_APPLE_client_storage && have_extension("GL_APPLE_client_storage")){
     have_clientstorage = true;
-    cout << "Have GL client storage\n";
+    if (verbose) cout << "Have GL client storage\n";
   }
   have_texturerange = false;
   if(GL_APPLE_texture_range && have_extension("GL_APPLE_texture_range")){
     have_texturerange = true;
-    cout << "Have GL texture range\n";
+    if (verbose) cout << "Have GL texture range\n";
   }
+  have_pbo = false;
+  if(GL_ARB_pixel_buffer_object &&
+     (have_extension("GL_ARB_pixel_buffer_object") ||
+      have_extension("GL_EXT_pixel_buffer_object")))
+    {
+      have_pbo = true;
+      if (verbose) cout << "Have GL pixel buffer object\n";
+    }
+      
 
   // We don't want depth to be taken into consideration
   glDisable(GL_DEPTH_TEST);
@@ -188,8 +235,10 @@
   image->getResolution(stereo, xres, yres);
   glViewport(0, 0, xres, yres);
 
-  if(mode == "texture" && drawImage_texture(image)){
+  if(mode == "pbo" && drawImage_pbo(image)){
     // Drawn by texture...
+  } else if (mode == "texture" && drawImage_texture(image)) {
+    // Drawn by pbo...
   } else if(drawImage_pixels(image)){
     // Drawn by pixels...
   } else {
@@ -321,10 +370,11 @@
                xres, yres, 0, format, type,
                si->getRawData(0));
   if(stereo){
-    glBindTexture(GL_TEXTURE_RECTANGLE_EXT, texids[0]);
+    glBindTexture(GL_TEXTURE_RECTANGLE_EXT, texids[1]);
     glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, have_alpha? GL_RGBA : GL_RGB,
                  xres, yres, 0, format, type,
-                 si->getRawData(0));
+                 si->getRawData(1));
+    // Bind the left image again
     glBindTexture(GL_TEXTURE_RECTANGLE_EXT, texids[0]);
   }
 
@@ -357,6 +407,145 @@
   return true;
 }
 
+bool
+PureOpenGLDisplay::drawImage_pbo(const Image* image)
+{
+  if(!have_texturerectangle || !have_pbo)
+    return false;
+  const SimpleImageBase* si = dynamic_cast<const SimpleImageBase*>(image);
+  if(!si)
+    return false;
+
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+  gluOrtho2D(0, 1, 0, 1);
+  glMatrixMode(GL_MODELVIEW);
+  glLoadIdentity();
+
+  glDisable(GL_TEXTURE_2D);
+  glEnable(GL_TEXTURE_RECTANGLE_EXT);
+
+  gl_print_error(__FILE__,__LINE__);
+
+  GLenum format;
+  GLenum type;
+  bool have_alpha;
+
+  if (!glImageInfo(image, format, type, have_alpha)) {
+    return false;
+  }
+
+  PBO* pbo = &pbos[current_pbo];
+
+  if(pbo->need_ids){
+    cerr << "Computing ids for current_pbo = "<<current_pbo<<"\n";
+    // Free old textures if necessary...
+    glGenTextures(1, &pbo->texId);
+    glGenBuffers(1, &pbo->bufId);
+    pbo->need_ids = false;
+  }
+
+  bool resize_buffer = pbo->initialize(si);
+
+  // Set up the buffer
+  glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo->bufId);
+
+  if (resize_buffer) {
+    // Allocate more space for the buffer.
+    
+    // Reset the contents of the texSize-sized buffer object
+    glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo->texSize, NULL,
+                    GL_STREAM_DRAW_ARB);
+  }
+
+  // Map the memory.  The contents are undefined, because of the
+  // previous call to glBufferData.
+  void* pboMemory = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB,
+                                   GL_WRITE_ONLY);
+
+  // Copy the data
+  memcpy(pboMemory, si->getRawData(0), pbo->texSize);
+
+  // Unmap the texture image buffer
+  glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
+
+  // Now use the other pbo, but only if it has been initialized
+  int prev_pbo = 1 - current_pbo;
+  // Cycle to the next pbo
+  current_pbo = 1 - current_pbo;
+  pbo = &pbos[prev_pbo];
+  if(pbo->need_ids) {
+    cerr << "Skipping rendering frame for prev_pbo = "<<prev_pbo<<"\n";
+    return true;
+  }
+
+  
+  glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo->bufId);
+  
+  // Bind the texture and supply the parameters
+  glBindTexture(GL_TEXTURE_RECTANGLE_EXT, pbo->texId);
+
+  glTexParameteri (GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_S, 
GL_CLAMP_TO_EDGE);
+  glTexParameteri (GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_T, 
GL_CLAMP_TO_EDGE);
+  glTexParameteri (GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, 
GL_NEAREST);
+  glTexParameteri (GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, 
GL_NEAREST);
+  glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+  
+  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
+  glPixelStorei(GL_UNPACK_ROW_LENGTH, pbo->rowLength);
+
+  int xres = pbo->xres;
+  int yres = pbo->yres;
+  bool stereo = pbo->stereo;
+  // Check to see if we need to rebind a new texture or do a SubImage
+  if (resize_buffer                ||
+      format     != pbo->format     ||
+      type       != pbo->type       ||
+      have_alpha != pbo->have_alpha)
+    {
+
+      glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0,
+                   have_alpha? GL_RGBA : GL_RGB,
+                   xres, yres+(stereo? yres : 0), 0,
+                   format, type,
+                   NULL);
+      pbo->format     = format;
+      pbo->type       = type;
+      pbo->have_alpha = have_alpha;
+    } else {
+      // Use SubImage
+      glTexSubImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, 0, 0,
+                      xres, yres+(stereo? yres : 0),
+                      pbo->format, pbo->type, NULL);
+    }
+
+  glDrawBuffer(stereo? GL_BACK_LEFT : GL_BACK);
+  glBegin(GL_QUADS);
+  {
+    glTexCoord2f(   0,    0); glVertex2f(0, 0);
+    glTexCoord2f(xres,    0); glVertex2f(1, 0);
+    glTexCoord2f(xres, yres); glVertex2f(1, 1);
+    glTexCoord2f(   0, yres); glVertex2f(0, 1);
+  }
+  glEnd();
+  if(stereo){
+    glDrawBuffer(GL_BACK_RIGHT);
+    glBegin(GL_QUADS);
+    {
+      glTexCoord2f(   0,      yres);  glVertex2f(0, 0);
+      glTexCoord2f(xres,      yres);  glVertex2f(1, 0);
+      glTexCoord2f(xres, yres+yres);  glVertex2f(1, 1);
+      glTexCoord2f(   0, yres+yres);  glVertex2f(0, 1);
+    }
+    glEnd();
+  }
+
+  // Cleanup
+  glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
+  glDisable(GL_TEXTURE_RECTANGLE_EXT);
+  return true;
+}
+
 void
 PureOpenGLDisplay::gl_print_error(const char *file, int line)
 {
@@ -384,3 +573,28 @@
   }
   return false;
 }
+
+bool
+PureOpenGLDisplay::glImageInfo(const Image* image,
+                               GLenum& format, GLenum& type, bool& 
have_alpha)
+{
+  have_alpha = true;
+  if(typeid(*image) == typeid(SimpleImage<RGBA8Pixel>)){
+    format = GL_RGBA;
+    type = big_endian? GL_UNSIGNED_INT_8_8_8_8 : GL_UNSIGNED_INT_8_8_8_8_REV;
+  } else if(typeid(*image) == typeid(SimpleImage<RGB8Pixel>)){
+    format = GL_RGB;
+    type = GL_UNSIGNED_BYTE;
+    have_alpha = false;
+  } else if(typeid(*image) == typeid(SimpleImage<ABGR8Pixel>)){
+    format = GL_RGBA;
+    type = big_endian? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_INT_8_8_8_8;
+  } else if(typeid(*image) == typeid(SimpleImage<ARGB8Pixel>)){
+    format = GL_BGRA;
+    type = big_endian? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_INT_8_8_8_8;
+  } else {
+    return false;
+  }
+
+  return true;
+}  

Modified: trunk/Engine/Display/PureOpenGLDisplay.h
==============================================================================
--- trunk/Engine/Display/PureOpenGLDisplay.h    (original)
+++ trunk/Engine/Display/PureOpenGLDisplay.h    Tue May 23 11:58:11 2006
@@ -44,7 +44,8 @@
 
 namespace Manta {
   class Image;
-
+  class SimpleImageBase;
+  
   using namespace std;
   
   class PureOpenGLDisplay {
@@ -62,6 +63,7 @@
     // Renders the image
     void displayImage(const Image* image);
 
+    bool verbose;
   private:
     PureOpenGLDisplay(const PureOpenGLDisplay&);
     PureOpenGLDisplay& operator=(const PureOpenGLDisplay&);
@@ -72,17 +74,45 @@
     bool need_texids;
     GLuint texids[2];
 
+    struct PBO {
+      PBO();
+      ~PBO();
+
+      bool need_ids;
+      GLuint bufId, texId;
+      GLsizeiptrARB texSize;
+      GLenum format;
+      GLenum type;
+      bool have_alpha;
+
+      GLint rowLength;
+      int xres, yres;
+      bool stereo;
+
+      // Takes info from SimpleImageBase and fills in some values.
+      // Returns true if resizing should occur.
+      bool initialize(const SimpleImageBase* si);
+    };
+    PBO pbos[2];
+    int current_pbo;
+
     bool have_texturerectangle;
     bool have_clientstorage;
     bool have_texturerange;
+    bool have_pbo;
     bool big_endian;
 
     bool drawImage_texture(const Image* image);
+    bool drawImage_pbo(const Image* image);
     bool drawImage_pixels(const Image* image);
 
     bool have_extension(const char* name);
     
     void gl_print_error(const char *file, int line);
+
+    // returns false if there were problems
+    bool glImageInfo(const Image* image,
+                     GLenum& format, GLenum& type, bool& have_alpha);
 
     // Called at the beginning of the constructor
     void setDefaults();

Modified: trunk/Image/SimpleImage.h
==============================================================================
--- trunk/Image/SimpleImage.h   (original)
+++ trunk/Image/SimpleImage.h   Tue May 23 11:58:11 2006
@@ -52,6 +52,7 @@
     Pixel* getRawPixels(int which_eye) const;
     void* getRawData(int which_eye) const;
 
+    virtual size_t pixelSize() const { return sizeof(Pixel); }
   private:
     SimpleImage(const Image&);
     SimpleImage& operator=(const SimpleImage&);

Modified: trunk/Image/SimpleImageBase.h
==============================================================================
--- trunk/Image/SimpleImageBase.h       (original)
+++ trunk/Image/SimpleImageBase.h       Tue May 23 11:58:11 2006
@@ -40,6 +40,7 @@
     virtual void setValid(bool to);
     virtual void getResolution(bool& stereo, int& xres, int& yres) const;
     virtual void* getRawData(int which_eye) const = 0;
+    virtual size_t pixelSize() const = 0;
 
     int getRowLength() const {
       return xpad;

Modified: trunk/Model/Materials/Phong.cc
==============================================================================
--- trunk/Model/Materials/Phong.cc      (original)
+++ trunk/Model/Materials/Phong.cc      Tue May 23 11:58:11 2006
@@ -57,7 +57,7 @@
   speculartex = new Constant<Color>(specular);
   refltex = new Constant<ColorComponent>(refl);
   do_refl = (refl != 0);
-  highlight_threshold = pow(COLOR_EPSILON, 1./specpow);
+  highlight_threshold = SCIRun::Pow(COLOR_EPSILON, 1./specpow);
 }
 
 Phong::Phong(const Texture<Color>* diffusetex,
@@ -75,7 +75,7 @@
   } else {
     do_refl = false;
   }
-  highlight_threshold = pow(COLOR_EPSILON, 1./specpow);
+  highlight_threshold = SCIRun::Pow(COLOR_EPSILON, 1./specpow);
 }
 
 Phong::~Phong()




  • [MANTA] r1080 - in trunk: Engine/Display Image Model/Materials, bigler, 05/23/2006

Archive powered by MHonArc 2.6.16.

Top of page