//============================================================
// <T>转换脚本。</T>
//
// @param pOutputScript 输出脚本
// @param pInputScript 输入脚本
// @return 处理结果
//============================================================
TResult FPoRenderShaderTransformer::Convert(MString* pOutputScript, MString* pInputScript){
   Hlsl2Glsl_Initialize();
   ShHandle parser = Hlsl2Glsl_ConstructCompiler(EShLangVertex);
   TInt parseOk = Hlsl2Glsl_Parse(parser, pInputScript->MemoryC(), ETargetGLSL_120, ETranslateOpIntermediate);
	TCharC* pInfo = Hlsl2Glsl_GetInfoLog(parser);
   if(parseOk){
   	TInt translateOk = Hlsl2Glsl_Translate(parser, "main", ETargetGLSL_120, ETranslateOpIntermediate);
		TCharC* infoLog = Hlsl2Glsl_GetInfoLog( parser );
		if(translateOk){
      	TCharC* pGlslScript = Hlsl2Glsl_GetShader(parser);
         pOutputScript->Assign(pGlslScript);
      }
   }
   Hlsl2Glsl_DestructCompiler(parser);
	Hlsl2Glsl_Shutdown();
   return ESuccess;
}
Example #2
0
int main (int argc, const char** argv)
{
	if (argc < 2)
	{
		printf ("USAGE: hlsl2glsltest testfolder\n");
		return 1;
	}

	bool hasOpenGL = InitializeOpenGL ();
	if (!hasOpenGL)
		printf("NOTE: will not check GLSL with actual driver (no GL/GLSL)\n");
	
	clock_t time0 = clock();
	
	Hlsl2Glsl_Initialize ();

	std::string baseFolder = argv[1];

	size_t tests = 0;
	size_t errors = 0;
	for (int type = 0; type < NUM_RUN_TYPES; ++type)
	{
		printf ("TESTING %s...\n", kTypeName[type]);
		const ETargetVersion version1 = kTargets1[type];
		const ETargetVersion version2 = kTargets2[type];
		const ETargetVersion version3 = kTargets3[type];
		std::string testFolder = baseFolder + "/" + kTypeName[type];
		StringVector inputFiles = GetFiles (testFolder, "-in.txt");

		size_t n = inputFiles.size();
		tests += n;
		for (size_t i = 0; i < n; ++i)
		{
			std::string inname = inputFiles[i];
			//if (inname != "_zzz-in.txt")
			//	continue;
			const bool preprocessorTest = (inname.find("pp-") == 0);
			bool ok = true;
			
			printf ("test %s\n", inname.c_str());
			if (type == BOTH) {
				ok = TestCombinedFile(testFolder + "/" + inname, version1, hasOpenGL);
				if (ok && version2 != ETargetVersionCount)
					ok = TestCombinedFile(testFolder + "/" + inname, version2, hasOpenGL);
			} else {
				ok = TestFile(TestRun(type), testFolder + "/" + inname, version1, 0, hasOpenGL);
				if (!preprocessorTest)
				{
					if (ok && version2 != ETargetVersionCount)
						ok = TestFile(TestRun(type), testFolder + "/" + inname, version2, ETranslateOpEmitGLSL120ArrayInitWorkaround, hasOpenGL);
					if (ok && version3 != ETargetVersionCount)
						ok = TestFile(TestRun(type), testFolder + "/" + inname, version3, 0, hasOpenGL);
				}
			}
			
			if (!ok)
				++errors;
		}		
	}

	clock_t time1 = clock();
	float t = float(time1-time0) / float(CLOCKS_PER_SEC);
	if (errors != 0)
		printf ("%i tests, %i FAILED, %.2fs\n", (int)tests, (int)errors, t);
	else
		printf ("%i tests succeeded, %.2fs\n", (int)tests, t);
	
	Hlsl2Glsl_Shutdown();
	CleanupOpenGL();

	return errors ? 1 : 0;
}
 //-----------------------------------------------------------------------
 GLSLESCgProgramFactory::GLSLESCgProgramFactory()
 {
         Hlsl2Glsl_Initialize();
 }
