standalone.cpp revision 7e102996
101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2008, 2009 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg#include <getopt.h>
2401e04c3fSmrg
2501e04c3fSmrg/** @file standalone.cpp
2601e04c3fSmrg *
2701e04c3fSmrg * Standalone compiler helper lib.  Used by standalone glsl_compiler and
2801e04c3fSmrg * also available to drivers to implement their own standalone compiler
2901e04c3fSmrg * with driver backend.
3001e04c3fSmrg */
3101e04c3fSmrg
3201e04c3fSmrg#include "ast.h"
3301e04c3fSmrg#include "glsl_parser_extras.h"
3401e04c3fSmrg#include "ir_optimization.h"
3501e04c3fSmrg#include "program.h"
3601e04c3fSmrg#include "loop_analysis.h"
3701e04c3fSmrg#include "standalone_scaffolding.h"
3801e04c3fSmrg#include "standalone.h"
3901e04c3fSmrg#include "string_to_uint_map.h"
4001e04c3fSmrg#include "util/set.h"
4101e04c3fSmrg#include "linker.h"
4201e04c3fSmrg#include "glsl_parser_extras.h"
4301e04c3fSmrg#include "ir_builder_print_visitor.h"
4401e04c3fSmrg#include "builtin_functions.h"
4501e04c3fSmrg#include "opt_add_neg_to_sub.h"
4601e04c3fSmrg#include "main/mtypes.h"
4701e04c3fSmrg
4801e04c3fSmrgclass dead_variable_visitor : public ir_hierarchical_visitor {
4901e04c3fSmrgpublic:
5001e04c3fSmrg   dead_variable_visitor()
5101e04c3fSmrg   {
527e102996Smaya      variables = _mesa_pointer_set_create(NULL);
5301e04c3fSmrg   }
5401e04c3fSmrg
5501e04c3fSmrg   virtual ~dead_variable_visitor()
5601e04c3fSmrg   {
5701e04c3fSmrg      _mesa_set_destroy(variables, NULL);
5801e04c3fSmrg   }
5901e04c3fSmrg
6001e04c3fSmrg   virtual ir_visitor_status visit(ir_variable *ir)
6101e04c3fSmrg   {
6201e04c3fSmrg      /* If the variable is auto or temp, add it to the set of variables that
6301e04c3fSmrg       * are candidates for removal.
6401e04c3fSmrg       */
6501e04c3fSmrg      if (ir->data.mode != ir_var_auto && ir->data.mode != ir_var_temporary)
6601e04c3fSmrg         return visit_continue;
6701e04c3fSmrg
6801e04c3fSmrg      _mesa_set_add(variables, ir);
6901e04c3fSmrg
7001e04c3fSmrg      return visit_continue;
7101e04c3fSmrg   }
7201e04c3fSmrg
7301e04c3fSmrg   virtual ir_visitor_status visit(ir_dereference_variable *ir)
7401e04c3fSmrg   {
7501e04c3fSmrg      struct set_entry *entry = _mesa_set_search(variables, ir->var);
7601e04c3fSmrg
7701e04c3fSmrg      /* If a variable is dereferenced at all, remove it from the set of
7801e04c3fSmrg       * variables that are candidates for removal.
7901e04c3fSmrg       */
8001e04c3fSmrg      if (entry != NULL)
8101e04c3fSmrg         _mesa_set_remove(variables, entry);
8201e04c3fSmrg
8301e04c3fSmrg      return visit_continue;
8401e04c3fSmrg   }
8501e04c3fSmrg
8601e04c3fSmrg   void remove_dead_variables()
8701e04c3fSmrg   {
8801e04c3fSmrg      set_foreach(variables, entry) {
8901e04c3fSmrg         ir_variable *ir = (ir_variable *) entry->key;
9001e04c3fSmrg
9101e04c3fSmrg         assert(ir->ir_type == ir_type_variable);
9201e04c3fSmrg         ir->remove();
9301e04c3fSmrg      }
9401e04c3fSmrg   }
9501e04c3fSmrg
9601e04c3fSmrgprivate:
9701e04c3fSmrg   set *variables;
9801e04c3fSmrg};
9901e04c3fSmrg
10001e04c3fSmrgstatic void
10101e04c3fSmrginit_gl_program(struct gl_program *prog, bool is_arb_asm)
10201e04c3fSmrg{
10301e04c3fSmrg   prog->RefCount = 1;
10401e04c3fSmrg   prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
10501e04c3fSmrg   prog->is_arb_asm = is_arb_asm;
10601e04c3fSmrg}
10701e04c3fSmrg
10801e04c3fSmrgstatic struct gl_program *
10901e04c3fSmrgnew_program(UNUSED struct gl_context *ctx, GLenum target,
11001e04c3fSmrg            UNUSED GLuint id, bool is_arb_asm)
11101e04c3fSmrg{
11201e04c3fSmrg   switch (target) {
11301e04c3fSmrg   case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
11401e04c3fSmrg   case GL_GEOMETRY_PROGRAM_NV:
11501e04c3fSmrg   case GL_TESS_CONTROL_PROGRAM_NV:
11601e04c3fSmrg   case GL_TESS_EVALUATION_PROGRAM_NV:
11701e04c3fSmrg   case GL_FRAGMENT_PROGRAM_ARB:
11801e04c3fSmrg   case GL_COMPUTE_PROGRAM_NV: {
11901e04c3fSmrg      struct gl_program *prog = rzalloc(NULL, struct gl_program);
12001e04c3fSmrg      init_gl_program(prog, is_arb_asm);
12101e04c3fSmrg      return prog;
12201e04c3fSmrg   }
12301e04c3fSmrg   default:
12401e04c3fSmrg      printf("bad target in new_program\n");
12501e04c3fSmrg      return NULL;
12601e04c3fSmrg   }
12701e04c3fSmrg}
12801e04c3fSmrg
12901e04c3fSmrgstatic const struct standalone_options *options;
13001e04c3fSmrg
13101e04c3fSmrgstatic void
13201e04c3fSmrginitialize_context(struct gl_context *ctx, gl_api api)
13301e04c3fSmrg{
13401e04c3fSmrg   initialize_context_to_defaults(ctx, api);
1357e102996Smaya   glsl_type_singleton_init_or_ref();
13601e04c3fSmrg
13701e04c3fSmrg   /* The standalone compiler needs to claim support for almost
13801e04c3fSmrg    * everything in order to compile the built-in functions.
13901e04c3fSmrg    */
14001e04c3fSmrg   ctx->Const.GLSLVersion = options->glsl_version;
14101e04c3fSmrg   ctx->Extensions.ARB_ES3_compatibility = true;
1427e102996Smaya   ctx->Extensions.ARB_ES3_1_compatibility = true;
1437e102996Smaya   ctx->Extensions.ARB_ES3_2_compatibility = true;
14401e04c3fSmrg   ctx->Const.MaxComputeWorkGroupCount[0] = 65535;
14501e04c3fSmrg   ctx->Const.MaxComputeWorkGroupCount[1] = 65535;
14601e04c3fSmrg   ctx->Const.MaxComputeWorkGroupCount[2] = 65535;
14701e04c3fSmrg   ctx->Const.MaxComputeWorkGroupSize[0] = 1024;
14801e04c3fSmrg   ctx->Const.MaxComputeWorkGroupSize[1] = 1024;
14901e04c3fSmrg   ctx->Const.MaxComputeWorkGroupSize[2] = 64;
15001e04c3fSmrg   ctx->Const.MaxComputeWorkGroupInvocations = 1024;
15101e04c3fSmrg   ctx->Const.MaxComputeSharedMemorySize = 32768;
15201e04c3fSmrg   ctx->Const.MaxComputeVariableGroupSize[0] = 512;
15301e04c3fSmrg   ctx->Const.MaxComputeVariableGroupSize[1] = 512;
15401e04c3fSmrg   ctx->Const.MaxComputeVariableGroupSize[2] = 64;
15501e04c3fSmrg   ctx->Const.MaxComputeVariableGroupInvocations = 512;
15601e04c3fSmrg   ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
15701e04c3fSmrg   ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
15801e04c3fSmrg   ctx->Const.Program[MESA_SHADER_COMPUTE].MaxCombinedUniformComponents = 1024;
15901e04c3fSmrg   ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
16001e04c3fSmrg   ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
16101e04c3fSmrg   ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = 8;
16201e04c3fSmrg   ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = 8;
16301e04c3fSmrg   ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms = 8;
16401e04c3fSmrg   ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformBlocks = 12;
16501e04c3fSmrg
16601e04c3fSmrg   switch (ctx->Const.GLSLVersion) {
16701e04c3fSmrg   case 100:
16801e04c3fSmrg      ctx->Const.MaxClipPlanes = 0;
16901e04c3fSmrg      ctx->Const.MaxCombinedTextureImageUnits = 8;
17001e04c3fSmrg      ctx->Const.MaxDrawBuffers = 2;
17101e04c3fSmrg      ctx->Const.MinProgramTexelOffset = 0;
17201e04c3fSmrg      ctx->Const.MaxProgramTexelOffset = 0;
17301e04c3fSmrg      ctx->Const.MaxLights = 0;
17401e04c3fSmrg      ctx->Const.MaxTextureCoordUnits = 0;
17501e04c3fSmrg      ctx->Const.MaxTextureUnits = 8;
17601e04c3fSmrg
17701e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8;
17801e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
17901e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4;
18001e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 128 * 4;
18101e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
18201e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
18301e04c3fSmrg
18401e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
18501e04c3fSmrg         ctx->Const.MaxCombinedTextureImageUnits;
18601e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4;
18701e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 16 * 4;
18801e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
18901e04c3fSmrg         ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
19001e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
19101e04c3fSmrg
19201e04c3fSmrg      ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
19301e04c3fSmrg      break;
19401e04c3fSmrg   case 110:
19501e04c3fSmrg   case 120:
19601e04c3fSmrg      ctx->Const.MaxClipPlanes = 6;
19701e04c3fSmrg      ctx->Const.MaxCombinedTextureImageUnits = 2;
19801e04c3fSmrg      ctx->Const.MaxDrawBuffers = 1;
19901e04c3fSmrg      ctx->Const.MinProgramTexelOffset = 0;
20001e04c3fSmrg      ctx->Const.MaxProgramTexelOffset = 0;
20101e04c3fSmrg      ctx->Const.MaxLights = 8;
20201e04c3fSmrg      ctx->Const.MaxTextureCoordUnits = 2;
20301e04c3fSmrg      ctx->Const.MaxTextureUnits = 2;
20401e04c3fSmrg
20501e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
20601e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
20701e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512;
20801e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 512;
20901e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
21001e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
21101e04c3fSmrg
21201e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
21301e04c3fSmrg         ctx->Const.MaxCombinedTextureImageUnits;
21401e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64;
21501e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 64;
21601e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
21701e04c3fSmrg         ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
21801e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
21901e04c3fSmrg
22001e04c3fSmrg      ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
22101e04c3fSmrg      break;
22201e04c3fSmrg   case 130:
22301e04c3fSmrg   case 140:
22401e04c3fSmrg      ctx->Const.MaxClipPlanes = 8;
22501e04c3fSmrg      ctx->Const.MaxCombinedTextureImageUnits = 16;
22601e04c3fSmrg      ctx->Const.MaxDrawBuffers = 8;
22701e04c3fSmrg      ctx->Const.MinProgramTexelOffset = -8;
22801e04c3fSmrg      ctx->Const.MaxProgramTexelOffset = 7;
22901e04c3fSmrg      ctx->Const.MaxLights = 8;
23001e04c3fSmrg      ctx->Const.MaxTextureCoordUnits = 8;
23101e04c3fSmrg      ctx->Const.MaxTextureUnits = 2;
23201e04c3fSmrg      ctx->Const.MaxUniformBufferBindings = 84;
23301e04c3fSmrg      ctx->Const.MaxVertexStreams = 4;
23401e04c3fSmrg      ctx->Const.MaxTransformFeedbackBuffers = 4;
23501e04c3fSmrg
23601e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
23701e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
23801e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
23901e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
24001e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
24101e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
24201e04c3fSmrg
24301e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
24401e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
24501e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
24601e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
24701e04c3fSmrg         ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
24801e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
24901e04c3fSmrg
25001e04c3fSmrg      ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
25101e04c3fSmrg      break;
25201e04c3fSmrg   case 150:
25301e04c3fSmrg   case 330:
25401e04c3fSmrg   case 400:
25501e04c3fSmrg   case 410:
25601e04c3fSmrg   case 420:
25701e04c3fSmrg   case 430:
25801e04c3fSmrg   case 440:
25901e04c3fSmrg   case 450:
26001e04c3fSmrg   case 460:
26101e04c3fSmrg      ctx->Const.MaxClipPlanes = 8;
26201e04c3fSmrg      ctx->Const.MaxDrawBuffers = 8;
26301e04c3fSmrg      ctx->Const.MinProgramTexelOffset = -8;
26401e04c3fSmrg      ctx->Const.MaxProgramTexelOffset = 7;
26501e04c3fSmrg      ctx->Const.MaxLights = 8;
26601e04c3fSmrg      ctx->Const.MaxTextureCoordUnits = 8;
26701e04c3fSmrg      ctx->Const.MaxTextureUnits = 2;
26801e04c3fSmrg      ctx->Const.MaxUniformBufferBindings = 84;
26901e04c3fSmrg      ctx->Const.MaxVertexStreams = 4;
27001e04c3fSmrg      ctx->Const.MaxTransformFeedbackBuffers = 4;
2717e102996Smaya      ctx->Const.MaxShaderStorageBufferBindings = 4;
2727e102996Smaya      ctx->Const.MaxShaderStorageBlockSize = 4096;
2737e102996Smaya      ctx->Const.MaxAtomicBufferBindings = 4;
27401e04c3fSmrg
27501e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
27601e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
27701e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
27801e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
27901e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
28001e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
28101e04c3fSmrg
28201e04c3fSmrg      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16;
28301e04c3fSmrg      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents = 1024;
28401e04c3fSmrg      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxCombinedUniformComponents = 1024;
28501e04c3fSmrg      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents =
28601e04c3fSmrg         ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
28701e04c3fSmrg      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128;
28801e04c3fSmrg
28901e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
29001e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
29101e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
29201e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
29301e04c3fSmrg         ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;
29401e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
29501e04c3fSmrg
29601e04c3fSmrg      ctx->Const.MaxCombinedTextureImageUnits =
29701e04c3fSmrg         ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits
29801e04c3fSmrg         + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits
29901e04c3fSmrg         + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
30001e04c3fSmrg
30101e04c3fSmrg      ctx->Const.MaxGeometryOutputVertices = 256;
30201e04c3fSmrg      ctx->Const.MaxGeometryTotalOutputComponents = 1024;
30301e04c3fSmrg
30401e04c3fSmrg      ctx->Const.MaxVarying = 60 / 4;
30501e04c3fSmrg      break;
30601e04c3fSmrg   case 300:
30701e04c3fSmrg      ctx->Const.MaxClipPlanes = 8;
30801e04c3fSmrg      ctx->Const.MaxCombinedTextureImageUnits = 32;
30901e04c3fSmrg      ctx->Const.MaxDrawBuffers = 4;
31001e04c3fSmrg      ctx->Const.MinProgramTexelOffset = -8;
31101e04c3fSmrg      ctx->Const.MaxProgramTexelOffset = 7;
31201e04c3fSmrg      ctx->Const.MaxLights = 0;
31301e04c3fSmrg      ctx->Const.MaxTextureCoordUnits = 0;
31401e04c3fSmrg      ctx->Const.MaxTextureUnits = 0;
31501e04c3fSmrg      ctx->Const.MaxUniformBufferBindings = 84;
31601e04c3fSmrg      ctx->Const.MaxVertexStreams = 4;
31701e04c3fSmrg      ctx->Const.MaxTransformFeedbackBuffers = 4;
31801e04c3fSmrg
31901e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
32001e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
32101e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
32201e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
32301e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
32401e04c3fSmrg      ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4;
32501e04c3fSmrg
32601e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
32701e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224;
32801e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 224;
32901e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4;
33001e04c3fSmrg      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
33101e04c3fSmrg
33201e04c3fSmrg      ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4;
33301e04c3fSmrg      break;
33401e04c3fSmrg   }
33501e04c3fSmrg
33601e04c3fSmrg   ctx->Const.GenerateTemporaryNames = true;
33701e04c3fSmrg   ctx->Const.MaxPatchVertices = 32;
33801e04c3fSmrg
33901e04c3fSmrg   /* GL_ARB_explicit_uniform_location, GL_MAX_UNIFORM_LOCATIONS */
34001e04c3fSmrg   ctx->Const.MaxUserAssignableUniformLocations =
34101e04c3fSmrg      4 * MESA_SHADER_STAGES * MAX_UNIFORMS;
34201e04c3fSmrg
34301e04c3fSmrg   ctx->Driver.NewProgram = new_program;
34401e04c3fSmrg}
34501e04c3fSmrg
34601e04c3fSmrg/* Returned string will have 'ctx' as its ralloc owner. */
34701e04c3fSmrgstatic char *
34801e04c3fSmrgload_text_file(void *ctx, const char *file_name)
34901e04c3fSmrg{
35001e04c3fSmrg   char *text = NULL;
35101e04c3fSmrg   size_t size;
35201e04c3fSmrg   size_t total_read = 0;
35301e04c3fSmrg   FILE *fp = fopen(file_name, "rb");
35401e04c3fSmrg
35501e04c3fSmrg   if (!fp) {
35601e04c3fSmrg      return NULL;
35701e04c3fSmrg   }
35801e04c3fSmrg
35901e04c3fSmrg   fseek(fp, 0L, SEEK_END);
36001e04c3fSmrg   size = ftell(fp);
36101e04c3fSmrg   fseek(fp, 0L, SEEK_SET);
36201e04c3fSmrg
36301e04c3fSmrg   text = (char *) ralloc_size(ctx, size + 1);
36401e04c3fSmrg   if (text != NULL) {
36501e04c3fSmrg      do {
36601e04c3fSmrg         size_t bytes = fread(text + total_read,
36701e04c3fSmrg               1, size - total_read, fp);
36801e04c3fSmrg         if (bytes < size - total_read) {
36901e04c3fSmrg            free(text);
37001e04c3fSmrg            text = NULL;
37101e04c3fSmrg            goto error;
37201e04c3fSmrg         }
37301e04c3fSmrg
37401e04c3fSmrg         if (bytes == 0) {
37501e04c3fSmrg            break;
37601e04c3fSmrg         }
37701e04c3fSmrg
37801e04c3fSmrg         total_read += bytes;
37901e04c3fSmrg      } while (total_read < size);
38001e04c3fSmrg
38101e04c3fSmrg      text[total_read] = '\0';
38201e04c3fSmrg      error:;
38301e04c3fSmrg   }
38401e04c3fSmrg
38501e04c3fSmrg   fclose(fp);
38601e04c3fSmrg
38701e04c3fSmrg   return text;
38801e04c3fSmrg}
38901e04c3fSmrg
39001e04c3fSmrgstatic void
39101e04c3fSmrgcompile_shader(struct gl_context *ctx, struct gl_shader *shader)
39201e04c3fSmrg{
39301e04c3fSmrg   struct _mesa_glsl_parse_state *state =
39401e04c3fSmrg      new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
39501e04c3fSmrg
39601e04c3fSmrg   _mesa_glsl_compile_shader(ctx, shader, options->dump_ast,
39701e04c3fSmrg                             options->dump_hir, true);
39801e04c3fSmrg
39901e04c3fSmrg   /* Print out the resulting IR */
40001e04c3fSmrg   if (!state->error && options->dump_lir) {
40101e04c3fSmrg      _mesa_print_ir(stdout, shader->ir, state);
40201e04c3fSmrg   }
40301e04c3fSmrg
40401e04c3fSmrg   return;
40501e04c3fSmrg}
40601e04c3fSmrg
40701e04c3fSmrgextern "C" struct gl_shader_program *
40801e04c3fSmrgstandalone_compile_shader(const struct standalone_options *_options,
4097e102996Smaya      unsigned num_files, char* const* files, struct gl_context *ctx)
41001e04c3fSmrg{
41101e04c3fSmrg   int status = EXIT_SUCCESS;
41201e04c3fSmrg   bool glsl_es = false;
41301e04c3fSmrg
41401e04c3fSmrg   options = _options;
41501e04c3fSmrg
41601e04c3fSmrg   switch (options->glsl_version) {
41701e04c3fSmrg   case 100:
41801e04c3fSmrg   case 300:
41901e04c3fSmrg      glsl_es = true;
42001e04c3fSmrg      break;
42101e04c3fSmrg   case 110:
42201e04c3fSmrg   case 120:
42301e04c3fSmrg   case 130:
42401e04c3fSmrg   case 140:
42501e04c3fSmrg   case 150:
42601e04c3fSmrg   case 330:
42701e04c3fSmrg   case 400:
42801e04c3fSmrg   case 410:
42901e04c3fSmrg   case 420:
43001e04c3fSmrg   case 430:
43101e04c3fSmrg   case 440:
43201e04c3fSmrg   case 450:
43301e04c3fSmrg   case 460:
43401e04c3fSmrg      glsl_es = false;
43501e04c3fSmrg      break;
43601e04c3fSmrg   default:
43701e04c3fSmrg      fprintf(stderr, "Unrecognized GLSL version `%d'\n", options->glsl_version);
43801e04c3fSmrg      return NULL;
43901e04c3fSmrg   }
44001e04c3fSmrg
44101e04c3fSmrg   if (glsl_es) {
44201e04c3fSmrg      initialize_context(ctx, API_OPENGLES2);
44301e04c3fSmrg   } else {
44401e04c3fSmrg      initialize_context(ctx, options->glsl_version > 130 ? API_OPENGL_CORE : API_OPENGL_COMPAT);
44501e04c3fSmrg   }
44601e04c3fSmrg
44701e04c3fSmrg   struct gl_shader_program *whole_program;
44801e04c3fSmrg
44901e04c3fSmrg   whole_program = rzalloc (NULL, struct gl_shader_program);
45001e04c3fSmrg   assert(whole_program != NULL);
45101e04c3fSmrg   whole_program->data = rzalloc(whole_program, struct gl_shader_program_data);
45201e04c3fSmrg   assert(whole_program->data != NULL);
45301e04c3fSmrg   whole_program->data->InfoLog = ralloc_strdup(whole_program->data, "");
45401e04c3fSmrg
45501e04c3fSmrg   /* Created just to avoid segmentation faults */
45601e04c3fSmrg   whole_program->AttributeBindings = new string_to_uint_map;
45701e04c3fSmrg   whole_program->FragDataBindings = new string_to_uint_map;
45801e04c3fSmrg   whole_program->FragDataIndexBindings = new string_to_uint_map;
45901e04c3fSmrg
46001e04c3fSmrg   for (unsigned i = 0; i < num_files; i++) {
46101e04c3fSmrg      whole_program->Shaders =
46201e04c3fSmrg            reralloc(whole_program, whole_program->Shaders,
46301e04c3fSmrg                  struct gl_shader *, whole_program->NumShaders + 1);
46401e04c3fSmrg      assert(whole_program->Shaders != NULL);
46501e04c3fSmrg
46601e04c3fSmrg      struct gl_shader *shader = rzalloc(whole_program, gl_shader);
46701e04c3fSmrg
46801e04c3fSmrg      whole_program->Shaders[whole_program->NumShaders] = shader;
46901e04c3fSmrg      whole_program->NumShaders++;
47001e04c3fSmrg
47101e04c3fSmrg      const unsigned len = strlen(files[i]);
47201e04c3fSmrg      if (len < 6)
47301e04c3fSmrg         goto fail;
47401e04c3fSmrg
47501e04c3fSmrg      const char *const ext = & files[i][len - 5];
47601e04c3fSmrg      /* TODO add support to read a .shader_test */
47701e04c3fSmrg      if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0)
47801e04c3fSmrg	 shader->Type = GL_VERTEX_SHADER;
47901e04c3fSmrg      else if (strncmp(".tesc", ext, 5) == 0)
48001e04c3fSmrg	 shader->Type = GL_TESS_CONTROL_SHADER;
48101e04c3fSmrg      else if (strncmp(".tese", ext, 5) == 0)
48201e04c3fSmrg	 shader->Type = GL_TESS_EVALUATION_SHADER;
48301e04c3fSmrg      else if (strncmp(".geom", ext, 5) == 0)
48401e04c3fSmrg	 shader->Type = GL_GEOMETRY_SHADER;
48501e04c3fSmrg      else if (strncmp(".frag", ext, 5) == 0)
48601e04c3fSmrg	 shader->Type = GL_FRAGMENT_SHADER;
48701e04c3fSmrg      else if (strncmp(".comp", ext, 5) == 0)
48801e04c3fSmrg         shader->Type = GL_COMPUTE_SHADER;
48901e04c3fSmrg      else
49001e04c3fSmrg         goto fail;
49101e04c3fSmrg      shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type);
49201e04c3fSmrg
49301e04c3fSmrg      shader->Source = load_text_file(whole_program, files[i]);
49401e04c3fSmrg      if (shader->Source == NULL) {
49501e04c3fSmrg         printf("File \"%s\" does not exist.\n", files[i]);
49601e04c3fSmrg         exit(EXIT_FAILURE);
49701e04c3fSmrg      }
49801e04c3fSmrg
49901e04c3fSmrg      compile_shader(ctx, shader);
50001e04c3fSmrg
50101e04c3fSmrg      if (strlen(shader->InfoLog) > 0) {
50201e04c3fSmrg         if (!options->just_log)
50301e04c3fSmrg            printf("Info log for %s:\n", files[i]);
50401e04c3fSmrg
50501e04c3fSmrg         printf("%s", shader->InfoLog);
50601e04c3fSmrg         if (!options->just_log)
50701e04c3fSmrg            printf("\n");
50801e04c3fSmrg      }
50901e04c3fSmrg
51001e04c3fSmrg      if (!shader->CompileStatus) {
51101e04c3fSmrg         status = EXIT_FAILURE;
51201e04c3fSmrg         break;
51301e04c3fSmrg      }
51401e04c3fSmrg   }
51501e04c3fSmrg
51601e04c3fSmrg   if (status == EXIT_SUCCESS) {
51701e04c3fSmrg      _mesa_clear_shader_program_data(ctx, whole_program);
51801e04c3fSmrg
51901e04c3fSmrg      if (options->do_link)  {
52001e04c3fSmrg         link_shaders(ctx, whole_program);
52101e04c3fSmrg      } else {
52201e04c3fSmrg         const gl_shader_stage stage = whole_program->Shaders[0]->Stage;
52301e04c3fSmrg
52401e04c3fSmrg         whole_program->data->LinkStatus = LINKING_SUCCESS;
52501e04c3fSmrg         whole_program->_LinkedShaders[stage] =
52601e04c3fSmrg            link_intrastage_shaders(whole_program /* mem_ctx */,
52701e04c3fSmrg                                    ctx,
52801e04c3fSmrg                                    whole_program,
52901e04c3fSmrg                                    whole_program->Shaders,
53001e04c3fSmrg                                    1,
53101e04c3fSmrg                                    true);
53201e04c3fSmrg
53301e04c3fSmrg         /* Par-linking can fail, for example, if there are undefined external
53401e04c3fSmrg          * references.
53501e04c3fSmrg          */
53601e04c3fSmrg         if (whole_program->_LinkedShaders[stage] != NULL) {
53701e04c3fSmrg            assert(whole_program->data->LinkStatus);
53801e04c3fSmrg
53901e04c3fSmrg            struct gl_shader_compiler_options *const compiler_options =
54001e04c3fSmrg               &ctx->Const.ShaderCompilerOptions[stage];
54101e04c3fSmrg
54201e04c3fSmrg            exec_list *const ir =
54301e04c3fSmrg               whole_program->_LinkedShaders[stage]->ir;
54401e04c3fSmrg
54501e04c3fSmrg            bool progress;
54601e04c3fSmrg            do {
54701e04c3fSmrg               progress = do_function_inlining(ir);
54801e04c3fSmrg
54901e04c3fSmrg               progress = do_common_optimization(ir,
55001e04c3fSmrg                                                 false,
55101e04c3fSmrg                                                 false,
55201e04c3fSmrg                                                 compiler_options,
55301e04c3fSmrg                                                 true)
55401e04c3fSmrg                  && progress;
55501e04c3fSmrg            } while(progress);
55601e04c3fSmrg         }
55701e04c3fSmrg      }
55801e04c3fSmrg
55901e04c3fSmrg      status = (whole_program->data->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
56001e04c3fSmrg
56101e04c3fSmrg      if (strlen(whole_program->data->InfoLog) > 0) {
56201e04c3fSmrg         printf("\n");
56301e04c3fSmrg         if (!options->just_log)
56401e04c3fSmrg            printf("Info log for linking:\n");
56501e04c3fSmrg         printf("%s", whole_program->data->InfoLog);
56601e04c3fSmrg         if (!options->just_log)
56701e04c3fSmrg            printf("\n");
56801e04c3fSmrg      }
56901e04c3fSmrg
57001e04c3fSmrg      for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
57101e04c3fSmrg         struct gl_linked_shader *shader = whole_program->_LinkedShaders[i];
57201e04c3fSmrg
57301e04c3fSmrg         if (!shader)
57401e04c3fSmrg            continue;
57501e04c3fSmrg
57601e04c3fSmrg         add_neg_to_sub_visitor v;
57701e04c3fSmrg         visit_list_elements(&v, shader->ir);
57801e04c3fSmrg
57901e04c3fSmrg         dead_variable_visitor dv;
58001e04c3fSmrg         visit_list_elements(&dv, shader->ir);
58101e04c3fSmrg         dv.remove_dead_variables();
58201e04c3fSmrg      }
58301e04c3fSmrg
58401e04c3fSmrg      if (options->dump_builder) {
58501e04c3fSmrg         for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
58601e04c3fSmrg            struct gl_linked_shader *shader = whole_program->_LinkedShaders[i];
58701e04c3fSmrg
58801e04c3fSmrg            if (!shader)
58901e04c3fSmrg               continue;
59001e04c3fSmrg
59101e04c3fSmrg            _mesa_print_builder_for_ir(stdout, shader->ir);
59201e04c3fSmrg         }
59301e04c3fSmrg      }
59401e04c3fSmrg   }
59501e04c3fSmrg
59601e04c3fSmrg   return whole_program;
59701e04c3fSmrg
59801e04c3fSmrgfail:
59901e04c3fSmrg   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
60001e04c3fSmrg      if (whole_program->_LinkedShaders[i])
60101e04c3fSmrg         ralloc_free(whole_program->_LinkedShaders[i]->Program);
60201e04c3fSmrg   }
60301e04c3fSmrg
60401e04c3fSmrg   ralloc_free(whole_program);
60501e04c3fSmrg   return NULL;
60601e04c3fSmrg}
60701e04c3fSmrg
60801e04c3fSmrgextern "C" void
60901e04c3fSmrgstandalone_compiler_cleanup(struct gl_shader_program *whole_program)
61001e04c3fSmrg{
61101e04c3fSmrg   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
61201e04c3fSmrg      if (whole_program->_LinkedShaders[i])
61301e04c3fSmrg         ralloc_free(whole_program->_LinkedShaders[i]->Program);
61401e04c3fSmrg   }
61501e04c3fSmrg
61601e04c3fSmrg   delete whole_program->AttributeBindings;
61701e04c3fSmrg   delete whole_program->FragDataBindings;
61801e04c3fSmrg   delete whole_program->FragDataIndexBindings;
61901e04c3fSmrg
62001e04c3fSmrg   ralloc_free(whole_program);
6217e102996Smaya   glsl_type_singleton_decref();
62201e04c3fSmrg   _mesa_glsl_release_builtin_functions();
62301e04c3fSmrg}
624