mojoshader_d3d11.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 20 Jul 2020 19:24:46 -0400
changeset 1294 d4084c8412d0
parent 1262 8df5c62abd22
child 1296 9b3b01f2a9b7
permissions -rw-r--r--
Rebuilt the lexer with a much newer re2c.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1255
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     1
/**
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     3
 *  Direct3D shaders.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     4
 *
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     6
 *
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     8
 */
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
     9
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    10
#ifdef _WIN32
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    11
#define WIN32_LEAN_AND_MEAN 1
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    12
#include <windows.h> // Include this early to avoid SDL conflicts
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    13
#endif
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    14
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    15
#define __MOJOSHADER_INTERNAL__ 1
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    16
#include "mojoshader_internal.h"
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    17
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    18
#if SUPPORT_PROFILE_HLSL
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    19
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    20
#define D3D11_NO_HELPERS
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    21
#define CINTERFACE
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    22
#define COBJMACROS
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    23
#include <d3d11.h>
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    24
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    25
#ifndef WINAPI_FAMILY_WINRT
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    26
#define WINAPI_FAMILY_WINRT 0
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    27
#endif
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    28
#if WINAPI_FAMILY_WINRT
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    29
#include <d3dcompiler.h>
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    30
#endif
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    31
1262
8df5c62abd22 Disable D3D11 HLSL optimizations, as they break program behavior
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1255
diff changeset
    32
// D3DCompile optimization can be overzealous and cause very visible bugs,
8df5c62abd22 Disable D3D11 HLSL optimizations, as they break program behavior
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1255
diff changeset
    33
//  so we disable it when compiling shaders to preserve correctness.
8df5c62abd22 Disable D3D11 HLSL optimizations, as they break program behavior
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1255
diff changeset
    34
#define D3D_SKIP_OPT (1 << 2)
8df5c62abd22 Disable D3D11 HLSL optimizations, as they break program behavior
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1255
diff changeset
    35
1255
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    36
/* Error state */
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    37
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    38
static char error_buffer[1024] = { '\0' };
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    39
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    40
static void set_error(const char *str)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    41
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    42
    snprintf(error_buffer, sizeof (error_buffer), "%s", str);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    43
} // set_error
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    44
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    45
static inline void out_of_memory(void)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    46
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    47
    set_error("out of memory");
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    48
} // out_of_memory
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    49
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    50
/* D3DCompile signature */
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    51
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    52
typedef HRESULT(WINAPI *PFN_D3DCOMPILE)(
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    53
    LPCVOID pSrcData,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    54
    SIZE_T SrcDataSize,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    55
    LPCSTR pSourceName,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    56
    const D3D_SHADER_MACRO *pDefines,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    57
    ID3DInclude *pInclude,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    58
    LPCSTR pEntrypoint,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    59
    LPCSTR pTarget,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    60
    UINT Flags1,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    61
    UINT Flags2,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    62
    ID3DBlob **ppCode,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    63
    ID3DBlob **ppErrorMsgs
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    64
);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    65
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    66
/* Structs */
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    67
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    68
typedef struct d3d11ShaderMap
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    69
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    70
    void *val;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    71
    union
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    72
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    73
        struct
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    74
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    75
            uint64 layoutHash;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    76
            ID3D10Blob *blob;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    77
        } vertex;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    78
        struct
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    79
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    80
            MOJOSHADER_d3d11Shader *vshader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    81
        } pixel;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    82
    };
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    83
} d3d11ShaderMap;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    84
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    85
typedef struct MOJOSHADER_d3d11Shader
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    86
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    87
    const MOJOSHADER_parseData *parseData;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    88
    uint32 refcount;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    89
    ID3D11Buffer *ubo;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    90
    size_t buflen;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    91
    unsigned char *constantData;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    92
    unsigned int mapCapacity;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    93
    unsigned int numMaps;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    94
    d3d11ShaderMap *shaderMaps;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    95
} MOJOSHADER_d3d11Shader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    96
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    97
// Max entries for each register file type...
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    98
#define MAX_REG_FILE_F 8192
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
    99
#define MAX_REG_FILE_I 2047
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   100
#define MAX_REG_FILE_B 2047
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   101
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   102
typedef struct MOJOSHADER_d3d11Context
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   103
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   104
    // Allocators...
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   105
    MOJOSHADER_malloc malloc_fn;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   106
    MOJOSHADER_free free_fn;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   107
    void *malloc_data;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   108
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   109
    // The constant register files...
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   110
    // !!! FIXME: Man, it kills me how much memory this takes...
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   111
    // !!! FIXME:  ... make this dynamically allocated on demand.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   112
    float vs_reg_file_f[MAX_REG_FILE_F * 4];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   113
    int vs_reg_file_i[MAX_REG_FILE_I * 4];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   114
    uint8 vs_reg_file_b[MAX_REG_FILE_B];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   115
    float ps_reg_file_f[MAX_REG_FILE_F * 4];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   116
    int ps_reg_file_i[MAX_REG_FILE_I * 4];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   117
    uint8 ps_reg_file_b[MAX_REG_FILE_B];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   118
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   119
    // Pointer to the active ID3D11Device.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   120
    ID3D11Device *device;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   121
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   122
    // Pointer to the ID3D11DeviceContext.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   123
    ID3D11DeviceContext *deviceContext;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   124
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   125
    // Currently bound vertex and pixel shaders.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   126
    MOJOSHADER_d3d11Shader *vertexShader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   127
    MOJOSHADER_d3d11Shader *pixelShader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   128
    int vertexNeedsBound;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   129
    int pixelNeedsBound;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   130
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   131
    // D3DCompile function pointer.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   132
    PFN_D3DCOMPILE D3DCompileFunc;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   133
