bool C4Shader::Init(const char *szWhat, const char **szUniforms) { #ifndef USE_CONSOLE // No support? if(!GLEW_ARB_fragment_program) { Log(" gl: no shader support!"); return false; } // Clear old shader first if (hProg) Clear(); #endif StdStrBuf VertexShader = Build(VertexSlices, true), FragmentShader = Build(FragmentSlices, true); // Dump if (C4Shader::IsLogging()) { ShaderLogF("******** Vertex shader for %s:", szWhat); ShaderLog(VertexShader.getData()); ShaderLogF("******** Fragment shader for %s:", szWhat); ShaderLog(FragmentShader.getData()); } #ifndef USE_CONSOLE // Attempt to create shaders hVert = Create(GL_VERTEX_SHADER_ARB, FormatString("%s vertex shader", szWhat).getData(), VertexShader.getData()); hFrag = Create(GL_FRAGMENT_SHADER_ARB, FormatString("%s fragment shader", szWhat).getData(), FragmentShader.getData()); if(!hFrag || !hVert) return false; // Link program hProg = glCreateProgramObjectARB(); #ifdef GL_KHR_debug if (glObjectLabel) glObjectLabel(GL_PROGRAM, hProg, -1, szWhat); #endif glAttachObjectARB(hProg, hVert); glAttachObjectARB(hProg, hFrag); // Bind all input variables for (int i = 0; i <= VAI_BoneWeightsMax - VAI_BoneWeights; ++i) { glBindAttribLocation(hProg, VAI_BoneWeights + i, FormatString("oc_BoneWeights%d", i).getData()); glBindAttribLocation(hProg, VAI_BoneIndices + i, FormatString("oc_BoneIndices%d", i).getData()); } glLinkProgramARB(hProg); // Link successful? DumpInfoLog(FormatString("%s shader program", szWhat).getData(), hProg); if(GetObjectStatus(hProg, GL_OBJECT_LINK_STATUS_ARB) != 1) { Clear(); ShaderLogF(" gl: Failed to link %s shader!", szWhat); return false; } ShaderLogF(" gl: %s shader linked successfully", szWhat); // Okay, allocate uniform array iUniformCount = 0; while (szUniforms[iUniformCount]) iUniformCount++; pUniforms = new GLint[iUniformCount]; // Get uniform locations. Note this is expected to fail for a few of them // because the respective uniforms got optimized out! for (int i = 0; i < iUniformCount; i++) pUniforms[i] = glGetUniformLocationARB(hProg, szUniforms[i]); #endif return true; }
void moShaderGLSL::CreateVertShader(moText vert_source) { m_ProgramObject = glCreateProgramObjectARB(); compileVertShader(vert_source); linkProgram(); }
ShaderProgram::ShaderProgram() { mID = glCreateProgramObjectARB(); }
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_ARBShaderObjects_nglCreateProgramObjectARB(JNIEnv *env, jclass clazz, jlong function_pointer) { glCreateProgramObjectARBPROC glCreateProgramObjectARB = (glCreateProgramObjectARBPROC)((intptr_t)function_pointer); GLhandleARB __result = glCreateProgramObjectARB(); return __result; }
implementation(const std::string& vertex_source_str, const std::string& fragment_source_str) : program_(0) { GLint success; const char* vertex_source = vertex_source_str.c_str(); auto vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); GL(glShaderSourceARB(vertex_shader, 1, &vertex_source, NULL)); GL(glCompileShaderARB(vertex_shader)); GL(glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success)); if (success == GL_FALSE) { char info[2048]; GL(glGetInfoLogARB(vertex_shader, sizeof(info), 0, info)); GL(glDeleteObjectARB(vertex_shader)); std::stringstream str; str << "Failed to compile vertex shader:" << std::endl << info << std::endl; BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(str.str())); } const char* fragment_source = fragment_source_str.c_str(); auto fragmemt_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); GL(glShaderSourceARB(fragmemt_shader, 1, &fragment_source, NULL)); GL(glCompileShaderARB(fragmemt_shader)); GL(glGetObjectParameterivARB(fragmemt_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success)); if (success == GL_FALSE) { char info[2048]; GL(glGetInfoLogARB(fragmemt_shader, sizeof(info), 0, info)); GL(glDeleteObjectARB(fragmemt_shader)); std::stringstream str; str << "Failed to compile fragment shader:" << std::endl << info << std::endl; BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(str.str())); } program_ = glCreateProgramObjectARB(); GL(glAttachObjectARB(program_, vertex_shader)); GL(glAttachObjectARB(program_, fragmemt_shader)); GL(glLinkProgramARB(program_)); GL(glDeleteObjectARB(vertex_shader)); GL(glDeleteObjectARB(fragmemt_shader)); GL(glGetObjectParameterivARB(program_, GL_OBJECT_LINK_STATUS_ARB, &success)); if (success == GL_FALSE) { char info[2048]; GL(glGetInfoLogARB(program_, sizeof(info), 0, info)); GL(glDeleteObjectARB(program_)); std::stringstream str; str << "Failed to link shader program:" << std::endl << info << std::endl; BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(str.str())); } GL(glUseProgramObjectARB(program_)); }
/** * Loads vertex and fragment shader from disk/memory. * Loads any combination of vertex and fragment shader from disk or from a memory position. * Generates error/information messages to stdout during loading. * If nor successful the handle of the shader will be set to 0. * \param VSFile - name of the file containing the vertex shader * \param FSFile - name of the file containing the fragment shader * \param src - selects the source of vertex and fragment shader. Can be either GLSLPROGRAM_DISK or GLSLPROGRAM_STRING * \return void * \warning Uses glGetError() * \author <a href="mailto:[email protected]">Jens Schneider</a> * \date Aug.2004 * \see GLSLPROGRAM_SOURCE */ void GLSLProgram::Load(const char *VSFile, const char *FSFile, GLSLPROGRAM_SOURCE src) { CheckGLError(); // load GLuint hVS=0; GLuint hFS=0; bool bVSSuccess=true; // fixed function pipeline is always working if (VSFile!=NULL) { hVS=LoadShader(VSFile,GL_VERTEX_SHADER,src); if(hVS != 0) { m_sVS = std::string(VSFile); // record program source } else { bVSSuccess=false; if (src==GLSLPROGRAM_DISK) { T_ERROR("ERROR IN: %s", VSFile); } else { T_ERROR("---------- ERROR -----------"); int iPos=0; int iLine=1; char chLine[32]; char *chVerbose=new char[strlen(VSFile)+1]; memcpy(chVerbose,VSFile,strlen(VSFile)+1); for (unsigned int i=0; i<strlen(VSFile); i++) { if (chVerbose[i]=='\n') { chVerbose[i]='\0'; sprintf(chLine,"(%.4i) ",iLine++); T_ERROR("Load %s %s", chLine, &chVerbose[iPos]); iPos=i+1; } } delete[] chVerbose; } } } bool bFSSuccess=true; // fixed function pipeline is always working if (FSFile!=NULL) { hFS=LoadShader(FSFile,GL_FRAGMENT_SHADER,src); if(hVS != 0) { m_sFS = std::string(FSFile); // record program source } else { bFSSuccess=false; if (src==GLSLPROGRAM_DISK) { T_ERROR( "Error in fragment shader: %s", FSFile); } else { T_ERROR("---------- ERROR -----------"); int iPos=0; int iLine=1; char chLine[32]; char *chVerbose=new char[strlen(FSFile)+1]; memcpy(chVerbose,FSFile,strlen(FSFile)+1); for (unsigned int i=0; i<strlen(FSFile); i++) { if (chVerbose[i]=='\n') { chVerbose[i]='\0'; sprintf(chLine,"(%.4i) ",iLine++); T_ERROR( "Load %s %s",chLine, &chVerbose[iPos]); iPos=i+1; } } delete[] chVerbose; } } } if (m_bGLUseARB) { // attach to shader program m_hProgram=glCreateProgramObjectARB(); if (hVS) glAttachObjectARB(m_hProgram,hVS); if (hFS) glAttachObjectARB(m_hProgram,hFS); // link the program together if (bVSSuccess && bFSSuccess) { glLinkProgramARB(m_hProgram); // check for errors GLint iLinked; glGetObjectParameterivARB(m_hProgram,GL_OBJECT_LINK_STATUS_ARB,&iLinked); WriteError(m_hProgram); // delete temporary objects if (hVS) glDeleteObjectARB(hVS); if (hFS) glDeleteObjectARB(hFS); if (CheckGLError("Load()") || !iLinked) { glDeleteObjectARB(m_hProgram); m_bInitialized=false; return; } else { m_bInitialized=true; } } else { if (hVS) glDeleteObjectARB(hVS); if (hFS) glDeleteObjectARB(hFS); glDeleteObjectARB(m_hProgram); m_hProgram=0; m_bInitialized=false; if (!bVSSuccess && !bFSSuccess) T_ERROR("Error in vertex and fragment shaders"); else if (!bVSSuccess) T_ERROR("Error in vertex shader"); else if (!bFSSuccess) T_ERROR("Error in fragment shader"); } } else { // attach to program object m_hProgram=glCreateProgram(); if (hVS) glAttachShader(m_hProgram,hVS); if (hFS) glAttachShader(m_hProgram,hFS); // link the program together if (bVSSuccess && bFSSuccess) { glLinkProgram(m_hProgram); // check for errors GLint iLinked; glGetProgramiv(m_hProgram,GL_LINK_STATUS,&iLinked); std::string fileDesc = std::string("VS: ") + std::string(VSFile) + std::string(", FS:") + std::string(FSFile); WriteInfoLog(fileDesc.c_str(), m_hProgram, true); // flag shaders such that they can be deleted when they get detached if (hVS) glDeleteShader(hVS); if (hFS) glDeleteShader(hFS); if (CheckGLError("Load()") || iLinked!=GLint(GL_TRUE)) { glDeleteProgram(m_hProgram); m_hProgram=0; m_bInitialized=false; return; } else { m_bInitialized=true; } } else { if (hVS) glDeleteShader(hVS); if (hFS) glDeleteShader(hFS); glDeleteProgram(m_hProgram); m_hProgram=0; m_bInitialized=false; if (!bVSSuccess && !bFSSuccess) T_ERROR("Error in vertex and fragment shaders"); else if (!bVSSuccess) T_ERROR("Error in vertex shader"); else if (!bFSSuccess) T_ERROR("Error in fragment shader"); } } }
bool Shader::compile(const char *vertexShaderCode, const char *fragmentShaderCode) { ensureGLContext(); if (!isAvailable()) { std::cerr << "Failed to create a shader. Your system doesn't support shaders. " << "You should use Shader::isAvailable() befor trying to use shaders.\n"; return false; } if (mShaderProgram) { glCheck(glDeleteObjectARB(mShaderProgram)); } mCurrentTexture = -1; mTextures.clear(); mParams.clear(); mShaderProgram = glCheck(glCreateProgramObjectARB()); if (vertexShaderCode) { GLhandleARB vertexShader = glCheck(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)); glCheck(glShaderSourceARB(vertexShader, 1, &vertexShaderCode, null)); glCheck(glCompileShaderARB(vertexShader)); GLint success; glCheck(glGetObjectParameterivARB(vertexShader, GL_OBJECT_COMPILE_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; glCheck(glGetInfoLogARB(vertexShader, sizeof(log), 0, log)); std::cerr << "Failed to compile vertex shader. " << log << "\n"; glCheck(glDeleteObjectARB(mShaderProgram)); glCheck(glDeleteObjectARB(vertexShader)); mShaderProgram = 0; return false; } glCheck(glAttachObjectARB(mShaderProgram, vertexShader)); glCheck(glDeleteObjectARB(vertexShader)); } if (fragmentShaderCode) { GLhandleARB fragmentShader = glCheck(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB)); glCheck(glShaderSourceARB(fragmentShader, 1, &fragmentShaderCode, null)); glCheck(glCompileShaderARB(fragmentShader)); GLint success; glCheck(glGetObjectParameterivARB(fragmentShader, GL_OBJECT_COMPILE_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; glCheck(glGetInfoLogARB(fragmentShader, sizeof(log), 0, log)); std::cerr << "Failed to compile vertex shader. " << log << "\n"; glCheck(glDeleteObjectARB(mShaderProgram)); glCheck(glDeleteObjectARB(fragmentShader)); mShaderProgram = 0; return false; } glCheck(glAttachObjectARB(mShaderProgram, fragmentShader)); glCheck(glDeleteObjectARB(fragmentShader)); } glCheck(glLinkProgramARB(mShaderProgram)); GLint success; glCheck(glGetObjectParameterivARB(mShaderProgram, GL_OBJECT_LINK_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; glCheck(glGetInfoLogARB(mShaderProgram, sizeof(log), 0, log)); std::cerr << "Failed to link shader. " << log << "\n"; glCheck(glDeleteObjectARB(mShaderProgram)); mShaderProgram = 0; return false; } glCheck(glFlush()); return true; }
ShaderProgram::ShaderProgram() { _linked = false; _id = glCreateProgramObjectARB(); }
GPUShader *GPU_shader_create(const char *vertexcode, const char *fragcode, /*GPUShader *lib,*/ const char *libcode) { GLint status; GLcharARB log[5000]; const char *fragsource[2]; GLsizei length = 0; GLint count; GPUShader *shader; if (!GLEW_ARB_vertex_shader || !GLEW_ARB_fragment_shader) return NULL; shader = MEM_callocN(sizeof(GPUShader), "GPUShader"); if(vertexcode) shader->vertex = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); if(fragcode) shader->fragment = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); shader->object = glCreateProgramObjectARB(); if (!shader->object || (vertexcode && !shader->vertex) || (fragcode && !shader->fragment)) { fprintf(stderr, "GPUShader, object creation failed.\n"); GPU_shader_free(shader); return NULL; } if(vertexcode) { glAttachObjectARB(shader->object, shader->vertex); glShaderSourceARB(shader->vertex, 1, (const char**)&vertexcode, NULL); glCompileShaderARB(shader->vertex); glGetObjectParameterivARB(shader->vertex, GL_OBJECT_COMPILE_STATUS_ARB, &status); if (!status) { glGetInfoLogARB(shader->vertex, sizeof(log), &length, log); shader_print_errors("compile", log, vertexcode); GPU_shader_free(shader); return NULL; } } if(fragcode) { count = 0; if(libcode) fragsource[count++] = libcode; if(fragcode) fragsource[count++] = fragcode; glAttachObjectARB(shader->object, shader->fragment); glShaderSourceARB(shader->fragment, count, fragsource, NULL); glCompileShaderARB(shader->fragment); glGetObjectParameterivARB(shader->fragment, GL_OBJECT_COMPILE_STATUS_ARB, &status); if (!status) { glGetInfoLogARB(shader->fragment, sizeof(log), &length, log); shader_print_errors("compile", log, fragcode); GPU_shader_free(shader); return NULL; } } /*if(lib && lib->lib) glAttachObjectARB(shader->object, lib->lib);*/ glLinkProgramARB(shader->object); glGetObjectParameterivARB(shader->object, GL_OBJECT_LINK_STATUS_ARB, &status); if (!status) { glGetInfoLogARB(shader->object, sizeof(log), &length, log); if (fragcode) shader_print_errors("linking", log, fragcode); else if (vertexcode) shader_print_errors("linking", log, vertexcode); else if (libcode) shader_print_errors("linking", log, libcode); GPU_shader_free(shader); return NULL; } return shader; }
GLhandleARB Shader::loadShader(const std::string& filestr) { // std::string filestr(filename); bool vertexshader = false; bool fragmentshader = false; bool geometryshader = false; if(filestr.find(".vert") != std::string::npos){ vertexshader=true; }else{ if(filestr.find(".frag") != std::string::npos){ fragmentshader=true; } #ifdef USE_GEOMETRY_SHADER else{ if(filestr.find(".geo") != std::string::npos){ geometryshader=true; } #endif } if(!vertexshader && !fragmentshader && !geometryshader ) return 0; std::ifstream file(filestr.c_str()); if(!file) return 0; std::stringstream buffer; buffer << file.rdbuf(); file.close(); std::string s = buffer.str(); GLcharARB * source = new GLcharARB[4*(buffer.str().size()/4+1)]; if(source == 0) return 0; unsigned int i; for(i =0; i < buffer.str().size(); ++i){ source[i] = s[i]; } source[i] = '\0'; GLhandleARB so = 0; if(vertexshader) so = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); if(fragmentshader) so = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); #ifdef USE_GEOMETRY_SHADER if(geometryshader) so = glCreateShaderObjectARB(GL_GEOMETRY_SHADER_ARB); #endif glShaderSourceARB(so, 1,(const GLcharARB**) &source, 0); return so; } bool Shader::compileShader(GLhandleARB object) { if(object==0) return false; glCompileShaderARB(object); int status; glGetObjectParameterivARB(object, GL_OBJECT_COMPILE_STATUS_ARB, &status); if(status==0){ int length = 0; glGetObjectParameterivARB(object, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length); if(length>0){ GLsizei minlength = min(MAX_LOG_STRING,length); glGetInfoLogARB(object, minlength, 0, logstring); std::cerr << logstring << std::endl; } return false; } return true; } GLhandleARB Shader::linkShaders(GLhandleARB* object, const unsigned int& nb) { if(object==0) return 0; GLhandleARB po = 0; po = glCreateProgramObjectARB(); for(unsigned int i = 0; i < nb; ++i){ if(object[i]>0) glAttachObjectARB(po,object[i]); } glLinkProgramARB(po); int status; glGetObjectParameterivARB(po, GL_OBJECT_LINK_STATUS_ARB, &status); if(status==0){ int length = 0; glGetObjectParameterivARB(po, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length); if(length>0){ GLsizei minlength = min(MAX_LOG_STRING,length); glGetInfoLogARB(po, minlength, 0, logstring); std::cerr << logstring << std::endl; } return 0; } return po; } /* void Shader::PrintSupportedExtensions() { std::stringstream extensions = std::stringstream((char*)(glGetString(GL_EXTENSIONS))); std::cout << "Supported extensions :" << std::endl; while(!extensions.eof()) { std::string str("end"); extensions >> str; std::cout << "- " << str << std::endl; } } bool Shader::isExtensionSupported(const std::string& ext) { std::string extensions = std::string((char*)(glGetString(GL_EXTENSIONS))); if(extensions.find(ext) != std::string::npos) return true; std::cerr << "WARNING : Extension \"" << ext << "\" is not supported" << std::endl; return false; } */ bool Shader::Init() { s_bInitialized = true; s_pCurrent = NULL; if(! glInfo::GetSingleton().isExtensionSupported("GL_ARB_shading_language_100") ) s_bInitialized = false; return s_bInitialized; }
ShaderObject::ShaderObject() { Handle = glCreateProgramObjectARB(); Compiled = false; }
char GLSLBase::initializeShaders(std::string& log) { log = ""; GLint vertex_compiled, fragment_compiled; GLint linked; // Delete any existing program object if (program_object) { glDeleteObjectARB(program_object); program_object = NULL; } vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); const char* a = vertexProgramSource(); glShaderSourceARB(vertex_shader, 1, &a, NULL); glCompileShaderARB(vertex_shader); glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &vertex_compiled); fragment_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); const char* b = fragmentProgramSource(); glShaderSourceARB(fragment_shader, 1, &b, NULL); glCompileShaderARB(fragment_shader); glGetObjectParameterivARB(fragment_shader, GL_OBJECT_COMPILE_STATUS_ARB, &fragment_compiled); if (!vertex_compiled || !fragment_compiled) { if (vertex_shader) { glDeleteObjectARB(vertex_shader); vertex_shader = NULL; log += "\n Glsl error vertex shader not compiled \n"; } if (fragment_shader) { glDeleteObjectARB(fragment_shader); fragment_shader = NULL; log += "\n Glsl error fragment shader not compiled \n"; } return 0; } program_object = glCreateProgramObjectARB(); if (vertex_shader != NULL) { glAttachObjectARB(program_object, vertex_shader); glDeleteObjectARB(vertex_shader); } if (fragment_shader != NULL) { glAttachObjectARB(program_object, fragment_shader); glDeleteObjectARB(fragment_shader); } glLinkProgramARB(program_object); glGetObjectParameterivARB(program_object, GL_OBJECT_LINK_STATUS_ARB, &linked); if (!linked) { glDeleteObjectARB(program_object); program_object = NULL; log += "\n Glsl error shaders not linked"; return 0; } defaultShaderParameters(); log += "\n Glsl shaders compiled"; m_hasShaders = 1; return 1; }
bool Shader::compile(const char* vertexShaderCode, const char* fragmentShaderCode) { ensureGlContext(); // First make sure that we can use shaders if (!isAvailable()) { err() << "Failed to create a shader: your system doesn't support shaders " << "(you should test Shader::isAvailable() before trying to use the Shader class)" << std::endl; return false; } // Destroy the shader if it was already created if (m_shaderProgram) { glCheck(glDeleteObjectARB(m_shaderProgram)); } // Reset the internal state m_currentTexture = -1; m_textures.clear(); m_params.clear(); // Create the program m_shaderProgram = glCreateProgramObjectARB(); // Create the vertex shader if needed if (vertexShaderCode) { // Create and compile the shader GLhandleARB vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); glCheck(glShaderSourceARB(vertexShader, 1, &vertexShaderCode, NULL)); glCheck(glCompileShaderARB(vertexShader)); // Check the compile log GLint success; glCheck(glGetObjectParameterivARB(vertexShader, GL_OBJECT_COMPILE_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; glCheck(glGetInfoLogARB(vertexShader, sizeof(log), 0, log)); err() << "Failed to compile vertex shader:" << std::endl << log << std::endl; glCheck(glDeleteObjectARB(vertexShader)); glCheck(glDeleteObjectARB(m_shaderProgram)); m_shaderProgram = 0; return false; } glCheck(glAttachObjectARB(m_shaderProgram, vertexShader)); glCheck(glDeleteObjectARB(vertexShader)); } // Create the fragment shader if needed if (fragmentShaderCode) { // Create and compile the shader GLhandleARB fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); glCheck(glShaderSourceARB(fragmentShader, 1, &fragmentShaderCode, NULL)); glCheck(glCompileShaderARB(fragmentShader)); // Check the compile log GLint success; glCheck(glGetObjectParameterivARB(fragmentShader, GL_OBJECT_COMPILE_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; glCheck(glGetInfoLogARB(fragmentShader, sizeof(log), 0, log)); err() << "Failed to compile fragment shader:" << std::endl << log << std::endl; glCheck(glDeleteObjectARB(fragmentShader)); glCheck(glDeleteObjectARB(m_shaderProgram)); m_shaderProgram = 0; return false; } glCheck(glAttachObjectARB(m_shaderProgram, fragmentShader)); glCheck(glDeleteObjectARB(fragmentShader)); } // Link the program glCheck(glLinkProgramARB(m_shaderProgram)); // Check the link log GLint success; glCheck(glGetObjectParameterivARB(m_shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; glCheck(glGetInfoLogARB(m_shaderProgram, sizeof(log), 0, log)); err() << "Failed to link shader:" << std::endl << log << std::endl; glCheck(glDeleteObjectARB(m_shaderProgram)); m_shaderProgram = 0; return false; } // Force an OpenGL flush, so that the shader will appear updated // in all contexts immediately (solves problems in multi-threaded apps) glCheck(glFlush()); return true; }
shade::GLSLARBWrapper::Handle shade::GLSLARBWrapper::CreateProgram(void) { return glCreateProgramObjectARB(); }
void InitScene (void) { GLint params[1]; const char *tab[2]; /* * GL should silently ignore calls that delete object 0. */ begintest (TT_GETERROR_NOERROR, "glDeleteObject(0)"); glDeleteObjectARB (0); endtest (); /* * GL generates an error on invalid object handle. */ begintest (TT_GETERROR_INVALIDVALUE, "Pass invalid non-zero object handle"); glDeleteObjectARB (find_invalid_handle ()); endtest (); glUseProgramObjectARB (find_invalid_handle ()); endtest (); /* * Create object. GL should return unique non-zero values. */ begintest (TT_PARAM1_NONZERO, "Create object"); vert = glCreateShaderObjectARB (GL_VERTEX_SHADER_ARB); endtest1 (vert); frag = glCreateShaderObjectARB (GL_FRAGMENT_SHADER_ARB); endtest1 (frag); prog = glCreateProgramObjectARB (); endtest1 (prog); endtest1 (vert != frag && frag != prog && prog != vert); /* * Link empty program. */ begintest (TT_PARAM1_NONZERO, "Link empty program"); glLinkProgramARB (prog); endtest1 (CheckObjectStatus (prog)); /* * Use empty program object. Empty program objects are valid. */ begintest (TT_GETERROR_NOERROR, "Use empty program object"); glUseProgramObjectARB (prog); endtest (); /* * Attach invalid object handles. Program object 0 should not be accepted. */ begintest (TT_GETERROR_INVALIDVALUE, "Attach invalid object handle"); glAttachObjectARB (0, find_invalid_handle ()); endtest (); glAttachObjectARB (0, frag); endtest (); glAttachObjectARB (find_invalid_handle (), find_invalid_handle ()); endtest (); glAttachObjectARB (find_invalid_handle (), frag); endtest (); glAttachObjectARB (prog, find_invalid_handle ()); endtest (); /* * Attach valid object handles with wrong semantics. */ begintest (TT_GETERROR_INVALIDOPERATION, "Attach object badly"); glAttachObjectARB (vert, frag); endtest (); glAttachObjectARB (vert, prog); endtest (); glAttachObjectARB (prog, prog); endtest (); /* * Detach non-attached object. */ begintest (TT_GETERROR_INVALIDOPERATION, "Detach non-attached object"); glDetachObjectARB (prog, vert); endtest (); glDetachObjectARB (prog, frag); endtest (); /* * Attach shader. */ begintest (TT_GETERROR_NOERROR, "Attach shader to program object"); glAttachObjectARB (prog, vert); endtest (); glAttachObjectARB (prog, frag); endtest (); /* * Attach object twice. */ begintest (TT_GETERROR_INVALIDOPERATION, "Attach object twice"); glAttachObjectARB (prog, vert); endtest (); glAttachObjectARB (prog, frag); endtest (); /* * Detach attached object. */ begintest (TT_GETERROR_NOERROR, "Detach attached object"); glDetachObjectARB (prog, vert); endtest (); glDetachObjectARB (prog, frag); endtest (); /* * Attach shader again. */ begintest (TT_GETERROR_NOERROR, "Attach shader again"); glAttachObjectARB (prog, vert); endtest (); glAttachObjectARB (prog, frag); endtest (); /* * Delete attached object. */ begintest (TT_GETERROR_NOERROR, "Delete attached object"); glDeleteObjectARB (vert); endtest (); glDeleteObjectARB (frag); endtest (); /* * Query delete status. It should return TRUE. Object handles are still valid * as they are referenced by program object container. */ begintest (TT_PARAM1_NONZERO, "Query delete status"); glGetObjectParameterivARB (vert, GL_OBJECT_DELETE_STATUS_ARB, params); endtest1 (params[0]); glGetObjectParameterivARB (frag, GL_OBJECT_DELETE_STATUS_ARB, params); endtest1 (params[0]); /* * Delete already deleted attached object. The behaviour is undefined, but we * check for no errors. The object still exists, so the handle value is okay. * In other words, these calls should be silently ignored by GL. */ begintest (TT_GETERROR_NOERROR, "Delete already deleted attached object"); glDeleteObjectARB (vert); endtest (); glDeleteObjectARB (frag); endtest (); /* * Compile shader source with syntax error. */ begintest (TT_PARAM1_ZERO, "Compile shader source with syntax error"); glShaderSourceARB (vert, 1, &invsynvertsrc, NULL); glCompileShaderARB (vert); endtest1 (CheckObjectStatus (vert)); glShaderSourceARB (frag, 1, &invsynfragsrc, NULL); glCompileShaderARB (frag); endtest1 (CheckObjectStatus (frag)); /* * Compile shader source with semantic error. */ begintest (TT_PARAM1_ZERO, "Compile shader source with semantic error"); glShaderSourceARB (vert, 1, &invsemvertsrc, NULL); glCompileShaderARB (vert); endtest1 (CheckObjectStatus (vert)); glShaderSourceARB (frag, 1, &invsemfragsrc, NULL); glCompileShaderARB (frag); endtest1 (CheckObjectStatus (frag)); /* * Link ill-formed vertex-fragment program. */ begintest (TT_PARAM1_ZERO, "Link ill-formed vertex-fragment program"); glLinkProgramARB (prog); endtest1 (CheckObjectStatus (prog)); /* * Use badly linked program object. */ begintest (TT_GETERROR_INVALIDOPERATION, "Use badly linked program object"); glUseProgramObjectARB (prog); endtest (); /* * Compile well-formed shader source. Check if multi-string sources can be handled. */ begintest (TT_PARAM1_NONZERO, "Compile well-formed shader source"); tab[0] = uniforms; tab[1] = validvertsrc; glShaderSourceARB (vert, 2, tab, NULL); glCompileShaderARB (vert); endtest1 (CheckObjectStatus (vert)); tab[0] = uniforms; tab[1] = validfragsrc; glShaderSourceARB (frag, 2, tab, NULL); glCompileShaderARB (frag); endtest1 (CheckObjectStatus (frag)); /* * Link vertex-fragment program. */ begintest (TT_PARAM1_NONZERO, "Link vertex-fragment program"); glLinkProgramARB (prog); endtest1 (CheckObjectStatus (prog)); /* * Use valid linked program object. */ begintest (TT_GETERROR_NOERROR, "Use linked program object"); glUseProgramObjectARB (prog); endtest (); /* * Get current program. */ begintest (TT_PARAM1_NONZERO, "Get current program"); endtest1 (glGetHandleARB (GL_PROGRAM_OBJECT_ARB) == prog); /* * Use 0 program object. */ begintest (TT_GETERROR_NOERROR, "Use 0 program object"); glUseProgramObjectARB (0); endtest (); /* * Query uniform location. Uniforms with gl_ prefix cannot be queried. */ begintest (TT_PARAM1_NONZERO, "Query uniform location"); endtest1 (glGetUniformLocationARB (prog, "gl_ModelViewMatrix") == -1); endtest1 (glGetUniformLocationARB (prog, "UniformThatDoesNotExist") == -1); endtest1 (glGetUniformLocationARB (prog, "") == -1); endtest1 (glGetUniformLocationARB (prog, "CommonUniform") != -1); endtest1 (glGetUniformLocationARB (prog, "VertexUniform") != -1); endtest1 (glGetUniformLocationARB (prog, "FragmentUniform") != -1); /* * Query attrib location. Attribs with gl_ prefix cannot be queried. * When gl_Vertex is used, none of the generic attribs can have index 0. */ begintest (TT_PARAM1_NONZERO, "Query attrib location"); endtest1 (glGetAttribLocationARB (prog, "gl_Vertex") == -1); endtest1 (glGetAttribLocationARB (prog, "AttribThatDoesNotExist") == -1); endtest1 (glGetAttribLocationARB (prog, "") == -1); endtest1 (glGetAttribLocationARB (prog, "FirstAttrib") > 0); endtest1 (glGetAttribLocationARB (prog, "SecondAttrib") > 0); /* * Bind attrib locations, link and check if locations are correct. */ begintest (TT_PARAM1_NONZERO, "Bind attrib location #1"); glBindAttribLocationARB (prog, 1, "FirstAttrib"); glBindAttribLocationARB (prog, 2, "SecondAttrib"); glLinkProgramARB (prog); endtest1 (CheckObjectStatus (prog)); endtest1 (glGetAttribLocationARB (prog, "FirstAttrib") == 1); endtest1 (glGetAttribLocationARB (prog, "SecondAttrib") == 2); /* * Bind attrib locations in different order. Link and check if locations are correct. */ begintest (TT_PARAM1_NONZERO, "Bind attrib location #2"); glBindAttribLocationARB (prog, 1, "SecondAttrib"); glBindAttribLocationARB (prog, 2, "FirstAttrib"); glLinkProgramARB (prog); endtest1 (CheckObjectStatus (prog)); endtest1 (glGetAttribLocationARB (prog, "SecondAttrib") == 1); endtest1 (glGetAttribLocationARB (prog, "FirstAttrib") == 2); /* * Detach deleted object. */ begintest (TT_GETERROR_NOERROR, "Detach deleted object"); glDetachObjectARB (prog, vert); endtest (); glDetachObjectARB (prog, frag); endtest (); /* * Delete deleted detached object. */ begintest (TT_GETERROR_INVALIDVALUE, "Delete deleted detached object"); glDeleteObjectARB (vert); endtest (); glDeleteObjectARB (frag); endtest (); exit (0); }
GLuint CreateProgram() { if(arb) { return glCreateProgramObjectARB(); } else { return glCreateProgram(); } }
//--------------------------------------------------------------- void ofxShader::loadShader(string fragmentName, string vertexName){ fragmentName = ofToDataPath(fragmentName); vertexName = ofToDataPath(vertexName); bLoaded = false; if (GLEE_ARB_shader_objects){ // ---------------------------------- (a) load in the shaders char *vs = NULL,*fs = NULL; vs = LoadShaderText(vertexName.c_str()); fs = LoadShaderText(fragmentName.c_str()); vertexShader = (GLhandleARB)glCreateShader(GL_VERTEX_SHADER); fragmentShader = (GLhandleARB)glCreateShader(GL_FRAGMENT_SHADER); if( vs == NULL || fs == NULL ){ assert(NULL); printf("unable to load %s \n", vertexName.c_str()); return; } GLint length = strlen(vs); glShaderSourceARB(vertexShader, 1, (const char**)&vs, &length); length = strlen(fs); glShaderSourceARB(fragmentShader, 1, (const char**)&fs, &length); char infobuffer[1000]; GLsizei infobufferlen = 0; // ----------------------------------- (b) compile the shaders // try to compile "vertex shader" glCompileShaderARB(vertexShader); //please add compile status check in: GLint compileStatus = 0; glGetObjectParameterivARB( vertexShader, GL_COMPILE_STATUS, &compileStatus ); printf("vertexShader status = %i \n", compileStatus); glGetInfoLogARB(vertexShader, 999, &infobufferlen, infobuffer); if (infobufferlen != 0){ infobuffer[infobufferlen] = 0; printf("vertexShader reports: %s \n", infobuffer); return; } // ------------------------------------ // try to compile "fragment shader" glCompileShaderARB(fragmentShader); glGetObjectParameterivARB( fragmentShader, GL_COMPILE_STATUS, &compileStatus ); printf("fragmentShader status %i \n", compileStatus); glGetInfoLogARB(fragmentShader, 999, &infobufferlen, infobuffer); if (infobufferlen != 0){ infobuffer[infobufferlen] = 0; printf("fragmentShader reports: %s \n", infobuffer); return; } // ----------------------------------- (c) link the shaders shader = glCreateProgramObjectARB(); glAttachObjectARB(shader,vertexShader); glAttachObjectARB(shader,fragmentShader); glLinkProgramARB(shader); bLoaded = true; } else { printf("sorry, it looks like you can't run 'ARB_shader_objects' \n please check the capabilites of your graphics card (http://www.ozone3d.net/gpu_caps_viewer/) \n"); } }
int initShaderWithFile(std::string vertname, std::string fragname) { GLhandleARB my_program, my_vertex_shader,my_fragment_shader; int Vcompiled,Fcompiled,FShaderLength,VShaderLength; std::string fname,vname,my_fragment_shader_source, my_vertex_shader_source,lErrors; // Create Shader And Program Objects int result = 0; Vcompiled = 0; Fcompiled = 0; fname = fragname; vname = vertname; if (!fileexists(fname)){ fname = extractfilepath(fname); vname = vertname; } if (!fileexists(vname)){ vname = extractfilepath(vname); } if ((!(fileexists(fname))) || (!(fileexists(vname)))) { std::cout << "Unable to find GLSL shaders " << fname << " " << vname << std::endl; } // char** tmpArray = new char*[1]; // tmpArray[0] = (char*)my_fragment_shader_source.c_str(); // char** tmpArray2 = new char*[1]; // tmpArray2[0] = (char*)my_vertex_shader_source.c_str(); // // loadshader((char*)fname.c_str(), (GLchar**)tmpArray); // loadshader((char*)vname.c_str(), (GLchar**)tmpArray2); //loadshader((char*)fname.c_str(), (GLchar**)my_fragment_shader_source.c_str()); //loadshader((char*)vname.c_str(), (GLchar**)my_vertex_shader_source.c_str()); my_fragment_shader_source = LoadStr( fname); my_vertex_shader_source = LoadStr( vname); my_program = glCreateProgramObjectARB(); my_vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); my_fragment_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); // Load Shader Sources FShaderLength = (int)my_fragment_shader_source.length(); VShaderLength = (int)my_vertex_shader_source.length(); const char * tmp1 = (const char *)my_vertex_shader_source.c_str(); const char * tmp2 = (const char *)my_fragment_shader_source.c_str(); glShaderSourceARB(my_vertex_shader, 1, &tmp1, NULL); glShaderSourceARB(my_fragment_shader, 1, &tmp2, NULL); // Compile The Shaders glCompileShaderARB(my_vertex_shader); glGetObjectParameterivARB(my_vertex_shader,GL_OBJECT_COMPILE_STATUS_ARB, &Vcompiled); glCompileShaderARB(my_fragment_shader); glGetObjectParameterivARB(my_fragment_shader,GL_OBJECT_COMPILE_STATUS_ARB, &Fcompiled); // Attach The Shader Objects To The Program Object glAttachObjectARB(my_program, my_vertex_shader); glAttachObjectARB(my_program, my_fragment_shader); // Link The Program Object glLinkProgramARB(my_program); if ((Vcompiled!=1) || (Fcompiled!=1)){ // lErrors := CheckForErrors(my_program); // if lErrors <> '' then // showDebug('GLSL shader error '+lErrors); // end; // result := my_program ; std::cout << "shader has errors!" << std::endl; } //result = my_program int i = 0; memcpy(&i, &my_program, sizeof(my_program)); result = i; if (result == 0){ std::cout << "Unable to load or compile shader" << std::endl; } return result; }
/*************************************************************************** setShader ****************************************************************************/ void setShader(){ GLhandleARB my_program; GLhandleARB my_vertex_shader; GLhandleARB my_fragment_shader; GLsizei* elength = E_MALLOC(sizeof(GLint)); GLchar* infolog = E_MALLOC(5000*sizeof(GLchar)); // Create Shader And Program Objects my_program = glCreateProgramObjectARB(); my_vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); my_fragment_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); // Load Shader Sources GLcharARB* vertsrc; GLcharARB* fragsrc; switch(shademode){ case GOOCH_SHADER: vertsrc = readShaderSrc("/Users/tom/Desktop/all_shaders/gooch.vert"); fragsrc = readShaderSrc("/Users/tom/Desktop/all_shaders/gooch.frag"); break; case PHONG_SHADER: vertsrc = readShaderSrc("/Users/tom/Desktop/all_shaders/phong-use-diffuse.vert"); fragsrc = readShaderSrc("/Users/tom/Desktop/all_shaders/phong-use-diffuse.frag"); break; case LATTICE_SHADER: vertsrc = readShaderSrc("/Users/tom/Desktop/all_shaders/CH11-lattice.vert.txt"); fragsrc = readShaderSrc("/Users/tom/Desktop/all_shaders/CH11-lattice.frag.txt"); break; case PPL2_SHADER: vertsrc = readShaderSrc("/Users/tom/Desktop/all_shaders/per-pixel-lighting.vert"); fragsrc = readShaderSrc("/Users/tom/Desktop/all_shaders/per-pixel-lighting.frag"); break; case TEST_SHADER: vertsrc = readShaderSrc("/Users/tom/Desktop/all_shaders/CH14-chromaticAb.vert.txt"); fragsrc = readShaderSrc("/Users/tom/Desktop/all_shaders/CH14-chromaticAb.frag.txt"); break; } glShaderSourceARB(my_vertex_shader, 1, &vertsrc, NULL); glShaderSourceARB(my_fragment_shader, 1, &fragsrc, NULL); // Compile The Shaders glCompileShaderARB(my_vertex_shader); GLint blen = 0; GLint slen = 0; GLchar* compiler_log; glGetObjectParameterivARB(my_vertex_shader, GL_OBJECT_INFO_LOG_LENGTH_ARB , &blen); if (blen > 1) { if ((compiler_log = (GLcharARB*)E_MALLOC(blen)) == NULL) { printf("OOM\n"); } glGetInfoLogARB(my_vertex_shader, blen, &slen, compiler_log); if (compiler_log!=0) { printf("compiler_log: %s\n", compiler_log); free(compiler_log); } } glCompileShaderARB(my_fragment_shader); glGetObjectParameterivARB(my_fragment_shader, GL_OBJECT_INFO_LOG_LENGTH_ARB , &blen); if (blen > 1) { if ((compiler_log = (GLcharARB*)E_MALLOC(blen)) == NULL) { printf("OOM\n"); } glGetInfoLogARB(my_fragment_shader, blen, &slen, compiler_log); if (compiler_log!=0) { printf("compiler_log: %s\n", compiler_log); free(compiler_log); } } // Attach The Shader Objects To The Program Object glAttachObjectARB(my_program, my_vertex_shader); glAttachObjectARB(my_program, my_fragment_shader); glGetObjectParameterivARB(my_program, GL_OBJECT_INFO_LOG_LENGTH_ARB , elength); if (elength > 0){ glGetInfoLogARB(my_program, 500, elength, infolog); printf("%s\n", infolog); } // Link The Program Object glLinkProgramARB(my_program); glGetObjectParameterivARB(my_program, GL_OBJECT_INFO_LOG_LENGTH_ARB , elength); if (elength > 0){ glGetInfoLogARB(my_program, 500, elength, infolog); printf("%s\n", infolog); } glValidateProgramARB(my_program); if(GL_VALIDATE_STATUS){ // Use The Program Object Instead Of Fixed Function OpenGL glUseProgramObjectARB(my_program); if(shademode == GOOCH_SHADER){ // set gooch shader parameters GLint loc; GLfloat vs[]={0.75, 0.75, 0.75}; loc = glGetUniformLocationARB(my_program, "SurfaceColor"); glUniform3fv(loc, 1, vs); GLfloat vw[]={0.6, 0.6, 0.0}; loc = glGetUniformLocationARB(my_program, "WarmColor"); glUniform3fv(loc, 1, vw); GLfloat vc[]={0.0, 0.0, 0.6}; loc = glGetUniformLocationARB(my_program, "CoolColor"); glUniform3fv(loc, 1, vc); loc = glGetUniformLocationARB(my_program, "DiffuseWarm"); glUniform1f(loc, 0.45); loc = glGetUniformLocationARB(my_program, "DiffuseCool"); glUniform1f(loc, 0.40); } if(shademode == LATTICE_SHADER){ // set gooch shader parameters GLint loc; GLfloat vs[]={10.00, 100.0, 0.0}; loc = glGetUniformLocationARB(my_program, "LightPosition"); glUniform3fv(loc, 1, vs); GLfloat vw[]={0.6, 0.6, 0.0}; loc = glGetUniformLocationARB(my_program, "LightColor"); glUniform3fv(loc, 1, vw); GLfloat vc[]={0.0, 0.0, 100.0}; loc = glGetUniformLocationARB(my_program, "EyePosition"); glUniform3fv(loc, 1, vc); GLfloat vd[]={0.5, 0.5, 0.5}; loc = glGetUniformLocationARB(my_program, "Specular"); glUniform3fv(loc, 1, vd); GLfloat ve[]={1.0, 1.0, 1.0}; loc = glGetUniformLocationARB(my_program, "Ambient"); glUniform3fv(loc, 1, ve); loc = glGetUniformLocationARB(my_program, "Kd"); glUniform1f(loc, 0.40); GLfloat vf[]={20.0, 20.0}; loc = glGetUniformLocationARB(my_program, "Scale"); glUniform2fv(loc, 1, vf); GLfloat vg[]={0.3, 0.3}; loc = glGetUniformLocationARB(my_program, "Threshold"); glUniform2fv(loc, 1, vg); GLfloat vh[]={0.5, 0.5, 0.5}; loc = glGetUniformLocationARB(my_program, "SurfaceColor"); glUniform3fv(loc, 1, vh); } } glGetObjectParameterivARB(my_program, GL_OBJECT_INFO_LOG_LENGTH_ARB , elength); if (elength > 0){ glGetInfoLogARB(my_program, 500, elength, infolog); printf("%s\n", infolog); } }
bool Shader::CompileProgram() { // First make sure that we can use shaders if (!IsAvailable()) { Err() << "Failed to create a shader: your system doesn't support shaders " << "(you should test Shader::IsAvailable() before trying to use the Shader class)" << std::endl; return false; } // Make sure that GLEW is initialized (extra safety -- it is already done in IsAvailable()) priv::EnsureGlewInit(); // Destroy the shader if it was already created if (myShaderProgram) GLCheck(glDeleteObjectARB(myShaderProgram)); // Define the vertex shader source (we provide it directly as it doesn't have to change) static const char* vertexSrc = "void main()" "{" " gl_TexCoord[0] = gl_MultiTexCoord0;" " gl_FrontColor = gl_Color;" " gl_Position = ftransform();" "}"; // Create the program myShaderProgram = glCreateProgramObjectARB(); // Create the shaders GLhandleARB vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); GLhandleARB fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); // Compile them const char* fragmentSrc = myFragmentShader.c_str(); GLCheck(glShaderSourceARB(vertexShader, 1, &vertexSrc, NULL)); GLCheck(glShaderSourceARB(fragmentShader, 1, &fragmentSrc, NULL)); GLCheck(glCompileShaderARB(vertexShader)); GLCheck(glCompileShaderARB(fragmentShader)); // Check the compile logs GLint success; GLCheck(glGetObjectParameterivARB(vertexShader, GL_OBJECT_COMPILE_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; GLCheck(glGetInfoLogARB(vertexShader, sizeof(log), 0, log)); Err() << "Failed to compile shader:" << std::endl << log << std::endl; GLCheck(glDeleteObjectARB(vertexShader)); GLCheck(glDeleteObjectARB(fragmentShader)); GLCheck(glDeleteObjectARB(myShaderProgram)); myShaderProgram = 0; return false; } GLCheck(glGetObjectParameterivARB(fragmentShader, GL_OBJECT_COMPILE_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; GLCheck(glGetInfoLogARB(fragmentShader, sizeof(log), 0, log)); Err() << "Failed to compile shader:" << std::endl << log << std::endl; GLCheck(glDeleteObjectARB(vertexShader)); GLCheck(glDeleteObjectARB(fragmentShader)); GLCheck(glDeleteObjectARB(myShaderProgram)); myShaderProgram = 0; return false; } // Attach the shaders to the program GLCheck(glAttachObjectARB(myShaderProgram, vertexShader)); GLCheck(glAttachObjectARB(myShaderProgram, fragmentShader)); // We can now delete the shaders GLCheck(glDeleteObjectARB(vertexShader)); GLCheck(glDeleteObjectARB(fragmentShader)); // Link the program GLCheck(glLinkProgramARB(myShaderProgram)); // Get link log GLCheck(glGetObjectParameterivARB(myShaderProgram, GL_OBJECT_LINK_STATUS_ARB, &success)); if (success == GL_FALSE) { // Oops... link errors char log[1024]; GLCheck(glGetInfoLogARB(myShaderProgram, sizeof(log), 0, log)); Err() << "Failed to link shader:" << std::endl << log << std::endl; GLCheck(glDeleteObjectARB(myShaderProgram)); myShaderProgram = 0; return false; } return true; }
bool BL_Shader::LinkProgram() { int vertlen = 0, fraglen=0, proglen=0; int vertstatus=0, fragstatus=0, progstatus=0; unsigned int tmpVert=0, tmpFrag=0, tmpProg=0; int char_len=0; char *logInf =0; if (mError) goto programError; if (!vertProg || !fragProg) { spit("Invalid GLSL sources"); return false; } if ( !GLEW_ARB_fragment_shader) { spit("Fragment shaders not supported"); return false; } if ( !GLEW_ARB_vertex_shader) { spit("Vertex shaders not supported"); return false; } // -- vertex shader ------------------ tmpVert = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); glShaderSourceARB(tmpVert, 1, (const char**)&vertProg, 0); glCompileShaderARB(tmpVert); glGetObjectParameterivARB(tmpVert, GL_OBJECT_INFO_LOG_LENGTH_ARB,(GLint*) &vertlen); // print info if any if ( vertlen > 0 && vertlen < MAX_LOG_LEN) { logInf = (char*)MEM_mallocN(vertlen, "vert-log"); glGetInfoLogARB(tmpVert, vertlen, (GLsizei*)&char_len, logInf); if (char_len >0) { spit("---- Vertex Shader Error ----"); spit(logInf); } MEM_freeN(logInf); logInf=0; } // check for compile errors glGetObjectParameterivARB(tmpVert, GL_OBJECT_COMPILE_STATUS_ARB,(GLint*)&vertstatus); if (!vertstatus) { spit("---- Vertex shader failed to compile ----"); goto programError; } // -- fragment shader ---------------- tmpFrag = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); glShaderSourceARB(tmpFrag, 1,(const char**)&fragProg, 0); glCompileShaderARB(tmpFrag); glGetObjectParameterivARB(tmpFrag, GL_OBJECT_INFO_LOG_LENGTH_ARB, (GLint*) &fraglen); if (fraglen >0 && fraglen < MAX_LOG_LEN) { logInf = (char*)MEM_mallocN(fraglen, "frag-log"); glGetInfoLogARB(tmpFrag, fraglen,(GLsizei*) &char_len, logInf); if (char_len >0) { spit("---- Fragment Shader Error ----"); spit(logInf); } MEM_freeN(logInf); logInf=0; } glGetObjectParameterivARB(tmpFrag, GL_OBJECT_COMPILE_STATUS_ARB, (GLint*) &fragstatus); if (!fragstatus) { spit("---- Fragment shader failed to compile ----"); goto programError; } // -- program ------------------------ // set compiled vert/frag shader & link tmpProg = glCreateProgramObjectARB(); glAttachObjectARB(tmpProg, tmpVert); glAttachObjectARB(tmpProg, tmpFrag); glLinkProgramARB(tmpProg); glGetObjectParameterivARB(tmpProg, GL_OBJECT_INFO_LOG_LENGTH_ARB, (GLint*) &proglen); glGetObjectParameterivARB(tmpProg, GL_OBJECT_LINK_STATUS_ARB, (GLint*) &progstatus); if (proglen > 0 && proglen < MAX_LOG_LEN) { logInf = (char*)MEM_mallocN(proglen, "prog-log"); glGetInfoLogARB(tmpProg, proglen, (GLsizei*)&char_len, logInf); if (char_len >0) { spit("---- GLSL Program ----"); spit(logInf); } MEM_freeN(logInf); logInf=0; } if (!progstatus) { spit("---- GLSL program failed to link ----"); goto programError; } // set mShader = tmpProg; glDeleteObjectARB(tmpVert); glDeleteObjectARB(tmpFrag); mOk = 1; mError = 0; return true; programError: if (tmpVert) { glDeleteObjectARB(tmpVert); tmpVert=0; } if (tmpFrag) { glDeleteObjectARB(tmpFrag); tmpFrag=0; } if (tmpProg) { glDeleteObjectARB(tmpProg); tmpProg=0; } mOk = 0; mUse = 0; mError = 1; return false; }
void GlslProgram::test(){ GLhandleARB program1 = 0; // program handles program1 = glCreateProgramObjectARB(); checkGlError(); }
GPUShader *GPU_shader_create(const char *vertexcode, const char *fragcode, const char *libcode, const char *defines) { GLint status; GLcharARB log[5000]; GLsizei length = 0; GPUShader *shader; if (!GLEW_ARB_vertex_shader || !GLEW_ARB_fragment_shader) return NULL; shader = MEM_callocN(sizeof(GPUShader), "GPUShader"); if (vertexcode) shader->vertex = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); if (fragcode) shader->fragment = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); shader->object = glCreateProgramObjectARB(); if (!shader->object || (vertexcode && !shader->vertex) || (fragcode && !shader->fragment)) { fprintf(stderr, "GPUShader, object creation failed.\n"); GPU_shader_free(shader); return NULL; } if (vertexcode) { const char *source[4]; int num_source = 0; source[num_source++] = gpu_shader_standard_extensions(); source[num_source++] = gpu_shader_standard_defines(); if (defines) source[num_source++] = defines; if (vertexcode) source[num_source++] = vertexcode; glAttachObjectARB(shader->object, shader->vertex); glShaderSourceARB(shader->vertex, num_source, source, NULL); glCompileShaderARB(shader->vertex); glGetObjectParameterivARB(shader->vertex, GL_OBJECT_COMPILE_STATUS_ARB, &status); if (!status) { glGetInfoLogARB(shader->vertex, sizeof(log), &length, log); shader_print_errors("compile", log, vertexcode); GPU_shader_free(shader); return NULL; } } if (fragcode) { const char *source[5]; int num_source = 0; source[num_source++] = gpu_shader_standard_extensions(); source[num_source++] = gpu_shader_standard_defines(); if (defines) source[num_source++] = defines; if (libcode) source[num_source++] = libcode; if (fragcode) source[num_source++] = fragcode; glAttachObjectARB(shader->object, shader->fragment); glShaderSourceARB(shader->fragment, num_source, source, NULL); glCompileShaderARB(shader->fragment); glGetObjectParameterivARB(shader->fragment, GL_OBJECT_COMPILE_STATUS_ARB, &status); if (!status) { glGetInfoLogARB(shader->fragment, sizeof(log), &length, log); shader_print_errors("compile", log, fragcode); GPU_shader_free(shader); return NULL; } } #if 0 if (lib && lib->lib) glAttachObjectARB(shader->object, lib->lib); #endif glLinkProgramARB(shader->object); glGetObjectParameterivARB(shader->object, GL_OBJECT_LINK_STATUS_ARB, &status); if (!status) { glGetInfoLogARB(shader->object, sizeof(log), &length, log); if (fragcode) shader_print_errors("linking", log, fragcode); else if (vertexcode) shader_print_errors("linking", log, vertexcode); else if (libcode) shader_print_errors("linking", log, libcode); GPU_shader_free(shader); return NULL; } return shader; }
WShaderProgram::WShaderProgram() { progId = glCreateProgramObjectARB(); }
static void R_BuildGammaProgram(void) { GLint compiled; gammaProgram.vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); gammaProgram.fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); glShaderSourceARB(gammaProgram.vertexShader, 1, (const GLcharARB **)&simpleGammaVert, NULL); glShaderSourceARB(gammaProgram.fragmentShader, 1, (const GLcharARB **)&simpleGammaFrag, NULL); glCompileShaderARB(gammaProgram.vertexShader); glCompileShaderARB(gammaProgram.fragmentShader); glGetObjectParameterivARB(gammaProgram.vertexShader, GL_COMPILE_STATUS, &compiled); if (!compiled) { GLint blen = 0; GLsizei slen = 0; glGetShaderiv(gammaProgram.vertexShader, GL_INFO_LOG_LENGTH, &blen); if (blen > 1) { GLchar *compiler_log; compiler_log = (GLchar *) malloc(blen); glGetInfoLogARB(gammaProgram.vertexShader, blen, &slen, compiler_log); Ren_Fatal("Failed to compile the gamma vertex shader reason: %s\n", compiler_log); } else { Ren_Fatal("Failed to compile the gamma vertex shader\n"); } } glGetObjectParameterivARB(gammaProgram.fragmentShader, GL_COMPILE_STATUS, &compiled); if (!compiled) { Ren_Fatal("Failed to compile the gamma fragment shader\n"); } gammaProgram.program = glCreateProgramObjectARB(); if (!gammaProgram.program) { Ren_Fatal("Failed to create program\n"); } glAttachObjectARB(gammaProgram.program, gammaProgram.vertexShader); glAttachObjectARB(gammaProgram.program, gammaProgram.fragmentShader); glLinkProgramARB(gammaProgram.program); glGetProgramivARB(gammaProgram.program, GL_LINK_STATUS, &compiled); // this throws glGetError() = 0x500 if (!compiled) { Ren_Fatal("Failed to link gamma shaders\n"); } glUseProgramObjectARB(gammaProgram.program); gammaProgram.currentMapUniform = glGetUniformLocation(gammaProgram.program, "u_CurrentMap"); gammaProgram.gammaUniform = glGetUniformLocation(gammaProgram.program, "u_gamma"); glUseProgramObjectARB(0); }
/// This function loads a vertex and fragment shader file void GLSLShader::InitShaders() { // Make sure the user passed in at least a vertex and fragment shader file if(!GetVertexShaderFileName().length() || !GetFragmentShaderFileName().length()) { std::cerr << "GLSLShader requires setting a VertexShader and a FragmentShader" << std::endl; return; } // If any of our shader pointers are set, let's free them first. if(!m_hShaders.empty() || m_hProgramObject) Release(); bool ready = true; // Now we load and compile the shaders from their respective files for (std::map<GLint,std::string>::const_iterator it = m_hFileNames.begin(), itend = m_hFileNames.end(); it != itend; ++it) { ready &= CompileShader( it->first, it->second, header ); } if (!ready) { std::cerr << "SHADER compilation failed.\n"; return; } // Next we create a program object to represent our shaders m_hProgramObject = glCreateProgramObjectARB(); // We attach each shader we just loaded to our program object for (std::map<GLint,GLhandleARB>::const_iterator it = m_hShaders.begin(), itend = m_hShaders.end(); it != itend; ++it) { glAttachObjectARB(m_hProgramObject, it->second); } #if defined(GL_EXT_geometry_shader4) && defined(SOFA_HAVE_GLEW) if (GetGeometryShaderID()) { if (geometry_input_type != -1) glProgramParameteriEXT(m_hProgramObject, GL_GEOMETRY_INPUT_TYPE_EXT, geometry_input_type ); if (geometry_output_type != -1) glProgramParameteriEXT(m_hProgramObject, GL_GEOMETRY_OUTPUT_TYPE_EXT, geometry_output_type ); if (geometry_vertices_out != -1) glProgramParameteriEXT(m_hProgramObject, GL_GEOMETRY_VERTICES_OUT_EXT, geometry_vertices_out ); } #endif // Our last init function is to link our program object with OpenGL glLinkProgramARB(m_hProgramObject); GLint linked = 0, length = 0, laux = 0; glGetObjectParameterivARB(m_hProgramObject, GL_OBJECT_LINK_STATUS_ARB, &linked); if (!linked) std::cerr << "ERROR: Link of program shader failed:\n"<<std::endl; // else std::cout << "Link of program shader OK" << std::endl; glGetObjectParameterivARB(m_hProgramObject, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length); if (length > 1) { GLcharARB *logString = (GLcharARB *)malloc((length+1) * sizeof(GLcharARB)); glGetInfoLogARB(m_hProgramObject, length, &laux, logString); std::cerr << logString << std::endl; free(logString); for (std::map<GLint,std::string>::const_iterator it = m_hFileNames.begin(), itend = m_hFileNames.end(); it != itend; ++it) std::cerr << GetShaderStageName(it->first) << " shader file: " << it->second << std::endl; } // Now, let's turn off the shader initially. glUseProgramObjectARB(0); }
void moShaderGLSL::CreateFragShader(moText frag_source) { m_ProgramObject = glCreateProgramObjectARB(); compileFragShader(frag_source); linkProgram(); }
/* *vertexShaderSrc contains the memory representation of the vertex shader *fragmentShaderSrc contains the memory representation of the fragment shader */ void CShader::loadShadersVF() { GLint compiled; GLint blen = 0; GLsizei slen = 0; vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); glShaderSourceARB(vertexShader,1,(const char **)(&vertexSrcProgram),NULL); //COMPILATION OF SHADERS glCompileShaderARB(vertexShader); glGetShaderiv(vertexShader,GL_COMPILE_STATUS,&compiled); if(compiled == GL_FALSE) { std::cout<<"Vertex shader compiled Unsuccesfully"<<std::endl; glGetShaderiv(vertexShader,GL_INFO_LOG_LENGTH,&blen); if(blen > 1) { GLchar* compiler_log = new GLchar[blen]; glGetInfoLogARB(vertexShader,blen,&slen,compiler_log); std::cout<<"Compiler log: "<<compiler_log<<std::endl; delete compiler_log; } glDeleteShader(vertexShader); glDeleteShader(fragmentShader); return; } std::cout<<"Vertex shader compiled succesfully"<<std::endl; fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER); glShaderSourceARB(fragmentShader,1 ,(const char **)(&fragmentSrcProgram),NULL); glCompileShaderARB(fragmentShader); glGetShaderiv(fragmentShader,GL_COMPILE_STATUS,&compiled); if(compiled == GL_FALSE) { std::cout<<"Fragment shader compiled Unsuccesfully"<<std::endl; glGetShaderiv(fragmentShader,GL_INFO_LOG_LENGTH,&blen); if(blen > 1) { GLchar* compiler_log = new GLchar[blen]; glGetInfoLogARB(fragmentShader,blen,&slen,compiler_log); std::cout<<"Compiler log: "<<compiler_log<<std::endl; delete compiler_log; } glDeleteShader(vertexShader); glDeleteShader(fragmentShader); return; } std::cout<<"FragmentShader Conpiled Successfully"<<std::endl; //if everything compiled well, link the program shaderProgram = glCreateProgramObjectARB(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram,fragmentShader); //binding of attributes should be done here glLinkProgram(shaderProgram); //we do not need these any more glDeleteShader(vertexShader); glDeleteShader(fragmentShader); }
bool Shader::compile(const char* vertexShaderCode, const char* fragmentShaderCode) { ensureGlContext(); // First make sure that we can use shaders if (!isAvailable()) { err() << "Failed to create a shader: your system doesn't support shaders " << "(you should test Shader::isAvailable() before trying to use the Shader class)" << std::endl; return false; } // Destroy the shader if it was already created if (m_shaderProgram) glCheck(glDeleteObjectARB(m_shaderProgram)); // Create the program m_shaderProgram = glCreateProgramObjectARB(); // Create the vertex shader if needed if (vertexShaderCode) { // Create and compile the shader GLhandleARB vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); glCheck(glShaderSourceARB(vertexShader, 1, &vertexShaderCode, NULL)); glCheck(glCompileShaderARB(vertexShader)); // Check the compile log GLint success; glCheck(glGetObjectParameterivARB(vertexShader, GL_OBJECT_COMPILE_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; glCheck(glGetInfoLogARB(vertexShader, sizeof(log), 0, log)); err() << "Failed to compile vertex shader:" << std::endl << log << std::endl; glCheck(glDeleteObjectARB(vertexShader)); glCheck(glDeleteObjectARB(m_shaderProgram)); m_shaderProgram = 0; return false; } // Attach the shader to the program, and delete it (not needed anymore) glCheck(glAttachObjectARB(m_shaderProgram, vertexShader)); glCheck(glDeleteObjectARB(vertexShader)); } // Create the fragment shader if needed if (fragmentShaderCode) { // Create and compile the shader GLhandleARB fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); glCheck(glShaderSourceARB(fragmentShader, 1, &fragmentShaderCode, NULL)); glCheck(glCompileShaderARB(fragmentShader)); // Check the compile log GLint success; glCheck(glGetObjectParameterivARB(fragmentShader, GL_OBJECT_COMPILE_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; glCheck(glGetInfoLogARB(fragmentShader, sizeof(log), 0, log)); err() << "Failed to compile fragment shader:" << std::endl << log << std::endl; glCheck(glDeleteObjectARB(fragmentShader)); glCheck(glDeleteObjectARB(m_shaderProgram)); m_shaderProgram = 0; return false; } // Attach the shader to the program, and delete it (not needed anymore) glCheck(glAttachObjectARB(m_shaderProgram, fragmentShader)); glCheck(glDeleteObjectARB(fragmentShader)); } // Link the program glCheck(glLinkProgramARB(m_shaderProgram)); // Check the link log GLint success; glCheck(glGetObjectParameterivARB(m_shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &success)); if (success == GL_FALSE) { char log[1024]; glCheck(glGetInfoLogARB(m_shaderProgram, sizeof(log), 0, log)); err() << "Failed to link shader:" << std::endl << log << std::endl; glCheck(glDeleteObjectARB(m_shaderProgram)); m_shaderProgram = 0; return false; } return true; }
bool ShaderModeData::BeginSphereTexturing() { if(!ModeAvailable()) return false; if(!GLSL_shaders_setup) { GLsizei loglen = 0; #define MAX_LOG_SIZE 2000 char *log = new char[MAX_LOG_SIZE]; memset(log, 0, MAX_LOG_SIZE); GLint maxvarying = 0; glGetIntegerv(GL_MAX_VARYING_FLOATS_ARB, &maxvarying); debug1 << "Maximum # of varying floats:" << maxvarying << endl; // Create the shader program handle. p = glCreateProgramObjectARB(); // Create the vertex program and link it to the program. v = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); if(GLSL_set_depth) glShaderSourceARB(v, 1, &GLSL_spheredepth_vertex_program_source, NULL); else glShaderSourceARB(v, 1, &GLSL_sphere_vertex_program_source, NULL); glCompileShaderARB(v); GLint vc = 0; glGetObjectParameterivARB(v, GL_OBJECT_COMPILE_STATUS_ARB, &vc); debug1 << "Vertex program " << ((vc==1)?" compiled":" did not compile") << endl; glAttachObjectARB(p, v); memset(log, 0, MAX_LOG_SIZE); glGetInfoLogARB(v, MAX_LOG_SIZE, &loglen, log); debug1 << "Vertex log:\n" << log << endl; // Create the fragment program and link it to the program. f = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); if(GLSL_set_depth) glShaderSourceARB(f, 1, &GLSL_spheredepth_fragment_program_source, NULL); else glShaderSourceARB(f, 1, &GLSL_sphere_fragment_program_source, NULL); glCompileShaderARB(f); GLint fc = 0; glGetObjectParameterivARB(f, GL_OBJECT_COMPILE_STATUS_ARB, &fc); debug1 << "Fragment program " << ((fc==1)?" compiled":" did not compile") << endl; glAttachObjectARB(p, f); memset(log, 0, MAX_LOG_SIZE); glGetInfoLogARB(f, MAX_LOG_SIZE, &loglen, log); debug1 << "Fragment log:\n" << log << endl; glLinkProgramARB(p); GLint pls = 0; glGetObjectParameterivARB(p, GL_OBJECT_LINK_STATUS_ARB, &pls); debug1 << "Program " << ((pls==1)?" linked":" did not link") << endl; memset(log, 0, MAX_LOG_SIZE); glGetInfoLogARB(p, MAX_LOG_SIZE, &loglen, log); debug1 << "Program log:\n" << log << endl; delete [] log; GLSL_shaders_setup = true; if(vc == 0 || fc == 0 || pls == 0) { debug1 << "One or more of the required shader programs is " "not supported." << endl; return false; } } // Start using the shader. glUseProgramObjectARB(p); return true; }