1 // Copyright 2018 The Amber Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SRC_VULKAN_VERTEX_BUFFER_H_ 16 #define SRC_VULKAN_VERTEX_BUFFER_H_ 17 18 #include <memory> 19 #include <vector> 20 21 #include "amber/result.h" 22 #include "amber/value.h" 23 #include "amber/vulkan_header.h" 24 #include "src/buffer.h" 25 #include "src/format.h" 26 #include "src/vulkan/transfer_buffer.h" 27 28 namespace amber { 29 namespace vulkan { 30 31 class CommandBuffer; 32 class Device; 33 34 /// Wrapper around vertex data information. 35 class VertexBuffer { 36 public: 37 explicit VertexBuffer(Device* device); 38 ~VertexBuffer(); 39 40 Result SendVertexData(CommandBuffer* command); VertexDataSent()41 bool VertexDataSent() const { return !is_vertex_data_pending_; } 42 43 void SetData(uint8_t location, Buffer* buffer); 44 GetVkVertexInputAttr()45 const std::vector<VkVertexInputAttributeDescription>& GetVkVertexInputAttr() 46 const { 47 return vertex_attr_desc_; 48 } 49 GetVkVertexInputBinding()50 VkVertexInputBindingDescription GetVkVertexInputBinding() const { 51 VkVertexInputBindingDescription vertex_binding_desc = 52 VkVertexInputBindingDescription(); 53 vertex_binding_desc.binding = 0; 54 vertex_binding_desc.stride = Get4BytesAlignedStride(); 55 vertex_binding_desc.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; 56 return vertex_binding_desc; 57 } 58 GetVertexCount()59 uint32_t GetVertexCount() const { 60 if (data_.empty()) 61 return 0; 62 return data_[0]->ElementCount(); 63 } 64 65 void BindToCommandBuffer(CommandBuffer* command); 66 67 void SetBufferForTest(std::unique_ptr<TransferBuffer> buffer); 68 69 private: 70 Result FillVertexBufferWithData(CommandBuffer* command); 71 Get4BytesAlignedStride()72 uint32_t Get4BytesAlignedStride() const { 73 return ((stride_in_bytes_ + 3) / 4) * 4; 74 } 75 76 Device* device_ = nullptr; 77 78 bool is_vertex_data_pending_ = true; 79 80 std::unique_ptr<TransferBuffer> transfer_buffer_; 81 uint32_t stride_in_bytes_ = 0; 82 83 std::vector<Buffer*> data_; 84 std::vector<VkVertexInputAttributeDescription> vertex_attr_desc_; 85 }; 86 87 } // namespace vulkan 88 } // namespace amber 89 90 #endif // SRC_VULKAN_VERTEX_BUFFER_H_ 91