Commit 27a6f8af authored by Maciej Sumiński's avatar Maciej Sumiński

Shaders are built-in instead of being loaded from external files.

parent 335bf720
......@@ -12,6 +12,20 @@ include_directories(
)
if(KICAD_GAL)
# Generate files containing shader programs
add_custom_command (
OUTPUT gal/opengl/shader_src.h
DEPENDS gal/opengl/make_shader_src_h.sh
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/common/gal/opengl
COMMAND ${SHELL}
ARGS ${PROJECT_SOURCE_DIR}/common/gal/opengl/make_shader_src_h.sh
)
add_custom_target (
ShaderHeader ALL
DEPENDS gal/opengl/shader_src.h
)
set(GAL_SRCS
drawpanel_gal.cpp
painter.cpp
......@@ -27,6 +41,7 @@ set(GAL_SRCS
)
add_library(gal STATIC ${GAL_SRCS})
add_dependencies(gal ShaderHeader)
if(WIN32)
add_definitions(-DGLEW_STATIC)
......
......@@ -27,7 +27,6 @@
#include <wx/window.h>
#include <wx/event.h>
#include <wx/colour.h>
#include <wx/stdpaths.h>
#include <wx/filename.h>
#include <class_drawpanel_gal.h>
......@@ -56,11 +55,6 @@ EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWin
m_view = NULL;
m_painter = NULL;
wxStandardPaths paths;
wxFileName executableFile( paths.GetExecutablePath() );
m_galShaderPath = std::string( ( executableFile.GetPath() +
wxT( "/../../common/gal/opengl" ) ).mb_str() );
SwitchBackend( aGalType, true );
SetBackgroundStyle( wxBG_STYLE_CUSTOM );
......@@ -170,7 +164,6 @@ void EDA_DRAW_PANEL_GAL::SwitchBackend( GalType aGalType, bool aUseShaders )
{
case GAL_TYPE_OPENGL:
m_gal = new KiGfx::OPENGL_GAL( this, this, this, aUseShaders );
static_cast<KiGfx::OPENGL_GAL*> (m_gal)->SetShaderPath( m_galShaderPath );
break;
case GAL_TYPE_CAIRO:
......
#!/bin/bash
# Make a header file containing GLSL source code
echo "Generating headers containing GLSL source code.."
# Source files to be included
SHADER_SRC=( "shader.vert" "shader.frag" )
# Number of shaders
SHADERS_NUMBER=${#SHADER_SRC[@]}
OUTPUT="shader_src.h"
# Prepare GLSL source to be included in C array
function processSrc {
# 1st part: remove /* */ comments
# 2nd part: remove // comments
# 3rd part: remove blank lines (or containing only whitespaces)
# 4th & 5th part: wrap every line in quotation marks
sed '/\/\*/,/\*\//d; s/[ \t]*\/\/.*$//; /^[ \t]*$/d; s/^[ \t]*/"/; s/[ \t]*$/\\n"/' $1 >> $OUTPUT
echo "," >> $OUTPUT
}
# Header
echo "#ifndef SHADER_SRC_H
#define SHADER_SRC_H
const unsigned int shaders_number = $SHADERS_NUMBER;
const char *shaders_src[] = {" > $OUTPUT
# Main contents
for filename in "${SHADER_SRC[@]}"
do
processSrc $filename
done
# Footer
echo "};
#endif /* SHADER_SRC_H */" >> $OUTPUT
echo "Done."
......@@ -74,7 +74,6 @@ OPENGL_GAL::OPENGL_GAL( wxWindow* aParent, wxEvtHandler* aMouseListener,
isUseShader = isUseShaders;
isShaderInitialized = false;
isGrouping = false;
shaderPath = "../../common/gal/opengl";
wxSize parentSize = aParent->GetSize();
isVboInitialized = false;
......@@ -359,12 +358,12 @@ void OPENGL_GAL::BeginDrawing()
// Compile the shaders
if( !isShaderInitialized && isUseShader )
{
if( !shader.AddSource( shaderPath + std::string( "/shader.vert" ), SHADER_TYPE_VERTEX ) )
if( !shader.LoadBuiltinShader( 0, SHADER_TYPE_VERTEX ) )
{
wxLogFatalError( wxT( "Cannot compile vertex shader!" ) );
}
if( !shader.AddSource( shaderPath + std::string( "/shader.frag" ), SHADER_TYPE_FRAGMENT ) )
if( !shader.LoadBuiltinShader( 1, SHADER_TYPE_FRAGMENT ) )
{
wxLogFatalError( wxT( "Cannot compile fragment shader!" ) );
}
......
......@@ -32,14 +32,15 @@
#include <wx/log.h>
#include <gal/opengl/shader.h>
#include "shader_src.h"
using namespace KiGfx;
SHADER::SHADER() :
isProgramCreated( false ),
isShaderLinked( false ),
maximumVertices( 4 ),
active( false ),
maximumVertices( 4 ),
geomInputType( GL_LINES ),
geomOutputType( GL_LINES )
{
......@@ -62,7 +63,84 @@ SHADER::~SHADER()
}
void SHADER::ProgramInfo( GLuint aProgram )
bool SHADER::LoadBuiltinShader( unsigned int aShaderNumber, ShaderType aShaderType )
{
if( aShaderNumber >= shaders_number )
return false;
return addSource( std::string( shaders_src[aShaderNumber] ), aShaderType );
}
bool SHADER::LoadShaderFromFile( const std::string& aShaderSourceName, ShaderType aShaderType )
{
// Load shader sources
const std::string shaderSource = readSource( aShaderSourceName );
return addSource( shaderSource, aShaderType );
}
void SHADER::ConfigureGeometryShader( GLuint maxVertices, GLuint geometryInputType,
GLuint geometryOutputType )
{
maximumVertices = maxVertices;
geomInputType = geometryInputType;
geomOutputType = geometryOutputType;
}
bool SHADER::Link()
{
// Shader linking
glLinkProgram( programNumber );
programInfo( programNumber );
// Check the Link state
glGetObjectParameterivARB( programNumber, GL_OBJECT_LINK_STATUS_ARB, (GLint*) &isShaderLinked );
#ifdef __WXDEBUG__
if( !isShaderLinked )
{
int maxLength;
glGetProgramiv( programNumber, GL_INFO_LOG_LENGTH, &maxLength );
maxLength = maxLength + 1;
char *linkInfoLog = new char[maxLength];
glGetProgramInfoLog( programNumber, maxLength, &maxLength, linkInfoLog );
std::cerr << "Shader linking error:" << std::endl;
std::cerr << linkInfoLog;
delete[] linkInfoLog;
}
#endif /* __WXDEBUG__ */
return isShaderLinked;
}
void SHADER::AddParameter( const std::string& aParameterName )
{
GLint location = glGetUniformLocation( programNumber, aParameterName.c_str() );
if( location != -1 )
{
parameterLocation.push_back( location );
}
}
void SHADER::SetParameter( int parameterNumber, float value )
{
glUniform1f( parameterLocation[parameterNumber], value );
}
int SHADER::GetAttribute( std::string aAttributeName ) const
{
return glGetAttribLocation( programNumber, aAttributeName.c_str() );
}
void SHADER::programInfo( GLuint aProgram )
{
GLint glInfoLogLength = 0;
GLint writtenChars = 0;
......@@ -83,7 +161,7 @@ void SHADER::ProgramInfo( GLuint aProgram )
}
void SHADER::ShaderInfo( GLuint aShader )
void SHADER::shaderInfo( GLuint aShader )
{
GLint glInfoLogLength = 0;
GLint writtenChars = 0;
......@@ -104,7 +182,7 @@ void SHADER::ShaderInfo( GLuint aShader )
}
std::string SHADER::ReadSource( std::string aShaderSourceName )
std::string SHADER::readSource( std::string aShaderSourceName )
{
// Open the shader source for reading
std::ifstream inputFile( aShaderSourceName.c_str(), std::ifstream::in );
......@@ -130,7 +208,7 @@ std::string SHADER::ReadSource( std::string aShaderSourceName )
}
bool SHADER::AddSource( const std::string& aShaderSourceName, ShaderType aShaderType )
bool SHADER::addSource( const std::string& aShaderSource, ShaderType aShaderType )
{
if( isShaderLinked )
{
......@@ -144,24 +222,21 @@ bool SHADER::AddSource( const std::string& aShaderSourceName, ShaderType aShader
isProgramCreated = true;
}
// Load shader sources
std::string shaderSource = ReadSource( aShaderSourceName );
// Create a shader
GLuint shaderNumber = glCreateShader( aShaderType );
shaderNumbers.push_back( shaderNumber );
// Get the program info
ProgramInfo( programNumber );
programInfo( programNumber );
// Copy to char array
char* source = new char[shaderSource.size() + 1];
strcpy( source, shaderSource.c_str() );
char* source = new char[aShaderSource.size() + 1];
strcpy( source, aShaderSource.c_str() );
const char** source_ = (const char**) ( &source );
// Attach the source
glShaderSource( shaderNumber, 1, source_, NULL );
ProgramInfo( programNumber );
programInfo( programNumber );
// Compile and attach shader to the program
glCompileShader( shaderNumber );
......@@ -171,13 +246,13 @@ bool SHADER::AddSource( const std::string& aShaderSourceName, ShaderType aShader
{
wxLogError( wxT( "Shader compilation error" ) );
ShaderInfo( shaderNumber );
shaderInfo( shaderNumber );
return false;
}
glAttachShader( programNumber, shaderNumber );
ProgramInfo( programNumber );
programInfo( programNumber );
// Special handling for the geometry shader
if( aShaderType == SHADER_TYPE_GEOMETRY )
......@@ -193,61 +268,3 @@ bool SHADER::AddSource( const std::string& aShaderSourceName, ShaderType aShader
return true;
}
void SHADER::ConfigureGeometryShader( GLuint maxVertices, GLuint geometryInputType,
GLuint geometryOutputType )
{
maximumVertices = maxVertices;
geomInputType = geometryInputType;
geomOutputType = geometryOutputType;
}
bool SHADER::Link()
{
// Shader linking
glLinkProgram( programNumber );
ProgramInfo( programNumber );
// Check the Link state
glGetObjectParameterivARB( programNumber, GL_OBJECT_LINK_STATUS_ARB, (GLint*) &isShaderLinked );
#ifdef __WXDEBUG__
if( !isShaderLinked )
{
int maxLength;
glGetProgramiv( programNumber, GL_INFO_LOG_LENGTH, &maxLength );
maxLength = maxLength + 1;
char *linkInfoLog = new char[maxLength];
glGetProgramInfoLog( programNumber, maxLength, &maxLength, linkInfoLog );
std::cerr << "Shader linking error:" << std::endl;
std::cerr << linkInfoLog;
delete[] linkInfoLog;
}
#endif /* __WXDEBUG__ */
return isShaderLinked;
}
void SHADER::AddParameter( const std::string& aParameterName )
{
GLint location = glGetUniformLocation( programNumber, aParameterName.c_str() );
if( location != -1 )
{
parameterLocation.push_back( location );
}
}
void SHADER::SetParameter( int parameterNumber, float value )
{
glUniform1f( parameterLocation[parameterNumber], value );
}
int SHADER::GetAttribute( std::string aAttributeName ) const
{
return glGetAttribLocation( programNumber, aAttributeName.c_str() );
}
......@@ -91,8 +91,6 @@ protected:
GalType m_currentGal; ///< Currently used GAL
bool m_useShaders; ///< Are shaders used? (only for OpenGL GAL)
wxLongLong m_timeStamp;
std::string m_galShaderPath; ///< Path to shader files, used in OpenGL mode
};
#endif
......@@ -320,11 +320,6 @@ public:
paintListener = aPaintListener;
}
void SetShaderPath( const std::string& aPath )
{
shaderPath = aPath;
}
///< Parameters passed to the GLU tesselator
typedef struct
{
......@@ -389,7 +384,6 @@ private:
SHADER shader; ///< There is only one shader used for different objects
int shaderAttrib; ///< Location of shader attributes (for glVertexAttribPointer)
std::string shaderPath; ///< Location of shader files
// Cursor
int cursorSize; ///< Size of the cursor in pixels
......
......@@ -71,12 +71,22 @@ public:
virtual ~SHADER();
/**
* @brief Add a shader and compile the shader sources.
* @brief Loads one of the built-in shaders and compiles it.
*
* @param aShaderNumber is the shader number (indexing from 0).
* @param aShaderType is the type of the shader.
* @return True in case of success, false otherwise.
*/
bool LoadBuiltinShader( unsigned int aShaderNumber, ShaderType aShaderType );
/**
* @brief Loads one of the built-in shaders and compiles it.
*
* @param aShaderSourceName is the shader source file name.
* @param aShaderType is the type of the shader.
* @return True in case of success, false otherwise.
*/
bool AddSource( const std::string& aShaderSourceName, ShaderType aShaderType );
bool LoadShaderFromFile( const std::string& aShaderSourceName, ShaderType aShaderType );
/**
* @brief Link the shaders.
......@@ -157,14 +167,14 @@ private:
*
* @param aProgram is the program number.
*/
void ProgramInfo( GLuint aProgram );
void programInfo( GLuint aProgram );
/**
* @brief Get the shader information.
*
* @param aShader is the shader number.
*/
void ShaderInfo( GLuint aShader );
void shaderInfo( GLuint aShader );
/**
* @brief Read the shader source file
......@@ -172,7 +182,16 @@ private:
* @param aShaderSourceName is the shader source file name.
* @return the source as string
*/
std::string ReadSource( std::string aShaderSourceName );
std::string readSource( std::string aShaderSourceName );
/**
* @brief Add a shader and compile the shader sources.
*
* @param aShaderSource is the shader source content.
* @param aShaderType is the type of the shader.
* @return True in case of success, false otherwise.
*/
bool addSource( const std::string& aShaderSource, ShaderType aShaderType );
std::deque<GLuint> shaderNumbers; ///< Shader number list
GLuint programNumber; ///< Shader program number
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment