set sampler functions

submit_rewrite
cosmonaut 2020-12-18 21:35:21 -08:00
parent d193327760
commit bedc1f7196
4 changed files with 184 additions and 48 deletions

View File

@ -976,32 +976,36 @@ REFRESHAPI void REFRESH_PushShaderParamData(
/* Sets textures/samplers for use with the currently bound vertex shader. /* Sets textures/samplers for use with the currently bound vertex shader.
* *
* startIndex: The index at which to begin replacing sampler references. * NOTE:
* textures: A pointer to an array of textures. * The length of the passed arrays must be equal to the number
* samplers: A pointer to an array of samplers. * of sampler bindings specified by the shader.
* count: The length of the above arrays. *
* pipeline: The graphics pipeline to push shader data to.
* textures: A pointer to an array of textures.
* samplers: A pointer to an array of samplers.
*/ */
REFRESHAPI void REFRESH_SetVertexSamplers( REFRESHAPI void REFRESH_SetVertexSamplers(
REFRESH_Device *device, REFRESH_Device *device,
uint32_t startIndex, REFRESH_GraphicsPipeline *pipeline,
REFRESH_Texture *pTextures, REFRESH_Texture **pTextures,
REFRESH_Sampler *pSamplers, REFRESH_Sampler **pSamplers
uint32_t count
); );
/* Sets textures/samplers for use with the currently bound fragment shader. /* Sets textures/samplers for use with the currently bound fragment shader.
* *
* startIndex: The index at which to begin replacing sampler references. * NOTE:
* textures: A pointer to an array of textures. * The length of the passed arrays must be equal to the number
* samplers: A pointer to an array of samplers. * of sampler bindings specified by the shader.
* count: The length of the above arrays. *
* pipeline: The graphics pipeline to push shader data to.
* textures: A pointer to an array of textures.
* samplers: A pointer to an array of samplers.
*/ */
REFRESHAPI void REFRESH_SetFragmentSamplers( REFRESHAPI void REFRESH_SetFragmentSamplers(
REFRESH_Device *device, REFRESH_Device *device,
uint32_t startIndex, REFRESH_GraphicsPipeline *pipeline,
REFRESH_Texture *pTextures, REFRESH_Texture **pTextures,
REFRESH_Sampler *pSamplers, REFRESH_Sampler **pSamplers
uint32_t count
); );
/* Getters */ /* Getters */

View File

@ -551,35 +551,31 @@ void REFRESH_PushShaderParamData(
void REFRESH_SetVertexSamplers( void REFRESH_SetVertexSamplers(
REFRESH_Device *device, REFRESH_Device *device,
uint32_t startIndex, REFRESH_GraphicsPipeline *pipeline,
REFRESH_Texture *pTextures, REFRESH_Texture **pTextures,
REFRESH_Sampler *pSamplers, REFRESH_Sampler **pSamplers
uint32_t count
) { ) {
NULL_RETURN(device); NULL_RETURN(device);
device->SetVertexSamplers( device->SetVertexSamplers(
device->driverData, device->driverData,
startIndex, pipeline,
pTextures, pTextures,
pSamplers, pSamplers
count
); );
} }
void REFRESH_SetFragmentSamplers( void REFRESH_SetFragmentSamplers(
REFRESH_Device *device, REFRESH_Device *device,
uint32_t startIndex, REFRESH_GraphicsPipeline *pipeline,
REFRESH_Texture *pTextures, REFRESH_Texture **pTextures,
REFRESH_Sampler *pSamplers, REFRESH_Sampler **pSamplers
uint32_t count
) { ) {
NULL_RETURN(device); NULL_RETURN(device);
device->SetFragmentSamplers( device->SetFragmentSamplers(
device->driverData, device->driverData,
startIndex, pipeline,
pTextures, pTextures,
pSamplers, pSamplers
count
); );
} }

View File

@ -365,18 +365,16 @@ struct REFRESH_Device
void(*SetVertexSamplers)( void(*SetVertexSamplers)(
REFRESH_Renderer *driverData, REFRESH_Renderer *driverData,
uint32_t startIndex, REFRESH_GraphicsPipeline *pipeline,
REFRESH_Texture *pTextures, REFRESH_Texture **pTextures,
REFRESH_Sampler *pSamplers, REFRESH_Sampler **pSamplers
uint32_t count
); );
void(*SetFragmentSamplers)( void(*SetFragmentSamplers)(
REFRESH_Renderer *driverData, REFRESH_Renderer *driverData,
uint32_t startIndex, REFRESH_GraphicsPipeline *pipeline,
REFRESH_Texture *pTextures, REFRESH_Texture **pTextures,
REFRESH_Sampler *pSamplers, REFRESH_Sampler **pSamplers
uint32_t count
); );
/* Getters */ /* Getters */

View File

@ -670,6 +670,13 @@ typedef struct VulkanRenderer
VkCommandBuffer currentCommandBuffer; VkCommandBuffer currentCommandBuffer;
uint32_t numActiveCommands; uint32_t numActiveCommands;
/*
* TODO: we can get rid of this reference when we
* come up with a clever descriptor set reuse system
*/
VkDescriptorPool *descriptorPools;
uint32_t descriptorPoolCount;
VkDescriptorSetLayout vertexParamLayout; VkDescriptorSetLayout vertexParamLayout;
VkDescriptorSetLayout fragmentParamLayout; VkDescriptorSetLayout fragmentParamLayout;
@ -738,6 +745,10 @@ typedef struct VulkanGraphicsPipeline
{ {
VkPipeline pipeline; VkPipeline pipeline;
VkDescriptorPool descriptorPool; VkDescriptorPool descriptorPool;
VkDescriptorSetLayout vertexSamplerLayout;
uint32_t vertexSamplerBindingCount;
VkDescriptorSetLayout fragmentSamplerLayout;
uint32_t fragmentSamplerBindingCount;
} VulkanGraphicsPipeline; } VulkanGraphicsPipeline;
/* Forward declarations */ /* Forward declarations */
@ -2346,6 +2357,11 @@ static REFRESH_GraphicsPipeline* VULKAN_CreateGraphicsPipeline(
&pipelineLayout &pipelineLayout
); );
graphicsPipeline->vertexSamplerLayout = setLayouts[0];
graphicsPipeline->fragmentSamplerLayout = setLayouts[1];
graphicsPipeline->vertexSamplerBindingCount = pipelineCreateInfo->pipelineLayoutCreateInfo.vertexSamplerBindingCount;
graphicsPipeline->fragmentSamplerBindingCount = pipelineCreateInfo->pipelineLayoutCreateInfo.fragmentSamplerBindingCount;
/* Pipeline */ /* Pipeline */
vkPipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; vkPipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
@ -2409,6 +2425,13 @@ static REFRESH_GraphicsPipeline* VULKAN_CreateGraphicsPipeline(
return NULL; return NULL;
} }
renderer->descriptorPools = SDL_realloc(
renderer->descriptorPools,
renderer->descriptorPoolCount + 1
);
renderer->descriptorPools[renderer->descriptorPoolCount + 1] =
graphicsPipeline->descriptorPool;
return (REFRESH_GraphicsPipeline*) graphicsPipeline; return (REFRESH_GraphicsPipeline*) graphicsPipeline;
} }
@ -3598,22 +3621,132 @@ static void VULKAN_PushShaderParamData(
static void VULKAN_SetVertexSamplers( static void VULKAN_SetVertexSamplers(
REFRESH_Renderer *driverData, REFRESH_Renderer *driverData,
uint32_t startIndex, REFRESH_GraphicsPipeline *pipeline,
REFRESH_Texture *pTextures, REFRESH_Texture **pTextures,
REFRESH_Sampler *pSamplers, REFRESH_Sampler **pSamplers
uint32_t count
) { ) {
SDL_assert(0); /* TODO: we can defer and batch these */
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo;
VkDescriptorSet descriptorSet;
VkWriteDescriptorSet *writeDescriptorSets;
VkDescriptorImageInfo *descriptorImageInfos;
VulkanTexture *currentTexture;
VkSampler currentSampler;
uint32_t i;
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
VulkanGraphicsPipeline *graphicsPipeline = (VulkanGraphicsPipeline*) pipeline;
writeDescriptorSets = SDL_stack_alloc(VkWriteDescriptorSet, graphicsPipeline->vertexSamplerBindingCount);
descriptorImageInfos = SDL_stack_alloc(VkDescriptorImageInfo, graphicsPipeline->vertexSamplerBindingCount);
descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
descriptorSetAllocateInfo.pNext = NULL;
descriptorSetAllocateInfo.descriptorSetCount = 1;
descriptorSetAllocateInfo.descriptorPool = graphicsPipeline->descriptorPool;
descriptorSetAllocateInfo.pSetLayouts = &graphicsPipeline->vertexSamplerLayout;
renderer->vkAllocateDescriptorSets(
renderer->logicalDevice,
&descriptorSetAllocateInfo,
&descriptorSet
);
for (i = 0; i < graphicsPipeline->vertexSamplerBindingCount; i += 1)
{
currentTexture = (VulkanTexture*) pTextures[i];
currentSampler = (VkSampler) pSamplers[i];
descriptorImageInfos[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
descriptorImageInfos[i].imageView = currentTexture->view;
descriptorImageInfos[i].sampler = currentSampler;
writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSets[i].pNext = NULL;
writeDescriptorSets[i].dstSet = descriptorSet;
writeDescriptorSets[i].dstBinding = i;
writeDescriptorSets[i].dstArrayElement = 0;
writeDescriptorSets[i].descriptorCount = 1;
writeDescriptorSets[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
writeDescriptorSets[i].pImageInfo = &descriptorImageInfos[i];
}
renderer->vkUpdateDescriptorSets(
renderer->logicalDevice,
graphicsPipeline->vertexSamplerBindingCount,
writeDescriptorSets,
0,
NULL
);
SDL_stack_free(writeDescriptorSets);
SDL_stack_free(descriptorImageInfos);
} }
static void VULKAN_SetFragmentSamplers( static void VULKAN_SetFragmentSamplers(
REFRESH_Renderer *driverData, REFRESH_Renderer *driverData,
uint32_t startIndex, REFRESH_GraphicsPipeline *pipeline,
REFRESH_Texture *pTextures, REFRESH_Texture **pTextures,
REFRESH_Sampler *pSamplers, REFRESH_Sampler **pSamplers
uint32_t count
) { ) {
SDL_assert(0); /* TODO: we can defer and batch these */
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo;
VkDescriptorSet descriptorSet;
VkWriteDescriptorSet *writeDescriptorSets;
VkDescriptorImageInfo *descriptorImageInfos;
VulkanTexture *currentTexture;
VkSampler currentSampler;
uint32_t i;
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
VulkanGraphicsPipeline *graphicsPipeline = (VulkanGraphicsPipeline*) pipeline;
writeDescriptorSets = SDL_stack_alloc(VkWriteDescriptorSet, graphicsPipeline->fragmentSamplerBindingCount);
descriptorImageInfos = SDL_stack_alloc(VkDescriptorImageInfo, graphicsPipeline->fragmentSamplerBindingCount);
descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
descriptorSetAllocateInfo.pNext = NULL;
descriptorSetAllocateInfo.descriptorSetCount = 1;
descriptorSetAllocateInfo.descriptorPool = graphicsPipeline->descriptorPool;
descriptorSetAllocateInfo.pSetLayouts = &graphicsPipeline->fragmentSamplerLayout;
renderer->vkAllocateDescriptorSets(
renderer->logicalDevice,
&descriptorSetAllocateInfo,
&descriptorSet
);
for (i = 0; i < graphicsPipeline->fragmentSamplerBindingCount; i += 1)
{
currentTexture = (VulkanTexture*) pTextures[i];
currentSampler = (VkSampler) pSamplers[i];
descriptorImageInfos[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
descriptorImageInfos[i].imageView = currentTexture->view;
descriptorImageInfos[i].sampler = currentSampler;
writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSets[i].pNext = NULL;
writeDescriptorSets[i].dstSet = descriptorSet;
writeDescriptorSets[i].dstBinding = i;
writeDescriptorSets[i].dstArrayElement = 0;
writeDescriptorSets[i].descriptorCount = 1;
writeDescriptorSets[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
writeDescriptorSets[i].pImageInfo = &descriptorImageInfos[i];
}
renderer->vkUpdateDescriptorSets(
renderer->logicalDevice,
graphicsPipeline->fragmentSamplerBindingCount,
writeDescriptorSets,
0,
NULL
);
SDL_stack_free(writeDescriptorSets);
SDL_stack_free(descriptorImageInfos);
} }
static void VULKAN_GetTextureData2D( static void VULKAN_GetTextureData2D(
@ -5079,6 +5212,11 @@ static REFRESH_Device* VULKAN_CreateDevice(
return NULL; return NULL;
} }
/* Descriptor Pools */
renderer->descriptorPools = NULL;
renderer->descriptorPoolCount = 0;
/* Threading */ /* Threading */
renderer->allocatorLock = SDL_CreateMutex(); renderer->allocatorLock = SDL_CreateMutex();