ManaPlus
Public Member Functions
ShadersManager Class Reference

#include <shadersmanager.h>

Public Member Functions

 ShadersManager ()
 
ShadercreateShader (const unsigned int type, const std::string &fileName)
 
ShaderProgramcreateProgram (const std::string &vertex, const std::string &fragment, const bool isNewShader)
 
ShaderProgramgetSimpleProgram ()
 
ShaderProgramgetGles2Program ()
 

Detailed Description

Definition at line 34 of file shadersmanager.h.

Constructor & Destructor Documentation

◆ ShadersManager()

ShadersManager::ShadersManager ( )
inline

Definition at line 37 of file shadersmanager.h.

38  { }

Member Function Documentation

◆ createProgram()

ShaderProgram * ShadersManager::createProgram ( const std::string &  vertex,
const std::string &  fragment,
const bool  isNewShader 
)

Definition at line 71 of file shadersmanager.cpp.

74 {
75  Shader *const vertexShader = static_cast<Shader*>(
77  if (vertexShader == nullptr)
78  return nullptr;
79 
80  Shader *const fragmentShader = static_cast<Shader*>(
82 
83  if (fragmentShader == nullptr)
84  {
85  vertexShader->decRef();
86  return nullptr;
87  }
88 
89  GLuint programId = mglCreateProgram();
90  if (programId == 0U)
91  {
92  vertexShader->decRef();
93  fragmentShader->decRef();
94  return nullptr;
95  }
96 
97  mglAttachShader(programId, vertexShader->getShaderId());
98  mglAttachShader(programId, fragmentShader->getShaderId());
99  if (isNewShader)
100  mglBindFragDataLocation(programId, 0, "outColor");
101  else
102  mglBindAttribLocation(programId, 0, "position");
103  mglLinkProgram(programId);
104  GLint isLinked = 0;
105  mglGetProgramiv(programId, GL_LINK_STATUS, &isLinked);
106  if (isLinked == GL_TRUE)
107  {
108  mglValidateProgram(programId);
109  GLint isValidated = 0;
110  mglGetProgramiv(programId, GL_VALIDATE_STATUS, &isValidated);
111  if (isValidated == GL_TRUE)
112  return new ShaderProgram(programId, vertexShader, fragmentShader);
113  mglDeleteProgram(programId);
114  return nullptr;
115  }
116 
117  GLint len = 0;
118  mglGetProgramiv(programId, GL_INFO_LOG_LENGTH, &len);
119  char *buf = new char[CAST_SIZE(len) + 1];
120  mglGetProgramInfoLog(programId, len, &len, buf);
121  buf[len] = 0;
122  logger->log("Program '%s, %s' compilation error: %s",
123  vertexShader->mIdPath.c_str(),
124  fragmentShader->mIdPath.c_str(),
125  buf);
126  delete [] buf;
127  mglDeleteProgram(programId);
128  return nullptr;
129 }
#define CAST_SIZE
Definition: cast.h:34
void log(const char *const log_text,...)
Definition: logger.cpp:269
std::string mIdPath
Definition: resource.h:84
virtual void decRef()
Definition: resource.cpp:50
Definition: shader.h:30
unsigned int getShaderId() const
Definition: shader.h:38
Logger * logger
Definition: logger.cpp:89
#define GL_FRAGMENT_SHADER
Definition: mgldefines.h:95
#define GL_VALIDATE_STATUS
Definition: mgldefines.h:99
#define GL_VERTEX_SHADER
Definition: mgldefines.h:96
#define GL_LINK_STATUS
Definition: mgldefines.h:98
#define GL_INFO_LOG_LENGTH
Definition: mgldefines.h:100
Resource * getShader(const unsigned int type, const std::string &name)

References CAST_SIZE, Resource::decRef(), Loader::getShader(), Shader::getShaderId(), GL_FRAGMENT_SHADER, GL_INFO_LOG_LENGTH, GL_LINK_STATUS, GL_VALIDATE_STATUS, GL_VERTEX_SHADER, Logger::log(), logger, and Resource::mIdPath.

Referenced by getGles2Program(), getSimpleProgram(), and anonymous_namespace{shaderprogramloader.cpp}::ShaderProgramLoader::load().

◆ createShader()

Shader * ShadersManager::createShader ( const unsigned int  type,
const std::string &  fileName 
)

Definition at line 47 of file shadersmanager.cpp.

49 {
50  const std::string str = VirtFs::loadTextFileString(fileName);
51  const char *ptrStr = str.c_str();
52  GLuint shaderId = mglCreateShader(type);
53  mglShaderSource(shaderId, 1, &ptrStr, nullptr);
54  mglCompileShader(shaderId);
55 
56  GLint isCompiled = 0;
57  mglGetShaderiv(shaderId, GL_COMPILE_STATUS, &isCompiled);
58  if (isCompiled == GL_TRUE)
59  return new Shader(shaderId);
60  GLint len = 0;
61  mglGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &len);
62  char *buf = new char[CAST_SIZE(len) + 1];
63  mglGetShaderInfoLog(shaderId, len, &len, buf);
64  buf[len] = 0;
65  logger->log("Shader '%s' compilation error: %s", fileName.c_str(), buf);
66  delete [] buf;
67  mglDeleteShader(shaderId);
68  return nullptr;
69 }
#define GL_COMPILE_STATUS
Definition: mgldefines.h:97
std::string loadTextFileString(const std::string &fileName)
Definition: tools.cpp:122
std::string fileName
Definition: testmain.cpp:39

References CAST_SIZE, fileName, GL_COMPILE_STATUS, GL_INFO_LOG_LENGTH, VirtFs::loadTextFileString(), Logger::log(), and logger.

Referenced by anonymous_namespace{shaderloader.cpp}::ShaderLoader::load().

◆ getGles2Program()

ShaderProgram * ShadersManager::getGles2Program ( )

Definition at line 139 of file shadersmanager.cpp.

140 {
141  const std::string dir = paths.getStringValue("shaders");
142  return createProgram(dir + paths.getStringValue("gles2VertexShader"),
143  dir + paths.getStringValue("gles2FragmentShader"),
144  false);
145 }
std::string getStringValue(const std::string &key) const
ShaderProgram * createProgram(const std::string &vertex, const std::string &fragment, const bool isNewShader)
Configuration paths

References createProgram(), Configuration::getStringValue(), and paths.

Referenced by MobileOpenGL2Graphics::postInit().

◆ getSimpleProgram()

ShaderProgram * ShadersManager::getSimpleProgram ( )

Definition at line 131 of file shadersmanager.cpp.

132 {
133  const std::string dir = paths.getStringValue("shaders");
134  return createProgram(dir + paths.getStringValue("simpleVertexShader"),
135  dir + paths.getStringValue("simpleFragmentShader"),
136  true);
137 }

References createProgram(), Configuration::getStringValue(), and paths.

Referenced by ModernOpenGLGraphics::postInit().


The documentation for this class was generated from the following files: