Lionel Landwerlin | 2d9f563 | 2022-01-08 01:12:47 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2015-2022 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2022 Valve Corporation |
| 3 | * Copyright (c) 2015-2022 LunarG, Inc. |
| 4 | * Copyright (C) 2015-2022 Google Inc. |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 5 | * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * Author: Courtney Goeltzenleuchter <courtneygo@google.com> |
| 20 | * Author: Tobin Ehlis <tobine@google.com> |
| 21 | * Author: Chris Forbes <chrisf@ijw.co.nz> |
| 22 | * Author: Mark Lobodzinski <mark@lunarg.com> |
| 23 | * Author: Dave Houlton <daveh@lunarg.com> |
| 24 | * Author: John Zulauf <jzulauf@lunarg.com> |
| 25 | * Author: Tobias Hector <tobias.hector@amd.com> |
Jeremy Gebben | 11af979 | 2021-08-20 10:20:09 -0600 | [diff] [blame] | 26 | * Author: Jeremy Gebben <jeremyg@lunarg.com> |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 27 | */ |
| 28 | #include "pipeline_state.h" |
| 29 | #include "descriptor_sets.h" |
| 30 | #include "cmd_buffer_state.h" |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 31 | #include "state_tracker.h" |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 32 | #include "shader_module.h" |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 33 | |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 34 | static bool HasWriteableDescriptor(const std::vector<PipelineStageState::DescriptorUse> &descriptor_uses) { |
| 35 | return std::any_of(descriptor_uses.begin(), descriptor_uses.end(), |
ziga-lunarg | 1a53505 | 2022-03-10 01:04:28 +0100 | [diff] [blame] | 36 | [](const PipelineStageState::DescriptorUse &use) { return use.second.is_writable; }); |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static bool HasAtomicDescriptor(const std::vector<PipelineStageState::DescriptorUse> &descriptor_uses) { |
| 40 | return std::any_of(descriptor_uses.begin(), descriptor_uses.end(), |
ziga-lunarg | 1a53505 | 2022-03-10 01:04:28 +0100 | [diff] [blame] | 41 | [](const PipelineStageState::DescriptorUse &use) { return use.second.is_atomic_operation; }); |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 42 | } |
| 43 | |
Jeremy Gebben | 3dfeacf | 2021-12-02 08:46:39 -0700 | [diff] [blame] | 44 | static bool WrotePrimitiveShadingRate(VkShaderStageFlagBits stage_flag, spirv_inst_iter entrypoint, |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 45 | const SHADER_MODULE_STATE *module_state) { |
Jeremy Gebben | 3dfeacf | 2021-12-02 08:46:39 -0700 | [diff] [blame] | 46 | bool primitiverate_written = false; |
| 47 | if (stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT || |
| 48 | stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) { |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 49 | for (const auto &set : module_state->GetBuiltinDecorationList()) { |
| 50 | auto insn = module_state->at(set.offset); |
Jeremy Gebben | 3dfeacf | 2021-12-02 08:46:39 -0700 | [diff] [blame] | 51 | if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) { |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 52 | primitiverate_written = module_state->IsBuiltInWritten(insn, entrypoint); |
Jeremy Gebben | 3dfeacf | 2021-12-02 08:46:39 -0700 | [diff] [blame] | 53 | } |
| 54 | if (primitiverate_written) { |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | return primitiverate_written; |
| 60 | } |
| 61 | |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 62 | PipelineStageState::PipelineStageState(const safe_VkPipelineShaderStageCreateInfo *stage, |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 63 | std::shared_ptr<const SHADER_MODULE_STATE> &module_state) |
| 64 | : module_state(module_state), |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 65 | create_info(stage), |
| 66 | stage_flag(stage->stage), |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 67 | entrypoint(module_state->FindEntrypoint(stage->pName, stage->stage)), |
| 68 | accessible_ids(module_state->MarkAccessibleIds(entrypoint)), |
| 69 | descriptor_uses(module_state->CollectInterfaceByDescriptorSlot(accessible_ids)), |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 70 | has_writable_descriptor(HasWriteableDescriptor(descriptor_uses)), |
Jeremy Gebben | 3dfeacf | 2021-12-02 08:46:39 -0700 | [diff] [blame] | 71 | has_atomic_descriptor(HasAtomicDescriptor(descriptor_uses)), |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 72 | wrote_primitive_shading_rate(WrotePrimitiveShadingRate(stage_flag, entrypoint, module_state.get())) {} |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 73 | |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 74 | // static |
| 75 | PIPELINE_STATE::StageStateVec PIPELINE_STATE::GetStageStates(const ValidationStateTracker &state_data, |
| 76 | const PIPELINE_STATE &pipe_state) { |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 77 | PIPELINE_STATE::StageStateVec stage_states; |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 78 | // shader stages need to be recorded in pipeline order |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 79 | const auto stages = pipe_state.GetShaderStages(); |
| 80 | |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 81 | for (uint32_t stage_idx = 0; stage_idx < 32; ++stage_idx) { |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 82 | bool stage_found = false; |
| 83 | const auto stage = static_cast<VkShaderStageFlagBits>(1 << stage_idx); |
| 84 | for (const auto &shader_stage : stages) { |
| 85 | if (shader_stage.stage == stage) { |
| 86 | auto module = state_data.Get<SHADER_MODULE_STATE>(shader_stage.module); |
| 87 | if (!module) { |
| 88 | // If module is null and there is a VkShaderModuleCreateInfo in the pNext chain of the stage info, then this |
| 89 | // module is part of a library and the state must be created |
| 90 | const auto shader_ci = LvlFindInChain<VkShaderModuleCreateInfo>(shader_stage.pNext); |
| 91 | if (shader_ci) { |
| 92 | const uint32_t unique_shader_id = 0; // TODO GPU-AV rework required to get this value properly |
| 93 | module = state_data.CreateShaderModuleState(*shader_ci, unique_shader_id); |
| 94 | } else { |
| 95 | // TODO This should be an error of some sort. Just assert for now |
| 96 | assert(false); |
| 97 | } |
| 98 | } |
| 99 | stage_states.emplace_back(&shader_stage, module); |
| 100 | stage_found = true; |
| 101 | } |
| 102 | } |
| 103 | if (!stage_found) { |
| 104 | // Check if stage has been supplied by a library |
| 105 | switch (stage) { |
| 106 | case VK_SHADER_STAGE_VERTEX_BIT: |
| 107 | if (pipe_state.pre_raster_state && pipe_state.pre_raster_state->vertex_shader) { |
| 108 | stage_states.emplace_back(pipe_state.pre_raster_state->vertex_shader_ci, |
| 109 | pipe_state.pre_raster_state->vertex_shader); |
| 110 | } |
| 111 | break; |
| 112 | case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: |
| 113 | if (pipe_state.pre_raster_state && pipe_state.pre_raster_state->tessc_shader) { |
| 114 | stage_states.emplace_back(pipe_state.pre_raster_state->tessc_shader_ci, |
| 115 | pipe_state.pre_raster_state->tessc_shader); |
| 116 | } |
| 117 | break; |
| 118 | case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: |
| 119 | if (pipe_state.pre_raster_state && pipe_state.pre_raster_state->tesse_shader) { |
| 120 | stage_states.emplace_back(pipe_state.pre_raster_state->tesse_shader_ci, |
| 121 | pipe_state.pre_raster_state->tesse_shader); |
| 122 | } |
| 123 | break; |
| 124 | case VK_SHADER_STAGE_GEOMETRY_BIT: |
| 125 | if (pipe_state.pre_raster_state && pipe_state.pre_raster_state->geometry_shader) { |
| 126 | stage_states.emplace_back(pipe_state.pre_raster_state->geometry_shader_ci, |
| 127 | pipe_state.pre_raster_state->geometry_shader); |
| 128 | } |
| 129 | break; |
| 130 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
| 131 | if (pipe_state.fragment_shader_state && pipe_state.fragment_shader_state->fragment_shader) { |
| 132 | stage_states.emplace_back(pipe_state.fragment_shader_state->fragment_shader_ci.get(), |
| 133 | pipe_state.fragment_shader_state->fragment_shader); |
| 134 | } |
| 135 | break; |
| 136 | default: |
| 137 | // no-op |
| 138 | break; |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | return stage_states; |
| 143 | } |
| 144 | |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 145 | // static |
| 146 | PIPELINE_STATE::ActiveSlotMap PIPELINE_STATE::GetActiveSlots(const StageStateVec &stage_states) { |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 147 | PIPELINE_STATE::ActiveSlotMap active_slots; |
| 148 | for (const auto &stage : stage_states) { |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 149 | if (stage.entrypoint == stage.module_state->end()) { |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 150 | continue; |
| 151 | } |
| 152 | // Capture descriptor uses for the pipeline |
| 153 | for (const auto &use : stage.descriptor_uses) { |
| 154 | // While validating shaders capture which slots are used by the pipeline |
| 155 | auto &entry = active_slots[use.first.set][use.first.binding]; |
| 156 | entry.is_writable |= use.second.is_writable; |
| 157 | |
| 158 | auto &reqs = entry.reqs; |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 159 | reqs |= stage.module_state->DescriptorTypeToReqs(use.second.type_id); |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 160 | if (use.second.is_atomic_operation) reqs |= DESCRIPTOR_REQ_VIEW_ATOMIC_OPERATION; |
| 161 | if (use.second.is_sampler_implicitLod_dref_proj) reqs |= DESCRIPTOR_REQ_SAMPLER_IMPLICITLOD_DREF_PROJ; |
| 162 | if (use.second.is_sampler_bias_offset) reqs |= DESCRIPTOR_REQ_SAMPLER_BIAS_OFFSET; |
Lionel Landwerlin | bc7401b | 2021-12-07 15:43:05 +0200 | [diff] [blame] | 163 | if (use.second.is_read_without_format) reqs |= DESCRIPTOR_REQ_IMAGE_READ_WITHOUT_FORMAT; |
| 164 | if (use.second.is_write_without_format) reqs |= DESCRIPTOR_REQ_IMAGE_WRITE_WITHOUT_FORMAT; |
Lionel Landwerlin | cdbe868 | 2021-12-08 15:10:37 +0200 | [diff] [blame] | 165 | if (use.second.is_dref_operation) reqs |= DESCRIPTOR_REQ_IMAGE_DREF; |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 166 | |
| 167 | if (use.second.samplers_used_by_image.size()) { |
| 168 | if (use.second.samplers_used_by_image.size() > entry.samplers_used_by_image.size()) { |
| 169 | entry.samplers_used_by_image.resize(use.second.samplers_used_by_image.size()); |
| 170 | } |
| 171 | uint32_t image_index = 0; |
| 172 | for (const auto &samplers : use.second.samplers_used_by_image) { |
| 173 | for (const auto &sampler : samplers) { |
Jeremy Gebben | 856b8c6 | 2021-12-01 15:20:07 -0700 | [diff] [blame] | 174 | entry.samplers_used_by_image[image_index].emplace(sampler); |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 175 | } |
| 176 | ++image_index; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | return active_slots; |
| 182 | } |
| 183 | |
| 184 | static uint32_t GetMaxActiveSlot(const PIPELINE_STATE::ActiveSlotMap &active_slots) { |
| 185 | uint32_t max_active_slot = 0; |
| 186 | for (const auto &entry : active_slots) { |
| 187 | max_active_slot = std::max(max_active_slot, entry.first); |
| 188 | } |
| 189 | return max_active_slot; |
| 190 | } |
| 191 | |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 192 | static uint32_t GetActiveShaders(const PIPELINE_STATE::StageStateVec &stages) { |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 193 | uint32_t result = 0; |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 194 | for (const auto &stage : stages) { |
| 195 | result |= stage.stage_flag; |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 196 | } |
| 197 | return result; |
| 198 | } |
| 199 | |
| 200 | static layer_data::unordered_set<uint32_t> GetFSOutputLocations(const PIPELINE_STATE::StageStateVec &stage_states) { |
| 201 | layer_data::unordered_set<uint32_t> result; |
| 202 | for (const auto &stage : stage_states) { |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 203 | if (stage.entrypoint == stage.module_state->end()) { |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 204 | continue; |
| 205 | } |
| 206 | if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) { |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 207 | result = stage.module_state->CollectWritableOutputLocationinFS(stage.entrypoint); |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | } |
| 211 | return result; |
| 212 | } |
| 213 | |
| 214 | static VkPrimitiveTopology GetTopologyAtRasterizer(const PIPELINE_STATE::StageStateVec &stage_states, |
| 215 | const safe_VkPipelineInputAssemblyStateCreateInfo *assembly_state) { |
| 216 | VkPrimitiveTopology result = assembly_state ? assembly_state->topology : static_cast<VkPrimitiveTopology>(0); |
| 217 | for (const auto &stage : stage_states) { |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 218 | if (stage.entrypoint == stage.module_state->end()) { |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 219 | continue; |
| 220 | } |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 221 | auto stage_topo = stage.module_state->GetTopology(stage.entrypoint); |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 222 | if (stage_topo) { |
| 223 | result = *stage_topo; |
| 224 | } |
| 225 | } |
| 226 | return result; |
| 227 | } |
| 228 | |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 229 | // static |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 230 | std::shared_ptr<VertexInputState> PIPELINE_STATE::CreateVertexInputState(const PIPELINE_STATE &p, |
| 231 | const ValidationStateTracker &state, |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 232 | const safe_VkGraphicsPipelineCreateInfo &create_info) { |
| 233 | const auto lib_type = GetGraphicsLibType(create_info); |
| 234 | if (lib_type & VK_GRAPHICS_PIPELINE_LIBRARY_VERTEX_INPUT_INTERFACE_BIT_EXT) { // Vertex input graphics library |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 235 | return std::make_shared<VertexInputState>(p, create_info); |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | const auto link_info = LvlFindInChain<VkPipelineLibraryCreateInfoKHR>(create_info.pNext); |
| 239 | if (link_info) { |
Nathaniel Cesario | 0f5906a | 2022-04-05 11:10:18 -0600 | [diff] [blame] | 240 | auto ss = GetLibSubState<VK_GRAPHICS_PIPELINE_LIBRARY_VERTEX_INPUT_INTERFACE_BIT_EXT>(state, *link_info); |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 241 | if (ss) { |
| 242 | return ss; |
| 243 | } |
Nathaniel Cesario | fe8d786 | 2022-02-16 17:27:29 -0700 | [diff] [blame] | 244 | } else { |
| 245 | if (lib_type == static_cast<VkGraphicsPipelineLibraryFlagsEXT>(0)) { // Not a graphics library |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 246 | return std::make_shared<VertexInputState>(p, create_info); |
Nathaniel Cesario | fe8d786 | 2022-02-16 17:27:29 -0700 | [diff] [blame] | 247 | } |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // We shouldn't get here... |
| 251 | return {}; |
| 252 | } |
| 253 | |
| 254 | // static |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 255 | std::shared_ptr<PreRasterState> PIPELINE_STATE::CreatePreRasterState(const PIPELINE_STATE &p, const ValidationStateTracker &state, |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 256 | const safe_VkGraphicsPipelineCreateInfo &create_info) { |
| 257 | const auto lib_type = GetGraphicsLibType(create_info); |
| 258 | if (lib_type & VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT) { // Pre-raster graphics library |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 259 | return std::make_shared<PreRasterState>(p, state, create_info); |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | const auto link_info = LvlFindInChain<VkPipelineLibraryCreateInfoKHR>(create_info.pNext); |
| 263 | if (link_info) { |
Nathaniel Cesario | 0f5906a | 2022-04-05 11:10:18 -0600 | [diff] [blame] | 264 | auto ss = GetLibSubState<VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT>(state, *link_info); |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 265 | if (ss) { |
| 266 | return ss; |
| 267 | } |
Nathaniel Cesario | fe8d786 | 2022-02-16 17:27:29 -0700 | [diff] [blame] | 268 | } else { |
| 269 | if (lib_type == static_cast<VkGraphicsPipelineLibraryFlagsEXT>(0)) { // Not a graphics library |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 270 | return std::make_shared<PreRasterState>(p, state, create_info); |
Nathaniel Cesario | fe8d786 | 2022-02-16 17:27:29 -0700 | [diff] [blame] | 271 | } |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // We shouldn't get here... |
| 275 | return {}; |
| 276 | } |
| 277 | |
| 278 | // static |
| 279 | std::shared_ptr<FragmentShaderState> PIPELINE_STATE::CreateFragmentShaderState( |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 280 | const PIPELINE_STATE &p, const ValidationStateTracker &state, const VkGraphicsPipelineCreateInfo &create_info, |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 281 | const safe_VkGraphicsPipelineCreateInfo &safe_create_info) { |
| 282 | const auto lib_type = GetGraphicsLibType(create_info); |
| 283 | if (lib_type & VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT) { // Fragment shader graphics library |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 284 | return std::make_shared<FragmentShaderState>(p, state, create_info); |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | const auto link_info = LvlFindInChain<VkPipelineLibraryCreateInfoKHR>(create_info.pNext); |
| 288 | if (link_info) { |
Nathaniel Cesario | 0f5906a | 2022-04-05 11:10:18 -0600 | [diff] [blame] | 289 | auto ss = GetLibSubState<VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT>(state, *link_info); |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 290 | if (ss) { |
| 291 | return ss; |
| 292 | } |
Nathaniel Cesario | fe8d786 | 2022-02-16 17:27:29 -0700 | [diff] [blame] | 293 | } else { |
| 294 | if (lib_type == static_cast<VkGraphicsPipelineLibraryFlagsEXT>(0)) { // Not a graphics library |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 295 | return std::make_shared<FragmentShaderState>(p, state, safe_create_info); |
Nathaniel Cesario | fe8d786 | 2022-02-16 17:27:29 -0700 | [diff] [blame] | 296 | } |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // We shouldn't get here... |
| 300 | return {}; |
| 301 | } |
| 302 | |
| 303 | // static |
| 304 | // Pointers that should be ignored have been set to null in safe_create_info, but if this is a graphics library we need the "raw" |
| 305 | // create_info. |
| 306 | std::shared_ptr<FragmentOutputState> PIPELINE_STATE::CreateFragmentOutputState( |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 307 | const PIPELINE_STATE &p, const ValidationStateTracker &state, const VkGraphicsPipelineCreateInfo &create_info, |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 308 | const safe_VkGraphicsPipelineCreateInfo &safe_create_info) { |
| 309 | const auto lib_type = GetGraphicsLibType(create_info); |
| 310 | if (lib_type & VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT) { // Fragment output graphics library |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 311 | return std::make_shared<FragmentOutputState>(p, state, create_info); |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | const auto link_info = LvlFindInChain<VkPipelineLibraryCreateInfoKHR>(create_info.pNext); |
| 315 | if (link_info) { |
Nathaniel Cesario | 0f5906a | 2022-04-05 11:10:18 -0600 | [diff] [blame] | 316 | auto ss = GetLibSubState<VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT>(state, *link_info); |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 317 | if (ss) { |
| 318 | return ss; |
| 319 | } |
Nathaniel Cesario | fe8d786 | 2022-02-16 17:27:29 -0700 | [diff] [blame] | 320 | } else { |
| 321 | if (lib_type == static_cast<VkGraphicsPipelineLibraryFlagsEXT>(0)) { // Not a graphics library |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 322 | return std::make_shared<FragmentOutputState>(p, state, safe_create_info); |
Nathaniel Cesario | fe8d786 | 2022-02-16 17:27:29 -0700 | [diff] [blame] | 323 | } |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // We shouldn't get here... |
| 327 | return {}; |
| 328 | } |
| 329 | |
Nathaniel Cesario | 41f7c80 | 2022-03-01 14:28:58 -0700 | [diff] [blame^] | 330 | template <typename Substate> |
| 331 | void AppendDynamicStateFromSubstate(const Substate &substate, std::vector<VkDynamicState> &dyn_states, |
| 332 | VkPipelineDynamicStateCreateFlags &flags) { |
| 333 | if (substate) { |
| 334 | const auto *dyn_state = substate->parent.DynamicState(); |
| 335 | if (dyn_state) { |
| 336 | flags |= dyn_state->flags; |
| 337 | for (uint32_t i = 0; i < dyn_state->dynamicStateCount; ++i) { |
| 338 | const auto itr = std::find(dyn_states.cbegin(), dyn_states.cend(), dyn_state->pDynamicStates[i]); |
| 339 | if (itr == dyn_states.cend()) { |
| 340 | dyn_states.emplace_back(dyn_state->pDynamicStates[i]); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
Jeremy Gebben | 11af979 | 2021-08-20 10:20:09 -0600 | [diff] [blame] | 347 | PIPELINE_STATE::PIPELINE_STATE(const ValidationStateTracker *state_data, const VkGraphicsPipelineCreateInfo *pCreateInfo, |
| 348 | std::shared_ptr<const RENDER_PASS_STATE> &&rpstate, |
| 349 | std::shared_ptr<const PIPELINE_LAYOUT_STATE> &&layout) |
| 350 | : BASE_NODE(static_cast<VkPipeline>(VK_NULL_HANDLE), kVulkanObjectTypePipeline), |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 351 | create_info(pCreateInfo, rpstate), |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 352 | graphics_lib_type(GetGraphicsLibType(create_info.graphics)), |
Nathaniel Cesario | ef6e205 | 2022-02-28 14:09:45 -0700 | [diff] [blame] | 353 | vertex_input_state(CreateVertexInputState(*this, *state_data, create_info.graphics)), |
| 354 | pre_raster_state(CreatePreRasterState(*this, *state_data, create_info.graphics)), |
| 355 | fragment_shader_state(CreateFragmentShaderState(*this, *state_data, *pCreateInfo, create_info.graphics)), |
| 356 | fragment_output_state(CreateFragmentOutputState(*this, *state_data, *pCreateInfo, create_info.graphics)), |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 357 | stage_state(GetStageStates(*state_data, *this)), |
| 358 | fragmentShader_writable_output_location_list(GetFSOutputLocations(stage_state)), |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 359 | active_slots(GetActiveSlots(stage_state)), |
| 360 | max_active_slot(GetMaxActiveSlot(active_slots)), |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 361 | active_shaders(GetActiveShaders(stage_state)), |
| 362 | topology_at_rasterizer(GetTopologyAtRasterizer(stage_state, create_info.graphics.pInputAssemblyState)), |
Nathaniel Cesario | 81257cb | 2022-02-16 17:15:58 -0700 | [diff] [blame] | 363 | rp_state(rpstate) { |
| 364 | const auto link_info = LvlFindInChain<VkPipelineLibraryCreateInfoKHR>(PNext()); |
| 365 | if (link_info) { |
Nathaniel Cesario | 41f7c80 | 2022-03-01 14:28:58 -0700 | [diff] [blame^] | 366 | // accumulate dynamic state |
| 367 | // TODO is this correct? |
| 368 | auto *dyn_state_ci = const_cast<safe_VkPipelineDynamicStateCreateInfo *>(create_info.graphics.pDynamicState); |
| 369 | std::vector<VkDynamicState> dyn_states; |
| 370 | VkPipelineDynamicStateCreateFlags dyn_flags; |
| 371 | if (create_info.graphics.pDynamicState) { |
| 372 | std::copy(dyn_state_ci->pDynamicStates, dyn_state_ci->pDynamicStates + dyn_state_ci->dynamicStateCount, |
| 373 | std::back_inserter(dyn_states)); |
| 374 | dyn_flags = dyn_state_ci->flags; |
| 375 | } |
| 376 | AppendDynamicStateFromSubstate(vertex_input_state, dyn_states, dyn_flags); |
| 377 | AppendDynamicStateFromSubstate(pre_raster_state, dyn_states, dyn_flags); |
| 378 | AppendDynamicStateFromSubstate(fragment_shader_state, dyn_states, dyn_flags); |
| 379 | AppendDynamicStateFromSubstate(fragment_output_state, dyn_states, dyn_flags); |
| 380 | if (dyn_states.size() > 0) { |
| 381 | // We have dynamic state |
| 382 | if (!dyn_state_ci) { |
| 383 | // *All* dynamic state defined is coming from graphics libraries |
| 384 | // NOTE: heap allocation cleaned up in ~safe_VkGraphicsPipelineCreateInfo |
| 385 | dyn_state_ci = new safe_VkPipelineDynamicStateCreateInfo; |
| 386 | } |
| 387 | |
| 388 | if (dyn_state_ci->dynamicStateCount < dyn_states.size()) { |
| 389 | // There is dynamic state defined in libraries that the is not included in this pipeline's create info |
| 390 | dyn_state_ci->flags = dyn_flags; |
| 391 | dyn_state_ci->dynamicStateCount = static_cast<uint32_t>(dyn_states.size()); |
| 392 | // NOTE: heap allocation cleaned up in ~safe_VkPipelineDynamicStateCreateInfo |
| 393 | dyn_state_ci->pDynamicStates = new VkDynamicState[dyn_states.size()]; |
| 394 | std::copy(&dyn_states.front(), &dyn_states.front() + dyn_states.size(), |
| 395 | const_cast<VkDynamicState *>(dyn_state_ci->pDynamicStates)); |
| 396 | } |
| 397 | } |
| 398 | |
Nathaniel Cesario | 81257cb | 2022-02-16 17:15:58 -0700 | [diff] [blame] | 399 | const auto &exe_layout_state = state_data->Get<PIPELINE_LAYOUT_STATE>(create_info.graphics.layout); |
| 400 | const auto *exe_layout = exe_layout_state.get(); |
| 401 | const auto *pre_raster_layout = |
| 402 | (pre_raster_state && pre_raster_state->pipeline_layout) ? pre_raster_state->pipeline_layout.get() : nullptr; |
| 403 | const auto *fragment_shader_layout = (fragment_shader_state && fragment_shader_state->pipeline_layout) |
| 404 | ? fragment_shader_state->pipeline_layout.get() |
| 405 | : nullptr; |
| 406 | std::array<decltype(exe_layout), 3> layouts; |
| 407 | layouts[0] = pre_raster_layout; |
| 408 | layouts[1] = fragment_shader_layout; |
| 409 | layouts[2] = exe_layout; |
| 410 | merged_graphics_layout = std::make_shared<PIPELINE_LAYOUT_STATE>(layouts); |
Nathaniel Cesario | fe8d786 | 2022-02-16 17:27:29 -0700 | [diff] [blame] | 411 | |
| 412 | // TODO Could store the graphics_lib_type in the sub-state rather than searching for it again here. |
| 413 | // Or, could store a pointer back to the owning PIPELINE_STATE. |
| 414 | for (uint32_t i = 0; i < link_info->libraryCount; ++i) { |
| 415 | const auto &state = state_data->Get<PIPELINE_STATE>(link_info->pLibraries[i]); |
| 416 | if (state) { |
| 417 | graphics_lib_type |= state->graphics_lib_type; |
| 418 | } |
| 419 | } |
Nathaniel Cesario | 81257cb | 2022-02-16 17:15:58 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 422 | |
Jeremy Gebben | 11af979 | 2021-08-20 10:20:09 -0600 | [diff] [blame] | 423 | PIPELINE_STATE::PIPELINE_STATE(const ValidationStateTracker *state_data, const VkComputePipelineCreateInfo *pCreateInfo, |
| 424 | std::shared_ptr<const PIPELINE_LAYOUT_STATE> &&layout) |
| 425 | : BASE_NODE(static_cast<VkPipeline>(VK_NULL_HANDLE), kVulkanObjectTypePipeline), |
| 426 | create_info(pCreateInfo), |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 427 | stage_state(GetStageStates(*state_data, *this)), |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 428 | active_slots(GetActiveSlots(stage_state)), |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 429 | active_shaders(GetActiveShaders(stage_state)), |
| 430 | topology_at_rasterizer{}, |
| 431 | merged_graphics_layout(layout) { |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 432 | assert(active_shaders == VK_SHADER_STAGE_COMPUTE_BIT); |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 433 | } |
| 434 | |
Nathaniel Cesario | c8c11c1 | 2022-02-16 17:23:50 -0700 | [diff] [blame] | 435 | PIPELINE_STATE::PIPELINE_STATE(const ValidationStateTracker *state_data, const VkRayTracingPipelineCreateInfoKHR *pCreateInfo, |
| 436 | std::shared_ptr<const PIPELINE_LAYOUT_STATE> &&layout) |
| 437 | : BASE_NODE(static_cast<VkPipeline>(VK_NULL_HANDLE), kVulkanObjectTypePipeline), |
| 438 | create_info(pCreateInfo), |
| 439 | stage_state(GetStageStates(*state_data, *this)), |
| 440 | active_slots(GetActiveSlots(stage_state)), |
| 441 | active_shaders(GetActiveShaders(stage_state)), |
| 442 | topology_at_rasterizer{}, |
| 443 | merged_graphics_layout(std::move(layout)) { |
| 444 | assert(0 == (active_shaders & |
| 445 | ~(VK_SHADER_STAGE_RAYGEN_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | |
| 446 | VK_SHADER_STAGE_MISS_BIT_KHR | VK_SHADER_STAGE_INTERSECTION_BIT_KHR | VK_SHADER_STAGE_CALLABLE_BIT_KHR))); |
| 447 | } |
| 448 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 449 | PIPELINE_STATE::PIPELINE_STATE(const ValidationStateTracker *state_data, const VkRayTracingPipelineCreateInfoNV *pCreateInfo, |
Jeremy Gebben | 11af979 | 2021-08-20 10:20:09 -0600 | [diff] [blame] | 450 | std::shared_ptr<const PIPELINE_LAYOUT_STATE> &&layout) |
| 451 | : BASE_NODE(static_cast<VkPipeline>(VK_NULL_HANDLE), kVulkanObjectTypePipeline), |
| 452 | create_info(pCreateInfo), |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 453 | stage_state(GetStageStates(*state_data, *this)), |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 454 | active_slots(GetActiveSlots(stage_state)), |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 455 | active_shaders(GetActiveShaders(stage_state)), |
| 456 | topology_at_rasterizer{}, |
| 457 | merged_graphics_layout(std::move(layout)) { |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 458 | assert(0 == (active_shaders & |
| 459 | ~(VK_SHADER_STAGE_RAYGEN_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | |
| 460 | VK_SHADER_STAGE_MISS_BIT_KHR | VK_SHADER_STAGE_INTERSECTION_BIT_KHR | VK_SHADER_STAGE_CALLABLE_BIT_KHR))); |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 461 | } |
| 462 | |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 463 | void LAST_BOUND_STATE::UnbindAndResetPushDescriptorSet(CMD_BUFFER_STATE *cb_state, |
| 464 | std::shared_ptr<cvdescriptorset::DescriptorSet> &&ds) { |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 465 | if (push_descriptor_set) { |
| 466 | for (auto &ps : per_set) { |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 467 | if (ps.bound_descriptor_set == push_descriptor_set) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 468 | cb_state->RemoveChild(ps.bound_descriptor_set); |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 469 | ps.bound_descriptor_set.reset(); |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | } |
| 473 | cb_state->AddChild(ds); |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 474 | push_descriptor_set = std::move(ds); |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void LAST_BOUND_STATE::Reset() { |
| 478 | pipeline_state = nullptr; |
| 479 | pipeline_layout = VK_NULL_HANDLE; |
| 480 | if (push_descriptor_set) { |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 481 | push_descriptor_set->Destroy(); |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 482 | } |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 483 | push_descriptor_set.reset(); |
Jeremy Gebben | adb3eb0 | 2021-06-15 12:55:19 -0600 | [diff] [blame] | 484 | per_set.clear(); |
| 485 | } |