mojoshader_vulkan.c
author Evan Hemsley <evan@moonside.games>
Wed, 01 Jul 2020 04:29:09 -0400
changeset 1271 5a67d082c55f
child 1272 cddbd25553fe
permissions -rw-r--r--
Add support for Vulkan rendering. Co-authors include: - Caleb Cornett <caleb.cornett@outlook.com> - Martin Krošlák <kroslakma@gmail.com> - Ethan Lee <flibitijibibo@flibitijibibo.com>
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
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    15
#include "vulkan.h"
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
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    23
#define UBO_BUFFER_COUNT 8
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    24
#define UBO_BUFFER_SIZE 1048576 /* ~1MB */
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
    VkShaderModule shaderModule;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    31
    const MOJOSHADER_parseData *parseData;
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
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    35
typedef struct MOJOSHADER_vkUniformBuffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    36
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    37
    VkBuffer buffer;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    38
    VkDeviceSize bufferSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    39
    VkDeviceSize memoryOffset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    40
    VkDeviceSize dynamicOffset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    41
    VkDeviceSize currentBlockSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    42
    int32_t full; // Records frame on which it became full, -1 if not full
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    43
} 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
// Error state...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    46
static char error_buffer[1024] = { '\0' };
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    47
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    48
static void set_error(const char *str)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    49
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    50
    snprintf(error_buffer, sizeof (error_buffer), "%s", str);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    51
} // set_error
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    52
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    53
static inline void out_of_memory(void)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    54
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    55
    set_error("out of memory");
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    56
} // out_of_memory
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    57
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    58
/* Max entries for each register file type */
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    59
#define MAX_REG_FILE_F 8192
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    60
#define MAX_REG_FILE_I 2047
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    61
#define MAX_REG_FILE_B 2047
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    62
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    63
typedef struct MOJOSHADER_vkContext
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    64
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    65
    VkInstance *instance;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    66
    VkPhysicalDevice *physical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    67
    VkDevice *logical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    68
    PFN_vkGetInstanceProcAddr instance_proc_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    69
    PFN_vkGetDeviceProcAddr device_proc_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    70
    uint32_t graphics_queue_family_index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    71
    uint32_t maxUniformBufferRange;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    72
    uint32_t minUniformBufferOffsetAlignment;
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
    int32_t frames_in_flight;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    75
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    76
    MOJOSHADER_malloc malloc_fn;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    77
    MOJOSHADER_free free_fn;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    78
    void *malloc_data;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    79
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    80
    // The constant register files...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    81
    // !!! FIXME: Man, it kills me how much memory this takes...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    82
    // !!! FIXME:  ... make this dynamically allocated on demand.
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    83
    float vs_reg_file_f[MAX_REG_FILE_F * 4];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    84
    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
    85
    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
    86
    float ps_reg_file_f[MAX_REG_FILE_F * 4];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    87
    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
    88
    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
    89
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    90
    VkDeviceMemory vertUboMemory;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    91
    MOJOSHADER_vkUniformBuffer **vertUboBuffers;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    92
    uint32_t vertUboCurrentIndex;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    93
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    94
    VkDeviceMemory fragUboMemory;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    95
    MOJOSHADER_vkUniformBuffer **fragUboBuffers;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    96
    uint32_t fragUboCurrentIndex;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    97
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    98
    uint32_t uboBufferCount;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    99
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   100
    MOJOSHADER_vkShader *vertexShader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   101
    MOJOSHADER_vkShader *pixelShader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   102
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   103
    uint32_t currentFrame;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   104
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   105
    #define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   106
        vkfntype_MOJOSHADER_##func func;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   107
    #define VULKAN_DEVICE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   108
        vkfntype_MOJOSHADER_##func func;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   109
    #include "mojoshader_vulkan_vkfuncs.h"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   110
} MOJOSHADER_vkContext;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   111
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   112
static MOJOSHADER_vkContext *ctx = NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   113
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   114
static uint8_t find_memory_type(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   115
    MOJOSHADER_vkContext *ctx,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   116
	uint32_t typeFilter,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   117
	VkMemoryPropertyFlags properties,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   118
	uint32_t *result
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
	uint32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   121
	VkPhysicalDeviceMemoryProperties memoryProperties;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   122
	ctx->vkGetPhysicalDeviceMemoryProperties(*ctx->physical_device, &memoryProperties);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   123
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   124
	for (i = 0; i < memoryProperties.memoryTypeCount; i++)
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
		if ((typeFilter & (1 << i))
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   127
		 && (memoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   128
		{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   129
			*result = i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   130
			return 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   131
		} // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   132
	} // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   133
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   134
	return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   135
} // find_memory_type
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   136
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   137
static uint32_t next_highest_offset_alignment(uint32_t offset)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   138
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   139
    return (
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   140
        (offset + ctx->minUniformBufferOffsetAlignment - 1) /
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   141
        ctx->minUniformBufferOffsetAlignment *
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   142
        ctx->minUniformBufferOffsetAlignment
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   143
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   144
} // next_highest_offset_alignment
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   145
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   146
static MOJOSHADER_vkUniformBuffer *create_ubo(MOJOSHADER_vkContext *ctx,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   147
                                              MOJOSHADER_malloc m,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   148
                                              void *d
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
    MOJOSHADER_vkUniformBuffer *result = (MOJOSHADER_vkUniformBuffer *) m(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   151
        sizeof(MOJOSHADER_vkUniformBuffer),
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   152
        d
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   153
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   154
    VkBufferCreateInfo bufferCreateInfo =
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   155
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   156
        VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
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
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   159
    bufferCreateInfo.flags = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   160
    bufferCreateInfo.size = UBO_BUFFER_SIZE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   161
    bufferCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   162
    bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   163
    bufferCreateInfo.queueFamilyIndexCount = 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   164
    bufferCreateInfo.pQueueFamilyIndices = &ctx->graphics_queue_family_index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   165
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   166
    ctx->vkCreateBuffer(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   167
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   168
        &bufferCreateInfo,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   169
        NULL,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   170
        &result->buffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   171
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   172
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   173
    result->bufferSize = UBO_BUFFER_SIZE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   174
    result->currentBlockSize = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   175
    result->dynamicOffset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   176
    result->full = -1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   177
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   178
    return result;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   179
} // create_ubo
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
static uint32_t uniform_data_size(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   182
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   183
    int32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   184
    int32_t uniformSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   185
    int32_t buflen = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   186
    for (i = 0; i < shader->parseData->uniform_count; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   187
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   188
        const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   189
        uniformSize = 16;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   190
        if (shader->parseData->uniforms[i].type == MOJOSHADER_UNIFORM_BOOL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   191
            uniformSize = 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   192
        buflen += (arrayCount ? arrayCount : 1) * uniformSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   193
    } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   194
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   195
    return buflen;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   196
} // uniform_data_size
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   197
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   198
static VkBuffer get_uniform_buffer(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   199
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   200
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   201
        return VK_NULL_HANDLE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   202
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   203
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   204
        return ctx->vertUboBuffers[ctx->vertUboCurrentIndex]->buffer;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   205
    else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   206
        return ctx->fragUboBuffers[ctx->fragUboCurrentIndex]->buffer;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   207
} // get_uniform_buffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   208
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   209
static VkDeviceSize get_uniform_offset(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   210
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   211
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   212
        return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   213
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   214
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   215
        return ctx->vertUboBuffers[ctx->vertUboCurrentIndex]->dynamicOffset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   216
    else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   217
        return ctx->fragUboBuffers[ctx->fragUboCurrentIndex]->dynamicOffset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   218
} // get_uniform_offset
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   219
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   220
static VkDeviceSize get_uniform_size(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   221
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   222
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   223
        return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   224
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   225
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   226
        return ctx->vertUboBuffers[ctx->vertUboCurrentIndex]->currentBlockSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   227
    else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   228
        return ctx->fragUboBuffers[ctx->fragUboCurrentIndex]->currentBlockSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   229
} // get_uniform_size
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   230
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   231
static void update_uniform_buffer(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   232
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   233
    int32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   234
    void *map;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   235
    int32_t offset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   236
    uint8_t *contents;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   237
    float *regF; int *regI; uint8_t *regB;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   238
    MOJOSHADER_vkUniformBuffer *ubo;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   239
    VkDeviceMemory uboMemory;
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
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   242
        return;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   243
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   244
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
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
        regF = ctx->vs_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   247
        regI = ctx->vs_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   248
        regB = ctx->vs_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   249
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   250
        ubo = ctx->vertUboBuffers[ctx->vertUboCurrentIndex];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   251
        uboMemory = ctx->vertUboMemory;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   252
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   253
    else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   254
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   255
        regF = ctx->ps_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   256
        regI = ctx->ps_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   257
        regB = ctx->ps_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   258
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   259
        ubo = ctx->fragUboBuffers[ctx->fragUboCurrentIndex];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   260
        uboMemory = ctx->fragUboMemory;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   261
    } // else
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
    ubo->dynamicOffset += ubo->currentBlockSize;
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
    ubo->currentBlockSize = next_highest_offset_alignment(uniform_data_size(shader));
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   266
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   267
    // Rotate buffer if it would overrun
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   268
    if (ubo->dynamicOffset + ubo->currentBlockSize >= ubo->bufferSize)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   269
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   270
        ubo->full = ctx->currentFrame;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   272
        if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
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
            for (i = 0; i < ctx->uboBufferCount; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   275
            {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   276
                ctx->vertUboCurrentIndex = (ctx->vertUboCurrentIndex + 1) % ctx->uboBufferCount;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   277
                if (ctx->vertUboBuffers[ctx->vertUboCurrentIndex]->full == -1)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   278
                    break;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   279
            } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   280
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   281
            ubo = ctx->vertUboBuffers[ctx->vertUboCurrentIndex];
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
        else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   284
        {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   285
            for (int i = 0; i < ctx->uboBufferCount; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   286
            {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   287
                ctx->fragUboCurrentIndex = (ctx->fragUboCurrentIndex + 1) % ctx->uboBufferCount;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   288
                if (ctx->fragUboBuffers[ctx->fragUboCurrentIndex]->full == -1)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   289
                    break;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   290
            } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   291
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   292
            ubo = ctx->fragUboBuffers[ctx->fragUboCurrentIndex];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   293
        } // else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   294
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   295
        ubo->dynamicOffset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   296
        ubo->currentBlockSize = next_highest_offset_alignment(uniform_data_size(shader));
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   297
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   298
        if (ubo->full >= 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   299
            set_error("all UBO buffers are full");
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   300
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   301
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   302
    ctx->vkMapMemory(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   303
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   304
        uboMemory,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   305
        ubo->memoryOffset,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   306
        ubo->bufferSize,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   307
        0,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   308
        &map
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   309
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   310
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   311
    contents = ((uint8_t *) map) + ubo->dynamicOffset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   312
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   313
    offset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   314
    for (i = 0; i < shader->parseData->uniform_count; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   315
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   316
        const int32_t index = shader->parseData->uniforms[i].index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   317
        const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   318
        const int32_t size = arrayCount ? arrayCount : 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   319
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   320
        switch (shader->parseData->uniforms[i].type)
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
            case MOJOSHADER_UNIFORM_FLOAT:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   323
                memcpy(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   324
                    contents + (offset * 16),
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   325
                    &regF[4 * index],
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   326
                    size * 16
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   327
                );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   328
                break;
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
            case MOJOSHADER_UNIFORM_INT:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   331
                memcpy(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   332
                    contents + (offset * 16),
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   333
                    &regI[4 * index],
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   334
                    size * 16
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   335
                );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   336
                break;
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
            case MOJOSHADER_UNIFORM_BOOL:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   339
                memcpy(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   340
                    contents + offset,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   341
                    &regB[index],
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   342
                    size
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   343
                );
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
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   354
        offset += size;
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
    ctx->vkUnmapMemory(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   358
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   359
        uboMemory
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
} // update_uniform_buffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   362
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   363
static void lookup_entry_points(MOJOSHADER_vkContext *ctx)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   364
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   365
    #define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   366
        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
   367
    #define VULKAN_DEVICE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   368
        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
   369
    #include "mojoshader_vulkan_vkfuncs.h"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   370
} // lookup_entry_points
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   371
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   372
static int shader_bytecode_len(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   373
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   374
    return shader->parseData->output_len - sizeof(SpirvPatchTable);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   375
} // shader_bytecode_len
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   376
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   377
static void delete_shader(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   378
    VkShaderModule shaderModule
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   379
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   380
    ctx->vkDestroyShaderModule(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   381
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   382
        shaderModule,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   383
        NULL
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   384
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   385
} // delete_shader
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   386
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   387
// Public API
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   388
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   389
MOJOSHADER_vkContext *MOJOSHADER_vkCreateContext(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   390
    VkInstance *instance,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   391
    VkPhysicalDevice *physical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   392
    VkDevice *logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   393
    int frames_in_flight,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   394
    PFN_MOJOSHADER_vkGetInstanceProcAddr instance_lookup,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   395
    PFN_MOJOSHADER_vkGetDeviceProcAddr device_lookup,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   396
    unsigned int graphics_queue_family_index,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   397
    unsigned int max_uniform_buffer_range,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   398
    unsigned int min_uniform_buffer_offset_alignment,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   399
    MOJOSHADER_malloc m, MOJOSHADER_free f,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   400
    void *malloc_d
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   401
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   402
    int32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   403
    int32_t uboMemoryOffset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   404
    VkMemoryAllocateInfo allocate_info =
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   405
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   406
        VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   407
    };
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   408
    VkMemoryRequirements memoryRequirements;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   409
    MOJOSHADER_vkContext* resultCtx;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   410
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   411
    if (m == NULL) m = MOJOSHADER_internal_malloc;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   412
    if (f == NULL) f = MOJOSHADER_internal_free;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   413
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   414
    resultCtx = (MOJOSHADER_vkContext *) m(sizeof(MOJOSHADER_vkContext), malloc_d);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   415
    if (resultCtx == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   416
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   417
        out_of_memory();
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   418
        goto init_fail;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   419
    }
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   420
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   421
    memset(resultCtx, '\0', sizeof(MOJOSHADER_vkContext));
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   422
    resultCtx->malloc_fn = m;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   423
    resultCtx->free_fn = f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   424
    resultCtx->malloc_data = malloc_d;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   425
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   426
    resultCtx->instance = (VkInstance*) instance;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   427
    resultCtx->physical_device = (VkPhysicalDevice*) physical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   428
    resultCtx->logical_device = (VkDevice*) logical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   429
    resultCtx->instance_proc_lookup = (PFN_vkGetInstanceProcAddr) instance_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   430
    resultCtx->device_proc_lookup = (PFN_vkGetDeviceProcAddr) device_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   431
    resultCtx->frames_in_flight = frames_in_flight;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   432
    resultCtx->graphics_queue_family_index = graphics_queue_family_index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   433
    resultCtx->maxUniformBufferRange = max_uniform_buffer_range;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   434
    resultCtx->minUniformBufferOffsetAlignment = min_uniform_buffer_offset_alignment;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   435
    resultCtx->currentFrame = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   436
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   437
    lookup_entry_points(resultCtx);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   438
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   439
    resultCtx->uboBufferCount = UBO_BUFFER_COUNT;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   440
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   441
    // Allocate vert UBO
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   442
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   443
    resultCtx->vertUboCurrentIndex = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   444
    resultCtx->vertUboBuffers = (MOJOSHADER_vkUniformBuffer**) m(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   445
        sizeof(MOJOSHADER_vkUniformBuffer*) * resultCtx->uboBufferCount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   446
        malloc_d
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   447
    );
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
    for (i = 0; i < resultCtx->uboBufferCount; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   450
        resultCtx->vertUboBuffers[i] = create_ubo(resultCtx, m, malloc_d);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   451
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   452
    resultCtx->vkGetBufferMemoryRequirements(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   453
        *resultCtx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   454
        resultCtx->vertUboBuffers[0]->buffer,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   455
        &memoryRequirements
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   456
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   457
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   458
    allocate_info.allocationSize = UBO_BUFFER_SIZE * resultCtx->uboBufferCount;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   459
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   460
    if (!find_memory_type(resultCtx,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   461
                          memoryRequirements.memoryTypeBits,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   462
                          VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   463
                          &allocate_info.memoryTypeIndex))
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
        set_error("failed to find suitable memory type for UBO memory");
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   466
        return NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   467
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   468
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   469
    resultCtx->vkAllocateMemory(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   470
        *resultCtx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   471
        &allocate_info,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   472
        NULL,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   473
        &resultCtx->vertUboMemory
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
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   476
    uboMemoryOffset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   477
    for (i = 0; i < resultCtx->uboBufferCount; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   478
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   479
        resultCtx->vertUboBuffers[i]->memoryOffset = uboMemoryOffset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   480
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   481
        resultCtx->vkBindBufferMemory(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   482
            *resultCtx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   483
            resultCtx->vertUboBuffers[i]->buffer,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   484
            resultCtx->vertUboMemory,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   485
            uboMemoryOffset
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   486
        );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   487
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   488
        uboMemoryOffset += UBO_BUFFER_SIZE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   489
    } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   490
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   491
    // Allocate frag UBO
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   492
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   493
    resultCtx->fragUboCurrentIndex = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   494
    resultCtx->fragUboBuffers = (MOJOSHADER_vkUniformBuffer**) m(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   495
        sizeof(MOJOSHADER_vkUniformBuffer*) * resultCtx->uboBufferCount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   496
        malloc_d
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   497
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   498
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   499
    for (i = 0; i < resultCtx->uboBufferCount; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   500
        resultCtx->fragUboBuffers[i] = create_ubo(resultCtx, m, malloc_d);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   501
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   502
    resultCtx->vkGetBufferMemoryRequirements(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   503
        *resultCtx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   504
        resultCtx->fragUboBuffers[0]->buffer,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   505
        &memoryRequirements
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   506
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   507
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   508
    allocate_info.allocationSize = UBO_BUFFER_SIZE * resultCtx->uboBufferCount;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   509
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   510
    if (!find_memory_type(resultCtx,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   511
                          memoryRequirements.memoryTypeBits,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   512
                          VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   513
                          &allocate_info.memoryTypeIndex))
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   514
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   515
        set_error("failed to find suitable memory type for UBO memory");
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   516
        return NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   517
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   518
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   519
    resultCtx->vkAllocateMemory(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   520
        *resultCtx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   521
        &allocate_info,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   522
        NULL,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   523
        &resultCtx->fragUboMemory
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   524
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   525
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   526
    uboMemoryOffset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   527
    for (i = 0; i < resultCtx->uboBufferCount; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   528
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   529
        resultCtx->fragUboBuffers[i]->memoryOffset = uboMemoryOffset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   530
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   531
        resultCtx->vkBindBufferMemory(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   532
            *resultCtx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   533
            resultCtx->fragUboBuffers[i]->buffer,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   534
            resultCtx->fragUboMemory,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   535
            uboMemoryOffset
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   536
        );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   537
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   538
        uboMemoryOffset += UBO_BUFFER_SIZE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   539
    } // for
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
    return resultCtx;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   542
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   543
init_fail:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   544
    if (resultCtx != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   545
        f(resultCtx, malloc_d);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   546
    return NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   547
} // MOJOSHADER_vkCreateContext
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   548
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   549
void MOJOSHADER_vkMakeContextCurrent(MOJOSHADER_vkContext *_ctx)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   550
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   551
    ctx = _ctx;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   552
} // MOJOSHADER_vkMakeContextCurrent
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   553
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   554
void MOJOSHADER_vkDestroyContext()
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   555
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   556
    int32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   557
    for (i = 0; i < ctx->uboBufferCount; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   558
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   559
        ctx->vkDestroyBuffer(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   560
            *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   561
            ctx->vertUboBuffers[i]->buffer,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   562
            NULL
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
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   565
        ctx->free_fn(ctx->vertUboBuffers[i], ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   566
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   567
        ctx->vkDestroyBuffer(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   568
            *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   569
            ctx->fragUboBuffers[i]->buffer,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   570
            NULL
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   571
        );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   572
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   573
        ctx->free_fn(ctx->fragUboBuffers[i], ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   574
    } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   575
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   576
    ctx->free_fn(ctx->vertUboBuffers, ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   577
    ctx->free_fn(ctx->fragUboBuffers, ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   578
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   579
    ctx->vkFreeMemory(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   580
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   581
        ctx->vertUboMemory,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   582
        NULL
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   583
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   584
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   585
    ctx->vkFreeMemory(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   586
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   587
        ctx->fragUboMemory,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   588
        NULL
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   589
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   590
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   591
    ctx->free_fn(ctx, ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   592
} // MOJOSHADER_vkDestroyContext
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   593
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   594
MOJOSHADER_vkShader *MOJOSHADER_vkCompileShader(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   595
    const char *mainfn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   596
    const unsigned char *tokenbuf,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   597
    const unsigned int bufsize,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   598
    const MOJOSHADER_swizzle *swiz,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   599
    const unsigned int swizcount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   600
    const MOJOSHADER_samplerMap *smap,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   601
    const unsigned int smapcount
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   602
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   603
    VkResult result;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   604
    VkShaderModule shaderModule;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   605
    VkShaderModuleCreateInfo shaderModuleCreateInfo =
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   606
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   607
        VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   608
    };
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   609
    MOJOSHADER_vkShader *shader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   610
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   611
    const MOJOSHADER_parseData *pd = MOJOSHADER_parse(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   612
        "spirv", mainfn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   613
        tokenbuf, bufsize,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   614
        swiz, swizcount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   615
        smap, smapcount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   616
        ctx->malloc_fn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   617
        ctx->free_fn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   618
        ctx->malloc_data
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   619
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   620
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   621
    if (pd->error_count > 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   622
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   623
        set_error(pd->errors[0].error);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   624
        goto compile_shader_fail;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   625
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   626
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   627
    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
   628
    if (shader == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   629
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   630
        out_of_memory();
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   631
        goto compile_shader_fail;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   632
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   633
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   634
    shader->parseData = pd;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   635
    shader->refcount = 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   636
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   637
    shaderModuleCreateInfo.flags = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   638
    shaderModuleCreateInfo.codeSize = shader_bytecode_len(shader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   639
    shaderModuleCreateInfo.pCode = (uint32_t*) pd->output;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   640
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   641
    result = ctx->vkCreateShaderModule(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   642
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   643
        &shaderModuleCreateInfo,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   644
        NULL,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   645
        &shader->shaderModule
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   646
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   647
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   648
    if (result != VK_SUCCESS)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   649
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   650
        // FIXME: should display VK error code
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   651
        set_error("Error when creating VkShaderModule");
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   652
        goto compile_shader_fail;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   653
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   654
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   655
    return shader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   656
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   657
compile_shader_fail:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   658
    MOJOSHADER_freeParseData(pd);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   659
    if (shader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   660
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   661
        delete_shader(shader->shaderModule);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   662
        ctx->free_fn(shader, ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   663
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   664
    return NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   665
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   666
} // MOJOSHADER_vkMakeContextCurrent
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   667
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   668
void MOJOSHADER_vkShaderAddRef(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   669
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   670
    if (shader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   671
        shader->refcount++;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   672
} // MOJOShader_vkShaderAddRef
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   673
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   674
void MOJOSHADER_vkDeleteShader(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   675
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   676
    if (shader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   677
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   678
        if (shader->refcount > 1)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   679
            shader->refcount--;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   680
        else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   681
        {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   682
            delete_shader(shader->shaderModule);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   683
            MOJOSHADER_freeParseData(shader->parseData);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   684
            ctx->free_fn(shader, ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   685
        } // else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   686
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   687
} // MOJOSHADER_vkDeleteShader
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   688
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   689
const MOJOSHADER_parseData *MOJOSHADER_vkGetShaderParseData(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   690
    MOJOSHADER_vkShader *shader
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   691
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   692
    return (shader != NULL) ? shader->parseData : NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   693
} // MOJOSHADER_vkGetShaderParseData
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   694
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   695
void MOJOSHADER_vkBindShaders(MOJOSHADER_vkShader *vshader,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   696
                              MOJOSHADER_vkShader *pshader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   697
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   698
    /* NOOP if shader is null */
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   699
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   700
    if (vshader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   701
        ctx->vertexShader = vshader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   702
    if (pshader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   703
        ctx->pixelShader = pshader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   704
} // MOJOSHADER_vkBindShaders
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   705
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   706
void MOJOSHADER_vkGetBoundShaders(MOJOSHADER_vkShader **vshader,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   707
                                  MOJOSHADER_vkShader **pshader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   708
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   709
    *vshader = ctx->vertexShader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   710
    *pshader = ctx->pixelShader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   711
} // MOJOSHADER_vkGetBoundShaders
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   712
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   713
void MOJOSHADER_vkMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   714
                                         float **psf, int **psi, unsigned char **psb)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   715
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   716
    *vsf = ctx->vs_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   717
    *vsi = ctx->vs_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   718
    *vsb = ctx->vs_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   719
    *psf = ctx->ps_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   720
    *psi = ctx->ps_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   721
    *psb = ctx->ps_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   722
} // MOJOSHADER_vkMapUniformBufferMemory
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   723
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   724
void MOJOSHADER_vkUnmapUniformBufferMemory()
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   725
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   726
    /* Why is this function named unmap instead of update?
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   727
     * the world may never know...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   728
     */
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   729
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   730
    update_uniform_buffer(ctx->vertexShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   731
    update_uniform_buffer(ctx->pixelShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   732
} // MOJOSHADER_vkUnmapUniformBufferMemory
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   733
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   734
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
   735
                                    VkBuffer *pbuf, unsigned long long *poff, unsigned long long *psize)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   736
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   737
    *vbuf = get_uniform_buffer(ctx->vertexShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   738
    *voff = get_uniform_offset(ctx->vertexShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   739
    *vsize = get_uniform_size(ctx->vertexShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   740
    *pbuf = get_uniform_buffer(ctx->pixelShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   741
    *poff = get_uniform_offset(ctx->pixelShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   742
    *psize = get_uniform_size(ctx->pixelShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   743
} // MOJOSHADER_vkGetUniformBuffers
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   744
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   745
void MOJOSHADER_vkEndFrame()
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   746
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   747
    int32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   748
    ctx->currentFrame = (ctx->currentFrame + 1) % ctx->frames_in_flight;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   749
    for (i = 0; i < ctx->uboBufferCount; i++)
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
        if (ctx->vertUboBuffers[i]->full == ctx->currentFrame)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   752
        {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   753
            ctx->vertUboBuffers[i]->dynamicOffset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   754
            ctx->vertUboBuffers[i]->currentBlockSize = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   755
            ctx->vertUboBuffers[i]->full = -1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   756
        } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   757
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   758
        if (ctx->fragUboBuffers[i]->full == ctx->currentFrame)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   759
        {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   760
            ctx->fragUboBuffers[i]->dynamicOffset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   761
            ctx->fragUboBuffers[i]->currentBlockSize = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   762
            ctx->fragUboBuffers[i]->full = -1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   763
        } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   764
    } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   765
} // MOJOSHADER_VkEndFrame
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   766
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   767
int MOJOSHADER_vkGetVertexAttribLocation(MOJOSHADER_vkShader *vert,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   768
                                         MOJOSHADER_usage usage, int index)
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
    int32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   771
    if (vert == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   772
        return -1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   773
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   774
    for (i = 0; i < vert->parseData->attribute_count; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   775
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   776
        if (vert->parseData->attributes[i].usage == usage &&
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   777
            vert->parseData->attributes[i].index == index)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   778
        {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   779
            return i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   780
        } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   781
    } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   782
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   783
    // failure
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   784
    return -1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   785
} //MOJOSHADER_vkGetVertexAttribLocation
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   786
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   787
unsigned long long MOJOSHADER_vkGetShaderModule(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   788
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   789
    if (shader == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   790
        return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   791
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   792
    return (unsigned long long) shader->shaderModule;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   793
} //MOJOSHADER_vkGetShaderModule
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   794
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   795
const char *MOJOSHADER_vkGetError(void)
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
    return error_buffer;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   798
} // MOJOSHADER_vkGetError
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   799
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   800
#endif /* SUPPORT_PROFILE_SPIRV */
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   801
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   802
// end of mojoshader_vulkan.c ...