Skip to content

Commit 91c0fd2

Browse files
committed
rendering: add retained render-layer semantics
1 parent 2fc9d43 commit 91c0fd2

7 files changed

Lines changed: 460 additions & 9 deletions

File tree

src/elements/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ set(COIN_ELEMENTS_FILES
5858
SoProfileCoordinateElement.cpp
5959
SoProfileElement.cpp
6060
SoProjectionMatrixElement.cpp
61+
SoRenderPlacementElement.cpp
6162
SoReplacedElement.cpp
6263
SoShapeHintsElement.cpp
6364
SoShapeStyleElement.cpp
@@ -92,6 +93,7 @@ set(COIN_ELEMENTS_INTERNAL_FILES
9293
SoTextureScalePolicyElement.cpp
9394
SoTextureScaleQualityElement.h
9495
SoTextureScaleQualityElement.cpp
96+
SoRenderPlacementElement.h
9597
SoVertexAttributeData.h
9698
)
9799

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/**************************************************************************\
2+
* Copyright (c) Kongsberg Oil & Gas Technologies AS
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
*
12+
* Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* Neither the name of the copyright holder nor the names of its
17+
* contributors may be used to endorse or promote products derived from
18+
* this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
\**************************************************************************/
32+
33+
#include "SoRenderPlacementElement.h"
34+
35+
#include <cassert>
36+
37+
SO_ELEMENT_SOURCE(SoRenderPlacementElement);
38+
39+
void
40+
SoRenderPlacementElement::initClass(void)
41+
{
42+
SO_ELEMENT_INIT_CLASS(SoRenderPlacementElement, inherited);
43+
}
44+
45+
SoRenderPlacementElement::~SoRenderPlacementElement()
46+
{
47+
}
48+
49+
void
50+
SoRenderPlacementElement::init(SoState * state)
51+
{
52+
inherited::init(state);
53+
this->layer = MAIN;
54+
this->viewportOverride = FALSE;
55+
this->viewportX = 0;
56+
this->viewportY = 0;
57+
this->viewportWidth = 0;
58+
this->viewportHeight = 0;
59+
this->clearDepth = FALSE;
60+
this->commandMatricesOverride = FALSE;
61+
}
62+
63+
void
64+
SoRenderPlacementElement::push(SoState * state)
65+
{
66+
inherited::push(state);
67+
const SoRenderPlacementElement * prev =
68+
static_cast<const SoRenderPlacementElement *>(this->getNextInStack());
69+
this->layer = prev->layer;
70+
this->viewportOverride = prev->viewportOverride;
71+
this->viewportX = prev->viewportX;
72+
this->viewportY = prev->viewportY;
73+
this->viewportWidth = prev->viewportWidth;
74+
this->viewportHeight = prev->viewportHeight;
75+
this->clearDepth = prev->clearDepth;
76+
this->commandMatricesOverride = prev->commandMatricesOverride;
77+
}
78+
79+
void
80+
SoRenderPlacementElement::pop(SoState * state,
81+
const SoElement * prevTopElement)
82+
{
83+
inherited::pop(state, prevTopElement);
84+
85+
// A depth-clear request is consumed while traversing a child. Propagate that
86+
// consumption to the parent state so sibling separators do not turn one
87+
// render-layer clear into one clear per emitted command.
88+
const SoRenderPlacementElement * previous =
89+
static_cast<const SoRenderPlacementElement *>(prevTopElement);
90+
if (previous && !previous->clearDepth) {
91+
this->clearDepth = FALSE;
92+
}
93+
}
94+
95+
SbBool
96+
SoRenderPlacementElement::matches(const SoElement * /* element */) const
97+
{
98+
assert(0 && "should never be called.");
99+
return TRUE;
100+
}
101+
102+
SoElement *
103+
SoRenderPlacementElement::copyMatchInfo(void) const
104+
{
105+
assert(0 && "should never be called.");
106+
return NULL;
107+
}
108+
109+
void
110+
SoRenderPlacementElement::setLayer(SoState * state, Layer layer)
111+
{
112+
SoRenderPlacementElement * elem =
113+
static_cast<SoRenderPlacementElement *>
114+
(SoElement::getElement(state, classStackIndex));
115+
if (elem) {
116+
elem->layer = layer;
117+
}
118+
}
119+
120+
void
121+
SoRenderPlacementElement::setViewport(SoState * state,
122+
int x, int y, int width, int height)
123+
{
124+
SoRenderPlacementElement * elem =
125+
static_cast<SoRenderPlacementElement *>
126+
(SoElement::getElement(state, classStackIndex));
127+
if (elem) {
128+
elem->viewportOverride = TRUE;
129+
elem->viewportX = x;
130+
elem->viewportY = y;
131+
elem->viewportWidth = width;
132+
elem->viewportHeight = height;
133+
}
134+
}
135+
136+
void
137+
SoRenderPlacementElement::setClearDepth(SoState * state, SbBool clearDepth)
138+
{
139+
SoRenderPlacementElement * elem =
140+
static_cast<SoRenderPlacementElement *>
141+
(SoElement::getElement(state, classStackIndex));
142+
if (elem) {
143+
elem->clearDepth = clearDepth;
144+
}
145+
}
146+
147+
void
148+
SoRenderPlacementElement::setCommandMatricesOverride(SoState * state,
149+
SbBool enabled)
150+
{
151+
SoRenderPlacementElement * elem =
152+
static_cast<SoRenderPlacementElement *>(
153+
SoElement::getElement(state, classStackIndex));
154+
if (elem) {
155+
elem->commandMatricesOverride = enabled;
156+
}
157+
}
158+
159+
SoRenderPlacementElement::Layer
160+
SoRenderPlacementElement::getLayer(SoState * state)
161+
{
162+
const SoRenderPlacementElement * elem =
163+
static_cast<const SoRenderPlacementElement *>
164+
(SoElement::getConstElement(state, classStackIndex));
165+
return elem->layer;
166+
}
167+
168+
SbBool
169+
SoRenderPlacementElement::getViewport(SoState * state,
170+
int & x, int & y,
171+
int & width, int & height)
172+
{
173+
const SoRenderPlacementElement * elem =
174+
static_cast<const SoRenderPlacementElement *>
175+
(SoElement::getConstElement(state, classStackIndex));
176+
x = elem->viewportX;
177+
y = elem->viewportY;
178+
width = elem->viewportWidth;
179+
height = elem->viewportHeight;
180+
return elem->viewportOverride;
181+
}
182+
183+
SbBool
184+
SoRenderPlacementElement::consumeClearDepth(SoState * state)
185+
{
186+
SoRenderPlacementElement * elem =
187+
static_cast<SoRenderPlacementElement *>
188+
(SoElement::getElement(state, classStackIndex));
189+
if (!elem || !elem->clearDepth) return FALSE;
190+
elem->clearDepth = FALSE;
191+
return TRUE;
192+
}
193+
194+
SbBool
195+
SoRenderPlacementElement::getCommandMatricesOverride(SoState * state)
196+
{
197+
const SoRenderPlacementElement * elem =
198+
static_cast<const SoRenderPlacementElement *>(
199+
SoElement::getConstElement(state, classStackIndex));
200+
return elem ? elem->commandMatricesOverride : FALSE;
201+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#ifndef COIN_SORENDERPLACEMENTELEMENT_H
2+
#define COIN_SORENDERPLACEMENTELEMENT_H
3+
4+
/**************************************************************************\
5+
* Copyright (c) Kongsberg Oil & Gas Technologies AS
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are
10+
* met:
11+
*
12+
* Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
*
15+
* Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in the
17+
* documentation and/or other materials provided with the distribution.
18+
*
19+
* Neither the name of the copyright holder nor the names of its
20+
* contributors may be used to endorse or promote products derived from
21+
* this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
\**************************************************************************/
35+
36+
#ifndef COIN_INTERNAL
37+
#error this is a private header file
38+
#endif // !COIN_INTERNAL
39+
40+
#include <Inventor/elements/SoSubElement.h>
41+
42+
/*!
43+
\class SoRenderPlacementElement
44+
\brief Private traversal state for retained-render placement directives.
45+
46+
This element is enabled by SoIRRenderAction and carries action-local
47+
placement information through the normal Inventor state stack. It is not
48+
OpenGL state and does not issue backend commands itself. The default state
49+
is MAIN, with no viewport override and no depth-clear request. Values are
50+
inherited by push() and restored by the usual element-stack mechanism.
51+
52+
Viewport and depth-clear values are read while the action converts traversal
53+
state into SoRenderCommand state. The AfterMain render stage is controlled
54+
separately by SoIRRenderStageScope; it is not represented by Layer.
55+
*/
56+
class SoRenderPlacementElement : public SoElement {
57+
typedef SoElement inherited;
58+
59+
SO_ELEMENT_HEADER(SoRenderPlacementElement);
60+
61+
public:
62+
//! Logical placement hint. MAIN is the default; FOREGROUND is reserved for
63+
//! traversal code that explicitly consumes this element.
64+
enum Layer {
65+
MAIN = 0,
66+
FOREGROUND = 1
67+
};
68+
69+
static void initClass(void);
70+
71+
protected:
72+
virtual ~SoRenderPlacementElement();
73+
74+
public:
75+
virtual void init(SoState * state) override;
76+
virtual void push(SoState * state) override;
77+
virtual void pop(SoState * state, const SoElement * prevTopElement) override;
78+
79+
virtual SbBool matches(const SoElement * element) const override;
80+
virtual SoElement * copyMatchInfo(void) const override;
81+
82+
//! Set the logical placement hint for subsequent traversal.
83+
static void setLayer(SoState * state, Layer layer);
84+
85+
//! Request a viewport override in framebuffer pixel coordinates.
86+
static void setViewport(SoState * state,
87+
int x, int y, int width, int height);
88+
89+
//! Request a one-shot depth clear at this placement.
90+
static void setClearDepth(SoState * state, SbBool clearDepth);
91+
92+
//! Select command-local view/projection matrices for retained rendering.
93+
static void setCommandMatricesOverride(SoState * state, SbBool enabled);
94+
95+
//! Return the current logical placement hint.
96+
static Layer getLayer(SoState * state);
97+
98+
//! Return whether a viewport override is active and, if so, its rectangle.
99+
static SbBool getViewport(SoState * state,
100+
int & x, int & y, int & width, int & height);
101+
102+
//! Consume and return the current one-shot depth-clear request.
103+
static SbBool consumeClearDepth(SoState * state);
104+
105+
//! Return whether command-local matrices are selected.
106+
static SbBool getCommandMatricesOverride(SoState * state);
107+
108+
private:
109+
Layer layer;
110+
SbBool viewportOverride;
111+
int viewportX;
112+
int viewportY;
113+
int viewportWidth;
114+
int viewportHeight;
115+
SbBool clearDepth;
116+
SbBool commandMatricesOverride;
117+
};
118+
119+
#endif // !COIN_SORENDERPLACEMENTELEMENT_H

src/elements/all-elements-cpp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
#include "SoProfileCoordinateElement.cpp"
9090
#include "SoProfileElement.cpp"
9191
#include "SoProjectionMatrixElement.cpp"
92+
#include "SoRenderPlacementElement.cpp"
9293
#include "SoReplacedElement.cpp"
9394
#include "SoShapeHintsElement.cpp"
9495
#include "SoShapeStyleElement.cpp"

0 commit comments

Comments
 (0)