You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.2 KiB

/*
15 years ago
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum 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 Lesser General Public License version 3 for more details.
*/
#include "AbstractShaderProgram.h"
#include <fstream>
#include "Shader.h"
#define LINKER_MESSAGE_MAX_LENGTH 1024
using namespace std;
namespace Magnum {
bool AbstractShaderProgram::use() {
if(state != Linked) return false;
glUseProgram(_id);
return true;
}
bool AbstractShaderProgram::attachShader(Shader& shader) {
GLuint _shader = shader.compile();
if(!_shader) return false;
glAttachShader(_id, _shader);
return true;
}
void AbstractShaderProgram::bindAttributeLocation(GLuint location, const string& name) {
CORRADE_ASSERT(state == Initialized, "AbstractShaderProgram: attribute cannot be bound after linking.", );
glBindAttribLocation(_id, location, name.c_str());
}
#ifndef MAGNUM_TARGET_GLES
void AbstractShaderProgram::bindFragmentDataLocation(GLuint location, const std::string& name) {
CORRADE_ASSERT(state == Initialized, "AbstractShaderProgram: fragment data location cannot be bound after linking.", );
glBindFragDataLocation(_id, location, name.c_str());
}
void AbstractShaderProgram::bindFragmentDataLocationIndexed(GLuint location, GLuint index, const std::string& name) {
CORRADE_ASSERT(state == Initialized, "AbstractShaderProgram: fragment data location cannot be bound after linking.", );
glBindFragDataLocationIndexed(_id, location, index, name.c_str());
}
#endif
void AbstractShaderProgram::link() {
/* Already compiled or failed, exit */
if(state != Initialized) return;
/* Link shader program */
glLinkProgram(_id);
/* Check link status */
GLint status;
glGetProgramiv(_id, GL_LINK_STATUS, &status);
/* Display errors or warnings */
char message[LINKER_MESSAGE_MAX_LENGTH];
glGetProgramInfoLog(_id, LINKER_MESSAGE_MAX_LENGTH, nullptr, message);
/* Show error log and delete shader */
if(status == GL_FALSE) {
Error() << "AbstractShaderProgram: linking failed with the following message:\n"
<< message;
/* Or just warnings, if there are any */
} else if(message[0] != 0) {
Debug() << "AbstractShaderProgram: linking succeeded with the following message:\n"
<< message;
}
state = status == GL_FALSE ? Failed : Linked;
}
GLint AbstractShaderProgram::uniformLocation(const std::string& name) {
14 years ago
/** @todo What if linking just failed (not programmer error?) */
CORRADE_ASSERT(state == Linked, "AbstractShaderProgram: uniform location cannot be retrieved before linking.", -1);
GLint location = glGetUniformLocation(_id, name.c_str());
if(location == -1)
Warning() << "AbstractShaderProgram: location of uniform \'" + name + "\' cannot be retrieved!";
return location;
}
}