2013-07-06 10:08:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011-2013 Gregory hainaut
|
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
*
|
|
|
|
* 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 2, 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 GNU Make; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "GSShaderOGL.h"
|
2013-08-05 20:25:25 +00:00
|
|
|
#include "GLState.h"
|
2013-07-06 10:08:52 +00:00
|
|
|
|
2014-02-01 11:11:14 +00:00
|
|
|
GSShaderOGL::GSShaderOGL(bool debug) :
|
2013-07-06 10:08:52 +00:00
|
|
|
m_debug_shader(debug),
|
2013-10-24 20:54:27 +00:00
|
|
|
m_vs_sub_count(0),
|
|
|
|
m_ps_sub_count(0)
|
2013-07-06 10:08:52 +00:00
|
|
|
{
|
2013-08-10 19:43:59 +00:00
|
|
|
|
2013-10-24 20:54:27 +00:00
|
|
|
memset(&m_vs_sub, 0, countof(m_vs_sub)*sizeof(m_vs_sub[0]));
|
2013-08-17 08:57:52 +00:00
|
|
|
memset(&m_ps_sub, 0, countof(m_ps_sub)*sizeof(m_ps_sub[0]));
|
2013-08-10 19:43:59 +00:00
|
|
|
|
2013-07-06 10:08:52 +00:00
|
|
|
m_single_prog.clear();
|
2013-07-13 11:39:45 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLLoader::found_GL_ARB_separate_shader_objects) {
|
2013-07-06 10:08:52 +00:00
|
|
|
gl_GenProgramPipelines(1, &m_pipeline);
|
|
|
|
gl_BindProgramPipeline(m_pipeline);
|
|
|
|
}
|
2013-07-13 11:39:45 +00:00
|
|
|
#endif
|
2013-07-06 10:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSShaderOGL::~GSShaderOGL()
|
|
|
|
{
|
2013-07-13 11:39:45 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLLoader::found_GL_ARB_separate_shader_objects)
|
2013-07-06 10:08:52 +00:00
|
|
|
gl_DeleteProgramPipelines(1, &m_pipeline);
|
2013-07-13 11:39:45 +00:00
|
|
|
#endif
|
2013-07-06 10:08:52 +00:00
|
|
|
|
|
|
|
for (auto it = m_single_prog.begin(); it != m_single_prog.end() ; it++) gl_DeleteProgram(it->second);
|
|
|
|
m_single_prog.clear();
|
|
|
|
}
|
|
|
|
|
2013-10-24 20:54:27 +00:00
|
|
|
void GSShaderOGL::VS(GLuint s, GLuint sub_count)
|
2013-07-06 10:08:52 +00:00
|
|
|
{
|
2013-08-05 20:25:25 +00:00
|
|
|
if (GLState::vs != s)
|
2013-07-06 10:08:52 +00:00
|
|
|
{
|
2013-10-24 20:54:27 +00:00
|
|
|
m_vs_sub_count = sub_count;
|
|
|
|
|
2013-08-05 20:25:25 +00:00
|
|
|
GLState::vs = s;
|
2013-08-10 19:43:59 +00:00
|
|
|
GLState::dirty_prog = true;
|
2013-10-24 20:54:27 +00:00
|
|
|
GLState::dirty_subroutine_vs = true;
|
2013-07-13 11:39:45 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLLoader::found_GL_ARB_separate_shader_objects)
|
2013-07-06 10:08:52 +00:00
|
|
|
gl_UseProgramStages(m_pipeline, GL_VERTEX_SHADER_BIT, s);
|
2013-07-13 11:39:45 +00:00
|
|
|
#endif
|
2013-07-06 10:08:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 20:54:27 +00:00
|
|
|
void GSShaderOGL::VS_subroutine(GLuint *sub)
|
|
|
|
{
|
|
|
|
if (!(m_vs_sub[0] == sub[0])) {
|
|
|
|
m_vs_sub[0] = sub[0];
|
|
|
|
GLState::dirty_subroutine_vs = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 19:43:59 +00:00
|
|
|
void GSShaderOGL::PS_subroutine(GLuint *sub)
|
|
|
|
{
|
2013-10-24 20:54:27 +00:00
|
|
|
// FIXME could be more efficient with GSvector
|
|
|
|
if (!(m_ps_sub[0] == sub[0] && m_ps_sub[1] == sub[1] && m_ps_sub[2] == sub[2] && m_ps_sub[3] == sub[3] && m_ps_sub[4] == sub[4])) {
|
2013-08-10 19:43:59 +00:00
|
|
|
m_ps_sub[0] = sub[0];
|
|
|
|
m_ps_sub[1] = sub[1];
|
2013-10-24 20:54:27 +00:00
|
|
|
m_ps_sub[2] = sub[2];
|
|
|
|
m_ps_sub[3] = sub[3];
|
|
|
|
m_ps_sub[4] = sub[4];
|
2013-08-10 19:43:59 +00:00
|
|
|
GLState::dirty_subroutine_ps = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-17 08:57:52 +00:00
|
|
|
void GSShaderOGL::PS_ressources(GLuint64 handle[2])
|
|
|
|
{
|
|
|
|
if (handle[0] != GLState::tex_handle[0] || handle[1] != GLState::tex_handle[1]) {
|
|
|
|
GLState::tex_handle[0] = handle[0];
|
|
|
|
GLState::tex_handle[1] = handle[1];
|
|
|
|
GLState::dirty_ressources = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 19:43:59 +00:00
|
|
|
void GSShaderOGL::PS(GLuint s, GLuint sub_count)
|
2013-07-06 10:08:52 +00:00
|
|
|
{
|
2013-08-05 20:25:25 +00:00
|
|
|
if (GLState::ps != s)
|
2013-07-06 10:08:52 +00:00
|
|
|
{
|
2013-10-24 20:54:27 +00:00
|
|
|
m_ps_sub_count = sub_count;
|
2013-08-10 19:43:59 +00:00
|
|
|
|
2015-04-03 18:28:44 +00:00
|
|
|
#ifndef _DEBUG
|
|
|
|
// In debug always sets the program. It allow to replace the program in apitrace easily.
|
2013-08-05 20:25:25 +00:00
|
|
|
GLState::ps = s;
|
2015-04-03 18:28:44 +00:00
|
|
|
#endif
|
2013-08-10 19:43:59 +00:00
|
|
|
GLState::dirty_prog = true;
|
2013-08-28 08:44:16 +00:00
|
|
|
GLState::dirty_subroutine_ps = true;
|
2014-09-27 17:11:43 +00:00
|
|
|
GLState::dirty_ressources = true;
|
2013-07-13 11:39:45 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLLoader::found_GL_ARB_separate_shader_objects) {
|
2013-07-06 10:08:52 +00:00
|
|
|
gl_UseProgramStages(m_pipeline, GL_FRAGMENT_SHADER_BIT, s);
|
2013-08-10 19:43:59 +00:00
|
|
|
}
|
2013-07-13 11:39:45 +00:00
|
|
|
#endif
|
2013-07-06 10:08:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSShaderOGL::GS(GLuint s)
|
|
|
|
{
|
2013-08-05 20:25:25 +00:00
|
|
|
if (GLState::gs != s)
|
2013-07-06 10:08:52 +00:00
|
|
|
{
|
2013-08-05 20:25:25 +00:00
|
|
|
GLState::gs = s;
|
2013-08-10 19:43:59 +00:00
|
|
|
GLState::dirty_prog = true;
|
2013-07-13 11:39:45 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLLoader::found_GL_ARB_separate_shader_objects)
|
2013-07-06 10:08:52 +00:00
|
|
|
gl_UseProgramStages(m_pipeline, GL_GEOMETRY_SHADER_BIT, s);
|
2013-07-13 11:39:45 +00:00
|
|
|
#endif
|
2013-07-06 10:08:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-17 08:57:52 +00:00
|
|
|
void GSShaderOGL::SetupRessources()
|
|
|
|
{
|
2014-03-29 10:24:39 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-08-17 08:57:52 +00:00
|
|
|
if (!GLLoader::found_GL_ARB_bindless_texture) return;
|
|
|
|
|
|
|
|
if (GLState::dirty_ressources) {
|
|
|
|
GLState::dirty_ressources = false;
|
|
|
|
if (GLLoader::found_GL_ARB_separate_shader_objects) {
|
2014-09-27 17:11:43 +00:00
|
|
|
gl_ProgramUniformHandleui64vARB(GLState::ps, 0, 1, &GLState::tex_handle[0]);
|
|
|
|
if (GLState::tex_handle[1])
|
|
|
|
gl_ProgramUniformHandleui64vARB(GLState::ps, 1, 1, &GLState::tex_handle[1]);
|
2013-08-17 08:57:52 +00:00
|
|
|
} else {
|
2014-09-27 17:11:43 +00:00
|
|
|
gl_UniformHandleui64vARB(0, 1, &GLState::tex_handle[0]);
|
|
|
|
if (GLState::tex_handle[1])
|
|
|
|
gl_UniformHandleui64vARB(1, 1, &GLState::tex_handle[1]);
|
2013-08-17 08:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-29 10:24:39 +00:00
|
|
|
#endif
|
2013-08-17 08:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSShaderOGL::SetupSubroutineUniform()
|
2013-08-10 19:43:59 +00:00
|
|
|
{
|
2014-03-29 10:24:39 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-08-10 19:43:59 +00:00
|
|
|
if (!GLLoader::found_GL_ARB_shader_subroutine) return;
|
|
|
|
|
2013-10-24 20:54:27 +00:00
|
|
|
if (GLState::dirty_subroutine_vs && m_vs_sub_count) {
|
|
|
|
gl_UniformSubroutinesuiv(GL_VERTEX_SHADER, m_vs_sub_count, m_vs_sub);
|
|
|
|
GLState::dirty_subroutine_vs = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GLState::dirty_subroutine_ps && m_ps_sub_count) {
|
|
|
|
gl_UniformSubroutinesuiv(GL_FRAGMENT_SHADER, m_ps_sub_count, m_ps_sub);
|
2013-08-17 08:57:52 +00:00
|
|
|
GLState::dirty_subroutine_ps = false;
|
|
|
|
}
|
2014-03-29 10:24:39 +00:00
|
|
|
#endif
|
2013-08-10 19:43:59 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 10:08:52 +00:00
|
|
|
bool GSShaderOGL::ValidateShader(GLuint s)
|
|
|
|
{
|
|
|
|
if (!m_debug_shader) return true;
|
|
|
|
|
2014-11-09 14:27:24 +00:00
|
|
|
GLint status = 0;
|
2013-07-06 10:08:52 +00:00
|
|
|
gl_GetShaderiv(s, GL_COMPILE_STATUS, &status);
|
|
|
|
if (status) return true;
|
|
|
|
|
|
|
|
GLint log_length = 0;
|
|
|
|
gl_GetShaderiv(s, GL_INFO_LOG_LENGTH, &log_length);
|
|
|
|
if (log_length > 0) {
|
|
|
|
char* log = new char[log_length];
|
|
|
|
gl_GetShaderInfoLog(s, log_length, NULL, log);
|
|
|
|
fprintf(stderr, "%s", log);
|
|
|
|
delete[] log;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GSShaderOGL::ValidateProgram(GLuint p)
|
|
|
|
{
|
|
|
|
if (!m_debug_shader) return true;
|
|
|
|
|
2014-11-09 14:27:24 +00:00
|
|
|
GLint status = 0;
|
2013-07-06 10:08:52 +00:00
|
|
|
gl_GetProgramiv(p, GL_LINK_STATUS, &status);
|
|
|
|
if (status) return true;
|
|
|
|
|
|
|
|
GLint log_length = 0;
|
|
|
|
gl_GetProgramiv(p, GL_INFO_LOG_LENGTH, &log_length);
|
|
|
|
if (log_length > 0) {
|
|
|
|
char* log = new char[log_length];
|
|
|
|
gl_GetProgramInfoLog(p, log_length, NULL, log);
|
|
|
|
fprintf(stderr, "%s", log);
|
|
|
|
delete[] log;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GSShaderOGL::ValidatePipeline(GLuint p)
|
|
|
|
{
|
2013-07-13 11:39:45 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-07-06 10:08:52 +00:00
|
|
|
if (!m_debug_shader) return true;
|
|
|
|
|
|
|
|
// FIXME: might be mandatory to validate the pipeline
|
|
|
|
gl_ValidateProgramPipeline(p);
|
|
|
|
|
2014-11-09 14:27:24 +00:00
|
|
|
GLint status = 0;
|
2013-07-06 10:08:52 +00:00
|
|
|
gl_GetProgramPipelineiv(p, GL_VALIDATE_STATUS, &status);
|
|
|
|
if (status) return true;
|
|
|
|
|
|
|
|
GLint log_length = 0;
|
|
|
|
gl_GetProgramPipelineiv(p, GL_INFO_LOG_LENGTH, &log_length);
|
|
|
|
if (log_length > 0) {
|
|
|
|
char* log = new char[log_length];
|
|
|
|
gl_GetProgramPipelineInfoLog(p, log_length, NULL, log);
|
|
|
|
fprintf(stderr, "%s", log);
|
|
|
|
delete[] log;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2013-07-13 11:39:45 +00:00
|
|
|
#endif
|
|
|
|
|
2013-07-06 10:08:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint GSShaderOGL::LinkNewProgram()
|
|
|
|
{
|
|
|
|
GLuint p = gl_CreateProgram();
|
2013-08-05 20:25:25 +00:00
|
|
|
if (GLState::vs) gl_AttachShader(p, GLState::vs);
|
|
|
|
if (GLState::ps) gl_AttachShader(p, GLState::ps);
|
|
|
|
if (GLState::gs) gl_AttachShader(p, GLState::gs);
|
2013-07-06 10:08:52 +00:00
|
|
|
|
|
|
|
gl_LinkProgram(p);
|
|
|
|
|
|
|
|
ValidateProgram(p);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSShaderOGL::UseProgram()
|
|
|
|
{
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLState::dirty_prog) {
|
|
|
|
if (!GLLoader::found_GL_ARB_separate_shader_objects) {
|
2013-10-24 20:54:27 +00:00
|
|
|
GLState::dirty_subroutine_vs = true;
|
2013-08-28 08:44:16 +00:00
|
|
|
GLState::dirty_subroutine_ps = true;
|
|
|
|
GLState::dirty_ressources = true;
|
|
|
|
|
2013-08-10 19:43:59 +00:00
|
|
|
hash_map<uint64, GLuint >::iterator it;
|
|
|
|
// Note: shader are integer lookup pointer. They start from 1 and incr
|
|
|
|
// every time you create a new shader OR a new program.
|
|
|
|
// Note2: vs & gs are precompiled at startup. FGLRX and radeon got value < 128.
|
|
|
|
// We migth be able to pack the value in a 32bits int
|
|
|
|
// I would need to check the behavior on Nvidia (pause/resume).
|
|
|
|
uint64 sel = (uint64)GLState::vs << 40 | (uint64)GLState::gs << 20 | GLState::ps;
|
|
|
|
it = m_single_prog.find(sel);
|
|
|
|
if (it == m_single_prog.end()) {
|
|
|
|
GLState::program = LinkNewProgram();
|
|
|
|
m_single_prog[sel] = GLState::program;
|
|
|
|
|
|
|
|
ValidateProgram(GLState::program);
|
|
|
|
|
2013-08-05 20:25:25 +00:00
|
|
|
gl_UseProgram(GLState::program);
|
2013-08-10 19:43:59 +00:00
|
|
|
} else {
|
|
|
|
GLuint prog = it->second;
|
|
|
|
if (prog != GLState::program) {
|
|
|
|
GLState::program = prog;
|
|
|
|
gl_UseProgram(GLState::program);
|
|
|
|
}
|
2013-08-05 20:25:25 +00:00
|
|
|
}
|
2013-07-06 10:08:52 +00:00
|
|
|
|
2013-08-10 19:43:59 +00:00
|
|
|
} else {
|
|
|
|
ValidatePipeline(m_pipeline);
|
|
|
|
}
|
2013-07-06 10:08:52 +00:00
|
|
|
}
|
2013-08-10 19:43:59 +00:00
|
|
|
|
2013-08-17 08:57:52 +00:00
|
|
|
SetupRessources();
|
|
|
|
|
|
|
|
SetupSubroutineUniform();
|
2013-08-10 19:43:59 +00:00
|
|
|
|
|
|
|
GLState::dirty_prog = false;
|
2013-07-06 10:08:52 +00:00
|
|
|
}
|
|
|
|
|
2013-07-07 16:13:11 +00:00
|
|
|
std::string GSShaderOGL::GenGlslHeader(const std::string& entry, GLenum type, const std::string& macro)
|
2013-07-06 10:08:52 +00:00
|
|
|
{
|
2013-07-07 16:13:11 +00:00
|
|
|
std::string header;
|
2013-07-12 21:12:34 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2014-09-30 20:04:42 +00:00
|
|
|
header = "#version 330 core\n";
|
2014-10-01 07:50:59 +00:00
|
|
|
// Need GL version 420
|
|
|
|
header += "#extension GL_ARB_shading_language_420pack: require\n";
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLLoader::found_GL_ARB_separate_shader_objects) {
|
2013-07-28 14:40:43 +00:00
|
|
|
// Need GL version 410
|
2013-08-28 08:44:16 +00:00
|
|
|
header += "#extension GL_ARB_separate_shader_objects: require\n";
|
2013-07-06 10:08:52 +00:00
|
|
|
}
|
2013-08-28 08:44:16 +00:00
|
|
|
if (GLLoader::found_GL_ARB_shader_subroutine && GLLoader::found_GL_ARB_explicit_uniform_location) {
|
2013-08-10 19:43:59 +00:00
|
|
|
// Need GL version 400
|
|
|
|
header += "#define SUBROUTINE_GL40 1\n";
|
2013-08-28 08:44:16 +00:00
|
|
|
header += "#extension GL_ARB_shader_subroutine: require\n";
|
2013-10-24 20:54:27 +00:00
|
|
|
}
|
|
|
|
if (GLLoader::found_GL_ARB_explicit_uniform_location) {
|
2013-08-28 08:44:16 +00:00
|
|
|
// Need GL version 430
|
|
|
|
header += "#extension GL_ARB_explicit_uniform_location: require\n";
|
2013-08-10 19:43:59 +00:00
|
|
|
}
|
2013-10-24 20:54:27 +00:00
|
|
|
if (GLLoader::found_GL_ARB_shader_image_load_store) {
|
2013-07-28 14:40:43 +00:00
|
|
|
// Need GL version 420
|
|
|
|
header += "#extension GL_ARB_shader_image_load_store: require\n";
|
2013-10-24 20:54:27 +00:00
|
|
|
} else {
|
2013-07-28 14:40:43 +00:00
|
|
|
header += "#define DISABLE_GL42_image\n";
|
2013-10-24 20:54:27 +00:00
|
|
|
}
|
|
|
|
if (GLLoader::found_GL_ARB_bindless_texture && GLLoader::found_GL_ARB_explicit_uniform_location) {
|
2014-09-30 20:04:42 +00:00
|
|
|
// ARB extension (4.4)
|
2013-10-24 20:54:27 +00:00
|
|
|
header += "#extension GL_ARB_bindless_texture: require\n";
|
|
|
|
header += "#define ENABLE_BINDLESS_TEX\n";
|
|
|
|
}
|
2014-09-15 20:28:35 +00:00
|
|
|
if (GLLoader::found_GL_ARB_clip_control) {
|
2014-09-27 17:39:17 +00:00
|
|
|
header += "#define ZERO_TO_ONE_DEPTH\n";
|
2014-01-31 21:30:54 +00:00
|
|
|
}
|
2014-09-30 20:04:42 +00:00
|
|
|
#ifdef ENABLE_OGL_STENCIL_DEBUG
|
|
|
|
header += "#define ENABLE_OGL_STENCIL_DEBUG 1\n";
|
|
|
|
#endif
|
2014-09-30 19:56:36 +00:00
|
|
|
#else // ENABLE_GLES
|
|
|
|
header = "#version 310 es\n";
|
|
|
|
header += "#extension GL_EXT_shader_io_blocks: require\n";
|
2014-10-01 07:50:59 +00:00
|
|
|
// Disable full GL features (actually GLES3.1 could support it)
|
2013-07-28 14:40:43 +00:00
|
|
|
header += "#define DISABLE_GL42_image\n";
|
2013-07-12 21:12:34 +00:00
|
|
|
#endif
|
|
|
|
|
2014-01-11 15:01:13 +00:00
|
|
|
// Stupid GL implementation (can't use GL_ES)
|
|
|
|
// AMD/nvidia define it to 0
|
|
|
|
// intel window don't define it
|
|
|
|
// intel linux refuse to define it
|
|
|
|
#ifdef ENABLE_GLES
|
|
|
|
header += "#define pGL_ES 1\n";
|
2014-04-12 10:40:23 +00:00
|
|
|
header += "precision highp float;\n";
|
2014-01-11 15:01:13 +00:00
|
|
|
#else
|
|
|
|
header += "#define pGL_ES 0\n";
|
2013-12-30 14:48:26 +00:00
|
|
|
#endif
|
|
|
|
|
2013-07-06 10:08:52 +00:00
|
|
|
// Allow to puts several shader in 1 files
|
|
|
|
switch (type) {
|
|
|
|
case GL_VERTEX_SHADER:
|
2013-07-07 16:13:11 +00:00
|
|
|
header += "#define VERTEX_SHADER 1\n";
|
2013-07-06 10:08:52 +00:00
|
|
|
break;
|
2013-07-13 11:39:45 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-07-06 10:08:52 +00:00
|
|
|
case GL_GEOMETRY_SHADER:
|
2013-07-07 16:13:11 +00:00
|
|
|
header += "#define GEOMETRY_SHADER 1\n";
|
2013-07-06 10:08:52 +00:00
|
|
|
break;
|
2013-07-13 11:39:45 +00:00
|
|
|
#endif
|
2013-07-06 10:08:52 +00:00
|
|
|
case GL_FRAGMENT_SHADER:
|
2013-07-07 16:13:11 +00:00
|
|
|
header += "#define FRAGMENT_SHADER 1\n";
|
2013-07-06 10:08:52 +00:00
|
|
|
break;
|
|
|
|
default: ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select the entry point ie the main function
|
2013-07-07 16:13:11 +00:00
|
|
|
header += format("#define %s main\n", entry.c_str());
|
|
|
|
|
|
|
|
header += macro;
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
2013-07-06 10:08:52 +00:00
|
|
|
|
2013-07-07 16:13:11 +00:00
|
|
|
GLuint GSShaderOGL::Compile(const std::string& glsl_file, const std::string& entry, GLenum type, const char* glsl_h_code, const std::string& macro_sel)
|
|
|
|
{
|
2013-07-19 19:25:50 +00:00
|
|
|
ASSERT(glsl_h_code != NULL);
|
|
|
|
|
2013-07-07 16:13:11 +00:00
|
|
|
GLuint program = 0;
|
|
|
|
|
2013-07-13 11:39:45 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-07-07 16:13:11 +00:00
|
|
|
if (type == GL_GEOMETRY_SHADER && !GLLoader::found_geometry_shader) {
|
|
|
|
return program;
|
|
|
|
}
|
2013-07-13 11:39:45 +00:00
|
|
|
#endif
|
2013-07-06 10:08:52 +00:00
|
|
|
|
|
|
|
// Note it is better to separate header and source file to have the good line number
|
|
|
|
// in the glsl compiler report
|
2013-07-07 16:13:11 +00:00
|
|
|
const char* sources[2];
|
2013-07-06 10:08:52 +00:00
|
|
|
|
2013-07-07 16:13:11 +00:00
|
|
|
std::string header = GenGlslHeader(entry, type, macro_sel);
|
2013-07-19 19:25:50 +00:00
|
|
|
int shader_nb = 1;
|
2013-08-28 08:44:16 +00:00
|
|
|
#if 1
|
2013-07-07 16:13:11 +00:00
|
|
|
sources[0] = header.c_str();
|
2013-07-19 19:25:50 +00:00
|
|
|
sources[1] = glsl_h_code;
|
|
|
|
shader_nb++;
|
|
|
|
#else
|
|
|
|
sources[0] = header.append(glsl_h_code).c_str();
|
|
|
|
#endif
|
2013-07-06 10:08:52 +00:00
|
|
|
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLLoader::found_GL_ARB_separate_shader_objects) {
|
2013-07-13 11:39:45 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2013-07-19 19:25:50 +00:00
|
|
|
program = gl_CreateShaderProgramv(type, shader_nb, sources);
|
2013-07-13 11:39:45 +00:00
|
|
|
#endif
|
2013-07-06 10:08:52 +00:00
|
|
|
} else {
|
|
|
|
program = gl_CreateShader(type);
|
2013-07-19 19:25:50 +00:00
|
|
|
gl_ShaderSource(program, shader_nb, sources, NULL);
|
2013-07-06 10:08:52 +00:00
|
|
|
gl_CompileShader(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool status;
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLLoader::found_GL_ARB_separate_shader_objects)
|
2013-07-06 10:08:52 +00:00
|
|
|
status = ValidateProgram(program);
|
|
|
|
else
|
|
|
|
status = ValidateShader(program);
|
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
// print extra info
|
|
|
|
fprintf(stderr, "%s (entry %s, prog %d) :", glsl_file.c_str(), entry.c_str(), program);
|
|
|
|
fprintf(stderr, "\n%s", macro_sel.c_str());
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSShaderOGL::Delete(GLuint s)
|
|
|
|
{
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLLoader::found_GL_ARB_separate_shader_objects) {
|
2013-07-06 10:08:52 +00:00
|
|
|
gl_DeleteProgram(s);
|
|
|
|
} else {
|
|
|
|
gl_DeleteShader(s);
|
|
|
|
}
|
|
|
|
}
|