openspades/Sources/Draw/GLProgram.cpp

105 lines
2.7 KiB
C++
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades 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.
OpenSpades 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 OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
2013-08-18 16:18:06 +09:00
#include "GLProgram.h"
#include "GLShader.h"
#include "../Core/Exception.h"
#include "../Core/Debug.h"
#include <string.h>
#include <stdio.h>
namespace spades {
namespace draw {
GLProgram::GLProgram(IGLDevice *d, std::string name):
device(d),name(name){
SPADES_MARK_FUNCTION();
handle = device->CreateProgram();
}
GLProgram::~GLProgram() {
SPADES_MARK_FUNCTION();
device->DeleteProgram(handle);
}
void GLProgram::Attach(spades::draw::GLShader *shader){
SPADES_MARK_FUNCTION();
device->AttachShader(handle, shader->GetHandle());
}
void GLProgram::Attach(IGLDevice::UInteger shader){
SPADES_MARK_FUNCTION();
device->AttachShader(handle, shader);
}
void GLProgram::Link(){
SPADES_MARK_FUNCTION();
device->LinkProgram(handle);
std::vector<char> errMsg;
errMsg.resize(device->GetProgramInteger(handle, IGLDevice::InfoLogLength) + 1);
IGLDevice::Sizei outLen;
device->GetProgramInfoLog(handle, errMsg.size(), &outLen, errMsg.data());
errMsg[outLen] = 0;
std::string err = errMsg.data();
if(device->GetProgramInteger(handle, IGLDevice::LinkStatus) == 0){
// error
SPRaise("Error while linking a program '%s':\n\n%s",name.c_str(), err.c_str());
}else{
if(errMsg.size() > 4){
2013-08-26 01:27:44 +09:00
SPLog("Messages for linking program '%s':\n%s",
2013-08-18 16:18:06 +09:00
name.c_str(), err.c_str());
}
}
linked = true;
}
void GLProgram::Validate(){
SPADES_MARK_FUNCTION();
device->ValidateProgram(handle);
if(device->GetProgramInteger(handle, IGLDevice::ValidateStatus) == 0){
// error
std::vector<char> errMsg;
errMsg.resize(device->GetProgramInteger(handle, IGLDevice::InfoLogLength) + 1);
IGLDevice::Sizei outLen;
device->GetShaderInfoLog(handle, errMsg.size(), &outLen, errMsg.data());
errMsg[outLen] = 0;
std::string err = errMsg.data();
SPRaise("Validation error while linking a program '%s':\n\n%s",
name.c_str(),errMsg.data());
}
}
void GLProgram::Use(){
SPADES_MARK_FUNCTION();
device->UseProgram(handle);
}
}
}