swift - 如何快速编译着色器,位置总是返回-1

标签 swift opengl-es-3.0

我写了一段代码来编译着色器。

class DFShader {
    let ID: GLuint

    init(vPath: String, fPath: String) {
        ID = glCreateProgram()
        let vertex = compile(type: GLenum(GL_VERTEX_SHADER), path: vPath)
        let fragment = compile(type: GLenum(GL_FRAGMENT_SHADER), path: fPath)

        var success: GLint = 1
        glAttachShader(ID, vertex)
        glAttachShader(ID, fragment)
        glLinkProgram(ID)
        glGetProgramiv(ID, GLenum(GL_LINK_STATUS), &success)
        if success == GL_FALSE {
            let infoLog = UnsafeMutablePointer<GLchar>.allocate(capacity: 512)
            glGetProgramInfoLog(ID, 512, nil, infoLog)
            print("link program error. \(String.init(cString: infoLog))")
        }
        glDeleteShader(vertex)
        glDeleteShader(fragment)

        //log
        print("model", glGetUniformLocation(ID, "model")) // print 4
        print("projection", glGetUniformLocation(ID, "projection")) // print 0
        print(ID.description)
    }

    private func compile(type: GLenum, path: String) -> GLuint {
        let shader = glCreateShader(type)
        var success: GLint = 1
        do {
            let source = try String.init(contentsOfFile: path, encoding: String.Encoding.utf8).cString(using: String.Encoding.utf8)
            var castSource = UnsafePointer<GLchar>(source)
            glShaderSource(shader, 1, &castSource, nil)
            glCompileShader(shader)
            glGetShaderiv(shader, GLenum(GL_COMPILE_STATUS), &success)
            if success == GL_FALSE {
                let infoLog = UnsafeMutablePointer<GLchar>.allocate(capacity: 512)
                glGetShaderInfoLog(shader, 512, nil, &infoLog.pointee)
                print("compile error. \(String.init(cString: infoLog))")
            }
        } catch {
            print("failed to read from shader source file.")
        }
        return shader
    }

    func use() {
        glUseProgram(ID)
    }
}

//vertex shader code
#version 300 es
layout(location = 0) in vec2 position;
out vec4 VertexColor;
uniform mat4 model;
uniform mat4 projection;
uniform float pointSize;
uniform vec4 color;
void main() {
    VertexColor = color;
    gl_Position = projection * model * vec4(position, 0.0, 1.0);
gl_PointSize = 10.0f;
}

//fragment shader code
#version 300 es
precision mediump float;
out vec4 FragColor;
in vec4 VertexColor;
void main() {
    FragColor = VertexColor;
}

//In the view
...
func setupShaders() {
    shader.use()
    let projection = GLKMatrix4MakeOrtho(0, Float(width), 0, Float(height), -1, 1)
    shader.setMat4(name: "projection", value: projection)
    print("projection", glGetUniformLocation(shader.ID, "projection")) // print -1.

...
}

我想获得所有制服和属性值的正确位置。

但是我从 DFShader 实例得到的统一位置是-1。我只在

中找到了正确的位置
init(vPath: String, fPath: String)

,在上面的代码中打印 0 和 4。 但是,当我在

setupShaders()

它返回-1。我在任何其他地方使用 shader.ID 获取位置,我也得到 -1。

最佳答案

着色器对象没有统一的位置。只有程序对象可以。

A shader object只不过是文本着色器和最终程序之间的中介。他们实际上不多。他们没有要查询的状态。

关于swift - 如何快速编译着色器,位置总是返回-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47719298/

相关文章:

ios - UISearchBar 和导航 Controller 中的两个按钮

swift - 如何在不像 Whatsapp 那样遮挡整个屏幕的情况下禁用 tableview?

c++ - 计算着色器 Open GL ES 的多个输入

ios - OpenGL ES 2.0 片段着色器在 iPhone 5S 上编译缓慢

c++ - 级联阴影贴图意外行为

c++ - 如何将这些 OpenGL 着色器转换为适用于 Android NDK 的 GLES3 的 OpenGL ES 着色器?

Swift - 阻止循环继续直到下载操作完成

ios - 用户在 UIImageView 中绘制内容后,如何将绘图保存在 tableview 中?

ios - 快速调整表格单元格图像

c++ - 使用 OpenGL ES 进行条件混合