mojoshader_vulkan.c
author Ethan Lee <flibitijibibo@flibitijibibo.com>
Sun, 30 Aug 2020 21:35:31 -0400
changeset 1303 7a43f238f28a
parent 1302 974d2cc3558e
child 1304 d68910d81b6e
permissions -rw-r--r--
spirv: Separate pixel shader outputs from inputs, fix linker accordingly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     1
/**
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     3
 *  Direct3D shaders.
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     4
 *
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     6
 *
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     8
 */
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     9
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    10
#define __MOJOSHADER_INTERNAL__ 1
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    11
#include "mojoshader_internal.h"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    12
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    13
#if SUPPORT_PROFILE_SPIRV
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    14
1272
cddbd25553fe Fix include path for vulkan.h
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1271
diff changeset
    15
#include "vulkan/vulkan.h"
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    16
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    17
#define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    18
    typedef ret (VKAPI_CALL *vkfntype_MOJOSHADER_##func) params;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    19
#define VULKAN_DEVICE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    20
	typedef ret (VKAPI_CALL *vkfntype_MOJOSHADER_##func) params;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    21
#include "mojoshader_vulkan_vkfuncs.h"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    22
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    23
#define UBO_BUFFER_SIZE 8000000 // 8MB
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    24
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    25
// Internal struct defs...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    26
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    27
typedef struct MOJOSHADER_vkShader
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    28
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    29
    const MOJOSHADER_parseData *parseData;
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
    30
    uint16_t tag;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    31
    uint32_t refcount;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    32
} MOJOSHADER_vkShader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    33
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
    34
typedef struct MOJOSHADER_vkProgram
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
    35
{
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
    36
    VkShaderModule vertexModule;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
    37
    VkShaderModule pixelModule;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
    38
    MOJOSHADER_vkShader *vertexShader;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
    39
    MOJOSHADER_vkShader *pixelShader;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
    40
} MOJOSHADER_vkProgram;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
    41
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    42
typedef struct MOJOSHADER_vkUniformBuffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    43
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    44
    VkBuffer buffer;
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    45
    VkDeviceMemory deviceMemory;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    46
    VkDeviceSize bufferSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    47
    VkDeviceSize dynamicOffset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    48
    VkDeviceSize currentBlockSize;
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    49
    uint8_t *mapPointer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    50
} MOJOSHADER_vkUniformBuffer;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    51
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    52
// Error state...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    53
static char error_buffer[1024] = { '\0' };
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    54
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    55
static void set_error(const char *str)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    56
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    57
    snprintf(error_buffer, sizeof (error_buffer), "%s", str);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    58
} // set_error
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    59
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    60
static inline void out_of_memory(void)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    61
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    62
    set_error("out of memory");
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    63
} // out_of_memory
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    64
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    65
// Max entries for each register file type
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    66
#define MAX_REG_FILE_F 8192
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    67
#define MAX_REG_FILE_I 2047
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    68
#define MAX_REG_FILE_B 2047
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    69
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    70
typedef struct MOJOSHADER_vkContext
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    71
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    72
    VkInstance *instance;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    73
    VkPhysicalDevice *physical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    74
    VkDevice *logical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    75
    PFN_vkGetInstanceProcAddr instance_proc_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    76
    PFN_vkGetDeviceProcAddr device_proc_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    77
    uint32_t graphics_queue_family_index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    78
    uint32_t maxUniformBufferRange;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    79
    uint32_t minUniformBufferOffsetAlignment;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    80
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    81
    int32_t frames_in_flight;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    82
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    83
    MOJOSHADER_malloc malloc_fn;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    84
    MOJOSHADER_free free_fn;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    85
    void *malloc_data;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    86
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    87
    // The constant register files...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    88
    // !!! FIXME: Man, it kills me how much memory this takes...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    89
    // !!! FIXME:  ... make this dynamically allocated on demand.
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    90
    float vs_reg_file_f[MAX_REG_FILE_F * 4];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    91
    int32_t vs_reg_file_i[MAX_REG_FILE_I * 4];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    92
    uint8_t vs_reg_file_b[MAX_REG_FILE_B * 4];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    93
    float ps_reg_file_f[MAX_REG_FILE_F * 4];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    94
    int32_t ps_reg_file_i[MAX_REG_FILE_I * 4];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    95
    uint8_t ps_reg_file_b[MAX_REG_FILE_B * 4];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    96
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    97
    MOJOSHADER_vkUniformBuffer *vertUboBuffer;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    98
    MOJOSHADER_vkUniformBuffer *fragUboBuffer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    99
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   100
    MOJOSHADER_vkProgram *bound_program;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   101
    HashTable *linker_cache;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   102
