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
|
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org
Copyright (c) 2000-2014 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#include "OgreShaderPrecompiledHeaders.h"
namespace Ogre {
namespace RTShader {
//-----------------------------------------------------------------------
void ProgramWriter::writeProgramTitle(std::ostream& os, Program* program)
{
os << "//-----------------------------------------------------------------------------" << std::endl;
os << "// Program Type: " << to_string(program->getType()) << std::endl;
os << "// Language: " << getTargetLanguage() << std::endl;
os << "// Created by Ogre RT Shader Generator. All rights reserved." << std::endl;
os << "//-----------------------------------------------------------------------------" << std::endl;
}
//-----------------------------------------------------------------------
void ProgramWriter::writeUniformParametersTitle(std::ostream& os, Program* program)
{
os << "//-----------------------------------------------------------------------------" << std::endl;
os << "// GLOBAL PARAMETERS" << std::endl;
os << "//-----------------------------------------------------------------------------" << std::endl;
}
//-----------------------------------------------------------------------
void ProgramWriter::writeFunctionTitle(std::ostream& os, Function* function)
{
os << "//-----------------------------------------------------------------------------" << std::endl;
os << "// MAIN" << std::endl;
os << "//-----------------------------------------------------------------------------" << std::endl;
}
ProgramWriter::ProgramWriter()
{
mParamSemanticMap[Parameter::SPS_POSITION] = "POSITION";
mParamSemanticMap[Parameter::SPS_BLEND_WEIGHTS] = "BLENDWEIGHT";
mParamSemanticMap[Parameter::SPS_BLEND_INDICES] = "BLENDINDICES";
mParamSemanticMap[Parameter::SPS_NORMAL] = "NORMAL";
mParamSemanticMap[Parameter::SPS_COLOR] = "COLOR";
mParamSemanticMap[Parameter::SPS_TEXTURE_COORDINATES] = "TEXCOORD";
mParamSemanticMap[Parameter::SPS_BINORMAL] = "BINORMAL";
mParamSemanticMap[Parameter::SPS_TANGENT] = "TANGENT";
}
ProgramWriter::~ProgramWriter() {}
void ProgramWriter::writeParameter(std::ostream& os, const ParameterPtr& parameter)
{
if (!parameter->getStructType().empty())
{
os << parameter->getStructType() << '\t' << parameter->getName();
return;
}
os << mGpuConstTypeMap[parameter->getType()] << '\t' << parameter->getName();
if (parameter->isArray())
os << '[' << parameter->getSize() << ']';
}
void ProgramWriter::writeSamplerParameter(std::ostream& os, const UniformParameterPtr& parameter)
{
if (parameter->getType() == GCT_SAMPLER_EXTERNAL_OES)
{
os << "uniform\t";
writeParameter(os, parameter);
return;
}
switch(parameter->getType())
{
case GCT_SAMPLER1D:
os << "SAMPLER1D(";
break;
case GCT_SAMPLER2D:
os << "SAMPLER2D(";
break;
case GCT_SAMPLER3D:
os << "SAMPLER3D(";
break;
case GCT_SAMPLERCUBE:
os << "SAMPLERCUBE(";
break;
case GCT_SAMPLER2DSHADOW:
os << "SAMPLER2DSHADOW(";
break;
case GCT_SAMPLER2DARRAY:
os << "SAMPLER2DARRAY(";
break;
case GCT_SAMPLER2DARRAYSHADOW:
os << "SAMPLER2DARRAYSHADOW(";
break;
case GCT_SAMPLERCUBESHADOW:
os << "SAMPLERCUBESHADOW(";
break;
default:
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "unsupported sampler type");
}
os << parameter->getName() << ", " << parameter->getIndex() << ")";
}
void ProgramWriter::writeParameterSemantic(std::ostream& os, const ParameterPtr& parameter)
{
OgreAssertDbg(parameter->getSemantic() != Parameter::SPS_UNKNOWN, "invalid semantic");
os << mParamSemanticMap[parameter->getSemantic()];
if (parameter->getSemantic() == Parameter::SPS_TEXTURE_COORDINATES ||
(parameter->getSemantic() == Parameter::SPS_COLOR && parameter->getIndex() > 0))
{
os << parameter->getIndex();
}
}
void ProgramWriter::redirectGlobalWrites(std::ostream& os, FunctionAtom* func, const ShaderParameterList& inputs,
const UniformParameterList& uniforms)
{
for (auto& operand : func->getOperandList())
{
auto opSemantic = operand.getSemantic();
if (opSemantic != Operand::OPS_OUT && opSemantic != Operand::OPS_INOUT)
continue;
const ParameterPtr& param = operand.getParameter();
// Check if we write to an input variable because they are only readable
// Well, actually "attribute" were writable in GLSL < 120, but we dont care here
bool doLocalRename = std::find(inputs.begin(), inputs.end(), param) != inputs.end();
// If its not a varying param check if a uniform is written
if (!doLocalRename)
{
doLocalRename = std::find(uniforms.begin(), uniforms.end(), param) != uniforms.end();
}
// now we check if we already declared a redirector var
if (doLocalRename && mLocalRenames.find(param->getName()) == mLocalRenames.end())
{
// Declare the copy variable and assign the original
String newVar = "local_" + param->getName();
os << "\t" << mGpuConstTypeMap[param->getType()] << " " << newVar << " = " << param->getName() << ";"
<< std::endl;
// From now on we replace it automatic
param->_rename(newVar, true);
mLocalRenames.insert(newVar);
}
}
}
//-----------------------------------------------------------------------
void ProgramWriter::writeProgramDependencies(std::ostream& os, Program* program)
{
os << "//-----------------------------------------------------------------------------" << std::endl;
os << "// PROGRAM DEPENDENCIES" << std::endl;
os << "//-----------------------------------------------------------------------------" << std::endl;
os << "#include <OgreUnifiedShader.h>" << std::endl;
for (unsigned int i=0; i < program->getDependencyCount(); ++i)
{
os << "#include \"" << program->getDependency(i) << ".glsl\"" << std::endl;
}
}
}
}
|