GLhandleARB ccShaderARB::LoadShaderARB(GLenum type, const char *filename) { //Shader creation GLhandleARB shader = glCreateShaderObjectARB(type); if(shader == 0) { //ccLog::Error("Can't create shader!"); return 0; } //code loading char *src = ReadShaderFile(filename); if(!src) { glDeleteObjectARB(shader); return 0; } glShaderSourceARB(shader, 1, (const GLcharARB**)&src, NULL); glCompileShaderARB(shader); //we don't need the program code anymore delete[] src; src=0; //we must check compilation result /*GLint status = GL_TRUE; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if(status != GL_TRUE) { //log size GLsizei logSize; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize); //buffer to get log from OpenGL char* log = new char[logSize+1]; if(!log) { ccLog::Warning("Not enough memory to log shader creation..."); return 0; } memset(log, 0, logSize+1); glGetShaderInfoLog(shader, logSize, &logSize, log); ccLog::Error("Can't compile shader (file:'%s').\nLog: %s",filename,log); //free memory delete[] log; glDeleteShader(shader); return 0; } //*/ return shader; }
//loades the shaders into a program, retruning the programs id GLuint glslLoadShaderProgram( std::string vertex, std::string fragment ){ //a quick check if( vertex.empty() || fragment.empty() ){ printf( "ERROR: Unable to load programs, program defined as NULL" ); return 0; } printf( "INFO: Loading Shaders %s %s\n", vertex.c_str(), fragment.c_str()); //temp ids GLuint vsID, fsID, program; //make the vertex shader const char* vertex_data = ReadShaderFile( vertex ); vsID = glCreateShader( GL_VERTEX_SHADER ); glShaderSource( vsID, 1, &vertex_data, NULL ); glCompileShader( vsID ); printShaderLog( vsID ); delete [] vertex_data; //make the fragment shader const char * fragment_data = ReadShaderFile( fragment ); fsID = glCreateShader( GL_FRAGMENT_SHADER ); glShaderSource( fsID, 1, &fragment_data, NULL ); glCompileShader( fsID ); printShaderLog( fsID ); delete [] fragment_data; //make and link the program program = glCreateProgram(); glAttachShader( program, vsID ); glAttachShader( program, fsID ); glBindFragDataLocationEXT( program, 0, "pixelColor" ); glLinkProgram( program ); printProgramLog( program ); //and return, best of luck return program; }
/* ** Initializing the Shader objects */ AESDK_OpenGL_Err AESDK_OpenGL_InitShader( AESDK_OpenGL_EffectCommonData& inData, string inVertexShaderFile, string inFragmentShaderFile ) { AESDK_OpenGL_Err result = AESDK_OpenGL_OK; try { const char *vertexShaderStringsP[1]; const char *fragmentShaderStringsP[1]; GLint vertCompiledB; GLint fragCompiledB; GLint linkedB; // Create the vertex shader... inData.mVertexShaderSu = glCreateShaderObjectARB( GL_VERTEX_SHADER_ARB ); unsigned char* vertexShaderAssemblyP = NULL; if( (vertexShaderAssemblyP = ReadShaderFile( inVertexShaderFile )) == NULL) GL_CHECK(AESDK_OpenGL_ShaderInit_Err); vertexShaderStringsP[0] = (char*)vertexShaderAssemblyP; glShaderSourceARB( inData.mVertexShaderSu, 1, vertexShaderStringsP, NULL ); glCompileShaderARB( inData.mVertexShaderSu); delete vertexShaderAssemblyP; glGetObjectParameterivARB( inData.mVertexShaderSu, GL_OBJECT_COMPILE_STATUS_ARB, &vertCompiledB ); char str[4096]; if(!vertCompiledB) { glGetInfoLogARB(inData.mVertexShaderSu, sizeof(str), NULL, str); GL_CHECK(AESDK_OpenGL_ShaderInit_Err); } // Create the fragment shader... inData.mFragmentShaderSu = glCreateShaderObjectARB( GL_FRAGMENT_SHADER_ARB ); unsigned char* fragmentShaderAssemblyP = NULL; if( (fragmentShaderAssemblyP = ReadShaderFile( inFragmentShaderFile )) == NULL) GL_CHECK(AESDK_OpenGL_ShaderInit_Err); fragmentShaderStringsP[0] = (char*)fragmentShaderAssemblyP; glShaderSourceARB( inData.mFragmentShaderSu, 1, fragmentShaderStringsP, NULL ); glCompileShaderARB( inData.mFragmentShaderSu ); delete fragmentShaderAssemblyP; glGetObjectParameterivARB( inData.mFragmentShaderSu, GL_OBJECT_COMPILE_STATUS_ARB,&fragCompiledB ); if(!fragCompiledB) { //glGetInfoLogARB( inData.mFragmentShaderSu, sizeof(str), NULL, str ); GL_CHECK(AESDK_OpenGL_ShaderInit_Err); } // Create a program object and attach the two compiled shaders... inData.mProgramObjSu = glCreateProgramObjectARB(); glAttachObjectARB( inData.mProgramObjSu, inData.mVertexShaderSu ); glAttachObjectARB( inData.mProgramObjSu, inData.mFragmentShaderSu ); // Link the program object glLinkProgramARB( inData.mProgramObjSu ); glGetObjectParameterivARB( inData.mProgramObjSu, GL_OBJECT_LINK_STATUS_ARB, &linkedB ); if( !linkedB ) { //glGetInfoLogARB( inData.mProgramObjSu, sizeof(str), NULL, str ); GL_CHECK(AESDK_OpenGL_ShaderInit_Err); } else { inData.mUsingShaderB = true; } } catch(AESDK_OpenGL_Err& err) { result = err; } return result; }
Effect::Effect(const std::wstring& vsPath, const std::wstring& psPath, Layout* inputLayout, const std::wstring& gsPath, const std::wstring& hsPath, const std::wstring& dsPath, const std::wstring& csPath) : m_vertexShader(0), m_pixelShader(0), m_geometryShader(0), m_hullShader(0), m_domainShader(0), m_computeShader(0), m_vsBlob(0), m_psBlob(0), m_gsBlob(0), m_hsBlob(0), m_dsBlob(0), m_csBlob(0), m_inputLayout(inputLayout) { // Add error checking HRESULT hr; if (vsPath.length()) { ReadShaderFile(vsPath, &m_vsBlob, "vs_5_0"); hr = D3D11Renderer::Instance()->GetD3DDevice()->CreateVertexShader( m_vsBlob->GetBufferPointer(), m_vsBlob->GetBufferSize(), nullptr, &m_vertexShader ); HR(hr); } if (psPath.length()) { ReadShaderFile(psPath, &m_psBlob, "ps_5_0"); hr = D3D11Renderer::Instance()->GetD3DDevice()->CreatePixelShader( m_psBlob->GetBufferPointer(), m_psBlob->GetBufferSize(), nullptr, &m_pixelShader ); HR(hr); } if (gsPath.length()) { ReadShaderFile(gsPath, &m_gsBlob, "gs_5_0"); hr = D3D11Renderer::Instance()->GetD3DDevice()->CreateGeometryShader( m_gsBlob->GetBufferPointer(), m_gsBlob->GetBufferSize(), nullptr, &m_geometryShader ); HR(hr); } if (hsPath.length()) { ReadShaderFile(hsPath, &m_hsBlob, "hs_5_0"); hr = D3D11Renderer::Instance()->GetD3DDevice()->CreateHullShader( m_hsBlob->GetBufferPointer(), m_hsBlob->GetBufferSize(), nullptr, &m_hullShader ); HR(hr); } if (dsPath.length()) { ReadShaderFile(dsPath, &m_dsBlob, "ds_5_0"); hr = D3D11Renderer::Instance()->GetD3DDevice()->CreateDomainShader( m_dsBlob->GetBufferPointer(), m_dsBlob->GetBufferSize(), nullptr, &m_domainShader ); HR(hr); } if (csPath.length()) { ReadShaderFile(csPath, &m_csBlob, "cs_5_0"); hr = D3D11Renderer::Instance()->GetD3DDevice()->CreateComputeShader( m_csBlob->GetBufferPointer(), m_csBlob->GetBufferSize(), nullptr, &m_computeShader ); HR(hr); } if (m_inputLayout == nullptr) m_inputLayout = new StandardLayout(); m_inputLayout->CreateLayout(m_vsBlob); }