Further work on GraphicsContext/GraphicsThread

This commit is contained in:
Robert Osfield
2005-08-20 08:59:03 +00:00
parent f07b24e56b
commit ac07e07705
10 changed files with 582 additions and 295 deletions

View File

@@ -136,13 +136,16 @@ class OSG_EXPORT GraphicsContext : public Referenced
/** Realise the GraphicsContext.*/
virtual bool realize() = 0;
/** Return true if the graphics context has been realised and is ready to use.*/
virtual bool isRealized() const = 0;
bool realize();
/** close the graphics context.*/
virtual void close() = 0;
void close();
/** swap the front and back buffers.*/
void swapBuffers();
/** Return true if the graphics context has been realised and is ready to use.*/
inline bool isRealized() const { return isRealizedImplementation(); }
/** Make this graphics context current.
@@ -159,21 +162,16 @@ class OSG_EXPORT GraphicsContext : public Referenced
/** Return true if the current thread has this OpenGL graphics context.*/
inline bool isCurrent() const { return _threadOfLastMakeCurrent == OpenThreads::Thread::CurrentThread(); }
/** Make this graphics context current.*/
virtual void makeCurrentImplementation() = 0;
/** Make this graphics context current with specified read context.*/
virtual void makeContextCurrentImplementation(GraphicsContext* readContext) = 0;
/** Bind the graphics context to associated texture.*/
virtual void bindPBufferToTexture(GLenum buffer) = 0;
inline void bindPBufferToTexture(GLenum buffer) { bindPBufferToTextureImplementation(buffer); }
/** swap the front and back buffers.*/
virtual void swapBuffers() = 0;
/** Create a graphics thread to the graphics context, so that the thread handles all OpenGL operations.*/
void createGraphicsThread();
/** Assign a graphics thread to the graphics context, so that the thread handles all OpenGL operations.*/
void setGraphicsThread(GraphicsThread* gt) { _graphicsThread = gt; }
void setGraphicsThread(GraphicsThread* gt);
/** Get the graphics thread assigned the graphics context.*/
GraphicsThread* getGraphicsThread() { return _graphicsThread.get(); }
@@ -181,12 +179,45 @@ class OSG_EXPORT GraphicsContext : public Referenced
/** Get the const graphics thread assigned the graphics context.*/
const GraphicsThread* getGraphicsThread() const { return _graphicsThread.get(); }
/** Realise the GraphicsContext implementation,
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual bool realizeImplementation() = 0;
/** Return true if the graphics context has been realised, and is ready to use, implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual bool isRealizedImplementation() const = 0;
/** Close the graphics context implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual void closeImplementation() = 0;
/** Make this graphics context current implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual void makeCurrentImplementation() = 0;
/** Make this graphics context current with specified read context implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual void makeContextCurrentImplementation(GraphicsContext* readContext) = 0;
/** Pure virtual, Bind the graphics context to associated texture implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual void bindPBufferToTextureImplementation(GLenum buffer) = 0;
/** Swap the front and back buffers implementation.
* Pure virtual - must be implemented by Concrate implementations of GraphicsContext. */
virtual void swapBuffersImplementation() = 0;
protected:
GraphicsContext();
virtual ~GraphicsContext();
bool _closeOnDestruction;
ref_ptr<Traits> _traits;
ref_ptr<State> _state;
OpenThreads::Mutex _mutex;

View File

