Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gl_shader_patcher.cc « opengl « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15ab4222ae9bfb8e8e4d74e5020e79fa4c625b06 (plain)
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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2021 Blender Foundation.
 * All rights reserved.
 */

/** \file
 * \ingroup gpu
 *
 * Patch GLSL source from Vulkan GLSL to OpenGL GLSL.
 */

#include "gl_shader_patcher.hh"

#include <optional>

#include "CLG_log.h"

static CLG_LogRef LOG = {"gpu.gl.shader.converter"};

namespace blender::gpu {

static bool is_error_state(GLShaderPatcherState state)
{
  return !ELEM(state, GLShaderPatcherState::OkChanged, GLShaderPatcherState::OkUnchanged);
}

struct GLSLPatchResult {
  std::optional<std::string> patched_glsl;
  GLShaderPatcherState state = GLShaderPatcherState::OkUnchanged;

  void merge(const GLSLPatchResult &other, const StringRef unchanged_result)
  {
    switch (other.state) {
      case GLShaderPatcherState::OkUnchanged:
        patched_glsl = unchanged_result;
        break;
      case GLShaderPatcherState::OkChanged:
        patched_glsl = other.patched_glsl;
        if (state == GLShaderPatcherState::OkUnchanged) {
          state = GLShaderPatcherState::OkChanged;
        }
        break;
      case GLShaderPatcherState::MismatchedPushConstantNames:
        state = other.state;
        break;
    }
  }
};

class GLSLPatch {
 private:
  static constexpr StringRef WHITESPACES = " \t\n\v\f\r";
  static constexpr StringRef VALID_NAME_CHARS =
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01232456789_";

 public:
  virtual GLSLPatchResult patch(GLShaderPatcherContext &context, StringRef source) = 0;

 protected:
  static StringRef skip_whitespace(StringRef ref)
  {
    size_t skip = ref.find_first_not_of(WHITESPACES);
    if (skip == blender::StringRef::not_found) {
      return ref;
    }
    return ref.drop_prefix(skip);
  }

  static StringRef extract_name(StringRef src)
  {
    StringRef result = src;
    size_t skip = result.find_first_not_of(VALID_NAME_CHARS);
    BLI_assert(skip != StringRef::not_found);
    return result.substr(0, skip);
  }
};

class PatchPushConstants : public GLSLPatch {
  static constexpr StringRef LAYOUT_PUSH_CONSTANTS = "layout(push_constant)";
  static constexpr StringRef LAYOUT_STD140 = "layout(std140)";

 public:
  GLSLPatchResult patch(GLShaderPatcherContext &context, StringRef source) override
  {
    GLSLPatchResult result;

    size_t pos = source.find(LAYOUT_PUSH_CONSTANTS);
    if (pos == StringRef::not_found) {
      result.state = GLShaderPatcherState::OkUnchanged;
      return result;
    }

    std::stringstream patched_glsl;
    patched_glsl << source.substr(0, pos);
    patched_glsl << LAYOUT_STD140;
    patched_glsl << source.substr(pos + LAYOUT_PUSH_CONSTANTS.size());

    StringRef name = source.substr(pos + LAYOUT_PUSH_CONSTANTS.size());
    name = skip_whitespace(name);
    name = name.drop_known_prefix("uniform");
    name = skip_whitespace(name);
    name = extract_name(name);

    if (!context.push_constants.name) {
      context.push_constants.name = name;
    }
    else if (context.push_constants.name != name) {
      CLOG_ERROR(&LOG,
                 "Detected different push_constants binding names ('%s' and '%s'). push_constants "
                 "binding names must be identical across all stages.",
                 context.push_constants.name->c_str(),
                 std::string(name).c_str());
      result.state = GLShaderPatcherState::MismatchedPushConstantNames;
      return result;
    }

    std::string patched_glsl_str = patched_glsl.str();
    result.state = GLShaderPatcherState::OkChanged;
    GLSLPatchResult recursive_result = patch(context, patched_glsl_str);
    result.merge(recursive_result, patched_glsl_str);
    return result;
  };
};

class GLSLPatcher : public GLSLPatch {
 private:
  static void patch(GLShaderPatcherContext &context,
                    GLSLPatch &patch,
                    StringRef source,
                    GLSLPatchResult &r_result)
  {
    /* Do not patch when result is in error so the error state won't be rewritten. */
    if (is_error_state(r_result.state)) {
      return;
    }

    GLSLPatchResult patch_result = patch.patch(context, source);
    r_result.merge(patch_result, source);
  }

 public:
  GLSLPatchResult patch(GLShaderPatcherContext &context, StringRef source) override
  {
    GLSLPatchResult result;
    PatchPushConstants push_constants;
    patch(context, push_constants, source, result);
    return result;
  }
};

void patch_vulkan_to_opengl(GLShaderPatcherContext &context, MutableSpan<const char *> sources)
{
  for (int i = 0; i < sources.size(); i++) {
    GLSLPatcher patcher;
    const char *source = sources[i];
    GLSLPatchResult patch_result = patcher.patch(context, source);
    switch (patch_result.state) {
      case GLShaderPatcherState::OkUnchanged:
        break;

      case GLShaderPatcherState::OkChanged:
        BLI_assert(patch_result.patched_glsl);
        context.patched_sources.append(*patch_result.patched_glsl);
        sources[i] = context.patched_sources.last().c_str();

        /* Keep any errors from previous stages. */
        if (context.state == GLShaderPatcherState::OkUnchanged) {
          context.state = GLShaderPatcherState::OkChanged;
        }
        break;

      case GLShaderPatcherState::MismatchedPushConstantNames:
        context.state = patch_result.state;
        return;
        break;
    }
  }
}

void GLShaderPatcherContext::free_patched_sources()
{
  patched_sources.clear();
}

bool GLShaderPatcherContext::has_errors() const
{
  return is_error_state(state);
}

}  // namespace blender::gpu