1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSGUTIL_RENDERSTAGE
15#define OSGUTIL_RENDERSTAGE 1
17#include <osg/ColorMask>
18#include <osg/Viewport>
20#include <osg/FrameBufferObject>
23#include <osgUtil/RenderBin>
24#include <osgUtil/PositionalStateContainer>
29 * RenderStage base class. Used for encapsulate a complete stage in
30 * rendering - setting up of viewport, the projection and model
31 * matrices and rendering the RenderBin's enclosed with this RenderStage.
32 * RenderStage also has a dependency list of other RenderStages, each
33 * of which must be called before the rendering of this stage. These
34 * 'pre' rendering stages are used for advanced rendering techniques
35 * like multistage pixel shading or impostors.
37class OSGUTIL_EXPORT RenderStage : public RenderBin
41 typedef std::pair< int , osg::ref_ptr<RenderStage> > RenderStageOrderPair;
42 typedef std::list< RenderStageOrderPair > RenderStageList;
45 RenderStage(SortMode mode);
47 RenderStage(const RenderStage& rhs,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
49 virtual osg::Object* cloneType() const { return new RenderStage(); }
50 virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new RenderStage(*this,copyop); } // note only implements a clone of type.
51 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const RenderStage*>(obj)!=0L; }
52 virtual const char* className() const { return "RenderStage"; }
57 /** Set the draw buffer used at the start of each frame draw. */
58 void setDrawBuffer(GLenum buffer, bool applyMask = true ) { _drawBuffer = buffer; setDrawBufferApplyMask( applyMask ); }
60 /** Get the draw buffer used at the start of each frame draw. */
61 GLenum getDrawBuffer() const { return _drawBuffer; }
63 /** Get the apply mask defining whether glDrawBuffer is called at each frame draw. */
64 bool getDrawBufferApplyMask() const { return _drawBufferApplyMask; }
66 /** Set the apply mask defining whether glDrawBuffer is called at each frame draw. */
67 void setDrawBufferApplyMask( bool applyMask ) { _drawBufferApplyMask = applyMask; }
71 /** Set the read buffer for any required copy operations to use. */
72 void setReadBuffer(GLenum buffer, bool applyMask = true) { _readBuffer = buffer; setReadBufferApplyMask( applyMask ); }
74 /** Get the read buffer for any required copy operations to use. */
75 GLenum getReadBuffer() const { return _readBuffer; }
77 /** Get the apply mask defining whether glReadBuffer is called at each frame draw. */
78 bool getReadBufferApplyMask() const { return _readBufferApplyMask; }
80 /** Set the apply mask defining whether glReadBuffer is called at each frame draw. */
81 void setReadBufferApplyMask( bool applyMask ) { _readBufferApplyMask = applyMask; }
84 /** Set the viewport.*/
85 void setViewport(osg::Viewport* viewport) { _viewport = viewport; }
87 /** Get the const viewport. */
88 const osg::Viewport* getViewport() const { return _viewport.get(); }
90 /** Get the viewport. */
91 osg::Viewport* getViewport() { return _viewport.get(); }
93 /** Set the initial view matrix.*/
94 void setInitialViewMatrix(const osg::RefMatrix* matrix) { _initialViewMatrix = matrix; }
96 /** Get the initial view matrix.*/
97 const osg::RefMatrix* getInitialViewMatrix() { return _initialViewMatrix.get(); }
99 /** Set the clear mask used in glClear(..).
100 * Defaults to GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT. */
101 void setClearMask(GLbitfield mask) { _clearMask = mask; }
103 /** Get the clear mask.*/
104 GLbitfield getClearMask() const { return _clearMask; }
107 void setColorMask(osg::ColorMask* cm) { _colorMask = cm; }
108 osg::ColorMask* getColorMask() { return _colorMask.get(); }
109 const osg::ColorMask* getColorMask() const { return _colorMask.get(); }
112 /** Set the clear color used in glClearColor(..).
113 * glClearColor is only called if mask & GL_COLOR_BUFFER_BIT is true*/
114 void setClearColor(const osg::Vec4& color) { _clearColor=color; }
116 /** Get the clear color.*/
117 const osg::Vec4& getClearColor() const { return _clearColor; }
119 /** Set the clear accum used in glClearAccum(..).
120 * glClearAcumm is only called if mask & GL_ACCUM_BUFFER_BIT is true. */
121 void setClearAccum(const osg::Vec4& color) { _clearAccum=color; }
123 /** Get the clear accum.*/
124 const osg::Vec4& getClearAccum() const { return _clearAccum; }
126 /** Set the clear depth used in glClearDepth(..). Defaults to 1.0
127 * glClearDepth is only called if mask & GL_DEPTH_BUFFER_BIT is true. */
128 void setClearDepth(double depth) { _clearDepth=depth; }
130 /** Get the clear depth.*/
131 double getClearDepth() const { return _clearDepth; }
133 /** Set the clear stencil value used in glClearStencil(). Defaults to 0;
134 * glClearStencil is only called if mask & GL_STENCIL_BUFFER_BIT is true*/
135 void setClearStencil(int stencil) { _clearStencil=stencil; }
137 /** Get the clear color.*/
138 int getClearStencil() const { return _clearStencil; }
141 void setCamera(osg::Camera* camera) { if (_camera!=camera) { _camera = camera; _cameraRequiresSetUp = true; } }
142 osg::Camera* getCamera() { return _camera.get(); }
143 const osg::Camera* getCamera() const { return _camera.get(); }
145 void setCameraRequiresSetUp(bool flag) { _cameraRequiresSetUp = flag; }
146 bool getCameraRequiresSetUp() const { return _cameraRequiresSetUp; }
148 void setCameraAttachmentMapCount(unsigned int v) { _cameraAttachmentMapModifiedCount = v; }
149 unsigned int getCameraAttachmentMapCount() { return _cameraAttachmentMapModifiedCount; }
152 /** Attempt the set the RenderStage from the Camera settings.*/
153 void runCameraSetUp(osg::RenderInfo& renderInfo);
155 void setTexture(osg::Texture* texture, unsigned int level = 0, unsigned int face=0) { _texture = texture; _level = level; _face = face; }
156 osg::Texture* getTexture() { return _texture.get(); }
158 void setImage(osg::Image* image) { _image = image; }
159 osg::Image* getImage() { return _image.get(); }
161 void setImageReadPixelFormat(GLenum format) { _imageReadPixelFormat = format; }
162 GLenum getImageReadPixelFormat() const { return _imageReadPixelFormat; }
164 void setImageReadPixelDataType(GLenum type) { _imageReadPixelDataType = type; }
165 GLenum getImageReadPixelDataType() const { return _imageReadPixelDataType; }
167 /** Set a framebuffer object to render into. It is permissible for the
168 * framebuffer object to be multisampled, in which case you should also
169 * set a resolve framebuffer object - see setMultisampleResolveFramebufferObject(). */
170 void setFrameBufferObject(osg::FrameBufferObject* fbo) { _fbo = fbo; }
171 osg::FrameBufferObject* getFrameBufferObject() { return _fbo.get(); }
172 const osg::FrameBufferObject* getFrameBufferObject() const { return _fbo.get(); }
174 /** Sets the destination framebuffer object for glBlitFramebufferEXT to
175 * resolve a multisampled framebuffer object after the RenderStage is
176 * drawn. The resolve framebuffer object must not be multisampled. The
177 * resolve framebuffer object is only necessary if the primary framebuffer
178 * object is multisampled, if not then leave it set to null. */
179 void setMultisampleResolveFramebufferObject(osg::FrameBufferObject* fbo);
180 osg::FrameBufferObject* getMultisampleResolveFramebufferObject() { return _resolveFbo.get(); }
181 const osg::FrameBufferObject* getMultisampleResolveFramebufferObject() const { return _resolveFbo.get(); }
183 /** Set whether the framebuffer object should be unbound after
184 * rendering. By default this is set to true. Set it to false if the
185 * unbinding is not required. */
186 void setDisableFboAfterRender(bool disable) {_disableFboAfterRender = disable;}
187 bool getDisableFboAfterRender() const {return _disableFboAfterRender;}
189 void setGraphicsContext(osg::GraphicsContext* context) { _graphicsContext = context; }
190 osg::GraphicsContext* getGraphicsContext() { return _graphicsContext.get(); }
191 const osg::GraphicsContext* getGraphicsContext() const { return _graphicsContext.get(); }
196 void setInheritedPositionalStateContainerMatrix(const osg::Matrix& matrix) { _inheritedPositionalStateContainerMatrix = matrix; }
197 const osg::Matrix& getInheritedPositionalStateContainerMatrix() const { return _inheritedPositionalStateContainerMatrix; }
199 void setInheritedPositionalStateContainer(PositionalStateContainer* rsl) { _inheritedPositionalStateContainer = rsl; }
200 PositionalStateContainer* getInheritedPositionalStateContainer() { return _inheritedPositionalStateContainer.get(); }
202 void setPositionalStateContainer(PositionalStateContainer* rsl) { _renderStageLighting = rsl; }
204 PositionalStateContainer* getPositionalStateContainer() const
206 if (!_renderStageLighting.valid()) _renderStageLighting = new PositionalStateContainer;
207 return _renderStageLighting.get();
210 virtual void addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr)
212 getPositionalStateContainer()->addPositionedAttribute(matrix,attr);
215 virtual void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr)
217 getPositionalStateContainer()->addPositionedTextureAttribute(textureUnit, matrix,attr);
220 void copyTexture(osg::RenderInfo& renderInfo);
224 virtual void drawPreRenderStages(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
226 virtual void draw(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
228 virtual void drawInner(osg::RenderInfo& renderInfo,RenderLeaf*& previous, bool& doCopyTexture);
230 virtual void drawPostRenderStages(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
232 virtual void drawImplementation(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
235 void addToDependencyList(RenderStage* rs) { addPreRenderStage(rs); }
237 void addPreRenderStage(RenderStage* rs, int order = 0);
239 void addPostRenderStage(RenderStage* rs, int order = 0);
241 const RenderStageList& getPreRenderList() const { return _preRenderList; }
242 RenderStageList& getPreRenderList() { return _preRenderList; }
244 const RenderStageList& getPostRenderList() const { return _postRenderList; }
245 RenderStageList& getPostRenderList() { return _postRenderList; }
247 /** Extract stats for current draw list. */
248 bool getStats(Statistics& stats) const;
250 /** Compute the number of dynamic RenderLeaves.*/
251 virtual unsigned int computeNumberOfDynamicRenderLeaves() const;
255 osg::ref_ptr<osg::Image> _image;
256 GLenum _imageReadPixelFormat;
257 GLenum _imageReadPixelDataType;
260 void attach(osg::Camera::BufferComponent buffer, osg::Image* image);
262 /** search through any pre and post RenderStage that reference a Camera, and take a reference to each of these cameras to prevent them being deleted while they are still be used by the drawing thread.*/
263 void collateReferencesToDependentCameras();
265 /** clear the references to any dependent cameras.*/
266 void clearReferencesToDependentCameras();
268 /** If State is non-zero, this function releases any associated OpenGL objects for
269 * the specified graphics context. Otherwise, releases OpenGL objexts
270 * for all graphics contexts. */
271 virtual void releaseGLObjects(osg::State* state= 0) const;
275 virtual ~RenderStage();
277 typedef std::vector< osg::ref_ptr<osg::Camera> > Cameras;
279 bool _stageDrawnThisFrame;
280 RenderStageList _preRenderList;
281 RenderStageList _postRenderList;
283 Cameras _dependentCameras;
285 // viewport x,y,width,height.
286 osg::ref_ptr<osg::Viewport> _viewport;
287 osg::ref_ptr<const osg::RefMatrix> _initialViewMatrix;
290 bool _drawBufferApplyMask;
292 bool _readBufferApplyMask;
293 GLbitfield _clearMask;
294 osg::ref_ptr<osg::ColorMask> _colorMask;
295 osg::Vec4 _clearColor;
296 osg::Vec4 _clearAccum;
300 bool _cameraRequiresSetUp;
301 unsigned int _cameraAttachmentMapModifiedCount;
302 osg::observer_ptr<osg::Camera> _camera;
304 osg::ref_ptr<osg::Texture> _texture;
308 osg::ref_ptr<osg::Image> _image;
309 GLenum _imageReadPixelFormat;
310 GLenum _imageReadPixelDataType;
312 std::map< osg::Camera::BufferComponent, Attachment> _bufferAttachmentMap;
314 osg::ref_ptr<osg::FrameBufferObject> _fbo;
315 osg::ref_ptr<osg::FrameBufferObject> _resolveFbo;
316 osg::ref_ptr<osg::GraphicsContext> _graphicsContext;
317 bool _disableFboAfterRender;
319 mutable osg::Matrix _inheritedPositionalStateContainerMatrix;
320 mutable osg::ref_ptr<PositionalStateContainer> _inheritedPositionalStateContainer;
321 mutable osg::ref_ptr<PositionalStateContainer> _renderStageLighting;