void FTonemapShader::Bind(IRenderQueue *q) { auto &shader = mShader[gl_tonemap]; if (!shader) { auto prolog = GetDefines(gl_tonemap); shader.reset(screen->CreateShaderProgram()); shader->Compile(IShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330); shader->Compile(IShaderProgram::Fragment, "shaders/glsl/tonemap.fp", prolog, 330); shader->Link("shaders/glsl/tonemap"); } shader->Bind(q); }
void FTonemapShader::Bind() { auto &shader = mShader[gl_tonemap]; if (!shader) { shader.Compile(FShaderProgram::Vertex, "shaders/glsl/tonemap.vp", "", 330); shader.Compile(FShaderProgram::Fragment, "shaders/glsl/tonemap.fp", GetDefines(gl_tonemap), 330); shader.SetFragDataLocation(0, "FragColor"); shader.Link("shaders/glsl/tonemap"); shader.SetAttribLocation(0, "PositionInProjection"); SceneTexture.Init(shader, "InputTexture"); Exposure.Init(shader, "ExposureAdjustment"); } shader.Bind(); }
bool Shader::Compile(const std::string &vsData, const std::string &fsData) { DeleteProgram(); std::string defines; GetDefines(defines); std::string vsVersion, vsMain; GetVersionInfo(vsData, vsVersion, vsMain); vsVersion += "#define VERTEX_SHADER\n"; // compile vertex shader unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); if (vertexShader != 0) { const char *sources[3] = { vsVersion.c_str(), defines.c_str(), vsMain.c_str() }; const int counts[3] = { (int)vsVersion.size(), (int)defines.size(), (int)vsMain.size() }; glShaderSource(vertexShader, 3, sources, counts); glCompileShader(vertexShader); GLint status; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status); if (status != GL_TRUE) { vertexShader = 0; FURYE << m_Name << "'s vertex shader compile failed!"; return false; } } else { FURYE << "Failed to create vertex shader context!"; return false; } std::string fsVersion, fsMain; GetVersionInfo(fsData, fsVersion, fsMain); fsVersion += "#define FRAGMENT_SHADER\n"; // compile fragment shader unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); if (fragmentShader != 0) { const char *sources[3] = { fsVersion.c_str(), defines.c_str(), fsMain.c_str() }; const int counts[3] = { (int)fsVersion.size(), (int)defines.size(), (int)fsMain.size() }; glShaderSource(fragmentShader, 3, sources, counts); glCompileShader(fragmentShader); GLint status; glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status); if (status != GL_TRUE) { glDeleteShader(vertexShader); vertexShader = fragmentShader = 0; FURYE << m_Name << "'s fragment compile failed!"; return false; } } else { FURYE << "Failed to create fragment shader context!"; return false; } // link to program m_Program = glCreateProgram(); if (m_Program != 0) { glAttachShader(m_Program, vertexShader); glAttachShader(m_Program, fragmentShader); glLinkProgram(m_Program); glDetachShader(m_Program, vertexShader); glDetachShader(m_Program, fragmentShader); glDeleteShader(vertexShader); glDeleteShader(fragmentShader); GLint status; glGetProgramiv(m_Program, GL_LINK_STATUS, &status); if (status != GL_TRUE) { glDeleteProgram(m_Program); m_Program = fragmentShader = vertexShader = 0; FURYE << m_Name << " link failed!"; return false; } } else { FURYE << "Failed to create shader program context!"; return false; } m_Dirty = false; FURYD << m_Name << " compile & link success!"; return true; }