1e5c31af7Sopenharmony_ci// Copyright 2019 The Amber Authors.
2e5c31af7Sopenharmony_ci//
3e5c31af7Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4e5c31af7Sopenharmony_ci// you may not use this file except in compliance with the License.
5e5c31af7Sopenharmony_ci// You may obtain a copy of the License at
6e5c31af7Sopenharmony_ci//
7e5c31af7Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8e5c31af7Sopenharmony_ci//
9e5c31af7Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10e5c31af7Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11e5c31af7Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e5c31af7Sopenharmony_ci// See the License for the specific language governing permissions and
13e5c31af7Sopenharmony_ci// limitations under the License.
14e5c31af7Sopenharmony_ci
15e5c31af7Sopenharmony_ci#ifndef SRC_VULKAN_PUSH_CONSTANT_H_
16e5c31af7Sopenharmony_ci#define SRC_VULKAN_PUSH_CONSTANT_H_
17e5c31af7Sopenharmony_ci
18e5c31af7Sopenharmony_ci#include <memory>
19e5c31af7Sopenharmony_ci#include <vector>
20e5c31af7Sopenharmony_ci
21e5c31af7Sopenharmony_ci#include "amber/result.h"
22e5c31af7Sopenharmony_ci#include "amber/vulkan_header.h"
23e5c31af7Sopenharmony_ci#include "src/command.h"
24e5c31af7Sopenharmony_ci
25e5c31af7Sopenharmony_cinamespace amber {
26e5c31af7Sopenharmony_cinamespace vulkan {
27e5c31af7Sopenharmony_ci
28e5c31af7Sopenharmony_ciclass CommandBuffer;
29e5c31af7Sopenharmony_ciclass Device;
30e5c31af7Sopenharmony_ci
31e5c31af7Sopenharmony_ci/// Class to handle push constants.
32e5c31af7Sopenharmony_ciclass PushConstant {
33e5c31af7Sopenharmony_ci public:
34e5c31af7Sopenharmony_ci  explicit PushConstant(Device* device);
35e5c31af7Sopenharmony_ci  ~PushConstant();
36e5c31af7Sopenharmony_ci
37e5c31af7Sopenharmony_ci  /// Retrieves a `VkPushConstantRange` class describing our push constant
38e5c31af7Sopenharmony_ci  /// requirements.
39e5c31af7Sopenharmony_ci  VkPushConstantRange GetVkPushConstantRange();
40e5c31af7Sopenharmony_ci
41e5c31af7Sopenharmony_ci  Result RecordPushConstantVkCommand(CommandBuffer* command,
42e5c31af7Sopenharmony_ci                                     VkPipelineLayout pipeline_layout);
43e5c31af7Sopenharmony_ci
44e5c31af7Sopenharmony_ci  /// Add a set of values from the given |buffer| to the push constants
45e5c31af7Sopenharmony_ci  /// to be used on the next pipeline execution. The data will be added to
46e5c31af7Sopenharmony_ci  /// the push constants at |offset|.
47e5c31af7Sopenharmony_ci  Result AddBuffer(const Buffer* buffer, uint32_t offset);
48e5c31af7Sopenharmony_ci
49e5c31af7Sopenharmony_ci  /// Adds data into the push constant buffer.
50e5c31af7Sopenharmony_ci  Result AddBufferData(const BufferCommand* command);
51e5c31af7Sopenharmony_ci
52e5c31af7Sopenharmony_ci private:
53e5c31af7Sopenharmony_ci  // Information on filling the push constant buffer. The |buffer| memory will
54e5c31af7Sopenharmony_ci  // be copied into the result buffer at |offset|.
55e5c31af7Sopenharmony_ci  struct BufferInput {
56e5c31af7Sopenharmony_ci    uint32_t offset = 0;
57e5c31af7Sopenharmony_ci    const Buffer* buffer = nullptr;
58e5c31af7Sopenharmony_ci  };
59e5c31af7Sopenharmony_ci
60e5c31af7Sopenharmony_ci  Result UpdateMemoryWithInput(const BufferInput& input);
61e5c31af7Sopenharmony_ci
62e5c31af7Sopenharmony_ci  Device* device_;
63e5c31af7Sopenharmony_ci
64e5c31af7Sopenharmony_ci  /// Keeps the information of what and how to conduct push constant.
65e5c31af7Sopenharmony_ci  /// These are applied from lowest index to highest index, so that
66e5c31af7Sopenharmony_ci  /// if address ranges overlap, then the later values take effect.
67e5c31af7Sopenharmony_ci  std::vector<BufferInput> push_constant_data_;
68e5c31af7Sopenharmony_ci  std::unique_ptr<Buffer> buffer_;
69e5c31af7Sopenharmony_ci};
70e5c31af7Sopenharmony_ci
71e5c31af7Sopenharmony_ci}  // namespace vulkan
72e5c31af7Sopenharmony_ci}  // namespace amber
73e5c31af7Sopenharmony_ci
74e5c31af7Sopenharmony_ci#endif  // SRC_VULKAN_PUSH_CONSTANT_H_
75