1e5c31af7Sopenharmony_ci// Copyright 2018 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 AMBER_AMBER_H_
16e5c31af7Sopenharmony_ci#define AMBER_AMBER_H_
17e5c31af7Sopenharmony_ci
18e5c31af7Sopenharmony_ci#include <stdint.h>
19e5c31af7Sopenharmony_ci
20e5c31af7Sopenharmony_ci#include <map>
21e5c31af7Sopenharmony_ci#include <string>
22e5c31af7Sopenharmony_ci#include <vector>
23e5c31af7Sopenharmony_ci
24e5c31af7Sopenharmony_ci#include "amber/recipe.h"
25e5c31af7Sopenharmony_ci#include "amber/result.h"
26e5c31af7Sopenharmony_ci#include "amber/value.h"
27e5c31af7Sopenharmony_ci
28e5c31af7Sopenharmony_cinamespace amber {
29e5c31af7Sopenharmony_ci
30e5c31af7Sopenharmony_ci/// The shader map is a map from the name of a shader to the spirv-binary
31e5c31af7Sopenharmony_ci/// which is the compiled representation of that named shader.
32e5c31af7Sopenharmony_citypedef std::map<std::string, std::vector<uint32_t> > ShaderMap;
33e5c31af7Sopenharmony_ci
34e5c31af7Sopenharmony_cienum EngineType {
35e5c31af7Sopenharmony_ci  /// Use the Vulkan backend, if available
36e5c31af7Sopenharmony_ci  kEngineTypeVulkan = 0,
37e5c31af7Sopenharmony_ci  /// Use the Dawn backend, if available
38e5c31af7Sopenharmony_ci  kEngineTypeDawn,
39e5c31af7Sopenharmony_ci};
40e5c31af7Sopenharmony_ci
41e5c31af7Sopenharmony_cienum class ExecutionType {
42e5c31af7Sopenharmony_ci  /// Execute as normal.
43e5c31af7Sopenharmony_ci  kExecute = 0,
44e5c31af7Sopenharmony_ci  /// Only create the pipelines and then exit.
45e5c31af7Sopenharmony_ci  kPipelineCreateOnly
46e5c31af7Sopenharmony_ci};
47e5c31af7Sopenharmony_ci
48e5c31af7Sopenharmony_ci/// Override point of engines to add their own configuration.
49e5c31af7Sopenharmony_cistruct EngineConfig {
50e5c31af7Sopenharmony_ci  virtual ~EngineConfig();
51e5c31af7Sopenharmony_ci};
52e5c31af7Sopenharmony_ci
53e5c31af7Sopenharmony_ci/// Stores information for a buffer.
54e5c31af7Sopenharmony_cistruct BufferInfo {
55e5c31af7Sopenharmony_ci  BufferInfo();
56e5c31af7Sopenharmony_ci  BufferInfo(const BufferInfo&);
57e5c31af7Sopenharmony_ci  ~BufferInfo();
58e5c31af7Sopenharmony_ci
59e5c31af7Sopenharmony_ci  BufferInfo& operator=(const BufferInfo&);
60e5c31af7Sopenharmony_ci
61e5c31af7Sopenharmony_ci  /// Determines if this is an image buffer.
62e5c31af7Sopenharmony_ci  bool is_image_buffer;
63e5c31af7Sopenharmony_ci  /// Holds the buffer name
64e5c31af7Sopenharmony_ci  std::string buffer_name;
65e5c31af7Sopenharmony_ci  /// Holds the buffer width
66e5c31af7Sopenharmony_ci  uint32_t width;
67e5c31af7Sopenharmony_ci  /// Holds the buffer height
68e5c31af7Sopenharmony_ci  uint32_t height;
69e5c31af7Sopenharmony_ci  /// Contains the buffer internal data
70e5c31af7Sopenharmony_ci  std::vector<Value> values;
71e5c31af7Sopenharmony_ci};
72e5c31af7Sopenharmony_ci
73e5c31af7Sopenharmony_ci/// Types of source file to load buffer data from.
74e5c31af7Sopenharmony_cienum class BufferDataFileType : int8_t {
75e5c31af7Sopenharmony_ci  /// Unknown file type
76e5c31af7Sopenharmony_ci  kUnknown = -1,
77e5c31af7Sopenharmony_ci  /// A text file
78e5c31af7Sopenharmony_ci  kText = 0,
79e5c31af7Sopenharmony_ci  /// A binary file
80e5c31af7Sopenharmony_ci  kBinary,
81e5c31af7Sopenharmony_ci  /// A PNG file
82e5c31af7Sopenharmony_ci  kPng
83e5c31af7Sopenharmony_ci};
84e5c31af7Sopenharmony_ci
85e5c31af7Sopenharmony_ci/// Delegate class for various hook functions
86e5c31af7Sopenharmony_ciclass Delegate {
87e5c31af7Sopenharmony_ci public:
88e5c31af7Sopenharmony_ci  virtual ~Delegate();
89e5c31af7Sopenharmony_ci
90e5c31af7Sopenharmony_ci  /// Log the given message
91e5c31af7Sopenharmony_ci  virtual void Log(const std::string& message) = 0;
92e5c31af7Sopenharmony_ci  /// Tells whether to log the graphics API calls
93e5c31af7Sopenharmony_ci  virtual bool LogGraphicsCalls() const = 0;
94e5c31af7Sopenharmony_ci  /// Tells whether to log the duration of graphics API calls
95e5c31af7Sopenharmony_ci  virtual bool LogGraphicsCallsTime() const = 0;
96e5c31af7Sopenharmony_ci  /// Returns the current timestamp in nanoseconds
97e5c31af7Sopenharmony_ci  virtual uint64_t GetTimestampNs() const = 0;
98e5c31af7Sopenharmony_ci  /// Tells whether to log each test as it's executed
99e5c31af7Sopenharmony_ci  virtual bool LogExecuteCalls() const = 0;
100e5c31af7Sopenharmony_ci  /// Loads buffer data from a file
101e5c31af7Sopenharmony_ci  virtual amber::Result LoadBufferData(const std::string file_name,
102e5c31af7Sopenharmony_ci                                       BufferDataFileType file_type,
103e5c31af7Sopenharmony_ci                                       amber::BufferInfo* buffer) const = 0;
104e5c31af7Sopenharmony_ci};
105e5c31af7Sopenharmony_ci
106e5c31af7Sopenharmony_ci/// Stores configuration options for Amber.
107e5c31af7Sopenharmony_cistruct Options {
108e5c31af7Sopenharmony_ci  Options();
109e5c31af7Sopenharmony_ci  ~Options();
110e5c31af7Sopenharmony_ci
111e5c31af7Sopenharmony_ci  /// Sets the engine to be created. Default Vulkan.
112e5c31af7Sopenharmony_ci  EngineType engine;
113e5c31af7Sopenharmony_ci  /// Holds engine specific configuration. Ownership stays with the caller.
114e5c31af7Sopenharmony_ci  EngineConfig* config;
115e5c31af7Sopenharmony_ci  /// The SPIR-V environment to target.
116e5c31af7Sopenharmony_ci  /// E.g. "spv1.0", "spv1.3", "vulkan1.0", "vulkan1.1spv1.4".
117e5c31af7Sopenharmony_ci  /// If a Vulkan environment, uses the highest version of SPIR-V required
118e5c31af7Sopenharmony_ci  /// to be supported by that Vulkan environment.  For SPIR-V 1.4 in
119e5c31af7Sopenharmony_ci  /// Vulkan, use "vulkan1.1spv1.4".
120e5c31af7Sopenharmony_ci  /// If a SPIR-V environment is specified, assume the lowest version
121e5c31af7Sopenharmony_ci  /// of Vulkan that requires support for that version of SPIR-V.
122e5c31af7Sopenharmony_ci  /// Shader compilers may limit the list of supported environments.
123e5c31af7Sopenharmony_ci  /// If empty, a default of "spv1.0" is used.
124e5c31af7Sopenharmony_ci  std::string spv_env;
125e5c31af7Sopenharmony_ci  /// Lists the buffers to extract at the end of the execution
126e5c31af7Sopenharmony_ci  std::vector<BufferInfo> extractions;
127e5c31af7Sopenharmony_ci  /// The type of execution. For example, execute as normal or just create the
128e5c31af7Sopenharmony_ci  /// piplines and exit.
129e5c31af7Sopenharmony_ci  ExecutionType execution_type;
130e5c31af7Sopenharmony_ci  /// If true, disables SPIR-V validation. If false, SPIR-V shaders will be
131e5c31af7Sopenharmony_ci  /// validated using the Validator component (spirv-val) from SPIRV-Tools.
132e5c31af7Sopenharmony_ci  bool disable_spirv_validation;
133e5c31af7Sopenharmony_ci};
134e5c31af7Sopenharmony_ci
135e5c31af7Sopenharmony_ci/// Main interface to the Amber environment.
136e5c31af7Sopenharmony_ciclass Amber {
137e5c31af7Sopenharmony_ci public:
138e5c31af7Sopenharmony_ci  explicit Amber(Delegate* delegate);
139e5c31af7Sopenharmony_ci  ~Amber();
140e5c31af7Sopenharmony_ci
141e5c31af7Sopenharmony_ci  /// Parse the given |data| into the |recipe|.
142e5c31af7Sopenharmony_ci  amber::Result Parse(const std::string& data, amber::Recipe* recipe);
143e5c31af7Sopenharmony_ci
144e5c31af7Sopenharmony_ci  /// Determines whether the engine supports all features required by the
145e5c31af7Sopenharmony_ci  /// |recipe|. Modifies the |recipe| by applying some of the |opts| to the
146e5c31af7Sopenharmony_ci  /// recipe's internal state.
147e5c31af7Sopenharmony_ci  amber::Result AreAllRequirementsSupported(const amber::Recipe* recipe,
148e5c31af7Sopenharmony_ci                                            Options* opts);
149e5c31af7Sopenharmony_ci
150e5c31af7Sopenharmony_ci  /// Executes the given |recipe| with the provided |opts|. Returns a
151e5c31af7Sopenharmony_ci  /// |Result| which indicates if the execution succeded. Modifies the
152e5c31af7Sopenharmony_ci  /// |recipe| by applying some of the |opts| to the recipe's internal
153e5c31af7Sopenharmony_ci  /// state.
154e5c31af7Sopenharmony_ci  amber::Result Execute(const amber::Recipe* recipe, Options* opts);
155e5c31af7Sopenharmony_ci
156e5c31af7Sopenharmony_ci  /// Executes the given |recipe| with the provided |opts|. Will use
157e5c31af7Sopenharmony_ci  /// |shader_map| to lookup shader data before attempting to compile the
158e5c31af7Sopenharmony_ci  /// shader if possible.
159e5c31af7Sopenharmony_ci  amber::Result ExecuteWithShaderData(const amber::Recipe* recipe,
160e5c31af7Sopenharmony_ci                                      Options* opts,
161e5c31af7Sopenharmony_ci                                      const ShaderMap& shader_data);
162e5c31af7Sopenharmony_ci
163e5c31af7Sopenharmony_ci  /// Returns the delegate object.
164e5c31af7Sopenharmony_ci  Delegate* GetDelegate() const { return delegate_; }
165e5c31af7Sopenharmony_ci
166e5c31af7Sopenharmony_ci private:
167e5c31af7Sopenharmony_ci  Delegate* delegate_;
168e5c31af7Sopenharmony_ci};
169e5c31af7Sopenharmony_ci
170e5c31af7Sopenharmony_ci}  // namespace amber
171e5c31af7Sopenharmony_ci
172e5c31af7Sopenharmony_ci#endif  // AMBER_AMBER_H_
173