andy-thomason / Vookoo

A set of utilities for taking the pain out of Vulkan in header only modern C++
MIT License
521 stars 52 forks source link

Add Shader Specialization Constant capability. #27

Closed FunMiles closed 3 years ago

FunMiles commented 4 years ago

I'm going to start feeling like I'm nagging. 😃 As I'm replacing my own homegrown library with Vookoo in my own project, I am discovering a few features I had implemented and are not available.

One of them are shader specialization constants. I understand that not many people use them, but for me they allow me to write mini-uber-shaders to handle many of my cases (in scientific visualization, I may have a variable number of attributes and rather than writing one shader per case, the constant specialization allows me to write a single shader handling all cases).

In my own library, I had attached the specialization constants to the shader. In Vookoo, the role of that object is the ShaderModule right now. The vku::ShaderModule contains a vk::UniqueShaderModule. Thus it seems a bit inappropriate to try to attach the specialization directly to the vku::ShaderModule. Since the same UniqueShaderModule can be used with various values of the specialization constants. The specialization variable values need to be given when the pipeline is built. So I see taking one of two approaches:

  1. Pass the necessary info in the PipelineMaker::shader() method
  2. Replace the UniqueShaderModule in ShaderModule with a std::shared_ptr<UniqueShaderModule> and using a specialize method to create a lightweight SpecializedShaderModule that is passed to PipelineMaker::shader()

Any comments/suggestions?

FunMiles commented 4 years ago

I've taken a first pass at this. I've added an extra argument to PipelineMaker::shader(). I've got a demo in the teapot example. However the current version only supports 32bit integer type constants and the syntax is, maybe, a bit awkward, particularly if there is only one specialization constant. For example, to set variable 12 to 3 use: pm.shader(vk::ShaderStageFlagBits::eVertex, final_vert, { {12,3} });

See https://github.com/FunMiles/Vookoo/tree/shader_specialization

FunMiles commented 3 years ago

After using the resize equipped version from PR #28 I had to come back to this. I am going to make a PR. Maybe that will get a discussion about the syntax started.

FunMiles commented 3 years ago

This issue is closed, following the merge of commit c25c3addf9502e05ca47b84b608bdfb14c27fdc4