// Copyright 2018 The Amber Authors.
// Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
//
// 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 SRC_VULKAN_RESOURCE_H_
#define SRC_VULKAN_RESOURCE_H_

#include <map>
#include <memory>
#include <vector>

#include "amber/result.h"
#include "amber/value.h"
#include "amber/vulkan_header.h"

namespace amber {

class BLAS;
class TLAS;
class SBT;

namespace vulkan {

class CommandBuffer;
class Device;
class TransferBuffer;
class TransferImage;
class BLAS;
class TLAS;
class SBT;

typedef std::map<amber::BLAS*, std::unique_ptr<amber::vulkan::BLAS>> BlasesMap;
typedef std::map<amber::TLAS*, std::unique_ptr<amber::vulkan::TLAS>> TlasesMap;
typedef std::map<amber::SBT*, amber::vulkan::SBT*> SbtsMap;

// Class for Vulkan resources. Its children are Vulkan Buffer and Vulkan Image.
class Resource {
 public:
  virtual ~Resource();

  /// Records a command on |command_buffer| to copy the buffer contents from the
  /// host to the device.
  virtual void CopyToDevice(CommandBuffer* command_buffer) = 0;
  /// Records a command on |command_buffer| to copy the buffer contents from the
  /// device to the host.
  virtual void CopyToHost(CommandBuffer* command_buffer) = 0;

  void* HostAccessibleMemoryPtr() const { return memory_ptr_; }

  uint32_t GetSizeInBytes() const { return size_in_bytes_; }
  void UpdateMemoryWithRawData(const std::vector<uint8_t>& raw_data);

  bool IsReadOnly() const { return is_read_only_; }
  void SetReadOnly(bool read_only) { is_read_only_ = read_only; }
  virtual Result Initialize() = 0;
  virtual TransferBuffer* AsTransferBuffer() { return nullptr; }
  virtual TransferImage* AsTransferImage() { return nullptr; }
  virtual void AddAllocateFlags(VkMemoryAllocateFlags memory_allocate_flags) {
    memory_allocate_flags_ |= memory_allocate_flags;
  }
  VkMemoryPropertyFlags GetMemoryPropertiesFlags() {
    return memory_properties_flags_;
  }
  void SetMemoryPropertiesFlags(VkMemoryPropertyFlags flags) {
    memory_properties_flags_ = flags;
  }

 protected:
  Resource(Device* device, uint32_t size);
  Result CreateVkBuffer(VkBuffer* buffer, VkBufferUsageFlags usage);

  Result AllocateAndBindMemoryToVkBuffer(VkBuffer buffer,
                                         VkDeviceMemory* memory,
                                         VkMemoryPropertyFlags flags,
                                         bool force_flags,
                                         uint32_t* memory_type_index);

  Result MapMemory(VkDeviceMemory memory);
  void UnMapMemory(VkDeviceMemory memory);
  void SetMemoryPtr(void* ptr) { memory_ptr_ = ptr; }

  /// Records a memory barrier on |command_buffer|, to ensure prior writes to
  /// this buffer have completed and are available to subsequent commands.
  void MemoryBarrier(CommandBuffer* command_buffer);

  /// Returns a memory index for the given Vulkan device, for a memory type
  /// which has the given |flags| set. If no memory is found with the given
  /// |flags| set then the first non-zero memory index is returned. If
  /// |require_flags_found| is true then if no memory is found with the given
  /// |flags| then the maximum uint32_t value is returned instead of the
  /// first non-zero memory index.
  uint32_t ChooseMemory(uint32_t memory_type_bits,
                        VkMemoryPropertyFlags flags,
                        bool require_flags_found);
  Result AllocateMemory(VkDeviceMemory* memory,
                        VkDeviceSize size,
                        uint32_t memory_type_index);

  Device* device_ = nullptr;

 private:
  uint32_t size_in_bytes_ = 0;
  void* memory_ptr_ = nullptr;
  bool is_read_only_ = false;
  VkMemoryAllocateFlags memory_allocate_flags_ = 0u;
  VkMemoryPropertyFlags memory_properties_flags_ =
      VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
      VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
};

}  // namespace vulkan
}  // namespace amber

#endif  // SRC_VULKAN_RESOURCE_H_
