| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Vulkan/VKVertexFormat.h" |
| 5 | |
| 6 | #include "Common/Assert.h" |
| 7 | #include "Common/EnumMap.h" |
| 8 | |
| 9 | #include "VideoBackends/Vulkan/ObjectCache.h" |
| 10 | |
| 11 | #include "VideoCommon/VertexLoaderManager.h" |
| 12 | #include "VideoCommon/VertexShaderGen.h" |
| 13 | |
| 14 | namespace Vulkan |
| 15 | { |
| 16 | static VkFormat VarToVkFormat(ComponentFormat t, uint32_t components, bool integer) |
| 17 | { |
| 18 | using ComponentArray = std::array<VkFormat, 4>; |
| 19 | static constexpr auto f = [](ComponentArray a) { return a; }; // Deduction helper |
| 20 | |
| 21 | static constexpr Common::EnumMap<ComponentArray, ComponentFormat::InvalidFloat7> |
| 22 | float_type_lookup = { |
| 23 | f({VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM, VK_FORMAT_R8G8B8_UNORM, |
| 24 | VK_FORMAT_R8G8B8A8_UNORM}), // UByte |
| 25 | f({VK_FORMAT_R8_SNORM, VK_FORMAT_R8G8_SNORM, VK_FORMAT_R8G8B8_SNORM, |
| 26 | VK_FORMAT_R8G8B8A8_SNORM}), // Byte |
| 27 | f({VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM, VK_FORMAT_R16G16B16_UNORM, |
| 28 | VK_FORMAT_R16G16B16A16_UNORM}), // UShort |
| 29 | f({VK_FORMAT_R16_SNORM, VK_FORMAT_R16G16_SNORM, VK_FORMAT_R16G16B16_SNORM, |
| 30 | VK_FORMAT_R16G16B16A16_SNORM}), // Short |
| 31 | f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT, |
| 32 | VK_FORMAT_R32G32B32A32_SFLOAT}), // Float |
| 33 | f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT, |
| 34 | VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid |
| 35 | f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT, |
| 36 | VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid |
| 37 | f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT, |
| 38 | VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid |
| 39 | }; |
| 40 | |
| 41 | static constexpr Common::EnumMap<ComponentArray, ComponentFormat::InvalidFloat7> |
| 42 | integer_type_lookup = { |
| 43 | f({VK_FORMAT_R8_UINT, VK_FORMAT_R8G8_UINT, VK_FORMAT_R8G8B8_UINT, |
| 44 | VK_FORMAT_R8G8B8A8_UINT}), // UByte |
| 45 | f({VK_FORMAT_R8_SINT, VK_FORMAT_R8G8_SINT, VK_FORMAT_R8G8B8_SINT, |
| 46 | VK_FORMAT_R8G8B8A8_SINT}), // Byte |
| 47 | f({VK_FORMAT_R16_UINT, VK_FORMAT_R16G16_UINT, VK_FORMAT_R16G16B16_UINT, |
| 48 | VK_FORMAT_R16G16B16A16_UINT}), // UShort |
| 49 | f({VK_FORMAT_R16_SINT, VK_FORMAT_R16G16_SINT, VK_FORMAT_R16G16B16_SINT, |
| 50 | VK_FORMAT_R16G16B16A16_SINT}), // Short |
| 51 | f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT, |
| 52 | VK_FORMAT_R32G32B32A32_SFLOAT}), // Float |
| 53 | f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT, |
| 54 | VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid |
| 55 | f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT, |
| 56 | VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid |
| 57 | f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT, |
| 58 | VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid |
| 59 | }; |
| 60 | |
| 61 | ASSERT(components > 0 && components <= 4); |
| 62 | return integer ? integer_type_lookup[t][components - 1] : float_type_lookup[t][components - 1]; |
| 63 | } |
| 64 | |
| 65 | VertexFormat::VertexFormat(const PortableVertexDeclaration& vtx_decl) : NativeVertexFormat(vtx_decl) |
| 66 | { |
| 67 | MapAttributes(); |
| 68 | SetupInputState(); |
| 69 | } |
| 70 | |
| 71 | const VkPipelineVertexInputStateCreateInfo& VertexFormat::GetVertexInputStateInfo() const |
| 72 | { |
| 73 | return m_input_state_info; |
| 74 | } |
| 75 | |
| 76 | void VertexFormat::MapAttributes() |
| 77 | { |
| 78 | m_num_attributes = 0; |
| 79 | |
| 80 | if (m_decl.position.enable) |
| 81 | AddAttribute( |
| 82 | ShaderAttrib::Position, 0, |
| 83 | VarToVkFormat(m_decl.position.type, m_decl.position.components, m_decl.position.integer), |
| 84 | m_decl.position.offset); |
| 85 | |
| 86 | for (uint32_t i = 0; i < 3; i++) |
| 87 | { |
| 88 | if (m_decl.normals[i].enable) |
| 89 | AddAttribute(ShaderAttrib::Normal + i, 0, |
| 90 | VarToVkFormat(m_decl.normals[i].type, m_decl.normals[i].components, |
| 91 | m_decl.normals[i].integer), |
| 92 | m_decl.normals[i].offset); |
| 93 | } |
| 94 | |
| 95 | for (uint32_t i = 0; i < 2; i++) |
| 96 | { |
| 97 | if (m_decl.colors[i].enable) |
| 98 | AddAttribute(ShaderAttrib::Color0 + i, 0, |
| 99 | VarToVkFormat(m_decl.colors[i].type, m_decl.colors[i].components, |
| 100 | m_decl.colors[i].integer), |
| 101 | m_decl.colors[i].offset); |
| 102 | } |
| 103 | |
| 104 | for (uint32_t i = 0; i < 8; i++) |
| 105 | { |
| 106 | if (m_decl.texcoords[i].enable) |
| 107 | AddAttribute(ShaderAttrib::TexCoord0 + i, 0, |
| 108 | VarToVkFormat(m_decl.texcoords[i].type, m_decl.texcoords[i].components, |
| 109 | m_decl.texcoords[i].integer), |
| 110 | m_decl.texcoords[i].offset); |
| 111 | } |
| 112 | |
| 113 | if (m_decl.posmtx.enable) |
| 114 | AddAttribute(ShaderAttrib::PositionMatrix, 0, |
| 115 | VarToVkFormat(m_decl.posmtx.type, m_decl.posmtx.components, m_decl.posmtx.integer), |
| 116 | m_decl.posmtx.offset); |
| 117 | } |
| 118 | |
| 119 | void VertexFormat::SetupInputState() |
| 120 | { |
| 121 | m_binding_description.binding = 0; |
| 122 | m_binding_description.stride = m_decl.stride; |
| 123 | m_binding_description.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; |
| 124 | |
| 125 | m_input_state_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; |
| 126 | m_input_state_info.pNext = nullptr; |
| 127 | m_input_state_info.flags = 0; |
| 128 | m_input_state_info.vertexBindingDescriptionCount = 1; |
| 129 | m_input_state_info.pVertexBindingDescriptions = &m_binding_description; |
| 130 | m_input_state_info.vertexAttributeDescriptionCount = m_num_attributes; |
| 131 | m_input_state_info.pVertexAttributeDescriptions = m_attribute_descriptions.data(); |
| 132 | } |
| 133 | |
| 134 | void VertexFormat::AddAttribute(ShaderAttrib location, uint32_t binding, VkFormat format, |
| 135 | uint32_t offset) |
| 136 | { |
| 137 | ASSERT(m_num_attributes < MAX_VERTEX_ATTRIBUTES); |
| 138 | |
| 139 | m_attribute_descriptions[m_num_attributes].location = static_cast<uint32_t>(location); |
| 140 | m_attribute_descriptions[m_num_attributes].binding = binding; |
| 141 | m_attribute_descriptions[m_num_attributes].format = format; |
| 142 | m_attribute_descriptions[m_num_attributes].offset = offset; |
| 143 | m_num_attributes++; |
| 144 | } |
| 145 | } // namespace Vulkan |