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 <Inventor/elements/SoGLShaderProgramElement.h>
34 
35 #include <cassert>
36 
37 #include <Inventor/elements/SoGLCacheContextElement.h>
38 #include "SoGLShaderProgram.h"
39 
40 // *************************************************************************
41 
42 SO_ELEMENT_SOURCE(SoGLShaderProgramElement);
43 
44 // *************************************************************************
45 
46 void
initClass(void)47 SoGLShaderProgramElement::initClass(void)
48 {
49   SO_ELEMENT_INIT_CLASS(SoGLShaderProgramElement, inherited);
50 }
51 
~SoGLShaderProgramElement()52 SoGLShaderProgramElement::~SoGLShaderProgramElement()
53 {
54   this->shaderProgram = NULL;
55 }
56 
57 void
init(SoState * state)58 SoGLShaderProgramElement::init(SoState *state)
59 {
60   inherited::init(state);
61   this->shaderProgram = NULL;
62   this->enabled = FALSE;
63 }
64 
65 void
enable(SoState * const state,const SbBool onoff)66 SoGLShaderProgramElement::enable(SoState * const state, const SbBool onoff)
67 {
68   SoGLShaderProgramElement* element =
69     (SoGLShaderProgramElement*) SoElement::getElement(state,classStackIndex);
70   element->enabled = onoff;
71   element->objectids.truncate(0);
72 
73   if (element->shaderProgram) {
74     if (onoff) {
75       if (!element->shaderProgram->isEnabled()) element->shaderProgram->enable(state);
76     }
77     else {
78       if (element->shaderProgram->isEnabled()) element->shaderProgram->disable(state);
79     }
80     element->shaderProgram->getShaderObjectIds(element->objectids);
81   }
82 }
83 
84 void
set(SoState * const state,SoNode * const node,SoGLShaderProgram * program)85 SoGLShaderProgramElement::set(SoState* const state, SoNode *const node,
86                               SoGLShaderProgram* program)
87 {
88   SoGLShaderProgramElement* element =
89     (SoGLShaderProgramElement*)inherited::getElement(state,classStackIndex, node);
90 
91   if (program != element->shaderProgram) {
92     if (element->shaderProgram) element->shaderProgram->disable(state);
93   }
94   element->shaderProgram = program;
95   element->enabled = FALSE;
96   element->objectids.truncate(0);
97   if (program) program->getShaderObjectIds(element->objectids);
98   // don't enable new program here. The node will call enable()
99   // after setting up all the objects
100 }
101 
102 SoGLShaderProgram *
get(SoState * state)103 SoGLShaderProgramElement::get(SoState *state)
104 {
105   const SoElement *element = getConstElement(state, classStackIndex);
106   assert(element);
107   return ((const SoGLShaderProgramElement *)element)->shaderProgram;
108 }
109 
110 void
push(SoState * state)111 SoGLShaderProgramElement::push(SoState * state)
112 {
113   SoGLShaderProgramElement * prev = (SoGLShaderProgramElement *) getNextInStack();
114   assert(prev);
115   this->shaderProgram = prev->shaderProgram;
116   this->enabled = prev->enabled;
117   this->nodeId = prev->nodeId;
118   this->objectids = prev->objectids;
119   // capture previous element since we might or might not change the
120   // GL state in set/pop
121   prev->capture(state);
122 }
123 
124 void
pop(SoState * state,const SoElement * prevTopElement)125 SoGLShaderProgramElement::pop(SoState * state, const SoElement * prevTopElement)
126 {
127   SoGLShaderProgramElement * elem = (SoGLShaderProgramElement *)prevTopElement;
128   if (this->shaderProgram != elem->shaderProgram) {
129     if (elem->shaderProgram) {
130       elem->shaderProgram->disable(state);
131       elem->enabled = FALSE;
132     }
133     if (this->shaderProgram) {
134       if (this->enabled) this->shaderProgram->enable(state);
135     }
136   }
137   else if (this->shaderProgram) {
138     if (this->enabled != elem->enabled) {
139       if (this->enabled) this->shaderProgram->enable(state);
140       else this->shaderProgram->disable(state);
141     }
142   }
143   elem->shaderProgram = NULL;
144 }
145 
146 
147 SbBool
matches(const SoElement * element) const148 SoGLShaderProgramElement::matches(const SoElement * element) const
149 {
150   SoGLShaderProgramElement * elem = (SoGLShaderProgramElement*) element;
151   return (this->enabled == elem->enabled) && (this->objectids == elem->objectids);
152 }
153 
154 SoElement *
copyMatchInfo(void) const155 SoGLShaderProgramElement::copyMatchInfo(void) const
156 {
157   SoGLShaderProgramElement * elem =
158     (SoGLShaderProgramElement*) inherited::copyMatchInfo();
159 
160   elem->enabled = this->enabled;
161   elem->objectids = this->objectids;
162   return elem;
163 }
164