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