2010-06-14 14:46:09 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2008, 2009 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
2010-06-15 12:47:07 -07:00
|
|
|
#include <getopt.h>
|
2010-06-14 14:46:09 -07:00
|
|
|
|
|
|
|
|
#include "ast.h"
|
|
|
|
|
#include "glsl_parser_extras.h"
|
|
|
|
|
#include "glsl_parser.h"
|
|
|
|
|
#include "ir_optimization.h"
|
|
|
|
|
#include "ir_print_visitor.h"
|
2010-06-17 12:01:18 -07:00
|
|
|
#include "program.h"
|
2010-08-26 16:45:22 -07:00
|
|
|
#include "loop_analysis.h"
|
2010-06-14 14:46:09 -07:00
|
|
|
|
2010-07-09 14:09:34 -07:00
|
|
|
extern "C" struct gl_shader *
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type);
|
2010-07-09 14:09:34 -07:00
|
|
|
|
2010-10-14 13:28:42 -07:00
|
|
|
extern "C" void
|
|
|
|
|
_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
|
|
|
|
|
struct gl_shader *sh);
|
|
|
|
|
|
2010-07-09 14:09:34 -07:00
|
|
|
/* Copied from shader_api.c for the stand-alone compiler.
|
|
|
|
|
*/
|
2010-10-14 13:28:42 -07:00
|
|
|
void
|
|
|
|
|
_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
|
|
|
|
|
struct gl_shader *sh)
|
|
|
|
|
{
|
|
|
|
|
*ptr = sh;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-09 14:09:34 -07:00
|
|
|
struct gl_shader *
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
|
2010-07-09 14:09:34 -07:00
|
|
|
{
|
|
|
|
|
struct gl_shader *shader;
|
2010-08-02 11:46:22 -07:00
|
|
|
|
|
|
|
|
(void) ctx;
|
|
|
|
|
|
2010-07-09 14:09:34 -07:00
|
|
|
assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
|
2011-01-21 14:32:31 -08:00
|
|
|
shader = rzalloc(NULL, struct gl_shader);
|
2010-07-09 14:09:34 -07:00
|
|
|
if (shader) {
|
|
|
|
|
shader->Type = type;
|
|
|
|
|
shader->Name = name;
|
|
|
|
|
shader->RefCount = 1;
|
|
|
|
|
}
|
|
|
|
|
return shader;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-08 18:48:12 +08:00
|
|
|
static void
|
2010-10-12 12:26:10 -04:00
|
|
|
initialize_context(struct gl_context *ctx, gl_api api)
|
2010-09-08 18:48:12 +08:00
|
|
|
{
|
|
|
|
|
memset(ctx, 0, sizeof(*ctx));
|
|
|
|
|
|
|
|
|
|
ctx->API = api;
|
|
|
|
|
|
2011-01-31 14:52:27 -08:00
|
|
|
ctx->Extensions.ARB_ES2_compatibility = GL_TRUE;
|
2010-09-08 18:48:12 +08:00
|
|
|
ctx->Extensions.ARB_draw_buffers = GL_TRUE;
|
2010-12-08 18:25:38 -07:00
|
|
|
ctx->Extensions.ARB_draw_instanced = GL_TRUE;
|
2010-09-08 18:48:12 +08:00
|
|
|
ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE;
|
|
|
|
|
ctx->Extensions.EXT_texture_array = GL_TRUE;
|
|
|
|
|
ctx->Extensions.NV_texture_rectangle = GL_TRUE;
|
|
|
|
|
|
2011-01-31 14:52:27 -08:00
|
|
|
/* GLSL 1.30 isn't fully supported, but we need to advertise 1.30 so that
|
|
|
|
|
* the built-in functions for 1.30 can be built.
|
|
|
|
|
*/
|
|
|
|
|
ctx->Const.GLSLVersion = 130;
|
|
|
|
|
|
2010-09-08 18:48:12 +08:00
|
|
|
/* 1.10 minimums. */
|
|
|
|
|
ctx->Const.MaxLights = 8;
|
|
|
|
|
ctx->Const.MaxClipPlanes = 8;
|
|
|
|
|
ctx->Const.MaxTextureUnits = 2;
|
|
|
|
|
|
|
|
|
|
/* More than the 1.10 minimum to appease parser tests taken from
|
|
|
|
|
* apps that (hopefully) already checked the number of coords.
|
|
|
|
|
*/
|
|
|
|
|
ctx->Const.MaxTextureCoordUnits = 4;
|
|
|
|
|
|
|
|
|
|
ctx->Const.VertexProgram.MaxAttribs = 16;
|
|
|
|
|
ctx->Const.VertexProgram.MaxUniformComponents = 512;
|
|
|
|
|
ctx->Const.MaxVarying = 8;
|
|
|
|
|
ctx->Const.MaxVertexTextureImageUnits = 0;
|
|
|
|
|
ctx->Const.MaxCombinedTextureImageUnits = 2;
|
|
|
|
|
ctx->Const.MaxTextureImageUnits = 2;
|
|
|
|
|
ctx->Const.FragmentProgram.MaxUniformComponents = 64;
|
|
|
|
|
|
|
|
|
|
ctx->Const.MaxDrawBuffers = 2;
|
|
|
|
|
|
|
|
|
|
ctx->Driver.NewShader = _mesa_new_shader;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
/* Returned string will have 'ctx' as its ralloc owner. */
|
2010-06-14 14:46:09 -07:00
|
|
|
static char *
|
2010-06-30 11:05:43 -07:00
|
|
|
load_text_file(void *ctx, const char *file_name)
|
2010-06-14 14:46:09 -07:00
|
|
|
{
|
|
|
|
|
char *text = NULL;
|
2011-01-12 16:31:07 +00:00
|
|
|
size_t size;
|
|
|
|
|
size_t total_read = 0;
|
|
|
|
|
FILE *fp = fopen(file_name, "rb");
|
2010-06-14 14:46:09 -07:00
|
|
|
|
2011-01-12 16:31:07 +00:00
|
|
|
if (!fp) {
|
2010-06-14 14:46:09 -07:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-12 16:31:07 +00:00
|
|
|
fseek(fp, 0L, SEEK_END);
|
|
|
|
|
size = ftell(fp);
|
|
|
|
|
fseek(fp, 0L, SEEK_SET);
|
|
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
text = (char *) ralloc_size(ctx, size + 1);
|
2011-01-12 16:31:07 +00:00
|
|
|
if (text != NULL) {
|
|
|
|
|
do {
|
|
|
|
|
size_t bytes = fread(text + total_read,
|
|
|
|
|
1, size - total_read, fp);
|
|
|
|
|
if (bytes < size - total_read) {
|
|
|
|
|
free(text);
|
|
|
|
|
text = NULL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bytes == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
total_read += bytes;
|
|
|
|
|
} while (total_read < size);
|
|
|
|
|
|
|
|
|
|
text[total_read] = '\0';
|
2010-06-14 14:46:09 -07:00
|
|
|
}
|
|
|
|
|
|
2011-01-12 16:31:07 +00:00
|
|
|
fclose(fp);
|
2010-06-14 14:46:09 -07:00
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-08 18:52:27 +08:00
|
|
|
int glsl_es = 0;
|
2010-06-15 12:47:07 -07:00
|
|
|
int dump_ast = 0;
|
2010-06-25 15:27:47 -07:00
|
|
|
int dump_hir = 0;
|
2010-06-15 12:51:38 -07:00
|
|
|
int dump_lir = 0;
|
2010-06-17 19:51:48 -07:00
|
|
|
int do_link = 0;
|
2010-06-15 12:47:07 -07:00
|
|
|
|
|
|
|
|
const struct option compiler_opts[] = {
|
2010-09-08 18:52:27 +08:00
|
|
|
{ "glsl-es", 0, &glsl_es, 1 },
|
2010-06-15 12:47:07 -07:00
|
|
|
{ "dump-ast", 0, &dump_ast, 1 },
|
2010-06-25 15:27:47 -07:00
|
|
|
{ "dump-hir", 0, &dump_hir, 1 },
|
2010-06-15 12:51:38 -07:00
|
|
|
{ "dump-lir", 0, &dump_lir, 1 },
|
2010-06-17 19:51:48 -07:00
|
|
|
{ "link", 0, &do_link, 1 },
|
2010-06-15 12:47:07 -07:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
2010-11-17 15:28:36 -08:00
|
|
|
/**
|
|
|
|
|
* \brief Print proper usage and exit with failure.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
usage_fail(const char *name)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
const char *header =
|
|
|
|
|
"usage: %s [options] <file.vert | file.geom | file.frag>\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Possible options are:\n";
|
|
|
|
|
printf(header, name, name);
|
|
|
|
|
for (const struct option *o = compiler_opts; o->name != 0; ++o) {
|
|
|
|
|
printf(" --%s\n", o->name);
|
|
|
|
|
}
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-06-17 12:01:18 -07:00
|
|
|
void
|
2010-10-12 12:26:10 -04:00
|
|
|
compile_shader(struct gl_context *ctx, struct gl_shader *shader)
|
2010-06-14 14:46:09 -07:00
|
|
|
{
|
2010-07-22 16:20:36 -07:00
|
|
|
struct _mesa_glsl_parse_state *state =
|
2010-09-08 18:48:12 +08:00
|
|
|
new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
|
2010-07-20 14:03:35 -07:00
|
|
|
|
2010-06-16 12:18:00 -07:00
|
|
|
const char *source = shader->Source;
|
2010-07-22 16:20:36 -07:00
|
|
|
state->error = preprocess(state, &source, &state->info_log,
|
2011-01-14 17:45:23 -07:00
|
|
|
state->extensions, ctx->API) != 0;
|
2010-06-16 12:18:00 -07:00
|
|
|
|
2010-06-23 15:43:38 -07:00
|
|
|
if (!state->error) {
|
|
|
|
|
_mesa_glsl_lexer_ctor(state, source);
|
|
|
|
|
_mesa_glsl_parse(state);
|
|
|
|
|
_mesa_glsl_lexer_dtor(state);
|
2010-06-16 12:18:00 -07:00
|
|
|
}
|
2010-06-14 14:47:26 -07:00
|
|
|
|
2010-06-15 12:47:07 -07:00
|
|
|
if (dump_ast) {
|
2010-06-23 15:43:38 -07:00
|
|
|
foreach_list_const(n, &state->translation_unit) {
|
2010-06-15 12:47:07 -07:00
|
|
|
ast_node *ast = exec_node_data(ast_node, n, link);
|
|
|
|
|
ast->print();
|
|
|
|
|
}
|
|
|
|
|
printf("\n\n");
|
2010-06-14 14:46:09 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-30 11:05:43 -07:00
|
|
|
shader->ir = new(shader) exec_list;
|
2010-06-23 15:43:38 -07:00
|
|
|
if (!state->error && !state->translation_unit.is_empty())
|
2010-06-30 11:05:43 -07:00
|
|
|
_mesa_ast_to_hir(shader->ir, state);
|
2010-06-14 14:47:26 -07:00
|
|
|
|
2010-06-25 15:27:47 -07:00
|
|
|
/* Print out the unoptimized IR. */
|
|
|
|
|
if (!state->error && dump_hir) {
|
2010-07-20 11:37:45 -07:00
|
|
|
validate_ir_tree(shader->ir);
|
2010-06-30 11:05:43 -07:00
|
|
|
_mesa_print_ir(shader->ir, state);
|
2010-06-25 15:27:47 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-14 14:46:09 -07:00
|
|
|
/* Optimization passes */
|
2010-06-30 11:05:43 -07:00
|
|
|
if (!state->error && !shader->ir->is_empty()) {
|
2010-06-14 14:46:09 -07:00
|
|
|
bool progress;
|
|
|
|
|
do {
|
2010-11-24 23:54:03 -08:00
|
|
|
progress = do_common_optimization(shader->ir, false, 32);
|
2010-06-14 14:46:09 -07:00
|
|
|
} while (progress);
|
2010-07-20 11:37:45 -07:00
|
|
|
|
|
|
|
|
validate_ir_tree(shader->ir);
|
2010-06-14 14:46:09 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-22 12:07:21 -07:00
|
|
|
|
2010-06-14 14:46:09 -07:00
|
|
|
/* Print out the resulting IR */
|
2010-06-23 15:43:38 -07:00
|
|
|
if (!state->error && dump_lir) {
|
2010-06-30 11:05:43 -07:00
|
|
|
_mesa_print_ir(shader->ir, state);
|
2010-06-14 14:46:09 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-23 15:43:38 -07:00
|
|
|
shader->symbols = state->symbols;
|
|
|
|
|
shader->CompileStatus = !state->error;
|
2010-07-16 15:51:50 -07:00
|
|
|
shader->Version = state->language_version;
|
2010-07-20 11:29:46 -07:00
|
|
|
memcpy(shader->builtins_to_link, state->builtins_to_link,
|
|
|
|
|
sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
|
|
|
|
|
shader->num_builtins_to_link = state->num_builtins_to_link;
|
2010-06-19 01:39:14 -07:00
|
|
|
|
|
|
|
|
if (shader->InfoLog)
|
2011-01-21 14:32:31 -08:00
|
|
|
ralloc_free(shader->InfoLog);
|
2010-06-19 01:39:14 -07:00
|
|
|
|
2010-06-23 15:43:38 -07:00
|
|
|
shader->InfoLog = state->info_log;
|
|
|
|
|
|
2010-06-25 14:10:01 -07:00
|
|
|
/* Retain any live IR, but trash the rest. */
|
2010-07-20 11:27:38 -07:00
|
|
|
reparent_ir(shader->ir, shader);
|
2010-06-25 14:10:01 -07:00
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
ralloc_free(state);
|
2010-06-19 01:39:14 -07:00
|
|
|
|
2010-06-17 12:01:18 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main(int argc, char **argv)
|
|
|
|
|
{
|
2010-06-17 12:22:16 -07:00
|
|
|
int status = EXIT_SUCCESS;
|
2010-10-12 12:26:10 -04:00
|
|
|
struct gl_context local_ctx;
|
|
|
|
|
struct gl_context *ctx = &local_ctx;
|
2010-08-18 12:02:35 -07:00
|
|
|
|
2010-06-17 12:01:18 -07:00
|
|
|
int c;
|
|
|
|
|
int idx = 0;
|
|
|
|
|
while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
|
|
|
|
|
/* empty */ ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (argc <= optind)
|
|
|
|
|
usage_fail(argv[0]);
|
|
|
|
|
|
2010-09-08 18:52:27 +08:00
|
|
|
initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL);
|
2010-09-08 18:48:12 +08:00
|
|
|
|
2010-06-30 11:49:17 -07:00
|
|
|
struct gl_shader_program *whole_program;
|
2010-06-23 13:34:05 -07:00
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
whole_program = rzalloc (NULL, struct gl_shader_program);
|
2010-06-23 13:34:05 -07:00
|
|
|
assert(whole_program != NULL);
|
2010-06-17 12:01:18 -07:00
|
|
|
|
2010-06-17 12:22:16 -07:00
|
|
|
for (/* empty */; argc > optind; optind++) {
|
2011-01-21 14:32:31 -08:00
|
|
|
whole_program->Shaders =
|
|
|
|
|
reralloc(whole_program, whole_program->Shaders,
|
|
|
|
|
struct gl_shader *, whole_program->NumShaders + 1);
|
2010-06-23 13:34:05 -07:00
|
|
|
assert(whole_program->Shaders != NULL);
|
2010-06-17 12:01:18 -07:00
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
struct gl_shader *shader = rzalloc(whole_program, gl_shader);
|
2010-06-17 12:22:16 -07:00
|
|
|
|
2010-06-23 13:34:05 -07:00
|
|
|
whole_program->Shaders[whole_program->NumShaders] = shader;
|
|
|
|
|
whole_program->NumShaders++;
|
2010-06-17 12:22:16 -07:00
|
|
|
|
|
|
|
|
const unsigned len = strlen(argv[optind]);
|
|
|
|
|
if (len < 6)
|
|
|
|
|
usage_fail(argv[0]);
|
2010-06-17 12:01:18 -07:00
|
|
|
|
2010-06-17 12:22:16 -07:00
|
|
|
const char *const ext = & argv[optind][len - 5];
|
|
|
|
|
if (strncmp(".vert", ext, 5) == 0)
|
2010-06-17 15:28:34 -07:00
|
|
|
shader->Type = GL_VERTEX_SHADER;
|
2010-06-17 12:22:16 -07:00
|
|
|
else if (strncmp(".geom", ext, 5) == 0)
|
2010-06-17 15:28:34 -07:00
|
|
|
shader->Type = GL_GEOMETRY_SHADER;
|
2010-06-17 12:22:16 -07:00
|
|
|
else if (strncmp(".frag", ext, 5) == 0)
|
2010-06-17 15:28:34 -07:00
|
|
|
shader->Type = GL_FRAGMENT_SHADER;
|
2010-06-17 12:22:16 -07:00
|
|
|
else
|
|
|
|
|
usage_fail(argv[0]);
|
2010-06-17 12:01:18 -07:00
|
|
|
|
2010-06-30 11:05:43 -07:00
|
|
|
shader->Source = load_text_file(whole_program, argv[optind]);
|
2010-06-16 12:10:55 -07:00
|
|
|
if (shader->Source == NULL) {
|
|
|
|
|
printf("File \"%s\" does not exist.\n", argv[optind]);
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
2010-06-17 12:22:16 -07:00
|
|
|
|
2010-09-08 18:48:12 +08:00
|
|
|
compile_shader(ctx, shader);
|
2010-06-17 12:22:16 -07:00
|
|
|
|
2010-06-17 15:28:34 -07:00
|
|
|
if (!shader->CompileStatus) {
|
2010-06-19 11:31:01 -07:00
|
|
|
printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
|
2010-06-17 12:22:16 -07:00
|
|
|
status = EXIT_FAILURE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-06-14 14:46:09 -07:00
|
|
|
|
2010-06-17 19:51:48 -07:00
|
|
|
if ((status == EXIT_SUCCESS) && do_link) {
|
2010-08-18 12:02:35 -07:00
|
|
|
link_shaders(ctx, whole_program);
|
2010-06-23 13:34:05 -07:00
|
|
|
status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
|
2010-07-02 14:43:01 -07:00
|
|
|
|
|
|
|
|
if (strlen(whole_program->InfoLog) > 0)
|
|
|
|
|
printf("Info log for linking:\n%s\n", whole_program->InfoLog);
|
2010-06-17 15:04:20 -07:00
|
|
|
}
|
|
|
|
|
|
2010-10-14 13:28:42 -07:00
|
|
|
for (unsigned i = 0; i < MESA_SHADER_TYPES; i++)
|
2011-01-21 14:32:31 -08:00
|
|
|
ralloc_free(whole_program->_LinkedShaders[i]);
|
2010-07-09 14:09:34 -07:00
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
ralloc_free(whole_program);
|
2010-06-28 13:17:18 -07:00
|
|
|
_mesa_glsl_release_types();
|
2010-07-20 11:29:46 -07:00
|
|
|
_mesa_glsl_release_functions();
|
2010-06-23 13:34:05 -07:00
|
|
|
|
2010-06-17 12:22:16 -07:00
|
|
|
return status;
|
2010-06-14 14:46:09 -07:00
|
|
|
}
|