profiles/mojoshader_profile_spirv.h
author Martin Krošlák <kroslakma@gmail.com>
Thu, 23 Jul 2020 18:16:22 -0400
changeset 1299 02d36217591b
parent 1253 c84325b641cc
child 1318 ff4eb6d9c9c2
permissions -rw-r--r--
Fix SM1 shaders on SPIR-V after TEX* opcodes changes: - Fix implicit input attribute on SPIR-V profile - Remove texcoord attribs from other TEX* opcodes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
     1
/**
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
     3
 *  Direct3D shaders.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
     4
 *
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
     6
 *
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
     8
 */
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
     9
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    10
#ifndef MOJOSHADER_PROFILE_SPIRV_H
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    11
#define MOJOSHADER_PROFILE_SPIRV_H
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    12
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    13
#if SUPPORT_PROFILE_SPIRV
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    14
1253
c84325b641cc Add descriptor set assignment
Martin Krošlák <kroslakma@gmail.com>
parents: 1248
diff changeset
    15
#define MOJOSHADER_SPIRV_VS_SAMPLER_SET 0
c84325b641cc Add descriptor set assignment
Martin Krošlák <kroslakma@gmail.com>
parents: 1248
diff changeset
    16
#define MOJOSHADER_SPIRV_PS_SAMPLER_SET 1
c84325b641cc Add descriptor set assignment
Martin Krošlák <kroslakma@gmail.com>
parents: 1248
diff changeset
    17
#define MOJOSHADER_SPIRV_VS_UNIFORM_SET 2
c84325b641cc Add descriptor set assignment
Martin Krošlák <kroslakma@gmail.com>
parents: 1248
diff changeset
    18
