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_ENGINE_H_
16 #define SRC_ENGINE_H_
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "amber/amber.h"
24 #include "amber/result.h"
25 #include "src/buffer.h"
26 #include "src/command.h"
27 #include "src/format.h"
28 #include "src/pipeline.h"
29 
30 namespace amber {
31 
32 class VirtualFileStore;
33 
34 /// EngineData stores information used during engine execution.
35 struct EngineData {
36   /// The timeout to use for fences, in milliseconds.
37   uint32_t fence_timeout_ms = 10000;
38   bool pipeline_runtime_layer_enabled = false;
39 };
40 
41 /// Abstract class which describes a backing engine for Amber.
42 ///
43 /// The engine class has a defined lifecycle.
44 ///  1. The engine is created through Engine::Create.
45 ///  2. Engine::Initialize is called to provide the engine with the configured
46 ///     graphics device.
47 ///  3. Engine::CreatePipeline is called for each pipeline. The pipelines are
48 ///     fully specified at this point and include:
49 ///     * All compiled shader binaries
50 ///     * Vertex, Index, Storage, Uniform, Push Constant buffers
51 ///     * Colour attachment, and depth/stencil attachment buffers.
52 ///     * Extra engine data.
53 ///     The buffers all may have default values to be loaded into the device.
54 ///  4. Engine::Do* is called for each command.
55 ///     Note, it is assumed that the amber::Buffers are updated at the end of
56 ///     each Do* command and can be used immediately for comparisons.
57 ///  5. Engine destructor is called.
58 class Engine {
59  public:
60   /// Creates a new engine of the requested |type|.
61   static std::unique_ptr<Engine> Create(EngineType type);
62 
63   virtual ~Engine();
64 
65   /// Initialize the engine with the provided config. The config is _not_ owned
66   /// by the engine and will not be destroyed. The |features| and |extensions|
67   /// are for validation purposes only. If possible the engine should verify
68   /// that the constraints in |features| and |extensions| are valid and fail
69   /// otherwise.
70   virtual Result Initialize(
71       EngineConfig* config,
72       Delegate* delegate,
73       const std::vector<std::string>& features,
74       const std::vector<std::string>& instance_extensions,
75       const std::vector<std::string>& device_extensions) = 0;
76 
77   /// Create graphics pipeline.
78   virtual Result CreatePipeline(Pipeline* pipeline) = 0;
79 
80   /// Execute the clear color command
81   virtual Result DoClearColor(const ClearColorCommand* cmd) = 0;
82 
83   /// Execute the clear stencil command
84   virtual Result DoClearStencil(const ClearStencilCommand* cmd) = 0;
85 
86   /// Execute the clear depth command
87   virtual Result DoClearDepth(const ClearDepthCommand* cmd) = 0;
88 
89   /// Execute the clear command
90   virtual Result DoClear(const ClearCommand* cmd) = 0;
91 
92   /// Execute the draw rect command
93   virtual Result DoDrawRect(const DrawRectCommand* cmd) = 0;
94 
95   /// Execute the draw grid command
96   virtual Result DoDrawGrid(const DrawGridCommand* cmd) = 0;
97 
98   /// Execute the draw arrays command
99   virtual Result DoDrawArrays(const DrawArraysCommand* cmd) = 0;
100 
101   /// Execute the compute command
102   virtual Result DoCompute(const ComputeCommand* cmd) = 0;
103 
104   /// Execute the entry point command
105   virtual Result DoEntryPoint(const EntryPointCommand* cmd) = 0;
106 
107   /// Execute the patch command
108   virtual Result DoPatchParameterVertices(
109       const PatchParameterVerticesCommand* cmd) = 0;
110 
111   /// Execute the buffer command.
112   /// This declares an Amber buffer to be bound to a descriptor.
113   /// This covers both Vulkan buffers and images.
114   virtual Result DoBuffer(const BufferCommand* cmd) = 0;
115 
116   /// Sets the engine data to use.
SetEngineData(const EngineData & data)117   void SetEngineData(const EngineData& data) { engine_data_ = data; }
118 
119  protected:
120   Engine();
121 
122   /// Retrieves the engine data.
GetEngineData()123   const EngineData& GetEngineData() const { return engine_data_; }
124 
125  private:
126   EngineData engine_data_;
127 };
128 
129 }  // namespace amber
130 
131 #endif  // SRC_ENGINE_H_
132