Exemplo n.º 1
0
Effect::Effect(std::vector<std::string> filenames) {

	GLuint programID = glCreateProgram();

	std::vector<std::string> vscript, fscript;

	for (unsigned int i=0; i<filenames.size(); i++) {
		int shaderType = _checkIfShaderFile(filenames.at(i));
		if(shaderType != 0) {
			std::string text = _loadTextFile(filenames.at(i));
			if(shaderType == Vertex) {
				vscript.push_back(text);
			} else if(shaderType == Fragment) {
				fscript.push_back(text);
			}
		}
	}

	GLuint vshaderID = _compileShader(Vertex, vscript);
	glAttachShader(programID, vshaderID);
	GLuint fshaderID = _compileShader(Fragment, fscript);
	glAttachShader(programID, fshaderID);

	glLinkProgram(programID);
	GLint status;
	glGetProgramiv(programID, GL_LINK_STATUS, &status);
	if(status == GL_FALSE) {
		char programInfoLog[100];
		GLsizei length;
		glGetProgramInfoLog(programID, 100, &length, &programInfoLog[0]);
		throw Error(&programInfoLog[0]);
	}

	this->program = programID;
}
Exemplo n.º 2
0
bool gl4::Shader::attachTessellationShader(const char *controlFilename, const char *evalFilename)
{
	GLuint shader1 = _compileShader(GL_TESS_CONTROL_SHADER, controlFilename, "Tessellation control");
	GLuint shader2 = _compileShader(GL_TESS_EVALUATION_SHADER, evalFilename, "Tessellation evaluation");
	if (shader1 != 0 && shader2 != 0)
	{
		glAttachShader( _shaderProgram, shader1 );
		glAttachShader( _shaderProgram, shader2 );
	}
}
Exemplo n.º 3
0
    void Shader::prepare()
    {
        std::string vertexSrc;
        _readShaderSource(_vertexShaderPath.c_str(), vertexSrc);

        std::string fragmentSrc;
        _readShaderSource(_fragmentShaderPath.c_str(), fragmentSrc);

        //Compile vertex shader
        _compileShader(vertexSrc, _vertexShaderId, GL_VERTEX_SHADER);

        //Compile fragment shader
        _compileShader(fragmentSrc, _fragmentShaderId, GL_FRAGMENT_SHADER);

        //Attach and link shaders
        _attachAndLinkShader(_vertexShaderId, _fragmentShaderId, _shaderProgramId);
    }
