From 2bd6143061a78e6f09d9486b95b4e307fad59f8a Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Tue, 9 Jan 2018 07:26:52 -0800 Subject: [PATCH] Vulkan: Implement InputState --- src/backend/CMakeLists.txt | 2 + src/backend/vulkan/GeneratedCodeIncludes.h | 1 + src/backend/vulkan/InputStateVk.cpp | 93 ++++++++++++++++++++++ src/backend/vulkan/InputStateVk.h | 41 ++++++++++ src/backend/vulkan/VulkanBackend.cpp | 1 + src/backend/vulkan/VulkanBackend.h | 3 +- 6 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 src/backend/vulkan/InputStateVk.cpp create mode 100644 src/backend/vulkan/InputStateVk.h diff --git a/src/backend/CMakeLists.txt b/src/backend/CMakeLists.txt index 7c6ddb8017..a9053403a3 100644 --- a/src/backend/CMakeLists.txt +++ b/src/backend/CMakeLists.txt @@ -297,6 +297,8 @@ if (NXT_ENABLE_VULKAN) ${VULKAN_DIR}/CommandBufferVk.h ${VULKAN_DIR}/FencedDeleter.cpp ${VULKAN_DIR}/FencedDeleter.h + ${VULKAN_DIR}/InputStateVk.cpp + ${VULKAN_DIR}/InputStateVk.h ${VULKAN_DIR}/MemoryAllocator.cpp ${VULKAN_DIR}/MemoryAllocator.h ${VULKAN_DIR}/PipelineLayoutVk.cpp diff --git a/src/backend/vulkan/GeneratedCodeIncludes.h b/src/backend/vulkan/GeneratedCodeIncludes.h index 4d4ab07afa..7e83209018 100644 --- a/src/backend/vulkan/GeneratedCodeIncludes.h +++ b/src/backend/vulkan/GeneratedCodeIncludes.h @@ -14,6 +14,7 @@ #include "backend/vulkan/BufferVk.h" #include "backend/vulkan/CommandBufferVk.h" +#include "backend/vulkan/InputStateVk.h" #include "backend/vulkan/PipelineLayoutVk.h" #include "backend/vulkan/RenderPassVk.h" #include "backend/vulkan/TextureVk.h" diff --git a/src/backend/vulkan/InputStateVk.cpp b/src/backend/vulkan/InputStateVk.cpp new file mode 100644 index 0000000000..ec399ffcbb --- /dev/null +++ b/src/backend/vulkan/InputStateVk.cpp @@ -0,0 +1,93 @@ +// Copyright 2018 The NXT Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "backend/vulkan/InputStateVk.h" + +#include "common/BitSetIterator.h" + +namespace backend { namespace vulkan { + + namespace { + + VkVertexInputRate VulkanInputRate(nxt::InputStepMode stepMode) { + switch (stepMode) { + case nxt::InputStepMode::Vertex: + return VK_VERTEX_INPUT_RATE_VERTEX; + case nxt::InputStepMode::Instance: + return VK_VERTEX_INPUT_RATE_INSTANCE; + default: + UNREACHABLE(); + } + } + + VkFormat VulkanVertexFormat(nxt::VertexFormat format) { + switch (format) { + case nxt::VertexFormat::FloatR32G32B32A32: + return VK_FORMAT_R32G32B32A32_SFLOAT; + case nxt::VertexFormat::FloatR32G32B32: + return VK_FORMAT_R32G32B32_SFLOAT; + case nxt::VertexFormat::FloatR32G32: + return VK_FORMAT_R32G32_SFLOAT; + case nxt::VertexFormat::FloatR32: + return VK_FORMAT_R32_SFLOAT; + default: + UNREACHABLE(); + } + } + + } // anonymous namespace + + InputState::InputState(InputStateBuilder* builder) : InputStateBase(builder) { + // Fill in the "binding info" that will be chained in the create info + uint32_t bindingCount = 0; + for (uint32_t i : IterateBitSet(GetInputsSetMask())) { + const auto& bindingInfo = GetInput(i); + + auto& bindingDesc = mBindings[bindingCount]; + bindingDesc.binding = i; + bindingDesc.stride = bindingInfo.stride; + bindingDesc.inputRate = VulkanInputRate(bindingInfo.stepMode); + + bindingCount++; + } + + // Fill in the "attribute info" that will be chained in the create info + uint32_t attributeCount = 0; + for (uint32_t i : IterateBitSet(GetAttributesSetMask())) { + const auto& attributeInfo = GetAttribute(i); + + auto& attributeDesc = mAttributes[attributeCount]; + attributeDesc.location = i; + attributeDesc.binding = attributeInfo.bindingSlot; + attributeDesc.format = VulkanVertexFormat(attributeInfo.format); + attributeDesc.offset = attributeInfo.offset; + + attributeCount++; + } + + // Build the create info + mCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; + mCreateInfo.pNext = nullptr; + mCreateInfo.flags = 0; + mCreateInfo.vertexBindingDescriptionCount = bindingCount; + mCreateInfo.pVertexBindingDescriptions = mBindings.data(); + mCreateInfo.vertexAttributeDescriptionCount = attributeCount; + mCreateInfo.pVertexAttributeDescriptions = mAttributes.data(); + } + + const VkPipelineVertexInputStateCreateInfo* InputState::GetCreateInfo() const { + return &mCreateInfo; + } + +}} // namespace backend::vulkan diff --git a/src/backend/vulkan/InputStateVk.h b/src/backend/vulkan/InputStateVk.h new file mode 100644 index 0000000000..30d1929693 --- /dev/null +++ b/src/backend/vulkan/InputStateVk.h @@ -0,0 +1,41 @@ +// Copyright 2018 The NXT Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef BACKEND_VULKAN_INPUTSTATE_H_ +#define BACKEND_VULKAN_INPUTSTATE_H_ + +#include "backend/InputState.h" + +#include "backend/vulkan/vulkan_platform.h" + +namespace backend { namespace vulkan { + + class Device; + + // Pre-computes the input state configuration to give to a graphics pipeline create info. + class InputState : public InputStateBase { + public: + InputState(InputStateBuilder* builder); + + const VkPipelineVertexInputStateCreateInfo* GetCreateInfo() const; + + private: + VkPipelineVertexInputStateCreateInfo mCreateInfo; + std::array mBindings; + std::array mAttributes; + }; + +}} // namespace backend::vulkan + +#endif // BACKEND_VULKAN_INPUTSTATE_H_ diff --git a/src/backend/vulkan/VulkanBackend.cpp b/src/backend/vulkan/VulkanBackend.cpp index 4a8c45cd84..5aa0cc0aeb 100644 --- a/src/backend/vulkan/VulkanBackend.cpp +++ b/src/backend/vulkan/VulkanBackend.cpp @@ -19,6 +19,7 @@ #include "backend/vulkan/BufferVk.h" #include "backend/vulkan/CommandBufferVk.h" #include "backend/vulkan/FencedDeleter.h" +#include "backend/vulkan/InputStateVk.h" #include "backend/vulkan/PipelineLayoutVk.h" #include "backend/vulkan/RenderPassVk.h" #include "backend/vulkan/TextureVk.h" diff --git a/src/backend/vulkan/VulkanBackend.h b/src/backend/vulkan/VulkanBackend.h index ce7011f2e3..34215379da 100644 --- a/src/backend/vulkan/VulkanBackend.h +++ b/src/backend/vulkan/VulkanBackend.h @@ -24,7 +24,6 @@ #include "backend/DepthStencilState.h" #include "backend/Device.h" #include "backend/Framebuffer.h" -#include "backend/InputState.h" #include "backend/Queue.h" #include "backend/RenderPipeline.h" #include "backend/Sampler.h" @@ -51,7 +50,7 @@ namespace backend { namespace vulkan { using DepthStencilState = DepthStencilStateBase; class Device; using Framebuffer = FramebufferBase; - using InputState = InputStateBase; + class InputState; class PipelineLayout; class Queue; class RenderPass;