Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Add API to delete shader materials
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Feb 20, 2024
1 parent d77ccf7 commit 884a241
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions include/EMaterialTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef __E_MATERIAL_TYPES_H_INCLUDED__
#define __E_MATERIAL_TYPES_H_INCLUDED__

#include "irrTypes.h"

namespace irr
{
namespace video
Expand Down Expand Up @@ -67,6 +69,9 @@ namespace video
0
};

constexpr u32 numBuiltInMaterials =
sizeof(sBuiltInMaterialTypeNames) / sizeof(char*) - 1;

} // end namespace video
} // end namespace irr

Expand Down
9 changes: 9 additions & 0 deletions include/IGPUProgrammingServices.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ class IGPUProgrammingServices
scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0,
callback, baseMaterial, userData);
}

//! Delete a shader material and associated data.
/**
After you have deleted a material it is invalid to still use and doing
so might result in a crash. The ID may be reused in the future when new
materials are added.
\param material Number of the material type. Must not be a built-in
material. */
virtual void deleteShaderMaterial(s32 material) = 0;
};


Expand Down
34 changes: 31 additions & 3 deletions source/Irrlicht/CNullDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ IImageWriter* createImageWriterJPG();
//! creates a writer which is able to save png images
IImageWriter* createImageWriterPNG();

namespace {
//! no-op material renderer
class CDummyMaterialRenderer : public IMaterialRenderer {
public:
CDummyMaterialRenderer() {}
};
}


//! constructor
CNullDriver::CNullDriver(io::IFileSystem* io, const core::dimension2d<u32>& screenSize)
Expand Down Expand Up @@ -1525,7 +1533,7 @@ s32 CNullDriver::addMaterialRenderer(IMaterialRenderer* renderer, const char* na
r.Renderer = renderer;
r.Name = name;

if (name == 0 && (MaterialRenderers.size() < (sizeof(sBuiltInMaterialTypeNames) / sizeof(char*))-1 ))
if (name == 0 && MaterialRenderers.size() < numBuiltInMaterials)
{
// set name of built in renderer so that we don't have to implement name
// setting in all available renderers.
Expand All @@ -1542,8 +1550,7 @@ s32 CNullDriver::addMaterialRenderer(IMaterialRenderer* renderer, const char* na
//! Sets the name of a material renderer.
void CNullDriver::setMaterialRendererName(u32 idx, const char* name)
{
if (idx < (sizeof(sBuiltInMaterialTypeNames) / sizeof(char*))-1 ||
idx >= MaterialRenderers.size())
if (idx < numBuiltInMaterials || idx >= MaterialRenderers.size())
return;

MaterialRenderers[idx].Name = name;
Expand Down Expand Up @@ -1786,6 +1793,27 @@ s32 CNullDriver::addHighLevelShaderMaterialFromFiles(
return result;
}

void CNullDriver::deleteShaderMaterial(s32 material)
{
const u32 idx = (u32)material;
if (idx < numBuiltInMaterials || idx >= MaterialRenderers.size())
return;

// if this is the last material we can drop it without consequence
if (idx == MaterialRenderers.size() - 1) {
if (MaterialRenderers[idx].Renderer)
MaterialRenderers[idx].Renderer->drop();
MaterialRenderers.erase(idx);
return;
}
// otherwise replace with a dummy renderer, we have to preserve the IDs
auto &ref = MaterialRenderers[idx];
if (ref.Renderer)
ref.Renderer->drop();
ref.Renderer = new CDummyMaterialRenderer();
ref.Name.clear();
}


//! Creates a render target texture.
ITexture* CNullDriver::addRenderTargetTexture(const core::dimension2d<u32>& size,
Expand Down
2 changes: 2 additions & 0 deletions source/Irrlicht/CNullDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ namespace video
E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID,
s32 userData = 0) override;

virtual void deleteShaderMaterial(s32 material) override;

//! Returns a pointer to the mesh manipulator.
scene::IMeshManipulator* getMeshManipulator() override;

Expand Down

0 comments on commit 884a241

Please sign in to comment.