title: Vulkan material resource binding
body: <p>I want to render a scene with different materials and multiple objects. When I started to implement it myself, I found it difficult to bind material resources before iterating over the objects. I originally plan to use one descriptor set for each material that contains several parameters and textures and pass an array of them to our shader, but this is not possible because the glsl shader requires me to explicitly refer to the descriptor sets and bindings like</p>
  <pre><code>location(set = 0, binding = 0) uniform someUniformBuffer/someTextureSampler
  </code></pre>
  <p>Also, the number of descriptor sets and bindings need to be hard-coded into my shader code. Therefore, I do not think it would be possible to do it this way.</p>
  <p>After some searching I found two existing ways to bind multiple material resources, basically from <a href="https://github.com/SaschaWillems/Vulkan/blob/master/examples/texturearray/texturearray.cpp" rel="nofollow noreferrer">SaschaWillems Repo</a> and <a href="https://stackoverflow.com/questions/36772607/vulkan-texture-rendering-on-multiple-meshes">Vulkan texture rendering on multiple meshes</a>:</p>
  <ol>
  <li>Group the faces using the same material of an object, and bind material resources when drawing those faces (You have to do this because the materialId is specified per face). Then the framework would be like</li>
  </ol>
  <pre><code>// example for typical loops in rendering
  for each view {
    bind view resources          // camera, environment...
    for each shader {
      bind shader pipeline  
      bind shader resources      // shader control values
      for each object {
        bind object resources  // object transforms
        for each material group{
          bind material resources // material parameters and textures
          draw faces
        }
      }
    }
  }
  </code></pre>
  <p>This way works on all devices with Vulkan support, but is a little bit complicated because I will have to group the faces by their materialId when building my objects. This is similar to the framework proposed by The NVIDIA Post <a href="https://developer.nvidia.com/vulkan-shader-resource-binding" rel="nofollow noreferrer">Vulkan Shader Resource Binding</a> where they group the objects of the same material (In my case I have multiple materials inside the same object).</p>
  <ol start="2">
  <li><p>Use an array of textures and a separate sampler. In this case, I will have a bound for my texture array which could be as low as 128.</p>
  </li>
  <li><p>Use an array of combined samplers and use</p>
  </li>
  </ol>
  <pre><code>location(set = 0, binding = 0) uniform sampler2DArray
  </code></pre>
  <p>in my shader to deal with an array of textures. This requires the physical device feature <code>shaderSampledImageArrayDynamicIndexing </code> which is not supported by most mobile devices.</p>
  <p>In this case, I think I prefer the first method. However, in solution 1, the same material could be bound multiple times for different objects using this material. I am wondering if this would impact the performance and how to improve it considering that each object I want to draw has several materials for different faces.</p>