2010-05-10 11:44:09 -07:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 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.
|
|
|
|
*/
|
|
|
|
|
2011-03-04 12:53:14 +00:00
|
|
|
#include <stdio.h>
|
2010-08-23 09:42:14 -07:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2010-05-10 13:17:25 -07:00
|
|
|
#include "glcpp.h"
|
2010-09-05 01:27:02 -07:00
|
|
|
#include "main/mtypes.h"
|
2010-10-14 13:28:42 -07:00
|
|
|
#include "main/shaderobj.h"
|
2010-05-10 13:17:25 -07:00
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
extern int yydebug;
|
|
|
|
|
2010-10-14 13:28:42 -07:00
|
|
|
void
|
|
|
|
_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
|
|
|
|
struct gl_shader *sh)
|
|
|
|
{
|
2011-08-29 14:56:29 -07:00
|
|
|
(void) ctx;
|
2010-10-14 13:28:42 -07:00
|
|
|
*ptr = sh;
|
|
|
|
}
|
|
|
|
|
2011-03-04 12:53:14 +00:00
|
|
|
/* Read from fp until EOF and return a string of everything read.
|
2010-08-23 10:43:27 -07:00
|
|
|
*/
|
2010-06-23 14:00:27 -07:00
|
|
|
static char *
|
2011-03-04 12:53:14 +00:00
|
|
|
load_text_fp (void *ctx, FILE *fp)
|
2010-05-10 11:44:09 -07:00
|
|
|
{
|
2010-08-23 10:43:27 -07:00
|
|
|
#define CHUNK 4096
|
2010-06-23 14:00:27 -07:00
|
|
|
char *text = NULL;
|
2011-03-04 12:53:14 +00:00
|
|
|
size_t text_size = 0;
|
|
|
|
size_t total_read = 0;
|
|
|
|
size_t bytes;
|
2010-08-23 10:43:27 -07:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (total_read + CHUNK + 1 > text_size) {
|
|
|
|
text_size = text_size ? text_size * 2 : CHUNK + 1;
|
2011-01-21 14:32:31 -08:00
|
|
|
text = reralloc_size (ctx, text, text_size);
|
2010-08-23 10:43:27 -07:00
|
|
|
if (text == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-03-04 12:53:14 +00:00
|
|
|
bytes = fread (text + total_read, 1, CHUNK, fp);
|
|
|
|
total_read += bytes;
|
2010-08-23 09:42:14 -07:00
|
|
|
|
2011-03-04 12:53:14 +00:00
|
|
|
if (bytes < CHUNK) {
|
2010-08-23 10:43:27 -07:00
|
|
|
break;
|
|
|
|
}
|
2010-06-23 14:00:27 -07:00
|
|
|
}
|
|
|
|
|
2010-08-23 10:43:27 -07:00
|
|
|
text[total_read] = '\0';
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
load_text_file(void *ctx, const char *filename)
|
|
|
|
{
|
|
|
|
char *text;
|
2011-03-04 12:53:14 +00:00
|
|
|
FILE *fp;
|
2010-08-23 10:43:27 -07:00
|
|
|
|
|
|
|
if (filename == NULL || strcmp (filename, "-") == 0)
|
2011-03-04 12:53:14 +00:00
|
|
|
return load_text_fp (ctx, stdin);
|
2010-08-23 10:43:27 -07:00
|
|
|
|
2011-03-04 12:53:14 +00:00
|
|
|
fp = fopen (filename, "r");
|
|
|
|
if (fp == NULL) {
|
2010-08-23 10:43:27 -07:00
|
|
|
fprintf (stderr, "Failed to open file %s: %s\n",
|
|
|
|
filename, strerror (errno));
|
|
|
|
return NULL;
|
2010-06-23 14:00:27 -07:00
|
|
|
}
|
2010-05-10 11:52:29 -07:00
|
|
|
|
2011-03-04 12:53:14 +00:00
|
|
|
text = load_text_fp (ctx, fp);
|
2010-08-23 10:43:27 -07:00
|
|
|
|
2011-03-04 12:53:14 +00:00
|
|
|
fclose(fp);
|
2010-05-10 16:16:06 -07:00
|
|
|
|
2010-06-23 14:00:27 -07:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-08-17 23:13:56 -07:00
|
|
|
main (int argc, char *argv[])
|
2010-06-23 14:00:27 -07:00
|
|
|
{
|
2010-08-17 23:13:56 -07:00
|
|
|
char *filename = NULL;
|
2011-01-21 14:32:31 -08:00
|
|
|
void *ctx = ralloc(NULL, void*);
|
|
|
|
char *info_log = ralloc_strdup(ctx, "");
|
2010-08-17 23:13:56 -07:00
|
|
|
const char *shader;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (argc) {
|
|
|
|
filename = argv[1];
|
|
|
|
}
|
|
|
|
|
2010-08-23 10:43:27 -07:00
|
|
|
shader = load_text_file (ctx, filename);
|
2010-08-23 09:42:14 -07:00
|
|
|
if (shader == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2012-11-27 12:26:51 -08:00
|
|
|
ret = glcpp_preprocess(ctx, &shader, &info_log, NULL, API_OPENGL_COMPAT);
|
2010-05-10 16:16:06 -07:00
|
|
|
|
2010-06-23 14:00:27 -07:00
|
|
|
printf("%s", shader);
|
|
|
|
fprintf(stderr, "%s", info_log);
|
2010-06-16 11:57:48 -07:00
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
ralloc_free(ctx);
|
2010-05-10 11:52:29 -07:00
|
|
|
|
|
|
|
return ret;
|
2010-05-10 11:44:09 -07:00
|
|
|
}
|