Um graphics pipeline Vulkan é monolítico — compilar shaders, embutir
vertex input, blend state, rasterizer e info de render-pass num
objeto. Qualquer mudança → rebuild completo do pipeline → hitch. VK_EXT_graphics_pipeline_library (GPL, core no 1.4) divide o pipeline em
quatro bibliotecas independentes — vertex input, pre-rasterização,
fragment shader, fragment output — cada uma compilada uma vez, depois linkada
num pipeline final em microssegundos.
As quatro bibliotecas
libs.txt
vertex input interface — vertex buffer bindings/attributes,
topology (ia*, inputAssembly)
pre-rasterization shaders — vertex/tess/geometry/mesh shaders +
viewport/scissor/rasterizer state
fragment shader — the fragment shader module + its
pipeline layout subset
fragment output interface — blend state, color formats, samples,
render-pass attachment infoCada uma é um VkPipeline criado com a flag eLibrary e
VkGraphicsPipelineLibraryCreateInfoEXT::flags nomeando qual pedaço ela
é. O pipeline final de draw é vkCreateGraphicsPipelines com
VkPipelineLibraryCreateInfoKHR::pLibraries listando as quatro.
O padrão
gpl.cpp
// compile each piece once — cached, reused across material variants:
vk::GraphicsPipelineLibraryCreateInfoEXT lib{};
lib.flags = vk::GraphicsPipelineLibraryFlagBitsEXT
::eFragmentShader; // or eVertexInputInterface /
// ePreRasterizationShaders /
// eFragmentOutputInterface
auto fragShaderLib = dev.createGraphicsPipelineUnique({}, gpci);
// link the final pipeline — microseconds, no shader compile:
vk::PipelineLibraryCreateInfoKHR link{};
link.libraryCount = 4;
link.pLibraries = libs; // {vertInput, preRaster, fragShader,
// fragOutput}
finalGpci.pNext = &link;
auto pipeline = dev.createGraphicsPipelineUnique(cache, finalGpci);Por que vence o vkCreateGraphicsPipelines direto
wins.txt
material variants — 100 materials × same vert/fragoutput → compile
100 fragShader libs + 3 shared libs, link 100
pipelines instead of 100 full compiles
dynamic state combo — a new blend mode doesn't recompile shaders —
just link a new fragmentOutput library
shader cache hits — unchanged pieces stay in the pipeline cache;
only the changed library recompilesInteração com pipelines monolíticos
- Pipelines GPL ainda funcionam com
vkCmdBindPipeline— o pipeline linkado é umVkPipelinenormal; as bibliotecas em si não são vinculadas diretamente. - Flags
eRetainLinkTimeOptimized/eRetainLinkTimeOptimizationno passo de link trocam velocidade de link-time vs. runtime — uma biblioteca de “link rápido” pode ser re-linkada otimizada depois. VkGraphicsPipelineLibraryCreateInfoEXTna criação da biblioteca — a criação final usaVkPipelineLibraryCreateInfoKHRpara referenciar as quatro.
Regras práticas
- Bit de feature
graphicsPipelineLibrary(core no 1.4, ou EXT em versões mais velhas) — maisVK_KHR_pipeline_librarypara o mecanismo de linking. - As quatro bibliotecas devem ser fornecidas ou o estado das faltantes
deve ser totalmente dinâmico — uma lib fragmentOutput ausente significa que blend
state deve ser
VK_DYNAMIC_STATE_*no pipeline final. - O pipeline layout é por biblioteca — os shaders de cada biblioteca veem só seu próprio subconjunto de layout; o pipeline final precisa de um layout que cubra a união.
- Otimização em link-time é opcional —
eLinkTimeOptimizationEXTna criação final deixa o driver fundir as bibliotecas; deixe desligada para o caminho de “link rápido”, ligada para o build otimizado. - Melhor pareado com
VK_EXT_shader_object— GPL para pipelines, shader objects para estado; juntos quase eliminam o problema de “compilar um pipeline por permutação”.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow