mojoshader_metal.c
author Caleb Cornett <caleb.cornett@outlook.com>
Tue, 07 Jul 2020 19:25:47 -0400
changeset 1279 b1f51cfa8933
parent 1252 ed07dd994125
permissions -rw-r--r--
Metal uniform buffer overhaul. 1. Metal now uses a single, ~1MB uniform buffer per frame that is shared among all shaders. From my testing this should be more than enough room for the FNA catalog (aside from possibly Little Racers Street with its memory leak). This is a major boon for Metal memory management since it only requires one buffer instead of 3,000 (in LRS's case)! And it simplifies the internal workings of mojoshader_metal dramatically. 2. Metal now has a MOJOSHADER_mtlMakeContextCurrent API to match GL and Vulkan. Other parts of the context API have been rewritten as well to match the behavior you'd expect. 3. The MOJOSHADER_mtlGetUniformBuffers function has been replaced with MOJOSHADER_mtlGetUniformData. Now that we only use a single uniform buffer, there's no need to return one for each of the shaders. 4. The MOJOSHADER_mtlGetFunctionHandle function no longer allocates memory. Instead, all MTLFunction handles are cached in the MOJOSHADER_mtlShader structs at library creation time. This removes the burden of memory management from the user. 5. A variety of formatting and documentation updates.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     1
/**
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     3
 *  Direct3D shaders.
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     4
 *
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     6
 *
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     8
 */
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     9
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    10
#define __MOJOSHADER_INTERNAL__ 1
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    11
#include "mojoshader_internal.h"
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    12
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    13
#if (defined(__APPLE__) && defined(__MACH__))
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    14
#define PLATFORM_APPLE 1
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    15
#endif /* (defined(__APPLE__) && defined(__MACH__)) */
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    16
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    17
typedef struct MOJOSHADER_mtlShader
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    18
{
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    19
    const MOJOSHADER_parseData *parseData;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    20
    uint32 refcount;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    21
    void *handle; // MTLFunction*
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    22
} MOJOSHADER_mtlShader;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    23
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    24
// profile-specific implementations...
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    25
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    26
#if SUPPORT_PROFILE_METAL && PLATFORM_APPLE
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    27
#ifdef MOJOSHADER_EFFECT_SUPPORT
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    28
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    29
#include "TargetConditionals.h"
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    30
#include <objc/message.h>
1241
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
    31
#define msg     ((void* (*)(void*, void*))objc_msgSend)
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
    32
#define msg_s   ((void* (*)(void*, void*, const char*))objc_msgSend)
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
    33
#define msg_p   ((void* (*)(void*, void*, void*))objc_msgSend)
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    34
#define msg_uu  ((void* (*)(void*, void*, uint64, uint64))objc_msgSend)
1241
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
    35
#define msg_ppp ((void* (*)(void*, void*, void*, void*, void*))objc_msgSend)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    36
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    37
// Error state...
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    38
static char error_buffer[1024] = { '\0' };
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    39
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    40
static void set_error(const char *str)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    41
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    42
    snprintf(error_buffer, sizeof (error_buffer), "%s", str);
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    43
} // set_error
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    44
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    45
static inline void out_of_memory(void)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    46
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    47
    set_error("out of memory");
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    48
} // out_of_memory
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    49
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    50
// Max entries for each register file type...
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    51
#define MAX_REG_FILE_F 8192
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    52
#define MAX_REG_FILE_I 2047
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    53
#define MAX_REG_FILE_B 2047
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    54
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    55
typedef struct MOJOSHADER_mtlContext
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    56
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    57
    // Allocators...
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    58
    MOJOSHADER_malloc malloc_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    59
    MOJOSHADER_free free_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    60
    void *malloc_data;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    61
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    62
    // The constant register files...
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    63
    // !!! FIXME: Man, it kills me how much memory this takes...
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    64
    // !!! FIXME:  ... make this dynamically allocated on demand.
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    65
    float vs_reg_file_f[MAX_REG_FILE_F * 4];
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    66
    int vs_reg_file_i[MAX_REG_FILE_I * 4];
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    67
    uint8 vs_reg_file_b[MAX_REG_FILE_B];
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    68
    float ps_reg_file_f[MAX_REG_FILE_F * 4];
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    69
    int ps_reg_file_i[MAX_REG_FILE_I * 4];
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    70
    uint8 ps_reg_file_b[MAX_REG_FILE_B];
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    71
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    72
    // Pointer to the active MTLDevice.
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    73
    void* device;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    74
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    75
    // The uniform MTLBuffer shared between all shaders in the context.
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    76
    void *ubo;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    77
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    78
    // The current offsets into the UBO, per shader.
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    79
    int vertexUniformOffset;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    80
    int pixelUniformOffset;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    81
    int totalUniformOffset;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    82
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    83
    // The currently bound shaders.
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    84
    MOJOSHADER_mtlShader *vertexShader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    85
    MOJOSHADER_mtlShader *pixelShader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    86
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    87
    // Objective-C Selectors
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    88
    void* classNSString;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    89
    void* selAlloc;
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    90
    void* selContents;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    91
    void* selInitWithUTF8String;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    92
    void* selLength;
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    93
    void* selLocalizedDescription;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    94
    void* selNewBufferWithLength;
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    95
    void* selNewFunctionWithName;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    96
    void* selNewLibraryWithSource;
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    97
    void* selRelease;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
    98
    void* selUTF8String;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    99
} MOJOSHADER_mtlContext;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   100
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   101
static MOJOSHADER_mtlContext *ctx = NULL;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   102
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   103
/* Uniform buffer utilities */
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   104
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   105
static inline int next_highest_alignment(int n)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   106
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   107
    #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_SIMULATOR
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   108
    int align = 16;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   109
    #else
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   110
    // !!! FIXME: Will Apple Silicon Macs have a different minimum alignment?
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   111
    int align = 256;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   112
    #endif
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   113
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   114
    return align * ((n + align - 1) / align);
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   115
} // next_highest_alignment
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   116
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   117
static void update_uniform_buffer(MOJOSHADER_mtlShader *shader)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   118
{
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   119
    if (shader == NULL || shader->parseData->uniform_count == 0)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   120
        return;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   121
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   122
    float *regF; int *regI; uint8 *regB;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   123
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   124
    {
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   125
        ctx->vertexUniformOffset = ctx->totalUniformOffset;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   126
        regF = ctx->vs_reg_file_f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   127
        regI = ctx->vs_reg_file_i;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   128
        regB = ctx->vs_reg_file_b;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   129
    } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   130
    else
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   131
    {
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   132
        ctx->pixelUniformOffset = ctx->totalUniformOffset;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   133
        regF = ctx->ps_reg_file_f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   134
        regI = ctx->ps_reg_file_i;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   135
        regB = ctx->ps_reg_file_b;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   136
    } // else
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   137
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   138
    void *contents = msg(ctx->ubo, ctx->selContents) + ctx->totalUniformOffset;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   139
    int offset = 0;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   140
    for (int i = 0; i < shader->parseData->uniform_count; i++)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   141
    {
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   142
        if (shader->parseData->uniforms[i].constant)
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   143
            continue;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   144
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   145
        int idx = shader->parseData->uniforms[i].index;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   146
        int arrayCount = shader->parseData->uniforms[i].array_count;
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   147
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   148
        void *src = NULL;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   149
        int size = arrayCount ? arrayCount : 1;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   150
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   151
        switch (shader->parseData->uniforms[i].type)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   152
        {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   153
            case MOJOSHADER_UNIFORM_FLOAT:
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   154
                src = &regF[4 * idx];
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   155
                size *= 16;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   156
                break;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   157
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   158
            case MOJOSHADER_UNIFORM_INT:
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   159
                src = &regI[4 * idx];
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   160
                size *= 16;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   161
                break;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   162
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   163
            case MOJOSHADER_UNIFORM_BOOL:
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   164
                src = &regB[idx];
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   165
                break;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   166
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   167
            default:
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   168
                assert(0); // This should never happen.
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   169
                break;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   170
        } // switch
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   171
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   172
        memcpy(contents + offset, src, size);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   173
        offset += size;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   174
    } // for
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   175
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   176
    ctx->totalUniformOffset = next_highest_alignment(ctx->totalUniformOffset + offset);
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   177
    if (ctx->totalUniformOffset >= (int) msg(ctx->ubo, ctx->selLength))
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   178
    {
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   179
        // !!! FIXME: Is there a better way to handle this?
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   180
        assert(0 && "Uniform data exceeded the size of the buffer!");
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   181
    } // if
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   182
} // update_uniform_buffer
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   183
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   184
/* Public API */
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   185
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   186
MOJOSHADER_mtlContext *MOJOSHADER_mtlCreateContext(void* mtlDevice,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   187
                                    MOJOSHADER_malloc m, MOJOSHADER_free f,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   188
                                    void *malloc_d)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   189
{
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   190
    MOJOSHADER_mtlContext *retval = NULL;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   191
    MOJOSHADER_mtlContext *current_ctx = ctx;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   192
    int i;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   193
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   194
    ctx = NULL;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   195
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   196
    if (m == NULL) m = MOJOSHADER_internal_malloc;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   197
    if (f == NULL) f = MOJOSHADER_internal_free;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   198
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   199
    ctx = (MOJOSHADER_mtlContext *) m(sizeof (MOJOSHADER_mtlContext), malloc_d);
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   200
    if (ctx == NULL)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   201
    {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   202
        out_of_memory();
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   203
        goto init_fail;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   204
    } // if
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   205
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   206
    memset(ctx, '\0', sizeof (MOJOSHADER_mtlContext));
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   207
    ctx->malloc_fn = m;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   208
    ctx->free_fn = f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   209
    ctx->malloc_data = malloc_d;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   210
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   211
    // Initialize the Metal state
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   212
    ctx->device = mtlDevice;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   213
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   214
    // Grab references to Objective-C selectors
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   215
    ctx->classNSString = objc_getClass("NSString");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   216
    ctx->selAlloc = sel_registerName("alloc");
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   217
    ctx->selContents = sel_registerName("contents");
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   218
    ctx->selInitWithUTF8String = sel_registerName("initWithUTF8String:");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   219
    ctx->selLength = sel_registerName("length");
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   220
    ctx->selLocalizedDescription = sel_registerName("localizedDescription");
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   221
    ctx->selNewBufferWithLength = sel_registerName("newBufferWithLength:options:");
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   222
    ctx->selNewFunctionWithName = sel_registerName("newFunctionWithName:");
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   223
    ctx->selNewLibraryWithSource = sel_registerName("newLibraryWithSource:options:error:");
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   224
    ctx->selRelease = sel_registerName("release");
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   225
    ctx->selUTF8String = sel_registerName("UTF8String");
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   226
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   227
    // Create the uniform buffer
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   228
    ctx->ubo = msg_uu(mtlDevice, ctx->selNewBufferWithLength,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   229
                      next_highest_alignment(1000000), 0);
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   230
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   231
    retval = ctx;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   232
    ctx = current_ctx;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   233
    return retval;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   234
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   235
init_fail:
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   236
    if (ctx != NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   237
        f(ctx, malloc_d);
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   238
    ctx = current_ctx;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   239
    return NULL;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   240
} // MOJOSHADER_mtlCreateContext
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   241
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   242
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   243
void MOJOSHADER_mtlMakeContextCurrent(MOJOSHADER_mtlContext *_ctx)
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   244
{
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   245
    ctx = _ctx;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   246
} // MOJOSHADER_mtlMakeContextCurrent
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   247
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   248
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   249
void *MOJOSHADER_mtlCompileLibrary(MOJOSHADER_effect *effect)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   250
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   251
    MOJOSHADER_malloc m = ctx->malloc_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   252
    MOJOSHADER_free f = ctx->free_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   253
    void *d = ctx->malloc_data;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   254
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   255
    int i, src_len, src_pos, output_len;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   256
    char *shader_source, *ptr;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   257
    const char *repl;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   258
    MOJOSHADER_effectObject *object;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   259
    MOJOSHADER_mtlShader *shader;
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   260
    void *retval, *compileError, *shader_source_ns, *fnname;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   261
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   262
    // Count the number of shaders before allocating
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   263
    src_len = 0;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   264
    for (i = 0; i < effect->object_count; i++)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   265
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   266
        object = &effect->objects[i];
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   267
        if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   268
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   269
        {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   270
            if (!object->shader.is_preshader)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   271
            {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   272
                shader = (MOJOSHADER_mtlShader*) object->shader.shader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   273
                src_len += shader->parseData->output_len;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   274
            } // if
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   275
        } // if
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   276
    } // for
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   277
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   278
    // Allocate shader source buffer
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   279
    shader_source = (char *) m(src_len + 1, d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   280
    memset(shader_source, '\0', src_len + 1);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   281
    src_pos = 0;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   282
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   283
    // Copy all the source text into the buffer
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   284
    for (i = 0; i < effect->object_count; i++)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   285
    {
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   286
        object = &effect->objects[i];
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   287
        if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   288
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   289
        {
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   290
            if (!object->shader.is_preshader)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   291
            {
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   292
                shader = (MOJOSHADER_mtlShader*) object->shader.shader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   293
                memcpy(&shader_source[src_pos], shader->parseData->output,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   294
                                                shader->parseData->output_len);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   295
                src_pos += shader->parseData->output_len;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   296
            } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   297
        } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   298
    } // for
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   299
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   300
    // Handle texcoord0 -> point_coord conversion
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   301
    if (strstr(shader_source, "[[point_size]]"))
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   302
    {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   303
        // !!! FIXME: This assumes all texcoord0 attributes in the effect are
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   304
        // !!! FIXME:  actually point coords! It ain't necessarily so! -caleb
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   305
        repl = "[[  point_coord  ]]";
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   306
        while ((ptr = strstr(shader_source, "[[user(texcoord0)]]")))
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   307
        {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   308
            memcpy(ptr, repl, strlen(repl));
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   309
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   310
            // "float4" -> "float2"
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   311
            int spaces = 0;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   312
            while (spaces < 2)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   313
                if (*(ptr--) == ' ')
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   314
                    spaces++;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   315
            memcpy(ptr, "2", sizeof(char));
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   316
        } // while
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   317
    } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   318
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   319
    // Compile the source into a library
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   320
    compileError = NULL;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   321
    shader_source_ns = msg_s(
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   322
        msg(ctx->classNSString, ctx->selAlloc),
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   323
        ctx->selInitWithUTF8String,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   324
        shader_source
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   325
    );
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   326
    retval = msg_ppp(ctx->device, ctx->selNewLibraryWithSource,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   327
                        shader_source_ns, NULL, &compileError);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   328
    f(shader_source, d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   329
    msg(shader_source_ns, ctx->selRelease);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   330
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   331
    if (retval == NULL)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   332
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   333
        compileError = msg(compileError, ctx->selLocalizedDescription);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   334
        set_error((char*) msg(compileError, ctx->selUTF8String));
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   335
        return NULL;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   336
    } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   337
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   338
    // Run through the shaders again, getting the function handles
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   339
    for (i = 0; i < effect->object_count; i++)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   340
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   341
        object = &effect->objects[i];
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   342
        if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   343
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   344
        {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   345
            if (object->shader.is_preshader)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   346
                continue;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   347
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   348
            shader = (MOJOSHADER_mtlShader*) object->shader.shader;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   349
            fnname = msg_s(
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   350
                msg(ctx->classNSString, ctx->selAlloc),
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   351
                ctx->selInitWithUTF8String,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   352
                shader->parseData->mainfn
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   353
            );
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   354
            shader->handle = msg_p(
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   355
                retval,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   356
                ctx->selNewFunctionWithName,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   357
                fnname
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   358
            );
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   359
            msg(fnname, ctx->selRelease);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   360
        } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   361
    } // for
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   362
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   363
    return retval;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   364
} // MOJOSHADER_mtlCompileLibrary
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   365
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   366
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   367
MOJOSHADER_mtlShader *MOJOSHADER_mtlCompileShader(const char *mainfn,
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   368
                                            const unsigned char *tokenbuf,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   369
                                            const unsigned int bufsize,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   370
                                            const MOJOSHADER_swizzle *swiz,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   371
                                            const unsigned int swizcount,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   372
                                            const MOJOSHADER_samplerMap *smap,
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   373
                                            const unsigned int smapcount)
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   374
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   375
    MOJOSHADER_malloc m = ctx->malloc_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   376
    MOJOSHADER_free f = ctx->free_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   377
    void *d = ctx->malloc_data;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   378
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   379
    const MOJOSHADER_parseData *pd = MOJOSHADER_parse("metal", mainfn, tokenbuf,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   380
                                                     bufsize, swiz, swizcount,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   381
                                                     smap, smapcount, m, f, d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   382
    if (pd->error_count > 0)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   383
    {
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   384
        // !!! FIXME: put multiple errors in the buffer? Don't use
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   385
        // !!! FIXME:  MOJOSHADER_mtlGetError() for this?
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   386
        set_error(pd->errors[0].error);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   387
        goto compile_shader_fail;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   388
    } // if
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   389
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   390
    MOJOSHADER_mtlShader *retval = (MOJOSHADER_mtlShader *) m(sizeof(MOJOSHADER_mtlShader), d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   391
    if (retval == NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   392
        goto compile_shader_fail;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   393
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   394
    retval->parseData = pd;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   395
    retval->refcount = 1;
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   396
    retval->handle = NULL; // populated by MOJOSHADER_mtlCompileLibrary
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   397
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   398
    return retval;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   399
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   400
compile_shader_fail:
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   401
    MOJOSHADER_freeParseData(retval->parseData);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   402
    f(retval, d);
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   403
    return NULL;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   404
} // MOJOSHADER_mtlCompileShader
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   405
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   406
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   407
void MOJOSHADER_mtlShaderAddRef(MOJOSHADER_mtlShader *shader)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   408
{
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   409
    if (shader != NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   410
        shader->refcount++;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   411
} // MOJOSHADER_mtlShaderAddRef
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   412
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   413
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   414
const MOJOSHADER_parseData *MOJOSHADER_mtlGetShaderParseData(
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   415
                                                MOJOSHADER_mtlShader *shader)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   416
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   417
    return (shader != NULL) ? shader->parseData : NULL;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   418
} // MOJOSHADER_mtlGetParseData
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   419
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   420
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   421
void MOJOSHADER_mtlBindShaders(MOJOSHADER_mtlShader *vshader,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   422
                               MOJOSHADER_mtlShader *pshader)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   423
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   424
    // Use the last bound shaders in case of NULL
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   425
    if (vshader != NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   426
        ctx->vertexShader = vshader;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   427
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   428
    if (pshader != NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   429
        ctx->pixelShader = pshader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   430
} // MOJOSHADER_mtlBindShaders
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   431
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   432
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   433
void MOJOSHADER_mtlGetBoundShaders(MOJOSHADER_mtlShader **vshader,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   434
                                   MOJOSHADER_mtlShader **pshader)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   435
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   436
    *vshader = ctx->vertexShader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   437
    *pshader = ctx->pixelShader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   438
} // MOJOSHADER_mtlGetBoundShaders
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   439
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   440
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   441
void MOJOSHADER_mtlMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   442
                                          float **psf, int **psi, unsigned char **psb)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   443
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   444
    *vsf = ctx->vs_reg_file_f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   445
    *vsi = ctx->vs_reg_file_i;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   446
    *vsb = ctx->vs_reg_file_b;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   447
    *psf = ctx->ps_reg_file_f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   448
    *psi = ctx->ps_reg_file_i;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   449
    *psb = ctx->ps_reg_file_b;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   450
} // MOJOSHADER_mtlMapUniformBufferMemory
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   451
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   452
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   453
void MOJOSHADER_mtlUnmapUniformBufferMemory()
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   454
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   455
    /* This has nothing to do with unmapping memory
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   456
     * and everything to do with updating uniform
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   457
     * buffers with the latest parameter contents.
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   458
     */
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   459
    update_uniform_buffer(ctx->vertexShader);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   460
    update_uniform_buffer(ctx->pixelShader);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   461
} // MOJOSHADER_mtlUnmapUniformBufferMemory
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   462
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   463
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   464
void MOJOSHADER_mtlGetUniformData(void **buf, int *voff, int *poff)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   465
{
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   466
    *buf = ctx->ubo;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   467
    *voff = ctx->vertexUniformOffset;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   468
    *poff = ctx->pixelUniformOffset;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   469
} // MOJOSHADER_mtlGetUniformBuffers
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   470
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   471
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   472
void *MOJOSHADER_mtlGetFunctionHandle(MOJOSHADER_mtlShader *shader)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   473
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   474
    if (shader == NULL)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   475
        return NULL;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   476
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   477
    return shader->handle;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   478
} // MOJOSHADER_mtlGetFunctionHandle
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   479
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   480
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   481
void MOJOSHADER_mtlEndFrame()
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   482
{
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   483
    ctx->totalUniformOffset = 0;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   484
    ctx->vertexUniformOffset = 0;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   485
    ctx->pixelUniformOffset = 0;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   486
} // MOJOSHADER_mtlEndFrame
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   487
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   488
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   489
int MOJOSHADER_mtlGetVertexAttribLocation(MOJOSHADER_mtlShader *vert,
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   490
                                          MOJOSHADER_usage usage, int index)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   491
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   492
    if (vert == NULL)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   493
        return -1;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   494
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   495
    for (int i = 0; i < vert->parseData->attribute_count; i++)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   496
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   497
        if (vert->parseData->attributes[i].usage == usage &&
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   498
            vert->parseData->attributes[i].index == index)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   499
        {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   500
            return i;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   501
        } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   502
    } // for
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   503
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   504
    // failure, couldn't find requested attribute
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   505
    return -1;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   506
} // MOJOSHADER_mtlGetVertexAttribLocation
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   507
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   508
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   509
const char *MOJOSHADER_mtlGetError(void)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   510
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   511
    return error_buffer;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   512
} // MOJOSHADER_mtlGetError
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   513
1279
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   514
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   515
void MOJOSHADER_mtlDeleteLibrary(void *library)
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   516
{
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   517
    msg(library, ctx->selRelease);
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   518
} // MOJOSHADER_mtlDeleteLibrary
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   519
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   520
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   521
void MOJOSHADER_mtlDeleteShader(MOJOSHADER_mtlShader *shader)
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   522
{
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   523
    if (shader != NULL)
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   524
    {
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   525
        if (shader->refcount > 1)
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   526
            shader->refcount--;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   527
        else
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   528
        {
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   529
            msg(shader->handle, ctx->selRelease);
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   530
            MOJOSHADER_freeParseData(shader->parseData);
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   531
            ctx->free_fn(shader, ctx->malloc_data);
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   532
        } // else
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   533
    } // if
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   534
} // MOJOSHADER_mtlDeleteShader
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   535
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   536
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   537
void MOJOSHADER_mtlDestroyContext(MOJOSHADER_mtlContext *_ctx)
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   538
{
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   539
    MOJOSHADER_mtlContext *current_ctx = ctx;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   540
    ctx = _ctx;
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   541
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   542
    if (ctx->ubo != NULL)
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   543
        msg(ctx->ubo, ctx->selRelease);
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   544
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   545
    if (ctx != NULL)
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   546
        ctx->free_fn(ctx, ctx->malloc_data);
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   547
    ctx = ((current_ctx == _ctx) ? NULL : current_ctx);
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   548
} // MOJOSHADER_mtlDestroyContext
b1f51cfa8933 Metal uniform buffer overhaul.
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1252
diff changeset
   549
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   550
#endif /* MOJOSHADER_EFFECT_SUPPORT */
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   551
#endif /* SUPPORT_PROFILE_METAL && PLATFORM_APPLE */
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   552
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   553
// end of mojoshader_metal.c ...