Jump to content
  • entries
    945
  • comments
    5,899
  • views
    930,073

Vulkan Shader Uniforms


Josh

1,573 views

 Share

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 2
 Share

3 Comments


Recommended Comments

Yes, I am allocating one 4x4 matrix in the material structure to store user parameters. These will be defined in the shader family, so you can set them with a string. They will be loaded from the material and used in the shader.

For post-processing effects, I am doing something similar.

Something like this:

material->SetParameter("normalstrength", 2.0);

 

  • Like 1
  • Upvote 1
Link to comment

So that would mean a limit of 16 floats per material?  That's not too bad.  I guess we can store other data in a texture or something if we need to.

Link to comment
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...