| 1 |
/* |
| 2 |
* Copyright © 2008, 2009 Intel Corporation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
#include <getopt.h> |
| 24 |
|
| 25 |
/** @file standalone.cpp |
| 26 |
* |
| 27 |
* Standalone compiler helper lib. Used by standalone glsl_compiler and |
| 28 |
* also available to drivers to implement their own standalone compiler |
| 29 |
* with driver backend. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include "ast.h" |
| 33 |
#include "glsl_parser_extras.h" |
| 34 |
#include "ir_optimization.h" |
| 35 |
#include "program.h" |
| 36 |
#include "loop_analysis.h" |
| 37 |
#include "standalone_scaffolding.h" |
| 38 |
#include "standalone.h" |
| 39 |
#include "string_to_uint_map.h" |
| 40 |
#include "util/set.h" |
| 41 |
#include "linker.h" |
| 42 |
#include "glsl_parser_extras.h" |
| 43 |
#include "ir_builder_print_visitor.h" |
| 44 |
#include "builtin_functions.h" |
| 45 |
#include "opt_add_neg_to_sub.h" |
| 46 |
#include "main/mtypes.h" |
| 47 |
#include "program/program.h" |
| 48 |
|
| 49 |
class dead_variable_visitor : public ir_hierarchical_visitor { |
| 50 |
public: |
| 51 |
dead_variable_visitor() |
| 52 |
{ |
| 53 |
variables = _mesa_pointer_set_create(NULL); |
| 54 |
} |
| 55 |
|
| 56 |
virtual ~dead_variable_visitor() |
| 57 |
{ |
| 58 |
_mesa_set_destroy(variables, NULL); |
| 59 |
} |
| 60 |
|
| 61 |
virtual ir_visitor_status visit(ir_variable *ir) |
| 62 |
{ |
| 63 |
/* If the variable is auto or temp, add it to the set of variables that |
| 64 |
* are candidates for removal. |
| 65 |
*/ |
| 66 |
if (ir->data.mode != ir_var_auto && ir->data.mode != ir_var_temporary) |
| 67 |
return visit_continue; |
| 68 |
|
| 69 |
_mesa_set_add(variables, ir); |
| 70 |
|
| 71 |
return visit_continue; |
| 72 |
} |
| 73 |
|
| 74 |
virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 75 |
{ |
| 76 |
struct set_entry *entry = _mesa_set_search(variables, ir->var); |
| 77 |
|
| 78 |
/* If a variable is dereferenced at all, remove it from the set of |
| 79 |
* variables that are candidates for removal. |
| 80 |
*/ |
| 81 |
if (entry != NULL) |
| 82 |
_mesa_set_remove(variables, entry); |
| 83 |
|
| 84 |
return visit_continue; |
| 85 |
} |
| 86 |
|
| 87 |
void remove_dead_variables() |
| 88 |
{ |
| 89 |
set_foreach(variables, entry) { |
| 90 |
ir_variable *ir = (ir_variable *) entry->key; |
| 91 |
|
| 92 |
assert(ir->ir_type == ir_type_variable); |
| 93 |
ir->remove(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
private: |
| 98 |
set *variables; |
| 99 |
}; |
| 100 |
|
| 101 |
static void |
| 102 |
init_gl_program(struct gl_program *prog, bool is_arb_asm, gl_shader_stage stage) |
| 103 |
{ |
| 104 |
prog->RefCount = 1; |
| 105 |
prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB; |
| 106 |
prog->is_arb_asm = is_arb_asm; |
| 107 |
prog->info.stage = stage; |
| 108 |
} |
| 109 |
|
| 110 |
static struct gl_program * |
| 111 |
new_program(UNUSED struct gl_context *ctx, gl_shader_stage stage, |
| 112 |
UNUSED GLuint id, bool is_arb_asm) |
| 113 |
{ |
| 114 |
struct gl_program *prog = rzalloc(NULL, struct gl_program); |
| 115 |
init_gl_program(prog, is_arb_asm, stage); |
| 116 |
return prog; |
| 117 |
} |
| 118 |
|
| 119 |
static const struct standalone_options *options; |
| 120 |
|
| 121 |
static void |
| 122 |
initialize_context(struct gl_context *ctx, gl_api api) |
| 123 |
{ |
| 124 |
initialize_context_to_defaults(ctx, api); |
| 125 |
_mesa_glsl_builtin_functions_init_or_ref(); |
| 126 |
|
| 127 |
/* The standalone compiler needs to claim support for almost |
| 128 |
* everything in order to compile the built-in functions. |
| 129 |
*/ |
| 130 |
ctx->Const.GLSLVersion = options->glsl_version; |
| 131 |
ctx->Extensions.ARB_ES3_compatibility = true; |
| 132 |
ctx->Extensions.ARB_ES3_1_compatibility = true; |
| 133 |
ctx->Extensions.ARB_ES3_2_compatibility = true; |
| 134 |
ctx->Const.MaxComputeWorkGroupCount[0] = 65535; |
| 135 |
ctx->Const.MaxComputeWorkGroupCount[1] = 65535; |
| 136 |
ctx->Const.MaxComputeWorkGroupCount[2] = 65535; |
| 137 |
ctx->Const.MaxComputeWorkGroupSize[0] = 1024; |
| 138 |
ctx->Const.MaxComputeWorkGroupSize[1] = 1024; |
| 139 |
ctx->Const.MaxComputeWorkGroupSize[2] = 64; |
| 140 |
ctx->Const.MaxComputeWorkGroupInvocations = 1024; |
| 141 |
ctx->Const.MaxComputeSharedMemorySize = 32768; |
| 142 |
ctx->Const.MaxComputeVariableGroupSize[0] = 512; |
| 143 |
ctx->Const.MaxComputeVariableGroupSize[1] = 512; |
| 144 |
ctx->Const.MaxComputeVariableGroupSize[2] = 64; |
| 145 |
ctx->Const.MaxComputeVariableGroupInvocations = 512; |
| 146 |
ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16; |
| 147 |
ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024; |
| 148 |
ctx->Const.Program[MESA_SHADER_COMPUTE].MaxCombinedUniformComponents = 1024; |
| 149 |
ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */ |
| 150 |
ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */ |
| 151 |
ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = 8; |
| 152 |
ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = 8; |
| 153 |
ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms = 8; |
| 154 |
ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformBlocks = 12; |
| 155 |
|
| 156 |
switch (ctx->Const.GLSLVersion) { |
| 157 |
case 100: |
| 158 |
ctx->Const.MaxClipPlanes = 0; |
| 159 |
ctx->Const.MaxCombinedTextureImageUnits = 8; |
| 160 |
ctx->Const.MaxDrawBuffers = 2; |
| 161 |
ctx->Const.MinProgramTexelOffset = 0; |
| 162 |
ctx->Const.MaxProgramTexelOffset = 0; |
| 163 |
ctx->Const.MaxLights = 0; |
| 164 |
ctx->Const.MaxTextureCoordUnits = 0; |
| 165 |
ctx->Const.MaxTextureUnits = 8; |
| 166 |
|
| 167 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8; |
| 168 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0; |
| 169 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4; |
| 170 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 128 * 4; |
| 171 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ |
| 172 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32; |
| 173 |
|
| 174 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = |
| 175 |
ctx->Const.MaxCombinedTextureImageUnits; |
| 176 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4; |
| 177 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 16 * 4; |
| 178 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = |
| 179 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; |
| 180 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ |
| 181 |
|
| 182 |
ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4; |
| 183 |
break; |
| 184 |
case 110: |
| 185 |
case 120: |
| 186 |
ctx->Const.MaxClipPlanes = 6; |
| 187 |
ctx->Const.MaxCombinedTextureImageUnits = 2; |
| 188 |
ctx->Const.MaxDrawBuffers = 1; |
| 189 |
ctx->Const.MinProgramTexelOffset = 0; |
| 190 |
ctx->Const.MaxProgramTexelOffset = 0; |
| 191 |
ctx->Const.MaxLights = 8; |
| 192 |
ctx->Const.MaxTextureCoordUnits = 2; |
| 193 |
ctx->Const.MaxTextureUnits = 2; |
| 194 |
|
| 195 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; |
| 196 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0; |
| 197 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512; |
| 198 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 512; |
| 199 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ |
| 200 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32; |
| 201 |
|
| 202 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = |
| 203 |
ctx->Const.MaxCombinedTextureImageUnits; |
| 204 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64; |
| 205 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 64; |
| 206 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = |
| 207 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; |
| 208 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ |
| 209 |
|
| 210 |
ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4; |
| 211 |
break; |
| 212 |
case 130: |
| 213 |
case 140: |
| 214 |
ctx->Const.MaxClipPlanes = 8; |
| 215 |
ctx->Const.MaxCombinedTextureImageUnits = 16; |
| 216 |
ctx->Const.MaxDrawBuffers = 8; |
| 217 |
ctx->Const.MinProgramTexelOffset = -8; |
| 218 |
ctx->Const.MaxProgramTexelOffset = 7; |
| 219 |
ctx->Const.MaxLights = 8; |
| 220 |
ctx->Const.MaxTextureCoordUnits = 8; |
| 221 |
ctx->Const.MaxTextureUnits = 2; |
| 222 |
ctx->Const.MaxUniformBufferBindings = 84; |
| 223 |
ctx->Const.MaxVertexStreams = 4; |
| 224 |
ctx->Const.MaxTransformFeedbackBuffers = 4; |
| 225 |
|
| 226 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; |
| 227 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16; |
| 228 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024; |
| 229 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024; |
| 230 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ |
| 231 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64; |
| 232 |
|
| 233 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16; |
| 234 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024; |
| 235 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024; |
| 236 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = |
| 237 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; |
| 238 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ |
| 239 |
|
| 240 |
ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4; |
| 241 |
break; |
| 242 |
case 150: |
| 243 |
case 330: |
| 244 |
case 400: |
| 245 |
case 410: |
| 246 |
case 420: |
| 247 |
case 430: |
| 248 |
case 440: |
| 249 |
case 450: |
| 250 |
case 460: |
| 251 |
ctx->Const.MaxClipPlanes = 8; |
| 252 |
ctx->Const.MaxDrawBuffers = 8; |
| 253 |
ctx->Const.MinProgramTexelOffset = -8; |
| 254 |
ctx->Const.MaxProgramTexelOffset = 7; |
| 255 |
ctx->Const.MaxLights = 8; |
| 256 |
ctx->Const.MaxTextureCoordUnits = 8; |
| 257 |
ctx->Const.MaxTextureUnits = 2; |
| 258 |
ctx->Const.MaxUniformBufferBindings = 84; |
| 259 |
ctx->Const.MaxVertexStreams = 4; |
| 260 |
ctx->Const.MaxTransformFeedbackBuffers = 4; |
| 261 |
ctx->Const.MaxShaderStorageBufferBindings = 4; |
| 262 |
ctx->Const.MaxShaderStorageBlockSize = 4096; |
| 263 |
ctx->Const.MaxAtomicBufferBindings = 4; |
| 264 |
|
| 265 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; |
| 266 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16; |
| 267 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024; |
| 268 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024; |
| 269 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ |
| 270 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64; |
| 271 |
|
| 272 |
ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16; |
| 273 |
ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents = 1024; |
| 274 |
ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxCombinedUniformComponents = 1024; |
| 275 |
ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents = |
| 276 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; |
| 277 |
ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128; |
| 278 |
|
| 279 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16; |
| 280 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024; |
| 281 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024; |
| 282 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = |
| 283 |
ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents; |
| 284 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ |
| 285 |
|
| 286 |
ctx->Const.MaxCombinedTextureImageUnits = |
| 287 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits |
| 288 |
+ ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits |
| 289 |
+ ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits; |
| 290 |
|
| 291 |
ctx->Const.MaxGeometryOutputVertices = 256; |
| 292 |
ctx->Const.MaxGeometryTotalOutputComponents = 1024; |
| 293 |
|
| 294 |
ctx->Const.MaxVarying = 60 / 4; |
| 295 |
break; |
| 296 |
case 300: |
| 297 |
ctx->Const.MaxClipPlanes = 8; |
| 298 |
ctx->Const.MaxCombinedTextureImageUnits = 32; |
| 299 |
ctx->Const.MaxDrawBuffers = 4; |
| 300 |
ctx->Const.MinProgramTexelOffset = -8; |
| 301 |
ctx->Const.MaxProgramTexelOffset = 7; |
| 302 |
ctx->Const.MaxLights = 0; |
| 303 |
ctx->Const.MaxTextureCoordUnits = 0; |
| 304 |
ctx->Const.MaxTextureUnits = 0; |
| 305 |
ctx->Const.MaxUniformBufferBindings = 84; |
| 306 |
ctx->Const.MaxVertexStreams = 4; |
| 307 |
ctx->Const.MaxTransformFeedbackBuffers = 4; |
| 308 |
|
| 309 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; |
| 310 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16; |
| 311 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024; |
| 312 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024; |
| 313 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ |
| 314 |
ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4; |
| 315 |
|
| 316 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16; |
| 317 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224; |
| 318 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 224; |
| 319 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4; |
| 320 |
ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ |
| 321 |
|
| 322 |
ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4; |
| 323 |
break; |
| 324 |
} |
| 325 |
|
| 326 |
ctx->Const.GenerateTemporaryNames = true; |
| 327 |
ctx->Const.MaxPatchVertices = 32; |
| 328 |
|
| 329 |
/* GL_ARB_explicit_uniform_location, GL_MAX_UNIFORM_LOCATIONS */ |
| 330 |
ctx->Const.MaxUserAssignableUniformLocations = |
| 331 |
4 * MESA_SHADER_STAGES * MAX_UNIFORMS; |
| 332 |
|
| 333 |
ctx->Driver.NewProgram = new_program; |
| 334 |
} |
| 335 |
|
| 336 |
/* Returned string will have 'ctx' as its ralloc owner. */ |
| 337 |
static char * |
| 338 |
load_text_file(void *ctx, const char *file_name) |
| 339 |
{ |
| 340 |
char *text = NULL; |
| 341 |
size_t size; |
| 342 |
size_t total_read = 0; |
| 343 |
FILE *fp = fopen(file_name, "rb"); |
| 344 |
|
| 345 |
if (!fp) { |
| 346 |
return NULL; |
| 347 |
} |
| 348 |
|
| 349 |
fseek(fp, 0L, SEEK_END); |
| 350 |
size = ftell(fp); |
| 351 |
fseek(fp, 0L, SEEK_SET); |
| 352 |
|
| 353 |
text = (char *) ralloc_size(ctx, size + 1); |
| 354 |
if (text != NULL) { |
| 355 |
do { |
| 356 |
size_t bytes = fread(text + total_read, |
| 357 |
1, size - total_read, fp); |
| 358 |
if (bytes < size - total_read) { |
| 359 |
free(text); |
| 360 |
text = NULL; |
| 361 |
goto error; |
| 362 |
} |
| 363 |
|
| 364 |
if (bytes == 0) { |
| 365 |
break; |
| 366 |
} |
| 367 |
|
| 368 |
total_read += bytes; |
| 369 |
} while (total_read < size); |
| 370 |
|
| 371 |
text[total_read] = '\0'; |
| 372 |
error:; |
| 373 |
} |
| 374 |
|
| 375 |
fclose(fp); |
| 376 |
|
| 377 |
return text; |
| 378 |
} |
| 379 |
|
| 380 |
static void |
| 381 |
compile_shader(struct gl_context *ctx, struct gl_shader *shader) |
| 382 |
{ |
| 383 |
struct _mesa_glsl_parse_state *state = |
| 384 |
new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader); |
| 385 |
|
| 386 |
_mesa_glsl_compile_shader(ctx, shader, options->dump_ast, |
| 387 |
options->dump_hir, true); |
| 388 |
|
| 389 |
/* Print out the resulting IR */ |
| 390 |
if (!state->error && options->dump_lir) { |
| 391 |
_mesa_print_ir(stdout, shader->ir, state); |
| 392 |
} |
| 393 |
|
| 394 |
return; |
| 395 |
} |
| 396 |
|
| 397 |
extern "C" struct gl_shader_program * |
| 398 |
standalone_compile_shader(const struct standalone_options *_options, |
| 399 |
unsigned num_files, char* const* files, struct gl_context *ctx) |
| 400 |
{ |
| 401 |
int status = EXIT_SUCCESS; |
| 402 |
bool glsl_es = false; |
| 403 |
|
| 404 |
options = _options; |
| 405 |
|
| 406 |
switch (options->glsl_version) { |
| 407 |
case 100: |
| 408 |
case 300: |
| 409 |
glsl_es = true; |
| 410 |
break; |
| 411 |
case 110: |
| 412 |
case 120: |
| 413 |
case 130: |
| 414 |
case 140: |
| 415 |
case 150: |
| 416 |
case 330: |
| 417 |
case 400: |
| 418 |
case 410: |
| 419 |
case 420: |
| 420 |
case 430: |
| 421 |
case 440: |
| 422 |
case 450: |
| 423 |
case 460: |
| 424 |
glsl_es = false; |
| 425 |
break; |
| 426 |
default: |
| 427 |
fprintf(stderr, "Unrecognized GLSL version `%d'\n", options->glsl_version); |
| 428 |
return NULL; |
| 429 |
} |
| 430 |
|
| 431 |
if (glsl_es) { |
| 432 |
initialize_context(ctx, API_OPENGLES2); |
| 433 |
} else { |
| 434 |
initialize_context(ctx, options->glsl_version > 130 ? API_OPENGL_CORE : API_OPENGL_COMPAT); |
| 435 |
} |
| 436 |
|
| 437 |
if (options->lower_precision) { |
| 438 |
for (unsigned i = MESA_SHADER_VERTEX; i <= MESA_SHADER_FRAGMENT; i++) { |
| 439 |
struct gl_shader_compiler_options *options = |
| 440 |
&ctx->Const.ShaderCompilerOptions[i]; |
| 441 |
options->LowerPrecision = true; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
struct gl_shader_program *whole_program; |
| 446 |
|
| 447 |
whole_program = rzalloc (NULL, struct gl_shader_program); |
| 448 |
assert(whole_program != NULL); |
| 449 |
whole_program->data = rzalloc(whole_program, struct gl_shader_program_data); |
| 450 |
assert(whole_program->data != NULL); |
| 451 |
whole_program->data->InfoLog = ralloc_strdup(whole_program->data, ""); |
| 452 |
|
| 453 |
/* Created just to avoid segmentation faults */ |
| 454 |
whole_program->AttributeBindings = new string_to_uint_map; |
| 455 |
whole_program->FragDataBindings = new string_to_uint_map; |
| 456 |
whole_program->FragDataIndexBindings = new string_to_uint_map; |
| 457 |
|
| 458 |
for (unsigned i = 0; i < num_files; i++) { |
| 459 |
whole_program->Shaders = |
| 460 |
reralloc(whole_program, whole_program->Shaders, |
| 461 |
struct gl_shader *, whole_program->NumShaders + 1); |
| 462 |
assert(whole_program->Shaders != NULL); |
| 463 |
|
| 464 |
struct gl_shader *shader = rzalloc(whole_program, gl_shader); |
| 465 |
|
| 466 |
whole_program->Shaders[whole_program->NumShaders] = shader; |
| 467 |
whole_program->NumShaders++; |
| 468 |
|
| 469 |
const unsigned len = strlen(files[i]); |
| 470 |
if (len < 6) |
| 471 |
goto fail; |
| 472 |
|
| 473 |
const char *const ext = & files[i][len - 5]; |
| 474 |
/* TODO add support to read a .shader_test */ |
| 475 |
if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0) |
| 476 |
shader->Type = GL_VERTEX_SHADER; |
| 477 |
else if (strncmp(".tesc", ext, 5) == 0) |
| 478 |
shader->Type = GL_TESS_CONTROL_SHADER; |
| 479 |
else if (strncmp(".tese", ext, 5) == 0) |
| 480 |
shader->Type = GL_TESS_EVALUATION_SHADER; |
| 481 |
else if (strncmp(".geom", ext, 5) == 0) |
| 482 |
shader->Type = GL_GEOMETRY_SHADER; |
| 483 |
else if (strncmp(".frag", ext, 5) == 0) |
| 484 |
shader->Type = GL_FRAGMENT_SHADER; |
| 485 |
else if (strncmp(".comp", ext, 5) == 0) |
| 486 |
shader->Type = GL_COMPUTE_SHADER; |
| 487 |
else |
| 488 |
goto fail; |
| 489 |
shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type); |
| 490 |
|
| 491 |
shader->Source = load_text_file(whole_program, files[i]); |
| 492 |
if (shader->Source == NULL) { |
| 493 |
printf("File \"%s\" does not exist.\n", files[i]); |
| 494 |
exit(EXIT_FAILURE); |
| 495 |
} |
| 496 |
|
| 497 |
compile_shader(ctx, shader); |
| 498 |
|
| 499 |
if (strlen(shader->InfoLog) > 0) { |
| 500 |
if (!options->just_log) |
| 501 |
printf("Info log for %s:\n", files[i]); |
| 502 |
|
| 503 |
printf("%s", shader->InfoLog); |
| 504 |
if (!options->just_log) |
| 505 |
printf("\n"); |
| 506 |
} |
| 507 |
|
| 508 |
if (!shader->CompileStatus) { |
| 509 |
status = EXIT_FAILURE; |
| 510 |
break; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
if (status == EXIT_SUCCESS) { |
| 515 |
_mesa_clear_shader_program_data(ctx, whole_program); |
| 516 |
|
| 517 |
if (options->do_link) { |
| 518 |
link_shaders(ctx, whole_program); |
| 519 |
} else { |
| 520 |
const gl_shader_stage stage = whole_program->Shaders[0]->Stage; |
| 521 |
|
| 522 |
whole_program->data->LinkStatus = LINKING_SUCCESS; |
| 523 |
whole_program->_LinkedShaders[stage] = |
| 524 |
link_intrastage_shaders(whole_program /* mem_ctx */, |
| 525 |
ctx, |
| 526 |
whole_program, |
| 527 |
whole_program->Shaders, |
| 528 |
1, |
| 529 |
true); |
| 530 |
|
| 531 |
/* Par-linking can fail, for example, if there are undefined external |
| 532 |
* references. |
| 533 |
*/ |
| 534 |
if (whole_program->_LinkedShaders[stage] != NULL) { |
| 535 |
assert(whole_program->data->LinkStatus); |
| 536 |
|
| 537 |
struct gl_shader_compiler_options *const compiler_options = |
| 538 |
&ctx->Const.ShaderCompilerOptions[stage]; |
| 539 |
|
| 540 |
exec_list *const ir = |
| 541 |
whole_program->_LinkedShaders[stage]->ir; |
| 542 |
|
| 543 |
bool progress; |
| 544 |
do { |
| 545 |
progress = do_function_inlining(ir); |
| 546 |
|
| 547 |
progress = do_common_optimization(ir, |
| 548 |
false, |
| 549 |
false, |
| 550 |
compiler_options, |
| 551 |
true) |
| 552 |
&& progress; |
| 553 |
} while(progress); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
status = (whole_program->data->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 558 |
|
| 559 |
if (strlen(whole_program->data->InfoLog) > 0) { |
| 560 |
printf("\n"); |
| 561 |
if (!options->just_log) |
| 562 |
printf("Info log for linking:\n"); |
| 563 |
printf("%s", whole_program->data->InfoLog); |
| 564 |
if (!options->just_log) |
| 565 |
printf("\n"); |
| 566 |
} |
| 567 |
|
| 568 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 569 |
struct gl_linked_shader *shader = whole_program->_LinkedShaders[i]; |
| 570 |
|
| 571 |
if (!shader) |
| 572 |
continue; |
| 573 |
|
| 574 |
add_neg_to_sub_visitor v; |
| 575 |
visit_list_elements(&v, shader->ir); |
| 576 |
|
| 577 |
dead_variable_visitor dv; |
| 578 |
visit_list_elements(&dv, shader->ir); |
| 579 |
dv.remove_dead_variables(); |
| 580 |
} |
| 581 |
|
| 582 |
if (options->dump_builder) { |
| 583 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 584 |
struct gl_linked_shader *shader = whole_program->_LinkedShaders[i]; |
| 585 |
|
| 586 |
if (!shader) |
| 587 |
continue; |
| 588 |
|
| 589 |
_mesa_print_builder_for_ir(stdout, shader->ir); |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
return whole_program; |
| 595 |
|
| 596 |
fail: |
| 597 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 598 |
if (whole_program->_LinkedShaders[i]) |
| 599 |
ralloc_free(whole_program->_LinkedShaders[i]->Program); |
| 600 |
} |
| 601 |
|
| 602 |
ralloc_free(whole_program); |
| 603 |
return NULL; |
| 604 |
} |
| 605 |
|
| 606 |
extern "C" void |
| 607 |
standalone_compiler_cleanup(struct gl_shader_program *whole_program) |
| 608 |
{ |
| 609 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 610 |
if (whole_program->_LinkedShaders[i]) |
| 611 |
ralloc_free(whole_program->_LinkedShaders[i]->Program); |
| 612 |
} |
| 613 |
|
| 614 |
delete whole_program->AttributeBindings; |
| 615 |
delete whole_program->FragDataBindings; |
| 616 |
delete whole_program->FragDataIndexBindings; |
| 617 |
|
| 618 |
ralloc_free(whole_program); |
| 619 |
_mesa_glsl_builtin_functions_decref(); |
| 620 |
} |