#if !WINAPI_FAMILY_WINRT
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   134
    HMODULE d3dcompilerDLL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   135
#endif
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   136
} MOJOSHADER_d3d11Context;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   137
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   138
static MOJOSHADER_d3d11Context *ctx = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   139
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   140
/* Uniform buffer utilities */
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   141
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   142
static inline int next_highest_alignment(int n)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   143
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   144
    const int align = 16;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   145
    return align * ((n + align - 1) / align);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   146
} // next_highest_alignment
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   147
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   148
static inline void *get_uniform_buffer(MOJOSHADER_d3d11Shader *shader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   149
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   150
    return (shader == NULL || shader->ubo == NULL) ? NULL : shader->ubo;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   151
} // get_uniform_buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   152
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   153
static void update_uniform_buffer(MOJOSHADER_d3d11Shader *shader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   154
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   155
    if (shader == NULL || shader->ubo == NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   156
        return;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   157
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   158
    float *regF; int *regI; uint8 *regB;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   159
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   160
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   161
        regF = ctx->vs_reg_file_f;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   162
        regI = ctx->vs_reg_file_i;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   163
        regB = ctx->vs_reg_file_b;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   164
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   165
    else
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   166
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   167
        regF = ctx->ps_reg_file_f;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   168
        regI = ctx->ps_reg_file_i;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   169
        regB = ctx->ps_reg_file_b;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   170
    } // else
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   171
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   172
    // Update the buffer contents
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   173
    int needsUpdate = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   174
    size_t offset = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   175
    for (int i = 0; i < shader->parseData->uniform_count; i++)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   176
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   177
        if (shader->parseData->uniforms[i].constant)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   178
            continue;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   179
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   180
        int idx = shader->parseData->uniforms[i].index;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   181
        int arrayCount = shader->parseData->uniforms[i].array_count;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   182
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   183
        void *src = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   184
        void *dst = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   185
        size_t size = arrayCount ? arrayCount : 1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   186
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   187
        switch (shader->parseData->uniforms[i].type)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   188
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   189
            case MOJOSHADER_UNIFORM_FLOAT:
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   190
                src = &regF[4 * idx];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   191
                dst = shader->constantData + offset;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   192
                size *= 16;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   193
                break;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   194
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   195
            case MOJOSHADER_UNIFORM_INT:
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   196
                src = &regI[4 * idx];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   197
                dst = shader->constantData + offset;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   198
                size *= 16;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   199
                break;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   200
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   201
            case MOJOSHADER_UNIFORM_BOOL:
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   202
                src = &regB[idx];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   203
                dst = shader->constantData + offset;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   204
                break;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   205
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   206
            default:
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   207
                assert(0); // This should never happen.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   208
                break;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   209
        } // switch
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   210
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   211
        if (memcmp(dst, src, size) != 0)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   212
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   213
            memcpy(dst, src, size);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   214
            needsUpdate = 1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   215
        } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   216
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   217
        offset += size;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   218
    } // for
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   219
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   220
    if (needsUpdate)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   221
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   222
        // Map the buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   223
        D3D11_MAPPED_SUBRESOURCE res;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   224
        ID3D11DeviceContext_Map((ID3D11DeviceContext*) ctx->deviceContext,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   225
                                (ID3D11Resource*) shader->ubo, 0,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   226
                                D3D11_MAP_WRITE_DISCARD, 0, &res);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   227
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   228
        // Copy the contents
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   229
        memcpy(res.pData, shader->constantData, shader->buflen);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   230
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   231
        // Unmap the buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   232
        ID3D11DeviceContext_Unmap(
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   233
            (ID3D11DeviceContext*) ctx->deviceContext,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   234
            (ID3D11Resource*) shader->ubo,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   235
            0
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   236
        );
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   237
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   238
} // update_uniform_buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   239
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   240
static inline void expand_map(MOJOSHADER_d3d11Shader *shader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   241
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   242
    if (shader->numMaps == shader->mapCapacity)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   243
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   244
        d3d11ShaderMap *newMap = (d3d11ShaderMap *) ctx->malloc_fn(
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   245
            sizeof(d3d11ShaderMap) * shader->mapCapacity * 2,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   246
            ctx->malloc_data
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   247
        );
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   248
        memcpy(newMap, shader->shaderMaps,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   249
            sizeof(d3d11ShaderMap) * shader->mapCapacity);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   250
        shader->mapCapacity *= 2;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   251
        ctx->free_fn(shader->shaderMaps, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   252
        shader->shaderMaps = newMap;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   253
        newMap = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   254
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   255
} // expand_map
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   256
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   257
static inline int element_is_uint(DXGI_FORMAT format)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   258
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   259
    return  format == DXGI_FORMAT_R32G32B32A32_UINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   260
            format == DXGI_FORMAT_R32G32B32_UINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   261
            format == DXGI_FORMAT_R16G16B16A16_UINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   262
            format == DXGI_FORMAT_R32G32_UINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   263
            format == DXGI_FORMAT_R10G10B10A2_UINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   264
            format == DXGI_FORMAT_R8G8B8A8_UINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   265
            format == DXGI_FORMAT_R16G16_UINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   266
            format == DXGI_FORMAT_R32_UINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   267
            format == DXGI_FORMAT_R8G8_UINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   268
            format == DXGI_FORMAT_R16_UINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   269
            format == DXGI_FORMAT_R8_UINT;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   270
} // element_is_uint
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   271
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   272
static inline int element_is_int(DXGI_FORMAT format)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   273
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   274
    return  format == DXGI_FORMAT_R32G32B32A32_SINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   275
            format == DXGI_FORMAT_R32G32B32_SINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   276
            format == DXGI_FORMAT_R16G16B16A16_SINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   277
            format == DXGI_FORMAT_R32G32_SINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   278
            format == DXGI_FORMAT_R8G8B8A8_SINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   279
            format == DXGI_FORMAT_R16G16_SINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   280
            format == DXGI_FORMAT_R32_SINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   281
            format == DXGI_FORMAT_R8G8_SINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   282
            format == DXGI_FORMAT_R16_SINT ||
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   283
            format == DXGI_FORMAT_R8_SINT;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   284
} // element_is_int
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   285
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   286
/* Shader Compilation Utilities */
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   287
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   288
static ID3D11VertexShader *compileVertexShader(MOJOSHADER_d3d11Shader *shader,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   289
                                               const char *src, int src_len,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   290
                                               ID3D10Blob **blob)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   291
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   292
    const MOJOSHADER_parseData *pd = shader->parseData;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   293
    HRESULT result = ctx->D3DCompileFunc(src, src_len, pd->mainfn,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   294
                                         NULL, NULL, pd->mainfn, "vs_4_0",
1262
8df5c62abd22 Disable D3D11 HLSL optimizations, as they break program behavior
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1255
diff changeset
   295
                                         D3D_SKIP_OPT, 0, blob, blob);
