Revert "Removed HLSL cry compilers and tools"
This reverts commit f8a72e5a04.
This commit is contained in:
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
Copyright (c) 2009 Dave Gamble
|
||||
|
||||
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 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.
|
||||
*/
|
||||
|
||||
Welcome to cJSON.
|
||||
|
||||
cJSON aims to be the dumbest possible parser that you can get your job done with.
|
||||
It's a single file of C, and a single header file.
|
||||
|
||||
JSON is described best here: http://www.json.org/
|
||||
It's like XML, but fat-free. You use it to move data around, store things, or just
|
||||
generally represent your program's state.
|
||||
|
||||
|
||||
First up, how do I build?
|
||||
Add cJSON.c to your project, and put cJSON.h somewhere in the header search path.
|
||||
For example, to build the test app:
|
||||
|
||||
gcc cJSON.c test.c -o test -lm
|
||||
./test
|
||||
|
||||
|
||||
As a library, cJSON exists to take away as much legwork as it can, but not get in your way.
|
||||
As a point of pragmatism (i.e. ignoring the truth), I'm going to say that you can use it
|
||||
in one of two modes: Auto and Manual. Let's have a quick run-through.
|
||||
|
||||
|
||||
I lifted some JSON from this page: http://www.json.org/fatfree.html
|
||||
That page inspired me to write cJSON, which is a parser that tries to share the same
|
||||
philosophy as JSON itself. Simple, dumb, out of the way.
|
||||
|
||||
Some JSON:
|
||||
{
|
||||
"name": "Jack (\"Bee\") Nimble",
|
||||
"format": {
|
||||
"type": "rect",
|
||||
"width": 1920,
|
||||
"height": 1080,
|
||||
"interlace": false,
|
||||
"frame rate": 24
|
||||
}
|
||||
}
|
||||
|
||||
Assume that you got this from a file, a webserver, or magic JSON elves, whatever,
|
||||
you have a char * to it. Everything is a cJSON struct.
|
||||
Get it parsed:
|
||||
cJSON *root = cJSON_Parse(my_json_string);
|
||||
|
||||
This is an object. We're in C. We don't have objects. But we do have structs.
|
||||
What's the framerate?
|
||||
|
||||
cJSON *format = cJSON_GetObjectItem(root,"format");
|
||||
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
|
||||
|
||||
|
||||
Want to change the framerate?
|
||||
cJSON_GetObjectItem(format,"frame rate")->valueint=25;
|
||||
|
||||
Back to disk?
|
||||
char *rendered=cJSON_Print(root);
|
||||
|
||||
Finished? Delete the root (this takes care of everything else).
|
||||
cJSON_Delete(root);
|
||||
|
||||
That's AUTO mode. If you're going to use Auto mode, you really ought to check pointers
|
||||
before you dereference them. If you want to see how you'd build this struct in code?
|
||||
cJSON *root,*fmt;
|
||||
root=cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
|
||||
cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());
|
||||
cJSON_AddStringToObject(fmt,"type", "rect");
|
||||
cJSON_AddNumberToObject(fmt,"width", 1920);
|
||||
cJSON_AddNumberToObject(fmt,"height", 1080);
|
||||
cJSON_AddFalseToObject (fmt,"interlace");
|
||||
cJSON_AddNumberToObject(fmt,"frame rate", 24);
|
||||
|
||||
Hopefully we can agree that's not a lot of code? There's no overhead, no unnecessary setup.
|
||||
Look at test.c for a bunch of nice examples, mostly all ripped off the json.org site, and
|
||||
a few from elsewhere.
|
||||
|
||||
What about manual mode? First up you need some detail.
|
||||
Let's cover how the cJSON objects represent the JSON data.
|
||||
cJSON doesn't distinguish arrays from objects in handling; just type.
|
||||
Each cJSON has, potentially, a child, siblings, value, a name.
|
||||
|
||||
The root object has: Object Type and a Child
|
||||
The Child has name "name", with value "Jack ("Bee") Nimble", and a sibling:
|
||||
Sibling has type Object, name "format", and a child.
|
||||
That child has type String, name "type", value "rect", and a sibling:
|
||||
Sibling has type Number, name "width", value 1920, and a sibling:
|
||||
Sibling has type Number, name "height", value 1080, and a sibling:
|
||||
Sibling hs type False, name "interlace", and a sibling:
|
||||
Sibling has type Number, name "frame rate", value 24
|
||||
|
||||
Here's the structure:
|
||||
typedef struct cJSON {
|
||||
struct cJSON *next,*prev;
|
||||
struct cJSON *child;
|
||||
|
||||
int type;
|
||||
|
||||
char *valuestring;
|
||||
int valueint;
|
||||
double valuedouble;
|
||||
|
||||
char *string;
|
||||
} cJSON;
|
||||
|
||||
By default all values are 0 unless set by virtue of being meaningful.
|
||||
|
||||
next/prev is a doubly linked list of siblings. next takes you to your sibling,
|
||||
prev takes you back from your sibling to you.
|
||||
Only objects and arrays have a "child", and it's the head of the doubly linked list.
|
||||
A "child" entry will have prev==0, but next potentially points on. The last sibling has next=0.
|
||||
The type expresses Null/True/False/Number/String/Array/Object, all of which are #defined in
|
||||
cJSON.h
|
||||
|
||||
A Number has valueint and valuedouble. If you're expecting an int, read valueint, if not read
|
||||
valuedouble.
|
||||
|
||||
Any entry which is in the linked list which is the child of an object will have a "string"
|
||||
which is the "name" of the entry. When I said "name" in the above example, that's "string".
|
||||
"string" is the JSON name for the 'variable name' if you will.
|
||||
|
||||
Now you can trivially walk the lists, recursively, and parse as you please.
|
||||
You can invoke cJSON_Parse to get cJSON to parse for you, and then you can take
|
||||
the root object, and traverse the structure (which is, formally, an N-tree),
|
||||
and tokenise as you please. If you wanted to build a callback style parser, this is how
|
||||
you'd do it (just an example, since these things are very specific):
|
||||
|
||||
void parse_and_callback(cJSON *item,const char *prefix)
|
||||
{
|
||||
while (item)
|
||||
{
|
||||
char *newprefix=malloc(strlen(prefix)+strlen(item->name)+2);
|
||||
sprintf(newprefix,"%s/%s",prefix,item->name);
|
||||
int dorecurse=callback(newprefix, item->type, item);
|
||||
if (item->child && dorecurse) parse_and_callback(item->child,newprefix);
|
||||
item=item->next;
|
||||
free(newprefix);
|
||||
}
|
||||
}
|
||||
|
||||
The prefix process will build you a separated list, to simplify your callback handling.
|
||||
The 'dorecurse' flag would let the callback decide to handle sub-arrays on it's own, or
|
||||
let you invoke it per-item. For the item above, your callback might look like this:
|
||||
|
||||
int callback(const char *name,int type,cJSON *item)
|
||||
{
|
||||
if (!strcmp(name,"name")) { /* populate name */ }
|
||||
else if (!strcmp(name,"format/type") { /* handle "rect" */ }
|
||||
else if (!strcmp(name,"format/width") { /* 800 */ }
|
||||
else if (!strcmp(name,"format/height") { /* 600 */ }
|
||||
else if (!strcmp(name,"format/interlace") { /* false */ }
|
||||
else if (!strcmp(name,"format/frame rate") { /* 24 */ }
|
||||
return 1;
|
||||
}
|
||||
|
||||
Alternatively, you might like to parse iteratively.
|
||||
You'd use:
|
||||
|
||||
void parse_object(cJSON *item)
|
||||
{
|
||||
int i; for (i=0;i<cJSON_GetArraySize(item);i++)
|
||||
{
|
||||
cJSON *subitem=cJSON_GetArrayItem(item,i);
|
||||
// handle subitem.
|
||||
}
|
||||
}
|
||||
|
||||
Or, for PROPER manual mode:
|
||||
|
||||
void parse_object(cJSON *item)
|
||||
{
|
||||
cJSON *subitem=item->child;
|
||||
while (subitem)
|
||||
{
|
||||
// handle subitem
|
||||
if (subitem->child) parse_object(subitem->child);
|
||||
|
||||
subitem=subitem->next;
|
||||
}
|
||||
}
|
||||
|
||||
Of course, this should look familiar, since this is just a stripped-down version
|
||||
of the callback-parser.
|
||||
|
||||
This should cover most uses you'll find for parsing. The rest should be possible
|
||||
to infer.. and if in doubt, read the source! There's not a lot of it! ;)
|
||||
|
||||
|
||||
In terms of constructing JSON data, the example code above is the right way to do it.
|
||||
You can, of course, hand your sub-objects to other functions to populate.
|
||||
Also, if you find a use for it, you can manually build the objects.
|
||||
For instance, suppose you wanted to build an array of objects?
|
||||
|
||||
cJSON *objects[24];
|
||||
|
||||
cJSON *Create_array_of_anything(cJSON **items,int num)
|
||||
{
|
||||
int i;cJSON *prev, *root=cJSON_CreateArray();
|
||||
for (i=0;i<24;i++)
|
||||
{
|
||||
if (!i) root->child=objects[i];
|
||||
else prev->next=objects[i], objects[i]->prev=prev;
|
||||
prev=objects[i];
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
and simply: Create_array_of_anything(objects,24);
|
||||
|
||||
cJSON doesn't make any assumptions about what order you create things in.
|
||||
You can attach the objects, as above, and later add children to each
|
||||
of those objects.
|
||||
|
||||
As soon as you call cJSON_Print, it renders the structure to text.
|
||||
|
||||
|
||||
|
||||
The test.c code shows how to handle a bunch of typical cases. If you uncomment
|
||||
the code, it'll load, parse and print a bunch of test files, also from json.org,
|
||||
which are more complex than I'd care to try and stash into a const char array[].
|
||||
|
||||
|
||||
Enjoy cJSON!
|
||||
|
||||
|
||||
- Dave Gamble, Aug 2009
|
||||
@@ -0,0 +1,578 @@
|
||||
/*
|
||||
Copyright (c) 2009 Dave Gamble
|
||||
|
||||
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 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.
|
||||
*/
|
||||
// Modifications copyright Amazon.com, Inc. or its affiliates.
|
||||
|
||||
/* cJSON */
|
||||
/* JSON parser in C. */
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <float.h>
|
||||
#include <limits.h>
|
||||
#include <ctype.h>
|
||||
#include "cJSON.h"
|
||||
#include <AzCore/PlatformDef.h>
|
||||
|
||||
static const char *ep;
|
||||
|
||||
const char *cJSON_GetErrorPtr(void) {return ep;}
|
||||
|
||||
static int cJSON_strcasecmp(const char *s1,const char *s2)
|
||||
{
|
||||
if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
|
||||
for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0;
|
||||
return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
|
||||
}
|
||||
|
||||
AZ_PUSH_DISABLE_WARNING(4232, "-Wunknown-warning-option") // address of malloc/free are not static
|
||||
static void *(*cJSON_malloc)(size_t sz) = malloc;
|
||||
static void (*cJSON_free)(void *ptr) = free;
|
||||
AZ_POP_DISABLE_WARNING
|
||||
|
||||
static char* cJSON_strdup(const char* str)
|
||||
{
|
||||
size_t len = strlen(str) + 1;
|
||||
char* copy = (char*)cJSON_malloc(len);
|
||||
|
||||
if (!copy) return 0;
|
||||
memcpy(copy,str,len);
|
||||
return copy;
|
||||
}
|
||||
|
||||
void cJSON_InitHooks(cJSON_Hooks* hooks)
|
||||
{
|
||||
if (!hooks) { /* Reset hooks */
|
||||
cJSON_malloc = malloc;
|
||||
cJSON_free = free;
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
|
||||
cJSON_free = (hooks->free_fn)?hooks->free_fn:free;
|
||||
}
|
||||
|
||||
/* Internal constructor. */
|
||||
static cJSON *cJSON_New_Item(void)
|
||||
{
|
||||
cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
|
||||
if (node) memset(node,0,sizeof(cJSON));
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Delete a cJSON structure. */
|
||||
void cJSON_Delete(cJSON *c)
|
||||
{
|
||||
cJSON *next;
|
||||
while (c)
|
||||
{
|
||||
next=c->next;
|
||||
if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child);
|
||||
if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
|
||||
if (c->string) cJSON_free(c->string);
|
||||
cJSON_free(c);
|
||||
c=next;
|
||||
}
|
||||
}
|
||||
|
||||
/* Parse the input text to generate a number, and populate the result into item. */
|
||||
static const char *parse_number(cJSON *item,const char *num)
|
||||
{
|
||||
double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;
|
||||
|
||||
/* Could use sscanf for this? */
|
||||
if (*num=='-') sign=-1,num++; /* Has sign? */
|
||||
if (*num=='0') num++; /* is zero */
|
||||
if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */
|
||||
if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */
|
||||
if (*num=='e' || *num=='E') /* Exponent? */
|
||||
{ num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */
|
||||
while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */
|
||||
}
|
||||
|
||||
n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
|
||||
|
||||
item->valuedouble=n;
|
||||
item->valueint=(int)n;
|
||||
item->type=cJSON_Number;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Render the number nicely from the given item into a string. */
|
||||
static char *print_number(cJSON *item)
|
||||
{
|
||||
char *str;
|
||||
double d=item->valuedouble;
|
||||
if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
|
||||
{
|
||||
str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
|
||||
if (str) sprintf(str,"%d",item->valueint);
|
||||
}
|
||||
else
|
||||
{
|
||||
str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */
|
||||
if (str)
|
||||
{
|
||||
if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60)sprintf(str,"%.0f",d);
|
||||
else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d);
|
||||
else sprintf(str,"%f",d);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/* Parse the input text into an unescaped cstring, and populate item. */
|
||||
static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
|
||||
static const char *parse_string(cJSON *item,const char *str)
|
||||
{
|
||||
const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2;
|
||||
if (*str!='\"') {ep=str;return 0;} /* not a string! */
|
||||
|
||||
while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
|
||||
|
||||
out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */
|
||||
if (!out) return 0;
|
||||
|
||||
ptr=str+1;ptr2=out;
|
||||
while (*ptr!='\"' && *ptr)
|
||||
{
|
||||
if (*ptr!='\\') *ptr2++=*ptr++;
|
||||
else
|
||||
{
|
||||
ptr++;
|
||||
switch (*ptr)
|
||||
{
|
||||
case 'b': *ptr2++='\b'; break;
|
||||
case 'f': *ptr2++='\f'; break;
|
||||
case 'n': *ptr2++='\n'; break;
|
||||
case 'r': *ptr2++='\r'; break;
|
||||
case 't': *ptr2++='\t'; break;
|
||||
case 'u': /* transcode utf16 to utf8. */
|
||||
sscanf(ptr+1,"%4x",&uc);ptr+=4; /* get the unicode char. */
|
||||
|
||||
if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; /* check for invalid. */
|
||||
|
||||
if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */
|
||||
{
|
||||
if (ptr[1]!='\\' || ptr[2]!='u') break; /* missing second-half of surrogate. */
|
||||
sscanf(ptr+3,"%4x",&uc2);ptr+=6;
|
||||
if (uc2<0xDC00 || uc2>0xDFFF) break; /* invalid second-half of surrogate. */
|
||||
uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF));
|
||||
}
|
||||
|
||||
len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len;
|
||||
|
||||
switch (len) {
|
||||
case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
|
||||
case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
|
||||
case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
|
||||
case 1: *--ptr2 =(uc | firstByteMark[len]);
|
||||
}
|
||||
ptr2+=len;
|
||||
break;
|
||||
default: *ptr2++=*ptr; break;
|
||||
}
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
*ptr2=0;
|
||||
if (*ptr=='\"') ptr++;
|
||||
item->valuestring=out;
|
||||
item->type=cJSON_String;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/* Render the cstring provided to an escaped version that can be printed. */
|
||||
static char *print_string_ptr(const char *str)
|
||||
{
|
||||
const char *ptr;char *ptr2,*out;int len=0;unsigned char token;
|
||||
|
||||
if (!str) return cJSON_strdup("");
|
||||
ptr=str;
|
||||
token = *ptr;
|
||||
while (token && ++len)
|
||||
{
|
||||
if (strchr("\"\\\b\f\n\r\t",token)) len++;
|
||||
else if (token<32) len+=5;
|
||||
ptr++;
|
||||
token = *ptr;
|
||||
}
|
||||
|
||||
out=(char*)cJSON_malloc(len+3);
|
||||
if (!out) return 0;
|
||||
|
||||
ptr2=out;ptr=str;
|
||||
*ptr2++='\"';
|
||||
while (*ptr)
|
||||
{
|
||||
if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
|
||||
else
|
||||
{
|
||||
*ptr2++='\\';
|
||||
switch (token=*ptr++)
|
||||
{
|
||||
case '\\': *ptr2++='\\'; break;
|
||||
case '\"': *ptr2++='\"'; break;
|
||||
case '\b': *ptr2++='b'; break;
|
||||
case '\f': *ptr2++='f'; break;
|
||||
case '\n': *ptr2++='n'; break;
|
||||
case '\r': *ptr2++='r'; break;
|
||||
case '\t': *ptr2++='t'; break;
|
||||
default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */
|
||||
}
|
||||
}
|
||||
}
|
||||
*ptr2++='\"';*ptr2++=0;
|
||||
return out;
|
||||
}
|
||||
/* Invote print_string_ptr (which is useful) on an item. */
|
||||
static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);}
|
||||
|
||||
/* Predeclare these prototypes. */
|
||||
static const char *parse_value(cJSON *item,const char *value);
|
||||
static char *print_value(cJSON *item,int depth,int fmt);
|
||||
static const char *parse_array(cJSON *item,const char *value);
|
||||
static char *print_array(cJSON *item,int depth,int fmt);
|
||||
static const char *parse_object(cJSON *item,const char *value);
|
||||
static char *print_object(cJSON *item,int depth,int fmt);
|
||||
|
||||
/* Utility to jump whitespace and cr/lf */
|
||||
static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;}
|
||||
|
||||
/* Parse an object - create a new root, and populate. */
|
||||
cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated)
|
||||
{
|
||||
const char *end=0;
|
||||
cJSON *c=cJSON_New_Item();
|
||||
ep=0;
|
||||
if (!c) return 0; /* memory fail */
|
||||
|
||||
end=parse_value(c,skip(value));
|
||||
if (!end) {cJSON_Delete(c);return 0;} /* parse failure. ep is set. */
|
||||
|
||||
/* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
|
||||
if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}}
|
||||
if (return_parse_end) *return_parse_end=end;
|
||||
return c;
|
||||
}
|
||||
/* Default options for cJSON_Parse */
|
||||
cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);}
|
||||
|
||||
/* Render a cJSON item/entity/structure to text. */
|
||||
char *cJSON_Print(cJSON *item) {return print_value(item,0,1);}
|
||||
char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);}
|
||||
|
||||
/* Parser core - when encountering text, process appropriately. */
|
||||
static const char *parse_value(cJSON *item,const char *value)
|
||||
{
|
||||
if (!value) return 0; /* Fail on null. */
|
||||
if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; }
|
||||
if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; }
|
||||
if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; }
|
||||
if (*value=='\"') { return parse_string(item,value); }
|
||||
if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); }
|
||||
if (*value=='[') { return parse_array(item,value); }
|
||||
if (*value=='{') { return parse_object(item,value); }
|
||||
|
||||
ep=value;return 0; /* failure. */
|
||||
}
|
||||
|
||||
/* Render a value to text. */
|
||||
static char *print_value(cJSON *item,int depth,int fmt)
|
||||
{
|
||||
char *out=0;
|
||||
if (!item) return 0;
|
||||
switch ((item->type)&255)
|
||||
{
|
||||
case cJSON_NULL: out=cJSON_strdup("null"); break;
|
||||
case cJSON_False: out=cJSON_strdup("false");break;
|
||||
case cJSON_True: out=cJSON_strdup("true"); break;
|
||||
case cJSON_Number: out=print_number(item);break;
|
||||
case cJSON_String: out=print_string(item);break;
|
||||
case cJSON_Array: out=print_array(item,depth,fmt);break;
|
||||
case cJSON_Object: out=print_object(item,depth,fmt);break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/* Build an array from input text. */
|
||||
static const char *parse_array(cJSON *item,const char *value)
|
||||
{
|
||||
cJSON *child;
|
||||
if (*value!='[') {ep=value;return 0;} /* not an array! */
|
||||
|
||||
item->type=cJSON_Array;
|
||||
value=skip(value+1);
|
||||
if (*value==']') return value+1; /* empty array. */
|
||||
|
||||
item->child=child=cJSON_New_Item();
|
||||
if (!item->child) return 0; /* memory fail */
|
||||
value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */
|
||||
if (!value) return 0;
|
||||
|
||||
while (*value==',')
|
||||
{
|
||||
cJSON *new_item = cJSON_New_Item();
|
||||
if (!new_item) return 0; /* memory fail */
|
||||
child->next=new_item;new_item->prev=child;child=new_item;
|
||||
value=skip(parse_value(child,skip(value+1)));
|
||||
if (!value) return 0; /* memory fail */
|
||||
}
|
||||
|
||||
if (*value==']') return value+1; /* end of array */
|
||||
ep=value;return 0; /* malformed. */
|
||||
}
|
||||
|
||||
/* Render an array to text */
|
||||
static char *print_array(cJSON *item,int depth,int fmt)
|
||||
{
|
||||
char **entries;
|
||||
char *out=0,*ptr,*ret;int len=5;
|
||||
cJSON *child=item->child;
|
||||
int numentries=0,i=0,fail=0;
|
||||
|
||||
/* How many entries in the array? */
|
||||
while (child) numentries++,child=child->next;
|
||||
/* Explicitly handle numentries==0 */
|
||||
if (!numentries)
|
||||
{
|
||||
out=(char*)cJSON_malloc(3);
|
||||
if (out) strcpy(out,"[]");
|
||||
return out;
|
||||
}
|
||||
/* Allocate an array to hold the values for each */
|
||||
entries=(char**)cJSON_malloc(numentries*sizeof(char*));
|
||||
if (!entries) return 0;
|
||||
memset(entries,0,numentries*sizeof(char*));
|
||||
/* Retrieve all the results: */
|
||||
child=item->child;
|
||||
while (child && !fail)
|
||||
{
|
||||
ret=print_value(child,depth+1,fmt);
|
||||
entries[i++]=ret;
|
||||
if (ret) len+=(int)strlen(ret)+2+(fmt?1:0); else fail=1;
|
||||
child=child->next;
|
||||
}
|
||||
|
||||
/* If we didn't fail, try to malloc the output string */
|
||||
if (!fail) out=(char*)cJSON_malloc(len);
|
||||
/* If that fails, we fail. */
|
||||
if (!out) fail=1;
|
||||
|
||||
/* Handle failure. */
|
||||
if (fail)
|
||||
{
|
||||
for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]);
|
||||
cJSON_free(entries);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Compose the output array. */
|
||||
*out='[';
|
||||
ptr=out+1;*ptr=0;
|
||||
for (i=0;i<numentries;i++)
|
||||
{
|
||||
strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
|
||||
if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}
|
||||
cJSON_free(entries[i]);
|
||||
}
|
||||
cJSON_free(entries);
|
||||
*ptr++=']';*ptr++=0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/* Build an object from the text. */
|
||||
static const char *parse_object(cJSON *item,const char *value)
|
||||
{
|
||||
cJSON *child;
|
||||
if (*value!='{') {ep=value;return 0;} /* not an object! */
|
||||
|
||||
item->type=cJSON_Object;
|
||||
value=skip(value+1);
|
||||
if (*value=='}') return value+1; /* empty array. */
|
||||
|
||||
item->child=child=cJSON_New_Item();
|
||||
if (!item->child) return 0;
|
||||
value=skip(parse_string(child,skip(value)));
|
||||
if (!value) return 0;
|
||||
child->string=child->valuestring;child->valuestring=0;
|
||||
if (*value!=':') {ep=value;return 0;} /* fail! */
|
||||
value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
|
||||
if (!value) return 0;
|
||||
|
||||
while (*value==',')
|
||||
{
|
||||
cJSON *new_item = cJSON_New_Item();
|
||||
if (!new_item) return 0; /* memory fail */
|
||||
child->next=new_item;new_item->prev=child;child=new_item;
|
||||
value=skip(parse_string(child,skip(value+1)));
|
||||
if (!value) return 0;
|
||||
child->string=child->valuestring;child->valuestring=0;
|
||||
if (*value!=':') {ep=value;return 0;} /* fail! */
|
||||
value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
|
||||
if (!value) return 0;
|
||||
}
|
||||
|
||||
if (*value=='}') return value+1; /* end of array */
|
||||
ep=value;return 0; /* malformed. */
|
||||
}
|
||||
|
||||
/* Render an object to text. */
|
||||
static char *print_object(cJSON *item,int depth,int fmt)
|
||||
{
|
||||
char **entries=0,**names=0;
|
||||
char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
|
||||
cJSON *child=item->child;
|
||||
int numentries=0,fail=0;
|
||||
/* Count the number of entries. */
|
||||
while (child) numentries++,child=child->next;
|
||||
/* Explicitly handle empty object case */
|
||||
if (!numentries)
|
||||
{
|
||||
out=(char*)cJSON_malloc(fmt?depth+3:3);
|
||||
if (!out) return 0;
|
||||
ptr=out;*ptr++='{';
|
||||
if (fmt) {*ptr++='\n';for (i=0;i<depth-1;i++) *ptr++='\t';}
|
||||
*ptr++='}';*ptr++=0;
|
||||
return out;
|
||||
}
|
||||
/* Allocate space for the names and the objects */
|
||||
entries=(char**)cJSON_malloc(numentries*sizeof(char*));
|
||||
if (!entries) return 0;
|
||||
names=(char**)cJSON_malloc(numentries*sizeof(char*));
|
||||
if (!names) {cJSON_free(entries);return 0;}
|
||||
memset(entries,0,sizeof(char*)*numentries);
|
||||
memset(names,0,sizeof(char*)*numentries);
|
||||
|
||||
/* Collect all the results into our arrays: */
|
||||
child=item->child;depth++;if (fmt) len+=depth;
|
||||
while (child)
|
||||
{
|
||||
names[i]=str=print_string_ptr(child->string);
|
||||
entries[i++]=ret=print_value(child,depth,fmt);
|
||||
if (str && ret) len+=(int)(strlen(ret)+strlen(str))+2+(fmt?2+depth:0); else fail=1;
|
||||
child=child->next;
|
||||
}
|
||||
|
||||
/* Try to allocate the output string */
|
||||
if (!fail) out=(char*)cJSON_malloc(len);
|
||||
if (!out) fail=1;
|
||||
|
||||
/* Handle failure */
|
||||
if (fail)
|
||||
{
|
||||
for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
|
||||
cJSON_free(names);cJSON_free(entries);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Compose the output: */
|
||||
*out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
|
||||
for (i=0;i<numentries;i++)
|
||||
{
|
||||
if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
|
||||
strcpy(ptr,names[i]);ptr+=strlen(names[i]);
|
||||
*ptr++=':';if (fmt) *ptr++='\t';
|
||||
strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
|
||||
if (i!=numentries-1) *ptr++=',';
|
||||
if (fmt) *ptr++='\n';*ptr=0;
|
||||
cJSON_free(names[i]);cJSON_free(entries[i]);
|
||||
}
|
||||
|
||||
cJSON_free(names);cJSON_free(entries);
|
||||
if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
|
||||
*ptr++='}';*ptr++=0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/* Get Array size/item / object item. */
|
||||
int cJSON_GetArraySize(cJSON *array) {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}
|
||||
cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}
|
||||
cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
|
||||
|
||||
/* Utility for array list handling. */
|
||||
static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}
|
||||
/* Utility for handling references. */
|
||||
static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();if (!ref) return 0;memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;}
|
||||
|
||||
/* Add item to array/object. */
|
||||
void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!item) return; if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}}
|
||||
void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);}
|
||||
void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));}
|
||||
void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));}
|
||||
|
||||
cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0;
|
||||
if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;}
|
||||
void cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));}
|
||||
cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;}
|
||||
void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
|
||||
|
||||
/* Replace array/object items with new ones. */
|
||||
void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;
|
||||
newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
|
||||
if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}
|
||||
void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}}
|
||||
|
||||
/* Create basic types: */
|
||||
cJSON *cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;}
|
||||
cJSON *cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;}
|
||||
cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;}
|
||||
cJSON *cJSON_CreateBool(int b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
|
||||
cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int)num;}return item;}
|
||||
cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
|
||||
cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}
|
||||
cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}
|
||||
|
||||
/* Create Arrays: */
|
||||
cJSON *cJSON_CreateIntArray(int *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
|
||||
cJSON *cJSON_CreateFloatArray(float *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
|
||||
cJSON *cJSON_CreateDoubleArray(double *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
|
||||
cJSON *cJSON_CreateStringArray(const char **strings,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateString(strings[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
|
||||
|
||||
/* Duplication */
|
||||
cJSON *cJSON_Duplicate(cJSON *item,int recurse)
|
||||
{
|
||||
cJSON *newitem,*cptr,*nptr=0,*newchild;
|
||||
/* Bail on bad ptr */
|
||||
if (!item) return 0;
|
||||
/* Create new item */
|
||||
newitem=cJSON_New_Item();
|
||||
if (!newitem) return 0;
|
||||
/* Copy over all vars */
|
||||
newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble;
|
||||
if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}}
|
||||
if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}}
|
||||
/* If non-recursive, then we're done! */
|
||||
if (!recurse) return newitem;
|
||||
/* Walk the ->next chain for the child. */
|
||||
cptr=item->child;
|
||||
while (cptr)
|
||||
{
|
||||
newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */
|
||||
if (!newchild) {cJSON_Delete(newitem);return 0;}
|
||||
if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */
|
||||
else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */
|
||||
cptr=cptr->next;
|
||||
}
|
||||
return newitem;
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
Copyright (c) 2009 Dave Gamble
|
||||
|
||||
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 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.
|
||||
*/
|
||||
// Modifications copyright Amazon.com, Inc. or its affiliates
|
||||
|
||||
#ifndef cJSON__h
|
||||
#define cJSON__h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* cJSON Types: */
|
||||
#define cJSON_False 0
|
||||
#define cJSON_True 1
|
||||
#define cJSON_NULL 2
|
||||
#define cJSON_Number 3
|
||||
#define cJSON_String 4
|
||||
#define cJSON_Array 5
|
||||
#define cJSON_Object 6
|
||||
|
||||
#define cJSON_IsReference 256
|
||||
|
||||
/* The cJSON structure: */
|
||||
typedef struct cJSON {
|
||||
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
|
||||
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
|
||||
|
||||
int type; /* The type of the item, as above. */
|
||||
|
||||
char *valuestring; /* The item's string, if type==cJSON_String */
|
||||
int valueint; /* The item's number, if type==cJSON_Number */
|
||||
double valuedouble; /* The item's number, if type==cJSON_Number */
|
||||
|
||||
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
||||
} cJSON;
|
||||
|
||||
typedef struct cJSON_Hooks {
|
||||
void *(*malloc_fn)(size_t sz);
|
||||
void (*free_fn)(void *ptr);
|
||||
} cJSON_Hooks;
|
||||
|
||||
/* Supply malloc, realloc and free functions to cJSON */
|
||||
extern void cJSON_InitHooks(cJSON_Hooks* hooks);
|
||||
|
||||
|
||||
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
|
||||
extern cJSON *cJSON_Parse(const char *value);
|
||||
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
|
||||
extern char *cJSON_Print(cJSON *item);
|
||||
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
|
||||
extern char *cJSON_PrintUnformatted(cJSON *item);
|
||||
/* Delete a cJSON entity and all subentities. */
|
||||
extern void cJSON_Delete(cJSON *c);
|
||||
|
||||
/* Returns the number of items in an array (or object). */
|
||||
extern int cJSON_GetArraySize(cJSON *array);
|
||||
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
|
||||
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
|
||||
/* Get item "string" from object. Case insensitive. */
|
||||
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
|
||||
|
||||
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
|
||||
extern const char *cJSON_GetErrorPtr(void);
|
||||
|
||||
/* These calls create a cJSON item of the appropriate type. */
|
||||
extern cJSON *cJSON_CreateNull(void);
|
||||
extern cJSON *cJSON_CreateTrue(void);
|
||||
extern cJSON *cJSON_CreateFalse(void);
|
||||
extern cJSON *cJSON_CreateBool(int b);
|
||||
extern cJSON *cJSON_CreateNumber(double num);
|
||||
extern cJSON *cJSON_CreateString(const char *string);
|
||||
extern cJSON *cJSON_CreateArray(void);
|
||||
extern cJSON *cJSON_CreateObject(void);
|
||||
|
||||
/* These utilities create an Array of count items. */
|
||||
extern cJSON *cJSON_CreateIntArray(int *numbers,int count);
|
||||
extern cJSON *cJSON_CreateFloatArray(float *numbers,int count);
|
||||
extern cJSON *cJSON_CreateDoubleArray(double *numbers,int count);
|
||||
extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
|
||||
|
||||
/* Append item to the specified array/object. */
|
||||
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
||||
extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
|
||||
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
||||
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
||||
extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
|
||||
|
||||
/* Remove/Detatch items from Arrays/Objects. */
|
||||
extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
|
||||
extern void cJSON_DeleteItemFromArray(cJSON *array,int which);
|
||||
extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
|
||||
extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);
|
||||
|
||||
/* Update array items. */
|
||||
extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
|
||||
extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
|
||||
|
||||
/* Duplicate a cJSON item */
|
||||
extern cJSON *cJSON_Duplicate(cJSON *item,int recurse);
|
||||
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
|
||||
need to be released. With recurse!=0, it will duplicate any children connected to the item.
|
||||
The item->next and ->prev pointers are always zero on return from Duplicate. */
|
||||
|
||||
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
|
||||
extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);
|
||||
|
||||
/* Macros for creating things quickly. */
|
||||
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
|
||||
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
|
||||
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
|
||||
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
|
||||
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
|
||||
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
|
||||
|
||||
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
|
||||
#define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val))
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,825 @@
|
||||
// Modifications copyright Amazon.com, Inc. or its affiliates
|
||||
// Modifications copyright Crytek GmbH
|
||||
|
||||
#include "hlslcc.hpp"
|
||||
#include "stdlib.h"
|
||||
#include "stdio.h"
|
||||
#include "bstrlib.h"
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include "hash.h"
|
||||
#include "serializeReflection.h"
|
||||
#include "hlslcc_bin.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
#include "timer.h"
|
||||
|
||||
#if defined(_WIN32) && !defined(PORTABLE)
|
||||
#define VALIDATE_OUTPUT
|
||||
#endif
|
||||
|
||||
#if defined(VALIDATE_OUTPUT)
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#include <gl/GL.h>
|
||||
|
||||
#pragma comment(lib, "opengl32.lib")
|
||||
|
||||
typedef char GLcharARB; /* native character */
|
||||
typedef unsigned int GLhandleARB; /* shader object handle */
|
||||
#define GL_OBJECT_COMPILE_STATUS_ARB 0x8B81
|
||||
#define GL_OBJECT_LINK_STATUS_ARB 0x8B82
|
||||
#define GL_OBJECT_INFO_LOG_LENGTH_ARB 0x8B84
|
||||
typedef void (WINAPI * PFNGLDELETEOBJECTARBPROC) (GLhandleARB obj);
|
||||
typedef GLhandleARB(WINAPI * PFNGLCREATESHADEROBJECTARBPROC) (GLenum shaderType);
|
||||
typedef void (WINAPI * PFNGLSHADERSOURCEARBPROC) (GLhandleARB shaderObj, GLsizei count, const GLcharARB* *string, const GLint *length);
|
||||
typedef void (WINAPI * PFNGLCOMPILESHADERARBPROC) (GLhandleARB shaderObj);
|
||||
typedef void (WINAPI * PFNGLGETINFOLOGARBPROC) (GLhandleARB obj, GLsizei maxLength, GLsizei *length, GLcharARB *infoLog);
|
||||
typedef void (WINAPI * PFNGLGETOBJECTPARAMETERIVARBPROC) (GLhandleARB obj, GLenum pname, GLint *params);
|
||||
typedef GLhandleARB(WINAPI * PFNGLCREATEPROGRAMOBJECTARBPROC) (void);
|
||||
typedef void (WINAPI * PFNGLATTACHOBJECTARBPROC) (GLhandleARB containerObj, GLhandleARB obj);
|
||||
typedef void (WINAPI * PFNGLLINKPROGRAMARBPROC) (GLhandleARB programObj);
|
||||
typedef void (WINAPI * PFNGLUSEPROGRAMOBJECTARBPROC) (GLhandleARB programObj);
|
||||
typedef void (WINAPI * PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei* length, GLcharARB* infoLog);
|
||||
|
||||
static PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
|
||||
static PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
|
||||
static PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
|
||||
static PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
|
||||
static PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
|
||||
static PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
|
||||
static PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
|
||||
static PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
|
||||
static PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
|
||||
static PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
|
||||
static PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
|
||||
|
||||
#define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
|
||||
#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
|
||||
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
|
||||
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
|
||||
#define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
|
||||
#define WGL_CONTEXT_FLAGS_ARB 0x2094
|
||||
#define ERROR_INVALID_VERSION_ARB 0x2095
|
||||
#define ERROR_INVALID_PROFILE_ARB 0x2096
|
||||
|
||||
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
|
||||
#define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
|
||||
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
|
||||
|
||||
typedef HGLRC(WINAPI * PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC, HGLRC hShareContext, const int* attribList);
|
||||
static PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
|
||||
|
||||
void InitOpenGL()
|
||||
{
|
||||
HGLRC rc;
|
||||
|
||||
// setup minimal required GL
|
||||
HWND wnd = CreateWindowA(
|
||||
"STATIC",
|
||||
"GL",
|
||||
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
|
||||
0, 0, 16, 16,
|
||||
NULL, NULL,
|
||||
GetModuleHandle(NULL), NULL);
|
||||
HDC dc = GetDC(wnd);
|
||||
|
||||
PIXELFORMATDESCRIPTOR pfd = {
|
||||
sizeof(PIXELFORMATDESCRIPTOR), 1,
|
||||
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
|
||||
PFD_TYPE_RGBA, 32,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
16, 0,
|
||||
0, PFD_MAIN_PLANE, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
int fmt = ChoosePixelFormat(dc, &pfd);
|
||||
SetPixelFormat(dc, fmt, &pfd);
|
||||
|
||||
rc = wglCreateContext(dc);
|
||||
wglMakeCurrent(dc, rc);
|
||||
|
||||
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
|
||||
|
||||
if (wglCreateContextAttribsARB)
|
||||
{
|
||||
const int OpenGLContextAttribs[] = {
|
||||
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
||||
WGL_CONTEXT_MINOR_VERSION_ARB, 3,
|
||||
#if defined(_DEBUG)
|
||||
//WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB | WGL_CONTEXT_DEBUG_BIT_ARB,
|
||||
#else
|
||||
//WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
|
||||
#endif
|
||||
//WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
||||
0, 0
|
||||
};
|
||||
|
||||
const HGLRC OpenGLContext = wglCreateContextAttribsARB(dc, 0, OpenGLContextAttribs);
|
||||
|
||||
wglMakeCurrent(dc, OpenGLContext);
|
||||
|
||||
wglDeleteContext(rc);
|
||||
|
||||
rc = OpenGLContext;
|
||||
}
|
||||
|
||||
glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC)wglGetProcAddress("glDeleteObjectARB");
|
||||
glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)wglGetProcAddress("glCreateShaderObjectARB");
|
||||
glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)wglGetProcAddress("glShaderSourceARB");
|
||||
glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC)wglGetProcAddress("glCompileShaderARB");
|
||||
glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC)wglGetProcAddress("glGetInfoLogARB");
|
||||
glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC)wglGetProcAddress("glGetObjectParameterivARB");
|
||||
glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC)wglGetProcAddress("glCreateProgramObjectARB");
|
||||
glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC)wglGetProcAddress("glAttachObjectARB");
|
||||
glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC)wglGetProcAddress("glLinkProgramARB");
|
||||
glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)wglGetProcAddress("glUseProgramObjectARB");
|
||||
glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog");
|
||||
}
|
||||
#endif
|
||||
|
||||
void PrintSingleLineError(FILE* pFile, char* error)
|
||||
{
|
||||
while (*error != '\0')
|
||||
{
|
||||
char* pLineEnd = strchr(error, '\n');
|
||||
if (pLineEnd == 0)
|
||||
pLineEnd = error + strlen(error) - 1;
|
||||
fwrite(error, 1, pLineEnd - error, pFile);
|
||||
fwrite("\r", 1, 1, pFile);
|
||||
error = pLineEnd + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int TryCompileShader(GLenum eShaderType, const char* inFilename, char* shader, double* pCompileTime, int useStdErr)
|
||||
{
|
||||
GLint iCompileStatus;
|
||||
GLuint hShader;
|
||||
Timer_t timer;
|
||||
|
||||
InitTimer(&timer);
|
||||
|
||||
InitOpenGL();
|
||||
|
||||
hShader = glCreateShaderObjectARB(eShaderType);
|
||||
glShaderSourceARB(hShader, 1, (const char **)&shader, NULL);
|
||||
|
||||
ResetTimer(&timer);
|
||||
glCompileShaderARB(hShader);
|
||||
*pCompileTime = ReadTimer(&timer);
|
||||
|
||||
/* Check it compiled OK */
|
||||
glGetObjectParameterivARB(hShader, GL_OBJECT_COMPILE_STATUS_ARB, &iCompileStatus);
|
||||
|
||||
if (iCompileStatus != GL_TRUE)
|
||||
{
|
||||
FILE* errorFile = NULL;
|
||||
GLint iInfoLogLength = 0;
|
||||
char* pszInfoLog;
|
||||
|
||||
glGetObjectParameterivARB(hShader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &iInfoLogLength);
|
||||
|
||||
pszInfoLog = new char[iInfoLogLength];
|
||||
|
||||
printf("Error: Failed to compile GLSL shader\n");
|
||||
|
||||
glGetInfoLogARB(hShader, iInfoLogLength, NULL, pszInfoLog);
|
||||
|
||||
printf(pszInfoLog);
|
||||
|
||||
if (!useStdErr)
|
||||
{
|
||||
std::string filename;
|
||||
filename += inFilename;
|
||||
filename += "_compileErrors.txt";
|
||||
|
||||
//Dump to file
|
||||
errorFile = fopen(filename.c_str(), "w");
|
||||
|
||||
fclose(errorFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Present error to stderror with no "new lines" as required by remote shader compiler
|
||||
fprintf(stderr, "%s(-) error: ", inFilename);
|
||||
PrintSingleLineError(stderr, pszInfoLog);
|
||||
fprintf(stderr, "\rshader: ");
|
||||
PrintSingleLineError(stderr, shader);
|
||||
}
|
||||
|
||||
delete[] pszInfoLog;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int fileExists(const char* path)
|
||||
{
|
||||
FILE* shaderFile;
|
||||
shaderFile = fopen(path, "rb");
|
||||
|
||||
if (shaderFile)
|
||||
{
|
||||
fclose(shaderFile);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ShaderLang LanguageFromString(const char* str)
|
||||
{
|
||||
if (strcmp(str, "es100") == 0)
|
||||
{
|
||||
return LANG_ES_100;
|
||||
}
|
||||
if (strcmp(str, "es300") == 0)
|
||||
{
|
||||
return LANG_ES_300;
|
||||
}
|
||||
if (strcmp(str, "es310") == 0)
|
||||
{
|
||||
return LANG_ES_310;
|
||||
}
|
||||
if (strcmp(str, "120") == 0)
|
||||
{
|
||||
return LANG_120;
|
||||
}
|
||||
if (strcmp(str, "130") == 0)
|
||||
{
|
||||
return LANG_130;
|
||||
}
|
||||
if (strcmp(str, "140") == 0)
|
||||
{
|
||||
return LANG_140;
|
||||
}
|
||||
if (strcmp(str, "150") == 0)
|
||||
{
|
||||
return LANG_150;
|
||||
}
|
||||
if (strcmp(str, "330") == 0)
|
||||
{
|
||||
return LANG_330;
|
||||
}
|
||||
if (strcmp(str, "400") == 0)
|
||||
{
|
||||
return LANG_400;
|
||||
}
|
||||
if (strcmp(str, "410") == 0)
|
||||
{
|
||||
return LANG_410;
|
||||
}
|
||||
if (strcmp(str, "420") == 0)
|
||||
{
|
||||
return LANG_420;
|
||||
}
|
||||
if (strcmp(str, "430") == 0)
|
||||
{
|
||||
return LANG_430;
|
||||
}
|
||||
if (strcmp(str, "440") == 0)
|
||||
{
|
||||
return LANG_440;
|
||||
}
|
||||
if (strcmp(str, "metal") == 0)
|
||||
{
|
||||
return LANG_METAL;
|
||||
}
|
||||
return LANG_DEFAULT;
|
||||
}
|
||||
|
||||
#define MAX_PATH_CHARS 256
|
||||
#define MAX_FXC_CMD_CHARS 1024
|
||||
#define MAX_DEBUG_READ_CHARS 512
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ShaderLang language;
|
||||
|
||||
int flags;
|
||||
|
||||
const char* shaderFile;
|
||||
char* outputShaderFile;
|
||||
|
||||
char* reflectPath;
|
||||
|
||||
char cacheKey[MAX_PATH_CHARS];
|
||||
|
||||
int bUseFxc;
|
||||
std::string fxcCmdLine;
|
||||
} Options;
|
||||
|
||||
void InitOptions(Options* psOptions)
|
||||
{
|
||||
psOptions->language = LANG_DEFAULT;
|
||||
psOptions->flags = 0;
|
||||
psOptions->reflectPath = NULL;
|
||||
|
||||
psOptions->shaderFile = NULL;
|
||||
|
||||
psOptions->bUseFxc = 0;
|
||||
}
|
||||
|
||||
void PrintHelp()
|
||||
{
|
||||
printf("Command line options:\n");
|
||||
|
||||
printf("\t-lang=X \t Language to use. e.g. es100 or 140 or metal.\n");
|
||||
printf("\t-flags=X \t The integer value of the HLSLCC_FLAGS to used.\n");
|
||||
printf("\t-reflect=X \t File to write reflection JSON to.\n");
|
||||
printf("\t-in=X \t Shader file to compile.\n");
|
||||
printf("\t-out=X \t File to write the compiled shader from -in to.\n");
|
||||
|
||||
printf("\t-hashout=[dir/]out-file-name \t Output file name is a hash of 'out-file-name', put in the directory 'dir'.\n");
|
||||
|
||||
printf("\t-fxc=\"CMD\" HLSL compiler command line. If specified the input shader will be first compiled through this command first and then the resulting bytecode translated.\n");
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int GetOptions(int argc, char** argv, Options* psOptions)
|
||||
{
|
||||
int i;
|
||||
int fullShaderChain = -1;
|
||||
|
||||
InitOptions(psOptions);
|
||||
|
||||
for (i = 1; i < argc; i++)
|
||||
{
|
||||
char *option;
|
||||
|
||||
option = strstr(argv[i], "-help");
|
||||
if (option != NULL)
|
||||
{
|
||||
PrintHelp();
|
||||
return 0;
|
||||
}
|
||||
|
||||
option = strstr(argv[i], "-reflect=");
|
||||
if (option != NULL)
|
||||
{
|
||||
psOptions->reflectPath = option + strlen("-reflect=");
|
||||
}
|
||||
|
||||
option = strstr(argv[i], "-lang=");
|
||||
if (option != NULL)
|
||||
{
|
||||
psOptions->language = LanguageFromString((&option[strlen("-lang=")]));
|
||||
}
|
||||
|
||||
option = strstr(argv[i], "-flags=");
|
||||
if (option != NULL)
|
||||
{
|
||||
psOptions->flags = atol(&option[strlen("-flags=")]);
|
||||
}
|
||||
|
||||
option = strstr(argv[i], "-in=");
|
||||
if (option != NULL)
|
||||
{
|
||||
fullShaderChain = 0;
|
||||
psOptions->shaderFile = option + strlen("-in=");
|
||||
if (!fileExists(psOptions->shaderFile))
|
||||
{
|
||||
printf("Invalid path: %s\n", psOptions->shaderFile);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
option = strstr(argv[i], "-out=");
|
||||
if (option != NULL)
|
||||
{
|
||||
fullShaderChain = 0;
|
||||
psOptions->outputShaderFile = option + strlen("-out=");
|
||||
}
|
||||
|
||||
option = strstr(argv[i], "-hashout");
|
||||
if (option != NULL)
|
||||
{
|
||||
fullShaderChain = 0;
|
||||
psOptions->outputShaderFile = option + strlen("-hashout=");
|
||||
|
||||
char* dir;
|
||||
int64_t length;
|
||||
|
||||
uint64_t hash = hash64((const uint8_t*)psOptions->outputShaderFile, (uint32_t)strlen(psOptions->outputShaderFile), 0);
|
||||
|
||||
dir = strrchr(psOptions->outputShaderFile, '\\');
|
||||
|
||||
if (!dir)
|
||||
{
|
||||
dir = strrchr(psOptions->outputShaderFile, '//');
|
||||
}
|
||||
|
||||
if (!dir)
|
||||
{
|
||||
length = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
length = (int)(dir - psOptions->outputShaderFile) + 1;
|
||||
}
|
||||
|
||||
for (i = 0; i < length; ++i)
|
||||
{
|
||||
psOptions->cacheKey[i] = psOptions->outputShaderFile[i];
|
||||
}
|
||||
|
||||
//sprintf(psOptions->cacheKey, "%x%x", high, low);
|
||||
sprintf(&psOptions->cacheKey[i], "%010llX", hash);
|
||||
|
||||
psOptions->outputShaderFile = psOptions->cacheKey;
|
||||
}
|
||||
|
||||
option = strstr(argv[i], "-fxc=");
|
||||
if (option != NULL)
|
||||
{
|
||||
char* cmdLine = option + strlen("-fxc=");
|
||||
size_t cmdLineLen = strlen(cmdLine);
|
||||
if (cmdLineLen == 0 || cmdLineLen + 1 >= MAX_FXC_CMD_CHARS)
|
||||
return 0;
|
||||
psOptions->fxcCmdLine = std::string(cmdLine, cmdLineLen);
|
||||
psOptions->bUseFxc = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *malloc_hook(size_t size)
|
||||
{
|
||||
return malloc(size);
|
||||
}
|
||||
void *calloc_hook(size_t num, size_t size)
|
||||
{
|
||||
return calloc(num, size);
|
||||
}
|
||||
void *realloc_hook(void *p, size_t size)
|
||||
{
|
||||
return realloc(p, size);
|
||||
}
|
||||
void free_hook(void *p)
|
||||
{
|
||||
free(p);
|
||||
}
|
||||
|
||||
int Run(const char* srcPath, const char* destPath, ShaderLang language, int flags, const char* reflectPath, Shader* shader, int useStdErr, [[maybe_unused]] const char *fxcCmdLine, [[maybe_unused]] const char *debugSrcPath)
|
||||
{
|
||||
FILE* outputFile;
|
||||
Shader tempShader;
|
||||
Shader* result = shader ? shader : &tempShader;
|
||||
Timer_t timer;
|
||||
int compiledOK = 0;
|
||||
double crossCompileTime = 0;
|
||||
double glslCompileTime = 0;
|
||||
|
||||
HLSLcc_SetMemoryFunctions(malloc_hook, calloc_hook, free_hook, realloc_hook);
|
||||
|
||||
InitTimer(&timer);
|
||||
|
||||
ResetTimer(&timer);
|
||||
GlExtensions ext;
|
||||
ext.ARB_explicit_attrib_location = 0;
|
||||
ext.ARB_explicit_uniform_location = 0;
|
||||
ext.ARB_shading_language_420pack = 0;
|
||||
if (language == LANG_METAL)
|
||||
{
|
||||
compiledOK = TranslateHLSLFromFileToMETAL(srcPath, flags, language, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
compiledOK = TranslateHLSLFromFileToGLSL(srcPath, flags, language, &ext, result);
|
||||
}
|
||||
crossCompileTime = ReadTimer(&timer);
|
||||
|
||||
if (compiledOK)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
bstring debugString = bfromcstr(result->sourceCode);
|
||||
|
||||
bcatcstr(debugString, "\n\n// ------- DEBUG INFORMATION -------");
|
||||
|
||||
bformata(debugString, "\n// Shader Object Input: %s", srcPath);
|
||||
bformata(debugString, "\n// Shader Output: %s", destPath);
|
||||
if (debugSrcPath)
|
||||
{
|
||||
char debugStr[MAX_DEBUG_READ_CHARS];
|
||||
FILE* debugFile = fopen(debugSrcPath, "r");
|
||||
if (debugFile)
|
||||
{
|
||||
bformata(debugString, "\n// Shader HLSL Input: ");
|
||||
while (!feof(debugFile))
|
||||
bformata(debugString, "// %s", fgets(debugStr, MAX_DEBUG_READ_CHARS, debugFile));
|
||||
fclose(debugFile);
|
||||
}
|
||||
}
|
||||
if (fxcCmdLine)
|
||||
bformata(debugString, "\n// FXC Command: %s", fxcCmdLine);
|
||||
|
||||
result->sourceCode = bstr2cstr(debugString, '\0');
|
||||
#endif
|
||||
printf("cc time: %.2f us\n", crossCompileTime);
|
||||
|
||||
#if !defined(APPLE)
|
||||
// https://msdn.microsoft.com/en-us/library/ms175782.aspx. As to disable the "("'n' format specifier disabled", 0)" assertion.
|
||||
_set_printf_count_output(1);
|
||||
#endif
|
||||
|
||||
if (destPath)
|
||||
{
|
||||
//Dump to file
|
||||
outputFile = fopen(destPath, "w");
|
||||
fprintf(outputFile, result->sourceCode);
|
||||
|
||||
fclose(outputFile);
|
||||
}
|
||||
|
||||
if (reflectPath)
|
||||
{
|
||||
const char* jsonString = SerializeReflection(&result->reflection);
|
||||
outputFile = fopen(reflectPath, "w");
|
||||
fprintf(outputFile, jsonString);
|
||||
fclose(outputFile);
|
||||
}
|
||||
|
||||
#if defined(VALIDATE_OUTPUT)
|
||||
if (language != LANG_METAL)
|
||||
{
|
||||
compiledOK = TryCompileShader(result->shaderType, destPath ? destPath : "", result->sourceCode, &glslCompileTime, useStdErr);
|
||||
|
||||
if (compiledOK)
|
||||
{
|
||||
printf("glsl time: %.2f us\n", glslCompileTime);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!shader)
|
||||
FreeShader(result);
|
||||
}
|
||||
else if (useStdErr)
|
||||
{
|
||||
fprintf(stderr, "TranslateHLSLFromFile failed");
|
||||
}
|
||||
|
||||
return compiledOK;
|
||||
}
|
||||
|
||||
struct SDXBCFile
|
||||
{
|
||||
FILE* m_pFile;
|
||||
|
||||
bool Read(void* pElements, size_t uSize)
|
||||
{
|
||||
return fread(pElements, 1, uSize, m_pFile) == uSize;
|
||||
}
|
||||
|
||||
bool Write(const void* pElements, size_t uSize)
|
||||
{
|
||||
return fwrite(pElements, 1, uSize, m_pFile) == uSize;
|
||||
}
|
||||
|
||||
bool SeekRel(int32_t iOffset)
|
||||
{
|
||||
return fseek(m_pFile, iOffset, SEEK_CUR) == 0;
|
||||
}
|
||||
|
||||
bool SeekAbs(uint32_t uPosition)
|
||||
{
|
||||
return fseek(m_pFile, uPosition, SEEK_SET) == 0;
|
||||
}
|
||||
};
|
||||
|
||||
int CombineDXBCWithGLSL(char* dxbcFileName, char* outputFileName, Shader* shader)
|
||||
{
|
||||
SDXBCFile dxbcFile = { fopen(dxbcFileName, "rb") };
|
||||
SDXBCFile outputFile = { fopen(outputFileName, "wb") };
|
||||
|
||||
bool result =
|
||||
dxbcFile.m_pFile != NULL && outputFile.m_pFile != NULL &&
|
||||
DXBCCombineWithGLSL(dxbcFile, outputFile, shader);
|
||||
|
||||
if (dxbcFile.m_pFile != NULL)
|
||||
fclose(dxbcFile.m_pFile);
|
||||
if (outputFile.m_pFile != NULL)
|
||||
fclose(outputFile.m_pFile);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#if !defined(_MSC_VER)
|
||||
#define sprintf_s(dest, size, ...) sprintf(dest, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) && defined(PORTABLE)
|
||||
|
||||
DWORD FilterException(DWORD uExceptionCode)
|
||||
{
|
||||
const char* szExceptionName;
|
||||
char acTemp[10];
|
||||
switch (uExceptionCode)
|
||||
{
|
||||
#define _CASE(_Name) \
|
||||
case _Name: \
|
||||
szExceptionName = #_Name; \
|
||||
break;
|
||||
_CASE(EXCEPTION_ACCESS_VIOLATION)
|
||||
_CASE(EXCEPTION_DATATYPE_MISALIGNMENT)
|
||||
_CASE(EXCEPTION_BREAKPOINT)
|
||||
_CASE(EXCEPTION_SINGLE_STEP)
|
||||
_CASE(EXCEPTION_ARRAY_BOUNDS_EXCEEDED)
|
||||
_CASE(EXCEPTION_FLT_DENORMAL_OPERAND)
|
||||
_CASE(EXCEPTION_FLT_DIVIDE_BY_ZERO)
|
||||
_CASE(EXCEPTION_FLT_INEXACT_RESULT)
|
||||
_CASE(EXCEPTION_FLT_INVALID_OPERATION)
|
||||
_CASE(EXCEPTION_FLT_OVERFLOW)
|
||||
_CASE(EXCEPTION_FLT_STACK_CHECK)
|
||||
_CASE(EXCEPTION_FLT_UNDERFLOW)
|
||||
_CASE(EXCEPTION_INT_DIVIDE_BY_ZERO)
|
||||
_CASE(EXCEPTION_INT_OVERFLOW)
|
||||
_CASE(EXCEPTION_PRIV_INSTRUCTION)
|
||||
_CASE(EXCEPTION_IN_PAGE_ERROR)
|
||||
_CASE(EXCEPTION_ILLEGAL_INSTRUCTION)
|
||||
_CASE(EXCEPTION_NONCONTINUABLE_EXCEPTION)
|
||||
_CASE(EXCEPTION_STACK_OVERFLOW)
|
||||
_CASE(EXCEPTION_INVALID_DISPOSITION)
|
||||
_CASE(EXCEPTION_GUARD_PAGE)
|
||||
_CASE(EXCEPTION_INVALID_HANDLE)
|
||||
//_CASE(EXCEPTION_POSSIBLE_DEADLOCK)
|
||||
#undef _CASE
|
||||
default:
|
||||
sprintf_s(acTemp, "0x%08X", uExceptionCode);
|
||||
szExceptionName = acTemp;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Hardware exception thrown (%s)\n", szExceptionName);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
const char* PatchHLSLShaderFile(const char* path)
|
||||
{
|
||||
// Need to transform "half" into "min16float" so FXC preserve min precision to the operands.
|
||||
static char patchedFileName[MAX_PATH_CHARS];
|
||||
const char* defines = "#define half min16float\n"
|
||||
"#define half2 min16float2\n"
|
||||
"#define half3 min16float3\n"
|
||||
"#define half4 min16float4\n";
|
||||
|
||||
sprintf_s(patchedFileName, sizeof(patchedFileName), "%s.hlslPatched", path);
|
||||
FILE* shaderFile = fopen(path, "rb");
|
||||
if (!shaderFile)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
FILE* patchedFile = fopen(patchedFileName, "wb");
|
||||
if (!patchedFile)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get size of file
|
||||
bool result = false;
|
||||
fseek(shaderFile, 0, SEEK_END);
|
||||
long size = ftell(shaderFile);
|
||||
fseek(shaderFile, 0, SEEK_SET);
|
||||
unsigned char* data = new unsigned char[size + 1]; // Extra byte for the '/0' character.
|
||||
if (fread(data, 1, size, shaderFile) == size)
|
||||
{
|
||||
data[size] = '\0';
|
||||
fprintf(patchedFile, "%s%s", defines, data);
|
||||
result = true;
|
||||
}
|
||||
|
||||
if (shaderFile)
|
||||
{
|
||||
fclose(shaderFile);
|
||||
}
|
||||
|
||||
if (patchedFile)
|
||||
{
|
||||
fclose(patchedFile);
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
return result ? patchedFileName : NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Options options;
|
||||
|
||||
#if defined(_WIN32) && defined(PORTABLE)
|
||||
__try
|
||||
{
|
||||
#endif
|
||||
|
||||
if (!GetOptions(argc, argv, &options))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (options.bUseFxc)
|
||||
{
|
||||
char dxbcFileName[MAX_PATH_CHARS];
|
||||
char glslFileName[MAX_PATH_CHARS];
|
||||
char fullFxcCmdLine[MAX_FXC_CMD_CHARS];
|
||||
int retValue;
|
||||
|
||||
if (options.flags & HLSLCC_FLAG_HALF_FLOAT_TRANSFORM)
|
||||
{
|
||||
options.shaderFile = PatchHLSLShaderFile(options.shaderFile);
|
||||
if (!options.shaderFile)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
sprintf_s(dxbcFileName, sizeof(dxbcFileName), "%s.dxbc", options.shaderFile);
|
||||
sprintf_s(glslFileName, sizeof(glslFileName), "%s.patched", options.shaderFile);
|
||||
|
||||
// Need to extract the path to the executable so we can enclose it in quotes
|
||||
// in case it contains spaces.
|
||||
const std::string fxcExeName = "fxc.exe";
|
||||
|
||||
// Case insensitive search
|
||||
std::string::iterator fxcPos = std::search(
|
||||
options.fxcCmdLine.begin(), options.fxcCmdLine.end(),
|
||||
fxcExeName.begin(), fxcExeName.end(),
|
||||
[](char ch1, char ch2) { return std::tolower(ch1) == std::tolower(ch2); }
|
||||
);
|
||||
|
||||
if (fxcPos == options.fxcCmdLine.end())
|
||||
{
|
||||
fprintf(stderr, "Could not find fxc.exe in command line");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Add the fxcExeName so it gets copied to the fxcExe path.
|
||||
fxcPos += fxcExeName.length();
|
||||
std::string fxcExe(options.fxcCmdLine.begin(), fxcPos);
|
||||
std::string fxcArguments(fxcPos, options.fxcCmdLine.end());
|
||||
|
||||
#if defined(APPLE)
|
||||
fprintf(stderr, "fxc.exe cannot be executed on Mac");
|
||||
return 1;
|
||||
#else
|
||||
// Need an extra set of quotes around the full command line because the way "system" executes it using cmd.
|
||||
sprintf_s(fullFxcCmdLine, sizeof(fullFxcCmdLine), "\"\"%s\" %s \"%s\" \"%s\"\"", fxcExe.c_str(), fxcArguments.c_str(), dxbcFileName, options.shaderFile);
|
||||
#endif
|
||||
|
||||
retValue = system(fullFxcCmdLine);
|
||||
|
||||
if (retValue == 0)
|
||||
{
|
||||
Shader shader;
|
||||
retValue = !Run(dxbcFileName, glslFileName, options.language, options.flags, options.reflectPath, &shader, 1, fullFxcCmdLine, options.shaderFile);
|
||||
|
||||
if (retValue == 0)
|
||||
{
|
||||
retValue = !CombineDXBCWithGLSL(dxbcFileName, options.outputShaderFile, &shader);
|
||||
FreeShader(&shader);
|
||||
}
|
||||
}
|
||||
|
||||
remove(dxbcFileName);
|
||||
remove(glslFileName);
|
||||
if (options.flags & HLSLCC_FLAG_HALF_FLOAT_TRANSFORM)
|
||||
{
|
||||
// Removed the hlsl patched file that was created.
|
||||
remove(options.shaderFile);
|
||||
}
|
||||
|
||||
return retValue;
|
||||
}
|
||||
else if (options.shaderFile)
|
||||
{
|
||||
if (!Run(options.shaderFile, options.outputShaderFile, options.language, options.flags, options.reflectPath, NULL, 0, NULL, NULL))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(_WIN32) && defined(PORTABLE)
|
||||
}
|
||||
__except (FilterException(GetExceptionCode()))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
// Modifications copyright Amazon.com, Inc. or its affiliates
|
||||
// Modifications copyright Crytek GmbH
|
||||
|
||||
#ifndef HASH_H_
|
||||
#define HASH_H_
|
||||
|
||||
/*
|
||||
--------------------------------------------------------------------
|
||||
mix -- mix 3 64-bit values reversibly.
|
||||
mix() takes 48 machine instructions, but only 24 cycles on a superscalar
|
||||
machine (like Intel's new MMX architecture). It requires 4 64-bit
|
||||
registers for 4::2 parallelism.
|
||||
All 1-bit deltas, all 2-bit deltas, all deltas composed of top bits of
|
||||
(a,b,c), and all deltas of bottom bits were tested. All deltas were
|
||||
tested both on random keys and on keys that were nearly all zero.
|
||||
These deltas all cause every bit of c to change between 1/3 and 2/3
|
||||
of the time (well, only 113/400 to 287/400 of the time for some
|
||||
2-bit delta). These deltas all cause at least 80 bits to change
|
||||
among (a,b,c) when the mix is run either forward or backward (yes it
|
||||
is reversible).
|
||||
This implies that a hash using mix64 has no funnels. There may be
|
||||
characteristics with 3-bit deltas or bigger, I didn't test for
|
||||
those.
|
||||
--------------------------------------------------------------------
|
||||
*/
|
||||
#define mix64(a,b,c) \
|
||||
{ \
|
||||
a -= b; a -= c; a ^= (c>>43); \
|
||||
b -= c; b -= a; b ^= (a<<9); \
|
||||
c -= a; c -= b; c ^= (b>>8); \
|
||||
a -= b; a -= c; a ^= (c>>38); \
|
||||
b -= c; b -= a; b ^= (a<<23); \
|
||||
c -= a; c -= b; c ^= (b>>5); \
|
||||
a -= b; a -= c; a ^= (c>>35); \
|
||||
b -= c; b -= a; b ^= (a<<49); \
|
||||
c -= a; c -= b; c ^= (b>>11); \
|
||||
a -= b; a -= c; a ^= (c>>12); \
|
||||
b -= c; b -= a; b ^= (a<<18); \
|
||||
c -= a; c -= b; c ^= (b>>22); \
|
||||
}
|
||||
|
||||
/*
|
||||
--------------------------------------------------------------------
|
||||
hash64() -- hash a variable-length key into a 64-bit value
|
||||
k : the key (the unaligned variable-length array of bytes)
|
||||
len : the length of the key, counting by bytes
|
||||
level : can be any 8-byte value
|
||||
Returns a 64-bit value. Every bit of the key affects every bit of
|
||||
the return value. No funnels. Every 1-bit and 2-bit delta achieves
|
||||
avalanche. About 41+5len instructions.
|
||||
|
||||
The best hash table sizes are powers of 2. There is no need to do
|
||||
mod a prime (mod is sooo slow!). If you need less than 64 bits,
|
||||
use a bitmask. For example, if you need only 10 bits, do
|
||||
h = (h & hashmask(10));
|
||||
In which case, the hash table should have hashsize(10) elements.
|
||||
|
||||
If you are hashing n strings (ub1 **)k, do it like this:
|
||||
for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
|
||||
|
||||
By Bob Jenkins, Jan 4 1997. bob_jenkins@burtleburtle.net. You may
|
||||
use this code any way you wish, private, educational, or commercial,
|
||||
but I would appreciate if you give me credit.
|
||||
|
||||
See http://burtleburtle.net/bob/hash/evahash.html
|
||||
Use for hash table lookup, or anything where one collision in 2^^64
|
||||
is acceptable. Do NOT use for cryptographic purposes.
|
||||
--------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static uint64_t hash64( const uint8_t *k, uint32_t length, uint64_t initval )
|
||||
{
|
||||
uint64_t a,b,c,len;
|
||||
|
||||
/* Set up the internal state */
|
||||
len = length;
|
||||
a = b = initval; /* the previous hash value */
|
||||
c = 0x9e3779b97f4a7c13LL; /* the golden ratio; an arbitrary value */
|
||||
|
||||
/*---------------------------------------- handle most of the key */
|
||||
while (len >= 24)
|
||||
{
|
||||
a += (k[0] +((uint64_t)k[ 1]<< 8)+((uint64_t)k[ 2]<<16)+((uint64_t)k[ 3]<<24)
|
||||
+((uint64_t)k[4 ]<<32)+((uint64_t)k[ 5]<<40)+((uint64_t)k[ 6]<<48)+((uint64_t)k[ 7]<<56));
|
||||
b += (k[8] +((uint64_t)k[ 9]<< 8)+((uint64_t)k[10]<<16)+((uint64_t)k[11]<<24)
|
||||
+((uint64_t)k[12]<<32)+((uint64_t)k[13]<<40)+((uint64_t)k[14]<<48)+((uint64_t)k[15]<<56));
|
||||
c += (k[16] +((uint64_t)k[17]<< 8)+((uint64_t)k[18]<<16)+((uint64_t)k[19]<<24)
|
||||
+((uint64_t)k[20]<<32)+((uint64_t)k[21]<<40)+((uint64_t)k[22]<<48)+((uint64_t)k[23]<<56));
|
||||
mix64(a,b,c);
|
||||
k += 24; len -= 24;
|
||||
}
|
||||
|
||||
/*------------------------------------- handle the last 23 bytes */
|
||||
c += length;
|
||||
switch(len) /* all the case statements fall through */
|
||||
{
|
||||
case 23: c+=((uint64_t)k[22]<<56);
|
||||
case 22: c+=((uint64_t)k[21]<<48);
|
||||
case 21: c+=((uint64_t)k[20]<<40);
|
||||
case 20: c+=((uint64_t)k[19]<<32);
|
||||
case 19: c+=((uint64_t)k[18]<<24);
|
||||
case 18: c+=((uint64_t)k[17]<<16);
|
||||
case 17: c+=((uint64_t)k[16]<<8);
|
||||
/* the first byte of c is reserved for the length */
|
||||
case 16: b+=((uint64_t)k[15]<<56);
|
||||
case 15: b+=((uint64_t)k[14]<<48);
|
||||
case 14: b+=((uint64_t)k[13]<<40);
|
||||
case 13: b+=((uint64_t)k[12]<<32);
|
||||
case 12: b+=((uint64_t)k[11]<<24);
|
||||
case 11: b+=((uint64_t)k[10]<<16);
|
||||
case 10: b+=((uint64_t)k[ 9]<<8);
|
||||
case 9: b+=((uint64_t)k[ 8]);
|
||||
case 8: a+=((uint64_t)k[ 7]<<56);
|
||||
case 7: a+=((uint64_t)k[ 6]<<48);
|
||||
case 6: a+=((uint64_t)k[ 5]<<40);
|
||||
case 5: a+=((uint64_t)k[ 4]<<32);
|
||||
case 4: a+=((uint64_t)k[ 3]<<24);
|
||||
case 3: a+=((uint64_t)k[ 2]<<16);
|
||||
case 2: a+=((uint64_t)k[ 1]<<8);
|
||||
case 1: a+=((uint64_t)k[ 0]);
|
||||
/* case 0: nothing left to add */
|
||||
}
|
||||
mix64(a,b,c);
|
||||
/*-------------------------------------------- report the result */
|
||||
return c;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,207 @@
|
||||
// Modifications copyright Amazon.com, Inc. or its affiliates
|
||||
// Modifications copyright Crytek GmbH
|
||||
|
||||
#include "serializeReflection.h"
|
||||
#include "cJSON.h"
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
void* jsonMalloc(size_t sz)
|
||||
{
|
||||
return new char[sz];
|
||||
}
|
||||
void jsonFree(void* ptr)
|
||||
{
|
||||
char* charPtr = static_cast<char*>(ptr);
|
||||
delete [] charPtr;
|
||||
}
|
||||
|
||||
static void AppendIntToString(std::string& str, uint32_t num)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << num;
|
||||
str += ss.str();
|
||||
}
|
||||
|
||||
static void WriteInOutSignature(InOutSignature* psSignature, cJSON* obj)
|
||||
{
|
||||
cJSON_AddItemToObject(obj, "SemanticName", cJSON_CreateString(psSignature->SemanticName));
|
||||
cJSON_AddItemToObject(obj, "ui32SemanticIndex", cJSON_CreateNumber(psSignature->ui32SemanticIndex));
|
||||
cJSON_AddItemToObject(obj, "eSystemValueType", cJSON_CreateNumber(psSignature->eSystemValueType));
|
||||
cJSON_AddItemToObject(obj, "eComponentType", cJSON_CreateNumber(psSignature->eComponentType));
|
||||
cJSON_AddItemToObject(obj, "ui32Register", cJSON_CreateNumber(psSignature->ui32Register));
|
||||
cJSON_AddItemToObject(obj, "ui32Mask", cJSON_CreateNumber(psSignature->ui32Mask));
|
||||
cJSON_AddItemToObject(obj, "ui32ReadWriteMask", cJSON_CreateNumber(psSignature->ui32ReadWriteMask));
|
||||
}
|
||||
|
||||
static void WriteResourceBinding(ResourceBinding* psBinding, cJSON* obj)
|
||||
{
|
||||
cJSON_AddItemToObject(obj, "Name", cJSON_CreateString(psBinding->Name));
|
||||
cJSON_AddItemToObject(obj, "eType", cJSON_CreateNumber(psBinding->eType));
|
||||
cJSON_AddItemToObject(obj, "ui32BindPoint", cJSON_CreateNumber(psBinding->ui32BindPoint));
|
||||
cJSON_AddItemToObject(obj, "ui32BindCount", cJSON_CreateNumber(psBinding->ui32BindCount));
|
||||
cJSON_AddItemToObject(obj, "ui32Flags", cJSON_CreateNumber(psBinding->ui32Flags));
|
||||
cJSON_AddItemToObject(obj, "eDimension", cJSON_CreateNumber(psBinding->eDimension));
|
||||
cJSON_AddItemToObject(obj, "ui32ReturnType", cJSON_CreateNumber(psBinding->ui32ReturnType));
|
||||
cJSON_AddItemToObject(obj, "ui32NumSamples", cJSON_CreateNumber(psBinding->ui32NumSamples));
|
||||
}
|
||||
|
||||
static void WriteShaderVar(ShaderVar* psVar, cJSON* obj)
|
||||
{
|
||||
cJSON_AddItemToObject(obj, "Name", cJSON_CreateString(psVar->Name));
|
||||
if(psVar->haveDefaultValue)
|
||||
{
|
||||
cJSON_AddItemToObject(obj, "aui32DefaultValues", cJSON_CreateIntArray((int*)psVar->pui32DefaultValues, psVar->ui32Size/4));
|
||||
}
|
||||
cJSON_AddItemToObject(obj, "ui32StartOffset", cJSON_CreateNumber(psVar->ui32StartOffset));
|
||||
cJSON_AddItemToObject(obj, "ui32Size", cJSON_CreateNumber(psVar->ui32Size));
|
||||
}
|
||||
|
||||
static void WriteConstantBuffer(ConstantBuffer* psCBuf, cJSON* obj)
|
||||
{
|
||||
cJSON_AddItemToObject(obj, "Name", cJSON_CreateString(psCBuf->Name));
|
||||
cJSON_AddItemToObject(obj, "ui32NumVars", cJSON_CreateNumber(psCBuf->ui32NumVars));
|
||||
|
||||
for(uint32_t i = 0; i < psCBuf->ui32NumVars; ++i)
|
||||
{
|
||||
std::string name;
|
||||
name += "var";
|
||||
AppendIntToString(name, i);
|
||||
|
||||
cJSON* varObj = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(obj, name.c_str(), varObj);
|
||||
|
||||
WriteShaderVar(&psCBuf->asVars[i], varObj);
|
||||
}
|
||||
|
||||
cJSON_AddItemToObject(obj, "ui32TotalSizeInBytes", cJSON_CreateNumber(psCBuf->ui32TotalSizeInBytes));
|
||||
}
|
||||
|
||||
static void WriteClassType(ClassType* psClassType, cJSON* obj)
|
||||
{
|
||||
cJSON_AddItemToObject(obj, "Name", cJSON_CreateString(psClassType->Name));
|
||||
cJSON_AddItemToObject(obj, "ui16ID", cJSON_CreateNumber(psClassType->ui16ID));
|
||||
cJSON_AddItemToObject(obj, "ui16ConstBufStride", cJSON_CreateNumber(psClassType->ui16ConstBufStride));
|
||||
cJSON_AddItemToObject(obj, "ui16Texture", cJSON_CreateNumber(psClassType->ui16Texture));
|
||||
cJSON_AddItemToObject(obj, "ui16Sampler", cJSON_CreateNumber(psClassType->ui16Sampler));
|
||||
}
|
||||
|
||||
static void WriteClassInstance(ClassInstance* psClassInst, cJSON* obj)
|
||||
{
|
||||
cJSON_AddItemToObject(obj, "Name", cJSON_CreateString(psClassInst->Name));
|
||||
cJSON_AddItemToObject(obj, "ui16ID", cJSON_CreateNumber(psClassInst->ui16ID));
|
||||
cJSON_AddItemToObject(obj, "ui16ConstBuf", cJSON_CreateNumber(psClassInst->ui16ConstBuf));
|
||||
cJSON_AddItemToObject(obj, "ui16ConstBufOffset", cJSON_CreateNumber(psClassInst->ui16ConstBufOffset));
|
||||
cJSON_AddItemToObject(obj, "ui16Texture", cJSON_CreateNumber(psClassInst->ui16Texture));
|
||||
cJSON_AddItemToObject(obj, "ui16Sampler", cJSON_CreateNumber(psClassInst->ui16Sampler));
|
||||
}
|
||||
|
||||
const char* SerializeReflection(ShaderInfo* psReflection)
|
||||
{
|
||||
cJSON* root;
|
||||
|
||||
cJSON_Hooks hooks;
|
||||
hooks.malloc_fn = jsonMalloc;
|
||||
hooks.free_fn = jsonFree;
|
||||
cJSON_InitHooks(&hooks);
|
||||
|
||||
root=cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, "ui32MajorVersion", cJSON_CreateNumber(psReflection->ui32MajorVersion));
|
||||
cJSON_AddItemToObject(root, "ui32MinorVersion", cJSON_CreateNumber(psReflection->ui32MinorVersion));
|
||||
|
||||
cJSON_AddItemToObject(root, "ui32NumInputSignatures", cJSON_CreateNumber(psReflection->ui32NumInputSignatures));
|
||||
|
||||
for(uint32_t i = 0; i < psReflection->ui32NumInputSignatures; ++i)
|
||||
{
|
||||
std::string name;
|
||||
name += "input";
|
||||
AppendIntToString(name, i);
|
||||
|
||||
cJSON* obj = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, name.c_str(), obj);
|
||||
|
||||
WriteInOutSignature(psReflection->psInputSignatures+i, obj);
|
||||
}
|
||||
|
||||
cJSON_AddItemToObject(root, "ui32NumOutputSignatures", cJSON_CreateNumber(psReflection->ui32NumOutputSignatures));
|
||||
|
||||
for(uint32_t i = 0; i < psReflection->ui32NumOutputSignatures; ++i)
|
||||
{
|
||||
std::string name;
|
||||
name += "output";
|
||||
AppendIntToString(name, i);
|
||||
|
||||
cJSON* obj = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, name.c_str(), obj);
|
||||
|
||||
WriteInOutSignature(psReflection->psOutputSignatures+i, obj);
|
||||
}
|
||||
|
||||
cJSON_AddItemToObject(root, "ui32NumResourceBindings", cJSON_CreateNumber(psReflection->ui32NumResourceBindings));
|
||||
|
||||
for(uint32_t i = 0; i < psReflection->ui32NumResourceBindings; ++i)
|
||||
{
|
||||
std::string name;
|
||||
name += "resource";
|
||||
AppendIntToString(name, i);
|
||||
|
||||
cJSON* obj = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, name.c_str(), obj);
|
||||
|
||||
WriteResourceBinding(psReflection->psResourceBindings+i, obj);
|
||||
}
|
||||
|
||||
cJSON_AddItemToObject(root, "ui32NumConstantBuffers", cJSON_CreateNumber(psReflection->ui32NumConstantBuffers));
|
||||
|
||||
for(uint32_t i = 0; i < psReflection->ui32NumConstantBuffers; ++i)
|
||||
{
|
||||
std::string name;
|
||||
name += "cbuf";
|
||||
AppendIntToString(name, i);
|
||||
|
||||
cJSON* obj = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, name.c_str(), obj);
|
||||
|
||||
WriteConstantBuffer(psReflection->psConstantBuffers+i, obj);
|
||||
}
|
||||
|
||||
//psThisPointerConstBuffer is a cache. Don't need to write this out.
|
||||
//It just points to the $ThisPointer cbuffer within the psConstantBuffers array.
|
||||
|
||||
for(uint32_t i = 0; i < psReflection->ui32NumClassTypes; ++i)
|
||||
{
|
||||
std::string name;
|
||||
name += "classType";
|
||||
AppendIntToString(name, i);
|
||||
|
||||
cJSON* obj = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, name.c_str(), obj);
|
||||
|
||||
WriteClassType(psReflection->psClassTypes+i, obj);
|
||||
}
|
||||
|
||||
for(uint32_t i = 0; i < psReflection->ui32NumClassInstances; ++i)
|
||||
{
|
||||
std::string name;
|
||||
name += "classInst";
|
||||
AppendIntToString(name, i);
|
||||
|
||||
cJSON* obj = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, name.c_str(), obj);
|
||||
|
||||
WriteClassInstance(psReflection->psClassInstances+i, obj);
|
||||
}
|
||||
|
||||
//psReflection->aui32TableIDToTypeID
|
||||
//psReflection->aui32ConstBufferBindpointRemap
|
||||
|
||||
cJSON_AddItemToObject(root, "eTessPartitioning", cJSON_CreateNumber(psReflection->eTessPartitioning));
|
||||
cJSON_AddItemToObject(root, "eTessOutPrim", cJSON_CreateNumber(psReflection->eTessOutPrim));
|
||||
|
||||
|
||||
const char* jsonString = cJSON_Print(root);
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
return jsonString;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// Modifications copyright Amazon.com, Inc. or its affiliates
|
||||
// Modifications copyright Crytek GmbH
|
||||
|
||||
#ifndef SERIALIZE_REFLECTION_H_
|
||||
#define SERIALIZE_REFLECTION_H_
|
||||
|
||||
#include "hlslcc.h"
|
||||
|
||||
const char* SerializeReflection(ShaderInfo* psReflection);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
// Modifications copyright Amazon.com, Inc. or its affiliates
|
||||
// Modifications copyright Crytek GmbH
|
||||
|
||||
#include "timer.h"
|
||||
|
||||
void InitTimer(Timer_t* psTimer)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
QueryPerformanceFrequency(&psTimer->frequency);
|
||||
#endif
|
||||
}
|
||||
|
||||
void ResetTimer(Timer_t* psTimer)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
QueryPerformanceCounter(&psTimer->startCount);
|
||||
#else
|
||||
gettimeofday(&psTimer->startCount, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Returns time in micro seconds */
|
||||
double ReadTimer(Timer_t* psTimer)
|
||||
{
|
||||
double startTimeInMicroSec, endTimeInMicroSec;
|
||||
|
||||
#if defined(_WIN32)
|
||||
const double freq = (1000000.0 / psTimer->frequency.QuadPart);
|
||||
QueryPerformanceCounter(&psTimer->endCount);
|
||||
startTimeInMicroSec = psTimer->startCount.QuadPart * freq;
|
||||
endTimeInMicroSec = psTimer->endCount.QuadPart * freq;
|
||||
#else
|
||||
gettimeofday(&psTimer->endCount, 0);
|
||||
startTimeInMicroSec = (psTimer->startCount.tv_sec * 1000000.0) + psTimer->startCount.tv_usec;
|
||||
endTimeInMicroSec = (psTimer->endCount.tv_sec * 1000000.0) + psTimer->endCount.tv_usec;
|
||||
#endif
|
||||
|
||||
return endTimeInMicroSec - startTimeInMicroSec;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// Modifications copyright Amazon.com, Inc. or its affiliates
|
||||
// Modifications copyright Crytek GmbH
|
||||
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
#ifdef _WIN32
|
||||
LARGE_INTEGER frequency;
|
||||
LARGE_INTEGER startCount;
|
||||
LARGE_INTEGER endCount;
|
||||
#else
|
||||
struct timeval startCount;
|
||||
struct timeval endCount;
|
||||
#endif
|
||||
} Timer_t;
|
||||
|
||||
void InitTimer(Timer_t* psTimer);
|
||||
void ResetTimer(Timer_t* psTimer);
|
||||
double ReadTimer(Timer_t* psTimer);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user