mojoshader_vulkan.c
changeset 1304 d68910d81b6e
parent 1302 974d2cc3558e
child 1315 c54ad4b34c03
equal deleted inserted replaced
1303:7a43f238f28a 1304:d68910d81b6e
    19 #define VULKAN_DEVICE_FUNCTION(ret, func, params) \
    19 #define VULKAN_DEVICE_FUNCTION(ret, func, params) \
    20 	typedef ret (VKAPI_CALL *vkfntype_MOJOSHADER_##func) params;
    20 	typedef ret (VKAPI_CALL *vkfntype_MOJOSHADER_##func) params;
    21 #include "mojoshader_vulkan_vkfuncs.h"
    21 #include "mojoshader_vulkan_vkfuncs.h"
    22 
    22 
    23 #define UBO_BUFFER_SIZE 8000000 // 8MB
    23 #define UBO_BUFFER_SIZE 8000000 // 8MB
       
    24 #define UBO_ACTUAL_SIZE (UBO_BUFFER_SIZE * 2) // Double so we can "rotate" the buffer and unblock main thread
    24 
    25 
    25 // Internal struct defs...
    26 // Internal struct defs...
    26 
    27 
    27 typedef struct MOJOSHADER_vkShader
    28 typedef struct MOJOSHADER_vkShader
    28 {
    29 {
    76     PFN_vkGetDeviceProcAddr device_proc_lookup;
    77     PFN_vkGetDeviceProcAddr device_proc_lookup;
    77     uint32_t graphics_queue_family_index;
    78     uint32_t graphics_queue_family_index;
    78     uint32_t maxUniformBufferRange;
    79     uint32_t maxUniformBufferRange;
    79     uint32_t minUniformBufferOffsetAlignment;
    80     uint32_t minUniformBufferOffsetAlignment;
    80 
    81 
    81     int32_t frames_in_flight;
    82     uint32_t frameIndex;
    82 
    83 
    83     MOJOSHADER_malloc malloc_fn;
    84     MOJOSHADER_malloc malloc_fn;
    84     MOJOSHADER_free free_fn;
    85     MOJOSHADER_free free_fn;
    85     void *malloc_data;
    86     void *malloc_data;
    86 
    87 
   162     {
   163     {
   163         VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
   164         VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
   164     };
   165     };
   165 
   166 
   166     bufferCreateInfo.flags = 0;
   167     bufferCreateInfo.flags = 0;
   167     bufferCreateInfo.size = UBO_BUFFER_SIZE;
   168     bufferCreateInfo.size = UBO_ACTUAL_SIZE;
   168     bufferCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
   169     bufferCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
   169     bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
   170     bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
   170     bufferCreateInfo.queueFamilyIndexCount = 1;
   171     bufferCreateInfo.queueFamilyIndexCount = 1;
   171     bufferCreateInfo.pQueueFamilyIndices = &ctx->graphics_queue_family_index;
   172     bufferCreateInfo.pQueueFamilyIndices = &ctx->graphics_queue_family_index;
   172 
   173 
   181         *ctx->logical_device,
   182         *ctx->logical_device,
   182         result->buffer,
   183         result->buffer,
   183         &memoryRequirements
   184         &memoryRequirements
   184     );
   185     );
   185 
   186 
   186     allocateInfo.allocationSize = UBO_BUFFER_SIZE;
   187     allocateInfo.allocationSize = UBO_ACTUAL_SIZE;
   187 
   188 
   188     if (!find_memory_type(ctx,
   189     if (!find_memory_type(ctx,
   189                           memoryRequirements.memoryTypeBits,
   190                           memoryRequirements.memoryTypeBits,
   190                           VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
   191                           VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
   191                           &allocateInfo.memoryTypeIndex))
   192                           &allocateInfo.memoryTypeIndex))
   207     );
   208     );
   208 
   209 
   209     ctx->vkMapMemory(*ctx->logical_device,
   210     ctx->vkMapMemory(*ctx->logical_device,
   210                      result->deviceMemory,
   211                      result->deviceMemory,
   211                      0,
   212                      0,
   212                      UBO_BUFFER_SIZE,
   213                      UBO_ACTUAL_SIZE,
   213                      0,
   214                      0,
   214                      (void**) &result->mapPointer
   215                      (void**) &result->mapPointer
   215     );
   216     );
   216 
   217 
   217     result->bufferSize = UBO_BUFFER_SIZE;
   218     result->bufferSize = UBO_ACTUAL_SIZE;
   218     result->currentBlockSize = 0;
   219     result->currentBlockSize = 0;
   219     result->dynamicOffset = 0;
   220     result->dynamicOffset = 0;
   220 
   221 
   221     return result;
   222     return result;
   222 } // create_ubo
   223 } // create_ubo
   299 
   300 
   300     ubo->dynamicOffset += ubo->currentBlockSize;
   301     ubo->dynamicOffset += ubo->currentBlockSize;
   301 
   302 
   302     ubo->currentBlockSize = next_highest_offset_alignment(uniform_data_size(shader));
   303     ubo->currentBlockSize = next_highest_offset_alignment(uniform_data_size(shader));
   303 
   304 
   304     if (ubo->dynamicOffset + ubo->currentBlockSize >= ubo->bufferSize)
   305     if (ubo->dynamicOffset + ubo->currentBlockSize >= ubo->bufferSize * ctx->frameIndex)
   305     {
   306     {
   306         set_error("UBO overflow!!");
   307         set_error("UBO overflow!!");
   307     } // if
   308     } // if
   308 
   309 
   309     contents = ubo->mapPointer + ubo->dynamicOffset;
   310     contents = ubo->mapPointer + ubo->dynamicOffset;
   446 
   447 
   447 MOJOSHADER_vkContext *MOJOSHADER_vkCreateContext(
   448 MOJOSHADER_vkContext *MOJOSHADER_vkCreateContext(
   448     VkInstance *instance,
   449     VkInstance *instance,
   449     VkPhysicalDevice *physical_device,
   450     VkPhysicalDevice *physical_device,
   450     VkDevice *logical_device,
   451     VkDevice *logical_device,
   451     int frames_in_flight,
       
   452     PFN_MOJOSHADER_vkGetInstanceProcAddr instance_lookup,
   452     PFN_MOJOSHADER_vkGetInstanceProcAddr instance_lookup,
   453     PFN_MOJOSHADER_vkGetDeviceProcAddr device_lookup,
   453     PFN_MOJOSHADER_vkGetDeviceProcAddr device_lookup,
   454     unsigned int graphics_queue_family_index,
   454     unsigned int graphics_queue_family_index,
   455     unsigned int max_uniform_buffer_range,
   455     unsigned int max_uniform_buffer_range,
   456     unsigned int min_uniform_buffer_offset_alignment,
   456     unsigned int min_uniform_buffer_offset_alignment,
   477     resultCtx->instance = (VkInstance*) instance;
   477     resultCtx->instance = (VkInstance*) instance;
   478     resultCtx->physical_device = (VkPhysicalDevice*) physical_device;
   478     resultCtx->physical_device = (VkPhysicalDevice*) physical_device;
   479     resultCtx->logical_device = (VkDevice*) logical_device;
   479     resultCtx->logical_device = (VkDevice*) logical_device;
   480     resultCtx->instance_proc_lookup = (PFN_vkGetInstanceProcAddr) instance_lookup;
   480     resultCtx->instance_proc_lookup = (PFN_vkGetInstanceProcAddr) instance_lookup;
   481     resultCtx->device_proc_lookup = (PFN_vkGetDeviceProcAddr) device_lookup;
   481     resultCtx->device_proc_lookup = (PFN_vkGetDeviceProcAddr) device_lookup;
   482     resultCtx->frames_in_flight = frames_in_flight;
   482     resultCtx->frameIndex = 0;
   483     resultCtx->graphics_queue_family_index = graphics_queue_family_index;
   483     resultCtx->graphics_queue_family_index = graphics_queue_family_index;
   484     resultCtx->maxUniformBufferRange = max_uniform_buffer_range;
   484     resultCtx->maxUniformBufferRange = max_uniform_buffer_range;
   485     resultCtx->minUniformBufferOffsetAlignment = min_uniform_buffer_offset_alignment;
   485     resultCtx->minUniformBufferOffsetAlignment = min_uniform_buffer_offset_alignment;
   486 
   486 
   487     lookup_entry_points(resultCtx);
   487     lookup_entry_points(resultCtx);
   776     *psize = get_uniform_size(ctx->bound_program->pixelShader);
   776     *psize = get_uniform_size(ctx->bound_program->pixelShader);
   777 } // MOJOSHADER_vkGetUniformBuffers
   777 } // MOJOSHADER_vkGetUniformBuffers
   778 
   778 
   779 void MOJOSHADER_vkEndFrame()
   779 void MOJOSHADER_vkEndFrame()
   780 {
   780 {
   781     ctx->vertUboBuffer->dynamicOffset = 0;
   781     ctx->frameIndex = (ctx->frameIndex + 1) % 2;
       
   782 
       
   783     // Reset counters
       
   784     // Offset by size of buffer to simulate "rotating" the buffers
       
   785     ctx->vertUboBuffer->dynamicOffset = UBO_BUFFER_SIZE * ctx->frameIndex;
   782     ctx->vertUboBuffer->currentBlockSize = 0;
   786     ctx->vertUboBuffer->currentBlockSize = 0;
   783     ctx->fragUboBuffer->dynamicOffset = 0;
   787     ctx->fragUboBuffer->dynamicOffset = UBO_BUFFER_SIZE * ctx->frameIndex;
   784     ctx->fragUboBuffer->currentBlockSize = 0;
   788     ctx->fragUboBuffer->currentBlockSize = 0;
   785 } // MOJOSHADER_VkEndFrame
   789 } // MOJOSHADER_VkEndFrame
   786 
   790 
   787 int MOJOSHADER_vkGetVertexAttribLocation(MOJOSHADER_vkShader *vert,
   791 int MOJOSHADER_vkGetVertexAttribLocation(MOJOSHADER_vkShader *vert,
   788                                          MOJOSHADER_usage usage, int index)
   792                                          MOJOSHADER_usage usage, int index)