/src/ogre/Components/RTShaderSystem/src/OgreShaderProgramWriter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | | of this software and associated documentation files (the "Software"), to deal |
10 | | in the Software without restriction, including without limitation the rights |
11 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12 | | copies of the Software, and to permit persons to whom the Software is |
13 | | furnished to do so, subject to the following conditions: |
14 | | |
15 | | The above copyright notice and this permission notice shall be included in |
16 | | all copies or substantial portions of the Software. |
17 | | |
18 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24 | | THE SOFTWARE. |
25 | | ----------------------------------------------------------------------------- |
26 | | */ |
27 | | #include "OgreShaderPrecompiledHeaders.h" |
28 | | |
29 | | namespace Ogre { |
30 | | namespace RTShader { |
31 | | |
32 | | //----------------------------------------------------------------------- |
33 | | void ProgramWriter::writeProgramTitle(std::ostream& os, Program* program) |
34 | 0 | { |
35 | 0 | os << "//-----------------------------------------------------------------------------" << std::endl; |
36 | 0 | os << "// Program Type: " << to_string(program->getType()) << std::endl; |
37 | 0 | os << "// Language: " << getTargetLanguage() << std::endl; |
38 | 0 | os << "// Created by Ogre RT Shader Generator. All rights reserved." << std::endl; |
39 | 0 | os << "//-----------------------------------------------------------------------------" << std::endl; |
40 | 0 | } |
41 | | |
42 | | //----------------------------------------------------------------------- |
43 | | void ProgramWriter::writeUniformParametersTitle(std::ostream& os, Program* program) |
44 | 0 | { |
45 | 0 | os << "//-----------------------------------------------------------------------------" << std::endl; |
46 | 0 | os << "// GLOBAL PARAMETERS" << std::endl; |
47 | 0 | os << "//-----------------------------------------------------------------------------" << std::endl; |
48 | 0 | } |
49 | | //----------------------------------------------------------------------- |
50 | | void ProgramWriter::writeFunctionTitle(std::ostream& os, Function* function) |
51 | 0 | { |
52 | 0 | os << "//-----------------------------------------------------------------------------" << std::endl; |
53 | 0 | os << "// MAIN" << std::endl; |
54 | 0 | os << "//-----------------------------------------------------------------------------" << std::endl; |
55 | 0 | } |
56 | | |
57 | | ProgramWriter::ProgramWriter() |
58 | 0 | { |
59 | 0 | mParamSemanticMap[Parameter::SPS_POSITION] = "POSITION"; |
60 | 0 | mParamSemanticMap[Parameter::SPS_BLEND_WEIGHTS] = "BLENDWEIGHT"; |
61 | 0 | mParamSemanticMap[Parameter::SPS_BLEND_INDICES] = "BLENDINDICES"; |
62 | 0 | mParamSemanticMap[Parameter::SPS_NORMAL] = "NORMAL"; |
63 | 0 | mParamSemanticMap[Parameter::SPS_COLOR] = "COLOR"; |
64 | 0 | mParamSemanticMap[Parameter::SPS_TEXTURE_COORDINATES] = "TEXCOORD"; |
65 | 0 | mParamSemanticMap[Parameter::SPS_BINORMAL] = "BINORMAL"; |
66 | 0 | mParamSemanticMap[Parameter::SPS_TANGENT] = "TANGENT"; |
67 | 0 | } |
68 | | |
69 | 0 | ProgramWriter::~ProgramWriter() {} |
70 | | |
71 | | void ProgramWriter::writeParameter(std::ostream& os, const ParameterPtr& parameter) |
72 | 0 | { |
73 | 0 | if (!parameter->getStructType().empty()) |
74 | 0 | { |
75 | 0 | os << parameter->getStructType() << '\t' << parameter->getName(); |
76 | 0 | return; |
77 | 0 | } |
78 | | |
79 | 0 | os << mGpuConstTypeMap[parameter->getType()] << '\t' << parameter->getName(); |
80 | 0 | if (parameter->isArray()) |
81 | 0 | os << '[' << parameter->getSize() << ']'; |
82 | 0 | } |
83 | | |
84 | | void ProgramWriter::writeSamplerParameter(std::ostream& os, const UniformParameterPtr& parameter) |
85 | 0 | { |
86 | 0 | if (parameter->getType() == GCT_SAMPLER_EXTERNAL_OES) |
87 | 0 | { |
88 | 0 | os << "uniform\t"; |
89 | 0 | writeParameter(os, parameter); |
90 | 0 | return; |
91 | 0 | } |
92 | | |
93 | 0 | switch(parameter->getType()) |
94 | 0 | { |
95 | 0 | case GCT_SAMPLER1D: |
96 | 0 | os << "SAMPLER1D("; |
97 | 0 | break; |
98 | 0 | case GCT_SAMPLER2D: |
99 | 0 | os << "SAMPLER2D("; |
100 | 0 | break; |
101 | 0 | case GCT_SAMPLER3D: |
102 | 0 | os << "SAMPLER3D("; |
103 | 0 | break; |
104 | 0 | case GCT_SAMPLERCUBE: |
105 | 0 | os << "SAMPLERCUBE("; |
106 | 0 | break; |
107 | 0 | case GCT_SAMPLER2DSHADOW: |
108 | 0 | os << "SAMPLER2DSHADOW("; |
109 | 0 | break; |
110 | 0 | case GCT_SAMPLER2DARRAY: |
111 | 0 | os << "SAMPLER2DARRAY("; |
112 | 0 | break; |
113 | 0 | case GCT_SAMPLER2DARRAYSHADOW: |
114 | 0 | os << "SAMPLER2DARRAYSHADOW("; |
115 | 0 | break; |
116 | 0 | case GCT_SAMPLERCUBESHADOW: |
117 | 0 | os << "SAMPLERCUBESHADOW("; |
118 | 0 | break; |
119 | 0 | default: |
120 | 0 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "unsupported sampler type"); |
121 | 0 | } |
122 | 0 | os << parameter->getName() << ", " << parameter->getIndex() << ")"; |
123 | 0 | } |
124 | | |
125 | | void ProgramWriter::writeParameterSemantic(std::ostream& os, const ParameterPtr& parameter) |
126 | 0 | { |
127 | 0 | OgreAssertDbg(parameter->getSemantic() != Parameter::SPS_UNKNOWN, "invalid semantic"); |
128 | 0 | os << mParamSemanticMap[parameter->getSemantic()]; |
129 | |
|
130 | 0 | if (parameter->getSemantic() == Parameter::SPS_TEXTURE_COORDINATES || |
131 | 0 | (parameter->getSemantic() == Parameter::SPS_COLOR && parameter->getIndex() > 0)) |
132 | 0 | { |
133 | 0 | os << parameter->getIndex(); |
134 | 0 | } |
135 | 0 | } |
136 | | |
137 | | void ProgramWriter::redirectGlobalWrites(std::ostream& os, FunctionAtom* func, const ShaderParameterList& inputs, |
138 | | const UniformParameterList& uniforms) |
139 | 0 | { |
140 | 0 | for (auto& operand : func->getOperandList()) |
141 | 0 | { |
142 | 0 | auto opSemantic = operand.getSemantic(); |
143 | |
|
144 | 0 | if (opSemantic != Operand::OPS_OUT && opSemantic != Operand::OPS_INOUT) |
145 | 0 | continue; |
146 | | |
147 | 0 | const ParameterPtr& param = operand.getParameter(); |
148 | | |
149 | | // Check if we write to an input variable because they are only readable |
150 | | // Well, actually "attribute" were writable in GLSL < 120, but we dont care here |
151 | 0 | bool doLocalRename = std::find(inputs.begin(), inputs.end(), param) != inputs.end(); |
152 | | |
153 | | // If its not a varying param check if a uniform is written |
154 | 0 | if (!doLocalRename) |
155 | 0 | { |
156 | 0 | doLocalRename = std::find(uniforms.begin(), uniforms.end(), param) != uniforms.end(); |
157 | 0 | } |
158 | | |
159 | | // now we check if we already declared a redirector var |
160 | 0 | if (doLocalRename && mLocalRenames.find(param->getName()) == mLocalRenames.end()) |
161 | 0 | { |
162 | | // Declare the copy variable and assign the original |
163 | 0 | String newVar = "local_" + param->getName(); |
164 | 0 | os << "\t" << mGpuConstTypeMap[param->getType()] << " " << newVar << " = " << param->getName() << ";" |
165 | 0 | << std::endl; |
166 | | |
167 | | // From now on we replace it automatic |
168 | 0 | param->_rename(newVar, true); |
169 | 0 | mLocalRenames.insert(newVar); |
170 | 0 | } |
171 | 0 | } |
172 | 0 | } |
173 | | |
174 | | //----------------------------------------------------------------------- |
175 | | void ProgramWriter::writeProgramDependencies(std::ostream& os, Program* program) |
176 | 0 | { |
177 | 0 | os << "//-----------------------------------------------------------------------------" << std::endl; |
178 | 0 | os << "// PROGRAM DEPENDENCIES" << std::endl; |
179 | 0 | os << "//-----------------------------------------------------------------------------" << std::endl; |
180 | 0 | os << "#include <OgreUnifiedShader.h>" << std::endl; |
181 | |
|
182 | 0 | for (unsigned int i=0; i < program->getDependencyCount(); ++i) |
183 | 0 | { |
184 | 0 | os << "#include \"" << program->getDependency(i) << ".glsl\"" << std::endl; |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | | } |
189 | | } |