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 66 private: 67 // Returns the Dawn-specific render pipeline for the given command, 68 // if it exists. Returns nullptr otherwise. GetRenderPipeline(const::amber::PipelineCommand * command)69 RenderPipelineInfo* GetRenderPipeline( 70 const ::amber::PipelineCommand* command) { 71 return pipeline_map_[command->GetPipeline()].render_pipeline.get(); 72 } 73 // Returns the Dawn-specific compute pipeline for the given command, 74 // if it exists. Returns nullptr otherwise. GetComputePipeline(const::amber::PipelineCommand * command)75 ComputePipelineInfo* GetComputePipeline( 76 const ::amber::PipelineCommand* command) { 77 return pipeline_map_[command->GetPipeline()].compute_pipeline.get(); 78 } 79 // Creates and attaches index, vertex, storage, uniform and depth-stencil 80 // buffers. Sets up bindings. Also creates textures and texture views if not 81 // created yet. Used in the Graphics pipeline creation. 82 Result AttachBuffersAndTextures(RenderPipelineInfo* render_pipeline); 83 // Creates and attaches index, vertex, storage, uniform and depth-stencil 84 // buffers. Used in the Compute pipeline creation. 85 Result AttachBuffers(ComputePipelineInfo* compute_pipeline); 86 // Creates and submits a command to copy dawn textures back to amber color 87 // attachments. 88 Result MapDeviceTextureToHostBuffer(const RenderPipelineInfo& render_pipeline, 89 const ::dawn::Device& device); 90 // Creates and submits a command to copy dawn buffers back to amber buffers 91 Result MapDeviceBufferToHostBuffer( 92 const ComputePipelineInfo& compute_pipeline, 93 const ::dawn::Device& device); 94 95 // Borrowed from the engine config 96 ::dawn::Device* device_ = nullptr; 97 // Dawn color attachment textures 98 std::vector<::dawn::Texture> textures_; 99 // Views into Dawn color attachment textures 100 std::vector<::dawn::TextureView> texture_views_; 101 // Dawn depth/stencil texture 102 ::dawn::Texture depth_stencil_texture_; 103 // Mapping from the generic engine's Pipeline object to our own Dawn-specific 104 // pipelines. 105 std::unordered_map<amber::Pipeline*, ::amber::dawn::Pipeline> pipeline_map_; 106 }; 107 108 } // namespace dawn 109 } // namespace amber 110 111 #endif // SRC_DAWN_ENGINE_DAWN_H_ 112