diff options
Diffstat (limited to 'src/libjin/Graphics/Shader/je_shader.cpp')
-rw-r--r-- | src/libjin/Graphics/Shader/je_shader.cpp | 278 |
1 files changed, 0 insertions, 278 deletions
diff --git a/src/libjin/Graphics/Shader/je_shader.cpp b/src/libjin/Graphics/Shader/je_shader.cpp deleted file mode 100644 index b0e7506..0000000 --- a/src/libjin/Graphics/Shader/je_shader.cpp +++ /dev/null @@ -1,278 +0,0 @@ -#include "../../core/je_configuration.h" -#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader) - -#include <iostream> - -#include "../../filesystem/je_buffer.h" -#include "../../utils/je_macros.h" - -#include "je_jsl_compiler.h" -#include "je_shader.h" - -namespace JinEngine -{ - namespace Graphics - { - - using namespace std; - using namespace JinEngine::Filesystem; - - // - // default_texture - // base_shader - // SHADER_FORMAT_SIZE - // formatShader - // - #include "je_default.shader.h" - - // - // https://stackoverflow.com/questions/27941496/use-sampler-without-passing-through-value - // The default value of a sampler variable is 0. From the GLSL 3.30 spec, - // section "4.3.5 Uniforms": - // - // The link time initial value is either the value of the variable's - // initializer, if present, or 0 if no initializer is present.Sampler - // types cannot have initializers. - // - // Since a value of 0 means that it's sampling from texture unit 0, it will - // work without ever setting the value as long as you bind your textures to - // unit 0. This is well defined behavior. - // - // Since texture unit 0 is also the default until you call glActiveTexture() - // with a value other than GL_TEXTURE0, it's very common to always use unit - // 0 as long as shaders do not need more than one texture.Which means that - // often times, setting the sampler uniforms is redundant for simple - // applications. - // - // I would still prefer to always set the values.If nothing else, it makes - // it clear to anybody reading your code that you really mean to sample from - // texture unit 0, and did not just forget to set the value. - // - const int DEFAULT_TEXTURE_UNIT = 0; - - /*static*/ Shader* Shader::CurrentShader = nullptr; - - Shader* Shader::createShader(const string& program) - { - Shader* shader = nullptr; - try - { - shader = new Shader(program); - } - catch(...) - { - return nullptr; - } - return shader; - } - - Shader::Shader(const string& program) - : mCurrentTextureUnit(DEFAULT_TEXTURE_UNIT) - { - if (!compile(program)) - throw 0; - } - - Shader::~Shader() - { - if (CurrentShader == this) - unuse(); - // delete shader program - glDeleteShader(mPID); - } - - bool Shader::compile(const string& program) - { - string vertex_shader, fragment_shader; - // Compile JSL shader source into GLSL shader source. - JSLCompiler* compiler = JSLCompiler::get(); - if (!compiler->compile(program, &vertex_shader, &fragment_shader)) - { - return false; - } -#define glsl(SHADER_MODE, SHADER, SRC) \ -do{ \ -const GLchar* src = SRC.c_str(); \ -glShaderSource(SHADER, 1, &src, NULL); \ -glCompileShader(SHADER); \ -GLint success; \ -glGetShaderiv(SHADER, GL_COMPILE_STATUS, &success); \ -if (success == GL_FALSE) \ - return false; \ -}while(0) - // Compile vertex shader. - GLuint vid = glCreateShader(GL_VERTEX_SHADER); - glsl(GL_VERTEX_SHADER, vid, vertex_shader); - // Compile fragment shader. - GLuint fid = glCreateShader(GL_FRAGMENT_SHADER); - glsl(GL_FRAGMENT_SHADER, fid, fragment_shader); -#undef glsl - // Create OpenGL shader program. - mPID = glCreateProgram(); - glAttachShader(mPID, vid); - glAttachShader(mPID, fid); - glLinkProgram(mPID); - GLint success; - glGetProgramiv(mPID, GL_LINK_STATUS, &success); - if (success == GL_FALSE) - return false; - } - - static inline GLint getMaxTextureUnits() - { - GLint maxTextureUnits = 0; - glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits); - return maxTextureUnits; - } - - void Shader::use() - { - glUseProgram(mPID); - CurrentShader = this; - sendInt(SHADER_MAIN_TEXTURE, DEFAULT_TEXTURE_UNIT); - } - - /*static*/ void Shader::unuse() - { - glUseProgram(0); - CurrentShader = nullptr; - } - - GLint Shader::claimTextureUnit(const std::string& name) - { - std::map<std::string, GLint>::iterator unit = mTextureUnits.find(name); - if (unit != mTextureUnits.end()) - return unit->second; - static GLint MAX_TEXTURE_UNITS = getMaxTextureUnits(); - if (++mCurrentTextureUnit >= MAX_TEXTURE_UNITS) - return 0; - mTextureUnits[name] = mCurrentTextureUnit; - return mCurrentTextureUnit; - } - - #define checkJSL() \ - if (CurrentShader != this) \ - return - - void Shader::sendInt(const char* name, int value) - { - checkJSL(); - int loc = glGetUniformLocation(mPID, name); - glUniform1i(loc, value); - } - - void Shader::sendFloat(const char* variable, float number) - { - checkJSL(); - int loc = glGetUniformLocation(mPID, variable); - glUniform1f(loc, number); - } - - // - // https://www.douban.com/note/627332677/ - // struct TextureUnit - // { - // GLuint targetTexture1D; - // GLuint targetTexture2D; - // GLuint targetTexture3D; - // GLuint targetTextureCube; - // ... - // }; - // - // TextureUnit mTextureUnits[GL_MAX_TEXTURE_IMAGE_UNITS] - // GLuint mCurrentTextureUnit = 0; - // - void Shader::sendTexture(const char* variable, const Texture* tex) - { - checkJSL(); - GLint location = glGetUniformLocation(mPID, variable); - if (location == -1) - return; - GLint unit = claimTextureUnit(variable); - if (unit == 0) - { - // TODO: 쳣 - return; - } - gl.activeTexUnit(unit); - glUniform1i(location, unit); - gl.bindTexture(tex->getTexture()); - gl.activeTexUnit(0); - } - - void Shader::sendCanvas(const char* variable, const Canvas* canvas) - { - checkJSL(); - GLint location = glGetUniformLocation(mPID, variable); - if (location == -1) - return; - GLint unit = claimTextureUnit(variable); - if (unit == 0) - { - // TODO: 쳣 - return; - } - glUniform1i(location, unit); - glActiveTexture(GL_TEXTURE0 + unit); - gl.bindTexture(canvas->getTexture()); - - glActiveTexture(GL_TEXTURE0); - } - - void Shader::sendVec2(const char* name, float x, float y) - { - checkJSL(); - int loc = glGetUniformLocation(mPID, name); - glUniform2f(loc, x, y); - } - - void Shader::sendVec3(const char* name, float x, float y, float z) - { - checkJSL(); - int loc = glGetUniformLocation(mPID, name); - glUniform3f(loc, x, y, z); - } - - void Shader::sendVec4(const char* name, float x, float y, float z, float w) - { - checkJSL(); - int loc = glGetUniformLocation(mPID, name); - glUniform4f(loc, x, y, z, w); - } - - void Shader::sendColor(const char* name, const Color* col) - { - checkJSL(); - int loc = glGetUniformLocation(mPID, name); - glUniform4f(loc, - col->r / 255.f, - col->g / 255.f, - col->b / 255.f, - col->a / 255.f - ); - } - - void Shader::sendMatrix4(const char* name, const Math::Matrix* mat4) - { - int loc = glGetUniformLocation(mPID, name); - glUniformMatrix4fv(loc, 1, GL_FALSE, mat4->getElements()); - } - - void Shader::bindVertexPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers) - { - GLint loc = glGetAttribLocation(mPID, SHADER_VERTEX_COORDS); - glEnableVertexAttribArray(0); - glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers); - } - - void Shader::bindUVPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers) - { - GLint loc = glGetAttribLocation(mPID, SHADER_TEXTURE_COORDS); - glEnableVertexAttribArray(1); - glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers); - } - - } // namespace Graphics -} // namespace JinEngine - -#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader)
\ No newline at end of file |