File: ShaderPreprocessor.cpp

package info (click to toggle)
pymol 3.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,084 kB
  • sloc: cpp: 482,660; python: 89,328; ansic: 29,512; javascript: 6,792; sh: 84; makefile: 25
file content (283 lines) | stat: -rw-r--r-- 8,635 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "ShaderPreprocessor.h"

#include <bitset>
#include <cstdint>
#include <string_view>

#include "Feedback.h"
#include "FileStream.h"
#include "PyMOLGlobals.h"
#include "Setting.h"
#include "pymol/utility.h"

enum class PreProcType : std::uint32_t {
  Ifdef = 1 << 0,
  Ifndef = 1 << 1,
  Else = 1 << 2,
  Endif = 1 << 3,
  Include = 1 << 4,
  Lookup = 1 << 5,
};

constexpr std::size_t PreprocTypeElemCount = 6;

constexpr std::bitset<PreprocTypeElemCount> AsPreProcBS(PreProcType e)
{
  return std::bitset<PreprocTypeElemCount>(pymol::to_underlying(e));
}

constexpr PreProcType operator|(const PreProcType& lhs, const PreProcType& rhs)
{
  return static_cast<PreProcType>(
      pymol::to_underlying(lhs) | pymol::to_underlying(rhs));
}

constexpr PreProcType operator&(const PreProcType& lhs, const PreProcType& rhs)
{
  return static_cast<PreProcType>(
      pymol::to_underlying(lhs) & pymol::to_underlying(rhs));
}

/**
 * C++ 23's std::string_view::contains
 * @param str The string to search
 * @param c The character to search for
 * @return true if the string contains the character
 */
static constexpr bool contains(std::string_view str, char c)
{
  return str.find(c) != std::string_view::npos;
}

/**
 * Skips the string view to its next line
 * @param str The string view to skip
 * @return The string view after skipping to the next line
 */
static std::string_view SkipToNextLine(std::string_view str)
{
  auto lineBeginning = std::find_if(str.begin(), str.end(),
      [&](char c) { return c == '\n' || c == '\r' || c == '\0'; });

  static const auto whitespace = std::string_view(" \n\r\t");
  auto nonWhitespace = std::find_if_not(lineBeginning, str.end(), [](char c) {
    // C++23: return whitespace.contains(c);
    return contains(whitespace, c);
  });

  auto sizeToRemove = std::distance(str.begin(), nonWhitespace);
  return str.substr(sizeToRemove);
}

/**
 * Skips the string view to its next whitespace
 * @param str The string view to skip
 * @return The string view after skipping to the next whitespace
 */
static std::string_view SkipToNextWhitespace(std::string_view str)
{
  static const auto whitespace = std::string_view(" \n\r\t");
  auto whitespaceBeginning = std::find_if(str.begin(), str.end(), [&](char c) {
    // C++23: return whitespace.contains(c);
    return contains(whitespace, c);
  });
  auto sizeToRemove = std::distance(str.begin(), whitespaceBeginning);
  return str.substr(sizeToRemove);
}

ShaderPreprocessor::ShaderPreprocessor(
    PyMOLGlobals* G, std::map<std::string, const char*>* rawShaderCache)
    : m_G(G)
    , m_rawShaderCache(rawShaderCache)
{
}

void ShaderPreprocessor::setVar(std::string_view key, bool value)
{
  m_vars[std::string(key)] = value;
}

bool& ShaderPreprocessor::getVar(std::string_view key)
{
  return m_vars[std::string(key)];
}

static bool HasBit(
    std::bitset<PreprocTypeElemCount> bs, PreProcType preprocType)
{
  return (bs & AsPreProcBS(preprocType)).any();
}

static std::string GetShaderFromDisk(PyMOLGlobals* G, std::string_view filename)
{
  std::string_view pymolDataPath = getenv("PYMOL_DATA");

  if (pymolDataPath.empty()) {
    auto reason = std::string("shaders_from_disk=on, but PYMOL_DATA not set") +
                  pymolDataPath.data() + "\n";
    return {};
  }
  auto shaderPath = std::string(pymolDataPath)
                        .append(PATH_SEP)
                        .append("shaders")
                        .append(PATH_SEP)
                        .append(filename);

  std::string buffer;
  std::string_view pl;
  try {
    buffer = pymol::file_get_contents(shaderPath.c_str());
    pl = buffer.c_str();
  } catch (const std::exception&) {
    auto reason =
        std::string("shaders_from_dist=on, but unable to open file: ") +
        shaderPath;
    G->Feedback->autoAdd(FB_ShaderMgr, FB_Warnings, reason.c_str());
    return {};
  }
  return buffer;
}