#define MOJOSHADER_SPIRV_PS_UNIFORM_SET 3
c84325b641cc Add descriptor set assignment
Martin Krošlák <kroslakma@gmail.com>
parents: 1248
diff changeset
    19
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    20
// For baked-in constants in SPIR-V we want to store scalar values that we can
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    21
// use in composites, since OpConstantComposite uses result ids constituates
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    22
// rather than value literals.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    23
// We'll store these lists grouped by type and have the lists themselves
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    24
// ordered by value in the ctx.spirv struct.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    25
typedef struct ComponentList
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    26
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    27
    // result id from OpConstant
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    28
    uint32 id;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    29
    union {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    30
        float f;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    31
        int i;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    32
        uint32 u;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    33
    } v;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    34
    struct ComponentList *next;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    35
} ComponentList;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    36
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    37
typedef struct SpirvLoopInfo
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    38
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    39
    uint32 tid_counter;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    40
    uint32 id_counter;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    41
    uint32 id_counter_next;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    42
    uint32 id_aL;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    43
    uint32 id_label_header;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    44
    uint32 id_label_continue;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    45
    uint32 id_label_merge;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    46
} SpirvLoopInfo;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    47
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    48
typedef enum SpirvType
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    49
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    50
    ST_FLOAT = 0,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    51
    ST_SINT = 1,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    52
    ST_UINT = 2,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    53
    ST_BOOL = 3,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    54
} SpirvType;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    55
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    56
typedef enum SpirvStorageClass
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    57
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    58
    SC_INPUT = 0,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    59
    SC_OUTPUT = 1,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    60
    SC_PRIVATE = 2,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    61
    SC_UNIFORM_CONSTANT = 3,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    62
} SpirvStorageClass;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    63
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    64
/* Not all type parameter combinations are actually used, but it's all rounded up to 64 so
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    65
 * it's easier to work with.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    66
 */
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    67
typedef enum SpirvTypeIdx
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    68
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    69
    STI_VOID          = 0,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    70
    STI_FUNC_VOID     = 1,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    71
    STI_FUNC_LIT      = 2,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    72
    STI_IMAGE2D       = 3,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    73
    STI_IMAGE3D       = 4,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    74
    STI_IMAGECUBE     = 5,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    75
    STI_PTR_IMAGE2D   = 6,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    76
    STI_PTR_IMAGE3D   = 7,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    77
    STI_PTR_IMAGECUBE = 8,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    78
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    79
    // 7 unused entries
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    80
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    81
    // 4 base types * 4 vector sizes = 16 entries
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    82
    STI_FLOAT = (0 << 5) | (1 << 4) | (ST_FLOAT << 2) | 0,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    83
    STI_VEC2  = (0 << 5) | (1 << 4) | (ST_FLOAT << 2) | 1,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    84
    STI_VEC3  = (0 << 5) | (1 << 4) | (ST_FLOAT << 2) | 2,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    85
    STI_VEC4  = (0 << 5) | (1 << 4) | (ST_FLOAT << 2) | 3,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    86
    STI_INT   = (0 << 5) | (1 << 4) | (ST_SINT  << 2) | 0,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    87
    STI_IVEC2 = (0 << 5) | (1 << 4) | (ST_SINT  << 2) | 1,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    88
    STI_IVEC3 = (0 << 5) | (1 << 4) | (ST_SINT  << 2) | 2,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    89
    STI_IVEC4 = (0 << 5) | (1 << 4) | (ST_SINT  << 2) | 3,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    90
    STI_UINT  = (0 << 5) | (1 << 4) | (ST_UINT  << 2) | 0,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    91
    STI_UVEC2 = (0 << 5) | (1 << 4) | (ST_UINT  << 2) | 1,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    92
    STI_UVEC3 = (0 << 5) | (1 << 4) | (ST_UINT  << 2) | 2,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    93
    STI_UVEC4 = (0 << 5) | (1 << 4) | (ST_UINT  << 2) | 3,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    94
    STI_BOOL  = (0 << 5) | (1 << 4) | (ST_BOOL  << 2) | 0,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    95
    STI_BVEC2 = (0 << 5) | (1 << 4) | (ST_BOOL  << 2) | 1,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    96
    STI_BVEC3 = (0 << 5) | (1 << 4) | (ST_BOOL  << 2) | 2,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    97
    STI_BVEC4 = (0 << 5) | (1 << 4) | (ST_BOOL  << 2) | 3,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    98
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
    99
    // 2 dims (vec4 + scalar) * 4 base types * 4 storage classes
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   100
    STI_PTR_FLOAT_I = (1 << 5) | (0 << 4) | (ST_FLOAT << 2) | SC_INPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   101
    STI_PTR_FLOAT_O = (1 << 5) | (0 << 4) | (ST_FLOAT << 2) | SC_OUTPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   102
    STI_PTR_FLOAT_P = (1 << 5) | (0 << 4) | (ST_FLOAT << 2) | SC_PRIVATE,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   103
    STI_PTR_FLOAT_U = (1 << 5) | (0 << 4) | (ST_FLOAT << 2) | SC_UNIFORM_CONSTANT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   104
    STI_PTR_INT_I   = (1 << 5) | (0 << 4) | (ST_SINT  << 2) | SC_INPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   105
    STI_PTR_INT_O   = (1 << 5) | (0 << 4) | (ST_SINT  << 2) | SC_OUTPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   106
    STI_PTR_INT_P   = (1 << 5) | (0 << 4) | (ST_SINT  << 2) | SC_PRIVATE,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   107
    STI_PTR_INT_U   = (1 << 5) | (0 << 4) | (ST_SINT  << 2) | SC_UNIFORM_CONSTANT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   108
    STI_PTR_UINT_I  = (1 << 5) | (0 << 4) | (ST_UINT  << 2) | SC_INPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   109
    STI_PTR_UINT_O  = (1 << 5) | (0 << 4) | (ST_UINT  << 2) | SC_OUTPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   110
    STI_PTR_UINT_P  = (1 << 5) | (0 << 4) | (ST_UINT  << 2) | SC_PRIVATE,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   111
    STI_PTR_UINT_U  = (1 << 5) | (0 << 4) | (ST_UINT  << 2) | SC_UNIFORM_CONSTANT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   112
    STI_PTR_BOOL_I  = (1 << 5) | (0 << 4) | (ST_BOOL  << 2) | SC_INPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   113
    STI_PTR_BOOL_O  = (1 << 5) | (0 << 4) | (ST_BOOL  << 2) | SC_OUTPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   114
    STI_PTR_BOOL_P  = (1 << 5) | (0 << 4) | (ST_BOOL  << 2) | SC_PRIVATE,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   115
    STI_PTR_BOOL_U  = (1 << 5) | (0 << 4) | (ST_BOOL  << 2) | SC_UNIFORM_CONSTANT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   116
    STI_PTR_VEC4_I  = (1 << 5) | (1 << 4) | (ST_FLOAT << 2) | SC_INPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   117
    STI_PTR_VEC4_O  = (1 << 5) | (1 << 4) | (ST_FLOAT << 2) | SC_OUTPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   118
    STI_PTR_VEC4_P  = (1 << 5) | (1 << 4) | (ST_FLOAT << 2) | SC_PRIVATE,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   119
    STI_PTR_VEC4_U  = (1 << 5) | (1 << 4) | (ST_FLOAT << 2) | SC_UNIFORM_CONSTANT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   120
    STI_PTR_IVEC4_I = (1 << 5) | (1 << 4) | (ST_SINT  << 2) | SC_INPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   121
    STI_PTR_IVEC4_O = (1 << 5) | (1 << 4) | (ST_SINT  << 2) | SC_OUTPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   122
    STI_PTR_IVEC4_P = (1 << 5) | (1 << 4) | (ST_SINT  << 2) | SC_PRIVATE,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   123
    STI_PTR_IVEC4_U = (1 << 5) | (1 << 4) | (ST_SINT  << 2) | SC_UNIFORM_CONSTANT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   124
    STI_PTR_UVEC4_I = (1 << 5) | (1 << 4) | (ST_UINT  << 2) | SC_INPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   125
    STI_PTR_UVEC4_O = (1 << 5) | (1 << 4) | (ST_UINT  << 2) | SC_OUTPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   126
    STI_PTR_UVEC4_P = (1 << 5) | (1 << 4) | (ST_UINT  << 2) | SC_PRIVATE,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   127
    STI_PTR_UVEC4_U = (1 << 5) | (1 << 4) | (ST_UINT  << 2) | SC_UNIFORM_CONSTANT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   128
    STI_PTR_BVEC4_I = (1 << 5) | (1 << 4) | (ST_BOOL  << 2) | SC_INPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   129
    STI_PTR_BVEC4_O = (1 << 5) | (1 << 4) | (ST_BOOL  << 2) | SC_OUTPUT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   130
    STI_PTR_BVEC4_P = (1 << 5) | (1 << 4) | (ST_BOOL  << 2) | SC_PRIVATE,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   131
    STI_PTR_BVEC4_U = (1 << 5) | (1 << 4) | (ST_BOOL  << 2) | SC_UNIFORM_CONSTANT,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   132
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   133
    // 2 + 6 + 16 + 32 = 56 entries (+ 8 unused)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   134
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   135
    // Helpers
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   136
    STI_LENGTH_,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   137
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   138
    STI_MISC_START_ = 0,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   139
    STI_MISC_END_   = 8,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   140
    STI_CORE_START_ = (0 << 5) | (1 << 4),
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   141
    STI_PTR_START_  = (1 << 5) | (0 << 4),
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   142
    STI_CORE_END_   = STI_PTR_START_,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   143
    STI_PTR_END_    = STI_LENGTH_,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   144
} SpirvTypeIdx;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   145
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   146
// In addition to result ID we also need type ID (can't assume everything is vec4).
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   147
typedef struct SpirvResult
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   148
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   149
    uint32 tid;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   150
    uint32 id;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   151
} SpirvResult;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   152
1248
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   153
// This doesn't 100% correspond to glslangValidator semantics. It just says which mode to use at
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   154
// runtime (different from compile-time support being enabled). Technically, we could generate the
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   155
// same for both, but that would require GL code to use UBOs.
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   156
typedef enum SpirvMode
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   157
{
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   158
    SPIRV_MODE_GL,
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   159
    SPIRV_MODE_VK,
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   160
} SpirvMode;
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   161
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   162
typedef struct SpirvContext
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   163
{
1225
50b8dd7e0b1a Add GLSPIRV profile, to allow for both GL- and VK-friendly SPIR-V output
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1224
diff changeset
   164
#if SUPPORT_PROFILE_GLSPIRV
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   165
    uint32 id_vs_main_end;
1225
50b8dd7e0b1a Add GLSPIRV profile, to allow for both GL- and VK-friendly SPIR-V output
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1224
diff changeset
   166
#endif // SUPPORT_PROFILE_GLSPIRV
1248
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   167
    SpirvMode mode;
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   168
    // ext. glsl instructions have been imported
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   169
    uint32 idext;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   170
    uint32 idmax;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   171
    uint32 idmain;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   172
    uint32 id_func_lit;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   173
    uint32 inoutcount;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   174
    uint32 id_var_fragcoord;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   175
    uint32 id_var_vpos;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   176
    uint32 id_var_frontfacing;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   177
    uint32 id_var_vface;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   178
    // ids for types so we can reuse them after they're declared
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   179
    uint32 tid[STI_LENGTH_];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   180
    uint32 idtrue;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   181
    uint32 idfalse;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   182
    uint32 id_0_0[4];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   183
    uint32 id_0_125[4];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   184
    uint32 id_0_25[4];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   185
    uint32 id_0_5[4];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   186
    uint32 id_1_0[4];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   187
    uint32 id_2_0[4];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   188
    uint32 id_4_0[4];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   189
    uint32 id_8_0[4];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   190
    uint32 id_flt_max[4];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   191
    struct {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   192
        uint32 idvec4;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   193
        uint32 idivec4;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   194
        uint32 idbool;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   195
    } uniform_arrays;
1248
2f03f0b5ded3 Add uniform block support for vulkan
Martin Krošlák <kroslakma@gmail.com>
parents: 1225
diff changeset
   196
    uint32 id_uniform_block;
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   197
    struct {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   198
        uint32 idvec4;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   199
    } constant_arrays;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   200
    struct {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   201
        ComponentList f;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   202
        ComponentList i;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   203
        ComponentList u;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   204
    } cl;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   205
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   206
    SpirvPatchTable patch_table;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   207
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   208
    // Required only on ps_1_3 and below, which only has 4 registers for this purpose.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   209
    struct {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   210
        uint32 idtexbem;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   211
        uint32 idtexbeml;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   212
    } sampler_extras[4];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   213
1299
02d36217591b Fix SM1 shaders on SPIR-V after TEX* opcodes changes:
Martin Krošlák <kroslakma@gmail.com>
parents: 1253
diff changeset
   214
    // TEX opcode in ps_1_3 and below has one implicit texcoord input attribute for each texture
02d36217591b Fix SM1 shaders on SPIR-V after TEX* opcodes changes:
Martin Krošlák <kroslakma@gmail.com>
parents: 1253
diff changeset
   215
    // register. We use this array to hold SSA id of this input attribute (see emit_SPIRV_global
02d36217591b Fix SM1 shaders on SPIR-V after TEX* opcodes changes:
Martin Krošlák <kroslakma@gmail.com>
parents: 1253
diff changeset
   216
    // for details).
02d36217591b Fix SM1 shaders on SPIR-V after TEX* opcodes changes:
Martin Krošlák <kroslakma@gmail.com>
parents: 1253
diff changeset
   217
    uint32 id_implicit_input[4];
02d36217591b Fix SM1 shaders on SPIR-V after TEX* opcodes changes:
Martin Krošlák <kroslakma@gmail.com>
parents: 1253
diff changeset
   218
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   219
    int loop_stack_idx;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   220
    SpirvLoopInfo loop_stack[32];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   221
} SpirvContext;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   222
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   223
#endif // if SUPPORT_PROFILE_SPIRV
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents:
diff changeset
   225
#endif