openscenegraph
RenderStage
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
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.
7 *
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.
12*/
13
14#ifndef OSGUTIL_RENDERSTAGE
15#define OSGUTIL_RENDERSTAGE 1
16
17#include <osg/ColorMask>
18#include <osg/Viewport>
19#include <osg/Texture>
20#include <osg/FrameBufferObject>
21#include <osg/Camera>
22
23#include <osgUtil/RenderBin>
24#include <osgUtil/PositionalStateContainer>
25
26namespace osgUtil {
27
28/**
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.
36 */
37class OSGUTIL_EXPORT RenderStage : public RenderBin
38{
39 public:
40
41 typedef std::pair< int , osg::ref_ptr<RenderStage> > RenderStageOrderPair;
42 typedef std::list< RenderStageOrderPair > RenderStageList;
43
44 RenderStage();
45 RenderStage(SortMode mode);
46
47 RenderStage(const RenderStage& rhs,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
48
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"; }
53
54 virtual void reset();
55
56
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 ); }
59
60 /** Get the draw buffer used at the start of each frame draw. */
61 GLenum getDrawBuffer() const { return _drawBuffer; }
62
63 /** Get the apply mask defining whether glDrawBuffer is called at each frame draw. */
64 bool getDrawBufferApplyMask() const { return _drawBufferApplyMask; }
65
66 /** Set the apply mask defining whether glDrawBuffer is called at each frame draw. */
67 void setDrawBufferApplyMask( bool applyMask ) { _drawBufferApplyMask = applyMask; }
68
69
70
71 /** Set the read buffer for any required copy operations to use. */
72 void setReadBuffer(GLenum buffer, bool applyMask = true) { _readBuffer = buffer; setReadBufferApplyMask( applyMask ); }
73
74 /** Get the read buffer for any required copy operations to use. */
75 GLenum getReadBuffer() const { return _readBuffer; }
76
77 /** Get the apply mask defining whether glReadBuffer is called at each frame draw. */
78 bool getReadBufferApplyMask() const { return _readBufferApplyMask; }
79
80 /** Set the apply mask defining whether glReadBuffer is called at each frame draw. */
81 void setReadBufferApplyMask( bool applyMask ) { _readBufferApplyMask = applyMask; }
82
83
84 /** Set the viewport.*/
85 void setViewport(osg::Viewport* viewport) { _viewport = viewport; }
86
87 /** Get the const viewport. */
88 const osg::Viewport* getViewport() const { return _viewport.get(); }
89
90 /** Get the viewport. */
91 osg::Viewport* getViewport() { return _viewport.get(); }
92
93 /** Set the initial view matrix.*/
94 void setInitialViewMatrix(const osg::RefMatrix* matrix) { _initialViewMatrix = matrix; }
95
96 /** Get the initial view matrix.*/
97 const osg::RefMatrix* getInitialViewMatrix() { return _initialViewMatrix.get(); }
98
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; }
102
103 /** Get the clear mask.*/
104 GLbitfield getClearMask() const { return _clearMask; }
105
106
107 void setColorMask(osg::ColorMask* cm) { _colorMask = cm; }
108 osg::ColorMask* getColorMask() { return _colorMask.get(); }
109 const osg::ColorMask* getColorMask() const { return _colorMask.get(); }
110
111
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; }
115
116 /** Get the clear color.*/
117 const osg::Vec4& getClearColor() const { return _clearColor; }
118
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; }
122
123 /** Get the clear accum.*/
124 const osg::Vec4& getClearAccum() const { return _clearAccum; }
125
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; }
129
130 /** Get the clear depth.*/
131 double getClearDepth() const { return _clearDepth; }
132
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; }
136
137 /** Get the clear color.*/
138 int getClearStencil() const { return _clearStencil; }
139
140
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(); }
144
145 void setCameraRequiresSetUp(bool flag) { _cameraRequiresSetUp = flag; }
146 bool getCameraRequiresSetUp() const { return _cameraRequiresSetUp; }
147
148 void setCameraAttachmentMapCount(unsigned int v) { _cameraAttachmentMapModifiedCount = v; }
149 unsigned int getCameraAttachmentMapCount() { return _cameraAttachmentMapModifiedCount; }
150
151
152 /** Attempt the set the RenderStage from the Camera settings.*/
153 void runCameraSetUp(osg::RenderInfo& renderInfo);
154
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(); }
157
158 void setImage(osg::Image* image) { _image = image; }
159 osg::Image* getImage() { return _image.get(); }
160
161 void setImageReadPixelFormat(GLenum format) { _imageReadPixelFormat = format; }
162 GLenum getImageReadPixelFormat() const { return _imageReadPixelFormat; }
163
164 void setImageReadPixelDataType(GLenum type) { _imageReadPixelDataType = type; }
165 GLenum getImageReadPixelDataType() const { return _imageReadPixelDataType; }
166
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(); }
173
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(); }
182
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;}
188
189 void setGraphicsContext(osg::GraphicsContext* context) { _graphicsContext = context; }
190 osg::GraphicsContext* getGraphicsContext() { return _graphicsContext.get(); }
191 const osg::GraphicsContext* getGraphicsContext() const { return _graphicsContext.get(); }
192
193
194
195
196 void setInheritedPositionalStateContainerMatrix(const osg::Matrix& matrix) { _inheritedPositionalStateContainerMatrix = matrix; }
197 const osg::Matrix& getInheritedPositionalStateContainerMatrix() const { return _inheritedPositionalStateContainerMatrix; }
198
199 void setInheritedPositionalStateContainer(PositionalStateContainer* rsl) { _inheritedPositionalStateContainer = rsl; }
200 PositionalStateContainer* getInheritedPositionalStateContainer() { return _inheritedPositionalStateContainer.get(); }
201
202 void setPositionalStateContainer(PositionalStateContainer* rsl) { _renderStageLighting = rsl; }
203
204 PositionalStateContainer* getPositionalStateContainer() const
205 {
206 if (!_renderStageLighting.valid()) _renderStageLighting = new PositionalStateContainer;
207 return _renderStageLighting.get();
208 }
209
210 virtual void addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr)
211 {
212 getPositionalStateContainer()->addPositionedAttribute(matrix,attr);
213 }
214
215 virtual void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr)
216 {
217 getPositionalStateContainer()->addPositionedTextureAttribute(textureUnit, matrix,attr);
218 }
219
220 void copyTexture(osg::RenderInfo& renderInfo);
221
222 virtual void sort();
223
224 virtual void drawPreRenderStages(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
225
226 virtual void draw(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
227
228 virtual void drawInner(osg::RenderInfo& renderInfo,RenderLeaf*& previous, bool& doCopyTexture);
229
230 virtual void drawPostRenderStages(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
231
232 virtual void drawImplementation(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
233
234
235 void addToDependencyList(RenderStage* rs) { addPreRenderStage(rs); }
236
237 void addPreRenderStage(RenderStage* rs, int order = 0);
238
239 void addPostRenderStage(RenderStage* rs, int order = 0);
240
241 const RenderStageList& getPreRenderList() const { return _preRenderList; }
242 RenderStageList& getPreRenderList() { return _preRenderList; }
243
244 const RenderStageList& getPostRenderList() const { return _postRenderList; }
245 RenderStageList& getPostRenderList() { return _postRenderList; }
246
247 /** Extract stats for current draw list. */
248 bool getStats(Statistics& stats) const;
249
250 /** Compute the number of dynamic RenderLeaves.*/
251 virtual unsigned int computeNumberOfDynamicRenderLeaves() const;
252
253 struct Attachment
254 {
255 osg::ref_ptr<osg::Image> _image;
256 GLenum _imageReadPixelFormat;
257 GLenum _imageReadPixelDataType;
258 };
259
260 void attach(osg::Camera::BufferComponent buffer, osg::Image* image);
261
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();
264
265 /** clear the references to any dependent cameras.*/
266 void clearReferencesToDependentCameras();
267
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;
272
273protected:
274
275 virtual ~RenderStage();
276
277 typedef std::vector< osg::ref_ptr<osg::Camera> > Cameras;
278
279 bool _stageDrawnThisFrame;
280 RenderStageList _preRenderList;
281 RenderStageList _postRenderList;
282
283 Cameras _dependentCameras;
284
285 // viewport x,y,width,height.
286 osg::ref_ptr<osg::Viewport> _viewport;
287 osg::ref_ptr<const osg::RefMatrix> _initialViewMatrix;
288
289 GLenum _drawBuffer;
290 bool _drawBufferApplyMask;
291 GLenum _readBuffer;
292 bool _readBufferApplyMask;
293 GLbitfield _clearMask;
294 osg::ref_ptr<osg::ColorMask> _colorMask;
295 osg::Vec4 _clearColor;
296 osg::Vec4 _clearAccum;
297 double _clearDepth;
298 int _clearStencil;
299
300 bool _cameraRequiresSetUp;
301 unsigned int _cameraAttachmentMapModifiedCount;
302 osg::observer_ptr<osg::Camera> _camera;
303
304 osg::ref_ptr<osg::Texture> _texture;
305 unsigned int _level;
306 unsigned int _face;
307
308 osg::ref_ptr<osg::Image> _image;
309 GLenum _imageReadPixelFormat;
310 GLenum _imageReadPixelDataType;
311
312 std::map< osg::Camera::BufferComponent, Attachment> _bufferAttachmentMap;
313
314 osg::ref_ptr<osg::FrameBufferObject> _fbo;
315 osg::ref_ptr<osg::FrameBufferObject> _resolveFbo;
316 osg::ref_ptr<osg::GraphicsContext> _graphicsContext;
317 bool _disableFboAfterRender;
318
319 mutable osg::Matrix _inheritedPositionalStateContainerMatrix;
320 mutable osg::ref_ptr<PositionalStateContainer> _inheritedPositionalStateContainer;
321 mutable osg::ref_ptr<PositionalStateContainer> _renderStageLighting;
322
323
324};
325
326}
327
328#endif
329