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

GLShader.cpp « GUI « slic3r « src - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f3bc0893a92c863b744a415476f7cba69d49966 (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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <GL/glew.h>

#include "GLShader.hpp"

#include "libslic3r/Utils.hpp"
#include "3DScene.hpp"
#include <boost/nowide/fstream.hpp>

#include <string>
#include <utility>
#include <assert.h>

namespace Slic3r {

GLShader::~GLShader()
{
    assert(fragment_program_id == 0);
    assert(vertex_program_id == 0);
    assert(shader_program_id == 0);
}

// A safe wrapper around glGetString to report a "N/A" string in case glGetString returns nullptr.
inline std::string gl_get_string_safe(GLenum param)
{
    const char *value = (const char*)glGetString(param);
    return std::string(value ? value : "N/A");
}

bool GLShader::load_from_text(const char *fragment_shader, const char *vertex_shader)
{
    std::string gl_version = gl_get_string_safe(GL_VERSION);
    int major = atoi(gl_version.c_str());
    //int minor = atoi(gl_version.c_str() + gl_version.find('.') + 1);
    if (major < 2) {
        // Cannot create a shader object on OpenGL 1.x.
        // Form an error message.
        std::string gl_vendor    = gl_get_string_safe(GL_VENDOR);
        std::string gl_renderer  = gl_get_string_safe(GL_RENDERER);
        std::string glsl_version = gl_get_string_safe(GL_SHADING_LANGUAGE_VERSION);
        last_error = "Your computer does not support OpenGL shaders.\n";
#ifdef _WIN32
        if (gl_vendor == "Microsoft Corporation" && gl_renderer == "GDI Generic") {
            last_error = "Windows is using a software OpenGL renderer.\n"
                         "You are either connected over remote desktop,\n"
                         "or a hardware acceleration is not available.\n";
        }
#endif
        last_error += "GL version:   " + gl_version   + "\n";
        last_error += "vendor:       " + gl_vendor    + "\n";
        last_error += "renderer:     " + gl_renderer  + "\n";
        last_error += "GLSL version: " + glsl_version + "\n";
        return false;
    }

    if (fragment_shader != nullptr) {
        this->fragment_program_id = ::glCreateShader(GL_FRAGMENT_SHADER);
        glcheck();
        if (this->fragment_program_id == 0) {
            last_error = "glCreateShader(GL_FRAGMENT_SHADER) failed.";
            return false;
        }
        GLint len = (GLint)strlen(fragment_shader);
        glsafe(::glShaderSource(this->fragment_program_id, 1, &fragment_shader, &len));
        glsafe(::glCompileShader(this->fragment_program_id));
        GLint params;
        glsafe(::glGetShaderiv(this->fragment_program_id, GL_COMPILE_STATUS, &params));
        if (params == GL_FALSE) {
            // Compilation failed. Get the log.
            glsafe(::glGetShaderiv(this->fragment_program_id, GL_INFO_LOG_LENGTH, &params));
            std::vector<char> msg(params);
            glsafe(::glGetShaderInfoLog(this->fragment_program_id, params, &params, msg.data()));
            this->last_error = std::string("Fragment shader compilation failed:\n") + msg.data();
            this->release();
            return false;
        }
    }

    if (vertex_shader != nullptr) {
        this->vertex_program_id = ::glCreateShader(GL_VERTEX_SHADER);
        glcheck();
        if (this->vertex_program_id == 0) {
            last_error = "glCreateShader(GL_VERTEX_SHADER) failed.";
            this->release();
            return false;
        }
        GLint len = (GLint)strlen(vertex_shader);
        glsafe(::glShaderSource(this->vertex_program_id, 1, &vertex_shader, &len));
        glsafe(::glCompileShader(this->vertex_program_id));
        GLint params;
        glsafe(::glGetShaderiv(this->vertex_program_id, GL_COMPILE_STATUS, &params));
        if (params == GL_FALSE) {
            // Compilation failed. Get the log.
            glsafe(::glGetShaderiv(this->vertex_program_id, GL_INFO_LOG_LENGTH, &params));
            std::vector<char> msg(params);
            glsafe(::glGetShaderInfoLog(this->vertex_program_id, params, &params, msg.data()));
            this->last_error = std::string("Vertex shader compilation failed:\n") + msg.data();
            this->release();
            return false;
        }
    }

    // Link shaders
    this->shader_program_id = ::glCreateProgram();
    glcheck();
    if (this->shader_program_id == 0) {
        last_error = "glCreateProgram() failed.";
        this->release();
        return false;
    }

    if (this->fragment_program_id)
        glsafe(::glAttachShader(this->shader_program_id, this->fragment_program_id));
    if (this->vertex_program_id)
        glsafe(::glAttachShader(this->shader_program_id, this->vertex_program_id));
    glsafe(::glLinkProgram(this->shader_program_id));

    GLint params;
    glsafe(::glGetProgramiv(this->shader_program_id, GL_LINK_STATUS, &params));
    if (params == GL_FALSE) {
        // Linking failed. Get the log.
        glsafe(::glGetProgramiv(this->vertex_program_id, GL_INFO_LOG_LENGTH, &params));
        std::vector<char> msg(params);
        glsafe(::glGetProgramInfoLog(this->vertex_program_id, params, &params, msg.data()));
        this->last_error = std::string("Shader linking failed:\n") + msg.data();
        this->release();
        return false;
    }

    last_error.clear();
    return true;
}

bool GLShader::load_from_file(const char* fragment_shader_filename, const char* vertex_shader_filename)
{
    const std::string& path = resources_dir() + "/shaders/";

    boost::nowide::ifstream vs(path + std::string(vertex_shader_filename), boost::nowide::ifstream::binary);
    if (!vs.good())
        return false;

    vs.seekg(0, vs.end);
    int file_length = (int)vs.tellg();
    vs.seekg(0, vs.beg);
    std::string vertex_shader(file_length, '\0');
    vs.read(const_cast<char*>(vertex_shader.data()), file_length);
    if (!vs.good())
        return false;

    vs.close();

    boost::nowide::ifstream fs(path + std::string(fragment_shader_filename), boost::nowide::ifstream::binary);
    if (!fs.good())
        return false;

    fs.seekg(0, fs.end);
    file_length = (int)fs.tellg();
    fs.seekg(0, fs.beg);
    std::string fragment_shader(file_length, '\0');
    fs.read(const_cast<char*>(fragment_shader.data()), file_length);
    if (!fs.good())
        return false;

    fs.close();

    return load_from_text(fragment_shader.c_str(), vertex_shader.c_str());
}

void GLShader::release()
{
    if (this->shader_program_id) {
        if (this->vertex_program_id)
            glsafe(::glDetachShader(this->shader_program_id, this->vertex_program_id));
        if (this->fragment_program_id)
            glsafe(::glDetachShader(this->shader_program_id, this->fragment_program_id));
        glsafe(::glDeleteProgram(this->shader_program_id));
        this->shader_program_id = 0;
    }

    if (this->vertex_program_id) {
        glsafe(::glDeleteShader(this->vertex_program_id));
        this->vertex_program_id = 0;
    }
    if (this->fragment_program_id) {
        glsafe(::glDeleteShader(this->fragment_program_id));
        this->fragment_program_id = 0;
    }
}

void GLShader::enable() const
{
    glsafe(::glUseProgram(this->shader_program_id));
}

void GLShader::disable() const
{
    glsafe(::glUseProgram(0));
}

// Return shader vertex attribute ID
int GLShader::get_attrib_location(const char *name) const
{
    return this->shader_program_id ? glGetAttribLocation(this->shader_program_id, name) : -1;
}

// Return shader uniform variable ID
int GLShader::get_uniform_location(const char *name) const
{
    return this->shader_program_id ? glGetUniformLocation(this->shader_program_id, name) : -1;
}

bool GLShader::set_uniform(const char *name, float value) const
{
    int id = this->get_uniform_location(name);
    if (id >= 0) { 
        glsafe(::glUniform1fARB(id, value));
        return true;
    }
    return false;
}

bool GLShader::set_uniform(const char* name, const float* matrix) const
{
    int id = get_uniform_location(name);
    if (id >= 0)
    {
        glsafe(::glUniformMatrix4fv(id, 1, GL_FALSE, (const GLfloat*)matrix));
        return true;
    }
    return false;
}

bool GLShader::set_uniform(const char* name, int value) const
{
    int id = get_uniform_location(name);
    if (id >= 0)
    {
        glsafe(::glUniform1i(id, value));
        return true;
    }
    return false;
}

/*
# Set shader vector
sub SetVector
{
    my($self,$var,@values) = @_;

    my $id = $self->Map($var);
    return 'Unable to map $var' if (!defined($id));

    my $count = scalar(@values);
    eval('glUniform'.$count.'fARB($id,@values)');

    return '';
}

# Set shader 4x4 matrix
sub SetMatrix
{
    my($self,$var,$oga) = @_;

    my $id = $self->Map($var);
    return 'Unable to map $var' if (!defined($id));

    glUniformMatrix4fvARB_c($id,1,0,$oga->ptr());
    return '';
}
*/

#if ENABLE_TEXTURES_FROM_SVG
Shader::Shader()
    : m_shader(nullptr)
{
}

Shader::~Shader()
{
    reset();
}

bool Shader::init(const std::string& vertex_shader_filename, const std::string& fragment_shader_filename)
{
    if (is_initialized())
        return true;

    m_shader = new GLShader();
    if (m_shader != nullptr)
    {
        if (!m_shader->load_from_file(fragment_shader_filename.c_str(), vertex_shader_filename.c_str()))
        {
            std::cout << "Compilaton of shader failed:" << std::endl;
            std::cout << m_shader->last_error << std::endl;
            reset();
            return false;
        }
    }

    return true;
}

bool Shader::is_initialized() const
{
    return (m_shader != nullptr);
}

bool Shader::start_using() const
{
    if (is_initialized())
    {
        m_shader->enable();
        return true;
    }
    else
        return false;
}

void Shader::stop_using() const
{
    if (m_shader != nullptr)
        m_shader->disable();
}

int Shader::get_attrib_location(const std::string& name) const
{
    return (m_shader != nullptr) ? m_shader->get_attrib_location(name.c_str()) : -1;
}

int Shader::get_uniform_location(const std::string& name) const
{
    return (m_shader != nullptr) ? m_shader->get_uniform_location(name.c_str()) : -1;
}

void Shader::set_uniform(const std::string& name, float value) const
{
    if (m_shader != nullptr)
        m_shader->set_uniform(name.c_str(), value);
}

void Shader::set_uniform(const std::string& name, const float* matrix) const
{
    if (m_shader != nullptr)
        m_shader->set_uniform(name.c_str(), matrix);
}

void Shader::set_uniform(const std::string& name, bool value) const
{
    if (m_shader != nullptr)
        m_shader->set_uniform(name.c_str(), value ? 1 : 0);
}

unsigned int Shader::get_shader_program_id() const
{
    return (m_shader != nullptr) ? m_shader->shader_program_id : 0;
}

void Shader::reset()
{
    if (m_shader != nullptr)
    {
        m_shader->release();
        delete m_shader;
        m_shader = nullptr;
    }
}
#endif // ENABLE_TEXTURES_FROM_SVG

} // namespace Slic3r