int main (int argc, const char** argv)
{
	if (argc < 2)
	{
		printf ("USAGE: hlsl2glsltest testfolder\n");
		return 1;
	}

	bool hasOpenGL = InitializeOpenGL ();
	
	clock_t time0 = clock();
	
	Hlsl2Glsl_Initialize ();

	std::string baseFolder = argv[1];

	static const char* kTypeName[2] = { "vertex", "fragment" };
	size_t tests = 0;
	size_t errors = 0;
	for (int type = 0; type < 2; ++type)
	{
		printf ("testing %s...\n", kTypeName[type]);
		std::string testFolder = baseFolder + "/" + kTypeName[type];
		StringVector inputFiles = GetFiles (testFolder, "-in.txt");

		size_t n = inputFiles.size();
		tests += n;
		for (size_t i = 0; i < n; ++i)
		{
			std::string inname = inputFiles[i];
			printf ("test %s\n", inname.c_str());
			std::string outname = inname.substr (0,inname.size()-7) + "-out.txt";
			std::string outnameES = inname.substr (0,inname.size()-7) + "-outES.txt";
			bool ok = TestFile (type==0,
				testFolder + "/" + inname,
				testFolder + "/" + outname,
				false,
				hasOpenGL);
			if (ok)
			{
				ok = TestFile (type==0,
					testFolder + "/" + inname,
					testFolder + "/" + outnameES,
					true,
					false);
			}
			if (!ok)
			{
				++errors;
			}
		}
	}
	clock_t time1 = clock();
	float t = float(time1-time0) / float(CLOCKS_PER_SEC);
	if (errors != 0)
		printf ("%i tests, %i FAILED, %.2fs\n", tests, errors, t);
	else
		printf ("%i tests succeeded, %.2fs\n", tests, t);

	return errors ? 1 : 0;
}
Example #5
0
int C_DECL main(int argc, char* argv[])
{
   bool parseFailed = false;
   bool translateFailed = false;
   int debugOptions = 0;
	 std::map<EShLanguage, std::string> Entrys;
	 std::map<EShLanguage, ShHandle> parsers;
   std::map<EShLanguage, std::string> parsersIns;
	 std::map<EShLanguage, std::string> Outs;
   char *uOut = 0;
   char *shaderHeader = 0;

   //ShHandle    translator = 0;
   //ShHandle    uniformMap = 0;
   bool        bUseUserVaryings = false;
   int             attribSemanticCount = 0;
   EAttribSemantic attribSemanticEnums[EAttrSemCount];
   char*           attribSemanticNames[EAttrSemCount];

	for ( int i = 0; i < EAttrSemCount; i++ )
	{
		attribSemanticEnums[i] = EAttrSemUnknown;
		attribSemanticNames[i] = NULL;
	};

	Hlsl2Glsl_Initialize();
	argc--;
	argv++;    
   
	for (; argc >= 1; argc--, argv++)
	{
		if (argv[0][0] == '-' || argv[0][0] == '/')
		{
			switch (argv[0][1])
			{
				case 'f': Entrys.insert(std::pair<EShLanguage, std::string>(EShLangFragment, (++argv)[0])); argc--;             break;
				case 'v': Entrys.insert(std::pair<EShLanguage, std::string>(EShLangVertex, (++argv)[0])); argc--;             break;
				case 'o':
					if (strlen(argv[0]) > 2)
					{
                switch (argv[0][2])
                {
                case 'f':
										Outs.insert(std::pair<EShLanguage, std::string>(EShLangFragment, (++argv)[0]));
                    argc--;
                    break;
                case 'v':
										Outs.insert(std::pair<EShLanguage, std::string>(EShLangVertex, (++argv)[0]));
                    argc--;
                    break;
                case 'u':
                    uOut = (++argv)[0]; argc--;
                    break;
                default: 
                    usage();
                    return EFailUsage;
                    break;
                }
					}
					break;

				case 'i':
					if (strlen(argv[0]) > 2)
					{
						EShLanguage language = EShLangFragment;

						switch (argv[0][2])
						{
						case 'f':
								language = EShLangFragment;
								break;
						case 'v':
								language = EShLangVertex;
								break;
						default: 
								usage();
								return EFailUsage;
								break;
						};

						ShHandle l_parser = Hlsl2Glsl_ConstructCompiler(language);

						if (l_parser == 0)
								return EFailParserCreate;

						argv++;
						argc--;

						parsers.insert(std::pair<EShLanguage, ShHandle>(language, l_parser));
						parsersIns.insert(std::pair<EShLanguage, std::string>(language, argv[0]));
					}
					break;

				case 'u':
          bUseUserVaryings = true;
          break;

				case 'a':
					{
						char *configFileName = (++argv)[0];
						argc--;

						// Load a configuration file specifying user attributes
						if ( !LoadUserAttribConfigFile ( configFileName, attribSemanticEnums, attribSemanticNames, &attribSemanticCount ) )
						{
								printf ( "Error loading attribute configuration file '%s'.\n", configFileName );
								return EFailUsage;
						}                  
					}
					break;

				case 'h':
              {
                char *shaderHeaderName = (++argv)[0];
                argc--;

                shaderHeader = ReadFileData ( shaderHeaderName );
              }
              break;

				default:  
					usage();                       
					return EFailUsage;
			}
		}
		else
		{
					EShLanguage language = FindLanguage(argv[0]);
					ShHandle l_parser = Hlsl2Glsl_ConstructCompiler(language);

          if (l_parser == 0)
              return EFailParserCreate;

					parsers.insert(std::pair<EShLanguage, ShHandle>(language, l_parser));
					parsersIns.insert(std::pair<EShLanguage, std::string>(language, argv[0]));
		}
	}

	if (parsers.size() == 0)
	{
		usage();
		return EFailUsage;
	}

/*
    translator = Hlsl2Glsl_ConstructTranslator(debugOptions);
    if (translator == 0)
        return EFailTranslatorCreate;
*/
	if ( bUseUserVaryings )
	{
		// Set the translator up to use user varyings
		for(std::map<EShLanguage, ShHandle>::iterator it = parsers.begin(); it != parsers.end(); ++it)
		{
			Hlsl2Glsl_UseUserVaryings (it->second, bUseUserVaryings );
		};
	};

/*
	if ( shaderHeader != NULL )
	{
		// Set a shader header if requested
		Hlsl2Glsl_SetShaderHeader ( translator, true, shaderHeader );
	}
*/

	if ( attribSemanticCount > 0 )
	{
			for(std::map<EShLanguage, ShHandle>::iterator it = parsers.begin(); it != parsers.end(); ++it)
			{
				// Use user attributes if the user specified user attribute configuration file
				if ( !Hlsl2Glsl_SetUserAttributeNames (it->second, attribSemanticEnums, (const char**)&attribSemanticNames[0], attribSemanticCount ) )
				{
						printf ("ERROR: Setting user attribute names\n");                        
				};
			};
	};


	if (parsers.size() > 0)
	{
		int l_i = -1;

		for(std::map<EShLanguage, ShHandle>::iterator it = parsers.begin(); it != parsers.end(); ++it)
		{
			EShLanguage l_language = it->first;
			ShHandle l_parser = it->second;

			std::string l_parsedfile = parsersIns[l_language];
			l_i += 1;

			if(ParseFile(l_parsedfile.c_str(), l_parser, debugOptions))
			{
				if(Entrys.find(l_language) != Entrys.end())
				{					
					std::string l_entrypointname = Entrys[l_language];

					//TODO
					if (Hlsl2Glsl_Translate(l_parser, l_entrypointname.c_str() , ETargetGLSL_120, 0))
					{
						if(Outs.find(l_language) != Outs.end())
						{
							SaveShader(Outs[l_language].c_str(), l_parser, shaderHeader);
						};

						if (uOut)
							SaveUniforms(uOut, l_parser);
					}
					else
					{
						translateFailed = true;
					};
				}
			}
			else
			{
				parseFailed = true;
			};

			InfoLogMsg("BEGIN", "COMPILER", l_i);
			puts(Hlsl2Glsl_GetInfoLog(l_parser));
			InfoLogMsg("END", "COMPILER", l_i);
		};
	};
/*
    InfoLogMsg("BEGIN", "LINKER", -1);
    puts(Hlsl2Glsl_GetInfoLog(translator));
    InfoLogMsg("END", "LINKER", -1);
    if (!translateFailed )
    {
        if (fOut)
          SaveShader( fOut, translator, EShLangFragment);
        if (vOut)
          SaveShader( vOut, translator, EShLangVertex);
        if (uOut)
          SaveUniforms( uOut, translator);
    }
*/

	for(std::map<EShLanguage, ShHandle>::iterator it = parsers.begin(); it != parsers.end(); ++it)
	{
		Hlsl2Glsl_DestructCompiler(it->second);
	};

//      Hlsl2Glsl_Destruct(translator);
//      Hlsl2Glsl_Destruct(uniformMap);

	for ( int i = 0; i < EAttrSemCount; i++ )
	{
		if ( attribSemanticNames[i] != NULL )
		{
			delete[] attribSemanticNames[i];
			attribSemanticNames[i] = NULL;
		};
	};

	if ( shaderHeader )
	{
		FreeFileData ( shaderHeader );
	};

	if (parseFailed)
		return EFailParse;

	if (translateFailed)
		return EFailTranslate;

	return 0;
}