mojoshader_metal.c
author Ethan Lee <flibitijibibo@flibitijibibo.com>
Fri, 24 Apr 2020 09:13:10 -0400
changeset 1245 ad9a16c8b023
parent 1241 01fea3855ffb
child 1250 30f8b128b906
permissions -rw-r--r--
Major refactor of the Effects API. The previous API was very GL-specific and had no abstraction at all, meaning new backends had to copypaste WAY too much code just to get started. Additionally, parsing/compiled were separate but still very tightly coupled, to the point where memory leaks in the GL implementation were inevitable. TL;DR: It was crap and nobody liked working with it. The new API provides only a compile function, and moves ALL the effect work to a common implementation. This is done by providing a shader context at compile time, with functions that map almost directly to the existing MojoShader GL API. This is MUCH less work to maintain and add to, to the point where you can see how a parse API is still possible in the updated testparse! It also means new shader backends no longer need to worry about the effects framework so much. The Metal work in this patch was done by Caleb Cornett, as always.
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
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    10
#if (defined(__APPLE__) && defined(__MACH__))
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    11
#define PLATFORM_APPLE 1
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    12
#include "TargetConditionals.h"
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    13
#include <objc/message.h>
1241
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
    14
#define msg     ((void* (*)(void*, void*))objc_msgSend)
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
    15
#define msg_s   ((void* (*)(void*, void*, const char*))objc_msgSend)
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
    16
#define msg_p   ((void* (*)(void*, void*, void*))objc_msgSend)
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
    17
#define msg_ip  ((void* (*)(void*, void*, int, void*))objc_msgSend)
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
    18
#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
    19
