GLSL-Shader-Includes icon indicating copy to clipboard operation
GLSL-Shader-Includes copied to clipboard

Loading same file recursively

Open RaphaelK12 opened this issue 4 years ago • 3 comments

Congratulations on the job of creating a nice feature to load files recursively, it's something very useful, because OpenGL doesn't have the feature to include files.

I had a small problem when 2 files included the same file, causing it to generate repeated code, here is a more critical example.

Having these 3 files: file1.txt file2.txt file3.txt

[file1.txt]

//some code
#include file2.txt

// add code

[file2.txt]

//some code
#include file3.txt
// add code

[file3.txt]

// add code
#include file2.txt

The code will load the files: file1.txt file2.txt file3.txt file2.txt file3.txt file2.txt file3.txt file2.txt file3.txt ...

Until the moment it fills up memory or fails to load any of the files, as it is loading recursively and does not have the implementation to check if the file has already been included or not.

But it is possible to make it not add the same file more than once with the following modification:

...
class Shadinclude {
public:
	// Return the source code of the complete shader
	static std::string load(std::string path, std::string includeIndentifier = "#include") {
		std::list<std::string> fileList;
		// add the original path to the list to avoid reuse
		fileList.push_back(path);
		return load2(path, includeIndentifier + ' ', fileList);
	}

private:
	static std::string load2(std::string path, std::string includeIndentifier, std::list<std::string> &fileList) {
		//includeIndentifier += ' ';
		static bool isRecursiveCall = false;
...
		auto it = std::find(fileList.begin(), fileList.end(), lineBuffer);
		// test if the path is not on the list
		if (fileList.end() == it) {
			isRecursiveCall = true;
			//add path to list
			fileList.push_back(lineBuffer);
			// By using recursion, the new include file can be extracted
			// and inserted at this location in the shader source code
			// if it has not already been inserted
			fullSourceCode += load2(lineBuffer, includeIndentifier, fileList);
		}
		else {
			// file already included, no need to include it again
		}

...

Hope I helped improve something :)

Thanks.

RaphaelK12 avatar Oct 06 '21 16:10 RaphaelK12

Hey @RaphaelK12!

Apologies for relying so late to this issue! I've been crazy busy at work and haven't been able to do much work on this repository lately.

Good catch, this is definitely something I missed before. Thanks a bunch for the suggestions.

I'll try to update the repository sometime soon with this solution.

Cheers!

tntmeijs avatar Dec 20 '21 16:12 tntmeijs

Hey @tntmeijs! It's been a while since you decided to update the repo to resolve this issue.. so.

krovee avatar Jun 06 '24 23:06 krovee

Hey @krovee, thanks for the reminder! My aplogies, completely slipped my mind. I haven't had a chance to get back to this project to open a PR with these changes. I do welcome PRs, though! If you propose the changes, I'd be more than happy to merge 'em! :D

tntmeijs avatar Jun 09 '24 22:06 tntmeijs