Hash :
e4249f02
Author :
Date :
2010-07-26T18:13:52
Refactored the way symbol tables are initialized and stored. This was done in response to the addition of EShSpec. Symbol table entries depend on three things - language, spec (not now but may eventually), and built-in resources. We used to build two global symbol-tables - one for each language. During each compile, one of the symbol table was copied and resource-specific stuff was added. I have moved the symbol table to TCompiler that gets initilized when compiler is created and reused for each compile. This makes it much cleaner and extensible in case a spec requires special entries to be added to the symbol table. PS: Sorry for the long CL, but all of it needed to be done in one CL. I have verified that everything still compiles and passes all conformance tests. Review URL: http://codereview.appspot.com/1864044 git-svn-id: https://angleproject.googlecode.com/svn/trunk@351 736b8ea6-26fd-11df-bfd4-992fa37f6226
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
//
// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "GLSLANG/ShaderLang.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//
// Return codes from main.
//
enum TFailCode {
ESuccess = 0,
EFailUsage,
EFailCompile,
EFailCompilerCreate,
};
static EShLanguage FindLanguage(char *lang);
bool CompileFile(char *fileName, ShHandle, int);
void usage();
void FreeFileData(char **data);
char** ReadFileData(char *fileName);
void LogMsg(char* msg, const char* name, const int num, const char* logName);
//Added to accomodate the multiple strings.
int OutputMultipleStrings = 1;
//
// Set up the per compile resources
//
void GenerateResources(TBuiltInResource& resources)
{
resources.maxVertexAttribs = 8;
resources.maxVertexUniformVectors = 128;
resources.maxVaryingVectors = 8;
resources.maxVertexTextureImageUnits = 0;
resources.maxCombinedTextureImageUnits = 8;
resources.maxTextureImageUnits = 8;
resources.maxFragmentUniformVectors = 16;
resources.maxDrawBuffers = 1;
}
int main(int argc, char* argv[])
{
TFailCode failCode = ESuccess;
int debugOptions = 0;
bool writeObjectCode = false;
int numCompiles = 0;
ShHandle vertexCompiler = 0;
ShHandle fragmentCompiler = 0;
ShInitialize();
TBuiltInResource resources;
GenerateResources(resources);
argc--;
argv++;
for (; (argc >= 1) && (failCode == ESuccess); argc--, argv++) {
if (argv[0][0] == '-' || argv[0][0] == '/') {
switch (argv[0][1]) {
case 'i': debugOptions |= EDebugOpIntermediate; break;
case 'o': writeObjectCode = true; break;
default: failCode = EFailUsage;
}
} else {
ShHandle compiler = 0;
switch (FindLanguage(argv[0])) {
case EShLangVertex:
if (vertexCompiler == 0)
vertexCompiler = ShConstructCompiler(EShLangVertex, EShSpecGLES2, &resources);
compiler = vertexCompiler;
break;
case EShLangFragment:
if (fragmentCompiler == 0)
fragmentCompiler = ShConstructCompiler(EShLangFragment, EShSpecGLES2, &resources);
compiler = fragmentCompiler;
break;
default: break;
}
if (compiler) {
bool compiled = CompileFile(argv[0], compiler, debugOptions);
LogMsg("BEGIN", "COMPILER", numCompiles, "INFO LOG");
puts(ShGetInfoLog(compiler));
LogMsg("END", "COMPILER", numCompiles, "INFO LOG");
if (compiled && writeObjectCode) {
LogMsg("BEGIN", "COMPILER", numCompiles, "OBJ CODE");
puts(ShGetObjectCode(compiler));
LogMsg("END", "COMPILER", numCompiles, "OBJ CODE");
}
if (!compiled)
failCode = EFailCompile;
++numCompiles;
} else {
failCode = EFailCompilerCreate;
}
}
}
if ((vertexCompiler == 0) && (fragmentCompiler == 0))
failCode = EFailUsage;
if (failCode == EFailUsage)
usage();
if (vertexCompiler)
ShDestruct(vertexCompiler);
if (fragmentCompiler)
ShDestruct(fragmentCompiler);
ShFinalize();
return failCode;
}
//
// Deduce the language from the filename. Files must end in one of the
// following extensions:
//
// .frag* = fragment programs
// .vert* = vertex programs
//
static EShLanguage FindLanguage(char *name)
{
if (!name)
return EShLangFragment;
char *ext = strrchr(name, '.');
if (ext && strcmp(ext, ".sl") == 0)
for (; ext > name && ext[0] != '.'; ext--);
if (ext = strrchr(name, '.')) {
if (strncmp(ext, ".frag", 4) == 0) return EShLangFragment;
if (strncmp(ext, ".vert", 4) == 0) return EShLangVertex;
}
return EShLangFragment;
}
//
// Read a file's data into a string, and compile it using ShCompile
//
bool CompileFile(char *fileName, ShHandle compiler, int debugOptions)
{
int ret;
char **data = ReadFileData(fileName);
if (!data)
return false;
ret = ShCompile(compiler, data, OutputMultipleStrings, EShOptNone, debugOptions);
FreeFileData(data);
return ret ? true : false;
}
//
// print usage to stdout
//
void usage()
{
printf("Usage: translate [-i -o] file1 file2 ...\n"
"Where: filename = filename ending in .frag or .vert\n"
" -i = print intermediate tree\n"
" -o = print translated code\n");
}
//
// Malloc a string of sufficient size and read a string into it.
//
# define MAX_SOURCE_STRINGS 5
char** ReadFileData(char *fileName)
{
FILE* in = fopen(fileName, "r");
char* fdata = 0;
int count = 0;
char** return_data = (char**)malloc(MAX_SOURCE_STRINGS+1);
if (!in) {
printf("Error: unable to open input file: %s\n", fileName);
return 0;
}
while (fgetc(in) != EOF)
count++;
fseek(in, 0, SEEK_SET);
if (!(fdata = (char*)malloc(count+2))) {
printf("Error allocating memory\n");
return 0;
}
if (fread(fdata, 1, count, in) != count) {
printf("Error reading input file: %s\n", fileName);
return 0;
}
fdata[count] = '\0';
fclose(in);
if (count == 0){
return_data[0] = (char*)malloc(count+2);
return_data[0][0] = '\0';
OutputMultipleStrings = 0;
return return_data;
}
int len = (int)(ceil)((float)count / (float)OutputMultipleStrings);
int ptr_len = 0, i = 0;
while (count > 0) {
return_data[i] = (char*)malloc(len+2);
memcpy(return_data[i], fdata+ptr_len, len);
return_data[i][len] = '\0';
count -= (len);
ptr_len += (len);
if (count < len){
if(count == 0){
OutputMultipleStrings = (i+1);
break;
}
len = count;
}
++i;
}
return return_data;
}
void FreeFileData(char** data)
{
for(int i=0;i<OutputMultipleStrings;i++)
free(data[i]);
}
void LogMsg(char* msg, const char* name, const int num, const char* logName)
{
printf(num >= 0 ? "#### %s %s %d %s ####\n" :
"#### %s %s INFO LOG ####\n", msg, name, num, logName);
}