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_DAWN_ENGINE_DAWN_H_
16 #define SRC_DAWN_ENGINE_DAWN_H_
17 
18 #include <cstdint>
19 #include <string>
20 #include <unordered_map>
21 #include <utility>
22 #include <vector>
23 
24 #include "dawn/dawncpp.h"
25 #include "src/cast_hash.h"
26 #include "src/command.h"
27 #include "src/dawn/pipeline_info.h"
28 #include "src/engine.h"
29 
30 namespace amber {
31 namespace dawn {
32 
33 /// Engine implementation using the Dawn API.
34 class EngineDawn : public Engine {
35  public:
36   EngineDawn();
37   ~EngineDawn() override;
38 
39   // Engine
40   // Initialize with given configuration data.
41   Result Initialize(EngineConfig* config,
42                     Delegate*,
43                     const std::vector<std::string>& features,
44                     const std::vector<std::string>& instance_extensions,
45                     const std::vector<std::string>& device_extensions) override;
46 
47   // Record info for a pipeline.  The Dawn render pipeline will be created
48   // later.  Assumes necessary shader modules have been created.  A compute
49   // pipeline requires a compute shader.  A graphics pipeline requires a vertex
50   // and a fragment shader.
51   Result CreatePipeline(::amber::Pipeline*) override;
52 
53   Result DoClearColor(const ClearColorCommand* cmd) override;
54   Result DoClearStencil(const ClearStencilCommand* cmd) override;
55   Result DoClearDepth(const ClearDepthCommand* cmd) override;
56   Result DoClear(const ClearCommand* cmd) override;
57   Result DoDrawRect(const DrawRectCommand* cmd) override;
58   Result DoDrawGrid(const DrawGridCommand* cmd) override;
59   Result DoDrawArrays(const DrawArraysCommand* cmd) override;
60   Result DoCompute(const ComputeCommand* cmd) override;
61   Result DoEntryPoint(const EntryPointCommand* cmd) override;
62   Result DoPatchParameterVertices(
63       const PatchParameterVerticesCommand* cmd) override;
64   Result DoBuffer(const BufferCommand* cmd) override;
65 
GetDebugger(VirtualFileStore *)66   std::pair<Debugger*, Result> GetDebugger(VirtualFileStore*) override {
67     return {nullptr, Result("Dawn does not currently support a debugger")};
68   }
69 
70  private:
71   // Returns the Dawn-specific render pipeline for the given command,
72   // if it exists.  Returns nullptr otherwise.
GetRenderPipeline(const::amber::PipelineCommand * command)73   RenderPipelineInfo* GetRenderPipeline(
74       const ::amber::PipelineCommand* command) {
75     return pipeline_map_[command->GetPipeline()].render_pipeline.get();
76   }
77   // Returns the Dawn-specific compute pipeline for the given command,
78   // if it exists.  Returns nullptr otherwise.
GetComputePipeline(const::amber::PipelineCommand * command)79   ComputePipelineInfo* GetComputePipeline(
80       const ::amber::PipelineCommand* command) {
81     return pipeline_map_[command->GetPipeline()].compute_pipeline.get();
82   }
83   // Creates and attaches index, vertex, storage, uniform and depth-stencil
84   // buffers. Sets up bindings. Also creates textures and texture views if not
85   // created yet. Used in the Graphics pipeline creation.
86   Result AttachBuffersAndTextures(RenderPipelineInfo* render_pipeline);
87   // Creates and attaches index, vertex, storage, uniform and depth-stencil
88   // buffers. Used in the Compute pipeline creation.
89   Result AttachBuffers(ComputePipelineInfo* compute_pipeline);
90   // Creates and submits a command to copy dawn textures back to amber color
91   // attachments.
92   Result MapDeviceTextureToHostBuffer(const RenderPipelineInfo& render_pipeline,
93                                       const ::dawn::Device& device);
94   // Creates and submits a command to copy dawn buffers back to amber buffers
95   Result MapDeviceBufferToHostBuffer(
96       const ComputePipelineInfo& compute_pipeline,
97       const ::dawn::Device& device);
98 
99   // Borrowed from the engine config
100   ::dawn::Device* device_ = nullptr;
101   // Dawn color attachment textures
102   std::vector<::dawn::Texture> textures_;
103   // Views into Dawn color attachment textures
104   std::vector<::dawn::TextureView> texture_views_;
105   // Dawn depth/stencil texture
106   ::dawn::Texture depth_stencil_texture_;
107   // Mapping from the generic engine's Pipeline object to our own Dawn-specific
108   // pipelines.
109   std::unordered_map<amber::Pipeline*, ::amber::dawn::Pipeline> pipeline_map_;
110 };
111 
112 }  // namespace dawn
113 }  // namespace amber
114 
115 #endif  // SRC_DAWN_ENGINE_DAWN_H_
116