Home | History | Annotate | Download | only in libshaderc_util
      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 LIBSHADERC_UTIL_COUNTING_INCLUDER_H
     16 #define LIBSHADERC_UTIL_COUNTING_INCLUDER_H
     17 
     18 #include <atomic>
     19 
     20 #include "glslang/Public/ShaderLang.h"
     21 
     22 namespace shaderc_util {
     23 
     24 // An Includer that counts how many #include directives it saw.
     25 class CountingIncluder : public glslang::TShader::Includer {
     26  public:
     27   // Done as .store(0) instead of in the initializer list for the following
     28   // reasons:
     29   // Clang > 3.6 will complain about it if it is written as ({0}).
     30   // VS2013 fails if it is written as {0}.
     31   // G++-4.8 does not correctly support std::atomic_init.
     32   CountingIncluder() {
     33     num_include_directives_.store(0);
     34   }
     35 
     36   // Resolves an include request for a source by name, type, and name of the
     37   // requesting source.  For the semantics of the result, see the base class.
     38   // Also increments num_include_directives and returns the results of
     39   // include_delegate(filename).  Subclasses should override include_delegate()
     40   // instead of this method.
     41   glslang::TShader::Includer::IncludeResult* include(
     42       const char* requested_source, glslang::TShader::Includer::IncludeType type,
     43       const char* requesting_source,
     44       size_t include_depth) final {
     45     ++num_include_directives_;
     46     return include_delegate(requested_source, type, requesting_source,
     47                             include_depth);
     48   }
     49 
     50   // Releases the given IncludeResult.
     51   void releaseInclude(glslang::TShader::Includer::IncludeResult* result) final {
     52     release_delegate(result);
     53   }
     54 
     55   int num_include_directives() const { return num_include_directives_.load(); }
     56 
     57  private:
     58   // Invoked by this class to provide results to
     59   // glslang::TShader::Includer::include.
     60   virtual glslang::TShader::Includer::IncludeResult* include_delegate(
     61       const char* requested_source, glslang::TShader::Includer::IncludeType type,
     62       const char* requesting_source, size_t include_depth) = 0;
     63 
     64   // Release the given IncludeResult.
     65   virtual void release_delegate(
     66       glslang::TShader::Includer::IncludeResult* result) = 0;
     67 
     68   // The number of #include directive encountered.
     69   std::atomic_int num_include_directives_;
     70 };
     71 }
     72 
     73 #endif  // LIBSHADERC_UTIL_COUNTING_INCLUDER_H
     74