#endif /* (defined(__APPLE__) && defined(__MACH__)) */
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    20
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    21
#define __MOJOSHADER_INTERNAL__ 1
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    22
#include "mojoshader_internal.h"
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    23
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    24
typedef struct MOJOSHADER_mtlUniformBuffer MOJOSHADER_mtlUniformBuffer;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    25
typedef struct MOJOSHADER_mtlShader
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    26
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    27
    const MOJOSHADER_parseData *parseData;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    28
    MOJOSHADER_mtlUniformBuffer *ubo;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    29
    uint32 refcount;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    30
    void *library; // MTLLibrary*
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    31
} MOJOSHADER_mtlShader;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    32
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    33
// Error state...
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    34
static char error_buffer[1024] = { '\0' };
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    35
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    36
static void set_error(const char *str)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    37
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    38
    snprintf(error_buffer, sizeof (error_buffer), "%s", str);
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    39
} // set_error
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    40
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    41
static inline void out_of_memory(void)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    42
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    43
    set_error("out of memory");
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    44
} // out_of_memory
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    45
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    46
// profile-specific implementations...
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    47
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    48
#if SUPPORT_PROFILE_METAL && PLATFORM_APPLE
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    49
#ifdef MOJOSHADER_EFFECT_SUPPORT
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    50
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    51
/* Structs */
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    52
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    53
typedef struct MOJOSHADER_mtlUniformBuffer
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    54
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    55
    int bufferSize;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    56
    void **internalBuffers; // MTLBuffer*
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    57
    int internalBufferSize;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    58
    int internalOffset;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    59
    int currentFrame;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    60
    int inUse;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    61
} MOJOSHADER_mtlUniformBuffer;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    62
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    63
// Max entries for each register file type...
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    64
#define MAX_REG_FILE_F 8192
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    65
#define MAX_REG_FILE_I 2047
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    66
#define MAX_REG_FILE_B 2047
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    67
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    68
typedef struct MOJOSHADER_mtlContext
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    69
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    70
    // Allocators...
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    71
    MOJOSHADER_malloc malloc_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    72
    MOJOSHADER_free free_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    73
    void *malloc_data;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    74
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    75
    // The constant register files...
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    76
    // !!! 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
    77
    // !!! FIXME:  ... make this dynamically allocated on demand.
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    78
    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
    79
    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
    80
    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
    81
    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
    82
    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
    83
    uint8 ps_reg_file_b[MAX_REG_FILE_B];
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    84
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    85
    // Pointer to the active MTLDevice.
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    86
    void* device;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    87
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    88
    // The maximum number of frames in flight.
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    89
    int framesInFlight;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    90
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    91
    // Array of UBOs that are being used in the current frame.
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    92
    MOJOSHADER_mtlUniformBuffer **buffersInUse;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    93
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    94
    // The current capacity of the uniform buffer array.
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    95
    int bufferArrayCapacity;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    96
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    97
    // The actual number of UBOs used in the current frame.
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    98
    int numBuffersInUse;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
    99
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   100
    // The currently bound shaders.
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   101
    MOJOSHADER_mtlShader *vertexShader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   102
    MOJOSHADER_mtlShader *pixelShader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   103
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   104
    // Objective-C Selectors
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   105
    void* classNSString;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   106
    void* selAlloc;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   107
    void* selInitWithUTF8String;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   108
    void* selUTF8String;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   109
    void* selLength;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   110
    void* selContents;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   111
    void* selNewBufferWithLength;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   112
    void* selRelease;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   113
    void* selNewLibraryWithSource;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   114
    void* selLocalizedDescription;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   115
    void* selNewFunctionWithName;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   116
    void* selRetain;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   117
} MOJOSHADER_mtlContext;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   118
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   119
static MOJOSHADER_mtlContext *ctx = NULL;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   120
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   121
/* Uniform buffer utilities */
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   122
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   123
static inline int next_highest_alignment(int n)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   124
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   125
    #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_SIMULATOR
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   126
    int align = 16;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   127
    #else
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   128
    int align = 256;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   129
    #endif
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   130
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   131
    return align * ((n + align - 1) / align);
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   132
} // next_highest_alignment
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   133
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   134
static void* create_ubo_backing_buffer(MOJOSHADER_mtlUniformBuffer *ubo,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   135
                                                               int frame)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   136
{
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   137
    void *oldBuffer = ubo->internalBuffers[frame];
1241
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
   138
    void *newBuffer = msg_ip(
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   139
        ctx->device,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   140
        ctx->selNewBufferWithLength,
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   141
        ubo->internalBufferSize,
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   142
        NULL
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   143
    );
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   144
    if (oldBuffer != NULL)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   145
    {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   146
        // Copy over data from old buffer
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   147
        memcpy(
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   148
            msg(newBuffer, ctx->selContents),
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   149
            msg(oldBuffer, ctx->selContents),
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   150
            (int) msg(oldBuffer, ctx->selLength)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   151
        );
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
        // Free the old buffer
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   154
        msg(oldBuffer, ctx->selRelease);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   155
    } //if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   156
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   157
    return newBuffer;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   158
} // create_ubo_backing_buffer
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   159
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   160
static void predraw_ubo(MOJOSHADER_mtlUniformBuffer *ubo)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   161
{
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   162
    if (!ubo->inUse)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   163
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   164
        ubo->inUse = 1;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   165
        ctx->buffersInUse[ctx->numBuffersInUse++] = ubo;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   166
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   167
        // Double the array size if we run out of room
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   168
        if (ctx->numBuffersInUse >= ctx->bufferArrayCapacity)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   169
        {
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   170
            int oldlen = ctx->bufferArrayCapacity;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   171
            ctx->bufferArrayCapacity *= 2;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   172
            MOJOSHADER_mtlUniformBuffer **tmp;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   173
            tmp = (MOJOSHADER_mtlUniformBuffer**) ctx->malloc_fn(
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   174
                ctx->bufferArrayCapacity * sizeof(MOJOSHADER_mtlUniformBuffer *),
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   175
                ctx->malloc_data
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   176
            );
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   177
            memcpy(tmp, ctx->buffersInUse, oldlen * sizeof(MOJOSHADER_mtlUniformBuffer *));
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   178
            ctx->free_fn(ctx->buffersInUse, ctx->malloc_data);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   179
            ctx->buffersInUse = tmp;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   180
        }
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   181
        return;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   182
    } // if
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
    ubo->internalOffset += ubo->bufferSize;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   185
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   186
    int buflen = (int) msg(
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   187
        ubo->internalBuffers[ubo->currentFrame],
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   188
        ctx->selLength
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   189
    );
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   190
    if (ubo->internalOffset >= buflen)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   191
    {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   192
        // Double capacity when we're out of room
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   193
        if (ubo->internalOffset >= ubo->internalBufferSize)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   194
            ubo->internalBufferSize *= 2;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   195
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   196
        ubo->internalBuffers[ubo->currentFrame] =
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   197
            create_ubo_backing_buffer(ubo, ubo->currentFrame);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   198
    } //if
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   199
} // predraw_ubo
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   200
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   201
static MOJOSHADER_mtlUniformBuffer* create_ubo(MOJOSHADER_mtlShader *shader,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   202
                                               MOJOSHADER_malloc m, void* d)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   203
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   204
    int uniformCount = shader->parseData->uniform_count;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   205
    if (uniformCount == 0)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   206
        return NULL;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   207
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   208
    // Calculate how big we need to make the buffer
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   209
    int buflen = 0;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   210
    for (int i = 0; i < uniformCount; i += 1)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   211
    {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   212
        int arrayCount = shader->parseData->uniforms[i].array_count;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   213
        int uniformSize = 16;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   214
        if (shader->parseData->uniforms[i].type == MOJOSHADER_UNIFORM_BOOL)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   215
            uniformSize = 1;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   216
        buflen += (arrayCount ? arrayCount : 1) * uniformSize;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   217
    } // for
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   218
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   219
    // Allocate the UBO
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   220
    MOJOSHADER_mtlUniformBuffer *retval;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   221
    retval = (MOJOSHADER_mtlUniformBuffer *) m(sizeof(MOJOSHADER_mtlUniformBuffer), d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   222
    retval->bufferSize = next_highest_alignment(buflen);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   223
    retval->internalBufferSize = retval->bufferSize * 16; // pre-allocate some extra room!
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   224
    retval->internalBuffers = m(ctx->framesInFlight * sizeof(void*), d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   225
    retval->internalOffset = 0;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   226
    retval->inUse = 0;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   227
    retval->currentFrame = 0;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   228
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   229
    // Create the backing buffers
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   230
    for (int i = 0; i < ctx->framesInFlight; i++)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   231
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   232
        retval->internalBuffers[i] = NULL; // basically a memset('\0')
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   233
        retval->internalBuffers[i] = create_ubo_backing_buffer(retval, i);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   234
    } // for
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   235
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   236
    return retval;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   237
} // create_ubo
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   238
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   239
static void dealloc_ubo(MOJOSHADER_mtlShader *shader,
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   240
                        MOJOSHADER_free f,
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   241
                        void* d)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   242
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   243
    if (shader->ubo == NULL)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   244
        return;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   245
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   246
    for (int i = 0; i < ctx->framesInFlight; i++)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   247
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   248
        msg(shader->ubo->internalBuffers[i], ctx->selRelease);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   249
        shader->ubo->internalBuffers[i] = NULL;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   250
    } // for
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   251
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   252
    f(shader->ubo->internalBuffers, d);
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   253
    f(shader->ubo, d);
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   254
} // dealloc_ubo
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   255
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   256
static void *get_uniform_buffer(MOJOSHADER_mtlShader *shader)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   257
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   258
    if (shader == NULL || shader->ubo == NULL)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   259
        return NULL;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   260
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   261
    return shader->ubo->internalBuffers[shader->ubo->currentFrame];
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   262
} // get_uniform_buffer
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   263
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   264
static int get_uniform_offset(MOJOSHADER_mtlShader *shader)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   265
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   266
    if (shader == NULL || shader->ubo == NULL)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   267
        return 0;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   268
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   269
    return shader->ubo->internalOffset;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   270
} // get_uniform_offset
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   271
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   272
static void update_uniform_buffer(MOJOSHADER_mtlShader *shader)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   273
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   274
    if (shader == NULL || shader->ubo == NULL)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   275
        return;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   276
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   277
    float *regF; int *regI; uint8 *regB;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   278
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   279
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   280
        regF = ctx->vs_reg_file_f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   281
        regI = ctx->vs_reg_file_i;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   282
        regB = ctx->vs_reg_file_b;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   283
    } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   284
    else
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   285
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   286
        regF = ctx->ps_reg_file_f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   287
        regI = ctx->ps_reg_file_i;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   288
        regB = ctx->ps_reg_file_b;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   289
    } // else
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   290
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   291
    predraw_ubo(shader->ubo);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   292
    void *buf = shader->ubo->internalBuffers[shader->ubo->currentFrame];
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   293
    void *contents = msg(buf, ctx->selContents) + shader->ubo->internalOffset;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   294
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   295
    int offset = 0;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   296
    for (int i = 0; i < shader->parseData->uniform_count; i++)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   297
    {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   298
        int idx = shader->parseData->uniforms[i].index;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   299
        int arrayCount = shader->parseData->uniforms[i].array_count;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   300
        int size = arrayCount ? arrayCount : 1;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   301
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   302
        switch (shader->parseData->uniforms[i].type)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   303
        {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   304
            case MOJOSHADER_UNIFORM_FLOAT:
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   305
                memcpy(
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   306
                    contents + (offset * 16),
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   307
                    &regF[4 * idx],
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   308
                    size * 16
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   309
                );
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   310
                break;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   311
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   312
            case MOJOSHADER_UNIFORM_INT:
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   313
                // !!! FIXME: Need a test case
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   314
                memcpy(
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   315
                    contents + (offset * 16),
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   316
                    &regI[4 * idx],
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   317
                    size * 16
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   318
                );
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   319
                break;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   320
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   321
            case MOJOSHADER_UNIFORM_BOOL:
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   322
                // !!! FIXME: Need a test case
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   323
                memcpy(
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   324
                    contents + offset,
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   325
                    &regB[idx],
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   326
                    size
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   327
                );
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   328
                break;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   329
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   330
            default:
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   331
                assert(0); // This should never happen.
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   332
                break;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   333
        } // switch
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   334
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   335
        offset += size;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   336
    } // for
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   337
} // update_uniform_buffer
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   338
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   339
/* Public API */
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
int MOJOSHADER_mtlCreateContext(void* mtlDevice, int framesInFlight,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   342
                                MOJOSHADER_malloc m, MOJOSHADER_free f,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   343
                                void *malloc_d)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   344
{
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   345
    assert(ctx == NULL);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   346
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   347
    if (m == NULL) m = MOJOSHADER_internal_malloc;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   348
    if (f == NULL) f = MOJOSHADER_internal_free;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   349
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   350
    ctx = (MOJOSHADER_mtlContext *) m(sizeof(MOJOSHADER_mtlContext), malloc_d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   351
    if (ctx == NULL)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   352
    {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   353
        out_of_memory();
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   354
        goto init_fail;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   355
    } // if
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   356
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   357
    memset(ctx, '\0', sizeof (MOJOSHADER_mtlContext));
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   358
    ctx->malloc_fn = m;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   359
    ctx->free_fn = f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   360
    ctx->malloc_data = malloc_d;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   361
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   362
    // Initialize the Metal state
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   363
    ctx->device = mtlDevice;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   364
    ctx->framesInFlight = framesInFlight;
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
    // Allocate the uniform buffer object array
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   367
    ctx->bufferArrayCapacity = 32; // arbitrary!
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   368
    ctx->buffersInUse = ctx->malloc_fn(
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   369
        ctx->bufferArrayCapacity * sizeof(MOJOSHADER_mtlUniformBuffer *),
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   370
        ctx->malloc_data
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   371
    );
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   372
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   373
    // Grab references to Objective-C selectors
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   374
    ctx->classNSString = objc_getClass("NSString");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   375
    ctx->selAlloc = sel_registerName("alloc");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   376
    ctx->selInitWithUTF8String = sel_registerName("initWithUTF8String:");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   377
    ctx->selUTF8String = sel_registerName("UTF8String");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   378
    ctx->selLength = sel_registerName("length");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   379
    ctx->selContents = sel_registerName("contents");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   380
    ctx->selNewBufferWithLength = sel_registerName("newBufferWithLength:options:");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   381
    ctx->selRelease = sel_registerName("release");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   382
    ctx->selNewLibraryWithSource = sel_registerName("newLibraryWithSource:options:error:");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   383
    ctx->selLocalizedDescription = sel_registerName("localizedDescription");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   384
    ctx->selNewFunctionWithName = sel_registerName("newFunctionWithName:");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   385
    ctx->selRetain = sel_registerName("retain");
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   386
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   387
    return 0;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   388
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   389
init_fail:
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   390
    if (ctx != NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   391
        f(ctx, malloc_d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   392
    return -1;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   393
} // MOJOSHADER_mtlCreateContext
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   394
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   395
void MOJOSHADER_mtlDestroyContext(void)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   396
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   397
    ctx->free_fn(ctx->buffersInUse, ctx->malloc_data);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   398
    ctx->free_fn(ctx, ctx->malloc_data);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   399
} // MOJOSHADER_mtlDestroyContext
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   400
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   401
void *MOJOSHADER_mtlCompileLibrary(MOJOSHADER_effect *effect)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   402
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   403
    MOJOSHADER_malloc m = ctx->malloc_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   404
    MOJOSHADER_free f = ctx->free_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   405
    void *d = ctx->malloc_data;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   406
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   407
    int i, src_len, src_pos, output_len;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   408
    char *shader_source, *ptr;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   409
    const char *repl;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   410
    MOJOSHADER_effectObject *object;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   411
    MOJOSHADER_mtlShader *shader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   412
    void *retval, *compileError, *shader_source_ns;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   413
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   414
    // Count the number of shaders before allocating
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   415
    src_len = 0;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   416
    for (i = 0; i < effect->object_count; i++)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   417
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   418
        object = &effect->objects[i];
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   419
        if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   420
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   421
        {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   422
            if (!object->shader.is_preshader)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   423
            {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   424
                shader = (MOJOSHADER_mtlShader*) object->shader.shader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   425
                src_len += shader->parseData->output_len;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   426
            } // if
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   427
        } // if
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   428
    } // for
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   429
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   430
    // Allocate shader source buffer
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   431
    shader_source = (char *) m(src_len + 1, d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   432
    memset(shader_source, '\0', src_len + 1);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   433
    src_pos = 0;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   434
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   435
    // Copy all the source text into the buffer
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   436
    for (i = 0; i < effect->object_count; i++)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   437
    {
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   438
        object = &effect->objects[i];
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   439
        if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   440
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   441
        {
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   442
            if (!object->shader.is_preshader)
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
                shader = (MOJOSHADER_mtlShader*) object->shader.shader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   445
                memcpy(&shader_source[src_pos], shader->parseData->output,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   446
                                                shader->parseData->output_len);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   447
                src_pos += shader->parseData->output_len;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   448
            } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   449
        } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   450
    } // for
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   451
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   452
    // Handle texcoord0 -> point_coord conversion
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   453
    if (strstr(shader_source, "[[point_size]]"))
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   454
    {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   455
        // !!! FIXME: This assumes all texcoord0 attributes in the effect are
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   456
        // !!! 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
   457
        repl = "[[  point_coord  ]]";
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   458
        while ((ptr = strstr(shader_source, "[[user(texcoord0)]]")))
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   459
        {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   460
            memcpy(ptr, repl, strlen(repl));
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   461
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   462
            // "float4" -> "float2"
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   463
            int spaces = 0;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   464
            while (spaces < 2)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   465
                if (*(ptr--) == ' ')
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   466
                    spaces++;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   467
            memcpy(ptr, "2", sizeof(char));
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   468
        } // while
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   469
    } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   470
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   471
    // Compile the source into a library
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   472
    compileError = NULL;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   473
    shader_source_ns = msg_s(
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   474
        msg(ctx->classNSString, ctx->selAlloc),
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   475
        ctx->selInitWithUTF8String,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   476
        shader_source
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   477
    );
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   478
    retval = msg_ppp(ctx->device, ctx->selNewLibraryWithSource,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   479
                        shader_source_ns, NULL, &compileError);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   480
    f(shader_source, d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   481
    msg(shader_source_ns, ctx->selRelease);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   482
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   483
    if (retval == NULL)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   484
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   485
        compileError = msg(compileError, ctx->selLocalizedDescription);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   486
        set_error((char*) msg(compileError, ctx->selUTF8String));
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   487
        return NULL;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   488
    } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   489
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   490
    // Run through the shaders again, setting the library reference
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   491
    for (i = 0; i < effect->object_count; i++)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   492
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   493
        object = &effect->objects[i];
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   494
        if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   495
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   496
        {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   497
            if (object->shader.is_preshader)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   498
                continue;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   499
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   500
            ((MOJOSHADER_mtlShader*) object->shader.shader)->library = retval;
1229
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
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   504
    return retval;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   505
} // MOJOSHADER_mtlCompileLibrary
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   506
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   507
void MOJOSHADER_mtlDeleteLibrary(void *library)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   508
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   509
    msg(library, ctx->selRelease);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   510
} // MOJOSHADER_mtlDeleteLibrary
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   511
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   512
MOJOSHADER_mtlShader *MOJOSHADER_mtlCompileShader(const char *mainfn,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   513
                                                  const unsigned char *tokenbuf,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   514
                                                  const unsigned int bufsize,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   515
                                                  const MOJOSHADER_swizzle *swiz,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   516
                                                  const unsigned int swizcount,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   517
                                                  const MOJOSHADER_samplerMap *smap,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   518
                                                  const unsigned int smapcount)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   519
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   520
    MOJOSHADER_malloc m = ctx->malloc_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   521
    MOJOSHADER_free f = ctx->free_fn;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   522
    void *d = ctx->malloc_data;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   523
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   524
    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
   525
                                                     bufsize, swiz, swizcount,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   526
                                                     smap, smapcount, m, f, d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   527
    if (pd->error_count > 0)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   528
    {
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   529
        // !!! 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
   530
        // !!! FIXME:  MOJOSHADER_mtlGetError() for this?
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   531
        set_error(pd->errors[0].error);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   532
        goto compile_shader_fail;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   533
    } // if
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   534
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   535
    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
   536
    if (retval == NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   537
        goto compile_shader_fail;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   538
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   539
    retval->parseData = pd;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   540
    retval->refcount = 1;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   541
    retval->ubo = create_ubo(retval, m, d);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   542
    retval->library = NULL; // populated by MOJOSHADER_mtlCompileLibrary
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   543
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   544
    return retval;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   545
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   546
compile_shader_fail:
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   547
    MOJOSHADER_freeParseData(retval->parseData);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   548
    f(retval, d);
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   549
    return NULL;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   550
} // MOJOSHADER_mtlCompileShader
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   551
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   552
void MOJOSHADER_mtlShaderAddRef(MOJOSHADER_mtlShader *shader)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   553
{
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   554
    if (shader != NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   555
        shader->refcount++;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   556
} // MOJOSHADER_mtlShaderAddRef
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   557
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   558
void MOJOSHADER_mtlDeleteShader(MOJOSHADER_mtlShader *shader)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   559
{
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   560
    if (shader != NULL)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   561
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   562
        if (shader->refcount > 1)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   563
            shader->refcount--;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   564
        else
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   565
        {
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   566
            dealloc_ubo(shader, ctx->free_fn, ctx->malloc_data);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   567
            ctx->free_fn(shader, ctx->malloc_data);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   568
        } // else
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   569
    } // if
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   570
} // MOJOSHADER_mtlDeleteShader
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   571
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   572
const MOJOSHADER_parseData *MOJOSHADER_mtlGetShaderParseData(
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   573
                                                MOJOSHADER_mtlShader *shader)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   574
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   575
    return (shader != NULL) ? shader->parseData : NULL;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   576
} // MOJOSHADER_mtlGetParseData
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   577
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   578
void MOJOSHADER_mtlBindShaders(MOJOSHADER_mtlShader *vshader,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   579
                               MOJOSHADER_mtlShader *pshader)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   580
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   581
    // 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
   582
    if (vshader != NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   583
        ctx->vertexShader = vshader;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   584
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   585
    if (pshader != NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   586
        ctx->pixelShader = pshader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   587
} // MOJOSHADER_mtlBindShaders
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   588
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   589
void MOJOSHADER_mtlGetBoundShaders(MOJOSHADER_mtlShader **vshader,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   590
                                   MOJOSHADER_mtlShader **pshader)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   591
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   592
    *vshader = ctx->vertexShader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   593
    *pshader = ctx->pixelShader;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   594
} // MOJOSHADER_mtlGetBoundShaders
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   595
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   596
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
   597
                                          float **psf, int **psi, unsigned char **psb)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   598
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   599
    *vsf = ctx->vs_reg_file_f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   600
    *vsi = ctx->vs_reg_file_i;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   601
    *vsb = ctx->vs_reg_file_b;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   602
    *psf = ctx->ps_reg_file_f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   603
    *psi = ctx->ps_reg_file_i;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   604
    *psb = ctx->ps_reg_file_b;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   605
} // MOJOSHADER_mtlMapUniformBufferMemory
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   606
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   607
void MOJOSHADER_mtlUnmapUniformBufferMemory()
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   608
{
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   609
    /* This has nothing to do with unmapping memory
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   610
     * and everything to do with updating uniform
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   611
     * buffers with the latest parameter contents.
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   612
     */
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   613
    update_uniform_buffer(ctx->vertexShader);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   614
    update_uniform_buffer(ctx->pixelShader);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   615
} // MOJOSHADER_mtlUnmapUniformBufferMemory
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   616
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   617
void MOJOSHADER_mtlGetUniformBuffers(void **vbuf, int *voff,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   618
                                     void **pbuf, int *poff)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   619
{
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   620
    *vbuf = get_uniform_buffer(ctx->vertexShader);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   621
    *voff = get_uniform_offset(ctx->vertexShader);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   622
    *pbuf = get_uniform_buffer(ctx->pixelShader);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   623
    *poff = get_uniform_offset(ctx->pixelShader);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   624
} // MOJOSHADER_mtlGetUniformBuffers
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   625
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   626
void *MOJOSHADER_mtlGetFunctionHandle(MOJOSHADER_mtlShader *shader)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   627
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   628
    if (shader == NULL)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   629
        return NULL;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   630
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   631
    void *fnname = msg_s(
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   632
        msg(ctx->classNSString, ctx->selAlloc),
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   633
        ctx->selInitWithUTF8String,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   634
        shader->parseData->mainfn
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   635
    );
