Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-130324

Documentation for QOpenGLShaderProgram::addShaderFromSourceFile() and friends does not mention #pragma include

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • 6.7.2
    • GUI: OpenGL
    • None
    • All

    Description

      I am writing a small OpenGL enabled program and enjoying the Qt OpenGL spesific APIs such as QOpenGLWidget and QOpenGLShaderProgram etc.

      However one of my shaders which I am adapting from existing software has the following:

      #pragma include "common.glsl"
      #pragma include "triangle_table.glsl"

      and each of those separarate shader files contain the following at the top:

      #pragma once

      I tried loading the shaders using QOpenGLShaderProgram::addShaderFromSourceFile() in the hope that Qt6 OpenGL primitives would automatically discover and handle these pragma include and pragma once statements. It seems this is not the case, but looking at the documentation linked above there is no mention of this anywhere.

      So this suggestion is several parts:

      1. Is #pragma include supported for regular old files on disk?
      2. Is #pragma include{} supported for files embedded through the resource mechanism?
      3. Is #pragma once supported?
      4. Is support for these pragmas documented, and in that case where?

      It would be awesome to have a definitive answer to this in the documentation at least, and even better, full support for the statements.

      Please note that the naive workaround of "just do the include yourself" fails miserably. You can find my attempt below:

      // Function to recursively load shader source and resolve #pragma include and #pragma once directives
      QString ChunkWidget::preprocessShader(const QString &filePath) {
          auto out = qDebug().noquote().nospace();
          const auto alreadySeen = mProcessed.contains(filePath);
          mProcessed << filePath;
          QFile file(filePath);
          if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
              qWarning() << "Unable to open shader file:" << filePath;
              return QString();
      {{    }}}
          QTextStream in(&file);
          auto shaderSource = in.readAll();
          file.close();    QRegularExpression onceRegex(R"REGEX(#pragma\s+once\s*")REGEX");
          auto onceMatches = onceRegex.globalMatch(shaderSource);
          while(onceMatches.hasNext()){
              // Put pragma once into effect
              if(alreadySeen){
                  out<<"\n";
                  out<<"SHADER ----------------------\n";
                  out<<"SHADER" << filePath << ": PRAGMA ONCE SKIPPED\n";
                  out<<"\n";
                  return QString();
      {{        }}}
              auto match = onceMatches.next();
              shaderSource.replace(match.capturedStart(), match.capturedLength(), "");
      {{    }}}
          
          QRegularExpression includeRegex(R"REGEX(#pragma\s+include\s+"([^"]+)")REGEX");
          auto matches = includeRegex.globalMatch(shaderSource);
          while (matches.hasNext()) {
              auto match = matches.next();
              QString includeFile = match.captured(1);
              QString includePath = QFileInfo(filePath).absolutePath() + "/" + includeFile;
              QString includeContent = preprocessShader(includePath);
              shaderSource.replace(match.capturedStart(), match.capturedLength(), includeContent);
      {{    }}}
          out<<"\n";
          out<<"SHADER ----------------------\n";
          out<<"SHADER" << filePath << ":\n";
          //out<<"SHADER" << shaderSource;
          out<<"\n";
          return shaderSource;
      }

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            lagocs Laszlo Agocs
            lennartrolland@gmail.com Lennart Rolland
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes