1 // Copyright 2015 The Shaderc Authors. All rights reserved.
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 GLSLC_FILE_INCLUDER_H_
16 #define GLSLC_FILE_INCLUDER_H_
17 
18 #include <mutex>
19 #include <string>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <utility>
23 #include <vector>
24 #include <unordered_set>
25 
26 #include "libshaderc_util/file_finder.h"
27 #include "shaderc/shaderc.hpp"
28 
29 namespace glslc {
30 
31 // An includer for files implementing shaderc's includer interface. It responds
32 // to the file including query from the compiler with the full path and content
33 // of the file to be included. In the case that the file is not found or cannot
34 // be opened, the full path field of in the response will point to an empty
35 // string, and error message will be passed to the content field.
36 // This class provides the basic thread-safety guarantee.
37 class FileIncluder : public shaderc::CompileOptions::IncluderInterface {
38  public:
FileIncluder(const shaderc_util::FileFinder * file_finder)39   explicit FileIncluder(const shaderc_util::FileFinder* file_finder)
40       : file_finder_(*file_finder) {}
41 
42   ~FileIncluder() override;
43 
44   // Resolves a requested source file of a given type from a requesting
45   // source into a shaderc_include_result whose contents will remain valid
46   // until it's released.
47   shaderc_include_result* GetInclude(const char* requested_source,
48                                      shaderc_include_type type,
49                                      const char* requesting_source,
50                                      size_t include_depth) override;
51   // Releases an include result.
52   void ReleaseInclude(shaderc_include_result* include_result) override;
53 
54   // Returns a reference to the member storing the set of included files.
file_path_trace()55   const std::unordered_set<std::string>& file_path_trace() const {
56     return included_files_;
57   }
58 
59  private:
60   // Used by GetInclude() to get the full filepath.
61   const shaderc_util::FileFinder& file_finder_;
62   // The full path and content of a source file.
63   struct FileInfo {
64     const std::string full_path;
65     std::vector<char> contents;
66   };
67 
68   // The set of full paths of included files.
69   std::unordered_set<std::string> included_files_;
70 };
71 
72 }  // namespace glslc
73 
74 #endif  // GLSLC_FILE_INCLUDER_H_
75