1255
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   296
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   297
    if (result < 0)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   298
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   299
        set_error((const char *) ID3D10Blob_GetBufferPointer(*blob));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   300
        ID3D10Blob_Release(*blob);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   301
        return NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   302
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   303
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   304
    void *bytecode = ID3D10Blob_GetBufferPointer(*blob);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   305
    int bytecodeLength = ID3D10Blob_GetBufferSize(*blob);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   306
    ID3D11VertexShader *ret = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   307
    ID3D11Device_CreateVertexShader(ctx->device, bytecode, bytecodeLength,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   308
                                    NULL, &ret);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   309
    return ret;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   310
} // compileVertexShader
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   311
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   312
static void replaceVarname(const char *find, const char *replace,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   313
                           const char **source)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   314
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   315
    const char *srcbuf = *source;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   316
    size_t find_len = strlen(find);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   317
    size_t replace_len = strlen(replace);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   318
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   319
    #define IS_PARTIAL_TOKEN(token) \
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   320
        (isalnum(*(token + find_len)) || isalnum(*(token-1)))
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   321
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   322
    // How many times does `find` occur in the source buffer?
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   323
    int count = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   324
    char *ptr = (char *) strstr(srcbuf, find);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   325
    while (ptr != NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   326
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   327
        if (!IS_PARTIAL_TOKEN(ptr))
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   328
            count++;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   329
        ptr = strstr(ptr + find_len, find);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   330
    } // while
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   331
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   332
    // How big should we make the new text buffer?
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   333
    size_t oldlen = strlen(srcbuf) + 1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   334
    size_t newlen = oldlen + (count * (replace_len - find_len));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   335
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   336
    // Easy case; just find/replace in the original buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   337
    if (newlen == oldlen)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   338
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   339
        ptr = (char *) strstr(srcbuf, find);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   340
        while (ptr != NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   341
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   342
            if (!IS_PARTIAL_TOKEN(ptr))
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   343
                memcpy(ptr, replace, replace_len);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   344
            ptr = strstr(ptr + find_len, find);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   345
        } // while
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   346
        return;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   347
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   348
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   349
    // Allocate a new buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   350
    char *newbuf = (char *) ctx->malloc_fn(newlen, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   351
    memset(newbuf, '\0', newlen);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   352
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   353
    // Find + replace
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   354
    char *prev_ptr = (char *) srcbuf;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   355
    char *curr_ptr = (char *) newbuf;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   356
    ptr = (char*) strstr(srcbuf, find);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   357
    while (ptr != NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   358
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   359
        memcpy(curr_ptr, prev_ptr, ptr - prev_ptr);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   360
        curr_ptr += ptr - prev_ptr;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   361
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   362
        if (!IS_PARTIAL_TOKEN(ptr))
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   363
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   364
            memcpy(curr_ptr, replace, replace_len);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   365
            curr_ptr += replace_len;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   366
        } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   367
        else
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   368
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   369
            // Don't accidentally eat partial tokens...
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   370
            memcpy(curr_ptr, find, find_len);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   371
            curr_ptr += find_len;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   372
        } // else
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   373
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   374
        prev_ptr = ptr + find_len;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   375
        ptr = strstr(prev_ptr, find);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   376
    } // while
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   377
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   378
    #undef IS_PARTIAL_TOKEN
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   379
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   380
    // Copy the remaining part of the source buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   381
    memcpy(curr_ptr, prev_ptr, (srcbuf + oldlen) - prev_ptr);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   382
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   383
    // Free the source buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   384
    ctx->free_fn((void *) srcbuf, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   385
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   386
    // Point the source parameter to the new buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   387
    *source = newbuf;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   388
} // replaceVarname
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   389
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   390
static char *rewritePixelShader(MOJOSHADER_d3d11Shader *vshader,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   391
                                MOJOSHADER_d3d11Shader *pshader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   392
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   393
    const MOJOSHADER_parseData *vpd = vshader->parseData;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   394
    const MOJOSHADER_parseData *ppd = pshader->parseData;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   395
    const char *_Output = "_Output" ENDLINE_STR "{" ENDLINE_STR;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   396
    const char *_Input = "_Input" ENDLINE_STR "{" ENDLINE_STR;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   397
    const char *vsrc = vpd->output;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   398
    const char *psrc = ppd->output;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   399
    const char *a, *b, *vout, *pstart, *vface, *pend;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   400
    size_t substr_len;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   401
    char *pfinal;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   402
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   403
    #define MAKE_STRBUF(buf) \
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   404
        substr_len = b - a; \
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   405
        buf = (const char *) ctx->malloc_fn(substr_len + 1, ctx->malloc_data); \
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   406
        memset((void *) buf, '\0', substr_len + 1); \
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   407
        memcpy((void *) buf, a, substr_len);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   408
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   409
    // Copy the vertex function's output struct into a buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   410
    a = strstr(vsrc, _Output) + strlen(_Output);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   411
    b = a;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   412
    while (*(b++) != '}');
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   413
    b--;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   414
    MAKE_STRBUF(vout)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   415
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   416
    // Split up the pixel shader text...
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   417
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   418
    // ...everything up to the input contents...
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   419
    a = psrc;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   420
    b = strstr(psrc, _Input) + strlen(_Input);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   421
    MAKE_STRBUF(pstart)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   422
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   423
    // ...everything after the input contents.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   424
    a = b;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   425
    while (*(a++) != '}');
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   426
    a--;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   427
    while (*(b++) != '\0');
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   428
    MAKE_STRBUF(pend)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   429
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   430
    // Find matching semantics
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   431
    int i, j;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   432
    int vfaceidx = -1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   433
    const char *pvarname, *vvarname;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   434
    for (i = 0; i < ppd->attribute_count; i++)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   435
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   436
        for (j = 0; j < vpd->output_count; j++)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   437
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   438
            if (ppd->attributes[i].usage == vpd->outputs[j].usage &&
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   439
                ppd->attributes[i].index == vpd->outputs[j].index)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   440
            {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   441
                pvarname = ppd->attributes[i].name;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   442
                vvarname = vpd->outputs[j].name;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   443
                if (strcmp(pvarname, vvarname) != 0)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   444
                    replaceVarname(pvarname, vvarname, &pend);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   445
            } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   446
            else if (strcmp(ppd->attributes[i].name, "vPos") == 0 &&
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   447
                     vpd->outputs[j].usage == MOJOSHADER_USAGE_POSITION &&
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   448
                     vpd->outputs[j].index == 0)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   449
            {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   450
                pvarname = ppd->attributes[i].name;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   451
                vvarname = vpd->outputs[j].name;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   452
                if (strcmp(pvarname, vvarname) != 0)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   453
                    replaceVarname(pvarname, vvarname, &pend);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   454
            } // else if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   455
        } // for
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   456
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   457
        if (strcmp(ppd->attributes[i].name, "vFace") == 0)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   458
            vfaceidx = i;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   459
    } // for
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   460
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   461
    // Special handling for VFACE
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   462
    vface = (vfaceidx != -1) ? "\tbool m_vFace : SV_IsFrontFace;\n" : "";
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   463
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   464
    // Concatenate the shader pieces together
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   465
    substr_len = strlen(pstart) + strlen(vout) + strlen(vface) + strlen(pend);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   466
    pfinal = (char *) ctx->malloc_fn(substr_len + 1, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   467
    memset((void *) pfinal, '\0', substr_len + 1);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   468
    memcpy(pfinal, pstart, strlen(pstart));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   469
    memcpy(pfinal + strlen(pstart), vout, strlen(vout));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   470
    memcpy(pfinal + strlen(pstart) + strlen(vout), vface, strlen(vface));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   471
    memcpy(pfinal + strlen(pstart) + strlen(vout) + strlen(vface), pend, strlen(pend));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   472
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   473
    // Free the temporary buffers
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   474
    ctx->free_fn((void *) vout, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   475
    ctx->free_fn((void *) pstart, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   476
    ctx->free_fn((void *) pend, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   477
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   478
    #undef MAKE_STRBUF
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   479
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   480
    return pfinal;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   481
} // spliceVertexShaderInput
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   482
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   483
static ID3D11PixelShader *compilePixelShader(MOJOSHADER_d3d11Shader *vshader,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   484
                                             MOJOSHADER_d3d11Shader *pshader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   485
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   486
    ID3D11PixelShader *retval = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   487
    const char *source;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   488
    ID3DBlob *blob;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   489
    HRESULT result;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   490
    int needs_free;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   491
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   492
    if (pshader->parseData->attribute_count > 0)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   493
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   494
        source = rewritePixelShader(vshader, pshader);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   495
        needs_free = 1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   496
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   497
    else
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   498
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   499
        source = pshader->parseData->output;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   500
        needs_free = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   501
    } // else
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   502
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   503
    result = ctx->D3DCompileFunc(source, strlen(source),
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   504
                                 pshader->parseData->mainfn, NULL, NULL,
1262
8df5c62abd22 Disable D3D11 HLSL optimizations, as they break program behavior
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1255
diff changeset
   505
                                 pshader->parseData->mainfn, "ps_4_0",
8df5c62abd22 Disable D3D11 HLSL optimizations, as they break program behavior
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1255
diff changeset
   506
                                 D3D_SKIP_OPT, 0, &blob, &blob);
1255
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   507
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   508
    if (result < 0)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   509
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   510
        set_error((const char *) ID3D10Blob_GetBufferPointer(blob));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   511
        ctx->free_fn((void *) source, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   512
        return NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   513
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   514
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   515
    ID3D11Device_CreatePixelShader(ctx->device,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   516
                                   ID3D10Blob_GetBufferPointer(blob),
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   517
                                   ID3D10Blob_GetBufferSize(blob),
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   518
                                   NULL, &retval);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   519
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   520
    ID3D10Blob_Release(blob);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   521
    if (needs_free)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   522
        ctx->free_fn((void *) source, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   523
    return retval;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   524
} // compilePixelShader
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   525
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   526
/* Public API */
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   527
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   528
int MOJOSHADER_d3d11CreateContext(void *device, void *deviceContext,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   529
                                  MOJOSHADER_malloc m, MOJOSHADER_free f,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   530
                                  void *malloc_d)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   531
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   532
    assert(ctx == NULL);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   533
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   534
    if (m == NULL) m = MOJOSHADER_internal_malloc;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   535
    if (f == NULL) f = MOJOSHADER_internal_free;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   536
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   537
    ctx = (MOJOSHADER_d3d11Context *) m(sizeof(MOJOSHADER_d3d11Context), malloc_d);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   538
    if (ctx == NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   539
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   540
        out_of_memory();
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   541
        goto init_fail;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   542
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   543
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   544
    memset(ctx, '\0', sizeof (MOJOSHADER_d3d11Context));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   545
    ctx->malloc_fn = m;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   546
    ctx->free_fn = f;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   547
    ctx->malloc_data = malloc_d;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   548
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   549
    // Store references to the D3D device and immediate context
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   550
    ctx->device = (ID3D11Device*) device;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   551
    ctx->deviceContext = (ID3D11DeviceContext*) deviceContext;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   552
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   553
    // Grab the D3DCompile function pointer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   554
#if WINAPI_FAMILY_WINRT
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   555
    ctx->D3DCompileFunc = D3DCompile;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   556
#else
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   557
    ctx->d3dcompilerDLL = LoadLibrary("d3dcompiler_47.dll");
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   558
    assert(ctx->d3dcompilerDLL != NULL);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   559
    ctx->D3DCompileFunc = (PFN_D3DCOMPILE) GetProcAddress(ctx->d3dcompilerDLL,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   560
                                                          "D3DCompile");
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   561
#endif /* WINAPI_FAMILY_WINRT */
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   562
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   563
    return 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   564
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   565
init_fail:
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   566
    if (ctx != NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   567
        f(ctx, malloc_d);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   568
    return -1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   569
} // MOJOSHADER_d3d11CreateContext
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   570
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   571
void MOJOSHADER_d3d11DestroyContext(void)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   572
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   573
#if !WINAPI_FAMILY_WINRT
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   574
    FreeLibrary(ctx->d3dcompilerDLL);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   575
#endif
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   576
    ctx->free_fn(ctx, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   577
    ctx = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   578
} // MOJOSHADER_d3d11DestroyContext
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   579
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   580
MOJOSHADER_d3d11Shader *MOJOSHADER_d3d11CompileShader(const char *mainfn,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   581
                                                      const unsigned char *tokenbuf,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   582
                                                      const unsigned int bufsize,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   583
                                                      const MOJOSHADER_swizzle *swiz,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   584
                                                      const unsigned int swizcount,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   585
                                                      const MOJOSHADER_samplerMap *smap,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   586
                                                      const unsigned int smapcount)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   587
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   588
    MOJOSHADER_malloc m = ctx->malloc_fn;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   589
    MOJOSHADER_free f = ctx->free_fn;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   590
    void *d = ctx->malloc_data;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   591
    int i;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   592
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   593
    const MOJOSHADER_parseData *pd = MOJOSHADER_parse("hlsl", mainfn, tokenbuf,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   594
                                                     bufsize, swiz, swizcount,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   595
                                                     smap, smapcount, m, f, d);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   596
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   597
    if (pd->error_count > 0)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   598
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   599
        // !!! FIXME: put multiple errors in the buffer? Don't use
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   600
        // !!! FIXME:  MOJOSHADER_d3d11GetError() for this?
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   601
        set_error(pd->errors[0].error);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   602
        goto compile_shader_fail;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   603
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   604
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   605
    MOJOSHADER_d3d11Shader *retval = (MOJOSHADER_d3d11Shader *) m(sizeof(MOJOSHADER_d3d11Shader), d);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   606
    if (retval == NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   607
        goto compile_shader_fail;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   608
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   609
    retval->parseData = pd;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   610
    retval->refcount = 1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   611
    retval->ubo = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   612
    retval->constantData = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   613
    retval->buflen = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   614
    retval->numMaps = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   615
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   616
    // Allocate shader maps
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   617
    retval->mapCapacity = 4; // arbitrary!
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   618
    retval->shaderMaps = (d3d11ShaderMap *) m(retval->mapCapacity * sizeof(d3d11ShaderMap), d);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   619
    if (retval->shaderMaps == NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   620
        goto compile_shader_fail;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   621
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   622
    memset(retval->shaderMaps, '\0', retval->mapCapacity * sizeof(d3d11ShaderMap));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   623
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   624
    // Create the uniform buffer, if needed
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   625
    if (pd->uniform_count > 0)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   626
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   627
        // Calculate how big we need to make the buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   628
        for (i = 0; i < pd->uniform_count; i++)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   629
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   630
            int arrayCount = pd->uniforms[i].array_count;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   631
            int uniformSize = 16;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   632
            if (pd->uniforms[i].type == MOJOSHADER_UNIFORM_BOOL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   633
                uniformSize = 1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   634
            retval->buflen += (arrayCount ? arrayCount : 1) * uniformSize;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   635
        } // for
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   636
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   637
        D3D11_BUFFER_DESC bdesc;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   638
        bdesc.ByteWidth = next_highest_alignment(retval->buflen);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   639
        bdesc.Usage = D3D11_USAGE_DYNAMIC;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   640
        bdesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   641
        bdesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   642
        bdesc.MiscFlags = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   643
        bdesc.StructureByteStride = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   644
        ID3D11Device_CreateBuffer((ID3D11Device*) ctx->device, &bdesc, NULL,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   645
                                  (ID3D11Buffer**) &retval->ubo);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   646
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   647
        // Additionally allocate a CPU-side staging buffer
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   648
        retval->constantData = (unsigned char *) m(retval->buflen, d);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   649
        memset(retval->constantData, '\0', retval->buflen);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   650
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   651
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   652
    return retval;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   653
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   654
compile_shader_fail:
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   655
    MOJOSHADER_freeParseData(pd);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   656
    return NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   657
} // MOJOSHADER_d3d11CompileShader
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   658
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   659
void MOJOSHADER_d3d11ShaderAddRef(MOJOSHADER_d3d11Shader *shader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   660
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   661
    if (shader != NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   662
        shader->refcount++;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   663
} // MOJOSHADER_d3d11ShaderAddRef
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   664
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   665
void MOJOSHADER_d3d11DeleteShader(MOJOSHADER_d3d11Shader *shader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   666
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   667
    if (shader != NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   668
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   669
        if (shader->refcount > 1)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   670
            shader->refcount--;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   671
        else
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   672
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   673
            if (shader->ubo != NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   674
            {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   675
                ID3D11Buffer_Release((ID3D11Buffer*) shader->ubo);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   676
                ctx->free_fn(shader->constantData, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   677
            } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   678
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   679
            if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   680
            {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   681
                for (int i = 0; i < shader->numMaps; i++)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   682
                {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   683
                    ID3D11VertexShader_Release((ID3D11VertexShader *) shader->shaderMaps[i].val);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   684
                    ID3D10Blob_Release(shader->shaderMaps[i].vertex.blob);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   685
                } // for
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   686
            } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   687
            else if (shader->parseData->shader_type == MOJOSHADER_TYPE_PIXEL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   688
            {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   689
                for (int i = 0; i < shader->numMaps; i++)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   690
                    ID3D11PixelShader_Release((ID3D11PixelShader *) shader->shaderMaps[i].val);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   691
            } // else if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   692
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   693
            ctx->free_fn(shader->shaderMaps, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   694
            shader->shaderMaps = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   695
            MOJOSHADER_freeParseData(shader->parseData);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   696
            ctx->free_fn(shader, ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   697
        } // else
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   698
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   699
} // MOJOSHADER_d3d11DeleteShader
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   700
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   701
const MOJOSHADER_parseData *MOJOSHADER_d3d11GetShaderParseData(
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   702
                                                MOJOSHADER_d3d11Shader *shader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   703
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   704
    return (shader != NULL) ? shader->parseData : NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   705
} // MOJOSHADER_d3d11GetParseData
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   706
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   707
void MOJOSHADER_d3d11BindShaders(MOJOSHADER_d3d11Shader *vshader,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   708
                                 MOJOSHADER_d3d11Shader *pshader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   709
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   710
    // Use the last bound shaders in case of NULL
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   711
    if (vshader != NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   712
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   713
        ctx->vertexShader = vshader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   714
        ctx->vertexNeedsBound = 1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   715
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   716
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   717
    if (pshader != NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   718
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   719
        ctx->pixelShader = pshader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   720
        ctx->pixelNeedsBound = 1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   721
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   722
} // MOJOSHADER_d3d11BindShaders
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   723
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   724
void MOJOSHADER_d3d11GetBoundShaders(MOJOSHADER_d3d11Shader **vshader,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   725
                                     MOJOSHADER_d3d11Shader **pshader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   726
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   727
    *vshader = ctx->vertexShader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   728
    *pshader = ctx->pixelShader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   729
} // MOJOSHADER_d3d11GetBoundShaders
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   730
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   731
void MOJOSHADER_d3d11MapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   732
                                            float **psf, int **psi, unsigned char **psb)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   733
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   734
    *vsf = ctx->vs_reg_file_f;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   735
    *vsi = ctx->vs_reg_file_i;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   736
    *vsb = ctx->vs_reg_file_b;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   737
    *psf = ctx->ps_reg_file_f;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   738
    *psi = ctx->ps_reg_file_i;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   739
    *psb = ctx->ps_reg_file_b;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   740
} // MOJOSHADER_d3d11MapUniformBufferMemory
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   741
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   742
void MOJOSHADER_d3d11UnmapUniformBufferMemory()
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   743
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   744
    /* This has nothing to do with unmapping memory
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   745
     * and everything to do with updating uniform
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   746
     * buffers with the latest parameter contents.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   747
     */
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   748
    MOJOSHADER_d3d11Shader *vs, *ps;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   749
    MOJOSHADER_d3d11GetBoundShaders(&vs, &ps);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   750
    update_uniform_buffer(vs);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   751
    update_uniform_buffer(ps);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   752
} // MOJOSHADER_d3d11UnmapUniformBufferMemory
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   753
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   754
int MOJOSHADER_d3d11GetVertexAttribLocation(MOJOSHADER_d3d11Shader *vert,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   755
                                            MOJOSHADER_usage usage, int index)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   756
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   757
    if (vert == NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   758
        return -1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   759
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   760
    for (int i = 0; i < vert->parseData->attribute_count; i++)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   761
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   762
        if (vert->parseData->attributes[i].usage == usage &&
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   763
            vert->parseData->attributes[i].index == index)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   764
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   765
            return i;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   766
        } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   767
    } // for
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   768
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   769
    // failure, couldn't find requested attribute
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   770
    return -1;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   771
} // MOJOSHADER_d3d11GetVertexAttribLocation
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   772
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   773
void MOJOSHADER_d3d11CompileVertexShader(unsigned long long inputLayoutHash,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   774
                                         void* elements, int elementCount,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   775
                                         void **bytecode, int *bytecodeLength)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   776
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   777
    MOJOSHADER_d3d11Shader *vshader = ctx->vertexShader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   778
    ID3D10Blob *blob;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   779
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   780
    // Don't compile if there's already a mapping for this layout.
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   781
    for (int i = 0; i < vshader->numMaps; i++)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   782
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   783
        if (inputLayoutHash == vshader->shaderMaps[i].vertex.layoutHash)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   784
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   785
            blob = vshader->shaderMaps[i].vertex.blob;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   786
            *bytecode = ID3D10Blob_GetBufferPointer(blob);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   787
            *bytecodeLength = ID3D10Blob_GetBufferSize(blob);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   788
            return;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   789
        } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   790
    } // for
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   791
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   792
    // Check for and replace non-float types
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   793
    D3D11_INPUT_ELEMENT_DESC *d3dElements = (D3D11_INPUT_ELEMENT_DESC*) elements;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   794
    const char *origSource = vshader->parseData->output;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   795
    int srcLength = vshader->parseData->output_len;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   796
    char *newSource = (char*) origSource;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   797
    for (int i = 0; i < elementCount; i += 1)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   798
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   799
        D3D11_INPUT_ELEMENT_DESC e = d3dElements[i];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   800
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   801
        const char *replace;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   802
        if (element_is_uint(e.Format))
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   803
            replace = " uint4";
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   804
        else if (element_is_int(e.Format))
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   805
            replace = "  int4";
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   806
        else
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   807
            replace = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   808
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   809
        if (replace != NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   810
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   811
            char sem[16];
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   812
            memset(sem, '\0', sizeof(sem));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   813
            snprintf(sem, sizeof(sem), "%s%d", e.SemanticName, e.SemanticIndex);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   814
            // !!! FIXME: POSITIONT has no index. What to do? -caleb
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   815
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   816
            if (newSource == origSource)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   817
            {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   818
                newSource = (char *) ctx->malloc_fn(srcLength + 1,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   819
                                                    ctx->malloc_data);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   820
                strcpy(newSource, origSource);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   821
            } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   822
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   823
            char *ptr = strstr(newSource, sem);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   824
            assert(ptr != NULL && "Could not find semantic in shader source!");
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   825
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   826
            int spaces = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   827
            while (spaces < 3)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   828
                if (*(--ptr) == ' ') spaces++;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   829
            memcpy(ptr - strlen("float4"), replace, strlen(replace));
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   830
        } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   831
    } // for
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   832
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   833
    // Expand the map array, if needed
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   834
    expand_map(vshader);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   835
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   836
    // Add the new mapping
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   837
    vshader->shaderMaps[vshader->numMaps].vertex.layoutHash = inputLayoutHash;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   838
    ID3D11VertexShader *vs = compileVertexShader(vshader, newSource,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   839
                                                 srcLength, &blob);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   840
    vshader->shaderMaps[ctx->vertexShader->numMaps].val = vs;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   841
    vshader->shaderMaps[ctx->vertexShader->numMaps].vertex.blob = blob;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   842
    ctx->vertexShader->numMaps++;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   843
    assert(vs != NULL);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   844
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   845
    // Return the bytecode info
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   846
    *bytecode = ID3D10Blob_GetBufferPointer(blob);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   847
    *bytecodeLength = ID3D10Blob_GetBufferSize(blob);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   848
} // MOJOSHADER_d3d11CompileVertexShader
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   849
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   850
void MOJOSHADER_d3d11ProgramReady(unsigned long long inputLayoutHash)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   851
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   852
    MOJOSHADER_d3d11Shader *vshader = ctx->vertexShader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   853
    MOJOSHADER_d3d11Shader *pshader = ctx->pixelShader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   854
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   855
    // Vertex shader...
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   856
    if (ctx->vertexNeedsBound)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   857
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   858
        ID3D11VertexShader *realVS = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   859
        for (int i = 0; i < vshader->numMaps; i++)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   860
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   861
            if (inputLayoutHash == vshader->shaderMaps[i].vertex.layoutHash)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   862
            {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   863
                realVS = (ID3D11VertexShader *) vshader->shaderMaps[i].val;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   864
                break;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   865
            } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   866
        } // for
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   867
        assert(realVS != NULL);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   868
        ID3D11DeviceContext_VSSetShader(ctx->deviceContext, realVS, NULL, 0);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   869
        ID3D11DeviceContext_VSSetConstantBuffers(ctx->deviceContext, 0, 1,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   870
                                                 &vshader->ubo);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   871
        ctx->vertexNeedsBound = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   872
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   873
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   874
    // Pixel shader...
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   875
    if (ctx->pixelNeedsBound)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   876
    {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   877
        // Is there already a mapping for the current vertex shader?
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   878
        ID3D11PixelShader *realPS = NULL;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   879
        for (int i = 0; i < pshader->numMaps; i++)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   880
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   881
            if (pshader->shaderMaps[i].pixel.vshader == vshader)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   882
            {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   883
                realPS = (ID3D11PixelShader *) pshader->shaderMaps[i].val;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   884
                break;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   885
            } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   886
        } // for
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   887
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   888
        // We have to create a new vertex/pixel shader mapping...
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   889
        if (realPS == NULL)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   890
        {
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   891
            // Expand the map array, if needed
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   892
            expand_map(pshader);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   893
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   894
            // Add the new mapping
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   895
            pshader->shaderMaps[pshader->numMaps].pixel.vshader = vshader;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   896
            realPS = compilePixelShader(vshader, pshader);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   897
            pshader->shaderMaps[pshader->numMaps].val = realPS;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   898
            pshader->numMaps++;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   899
            assert(realPS != NULL);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   900
        } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   901
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   902
        ID3D11DeviceContext_PSSetShader(ctx->deviceContext, realPS, NULL, 0);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   903
        ID3D11DeviceContext_PSSetConstantBuffers(ctx->deviceContext, 0, 1,
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   904
                                                 &pshader->ubo);
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   905
        ctx->pixelNeedsBound = 0;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   906
    } // if
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   907
} // MOJOSHADER_d3d11ProgramReady
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   908
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   909
const char *MOJOSHADER_d3d11GetError(void)
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   910
{
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   911
    return error_buffer;
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   912
} // MOJOSHADER_d3d11GetError
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   913
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   914
#endif /* SUPPORT_PROFILE_HLSL */
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   915
0135d797e287 Implement HLSL emitter, MOJOSHADER_d3d11 API
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff changeset
   916
// end of mojoshader_d3d11.c ...