aboutsummaryrefslogtreecommitdiff
path: root/src/dawn/engine_dawn.h
blob: 540a7051a4e718b55034d27aadd2045edd1abfc7 (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
// 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_DAWN_ENGINE_DAWN_H_
#define SRC_DAWN_ENGINE_DAWN_H_

#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>

#include "dawn/dawncpp.h"
#include "src/cast_hash.h"
#include "src/command.h"
#include "src/dawn/pipeline_info.h"
#include "src/engine.h"

namespace amber {
namespace dawn {

/// Engine implementation using the Dawn API.
class EngineDawn : public Engine {
 public:
  EngineDawn();
  ~EngineDawn() override;

  // Engine
  // Initialize with given configuration data.
  Result Initialize(EngineConfig* config,
                    Delegate*,
                    const std::vector<std::string>& features,
                    const std::vector<std::string>& instance_extensions,
                    const std::vector<std::string>& device_extensions) override;

  // Record info for a pipeline.  The Dawn render pipeline will be created
  // later.  Assumes necessary shader modules have been created.  A compute
  // pipeline requires a compute shader.  A graphics pipeline requires a vertex
  // and a fragment shader.
  Result CreatePipeline(::amber::Pipeline*) override;

  Result DoClearColor(const ClearColorCommand* cmd) override;
  Result DoClearStencil(const ClearStencilCommand* cmd) override;
  Result DoClearDepth(const ClearDepthCommand* cmd) override;
  Result DoClear(const ClearCommand* cmd) override;
  Result DoDrawRect(const DrawRectCommand* cmd) override;
  Result DoDrawArrays(const DrawArraysCommand* cmd) override;
  Result DoCompute(const ComputeCommand* cmd) override;
  Result DoEntryPoint(const EntryPointCommand* cmd) override;
  Result DoPatchParameterVertices(
      const PatchParameterVerticesCommand* cmd) override;
  Result DoBuffer(const BufferCommand* cmd) override;

 private:
  // Returns the Dawn-specific render pipeline for the given command,
  // if it exists.  Returns nullptr otherwise.
  RenderPipelineInfo* GetRenderPipeline(
      const ::amber::PipelineCommand* command) {
    return pipeline_map_[command->GetPipeline()].render_pipeline.get();
  }
  // Returns the Dawn-specific compute pipeline for the given command,
  // if it exists.  Returns nullptr otherwise.
  ComputePipelineInfo* GetComputePipeline(
      const ::amber::PipelineCommand* command) {
    return pipeline_map_[command->GetPipeline()].compute_pipeline.get();
  }
  // Creates and attaches index, vertex, storage, uniform and depth-stencil
  // buffers. Sets up bindings. Also creates textures and texture views if not
  // created yet. Used in the Graphics pipeline creation.
  Result AttachBuffersAndTextures(RenderPipelineInfo* render_pipeline);
  // Creates and attaches index, vertex, storage, uniform and depth-stencil
  // buffers. Used in the Compute pipeline creation.
  Result AttachBuffers(ComputePipelineInfo* compute_pipeline);
  // Creates and submits a command to copy dawn textures back to amber color
  // attachments.
  Result MapDeviceTextureToHostBuffer(const RenderPipelineInfo& render_pipeline,
                                      const ::dawn::Device& device);
  // Creates and submits a command to copy dawn buffers back to amber buffers
  Result MapDeviceBufferToHostBuffer(
      const ComputePipelineInfo& compute_pipeline,
      const ::dawn::Device& device);

  // Borrowed from the engine config
  ::dawn::Device* device_ = nullptr;
  // Dawn color attachment textures
  std::vector<::dawn::Texture> textures_;
  // Views into Dawn color attachment textures
  std::vector<::dawn::TextureView> texture_views_;
  // Dawn depth/stencil texture
  ::dawn::Texture depth_stencil_texture_;
  // Mapping from the generic engine's Pipeline object to our own Dawn-specific
  // pipelines.
  std::unordered_map<amber::Pipeline*, ::amber::dawn::Pipeline> pipeline_map_;
};

}  // namespace dawn
}  // namespace amber

#endif  // SRC_DAWN_ENGINE_DAWN_H_