forked from Kitware/VTK
/
vtkOpenGLShaderProperty.cxx
223 lines (193 loc) · 7.09 KB
/
vtkOpenGLShaderProperty.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*=========================================================================
Program: Visualization Toolkit
Module: vtkShaderProperty.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkOpenGLShaderProperty.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLUniforms.h"
#include <algorithm>
vtkStandardNewMacro(vtkOpenGLShaderProperty);
vtkOpenGLShaderProperty::vtkOpenGLShaderProperty() = default;
vtkOpenGLShaderProperty::~vtkOpenGLShaderProperty() = default;
void vtkOpenGLShaderProperty::DeepCopy(vtkOpenGLShaderProperty* p)
{
vtkShaderProperty::DeepCopy(p);
this->UserShaderReplacements = p->UserShaderReplacements;
}
void vtkOpenGLShaderProperty::AddVertexShaderReplacement(const std::string& originalValue,
bool replaceFirst, // do this replacement before the default
const std::string& replacementValue, bool replaceAll)
{
this->AddShaderReplacement(
vtkShader::Vertex, originalValue, replaceFirst, replacementValue, replaceAll);
}
void vtkOpenGLShaderProperty::AddFragmentShaderReplacement(const std::string& originalValue,
bool replaceFirst, // do this replacement before the default
const std::string& replacementValue, bool replaceAll)
{
this->AddShaderReplacement(
vtkShader::Fragment, originalValue, replaceFirst, replacementValue, replaceAll);
}
void vtkOpenGLShaderProperty::AddGeometryShaderReplacement(const std::string& originalValue,
bool replaceFirst, // do this replacement before the default
const std::string& replacementValue, bool replaceAll)
{
this->AddShaderReplacement(
vtkShader::Geometry, originalValue, replaceFirst, replacementValue, replaceAll);
}
int vtkOpenGLShaderProperty::GetNumberOfShaderReplacements()
{
return static_cast<int>(UserShaderReplacements.size());
}
std::string vtkOpenGLShaderProperty::GetNthShaderReplacementTypeAsString(vtkIdType index)
{
if (index >= static_cast<vtkIdType>(this->UserShaderReplacements.size()))
{
vtkErrorMacro(<< "Trying to access out of bound shader replacement.");
return std::string("");
}
ReplacementMap::iterator it = this->UserShaderReplacements.begin();
std::advance(it, index);
if (it->first.ShaderType == vtkShader::Vertex)
{
return std::string("Vertex");
}
else if (it->first.ShaderType == vtkShader::Fragment)
{
return std::string("Fragment");
}
else if (it->first.ShaderType == vtkShader::Geometry)
{
return std::string("Geometry");
}
return std::string("Unknown");
}
void vtkOpenGLShaderProperty::GetNthShaderReplacement(vtkIdType index, std::string& name,
bool& replaceFirst, std::string& replacementValue, bool& replaceAll)
{
if (index >= static_cast<vtkIdType>(this->UserShaderReplacements.size()))
{
vtkErrorMacro(<< "Trying to access out of bound shader replacement.");
}
ReplacementMap::iterator it = this->UserShaderReplacements.begin();
std::advance(it, index);
name = it->first.OriginalValue;
replaceFirst = it->first.ReplaceFirst;
replacementValue = it->second.Replacement;
replaceAll = it->second.ReplaceAll;
}
void vtkOpenGLShaderProperty::ClearVertexShaderReplacement(
const std::string& originalValue, bool replaceFirst)
{
this->ClearShaderReplacement(vtkShader::Vertex, originalValue, replaceFirst);
}
void vtkOpenGLShaderProperty::ClearFragmentShaderReplacement(
const std::string& originalValue, bool replaceFirst)
{
this->ClearShaderReplacement(vtkShader::Fragment, originalValue, replaceFirst);
}
void vtkOpenGLShaderProperty::ClearGeometryShaderReplacement(
const std::string& originalValue, bool replaceFirst)
{
this->ClearShaderReplacement(vtkShader::Geometry, originalValue, replaceFirst);
}
void vtkOpenGLShaderProperty::ClearAllVertexShaderReplacements()
{
this->ClearAllShaderReplacements(vtkShader::Vertex);
}
void vtkOpenGLShaderProperty::ClearAllFragmentShaderReplacements()
{
this->ClearAllShaderReplacements(vtkShader::Fragment);
}
void vtkOpenGLShaderProperty::ClearAllGeometryShaderReplacements()
{
this->ClearAllShaderReplacements(vtkShader::Geometry);
}
//------------------------------------------------------------------------------
void vtkOpenGLShaderProperty::ClearAllShaderReplacements()
{
this->SetVertexShaderCode(nullptr);
this->SetFragmentShaderCode(nullptr);
this->SetGeometryShaderCode(nullptr);
this->UserShaderReplacements.clear();
this->Modified();
}
//------------------------------------------------------------------------------
void vtkOpenGLShaderProperty::AddShaderReplacement(
vtkShader::Type shaderType, // vertex, fragment, etc
const std::string& originalValue,
bool replaceFirst, // do this replacement before the default
const std::string& replacementValue, bool replaceAll)
{
vtkShader::ReplacementSpec spec;
spec.ShaderType = shaderType;
spec.OriginalValue = originalValue;
spec.ReplaceFirst = replaceFirst;
vtkShader::ReplacementValue values;
values.Replacement = replacementValue;
values.ReplaceAll = replaceAll;
this->UserShaderReplacements[spec] = values;
this->Modified();
}
//------------------------------------------------------------------------------
void vtkOpenGLShaderProperty::ClearShaderReplacement(
vtkShader::Type shaderType, // vertex, fragment, etc
const std::string& originalValue, bool replaceFirst)
{
vtkShader::ReplacementSpec spec;
spec.ShaderType = shaderType;
spec.OriginalValue = originalValue;
spec.ReplaceFirst = replaceFirst;
typedef std::map<vtkShader::ReplacementSpec, vtkShader::ReplacementValue>::iterator RIter;
RIter found = this->UserShaderReplacements.find(spec);
if (found != this->UserShaderReplacements.end())
{
this->UserShaderReplacements.erase(found);
this->Modified();
}
}
//------------------------------------------------------------------------------
void vtkOpenGLShaderProperty::ClearAllShaderReplacements(vtkShader::Type shaderType)
{
bool modified = false;
// First clear all shader code
if ((shaderType == vtkShader::Vertex) && this->VertexShaderCode)
{
this->SetVertexShaderCode(nullptr);
modified = true;
}
else if ((shaderType == vtkShader::Fragment) && this->FragmentShaderCode)
{
this->SetFragmentShaderCode(nullptr);
modified = true;
}
// Now clear custom tag replacements
std::map<vtkShader::ReplacementSpec, vtkShader::ReplacementValue>::iterator rIter;
for (rIter = this->UserShaderReplacements.begin(); rIter != this->UserShaderReplacements.end();)
{
if (rIter->first.ShaderType == shaderType)
{
this->UserShaderReplacements.erase(rIter++);
modified = true;
}
else
{
++rIter;
}
}
if (modified)
{
this->Modified();
}
}
//------------------------------------------------------------------------------
void vtkOpenGLShaderProperty::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}