aboutsummaryrefslogtreecommitdiff
path: root/src/engine.h
blob: 0b63f88fd2cf135c566b4a4d00cdb87c12688de3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2018 The Amber Authors.
//
// 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_ENGINE_H_
#define SRC_ENGINE_H_

#include <memory>
#include <string>
#include <vector>

#include "amber/amber.h"
#include "amber/result.h"
#include "src/buffer_data.h"
#include "src/command.h"
#include "src/format.h"
#include "src/pipeline.h"

namespace amber {

/// The type of resource being described.
enum class ResourceInfoType : uint8_t {
  /// A buffer resource.
  kBuffer = 0,
  /// An image resource.
  kImage,
};

/// EngineData stores information used during engine execution.
struct EngineData {
  /// The timeout to use for fences, in milliseconds.
  uint32_t fence_timeout_ms = 100;
};

/// Contains information relating to a backing resource from the engine.
struct ResourceInfo {
  ResourceInfoType type = ResourceInfoType::kBuffer;

  /// Key metrics of a 2D image.
  /// For higher dimensions or arrayed images, we would need more strides.
  /// For example, see VkSubresourceLayout.
  struct {
    const Format* texel_format = nullptr;  // Format of a single texel.
    uint32_t texel_stride = 0;  // Number of bytes for a single texel.
    uint32_t row_stride = 0;  // Number of bytes between successive pixel rows.
    uint32_t width = 0;
    uint32_t height = 0;
    uint32_t depth = 0;
  } image_info;

  /// The size in bytes of Vulkan memory pointed by |cpu_memory|.
  /// For the case when it is an image resource, |size_in_bytes| must
  /// be |image_info.row_stride * image_info.height * image_info.depth|.
  size_t size_in_bytes = 0;

  /// If the primitive type of resource is the same with the type
  /// of actual data, the alignment must be properly determined by
  /// Vulkan's internal memory allocation. In these cases, script
  /// writers can assume that there is no alignment issues.
  const void* cpu_memory = nullptr;
};

/// Abstract class which describes a backing engine for Amber.
class Engine {
 public:
  /// Creates a new engine of the requested |type|.
  static std::unique_ptr<Engine> Create(EngineType type);

  virtual ~Engine();

  /// Initialize the engine with the provided config. The config is _not_ owned
  /// by the engine and will not be destroyed. The |features| and |extensions|
  /// are for validation purposes only. If possible the engine should verify
  /// that the constraints in |features| and |extensions| are valid and fail
  /// otherwise.
  virtual Result Initialize(
      EngineConfig* config,
      Delegate* delegate,
      const std::vector<std::string>& features,
      const std::vector<std::string>& instance_extensions,
      const std::vector<std::string>& device_extensions) = 0;

  /// Shutdown the engine and cleanup any resources.
  virtual Result Shutdown() = 0;

  /// Create graphics pipeline.
  virtual Result CreatePipeline(Pipeline* pipeline) = 0;

  /// Execute the clear color command
  virtual Result DoClearColor(const ClearColorCommand* cmd) = 0;

  /// Execute the clear stencil command
  virtual Result DoClearStencil(const ClearStencilCommand* cmd) = 0;

  /// Execute the clear depth command
  virtual Result DoClearDepth(const ClearDepthCommand* cmd) = 0;

  /// Execute the clear command
  virtual Result DoClear(const ClearCommand* cmd) = 0;

  /// Execute the draw rect command
  virtual Result DoDrawRect(const DrawRectCommand* cmd) = 0;

  /// Execute the draw arrays command
  virtual Result DoDrawArrays(const DrawArraysCommand* cmd) = 0;

  /// Execute the compute command
  virtual Result DoCompute(const ComputeCommand* cmd) = 0;

  /// Execute the entry point command
  virtual Result DoEntryPoint(const EntryPointCommand* cmd) = 0;

  /// Execute the patch command
  virtual Result DoPatchParameterVertices(
      const PatchParameterVerticesCommand* cmd) = 0;

  /// Execute the buffer command.
  /// This declares an Amber buffer to be bound to a descriptor.
  /// This covers both Vulkan buffers and images.
  virtual Result DoBuffer(const BufferCommand* cmd) = 0;

  /// Run all queued commands and copy frame buffer data to the host
  /// if graphics pipeline.
  virtual Result DoProcessCommands(amber::Pipeline* pipeline) = 0;

  /// Get stride, width, height, and memory pointer of color frame buffer.
  /// This is only valid if the buffer of color framebuffer is mapped into
  /// the host address space. In particular, if we have run
  /// DoProcessCommands() and since then no graphics pipeline drawing
  /// commands have occurred e.g., DoClear, DoDrawArrays, DoDrawRect.
  virtual Result GetFrameBufferInfo(Pipeline* pipeline,
                                    size_t attachment_idx,
                                    ResourceInfo* info) = 0;

  /// Copy the content of the framebuffer into |values|, each value is a pixel
  /// in R8G8B8A8 format.
  virtual Result GetFrameBuffer(Pipeline* pipeline,
                                size_t attachment_idx,
                                std::vector<Value>* values) = 0;

  /// Copy the contents of the resource bound to the given descriptor
  /// and get the resource information e.g., size for buffer, width,
  /// height, depth for image of descriptor given as |descriptor_set|
  /// and |binding|.
  virtual Result GetDescriptorInfo(Pipeline* pipeline,
                                   const uint32_t descriptor_set,
                                   const uint32_t binding,
                                   ResourceInfo* info) = 0;

  /// Sets the engine data to use.
  void SetEngineData(const EngineData& data) { engine_data_ = data; }

 protected:
  Engine();

  /// Retrieves the engine data.
  const EngineData& GetEngineData() const { return engine_data_; }

 private:
  EngineData engine_data_;
};

}  // namespace amber

#endif  // SRC_ENGINE_H_