Jump to content

Vulkan Shader Uniforms

Josh

152 views

In Vulkan all shader uniforms are packed into a single structure declared in a GLSL shader like this:

layout(push_constant) uniform pushBlock
{
    vec4 color;
}
pushConstantsBlock;

You can add more values, but the shaders all need to use the same structure, and it needs to be declared exactly the same inside the program.

Like everything else in Vulkan, shaders are set inside a command buffer. But these shader values are likely to be constantly changing each frame, so how do you handle this? The answer is to have a pool of command buffers and retrieve an available one when needed to perform this operation.

void Vk::SetShaderGlobals(const VkShaderGlobals& shaderglobals)
{
	VkCommandBuffer commandbuffer;
	VkFence fence;
	commandbuffermanager->GetManagedCommandBuffer(commandbuffer,fence);
		
	VkCommandBufferBeginInfo beginInfo = {};
	beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
	beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
		
	vkBeginCommandBuffer(commandbuffer, &beginInfo);
	vkCmdPushConstants(commandbuffer, pipelineLayout, VK_SHADER_STAGE_ALL, 0, sizeof(shaderglobals), &shaderglobals);
	vkEndCommandBuffer(commandbuffer);

	VkSubmitInfo submitInfo = {};
	submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
	submitInfo.commandBufferCount = 1;
	submitInfo.pCommandBuffers = &commandbuffer;

	vkQueueSubmit(devicequeue[0], 1, &submitInfo, fence);
}

I now have a rectangle that flashes on and off based on the current time, which is fed in through a shader uniform structure. Now at 1500 lines of code.

You can download my command buffer manager code at the Leadwerks Github page:

  • Like 1


0 Comments


Recommended Comments

There are no comments to display.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...