Nathaniel Cesario | 9ee6c57 | 2022-04-06 10:56:19 -0600 | [diff] [blame^] | 1 | /* Copyright (c) 2015-2017, 2019-2022 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2017, 2019-2022 Valve Corporation |
| 3 | * Copyright (c) 2015-2017, 2019-2022 LunarG, Inc. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | * Author: Nathaniel Cesario <nathaniel@lunarg.com> |
| 18 | */ |
| 19 | |
Nathaniel Cesario | 5239844 | 2022-02-15 16:33:46 -0700 | [diff] [blame] | 20 | #include "pipeline_sub_state.h" |
| 21 | |
| 22 | #include "state_tracker.h" |
| 23 | |
| 24 | VertexInputState::VertexInputState(const safe_VkGraphicsPipelineCreateInfo &create_info) |
| 25 | : input_state(create_info.pVertexInputState), input_assembly_state(create_info.pInputAssemblyState) { |
| 26 | if (create_info.pVertexInputState) { |
| 27 | const auto *vici = create_info.pVertexInputState; |
| 28 | if (vici->vertexBindingDescriptionCount) { |
| 29 | const auto count = vici->vertexBindingDescriptionCount; |
| 30 | binding_descriptions.reserve(count); |
| 31 | binding_to_index_map.reserve(count); |
| 32 | |
| 33 | for (uint32_t i = 0; i < count; i++) { |
| 34 | binding_descriptions.emplace_back(vici->pVertexBindingDescriptions[i]); |
| 35 | binding_to_index_map[binding_descriptions.back().binding] = i; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | if (vici->vertexAttributeDescriptionCount) { |
| 40 | vertex_attribute_descriptions.reserve(vici->vertexAttributeDescriptionCount); |
| 41 | std::copy(vici->pVertexAttributeDescriptions, |
| 42 | vici->pVertexAttributeDescriptions + vici->vertexAttributeDescriptionCount, |
| 43 | std::back_inserter(vertex_attribute_descriptions)); |
| 44 | } |
| 45 | |
| 46 | vertex_attribute_alignments.reserve(vertex_attribute_descriptions.size()); |
| 47 | for (const auto &attr : vertex_attribute_descriptions) { |
| 48 | VkDeviceSize vtx_attrib_req_alignment = FormatElementSize(attr.format); |
| 49 | if (FormatElementIsTexel(attr.format)) { |
| 50 | vtx_attrib_req_alignment = SafeDivision(vtx_attrib_req_alignment, FormatComponentCount(attr.format)); |
| 51 | } |
| 52 | vertex_attribute_alignments.push_back(vtx_attrib_req_alignment); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | PreRasterState::PreRasterState(const ValidationStateTracker &dev_data, const safe_VkGraphicsPipelineCreateInfo &create_info) |
| 58 | : subpass(create_info.subpass) { |
| 59 | pipeline_layout = dev_data.Get<PIPELINE_LAYOUT_STATE>(create_info.layout); |
| 60 | |
| 61 | viewport_state = create_info.pViewportState; |
| 62 | |
| 63 | rp_state = dev_data.Get<RENDER_PASS_STATE>(create_info.renderPass); |
| 64 | |
| 65 | raster_state = create_info.pRasterizationState; |
| 66 | |
| 67 | tess_create_info = create_info.pTessellationState; |
| 68 | |
| 69 | for (uint32_t i = 0; i < create_info.stageCount; ++i) { |
| 70 | // TODO might need to filter out more than just fragment shaders here |
| 71 | if (create_info.pStages[i].stage != VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 72 | auto module_state = dev_data.Get<SHADER_MODULE_STATE>(create_info.pStages[i].module); |
| 73 | if (!module_state) { |
| 74 | // If module is null and there is a VkShaderModuleCreateInfo in the pNext chain of the stage info, then this |
| 75 | // module is part of a library and the state must be created |
| 76 | const auto shader_ci = LvlFindInChain<VkShaderModuleCreateInfo>(create_info.pStages[i].pNext); |
| 77 | if (shader_ci) { |
| 78 | const uint32_t unique_shader_id = 0; // TODO GPU-AV rework required to get this value properly |
| 79 | module_state = dev_data.CreateShaderModuleState(*shader_ci, unique_shader_id); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (module_state) { |
| 84 | const auto *stage_ci = &create_info.pStages[i]; |
| 85 | switch (create_info.pStages[i].stage) { |
| 86 | case VK_SHADER_STAGE_VERTEX_BIT: |
| 87 | vertex_shader = std::move(module_state); |
| 88 | vertex_shader_ci = stage_ci; |
| 89 | break; |
| 90 | case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: |
| 91 | tessc_shader = std::move(module_state); |
| 92 | tessc_shader_ci = stage_ci; |
| 93 | break; |
| 94 | case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: |
| 95 | tesse_shader = std::move(module_state); |
| 96 | tesse_shader_ci = stage_ci; |
| 97 | break; |
| 98 | case VK_SHADER_STAGE_GEOMETRY_BIT: |
| 99 | geometry_shader = std::move(module_state); |
| 100 | geometry_shader_ci = stage_ci; |
| 101 | break; |
| 102 | // TODO mesh shaders? |
| 103 | default: |
| 104 | // TODO is this an error? |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | std::unique_ptr<const safe_VkPipelineColorBlendStateCreateInfo> ToSafeColorBlendState( |
| 113 | const safe_VkPipelineColorBlendStateCreateInfo &cbs) { |
| 114 | // This is needlessly copied here. Might better to make this a plain pointer, with an optional "backing unique_ptr" |
| 115 | return layer_data::make_unique<const safe_VkPipelineColorBlendStateCreateInfo>(cbs); |
| 116 | } |
| 117 | std::unique_ptr<const safe_VkPipelineColorBlendStateCreateInfo> ToSafeColorBlendState( |
| 118 | const VkPipelineColorBlendStateCreateInfo &cbs) { |
| 119 | return layer_data::make_unique<const safe_VkPipelineColorBlendStateCreateInfo>(&cbs); |
| 120 | } |
| 121 | std::unique_ptr<const safe_VkPipelineMultisampleStateCreateInfo> ToSafeMultisampleState( |
| 122 | const safe_VkPipelineMultisampleStateCreateInfo &cbs) { |
| 123 | // This is needlessly copied here. Might better to make this a plain pointer, with an optional "backing unique_ptr" |
| 124 | return layer_data::make_unique<const safe_VkPipelineMultisampleStateCreateInfo>(cbs); |
| 125 | } |
| 126 | std::unique_ptr<const safe_VkPipelineMultisampleStateCreateInfo> ToSafeMultisampleState( |
| 127 | const VkPipelineMultisampleStateCreateInfo &cbs) { |
| 128 | return layer_data::make_unique<const safe_VkPipelineMultisampleStateCreateInfo>(&cbs); |
| 129 | } |
| 130 | std::unique_ptr<const safe_VkPipelineDepthStencilStateCreateInfo> ToSafeDepthStencilState( |
| 131 | const safe_VkPipelineDepthStencilStateCreateInfo &cbs) { |
| 132 | // This is needlessly copied here. Might better to make this a plain pointer, with an optional "backing unique_ptr" |
| 133 | return layer_data::make_unique<const safe_VkPipelineDepthStencilStateCreateInfo>(cbs); |
| 134 | } |
| 135 | std::unique_ptr<const safe_VkPipelineDepthStencilStateCreateInfo> ToSafeDepthStencilState( |
| 136 | const VkPipelineDepthStencilStateCreateInfo &cbs) { |
| 137 | return layer_data::make_unique<const safe_VkPipelineDepthStencilStateCreateInfo>(&cbs); |
| 138 | } |
| 139 | std::unique_ptr<const safe_VkPipelineShaderStageCreateInfo> ToShaderStageCI(const safe_VkPipelineShaderStageCreateInfo &cbs) { |
| 140 | // This is needlessly copied here. Might better to make this a plain pointer, with an optional "backing unique_ptr" |
| 141 | return layer_data::make_unique<const safe_VkPipelineShaderStageCreateInfo>(cbs); |
| 142 | } |
| 143 | std::unique_ptr<const safe_VkPipelineShaderStageCreateInfo> ToShaderStageCI(const VkPipelineShaderStageCreateInfo &cbs) { |
| 144 | return layer_data::make_unique<const safe_VkPipelineShaderStageCreateInfo>(&cbs); |
| 145 | } |
| 146 | |
| 147 | template <typename CreateInfo> |
| 148 | void SetFragmentShaderInfoPrivate(FragmentShaderState &fs_state, const ValidationStateTracker &state_data, |
| 149 | const CreateInfo &create_info) { |
| 150 | for (uint32_t i = 0; i < create_info.stageCount; ++i) { |
| 151 | if (create_info.pStages[i].stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 152 | auto module_state = state_data.Get<SHADER_MODULE_STATE>(create_info.pStages[i].module); |
| 153 | if (!module_state) { |
| 154 | // If module is null and there is a VkShaderModuleCreateInfo in the pNext chain of the stage info, then this |
| 155 | // module is part of a library and the state must be created |
| 156 | const auto shader_ci = LvlFindInChain<VkShaderModuleCreateInfo>(create_info.pStages[i].pNext); |
| 157 | if (shader_ci) { |
| 158 | const uint32_t unique_shader_id = 0; // TODO GPU-AV rework required to get this value properly |
| 159 | module_state = state_data.CreateShaderModuleState(*shader_ci, unique_shader_id); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (module_state) { |
| 164 | fs_state.fragment_shader = std::move(module_state); |
| 165 | fs_state.fragment_shader_ci = ToShaderStageCI(create_info.pStages[i]); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // static |
| 172 | void FragmentShaderState::SetFragmentShaderInfo(FragmentShaderState &fs_state, const ValidationStateTracker &state_data, |
| 173 | const VkGraphicsPipelineCreateInfo &create_info) { |
| 174 | SetFragmentShaderInfoPrivate(fs_state, state_data, create_info); |
| 175 | } |
| 176 | |
| 177 | // static |
| 178 | void FragmentShaderState::SetFragmentShaderInfo(FragmentShaderState &fs_state, const ValidationStateTracker &state_data, |
| 179 | const safe_VkGraphicsPipelineCreateInfo &create_info) { |
| 180 | SetFragmentShaderInfoPrivate(fs_state, state_data, create_info); |
| 181 | } |
| 182 | |
| 183 | FragmentShaderState::FragmentShaderState(const ValidationStateTracker &dev_data, VkRenderPass rp, uint32_t subp, |
| 184 | VkPipelineLayout layout) |
| 185 | : rp_state(dev_data.Get<RENDER_PASS_STATE>(rp)), subpass(subp), pipeline_layout(dev_data.Get<PIPELINE_LAYOUT_STATE>(layout)) {} |
| 186 | |
| 187 | FragmentOutputState::FragmentOutputState(const ValidationStateTracker &dev_data, VkRenderPass rp, uint32_t sp) |
| 188 | : rp_state(dev_data.Get<RENDER_PASS_STATE>(rp)), subpass(sp) {} |
| 189 | |
| 190 | // static |
| 191 | bool FragmentOutputState::IsBlendConstantsEnabled(const AttachmentVector &attachments) { |
| 192 | bool result = false; |
| 193 | for (const auto &attachment : attachments) { |
| 194 | if (VK_TRUE == attachment.blendEnable) { |
| 195 | if (((attachment.dstAlphaBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) && |
| 196 | (attachment.dstAlphaBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA)) || |
| 197 | ((attachment.dstColorBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) && |
| 198 | (attachment.dstColorBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA)) || |
| 199 | ((attachment.srcAlphaBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) && |
| 200 | (attachment.srcAlphaBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA)) || |
| 201 | ((attachment.srcColorBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) && |
| 202 | (attachment.srcColorBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA))) { |
| 203 | result = true; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | return result; |
| 209 | } |