Skip to content

Commit

Permalink
Add pixel buffers to the USE_SHADERS codepath. Turn on USE_SHADERS.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://truffle/home/andrew/repo/memview@63 6d4121a9-3ab8-48c6-8db9-97dca464e206
  • Loading branch information
ajclinto committed Jan 23, 2012
1 parent 549b866 commit 9962496
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
38 changes: 33 additions & 5 deletions GLImage.h
Expand Up @@ -6,25 +6,52 @@ class GLImage {
GLImage() GLImage()
: myData(0) : myData(0)
, myWidth(0) , myWidth(0)
, myHeight(0) {} , myHeight(0)
, myOwnData(false) {}
~GLImage()
{
if (myOwnData)
delete [] myData;
}


void resize(int width, int height) void resize(int width, int height)
{ {
if (myWidth != width || if (myWidth != width ||
myHeight != height) myHeight != height)
{ {
delete [] myData; if (myOwnData)
myData = 0; {
delete [] myData;
myData = 0;
}

myWidth = width; myWidth = width;
myHeight = height; myHeight = height;

if (myWidth && myHeight)
{
myData = new uint32[myWidth*myHeight];
myOwnData = true;
}
} }
}


if (!myData) // Interface to allow external ownership
myData = new uint32[myWidth*myHeight]; void setSize(int width, int height)
{
myWidth = width;
myHeight = height;
myOwnData = false;
}
void setData(uint32 *data)
{
myData = data;
myOwnData = false;
} }


int width() const { return myWidth; } int width() const { return myWidth; }
int height() const { return myHeight; } int height() const { return myHeight; }
size_t bytes() const { return myWidth*myHeight*sizeof(uint32); }
const uint32 *data() const { return myData; } const uint32 *data() const { return myData; }


void fill(uint32 val) void fill(uint32 val)
Expand All @@ -46,6 +73,7 @@ class GLImage {
uint32 *myData; uint32 *myData;
int myWidth; int myWidth;
int myHeight; int myHeight;
bool myOwnData;
}; };


#endif #endif
36 changes: 31 additions & 5 deletions Window.C
@@ -1,6 +1,13 @@
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>

#include "Window.h" #include "Window.h"
#include "MemoryState.h" #include "MemoryState.h"


// This define causes rendering to use textures, PBOs, and to render a
// full-screen quad. When disabled, glDrawPixels is used.
#define USE_SHADERS

static const QSize theDefaultSize(800, 600); static const QSize theDefaultSize(800, 600);


Window::Window(int argc, char *argv[]) Window::Window(int argc, char *argv[])
Expand Down Expand Up @@ -61,6 +68,7 @@ MemViewWidget::MemViewWidget(int argc, char *argv[],
, myHScrollBar(hscrollbar) , myHScrollBar(hscrollbar)
, myTexture(0) , myTexture(0)
, myList(0) , myList(0)
, myPixelBuffer(0)
, myStopWatch(false) , myStopWatch(false)
, myDragging(false) , myDragging(false)
{ {
Expand Down Expand Up @@ -109,6 +117,8 @@ MemViewWidget::initializeGL()
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);


glGenBuffers(1, &myPixelBuffer);

myList = glGenLists(1); myList = glGenLists(1);
glNewList(myList, GL_COMPILE); glNewList(myList, GL_COMPILE);


Expand Down Expand Up @@ -163,6 +173,16 @@ MemViewWidget::resizeGL(int width, int height)


glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();

#if defined(USE_SHADERS)
myImage.setSize(width, height);

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, myPixelBuffer);
glBufferData(GL_PIXEL_UNPACK_BUFFER, myImage.bytes(), 0, GL_STREAM_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
#else
myImage.resize(width, height);
#endif
} }


void void
Expand All @@ -175,15 +195,22 @@ MemViewWidget::paintGL()
myAnchor.myAbsoluteOffset; myAnchor.myAbsoluteOffset;
myAnchor.myColumn += myHScrollBar->value() - myAnchor.myColumn; myAnchor.myColumn += myHScrollBar->value() - myAnchor.myColumn;


#if !defined(USE_SHADERS)
myState->fillImage(myImage, myAnchor); myState->fillImage(myImage, myAnchor);


#if !defined(USE_SHADERS)
glDrawPixels(myImage.width(), myImage.height(), GL_BGRA, glDrawPixels(myImage.width(), myImage.height(), GL_BGRA,
GL_UNSIGNED_BYTE, myImage.data()); GL_UNSIGNED_BYTE, myImage.data());
#else #else
glTexImage2D(GL_TEXTURE_2D, 0, 3, glBindBuffer(GL_PIXEL_UNPACK_BUFFER, myPixelBuffer);

myImage.setData((uint32 *)
glMapBufferARB(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY));
myState->fillImage(myImage, myAnchor);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
myImage.width(), myImage.height(), 0, GL_BGRA, myImage.width(), myImage.height(), 0, GL_BGRA,
GL_UNSIGNED_BYTE, myImage.data()); GL_UNSIGNED_BYTE, 0);


glCallList(myList); glCallList(myList);
#endif #endif
Expand All @@ -207,10 +234,9 @@ MemViewWidget::resizeEvent(QResizeEvent *event)


myVScrollBar->setPageStep(h); myVScrollBar->setPageStep(h);
myHScrollBar->setPageStep(w); myHScrollBar->setPageStep(w);
myImage.resize(w, h);
update();


QGLWidget::resizeEvent(event); QGLWidget::resizeEvent(event);
update();
} }
} }


Expand Down
1 change: 1 addition & 0 deletions Window.h
Expand Up @@ -81,6 +81,7 @@ private slots:
QGLShaderProgram *myProgram; QGLShaderProgram *myProgram;
GLuint myTexture; GLuint myTexture;
GLuint myList; GLuint myList;
GLuint myPixelBuffer;


MemoryState::AnchorInfo myAnchor; MemoryState::AnchorInfo myAnchor;
MemoryState *myState; MemoryState *myState;
Expand Down

0 comments on commit 9962496

Please sign in to comment.