1 // Copyright 2018 The Amber Authors. 2 // Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 #ifndef SRC_VULKAN_ENGINE_VULKAN_H_ 17 #define SRC_VULKAN_ENGINE_VULKAN_H_ 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 #include <utility> 24 #include <vector> 25 26 #include "amber/vulkan_header.h" 27 #include "src/acceleration_structure.h" 28 #include "src/cast_hash.h" 29 #include "src/engine.h" 30 #include "src/pipeline.h" 31 #include "src/vulkan/blas.h" 32 #include "src/vulkan/buffer_descriptor.h" 33 #include "src/vulkan/command_pool.h" 34 #include "src/vulkan/device.h" 35 #include "src/vulkan/pipeline.h" 36 #include "src/vulkan/tlas.h" 37 #include "src/vulkan/tlas_descriptor.h" 38 #include "src/vulkan/vertex_buffer.h" 39 40 namespace amber { 41 namespace vulkan { 42 43 /// Engine implementation based on Vulkan. 44 class EngineVulkan : public Engine { 45 public: 46 EngineVulkan(); 47 ~EngineVulkan() override; 48 49 // Engine 50 Result Initialize(EngineConfig* config, 51 Delegate* delegate, 52 const std::vector<std::string>& features, 53 const std::vector<std::string>& properties, 54 const std::vector<std::string>& instance_extensions, 55 const std::vector<std::string>& device_extensions) override; 56 Result CreatePipeline(amber::Pipeline* type) override; 57 58 Result DoClearColor(const ClearColorCommand* cmd) override; 59 Result DoClearStencil(const ClearStencilCommand* cmd) override; 60 Result DoClearDepth(const ClearDepthCommand* cmd) override; 61 Result DoClear(const ClearCommand* cmd) override; 62 Result DoDrawRect(const DrawRectCommand* cmd) override; 63 Result DoDrawGrid(const DrawGridCommand* cmd) override; 64 Result DoDrawArrays(const DrawArraysCommand* cmd) override; 65 Result DoCompute(const ComputeCommand* cmd) override; 66 Result DoTraceRays(const RayTracingCommand* cmd) override; 67 Result DoEntryPoint(const EntryPointCommand* cmd) override; 68 Result DoPatchParameterVertices( 69 const PatchParameterVerticesCommand* cmd) override; 70 Result DoBuffer(const BufferCommand* cmd) override; 71 72 private: 73 struct PipelineInfo { 74 std::unique_ptr<Pipeline> vk_pipeline; 75 std::unique_ptr<VertexBuffer> vertex_buffer; 76 struct ShaderInfo { 77 ShaderType type; 78 VkShaderModule shader; 79 std::unique_ptr<std::vector<VkSpecializationMapEntry>> 80 specialization_entries; 81 std::unique_ptr<std::vector<uint32_t>> specialization_data; 82 std::unique_ptr<VkSpecializationInfo> specialization_info; 83 uint32_t required_subgroup_size; 84 VkPipelineShaderStageCreateFlags create_flags; 85 }; 86 std::unordered_map<ShaderType, ShaderInfo, CastHash<ShaderType>> 87 shader_info; 88 std::vector<PipelineInfo::ShaderInfo> shader_info_rt; 89 }; 90 91 Result GetVkShaderStageInfo(ShaderType shader_type, 92 const PipelineInfo::ShaderInfo& shader_info, 93 VkPipelineShaderStageCreateInfo* stage_ci); 94 95 Result GetVkShaderStageInfo( 96 amber::Pipeline* pipeline, 97 std::vector<VkPipelineShaderStageCreateInfo>* out); 98 99 Result SetShader(amber::Pipeline* pipeline, 100 const amber::Pipeline::ShaderInfo& shader, 101 size_t index); 102 103 Result GetVkShaderGroupInfo( 104 amber::Pipeline* pipeline, 105 std::vector<VkRayTracingShaderGroupCreateInfoKHR>* out); 106 107 Result InitDependendLibraries(amber::Pipeline* pipeline, 108 std::vector<VkPipeline>* libs); 109 110 std::unique_ptr<Device> device_; 111 std::unique_ptr<CommandPool> pool_; 112 113 std::map<amber::Pipeline*, PipelineInfo> pipeline_map_; 114 115 std::map<std::string, VkShaderModule> shaders_; 116 117 BlasesMap blases_; 118 119 TlasesMap tlases_; 120 }; 121 122 } // namespace vulkan 123 } // namespace amber 124 125 #endif // SRC_VULKAN_ENGINE_VULKAN_H_ 126