Clear Shader memory

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
vivekSivamRP
Posts: 66
Joined: Sat Sep 29, 2012 11:58 am

Clear Shader memory

Post by vivekSivamRP »

Irrlicht clears the shader memory only if the irrlicht device is deleted (Irrlicht device is created using CreateDevice(..) method ). This caused a huge memory leak in my project, in which multiple number of unique shaders are needed to be generated and removed without deleting the irrlicht device frequently. For removing the memory of the shader materials which is not necessary at some point of time i’ve written removeShaderMaterials() method.

Add the lines which starts with + symbol

Changes in Irrlicht Classes:

Class: IVideoDriver.h
Line: Anywhere under public category
Change:
class IVideoDriver : public virtual IReferenceCounted
{
public:
+ virtual void removeShaderProgram(int materialIndex)= 0;


Class: CNullDriver.h
Line: Anywhere under public category
Change:
class CNullDriver : public IVideoDriver, public IGPUProgrammingServices
{
public:
+virtual void removeShaderMaterials();

Class: CNullDriver.cpp
Line: Anywhere under public category
Change:
+void CNullDriver::emoveShaderMaterials() {
+}

Class: COGLES2Driver.h
Line: Anywhere under public category
Change:
+virtual void removeShaderMaterials();
+core array<GLuint> *shaderMaterials;


Class: COGLES2Driver.cpp
Change1:
core::dimension2d<u32> WindowSize(backingWidth, backingHeight);
CNullDriver::ScreenSize = WindowSize;
CNullDriver::ViewPort = core::rect<s32>(core::position2d<s32>(0,0), core::dimension2di(WindowSize));

genericDriverInit(WindowSize, params.Stencilbuffer);
+ shaderPrograms = new core::array<GLuint>();
materialRenderers = new irr::core::array<irr::video::COGLES2MaterialRenderer*>();
#endif
}
Change2:
+void COGLES2Driver::removeShaderMaterials(){

+ for(int i = 0;i < shaderMaterials->size();i++){
+ GLuint Program = (* shaderMaterials);
+ if (Program)
+ {
+ GLuint shaders[8];
+ GLint count;
+ glGetAttachedShaders(Program, 8, &count, shaders);
+
+ count=core::min_(count,8);
+ for (GLint i=0; i<count; ++i)
+ glDeleteShader(shaders);
+ glDeleteProgram(Program);
+ Program = 0;
+ }
+ (*materialRenderers)->UniformInfo.clear();
+ }
+ }
+}

Change3:
s32 COGLES2Driver::addHighLevelShaderMaterial(
const c8* vertexShaderProgram,
const c8* vertexShaderEntryPointName,
E_VERTEX_SHADER_TYPE vsCompileTarget,
const c8* pixelShaderProgram,
const c8* pixelShaderEntryPointName,
E_PIXEL_SHADER_TYPE psCompileTarget,
const c8* geometryShaderProgram,
const c8* geometryShaderEntryPointName,
E_GEOMETRY_SHADER_TYPE gsCompileTarget,
scene::E_PRIMITIVE_TYPE inType,
scene::E_PRIMITIVE_TYPE outType,
u32 verticesOut,
IShaderConstantSetCallBack* callback,
E_MATERIAL_TYPE baseMaterial,
s32 userData, E_GPU_SHADING_LANGUAGE shadingLang)
{
s32 nr = -1;
COGLES2MaterialRenderer* r = new COGLES2MaterialRenderer(this, nr, vertexShaderProgram,pixelShaderProgram,callback, baseMaterial, userData);
+ shaderPrograms->push_back(r->getProgram());
materialRenderers->push_back(r);
r->drop();
return nr;
}
Change4:
//! destructor
COGLES2Driver::~COGLES2Driver()
{
+ delete shaderPrograms;
delete materialRenderers;

Change5:
Class: COGLES2MaterialRenderer.cpp

GLuint COGLES2MaterialRenderer::getProgram() const
{
return Program;
}
Last edited by vivekSivamRP on Fri Nov 28, 2014 9:48 am, edited 4 times in total.
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Re: Remove Shader memory

Post by devsh »

while rewriting for OGL 3.2 core compliance in shaders, I noticed how shaders are bound to material IDs.... you could make a "bool removeShader(video::E_MATERIAL_TYPE)" method to remove shader by its ID
vivekSivamRP
Posts: 66
Joined: Sat Sep 29, 2012 11:58 am

Re: Remove Shader memory

Post by vivekSivamRP »

yes you are right, we can edit these methods depending on our convenience.
Post Reply