1302
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   103
    // Note that these may not necessarily align with bound_program!
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   104
    // We need to store these so effects can have overlapping shaders.
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   105
    MOJOSHADER_vkShader *bound_vshader;
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   106
    MOJOSHADER_vkShader *bound_pshader;
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   107
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   108
    #define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   109
        vkfntype_MOJOSHADER_##func func;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   110
    #define VULKAN_DEVICE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   111
        vkfntype_MOJOSHADER_##func func;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   112
    #include "mojoshader_vulkan_vkfuncs.h"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   113
} MOJOSHADER_vkContext;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   114
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   115
static MOJOSHADER_vkContext *ctx = NULL;
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   116
static uint16_t tagCounter = 1;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   117
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   118
static uint8_t find_memory_type(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   119
    MOJOSHADER_vkContext *ctx,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   120
	uint32_t typeFilter,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   121
	VkMemoryPropertyFlags properties,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   122
	uint32_t *result
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   123
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   124
	uint32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   125
	VkPhysicalDeviceMemoryProperties memoryProperties;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   126
	ctx->vkGetPhysicalDeviceMemoryProperties(*ctx->physical_device, &memoryProperties);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   127
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   128
	for (i = 0; i < memoryProperties.memoryTypeCount; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   129
	{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   130
		if ((typeFilter & (1 << i))
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   131
		 && (memoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   132
		{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   133
			*result = i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   134
			return 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   135
		} // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   136
	} // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   137
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   138
	return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   139
} // find_memory_type
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   140
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   141
static uint32_t next_highest_offset_alignment(uint32_t offset)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   142
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   143
    return (
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   144
        (offset + ctx->minUniformBufferOffsetAlignment - 1) /
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   145
        ctx->minUniformBufferOffsetAlignment *
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   146
        ctx->minUniformBufferOffsetAlignment
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   147
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   148
} // next_highest_offset_alignment
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   149
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   150
static MOJOSHADER_vkUniformBuffer *create_ubo(MOJOSHADER_vkContext *ctx)
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   151
{
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   152
    MOJOSHADER_vkUniformBuffer *result = (MOJOSHADER_vkUniformBuffer *) ctx->malloc_fn(
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   153
        sizeof(MOJOSHADER_vkUniformBuffer),
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   154
        ctx->malloc_data
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   155
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   156
    VkBufferCreateInfo bufferCreateInfo =
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   157
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   158
        VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   159
    };
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   160
    VkMemoryRequirements memoryRequirements;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   161
    VkMemoryAllocateInfo allocateInfo =
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   162
    {
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   163
        VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   164
    };
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   165
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   166
    bufferCreateInfo.flags = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   167
    bufferCreateInfo.size = UBO_BUFFER_SIZE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   168
    bufferCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   169
    bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   170
    bufferCreateInfo.queueFamilyIndexCount = 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   171
    bufferCreateInfo.pQueueFamilyIndices = &ctx->graphics_queue_family_index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   172
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   173
    ctx->vkCreateBuffer(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   174
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   175
        &bufferCreateInfo,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   176
        NULL,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   177
        &result->buffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   178
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   179
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   180
    ctx->vkGetBufferMemoryRequirements(
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   181
        *ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   182
        result->buffer,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   183
        &memoryRequirements
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   184
    );
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   185
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   186
    allocateInfo.allocationSize = UBO_BUFFER_SIZE;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   187
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   188
    if (!find_memory_type(ctx,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   189
                          memoryRequirements.memoryTypeBits,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   190
                          VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   191
                          &allocateInfo.memoryTypeIndex))
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   192
    {
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   193
        set_error("failed to find suitable memory type for UBO memory");
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   194
        return NULL;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   195
    } // if
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   196
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   197
    ctx->vkAllocateMemory(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   198
                          &allocateInfo,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   199
                          NULL,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   200
                          &result->deviceMemory
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   201
    );
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   202
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   203
    ctx->vkBindBufferMemory(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   204
                            result->buffer,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   205
                            result->deviceMemory,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   206
                            0
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   207
    );
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   208
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   209
    ctx->vkMapMemory(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   210
                     result->deviceMemory,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   211
                     0,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   212
                     UBO_BUFFER_SIZE,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   213
                     0,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   214
                     (void**) &result->mapPointer
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   215
    );
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   216
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   217
    result->bufferSize = UBO_BUFFER_SIZE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   218
    result->currentBlockSize = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   219
    result->dynamicOffset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   220
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   221
    return result;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   222
} // create_ubo
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   223
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   224
static uint32_t uniform_data_size(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   225
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   226
    int32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   227
    int32_t buflen = 0;
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   228
    const int32_t uniformSize = 16; // Yes, even the bool registers
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   229
    for (i = 0; i < shader->parseData->uniform_count; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   230
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   231
        const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   232
        buflen += (arrayCount ? arrayCount : 1) * uniformSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   233
    } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   234
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   235
    return buflen;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   236
} // uniform_data_size
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   237
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   238
static VkBuffer get_uniform_buffer(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   239
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   240
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   241
        return VK_NULL_HANDLE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   242
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   243
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   244
        return ctx->vertUboBuffer->buffer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   245
    else
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   246
        return ctx->fragUboBuffer->buffer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   247
} // get_uniform_buffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   248
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   249
static VkDeviceSize get_uniform_offset(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   250
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   251
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   252
        return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   253
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   254
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   255
        return ctx->vertUboBuffer->dynamicOffset;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   256
    else
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   257
        return ctx->fragUboBuffer->dynamicOffset;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   258
} // get_uniform_offset
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   259
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   260
static VkDeviceSize get_uniform_size(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   261
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   262
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   263
        return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   264
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   265
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   266
        return ctx->vertUboBuffer->currentBlockSize;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   267
    else
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   268
        return ctx->fragUboBuffer->currentBlockSize;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   269
} // get_uniform_size
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   270
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   271
static void update_uniform_buffer(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   272
{
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   273
    int32_t i, j;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   274
    int32_t offset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   275
    uint8_t *contents;
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   276
    uint32_t *contentsI;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   277
    float *regF; int *regI; uint8_t *regB;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   278
    MOJOSHADER_vkUniformBuffer *ubo;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   279
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   280
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   281
        return;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   282
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   283
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   284
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   285
        regF = ctx->vs_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   286
        regI = ctx->vs_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   287
        regB = ctx->vs_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   288
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   289
        ubo = ctx->vertUboBuffer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   290
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   291
    else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   292
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   293
        regF = ctx->ps_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   294
        regI = ctx->ps_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   295
        regB = ctx->ps_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   296
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   297
        ubo = ctx->fragUboBuffer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   298
    } // else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   299
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   300
    ubo->dynamicOffset += ubo->currentBlockSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   301
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   302
    ubo->currentBlockSize = next_highest_offset_alignment(uniform_data_size(shader));
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   303
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   304
    if (ubo->dynamicOffset + ubo->currentBlockSize >= ubo->bufferSize)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   305
    {
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   306
        set_error("UBO overflow!!");
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   307
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   308
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   309
    contents = ubo->mapPointer + ubo->dynamicOffset;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   310
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   311
    offset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   312
    for (i = 0; i < shader->parseData->uniform_count; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   313
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   314
        const int32_t index = shader->parseData->uniforms[i].index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   315
        const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   316
        const int32_t size = arrayCount ? arrayCount : 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   317
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   318
        switch (shader->parseData->uniforms[i].type)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   319
        {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   320
            case MOJOSHADER_UNIFORM_FLOAT:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   321
                memcpy(
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   322
                    contents + offset,
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   323
                    &regF[4 * index],
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   324
                    size * 16
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   325
                );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   326
                break;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   327
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   328
            case MOJOSHADER_UNIFORM_INT:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   329
                memcpy(
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   330
                    contents + offset,
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   331
                    &regI[4 * index],
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   332
                    size * 16
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   333
                );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   334
                break;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   335
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   336
            case MOJOSHADER_UNIFORM_BOOL:
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   337
                contentsI = (uint32_t *) (contents + offset);
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   338
                for (j = 0; j < size; j++)
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   339
                    contentsI[j * 4] = regB[index + j];
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   340
                break;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   341
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   342
            default:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   343
                set_error(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   344
                    "SOMETHING VERY WRONG HAPPENED WHEN UPDATING UNIFORMS"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   345
                );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   346
                assert(0);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   347
                break;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   348
        } // switch
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   349
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   350
        offset += size * 16;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   351
    } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   352
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   353
} // update_uniform_buffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   354
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   355
static void lookup_entry_points(MOJOSHADER_vkContext *ctx)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   356
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   357
    #define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   358
        ctx->func = (vkfntype_MOJOSHADER_##func) ctx->instance_proc_lookup(*ctx->instance, #func);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   359
    #define VULKAN_DEVICE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   360
        ctx->func = (vkfntype_MOJOSHADER_##func) ctx->device_proc_lookup(*ctx->logical_device, #func);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   361
    #include "mojoshader_vulkan_vkfuncs.h"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   362
} // lookup_entry_points
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   363
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   364
static int shader_bytecode_len(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   365
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   366
    return shader->parseData->output_len - sizeof(SpirvPatchTable);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   367
} // shader_bytecode_len
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   368
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   369
static VkShaderModule compile_shader(MOJOSHADER_vkShader *shader)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   370
{
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   371
    VkResult result;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   372
    VkShaderModule module;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   373
    VkShaderModuleCreateInfo shaderModuleCreateInfo =
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   374
    {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   375
        VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   376
    };
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   377
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   378
    shaderModuleCreateInfo.flags = 0;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   379
    shaderModuleCreateInfo.codeSize = shader_bytecode_len(shader);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   380
    shaderModuleCreateInfo.pCode = (uint32_t*) shader->parseData->output;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   381
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   382
    result = ctx->vkCreateShaderModule(
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   383
        *ctx->logical_device,
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   384
        &shaderModuleCreateInfo,
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   385
        NULL,
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   386
        &module
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   387
    );
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   388
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   389
    if (result != VK_SUCCESS)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   390
    {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   391
        // FIXME: should display VK error code
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   392
        set_error("Error when creating VkShaderModule");
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   393
        ctx->vkDestroyShaderModule(
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   394
            *ctx->logical_device,
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   395
            module,
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   396
            NULL
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   397
        );
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   398
        return VK_NULL_HANDLE;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   399
    } // if
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   400
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   401
    return module;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   402
} // compile_shader
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   403
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   404
typedef struct
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   405
{
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   406
    MOJOSHADER_vkShader *vertex;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   407
    MOJOSHADER_vkShader *fragment;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   408
} BoundShaders;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   409
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   410
static uint32_t hash_shaders(const void *sym, void *data)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   411
{
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   412
    (void) data;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   413
    const BoundShaders *s = (const BoundShaders *) sym;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   414
    const uint16_t v = (s->vertex) ? s->vertex->tag : 0;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   415
    const uint16_t f = (s->fragment) ? s->fragment->tag : 0;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   416
    return ((uint32_t) v << 16) | (uint32_t) f;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   417
} // hash_shaders
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   418
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   419
static int match_shaders(const void *_a, const void *_b, void *data)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   420
{
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   421
    (void) data;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   422
    const BoundShaders *a = (const BoundShaders *) _a;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   423
    const BoundShaders *b = (const BoundShaders *) _b;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   424
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   425
    const uint16_t av = (a->vertex) ? a->vertex->tag : 0;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   426
    const uint16_t bv = (b->vertex) ? b->vertex->tag : 0;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   427
    if (av != bv)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   428
        return 0;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   429
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   430
    const uint16_t af = (a->fragment) ? a->fragment->tag : 0;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   431
    const uint16_t bf = (b->fragment) ? b->fragment->tag : 0;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   432
    if (af != bf)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   433
        return 0;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   434
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   435
    return 1;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   436
} // match_shaders
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   437
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   438
static void nuke_shaders(const void *key, const void *value, void *data)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   439
{
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   440
    (void) data;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   441
    ctx->free_fn((void *) key, ctx->malloc_data); // this was a BoundShaders struct.
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   442
    MOJOSHADER_vkDeleteProgram((MOJOSHADER_vkProgram *) value);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   443
} // nuke_shaders
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   444
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   445
// Public API
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   446
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   447
MOJOSHADER_vkContext *MOJOSHADER_vkCreateContext(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   448
    VkInstance *instance,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   449
    VkPhysicalDevice *physical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   450
    VkDevice *logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   451
    int frames_in_flight,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   452
    PFN_MOJOSHADER_vkGetInstanceProcAddr instance_lookup,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   453
    PFN_MOJOSHADER_vkGetDeviceProcAddr device_lookup,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   454
    unsigned int graphics_queue_family_index,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   455
    unsigned int max_uniform_buffer_range,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   456
    unsigned int min_uniform_buffer_offset_alignment,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   457
    MOJOSHADER_malloc m, MOJOSHADER_free f,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   458
    void *malloc_d
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   459
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   460
    MOJOSHADER_vkContext* resultCtx;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   461
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   462
    if (m == NULL) m = MOJOSHADER_internal_malloc;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   463
    if (f == NULL) f = MOJOSHADER_internal_free;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   464
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   465
    resultCtx = (MOJOSHADER_vkContext *) m(sizeof(MOJOSHADER_vkContext), malloc_d);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   466
    if (resultCtx == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   467
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   468
        out_of_memory();
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   469
        goto init_fail;
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   470
    } // if
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   471
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   472
    memset(resultCtx, '\0', sizeof(MOJOSHADER_vkContext));
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   473
    resultCtx->malloc_fn = m;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   474
    resultCtx->free_fn = f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   475
    resultCtx->malloc_data = malloc_d;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   476
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   477
    resultCtx->instance = (VkInstance*) instance;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   478
    resultCtx->physical_device = (VkPhysicalDevice*) physical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   479
    resultCtx->logical_device = (VkDevice*) logical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   480
    resultCtx->instance_proc_lookup = (PFN_vkGetInstanceProcAddr) instance_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   481
    resultCtx->device_proc_lookup = (PFN_vkGetDeviceProcAddr) device_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   482
    resultCtx->frames_in_flight = frames_in_flight;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   483
    resultCtx->graphics_queue_family_index = graphics_queue_family_index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   484
    resultCtx->maxUniformBufferRange = max_uniform_buffer_range;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   485
    resultCtx->minUniformBufferOffsetAlignment = min_uniform_buffer_offset_alignment;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   486
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   487
    lookup_entry_points(resultCtx);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   488
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   489
    resultCtx->vertUboBuffer = create_ubo(resultCtx);
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   490
    resultCtx->fragUboBuffer = create_ubo(resultCtx);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   491
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   492
    return resultCtx;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   493
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   494
init_fail:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   495
    if (resultCtx != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   496
        f(resultCtx, malloc_d);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   497
    return NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   498
} // MOJOSHADER_vkCreateContext
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   499
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   500
void MOJOSHADER_vkMakeContextCurrent(MOJOSHADER_vkContext *_ctx)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   501
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   502
    ctx = _ctx;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   503
} // MOJOSHADER_vkMakeContextCurrent
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   504
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   505
void MOJOSHADER_vkDestroyContext(MOJOSHADER_vkContext *_ctx)
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   506
{
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   507
    MOJOSHADER_vkContext *current_ctx = ctx;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   508
    ctx = _ctx;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   509
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   510
    MOJOSHADER_vkBindProgram(NULL);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   511
    if (ctx->linker_cache)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   512
        hash_destroy(ctx->linker_cache);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   513
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   514
    ctx->vkDestroyBuffer(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   515
                         ctx->vertUboBuffer->buffer,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   516
                         NULL);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   517
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   518
    ctx->vkDestroyBuffer(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   519
                         ctx->fragUboBuffer->buffer,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   520
                         NULL);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   521
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   522
    ctx->vkFreeMemory(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   523
                      ctx->vertUboBuffer->deviceMemory,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   524
                      NULL);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   525
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   526
    ctx->vkFreeMemory(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   527
                      ctx->fragUboBuffer->deviceMemory,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   528
                      NULL);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   529
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   530
    ctx->free_fn(ctx->vertUboBuffer, ctx->malloc_data);
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   531
    ctx->free_fn(ctx->fragUboBuffer, ctx->malloc_data);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   532
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   533
    ctx->free_fn(ctx, ctx->malloc_data);
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   534
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   535
    ctx = ((current_ctx == _ctx) ? NULL : current_ctx);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   536
} // MOJOSHADER_vkDestroyContext
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   537
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   538
MOJOSHADER_vkShader *MOJOSHADER_vkCompileShader(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   539
    const char *mainfn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   540
    const unsigned char *tokenbuf,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   541
    const unsigned int bufsize,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   542
    const MOJOSHADER_swizzle *swiz,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   543
    const unsigned int swizcount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   544
    const MOJOSHADER_samplerMap *smap,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   545
    const unsigned int smapcount
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   546
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   547
    MOJOSHADER_vkShader *shader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   548
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   549
    const MOJOSHADER_parseData *pd = MOJOSHADER_parse(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   550
        "spirv", mainfn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   551
        tokenbuf, bufsize,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   552
        swiz, swizcount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   553
        smap, smapcount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   554
        ctx->malloc_fn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   555
        ctx->free_fn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   556
        ctx->malloc_data
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   557
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   558
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   559
    if (pd->error_count > 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   560
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   561
        set_error(pd->errors[0].error);
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   562
        goto parse_shader_fail;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   563
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   564
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   565
    shader = (MOJOSHADER_vkShader *) ctx->malloc_fn(sizeof(MOJOSHADER_vkShader), ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   566
    if (shader == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   567
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   568
        out_of_memory();
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   569
        goto parse_shader_fail;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   570
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   571
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   572
    shader->parseData = pd;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   573
    shader->refcount = 1;
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   574
    shader->tag = tagCounter++;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   575
    return shader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   576
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   577
parse_shader_fail:
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   578
    MOJOSHADER_freeParseData(pd);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   579
    if (shader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   580
        ctx->free_fn(shader, ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   581
    return NULL;
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   582
} // MOJOSHADER_vkCompileShader
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   583
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   584
void MOJOSHADER_vkShaderAddRef(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   585
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   586
    if (shader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   587
        shader->refcount++;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   588
} // MOJOShader_vkShaderAddRef
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   589
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   590
void MOJOSHADER_vkDeleteShader(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   591
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   592
    if (shader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   593
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   594
        if (shader->refcount > 1)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   595
            shader->refcount--;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   596
        else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   597
        {
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   598
            // See if this was bound as an unlinked program anywhere...
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   599
            if (ctx->linker_cache)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   600
            {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   601
                const void *key = NULL;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   602
                void *iter = NULL;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   603
                int morekeys = hash_iter_keys(ctx->linker_cache, &key, &iter);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   604
                while (morekeys)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   605
                {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   606
                    const BoundShaders *shaders = (const BoundShaders *) key;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   607
                    // Do this here so we don't confuse the iteration by removing...
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   608
                    morekeys = hash_iter_keys(ctx->linker_cache, &key, &iter);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   609
                    if ((shaders->vertex == shader) || (shaders->fragment == shader))
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   610
                    {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   611
                        // Deletes the linked program
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   612
                        hash_remove(ctx->linker_cache, shaders);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   613
                    } // if
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   614
                } // while
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   615
            } // if
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   616
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   617
            MOJOSHADER_freeParseData(shader->parseData);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   618
            ctx->free_fn(shader, ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   619
        } // else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   620
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   621
} // MOJOSHADER_vkDeleteShader
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   622
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   623
const MOJOSHADER_parseData *MOJOSHADER_vkGetShaderParseData(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   624
    MOJOSHADER_vkShader *shader
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   625
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   626
    return (shader != NULL) ? shader->parseData : NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   627
} // MOJOSHADER_vkGetShaderParseData
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   628
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   629
void MOJOSHADER_vkDeleteProgram(MOJOSHADER_vkProgram *p)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   630
{
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   631
    if (p->vertexModule != VK_NULL_HANDLE)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   632
        ctx->vkDestroyShaderModule(*ctx->logical_device, p->vertexModule, NULL);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   633
    if (p->pixelModule != VK_NULL_HANDLE)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   634
        ctx->vkDestroyShaderModule(*ctx->logical_device, p->pixelModule, NULL);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   635
    ctx->free_fn(p, ctx->malloc_data);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   636
} // MOJOSHADER_vkDeleteProgram
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   637
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   638
MOJOSHADER_vkProgram *MOJOSHADER_vkLinkProgram(MOJOSHADER_vkShader *vshader,
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   639
                                               MOJOSHADER_vkShader *pshader)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   640
{
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   641
    MOJOSHADER_vkProgram *result;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   642
1302
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   643
    if ((vshader == NULL) || (pshader == NULL)) // Both shaders MUST exist!
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   644
        return NULL;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   645
1280
d2a0d76469f9 VS2010 buildfix
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1277
diff changeset
   646
    result = (MOJOSHADER_vkProgram *) ctx->malloc_fn(sizeof (MOJOSHADER_vkProgram),
d2a0d76469f9 VS2010 buildfix
Caleb Cornett <caleb.cornett@outlook.com>
parents: 1277
diff changeset
   647
                                                     ctx->malloc_data);
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   648
    if (result == NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   649
    {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   650
        out_of_memory();
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   651
        return NULL;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   652
    } // if
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   653
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   654
    MOJOSHADER_spirv_link_attributes(vshader->parseData, pshader->parseData);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   655
    result->vertexModule = compile_shader(vshader);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   656
    result->pixelModule = compile_shader(pshader);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   657
    result->vertexShader = vshader;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   658
    result->pixelShader = pshader;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   659
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   660
    if (result->vertexModule == VK_NULL_HANDLE
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   661
     || result->pixelModule == VK_NULL_HANDLE)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   662
    {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   663
        MOJOSHADER_vkDeleteProgram(result);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   664
        return NULL;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   665
    }
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   666
    return result;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   667
} // MOJOSHADER_vkLinkProgram
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   668
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   669
void MOJOSHADER_vkBindProgram(MOJOSHADER_vkProgram *p)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   670
{
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   671
    ctx->bound_program = p;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   672
} // MOJOSHADER_vkBindProgram
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   673
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   674
void MOJOSHADER_vkBindShaders(MOJOSHADER_vkShader *vshader,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   675
                              MOJOSHADER_vkShader *pshader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   676
{
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   677
    if (ctx->linker_cache == NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   678
    {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   679
        ctx->linker_cache = hash_create(NULL, hash_shaders, match_shaders,
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   680
                                        nuke_shaders, 0, ctx->malloc_fn,
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   681
                                        ctx->free_fn, ctx->malloc_data);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   682
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   683
        if (ctx->linker_cache == NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   684
        {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   685
            out_of_memory();
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   686
            return;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   687
        } // if
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   688
    } // if
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   689
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   690
    MOJOSHADER_vkProgram *program = NULL;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   691
    BoundShaders shaders;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   692
    shaders.vertex = vshader;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   693
    shaders.fragment = pshader;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   694
1302
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   695
    ctx->bound_vshader = vshader;
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   696
    ctx->bound_pshader = pshader;
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   697
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   698
    const void *val = NULL;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   699
    if (hash_find(ctx->linker_cache, &shaders, &val))
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   700
        program = (MOJOSHADER_vkProgram *) val;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   701
    else
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   702
    {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   703
        program = MOJOSHADER_vkLinkProgram(vshader, pshader);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   704
        if (program == NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   705
            return;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   706
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   707
        BoundShaders *item = (BoundShaders *) ctx->malloc_fn(sizeof (BoundShaders),
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   708
                                                             ctx->malloc_data);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   709
        if (item == NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   710
        {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   711
            MOJOSHADER_vkDeleteProgram(program);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   712
            return;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   713
        } // if
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   714
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   715
        memcpy(item, &shaders, sizeof (BoundShaders));
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   716
        if (hash_insert(ctx->linker_cache, item, program) != 1)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   717
        {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   718
            ctx->free_fn(item, ctx->malloc_data);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   719
            MOJOSHADER_vkDeleteProgram(program);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   720
            out_of_memory();
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   721
            return;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   722
        } // if
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   723
    } // else
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   724
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   725
    assert(program != NULL);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   726
    ctx->bound_program = program;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   727
} // MOJOSHADER_vkBindShaders
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   728
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   729
void MOJOSHADER_vkGetBoundShaders(MOJOSHADER_vkShader **vshader,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   730
                                  MOJOSHADER_vkShader **pshader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   731
{
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   732
    if (vshader != NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   733
    {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   734
        if (ctx->bound_program != NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   735
            *vshader = ctx->bound_program->vertexShader;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   736
        else
1302
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   737
            *vshader = ctx->bound_vshader; // In case a pshader isn't set yet
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   738
    } // if
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   739
    if (pshader != NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   740
    {
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   741
        if (ctx->bound_program != NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   742
            *pshader = ctx->bound_program->pixelShader;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   743
        else
1302
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   744
            *pshader = ctx->bound_pshader; // In case a vshader isn't set yet
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   745
    } // if
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   746
} // MOJOSHADER_vkGetBoundShaders
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   747
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   748
void MOJOSHADER_vkMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   749
                                         float **psf, int **psi, unsigned char **psb)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   750
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   751
    *vsf = ctx->vs_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   752
    *vsi = ctx->vs_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   753
    *vsb = ctx->vs_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   754
    *psf = ctx->ps_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   755
    *psi = ctx->ps_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   756
    *psb = ctx->ps_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   757
} // MOJOSHADER_vkMapUniformBufferMemory
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   758
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   759
void MOJOSHADER_vkUnmapUniformBufferMemory()
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   760
{
1302
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   761
    if (ctx->bound_program == NULL)
974d2cc3558e vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1280
diff changeset
   762
        return; // Ignore buffer updates until we have a real program linked
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   763
    update_uniform_buffer(ctx->bound_program->vertexShader);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   764
    update_uniform_buffer(ctx->bound_program->pixelShader);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   765
} // MOJOSHADER_vkUnmapUniformBufferMemory
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   766
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   767
void MOJOSHADER_vkGetUniformBuffers(VkBuffer *vbuf, unsigned long long *voff, unsigned long long *vsize,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   768
                                    VkBuffer *pbuf, unsigned long long *poff, unsigned long long *psize)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   769
{
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   770
    assert(ctx->bound_program != NULL);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   771
    *vbuf = get_uniform_buffer(ctx->bound_program->vertexShader);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   772
    *voff = get_uniform_offset(ctx->bound_program->vertexShader);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   773
    *vsize = get_uniform_size(ctx->bound_program->vertexShader);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   774
    *pbuf = get_uniform_buffer(ctx->bound_program->pixelShader);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   775
    *poff = get_uniform_offset(ctx->bound_program->pixelShader);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   776
    *psize = get_uniform_size(ctx->bound_program->pixelShader);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   777
} // MOJOSHADER_vkGetUniformBuffers
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   778
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   779
void MOJOSHADER_vkEndFrame()
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   780
{
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   781
    ctx->vertUboBuffer->dynamicOffset = 0;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   782
    ctx->vertUboBuffer->currentBlockSize = 0;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   783
    ctx->fragUboBuffer->dynamicOffset = 0;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   784
    ctx->fragUboBuffer->currentBlockSize = 0;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   785
} // MOJOSHADER_VkEndFrame
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   786
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   787
int MOJOSHADER_vkGetVertexAttribLocation(MOJOSHADER_vkShader *vert,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   788
                                         MOJOSHADER_usage usage, int index)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   789
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   790
    int32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   791
    if (vert == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   792
        return -1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   793
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   794
    for (i = 0; i < vert->parseData->attribute_count; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   795
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   796
        if (vert->parseData->attributes[i].usage == usage &&
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   797
            vert->parseData->attributes[i].index == index)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   798
        {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   799
            return i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   800
        } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   801
    } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   802
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   803
    // failure
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   804
    return -1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   805
} //MOJOSHADER_vkGetVertexAttribLocation
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   806
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   807
void MOJOSHADER_vkGetShaderModules(VkShaderModule *vmodule,
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   808
                                   VkShaderModule *pmodule)
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   809
{
1277
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   810
    assert(ctx->bound_program != NULL);
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   811
    if (vmodule != NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   812
        *vmodule = ctx->bound_program->vertexModule;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   813
    if (pmodule != NULL)
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   814
        *pmodule = ctx->bound_program->pixelModule;
da61410edbc9 Add dynamic linking support for SPIR-V modules
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1276
diff changeset
   815
} //MOJOSHADER_vkGetShaderModules
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   816
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   817
const char *MOJOSHADER_vkGetError(void)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   818
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   819
    return error_buffer;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   820
} // MOJOSHADER_vkGetError
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   821
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   822
#endif /* SUPPORT_PROFILE_SPIRV */
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   823
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   824
// end of mojoshader_vulkan.c ...