Browse Source

shaderpipeline: Fixes for resolving includes, always require full path

rdb 5 years ago
parent
commit
c90481e103

+ 5 - 5
panda/src/gobj/shader.cxx

@@ -806,7 +806,7 @@ do_read_source(Stage stage, const Filename &fn, BamCacheRecord *record) {
   }
   }
 
 
   shader_cat.info() << "Reading shader file: " << fn << "\n";
   shader_cat.info() << "Reading shader file: " << fn << "\n";
-  if (!do_read_source(stage, *in, fn, record)) {
+  if (!do_read_source(stage, *in, fullpath, record)) {
     vf->close_read_file(in);
     vf->close_read_file(in);
     return false;
     return false;
   }
   }
@@ -833,17 +833,17 @@ do_read_source(Stage stage, const Filename &fn, BamCacheRecord *record) {
  */
  */
 bool Shader::
 bool Shader::
 do_read_source(ShaderModule::Stage stage, std::istream &in,
 do_read_source(ShaderModule::Stage stage, std::istream &in,
-               const Filename &source_filename, BamCacheRecord *record) {
+               const Filename &fullpath, BamCacheRecord *record) {
   ShaderCompiler *compiler = get_compiler(_language);
   ShaderCompiler *compiler = get_compiler(_language);
   nassertr(compiler != nullptr, false);
   nassertr(compiler != nullptr, false);
 
 
-  PT(ShaderModule) module = compiler->compile_now(stage, in, source_filename, record);
+  PT(ShaderModule) module = compiler->compile_now(stage, in, fullpath, record);
   if (!module) {
   if (!module) {
     return false;
     return false;
   }
   }
 
 
-  if (!source_filename.empty()) {
-    module->set_source_filename(source_filename);
+  if (!fullpath.empty()) {
+    module->set_source_filename(fullpath);
   }
   }
 
 
   if (has_stage(stage)) {
   if (has_stage(stage)) {

+ 1 - 1
panda/src/gobj/shader.h

@@ -513,7 +513,7 @@ private:
   bool load(const ShaderFile &sbody, BamCacheRecord *record = nullptr);
   bool load(const ShaderFile &sbody, BamCacheRecord *record = nullptr);
   bool do_read_source(ShaderModule::Stage stage, const Filename &fn, BamCacheRecord *record);
   bool do_read_source(ShaderModule::Stage stage, const Filename &fn, BamCacheRecord *record);
   bool do_read_source(ShaderModule::Stage stage, std::istream &in,
   bool do_read_source(ShaderModule::Stage stage, std::istream &in,
-                      const Filename &source_filename, BamCacheRecord *record);
+                      const Filename &fullpath, BamCacheRecord *record);
   bool do_load_source(ShaderModule::Stage stage, const std::string &source, BamCacheRecord *record);
   bool do_load_source(ShaderModule::Stage stage, const std::string &source, BamCacheRecord *record);
 
 
 public:
 public:

+ 1 - 1
panda/src/shaderpipeline/shaderCompiler.cxx

@@ -54,7 +54,7 @@ compile_now(ShaderModule::Stage stage, const Filename &fn, BamCacheRecord *recor
   }
   }
 
 
   // The default implementation calls the version that takes an istream.
   // The default implementation calls the version that takes an istream.
-  PT(ShaderModule) module = compile_now(stage, *in, fn, record);
+  PT(ShaderModule) module = compile_now(stage, *in, vf->get_filename(), record);
   vf->close_read_file(in);
   vf->close_read_file(in);
   return module;
   return module;
 }
 }

+ 1 - 1
panda/src/shaderpipeline/shaderCompiler.h

@@ -41,7 +41,7 @@ PUBLISHED:
   virtual PT(ShaderModule) compile_now(Stage stage, const Filename &path,
   virtual PT(ShaderModule) compile_now(Stage stage, const Filename &path,
                                        BamCacheRecord *record = nullptr) const;
                                        BamCacheRecord *record = nullptr) const;
   virtual PT(ShaderModule) compile_now(Stage stage, std::istream &in,
   virtual PT(ShaderModule) compile_now(Stage stage, std::istream &in,
-                                       const std::string &filename = "created-shader",
+                                       const Filename &fullpath,
                                        BamCacheRecord *record = nullptr) const=0;
                                        BamCacheRecord *record = nullptr) const=0;
 
 
 public:
 public:

+ 16 - 2
panda/src/shaderpipeline/shaderCompilerGlslPreProc.cxx

@@ -51,13 +51,27 @@ get_languages() const {
  */
  */
 PT(ShaderModule) ShaderCompilerGlslPreProc::
 PT(ShaderModule) ShaderCompilerGlslPreProc::
 compile_now(ShaderModule::Stage stage, std::istream &in,
 compile_now(ShaderModule::Stage stage, std::istream &in,
-            const std::string &filename, BamCacheRecord *record) const {
+            const Filename &fullpath, BamCacheRecord *record) const {
   PT(ShaderModuleGlsl) module = new ShaderModuleGlsl(stage);
   PT(ShaderModuleGlsl) module = new ShaderModuleGlsl(stage);
   std::string &into = module->_raw_source;
   std::string &into = module->_raw_source;
 
 
+  // Create a name that's easier to read in error messages.
+  std::string filename;
+  if (fullpath.empty()) {
+    filename = "created-shader";
+  } else {
+    Filename fullpath_rel = fullpath;
+    if (fullpath_rel.make_relative_to(ExecutionEnvironment::get_environment_variable("MAIN_DIR")) &&
+        fullpath_rel.length() < fullpath.length()) {
+      filename = fullpath_rel;
+    } else {
+      filename = fullpath;
+    }
+  }
+
   std::ostringstream sstr;
   std::ostringstream sstr;
   std::set<Filename> open_files;
   std::set<Filename> open_files;
-  if (r_preprocess_source(module, sstr, in, filename, filename, open_files, record)) {
+  if (r_preprocess_source(module, sstr, in, filename, fullpath, open_files, record)) {
     into = sstr.str();
     into = sstr.str();
 
 
     // Strip trailing whitespace.
     // Strip trailing whitespace.

+ 1 - 1
panda/src/shaderpipeline/shaderCompilerGlslPreProc.h

@@ -30,7 +30,7 @@ public:
   virtual std::string get_name() const override;
   virtual std::string get_name() const override;
   virtual ShaderLanguages get_languages() const override;
   virtual ShaderLanguages get_languages() const override;
   virtual PT(ShaderModule) compile_now(Stage stage, std::istream &in,
   virtual PT(ShaderModule) compile_now(Stage stage, std::istream &in,
-                                       const std::string &filename = "created-shader",
+                                       const Filename &fullpath,
                                        BamCacheRecord *record = nullptr) const override;
                                        BamCacheRecord *record = nullptr) const override;
 
 
 private:
 private:

+ 19 - 5
panda/src/shaderpipeline/shaderCompilerGlslang.cxx

@@ -274,7 +274,7 @@ get_languages() const {
  */
  */
 PT(ShaderModule) ShaderCompilerGlslang::
 PT(ShaderModule) ShaderCompilerGlslang::
 compile_now(ShaderModule::Stage stage, std::istream &in,
 compile_now(ShaderModule::Stage stage, std::istream &in,
-            const std::string &filename, BamCacheRecord *record) const {
+            const Filename &fullpath, BamCacheRecord *record) const {
 
 
   vector_uchar code;
   vector_uchar code;
   if (!VirtualFile::simple_read_file(&in, code)) {
   if (!VirtualFile::simple_read_file(&in, code)) {
@@ -290,16 +290,30 @@ compile_now(ShaderModule::Stage stage, std::istream &in,
   }
   }
   else {
   else {
     pset<Filename> once_files;
     pset<Filename> once_files;
-    if (!preprocess_glsl(code, glsl_version, filename, once_files, record)) {
+    if (!preprocess_glsl(code, glsl_version, fullpath, once_files, record)) {
       return nullptr;
       return nullptr;
     }
     }
   }
   }
 
 
+  // Create a name that's easier to read in error messages.
+  std::string filename;
+  if (fullpath.empty()) {
+    filename = "created-shader";
+  } else {
+    Filename fullpath_rel = fullpath;
+    if (fullpath_rel.make_relative_to(ExecutionEnvironment::get_environment_variable("MAIN_DIR")) &&
+        fullpath_rel.length() < fullpath.length()) {
+      filename = fullpath_rel;
+    } else {
+      filename = fullpath;
+    }
+  }
+
   if (!is_cg && glsl_version < 330 && glsl_version != 150) {
   if (!is_cg && glsl_version < 330 && glsl_version != 150) {
     if (glsl_version != 100 && glsl_version != 110 && glsl_version != 120 &&
     if (glsl_version != 100 && glsl_version != 110 && glsl_version != 120 &&
         glsl_version != 130 && glsl_version != 140 && glsl_version != 300) {
         glsl_version != 130 && glsl_version != 140 && glsl_version != 300) {
       shader_cat.error()
       shader_cat.error()
-        << "Invalid GLSL version " << glsl_version << ".\n";
+        << filename << " uses invalid GLSL version " << glsl_version << ".\n";
       return nullptr;
       return nullptr;
     }
     }
 
 
@@ -311,7 +325,7 @@ compile_now(ShaderModule::Stage stage, std::istream &in,
     static ShaderCompilerGlslPreProc preprocessor;
     static ShaderCompilerGlslPreProc preprocessor;
 
 
     std::istringstream stream(std::string((const char *)&code[0], code.size()));
     std::istringstream stream(std::string((const char *)&code[0], code.size()));
-    return preprocessor.compile_now(stage, stream, filename, record);
+    return preprocessor.compile_now(stage, stream, fullpath, record);
   }
   }
 
 
   static bool is_initialized = false;
   static bool is_initialized = false;
@@ -351,7 +365,7 @@ compile_now(ShaderModule::Stage stage, std::istream &in,
 
 
   const char *string = (const char *)code.data();
   const char *string = (const char *)code.data();
   const int length = (int)code.size();
   const int length = (int)code.size();
-  const char *fname = filename.c_str();
+  const char *fname = fullpath.c_str();
   shader.setStringsWithLengthsAndNames(&string, &length, &fname, 1);
   shader.setStringsWithLengthsAndNames(&string, &length, &fname, 1);
   shader.setEntryPoint("main");
   shader.setEntryPoint("main");
 
 

+ 1 - 1
panda/src/shaderpipeline/shaderCompilerGlslang.h

@@ -30,7 +30,7 @@ public:
   virtual std::string get_name() const override;
   virtual std::string get_name() const override;
   virtual ShaderLanguages get_languages() const override;
   virtual ShaderLanguages get_languages() const override;
   virtual PT(ShaderModule) compile_now(Stage stage, std::istream &in,
   virtual PT(ShaderModule) compile_now(Stage stage, std::istream &in,
-                                       const std::string &filename = "created-shader",
+                                       const Filename &fullpath,
                                        BamCacheRecord *record = nullptr) const override;
                                        BamCacheRecord *record = nullptr) const override;
 
 
 private:
 private: