aboutsummaryrefslogtreecommitdiff
path: root/src/engine.h
blob: 0d6cb9fe791c8a4ba546b2967fb4350bb8e17641 (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
// 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 <vector>

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

namespace amber {

enum class PipelineType : uint8_t {
  kCompute = 0,
  kGraphics,
};

enum class ResourceInfoType : uint8_t {
  kBuffer = 0,
  kImage,
};

struct ResourceInfo {
  ResourceInfoType type = ResourceInfoType::kBuffer;

  struct {
    uint32_t texel_stride = 0;  // Number of bytes for a single texel.
    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.width * image_info.height * image_info.depth *
  // image_info.texel_stride|.
  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;
};

class Engine {
 public:
  static std::unique_ptr<Engine> Create(EngineType type);

  virtual ~Engine();

  // Initialize the engine.
  virtual Result Initialize() = 0;

  // Initialize the engine with the provided device. The device is _not_ owned
  // by the engine and should not be destroyed.
  virtual Result InitializeWithDevice(void* default_device) = 0;

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

  // Enable |feature|. If the feature requires a pixel format it will be
  // provided in |format|, otherwise |format| is a nullptr. If the feature
  // requires a uint32 value it will be set in the |uint32_t|.
  virtual Result AddRequirement(Feature feature,
                                const Format* format,
                                uint32_t) = 0;

  // Create graphics pipeline.
  virtual Result CreatePipeline(PipelineType type) = 0;

  // Set the shader of |type| to the binary |data|.
  virtual Result SetShader(ShaderType type,
                           const std::vector<uint32_t>& data) = 0;

  // Provides the data for a given buffer to be bound at the given location
  // This is used to declare and populate vertex and index inputs to a graphics
  // pipeline.
  virtual Result SetBuffer(BufferType type,
                           uint8_t location,
                           const Format& format,
                           const std::vector<Value>& data) = 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() = 0;

  // Get stride, width, height, and memory pointer of frame buffer.
  // This is only valid if the buffer of 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(ResourceInfo* info) = 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(const uint32_t descriptor_set,
                                   const uint32_t binding,
                                   ResourceInfo* info) = 0;

 protected:
  Engine();
};

}  // namespace amber

#endif  // SRC_ENGINE_H_