mojoshader_vulkan.c
author Evan Hemsley <evan@moonside.games>
Mon, 06 Jul 2020 16:23:06 -0400
changeset 1276 89c389e4112f
parent 1275 7fc13cff18ff
child 1277 da61410edbc9
permissions -rw-r--r--
vulkan: Rework UBO allocation to use a single monolithic buffer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     1
/**
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     3
 *  Direct3D shaders.
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     4
 *
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     6
 *
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     8
 */
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
     9
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    10
#define __MOJOSHADER_INTERNAL__ 1
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    11
#include "mojoshader_internal.h"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    12
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    13
#if SUPPORT_PROFILE_SPIRV
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    14
1272
cddbd25553fe Fix include path for vulkan.h
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1271
diff changeset
    15
#include "vulkan/vulkan.h"
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    16
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    17
#define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    18
    typedef ret (VKAPI_CALL *vkfntype_MOJOSHADER_##func) params;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    19
#define VULKAN_DEVICE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    20
	typedef ret (VKAPI_CALL *vkfntype_MOJOSHADER_##func) params;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    21
#include "mojoshader_vulkan_vkfuncs.h"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    22
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    23
#define UBO_BUFFER_SIZE 8000000 // 8MB
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    24
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    25
// Internal struct defs...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    26
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    27
typedef struct MOJOSHADER_vkShader
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    28
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    29
    VkShaderModule shaderModule;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    30
    const MOJOSHADER_parseData *parseData;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    31
    uint32_t refcount;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    32
} MOJOSHADER_vkShader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    33
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    34
typedef struct MOJOSHADER_vkUniformBuffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    35
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    36
    VkBuffer buffer;
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    37
    VkDeviceMemory deviceMemory;
1271
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 dynamicOffset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    40
    VkDeviceSize currentBlockSize;
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    41
    uint8_t *mapPointer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    42
} MOJOSHADER_vkUniformBuffer;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    43
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    44
// Error state...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    45
static char error_buffer[1024] = { '\0' };
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    46
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    47
static void set_error(const char *str)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    48
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    49
    snprintf(error_buffer, sizeof (error_buffer), "%s", str);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    50
} // set_error
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    51
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    52
static inline void out_of_memory(void)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    53
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    54
    set_error("out of memory");
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    55
} // out_of_memory
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    56
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    57
// Max entries for each register file type
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    58
#define MAX_REG_FILE_F 8192
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    59
#define MAX_REG_FILE_I 2047
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    60
#define MAX_REG_FILE_B 2047
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    61
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    62
typedef struct MOJOSHADER_vkContext
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    63
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    64
    VkInstance *instance;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    65
    VkPhysicalDevice *physical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    66
    VkDevice *logical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    67
    PFN_vkGetInstanceProcAddr instance_proc_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    68
    PFN_vkGetDeviceProcAddr device_proc_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    69
    uint32_t graphics_queue_family_index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    70
    uint32_t maxUniformBufferRange;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    71
    uint32_t minUniformBufferOffsetAlignment;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    72
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    73
    int32_t frames_in_flight;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    74
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    75
    MOJOSHADER_malloc malloc_fn;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    76
    MOJOSHADER_free free_fn;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    77
    void *malloc_data;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    78
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    79
    // The constant register files...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    80
    // !!! FIXME: Man, it kills me how much memory this takes...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    81
    // !!! FIXME:  ... make this dynamically allocated on demand.
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    82
    float vs_reg_file_f[MAX_REG_FILE_F * 4];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    83
    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
    84
    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
    85
    float ps_reg_file_f[MAX_REG_FILE_F * 4];
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    86
    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
    87
    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
    88
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    89
    MOJOSHADER_vkUniformBuffer *vertUboBuffer;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
    90
    MOJOSHADER_vkUniformBuffer *fragUboBuffer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    91
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    92
    MOJOSHADER_vkShader *vertexShader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    93
    MOJOSHADER_vkShader *pixelShader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    94
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    95
    #define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    96
        vkfntype_MOJOSHADER_##func func;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    97
    #define VULKAN_DEVICE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    98
        vkfntype_MOJOSHADER_##func func;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
    99
    #include "mojoshader_vulkan_vkfuncs.h"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   100
} MOJOSHADER_vkContext;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   101
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   102
static MOJOSHADER_vkContext *ctx = NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   103
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   104
static uint8_t find_memory_type(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   105
    MOJOSHADER_vkContext *ctx,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   106
	uint32_t typeFilter,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   107
	VkMemoryPropertyFlags properties,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   108
	uint32_t *result
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   109
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   110
	uint32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   111
	VkPhysicalDeviceMemoryProperties memoryProperties;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   112
	ctx->vkGetPhysicalDeviceMemoryProperties(*ctx->physical_device, &memoryProperties);
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
	for (i = 0; i < memoryProperties.memoryTypeCount; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   115
	{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   116
		if ((typeFilter & (1 << i))
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   117
		 && (memoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   118
		{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   119
			*result = i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   120
			return 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   121
		} // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   122
	} // for
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
	return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   125
} // find_memory_type
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   126
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   127
static uint32_t next_highest_offset_alignment(uint32_t offset)
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
    return (
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   130
        (offset + ctx->minUniformBufferOffsetAlignment - 1) /
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   131
        ctx->minUniformBufferOffsetAlignment *
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   132
        ctx->minUniformBufferOffsetAlignment
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
} // next_highest_offset_alignment
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   135
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   136
static MOJOSHADER_vkUniformBuffer *create_ubo(MOJOSHADER_vkContext *ctx)
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   137
{
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   138
    MOJOSHADER_vkUniformBuffer *result = (MOJOSHADER_vkUniformBuffer *) ctx->malloc_fn(
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   139
        sizeof(MOJOSHADER_vkUniformBuffer),
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   140
        ctx->malloc_data
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   141
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   142
    VkBufferCreateInfo bufferCreateInfo =
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
        VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   145
    };
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   146
    VkMemoryRequirements memoryRequirements;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   147
    VkMemoryAllocateInfo allocateInfo =
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   148
    {
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   149
        VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   150
    };
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   151
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   152
    bufferCreateInfo.flags = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   153
    bufferCreateInfo.size = UBO_BUFFER_SIZE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   154
    bufferCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   155
    bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   156
    bufferCreateInfo.queueFamilyIndexCount = 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   157
    bufferCreateInfo.pQueueFamilyIndices = &ctx->graphics_queue_family_index;
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
    ctx->vkCreateBuffer(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   160
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   161
        &bufferCreateInfo,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   162
        NULL,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   163
        &result->buffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   164
    );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   165
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   166
    ctx->vkGetBufferMemoryRequirements(
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   167
        *ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   168
        result->buffer,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   169
        &memoryRequirements
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   170
    );
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   171
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   172
    allocateInfo.allocationSize = UBO_BUFFER_SIZE;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   173
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   174
    if (!find_memory_type(ctx,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   175
                          memoryRequirements.memoryTypeBits,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   176
                          VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   177
                          &allocateInfo.memoryTypeIndex))
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   178
    {
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   179
        set_error("failed to find suitable memory type for UBO memory");
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   180
        return NULL;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   181
    } // if
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   182
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   183
    ctx->vkAllocateMemory(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   184
                          &allocateInfo,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   185
                          NULL,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   186
                          &result->deviceMemory
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   187
    );
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   188
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   189
    ctx->vkBindBufferMemory(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   190
                            result->buffer,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   191
                            result->deviceMemory,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   192
                            0
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   193
    );
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   194
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   195
    ctx->vkMapMemory(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   196
                     result->deviceMemory,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   197
                     0,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   198
                     UBO_BUFFER_SIZE,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   199
                     0,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   200
                     (void**) &result->mapPointer
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   201
    );
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   202
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   203
    result->bufferSize = UBO_BUFFER_SIZE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   204
    result->currentBlockSize = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   205
    result->dynamicOffset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   206
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   207
    return result;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   208
} // create_ubo
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   209
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   210
static uint32_t uniform_data_size(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   211
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   212
    int32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   213
    int32_t buflen = 0;
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   214
    const int32_t uniformSize = 16; // Yes, even the bool registers
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   215
    for (i = 0; i < shader->parseData->uniform_count; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   216
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   217
        const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   218
        buflen += (arrayCount ? arrayCount : 1) * uniformSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   219
    } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   220
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   221
    return buflen;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   222
} // uniform_data_size
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   223
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   224
static VkBuffer get_uniform_buffer(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   225
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   226
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   227
        return VK_NULL_HANDLE;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   228
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   229
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   230
        return ctx->vertUboBuffer->buffer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   231
    else
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   232
        return ctx->fragUboBuffer->buffer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   233
} // get_uniform_buffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   234
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   235
static VkDeviceSize get_uniform_offset(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   236
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   237
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   238
        return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   239
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   240
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   241
        return ctx->vertUboBuffer->dynamicOffset;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   242
    else
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   243
        return ctx->fragUboBuffer->dynamicOffset;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   244
} // get_uniform_offset
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
static VkDeviceSize get_uniform_size(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   247
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   248
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   249
        return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   250
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   251
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   252
        return ctx->vertUboBuffer->currentBlockSize;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   253
    else
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   254
        return ctx->fragUboBuffer->currentBlockSize;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   255
} // get_uniform_size
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   256
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   257
static void update_uniform_buffer(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   258
{
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   259
    int32_t i, j;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   260
    int32_t offset;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   261
    uint8_t *contents;
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   262
    uint32_t *contentsI;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   263
    float *regF; int *regI; uint8_t *regB;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   264
    MOJOSHADER_vkUniformBuffer *ubo;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   265
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   266
    if (shader == NULL || shader->parseData->uniform_count == 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   267
        return;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   268
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   269
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   270
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   271
        regF = ctx->vs_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   272
        regI = ctx->vs_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   273
        regB = ctx->vs_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   274
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   275
        ubo = ctx->vertUboBuffer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   276
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   277
    else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   278
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   279
        regF = ctx->ps_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   280
        regI = ctx->ps_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   281
        regB = ctx->ps_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   282
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   283
        ubo = ctx->fragUboBuffer;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   284
    } // else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   285
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   286
    ubo->dynamicOffset += ubo->currentBlockSize;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   287
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   288
    ubo->currentBlockSize = next_highest_offset_alignment(uniform_data_size(shader));
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   289
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   290
    if (ubo->dynamicOffset + ubo->currentBlockSize >= ubo->bufferSize)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   291
    {
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   292
        set_error("UBO overflow!!");
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   293
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   294
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   295
    contents = ubo->mapPointer + ubo->dynamicOffset;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   296
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   297
    offset = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   298
    for (i = 0; i < shader->parseData->uniform_count; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   299
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   300
        const int32_t index = shader->parseData->uniforms[i].index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   301
        const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   302
        const int32_t size = arrayCount ? arrayCount : 1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   303
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   304
        switch (shader->parseData->uniforms[i].type)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   305
        {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   306
            case MOJOSHADER_UNIFORM_FLOAT:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   307
                memcpy(
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   308
                    contents + offset,
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   309
                    &regF[4 * index],
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   310
                    size * 16
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   311
                );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   312
                break;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   313
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   314
            case MOJOSHADER_UNIFORM_INT:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   315
                memcpy(
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   316
                    contents + offset,
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   317
                    &regI[4 * index],
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   318
                    size * 16
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
                break;
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_BOOL:
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   323
                contentsI = (uint32_t *) (contents + offset);
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   324
                for (j = 0; j < size; j++)
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   325
                    contentsI[j * 4] = regB[index + j];
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   326
                break;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   327
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   328
            default:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   329
                set_error(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   330
                    "SOMETHING VERY WRONG HAPPENED WHEN UPDATING UNIFORMS"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   331
                );
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   332
                assert(0);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   333
                break;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   334
        } // switch
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   335
1275
7fc13cff18ff vulkan: Fix uniform buffer copies for bools
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1272
diff changeset
   336
        offset += size * 16;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   337
    } // for
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   338
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   339
} // update_uniform_buffer
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   340
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   341
static void lookup_entry_points(MOJOSHADER_vkContext *ctx)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   342
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   343
    #define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   344
        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
   345
    #define VULKAN_DEVICE_FUNCTION(ret, func, params) \
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   346
        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
   347
    #include "mojoshader_vulkan_vkfuncs.h"
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   348
} // lookup_entry_points
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
static int shader_bytecode_len(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   351
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   352
    return shader->parseData->output_len - sizeof(SpirvPatchTable);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   353
} // shader_bytecode_len
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   354
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   355
static void delete_shader(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   356
    VkShaderModule shaderModule
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   357
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   358
    ctx->vkDestroyShaderModule(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   359
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   360
        shaderModule,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   361
        NULL
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
} // delete_shader
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
// Public API
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   366
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   367
MOJOSHADER_vkContext *MOJOSHADER_vkCreateContext(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   368
    VkInstance *instance,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   369
    VkPhysicalDevice *physical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   370
    VkDevice *logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   371
    int frames_in_flight,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   372
    PFN_MOJOSHADER_vkGetInstanceProcAddr instance_lookup,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   373
    PFN_MOJOSHADER_vkGetDeviceProcAddr device_lookup,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   374
    unsigned int graphics_queue_family_index,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   375
    unsigned int max_uniform_buffer_range,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   376
    unsigned int min_uniform_buffer_offset_alignment,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   377
    MOJOSHADER_malloc m, MOJOSHADER_free f,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   378
    void *malloc_d
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
    MOJOSHADER_vkContext* resultCtx;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   381
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   382
    if (m == NULL) m = MOJOSHADER_internal_malloc;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   383
    if (f == NULL) f = MOJOSHADER_internal_free;
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
    resultCtx = (MOJOSHADER_vkContext *) m(sizeof(MOJOSHADER_vkContext), malloc_d);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   386
    if (resultCtx == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   387
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   388
        out_of_memory();
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   389
        goto init_fail;
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   390
    } // if
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   391
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   392
    memset(resultCtx, '\0', sizeof(MOJOSHADER_vkContext));
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   393
    resultCtx->malloc_fn = m;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   394
    resultCtx->free_fn = f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   395
    resultCtx->malloc_data = malloc_d;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   396
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   397
    resultCtx->instance = (VkInstance*) instance;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   398
    resultCtx->physical_device = (VkPhysicalDevice*) physical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   399
    resultCtx->logical_device = (VkDevice*) logical_device;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   400
    resultCtx->instance_proc_lookup = (PFN_vkGetInstanceProcAddr) instance_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   401
    resultCtx->device_proc_lookup = (PFN_vkGetDeviceProcAddr) device_lookup;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   402
    resultCtx->frames_in_flight = frames_in_flight;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   403
    resultCtx->graphics_queue_family_index = graphics_queue_family_index;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   404
    resultCtx->maxUniformBufferRange = max_uniform_buffer_range;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   405
    resultCtx->minUniformBufferOffsetAlignment = min_uniform_buffer_offset_alignment;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   406
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   407
    lookup_entry_points(resultCtx);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   408
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   409
    resultCtx->vertUboBuffer = create_ubo(resultCtx);
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   410
    resultCtx->fragUboBuffer = create_ubo(resultCtx);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   411
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   412
    return resultCtx;
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
init_fail:
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
        f(resultCtx, malloc_d);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   417
    return NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   418
} // MOJOSHADER_vkCreateContext
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
void MOJOSHADER_vkMakeContextCurrent(MOJOSHADER_vkContext *_ctx)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   421
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   422
    ctx = _ctx;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   423
} // MOJOSHADER_vkMakeContextCurrent
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   424
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   425
void MOJOSHADER_vkDestroyContext()
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   426
{
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   427
    ctx->vkDestroyBuffer(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   428
                         ctx->vertUboBuffer->buffer,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   429
                         NULL);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   430
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   431
    ctx->vkDestroyBuffer(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   432
                         ctx->fragUboBuffer->buffer,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   433
                         NULL);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   434
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   435
    ctx->vkFreeMemory(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   436
                      ctx->vertUboBuffer->deviceMemory,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   437
                      NULL);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   438
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   439
    ctx->vkFreeMemory(*ctx->logical_device,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   440
                      ctx->fragUboBuffer->deviceMemory,
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   441
                      NULL);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   442
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   443
    ctx->free_fn(ctx->vertUboBuffer, ctx->malloc_data);
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   444
    ctx->free_fn(ctx->fragUboBuffer, ctx->malloc_data);
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   445
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   446
    ctx->free_fn(ctx, ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   447
} // MOJOSHADER_vkDestroyContext
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
MOJOSHADER_vkShader *MOJOSHADER_vkCompileShader(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   450
    const char *mainfn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   451
    const unsigned char *tokenbuf,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   452
    const unsigned int bufsize,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   453
    const MOJOSHADER_swizzle *swiz,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   454
    const unsigned int swizcount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   455
    const MOJOSHADER_samplerMap *smap,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   456
    const unsigned int smapcount
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
    VkResult result;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   459
    VkShaderModuleCreateInfo shaderModuleCreateInfo =
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   460
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   461
        VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   462
    };
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   463
    MOJOSHADER_vkShader *shader;
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
    const MOJOSHADER_parseData *pd = MOJOSHADER_parse(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   466
        "spirv", mainfn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   467
        tokenbuf, bufsize,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   468
        swiz, swizcount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   469
        smap, smapcount,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   470
        ctx->malloc_fn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   471
        ctx->free_fn,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   472
        ctx->malloc_data
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   473
    );
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
    if (pd->error_count > 0)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   476
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   477
        set_error(pd->errors[0].error);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   478
        goto compile_shader_fail;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   479
    } // if
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
    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
   482
    if (shader == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   483
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   484
        out_of_memory();
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   485
        goto compile_shader_fail;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   486
    } // if
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
    shader->parseData = pd;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   489
    shader->refcount = 1;
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
    shaderModuleCreateInfo.flags = 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   492
    shaderModuleCreateInfo.codeSize = shader_bytecode_len(shader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   493
    shaderModuleCreateInfo.pCode = (uint32_t*) pd->output;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   494
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   495
    result = ctx->vkCreateShaderModule(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   496
        *ctx->logical_device,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   497
        &shaderModuleCreateInfo,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   498
        NULL,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   499
        &shader->shaderModule
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   500
    );
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
    if (result != VK_SUCCESS)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   503
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   504
        // FIXME: should display VK error code
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   505
        set_error("Error when creating VkShaderModule");
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   506
        goto compile_shader_fail;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   507
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   508
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   509
    return shader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   510
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   511
compile_shader_fail:
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   512
    MOJOSHADER_freeParseData(pd);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   513
    if (shader != NULL)
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
        delete_shader(shader->shaderModule);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   516
        ctx->free_fn(shader, ctx->malloc_data);
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
    return NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   519
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   520
} // MOJOSHADER_vkMakeContextCurrent
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   521
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   522
void MOJOSHADER_vkShaderAddRef(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   523
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   524
    if (shader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   525
        shader->refcount++;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   526
} // MOJOShader_vkShaderAddRef
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   527
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   528
void MOJOSHADER_vkDeleteShader(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   529
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   530
    if (shader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   531
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   532
        if (shader->refcount > 1)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   533
            shader->refcount--;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   534
        else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   535
        {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   536
            delete_shader(shader->shaderModule);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   537
            MOJOSHADER_freeParseData(shader->parseData);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   538
            ctx->free_fn(shader, ctx->malloc_data);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   539
        } // else
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   540
    } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   541
} // MOJOSHADER_vkDeleteShader
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
const MOJOSHADER_parseData *MOJOSHADER_vkGetShaderParseData(
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   544
    MOJOSHADER_vkShader *shader
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   545
) {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   546
    return (shader != NULL) ? shader->parseData : NULL;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   547
} // MOJOSHADER_vkGetShaderParseData
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_vkBindShaders(MOJOSHADER_vkShader *vshader,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   550
                              MOJOSHADER_vkShader *pshader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   551
{
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   552
    // NOOP if shader is null
1271
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
    if (vshader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   555
        ctx->vertexShader = vshader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   556
    if (pshader != NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   557
        ctx->pixelShader = pshader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   558
} // MOJOSHADER_vkBindShaders
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   559
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   560
void MOJOSHADER_vkGetBoundShaders(MOJOSHADER_vkShader **vshader,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   561
                                  MOJOSHADER_vkShader **pshader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   562
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   563
    *vshader = ctx->vertexShader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   564
    *pshader = ctx->pixelShader;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   565
} // MOJOSHADER_vkGetBoundShaders
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
void MOJOSHADER_vkMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   568
                                         float **psf, int **psi, unsigned char **psb)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   569
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   570
    *vsf = ctx->vs_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   571
    *vsi = ctx->vs_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   572
    *vsb = ctx->vs_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   573
    *psf = ctx->ps_reg_file_f;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   574
    *psi = ctx->ps_reg_file_i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   575
    *psb = ctx->ps_reg_file_b;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   576
} // MOJOSHADER_vkMapUniformBufferMemory
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   577
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   578
void MOJOSHADER_vkUnmapUniformBufferMemory()
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   579
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   580
    /* Why is this function named unmap instead of update?
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   581
     * the world may never know...
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   582
     */
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
    update_uniform_buffer(ctx->vertexShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   585
    update_uniform_buffer(ctx->pixelShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   586
} // MOJOSHADER_vkUnmapUniformBufferMemory
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   587
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   588
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
   589
                                    VkBuffer *pbuf, unsigned long long *poff, unsigned long long *psize)
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
    *vbuf = get_uniform_buffer(ctx->vertexShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   592
    *voff = get_uniform_offset(ctx->vertexShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   593
    *vsize = get_uniform_size(ctx->vertexShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   594
    *pbuf = get_uniform_buffer(ctx->pixelShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   595
    *poff = get_uniform_offset(ctx->pixelShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   596
    *psize = get_uniform_size(ctx->pixelShader);
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   597
} // MOJOSHADER_vkGetUniformBuffers
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   598
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   599
void MOJOSHADER_vkEndFrame()
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   600
{
1276
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   601
    ctx->vertUboBuffer->dynamicOffset = 0;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   602
    ctx->vertUboBuffer->currentBlockSize = 0;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   603
    ctx->fragUboBuffer->dynamicOffset = 0;
89c389e4112f vulkan: Rework UBO allocation to use a single monolithic buffer
Evan Hemsley <evan@moonside.games>
parents: 1275
diff changeset
   604
    ctx->fragUboBuffer->currentBlockSize = 0;
1271
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   605
} // MOJOSHADER_VkEndFrame
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
int MOJOSHADER_vkGetVertexAttribLocation(MOJOSHADER_vkShader *vert,
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   608
                                         MOJOSHADER_usage usage, int index)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   609
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   610
    int32_t i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   611
    if (vert == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   612
        return -1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   613
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   614
    for (i = 0; i < vert->parseData->attribute_count; i++)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   615
    {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   616
        if (vert->parseData->attributes[i].usage == usage &&
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   617
            vert->parseData->attributes[i].index == index)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   618
        {
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   619
            return i;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   620
        } // if
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   621
    } // for
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
    // failure
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   624
    return -1;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   625
} //MOJOSHADER_vkGetVertexAttribLocation
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
unsigned long long MOJOSHADER_vkGetShaderModule(MOJOSHADER_vkShader *shader)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   628
{
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   629
    if (shader == NULL)
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   630
        return 0;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   631
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   632
    return (unsigned long long) shader->shaderModule;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   633
} //MOJOSHADER_vkGetShaderModule
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   634
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   635
const char *MOJOSHADER_vkGetError(void)
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
    return error_buffer;
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   638
} // MOJOSHADER_vkGetError
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   639
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   640
#endif /* SUPPORT_PROFILE_SPIRV */
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   641
5a67d082c55f Add support for Vulkan rendering.
Evan Hemsley <evan@moonside.games>
parents:
diff changeset
   642
// end of mojoshader_vulkan.c ...