Skip to content

Commit

Permalink
Added support for producing shader language source code for Apple's M…
Browse files Browse the repository at this point in the history
…etal API.
  • Loading branch information
icculus committed Apr 25, 2016
1 parent a5e7941 commit dc12246
Show file tree
Hide file tree
Showing 9 changed files with 2,642 additions and 251 deletions.
2,852 changes: 2,608 additions & 244 deletions mojoshader.c

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions mojoshader.h
Expand Up @@ -534,6 +534,14 @@ typedef struct MOJOSHADER_parseData
*/
int minor_ver;

/*
* This is the main function name of the shader. On profiles that need
* the caller to supply this, this will be the caller-supplied string.
* Otherwise, it'll be the name chosen by the profile ("main") or
* whatnot.
*/
const char *mainfn;

/*
* The number of elements pointed to by (uniforms).
*/
Expand Down Expand Up @@ -698,6 +706,11 @@ typedef struct MOJOSHADER_parseData
*/
#define MOJOSHADER_PROFILE_NV4 "nv4"

/*
* Profile string for Metal: Apple's lowlevel API's high-level shader language.
*/
#define MOJOSHADER_PROFILE_METAL "metal"

/*
* Determine the highest supported Shader Model for a profile.
*/
Expand Down Expand Up @@ -753,11 +766,18 @@ DECLSPEC int MOJOSHADER_maxShaderModel(const char *profile);
* risk a buffer overflow if you have corrupt data, etc. Supply the value
* if you can.
*
* You should pass a name for your shader's main function in here, via the
* (mainfn) param. Some profiles need this name to be unique. Passing a NULL
* here will pick a reasonable default, and most profiles will ignore it
* anyhow. As the name of the shader's main function, etc, so make it a
* simple name that would match C's identifier rules. Keep it simple!
*
* This function is thread safe, so long as (m) and (f) are too, and that
* (tokenbuf) remains intact for the duration of the call. This allows you
* to parse several shaders on separate CPU cores at the same time.
*/
DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile,
const char *mainfn,
const unsigned char *tokenbuf,
const unsigned int bufsize,
const MOJOSHADER_swizzle *swiz,
Expand Down
2 changes: 1 addition & 1 deletion mojoshader_assembler.c
Expand Up @@ -1676,7 +1676,7 @@ static const MOJOSHADER_parseData *build_final_assembly(Context *ctx)
// It also saves us the trouble of duplicating all the other work,
// like setting up the uniforms list, etc.
MOJOSHADER_parseData *retval = (MOJOSHADER_parseData *)
MOJOSHADER_parse(MOJOSHADER_PROFILE_BYTECODE,
MOJOSHADER_parse(MOJOSHADER_PROFILE_BYTECODE, NULL,
bytecode, output_len, NULL, 0, NULL, 0,
ctx->malloc, ctx->free, ctx->malloc_data);
Free(ctx, bytecode);
Expand Down
2 changes: 1 addition & 1 deletion mojoshader_effects.c
Expand Up @@ -745,7 +745,7 @@ static void readlargeobjects(const uint32 numlargeobjects,
} // if
else
{
object->shader.shader = MOJOSHADER_parse(emitter, *ptr, length,
object->shader.shader = MOJOSHADER_parse(emitter, NULL, *ptr, length,
swiz, swizcount, smap, smapcount,
m, f, d);
// !!! FIXME: check for errors.
Expand Down
4 changes: 4 additions & 0 deletions mojoshader_internal.h
Expand Up @@ -64,6 +64,10 @@
#define SUPPORT_PROFILE_ARB1_NV 1
#endif

#ifndef SUPPORT_PROFILE_METAL
#define SUPPORT_PROFILE_METAL 1
#endif

#if SUPPORT_PROFILE_ARB1_NV && !SUPPORT_PROFILE_ARB1
#error nv profiles require arb1 profile. Fix your build.
#endif
Expand Down
7 changes: 5 additions & 2 deletions mojoshader_opengl.c
Expand Up @@ -1537,8 +1537,11 @@ MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
{
MOJOSHADER_glShader *retval = NULL;
GLuint shader = 0;
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(ctx->profile, tokenbuf,
bufsize, swiz, swizcount,

// This doesn't need a mainfn, since there's no GL lang that does.
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(ctx->profile, NULL,
tokenbuf, bufsize,
swiz, swizcount,
smap, smapcount,
ctx->malloc_fn,
ctx->free_fn,
Expand Down
2 changes: 1 addition & 1 deletion utils/finderrors.c
Expand Up @@ -160,7 +160,7 @@ static int do_file(const char *profile, const char *dname, const char *fn, int *
MOJOSHADER_glDeleteShader(shader);
}
#else
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, buf, rc, NULL, 0, NULL, 0, NULL, NULL, NULL);
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, NULL, buf, rc, NULL, 0, NULL, 0, NULL, NULL, NULL);
if (pd->error_count == 0)
report("PASS: %s\n", fname);
else
Expand Down
2 changes: 1 addition & 1 deletion utils/testoutput.c
Expand Up @@ -16,7 +16,7 @@ static int do_parse(const unsigned char *buf, const int len, const char *prof)
const MOJOSHADER_parseData *pd;
int retval = 0;

pd = MOJOSHADER_parse(prof, buf, len, NULL, 0, NULL, 0, NULL, NULL, NULL);
pd = MOJOSHADER_parse(prof, NULL, buf, len, NULL, 0, NULL, 0, NULL, NULL, NULL);
if (pd->error_count > 0)
{
int i;
Expand Down
2 changes: 1 addition & 1 deletion utils/testparse.c
Expand Up @@ -688,7 +688,7 @@ static int do_parse(const char *fname, const unsigned char *buf,
else // do it as a regular compiled shader.
{
const MOJOSHADER_parseData *pd;
pd = MOJOSHADER_parse(prof, buf, len, NULL, 0,
pd = MOJOSHADER_parse(prof, NULL, buf, len, NULL, 0,
NULL, 0, Malloc, Free, NULL);
retval = (pd->error_count == 0);
printf("SHADER: %s\n", fname);
Expand Down

0 comments on commit dc12246

Please sign in to comment.