diff --git a/Engine/source/shaderGen/NODE/shaderGenNodes.cpp b/Engine/source/shaderGen/NODE/shaderGenNodes.cpp index 2ef78ab47..5ae6de566 100644 --- a/Engine/source/shaderGen/NODE/shaderGenNodes.cpp +++ b/Engine/source/shaderGen/NODE/shaderGenNodes.cpp @@ -509,7 +509,7 @@ void NormalMapFeature::processPix(Vector& componentList, const MultiLine* meta = new MultiLine; - // TEMP float3 for base decoded normal + // TEMP float4 for base decoded normal Var* tempNorm = new Var; tempNorm->setName(params->inputName + "_normTemp"); tempNorm->setType(GFXSCT_Float4); diff --git a/Engine/source/shaderGen/langElement.cpp b/Engine/source/shaderGen/langElement.cpp index e0e197c9b..7148e85f3 100644 --- a/Engine/source/shaderGen/langElement.cpp +++ b/Engine/source/shaderGen/langElement.cpp @@ -23,6 +23,7 @@ #include "core/strings/stringFunctions.h" #include "core/util/str.h" #include "gfx/gfxDevice.h" +#include "shaderGen/shaderOp.h" #include "langElement.h" //************************************************************************** @@ -132,6 +133,56 @@ GFXShaderConstType LangElement::stringToConstType(const char* name) return GFXSCT_Uknown; } +bool LangElement::resolveSourceType(LangElement* elem, Var*& outVar, const ShaderTypeInfo*& outInfo) +{ + outVar = nullptr; + outInfo = nullptr; + + // DIRECT VAR + if (Var* v = dynamic_cast(elem)) + { + outVar = v; + outInfo = getTypeInfo(stringToConstType((const char*)v->type)); + return outInfo != nullptr; + } + + // INDEX OP: arrVar[index] + if (IndexOp* idx = dynamic_cast(elem)) + { + Var* arr = dynamic_cast(idx->mInput[0]); + if (!arr) + return false; + + const ShaderTypeInfo* arrInfo = getTypeInfo(stringToConstType((const char*)arr->type)); + if (!arrInfo) + return false; + + // array element type = same as var type but no array dimension + outVar = arr; + outInfo = arrInfo; + return true; + } + + // CAST OP: cast var + if (CastOp* cast = dynamic_cast(elem)) + { + Var* castVar = dynamic_cast(cast->mInput[0]); + if (!castVar) + return false; + + const ShaderTypeInfo* castInfo = getTypeInfo(cast->mTargetType);// get the casts target type. + if (!castInfo) + return false; + + outVar = castVar; // we should probably return null as we should just write the castop langelement, not a var. + outInfo = castInfo; + return true; + } + + return false; +} + + //-------------------------------------------------------------------------- // Constructor //-------------------------------------------------------------------------- diff --git a/Engine/source/shaderGen/langElement.h b/Engine/source/shaderGen/langElement.h index 48c6afcb2..3105e8ce5 100644 --- a/Engine/source/shaderGen/langElement.h +++ b/Engine/source/shaderGen/langElement.h @@ -92,6 +92,7 @@ struct ShaderTypeInfo //************************************************************************** // Language element //************************************************************************** +struct Var; // forward declaration struct LangElement { static void buildTypeMaps(); @@ -99,7 +100,8 @@ struct LangElement static LangElement * find( const char *name ); static void deleteElements(); static const ShaderTypeInfo* getTypeInfo(GFXShaderConstType type); - + static bool resolveSourceType(LangElement* elem, Var*& outVar, const ShaderTypeInfo*& outInfo); + U8 name[32]; static const char* constTypeToString(GFXShaderConstType constType, bool sampler = false, bool matrix = false); diff --git a/Engine/source/shaderGen/shaderOp.cpp b/Engine/source/shaderGen/shaderOp.cpp index 8a514f21c..d8979b011 100644 --- a/Engine/source/shaderGen/shaderOp.cpp +++ b/Engine/source/shaderGen/shaderOp.cpp @@ -26,53 +26,6 @@ #include "shaderOp.h" -bool ShaderOp::resolveSourceType(LangElement* elem, Var*& outVar, const ShaderTypeInfo*& outInfo) -{ - outVar = nullptr; - outInfo = nullptr; - - // DIRECT VAR - if (Var* v = dynamic_cast(elem)) - { - outVar = v; - outInfo = getTypeInfo(stringToConstType((const char*)v->type)); - return outInfo != nullptr; - } - - // INDEX OP: arrVar[index] - if (IndexOp* idx = dynamic_cast(elem)) - { - Var* arr = dynamic_cast(idx->mInput[0]); - if (!arr) - return false; - - const ShaderTypeInfo* arrInfo = getTypeInfo(stringToConstType((const char*)arr->type)); - if (!arrInfo) - return false; - - // array element type = same as var type but no array dimension - outVar = arr; - outInfo = arrInfo; - return true; - } - - if (CastOp* cast = dynamic_cast(elem)) - { - Var* castVar = dynamic_cast(cast->mInput[0]); - if (!castVar) - return false; - - const ShaderTypeInfo* castInfo = getTypeInfo(cast->mTargetType);// get the casts target type. - if (!castInfo) - return false; - - outVar = castVar; - outInfo = castInfo; - } - - return false; -} - //************************************************************************** // Shader Operations //************************************************************************** @@ -396,32 +349,40 @@ void MatrixInitializeOp::print(Stream& stream) U32 count = 0; for (U32 elem = 0; elem < mInitialVals.size(); elem++) { - LangElement* writeOut = NULL; - Var* curVar = dynamic_cast(mInitialVals[elem]); - if (curVar) // is a var + LangElement* initElem = mInitialVals[elem]; + Var* initVar = nullptr; + const ShaderTypeInfo* initInfo = nullptr; + + if (!resolveSourceType(initElem, initVar, initInfo)) { - const ShaderTypeInfo* curInfo = getTypeInfo(stringToConstType((const char*)curVar->type)); - if (!curInfo) // no info, cant do it cleanly. - return; + return; + } - const U32 curSize = curInfo->cols; + if (dynamic_cast(initElem) || dynamic_cast(initElem)) // if we are a cast or index, write out. + { + count += initInfo->cols; + initElem->print(stream); + } + else if(dynamic_cast(initElem)) // we are a var (hopefully) + { + const U32 varSize = initInfo->cols; + const bool cast = cols != varSize; - // if we are an array - if (curVar->arraySize > 1) + if (initVar->arraySize > 1) { - for (U32 arr = 0; arr < curVar->arraySize; arr++) + for (U32 arr = 0; arr < initVar->arraySize; arr++) { - writeOut = new IndexOp(curVar, arr); - if (curSize != cols) + initElem = new IndexOp(initVar, arr); + if (cast) { - CastOp* cast = new CastOp(writeOut, (GFXShaderConstType)(GFXSCT_Float + (cols - 1))); + CastOp* cast = new CastOp(initElem, (GFXShaderConstType)(GFXSCT_Float + (cols - 1))); cast->print(stream); count += cols; } else { - writeOut->print(stream); - count += curSize; + initElem->print(stream); + count += varSize; } if (count < matSize) @@ -432,16 +393,16 @@ void MatrixInitializeOp::print(Stream& stream) } else { - if (curSize != cols) + if (cast) { - CastOp* cast = new CastOp(curVar, (GFXShaderConstType)(GFXSCT_Float + (cols - 1))); + CastOp* cast = new CastOp(initElem, (GFXShaderConstType)(GFXSCT_Float + (cols - 1))); cast->print(stream); count += cols; } else { - curVar->print(stream); - count += curSize; + initElem->print(stream); + count += varSize; } if (count < matSize) @@ -450,18 +411,9 @@ void MatrixInitializeOp::print(Stream& stream) } } } - else - { - // Non-var LangElement, assume it produces correct vector - mInitialVals[elem]->print(stream); - count += cols; - - if (count < matSize) - WRITESTR(",\r\n"); - } } - // If not enough elements → pad with zeros + // If not enough elements → pad with identity while (count < matSize) { U32 row = count / cols; @@ -512,7 +464,7 @@ void MatrixMultiplyOp::print(Stream& stream) return; } - LangElement* rightElem = mInput[0]; + LangElement* rightElem = mInput[1]; Var* rightVar = nullptr; const ShaderTypeInfo* rightInfo = nullptr; diff --git a/Engine/source/shaderGen/shaderOp.h b/Engine/source/shaderGen/shaderOp.h index 7fb3da152..d7035d496 100644 --- a/Engine/source/shaderGen/shaderOp.h +++ b/Engine/source/shaderGen/shaderOp.h @@ -52,11 +52,8 @@ ///************************************************************************** class ShaderOp : public LangElement { -protected: - LangElement * mInput[2]; - public: - bool resolveSourceType(LangElement* elem, Var*& outVar, const ShaderTypeInfo*& outInfo); + LangElement* mInput[2]; ShaderOp( LangElement *in1, LangElement *in2 ); };