@@ -79,8 +79,7 @@ class Block: public osg::Referenced {
class GraphicsThread : public Referenced, public OpenThreads::Thread
{
public:
GraphicsThread(): _graphicsContext(0) {}
GraphicsThread();
/** Base class for implementing GraphicsThread operations.*/
struct OSG_EXPORT Operation : public Referenced
@@ -92,7 +91,11 @@ class GraphicsThread : public Referenced, public OpenThreads::Thread
* executed by the graphics thread once this operation gets to the head of the queue.*/
void add(Operation* operation, bool waitForCompletion=false);
/** Run does the graphics thread run loop.*/
virtual void run();
/** Cancel this graphics thread.*/
virtual int cancel();
protected:
@@ -102,14 +105,18 @@ class GraphicsThread : public Referenced, public OpenThreads::Thread
GraphicsContext* _graphicsContext;
typedef std::list< ref_ptr<Operation> > OperationQueue;
OpenThreads::Mutex _operationsMutex;
OperationQueue _operations;
bool _done;
OpenThreads::Mutex _operationsMutex;
osg::ref_ptr<osg::Block> _operationsBlock;
OperationQueue _operations;
};
/** SwapBufferOperation calls swap buffers on the GraphicsContext.*/
struct OSG_EXPORT SwapBufferOperation : public GraphicsThread::Operation
struct OSG_EXPORT SwapBuffersOperation : public GraphicsThread::Operation
{
virtual void operator () (GraphicsContext* context);
};
@@ -117,9 +124,18 @@ struct OSG_EXPORT SwapBufferOperation : public GraphicsThread::Operation
/** BarrierOperation allows one syncronize multiple GraphicsThreads with each other.*/
struct OSG_EXPORT BarrierOperation : public GraphicsThread::Operation, public OpenThreads::Barrier
{
BarrierOperation(int numThreads=0): OpenThreads::Barrier(numThreads) {}
enum PreBlockOp
{
NO_OPERATION,
GL_FLUSH,
GL_FINISH
};
BarrierOperation(int numThreads, PreBlockOp op=NO_OPERATION): OpenThreads::Barrier(numThreads), _preBlockOp(op) {}
virtual void operator () (GraphicsContext* context);
PreBlockOp _preBlockOp;
};
/** ReleaseContext_Block_MakeCurrentOperation releases the context for another thread to aquire,

View File

@@ -45,13 +45,13 @@ class OSGPRODUCER_EXPORT GraphicsContextImplementation : public osg::GraphicsCon
/** Realise the GraphicsContext.*/
virtual bool realize();
virtual bool realizeImplementation();
/** Return true if the graphics context has been realised and is ready to use.*/
virtual bool isRealized() const { return _rs.valid() && _rs->isRealized(); }
virtual bool isRealizedImplementation() const { return _rs.valid() && _rs->isRealized(); }
/** Release the graphics context.*/
virtual void close();
virtual void closeImplementation();
/** Make this graphics context current.*/
virtual void makeCurrentImplementation();
@@ -60,10 +60,10 @@ class OSGPRODUCER_EXPORT GraphicsContextImplementation : public osg::GraphicsCon
virtual void makeContextCurrentImplementation(GraphicsContext* readContext);
/** Bind the graphics context to associated texture.*/
virtual void bindPBufferToTexture(GLenum buffer);
virtual void bindPBufferToTextureImplementation(GLenum buffer);
/** swap the front and back buffers.*/
virtual void swapBuffers();
virtual void swapBuffersImplementation();
protected:

View File

@@ -171,14 +171,19 @@ class OSGUTIL_EXPORT RenderStage : public RenderBin
getRenderStageLighting()->addPositionedTextureAttribute(textureUnit, matrix,attr);
}
void copyTexture(osg::State& state);
virtual void drawPreRenderStages(osg::State& state,RenderLeaf*& previous);
virtual void draw(osg::State& state,RenderLeaf*& previous);
virtual void drawInner(osg::State& state,RenderLeaf*& previous, bool& doCopyTexture);
virtual void drawPostRenderStages(osg::State& state,RenderLeaf*& previous);
virtual void drawImplementation(osg::State& state,RenderLeaf*& previous);
void addToDependencyList(RenderStage* rs) { addPreRenderStage(rs); }
void addPreRenderStage(RenderStage* rs);
@@ -187,10 +192,12 @@ class OSGUTIL_EXPORT RenderStage : public RenderBin
/** Extract stats for current draw list. */
bool getStats(Statistics* primStats);
protected:
virtual ~RenderStage();
typedef std::vector< osg::ref_ptr<RenderStage> > RenderStageList;
bool _stageDrawnThisFrame;