std::string ShaderPreprocessor::getSource(std::string_view filename)
{
  auto it = m_cache_processed.find(filename.data());
  if (it != m_cache_processed.end()) {
    return it->second;
  }

  static const std::unordered_map<std::string, PreProcType> preProcMap{
      {"#ifdef", PreProcType::Lookup | PreProcType::Ifdef},
      {"#ifndef",
          PreProcType::Lookup | PreProcType::Ifdef | PreProcType::Ifndef},
      {"#else", PreProcType::Else},
      {"#endif", PreProcType::Endif},
      {"#include", PreProcType::Lookup | PreProcType::Include},
  };

  std::string shaderContentsBuffer;
  std::string_view shaderContents;
  if (SettingGet<bool>(m_G, cSetting_shaders_from_disk)) {
    shaderContentsBuffer = GetShaderFromDisk(m_G, filename);
    shaderContents = shaderContentsBuffer.c_str();
  }

  if (shaderContents.empty()) {
    if (m_rawShaderCache != nullptr) {
      auto it = m_rawShaderCache->find(filename.data());
      if (it != m_rawShaderCache->end()) {
        shaderContents = it->second;
      }
    }
    if (shaderContents.empty()) {
      auto reason = pymol::join_to_string(
          "Warning: Unable to find shader: ", filename, "\n");
      m_G->Feedback->autoAdd(FB_ShaderMgr, FB_Warnings, reason.c_str());
      return {};
    }
  }

  /* "if_depth" counts the level of nesting, and "true_depth" how far the
   * if conditions were actually true. So if the current block is true, then
   * if_depth == true_depth, otherwise if_depth > true_depth.
   */
  int if_depth = 0;
  int true_depth = 0;

  std::bitset<PreprocTypeElemCount> preproc;
  std::ostringstream newBuffer;
  /* Now we need to read through the shader and do processing if necessary */
  while (!shaderContents.empty()) {
    preproc.reset();

    // only do preprocessor lookup if line starts with a hash
    if (shaderContents[0] == '#') {
      // next white space
      auto tempShaderContents = SkipToNextWhitespace(shaderContents);

      // copy of first word
      std::string preprocKeyword(shaderContents.data(),
          tempShaderContents.data() - shaderContents.data());

      // lookup word in preprocmap
      auto preprocit = preProcMap.find(preprocKeyword);
      if (preprocit != preProcMap.end()) {
        preproc = AsPreProcBS(preprocit->second);

        if (HasBit(preproc, PreProcType::Lookup)) {
          if (if_depth == true_depth) {
            // copy of second word
            tempShaderContents.remove_prefix(1);
            std::string preprocArg(tempShaderContents.data(),
                SkipToNextWhitespace(tempShaderContents).data() -
                    tempShaderContents.data());

            if (HasBit(preproc, PreProcType::Ifdef)) {
              bool if_value = false;

              // lookup for boolean shader preprocessor values
              auto item = m_vars.find(preprocArg);
              if (item != m_vars.end()) {
                if_value = item->second;
              }
              if (HasBit(preproc, PreProcType::Ifndef)) {
                if_value = !if_value;
              }
              if (if_value) {
                true_depth++;
              }
            } else if (HasBit(preproc, PreProcType::Include)) {
              std::string includeFile(tempShaderContents.data(),
                  SkipToNextWhitespace(tempShaderContents).data() -
                      tempShaderContents.data());
              newBuffer << getSource(includeFile);
            }
          }

          if (HasBit(preproc, PreProcType::Ifdef)) {
            if_depth++;
          }

        } else if (HasBit(preproc, PreProcType::Endif)) {
          if (if_depth-- == true_depth) {
            true_depth--;
          }
        } else if (HasBit(preproc, PreProcType::Else)) {
          if (if_depth == true_depth) {
            true_depth--;
          } else if (if_depth == true_depth + 1) {
            true_depth++;
          }
        }
      }
    }

    std::string_view writeReadyShaderContents = shaderContents;
    shaderContents = SkipToNextLine(shaderContents);

    // add to the output buffer if this is a regular active line
    if (preproc.none() && if_depth == true_depth) {
      std::ptrdiff_t sizeToWrite =
          shaderContents.data() - writeReadyShaderContents.data();
      newBuffer.write(writeReadyShaderContents.data(), sizeToWrite);
    }
  }

  std::string result = newBuffer.str();
  setSource(filename, result);
  return result;
}

void ShaderPreprocessor::setSource(std::string_view filename, std::string_view source)
{
  m_cache_processed[filename.data()] = source;
}

void ShaderPreprocessor::invalidate(std::string_view filename)
{
  m_cache_processed.erase(std::string(filename));
}

void ShaderPreprocessor::clear()
{
  m_cache_processed.clear();
}