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

utils_shader.py « snap_context « modules - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1758585af361f12e6f0dbacadc3693c5ad6ec0ee (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
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  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 3
#  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, see <http://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####


import bgl

def check_shaderError(shader, flag, isProgram, errorMessage):
    success = bgl.Buffer(bgl.GL_INT, 1)

    if isProgram:
        bgl.glGetProgramiv(shader, flag, success)
    else:
        bgl.glGetShaderiv(shader, flag, success)

    if success[0] == bgl.GL_FALSE:
        import numpy as np
        import ctypes

        offset = bgl.Buffer(bgl.GL_INT, 1, (ctypes.c_int32 * 1).from_address(0))
        error = bgl.Buffer(bgl.GL_BYTE, 1024)
        if isProgram:
            bgl.glGetProgramInfoLog(shader, 1024, offset, error)
            print(errorMessage, np.bytes_(error).decode("utf-8"))
        else:
            bgl.glGetShaderInfoLog(shader, 1024, offset, error)
            print(errorMessage, np.bytes_(error).decode("utf-8"))

        del offset
        raise #RuntimeError(errorMessage, bgl.glGetShaderInfoLog(shader))


def create_shader(source, shaderType):
    shader = bgl.glCreateShader(shaderType)

    if shader == 0:
        raise RuntimeError("Error: Shader creation failed!")

    bgl.glShaderSource(shader, source)
    bgl.glCompileShader(shader)

    check_shaderError(shader, bgl.GL_COMPILE_STATUS, False, "Error: Shader compilation failed:")

    return shader


class Shader():
    def __init__(self, vertexcode, geomcode, fragcode):
        self.program = bgl.glCreateProgram()
        self.shaders = []

        if vertexcode:
            self.shaders.append(create_shader(vertexcode, bgl.GL_VERTEX_SHADER))
        if geomcode:
            self.shaders.append(create_shader(geomcode, bgl.GL_GEOMETRY_SHADER))
        if fragcode:
            self.shaders.append(create_shader(fragcode, bgl.GL_FRAGMENT_SHADER))

        for shad in self.shaders:
            bgl.glAttachShader(self.program, shad)

        bgl.glLinkProgram(self.program)
        check_shaderError(self.program, bgl.GL_LINK_STATUS, True, "Error: Program linking failed:")
        bgl.glValidateProgram(self.program)
        check_shaderError(self.program, bgl.GL_VALIDATE_STATUS, True, "Error: Program is invalid:")

    def __del__(self):
        for shad in self.shaders:
            bgl.glDetachShader(self.program, shad)
            bgl.glDeleteShader(shad)
        bgl.glDeleteProgram(self.program)
        #print('shader_del')