| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "BsSLFXCompiler.h"
- #include "RenderAPI/BsGpuProgram.h"
- #include <regex>
- #include "Material/BsShader.h"
- #include "Material/BsTechnique.h"
- #include "Material/BsPass.h"
- #include "RenderAPI/BsSamplerState.h"
- #include "RenderAPI/BsRenderAPI.h"
- #include "Debug/BsDebug.h"
- #include "Material/BsShaderManager.h"
- #include "Material/BsShaderInclude.h"
- #include "Math/BsMatrix4.h"
- #include "Resources/BsBuiltinResources.h"
- #define XSC_ENABLE_LANGUAGE_EXT 1
- #include "Xsc/Xsc.h"
- #include "Material/BsShaderVariation.h"
- extern "C" {
- #include "BsMMAlloc.h"
- #include "BsParserFX.h"
- #include "BsLexerFX.h"
- }
- using namespace std;
- namespace bs
- {
- // Print out the FX AST, only for debug purposes
- void SLFXDebugPrint(ASTFXNode* node, String indent)
- {
- LOGDBG(indent + "NODE " + toString(node->type));
- for (int i = 0; i < node->options->count; i++)
- {
- OptionDataType odt = OPTION_LOOKUP[(int)node->options->entries[i].type].dataType;
- if (odt == ODT_Complex)
- {
- LOGDBG(indent + toString(i) + ". " + toString(node->options->entries[i].type));
- SLFXDebugPrint(node->options->entries[i].value.nodePtr, indent + "\t");
- continue;
- }
- String value;
- switch (odt)
- {
- case ODT_Bool:
- value = toString(node->options->entries[i].value.intValue != 0);
- break;
- case ODT_Int:
- value = toString(node->options->entries[i].value.intValue);
- break;
- case ODT_Float:
- value = toString(node->options->entries[i].value.floatValue);
- break;
- case ODT_String:
- value = node->options->entries[i].value.strValue;
- break;
- case ODT_Matrix:
- {
- Matrix4 mat4 = *(Matrix4*)(node->options->entries[i].value.matrixValue);
- value = toString(mat4);
- }
- break;
- default:
- break;
- }
- LOGDBG(indent + toString(i) + ". " + toString(node->options->entries[i].type) + " = " + value);
- }
- }
- class XscLog : public Xsc::Log
- {
- public:
- void SubmitReport(const Xsc::Report& report) override
- {
- switch (report.Type())
- {
- case Xsc::ReportTypes::Info:
- mInfos.push_back({ FullIndent(), report });
- break;
- case Xsc::ReportTypes::Warning:
- mWarnings.push_back({ FullIndent(), report });
- break;
- case Xsc::ReportTypes::Error:
- mErrors.push_back({ FullIndent(), report });
- break;
- }
- }
- void getMessages(StringStream& output)
- {
- printAndClearReports(output, mInfos);
- printAndClearReports(output, mWarnings, (mWarnings.size() == 1 ? "WARNING" : "WARNINGS"));
- printAndClearReports(output, mErrors, (mErrors.size() == 1 ? "ERROR" : "ERRORS"));
- }
- private:
- struct IndentReport
- {
- std::string indent;
- Xsc::Report report;
- };
- static void printMultiLineString(StringStream& output, const std::string& str, const std::string& indent)
- {
- // Determine at which position the actual text begins (excluding the "error (X:Y) : " or the like)
- auto textStartPos = str.find(" : ");
- if (textStartPos != std::string::npos)
- textStartPos += 3;
- else
- textStartPos = 0;
- std::string newLineIndent(textStartPos, ' ');
- size_t start = 0;
- bool useNewLineIndent = false;
- while (start < str.size())
- {
- output << indent;
- if (useNewLineIndent)
- output << newLineIndent;
- // Print next line
- auto end = str.find('\n', start);
- if (end != std::string::npos)
- {
- output << str.substr(start, end - start);
- start = end + 1;
- }
- else
- {
- output << str.substr(start);
- start = end;
- }
- output << std::endl;
- useNewLineIndent = true;
- }
- }
- void printReport(StringStream& output, const IndentReport& r)
- {
- // Print optional context description
- if (!r.report.Context().empty())
- printMultiLineString(output, r.report.Context(), r.indent);
- // Print report message
- const auto& msg = r.report.Message();
- printMultiLineString(output, msg, r.indent);
- // Print optional line and line-marker
- if (r.report.HasLine())
- {
- const auto& line = r.report.Line();
- const auto& marker = r.report.Marker();
- // Print line
- output << r.indent << line << std::endl;
- // Print line marker
- output << r.indent << marker << std::endl;
- }
- // Print optional hints
- for (const auto& hint : r.report.GetHints())
- output << r.indent << hint << std::endl;
- }
- void printAndClearReports(StringStream& output, Vector<IndentReport>& reports, const String& headline = "")
- {
- if (!reports.empty())
- {
- if (!headline.empty())
- {
- String s = toString((UINT32)reports.size()) + " " + headline;
- output << s << std::endl;
- output << String(s.size(), '-') << std::endl;
- }
- for (const auto& r : reports)
- printReport(output, r);
- reports.clear();
- }
- }
- Vector<IndentReport> mInfos;
- Vector<IndentReport> mWarnings;
- Vector<IndentReport> mErrors;
- };
- GpuParamObjectType ReflTypeToTextureType(Xsc::Reflection::BufferType type)
- {
- switch(type)
- {
- case Xsc::Reflection::BufferType::RWTexture1D: return GPOT_RWTEXTURE1D;
- case Xsc::Reflection::BufferType::RWTexture1DArray: return GPOT_RWTEXTURE1DARRAY;
- case Xsc::Reflection::BufferType::RWTexture2D: return GPOT_RWTEXTURE2D;
- case Xsc::Reflection::BufferType::RWTexture2DArray: return GPOT_RWTEXTURE2DARRAY;
- case Xsc::Reflection::BufferType::RWTexture3D: return GPOT_RWTEXTURE3D;
- case Xsc::Reflection::BufferType::Texture1D: return GPOT_TEXTURE1D;
- case Xsc::Reflection::BufferType::Texture1DArray: return GPOT_TEXTURE1DARRAY;
- case Xsc::Reflection::BufferType::Texture2D: return GPOT_TEXTURE2D;
- case Xsc::Reflection::BufferType::Texture2DArray: return GPOT_TEXTURE2DARRAY;
- case Xsc::Reflection::BufferType::Texture3D: return GPOT_TEXTURE3D;
- case Xsc::Reflection::BufferType::TextureCube: return GPOT_TEXTURECUBE;
- case Xsc::Reflection::BufferType::TextureCubeArray: return GPOT_TEXTURECUBEARRAY;
- case Xsc::Reflection::BufferType::Texture2DMS: return GPOT_TEXTURE2DMS;
- case Xsc::Reflection::BufferType::Texture2DMSArray: return GPOT_TEXTURE2DMSARRAY;
- default: return GPOT_UNKNOWN;
- }
- }
- GpuParamObjectType ReflTypeToBufferType(Xsc::Reflection::BufferType type)
- {
- switch(type)
- {
- case Xsc::Reflection::BufferType::Buffer: return GPOT_RWTYPED_BUFFER;
- case Xsc::Reflection::BufferType::StructuredBuffer: return GPOT_STRUCTURED_BUFFER;
- case Xsc::Reflection::BufferType::ByteAddressBuffer: return GPOT_BYTE_BUFFER;
- case Xsc::Reflection::BufferType::RWBuffer: return GPOT_RWTYPED_BUFFER;
- case Xsc::Reflection::BufferType::RWStructuredBuffer: return GPOT_RWSTRUCTURED_BUFFER;
- case Xsc::Reflection::BufferType::RWByteAddressBuffer: return GPOT_RWBYTE_BUFFER;
- case Xsc::Reflection::BufferType::AppendStructuredBuffer: return GPOT_RWAPPEND_BUFFER;
- case Xsc::Reflection::BufferType::ConsumeStructuredBuffer: return GPOT_RWCONSUME_BUFFER;
- default: return GPOT_UNKNOWN;
- }
- }
- GpuParamDataType ReflTypeToDataType(Xsc::Reflection::DataType type)
- {
- switch (type)
- {
- case Xsc::Reflection::DataType::Bool: return GPDT_BOOL;
- case Xsc::Reflection::DataType::Float: return GPDT_FLOAT1;
- case Xsc::Reflection::DataType::Float2: return GPDT_FLOAT2;
- case Xsc::Reflection::DataType::Float3: return GPDT_FLOAT3;
- case Xsc::Reflection::DataType::Float4: return GPDT_FLOAT4;
- case Xsc::Reflection::DataType::UInt:
- case Xsc::Reflection::DataType::Int:
- return GPDT_INT1;
- case Xsc::Reflection::DataType::UInt2:
- case Xsc::Reflection::DataType::Int2:
- return GPDT_INT2;
- case Xsc::Reflection::DataType::UInt3:
- case Xsc::Reflection::DataType::Int3:
- return GPDT_INT3;
- case Xsc::Reflection::DataType::UInt4:
- case Xsc::Reflection::DataType::Int4:
- return GPDT_INT4;
- case Xsc::Reflection::DataType::Float2x2: return GPDT_MATRIX_2X2;
- case Xsc::Reflection::DataType::Float2x3: return GPDT_MATRIX_2X3;
- case Xsc::Reflection::DataType::Float2x4: return GPDT_MATRIX_2X4;
- case Xsc::Reflection::DataType::Float3x2: return GPDT_MATRIX_3X4;
- case Xsc::Reflection::DataType::Float3x3: return GPDT_MATRIX_3X3;
- case Xsc::Reflection::DataType::Float3x4: return GPDT_MATRIX_3X4;
- case Xsc::Reflection::DataType::Float4x2: return GPDT_MATRIX_4X2;
- case Xsc::Reflection::DataType::Float4x3: return GPDT_MATRIX_4X3;
- case Xsc::Reflection::DataType::Float4x4: return GPDT_MATRIX_4X4;
- default: return GPDT_UNKNOWN;
- }
- }
- HTexture getBuiltinTexture(UINT32 idx)
- {
- if (idx == 1)
- return BuiltinResources::getTexture(BuiltinTexture::White);
- else if (idx == 2)
- return BuiltinResources::getTexture(BuiltinTexture::Black);
- else if (idx == 3)
- return BuiltinResources::getTexture(BuiltinTexture::Normal);
- return HTexture();
- }
- TextureAddressingMode parseTexAddrMode(Xsc::Reflection::TextureAddressMode addrMode)
- {
- switch (addrMode)
- {
- case Xsc::Reflection::TextureAddressMode::Border:
- return TAM_BORDER;
- case Xsc::Reflection::TextureAddressMode::Clamp:
- return TAM_CLAMP;
- case Xsc::Reflection::TextureAddressMode::Mirror:
- case Xsc::Reflection::TextureAddressMode::MirrorOnce:
- return TAM_MIRROR;
- case Xsc::Reflection::TextureAddressMode::Wrap:
- default:
- return TAM_WRAP;
- }
- }
- CompareFunction parseCompFunction(Xsc::Reflection::ComparisonFunc compFunc)
- {
- switch(compFunc)
- {
- case Xsc::Reflection::ComparisonFunc::Always:
- default:
- return CMPF_ALWAYS_PASS;
- case Xsc::Reflection::ComparisonFunc::Never:
- return CMPF_ALWAYS_FAIL;
- case Xsc::Reflection::ComparisonFunc::Equal:
- return CMPF_EQUAL;
- case Xsc::Reflection::ComparisonFunc::Greater:
- return CMPF_GREATER;
- case Xsc::Reflection::ComparisonFunc::GreaterEqual:
- return CMPF_GREATER_EQUAL;
- case Xsc::Reflection::ComparisonFunc::Less:
- return CMPF_LESS;
- case Xsc::Reflection::ComparisonFunc::LessEqual:
- return CMPF_LESS_EQUAL;
- case Xsc::Reflection::ComparisonFunc::NotEqual:
- return CMPF_NOT_EQUAL;
- }
- }
- SPtr<SamplerState> parseSamplerState(const Xsc::Reflection::SamplerState& sampState)
- {
- SAMPLER_STATE_DESC desc;
- desc.addressMode.u = parseTexAddrMode(sampState.addressU);
- desc.addressMode.v = parseTexAddrMode(sampState.addressV);
- desc.addressMode.w = parseTexAddrMode(sampState.addressW);
- desc.borderColor[0] = sampState.borderColor[0];
- desc.borderColor[1] = sampState.borderColor[1];
- desc.borderColor[2] = sampState.borderColor[2];
- desc.borderColor[3] = sampState.borderColor[3];
- desc.comparisonFunc = parseCompFunction(sampState.comparisonFunc);
- desc.maxAniso = sampState.maxAnisotropy;
- desc.mipMax = sampState.maxLOD;
- desc.mipMin = sampState.minLOD;
- desc.mipmapBias = sampState.mipLODBias;
- switch (sampState.filter)
- {
- case Xsc::Reflection::Filter::MinMagMipPoint:
- case Xsc::Reflection::Filter::ComparisonMinMagMipPoint:
- desc.minFilter = FO_POINT;
- desc.magFilter = FO_POINT;
- desc.mipFilter = FO_POINT;
- break;
- case Xsc::Reflection::Filter::MinMagPointMipLinear:
- case Xsc::Reflection::Filter::ComparisonMinMagPointMipLinear:
- desc.minFilter = FO_POINT;
- desc.magFilter = FO_POINT;
- desc.mipFilter = FO_LINEAR;
- break;
- case Xsc::Reflection::Filter::MinPointMagLinearMipPoint:
- case Xsc::Reflection::Filter::ComparisonMinPointMagLinearMipPoint:
- desc.minFilter = FO_POINT;
- desc.magFilter = FO_LINEAR;
- desc.mipFilter = FO_POINT;
- break;
- case Xsc::Reflection::Filter::MinPointMagMipLinear:
- case Xsc::Reflection::Filter::ComparisonMinPointMagMipLinear:
- desc.minFilter = FO_POINT;
- desc.magFilter = FO_LINEAR;
- desc.mipFilter = FO_LINEAR;
- break;
- case Xsc::Reflection::Filter::MinLinearMagMipPoint:
- case Xsc::Reflection::Filter::ComparisonMinLinearMagMipPoint:
- desc.minFilter = FO_LINEAR;
- desc.magFilter = FO_POINT;
- desc.mipFilter = FO_POINT;
- break;
- case Xsc::Reflection::Filter::MinLinearMagPointMipLinear:
- case Xsc::Reflection::Filter::ComparisonMinLinearMagPointMipLinear:
- desc.minFilter = FO_LINEAR;
- desc.magFilter = FO_POINT;
- desc.mipFilter = FO_LINEAR;
- break;
- case Xsc::Reflection::Filter::MinMagLinearMipPoint:
- case Xsc::Reflection::Filter::ComparisonMinMagLinearMipPoint:
- desc.minFilter = FO_LINEAR;
- desc.magFilter = FO_LINEAR;
- desc.mipFilter = FO_POINT;
- break;
- case Xsc::Reflection::Filter::MinMagMipLinear:
- case Xsc::Reflection::Filter::ComparisonMinMagMipLinear:
- desc.minFilter = FO_LINEAR;
- desc.magFilter = FO_LINEAR;
- desc.mipFilter = FO_LINEAR;
- break;
- case Xsc::Reflection::Filter::Anisotropic:
- case Xsc::Reflection::Filter::ComparisonAnisotropic:
- desc.minFilter = FO_ANISOTROPIC;
- desc.magFilter = FO_ANISOTROPIC;
- desc.minFilter = FO_ANISOTROPIC;
- break;
- default:
- break;
- }
-
- return SamplerState::create(desc);
- }
- void parseParameters(const Xsc::Reflection::ReflectionData& reflData, SHADER_DESC& desc)
- {
- for(auto& entry : reflData.uniforms)
- {
- if ((entry.flags & Xsc::Reflection::Uniform::Flags::Internal) != 0)
- continue;
- String ident = entry.ident.c_str();
- switch(entry.type)
- {
- case Xsc::Reflection::UniformType::UniformBuffer:
- desc.setParamBlockAttribs(entry.ident.c_str(), false, GPBU_STATIC);
- break;
- case Xsc::Reflection::UniformType::Buffer:
- {
- GpuParamObjectType objType = ReflTypeToTextureType((Xsc::Reflection::BufferType)entry.baseType);
- if(objType != GPOT_UNKNOWN)
- {
- // Ignore parameters that were already registered in some previous variation. Note that this implies
- // you cannot have same names for different parameters in different variations.
- if (desc.textureParams.find(ident) != desc.textureParams.end())
- continue;
- if (entry.defaultValue == -1)
- desc.addParameter(ident, ident, objType);
- else
- desc.addParameter(ident, ident, objType, getBuiltinTexture(entry.defaultValue));
- }
- else
- {
- // Ignore parameters that were already registered in some previous variation. Note that this implies
- // you cannot have same names for different parameters in different variations.
- if (desc.bufferParams.find(ident) != desc.bufferParams.end())
- continue;
- objType = ReflTypeToBufferType((Xsc::Reflection::BufferType)entry.baseType);
- desc.addParameter(ident, ident, objType);
- }
- }
- break;
- case Xsc::Reflection::UniformType::Sampler:
- {
- auto findIter = reflData.samplerStates.find(entry.ident);
- if (findIter != reflData.samplerStates.end())
- {
- // Ignore parameters that were already registered in some previous variation. Note that this implies
- // you cannot have same names for different parameters in different variations.
- if(desc.samplerParams.find(ident) != desc.samplerParams.end())
- continue;
- String alias = findIter->second.alias.c_str();
- if(findIter->second.isNonDefault)
- {
- SPtr<SamplerState> defaultVal = parseSamplerState(findIter->second);
- desc.addParameter(ident, ident, GPOT_SAMPLER2D, defaultVal);
- if (!alias.empty())
- desc.addParameter(ident, alias, GPOT_SAMPLER2D, defaultVal);
- }
- else
- {
- desc.addParameter(ident, ident, GPOT_SAMPLER2D);
- if (!alias.empty())
- desc.addParameter(ident, alias, GPOT_SAMPLER2D);
- }
- }
- else
- {
- desc.addParameter(ident, ident, GPOT_SAMPLER2D);
- }
- break;
- }
- case Xsc::Reflection::UniformType::Variable:
- {
- bool isBlockInternal = false;
- if(entry.uniformBlock != -1)
- {
- std::string blockName = reflData.constantBuffers[entry.uniformBlock].ident;
- for (auto& uniform : reflData.uniforms)
- {
- if (uniform.type == Xsc::Reflection::UniformType::UniformBuffer && uniform.ident == blockName)
- {
- isBlockInternal = (uniform.flags & Xsc::Reflection::Uniform::Flags::Internal) != 0;
- break;
- }
- }
- }
- if (!isBlockInternal)
- {
- GpuParamDataType type = ReflTypeToDataType((Xsc::Reflection::DataType)entry.baseType);
- if ((entry.flags & Xsc::Reflection::Uniform::Flags::Color) != 0 &&
- (type == GPDT_FLOAT3 || type == GPDT_FLOAT4))
- {
- type = GPDT_COLOR;
- }
- if (entry.defaultValue == -1)
- desc.addParameter(ident, ident, type);
- else
- {
- const Xsc::Reflection::DefaultValue& defVal = reflData.defaultValues[entry.defaultValue];
- desc.addParameter(ident, ident, type, StringID::NONE, 1, 0, (UINT8*)defVal.matrix);
- }
- }
- }
- break;
- case Xsc::Reflection::UniformType::Struct:
- break;
- default: ;
- }
- }
- }
- /** Types of supported code output when cross compiling HLSL to GLSL. */
- enum class CrossCompileOutput
- {
- GLSL45,
- GLSL41,
- VKSL45
- };
- String crossCompile(const String& hlsl, GpuProgramType type, CrossCompileOutput outputType, bool optionalEntry,
- UINT32& startBindingSlot, SHADER_DESC* shaderDesc = nullptr, Vector<GpuProgramType>* detectedTypes = nullptr)
- {
- SPtr<StringStream> input = bs_shared_ptr_new<StringStream>();
- bool isVulkan = outputType == CrossCompileOutput::VKSL45;
- if (isVulkan)
- *input << "#define VULKAN 1" << std::endl;
- else
- *input << "#define OPENGL 1" << std::endl;
- *input << hlsl;
- Xsc::ShaderInput inputDesc;
- inputDesc.shaderVersion = Xsc::InputShaderVersion::HLSL5;
- inputDesc.sourceCode = input;
- inputDesc.extensions = Xsc::Extensions::LayoutAttribute;
- switch (type)
- {
- case GPT_VERTEX_PROGRAM:
- inputDesc.shaderTarget = Xsc::ShaderTarget::VertexShader;
- inputDesc.entryPoint = "vsmain";
- break;
- case GPT_GEOMETRY_PROGRAM:
- inputDesc.shaderTarget = Xsc::ShaderTarget::GeometryShader;
- inputDesc.entryPoint = "gsmain";
- break;
- case GPT_HULL_PROGRAM:
- inputDesc.shaderTarget = Xsc::ShaderTarget::TessellationControlShader;
- inputDesc.entryPoint = "hsmain";
- break;
- case GPT_DOMAIN_PROGRAM:
- inputDesc.shaderTarget = Xsc::ShaderTarget::TessellationEvaluationShader;
- inputDesc.entryPoint = "dsmain";
- break;
- case GPT_FRAGMENT_PROGRAM:
- inputDesc.shaderTarget = Xsc::ShaderTarget::FragmentShader;
- inputDesc.entryPoint = "fsmain";
- break;
- case GPT_COMPUTE_PROGRAM:
- inputDesc.shaderTarget = Xsc::ShaderTarget::ComputeShader;
- inputDesc.entryPoint = "csmain";
- break;
- default:
- break;
- }
- StringStream output;
- Xsc::ShaderOutput outputDesc;
- outputDesc.sourceCode = &output;
- outputDesc.options.autoBinding = isVulkan;
- outputDesc.options.autoBindingStartSlot = startBindingSlot;
- outputDesc.options.fragmentLocations = true;
- outputDesc.options.separateShaders = true;
- outputDesc.options.separateSamplers = false;
- outputDesc.nameMangling.inputPrefix = "bs_";
- outputDesc.nameMangling.outputPrefix = "bs_";
- outputDesc.nameMangling.useAlwaysSemantics = true;
- outputDesc.nameMangling.renameBufferFields = true;
- switch(outputType)
- {
- case CrossCompileOutput::GLSL45:
- outputDesc.shaderVersion = Xsc::OutputShaderVersion::GLSL450;
- break;
- case CrossCompileOutput::GLSL41:
- outputDesc.shaderVersion = Xsc::OutputShaderVersion::GLSL410;
- break;
- case CrossCompileOutput::VKSL45:
- outputDesc.shaderVersion = Xsc::OutputShaderVersion::VKSL450;
- break;
- }
- XscLog log;
- Xsc::Reflection::ReflectionData reflectionData;
- bool compileSuccess = Xsc::CompileShader(inputDesc, outputDesc, &log, &reflectionData);
- if (!compileSuccess)
- {
- // If enabled, don't fail if entry point isn't found
- bool done = true;
- if(optionalEntry)
- {
- bool entryFound = false;
- for (auto& entry : reflectionData.functions)
- {
- if(entry.ident == inputDesc.entryPoint)
- {
- entryFound = true;
- break;
- }
- }
- if (!entryFound)
- done = false;
- }
- if (done)
- {
- StringStream logOutput;
- log.getMessages(logOutput);
- LOGERR("Shader cross compilation failed. Log: \n\n" + logOutput.str());
- return "";
- }
- }
- for (auto& entry : reflectionData.constantBuffers)
- startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
- for (auto& entry : reflectionData.textures)
- startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
- for (auto& entry : reflectionData.storageBuffers)
- startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
- if(detectedTypes != nullptr)
- {
- for(auto& entry : reflectionData.functions)
- {
- if (entry.ident == "vsmain")
- detectedTypes->push_back(GPT_VERTEX_PROGRAM);
- else if (entry.ident == "fsmain")
- detectedTypes->push_back(GPT_FRAGMENT_PROGRAM);
- else if (entry.ident == "gsmain")
- detectedTypes->push_back(GPT_GEOMETRY_PROGRAM);
- else if (entry.ident == "dsmain")
- detectedTypes->push_back(GPT_DOMAIN_PROGRAM);
- else if (entry.ident == "hsmain")
- detectedTypes->push_back(GPT_HULL_PROGRAM);
- else if (entry.ident == "csmain")
- detectedTypes->push_back(GPT_COMPUTE_PROGRAM);
- }
- // If no entry points found, and error occurred, report error
- if(!compileSuccess && detectedTypes->size() == 0)
- {
- StringStream logOutput;
- log.getMessages(logOutput);
- LOGERR("Shader cross compilation failed. Log: \n\n" + logOutput.str());
- return "";
- }
- }
- if (shaderDesc != nullptr)
- parseParameters(reflectionData, *shaderDesc);
- return output.str();
- }
- // Convert HLSL code to GLSL
- String HLSLtoGLSL(const String& hlsl, GpuProgramType type, CrossCompileOutput outputType, UINT32& startBindingSlot)
- {
- return crossCompile(hlsl, type, outputType, false, startBindingSlot);
- }
- void reflectHLSL(const String& hlsl, SHADER_DESC& shaderDesc, Vector<GpuProgramType>& entryPoints)
- {
- UINT32 dummy = 0;
- crossCompile(hlsl, GPT_VERTEX_PROGRAM, CrossCompileOutput::GLSL45, true, dummy, &shaderDesc, &entryPoints);
- }
- BSLFXCompileResult BSLFXCompiler::compile(const String& name, const String& source,
- const UnorderedMap<String, String>& defines)
- {
- String parsedSource = source;
- // Run the lexer/parser and generate the AST
- ParseState* parseState = parseStateCreate();
- BSLFXCompileResult output = parseFX(parseState, parsedSource.c_str(), defines);
- if(!output.errorMessage.empty())
- parseStateDelete(parseState);
- else
- {
- // Only enable for debug purposes
- //SLFXDebugPrint(parseState->rootNode, "");
- if (parseState->rootNode == nullptr || parseState->rootNode->type != NT_Shader)
- {
- parseStateDelete(parseState);
- output.errorMessage = "Root not is null or not a shader.";
- return output;
- }
- // Parse global shader options & technique meta-data
- SHADER_DESC shaderDesc;
- Vector<pair<ASTFXNode*, TechniqueMetaData>> techniqueMetaData;
- //// Go in reverse because options are added in reverse order during parsing
- for (int i = parseState->rootNode->options->count - 1; i >= 0; i--)
- {
- NodeOption* option = &parseState->rootNode->options->entries[i];
- switch (option->type)
- {
- case OT_Options:
- parseOptions(option->value.nodePtr, shaderDesc);
- break;
- case OT_Technique:
- {
- // We initially parse only meta-data, so we can handle out-of-order technique definitions
- TechniqueMetaData metaData = parseTechniqueMetaData(option->value.nodePtr);
- techniqueMetaData.push_back(std::make_pair(option->value.nodePtr, metaData));
- break;
- }
- default:
- break;
- }
- }
- // Inherit variations from mixins
- bool* techniqueWasParsed = bs_stack_alloc<bool>((UINT32)techniqueMetaData.size());
- std::function<bool(const TechniqueMetaData&, TechniqueMetaData&)> parseInherited =
- [&](const TechniqueMetaData& metaData, TechniqueMetaData& combinedMetaData)
- {
- for (auto riter = metaData.includes.rbegin(); riter != metaData.includes.rend(); ++riter)
- {
- const String& includes = *riter;
- UINT32 baseIdx = -1;
- for (UINT32 i = 0; i < (UINT32)techniqueMetaData.size(); i++)
- {
- auto& entry = techniqueMetaData[i];
- if (!entry.second.isMixin)
- continue;
- if (entry.second.name == includes)
- {
- bool matches = entry.second.language == metaData.language || entry.second.language == "Any";
- // We want the last matching technique, in order to allow techniques to override each other
- if (matches)
- baseIdx = i;
- }
- }
- if (baseIdx != (UINT32)-1)
- {
- auto& entry = techniqueMetaData[baseIdx];
- // Was already parsed previously, don't parse it multiple times (happens when multiple techniques
- // include the same mixin)
- if (techniqueWasParsed[baseIdx])
- continue;
- if (!parseInherited(entry.second, combinedMetaData))
- return false;
- for(auto& variation : entry.second.variations)
- combinedMetaData.variations.push_back(variation);
- techniqueWasParsed[baseIdx] = true;
- }
- else
- {
- output.errorMessage = "Mixin \"" + includes + "\" cannot be found.";
- return false;
- }
- }
- return true;
- };
- for (auto& entry : techniqueMetaData)
- {
- TechniqueMetaData& metaData = entry.second;
- if (metaData.isMixin)
- continue;
- bs_zero_out(techniqueWasParsed, techniqueMetaData.size());
- TechniqueMetaData combinedMetaData = metaData;
- if (!parseInherited(metaData, combinedMetaData))
- {
- parseStateDelete(parseState);
- bs_stack_free(techniqueWasParsed);
- return output;
- }
- entry.second = combinedMetaData;
- }
- bs_stack_free(techniqueWasParsed);
- parseStateDelete(parseState);
- // Build a list of different variations and re-parse the source using the relevant defines
- Vector<SPtr<Technique>> techniques;
- UnorderedSet<String> includes;
- for (auto& entry : techniqueMetaData)
- {
- TechniqueMetaData& metaData = entry.second;
- if (metaData.isMixin)
- continue;
- // Generate a list of variations
- Vector<ShaderVariation> variations;
- if (metaData.variations.empty())
- variations.push_back(ShaderVariation());
- else
- {
- Vector<const VariationData*> todo;
- for (UINT32 i = 0; i < (UINT32)metaData.variations.size(); i++)
- todo.push_back(&metaData.variations[i]);
- while (!todo.empty())
- {
- const VariationData* current = todo.back();
- todo.erase(todo.end() - 1);
- // Variation parameter that's either defined or isn't
- if (current->values.empty())
- {
- // This is the first variation parameter, register new variations
- if (variations.empty())
- {
- ShaderVariation a;
- ShaderVariation b;
- b.addParam(ShaderVariation::Param(current->identifier, 1));
- variations.push_back(a);
- variations.push_back(b);
- }
- else // Duplicate existing variations, and add the parameter
- {
- UINT32 numVariations = (UINT32)variations.size();
- for (UINT32 i = 0; i < numVariations; i++)
- {
- // Make a copy
- variations.push_back(variations[i]);
- // Add the parameter to existing variation
- variations[i].addParam(ShaderVariation::Param(current->identifier, 1));
- }
- }
- }
- else // Variation parameter with multiple values
- {
- // This is the first variation parameter, register new variations
- if (variations.empty())
- {
- for (UINT32 i = 0; i < (UINT32)current->values.size(); i++)
- {
- ShaderVariation variation;
- variation.addParam(ShaderVariation::Param(current->identifier, current->values[i]));
- variations.push_back(variation);
- }
- }
- else // Duplicate existing variations, and add the parameter
- {
- UINT32 numVariations = (UINT32)variations.size();
- for (UINT32 i = 0; i < numVariations; i++)
- {
- for (UINT32 j = 1; j < (UINT32)current->values.size(); j++)
- {
- ShaderVariation copy = variations[i];
- copy.addParam(ShaderVariation::Param(current->identifier, current->values[j]));
- variations.push_back(copy);
- }
- variations[i].addParam(ShaderVariation::Param(current->identifier, current->values[0]));
- }
- }
- }
- }
- }
- // For every variation, re-parse the file with relevant defines
- for(auto& variation : variations)
- {
- UnorderedMap<String, String> globalDefines = defines;
- UnorderedMap<String, String> variationDefines = variation.getDefines().getAll();
- for(auto& define : variationDefines)
- globalDefines[define.first] = define.second;
- ParseState* variationParseState = parseStateCreate();
- output = parseFX(variationParseState, parsedSource.c_str(), globalDefines);
- if (!output.errorMessage.empty())
- parseStateDelete(variationParseState);
- else
- {
- Vector<String> codeBlocks;
- CodeString* codeString = variationParseState->codeStrings;
- while (codeString != nullptr)
- {
- while ((INT32)codeBlocks.size() <= codeString->index)
- codeBlocks.push_back(String());
- codeBlocks[codeString->index] = String(codeString->code, codeString->size);
- codeString = codeString->next;
- }
- output = parseTechnique(variationParseState, entry.second.name, codeBlocks, variation, techniques,
- includes, shaderDesc);
- if(!output.errorMessage.empty())
- return output;
- }
- }
- }
- // Generate a shader from the parsed techniques
- Vector<String> includeArray;
- for (auto& entry : includes)
- includeArray.push_back(entry);
- output.shader = Shader::_createPtr(name, shaderDesc, techniques);
- output.shader->setIncludeFiles(includeArray);
- // Verify GPU programs compile correctly
- StringStream gpuProgError;
- bool hasError = false;
- if (output.shader != nullptr)
- {
- Vector<SPtr<Technique>> techniques = output.shader->getCompatibleTechniques();
- for (auto& technique : techniques)
- {
- UINT32 numPasses = technique->getNumPasses();
- for (UINT32 i = 0; i < numPasses; i++)
- {
- SPtr<Pass> pass = technique->getPass(i);
- auto checkCompileStatus = [&](const String& prefix, const SPtr<GpuProgram>& prog)
- {
- if (prog != nullptr)
- {
- prog->blockUntilCoreInitialized();
- if (!prog->isCompiled())
- {
- hasError = true;
- gpuProgError << prefix << ": " << prog->getCompileErrorMessage() << std::endl;
- }
- }
- };
- checkCompileStatus("Vertex program", pass->getVertexProgram());
- checkCompileStatus("Fragment program", pass->getFragmentProgram());
- checkCompileStatus("Geometry program", pass->getGeometryProgram());
- checkCompileStatus("Hull program", pass->getHullProgram());
- checkCompileStatus("Domain program", pass->getDomainProgram());
- checkCompileStatus("Compute program", pass->getComputeProgram());
- }
- }
- }
- if (hasError)
- {
- output.errorMessage = "Failed compiling GPU program(s): " + gpuProgError.str();
- output.errorLine = 0;
- output.errorColumn = 0;
- }
- }
- return output;
- }
- BSLFXCompileResult BSLFXCompiler::parseFX(ParseState* parseState, const char* source, const UnorderedMap<String, String>& defines)
- {
- for(auto& define : defines)
- {
- if (define.first.size() == 0)
- continue;
- addDefine(parseState, define.first.c_str());
- if(define.second.size() > 0)
- addDefineExpr(parseState, define.second.c_str());
- }
- yyscan_t scanner;
- YY_BUFFER_STATE state;
- BSLFXCompileResult output;
- if (yylex_init_extra(parseState, &scanner))
- {
- output.errorMessage = "Internal error: Lexer failed to initialize.";
- return output;
- }
- // If debug output from lexer is needed uncomment this and add %debug option to lexer file
- //yyset_debug(true, scanner);
- // If debug output from parser is needed uncomment this and add %debug option to parser file
- //yydebug = true;
- state = yy_scan_string(source, scanner);
- if (yyparse(parseState, scanner))
- {
- if (parseState->hasError > 0)
- {
- output.errorMessage = parseState->errorMessage;
- output.errorLine = parseState->errorLine;
- output.errorColumn = parseState->errorColumn;
- if (parseState->errorFile != nullptr)
- output.errorFile = parseState->errorFile;
- }
- else
- output.errorMessage = "Internal error: Parsing failed.";
- return output;
- }
- yy_delete_buffer(state, scanner);
- yylex_destroy(scanner);
- return output;
- }
- BSLFXCompiler::TechniqueMetaData BSLFXCompiler::parseTechniqueMetaData(ASTFXNode* technique)
- {
- TechniqueMetaData metaData;
- metaData.language = "hlsl";
- metaData.isMixin = technique->type == NT_Mixin;
- for (int i = 0; i < technique->options->count; i++)
- {
- NodeOption* option = &technique->options->entries[i];
- switch (option->type)
- {
- case OT_Tags:
- {
- ASTFXNode* tagsNode = option->value.nodePtr;
- for (int j = 0; j < tagsNode->options->count; j++)
- {
- NodeOption* tagOption = &tagsNode->options->entries[j];
- if (tagOption->type == OT_TagValue)
- metaData.tags.push_back(removeQuotes(tagOption->value.strValue));
- }
- }
- break;
- case OT_Variations:
- {
- ASTFXNode* variationsNode = option->value.nodePtr;
- for (int j = 0; j < variationsNode->options->count; j++)
- {
- NodeOption* variationOption = &variationsNode->options->entries[j];
- if(variationOption->type == OT_Variation)
- parseVariations(metaData, variationOption->value.nodePtr);
- }
- }
- break;
- case OT_Identifier:
- metaData.name = option->value.strValue;
- break;
- case OT_Mixin:
- metaData.includes.push_back(option->value.strValue);
- break;
- default:
- break;
- }
- }
- return metaData;
- }
- void BSLFXCompiler::parseVariations(TechniqueMetaData& metaData, ASTFXNode* variations)
- {
- assert(variations->type == NT_Variation);
- VariationData variationData;
- for (int i = 0; i < variations->options->count; i++)
- {
- NodeOption* option = &variations->options->entries[i];
- switch (option->type)
- {
- case OT_Identifier:
- variationData.identifier = option->value.strValue;
- break;
- case OT_VariationValue:
- variationData.values.push_back(option->value.intValue);
- break;
- default:
- break;
- }
- }
- if (!variationData.identifier.empty())
- metaData.variations.push_back(variationData);
- }
- QueueSortType BSLFXCompiler::parseSortType(CullAndSortModeValue sortType)
- {
- switch (sortType)
- {
- case CASV_BackToFront:
- return QueueSortType::BackToFront;
- case CASV_FrontToBack:
- return QueueSortType::FrontToBack;
- case CASV_None:
- return QueueSortType::None;
- default:
- break;
- }
- return QueueSortType::None;
- }
- CompareFunction BSLFXCompiler::parseCompFunc(CompFuncValue compFunc)
- {
- switch (compFunc)
- {
- case CFV_Pass:
- return CMPF_ALWAYS_PASS;
- case CFV_Fail:
- return CMPF_ALWAYS_FAIL;
- case CFV_LT:
- return CMPF_LESS;
- case CFV_LTE:
- return CMPF_LESS_EQUAL;
- case CFV_EQ:
- return CMPF_EQUAL;
- case CFV_NEQ:
- return CMPF_NOT_EQUAL;
- case CFV_GT:
- return CMPF_GREATER;
- case CFV_GTE:
- return CMPF_GREATER_EQUAL;
- }
- return CMPF_ALWAYS_PASS;
- }
- BlendFactor BSLFXCompiler::parseBlendFactor(OpValue factor)
- {
- switch (factor)
- {
- case OV_One:
- return BF_ONE;
- case OV_Zero:
- return BF_ZERO;
- case OV_DestColor:
- return BF_DEST_COLOR;
- case OV_SrcColor:
- return BF_SOURCE_COLOR;
- case OV_InvDestColor:
- return BF_INV_DEST_COLOR;
- case OV_InvSrcColor:
- return BF_INV_SOURCE_COLOR;
- case OV_DestAlpha:
- return BF_DEST_ALPHA;
- case OV_SrcAlpha:
- return BF_SOURCE_ALPHA;
- case OV_InvDestAlpha:
- return BF_INV_DEST_ALPHA;
- case OV_InvSrcAlpha:
- return BF_INV_SOURCE_ALPHA;
- default:
- break;
- }
- return BF_ONE;
- }
- BlendOperation BSLFXCompiler::parseBlendOp(BlendOpValue op)
- {
- switch (op)
- {
- case BOV_Add:
- return BO_ADD;
- case BOV_Max:
- return BO_MAX;
- case BOV_Min:
- return BO_MIN;
- case BOV_Subtract:
- return BO_SUBTRACT;
- case BOV_RevSubtract:
- return BO_REVERSE_SUBTRACT;
- }
- return BO_ADD;
- }
- StencilOperation BSLFXCompiler::parseStencilOp(OpValue op)
- {
- switch (op)
- {
- case OV_Keep:
- return SOP_KEEP;
- case OV_Zero:
- return SOP_ZERO;
- case OV_Replace:
- return SOP_REPLACE;
- case OV_Incr:
- return SOP_INCREMENT;
- case OV_Decr:
- return SOP_DECREMENT;
- case OV_IncrWrap:
- return SOP_INCREMENT_WRAP;
- case OV_DecrWrap:
- return SOP_DECREMENT_WRAP;
- case OV_Invert:
- return SOP_INVERT;
- default:
- break;
- }
- return SOP_KEEP;
- }
- CullingMode BSLFXCompiler::parseCullMode(CullAndSortModeValue cm)
- {
- switch (cm)
- {
- case CASV_None:
- return CULL_NONE;
- case CASV_CW:
- return CULL_CLOCKWISE;
- case CASV_CCW:
- return CULL_COUNTERCLOCKWISE;
- default:
- break;
- }
- return CULL_COUNTERCLOCKWISE;
- }
- PolygonMode BSLFXCompiler::parseFillMode(FillModeValue fm)
- {
- if (fm == FMV_Wire)
- return PM_WIREFRAME;
- return PM_SOLID;
- }
- void BSLFXCompiler::parseStencilFront(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode)
- {
- if (stencilOpNode == nullptr || stencilOpNode->type != NT_StencilOp)
- return;
- for (int i = 0; i < stencilOpNode->options->count; i++)
- {
- NodeOption* option = &stencilOpNode->options->entries[i];
- switch (option->type)
- {
- case OT_Fail:
- desc.frontStencilFailOp = parseStencilOp((OpValue)option->value.intValue);
- break;
- case OT_ZFail:
- desc.frontStencilZFailOp = parseStencilOp((OpValue)option->value.intValue);
- break;
- case OT_PassOp:
- desc.frontStencilPassOp = parseStencilOp((OpValue)option->value.intValue);
- break;
- case OT_CompareFunc:
- desc.frontStencilComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
- break;
- default:
- break;
- }
- }
- }
- void BSLFXCompiler::parseStencilBack(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode)
- {
- if (stencilOpNode == nullptr || stencilOpNode->type != NT_StencilOp)
- return;
- for (int i = 0; i < stencilOpNode->options->count; i++)
- {
- NodeOption* option = &stencilOpNode->options->entries[i];
- switch (option->type)
- {
- case OT_Fail:
- desc.backStencilFailOp = parseStencilOp((OpValue)option->value.intValue);
- break;
- case OT_ZFail:
- desc.backStencilZFailOp = parseStencilOp((OpValue)option->value.intValue);
- break;
- case OT_PassOp:
- desc.backStencilPassOp = parseStencilOp((OpValue)option->value.intValue);
- break;
- case OT_CompareFunc:
- desc.backStencilComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
- break;
- default:
- break;
- }
- }
- }
- void BSLFXCompiler::parseColorBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode)
- {
- if (blendDefNode == nullptr || blendDefNode->type != NT_BlendDef)
- return;
- for (int i = 0; i < blendDefNode->options->count; i++)
- {
- NodeOption* option = &blendDefNode->options->entries[i];
- switch (option->type)
- {
- case OT_Source:
- desc.srcBlend = parseBlendFactor((OpValue)option->value.intValue);
- break;
- case OT_Dest:
- desc.dstBlend = parseBlendFactor((OpValue)option->value.intValue);
- break;
- case OT_Op:
- desc.blendOp = parseBlendOp((BlendOpValue)option->value.intValue);
- break;
- default:
- break;
- }
- }
- }
- void BSLFXCompiler::parseAlphaBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode)
- {
- if (blendDefNode == nullptr || blendDefNode->type != NT_BlendDef)
- return;
- for (int i = 0; i < blendDefNode->options->count; i++)
- {
- NodeOption* option = &blendDefNode->options->entries[i];
- switch (option->type)
- {
- case OT_Source:
- desc.srcBlendAlpha = parseBlendFactor((OpValue)option->value.intValue);
- break;
- case OT_Dest:
- desc.dstBlendAlpha = parseBlendFactor((OpValue)option->value.intValue);
- break;
- case OT_Op:
- desc.blendOpAlpha = parseBlendOp((BlendOpValue)option->value.intValue);
- break;
- default:
- break;
- }
- }
- }
- void BSLFXCompiler::parseRenderTargetBlendState(BLEND_STATE_DESC& desc, ASTFXNode* targetNode)
- {
- if (targetNode == nullptr || targetNode->type != NT_Target)
- return;
- UINT32 index = 0;
- for (int i = 0; i < targetNode->options->count; i++)
- {
- NodeOption* option = &targetNode->options->entries[i];
- switch (option->type)
- {
- case OT_Index:
- index = option->value.intValue;
- break;
- default:
- break;
- }
- }
- if (index >= BS_MAX_MULTIPLE_RENDER_TARGETS)
- return;
- RENDER_TARGET_BLEND_STATE_DESC& rtDesc = desc.renderTargetDesc[index];
- for (int i = 0; i < targetNode->options->count; i++)
- {
- NodeOption* option = &targetNode->options->entries[i];
- switch (option->type)
- {
- case OT_Enabled:
- rtDesc.blendEnable = option->value.intValue > 0;
- break;
- case OT_Color:
- parseColorBlendDef(rtDesc, option->value.nodePtr);
- break;
- case OT_Alpha:
- parseAlphaBlendDef(rtDesc, option->value.nodePtr);
- break;
- case OT_WriteMask:
- rtDesc.renderTargetWriteMask = option->value.intValue;
- break;
- default:
- break;
- }
- }
- }
- bool BSLFXCompiler::parseBlendState(PassData& desc, ASTFXNode* blendNode)
- {
- if (blendNode == nullptr || blendNode->type != NT_Blend)
- return false;
- bool isDefault = true;
- for (int i = 0; i < blendNode->options->count; i++)
- {
- NodeOption* option = &blendNode->options->entries[i];
- switch (option->type)
- {
- case OT_AlphaToCoverage:
- desc.blendDesc.alphaToCoverageEnable = option->value.intValue > 0;
- isDefault = false;
- break;
- case OT_IndependantBlend:
- desc.blendDesc.independantBlendEnable = option->value.intValue > 0;
- isDefault = false;
- break;
- case OT_Target:
- parseRenderTargetBlendState(desc.blendDesc, option->value.nodePtr);
- isDefault = false;
- break;
- default:
- break;
- }
- }
- return !isDefault;
- }
- bool BSLFXCompiler::parseRasterizerState(PassData& desc, ASTFXNode* rasterNode)
- {
- if (rasterNode == nullptr || rasterNode->type != NT_Raster)
- return false;
- bool isDefault = true;
- for (int i = 0; i < rasterNode->options->count; i++)
- {
- NodeOption* option = &rasterNode->options->entries[i];
- switch (option->type)
- {
- case OT_FillMode:
- desc.rasterizerDesc.polygonMode = parseFillMode((FillModeValue)option->value.intValue);
- isDefault = false;
- break;
- case OT_CullMode:
- desc.rasterizerDesc.cullMode = parseCullMode((CullAndSortModeValue)option->value.intValue);
- isDefault = false;
- break;
- case OT_DepthBias:
- desc.rasterizerDesc.depthBias = option->value.floatValue;
- isDefault = false;
- break;
- case OT_SDepthBias:
- desc.rasterizerDesc.slopeScaledDepthBias = option->value.floatValue;
- isDefault = false;
- break;
- case OT_DepthClip:
- desc.rasterizerDesc.depthClipEnable = option->value.intValue > 0;
- isDefault = false;
- break;
- case OT_Scissor:
- desc.rasterizerDesc.scissorEnable = option->value.intValue > 0;
- isDefault = false;
- break;
- case OT_Multisample:
- desc.rasterizerDesc.multisampleEnable = option->value.intValue > 0;
- isDefault = false;
- break;
- case OT_AALine:
- desc.rasterizerDesc.antialiasedLineEnable = option->value.intValue > 0;
- isDefault = false;
- break;
- default:
- break;
- }
- }
- return !isDefault;
- }
- bool BSLFXCompiler::parseDepthState(PassData& passData, ASTFXNode* depthNode)
- {
- if (depthNode == nullptr || depthNode->type != NT_Depth)
- return false;
- bool isDefault = true;
- for (int i = 0; i < depthNode->options->count; i++)
- {
- NodeOption* option = &depthNode->options->entries[i];
- switch (option->type)
- {
- case OT_DepthRead:
- passData.depthStencilDesc.depthReadEnable = option->value.intValue > 0;
- isDefault = false;
- break;
- case OT_DepthWrite:
- passData.depthStencilDesc.depthWriteEnable = option->value.intValue > 0;
- isDefault = false;
- break;
- case OT_CompareFunc:
- passData.depthStencilDesc.depthComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
- isDefault = false;
- break;
- default:
- break;
- }
- }
- return !isDefault;
- }
- bool BSLFXCompiler::parseStencilState(PassData& passData, ASTFXNode* stencilNode)
- {
- if (stencilNode == nullptr || stencilNode->type != NT_Stencil)
- return false;
- bool isDefault = true;
- for (int i = 0; i < stencilNode->options->count; i++)
- {
- NodeOption* option = &stencilNode->options->entries[i];
- switch (option->type)
- {
- case OT_Enabled:
- passData.depthStencilDesc.stencilEnable = option->value.intValue > 0;
- isDefault = false;
- break;
- case OT_StencilReadMask:
- passData.depthStencilDesc.stencilReadMask = (UINT8)option->value.intValue;
- isDefault = false;
- break;
- case OT_StencilWriteMask:
- passData.depthStencilDesc.stencilWriteMask = (UINT8)option->value.intValue;
- isDefault = false;
- break;
- case OT_StencilOpFront:
- parseStencilFront(passData.depthStencilDesc, option->value.nodePtr);
- isDefault = false;
- break;
- case OT_StencilOpBack:
- parseStencilBack(passData.depthStencilDesc, option->value.nodePtr);
- isDefault = false;
- break;
- case OT_StencilRef:
- passData.stencilRefValue = option->value.intValue;
- break;
- default:
- break;
- }
- }
- return !isDefault;
- }
-
- void BSLFXCompiler::parseCodeBlock(ASTFXNode* codeNode, const Vector<String>& codeBlocks, PassData& passData)
- {
- if (codeNode == nullptr || (codeNode->type != NT_Code))
- {
- return;
- }
- UINT32 index = (UINT32)-1;
- for (int j = 0; j < codeNode->options->count; j++)
- {
- if (codeNode->options->entries[j].type == OT_Index)
- index = codeNode->options->entries[j].value.intValue;
- }
- if (index != (UINT32)-1 && index < (UINT32)codeBlocks.size())
- {
- switch (codeNode->type)
- {
- case NT_Code:
- passData.code += codeBlocks[index];
- break;
- default:
- break;
- }
- }
- }
- void BSLFXCompiler::parsePass(ASTFXNode* passNode, const Vector<String>& codeBlocks, PassData& passData)
- {
- if (passNode == nullptr || passNode->type != NT_Pass)
- return;
- for (int i = 0; i < passNode->options->count; i++)
- {
- NodeOption* option = &passNode->options->entries[i];
- switch (option->type)
- {
- case OT_Blend:
- passData.blendIsDefault &= !parseBlendState(passData, option->value.nodePtr);
- break;
- case OT_Raster:
- passData.rasterizerIsDefault &= !parseRasterizerState(passData, option->value.nodePtr);
- break;
- case OT_Depth:
- passData.depthStencilIsDefault &= !parseDepthState(passData, option->value.nodePtr);
- break;
- case OT_Stencil:
- passData.depthStencilIsDefault &= !parseStencilState(passData, option->value.nodePtr);
- break;
- case OT_Code:
- parseCodeBlock(option->value.nodePtr, codeBlocks, passData);
- break;
- default:
- break;
- }
- }
- }
- void BSLFXCompiler::parseTechnique(ASTFXNode* techniqueNode, const Vector<String>& codeBlocks, TechniqueData& techniqueData)
- {
- if (techniqueNode == nullptr || (techniqueNode->type != NT_Technique && techniqueNode->type != NT_Mixin))
- return;
- // There must always be at least one pass
- if(techniqueData.passes.empty())
- {
- techniqueData.passes.push_back(PassData());
- techniqueData.passes.back().seqIdx = 0;
- }
- PassData combinedCommonPassData;
- UINT32 nextPassIdx = 0;
- // Go in reverse because options are added in reverse order during parsing
- for (int i = techniqueNode->options->count - 1; i >= 0; i--)
- {
- NodeOption* option = &techniqueNode->options->entries[i];
- switch (option->type)
- {
- case OT_Pass:
- {
- UINT32 passIdx = nextPassIdx;
- PassData* passData = nullptr;
- for (auto& entry : techniqueData.passes)
- {
- if (entry.seqIdx == passIdx)
- passData = &entry;
- }
- if (passData == nullptr)
- {
- techniqueData.passes.push_back(PassData());
- passData = &techniqueData.passes.back();
- passData->seqIdx = passIdx;
- }
- nextPassIdx = std::max(nextPassIdx, passIdx) + 1;
- passData->code = combinedCommonPassData.code + passData->code;
-
- ASTFXNode* passNode = option->value.nodePtr;
- parsePass(passNode, codeBlocks, *passData);
- }
- break;
- case OT_Code:
- {
- PassData commonPassData;
- parseCodeBlock(option->value.nodePtr, codeBlocks, commonPassData);
- for (auto& passData : techniqueData.passes)
- passData.code += commonPassData.code;
- combinedCommonPassData.code += commonPassData.code;
- }
- break;
- case OT_FeatureSet:
- techniqueData.metaData.featureSet = option->value.strValue;
- break;
- default:
- break;
- }
- }
- // Parse common pass states
- for (int i = 0; i < techniqueNode->options->count; i++)
- {
- NodeOption* option = &techniqueNode->options->entries[i];
- switch (option->type)
- {
- case OT_Blend:
- for (auto& passData : techniqueData.passes)
- passData.blendIsDefault &= !parseBlendState(passData, option->value.nodePtr);
- break;
- case OT_Raster:
- for (auto& passData : techniqueData.passes)
- passData.rasterizerIsDefault &= !parseRasterizerState(passData, option->value.nodePtr);
- break;
- case OT_Depth:
- for (auto& passData : techniqueData.passes)
- passData.depthStencilIsDefault &= !parseDepthState(passData, option->value.nodePtr);
- break;
- case OT_Stencil:
- for (auto& passData : techniqueData.passes)
- passData.depthStencilIsDefault &= !parseStencilState(passData, option->value.nodePtr);
- break;
- default:
- break;
- }
- }
- }
- void BSLFXCompiler::parseOptions(ASTFXNode* optionsNode, SHADER_DESC& shaderDesc)
- {
- if (optionsNode == nullptr || optionsNode->type != NT_Options)
- return;
- for (int i = optionsNode->options->count - 1; i >= 0; i--)
- {
- NodeOption* option = &optionsNode->options->entries[i];
- switch (option->type)
- {
- case OT_Separable:
- shaderDesc.separablePasses = option->value.intValue > 1;
- break;
- case OT_Sort:
- shaderDesc.queueSortType = parseSortType((CullAndSortModeValue)option->value.intValue);
- break;
- case OT_Priority:
- shaderDesc.queuePriority = option->value.intValue;
- break;
- case OT_Transparent:
- shaderDesc.flags |= (UINT32)ShaderFlags::Transparent;
- break;
- default:
- break;
- }
- }
- }
- BSLFXCompileResult BSLFXCompiler::parseTechnique(ParseState* parseState, const String& name,
- const Vector<String>& codeBlocks, const ShaderVariation& variation, Vector<SPtr<Technique>>& techniques,
- UnorderedSet<String>& includes, SHADER_DESC& shaderDesc)
- {
- BSLFXCompileResult output;
- if (parseState->rootNode == nullptr || parseState->rootNode->type != NT_Shader)
- {
- parseStateDelete(parseState);
- output.errorMessage = "Root not is null or not a shader.";
- return output;
- }
- Vector<pair<ASTFXNode*, TechniqueData>> techniqueData;
- // Go in reverse because options are added in reverse order during parsing
- for (int i = parseState->rootNode->options->count - 1; i >= 0; i--)
- {
- NodeOption* option = &parseState->rootNode->options->entries[i];
- switch (option->type)
- {
- case OT_Technique:
- {
- // We initially parse only meta-data, so we can handle out-of-order technique definitions
- TechniqueMetaData metaData = parseTechniqueMetaData(option->value.nodePtr);
- // Skip all techniques except the one we're parsing
- if(metaData.name != name && !metaData.isMixin)
- continue;
- techniqueData.push_back(std::make_pair(option->value.nodePtr, TechniqueData()));
- TechniqueData& data = techniqueData.back().second;
- data.metaData = metaData;
- break;
- }
- default:
- break;
- }
- }
- bool* techniqueWasParsed = bs_stack_alloc<bool>((UINT32)techniqueData.size());
- std::function<bool(const TechniqueMetaData&, TechniqueData&)> parseInherited =
- [&](const TechniqueMetaData& metaData, TechniqueData& outTechnique)
- {
- for (auto riter = metaData.includes.rbegin(); riter != metaData.includes.rend(); ++riter)
- {
- const String& includes = *riter;
- UINT32 baseIdx = -1;
- for(UINT32 i = 0; i < (UINT32)techniqueData.size(); i++)
- {
- auto& entry = techniqueData[i];
- if (!entry.second.metaData.isMixin)
- continue;
- if (entry.second.metaData.name == includes)
- {
- bool matches =
- (entry.second.metaData.language == metaData.language ||
- entry.second.metaData.language == "Any");
- // We want the last matching technique, in order to allow techniques to override each other
- if (matches)
- baseIdx = i;
- }
- }
- if (baseIdx != (UINT32)-1)
- {
- auto& entry = techniqueData[baseIdx];
- // Was already parsed previously, don't parse it multiple times (happens when multiple techniques
- // include the same mixin)
- if (techniqueWasParsed[baseIdx])
- continue;
- if (!parseInherited(entry.second.metaData, outTechnique))
- return false;
- parseTechnique(entry.first, codeBlocks, outTechnique);
- techniqueWasParsed[baseIdx] = true;
-
- }
- else
- {
- output.errorMessage = "Mixin \"" + includes + "\" cannot be found.";
- return false;
- }
- }
- return true;
- };
- // Actually parse techniques
- for (auto& entry : techniqueData)
- {
- const TechniqueMetaData& metaData = entry.second.metaData;
- if (metaData.isMixin)
- continue;
- bs_zero_out(techniqueWasParsed, techniqueData.size());
- if (!parseInherited(metaData, entry.second))
- {
- parseStateDelete(parseState);
- bs_stack_free(techniqueWasParsed);
- return output;
- }
- parseTechnique(entry.first, codeBlocks, entry.second);
- }
- bs_stack_free(techniqueWasParsed);
- IncludeLink* includeLink = parseState->includes;
- while(includeLink != nullptr)
- {
- String includeFilename = includeLink->data->filename;
- auto iterFind = std::find(includes.begin(), includes.end(), includeFilename);
- if (iterFind == includes.end())
- includes.insert(includeFilename);
- includeLink = includeLink->next;
- }
- parseStateDelete(parseState);
- // Parse extended HLSL code and generate per-program code, also convert to GLSL/VKSL
- UINT32 end = (UINT32)techniqueData.size();
- for(UINT32 i = 0; i < end; i++)
- {
- const TechniqueMetaData& metaData = techniqueData[i].second.metaData;
- if (metaData.isMixin)
- continue;
- TechniqueData& hlslTechnique = techniqueData[i].second;
- TechniqueData glslTechnique = techniqueData[i].second;
- // When working with OpenGL, lower-end feature sets are supported. For other backends, high-end is always assumed.
- CrossCompileOutput glslVersion = CrossCompileOutput::GLSL41;
- if(glslTechnique.metaData.featureSet == "HighEnd")
- {
- glslTechnique.metaData.language = "glsl";
- glslVersion = CrossCompileOutput::GLSL45;
- }
- else
- glslTechnique.metaData.language = "glsl4_1";
- TechniqueData vkslTechnique = techniqueData[i].second;
- vkslTechnique.metaData.language = "vksl";
- UINT32 numPasses = (UINT32)hlslTechnique.passes.size();
- for(UINT32 j = 0; j < numPasses; j++)
- {
- PassData& hlslPassData = hlslTechnique.passes[j];
- PassData& glslPassData = glslTechnique.passes[j];
- PassData& vkslPassData = vkslTechnique.passes[j];
- // Clean non-standard HLSL
- static const std::regex regex("\\[\\s*layout\\s*\\(.*\\)\\s*\\]|\\[\\s*internal\\s*\\]|\\[\\s*color\\s*\\]|\\[\\s*alias\\s*\\(.*\\)\\s*\\]");
- hlslPassData.code = regex_replace(hlslPassData.code, regex, "");
- // Find valid entry points and parameters
- // Note: XShaderCompiler needs to do a full pass when doing reflection, and for each individual program
- // type. If performance is ever important here it could be good to update XShaderCompiler so it can
- // somehow save the AST and then re-use it for multiple actions.
- Vector<GpuProgramType> types;
- reflectHLSL(glslPassData.code, shaderDesc, types);
- UINT32 glslBinding = 0;
- UINT32 vkslBinding = 0;
- // Cross-compile for all detected shader types
- // Note: I'm just copying HLSL code as-is. This code will contain all entry points which could have
- // an effect on compile time. It would be ideal to remove dead code depending on program type. This would
- // involve adding a HLSL code generator to XShaderCompiler.
- for(auto& type : types)
- {
- switch(type)
- {
- case GPT_VERTEX_PROGRAM:
- hlslPassData.vertexCode = hlslPassData.code;
- glslPassData.vertexCode = HLSLtoGLSL(glslPassData.code, GPT_VERTEX_PROGRAM,
- glslVersion, glslBinding);
- vkslPassData.vertexCode = HLSLtoGLSL(glslPassData.code, GPT_VERTEX_PROGRAM,
- CrossCompileOutput::VKSL45, vkslBinding);
- break;
- case GPT_FRAGMENT_PROGRAM:
- hlslPassData.fragmentCode = hlslPassData.code;
- glslPassData.fragmentCode = HLSLtoGLSL(glslPassData.code, GPT_FRAGMENT_PROGRAM,
- glslVersion, glslBinding);
- vkslPassData.fragmentCode = HLSLtoGLSL(glslPassData.code, GPT_FRAGMENT_PROGRAM,
- CrossCompileOutput::VKSL45, vkslBinding);
- break;
- case GPT_GEOMETRY_PROGRAM:
- hlslPassData.geometryCode = hlslPassData.code;
- glslPassData.geometryCode = HLSLtoGLSL(glslPassData.code, GPT_GEOMETRY_PROGRAM,
- glslVersion, glslBinding);
- vkslPassData.geometryCode = HLSLtoGLSL(glslPassData.code, GPT_GEOMETRY_PROGRAM,
- CrossCompileOutput::VKSL45, vkslBinding);
- break;
- case GPT_HULL_PROGRAM:
- hlslPassData.hullCode = hlslPassData.code;
- glslPassData.hullCode = HLSLtoGLSL(glslPassData.code, GPT_HULL_PROGRAM,
- glslVersion, glslBinding);
- vkslPassData.hullCode = HLSLtoGLSL(glslPassData.code, GPT_HULL_PROGRAM,
- CrossCompileOutput::VKSL45, vkslBinding);
- break;
- case GPT_DOMAIN_PROGRAM:
- hlslPassData.domainCode = hlslPassData.code;
- glslPassData.domainCode = HLSLtoGLSL(glslPassData.code, GPT_DOMAIN_PROGRAM,
- glslVersion, glslBinding);
- vkslPassData.domainCode = HLSLtoGLSL(glslPassData.code, GPT_DOMAIN_PROGRAM,
- CrossCompileOutput::VKSL45, vkslBinding);
- break;
- case GPT_COMPUTE_PROGRAM:
- hlslPassData.computeCode = hlslPassData.code;
- glslPassData.computeCode = HLSLtoGLSL(glslPassData.code, GPT_COMPUTE_PROGRAM,
- glslVersion, glslBinding);
- vkslPassData.computeCode = HLSLtoGLSL(glslPassData.code, GPT_COMPUTE_PROGRAM,
- CrossCompileOutput::VKSL45, vkslBinding);
- break;
- default:
- break;
- }
- }
- }
- techniqueData.push_back(std::make_pair(nullptr, glslTechnique));
- techniqueData.push_back(std::make_pair(nullptr, vkslTechnique));
- }
- for(auto& entry : techniqueData)
- {
- const TechniqueMetaData& metaData = entry.second.metaData;
- if (metaData.isMixin)
- continue;
- Map<UINT32, SPtr<Pass>, std::greater<UINT32>> passes;
- for (auto& passData : entry.second.passes)
- {
- PASS_DESC passDesc;
- if (!passData.blendIsDefault)
- passDesc.blendState = BlendState::create(passData.blendDesc);
- if (!passData.rasterizerIsDefault)
- passDesc.rasterizerState = RasterizerState::create(passData.rasterizerDesc);
- if (!passData.depthStencilIsDefault)
- passDesc.depthStencilState = DepthStencilState::create(passData.depthStencilDesc);
- auto createProgram =
- [](const String& language, const String& entry, const String& code, GpuProgramType type) -> SPtr<GpuProgram>
- {
- if (code.empty())
- return nullptr;
- GPU_PROGRAM_DESC desc;
- desc.language = language;
- desc.entryPoint = entry;
- desc.source = code;
- desc.type = type;
- return GpuProgram::create(desc);
- };
- bool isHLSL = metaData.language == "hlsl";
- passDesc.vertexProgram = createProgram(
- metaData.language,
- isHLSL ? "vsmain" : "main",
- passData.vertexCode,
- GPT_VERTEX_PROGRAM);
- passDesc.fragmentProgram = createProgram(
- metaData.language,
- isHLSL ? "fsmain" : "main",
- passData.fragmentCode,
- GPT_FRAGMENT_PROGRAM);
- passDesc.geometryProgram = createProgram(
- metaData.language,
- isHLSL ? "gsmain" : "main",
- passData.geometryCode,
- GPT_GEOMETRY_PROGRAM);
- passDesc.hullProgram = createProgram(
- metaData.language,
- isHLSL ? "hsmain" : "main",
- passData.hullCode,
- GPT_HULL_PROGRAM);
- passDesc.domainProgram = createProgram(
- metaData.language,
- isHLSL ? "dsmain" : "main",
- passData.domainCode,
- GPT_DOMAIN_PROGRAM);
- passDesc.computeProgram = createProgram(
- metaData.language,
- isHLSL ? "csmain" : "main",
- passData.computeCode,
- GPT_COMPUTE_PROGRAM);
- passDesc.stencilRefValue = passData.stencilRefValue;
- SPtr<Pass> pass = Pass::create(passDesc);
- if (pass != nullptr)
- passes[passData.seqIdx] = pass;
- }
- Vector<SPtr<Pass>> orderedPasses;
- for (auto& KVP : passes)
- orderedPasses.push_back(KVP.second);
- if (orderedPasses.size() > 0)
- {
- SPtr<Technique> technique = Technique::create(metaData.language, metaData.tags, variation, orderedPasses);
- techniques.push_back(technique);
- }
- }
- return output;
- }
- String BSLFXCompiler::removeQuotes(const char* input)
- {
- UINT32 len = (UINT32)strlen(input);
- String output(len - 2, ' ');
- for (UINT32 i = 0; i < (len - 2); i++)
- output[i] = input[i + 1];
- return output;
- }
- }
|