1241
01fea3855ffb iOS buildfixes
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1240
diff changeset
   636
    void *ret = msg_p(
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   637
        shader->library,
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   638
        ctx->selNewFunctionWithName,
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   639
        fnname
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   640
    );
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   641
    msg(fnname, ctx->selRelease);
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   642
    msg(ret, ctx->selRetain);
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   643
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   644
    return ret;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   645
} // MOJOSHADER_mtlGetFunctionHandle
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   646
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   647
void MOJOSHADER_mtlEndFrame()
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   648
{
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   649
    for (int i = 0; i < ctx->numBuffersInUse; i += 1)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   650
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   651
        MOJOSHADER_mtlUniformBuffer *buf = ctx->buffersInUse[i];
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   652
        buf->internalOffset = 0;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   653
        buf->currentFrame = (buf->currentFrame + 1) % ctx->framesInFlight;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   654
        buf->inUse = 0;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   655
    } // for
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   656
    ctx->numBuffersInUse = 0;
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   657
} // MOJOSHADER_mtlEndFrame
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   658
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   659
int MOJOSHADER_mtlGetVertexAttribLocation(MOJOSHADER_mtlShader *vert,
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   660
                                          MOJOSHADER_usage usage, int index)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   661
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   662
    if (vert == NULL)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   663
        return -1;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   664
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   665
    for (int i = 0; i < vert->parseData->attribute_count; i++)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   666
    {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   667
        if (vert->parseData->attributes[i].usage == usage &&
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1241
diff changeset
   668
            vert->parseData->attributes[i].index == index)
1229
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   669
        {
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   670
            return i;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   671
        } // if
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   672
    } // for
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   673
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   674
    // failure, couldn't find requested attribute
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   675
    return -1;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   676
} // MOJOSHADER_mtlGetVertexAttribLocation
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   677
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   678
const char *MOJOSHADER_mtlGetError(void)
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   679
{
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   680
    return error_buffer;
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   681
} // MOJOSHADER_mtlGetError
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   682
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   683
#endif /* MOJOSHADER_EFFECT_SUPPORT */
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   684
#endif /* SUPPORT_PROFILE_METAL && PLATFORM_APPLE */
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   685
3ecd29a50d9d Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   686
// end of mojoshader_metal.c ...