From 0795f000998ca9aca19b27e1ef90f2f7cd5cea8f Mon Sep 17 00:00:00 2001 From: hybrid Date: Thu, 9 Jun 2011 13:12:48 +0000 Subject: [PATCH] Add bool return values on shader creation in order to signal problems and avoid illegal mem access on broken shaders. git-svn-id: http://svn.code.sf.net/p/irrlicht/code/branches/ogl-es@3834 dfc29bdd-3216-0410-991c-e03cc46cb475 --- source/Irrlicht/COGLES2NormalMapRenderer.cpp | 14 ++++++----- .../Irrlicht/COGLES2ParallaxMapRenderer.cpp | 14 ++++++----- source/Irrlicht/COGLES2Renderer2D.cpp | 3 ++- source/Irrlicht/COGLES2SLMaterialRenderer.cpp | 24 +++++++++++++------ source/Irrlicht/COGLES2SLMaterialRenderer.h | 6 ++--- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/source/Irrlicht/COGLES2NormalMapRenderer.cpp b/source/Irrlicht/COGLES2NormalMapRenderer.cpp index 48715081..9d393058 100644 --- a/source/Irrlicht/COGLES2NormalMapRenderer.cpp +++ b/source/Irrlicht/COGLES2NormalMapRenderer.cpp @@ -71,12 +71,14 @@ namespace video else { // compile shaders on our own - initFromFiles( outMaterialTypeNr, VertexShaderFile, FragmentShaderFile ); - useProgram(); - int dummy = 0; - setUniform( TEXTURE_UNIT0, &dummy ); - dummy = 1; - setUniform( TEXTURE_UNIT1, &dummy ); + if (initFromFiles(outMaterialTypeNr, VertexShaderFile, FragmentShaderFile)) + { + useProgram(); + int dummy = 0; + setUniform( TEXTURE_UNIT0, &dummy ); + dummy = 1; + setUniform( TEXTURE_UNIT1, &dummy ); + } } // fallback if compilation has failed diff --git a/source/Irrlicht/COGLES2ParallaxMapRenderer.cpp b/source/Irrlicht/COGLES2ParallaxMapRenderer.cpp index dbded0be..d52225fb 100644 --- a/source/Irrlicht/COGLES2ParallaxMapRenderer.cpp +++ b/source/Irrlicht/COGLES2ParallaxMapRenderer.cpp @@ -76,12 +76,14 @@ namespace video else { // compile shaders on our own - initFromFiles( outMaterialTypeNr, VertexShaderFile, FragmentShaderFile ); - useProgram(); - int dummy = 0; - setUniform( TEXTURE_UNIT0, &dummy ); - dummy = 1; - setUniform( TEXTURE_UNIT1, &dummy ); + if (initFromFiles( outMaterialTypeNr, VertexShaderFile, FragmentShaderFile)) + { + useProgram(); + int dummy = 0; + setUniform( TEXTURE_UNIT0, &dummy ); + dummy = 1; + setUniform( TEXTURE_UNIT1, &dummy ); + } } // fallback if compilation has failed diff --git a/source/Irrlicht/COGLES2Renderer2D.cpp b/source/Irrlicht/COGLES2Renderer2D.cpp index bcfdb635..d3d5693b 100644 --- a/source/Irrlicht/COGLES2Renderer2D.cpp +++ b/source/Irrlicht/COGLES2Renderer2D.cpp @@ -33,7 +33,8 @@ namespace video setDebugName( "COGLES2Renderer2d" ); #endif s32 dummy = -1; - initFromFiles( dummy, vertexShaderFile, fragmentShaderFile, false ); + if (!initFromFiles( dummy, vertexShaderFile, fragmentShaderFile, false)) + return; useProgram(); int texUnit = 0; setUniform( TEXTURE_UNIT, &texUnit ); diff --git a/source/Irrlicht/COGLES2SLMaterialRenderer.cpp b/source/Irrlicht/COGLES2SLMaterialRenderer.cpp index 409bd76f..bf676c81 100644 --- a/source/Irrlicht/COGLES2SLMaterialRenderer.cpp +++ b/source/Irrlicht/COGLES2SLMaterialRenderer.cpp @@ -89,7 +89,7 @@ namespace video BaseMaterial->drop(); } - void COGLES2SLMaterialRenderer::init(s32& outMaterialTypeNr, + bool COGLES2SLMaterialRenderer::init(s32& outMaterialTypeNr, const c8* vertexShaderProgram, const c8* pixelShaderProgram, bool registerMaterial ) @@ -97,32 +97,35 @@ namespace video outMaterialTypeNr = -1; if ( Program == 0 && !createProgram() ) - return; + return false; if ( vertexShaderProgram ) if ( !createShader( GL_VERTEX_SHADER, vertexShaderProgram, "" ) ) - return; + return false; if ( pixelShaderProgram ) if ( !createShader( GL_FRAGMENT_SHADER, pixelShaderProgram, "" ) ) - return; + return false; if ( !linkProgram() ) - return; + return false; // register myself as new material if ( registerMaterial ) outMaterialTypeNr = Driver->addMaterialRenderer( this ); + return true; } - void COGLES2SLMaterialRenderer::initFromFiles( s32 &outMaterialTypeNr, + bool COGLES2SLMaterialRenderer::initFromFiles( s32 &outMaterialTypeNr, const c8 *vertexShaderFile, const c8 *pixelShaderFile, bool registerMaterial ) { + outMaterialTypeNr = -1; if ( !createProgram() ) { os::Printer::log( "Could not create shader program.", ELL_ERROR ); + return false; } if ( !readVertexShader( vertexShaderFile ) ) @@ -141,13 +144,15 @@ namespace video if ( !linkProgram() ) { os::Printer::log( "Error linking fixed pipeline shader program.", ELL_ERROR ); + return false; } if ( registerMaterial ) outMaterialTypeNr = Driver->addMaterialRenderer( this ); + return true; } - void COGLES2SLMaterialRenderer::reloadFromFiles( const c8 *vertexShaderFile, + bool COGLES2SLMaterialRenderer::reloadFromFiles( const c8 *vertexShaderFile, const c8 *pixelShaderFile ) { GLsizei shaderCount; @@ -170,7 +175,10 @@ namespace video if ( !linkProgram() ) { os::Printer::log( "Error linking fixed pipeline shader program.", ELL_ERROR ); + return false; } + else + return true; } @@ -446,6 +454,8 @@ namespace video bool COGLES2SLMaterialRenderer::setUniform( int index, const void* data, int count ) { + if ((u32)index>=UniformInfo.size()) + return false; SUniformInfo& ui = UniformInfo[index]; if ( ui.location == -1 ) return false; diff --git a/source/Irrlicht/COGLES2SLMaterialRenderer.h b/source/Irrlicht/COGLES2SLMaterialRenderer.h index 84ec8063..bb79f6ba 100644 --- a/source/Irrlicht/COGLES2SLMaterialRenderer.h +++ b/source/Irrlicht/COGLES2SLMaterialRenderer.h @@ -97,17 +97,17 @@ namespace video const u32& uniformCount, s32 userData = 0); - void init(s32& outMaterialTypeNr, + bool init(s32& outMaterialTypeNr, const c8* vertexShaderProgram, const c8* pixelShaderProgram, bool registerMaterial = true); - void initFromFiles(s32& outMaterialTypeNr, + bool initFromFiles(s32& outMaterialTypeNr, const c8* vertexShaderFile, const c8* pixelShaderFile, bool registerMaterial = true); - void reloadFromFiles(const c8* vertexShaderFile, + bool reloadFromFiles(const c8* vertexShaderFile, const c8* pixelShaderFile); bool readVertexShader(const c8* vertexShaderFile);