profiles/mojoshader_profile_metal.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 20 Jul 2020 20:07:44 -0400
changeset 1297 5667281f0da9
parent 1220 048797e6aae1
permissions -rw-r--r--
metal: Fix RCP and RSQ output to match GLSL.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1199
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     1
/**
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     3
 *  Direct3D shaders.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     4
 *
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     6
 *
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     8
 */
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     9
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    10
#define __MOJOSHADER_INTERNAL__ 1
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    11
#include "mojoshader_profile.h"
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    12
1200
eb1e5280a5a9 Move the visibility pragmas below the includes.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1199
diff changeset
    13
#pragma GCC visibility push(hidden)
eb1e5280a5a9 Move the visibility pragmas below the includes.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1199
diff changeset
    14
1199
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    15
// !!! FIXME: A lot of this is cut-and-paste from the GLSL version.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    16
#if SUPPORT_PROFILE_METAL
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    17
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    18
#define EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(op) \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    19
    void emit_METAL_##op(Context *ctx) { \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    20
        fail(ctx, #op " unimplemented in Metal profile"); \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    21
    }
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    22
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    23
static inline const char *get_METAL_register_string(Context *ctx,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    24
                        const RegisterType regtype, const int regnum,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    25
                        char *regnum_str, const size_t regnum_size)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    26
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    27
    // turns out these are identical at the moment.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    28
    return get_D3D_register_string(ctx,regtype,regnum,regnum_str,regnum_size);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    29
} // get_METAL_register_string
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    30
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    31
const char *get_METAL_uniform_type(Context *ctx, const RegisterType rtype)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    32
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    33
    switch (rtype)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    34
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    35
        case REG_TYPE_CONST: return "float4";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    36
        case REG_TYPE_CONSTINT: return "int4";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    37
        case REG_TYPE_CONSTBOOL: return "bool";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    38
        default: fail(ctx, "BUG: used a uniform we don't know how to define.");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    39
    } // switch
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    40
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    41
    return NULL;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    42
} // get_METAL_uniform_type
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    43
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    44
const char *get_METAL_varname_in_buf(Context *ctx, RegisterType rt,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    45
                                     int regnum, char *buf,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    46
                                     const size_t len)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    47
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    48
    char regnum_str[16];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    49
    const char *regtype_str = get_METAL_register_string(ctx, rt, regnum,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    50
                                              regnum_str, sizeof (regnum_str));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    51
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    52
    // We don't separate vars with vs_ or ps_ here, because, for the most part,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    53
    //  there are only local vars in Metal shaders.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    54
    snprintf(buf, len, "%s%s", regtype_str, regnum_str);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    55
    return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    56
} // get_METAL_varname_in_buf
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    57
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    58
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    59
const char *get_METAL_varname(Context *ctx, RegisterType rt, int regnum)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    60
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    61
    char buf[64];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    62
    get_METAL_varname_in_buf(ctx, rt, regnum, buf, sizeof (buf));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    63
    return StrDup(ctx, buf);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    64
} // get_METAL_varname
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    65
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    66
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    67
static inline const char *get_METAL_const_array_varname_in_buf(Context *ctx,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    68
                                                const int base, const int size,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    69
                                                char *buf, const size_t buflen)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    70
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    71
    snprintf(buf, buflen, "const_array_%d_%d", base, size);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    72
    return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    73
} // get_METAL_const_array_varname_in_buf
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    74
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    75
const char *get_METAL_const_array_varname(Context *ctx, int base, int size)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    76
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    77
    char buf[64];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    78
    get_METAL_const_array_varname_in_buf(ctx, base, size, buf, sizeof (buf));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    79
    return StrDup(ctx, buf);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    80
} // get_METAL_const_array_varname
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    81
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    82
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    83
static inline const char *get_METAL_input_array_varname(Context *ctx,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    84
                                                char *buf, const size_t buflen)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    85
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    86
    snprintf(buf, buflen, "%s", "vertex_input_array");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    87
    return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    88
} // get_METAL_input_array_varname
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    89
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    90
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    91
const char *get_METAL_uniform_array_varname(Context *ctx,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    92
                                            const RegisterType regtype,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    93
                                            char *buf, const size_t len)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    94
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    95
    const char *shadertype = ctx->shader_type_str;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    96
    const char *type = get_METAL_uniform_type(ctx, regtype);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    97
    snprintf(buf, len, "uniforms.uniforms_%s", type);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    98
    return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    99
} // get_METAL_uniform_array_varname
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   100
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   101
const char *get_METAL_destarg_varname(Context *ctx, char *buf, size_t len)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   102
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   103
    const DestArgInfo *arg = &ctx->dest_arg;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   104
    return get_METAL_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, len);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   105
} // get_METAL_destarg_varname
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   106
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   107
const char *get_METAL_srcarg_varname(Context *ctx, const size_t idx,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   108
                                     char *buf, size_t len)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   109
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   110
    if (idx >= STATICARRAYLEN(ctx->source_args))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   111
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   112
        fail(ctx, "Too many source args");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   113
        *buf = '\0';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   114
        return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   115
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   116
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   117
    const SourceArgInfo *arg = &ctx->source_args[idx];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   118
    return get_METAL_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, len);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   119
} // get_METAL_srcarg_varname
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   120
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   121
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   122
const char *make_METAL_destarg_assign(Context *, char *, const size_t,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   123
                                      const char *, ...) ISPRINTF(4,5);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   124
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   125
const char *make_METAL_destarg_assign(Context *ctx, char *buf,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   126
                                      const size_t buflen,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   127
                                      const char *fmt, ...)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   128
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   129
    int need_parens = 0;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   130
    const DestArgInfo *arg = &ctx->dest_arg;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   131
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   132
    if (arg->writemask == 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   133
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   134
        *buf = '\0';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   135
        return buf;  // no writemask? It's a no-op.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   136
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   137
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   138
    char clampbuf[32] = { '\0' };
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   139
    const char *clampleft = "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   140
    const char *clampright = "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   141
    if (arg->result_mod & MOD_SATURATE)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   142
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   143
        ctx->metal_need_header_common = 1;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   144
        const int vecsize = vecsize_from_writemask(arg->writemask);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   145
        clampleft = "clamp(";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   146
        if (vecsize == 1)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   147
            clampright = ", 0.0, 1.0)";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   148
        else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   149
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   150
            snprintf(clampbuf, sizeof (clampbuf),
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   151
                     ", float%d(0.0), float%d(1.0))", vecsize, vecsize);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   152
            clampright = clampbuf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   153
        } // else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   154
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   155
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   156
    // MSDN says MOD_PP is a hint and many implementations ignore it. So do we.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   157
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   158
    // CENTROID only allowed in DCL opcodes, which shouldn't come through here.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   159
    assert((arg->result_mod & MOD_CENTROID) == 0);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   160
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   161
    if (ctx->predicated)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   162
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   163
        fail(ctx, "predicated destinations unsupported");  // !!! FIXME
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   164
        *buf = '\0';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   165
        return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   166
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   167
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   168
    char operation[256];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   169
    va_list ap;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   170
    va_start(ap, fmt);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   171
    const int len = vsnprintf(operation, sizeof (operation), fmt, ap);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   172
    va_end(ap);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   173
    if (len >= sizeof (operation))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   174
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   175
        fail(ctx, "operation string too large");  // I'm lazy.  :P
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   176
        *buf = '\0';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   177
        return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   178
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   179
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   180
    const char *result_shift_str = "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   181
    switch (arg->result_shift)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   182
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   183
        case 0x1: result_shift_str = " * 2.0"; break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   184
        case 0x2: result_shift_str = " * 4.0"; break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   185
        case 0x3: result_shift_str = " * 8.0"; break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   186
        case 0xD: result_shift_str = " / 8.0"; break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   187
        case 0xE: result_shift_str = " / 4.0"; break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   188
        case 0xF: result_shift_str = " / 2.0"; break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   189
    } // switch
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   190
    need_parens |= (result_shift_str[0] != '\0');
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   191
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   192
    char regnum_str[16];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   193
    const char *regtype_str = get_METAL_register_string(ctx, arg->regtype,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   194
                                                       arg->regnum, regnum_str,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   195
                                                       sizeof (regnum_str));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   196
    char writemask_str[6];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   197
    size_t i = 0;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   198
    const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   199
    if (!scalar && !writemask_xyzw(arg->writemask))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   200
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   201
        writemask_str[i++] = '.';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   202
        if (arg->writemask0) writemask_str[i++] = 'x';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   203
        if (arg->writemask1) writemask_str[i++] = 'y';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   204
        if (arg->writemask2) writemask_str[i++] = 'z';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   205
        if (arg->writemask3) writemask_str[i++] = 'w';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   206
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   207
    writemask_str[i] = '\0';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   208
    assert(i < sizeof (writemask_str));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   209
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   210
    const char *leftparen = (need_parens) ? "(" : "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   211
    const char *rightparen = (need_parens) ? ")" : "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   212
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   213
    snprintf(buf, buflen, "%s%s%s = %s%s%s%s%s%s;",
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   214
             regtype_str, regnum_str, writemask_str,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   215
             clampleft, leftparen, operation, rightparen, result_shift_str,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   216
             clampright);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   217
    // !!! FIXME: make sure the scratch buffer was large enough.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   218
    return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   219
} // make_METAL_destarg_assign
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   220
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   221
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   222
char *make_METAL_swizzle_string(char *swiz_str, const size_t strsize,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   223
                                const int swizzle, const int writemask)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   224
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   225
    size_t i = 0;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   226
    if ( (!no_swizzle(swizzle)) || (!writemask_xyzw(writemask)) )
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   227
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   228
        const int writemask0 = (writemask >> 0) & 0x1;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   229
        const int writemask1 = (writemask >> 1) & 0x1;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   230
        const int writemask2 = (writemask >> 2) & 0x1;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   231
        const int writemask3 = (writemask >> 3) & 0x1;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   232
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   233
        const int swizzle_x = (swizzle >> 0) & 0x3;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   234
        const int swizzle_y = (swizzle >> 2) & 0x3;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   235
        const int swizzle_z = (swizzle >> 4) & 0x3;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   236
        const int swizzle_w = (swizzle >> 6) & 0x3;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   237
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   238
        swiz_str[i++] = '.';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   239
        if (writemask0) swiz_str[i++] = swizzle_channels[swizzle_x];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   240
        if (writemask1) swiz_str[i++] = swizzle_channels[swizzle_y];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   241
        if (writemask2) swiz_str[i++] = swizzle_channels[swizzle_z];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   242
        if (writemask3) swiz_str[i++] = swizzle_channels[swizzle_w];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   243
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   244
    assert(i < strsize);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   245
    swiz_str[i] = '\0';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   246
    return swiz_str;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   247
} // make_METAL_swizzle_string
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   248
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   249
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   250
const char *make_METAL_srcarg_string(Context *ctx, const size_t idx,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   251
                                     const int writemask, char *buf,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   252
                                     const size_t buflen)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   253
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   254
    *buf = '\0';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   255
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   256
    if (idx >= STATICARRAYLEN(ctx->source_args))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   257
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   258
        fail(ctx, "Too many source args");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   259
        return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   260
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   261
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   262
    const SourceArgInfo *arg = &ctx->source_args[idx];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   263
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   264
    const char *premod_str = "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   265
    const char *postmod_str = "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   266
    switch (arg->src_mod)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   267
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   268
        case SRCMOD_NEGATE:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   269
            premod_str = "-";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   270
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   271
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   272
        case SRCMOD_BIASNEGATE:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   273
            premod_str = "-(";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   274
            postmod_str = " - 0.5)";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   275
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   276
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   277
        case SRCMOD_BIAS:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   278
            premod_str = "(";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   279
            postmod_str = " - 0.5)";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   280
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   281
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   282
        case SRCMOD_SIGNNEGATE:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   283
            premod_str = "-((";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   284
            postmod_str = " - 0.5) * 2.0)";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   285
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   286
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   287
        case SRCMOD_SIGN:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   288
            premod_str = "((";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   289
            postmod_str = " - 0.5) * 2.0)";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   290
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   291
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   292
        case SRCMOD_COMPLEMENT:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   293
            premod_str = "(1.0 - ";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   294
            postmod_str = ")";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   295
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   296
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   297
        case SRCMOD_X2NEGATE:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   298
            premod_str = "-(";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   299
            postmod_str = " * 2.0)";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   300
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   301
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   302
        case SRCMOD_X2:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   303
            premod_str = "(";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   304
            postmod_str = " * 2.0)";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   305
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   306
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   307
        case SRCMOD_DZ:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   308
            fail(ctx, "SRCMOD_DZ unsupported"); return buf; // !!! FIXME
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   309
            postmod_str = "_dz";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   310
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   311
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   312
        case SRCMOD_DW:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   313
            fail(ctx, "SRCMOD_DW unsupported"); return buf; // !!! FIXME
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   314
            postmod_str = "_dw";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   315
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   316
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   317
        case SRCMOD_ABSNEGATE:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   318
            ctx->metal_need_header_math = 1;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   319
            premod_str = "-abs(";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   320
            postmod_str = ")";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   321
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   322
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   323
        case SRCMOD_ABS:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   324
            ctx->metal_need_header_math = 1;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   325
            premod_str = "abs(";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   326
            postmod_str = ")";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   327
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   328
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   329
        case SRCMOD_NOT:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   330
            premod_str = "!";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   331
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   332
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   333
        case SRCMOD_NONE:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   334
        case SRCMOD_TOTAL:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   335
             break;  // stop compiler whining.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   336
    } // switch
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   337
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   338
    const char *regtype_str = NULL;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   339
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   340
    if (!arg->relative)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   341
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   342
        regtype_str = get_METAL_varname_in_buf(ctx, arg->regtype, arg->regnum,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   343
                                              (char *) alloca(64), 64);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   344
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   345
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   346
    const char *rel_lbracket = "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   347
    char rel_offset[32] = { '\0' };
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   348
    const char *rel_rbracket = "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   349
    char rel_swizzle[4] = { '\0' };
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   350
    const char *rel_regtype_str = "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   351
    if (arg->relative)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   352
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   353
        if (arg->regtype == REG_TYPE_INPUT)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   354
            regtype_str=get_METAL_input_array_varname(ctx,(char*)alloca(64),64);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   355
        else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   356
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   357
            assert(arg->regtype == REG_TYPE_CONST);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   358
            const int arrayidx = arg->relative_array->index;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   359
            const int offset = arg->regnum - arrayidx;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   360
            assert(offset >= 0);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   361
            if (arg->relative_array->constant)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   362
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   363
                const int arraysize = arg->relative_array->count;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   364
                regtype_str = get_METAL_const_array_varname_in_buf(ctx,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   365
                                arrayidx, arraysize, (char *) alloca(64), 64);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   366
                if (offset != 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   367
                    snprintf(rel_offset, sizeof (rel_offset), "%d + ", offset);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   368
            } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   369
            else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   370
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   371
                regtype_str = get_METAL_uniform_array_varname(ctx, arg->regtype,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   372
                                                      (char *) alloca(64), 64);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   373
                if (offset == 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   374
                {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   375
                    snprintf(rel_offset, sizeof (rel_offset),
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   376
                             "ARRAYBASE_%d + ", arrayidx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   377
                } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   378
                else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   379
                {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   380
                    snprintf(rel_offset, sizeof (rel_offset),
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   381
                             "(ARRAYBASE_%d + %d) + ", arrayidx, offset);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   382
                } // else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   383
            } // else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   384
        } // else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   385
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   386
        rel_lbracket = "[";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   387
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   388
        rel_regtype_str = get_METAL_varname_in_buf(ctx, arg->relative_regtype,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   389
                                                  arg->relative_regnum,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   390
                                                  (char *) alloca(64), 64);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   391
        rel_swizzle[0] = '.';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   392
        rel_swizzle[1] = swizzle_channels[arg->relative_component];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   393
        rel_swizzle[2] = '\0';
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   394
        rel_rbracket = "]";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   395
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   396
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   397
    char swiz_str[6] = { '\0' };
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   398
    if (!isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   399
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   400
        make_METAL_swizzle_string(swiz_str, sizeof (swiz_str),
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   401
                                 arg->swizzle, writemask);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   402
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   403
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   404
    if (regtype_str == NULL)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   405
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   406
        fail(ctx, "Unknown source register type.");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   407
        return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   408
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   409
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   410
    snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s",
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   411
             premod_str, regtype_str, rel_lbracket, rel_offset,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   412
             rel_regtype_str, rel_swizzle, rel_rbracket, swiz_str,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   413
             postmod_str);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   414
    // !!! FIXME: make sure the scratch buffer was large enough.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   415
    return buf;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   416
} // make_METAL_srcarg_string
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   417
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   418
// generate some convenience functions.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   419
#define MAKE_METAL_SRCARG_STRING_(mask, bitmask) \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   420
    static inline const char *make_METAL_srcarg_string_##mask(Context *ctx, \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   421
                                                const size_t idx, char *buf, \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   422
                                                const size_t buflen) { \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   423
        return make_METAL_srcarg_string(ctx, idx, bitmask, buf, buflen); \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   424
    }
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   425
MAKE_METAL_SRCARG_STRING_(x, (1 << 0))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   426
MAKE_METAL_SRCARG_STRING_(y, (1 << 1))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   427
MAKE_METAL_SRCARG_STRING_(z, (1 << 2))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   428
MAKE_METAL_SRCARG_STRING_(w, (1 << 3))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   429
MAKE_METAL_SRCARG_STRING_(scalar, (1 << 0))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   430
MAKE_METAL_SRCARG_STRING_(full, 0xF)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   431
MAKE_METAL_SRCARG_STRING_(masked, ctx->dest_arg.writemask)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   432
MAKE_METAL_SRCARG_STRING_(vec3, 0x7)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   433
MAKE_METAL_SRCARG_STRING_(vec2, 0x3)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   434
#undef MAKE_METAL_SRCARG_STRING_
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   435
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   436
// special cases for comparison opcodes...
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   437
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   438
const char *get_METAL_comparison_string_scalar(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   439
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   440
    const char *comps[] = { "", ">", "==", ">=", "<", "!=", "<=" };
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   441
    if (ctx->instruction_controls >= STATICARRAYLEN(comps))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   442
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   443
        fail(ctx, "unknown comparison control");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   444
        return "";
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   445
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   446
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   447
    return comps[ctx->instruction_controls];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   448
} // get_METAL_comparison_string_scalar
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   449
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   450
const char *get_METAL_comparison_string_vector(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   451
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   452
    return get_METAL_comparison_string_scalar(ctx);  // standard C operators work for vectors in Metal.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   453
} // get_METAL_comparison_string_vector
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   454
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   455
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   456
void emit_METAL_start(Context *ctx, const char *profilestr)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   457
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   458
    if (!shader_is_vertex(ctx) && !shader_is_pixel(ctx))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   459
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   460
        failf(ctx, "Shader type %u unsupported in this profile.",
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   461
              (uint) ctx->shader_type);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   462
        return;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   463
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   464
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   465
    if (!ctx->mainfn)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   466
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   467
        if (shader_is_vertex(ctx))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   468
            ctx->mainfn = StrDup(ctx, "VertexShader");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   469
        else if (shader_is_pixel(ctx))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   470
            ctx->mainfn = StrDup(ctx, "FragmentShader");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   471
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   472
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   473
    set_output(ctx, &ctx->mainline);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   474
    ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   475
} // emit_METAL_start
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   476
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   477
void emit_METAL_RET(Context *ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   478
void emit_METAL_end(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   479
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   480
    // !!! FIXME: maybe handle this at a higher level?
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   481
    // ps_1_* writes color to r0 instead oC0. We move it to the right place.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   482
    // We don't have to worry about a RET opcode messing this up, since
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   483
        //  RET isn't available before ps_2_0.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   484
    if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   485
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   486
        set_used_register(ctx, REG_TYPE_COLOROUT, 0, 1);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   487
        output_line(ctx, "oC0 = r0;");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   488
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   489
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   490
    // !!! FIXME: maybe handle this at a higher level?
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   491
    // force a RET opcode if we're at the end of the stream without one.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   492
    if (ctx->previous_opcode != OPCODE_RET)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   493
        emit_METAL_RET(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   494
} // emit_METAL_end
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   495
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   496
void emit_METAL_phase(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   497
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   498
    // no-op in Metal.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   499
} // emit_METAL_phase
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   500
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   501
void emit_METAL_finalize(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   502
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   503
    // If we had a relative addressing of REG_TYPE_INPUT, we need to build
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   504
    //  an array for it at the start of main(). GLSL doesn't let you specify
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   505
    //  arrays of attributes.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   506
    //float4 blah_array[BIGGEST_ARRAY];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   507
    if (ctx->have_relative_input_registers) // !!! FIXME
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   508
        fail(ctx, "Relative addressing of input registers not supported.");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   509
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   510
    // Insert header includes we need...
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   511
    push_output(ctx, &ctx->preflight);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   512
    #define INC_METAL_HEADER(name) \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   513
        if (ctx->metal_need_header_##name) { \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   514
            output_line(ctx, "#include <metal_" #name ">"); \
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   515
        }
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   516
    INC_METAL_HEADER(common);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   517
    INC_METAL_HEADER(math);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   518
    INC_METAL_HEADER(relational);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   519
    INC_METAL_HEADER(geometric);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   520
    INC_METAL_HEADER(graphics);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   521
    INC_METAL_HEADER(texture);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   522
    #undef INC_METAL_HEADER
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   523
    output_blank_line(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   524
    output_line(ctx, "using namespace metal;");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   525
    output_blank_line(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   526
    pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   527
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   528
    // Fill in the shader's mainline function signature.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   529
    push_output(ctx, &ctx->mainline_intro);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   530
    output_line(ctx, "%s %s%s %s (",
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   531
                shader_is_vertex(ctx) ? "vertex" : "fragment",
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   532
                ctx->outputs ? ctx->mainfn : "void",
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   533
                ctx->outputs ? "_Output" : "", ctx->mainfn);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   534
    pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   535
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   536
    push_output(ctx, &ctx->mainline_arguments);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   537
    ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   538
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   539
    const int uniform_count = ctx->uniform_float4_count + ctx->uniform_int4_count + ctx->uniform_bool_count;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   540
    int commas = 0;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   541
    if (uniform_count) commas++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   542
    if (ctx->inputs) commas++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   543
    if (commas) commas--;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   544
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   545
    if (uniform_count > 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   546
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   547
        push_output(ctx, &ctx->globals);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   548
        output_line(ctx, "struct %s_Uniforms", ctx->mainfn);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   549
        output_line(ctx, "{");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   550
        ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   551
        if (ctx->uniform_float4_count > 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   552
            output_line(ctx, "float4 uniforms_float4[%d];", ctx->uniform_float4_count);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   553
        if (ctx->uniform_int4_count > 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   554
            output_line(ctx, "int4 uniforms_int4[%d];", ctx->uniform_int4_count);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   555
        if (ctx->uniform_bool_count > 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   556
            output_line(ctx, "bool uniforms_bool[%d];", ctx->uniform_bool_count);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   557
        ctx->indent--;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   558
        output_line(ctx, "};");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   559
        pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   560
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   561
        output_line(ctx, "constant %s_Uniforms &uniforms [[buffer(16)]]%s", ctx->mainfn, commas ? "," : "");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   562
        commas--;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   563
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   564
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   565
    if (ctx->inputs)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   566
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   567
        output_line(ctx, "%s_Input input [[stage_in]]%s", ctx->mainfn, commas ? "," : "");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   568
        commas--;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   569
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   570
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   571
    ctx->indent--;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   572
    output_line(ctx, ") {");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   573
    if (ctx->outputs)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   574
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   575
        ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   576
        output_line(ctx, "%s_Output output;", ctx->mainfn);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   577
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   578
        push_output(ctx, &ctx->mainline);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   579
        ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   580
        output_line(ctx, "return output;");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   581
        pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   582
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   583
    pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   584
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   585
    if (ctx->inputs)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   586
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   587
        push_output(ctx, &ctx->inputs);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   588
        output_line(ctx, "};");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   589
        output_blank_line(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   590
        pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   591
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   592
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   593
    if (ctx->outputs)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   594
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   595
        push_output(ctx, &ctx->outputs);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   596
        output_line(ctx, "};");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   597
        output_blank_line(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   598
        pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   599
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   600
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   601
    // throw some blank lines around to make source more readable.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   602
    if (ctx->globals)  // don't add a blank line if the section is empty.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   603
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   604
        push_output(ctx, &ctx->globals);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   605
        output_blank_line(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   606
        pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   607
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   608
} // emit_METAL_finalize
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   609
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   610
void emit_METAL_global(Context *ctx, RegisterType regtype, int regnum)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   611
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   612
    char varname[64];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   613
    get_METAL_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   614
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   615
    // These aren't actually global in metal, set them up at top of mainline.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   616
    push_output(ctx, &ctx->mainline_top);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   617
    ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   618
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   619
    switch (regtype)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   620
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   621
        case REG_TYPE_ADDRESS:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   622
            if (shader_is_vertex(ctx))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   623
                output_line(ctx, "int4 %s;", varname);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   624
            else if (shader_is_pixel(ctx))  // actually REG_TYPE_TEXTURE.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   625
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   626
                // We have to map texture registers to temps for ps_1_1, since
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   627
                //  they work like temps, initialize with tex coords, and the
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   628
                //  ps_1_1 TEX opcode expects to overwrite it.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   629
                if (!shader_version_atleast(ctx, 1, 4))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   630
                    output_line(ctx, "float4 %s = input.%s;",varname,varname);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   631
            } // else if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   632
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   633
        case REG_TYPE_PREDICATE:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   634
            output_line(ctx, "bool4 %s;", varname);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   635
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   636
        case REG_TYPE_TEMP:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   637
            output_line(ctx, "float4 %s;", varname);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   638
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   639
        case REG_TYPE_LOOP:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   640
            break; // no-op. We declare these in for loops at the moment.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   641
        case REG_TYPE_LABEL:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   642
            break; // no-op. If we see it here, it means we optimized it out.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   643
        default:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   644
            fail(ctx, "BUG: we used a register we don't know how to define.");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   645
            break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   646
    } // switch
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   647
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   648
    pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   649
} // emit_METAL_global
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   650
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   651
void emit_METAL_array(Context *ctx, VariableList *var)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   652
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   653
    // All uniforms (except constant arrays, which are literally constant
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   654
    //  data embedded in Metal shaders) are now packed into a single array,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   655
    //  so we can batch the uniform transfers. So this doesn't actually
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   656
    //  define an array here; the one, big array is emitted during
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   657
    //  finalization instead.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   658
    // However, we need to #define the offset into the one, big array here,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   659
    //  and let dereferences use that #define.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   660
    const int base = var->index;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   661
    const int metalbase = ctx->uniform_float4_count;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   662
    push_output(ctx, &ctx->mainline_top);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   663
    ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   664
    output_line(ctx, "const int ARRAYBASE_%d = %d;", base, metalbase);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   665
    pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   666
    var->emit_position = metalbase;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   667
} // emit_METAL_array
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   668
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   669
void emit_METAL_const_array(Context *ctx, const ConstantsList *clist,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   670
                            int base, int size)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   671
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   672
    char varname[64];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   673
    get_METAL_const_array_varname_in_buf(ctx,base,size,varname,sizeof(varname));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   674
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   675
    const char *cstr = NULL;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   676
    push_output(ctx, &ctx->mainline_top);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   677
    ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   678
    output_line(ctx, "const float4 %s[%d] = {", varname, size);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   679
    ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   680
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   681
    int i;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   682
    for (i = 0; i < size; i++)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   683
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   684
        while (clist->constant.type != MOJOSHADER_UNIFORM_FLOAT)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   685
            clist = clist->next;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   686
        assert(clist->constant.index == (base + i));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   687
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   688
        char val0[32];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   689
        char val1[32];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   690
        char val2[32];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   691
        char val3[32];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   692
        floatstr(ctx, val0, sizeof (val0), clist->constant.value.f[0], 1);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   693
        floatstr(ctx, val1, sizeof (val1), clist->constant.value.f[1], 1);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   694
        floatstr(ctx, val2, sizeof (val2), clist->constant.value.f[2], 1);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   695
        floatstr(ctx, val3, sizeof (val3), clist->constant.value.f[3], 1);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   696
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   697
        output_line(ctx, "float4(%s, %s, %s, %s)%s", val0, val1, val2, val3,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   698
                        (i < (size-1)) ? "," : "");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   699
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   700
        clist = clist->next;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   701
    } // for
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   702
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   703
    ctx->indent--;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   704
    output_line(ctx, "};");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   705
    output_line(ctx, "(void) %s[0];", varname);  // stop compiler warnings.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   706
    pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   707
} // emit_METAL_const_array
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   708
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   709
void emit_METAL_uniform(Context *ctx, RegisterType regtype, int regnum,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   710
                              const VariableList *var)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   711
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   712
    // Now that we're pushing all the uniforms as one struct, pack these
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   713
    //  down, so if we only use register c439, it'll actually map to
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   714
    //  uniforms.uniforms_float4[0]. As we push one big struct, this will
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   715
    //  prevent uploading unused data.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   716
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   717
    const char *utype = get_METAL_uniform_type(ctx, regtype);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   718
    char varname[64];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   719
    char name[64];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   720
    int index = 0;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   721
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   722
    get_METAL_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   723
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   724
    push_output(ctx, &ctx->mainline_top);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   725
    ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   726
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   727
    if (var == NULL)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   728
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   729
        get_METAL_uniform_array_varname(ctx, regtype, name, sizeof (name));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   730
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   731
        if (regtype == REG_TYPE_CONST)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   732
            index = ctx->uniform_float4_count;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   733
        else if (regtype == REG_TYPE_CONSTINT)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   734
            index = ctx->uniform_int4_count;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   735
        else if (regtype == REG_TYPE_CONSTBOOL)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   736
            index = ctx->uniform_bool_count;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   737
        else  // get_METAL_uniform_array_varname() would have called fail().
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   738
            assert(!(ctx->isfail));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   739
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   740
        // !!! FIXME: can cause unused var warnings in Clang...
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   741
        //output_line(ctx, "constant %s &%s = %s[%d];", utype, varname, name, index);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   742
        output_line(ctx, "#define %s %s[%d]", varname, name, index);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   743
        push_output(ctx, &ctx->mainline);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   744
        ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   745
        output_line(ctx, "#undef %s", varname);  // !!! FIXME: gross.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   746
        pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   747
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   748
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   749
    else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   750
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   751
        const int arraybase = var->index;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   752
        if (var->constant)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   753
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   754
            get_METAL_const_array_varname_in_buf(ctx, arraybase, var->count,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   755
                                                name, sizeof (name));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   756
            index = (regnum - arraybase);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   757
        } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   758
        else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   759
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   760
            assert(var->emit_position != -1);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   761
            get_METAL_uniform_array_varname(ctx, regtype, name, sizeof (name));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   762
            index = (regnum - arraybase) + var->emit_position;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   763
        } // else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   764
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   765
        // !!! FIXME: might trigger unused var warnings in Clang.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   766
        //output_line(ctx, "constant %s &%s = %s[%d];", utype, varname, name, index);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   767
        output_line(ctx, "#define %s %s[%d];", varname, name, index);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   768
        push_output(ctx, &ctx->mainline);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   769
        ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   770
        output_line(ctx, "#undef %s", varname);  // !!! FIXME: gross.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   771
        pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   772
    } // else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   773
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   774
    pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   775
} // emit_METAL_uniform
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   776
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   777
void emit_METAL_sampler(Context *ctx,int stage,TextureType ttype,int tb)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   778
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   779
    char var[64];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   780
    const char *texsuffix = NULL;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   781
    switch (ttype)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   782
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   783
        case TEXTURE_TYPE_2D: texsuffix = "2d"; break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   784
        case TEXTURE_TYPE_CUBE: texsuffix = "cube"; break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   785
        case TEXTURE_TYPE_VOLUME: texsuffix = "3d"; break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   786
        default: assert(!"unexpected texture type"); return;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   787
    } // switch
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   788
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   789
    get_METAL_varname_in_buf(ctx, REG_TYPE_SAMPLER, stage, var, sizeof (var));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   790
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   791
    push_output(ctx, &ctx->mainline_arguments);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   792
    ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   793
    output_line(ctx, "texture%s<float> %s_texture [[texture(%d)]],",
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   794
                texsuffix, var, stage);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   795
    output_line(ctx, "sampler %s [[sampler(%d)]],", var, stage);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   796
    pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   797
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   798
    if (tb)  // This sampler used a ps_1_1 TEXBEM opcode?
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   799
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   800
        push_output(ctx, &ctx->mainline_top);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   801
        ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   802
        char name[64];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   803
        const int index = ctx->uniform_float4_count;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   804
        ctx->uniform_float4_count += 2;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   805
        get_METAL_uniform_array_varname(ctx, REG_TYPE_CONST, name, sizeof (name));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   806
        output_line(ctx, "constant float4 &%s_texbem = %s[%d];", var, name, index);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   807
        output_line(ctx, "constant float4 &%s_texbeml = %s[%d];", var, name, index+1);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   808
        pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   809
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   810
} // emit_METAL_sampler
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   811
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   812
void emit_METAL_attribute(Context *ctx, RegisterType regtype, int regnum,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   813
                          MOJOSHADER_usage usage, int index, int wmask,
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   814
                          int flags)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   815
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   816
    // !!! FIXME: this function doesn't deal with write masks at all yet!
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   817
    const char *usage_str = NULL;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   818
    char index_str[16] = { '\0' };
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   819
    char var[64];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   820
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   821
    get_METAL_varname_in_buf(ctx, regtype, regnum, var, sizeof (var));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   822
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   823
    //assert((flags & MOD_PP) == 0);  // !!! FIXME: is PP allowed?
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   824
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   825
    if (index != 0)  // !!! FIXME: a lot of these MUST be zero.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   826
        snprintf(index_str, sizeof (index_str), "%u", (uint) index);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   827
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   828
    if (shader_is_vertex(ctx))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   829
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   830
        // pre-vs3 output registers.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   831
        // these don't ever happen in DCL opcodes, I think. Map to vs_3_*
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   832
        //  output registers.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   833
        if (!shader_version_atleast(ctx, 3, 0))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   834
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   835
            if (regtype == REG_TYPE_RASTOUT)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   836
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   837
                regtype = REG_TYPE_OUTPUT;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   838
                index = regnum;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   839
                switch ((const RastOutType) regnum)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   840
                {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   841
                    case RASTOUT_TYPE_POSITION:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   842
                        usage = MOJOSHADER_USAGE_POSITION;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   843
                        break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   844
                    case RASTOUT_TYPE_FOG:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   845
                        usage = MOJOSHADER_USAGE_FOG;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   846
                        break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   847
                    case RASTOUT_TYPE_POINT_SIZE:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   848
                        usage = MOJOSHADER_USAGE_POINTSIZE;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   849
                        break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   850
                } // switch
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   851
            } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   852
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   853
            else if (regtype == REG_TYPE_ATTROUT)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   854
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   855
                regtype = REG_TYPE_OUTPUT;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   856
                usage = MOJOSHADER_USAGE_COLOR;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   857
                index = regnum;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   858
            } // else if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   859
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   860
            else if (regtype == REG_TYPE_TEXCRDOUT)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   861
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   862
                regtype = REG_TYPE_OUTPUT;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   863
                usage = MOJOSHADER_USAGE_TEXCOORD;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   864
                index = regnum;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   865
            } // else if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   866
        } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   867
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   868
        if (regtype == REG_TYPE_INPUT)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   869
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   870
            push_output(ctx, &ctx->inputs);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   871
            if (buffer_size(ctx->inputs) == 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   872
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   873
                output_line(ctx, "struct %s_Input", ctx->mainfn);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   874
                output_line(ctx, "{");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   875
            } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   876
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   877
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   878
            output_line(ctx, "float4 %s [[attribute(%d)]];", var, regnum);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   879
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   880
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   881
            push_output(ctx, &ctx->mainline_top);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   882
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   883
            // !!! FIXME: might trigger unused var warnings in Clang.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   884
            //output_line(ctx, "constant float4 &%s = input.%s;", var, var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   885
            output_line(ctx, "#define %s input.%s", var, var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   886
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   887
            push_output(ctx, &ctx->mainline);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   888
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   889
            output_line(ctx, "#undef %s", var);  // !!! FIXME: gross.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   890
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   891
        } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   892
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   893
        else if (regtype == REG_TYPE_OUTPUT)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   894
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   895
            push_output(ctx, &ctx->outputs);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   896
            if (buffer_size(ctx->outputs) == 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   897
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   898
                output_line(ctx, "struct %s_Output", ctx->mainfn);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   899
                output_line(ctx, "{");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   900
            } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   901
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   902
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   903
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   904
            switch (usage)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   905
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   906
                case MOJOSHADER_USAGE_POSITION:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   907
                    output_line(ctx, "float4 %s [[position]];", var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   908
                    break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   909
                case MOJOSHADER_USAGE_POINTSIZE:
1220
048797e6aae1 Fix point_size type for MSL
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1219
diff changeset
   910
                    output_line(ctx, "float %s [[point_size]];", var);
1199
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   911
                    break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   912
                case MOJOSHADER_USAGE_COLOR:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   913
                    output_line(ctx, "float4 %s [[user(color%d)]];", var, index);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   914
                    break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   915
                case MOJOSHADER_USAGE_FOG:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   916
                    output_line(ctx, "float4 %s [[user(fog)]];", var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   917
                    break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   918
                case MOJOSHADER_USAGE_TEXCOORD:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   919
                    output_line(ctx, "float4 %s [[user(texcoord%d)]];", var, index);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   920
                    break;
1217
065dcf3eed9c Handle MOJOSHADER_USAGE_NORMAL in Metal profile
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1200
diff changeset
   921
                case MOJOSHADER_USAGE_NORMAL:
065dcf3eed9c Handle MOJOSHADER_USAGE_NORMAL in Metal profile
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1200
diff changeset
   922
                    output_line(ctx, "float4 %s [[user(normal)]];", var);
1199
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   923
                default:
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   924
                    // !!! FIXME: we need to deal with some more built-in varyings here.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   925
                    break;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   926
            } // switch
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   927
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   928
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   929
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   930
            push_output(ctx, &ctx->mainline_top);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   931
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   932
            // !!! FIXME: this doesn't work.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   933
            //output_line(ctx, "float4 &%s = output.%s;", var, var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   934
            output_line(ctx, "#define %s output.%s", var, var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   935
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   936
            push_output(ctx, &ctx->mainline);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   937
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   938
            output_line(ctx, "#undef %s", var);  // !!! FIXME: gross.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   939
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   940
        } // else if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   941
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   942
        else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   943
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   944
            fail(ctx, "unknown vertex shader attribute register");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   945
        } // else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   946
    } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   947
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   948
    else if (shader_is_pixel(ctx))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   949
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   950
        // samplers DCLs get handled in emit_METAL_sampler().
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   951
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   952
        if (flags & MOD_CENTROID)  // !!! FIXME
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   953
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   954
            failf(ctx, "centroid unsupported in %s profile", ctx->profile->name);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   955
            return;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   956
        } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   957
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   958
        if ((regtype == REG_TYPE_COLOROUT) || (regtype == REG_TYPE_DEPTHOUT))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   959
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   960
            push_output(ctx, &ctx->outputs);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   961
            if (buffer_size(ctx->outputs) == 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   962
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   963
                output_line(ctx, "struct %s_Output", ctx->mainfn);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   964
                output_line(ctx, "{");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   965
            } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   966
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   967
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   968
            if (regtype == REG_TYPE_COLOROUT)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   969
                output_line(ctx, "float4 %s [[color(%d)]];", var, regnum);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   970
            else if (regtype == REG_TYPE_DEPTHOUT)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   971
                output_line(ctx, "float %s [[depth(any)]];", var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   972
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   973
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   974
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   975
            push_output(ctx, &ctx->mainline_top);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   976
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   977
            // !!! FIXME: this doesn't work.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   978
            //output_line(ctx, "float%s &%s = output.%s;", (regtype == REG_TYPE_DEPTHOUT) ? "" : "4", var, var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   979
            output_line(ctx, "#define %s output.%s", var, var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   980
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   981
            push_output(ctx, &ctx->mainline);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   982
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   983
            output_line(ctx, "#undef %s", var);  // !!! FIXME: gross.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   984
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   985
        } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   986
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   987
        // !!! FIXME: can you actualy have a texture register with COLOR usage?
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   988
        else if ((regtype == REG_TYPE_TEXTURE) ||
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   989
                 (regtype == REG_TYPE_INPUT) ||
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   990
                 (regtype == REG_TYPE_MISCTYPE))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   991
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   992
            int skipreference = 0;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   993
            push_output(ctx, &ctx->inputs);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   994
            if (buffer_size(ctx->inputs) == 0)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   995
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   996
                output_line(ctx, "struct %s_Input", ctx->mainfn);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   997
                output_line(ctx, "{");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   998
            } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   999
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1000
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1001
            if (regtype == REG_TYPE_MISCTYPE)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1002
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1003
                const MiscTypeType mt = (MiscTypeType) regnum;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1004
                if (mt == MISCTYPE_TYPE_FACE)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1005
                    output_line(ctx, "bool %s [[front_facing]];", var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1006
                else if (mt == MISCTYPE_TYPE_POSITION)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1007
                    output_line(ctx, "float4 %s [[position]];", var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1008
                else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1009
                    fail(ctx, "BUG: unhandled misc register");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1010
            } // else if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1011
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1012
            else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1013
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1014
                if (usage == MOJOSHADER_USAGE_TEXCOORD)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1015
                {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1016
                    // ps_1_1 does a different hack for this attribute.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1017
                    //  Refer to emit_METAL_global()'s REG_TYPE_ADDRESS code.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1018
                    if (!shader_version_atleast(ctx, 1, 4))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1019
                        skipreference = 1;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1020
                    output_line(ctx, "float4 %s [[user(texcoord%d)]];", var, index);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1021
                } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1022
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1023
                else if (usage == MOJOSHADER_USAGE_COLOR)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1024
                    output_line(ctx, "float4 %s [[user(color%d)]];", var, index);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1025
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1026
                else if (usage == MOJOSHADER_USAGE_FOG)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1027
                    output_line(ctx, "float4 %s [[user(fog)]];", var);
1217
065dcf3eed9c Handle MOJOSHADER_USAGE_NORMAL in Metal profile
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1200
diff changeset
  1028
065dcf3eed9c Handle MOJOSHADER_USAGE_NORMAL in Metal profile
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1200
diff changeset
  1029
                else if (usage == MOJOSHADER_USAGE_NORMAL)
065dcf3eed9c Handle MOJOSHADER_USAGE_NORMAL in Metal profile
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1200
diff changeset
  1030
                    output_line(ctx, "float4 %s [[user(normal)]];", var);
1199
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1031
            } // else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1032
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1033
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1034
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1035
            // !!! FIXME: can cause unused var warnings in Clang...
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1036
            #if 0
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1037
            push_output(ctx, &ctx->mainline_top);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1038
            ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1039
            if ((regtype == REG_TYPE_MISCTYPE)&&(regnum == MISCTYPE_TYPE_FACE))
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1040
                output_line(ctx, "constant bool &%s = input.%s;", var, var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1041
            else if (!skipreference)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1042
                output_line(ctx, "constant float4 &%s = input.%s;", var, var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1043
            pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1044
            #endif
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1045
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1046
            if (!skipreference)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1047
            {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1048
                push_output(ctx, &ctx->mainline_top);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1049
                ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1050
                output_line(ctx, "#define %s input.%s", var, var);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1051
                pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1052
                push_output(ctx, &ctx->mainline);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1053
                ctx->indent++;
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1054
                output_line(ctx, "#undef %s", var);  // !!! FIXME: gross.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1055
                pop_output(ctx);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1056
            } // if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1057
        } // else if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1058
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1059
        else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1060
        {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1061
            fail(ctx, "unknown pixel shader attribute register");
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1062
        } // else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1063
    } // else if
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1064
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1065
    else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1066
    {
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1067
        fail(ctx, "Unknown shader type");  // state machine should catch this.
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1068
    } // else
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1069
} // emit_METAL_attribute
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1070
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1071
void emit_METAL_NOP(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1072
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1073
    // no-op is a no-op.  :)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1074
} // emit_METAL_NOP
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1075
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1076
void emit_METAL_MOV(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1077
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1078
    char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1079
    char code[128];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1080
    make_METAL_destarg_assign(ctx, code, sizeof (code), "%s", src0);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1081
    output_line(ctx, "%s", code);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1082
} // emit_METAL_MOV
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1083
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1084
void emit_METAL_ADD(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1085
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1086
    char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1087
    char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1088
    char code[128];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1089
    make_METAL_destarg_assign(ctx, code, sizeof (code), "%s + %s", src0, src1);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1090
    output_line(ctx, "%s", code);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1091
} // emit_METAL_ADD
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1092
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1093
void emit_METAL_SUB(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1094
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1095
    char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1096
    char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1097
    char code[128];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1098
    make_METAL_destarg_assign(ctx, code, sizeof (code), "%s - %s", src0, src1);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1099
    output_line(ctx, "%s", code);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1100
} // emit_METAL_SUB
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1101
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1102
void emit_METAL_MAD(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1103
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1104
    char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1105
    char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1106
    char src2[64]; make_METAL_srcarg_string_masked(ctx, 2, src2, sizeof (src2));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1107
    char code[128];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1108
    make_METAL_destarg_assign(ctx, code, sizeof (code), "(%s * %s) + %s", src0, src1, src2);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1109
    output_line(ctx, "%s", code);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1110
} // emit_METAL_MAD
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1111
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1112
void emit_METAL_MUL(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1113
{
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1114
    char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1115
    char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1116
    char code[128];
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1117
    make_METAL_destarg_assign(ctx, code, sizeof (code), "%s * %s", src0, src1);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1118
    output_line(ctx, "%s", code);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1119
} // emit_METAL_MUL
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1120
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1121
void emit_METAL_RCP(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1122
{
1297
5667281f0da9 metal: Fix RCP and RSQ output to match GLSL.
Ryan C. Gordon <icculus@icculus.org>
parents: 1220
diff changeset
  1123
    const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
5667281f0da9 metal: Fix RCP and RSQ output to match GLSL.
Ryan C. Gordon <icculus@icculus.org>
parents: 1220
diff changeset
  1124
    char cast[16] = { '\0' };
5667281f0da9 metal: Fix RCP and RSQ output to match GLSL.
Ryan C. Gordon <icculus@icculus.org>
parents: 1220
diff changeset
  1125
    if (vecsize != 1)
5667281f0da9 metal: Fix RCP and RSQ output to match GLSL.
Ryan C. Gordon <icculus@icculus.org>
parents: 1220
diff changeset
  1126
        snprintf(cast, sizeof (cast), "float%d", vecsize);
5667281f0da9 metal: Fix RCP and RSQ output to match GLSL.
Ryan C. Gordon <icculus@icculus.org>
parents: 1220
diff changeset
  1127
    char src0[64]; make_METAL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
1199
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1128
    char code[128];
1219
c239540656e7 Replicate D3D's div by 0 behavior for RCP/RSQ
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1217
diff changeset
  1129
    ctx->metal_need_header_math = 1;
1297
5667281f0da9 metal: Fix RCP and RSQ output to match GLSL.
Ryan C. Gordon <icculus@icculus.org>
parents: 1220
diff changeset
  1130
    make_METAL_destarg_assign(ctx, code, sizeof (code), "%s((%s == 0.0) ? FLT_MAX : 1.0 / %s)", cast, src0, src0);
1199
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1131
    output_line(ctx, "%s", code);
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1132
} // emit_METAL_RCP
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1133
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1134
void emit_METAL_RSQ(Context *ctx)
b8ece252a201 Reorganize profiles into their own files
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
  1135
{
1297
5667281f0da9 metal: Fix RCP and RSQ output to match GLSL.
Ryan C. Gordon <icculus@icculus.org>
parents: 1220
diff changeset
  1136
    const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
5667281f0da9 metal: Fix RCP and RSQ output to match GLSL.
Ryan C. Gordon <icculus@icculus.org>
parents: 1220
diff changeset
  1137
    char cast[16] = { '\0' };
5667281f0da9 metal: Fix RCP and RSQ output to match GLSL.
Ryan C