Example #1
0
static inline
void AddCustomToken(GLToken* TokenHash, const char* Value)
{
  GLToken Token;
  GLString String;
  String.Chars = (char*)Value;
  String.Length = (unsigned int)strlen(Value);
  Token.Hash = GetStringHash(String);
  AddToken(TokenHash, Token);
}
Example #2
0
HRESULT GetDLParentDir(LPWSTR pszURL, LPWSTR pszParentDir)
{
    if(!pszURL)
        return S_FALSE;

    DWORD dwHash = GetStringHash(pszURL);
    // Convert to unicode.
    CParseUtils::BinToUnicodeHex( (PBYTE) &dwHash, sizeof(DWORD), pszParentDir);
    pszParentDir[DWORD_STRING_LEN] = L'\0';

    return S_OK;
}
Example #3
0
//----------------------------------------------------------------//
const USHashedString::USHashedStringEntry& USHashedString::GetHashedStringEntry ( cc8* name ) {

	static STLMap < u32, USHashedStringEntry* > gStringDictonary;

	size_t nameLength = name ? strlen ( name ) : 0;
	u32 nameHash = GetStringHash ( name, nameLength );

	USHashedStringEntry* entry = gStringDictonary.value_for_key ( nameHash );
	if ( entry == 0 ) {

		// Create a new hashed string entry
		entry = new USHashedStringEntry ( name, nameLength, nameHash );
		gStringDictonary [ nameHash ] = entry;
	}

	return *entry;
}
Example #4
0
static inline
GLToken ParseToken(GLTokenizer* Tokenizer)
{
  GLToken Token = {};
  while(*Tokenizer->At && (IsWhitespaceOrNewline(*Tokenizer->At) || !IsIdentifier(*Tokenizer->At)))
  {
    Tokenizer->At++;
  }
  Token.Value.Chars = Tokenizer->At;
  while(*Tokenizer->At && IsIdentifier(*Tokenizer->At))
  {
    Tokenizer->At++;
  }
  Token.Value.Length = (unsigned int)(Tokenizer->At - Token.Value.Chars);
  Token.Hash = GetStringHash(Token.Value);
  return Token;
}
Example #5
0
template <> StringHash Variant::Get<StringHash>() const
{
    return GetStringHash();
}
Example #6
0
static
int GenerateOpenGLHeader(GLSettings* Settings)
{
  char* ArbData = ReadMultiFiles(Settings->HeadersStart, Settings->HeadersEnd);
  FILE* Output = fopen(Settings->Output, "w");
  const char* ProcPrefix = "GEN_";
  int Success = -1;

  if (Settings->InputCount <= 0)
  {
    fprintf(stderr, "Invalid input count");
  }
  else if (Output && ArbData)
  {
    GLArbToken* ArbHash = (GLArbToken*)calloc(sizeof(GLArbToken), TOKEN_HASH_SIZE);
    GLToken* FunctionsHash = (GLToken*)calloc(sizeof(GLToken), TOKEN_HASH_SIZE);
    GLToken* DefinesHash = (GLToken*)calloc(sizeof(GLToken), TOKEN_HASH_SIZE);
    unsigned int FunctionCount = 0;
    unsigned int DefinesCount = 0;
    unsigned int ArbTokenCount = 0;

    GLTokenizer Tokenizer;
    Tokenizer.At = ArbData;
    while(*Tokenizer.At)
    {
      GLArbToken Token = ParseArbToken(&Tokenizer);
      if (Equal(Token.Value, "GLAPI"))
      {
        char* Start = Token.Value.Chars;
        char* ReturnType = Tokenizer.At;
        Token = ParseArbToken(&Tokenizer);
        if (Equal(Token.Value, "const"))
        {
          ParseArbToken(&Tokenizer);
        }
        unsigned int ReturnTypeLength = (unsigned int)(Tokenizer.At - ReturnType);
        ParseArbToken(&Tokenizer);
        Token = ParseArbToken(&Tokenizer);
        Token.Hash = GetStringHash(Token.Value);
        if (!GetToken(ArbHash, Token.Hash))
        {
          ArbTokenCount++;
          GLArbToken* Result = AddToken(ArbHash, Token);
          Result->FunctionName.Chars = Token.Value.Chars;
          Result->FunctionName.Length = (unsigned int)(Tokenizer.At - Token.Value.Chars);

          Result->ReturnType.Chars = ReturnType;
          Result->ReturnType.Length = ReturnTypeLength;

          Result->Parameters.Chars = Tokenizer.At;
          AdvanceToEndOfLine(&Tokenizer);
          Result->Parameters.Length = (unsigned int)(Tokenizer.At - Result->Parameters.Chars);

          Result->Line.Chars = Start;
          Result->Line.Length = (unsigned int)(Tokenizer.At - Start);
        }
      }
      else if (StartsWith(Token.Value, "#define"))
      {
        char* Start = Token.Value.Chars;
        Token = ParseArbToken(&Tokenizer);
        Token.Hash = GetStringHash(Token.Value);
        if (!GetToken(ArbHash, Token.Hash))
        {
          ArbTokenCount++;
          GLArbToken* Result = AddToken(ArbHash, Token);
          AdvanceToEndOfLine(&Tokenizer);
          Result->Line.Chars = Start;
          Result->Line.Length = (unsigned int)(Tokenizer.At - Start);
        }
      }
    }

    {
      DefinesCount = 2;
      AddCustomToken(DefinesHash, "GL_MAJOR_VERSION");
      AddCustomToken(DefinesHash, "GL_MINOR_VERSION");
    }
    {
      FunctionCount = 1;
      AddCustomToken(FunctionsHash, "glGetIntegerv");
    }

    for (int Index = 0; Index < Settings->InputCount; ++Index)
    {
      ParseFile(Settings->Inputs[Index], ArbHash, FunctionsHash, &FunctionCount,
                DefinesHash, &DefinesCount, Settings);
    }

    qsort(FunctionsHash, TOKEN_HASH_SIZE, sizeof(GLToken), TokenComparer);
    qsort(DefinesHash, TOKEN_HASH_SIZE, sizeof(GLToken), TokenComparer);

    {
      const char* Prefix = "";
      if (Settings->Prefix)
      {
        Prefix = (const char*)Settings->Prefix;
      }
      //NOTE: Create defines
      const char* Generated =
        "#ifndef INCLUDE_OPENGL_GENERATED_H\n"
        "#define INCLUDE_OPENGL_GENERATED_H\n\n"
        "// NOTE: This file is generated automatically. Do not edit.\n"
        "// @GENERATED: %llu\n\n";
      fprintf(Output, Generated, Settings->WriteTimestamp);

      Generated =
        "typedef struct %sOpenGLVersion\n"
        "{\n"
        "  int Major;\n"
        "  int Minor;\n"
        "} %sOpenGLVersion;\n"
        "// Call this function to initialize OpenGL.\n"
        "// Example:\n"
        "//\n"
        "//    %sOpenGLVersion Version;\n"
        "//    %sOpenGLInit(&Version);\n"
        "//    if(Version.Major < 3)\n"
        "//    {\n"
        "//       printf(\"OpenGL 3 or above required.\\n\");\n"
        "//       return 0;\n"
        "//    }\n"
        "//\n"
        "static void %sOpenGLInit(%sOpenGLVersion* Version);\n\n\n";
      if (Settings->Boilerplate)
      {
        fprintf(Output, Generated, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix);
      }

      Generated =
        "#ifndef APIENTRY\n"
        "#define APIENTRY\n"
        "#endif\n"
        "#ifndef APIENTRYP\n"
        "#define APIENTRYP APIENTRY *\n"
        "#endif\n"
        "#ifndef GLAPI\n"
        "#define GLAPI extern\n"
        "#endif\n\n"

        "typedef void GLvoid;\n"
        "typedef unsigned int GLenum;\n"
        "typedef float GLfloat;\n"
        "typedef int GLint;\n"
        "typedef int GLsizei;\n"
        "typedef unsigned int GLbitfield;\n"
        "typedef double GLdouble;\n"
        "typedef unsigned int GLuint;\n"
        "typedef unsigned char GLboolean;\n"
        "typedef unsigned char GLubyte;\n"
        "typedef char GLchar;\n"
        "typedef short GLshort;\n"
        "typedef signed char GLbyte;\n"
        "typedef unsigned short GLushort;\n"
        "typedef ptrdiff_t GLsizeiptr;\n"
        "typedef ptrdiff_t GLintptr;\n"
        "typedef float GLclampf;\n"
        "typedef double GLclampd;\n"
        "typedef unsigned short GLhalf;\n\n";


      fprintf(Output, "%s", Generated);


      for (unsigned int Index = 0; Index < DefinesCount; ++Index)
      {
        GLToken* Token = DefinesHash + Index;
        assert(Token->Hash);
        GLArbToken* ArbToken = GetToken(ArbHash, Token->Hash);
        if (ArbToken)
        {
          fwrite(ArbToken->Line.Chars, ArbToken->Line.Length + 1, 1, Output);
        }
      }
      const char* Spacer = "\n\n";
      fwrite(Spacer, strlen(Spacer), 1, Output);
      Generated =
        "typedef void (APIENTRY *GLDEBUGPROC)(GLenum source,GLenum type,GLuint id,GLenum severity,GLsizei length,const GLchar *message,const void *userParam);\n";
      fprintf(Output, "%s", Generated);

      for (unsigned int Index = 0; Index < FunctionCount; ++Index)
      {
        GLToken* Token = FunctionsHash + Index;
        GLArbToken* ArbToken = GetToken(ArbHash, Token->Hash);
        if (ArbToken)
        {
          char Name[512];
          UpperCase(Name, ArbToken->FunctionName);
          char Buffer[512];
          int Length = sprintf(Buffer, "typedef %" PRI_STR " (APIENTRYP PFN%sPROC) %" PRI_STR "\n",
                               ArbToken->ReturnType.Length, ArbToken->ReturnType.Chars,
                               Name,
                               ArbToken->Parameters.Length, ArbToken->Parameters.Chars);

          fwrite(Buffer, (size_t)Length, 1, Output);
        }
      }
      if (Settings->Boilerplate)
      {
        fwrite(Spacer, strlen(Spacer), 1, Output);
        for (unsigned int Index = 0; Index < FunctionCount; ++Index)
        {
          GLToken* Token = FunctionsHash + Index;
          GLArbToken* ArbToken = GetToken(ArbHash, Token->Hash);
          if (ArbToken)
          {
            char Buffer[512];
            int Length = sprintf(Buffer, "#define %" PRI_STR " %s%" PRI_STR "\n",
                                 ArbToken->FunctionName.Length, ArbToken->FunctionName.Chars,
                                 ProcPrefix,
                                 ArbToken->FunctionName.Length, ArbToken->FunctionName.Chars);


            fwrite(Buffer, (size_t)Length, 1, Output);
          }
        }

        fwrite(Spacer, strlen(Spacer), 1, Output);
        for (unsigned int Index = 0; Index < FunctionCount; ++Index)
        {
          GLToken* Token = FunctionsHash + Index;
          GLArbToken* ArbToken = GetToken(ArbHash, Token->Hash);
          if (ArbToken)
          {
            char Name[512];
            UpperCase(Name, ArbToken->FunctionName);
            char Buffer[512];
            int Length = sprintf(Buffer, "PFN%sPROC %s%" PRI_STR ";\n", Name, ProcPrefix,
                                 ArbToken->FunctionName.Length, ArbToken->FunctionName.Chars);

            fwrite(Buffer, (size_t)Length, 1, Output);
          }
        }

        Generated =
          "\n\n"
          "typedef void (*%sOpenGLProc)(void);\n\n"
          "#ifdef _WIN32\n"
          "static HMODULE %sOpenGLHandle;\n"
          "static void %sLoadOpenGL()\n"
          "{\n"
          "  %sOpenGLHandle = LoadLibraryA(\"opengl32.dll\");\n"
          "}\n"
          "static void %sUnloadOpenGL()\n"
          "{\n"
          "  FreeLibrary(%sOpenGLHandle);\n"
          "}\n"
          "static %sOpenGLProc %sOpenGLGetProc(const char *proc)\n"
          "{\n"
          "  %sOpenGLProc Result = (%sOpenGLProc)wglGetProcAddress(proc);\n"
          "  if (!Result)\n"
          "    Result = (%sOpenGLProc)GetProcAddress(%sOpenGLHandle, proc);\n"
          "  return Result;\n"
          "}\n"
          "#elif defined(__APPLE__) || defined(__APPLE_CC__)\n"
          "#include <Carbon/Carbon.h>\n"
          "\n"
          "static CFBundleRef GEN_Bundle;\n"
          "static CFURLRef GEN_BundleURL;\n"
          "\n"
          "static void %sLoadOpenGL()\n"
          "{\n"
          "  GEN_BundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,\n"
          "    CFSTR(\"/System/Library/Frameworks/OpenGL.framework\"),\n"
          "    kCFURLPOSIXPathStyle, 1);\n"
          "  GEN_Bundle = CFBundleCreate(kCFAllocatorDefault, GEN_BundleURL);\n"
          "}\n"
          "static void %sUnloadOpenGL()\n"
          "{\n"
          "  CFRelease(GEN_Bundle);\n"
          "  CFRelease(GEN_BundleURL);\n"
          "}\n"
          "static %sOpenGLProc %sOpenGLGetProc(const char *proc)\n"
          "{\n"
          "  CFStringRef ProcName = CFStringCreateWithCString(kCFAllocatorDefault, proc,\n"
          "    kCFStringEncodingASCII);\n"
          "  %sOpenGLProc Result = (%sOpenGLProc) CFBundleGetFunctionPointerForName(GEN_Bundle, ProcName);\n"
          "  CFRelease(ProcName);\n"
          "  return Result;\n"
          "}\n"
          "#else\n"
          "#include <dlfcn.h>\n"
          "\n"
          "static void *%sOpenGLHandle;\n"
          "typedef void (*__GLXextproc)(void);\n"
          "typedef __GLXextproc (* PFNGLXGETPROCADDRESSPROC) (const GLubyte *procName);\n"
          "static PFNGLXGETPROCADDRESSPROC glx_get_proc_address;\n"
          "static void %sLoadOpenGL()\n"
          "{\n"
          "  %sOpenGLHandle = dlopen(\"libGL.so.1\", RTLD_LAZY | RTLD_GLOBAL);\n"
          "  glx_get_proc_address = (PFNGLXGETPROCADDRESSPROC) dlsym(%sOpenGLHandle, \"glXGetProcAddressARB\");\n"
          "}\n"
          "static void %sUnloadOpenGL()\n"
          "{\n"
          "  dlclose(%sOpenGLHandle);\n"
          "}\n"
          "static %sOpenGLProc %sOpenGLGetProc(const char *proc)\n"
          "{\n"
          "  %sOpenGLProc Result = (%sOpenGLProc) glx_get_proc_address((const GLubyte *) proc);\n"
          "  if (!Result)\n"
          "    Result = (%sOpenGLProc) dlsym(%sOpenGLHandle, proc);\n"
          "  return Result;\n"
          "}\n"
          "#endif\n\n";
        fprintf(Output, Generated,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix,
                Prefix, Prefix, Prefix, Prefix, Prefix, Prefix, Prefix);
        Generated =
          "\n\nvoid %sOpenGLInit(%sOpenGLVersion* Version)\n"
          "{\n"
          "  %sLoadOpenGL();\n\n";
        fprintf(Output, Generated, Prefix, Prefix, Prefix);
        for (unsigned int Index = 0; Index < FunctionCount; ++Index)
        {
          GLToken* Token = FunctionsHash + Index;
          GLArbToken* ArbToken = GetToken(ArbHash, Token->Hash);
          if (ArbToken)
          {
            char Name[512];
            UpperCase(Name, ArbToken->FunctionName);
            char Buffer[512];
            int Length = sprintf(Buffer, "  %s%" PRI_STR " = (PFN%sPROC)%sOpenGLGetProc(\"%" PRI_STR "\");\n",
                                 ProcPrefix,
                                 ArbToken->FunctionName.Length, ArbToken->FunctionName.Chars,
                                 Name, Prefix, ArbToken->FunctionName.Length, ArbToken->FunctionName.Chars);

            fwrite(Buffer, (size_t)Length, 1, Output);
          }
        }
        Generated =
          "\n  %sUnloadOpenGL();\n"
          "\n"
          "  Version->Major = 0;\n"
          "  Version->Minor = 0;\n"
          "  if (glGetIntegerv)\n"
          "  {\n"
          "    glGetIntegerv(GL_MAJOR_VERSION, &Version->Major);\n"
          "    glGetIntegerv(GL_MINOR_VERSION, &Version->Minor);\n"
          "  }\n"
          "}\n\n"
          "#endif // INCLUDE_OPENGL_GENERATED_H\n";
        fprintf(Output, Generated, Prefix);
      }
      Success = 0;
      if (!Settings->Silent)
      {
        printf(GREEN("Completed!") " " GREEN("%u") " functions - " GREEN("%u") " defines - " GREEN("%u") " ARB tokens\n",
               FunctionCount, DefinesCount, ArbTokenCount);
      }
    }
  }

  if (Output)
  {
    fclose(Output);
  }

  if (ArbData)
  {
    free(ArbData);
  }

  return Success;
}