Exemplo n.º 4
0
bool gl4::Shader::attachGeometryShader(const char *filename)
{
	GLuint shader = _compileShader(GL_GEOMETRY_SHADER, filename, "Geometry");
	if (shader != 0)
	{
		glAttachShader( _shaderProgram, shader );
	}
}
Exemplo n.º 5
0
bool gl4::Shader::attachFragmentShader(const char *filename)
{
	GLuint shader = _compileShader(GL_FRAGMENT_SHADER, filename, "Fragment");
	if (shader != 0)
	{
		glAttachShader( _shaderProgram, shader );
	}
}
Exemplo n.º 6
0
bool gl4::Shader::attachVertexShader(const char *filename)
{
	GLuint shader = _compileShader(GL_VERTEX_SHADER, filename, "Vertex");
	if (shader != 0)
	{
		glAttachShader( _shaderProgram, shader );
	}
}
Exemplo n.º 7
0
Effect::Effect(std::vector<std::string> scripts, std::vector<int> types) {

	GLuint programID = glCreateProgram();

	std::vector<std::string> vscript, fscript;

	for (unsigned int i=0; i<scripts.size(); i++) {
		if(types.at(i) == Vertex) {
			vscript.push_back(scripts.at(i));
		} else if(types.at(i) == Fragment) {
			fscript.push_back(scripts.at(i));
		}
	}

	GLuint vshaderID = _compileShader(Vertex, vscript);
	glAttachShader(programID, vshaderID);
	GLuint fshaderID = _compileShader(Fragment, fscript);
	glAttachShader(programID, fshaderID);

	Error error = _checkForErrors();
	if(error.Report().length() > 0) {
		throw error;
	}

	glLinkProgram(programID);
	GLint status;
	glGetProgramiv(programID, GL_LINK_STATUS, &status);
	if(status == GL_FALSE) {
		char programInfoLog[100];
		GLsizei length;
		glGetProgramInfoLog(programID, 100, &length, &programInfoLog[0]);
		throw Error(&programInfoLog[0]);
	}

	this->program = programID;
}
bool CShaderMngr::LoadShader(const char *strFileName, GLenum nShaderType, const char *strName)
{
	shaderData *sData = new shaderData;

	if (_loadShader(strFileName, sData) == 0)
	{
		if (_compileShader(sData, nShaderType))
		{
			m_Shaders.push_back(std::pair<string, shaderData*>(string(strName), sData));
			return true;
		}
	}

	return false;
}
Exemplo n.º 9
0
bool GFXD3D9Shader::_init()
{
   PROFILE_SCOPE( GFXD3D9Shader_Init );
   
   if ( mPixVersion > GFX->getPixelShaderVersion() )
   {
      if ( smLogErrors )
         Con::errorf( "GFXD3D9Shader::init - Bad pixel shader version!" );

      return false;
   }

   if ( mPixVersion < 1.0f && mPixelFile.getFileName().isNotEmpty() )
   {
      if ( smLogErrors )
         Con::errorf( "GFXD3D9Shader::init - Pixel shaders not supported on SM %.1f!", mPixVersion );

      return false;
   }

   SAFE_RELEASE(mVertShader);
   SAFE_RELEASE(mPixShader);

   U32 mjVer = (U32)mFloor( mPixVersion );
   U32 mnVer = (U32)( ( mPixVersion - F32( mjVer ) ) * 10.01f ); // 10.01 instead of 10.0 because of floating point issues

   String vertTarget = String::ToString("vs_%d_%d", mjVer, mnVer);
   String pixTarget = String::ToString("ps_%d_%d", mjVer, mnVer);

   // Adjust version for vertex shaders
   if (mjVer == 2 && mnVer == 1)
   {      
      pixTarget  = "ps_2_a";
      vertTarget = "vs_2_0";
   }
   else if ( mjVer == 2 && mnVer == 2 )
   {      
      pixTarget  = "ps_2_b";
      vertTarget = "vs_2_0";
   }
   else if ( ( mPixVersion < 2.0f ) && ( mPixVersion > 1.101f ) )
      vertTarget = "vs_1_1";      

   // Create the macro array including the system wide macros.
   const U32 macroCount = smGlobalMacros.size() + mMacros.size() + 2;
   FrameTemp<D3DXMACRO> d3dXMacros( macroCount );
   for ( U32 i=0; i < smGlobalMacros.size(); i++ )
   {
      d3dXMacros[i].Name = smGlobalMacros[i].name.c_str();
      d3dXMacros[i].Definition = smGlobalMacros[i].value.c_str();
   }
   for ( U32 i=0; i < mMacros.size(); i++ )
   {
      d3dXMacros[i+smGlobalMacros.size()].Name = mMacros[i].name.c_str();
      d3dXMacros[i+smGlobalMacros.size()].Definition = mMacros[i].value.c_str();
   }
   String smVersion = String::ToString( mjVer * 10 + mnVer );
   d3dXMacros[macroCount - 2].Name = "TORQUE_SM";
   d3dXMacros[macroCount - 2].Definition = smVersion.c_str();
   d3dXMacros[macroCount - 1].Name = NULL;
   d3dXMacros[macroCount - 1].Definition = NULL;

   if ( !mVertexConstBufferLayoutF )
      mVertexConstBufferLayoutF = new GFXD3D9ShaderBufferLayout();
   else
      mVertexConstBufferLayoutF->clear();

   if ( !mVertexConstBufferLayoutI )
      mVertexConstBufferLayoutI = new GFXD3D9ShaderBufferLayout();
   else
      mVertexConstBufferLayoutI->clear();
      
   if ( !mPixelConstBufferLayoutF )
      mPixelConstBufferLayoutF = new GFXD3D9ShaderBufferLayout();
   else
      mPixelConstBufferLayoutF->clear();

   if ( !mPixelConstBufferLayoutI )
      mPixelConstBufferLayoutI = new GFXD3D9ShaderBufferLayout();
   else
      mPixelConstBufferLayoutI->clear();
   
   mSamplerDescriptions.clear();
   mShaderConsts.clear();

   if ( GFXD3DX.isLoaded && !Con::getBoolVariable( "$shaders::forceLoadCSF", false ) )
   {
      if (  !mVertexFile.isEmpty() &&
            !_compileShader( mVertexFile, vertTarget, d3dXMacros, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mSamplerDescriptions ) )
         return false;

      if (  !mPixelFile.isEmpty() &&
            !_compileShader( mPixelFile, pixTarget, d3dXMacros, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI, mSamplerDescriptions ) )
         return false;
   } 
   else 
   {
      if ( !_loadCompiledOutput( mVertexFile, vertTarget, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mSamplerDescriptions ) )
      {
         if ( smLogErrors )
            Con::errorf( "GFXD3D9Shader::init - Unable to load precompiled vertex shader for '%s'.", 
               mVertexFile.getFullPath().c_str() );

         return false;
      }

      if ( !_loadCompiledOutput( mPixelFile, pixTarget, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI, mSamplerDescriptions ) )
      {
         if ( smLogErrors )
            Con::errorf( "GFXD3D9Shader::init - Unable to load precompiled pixel shader for '%s'.", 
               mPixelFile.getFullPath().c_str() );

         return false;
      }
   }

   // Existing handles are resored to an uninitialized state.
   // Those that are found when parsing the layout parameters
   // will then be re-initialized.
   HandleMap::Iterator iter = mHandles.begin();
   for ( ; iter != mHandles.end(); iter++ )        
      (iter->value)->clear();      

   _buildShaderConstantHandles(mVertexConstBufferLayoutF, true);
   _buildShaderConstantHandles(mVertexConstBufferLayoutI, true);
   _buildShaderConstantHandles(mPixelConstBufferLayoutF, false);
   _buildShaderConstantHandles(mPixelConstBufferLayoutI, false);
   _buildSamplerShaderConstantHandles( mSamplerDescriptions );
   _buildInstancingShaderConstantHandles();

   // Notify any existing buffers that the buffer 
   // layouts have changed and they need to update.
   Vector<GFXShaderConstBuffer*>::iterator biter = mActiveBuffers.begin();
   for ( ; biter != mActiveBuffers.end(); biter++ )
      ((GFXD3D9ShaderConstBuffer*)(*biter))->onShaderReload( this );

   return true;
}